* dwarf2read.c (dwarf2_attr): Avoid tail-recursive call.
[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 static int pagesize;
89
90 /* When set, the file that we're processing is known to have debugging
91 info for C++ namespaces. GCC 3.3.x did not produce this information,
92 but later versions do. */
93
94 static int processing_has_namespace_info;
95
96 static const struct objfile_data *dwarf2_objfile_data_key;
97
98 struct dwarf2_section_info
99 {
100 asection *asection;
101 gdb_byte *buffer;
102 bfd_size_type size;
103 /* Not NULL if the section was actually mmapped. */
104 void *map_addr;
105 /* Page aligned size of mmapped area. */
106 bfd_size_type map_len;
107 /* True if we have tried to read this section. */
108 int readin;
109 };
110
111 typedef struct dwarf2_section_info dwarf2_section_info_def;
112 DEF_VEC_O (dwarf2_section_info_def);
113
114 /* All offsets in the index are of this type. It must be
115 architecture-independent. */
116 typedef uint32_t offset_type;
117
118 DEF_VEC_I (offset_type);
119
120 /* A description of the mapped index. The file format is described in
121 a comment by the code that writes the index. */
122 struct mapped_index
123 {
124 /* Index data format version. */
125 int version;
126
127 /* The total length of the buffer. */
128 off_t total_size;
129
130 /* A pointer to the address table data. */
131 const gdb_byte *address_table;
132
133 /* Size of the address table data in bytes. */
134 offset_type address_table_size;
135
136 /* The symbol table, implemented as a hash table. */
137 const offset_type *symbol_table;
138
139 /* Size in slots, each slot is 2 offset_types. */
140 offset_type symbol_table_slots;
141
142 /* A pointer to the constant pool. */
143 const char *constant_pool;
144 };
145
146 /* Collection of data recorded per objfile.
147 This hangs off of dwarf2_objfile_data_key. */
148
149 struct dwarf2_per_objfile
150 {
151 struct dwarf2_section_info info;
152 struct dwarf2_section_info abbrev;
153 struct dwarf2_section_info line;
154 struct dwarf2_section_info loc;
155 struct dwarf2_section_info macinfo;
156 struct dwarf2_section_info macro;
157 struct dwarf2_section_info str;
158 struct dwarf2_section_info ranges;
159 struct dwarf2_section_info frame;
160 struct dwarf2_section_info eh_frame;
161 struct dwarf2_section_info gdb_index;
162
163 VEC (dwarf2_section_info_def) *types;
164
165 /* Back link. */
166 struct objfile *objfile;
167
168 /* Table of all the compilation units. This is used to locate
169 the target compilation unit of a particular reference. */
170 struct dwarf2_per_cu_data **all_comp_units;
171
172 /* The number of compilation units in ALL_COMP_UNITS. */
173 int n_comp_units;
174
175 /* The number of .debug_types-related CUs. */
176 int n_type_units;
177
178 /* The .debug_types-related CUs (TUs). */
179 struct dwarf2_per_cu_data **all_type_units;
180
181 /* A chain of compilation units that are currently read in, so that
182 they can be freed later. */
183 struct dwarf2_per_cu_data *read_in_chain;
184
185 /* A table mapping .debug_types signatures to its signatured_type entry.
186 This is NULL if the .debug_types section hasn't been read in yet. */
187 htab_t signatured_types;
188
189 /* A flag indicating wether this objfile has a section loaded at a
190 VMA of 0. */
191 int has_section_at_zero;
192
193 /* True if we are using the mapped index,
194 or we are faking it for OBJF_READNOW's sake. */
195 unsigned char using_index;
196
197 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
198 struct mapped_index *index_table;
199
200 /* When using index_table, this keeps track of all quick_file_names entries.
201 TUs can share line table entries with CUs or other TUs, and there can be
202 a lot more TUs than unique line tables, so we maintain a separate table
203 of all line table entries to support the sharing. */
204 htab_t quick_file_names_table;
205
206 /* Set during partial symbol reading, to prevent queueing of full
207 symbols. */
208 int reading_partial_symbols;
209
210 /* Table mapping type .debug_info DIE offsets to types.
211 This is NULL if not allocated yet.
212 It (currently) makes sense to allocate debug_types_type_hash lazily.
213 To keep things simple we allocate both lazily. */
214 htab_t debug_info_type_hash;
215
216 /* Table mapping type .debug_types DIE offsets to types.
217 This is NULL if not allocated yet. */
218 htab_t debug_types_type_hash;
219 };
220
221 static struct dwarf2_per_objfile *dwarf2_per_objfile;
222
223 /* Default names of the debugging sections. */
224
225 /* Note that if the debugging section has been compressed, it might
226 have a name like .zdebug_info. */
227
228 static const struct dwarf2_debug_sections dwarf2_elf_names =
229 {
230 { ".debug_info", ".zdebug_info" },
231 { ".debug_abbrev", ".zdebug_abbrev" },
232 { ".debug_line", ".zdebug_line" },
233 { ".debug_loc", ".zdebug_loc" },
234 { ".debug_macinfo", ".zdebug_macinfo" },
235 { ".debug_macro", ".zdebug_macro" },
236 { ".debug_str", ".zdebug_str" },
237 { ".debug_ranges", ".zdebug_ranges" },
238 { ".debug_types", ".zdebug_types" },
239 { ".debug_frame", ".zdebug_frame" },
240 { ".eh_frame", NULL },
241 { ".gdb_index", ".zgdb_index" },
242 23
243 };
244
245 /* local data types */
246
247 /* We hold several abbreviation tables in memory at the same time. */
248 #ifndef ABBREV_HASH_SIZE
249 #define ABBREV_HASH_SIZE 121
250 #endif
251
252 /* The data in a compilation unit header, after target2host
253 translation, looks like this. */
254 struct comp_unit_head
255 {
256 unsigned int length;
257 short version;
258 unsigned char addr_size;
259 unsigned char signed_addr_p;
260 unsigned int abbrev_offset;
261
262 /* Size of file offsets; either 4 or 8. */
263 unsigned int offset_size;
264
265 /* Size of the length field; either 4 or 12. */
266 unsigned int initial_length_size;
267
268 /* Offset to the first byte of this compilation unit header in the
269 .debug_info section, for resolving relative reference dies. */
270 unsigned int offset;
271
272 /* Offset to first die in this cu from the start of the cu.
273 This will be the first byte following the compilation unit header. */
274 unsigned int first_die_offset;
275 };
276
277 /* Type used for delaying computation of method physnames.
278 See comments for compute_delayed_physnames. */
279 struct delayed_method_info
280 {
281 /* The type to which the method is attached, i.e., its parent class. */
282 struct type *type;
283
284 /* The index of the method in the type's function fieldlists. */
285 int fnfield_index;
286
287 /* The index of the method in the fieldlist. */
288 int index;
289
290 /* The name of the DIE. */
291 const char *name;
292
293 /* The DIE associated with this method. */
294 struct die_info *die;
295 };
296
297 typedef struct delayed_method_info delayed_method_info;
298 DEF_VEC_O (delayed_method_info);
299
300 /* Internal state when decoding a particular compilation unit. */
301 struct dwarf2_cu
302 {
303 /* The objfile containing this compilation unit. */
304 struct objfile *objfile;
305
306 /* The header of the compilation unit. */
307 struct comp_unit_head header;
308
309 /* Base address of this compilation unit. */
310 CORE_ADDR base_address;
311
312 /* Non-zero if base_address has been set. */
313 int base_known;
314
315 /* The language we are debugging. */
316 enum language language;
317 const struct language_defn *language_defn;
318
319 const char *producer;
320
321 /* The generic symbol table building routines have separate lists for
322 file scope symbols and all all other scopes (local scopes). So
323 we need to select the right one to pass to add_symbol_to_list().
324 We do it by keeping a pointer to the correct list in list_in_scope.
325
326 FIXME: The original dwarf code just treated the file scope as the
327 first local scope, and all other local scopes as nested local
328 scopes, and worked fine. Check to see if we really need to
329 distinguish these in buildsym.c. */
330 struct pending **list_in_scope;
331
332 /* DWARF abbreviation table associated with this compilation unit. */
333 struct abbrev_info **dwarf2_abbrevs;
334
335 /* Storage for the abbrev table. */
336 struct obstack abbrev_obstack;
337
338 /* Hash table holding all the loaded partial DIEs. */
339 htab_t partial_dies;
340
341 /* Storage for things with the same lifetime as this read-in compilation
342 unit, including partial DIEs. */
343 struct obstack comp_unit_obstack;
344
345 /* When multiple dwarf2_cu structures are living in memory, this field
346 chains them all together, so that they can be released efficiently.
347 We will probably also want a generation counter so that most-recently-used
348 compilation units are cached... */
349 struct dwarf2_per_cu_data *read_in_chain;
350
351 /* Backchain to our per_cu entry if the tree has been built. */
352 struct dwarf2_per_cu_data *per_cu;
353
354 /* How many compilation units ago was this CU last referenced? */
355 int last_used;
356
357 /* A hash table of die offsets for following references. */
358 htab_t die_hash;
359
360 /* Full DIEs if read in. */
361 struct die_info *dies;
362
363 /* A set of pointers to dwarf2_per_cu_data objects for compilation
364 units referenced by this one. Only set during full symbol processing;
365 partial symbol tables do not have dependencies. */
366 htab_t dependencies;
367
368 /* Header data from the line table, during full symbol processing. */
369 struct line_header *line_header;
370
371 /* A list of methods which need to have physnames computed
372 after all type information has been read. */
373 VEC (delayed_method_info) *method_list;
374
375 /* To be copied to symtab->call_site_htab. */
376 htab_t call_site_htab;
377
378 /* Mark used when releasing cached dies. */
379 unsigned int mark : 1;
380
381 /* This CU references .debug_loc. See the symtab->locations_valid field.
382 This test is imperfect as there may exist optimized debug code not using
383 any location list and still facing inlining issues if handled as
384 unoptimized code. For a future better test see GCC PR other/32998. */
385 unsigned int has_loclist : 1;
386 };
387
388 /* Persistent data held for a compilation unit, even when not
389 processing it. We put a pointer to this structure in the
390 read_symtab_private field of the psymtab. */
391
392 struct dwarf2_per_cu_data
393 {
394 /* The start offset and length of this compilation unit. 2**29-1
395 bytes should suffice to store the length of any compilation unit
396 - if it doesn't, GDB will fall over anyway.
397 NOTE: Unlike comp_unit_head.length, this length includes
398 initial_length_size. */
399 unsigned int offset;
400 unsigned int length : 29;
401
402 /* Flag indicating this compilation unit will be read in before
403 any of the current compilation units are processed. */
404 unsigned int queued : 1;
405
406 /* This flag will be set if we need to load absolutely all DIEs
407 for this compilation unit, instead of just the ones we think
408 are interesting. It gets set if we look for a DIE in the
409 hash table and don't find it. */
410 unsigned int load_all_dies : 1;
411
412 /* Non-null if this CU is from .debug_types; in which case it points
413 to the section. Otherwise it's from .debug_info. */
414 struct dwarf2_section_info *debug_types_section;
415
416 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
417 of the CU cache it gets reset to NULL again. */
418 struct dwarf2_cu *cu;
419
420 /* The corresponding objfile.
421 Normally we can get the objfile from dwarf2_per_objfile.
422 However we can enter this file with just a "per_cu" handle. */
423 struct objfile *objfile;
424
425 /* When using partial symbol tables, the 'psymtab' field is active.
426 Otherwise the 'quick' field is active. */
427 union
428 {
429 /* The partial symbol table associated with this compilation unit,
430 or NULL for partial units (which do not have an associated
431 symtab). */
432 struct partial_symtab *psymtab;
433
434 /* Data needed by the "quick" functions. */
435 struct dwarf2_per_cu_quick_data *quick;
436 } v;
437 };
438
439 /* Entry in the signatured_types hash table. */
440
441 struct signatured_type
442 {
443 ULONGEST signature;
444
445 /* Offset in .debug_types of the type defined by this TU. */
446 unsigned int type_offset;
447
448 /* The CU(/TU) of this type. */
449 struct dwarf2_per_cu_data per_cu;
450 };
451
452 /* Struct used to pass misc. parameters to read_die_and_children, et
453 al. which are used for both .debug_info and .debug_types dies.
454 All parameters here are unchanging for the life of the call. This
455 struct exists to abstract away the constant parameters of die
456 reading. */
457
458 struct die_reader_specs
459 {
460 /* The bfd of this objfile. */
461 bfd* abfd;
462
463 /* The CU of the DIE we are parsing. */
464 struct dwarf2_cu *cu;
465
466 /* Pointer to start of section buffer.
467 This is either the start of .debug_info or .debug_types. */
468 const gdb_byte *buffer;
469 };
470
471 /* The line number information for a compilation unit (found in the
472 .debug_line section) begins with a "statement program header",
473 which contains the following information. */
474 struct line_header
475 {
476 unsigned int total_length;
477 unsigned short version;
478 unsigned int header_length;
479 unsigned char minimum_instruction_length;
480 unsigned char maximum_ops_per_instruction;
481 unsigned char default_is_stmt;
482 int line_base;
483 unsigned char line_range;
484 unsigned char opcode_base;
485
486 /* standard_opcode_lengths[i] is the number of operands for the
487 standard opcode whose value is i. This means that
488 standard_opcode_lengths[0] is unused, and the last meaningful
489 element is standard_opcode_lengths[opcode_base - 1]. */
490 unsigned char *standard_opcode_lengths;
491
492 /* The include_directories table. NOTE! These strings are not
493 allocated with xmalloc; instead, they are pointers into
494 debug_line_buffer. If you try to free them, `free' will get
495 indigestion. */
496 unsigned int num_include_dirs, include_dirs_size;
497 char **include_dirs;
498
499 /* The file_names table. NOTE! These strings are not allocated
500 with xmalloc; instead, they are pointers into debug_line_buffer.
501 Don't try to free them directly. */
502 unsigned int num_file_names, file_names_size;
503 struct file_entry
504 {
505 char *name;
506 unsigned int dir_index;
507 unsigned int mod_time;
508 unsigned int length;
509 int included_p; /* Non-zero if referenced by the Line Number Program. */
510 struct symtab *symtab; /* The associated symbol table, if any. */
511 } *file_names;
512
513 /* The start and end of the statement program following this
514 header. These point into dwarf2_per_objfile->line_buffer. */
515 gdb_byte *statement_program_start, *statement_program_end;
516 };
517
518 /* When we construct a partial symbol table entry we only
519 need this much information. */
520 struct partial_die_info
521 {
522 /* Offset of this DIE. */
523 unsigned int offset;
524
525 /* DWARF-2 tag for this DIE. */
526 ENUM_BITFIELD(dwarf_tag) tag : 16;
527
528 /* Assorted flags describing the data found in this DIE. */
529 unsigned int has_children : 1;
530 unsigned int is_external : 1;
531 unsigned int is_declaration : 1;
532 unsigned int has_type : 1;
533 unsigned int has_specification : 1;
534 unsigned int has_pc_info : 1;
535
536 /* Flag set if the SCOPE field of this structure has been
537 computed. */
538 unsigned int scope_set : 1;
539
540 /* Flag set if the DIE has a byte_size attribute. */
541 unsigned int has_byte_size : 1;
542
543 /* Flag set if any of the DIE's children are template arguments. */
544 unsigned int has_template_arguments : 1;
545
546 /* Flag set if fixup_partial_die has been called on this die. */
547 unsigned int fixup_called : 1;
548
549 /* The name of this DIE. Normally the value of DW_AT_name, but
550 sometimes a default name for unnamed DIEs. */
551 char *name;
552
553 /* The linkage name, if present. */
554 const char *linkage_name;
555
556 /* The scope to prepend to our children. This is generally
557 allocated on the comp_unit_obstack, so will disappear
558 when this compilation unit leaves the cache. */
559 char *scope;
560
561 /* The location description associated with this DIE, if any. */
562 struct dwarf_block *locdesc;
563
564 /* If HAS_PC_INFO, the PC range associated with this DIE. */
565 CORE_ADDR lowpc;
566 CORE_ADDR highpc;
567
568 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
569 DW_AT_sibling, if any. */
570 /* NOTE: This member isn't strictly necessary, read_partial_die could
571 return DW_AT_sibling values to its caller load_partial_dies. */
572 gdb_byte *sibling;
573
574 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
575 DW_AT_specification (or DW_AT_abstract_origin or
576 DW_AT_extension). */
577 unsigned int spec_offset;
578
579 /* Pointers to this DIE's parent, first child, and next sibling,
580 if any. */
581 struct partial_die_info *die_parent, *die_child, *die_sibling;
582 };
583
584 /* This data structure holds the information of an abbrev. */
585 struct abbrev_info
586 {
587 unsigned int number; /* number identifying abbrev */
588 enum dwarf_tag tag; /* dwarf tag */
589 unsigned short has_children; /* boolean */
590 unsigned short num_attrs; /* number of attributes */
591 struct attr_abbrev *attrs; /* an array of attribute descriptions */
592 struct abbrev_info *next; /* next in chain */
593 };
594
595 struct attr_abbrev
596 {
597 ENUM_BITFIELD(dwarf_attribute) name : 16;
598 ENUM_BITFIELD(dwarf_form) form : 16;
599 };
600
601 /* Attributes have a name and a value. */
602 struct attribute
603 {
604 ENUM_BITFIELD(dwarf_attribute) name : 16;
605 ENUM_BITFIELD(dwarf_form) form : 15;
606
607 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
608 field should be in u.str (existing only for DW_STRING) but it is kept
609 here for better struct attribute alignment. */
610 unsigned int string_is_canonical : 1;
611
612 union
613 {
614 char *str;
615 struct dwarf_block *blk;
616 ULONGEST unsnd;
617 LONGEST snd;
618 CORE_ADDR addr;
619 struct signatured_type *signatured_type;
620 }
621 u;
622 };
623
624 /* This data structure holds a complete die structure. */
625 struct die_info
626 {
627 /* DWARF-2 tag for this DIE. */
628 ENUM_BITFIELD(dwarf_tag) tag : 16;
629
630 /* Number of attributes */
631 unsigned char num_attrs;
632
633 /* True if we're presently building the full type name for the
634 type derived from this DIE. */
635 unsigned char building_fullname : 1;
636
637 /* Abbrev number */
638 unsigned int abbrev;
639
640 /* Offset in .debug_info or .debug_types section. */
641 unsigned int offset;
642
643 /* The dies in a compilation unit form an n-ary tree. PARENT
644 points to this die's parent; CHILD points to the first child of
645 this node; and all the children of a given node are chained
646 together via their SIBLING fields. */
647 struct die_info *child; /* Its first child, if any. */
648 struct die_info *sibling; /* Its next sibling, if any. */
649 struct die_info *parent; /* Its parent, if any. */
650
651 /* An array of attributes, with NUM_ATTRS elements. There may be
652 zero, but it's not common and zero-sized arrays are not
653 sufficiently portable C. */
654 struct attribute attrs[1];
655 };
656
657 /* Get at parts of an attribute structure. */
658
659 #define DW_STRING(attr) ((attr)->u.str)
660 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
661 #define DW_UNSND(attr) ((attr)->u.unsnd)
662 #define DW_BLOCK(attr) ((attr)->u.blk)
663 #define DW_SND(attr) ((attr)->u.snd)
664 #define DW_ADDR(attr) ((attr)->u.addr)
665 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
666
667 /* Blocks are a bunch of untyped bytes. */
668 struct dwarf_block
669 {
670 unsigned int size;
671
672 /* Valid only if SIZE is not zero. */
673 gdb_byte *data;
674 };
675
676 #ifndef ATTR_ALLOC_CHUNK
677 #define ATTR_ALLOC_CHUNK 4
678 #endif
679
680 /* Allocate fields for structs, unions and enums in this size. */
681 #ifndef DW_FIELD_ALLOC_CHUNK
682 #define DW_FIELD_ALLOC_CHUNK 4
683 #endif
684
685 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
686 but this would require a corresponding change in unpack_field_as_long
687 and friends. */
688 static int bits_per_byte = 8;
689
690 /* The routines that read and process dies for a C struct or C++ class
691 pass lists of data member fields and lists of member function fields
692 in an instance of a field_info structure, as defined below. */
693 struct field_info
694 {
695 /* List of data member and baseclasses fields. */
696 struct nextfield
697 {
698 struct nextfield *next;
699 int accessibility;
700 int virtuality;
701 struct field field;
702 }
703 *fields, *baseclasses;
704
705 /* Number of fields (including baseclasses). */
706 int nfields;
707
708 /* Number of baseclasses. */
709 int nbaseclasses;
710
711 /* Set if the accesibility of one of the fields is not public. */
712 int non_public_fields;
713
714 /* Member function fields array, entries are allocated in the order they
715 are encountered in the object file. */
716 struct nextfnfield
717 {
718 struct nextfnfield *next;
719 struct fn_field fnfield;
720 }
721 *fnfields;
722
723 /* Member function fieldlist array, contains name of possibly overloaded
724 member function, number of overloaded member functions and a pointer
725 to the head of the member function field chain. */
726 struct fnfieldlist
727 {
728 char *name;
729 int length;
730 struct nextfnfield *head;
731 }
732 *fnfieldlists;
733
734 /* Number of entries in the fnfieldlists array. */
735 int nfnfields;
736
737 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
738 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
739 struct typedef_field_list
740 {
741 struct typedef_field field;
742 struct typedef_field_list *next;
743 }
744 *typedef_field_list;
745 unsigned typedef_field_list_count;
746 };
747
748 /* One item on the queue of compilation units to read in full symbols
749 for. */
750 struct dwarf2_queue_item
751 {
752 struct dwarf2_per_cu_data *per_cu;
753 struct dwarf2_queue_item *next;
754 };
755
756 /* The current queue. */
757 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
758
759 /* Loaded secondary compilation units are kept in memory until they
760 have not been referenced for the processing of this many
761 compilation units. Set this to zero to disable caching. Cache
762 sizes of up to at least twenty will improve startup time for
763 typical inter-CU-reference binaries, at an obvious memory cost. */
764 static int dwarf2_max_cache_age = 5;
765 static void
766 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
767 struct cmd_list_element *c, const char *value)
768 {
769 fprintf_filtered (file, _("The upper bound on the age of cached "
770 "dwarf2 compilation units is %s.\n"),
771 value);
772 }
773
774
775 /* Various complaints about symbol reading that don't abort the process. */
776
777 static void
778 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
779 {
780 complaint (&symfile_complaints,
781 _("statement list doesn't fit in .debug_line section"));
782 }
783
784 static void
785 dwarf2_debug_line_missing_file_complaint (void)
786 {
787 complaint (&symfile_complaints,
788 _(".debug_line section has line data without a file"));
789 }
790
791 static void
792 dwarf2_debug_line_missing_end_sequence_complaint (void)
793 {
794 complaint (&symfile_complaints,
795 _(".debug_line section has line "
796 "program sequence without an end"));
797 }
798
799 static void
800 dwarf2_complex_location_expr_complaint (void)
801 {
802 complaint (&symfile_complaints, _("location expression too complex"));
803 }
804
805 static void
806 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
807 int arg3)
808 {
809 complaint (&symfile_complaints,
810 _("const value length mismatch for '%s', got %d, expected %d"),
811 arg1, arg2, arg3);
812 }
813
814 static void
815 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
816 {
817 complaint (&symfile_complaints,
818 _("macro info runs off end of `%s' section"),
819 section->asection->name);
820 }
821
822 static void
823 dwarf2_macro_malformed_definition_complaint (const char *arg1)
824 {
825 complaint (&symfile_complaints,
826 _("macro debug info contains a "
827 "malformed macro definition:\n`%s'"),
828 arg1);
829 }
830
831 static void
832 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
833 {
834 complaint (&symfile_complaints,
835 _("invalid attribute class or form for '%s' in '%s'"),
836 arg1, arg2);
837 }
838
839 /* local function prototypes */
840
841 static void dwarf2_locate_sections (bfd *, asection *, void *);
842
843 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
844 struct objfile *);
845
846 static void dwarf2_find_base_address (struct die_info *die,
847 struct dwarf2_cu *cu);
848
849 static void dwarf2_build_psymtabs_hard (struct objfile *);
850
851 static void scan_partial_symbols (struct partial_die_info *,
852 CORE_ADDR *, CORE_ADDR *,
853 int, struct dwarf2_cu *);
854
855 static void add_partial_symbol (struct partial_die_info *,
856 struct dwarf2_cu *);
857
858 static void add_partial_namespace (struct partial_die_info *pdi,
859 CORE_ADDR *lowpc, CORE_ADDR *highpc,
860 int need_pc, struct dwarf2_cu *cu);
861
862 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
863 CORE_ADDR *highpc, int need_pc,
864 struct dwarf2_cu *cu);
865
866 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
867 struct dwarf2_cu *cu);
868
869 static void add_partial_subprogram (struct partial_die_info *pdi,
870 CORE_ADDR *lowpc, CORE_ADDR *highpc,
871 int need_pc, struct dwarf2_cu *cu);
872
873 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
874 gdb_byte *buffer, gdb_byte *info_ptr,
875 bfd *abfd, struct dwarf2_cu *cu);
876
877 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
878
879 static void psymtab_to_symtab_1 (struct partial_symtab *);
880
881 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
882
883 static void dwarf2_free_abbrev_table (void *);
884
885 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
886
887 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
888 struct dwarf2_cu *);
889
890 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
891 struct dwarf2_cu *);
892
893 static struct partial_die_info *load_partial_dies (bfd *,
894 gdb_byte *, gdb_byte *,
895 int, struct dwarf2_cu *);
896
897 static gdb_byte *read_partial_die (struct partial_die_info *,
898 struct abbrev_info *abbrev,
899 unsigned int, bfd *,
900 gdb_byte *, gdb_byte *,
901 struct dwarf2_cu *);
902
903 static struct partial_die_info *find_partial_die (unsigned int,
904 struct dwarf2_cu *);
905
906 static void fixup_partial_die (struct partial_die_info *,
907 struct dwarf2_cu *);
908
909 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
910 bfd *, gdb_byte *, struct dwarf2_cu *);
911
912 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
913 bfd *, gdb_byte *, struct dwarf2_cu *);
914
915 static unsigned int read_1_byte (bfd *, gdb_byte *);
916
917 static int read_1_signed_byte (bfd *, gdb_byte *);
918
919 static unsigned int read_2_bytes (bfd *, gdb_byte *);
920
921 static unsigned int read_4_bytes (bfd *, gdb_byte *);
922
923 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
924
925 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
926 unsigned int *);
927
928 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
929
930 static LONGEST read_checked_initial_length_and_offset
931 (bfd *, gdb_byte *, const struct comp_unit_head *,
932 unsigned int *, unsigned int *);
933
934 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
935 unsigned int *);
936
937 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
938
939 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
940
941 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
942
943 static char *read_indirect_string (bfd *, gdb_byte *,
944 const struct comp_unit_head *,
945 unsigned int *);
946
947 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
948
949 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
950
951 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
952
953 static void set_cu_language (unsigned int, struct dwarf2_cu *);
954
955 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
956 struct dwarf2_cu *);
957
958 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
959 unsigned int,
960 struct dwarf2_cu *);
961
962 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
963 struct dwarf2_cu *cu);
964
965 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
966
967 static struct die_info *die_specification (struct die_info *die,
968 struct dwarf2_cu **);
969
970 static void free_line_header (struct line_header *lh);
971
972 static void add_file_name (struct line_header *, char *, unsigned int,
973 unsigned int, unsigned int);
974
975 static struct line_header *(dwarf_decode_line_header
976 (unsigned int offset,
977 bfd *abfd, struct dwarf2_cu *cu));
978
979 static void dwarf_decode_lines (struct line_header *, const char *,
980 struct dwarf2_cu *, struct partial_symtab *,
981 int);
982
983 static void dwarf2_start_subfile (char *, const char *, const char *);
984
985 static struct symbol *new_symbol (struct die_info *, struct type *,
986 struct dwarf2_cu *);
987
988 static struct symbol *new_symbol_full (struct die_info *, struct type *,
989 struct dwarf2_cu *, struct symbol *);
990
991 static void dwarf2_const_value (struct attribute *, struct symbol *,
992 struct dwarf2_cu *);
993
994 static void dwarf2_const_value_attr (struct attribute *attr,
995 struct type *type,
996 const char *name,
997 struct obstack *obstack,
998 struct dwarf2_cu *cu, long *value,
999 gdb_byte **bytes,
1000 struct dwarf2_locexpr_baton **baton);
1001
1002 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1003
1004 static int need_gnat_info (struct dwarf2_cu *);
1005
1006 static struct type *die_descriptive_type (struct die_info *,
1007 struct dwarf2_cu *);
1008
1009 static void set_descriptive_type (struct type *, struct die_info *,
1010 struct dwarf2_cu *);
1011
1012 static struct type *die_containing_type (struct die_info *,
1013 struct dwarf2_cu *);
1014
1015 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1016 struct dwarf2_cu *);
1017
1018 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1019
1020 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1021
1022 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1023
1024 static char *typename_concat (struct obstack *obs, const char *prefix,
1025 const char *suffix, int physname,
1026 struct dwarf2_cu *cu);
1027
1028 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1029
1030 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1031
1032 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1033
1034 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1035
1036 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1037
1038 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1039 struct dwarf2_cu *, struct partial_symtab *);
1040
1041 static int dwarf2_get_pc_bounds (struct die_info *,
1042 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1043 struct partial_symtab *);
1044
1045 static void get_scope_pc_bounds (struct die_info *,
1046 CORE_ADDR *, CORE_ADDR *,
1047 struct dwarf2_cu *);
1048
1049 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1050 CORE_ADDR, struct dwarf2_cu *);
1051
1052 static void dwarf2_add_field (struct field_info *, struct die_info *,
1053 struct dwarf2_cu *);
1054
1055 static void dwarf2_attach_fields_to_type (struct field_info *,
1056 struct type *, struct dwarf2_cu *);
1057
1058 static void dwarf2_add_member_fn (struct field_info *,
1059 struct die_info *, struct type *,
1060 struct dwarf2_cu *);
1061
1062 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1063 struct type *,
1064 struct dwarf2_cu *);
1065
1066 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1067
1068 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1069
1070 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1071
1072 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1073
1074 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1075
1076 static struct type *read_module_type (struct die_info *die,
1077 struct dwarf2_cu *cu);
1078
1079 static const char *namespace_name (struct die_info *die,
1080 int *is_anonymous, struct dwarf2_cu *);
1081
1082 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1083
1084 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1085
1086 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1087 struct dwarf2_cu *);
1088
1089 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1090
1091 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1092 gdb_byte *info_ptr,
1093 gdb_byte **new_info_ptr,
1094 struct die_info *parent);
1095
1096 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1097 gdb_byte *info_ptr,
1098 gdb_byte **new_info_ptr,
1099 struct die_info *parent);
1100
1101 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1102 gdb_byte *info_ptr,
1103 gdb_byte **new_info_ptr,
1104 struct die_info *parent);
1105
1106 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1107 struct die_info **, gdb_byte *,
1108 int *);
1109
1110 static void process_die (struct die_info *, struct dwarf2_cu *);
1111
1112 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1113 struct obstack *);
1114
1115 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1116
1117 static const char *dwarf2_full_name (char *name,
1118 struct die_info *die,
1119 struct dwarf2_cu *cu);
1120
1121 static struct die_info *dwarf2_extension (struct die_info *die,
1122 struct dwarf2_cu **);
1123
1124 static char *dwarf_tag_name (unsigned int);
1125
1126 static char *dwarf_attr_name (unsigned int);
1127
1128 static char *dwarf_form_name (unsigned int);
1129
1130 static char *dwarf_bool_name (unsigned int);
1131
1132 static char *dwarf_type_encoding_name (unsigned int);
1133
1134 #if 0
1135 static char *dwarf_cfi_name (unsigned int);
1136 #endif
1137
1138 static struct die_info *sibling_die (struct die_info *);
1139
1140 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1141
1142 static void dump_die_for_error (struct die_info *);
1143
1144 static void dump_die_1 (struct ui_file *, int level, int max_level,
1145 struct die_info *);
1146
1147 /*static*/ void dump_die (struct die_info *, int max_level);
1148
1149 static void store_in_ref_table (struct die_info *,
1150 struct dwarf2_cu *);
1151
1152 static int is_ref_attr (struct attribute *);
1153
1154 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1155
1156 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1157
1158 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1159 struct attribute *,
1160 struct dwarf2_cu **);
1161
1162 static struct die_info *follow_die_ref (struct die_info *,
1163 struct attribute *,
1164 struct dwarf2_cu **);
1165
1166 static struct die_info *follow_die_sig (struct die_info *,
1167 struct attribute *,
1168 struct dwarf2_cu **);
1169
1170 static struct signatured_type *lookup_signatured_type_at_offset
1171 (struct objfile *objfile,
1172 struct dwarf2_section_info *section,
1173 unsigned int offset);
1174
1175 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1176
1177 static void read_signatured_type (struct signatured_type *type_sig);
1178
1179 /* memory allocation interface */
1180
1181 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1182
1183 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1184
1185 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1186
1187 static void dwarf_decode_macros (struct line_header *, unsigned int,
1188 char *, bfd *, struct dwarf2_cu *,
1189 struct dwarf2_section_info *,
1190 int);
1191
1192 static int attr_form_is_block (struct attribute *);
1193
1194 static int attr_form_is_section_offset (struct attribute *);
1195
1196 static int attr_form_is_constant (struct attribute *);
1197
1198 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1199 struct dwarf2_loclist_baton *baton,
1200 struct attribute *attr);
1201
1202 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1203 struct symbol *sym,
1204 struct dwarf2_cu *cu);
1205
1206 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1207 struct abbrev_info *abbrev,
1208 struct dwarf2_cu *cu);
1209
1210 static void free_stack_comp_unit (void *);
1211
1212 static hashval_t partial_die_hash (const void *item);
1213
1214 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1215
1216 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1217 (unsigned int offset, struct objfile *objfile);
1218
1219 static void init_one_comp_unit (struct dwarf2_cu *cu,
1220 struct dwarf2_per_cu_data *per_cu);
1221
1222 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1223 struct die_info *comp_unit_die);
1224
1225 static void free_heap_comp_unit (void *);
1226
1227 static void free_cached_comp_units (void *);
1228
1229 static void age_cached_comp_units (void);
1230
1231 static void free_one_cached_comp_unit (void *);
1232
1233 static struct type *set_die_type (struct die_info *, struct type *,
1234 struct dwarf2_cu *);
1235
1236 static void create_all_comp_units (struct objfile *);
1237
1238 static int create_debug_types_hash_table (struct objfile *objfile);
1239
1240 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1241
1242 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1243
1244 static void dwarf2_add_dependence (struct dwarf2_cu *,
1245 struct dwarf2_per_cu_data *);
1246
1247 static void dwarf2_mark (struct dwarf2_cu *);
1248
1249 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1250
1251 static struct type *get_die_type_at_offset (unsigned int,
1252 struct dwarf2_per_cu_data *per_cu);
1253
1254 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1255
1256 static void dwarf2_release_queue (void *dummy);
1257
1258 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1259
1260 static void process_queue (void);
1261
1262 static void find_file_and_directory (struct die_info *die,
1263 struct dwarf2_cu *cu,
1264 char **name, char **comp_dir);
1265
1266 static char *file_full_name (int file, struct line_header *lh,
1267 const char *comp_dir);
1268
1269 static gdb_byte *read_and_check_comp_unit_head
1270 (struct comp_unit_head *header,
1271 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1272 int is_debug_types_section);
1273
1274 static void init_cu_die_reader (struct die_reader_specs *reader,
1275 struct dwarf2_cu *cu);
1276
1277 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1278
1279 #if WORDS_BIGENDIAN
1280
1281 /* Convert VALUE between big- and little-endian. */
1282 static offset_type
1283 byte_swap (offset_type value)
1284 {
1285 offset_type result;
1286
1287 result = (value & 0xff) << 24;
1288 result |= (value & 0xff00) << 8;
1289 result |= (value & 0xff0000) >> 8;
1290 result |= (value & 0xff000000) >> 24;
1291 return result;
1292 }
1293
1294 #define MAYBE_SWAP(V) byte_swap (V)
1295
1296 #else
1297 #define MAYBE_SWAP(V) (V)
1298 #endif /* WORDS_BIGENDIAN */
1299
1300 /* The suffix for an index file. */
1301 #define INDEX_SUFFIX ".gdb-index"
1302
1303 static const char *dwarf2_physname (char *name, struct die_info *die,
1304 struct dwarf2_cu *cu);
1305
1306 /* Try to locate the sections we need for DWARF 2 debugging
1307 information and return true if we have enough to do something.
1308 NAMES points to the dwarf2 section names, or is NULL if the standard
1309 ELF names are used. */
1310
1311 int
1312 dwarf2_has_info (struct objfile *objfile,
1313 const struct dwarf2_debug_sections *names)
1314 {
1315 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1316 if (!dwarf2_per_objfile)
1317 {
1318 /* Initialize per-objfile state. */
1319 struct dwarf2_per_objfile *data
1320 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1321
1322 memset (data, 0, sizeof (*data));
1323 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1324 dwarf2_per_objfile = data;
1325
1326 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1327 (void *) names);
1328 dwarf2_per_objfile->objfile = objfile;
1329 }
1330 return (dwarf2_per_objfile->info.asection != NULL
1331 && dwarf2_per_objfile->abbrev.asection != NULL);
1332 }
1333
1334 /* When loading sections, we look either for uncompressed section or for
1335 compressed section names. */
1336
1337 static int
1338 section_is_p (const char *section_name,
1339 const struct dwarf2_section_names *names)
1340 {
1341 if (names->normal != NULL
1342 && strcmp (section_name, names->normal) == 0)
1343 return 1;
1344 if (names->compressed != NULL
1345 && strcmp (section_name, names->compressed) == 0)
1346 return 1;
1347 return 0;
1348 }
1349
1350 /* This function is mapped across the sections and remembers the
1351 offset and size of each of the debugging sections we are interested
1352 in. */
1353
1354 static void
1355 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1356 {
1357 const struct dwarf2_debug_sections *names;
1358
1359 if (vnames == NULL)
1360 names = &dwarf2_elf_names;
1361 else
1362 names = (const struct dwarf2_debug_sections *) vnames;
1363
1364 if (section_is_p (sectp->name, &names->info))
1365 {
1366 dwarf2_per_objfile->info.asection = sectp;
1367 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1368 }
1369 else if (section_is_p (sectp->name, &names->abbrev))
1370 {
1371 dwarf2_per_objfile->abbrev.asection = sectp;
1372 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1373 }
1374 else if (section_is_p (sectp->name, &names->line))
1375 {
1376 dwarf2_per_objfile->line.asection = sectp;
1377 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1378 }
1379 else if (section_is_p (sectp->name, &names->loc))
1380 {
1381 dwarf2_per_objfile->loc.asection = sectp;
1382 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1383 }
1384 else if (section_is_p (sectp->name, &names->macinfo))
1385 {
1386 dwarf2_per_objfile->macinfo.asection = sectp;
1387 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1388 }
1389 else if (section_is_p (sectp->name, &names->macro))
1390 {
1391 dwarf2_per_objfile->macro.asection = sectp;
1392 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1393 }
1394 else if (section_is_p (sectp->name, &names->str))
1395 {
1396 dwarf2_per_objfile->str.asection = sectp;
1397 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1398 }
1399 else if (section_is_p (sectp->name, &names->frame))
1400 {
1401 dwarf2_per_objfile->frame.asection = sectp;
1402 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1403 }
1404 else if (section_is_p (sectp->name, &names->eh_frame))
1405 {
1406 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1407
1408 if (aflag & SEC_HAS_CONTENTS)
1409 {
1410 dwarf2_per_objfile->eh_frame.asection = sectp;
1411 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1412 }
1413 }
1414 else if (section_is_p (sectp->name, &names->ranges))
1415 {
1416 dwarf2_per_objfile->ranges.asection = sectp;
1417 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1418 }
1419 else if (section_is_p (sectp->name, &names->types))
1420 {
1421 struct dwarf2_section_info type_section;
1422
1423 memset (&type_section, 0, sizeof (type_section));
1424 type_section.asection = sectp;
1425 type_section.size = bfd_get_section_size (sectp);
1426
1427 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1428 &type_section);
1429 }
1430 else if (section_is_p (sectp->name, &names->gdb_index))
1431 {
1432 dwarf2_per_objfile->gdb_index.asection = sectp;
1433 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1434 }
1435
1436 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1437 && bfd_section_vma (abfd, sectp) == 0)
1438 dwarf2_per_objfile->has_section_at_zero = 1;
1439 }
1440
1441 /* Decompress a section that was compressed using zlib. Store the
1442 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1443
1444 static void
1445 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1446 gdb_byte **outbuf, bfd_size_type *outsize)
1447 {
1448 bfd *abfd = objfile->obfd;
1449 #ifndef HAVE_ZLIB_H
1450 error (_("Support for zlib-compressed DWARF data (from '%s') "
1451 "is disabled in this copy of GDB"),
1452 bfd_get_filename (abfd));
1453 #else
1454 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1455 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1456 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1457 bfd_size_type uncompressed_size;
1458 gdb_byte *uncompressed_buffer;
1459 z_stream strm;
1460 int rc;
1461 int header_size = 12;
1462
1463 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1464 || bfd_bread (compressed_buffer,
1465 compressed_size, abfd) != compressed_size)
1466 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1467 bfd_get_filename (abfd));
1468
1469 /* Read the zlib header. In this case, it should be "ZLIB" followed
1470 by the uncompressed section size, 8 bytes in big-endian order. */
1471 if (compressed_size < header_size
1472 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1473 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1474 bfd_get_filename (abfd));
1475 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1476 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1477 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1478 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1479 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1480 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1481 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1482 uncompressed_size += compressed_buffer[11];
1483
1484 /* It is possible the section consists of several compressed
1485 buffers concatenated together, so we uncompress in a loop. */
1486 strm.zalloc = NULL;
1487 strm.zfree = NULL;
1488 strm.opaque = NULL;
1489 strm.avail_in = compressed_size - header_size;
1490 strm.next_in = (Bytef*) compressed_buffer + header_size;
1491 strm.avail_out = uncompressed_size;
1492 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1493 uncompressed_size);
1494 rc = inflateInit (&strm);
1495 while (strm.avail_in > 0)
1496 {
1497 if (rc != Z_OK)
1498 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1499 bfd_get_filename (abfd), rc);
1500 strm.next_out = ((Bytef*) uncompressed_buffer
1501 + (uncompressed_size - strm.avail_out));
1502 rc = inflate (&strm, Z_FINISH);
1503 if (rc != Z_STREAM_END)
1504 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1505 bfd_get_filename (abfd), rc);
1506 rc = inflateReset (&strm);
1507 }
1508 rc = inflateEnd (&strm);
1509 if (rc != Z_OK
1510 || strm.avail_out != 0)
1511 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1512 bfd_get_filename (abfd), rc);
1513
1514 do_cleanups (cleanup);
1515 *outbuf = uncompressed_buffer;
1516 *outsize = uncompressed_size;
1517 #endif
1518 }
1519
1520 /* A helper function that decides whether a section is empty. */
1521
1522 static int
1523 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1524 {
1525 return info->asection == NULL || info->size == 0;
1526 }
1527
1528 /* Read the contents of the section INFO from object file specified by
1529 OBJFILE, store info about the section into INFO.
1530 If the section is compressed, uncompress it before returning. */
1531
1532 static void
1533 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1534 {
1535 bfd *abfd = objfile->obfd;
1536 asection *sectp = info->asection;
1537 gdb_byte *buf, *retbuf;
1538 unsigned char header[4];
1539
1540 if (info->readin)
1541 return;
1542 info->buffer = NULL;
1543 info->map_addr = NULL;
1544 info->readin = 1;
1545
1546 if (dwarf2_section_empty_p (info))
1547 return;
1548
1549 /* Check if the file has a 4-byte header indicating compression. */
1550 if (info->size > sizeof (header)
1551 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1552 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1553 {
1554 /* Upon decompression, update the buffer and its size. */
1555 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1556 {
1557 zlib_decompress_section (objfile, sectp, &info->buffer,
1558 &info->size);
1559 return;
1560 }
1561 }
1562
1563 #ifdef HAVE_MMAP
1564 if (pagesize == 0)
1565 pagesize = getpagesize ();
1566
1567 /* Only try to mmap sections which are large enough: we don't want to
1568 waste space due to fragmentation. Also, only try mmap for sections
1569 without relocations. */
1570
1571 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1572 {
1573 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1574 MAP_PRIVATE, sectp->filepos,
1575 &info->map_addr, &info->map_len);
1576
1577 if ((caddr_t)info->buffer != MAP_FAILED)
1578 {
1579 #if HAVE_POSIX_MADVISE
1580 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1581 #endif
1582 return;
1583 }
1584 }
1585 #endif
1586
1587 /* If we get here, we are a normal, not-compressed section. */
1588 info->buffer = buf
1589 = obstack_alloc (&objfile->objfile_obstack, info->size);
1590
1591 /* When debugging .o files, we may need to apply relocations; see
1592 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1593 We never compress sections in .o files, so we only need to
1594 try this when the section is not compressed. */
1595 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1596 if (retbuf != NULL)
1597 {
1598 info->buffer = retbuf;
1599 return;
1600 }
1601
1602 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1603 || bfd_bread (buf, info->size, abfd) != info->size)
1604 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1605 bfd_get_filename (abfd));
1606 }
1607
1608 /* A helper function that returns the size of a section in a safe way.
1609 If you are positive that the section has been read before using the
1610 size, then it is safe to refer to the dwarf2_section_info object's
1611 "size" field directly. In other cases, you must call this
1612 function, because for compressed sections the size field is not set
1613 correctly until the section has been read. */
1614
1615 static bfd_size_type
1616 dwarf2_section_size (struct objfile *objfile,
1617 struct dwarf2_section_info *info)
1618 {
1619 if (!info->readin)
1620 dwarf2_read_section (objfile, info);
1621 return info->size;
1622 }
1623
1624 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1625 SECTION_NAME. */
1626
1627 void
1628 dwarf2_get_section_info (struct objfile *objfile,
1629 enum dwarf2_section_enum sect,
1630 asection **sectp, gdb_byte **bufp,
1631 bfd_size_type *sizep)
1632 {
1633 struct dwarf2_per_objfile *data
1634 = objfile_data (objfile, dwarf2_objfile_data_key);
1635 struct dwarf2_section_info *info;
1636
1637 /* We may see an objfile without any DWARF, in which case we just
1638 return nothing. */
1639 if (data == NULL)
1640 {
1641 *sectp = NULL;
1642 *bufp = NULL;
1643 *sizep = 0;
1644 return;
1645 }
1646 switch (sect)
1647 {
1648 case DWARF2_DEBUG_FRAME:
1649 info = &data->frame;
1650 break;
1651 case DWARF2_EH_FRAME:
1652 info = &data->eh_frame;
1653 break;
1654 default:
1655 gdb_assert_not_reached ("unexpected section");
1656 }
1657
1658 dwarf2_read_section (objfile, info);
1659
1660 *sectp = info->asection;
1661 *bufp = info->buffer;
1662 *sizep = info->size;
1663 }
1664
1665 \f
1666 /* DWARF quick_symbols_functions support. */
1667
1668 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1669 unique line tables, so we maintain a separate table of all .debug_line
1670 derived entries to support the sharing.
1671 All the quick functions need is the list of file names. We discard the
1672 line_header when we're done and don't need to record it here. */
1673 struct quick_file_names
1674 {
1675 /* The offset in .debug_line of the line table. We hash on this. */
1676 unsigned int offset;
1677
1678 /* The number of entries in file_names, real_names. */
1679 unsigned int num_file_names;
1680
1681 /* The file names from the line table, after being run through
1682 file_full_name. */
1683 const char **file_names;
1684
1685 /* The file names from the line table after being run through
1686 gdb_realpath. These are computed lazily. */
1687 const char **real_names;
1688 };
1689
1690 /* When using the index (and thus not using psymtabs), each CU has an
1691 object of this type. This is used to hold information needed by
1692 the various "quick" methods. */
1693 struct dwarf2_per_cu_quick_data
1694 {
1695 /* The file table. This can be NULL if there was no file table
1696 or it's currently not read in.
1697 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1698 struct quick_file_names *file_names;
1699
1700 /* The corresponding symbol table. This is NULL if symbols for this
1701 CU have not yet been read. */
1702 struct symtab *symtab;
1703
1704 /* A temporary mark bit used when iterating over all CUs in
1705 expand_symtabs_matching. */
1706 unsigned int mark : 1;
1707
1708 /* True if we've tried to read the file table and found there isn't one.
1709 There will be no point in trying to read it again next time. */
1710 unsigned int no_file_data : 1;
1711 };
1712
1713 /* Hash function for a quick_file_names. */
1714
1715 static hashval_t
1716 hash_file_name_entry (const void *e)
1717 {
1718 const struct quick_file_names *file_data = e;
1719
1720 return file_data->offset;
1721 }
1722
1723 /* Equality function for a quick_file_names. */
1724
1725 static int
1726 eq_file_name_entry (const void *a, const void *b)
1727 {
1728 const struct quick_file_names *ea = a;
1729 const struct quick_file_names *eb = b;
1730
1731 return ea->offset == eb->offset;
1732 }
1733
1734 /* Delete function for a quick_file_names. */
1735
1736 static void
1737 delete_file_name_entry (void *e)
1738 {
1739 struct quick_file_names *file_data = e;
1740 int i;
1741
1742 for (i = 0; i < file_data->num_file_names; ++i)
1743 {
1744 xfree ((void*) file_data->file_names[i]);
1745 if (file_data->real_names)
1746 xfree ((void*) file_data->real_names[i]);
1747 }
1748
1749 /* The space for the struct itself lives on objfile_obstack,
1750 so we don't free it here. */
1751 }
1752
1753 /* Create a quick_file_names hash table. */
1754
1755 static htab_t
1756 create_quick_file_names_table (unsigned int nr_initial_entries)
1757 {
1758 return htab_create_alloc (nr_initial_entries,
1759 hash_file_name_entry, eq_file_name_entry,
1760 delete_file_name_entry, xcalloc, xfree);
1761 }
1762
1763 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1764 have to be created afterwards. You should call age_cached_comp_units after
1765 processing PER_CU->CU. dw2_setup must have been already called. */
1766
1767 static void
1768 load_cu (struct dwarf2_per_cu_data *per_cu)
1769 {
1770 if (per_cu->debug_types_section)
1771 load_full_type_unit (per_cu);
1772 else
1773 load_full_comp_unit (per_cu);
1774
1775 gdb_assert (per_cu->cu != NULL);
1776
1777 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1778 }
1779
1780 /* Read in the symbols for PER_CU. */
1781
1782 static void
1783 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1784 {
1785 struct cleanup *back_to;
1786
1787 back_to = make_cleanup (dwarf2_release_queue, NULL);
1788
1789 queue_comp_unit (per_cu);
1790
1791 load_cu (per_cu);
1792
1793 process_queue ();
1794
1795 /* Age the cache, releasing compilation units that have not
1796 been used recently. */
1797 age_cached_comp_units ();
1798
1799 do_cleanups (back_to);
1800 }
1801
1802 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1803 the objfile from which this CU came. Returns the resulting symbol
1804 table. */
1805
1806 static struct symtab *
1807 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1808 {
1809 if (!per_cu->v.quick->symtab)
1810 {
1811 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1812 increment_reading_symtab ();
1813 dw2_do_instantiate_symtab (per_cu);
1814 do_cleanups (back_to);
1815 }
1816 return per_cu->v.quick->symtab;
1817 }
1818
1819 /* Return the CU given its index. */
1820
1821 static struct dwarf2_per_cu_data *
1822 dw2_get_cu (int index)
1823 {
1824 if (index >= dwarf2_per_objfile->n_comp_units)
1825 {
1826 index -= dwarf2_per_objfile->n_comp_units;
1827 return dwarf2_per_objfile->all_type_units[index];
1828 }
1829 return dwarf2_per_objfile->all_comp_units[index];
1830 }
1831
1832 /* A helper function that knows how to read a 64-bit value in a way
1833 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1834 otherwise. */
1835
1836 static int
1837 extract_cu_value (const char *bytes, ULONGEST *result)
1838 {
1839 if (sizeof (ULONGEST) < 8)
1840 {
1841 int i;
1842
1843 /* Ignore the upper 4 bytes if they are all zero. */
1844 for (i = 0; i < 4; ++i)
1845 if (bytes[i + 4] != 0)
1846 return 0;
1847
1848 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1849 }
1850 else
1851 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1852 return 1;
1853 }
1854
1855 /* Read the CU list from the mapped index, and use it to create all
1856 the CU objects for this objfile. Return 0 if something went wrong,
1857 1 if everything went ok. */
1858
1859 static int
1860 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1861 offset_type cu_list_elements)
1862 {
1863 offset_type i;
1864
1865 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1866 dwarf2_per_objfile->all_comp_units
1867 = obstack_alloc (&objfile->objfile_obstack,
1868 dwarf2_per_objfile->n_comp_units
1869 * sizeof (struct dwarf2_per_cu_data *));
1870
1871 for (i = 0; i < cu_list_elements; i += 2)
1872 {
1873 struct dwarf2_per_cu_data *the_cu;
1874 ULONGEST offset, length;
1875
1876 if (!extract_cu_value (cu_list, &offset)
1877 || !extract_cu_value (cu_list + 8, &length))
1878 return 0;
1879 cu_list += 2 * 8;
1880
1881 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1882 struct dwarf2_per_cu_data);
1883 the_cu->offset = offset;
1884 the_cu->length = length;
1885 the_cu->objfile = objfile;
1886 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1887 struct dwarf2_per_cu_quick_data);
1888 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1889 }
1890
1891 return 1;
1892 }
1893
1894 /* Create the signatured type hash table from the index. */
1895
1896 static int
1897 create_signatured_type_table_from_index (struct objfile *objfile,
1898 struct dwarf2_section_info *section,
1899 const gdb_byte *bytes,
1900 offset_type elements)
1901 {
1902 offset_type i;
1903 htab_t sig_types_hash;
1904
1905 dwarf2_per_objfile->n_type_units = elements / 3;
1906 dwarf2_per_objfile->all_type_units
1907 = obstack_alloc (&objfile->objfile_obstack,
1908 dwarf2_per_objfile->n_type_units
1909 * sizeof (struct dwarf2_per_cu_data *));
1910
1911 sig_types_hash = allocate_signatured_type_table (objfile);
1912
1913 for (i = 0; i < elements; i += 3)
1914 {
1915 struct signatured_type *type_sig;
1916 ULONGEST offset, type_offset, signature;
1917 void **slot;
1918
1919 if (!extract_cu_value (bytes, &offset)
1920 || !extract_cu_value (bytes + 8, &type_offset))
1921 return 0;
1922 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1923 bytes += 3 * 8;
1924
1925 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1926 struct signatured_type);
1927 type_sig->signature = signature;
1928 type_sig->type_offset = type_offset;
1929 type_sig->per_cu.debug_types_section = section;
1930 type_sig->per_cu.offset = offset;
1931 type_sig->per_cu.objfile = objfile;
1932 type_sig->per_cu.v.quick
1933 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1934 struct dwarf2_per_cu_quick_data);
1935
1936 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1937 *slot = type_sig;
1938
1939 dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1940 }
1941
1942 dwarf2_per_objfile->signatured_types = sig_types_hash;
1943
1944 return 1;
1945 }
1946
1947 /* Read the address map data from the mapped index, and use it to
1948 populate the objfile's psymtabs_addrmap. */
1949
1950 static void
1951 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1952 {
1953 const gdb_byte *iter, *end;
1954 struct obstack temp_obstack;
1955 struct addrmap *mutable_map;
1956 struct cleanup *cleanup;
1957 CORE_ADDR baseaddr;
1958
1959 obstack_init (&temp_obstack);
1960 cleanup = make_cleanup_obstack_free (&temp_obstack);
1961 mutable_map = addrmap_create_mutable (&temp_obstack);
1962
1963 iter = index->address_table;
1964 end = iter + index->address_table_size;
1965
1966 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1967
1968 while (iter < end)
1969 {
1970 ULONGEST hi, lo, cu_index;
1971 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1972 iter += 8;
1973 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1974 iter += 8;
1975 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1976 iter += 4;
1977
1978 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1979 dw2_get_cu (cu_index));
1980 }
1981
1982 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1983 &objfile->objfile_obstack);
1984 do_cleanups (cleanup);
1985 }
1986
1987 /* The hash function for strings in the mapped index. This is the same as
1988 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1989 implementation. This is necessary because the hash function is tied to the
1990 format of the mapped index file. The hash values do not have to match with
1991 SYMBOL_HASH_NEXT.
1992
1993 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
1994
1995 static hashval_t
1996 mapped_index_string_hash (int index_version, const void *p)
1997 {
1998 const unsigned char *str = (const unsigned char *) p;
1999 hashval_t r = 0;
2000 unsigned char c;
2001
2002 while ((c = *str++) != 0)
2003 {
2004 if (index_version >= 5)
2005 c = tolower (c);
2006 r = r * 67 + c - 113;
2007 }
2008
2009 return r;
2010 }
2011
2012 /* Find a slot in the mapped index INDEX for the object named NAME.
2013 If NAME is found, set *VEC_OUT to point to the CU vector in the
2014 constant pool and return 1. If NAME cannot be found, return 0. */
2015
2016 static int
2017 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2018 offset_type **vec_out)
2019 {
2020 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2021 offset_type hash;
2022 offset_type slot, step;
2023 int (*cmp) (const char *, const char *);
2024
2025 if (current_language->la_language == language_cplus
2026 || current_language->la_language == language_java
2027 || current_language->la_language == language_fortran)
2028 {
2029 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2030 not contain any. */
2031 const char *paren = strchr (name, '(');
2032
2033 if (paren)
2034 {
2035 char *dup;
2036
2037 dup = xmalloc (paren - name + 1);
2038 memcpy (dup, name, paren - name);
2039 dup[paren - name] = 0;
2040
2041 make_cleanup (xfree, dup);
2042 name = dup;
2043 }
2044 }
2045
2046 /* Index version 4 did not support case insensitive searches. But the
2047 indices for case insensitive languages are built in lowercase, therefore
2048 simulate our NAME being searched is also lowercased. */
2049 hash = mapped_index_string_hash ((index->version == 4
2050 && case_sensitivity == case_sensitive_off
2051 ? 5 : index->version),
2052 name);
2053
2054 slot = hash & (index->symbol_table_slots - 1);
2055 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2056 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2057
2058 for (;;)
2059 {
2060 /* Convert a slot number to an offset into the table. */
2061 offset_type i = 2 * slot;
2062 const char *str;
2063 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2064 {
2065 do_cleanups (back_to);
2066 return 0;
2067 }
2068
2069 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2070 if (!cmp (name, str))
2071 {
2072 *vec_out = (offset_type *) (index->constant_pool
2073 + MAYBE_SWAP (index->symbol_table[i + 1]));
2074 do_cleanups (back_to);
2075 return 1;
2076 }
2077
2078 slot = (slot + step) & (index->symbol_table_slots - 1);
2079 }
2080 }
2081
2082 /* Read the index file. If everything went ok, initialize the "quick"
2083 elements of all the CUs and return 1. Otherwise, return 0. */
2084
2085 static int
2086 dwarf2_read_index (struct objfile *objfile)
2087 {
2088 char *addr;
2089 struct mapped_index *map;
2090 offset_type *metadata;
2091 const gdb_byte *cu_list;
2092 const gdb_byte *types_list = NULL;
2093 offset_type version, cu_list_elements;
2094 offset_type types_list_elements = 0;
2095 int i;
2096
2097 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2098 return 0;
2099
2100 /* Older elfutils strip versions could keep the section in the main
2101 executable while splitting it for the separate debug info file. */
2102 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2103 & SEC_HAS_CONTENTS) == 0)
2104 return 0;
2105
2106 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2107
2108 addr = dwarf2_per_objfile->gdb_index.buffer;
2109 /* Version check. */
2110 version = MAYBE_SWAP (*(offset_type *) addr);
2111 /* Versions earlier than 3 emitted every copy of a psymbol. This
2112 causes the index to behave very poorly for certain requests. Version 3
2113 contained incomplete addrmap. So, it seems better to just ignore such
2114 indices. Index version 4 uses a different hash function than index
2115 version 5 and later. */
2116 if (version < 4)
2117 return 0;
2118 /* Indices with higher version than the one supported by GDB may be no
2119 longer backward compatible. */
2120 if (version > 5)
2121 return 0;
2122
2123 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2124 map->version = version;
2125 map->total_size = dwarf2_per_objfile->gdb_index.size;
2126
2127 metadata = (offset_type *) (addr + sizeof (offset_type));
2128
2129 i = 0;
2130 cu_list = addr + MAYBE_SWAP (metadata[i]);
2131 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2132 / 8);
2133 ++i;
2134
2135 types_list = addr + MAYBE_SWAP (metadata[i]);
2136 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2137 - MAYBE_SWAP (metadata[i]))
2138 / 8);
2139 ++i;
2140
2141 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2142 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2143 - MAYBE_SWAP (metadata[i]));
2144 ++i;
2145
2146 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2147 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2148 - MAYBE_SWAP (metadata[i]))
2149 / (2 * sizeof (offset_type)));
2150 ++i;
2151
2152 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2153
2154 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2155 return 0;
2156
2157 if (types_list_elements)
2158 {
2159 struct dwarf2_section_info *section;
2160
2161 /* We can only handle a single .debug_types when we have an
2162 index. */
2163 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2164 return 0;
2165
2166 section = VEC_index (dwarf2_section_info_def,
2167 dwarf2_per_objfile->types, 0);
2168
2169 if (!create_signatured_type_table_from_index (objfile, section,
2170 types_list,
2171 types_list_elements))
2172 return 0;
2173 }
2174
2175 create_addrmap_from_index (objfile, map);
2176
2177 dwarf2_per_objfile->index_table = map;
2178 dwarf2_per_objfile->using_index = 1;
2179 dwarf2_per_objfile->quick_file_names_table =
2180 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2181
2182 return 1;
2183 }
2184
2185 /* A helper for the "quick" functions which sets the global
2186 dwarf2_per_objfile according to OBJFILE. */
2187
2188 static void
2189 dw2_setup (struct objfile *objfile)
2190 {
2191 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2192 gdb_assert (dwarf2_per_objfile);
2193 }
2194
2195 /* A helper for the "quick" functions which attempts to read the line
2196 table for THIS_CU. */
2197
2198 static struct quick_file_names *
2199 dw2_get_file_names (struct objfile *objfile,
2200 struct dwarf2_per_cu_data *this_cu)
2201 {
2202 bfd *abfd = objfile->obfd;
2203 struct line_header *lh;
2204 struct attribute *attr;
2205 struct cleanup *cleanups;
2206 struct die_info *comp_unit_die;
2207 struct dwarf2_section_info* sec;
2208 gdb_byte *info_ptr;
2209 int has_children, i;
2210 struct dwarf2_cu cu;
2211 unsigned int bytes_read;
2212 struct die_reader_specs reader_specs;
2213 char *name, *comp_dir;
2214 void **slot;
2215 struct quick_file_names *qfn;
2216 unsigned int line_offset;
2217
2218 if (this_cu->v.quick->file_names != NULL)
2219 return this_cu->v.quick->file_names;
2220 /* If we know there is no line data, no point in looking again. */
2221 if (this_cu->v.quick->no_file_data)
2222 return NULL;
2223
2224 init_one_comp_unit (&cu, this_cu);
2225 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2226
2227 if (this_cu->debug_types_section)
2228 sec = this_cu->debug_types_section;
2229 else
2230 sec = &dwarf2_per_objfile->info;
2231 dwarf2_read_section (objfile, sec);
2232 info_ptr = sec->buffer + this_cu->offset;
2233
2234 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2235 this_cu->debug_types_section != NULL);
2236
2237 /* Skip dummy compilation units. */
2238 if (info_ptr >= (sec->buffer + sec->size)
2239 || peek_abbrev_code (abfd, info_ptr) == 0)
2240 {
2241 do_cleanups (cleanups);
2242 return NULL;
2243 }
2244
2245 dwarf2_read_abbrevs (&cu);
2246 make_cleanup (dwarf2_free_abbrev_table, &cu);
2247
2248 init_cu_die_reader (&reader_specs, &cu);
2249 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2250 &has_children);
2251
2252 lh = NULL;
2253 slot = NULL;
2254 line_offset = 0;
2255 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2256 if (attr)
2257 {
2258 struct quick_file_names find_entry;
2259
2260 line_offset = DW_UNSND (attr);
2261
2262 /* We may have already read in this line header (TU line header sharing).
2263 If we have we're done. */
2264 find_entry.offset = line_offset;
2265 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2266 &find_entry, INSERT);
2267 if (*slot != NULL)
2268 {
2269 do_cleanups (cleanups);
2270 this_cu->v.quick->file_names = *slot;
2271 return *slot;
2272 }
2273
2274 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2275 }
2276 if (lh == NULL)
2277 {
2278 do_cleanups (cleanups);
2279 this_cu->v.quick->no_file_data = 1;
2280 return NULL;
2281 }
2282
2283 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2284 qfn->offset = line_offset;
2285 gdb_assert (slot != NULL);
2286 *slot = qfn;
2287
2288 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2289
2290 qfn->num_file_names = lh->num_file_names;
2291 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2292 lh->num_file_names * sizeof (char *));
2293 for (i = 0; i < lh->num_file_names; ++i)
2294 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2295 qfn->real_names = NULL;
2296
2297 free_line_header (lh);
2298 do_cleanups (cleanups);
2299
2300 this_cu->v.quick->file_names = qfn;
2301 return qfn;
2302 }
2303
2304 /* A helper for the "quick" functions which computes and caches the
2305 real path for a given file name from the line table. */
2306
2307 static const char *
2308 dw2_get_real_path (struct objfile *objfile,
2309 struct quick_file_names *qfn, int index)
2310 {
2311 if (qfn->real_names == NULL)
2312 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2313 qfn->num_file_names, sizeof (char *));
2314
2315 if (qfn->real_names[index] == NULL)
2316 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2317
2318 return qfn->real_names[index];
2319 }
2320
2321 static struct symtab *
2322 dw2_find_last_source_symtab (struct objfile *objfile)
2323 {
2324 int index;
2325
2326 dw2_setup (objfile);
2327 index = dwarf2_per_objfile->n_comp_units - 1;
2328 return dw2_instantiate_symtab (dw2_get_cu (index));
2329 }
2330
2331 /* Traversal function for dw2_forget_cached_source_info. */
2332
2333 static int
2334 dw2_free_cached_file_names (void **slot, void *info)
2335 {
2336 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2337
2338 if (file_data->real_names)
2339 {
2340 int i;
2341
2342 for (i = 0; i < file_data->num_file_names; ++i)
2343 {
2344 xfree ((void*) file_data->real_names[i]);
2345 file_data->real_names[i] = NULL;
2346 }
2347 }
2348
2349 return 1;
2350 }
2351
2352 static void
2353 dw2_forget_cached_source_info (struct objfile *objfile)
2354 {
2355 dw2_setup (objfile);
2356
2357 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2358 dw2_free_cached_file_names, NULL);
2359 }
2360
2361 /* Helper function for dw2_map_symtabs_matching_filename that expands
2362 the symtabs and calls the iterator. */
2363
2364 static int
2365 dw2_map_expand_apply (struct objfile *objfile,
2366 struct dwarf2_per_cu_data *per_cu,
2367 const char *name,
2368 const char *full_path, const char *real_path,
2369 int (*callback) (struct symtab *, void *),
2370 void *data)
2371 {
2372 struct symtab *last_made = objfile->symtabs;
2373
2374 /* Don't visit already-expanded CUs. */
2375 if (per_cu->v.quick->symtab)
2376 return 0;
2377
2378 /* This may expand more than one symtab, and we want to iterate over
2379 all of them. */
2380 dw2_instantiate_symtab (per_cu);
2381
2382 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2383 objfile->symtabs, last_made);
2384 }
2385
2386 /* Implementation of the map_symtabs_matching_filename method. */
2387
2388 static int
2389 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2390 const char *full_path, const char *real_path,
2391 int (*callback) (struct symtab *, void *),
2392 void *data)
2393 {
2394 int i;
2395 const char *name_basename = lbasename (name);
2396 int name_len = strlen (name);
2397 int is_abs = IS_ABSOLUTE_PATH (name);
2398
2399 dw2_setup (objfile);
2400
2401 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2402 + dwarf2_per_objfile->n_type_units); ++i)
2403 {
2404 int j;
2405 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2406 struct quick_file_names *file_data;
2407
2408 /* We only need to look at symtabs not already expanded. */
2409 if (per_cu->v.quick->symtab)
2410 continue;
2411
2412 file_data = dw2_get_file_names (objfile, per_cu);
2413 if (file_data == NULL)
2414 continue;
2415
2416 for (j = 0; j < file_data->num_file_names; ++j)
2417 {
2418 const char *this_name = file_data->file_names[j];
2419
2420 if (FILENAME_CMP (name, this_name) == 0
2421 || (!is_abs && compare_filenames_for_search (this_name,
2422 name, name_len)))
2423 {
2424 if (dw2_map_expand_apply (objfile, per_cu,
2425 name, full_path, real_path,
2426 callback, data))
2427 return 1;
2428 }
2429
2430 /* Before we invoke realpath, which can get expensive when many
2431 files are involved, do a quick comparison of the basenames. */
2432 if (! basenames_may_differ
2433 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2434 continue;
2435
2436 if (full_path != NULL)
2437 {
2438 const char *this_real_name = dw2_get_real_path (objfile,
2439 file_data, j);
2440
2441 if (this_real_name != NULL
2442 && (FILENAME_CMP (full_path, this_real_name) == 0
2443 || (!is_abs
2444 && compare_filenames_for_search (this_real_name,
2445 name, name_len))))
2446 {
2447 if (dw2_map_expand_apply (objfile, per_cu,
2448 name, full_path, real_path,
2449 callback, data))
2450 return 1;
2451 }
2452 }
2453
2454 if (real_path != NULL)
2455 {
2456 const char *this_real_name = dw2_get_real_path (objfile,
2457 file_data, j);
2458
2459 if (this_real_name != NULL
2460 && (FILENAME_CMP (real_path, this_real_name) == 0
2461 || (!is_abs
2462 && compare_filenames_for_search (this_real_name,
2463 name, name_len))))
2464 {
2465 if (dw2_map_expand_apply (objfile, per_cu,
2466 name, full_path, real_path,
2467 callback, data))
2468 return 1;
2469 }
2470 }
2471 }
2472 }
2473
2474 return 0;
2475 }
2476
2477 static struct symtab *
2478 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2479 const char *name, domain_enum domain)
2480 {
2481 /* We do all the work in the pre_expand_symtabs_matching hook
2482 instead. */
2483 return NULL;
2484 }
2485
2486 /* A helper function that expands all symtabs that hold an object
2487 named NAME. */
2488
2489 static void
2490 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2491 {
2492 dw2_setup (objfile);
2493
2494 /* index_table is NULL if OBJF_READNOW. */
2495 if (dwarf2_per_objfile->index_table)
2496 {
2497 offset_type *vec;
2498
2499 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2500 name, &vec))
2501 {
2502 offset_type i, len = MAYBE_SWAP (*vec);
2503 for (i = 0; i < len; ++i)
2504 {
2505 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2506 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2507
2508 dw2_instantiate_symtab (per_cu);
2509 }
2510 }
2511 }
2512 }
2513
2514 static void
2515 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2516 enum block_enum block_kind, const char *name,
2517 domain_enum domain)
2518 {
2519 dw2_do_expand_symtabs_matching (objfile, name);
2520 }
2521
2522 static void
2523 dw2_print_stats (struct objfile *objfile)
2524 {
2525 int i, count;
2526
2527 dw2_setup (objfile);
2528 count = 0;
2529 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2530 + dwarf2_per_objfile->n_type_units); ++i)
2531 {
2532 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2533
2534 if (!per_cu->v.quick->symtab)
2535 ++count;
2536 }
2537 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2538 }
2539
2540 static void
2541 dw2_dump (struct objfile *objfile)
2542 {
2543 /* Nothing worth printing. */
2544 }
2545
2546 static void
2547 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2548 struct section_offsets *delta)
2549 {
2550 /* There's nothing to relocate here. */
2551 }
2552
2553 static void
2554 dw2_expand_symtabs_for_function (struct objfile *objfile,
2555 const char *func_name)
2556 {
2557 dw2_do_expand_symtabs_matching (objfile, func_name);
2558 }
2559
2560 static void
2561 dw2_expand_all_symtabs (struct objfile *objfile)
2562 {
2563 int i;
2564
2565 dw2_setup (objfile);
2566
2567 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2568 + dwarf2_per_objfile->n_type_units); ++i)
2569 {
2570 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2571
2572 dw2_instantiate_symtab (per_cu);
2573 }
2574 }
2575
2576 static void
2577 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2578 const char *filename)
2579 {
2580 int i;
2581
2582 dw2_setup (objfile);
2583
2584 /* We don't need to consider type units here.
2585 This is only called for examining code, e.g. expand_line_sal.
2586 There can be an order of magnitude (or more) more type units
2587 than comp units, and we avoid them if we can. */
2588
2589 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2590 {
2591 int j;
2592 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2593 struct quick_file_names *file_data;
2594
2595 /* We only need to look at symtabs not already expanded. */
2596 if (per_cu->v.quick->symtab)
2597 continue;
2598
2599 file_data = dw2_get_file_names (objfile, per_cu);
2600 if (file_data == NULL)
2601 continue;
2602
2603 for (j = 0; j < file_data->num_file_names; ++j)
2604 {
2605 const char *this_name = file_data->file_names[j];
2606 if (FILENAME_CMP (this_name, filename) == 0)
2607 {
2608 dw2_instantiate_symtab (per_cu);
2609 break;
2610 }
2611 }
2612 }
2613 }
2614
2615 static const char *
2616 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2617 {
2618 struct dwarf2_per_cu_data *per_cu;
2619 offset_type *vec;
2620 struct quick_file_names *file_data;
2621
2622 dw2_setup (objfile);
2623
2624 /* index_table is NULL if OBJF_READNOW. */
2625 if (!dwarf2_per_objfile->index_table)
2626 {
2627 struct symtab *s;
2628
2629 ALL_OBJFILE_SYMTABS (objfile, s)
2630 if (s->primary)
2631 {
2632 struct blockvector *bv = BLOCKVECTOR (s);
2633 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2634 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2635
2636 if (sym)
2637 return sym->symtab->filename;
2638 }
2639 return NULL;
2640 }
2641
2642 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2643 name, &vec))
2644 return NULL;
2645
2646 /* Note that this just looks at the very first one named NAME -- but
2647 actually we are looking for a function. find_main_filename
2648 should be rewritten so that it doesn't require a custom hook. It
2649 could just use the ordinary symbol tables. */
2650 /* vec[0] is the length, which must always be >0. */
2651 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2652
2653 file_data = dw2_get_file_names (objfile, per_cu);
2654 if (file_data == NULL)
2655 return NULL;
2656
2657 return file_data->file_names[file_data->num_file_names - 1];
2658 }
2659
2660 static void
2661 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2662 struct objfile *objfile, int global,
2663 int (*callback) (struct block *,
2664 struct symbol *, void *),
2665 void *data, symbol_compare_ftype *match,
2666 symbol_compare_ftype *ordered_compare)
2667 {
2668 /* Currently unimplemented; used for Ada. The function can be called if the
2669 current language is Ada for a non-Ada objfile using GNU index. As Ada
2670 does not look for non-Ada symbols this function should just return. */
2671 }
2672
2673 static void
2674 dw2_expand_symtabs_matching
2675 (struct objfile *objfile,
2676 int (*file_matcher) (const char *, void *),
2677 int (*name_matcher) (const char *, void *),
2678 enum search_domain kind,
2679 void *data)
2680 {
2681 int i;
2682 offset_type iter;
2683 struct mapped_index *index;
2684
2685 dw2_setup (objfile);
2686
2687 /* index_table is NULL if OBJF_READNOW. */
2688 if (!dwarf2_per_objfile->index_table)
2689 return;
2690 index = dwarf2_per_objfile->index_table;
2691
2692 if (file_matcher != NULL)
2693 {
2694 struct cleanup *cleanup;
2695 htab_t visited_found, visited_not_found;
2696
2697 visited_found = htab_create_alloc (10,
2698 htab_hash_pointer, htab_eq_pointer,
2699 NULL, xcalloc, xfree);
2700 cleanup = make_cleanup_htab_delete (visited_found);
2701 visited_not_found = htab_create_alloc (10,
2702 htab_hash_pointer, htab_eq_pointer,
2703 NULL, xcalloc, xfree);
2704 make_cleanup_htab_delete (visited_not_found);
2705
2706 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2707 + dwarf2_per_objfile->n_type_units); ++i)
2708 {
2709 int j;
2710 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2711 struct quick_file_names *file_data;
2712 void **slot;
2713
2714 per_cu->v.quick->mark = 0;
2715
2716 /* We only need to look at symtabs not already expanded. */
2717 if (per_cu->v.quick->symtab)
2718 continue;
2719
2720 file_data = dw2_get_file_names (objfile, per_cu);
2721 if (file_data == NULL)
2722 continue;
2723
2724 if (htab_find (visited_not_found, file_data) != NULL)
2725 continue;
2726 else if (htab_find (visited_found, file_data) != NULL)
2727 {
2728 per_cu->v.quick->mark = 1;
2729 continue;
2730 }
2731
2732 for (j = 0; j < file_data->num_file_names; ++j)
2733 {
2734 if (file_matcher (file_data->file_names[j], data))
2735 {
2736 per_cu->v.quick->mark = 1;
2737 break;
2738 }
2739 }
2740
2741 slot = htab_find_slot (per_cu->v.quick->mark
2742 ? visited_found
2743 : visited_not_found,
2744 file_data, INSERT);
2745 *slot = file_data;
2746 }
2747
2748 do_cleanups (cleanup);
2749 }
2750
2751 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2752 {
2753 offset_type idx = 2 * iter;
2754 const char *name;
2755 offset_type *vec, vec_len, vec_idx;
2756
2757 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2758 continue;
2759
2760 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2761
2762 if (! (*name_matcher) (name, data))
2763 continue;
2764
2765 /* The name was matched, now expand corresponding CUs that were
2766 marked. */
2767 vec = (offset_type *) (index->constant_pool
2768 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2769 vec_len = MAYBE_SWAP (vec[0]);
2770 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2771 {
2772 struct dwarf2_per_cu_data *per_cu;
2773
2774 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2775 if (file_matcher == NULL || per_cu->v.quick->mark)
2776 dw2_instantiate_symtab (per_cu);
2777 }
2778 }
2779 }
2780
2781 static struct symtab *
2782 dw2_find_pc_sect_symtab (struct objfile *objfile,
2783 struct minimal_symbol *msymbol,
2784 CORE_ADDR pc,
2785 struct obj_section *section,
2786 int warn_if_readin)
2787 {
2788 struct dwarf2_per_cu_data *data;
2789
2790 dw2_setup (objfile);
2791
2792 if (!objfile->psymtabs_addrmap)
2793 return NULL;
2794
2795 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2796 if (!data)
2797 return NULL;
2798
2799 if (warn_if_readin && data->v.quick->symtab)
2800 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2801 paddress (get_objfile_arch (objfile), pc));
2802
2803 return dw2_instantiate_symtab (data);
2804 }
2805
2806 static void
2807 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2808 void *data, int need_fullname)
2809 {
2810 int i;
2811 struct cleanup *cleanup;
2812 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2813 NULL, xcalloc, xfree);
2814
2815 cleanup = make_cleanup_htab_delete (visited);
2816 dw2_setup (objfile);
2817
2818 /* We can ignore file names coming from already-expanded CUs. */
2819 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2820 + dwarf2_per_objfile->n_type_units); ++i)
2821 {
2822 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2823
2824 if (per_cu->v.quick->symtab)
2825 {
2826 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2827 INSERT);
2828
2829 *slot = per_cu->v.quick->file_names;
2830 }
2831 }
2832
2833 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2834 + dwarf2_per_objfile->n_type_units); ++i)
2835 {
2836 int j;
2837 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2838 struct quick_file_names *file_data;
2839 void **slot;
2840
2841 /* We only need to look at symtabs not already expanded. */
2842 if (per_cu->v.quick->symtab)
2843 continue;
2844
2845 file_data = dw2_get_file_names (objfile, per_cu);
2846 if (file_data == NULL)
2847 continue;
2848
2849 slot = htab_find_slot (visited, file_data, INSERT);
2850 if (*slot)
2851 {
2852 /* Already visited. */
2853 continue;
2854 }
2855 *slot = file_data;
2856
2857 for (j = 0; j < file_data->num_file_names; ++j)
2858 {
2859 const char *this_real_name;
2860
2861 if (need_fullname)
2862 this_real_name = dw2_get_real_path (objfile, file_data, j);
2863 else
2864 this_real_name = NULL;
2865 (*fun) (file_data->file_names[j], this_real_name, data);
2866 }
2867 }
2868
2869 do_cleanups (cleanup);
2870 }
2871
2872 static int
2873 dw2_has_symbols (struct objfile *objfile)
2874 {
2875 return 1;
2876 }
2877
2878 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2879 {
2880 dw2_has_symbols,
2881 dw2_find_last_source_symtab,
2882 dw2_forget_cached_source_info,
2883 dw2_map_symtabs_matching_filename,
2884 dw2_lookup_symbol,
2885 dw2_pre_expand_symtabs_matching,
2886 dw2_print_stats,
2887 dw2_dump,
2888 dw2_relocate,
2889 dw2_expand_symtabs_for_function,
2890 dw2_expand_all_symtabs,
2891 dw2_expand_symtabs_with_filename,
2892 dw2_find_symbol_file,
2893 dw2_map_matching_symbols,
2894 dw2_expand_symtabs_matching,
2895 dw2_find_pc_sect_symtab,
2896 dw2_map_symbol_filenames
2897 };
2898
2899 /* Initialize for reading DWARF for this objfile. Return 0 if this
2900 file will use psymtabs, or 1 if using the GNU index. */
2901
2902 int
2903 dwarf2_initialize_objfile (struct objfile *objfile)
2904 {
2905 /* If we're about to read full symbols, don't bother with the
2906 indices. In this case we also don't care if some other debug
2907 format is making psymtabs, because they are all about to be
2908 expanded anyway. */
2909 if ((objfile->flags & OBJF_READNOW))
2910 {
2911 int i;
2912
2913 dwarf2_per_objfile->using_index = 1;
2914 create_all_comp_units (objfile);
2915 create_debug_types_hash_table (objfile);
2916 dwarf2_per_objfile->quick_file_names_table =
2917 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2918
2919 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2920 + dwarf2_per_objfile->n_type_units); ++i)
2921 {
2922 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2923
2924 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2925 struct dwarf2_per_cu_quick_data);
2926 }
2927
2928 /* Return 1 so that gdb sees the "quick" functions. However,
2929 these functions will be no-ops because we will have expanded
2930 all symtabs. */
2931 return 1;
2932 }
2933
2934 if (dwarf2_read_index (objfile))
2935 return 1;
2936
2937 return 0;
2938 }
2939
2940 \f
2941
2942 /* Build a partial symbol table. */
2943
2944 void
2945 dwarf2_build_psymtabs (struct objfile *objfile)
2946 {
2947 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2948 {
2949 init_psymbol_list (objfile, 1024);
2950 }
2951
2952 dwarf2_build_psymtabs_hard (objfile);
2953 }
2954
2955 /* Return TRUE if OFFSET is within CU_HEADER. */
2956
2957 static inline int
2958 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2959 {
2960 unsigned int bottom = cu_header->offset;
2961 unsigned int top = (cu_header->offset
2962 + cu_header->length
2963 + cu_header->initial_length_size);
2964
2965 return (offset >= bottom && offset < top);
2966 }
2967
2968 /* Read in the comp unit header information from the debug_info at info_ptr.
2969 NOTE: This leaves members offset, first_die_offset to be filled in
2970 by the caller. */
2971
2972 static gdb_byte *
2973 read_comp_unit_head (struct comp_unit_head *cu_header,
2974 gdb_byte *info_ptr, bfd *abfd)
2975 {
2976 int signed_addr;
2977 unsigned int bytes_read;
2978
2979 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2980 cu_header->initial_length_size = bytes_read;
2981 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2982 info_ptr += bytes_read;
2983 cu_header->version = read_2_bytes (abfd, info_ptr);
2984 info_ptr += 2;
2985 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2986 &bytes_read);
2987 info_ptr += bytes_read;
2988 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2989 info_ptr += 1;
2990 signed_addr = bfd_get_sign_extend_vma (abfd);
2991 if (signed_addr < 0)
2992 internal_error (__FILE__, __LINE__,
2993 _("read_comp_unit_head: dwarf from non elf file"));
2994 cu_header->signed_addr_p = signed_addr;
2995
2996 return info_ptr;
2997 }
2998
2999 /* Subroutine of read_and_check_comp_unit_head and
3000 read_and_check_type_unit_head to simplify them.
3001 Perform various error checking on the header. */
3002
3003 static void
3004 error_check_comp_unit_head (struct comp_unit_head *header,
3005 struct dwarf2_section_info *section)
3006 {
3007 bfd *abfd = section->asection->owner;
3008 const char *filename = bfd_get_filename (abfd);
3009
3010 if (header->version != 2 && header->version != 3 && header->version != 4)
3011 error (_("Dwarf Error: wrong version in compilation unit header "
3012 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3013 filename);
3014
3015 if (header->abbrev_offset
3016 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3017 &dwarf2_per_objfile->abbrev))
3018 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3019 "(offset 0x%lx + 6) [in module %s]"),
3020 (long) header->abbrev_offset, (long) header->offset,
3021 filename);
3022
3023 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3024 avoid potential 32-bit overflow. */
3025 if (((unsigned long) header->offset
3026 + header->length + header->initial_length_size)
3027 > section->size)
3028 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3029 "(offset 0x%lx + 0) [in module %s]"),
3030 (long) header->length, (long) header->offset,
3031 filename);
3032 }
3033
3034 /* Read in a CU/TU header and perform some basic error checking.
3035 The contents of the header are stored in HEADER.
3036 The result is a pointer to the start of the first DIE. */
3037
3038 static gdb_byte *
3039 read_and_check_comp_unit_head (struct comp_unit_head *header,
3040 struct dwarf2_section_info *section,
3041 gdb_byte *info_ptr,
3042 int is_debug_types_section)
3043 {
3044 gdb_byte *beg_of_comp_unit = info_ptr;
3045 bfd *abfd = section->asection->owner;
3046
3047 header->offset = beg_of_comp_unit - section->buffer;
3048
3049 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3050
3051 /* If we're reading a type unit, skip over the signature and
3052 type_offset fields. */
3053 if (is_debug_types_section)
3054 info_ptr += 8 /*signature*/ + header->offset_size;
3055
3056 header->first_die_offset = info_ptr - beg_of_comp_unit;
3057
3058 error_check_comp_unit_head (header, section);
3059
3060 return info_ptr;
3061 }
3062
3063 /* Read in the types comp unit header information from .debug_types entry at
3064 types_ptr. The result is a pointer to one past the end of the header. */
3065
3066 static gdb_byte *
3067 read_and_check_type_unit_head (struct comp_unit_head *header,
3068 struct dwarf2_section_info *section,
3069 gdb_byte *info_ptr,
3070 ULONGEST *signature, unsigned int *type_offset)
3071 {
3072 gdb_byte *beg_of_comp_unit = info_ptr;
3073 bfd *abfd = section->asection->owner;
3074
3075 header->offset = beg_of_comp_unit - section->buffer;
3076
3077 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3078
3079 /* If we're reading a type unit, skip over the signature and
3080 type_offset fields. */
3081 if (signature != NULL)
3082 *signature = read_8_bytes (abfd, info_ptr);
3083 info_ptr += 8;
3084 if (type_offset != NULL)
3085 *type_offset = read_offset_1 (abfd, info_ptr, header->offset_size);
3086 info_ptr += header->offset_size;
3087
3088 header->first_die_offset = info_ptr - beg_of_comp_unit;
3089
3090 error_check_comp_unit_head (header, section);
3091
3092 return info_ptr;
3093 }
3094
3095 /* Allocate a new partial symtab for file named NAME and mark this new
3096 partial symtab as being an include of PST. */
3097
3098 static void
3099 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3100 struct objfile *objfile)
3101 {
3102 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3103
3104 subpst->section_offsets = pst->section_offsets;
3105 subpst->textlow = 0;
3106 subpst->texthigh = 0;
3107
3108 subpst->dependencies = (struct partial_symtab **)
3109 obstack_alloc (&objfile->objfile_obstack,
3110 sizeof (struct partial_symtab *));
3111 subpst->dependencies[0] = pst;
3112 subpst->number_of_dependencies = 1;
3113
3114 subpst->globals_offset = 0;
3115 subpst->n_global_syms = 0;
3116 subpst->statics_offset = 0;
3117 subpst->n_static_syms = 0;
3118 subpst->symtab = NULL;
3119 subpst->read_symtab = pst->read_symtab;
3120 subpst->readin = 0;
3121
3122 /* No private part is necessary for include psymtabs. This property
3123 can be used to differentiate between such include psymtabs and
3124 the regular ones. */
3125 subpst->read_symtab_private = NULL;
3126 }
3127
3128 /* Read the Line Number Program data and extract the list of files
3129 included by the source file represented by PST. Build an include
3130 partial symtab for each of these included files. */
3131
3132 static void
3133 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3134 struct die_info *die,
3135 struct partial_symtab *pst)
3136 {
3137 struct objfile *objfile = cu->objfile;
3138 bfd *abfd = objfile->obfd;
3139 struct line_header *lh = NULL;
3140 struct attribute *attr;
3141
3142 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3143 if (attr)
3144 {
3145 unsigned int line_offset = DW_UNSND (attr);
3146
3147 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3148 }
3149 if (lh == NULL)
3150 return; /* No linetable, so no includes. */
3151
3152 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3153 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3154
3155 free_line_header (lh);
3156 }
3157
3158 static hashval_t
3159 hash_type_signature (const void *item)
3160 {
3161 const struct signatured_type *type_sig = item;
3162
3163 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3164 return type_sig->signature;
3165 }
3166
3167 static int
3168 eq_type_signature (const void *item_lhs, const void *item_rhs)
3169 {
3170 const struct signatured_type *lhs = item_lhs;
3171 const struct signatured_type *rhs = item_rhs;
3172
3173 return lhs->signature == rhs->signature;
3174 }
3175
3176 /* Allocate a hash table for signatured types. */
3177
3178 static htab_t
3179 allocate_signatured_type_table (struct objfile *objfile)
3180 {
3181 return htab_create_alloc_ex (41,
3182 hash_type_signature,
3183 eq_type_signature,
3184 NULL,
3185 &objfile->objfile_obstack,
3186 hashtab_obstack_allocate,
3187 dummy_obstack_deallocate);
3188 }
3189
3190 /* A helper function to add a signatured type CU to a table. */
3191
3192 static int
3193 add_signatured_type_cu_to_table (void **slot, void *datum)
3194 {
3195 struct signatured_type *sigt = *slot;
3196 struct dwarf2_per_cu_data ***datap = datum;
3197
3198 **datap = &sigt->per_cu;
3199 ++*datap;
3200
3201 return 1;
3202 }
3203
3204 /* Create the hash table of all entries in the .debug_types section(s).
3205 The result is zero if there is an error (e.g. missing .debug_types section),
3206 otherwise non-zero. */
3207
3208 static int
3209 create_debug_types_hash_table (struct objfile *objfile)
3210 {
3211 htab_t types_htab = NULL;
3212 struct dwarf2_per_cu_data **iter;
3213 int ix;
3214 struct dwarf2_section_info *section;
3215
3216 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3217 {
3218 dwarf2_per_objfile->signatured_types = NULL;
3219 return 0;
3220 }
3221
3222 for (ix = 0;
3223 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3224 ix, section);
3225 ++ix)
3226 {
3227 gdb_byte *info_ptr, *end_ptr;
3228
3229 dwarf2_read_section (objfile, section);
3230 info_ptr = section->buffer;
3231
3232 if (info_ptr == NULL)
3233 continue;
3234
3235 if (types_htab == NULL)
3236 types_htab = allocate_signatured_type_table (objfile);
3237
3238 if (dwarf2_die_debug)
3239 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3240
3241 end_ptr = info_ptr + section->size;
3242 while (info_ptr < end_ptr)
3243 {
3244 unsigned int offset;
3245 unsigned int type_offset;
3246 ULONGEST signature;
3247 struct signatured_type *type_sig;
3248 void **slot;
3249 gdb_byte *ptr = info_ptr;
3250 struct comp_unit_head header;
3251
3252 offset = ptr - section->buffer;
3253
3254 /* We need to read the type's signature in order to build the hash
3255 table, but we don't need anything else just yet. */
3256
3257 ptr = read_and_check_type_unit_head (&header, section, ptr,
3258 &signature, &type_offset);
3259
3260 /* Skip dummy type units. */
3261 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3262 {
3263 info_ptr = info_ptr + header.initial_length_size + header.length;
3264 continue;
3265 }
3266
3267 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3268 memset (type_sig, 0, sizeof (*type_sig));
3269 type_sig->signature = signature;
3270 type_sig->type_offset = type_offset;
3271 type_sig->per_cu.objfile = objfile;
3272 type_sig->per_cu.debug_types_section = section;
3273 type_sig->per_cu.offset = offset;
3274
3275 slot = htab_find_slot (types_htab, type_sig, INSERT);
3276 gdb_assert (slot != NULL);
3277 if (*slot != NULL)
3278 {
3279 const struct signatured_type *dup_sig = *slot;
3280
3281 complaint (&symfile_complaints,
3282 _("debug type entry at offset 0x%x is duplicate to the "
3283 "entry at offset 0x%x, signature 0x%s"),
3284 offset, dup_sig->per_cu.offset,
3285 phex (signature, sizeof (signature)));
3286 gdb_assert (signature == dup_sig->signature);
3287 }
3288 *slot = type_sig;
3289
3290 if (dwarf2_die_debug)
3291 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3292 offset, phex (signature, sizeof (signature)));
3293
3294 info_ptr = info_ptr + header.initial_length_size + header.length;
3295 }
3296 }
3297
3298 dwarf2_per_objfile->signatured_types = types_htab;
3299
3300 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3301 dwarf2_per_objfile->all_type_units
3302 = obstack_alloc (&objfile->objfile_obstack,
3303 dwarf2_per_objfile->n_type_units
3304 * sizeof (struct dwarf2_per_cu_data *));
3305 iter = &dwarf2_per_objfile->all_type_units[0];
3306 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3307 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3308 == dwarf2_per_objfile->n_type_units);
3309
3310 return 1;
3311 }
3312
3313 /* Lookup a signature based type.
3314 Returns NULL if SIG is not present in the table. */
3315
3316 static struct signatured_type *
3317 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3318 {
3319 struct signatured_type find_entry, *entry;
3320
3321 if (dwarf2_per_objfile->signatured_types == NULL)
3322 {
3323 complaint (&symfile_complaints,
3324 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3325 return 0;
3326 }
3327
3328 find_entry.signature = sig;
3329 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3330 return entry;
3331 }
3332
3333 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3334
3335 static void
3336 init_cu_die_reader (struct die_reader_specs *reader,
3337 struct dwarf2_cu *cu)
3338 {
3339 reader->abfd = cu->objfile->obfd;
3340 reader->cu = cu;
3341 if (cu->per_cu->debug_types_section)
3342 {
3343 gdb_assert (cu->per_cu->debug_types_section->readin);
3344 reader->buffer = cu->per_cu->debug_types_section->buffer;
3345 }
3346 else
3347 {
3348 gdb_assert (dwarf2_per_objfile->info.readin);
3349 reader->buffer = dwarf2_per_objfile->info.buffer;
3350 }
3351 }
3352
3353 /* Find the base address of the compilation unit for range lists and
3354 location lists. It will normally be specified by DW_AT_low_pc.
3355 In DWARF-3 draft 4, the base address could be overridden by
3356 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3357 compilation units with discontinuous ranges. */
3358
3359 static void
3360 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3361 {
3362 struct attribute *attr;
3363
3364 cu->base_known = 0;
3365 cu->base_address = 0;
3366
3367 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3368 if (attr)
3369 {
3370 cu->base_address = DW_ADDR (attr);
3371 cu->base_known = 1;
3372 }
3373 else
3374 {
3375 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3376 if (attr)
3377 {
3378 cu->base_address = DW_ADDR (attr);
3379 cu->base_known = 1;
3380 }
3381 }
3382 }
3383
3384 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3385 to combine the common parts.
3386 Process compilation unit THIS_CU for a psymtab.
3387 SECTION is the section the CU/TU comes from,
3388 either .debug_info or .debug_types. */
3389
3390 static void
3391 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3392 struct dwarf2_section_info *section,
3393 int is_debug_types_section)
3394 {
3395 struct objfile *objfile = this_cu->objfile;
3396 bfd *abfd = objfile->obfd;
3397 gdb_byte *buffer = section->buffer;
3398 gdb_byte *info_ptr = buffer + this_cu->offset;
3399 unsigned int buffer_size = section->size;
3400 gdb_byte *beg_of_comp_unit = info_ptr;
3401 struct die_info *comp_unit_die;
3402 struct partial_symtab *pst;
3403 CORE_ADDR baseaddr;
3404 struct cleanup *back_to_inner;
3405 struct dwarf2_cu cu;
3406 int has_children, has_pc_info;
3407 struct attribute *attr;
3408 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3409 struct die_reader_specs reader_specs;
3410 const char *filename;
3411
3412 /* If this compilation unit was already read in, free the
3413 cached copy in order to read it in again. This is
3414 necessary because we skipped some symbols when we first
3415 read in the compilation unit (see load_partial_dies).
3416 This problem could be avoided, but the benefit is
3417 unclear. */
3418 if (this_cu->cu != NULL)
3419 free_one_cached_comp_unit (this_cu->cu);
3420
3421 /* Note that this is a pointer to our stack frame, being
3422 added to a global data structure. It will be cleaned up
3423 in free_stack_comp_unit when we finish with this
3424 compilation unit. */
3425 init_one_comp_unit (&cu, this_cu);
3426 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3427
3428 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3429 is_debug_types_section);
3430
3431 /* Skip dummy compilation units. */
3432 if (info_ptr >= buffer + buffer_size
3433 || peek_abbrev_code (abfd, info_ptr) == 0)
3434 {
3435 do_cleanups (back_to_inner);
3436 return;
3437 }
3438
3439 cu.list_in_scope = &file_symbols;
3440
3441 /* Read the abbrevs for this compilation unit into a table. */
3442 dwarf2_read_abbrevs (&cu);
3443 make_cleanup (dwarf2_free_abbrev_table, &cu);
3444
3445 /* Read the compilation unit die. */
3446 init_cu_die_reader (&reader_specs, &cu);
3447 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3448 &has_children);
3449
3450 if (is_debug_types_section)
3451 {
3452 /* LENGTH has not been set yet for type units. */
3453 gdb_assert (this_cu->offset == cu.header.offset);
3454 this_cu->length = cu.header.length + cu.header.initial_length_size;
3455 }
3456 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3457 {
3458 do_cleanups (back_to_inner);
3459 return;
3460 }
3461
3462 prepare_one_comp_unit (&cu, comp_unit_die);
3463
3464 /* Allocate a new partial symbol table structure. */
3465 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3466 if (attr == NULL || !DW_STRING (attr))
3467 filename = "";
3468 else
3469 filename = DW_STRING (attr);
3470 pst = start_psymtab_common (objfile, objfile->section_offsets,
3471 filename,
3472 /* TEXTLOW and TEXTHIGH are set below. */
3473 0,
3474 objfile->global_psymbols.next,
3475 objfile->static_psymbols.next);
3476 pst->psymtabs_addrmap_supported = 1;
3477
3478 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3479 if (attr != NULL)
3480 pst->dirname = DW_STRING (attr);
3481
3482 pst->read_symtab_private = this_cu;
3483
3484 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3485
3486 /* Store the function that reads in the rest of the symbol table. */
3487 pst->read_symtab = dwarf2_psymtab_to_symtab;
3488
3489 this_cu->v.psymtab = pst;
3490
3491 dwarf2_find_base_address (comp_unit_die, &cu);
3492
3493 /* Possibly set the default values of LOWPC and HIGHPC from
3494 `DW_AT_ranges'. */
3495 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3496 &best_highpc, &cu, pst);
3497 if (has_pc_info == 1 && best_lowpc < best_highpc)
3498 /* Store the contiguous range if it is not empty; it can be empty for
3499 CUs with no code. */
3500 addrmap_set_empty (objfile->psymtabs_addrmap,
3501 best_lowpc + baseaddr,
3502 best_highpc + baseaddr - 1, pst);
3503
3504 /* Check if comp unit has_children.
3505 If so, read the rest of the partial symbols from this comp unit.
3506 If not, there's no more debug_info for this comp unit. */
3507 if (has_children)
3508 {
3509 struct partial_die_info *first_die;
3510 CORE_ADDR lowpc, highpc;
3511
3512 lowpc = ((CORE_ADDR) -1);
3513 highpc = ((CORE_ADDR) 0);
3514
3515 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3516
3517 scan_partial_symbols (first_die, &lowpc, &highpc,
3518 ! has_pc_info, &cu);
3519
3520 /* If we didn't find a lowpc, set it to highpc to avoid
3521 complaints from `maint check'. */
3522 if (lowpc == ((CORE_ADDR) -1))
3523 lowpc = highpc;
3524
3525 /* If the compilation unit didn't have an explicit address range,
3526 then use the information extracted from its child dies. */
3527 if (! has_pc_info)
3528 {
3529 best_lowpc = lowpc;
3530 best_highpc = highpc;
3531 }
3532 }
3533 pst->textlow = best_lowpc + baseaddr;
3534 pst->texthigh = best_highpc + baseaddr;
3535
3536 pst->n_global_syms = objfile->global_psymbols.next -
3537 (objfile->global_psymbols.list + pst->globals_offset);
3538 pst->n_static_syms = objfile->static_psymbols.next -
3539 (objfile->static_psymbols.list + pst->statics_offset);
3540 sort_pst_symbols (pst);
3541
3542 if (is_debug_types_section)
3543 {
3544 /* It's not clear we want to do anything with stmt lists here.
3545 Waiting to see what gcc ultimately does. */
3546 }
3547 else
3548 {
3549 /* Get the list of files included in the current compilation unit,
3550 and build a psymtab for each of them. */
3551 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3552 }
3553
3554 do_cleanups (back_to_inner);
3555 }
3556
3557 /* Traversal function for htab_traverse_noresize.
3558 Process one .debug_types comp-unit. */
3559
3560 static int
3561 process_type_comp_unit (void **slot, void *info)
3562 {
3563 struct signatured_type *entry = (struct signatured_type *) *slot;
3564 struct dwarf2_per_cu_data *this_cu;
3565
3566 gdb_assert (info == NULL);
3567 this_cu = &entry->per_cu;
3568
3569 gdb_assert (this_cu->debug_types_section->readin);
3570 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3571
3572 return 1;
3573 }
3574
3575 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3576 Build partial symbol tables for the .debug_types comp-units. */
3577
3578 static void
3579 build_type_psymtabs (struct objfile *objfile)
3580 {
3581 if (! create_debug_types_hash_table (objfile))
3582 return;
3583
3584 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3585 process_type_comp_unit, NULL);
3586 }
3587
3588 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3589
3590 static void
3591 psymtabs_addrmap_cleanup (void *o)
3592 {
3593 struct objfile *objfile = o;
3594
3595 objfile->psymtabs_addrmap = NULL;
3596 }
3597
3598 /* Build the partial symbol table by doing a quick pass through the
3599 .debug_info and .debug_abbrev sections. */
3600
3601 static void
3602 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3603 {
3604 struct cleanup *back_to, *addrmap_cleanup;
3605 struct obstack temp_obstack;
3606 int i;
3607
3608 dwarf2_per_objfile->reading_partial_symbols = 1;
3609
3610 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3611
3612 /* Any cached compilation units will be linked by the per-objfile
3613 read_in_chain. Make sure to free them when we're done. */
3614 back_to = make_cleanup (free_cached_comp_units, NULL);
3615
3616 build_type_psymtabs (objfile);
3617
3618 create_all_comp_units (objfile);
3619
3620 /* Create a temporary address map on a temporary obstack. We later
3621 copy this to the final obstack. */
3622 obstack_init (&temp_obstack);
3623 make_cleanup_obstack_free (&temp_obstack);
3624 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3625 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3626
3627 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3628 {
3629 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3630
3631 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3632 }
3633
3634 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3635 &objfile->objfile_obstack);
3636 discard_cleanups (addrmap_cleanup);
3637
3638 do_cleanups (back_to);
3639 }
3640
3641 /* Load the partial DIEs for a secondary CU into memory. */
3642
3643 static void
3644 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3645 {
3646 struct objfile *objfile = this_cu->objfile;
3647 bfd *abfd = objfile->obfd;
3648 gdb_byte *info_ptr;
3649 struct die_info *comp_unit_die;
3650 struct dwarf2_cu *cu;
3651 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3652 int has_children;
3653 struct die_reader_specs reader_specs;
3654 int read_cu = 0;
3655 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
3656
3657 gdb_assert (! this_cu->debug_types_section);
3658
3659 gdb_assert (section->readin);
3660 info_ptr = section->buffer + this_cu->offset;
3661
3662 if (this_cu->cu == NULL)
3663 {
3664 cu = xmalloc (sizeof (*cu));
3665 init_one_comp_unit (cu, this_cu);
3666
3667 read_cu = 1;
3668
3669 /* If an error occurs while loading, release our storage. */
3670 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3671
3672 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3673 0);
3674
3675 /* Skip dummy compilation units. */
3676 if (info_ptr >= (section->buffer + section->size)
3677 || peek_abbrev_code (abfd, info_ptr) == 0)
3678 {
3679 do_cleanups (free_cu_cleanup);
3680 return;
3681 }
3682
3683 /* Link this CU into read_in_chain. */
3684 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3685 dwarf2_per_objfile->read_in_chain = this_cu;
3686 }
3687 else
3688 {
3689 cu = this_cu->cu;
3690 info_ptr += cu->header.first_die_offset;
3691 }
3692
3693 /* Read the abbrevs for this compilation unit into a table. */
3694 gdb_assert (cu->dwarf2_abbrevs == NULL);
3695 dwarf2_read_abbrevs (cu);
3696 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3697
3698 /* Read the compilation unit die. */
3699 init_cu_die_reader (&reader_specs, cu);
3700 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3701 &has_children);
3702
3703 prepare_one_comp_unit (cu, comp_unit_die);
3704
3705 /* Check if comp unit has_children.
3706 If so, read the rest of the partial symbols from this comp unit.
3707 If not, there's no more debug_info for this comp unit. */
3708 if (has_children)
3709 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
3710
3711 do_cleanups (free_abbrevs_cleanup);
3712
3713 if (read_cu)
3714 {
3715 /* We've successfully allocated this compilation unit. Let our
3716 caller clean it up when finished with it. */
3717 discard_cleanups (free_cu_cleanup);
3718 }
3719 }
3720
3721 /* Create a list of all compilation units in OBJFILE.
3722 This is only done for -readnow and building partial symtabs. */
3723
3724 static void
3725 create_all_comp_units (struct objfile *objfile)
3726 {
3727 int n_allocated;
3728 int n_comp_units;
3729 struct dwarf2_per_cu_data **all_comp_units;
3730 gdb_byte *info_ptr;
3731
3732 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3733 info_ptr = dwarf2_per_objfile->info.buffer;
3734
3735 n_comp_units = 0;
3736 n_allocated = 10;
3737 all_comp_units = xmalloc (n_allocated
3738 * sizeof (struct dwarf2_per_cu_data *));
3739
3740 while (info_ptr < dwarf2_per_objfile->info.buffer
3741 + dwarf2_per_objfile->info.size)
3742 {
3743 unsigned int length, initial_length_size;
3744 struct dwarf2_per_cu_data *this_cu;
3745 unsigned int offset;
3746
3747 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3748
3749 /* Read just enough information to find out where the next
3750 compilation unit is. */
3751 length = read_initial_length (objfile->obfd, info_ptr,
3752 &initial_length_size);
3753
3754 /* Save the compilation unit for later lookup. */
3755 this_cu = obstack_alloc (&objfile->objfile_obstack,
3756 sizeof (struct dwarf2_per_cu_data));
3757 memset (this_cu, 0, sizeof (*this_cu));
3758 this_cu->offset = offset;
3759 this_cu->length = length + initial_length_size;
3760 this_cu->objfile = objfile;
3761
3762 if (n_comp_units == n_allocated)
3763 {
3764 n_allocated *= 2;
3765 all_comp_units = xrealloc (all_comp_units,
3766 n_allocated
3767 * sizeof (struct dwarf2_per_cu_data *));
3768 }
3769 all_comp_units[n_comp_units++] = this_cu;
3770
3771 info_ptr = info_ptr + this_cu->length;
3772 }
3773
3774 dwarf2_per_objfile->all_comp_units
3775 = obstack_alloc (&objfile->objfile_obstack,
3776 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3777 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3778 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3779 xfree (all_comp_units);
3780 dwarf2_per_objfile->n_comp_units = n_comp_units;
3781 }
3782
3783 /* Process all loaded DIEs for compilation unit CU, starting at
3784 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3785 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3786 DW_AT_ranges). If NEED_PC is set, then this function will set
3787 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3788 and record the covered ranges in the addrmap. */
3789
3790 static void
3791 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3792 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3793 {
3794 struct partial_die_info *pdi;
3795
3796 /* Now, march along the PDI's, descending into ones which have
3797 interesting children but skipping the children of the other ones,
3798 until we reach the end of the compilation unit. */
3799
3800 pdi = first_die;
3801
3802 while (pdi != NULL)
3803 {
3804 fixup_partial_die (pdi, cu);
3805
3806 /* Anonymous namespaces or modules have no name but have interesting
3807 children, so we need to look at them. Ditto for anonymous
3808 enums. */
3809
3810 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3811 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3812 {
3813 switch (pdi->tag)
3814 {
3815 case DW_TAG_subprogram:
3816 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3817 break;
3818 case DW_TAG_constant:
3819 case DW_TAG_variable:
3820 case DW_TAG_typedef:
3821 case DW_TAG_union_type:
3822 if (!pdi->is_declaration)
3823 {
3824 add_partial_symbol (pdi, cu);
3825 }
3826 break;
3827 case DW_TAG_class_type:
3828 case DW_TAG_interface_type:
3829 case DW_TAG_structure_type:
3830 if (!pdi->is_declaration)
3831 {
3832 add_partial_symbol (pdi, cu);
3833 }
3834 break;
3835 case DW_TAG_enumeration_type:
3836 if (!pdi->is_declaration)
3837 add_partial_enumeration (pdi, cu);
3838 break;
3839 case DW_TAG_base_type:
3840 case DW_TAG_subrange_type:
3841 /* File scope base type definitions are added to the partial
3842 symbol table. */
3843 add_partial_symbol (pdi, cu);
3844 break;
3845 case DW_TAG_namespace:
3846 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3847 break;
3848 case DW_TAG_module:
3849 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3850 break;
3851 default:
3852 break;
3853 }
3854 }
3855
3856 /* If the die has a sibling, skip to the sibling. */
3857
3858 pdi = pdi->die_sibling;
3859 }
3860 }
3861
3862 /* Functions used to compute the fully scoped name of a partial DIE.
3863
3864 Normally, this is simple. For C++, the parent DIE's fully scoped
3865 name is concatenated with "::" and the partial DIE's name. For
3866 Java, the same thing occurs except that "." is used instead of "::".
3867 Enumerators are an exception; they use the scope of their parent
3868 enumeration type, i.e. the name of the enumeration type is not
3869 prepended to the enumerator.
3870
3871 There are two complexities. One is DW_AT_specification; in this
3872 case "parent" means the parent of the target of the specification,
3873 instead of the direct parent of the DIE. The other is compilers
3874 which do not emit DW_TAG_namespace; in this case we try to guess
3875 the fully qualified name of structure types from their members'
3876 linkage names. This must be done using the DIE's children rather
3877 than the children of any DW_AT_specification target. We only need
3878 to do this for structures at the top level, i.e. if the target of
3879 any DW_AT_specification (if any; otherwise the DIE itself) does not
3880 have a parent. */
3881
3882 /* Compute the scope prefix associated with PDI's parent, in
3883 compilation unit CU. The result will be allocated on CU's
3884 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3885 field. NULL is returned if no prefix is necessary. */
3886 static char *
3887 partial_die_parent_scope (struct partial_die_info *pdi,
3888 struct dwarf2_cu *cu)
3889 {
3890 char *grandparent_scope;
3891 struct partial_die_info *parent, *real_pdi;
3892
3893 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3894 then this means the parent of the specification DIE. */
3895
3896 real_pdi = pdi;
3897 while (real_pdi->has_specification)
3898 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3899
3900 parent = real_pdi->die_parent;
3901 if (parent == NULL)
3902 return NULL;
3903
3904 if (parent->scope_set)
3905 return parent->scope;
3906
3907 fixup_partial_die (parent, cu);
3908
3909 grandparent_scope = partial_die_parent_scope (parent, cu);
3910
3911 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3912 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3913 Work around this problem here. */
3914 if (cu->language == language_cplus
3915 && parent->tag == DW_TAG_namespace
3916 && strcmp (parent->name, "::") == 0
3917 && grandparent_scope == NULL)
3918 {
3919 parent->scope = NULL;
3920 parent->scope_set = 1;
3921 return NULL;
3922 }
3923
3924 if (pdi->tag == DW_TAG_enumerator)
3925 /* Enumerators should not get the name of the enumeration as a prefix. */
3926 parent->scope = grandparent_scope;
3927 else if (parent->tag == DW_TAG_namespace
3928 || parent->tag == DW_TAG_module
3929 || parent->tag == DW_TAG_structure_type
3930 || parent->tag == DW_TAG_class_type
3931 || parent->tag == DW_TAG_interface_type
3932 || parent->tag == DW_TAG_union_type
3933 || parent->tag == DW_TAG_enumeration_type)
3934 {
3935 if (grandparent_scope == NULL)
3936 parent->scope = parent->name;
3937 else
3938 parent->scope = typename_concat (&cu->comp_unit_obstack,
3939 grandparent_scope,
3940 parent->name, 0, cu);
3941 }
3942 else
3943 {
3944 /* FIXME drow/2004-04-01: What should we be doing with
3945 function-local names? For partial symbols, we should probably be
3946 ignoring them. */
3947 complaint (&symfile_complaints,
3948 _("unhandled containing DIE tag %d for DIE at %d"),
3949 parent->tag, pdi->offset);
3950 parent->scope = grandparent_scope;
3951 }
3952
3953 parent->scope_set = 1;
3954 return parent->scope;
3955 }
3956
3957 /* Return the fully scoped name associated with PDI, from compilation unit
3958 CU. The result will be allocated with malloc. */
3959 static char *
3960 partial_die_full_name (struct partial_die_info *pdi,
3961 struct dwarf2_cu *cu)
3962 {
3963 char *parent_scope;
3964
3965 /* If this is a template instantiation, we can not work out the
3966 template arguments from partial DIEs. So, unfortunately, we have
3967 to go through the full DIEs. At least any work we do building
3968 types here will be reused if full symbols are loaded later. */
3969 if (pdi->has_template_arguments)
3970 {
3971 fixup_partial_die (pdi, cu);
3972
3973 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3974 {
3975 struct die_info *die;
3976 struct attribute attr;
3977 struct dwarf2_cu *ref_cu = cu;
3978
3979 attr.name = 0;
3980 attr.form = DW_FORM_ref_addr;
3981 attr.u.addr = pdi->offset;
3982 die = follow_die_ref (NULL, &attr, &ref_cu);
3983
3984 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3985 }
3986 }
3987
3988 parent_scope = partial_die_parent_scope (pdi, cu);
3989 if (parent_scope == NULL)
3990 return NULL;
3991 else
3992 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3993 }
3994
3995 static void
3996 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3997 {
3998 struct objfile *objfile = cu->objfile;
3999 CORE_ADDR addr = 0;
4000 char *actual_name = NULL;
4001 CORE_ADDR baseaddr;
4002 int built_actual_name = 0;
4003
4004 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4005
4006 actual_name = partial_die_full_name (pdi, cu);
4007 if (actual_name)
4008 built_actual_name = 1;
4009
4010 if (actual_name == NULL)
4011 actual_name = pdi->name;
4012
4013 switch (pdi->tag)
4014 {
4015 case DW_TAG_subprogram:
4016 if (pdi->is_external || cu->language == language_ada)
4017 {
4018 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4019 of the global scope. But in Ada, we want to be able to access
4020 nested procedures globally. So all Ada subprograms are stored
4021 in the global scope. */
4022 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4023 mst_text, objfile); */
4024 add_psymbol_to_list (actual_name, strlen (actual_name),
4025 built_actual_name,
4026 VAR_DOMAIN, LOC_BLOCK,
4027 &objfile->global_psymbols,
4028 0, pdi->lowpc + baseaddr,
4029 cu->language, objfile);
4030 }
4031 else
4032 {
4033 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4034 mst_file_text, objfile); */
4035 add_psymbol_to_list (actual_name, strlen (actual_name),
4036 built_actual_name,
4037 VAR_DOMAIN, LOC_BLOCK,
4038 &objfile->static_psymbols,
4039 0, pdi->lowpc + baseaddr,
4040 cu->language, objfile);
4041 }
4042 break;
4043 case DW_TAG_constant:
4044 {
4045 struct psymbol_allocation_list *list;
4046
4047 if (pdi->is_external)
4048 list = &objfile->global_psymbols;
4049 else
4050 list = &objfile->static_psymbols;
4051 add_psymbol_to_list (actual_name, strlen (actual_name),
4052 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4053 list, 0, 0, cu->language, objfile);
4054 }
4055 break;
4056 case DW_TAG_variable:
4057 if (pdi->locdesc)
4058 addr = decode_locdesc (pdi->locdesc, cu);
4059
4060 if (pdi->locdesc
4061 && addr == 0
4062 && !dwarf2_per_objfile->has_section_at_zero)
4063 {
4064 /* A global or static variable may also have been stripped
4065 out by the linker if unused, in which case its address
4066 will be nullified; do not add such variables into partial
4067 symbol table then. */
4068 }
4069 else if (pdi->is_external)
4070 {
4071 /* Global Variable.
4072 Don't enter into the minimal symbol tables as there is
4073 a minimal symbol table entry from the ELF symbols already.
4074 Enter into partial symbol table if it has a location
4075 descriptor or a type.
4076 If the location descriptor is missing, new_symbol will create
4077 a LOC_UNRESOLVED symbol, the address of the variable will then
4078 be determined from the minimal symbol table whenever the variable
4079 is referenced.
4080 The address for the partial symbol table entry is not
4081 used by GDB, but it comes in handy for debugging partial symbol
4082 table building. */
4083
4084 if (pdi->locdesc || pdi->has_type)
4085 add_psymbol_to_list (actual_name, strlen (actual_name),
4086 built_actual_name,
4087 VAR_DOMAIN, LOC_STATIC,
4088 &objfile->global_psymbols,
4089 0, addr + baseaddr,
4090 cu->language, objfile);
4091 }
4092 else
4093 {
4094 /* Static Variable. Skip symbols without location descriptors. */
4095 if (pdi->locdesc == NULL)
4096 {
4097 if (built_actual_name)
4098 xfree (actual_name);
4099 return;
4100 }
4101 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4102 mst_file_data, objfile); */
4103 add_psymbol_to_list (actual_name, strlen (actual_name),
4104 built_actual_name,
4105 VAR_DOMAIN, LOC_STATIC,
4106 &objfile->static_psymbols,
4107 0, addr + baseaddr,
4108 cu->language, objfile);
4109 }
4110 break;
4111 case DW_TAG_typedef:
4112 case DW_TAG_base_type:
4113 case DW_TAG_subrange_type:
4114 add_psymbol_to_list (actual_name, strlen (actual_name),
4115 built_actual_name,
4116 VAR_DOMAIN, LOC_TYPEDEF,
4117 &objfile->static_psymbols,
4118 0, (CORE_ADDR) 0, cu->language, objfile);
4119 break;
4120 case DW_TAG_namespace:
4121 add_psymbol_to_list (actual_name, strlen (actual_name),
4122 built_actual_name,
4123 VAR_DOMAIN, LOC_TYPEDEF,
4124 &objfile->global_psymbols,
4125 0, (CORE_ADDR) 0, cu->language, objfile);
4126 break;
4127 case DW_TAG_class_type:
4128 case DW_TAG_interface_type:
4129 case DW_TAG_structure_type:
4130 case DW_TAG_union_type:
4131 case DW_TAG_enumeration_type:
4132 /* Skip external references. The DWARF standard says in the section
4133 about "Structure, Union, and Class Type Entries": "An incomplete
4134 structure, union or class type is represented by a structure,
4135 union or class entry that does not have a byte size attribute
4136 and that has a DW_AT_declaration attribute." */
4137 if (!pdi->has_byte_size && pdi->is_declaration)
4138 {
4139 if (built_actual_name)
4140 xfree (actual_name);
4141 return;
4142 }
4143
4144 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4145 static vs. global. */
4146 add_psymbol_to_list (actual_name, strlen (actual_name),
4147 built_actual_name,
4148 STRUCT_DOMAIN, LOC_TYPEDEF,
4149 (cu->language == language_cplus
4150 || cu->language == language_java)
4151 ? &objfile->global_psymbols
4152 : &objfile->static_psymbols,
4153 0, (CORE_ADDR) 0, cu->language, objfile);
4154
4155 break;
4156 case DW_TAG_enumerator:
4157 add_psymbol_to_list (actual_name, strlen (actual_name),
4158 built_actual_name,
4159 VAR_DOMAIN, LOC_CONST,
4160 (cu->language == language_cplus
4161 || cu->language == language_java)
4162 ? &objfile->global_psymbols
4163 : &objfile->static_psymbols,
4164 0, (CORE_ADDR) 0, cu->language, objfile);
4165 break;
4166 default:
4167 break;
4168 }
4169
4170 if (built_actual_name)
4171 xfree (actual_name);
4172 }
4173
4174 /* Read a partial die corresponding to a namespace; also, add a symbol
4175 corresponding to that namespace to the symbol table. NAMESPACE is
4176 the name of the enclosing namespace. */
4177
4178 static void
4179 add_partial_namespace (struct partial_die_info *pdi,
4180 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4181 int need_pc, struct dwarf2_cu *cu)
4182 {
4183 /* Add a symbol for the namespace. */
4184
4185 add_partial_symbol (pdi, cu);
4186
4187 /* Now scan partial symbols in that namespace. */
4188
4189 if (pdi->has_children)
4190 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4191 }
4192
4193 /* Read a partial die corresponding to a Fortran module. */
4194
4195 static void
4196 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4197 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4198 {
4199 /* Now scan partial symbols in that module. */
4200
4201 if (pdi->has_children)
4202 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4203 }
4204
4205 /* Read a partial die corresponding to a subprogram and create a partial
4206 symbol for that subprogram. When the CU language allows it, this
4207 routine also defines a partial symbol for each nested subprogram
4208 that this subprogram contains.
4209
4210 DIE my also be a lexical block, in which case we simply search
4211 recursively for suprograms defined inside that lexical block.
4212 Again, this is only performed when the CU language allows this
4213 type of definitions. */
4214
4215 static void
4216 add_partial_subprogram (struct partial_die_info *pdi,
4217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4218 int need_pc, struct dwarf2_cu *cu)
4219 {
4220 if (pdi->tag == DW_TAG_subprogram)
4221 {
4222 if (pdi->has_pc_info)
4223 {
4224 if (pdi->lowpc < *lowpc)
4225 *lowpc = pdi->lowpc;
4226 if (pdi->highpc > *highpc)
4227 *highpc = pdi->highpc;
4228 if (need_pc)
4229 {
4230 CORE_ADDR baseaddr;
4231 struct objfile *objfile = cu->objfile;
4232
4233 baseaddr = ANOFFSET (objfile->section_offsets,
4234 SECT_OFF_TEXT (objfile));
4235 addrmap_set_empty (objfile->psymtabs_addrmap,
4236 pdi->lowpc + baseaddr,
4237 pdi->highpc - 1 + baseaddr,
4238 cu->per_cu->v.psymtab);
4239 }
4240 if (!pdi->is_declaration)
4241 /* Ignore subprogram DIEs that do not have a name, they are
4242 illegal. Do not emit a complaint at this point, we will
4243 do so when we convert this psymtab into a symtab. */
4244 if (pdi->name)
4245 add_partial_symbol (pdi, cu);
4246 }
4247 }
4248
4249 if (! pdi->has_children)
4250 return;
4251
4252 if (cu->language == language_ada)
4253 {
4254 pdi = pdi->die_child;
4255 while (pdi != NULL)
4256 {
4257 fixup_partial_die (pdi, cu);
4258 if (pdi->tag == DW_TAG_subprogram
4259 || pdi->tag == DW_TAG_lexical_block)
4260 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4261 pdi = pdi->die_sibling;
4262 }
4263 }
4264 }
4265
4266 /* Read a partial die corresponding to an enumeration type. */
4267
4268 static void
4269 add_partial_enumeration (struct partial_die_info *enum_pdi,
4270 struct dwarf2_cu *cu)
4271 {
4272 struct partial_die_info *pdi;
4273
4274 if (enum_pdi->name != NULL)
4275 add_partial_symbol (enum_pdi, cu);
4276
4277 pdi = enum_pdi->die_child;
4278 while (pdi)
4279 {
4280 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4281 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4282 else
4283 add_partial_symbol (pdi, cu);
4284 pdi = pdi->die_sibling;
4285 }
4286 }
4287
4288 /* Return the initial uleb128 in the die at INFO_PTR. */
4289
4290 static unsigned int
4291 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4292 {
4293 unsigned int bytes_read;
4294
4295 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4296 }
4297
4298 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4299 Return the corresponding abbrev, or NULL if the number is zero (indicating
4300 an empty DIE). In either case *BYTES_READ will be set to the length of
4301 the initial number. */
4302
4303 static struct abbrev_info *
4304 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4305 struct dwarf2_cu *cu)
4306 {
4307 bfd *abfd = cu->objfile->obfd;
4308 unsigned int abbrev_number;
4309 struct abbrev_info *abbrev;
4310
4311 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4312
4313 if (abbrev_number == 0)
4314 return NULL;
4315
4316 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4317 if (!abbrev)
4318 {
4319 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4320 abbrev_number, bfd_get_filename (abfd));
4321 }
4322
4323 return abbrev;
4324 }
4325
4326 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4327 Returns a pointer to the end of a series of DIEs, terminated by an empty
4328 DIE. Any children of the skipped DIEs will also be skipped. */
4329
4330 static gdb_byte *
4331 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4332 {
4333 struct abbrev_info *abbrev;
4334 unsigned int bytes_read;
4335
4336 while (1)
4337 {
4338 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4339 if (abbrev == NULL)
4340 return info_ptr + bytes_read;
4341 else
4342 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4343 }
4344 }
4345
4346 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4347 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4348 abbrev corresponding to that skipped uleb128 should be passed in
4349 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4350 children. */
4351
4352 static gdb_byte *
4353 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4354 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4355 {
4356 unsigned int bytes_read;
4357 struct attribute attr;
4358 bfd *abfd = cu->objfile->obfd;
4359 unsigned int form, i;
4360
4361 for (i = 0; i < abbrev->num_attrs; i++)
4362 {
4363 /* The only abbrev we care about is DW_AT_sibling. */
4364 if (abbrev->attrs[i].name == DW_AT_sibling)
4365 {
4366 read_attribute (&attr, &abbrev->attrs[i],
4367 abfd, info_ptr, cu);
4368 if (attr.form == DW_FORM_ref_addr)
4369 complaint (&symfile_complaints,
4370 _("ignoring absolute DW_AT_sibling"));
4371 else
4372 return buffer + dwarf2_get_ref_die_offset (&attr);
4373 }
4374
4375 /* If it isn't DW_AT_sibling, skip this attribute. */
4376 form = abbrev->attrs[i].form;
4377 skip_attribute:
4378 switch (form)
4379 {
4380 case DW_FORM_ref_addr:
4381 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4382 and later it is offset sized. */
4383 if (cu->header.version == 2)
4384 info_ptr += cu->header.addr_size;
4385 else
4386 info_ptr += cu->header.offset_size;
4387 break;
4388 case DW_FORM_addr:
4389 info_ptr += cu->header.addr_size;
4390 break;
4391 case DW_FORM_data1:
4392 case DW_FORM_ref1:
4393 case DW_FORM_flag:
4394 info_ptr += 1;
4395 break;
4396 case DW_FORM_flag_present:
4397 break;
4398 case DW_FORM_data2:
4399 case DW_FORM_ref2:
4400 info_ptr += 2;
4401 break;
4402 case DW_FORM_data4:
4403 case DW_FORM_ref4:
4404 info_ptr += 4;
4405 break;
4406 case DW_FORM_data8:
4407 case DW_FORM_ref8:
4408 case DW_FORM_ref_sig8:
4409 info_ptr += 8;
4410 break;
4411 case DW_FORM_string:
4412 read_direct_string (abfd, info_ptr, &bytes_read);
4413 info_ptr += bytes_read;
4414 break;
4415 case DW_FORM_sec_offset:
4416 case DW_FORM_strp:
4417 info_ptr += cu->header.offset_size;
4418 break;
4419 case DW_FORM_exprloc:
4420 case DW_FORM_block:
4421 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4422 info_ptr += bytes_read;
4423 break;
4424 case DW_FORM_block1:
4425 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4426 break;
4427 case DW_FORM_block2:
4428 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4429 break;
4430 case DW_FORM_block4:
4431 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4432 break;
4433 case DW_FORM_sdata:
4434 case DW_FORM_udata:
4435 case DW_FORM_ref_udata:
4436 info_ptr = skip_leb128 (abfd, info_ptr);
4437 break;
4438 case DW_FORM_indirect:
4439 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4440 info_ptr += bytes_read;
4441 /* We need to continue parsing from here, so just go back to
4442 the top. */
4443 goto skip_attribute;
4444
4445 default:
4446 error (_("Dwarf Error: Cannot handle %s "
4447 "in DWARF reader [in module %s]"),
4448 dwarf_form_name (form),
4449 bfd_get_filename (abfd));
4450 }
4451 }
4452
4453 if (abbrev->has_children)
4454 return skip_children (buffer, info_ptr, cu);
4455 else
4456 return info_ptr;
4457 }
4458
4459 /* Locate ORIG_PDI's sibling.
4460 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4461 in BUFFER. */
4462
4463 static gdb_byte *
4464 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4465 gdb_byte *buffer, gdb_byte *info_ptr,
4466 bfd *abfd, struct dwarf2_cu *cu)
4467 {
4468 /* Do we know the sibling already? */
4469
4470 if (orig_pdi->sibling)
4471 return orig_pdi->sibling;
4472
4473 /* Are there any children to deal with? */
4474
4475 if (!orig_pdi->has_children)
4476 return info_ptr;
4477
4478 /* Skip the children the long way. */
4479
4480 return skip_children (buffer, info_ptr, cu);
4481 }
4482
4483 /* Expand this partial symbol table into a full symbol table. */
4484
4485 static void
4486 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4487 {
4488 if (pst != NULL)
4489 {
4490 if (pst->readin)
4491 {
4492 warning (_("bug: psymtab for %s is already read in."),
4493 pst->filename);
4494 }
4495 else
4496 {
4497 if (info_verbose)
4498 {
4499 printf_filtered (_("Reading in symbols for %s..."),
4500 pst->filename);
4501 gdb_flush (gdb_stdout);
4502 }
4503
4504 /* Restore our global data. */
4505 dwarf2_per_objfile = objfile_data (pst->objfile,
4506 dwarf2_objfile_data_key);
4507
4508 /* If this psymtab is constructed from a debug-only objfile, the
4509 has_section_at_zero flag will not necessarily be correct. We
4510 can get the correct value for this flag by looking at the data
4511 associated with the (presumably stripped) associated objfile. */
4512 if (pst->objfile->separate_debug_objfile_backlink)
4513 {
4514 struct dwarf2_per_objfile *dpo_backlink
4515 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4516 dwarf2_objfile_data_key);
4517
4518 dwarf2_per_objfile->has_section_at_zero
4519 = dpo_backlink->has_section_at_zero;
4520 }
4521
4522 dwarf2_per_objfile->reading_partial_symbols = 0;
4523
4524 psymtab_to_symtab_1 (pst);
4525
4526 /* Finish up the debug error message. */
4527 if (info_verbose)
4528 printf_filtered (_("done.\n"));
4529 }
4530 }
4531 }
4532 \f
4533 /* Reading in full CUs. */
4534
4535 /* Add PER_CU to the queue. */
4536
4537 static void
4538 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4539 {
4540 struct dwarf2_queue_item *item;
4541
4542 per_cu->queued = 1;
4543 item = xmalloc (sizeof (*item));
4544 item->per_cu = per_cu;
4545 item->next = NULL;
4546
4547 if (dwarf2_queue == NULL)
4548 dwarf2_queue = item;
4549 else
4550 dwarf2_queue_tail->next = item;
4551
4552 dwarf2_queue_tail = item;
4553 }
4554
4555 /* Process the queue. */
4556
4557 static void
4558 process_queue (void)
4559 {
4560 struct dwarf2_queue_item *item, *next_item;
4561
4562 /* The queue starts out with one item, but following a DIE reference
4563 may load a new CU, adding it to the end of the queue. */
4564 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4565 {
4566 if (dwarf2_per_objfile->using_index
4567 ? !item->per_cu->v.quick->symtab
4568 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4569 process_full_comp_unit (item->per_cu);
4570
4571 item->per_cu->queued = 0;
4572 next_item = item->next;
4573 xfree (item);
4574 }
4575
4576 dwarf2_queue_tail = NULL;
4577 }
4578
4579 /* Free all allocated queue entries. This function only releases anything if
4580 an error was thrown; if the queue was processed then it would have been
4581 freed as we went along. */
4582
4583 static void
4584 dwarf2_release_queue (void *dummy)
4585 {
4586 struct dwarf2_queue_item *item, *last;
4587
4588 item = dwarf2_queue;
4589 while (item)
4590 {
4591 /* Anything still marked queued is likely to be in an
4592 inconsistent state, so discard it. */
4593 if (item->per_cu->queued)
4594 {
4595 if (item->per_cu->cu != NULL)
4596 free_one_cached_comp_unit (item->per_cu->cu);
4597 item->per_cu->queued = 0;
4598 }
4599
4600 last = item;
4601 item = item->next;
4602 xfree (last);
4603 }
4604
4605 dwarf2_queue = dwarf2_queue_tail = NULL;
4606 }
4607
4608 /* Read in full symbols for PST, and anything it depends on. */
4609
4610 static void
4611 psymtab_to_symtab_1 (struct partial_symtab *pst)
4612 {
4613 struct dwarf2_per_cu_data *per_cu;
4614 struct cleanup *back_to;
4615 int i;
4616
4617 for (i = 0; i < pst->number_of_dependencies; i++)
4618 if (!pst->dependencies[i]->readin)
4619 {
4620 /* Inform about additional files that need to be read in. */
4621 if (info_verbose)
4622 {
4623 /* FIXME: i18n: Need to make this a single string. */
4624 fputs_filtered (" ", gdb_stdout);
4625 wrap_here ("");
4626 fputs_filtered ("and ", gdb_stdout);
4627 wrap_here ("");
4628 printf_filtered ("%s...", pst->dependencies[i]->filename);
4629 wrap_here (""); /* Flush output. */
4630 gdb_flush (gdb_stdout);
4631 }
4632 psymtab_to_symtab_1 (pst->dependencies[i]);
4633 }
4634
4635 per_cu = pst->read_symtab_private;
4636
4637 if (per_cu == NULL)
4638 {
4639 /* It's an include file, no symbols to read for it.
4640 Everything is in the parent symtab. */
4641 pst->readin = 1;
4642 return;
4643 }
4644
4645 dw2_do_instantiate_symtab (per_cu);
4646 }
4647
4648 /* Load the DIEs associated with PER_CU into memory. */
4649
4650 static void
4651 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4652 {
4653 struct objfile *objfile = per_cu->objfile;
4654 bfd *abfd = objfile->obfd;
4655 struct dwarf2_cu *cu;
4656 unsigned int offset;
4657 gdb_byte *info_ptr, *beg_of_comp_unit;
4658 struct cleanup *free_cu_cleanup = NULL;
4659 struct attribute *attr;
4660 int read_cu = 0;
4661
4662 gdb_assert (! per_cu->debug_types_section);
4663
4664 /* Set local variables from the partial symbol table info. */
4665 offset = per_cu->offset;
4666
4667 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4668 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4669 beg_of_comp_unit = info_ptr;
4670
4671 if (per_cu->cu == NULL)
4672 {
4673 cu = xmalloc (sizeof (*cu));
4674 init_one_comp_unit (cu, per_cu);
4675
4676 read_cu = 1;
4677
4678 /* If an error occurs while loading, release our storage. */
4679 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4680
4681 /* Read in the comp_unit header. */
4682 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4683
4684 /* Skip dummy compilation units. */
4685 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4686 + dwarf2_per_objfile->info.size)
4687 || peek_abbrev_code (abfd, info_ptr) == 0)
4688 {
4689 do_cleanups (free_cu_cleanup);
4690 return;
4691 }
4692
4693 /* Complete the cu_header. */
4694 cu->header.offset = offset;
4695 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4696
4697 /* Link this CU into read_in_chain. */
4698 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4699 dwarf2_per_objfile->read_in_chain = per_cu;
4700 }
4701 else
4702 {
4703 cu = per_cu->cu;
4704 info_ptr += cu->header.first_die_offset;
4705 }
4706
4707 cu->dies = read_comp_unit (info_ptr, cu);
4708
4709 /* We try not to read any attributes in this function, because not
4710 all CUs needed for references have been loaded yet, and symbol
4711 table processing isn't initialized. But we have to set the CU language,
4712 or we won't be able to build types correctly. */
4713 prepare_one_comp_unit (cu, cu->dies);
4714
4715 /* Similarly, if we do not read the producer, we can not apply
4716 producer-specific interpretation. */
4717 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4718 if (attr)
4719 cu->producer = DW_STRING (attr);
4720
4721 if (read_cu)
4722 {
4723 /* We've successfully allocated this compilation unit. Let our
4724 caller clean it up when finished with it. */
4725 discard_cleanups (free_cu_cleanup);
4726 }
4727 }
4728
4729 /* Add a DIE to the delayed physname list. */
4730
4731 static void
4732 add_to_method_list (struct type *type, int fnfield_index, int index,
4733 const char *name, struct die_info *die,
4734 struct dwarf2_cu *cu)
4735 {
4736 struct delayed_method_info mi;
4737 mi.type = type;
4738 mi.fnfield_index = fnfield_index;
4739 mi.index = index;
4740 mi.name = name;
4741 mi.die = die;
4742 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4743 }
4744
4745 /* A cleanup for freeing the delayed method list. */
4746
4747 static void
4748 free_delayed_list (void *ptr)
4749 {
4750 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4751 if (cu->method_list != NULL)
4752 {
4753 VEC_free (delayed_method_info, cu->method_list);
4754 cu->method_list = NULL;
4755 }
4756 }
4757
4758 /* Compute the physnames of any methods on the CU's method list.
4759
4760 The computation of method physnames is delayed in order to avoid the
4761 (bad) condition that one of the method's formal parameters is of an as yet
4762 incomplete type. */
4763
4764 static void
4765 compute_delayed_physnames (struct dwarf2_cu *cu)
4766 {
4767 int i;
4768 struct delayed_method_info *mi;
4769 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4770 {
4771 const char *physname;
4772 struct fn_fieldlist *fn_flp
4773 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4774 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4775 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4776 }
4777 }
4778
4779 /* Generate full symbol information for PER_CU, whose DIEs have
4780 already been loaded into memory. */
4781
4782 static void
4783 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4784 {
4785 struct dwarf2_cu *cu = per_cu->cu;
4786 struct objfile *objfile = per_cu->objfile;
4787 CORE_ADDR lowpc, highpc;
4788 struct symtab *symtab;
4789 struct cleanup *back_to, *delayed_list_cleanup;
4790 CORE_ADDR baseaddr;
4791
4792 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4793
4794 buildsym_init ();
4795 back_to = make_cleanup (really_free_pendings, NULL);
4796 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4797
4798 cu->list_in_scope = &file_symbols;
4799
4800 /* Do line number decoding in read_file_scope () */
4801 process_die (cu->dies, cu);
4802
4803 /* Now that we have processed all the DIEs in the CU, all the types
4804 should be complete, and it should now be safe to compute all of the
4805 physnames. */
4806 compute_delayed_physnames (cu);
4807 do_cleanups (delayed_list_cleanup);
4808
4809 /* Some compilers don't define a DW_AT_high_pc attribute for the
4810 compilation unit. If the DW_AT_high_pc is missing, synthesize
4811 it, by scanning the DIE's below the compilation unit. */
4812 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4813
4814 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4815
4816 if (symtab != NULL)
4817 {
4818 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4819
4820 /* Set symtab language to language from DW_AT_language. If the
4821 compilation is from a C file generated by language preprocessors, do
4822 not set the language if it was already deduced by start_subfile. */
4823 if (!(cu->language == language_c && symtab->language != language_c))
4824 symtab->language = cu->language;
4825
4826 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4827 produce DW_AT_location with location lists but it can be possibly
4828 invalid without -fvar-tracking.
4829
4830 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4831 needed, it would be wrong due to missing DW_AT_producer there.
4832
4833 Still one can confuse GDB by using non-standard GCC compilation
4834 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4835 */
4836 if (cu->has_loclist && gcc_4_minor >= 0)
4837 symtab->locations_valid = 1;
4838
4839 if (gcc_4_minor >= 5)
4840 symtab->epilogue_unwind_valid = 1;
4841
4842 symtab->call_site_htab = cu->call_site_htab;
4843 }
4844
4845 if (dwarf2_per_objfile->using_index)
4846 per_cu->v.quick->symtab = symtab;
4847 else
4848 {
4849 struct partial_symtab *pst = per_cu->v.psymtab;
4850 pst->symtab = symtab;
4851 pst->readin = 1;
4852 }
4853
4854 do_cleanups (back_to);
4855 }
4856
4857 /* Process a die and its children. */
4858
4859 static void
4860 process_die (struct die_info *die, struct dwarf2_cu *cu)
4861 {
4862 switch (die->tag)
4863 {
4864 case DW_TAG_padding:
4865 break;
4866 case DW_TAG_compile_unit:
4867 read_file_scope (die, cu);
4868 break;
4869 case DW_TAG_type_unit:
4870 read_type_unit_scope (die, cu);
4871 break;
4872 case DW_TAG_subprogram:
4873 case DW_TAG_inlined_subroutine:
4874 read_func_scope (die, cu);
4875 break;
4876 case DW_TAG_lexical_block:
4877 case DW_TAG_try_block:
4878 case DW_TAG_catch_block:
4879 read_lexical_block_scope (die, cu);
4880 break;
4881 case DW_TAG_GNU_call_site:
4882 read_call_site_scope (die, cu);
4883 break;
4884 case DW_TAG_class_type:
4885 case DW_TAG_interface_type:
4886 case DW_TAG_structure_type:
4887 case DW_TAG_union_type:
4888 process_structure_scope (die, cu);
4889 break;
4890 case DW_TAG_enumeration_type:
4891 process_enumeration_scope (die, cu);
4892 break;
4893
4894 /* These dies have a type, but processing them does not create
4895 a symbol or recurse to process the children. Therefore we can
4896 read them on-demand through read_type_die. */
4897 case DW_TAG_subroutine_type:
4898 case DW_TAG_set_type:
4899 case DW_TAG_array_type:
4900 case DW_TAG_pointer_type:
4901 case DW_TAG_ptr_to_member_type:
4902 case DW_TAG_reference_type:
4903 case DW_TAG_string_type:
4904 break;
4905
4906 case DW_TAG_base_type:
4907 case DW_TAG_subrange_type:
4908 case DW_TAG_typedef:
4909 /* Add a typedef symbol for the type definition, if it has a
4910 DW_AT_name. */
4911 new_symbol (die, read_type_die (die, cu), cu);
4912 break;
4913 case DW_TAG_common_block:
4914 read_common_block (die, cu);
4915 break;
4916 case DW_TAG_common_inclusion:
4917 break;
4918 case DW_TAG_namespace:
4919 processing_has_namespace_info = 1;
4920 read_namespace (die, cu);
4921 break;
4922 case DW_TAG_module:
4923 processing_has_namespace_info = 1;
4924 read_module (die, cu);
4925 break;
4926 case DW_TAG_imported_declaration:
4927 case DW_TAG_imported_module:
4928 processing_has_namespace_info = 1;
4929 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4930 || cu->language != language_fortran))
4931 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4932 dwarf_tag_name (die->tag));
4933 read_import_statement (die, cu);
4934 break;
4935 default:
4936 new_symbol (die, NULL, cu);
4937 break;
4938 }
4939 }
4940
4941 /* A helper function for dwarf2_compute_name which determines whether DIE
4942 needs to have the name of the scope prepended to the name listed in the
4943 die. */
4944
4945 static int
4946 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4947 {
4948 struct attribute *attr;
4949
4950 switch (die->tag)
4951 {
4952 case DW_TAG_namespace:
4953 case DW_TAG_typedef:
4954 case DW_TAG_class_type:
4955 case DW_TAG_interface_type:
4956 case DW_TAG_structure_type:
4957 case DW_TAG_union_type:
4958 case DW_TAG_enumeration_type:
4959 case DW_TAG_enumerator:
4960 case DW_TAG_subprogram:
4961 case DW_TAG_member:
4962 return 1;
4963
4964 case DW_TAG_variable:
4965 case DW_TAG_constant:
4966 /* We only need to prefix "globally" visible variables. These include
4967 any variable marked with DW_AT_external or any variable that
4968 lives in a namespace. [Variables in anonymous namespaces
4969 require prefixing, but they are not DW_AT_external.] */
4970
4971 if (dwarf2_attr (die, DW_AT_specification, cu))
4972 {
4973 struct dwarf2_cu *spec_cu = cu;
4974
4975 return die_needs_namespace (die_specification (die, &spec_cu),
4976 spec_cu);
4977 }
4978
4979 attr = dwarf2_attr (die, DW_AT_external, cu);
4980 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4981 && die->parent->tag != DW_TAG_module)
4982 return 0;
4983 /* A variable in a lexical block of some kind does not need a
4984 namespace, even though in C++ such variables may be external
4985 and have a mangled name. */
4986 if (die->parent->tag == DW_TAG_lexical_block
4987 || die->parent->tag == DW_TAG_try_block
4988 || die->parent->tag == DW_TAG_catch_block
4989 || die->parent->tag == DW_TAG_subprogram)
4990 return 0;
4991 return 1;
4992
4993 default:
4994 return 0;
4995 }
4996 }
4997
4998 /* Retrieve the last character from a mem_file. */
4999
5000 static void
5001 do_ui_file_peek_last (void *object, const char *buffer, long length)
5002 {
5003 char *last_char_p = (char *) object;
5004
5005 if (length > 0)
5006 *last_char_p = buffer[length - 1];
5007 }
5008
5009 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5010 compute the physname for the object, which include a method's
5011 formal parameters (C++/Java) and return type (Java).
5012
5013 For Ada, return the DIE's linkage name rather than the fully qualified
5014 name. PHYSNAME is ignored..
5015
5016 The result is allocated on the objfile_obstack and canonicalized. */
5017
5018 static const char *
5019 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5020 int physname)
5021 {
5022 struct objfile *objfile = cu->objfile;
5023
5024 if (name == NULL)
5025 name = dwarf2_name (die, cu);
5026
5027 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5028 compute it by typename_concat inside GDB. */
5029 if (cu->language == language_ada
5030 || (cu->language == language_fortran && physname))
5031 {
5032 /* For Ada unit, we prefer the linkage name over the name, as
5033 the former contains the exported name, which the user expects
5034 to be able to reference. Ideally, we want the user to be able
5035 to reference this entity using either natural or linkage name,
5036 but we haven't started looking at this enhancement yet. */
5037 struct attribute *attr;
5038
5039 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5040 if (attr == NULL)
5041 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5042 if (attr && DW_STRING (attr))
5043 return DW_STRING (attr);
5044 }
5045
5046 /* These are the only languages we know how to qualify names in. */
5047 if (name != NULL
5048 && (cu->language == language_cplus || cu->language == language_java
5049 || cu->language == language_fortran))
5050 {
5051 if (die_needs_namespace (die, cu))
5052 {
5053 long length;
5054 const char *prefix;
5055 struct ui_file *buf;
5056
5057 prefix = determine_prefix (die, cu);
5058 buf = mem_fileopen ();
5059 if (*prefix != '\0')
5060 {
5061 char *prefixed_name = typename_concat (NULL, prefix, name,
5062 physname, cu);
5063
5064 fputs_unfiltered (prefixed_name, buf);
5065 xfree (prefixed_name);
5066 }
5067 else
5068 fputs_unfiltered (name, buf);
5069
5070 /* Template parameters may be specified in the DIE's DW_AT_name, or
5071 as children with DW_TAG_template_type_param or
5072 DW_TAG_value_type_param. If the latter, add them to the name
5073 here. If the name already has template parameters, then
5074 skip this step; some versions of GCC emit both, and
5075 it is more efficient to use the pre-computed name.
5076
5077 Something to keep in mind about this process: it is very
5078 unlikely, or in some cases downright impossible, to produce
5079 something that will match the mangled name of a function.
5080 If the definition of the function has the same debug info,
5081 we should be able to match up with it anyway. But fallbacks
5082 using the minimal symbol, for instance to find a method
5083 implemented in a stripped copy of libstdc++, will not work.
5084 If we do not have debug info for the definition, we will have to
5085 match them up some other way.
5086
5087 When we do name matching there is a related problem with function
5088 templates; two instantiated function templates are allowed to
5089 differ only by their return types, which we do not add here. */
5090
5091 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5092 {
5093 struct attribute *attr;
5094 struct die_info *child;
5095 int first = 1;
5096
5097 die->building_fullname = 1;
5098
5099 for (child = die->child; child != NULL; child = child->sibling)
5100 {
5101 struct type *type;
5102 long value;
5103 gdb_byte *bytes;
5104 struct dwarf2_locexpr_baton *baton;
5105 struct value *v;
5106
5107 if (child->tag != DW_TAG_template_type_param
5108 && child->tag != DW_TAG_template_value_param)
5109 continue;
5110
5111 if (first)
5112 {
5113 fputs_unfiltered ("<", buf);
5114 first = 0;
5115 }
5116 else
5117 fputs_unfiltered (", ", buf);
5118
5119 attr = dwarf2_attr (child, DW_AT_type, cu);
5120 if (attr == NULL)
5121 {
5122 complaint (&symfile_complaints,
5123 _("template parameter missing DW_AT_type"));
5124 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5125 continue;
5126 }
5127 type = die_type (child, cu);
5128
5129 if (child->tag == DW_TAG_template_type_param)
5130 {
5131 c_print_type (type, "", buf, -1, 0);
5132 continue;
5133 }
5134
5135 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5136 if (attr == NULL)
5137 {
5138 complaint (&symfile_complaints,
5139 _("template parameter missing "
5140 "DW_AT_const_value"));
5141 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5142 continue;
5143 }
5144
5145 dwarf2_const_value_attr (attr, type, name,
5146 &cu->comp_unit_obstack, cu,
5147 &value, &bytes, &baton);
5148
5149 if (TYPE_NOSIGN (type))
5150 /* GDB prints characters as NUMBER 'CHAR'. If that's
5151 changed, this can use value_print instead. */
5152 c_printchar (value, type, buf);
5153 else
5154 {
5155 struct value_print_options opts;
5156
5157 if (baton != NULL)
5158 v = dwarf2_evaluate_loc_desc (type, NULL,
5159 baton->data,
5160 baton->size,
5161 baton->per_cu);
5162 else if (bytes != NULL)
5163 {
5164 v = allocate_value (type);
5165 memcpy (value_contents_writeable (v), bytes,
5166 TYPE_LENGTH (type));
5167 }
5168 else
5169 v = value_from_longest (type, value);
5170
5171 /* Specify decimal so that we do not depend on
5172 the radix. */
5173 get_formatted_print_options (&opts, 'd');
5174 opts.raw = 1;
5175 value_print (v, buf, &opts);
5176 release_value (v);
5177 value_free (v);
5178 }
5179 }
5180
5181 die->building_fullname = 0;
5182
5183 if (!first)
5184 {
5185 /* Close the argument list, with a space if necessary
5186 (nested templates). */
5187 char last_char = '\0';
5188 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5189 if (last_char == '>')
5190 fputs_unfiltered (" >", buf);
5191 else
5192 fputs_unfiltered (">", buf);
5193 }
5194 }
5195
5196 /* For Java and C++ methods, append formal parameter type
5197 information, if PHYSNAME. */
5198
5199 if (physname && die->tag == DW_TAG_subprogram
5200 && (cu->language == language_cplus
5201 || cu->language == language_java))
5202 {
5203 struct type *type = read_type_die (die, cu);
5204
5205 c_type_print_args (type, buf, 1, cu->language);
5206
5207 if (cu->language == language_java)
5208 {
5209 /* For java, we must append the return type to method
5210 names. */
5211 if (die->tag == DW_TAG_subprogram)
5212 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5213 0, 0);
5214 }
5215 else if (cu->language == language_cplus)
5216 {
5217 /* Assume that an artificial first parameter is
5218 "this", but do not crash if it is not. RealView
5219 marks unnamed (and thus unused) parameters as
5220 artificial; there is no way to differentiate
5221 the two cases. */
5222 if (TYPE_NFIELDS (type) > 0
5223 && TYPE_FIELD_ARTIFICIAL (type, 0)
5224 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5225 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5226 0))))
5227 fputs_unfiltered (" const", buf);
5228 }
5229 }
5230
5231 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5232 &length);
5233 ui_file_delete (buf);
5234
5235 if (cu->language == language_cplus)
5236 {
5237 char *cname
5238 = dwarf2_canonicalize_name (name, cu,
5239 &objfile->objfile_obstack);
5240
5241 if (cname != NULL)
5242 name = cname;
5243 }
5244 }
5245 }
5246
5247 return name;
5248 }
5249
5250 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5251 If scope qualifiers are appropriate they will be added. The result
5252 will be allocated on the objfile_obstack, or NULL if the DIE does
5253 not have a name. NAME may either be from a previous call to
5254 dwarf2_name or NULL.
5255
5256 The output string will be canonicalized (if C++/Java). */
5257
5258 static const char *
5259 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5260 {
5261 return dwarf2_compute_name (name, die, cu, 0);
5262 }
5263
5264 /* Construct a physname for the given DIE in CU. NAME may either be
5265 from a previous call to dwarf2_name or NULL. The result will be
5266 allocated on the objfile_objstack or NULL if the DIE does not have a
5267 name.
5268
5269 The output string will be canonicalized (if C++/Java). */
5270
5271 static const char *
5272 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5273 {
5274 struct objfile *objfile = cu->objfile;
5275 struct attribute *attr;
5276 const char *retval, *mangled = NULL, *canon = NULL;
5277 struct cleanup *back_to;
5278 int need_copy = 1;
5279
5280 /* In this case dwarf2_compute_name is just a shortcut not building anything
5281 on its own. */
5282 if (!die_needs_namespace (die, cu))
5283 return dwarf2_compute_name (name, die, cu, 1);
5284
5285 back_to = make_cleanup (null_cleanup, NULL);
5286
5287 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5288 if (!attr)
5289 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5290
5291 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5292 has computed. */
5293 if (attr && DW_STRING (attr))
5294 {
5295 char *demangled;
5296
5297 mangled = DW_STRING (attr);
5298
5299 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5300 type. It is easier for GDB users to search for such functions as
5301 `name(params)' than `long name(params)'. In such case the minimal
5302 symbol names do not match the full symbol names but for template
5303 functions there is never a need to look up their definition from their
5304 declaration so the only disadvantage remains the minimal symbol
5305 variant `long name(params)' does not have the proper inferior type.
5306 */
5307
5308 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5309 | (cu->language == language_java
5310 ? DMGL_JAVA | DMGL_RET_POSTFIX
5311 : DMGL_RET_DROP)));
5312 if (demangled)
5313 {
5314 make_cleanup (xfree, demangled);
5315 canon = demangled;
5316 }
5317 else
5318 {
5319 canon = mangled;
5320 need_copy = 0;
5321 }
5322 }
5323
5324 if (canon == NULL || check_physname)
5325 {
5326 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5327
5328 if (canon != NULL && strcmp (physname, canon) != 0)
5329 {
5330 /* It may not mean a bug in GDB. The compiler could also
5331 compute DW_AT_linkage_name incorrectly. But in such case
5332 GDB would need to be bug-to-bug compatible. */
5333
5334 complaint (&symfile_complaints,
5335 _("Computed physname <%s> does not match demangled <%s> "
5336 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5337 physname, canon, mangled, die->offset, objfile->name);
5338
5339 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5340 is available here - over computed PHYSNAME. It is safer
5341 against both buggy GDB and buggy compilers. */
5342
5343 retval = canon;
5344 }
5345 else
5346 {
5347 retval = physname;
5348 need_copy = 0;
5349 }
5350 }
5351 else
5352 retval = canon;
5353
5354 if (need_copy)
5355 retval = obsavestring (retval, strlen (retval),
5356 &objfile->objfile_obstack);
5357
5358 do_cleanups (back_to);
5359 return retval;
5360 }
5361
5362 /* Read the import statement specified by the given die and record it. */
5363
5364 static void
5365 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5366 {
5367 struct objfile *objfile = cu->objfile;
5368 struct attribute *import_attr;
5369 struct die_info *imported_die, *child_die;
5370 struct dwarf2_cu *imported_cu;
5371 const char *imported_name;
5372 const char *imported_name_prefix;
5373 const char *canonical_name;
5374 const char *import_alias;
5375 const char *imported_declaration = NULL;
5376 const char *import_prefix;
5377 VEC (const_char_ptr) *excludes = NULL;
5378 struct cleanup *cleanups;
5379
5380 char *temp;
5381
5382 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5383 if (import_attr == NULL)
5384 {
5385 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5386 dwarf_tag_name (die->tag));
5387 return;
5388 }
5389
5390 imported_cu = cu;
5391 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5392 imported_name = dwarf2_name (imported_die, imported_cu);
5393 if (imported_name == NULL)
5394 {
5395 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5396
5397 The import in the following code:
5398 namespace A
5399 {
5400 typedef int B;
5401 }
5402
5403 int main ()
5404 {
5405 using A::B;
5406 B b;
5407 return b;
5408 }
5409
5410 ...
5411 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5412 <52> DW_AT_decl_file : 1
5413 <53> DW_AT_decl_line : 6
5414 <54> DW_AT_import : <0x75>
5415 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5416 <59> DW_AT_name : B
5417 <5b> DW_AT_decl_file : 1
5418 <5c> DW_AT_decl_line : 2
5419 <5d> DW_AT_type : <0x6e>
5420 ...
5421 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5422 <76> DW_AT_byte_size : 4
5423 <77> DW_AT_encoding : 5 (signed)
5424
5425 imports the wrong die ( 0x75 instead of 0x58 ).
5426 This case will be ignored until the gcc bug is fixed. */
5427 return;
5428 }
5429
5430 /* Figure out the local name after import. */
5431 import_alias = dwarf2_name (die, cu);
5432
5433 /* Figure out where the statement is being imported to. */
5434 import_prefix = determine_prefix (die, cu);
5435
5436 /* Figure out what the scope of the imported die is and prepend it
5437 to the name of the imported die. */
5438 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5439
5440 if (imported_die->tag != DW_TAG_namespace
5441 && imported_die->tag != DW_TAG_module)
5442 {
5443 imported_declaration = imported_name;
5444 canonical_name = imported_name_prefix;
5445 }
5446 else if (strlen (imported_name_prefix) > 0)
5447 {
5448 temp = alloca (strlen (imported_name_prefix)
5449 + 2 + strlen (imported_name) + 1);
5450 strcpy (temp, imported_name_prefix);
5451 strcat (temp, "::");
5452 strcat (temp, imported_name);
5453 canonical_name = temp;
5454 }
5455 else
5456 canonical_name = imported_name;
5457
5458 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5459
5460 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5461 for (child_die = die->child; child_die && child_die->tag;
5462 child_die = sibling_die (child_die))
5463 {
5464 /* DWARF-4: A Fortran use statement with a “rename list” may be
5465 represented by an imported module entry with an import attribute
5466 referring to the module and owned entries corresponding to those
5467 entities that are renamed as part of being imported. */
5468
5469 if (child_die->tag != DW_TAG_imported_declaration)
5470 {
5471 complaint (&symfile_complaints,
5472 _("child DW_TAG_imported_declaration expected "
5473 "- DIE at 0x%x [in module %s]"),
5474 child_die->offset, objfile->name);
5475 continue;
5476 }
5477
5478 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5479 if (import_attr == NULL)
5480 {
5481 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5482 dwarf_tag_name (child_die->tag));
5483 continue;
5484 }
5485
5486 imported_cu = cu;
5487 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5488 &imported_cu);
5489 imported_name = dwarf2_name (imported_die, imported_cu);
5490 if (imported_name == NULL)
5491 {
5492 complaint (&symfile_complaints,
5493 _("child DW_TAG_imported_declaration has unknown "
5494 "imported name - DIE at 0x%x [in module %s]"),
5495 child_die->offset, objfile->name);
5496 continue;
5497 }
5498
5499 VEC_safe_push (const_char_ptr, excludes, imported_name);
5500
5501 process_die (child_die, cu);
5502 }
5503
5504 cp_add_using_directive (import_prefix,
5505 canonical_name,
5506 import_alias,
5507 imported_declaration,
5508 excludes,
5509 &objfile->objfile_obstack);
5510
5511 do_cleanups (cleanups);
5512 }
5513
5514 /* Cleanup function for read_file_scope. */
5515
5516 static void
5517 free_cu_line_header (void *arg)
5518 {
5519 struct dwarf2_cu *cu = arg;
5520
5521 free_line_header (cu->line_header);
5522 cu->line_header = NULL;
5523 }
5524
5525 static void
5526 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5527 char **name, char **comp_dir)
5528 {
5529 struct attribute *attr;
5530
5531 *name = NULL;
5532 *comp_dir = NULL;
5533
5534 /* Find the filename. Do not use dwarf2_name here, since the filename
5535 is not a source language identifier. */
5536 attr = dwarf2_attr (die, DW_AT_name, cu);
5537 if (attr)
5538 {
5539 *name = DW_STRING (attr);
5540 }
5541
5542 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5543 if (attr)
5544 *comp_dir = DW_STRING (attr);
5545 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5546 {
5547 *comp_dir = ldirname (*name);
5548 if (*comp_dir != NULL)
5549 make_cleanup (xfree, *comp_dir);
5550 }
5551 if (*comp_dir != NULL)
5552 {
5553 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5554 directory, get rid of it. */
5555 char *cp = strchr (*comp_dir, ':');
5556
5557 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5558 *comp_dir = cp + 1;
5559 }
5560
5561 if (*name == NULL)
5562 *name = "<unknown>";
5563 }
5564
5565 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
5566 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5567 COMP_DIR is the compilation directory.
5568 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
5569
5570 static void
5571 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5572 const char *comp_dir, int want_line_info)
5573 {
5574 struct attribute *attr;
5575 struct objfile *objfile = cu->objfile;
5576 bfd *abfd = objfile->obfd;
5577
5578 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5579 if (attr)
5580 {
5581 unsigned int line_offset = DW_UNSND (attr);
5582 struct line_header *line_header
5583 = dwarf_decode_line_header (line_offset, abfd, cu);
5584
5585 if (line_header)
5586 {
5587 cu->line_header = line_header;
5588 make_cleanup (free_cu_line_header, cu);
5589 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
5590 }
5591 }
5592 }
5593
5594 /* Process DW_TAG_compile_unit. */
5595
5596 static void
5597 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5598 {
5599 struct objfile *objfile = cu->objfile;
5600 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5601 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5602 CORE_ADDR highpc = ((CORE_ADDR) 0);
5603 struct attribute *attr;
5604 char *name = NULL;
5605 char *comp_dir = NULL;
5606 struct die_info *child_die;
5607 bfd *abfd = objfile->obfd;
5608 CORE_ADDR baseaddr;
5609
5610 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5611
5612 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5613
5614 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5615 from finish_block. */
5616 if (lowpc == ((CORE_ADDR) -1))
5617 lowpc = highpc;
5618 lowpc += baseaddr;
5619 highpc += baseaddr;
5620
5621 find_file_and_directory (die, cu, &name, &comp_dir);
5622
5623 attr = dwarf2_attr (die, DW_AT_language, cu);
5624 if (attr)
5625 {
5626 set_cu_language (DW_UNSND (attr), cu);
5627 }
5628
5629 attr = dwarf2_attr (die, DW_AT_producer, cu);
5630 if (attr)
5631 cu->producer = DW_STRING (attr);
5632
5633 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5634 standardised yet. As a workaround for the language detection we fall
5635 back to the DW_AT_producer string. */
5636 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5637 cu->language = language_opencl;
5638
5639 /* We assume that we're processing GCC output. */
5640 processing_gcc_compilation = 2;
5641
5642 processing_has_namespace_info = 0;
5643
5644 start_symtab (name, comp_dir, lowpc);
5645 record_debugformat ("DWARF 2");
5646 record_producer (cu->producer);
5647
5648 /* Decode line number information if present. We do this before
5649 processing child DIEs, so that the line header table is available
5650 for DW_AT_decl_file. */
5651 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
5652
5653 /* Process all dies in compilation unit. */
5654 if (die->child != NULL)
5655 {
5656 child_die = die->child;
5657 while (child_die && child_die->tag)
5658 {
5659 process_die (child_die, cu);
5660 child_die = sibling_die (child_die);
5661 }
5662 }
5663
5664 /* Decode macro information, if present. Dwarf 2 macro information
5665 refers to information in the line number info statement program
5666 header, so we can only read it if we've read the header
5667 successfully. */
5668 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5669 if (attr && cu->line_header)
5670 {
5671 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5672 complaint (&symfile_complaints,
5673 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5674
5675 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5676 comp_dir, abfd, cu,
5677 &dwarf2_per_objfile->macro, 1);
5678 }
5679 else
5680 {
5681 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5682 if (attr && cu->line_header)
5683 {
5684 unsigned int macro_offset = DW_UNSND (attr);
5685
5686 dwarf_decode_macros (cu->line_header, macro_offset,
5687 comp_dir, abfd, cu,
5688 &dwarf2_per_objfile->macinfo, 0);
5689 }
5690 }
5691
5692 do_cleanups (back_to);
5693 }
5694
5695 /* Process DW_TAG_type_unit.
5696 For TUs we want to skip the first top level sibling if it's not the
5697 actual type being defined by this TU. In this case the first top
5698 level sibling is there to provide context only. */
5699
5700 static void
5701 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5702 {
5703 struct objfile *objfile = cu->objfile;
5704 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5705 CORE_ADDR lowpc;
5706 struct attribute *attr;
5707 char *name = NULL;
5708 char *comp_dir = NULL;
5709 struct die_info *child_die;
5710 bfd *abfd = objfile->obfd;
5711
5712 /* start_symtab needs a low pc, but we don't really have one.
5713 Do what read_file_scope would do in the absence of such info. */
5714 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5715
5716 /* Find the filename. Do not use dwarf2_name here, since the filename
5717 is not a source language identifier. */
5718 attr = dwarf2_attr (die, DW_AT_name, cu);
5719 if (attr)
5720 name = DW_STRING (attr);
5721
5722 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5723 if (attr)
5724 comp_dir = DW_STRING (attr);
5725 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5726 {
5727 comp_dir = ldirname (name);
5728 if (comp_dir != NULL)
5729 make_cleanup (xfree, comp_dir);
5730 }
5731
5732 if (name == NULL)
5733 name = "<unknown>";
5734
5735 attr = dwarf2_attr (die, DW_AT_language, cu);
5736 if (attr)
5737 set_cu_language (DW_UNSND (attr), cu);
5738
5739 /* This isn't technically needed today. It is done for symmetry
5740 with read_file_scope. */
5741 attr = dwarf2_attr (die, DW_AT_producer, cu);
5742 if (attr)
5743 cu->producer = DW_STRING (attr);
5744
5745 /* We assume that we're processing GCC output. */
5746 processing_gcc_compilation = 2;
5747
5748 processing_has_namespace_info = 0;
5749
5750 start_symtab (name, comp_dir, lowpc);
5751 record_debugformat ("DWARF 2");
5752 record_producer (cu->producer);
5753
5754 /* Decode line number information if present. We do this before
5755 processing child DIEs, so that the line header table is available
5756 for DW_AT_decl_file.
5757 We don't need the pc/line-number mapping for type units. */
5758 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
5759
5760 /* Process the dies in the type unit. */
5761 if (die->child == NULL)
5762 {
5763 dump_die_for_error (die);
5764 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5765 bfd_get_filename (abfd));
5766 }
5767
5768 child_die = die->child;
5769
5770 while (child_die && child_die->tag)
5771 {
5772 process_die (child_die, cu);
5773
5774 child_die = sibling_die (child_die);
5775 }
5776
5777 do_cleanups (back_to);
5778 }
5779
5780 /* qsort helper for inherit_abstract_dies. */
5781
5782 static int
5783 unsigned_int_compar (const void *ap, const void *bp)
5784 {
5785 unsigned int a = *(unsigned int *) ap;
5786 unsigned int b = *(unsigned int *) bp;
5787
5788 return (a > b) - (b > a);
5789 }
5790
5791 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5792 Inherit only the children of the DW_AT_abstract_origin DIE not being
5793 already referenced by DW_AT_abstract_origin from the children of the
5794 current DIE. */
5795
5796 static void
5797 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5798 {
5799 struct die_info *child_die;
5800 unsigned die_children_count;
5801 /* CU offsets which were referenced by children of the current DIE. */
5802 unsigned *offsets;
5803 unsigned *offsets_end, *offsetp;
5804 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5805 struct die_info *origin_die;
5806 /* Iterator of the ORIGIN_DIE children. */
5807 struct die_info *origin_child_die;
5808 struct cleanup *cleanups;
5809 struct attribute *attr;
5810 struct dwarf2_cu *origin_cu;
5811 struct pending **origin_previous_list_in_scope;
5812
5813 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5814 if (!attr)
5815 return;
5816
5817 /* Note that following die references may follow to a die in a
5818 different cu. */
5819
5820 origin_cu = cu;
5821 origin_die = follow_die_ref (die, attr, &origin_cu);
5822
5823 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5824 symbols in. */
5825 origin_previous_list_in_scope = origin_cu->list_in_scope;
5826 origin_cu->list_in_scope = cu->list_in_scope;
5827
5828 if (die->tag != origin_die->tag
5829 && !(die->tag == DW_TAG_inlined_subroutine
5830 && origin_die->tag == DW_TAG_subprogram))
5831 complaint (&symfile_complaints,
5832 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5833 die->offset, origin_die->offset);
5834
5835 child_die = die->child;
5836 die_children_count = 0;
5837 while (child_die && child_die->tag)
5838 {
5839 child_die = sibling_die (child_die);
5840 die_children_count++;
5841 }
5842 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5843 cleanups = make_cleanup (xfree, offsets);
5844
5845 offsets_end = offsets;
5846 child_die = die->child;
5847 while (child_die && child_die->tag)
5848 {
5849 /* For each CHILD_DIE, find the corresponding child of
5850 ORIGIN_DIE. If there is more than one layer of
5851 DW_AT_abstract_origin, follow them all; there shouldn't be,
5852 but GCC versions at least through 4.4 generate this (GCC PR
5853 40573). */
5854 struct die_info *child_origin_die = child_die;
5855 struct dwarf2_cu *child_origin_cu = cu;
5856
5857 while (1)
5858 {
5859 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5860 child_origin_cu);
5861 if (attr == NULL)
5862 break;
5863 child_origin_die = follow_die_ref (child_origin_die, attr,
5864 &child_origin_cu);
5865 }
5866
5867 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5868 counterpart may exist. */
5869 if (child_origin_die != child_die)
5870 {
5871 if (child_die->tag != child_origin_die->tag
5872 && !(child_die->tag == DW_TAG_inlined_subroutine
5873 && child_origin_die->tag == DW_TAG_subprogram))
5874 complaint (&symfile_complaints,
5875 _("Child DIE 0x%x and its abstract origin 0x%x have "
5876 "different tags"), child_die->offset,
5877 child_origin_die->offset);
5878 if (child_origin_die->parent != origin_die)
5879 complaint (&symfile_complaints,
5880 _("Child DIE 0x%x and its abstract origin 0x%x have "
5881 "different parents"), child_die->offset,
5882 child_origin_die->offset);
5883 else
5884 *offsets_end++ = child_origin_die->offset;
5885 }
5886 child_die = sibling_die (child_die);
5887 }
5888 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5889 unsigned_int_compar);
5890 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5891 if (offsetp[-1] == *offsetp)
5892 complaint (&symfile_complaints,
5893 _("Multiple children of DIE 0x%x refer "
5894 "to DIE 0x%x as their abstract origin"),
5895 die->offset, *offsetp);
5896
5897 offsetp = offsets;
5898 origin_child_die = origin_die->child;
5899 while (origin_child_die && origin_child_die->tag)
5900 {
5901 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5902 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5903 offsetp++;
5904 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5905 {
5906 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5907 process_die (origin_child_die, origin_cu);
5908 }
5909 origin_child_die = sibling_die (origin_child_die);
5910 }
5911 origin_cu->list_in_scope = origin_previous_list_in_scope;
5912
5913 do_cleanups (cleanups);
5914 }
5915
5916 static void
5917 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5918 {
5919 struct objfile *objfile = cu->objfile;
5920 struct context_stack *new;
5921 CORE_ADDR lowpc;
5922 CORE_ADDR highpc;
5923 struct die_info *child_die;
5924 struct attribute *attr, *call_line, *call_file;
5925 char *name;
5926 CORE_ADDR baseaddr;
5927 struct block *block;
5928 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5929 VEC (symbolp) *template_args = NULL;
5930 struct template_symbol *templ_func = NULL;
5931
5932 if (inlined_func)
5933 {
5934 /* If we do not have call site information, we can't show the
5935 caller of this inlined function. That's too confusing, so
5936 only use the scope for local variables. */
5937 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5938 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5939 if (call_line == NULL || call_file == NULL)
5940 {
5941 read_lexical_block_scope (die, cu);
5942 return;
5943 }
5944 }
5945
5946 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5947
5948 name = dwarf2_name (die, cu);
5949
5950 /* Ignore functions with missing or empty names. These are actually
5951 illegal according to the DWARF standard. */
5952 if (name == NULL)
5953 {
5954 complaint (&symfile_complaints,
5955 _("missing name for subprogram DIE at %d"), die->offset);
5956 return;
5957 }
5958
5959 /* Ignore functions with missing or invalid low and high pc attributes. */
5960 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5961 {
5962 attr = dwarf2_attr (die, DW_AT_external, cu);
5963 if (!attr || !DW_UNSND (attr))
5964 complaint (&symfile_complaints,
5965 _("cannot get low and high bounds "
5966 "for subprogram DIE at %d"),
5967 die->offset);
5968 return;
5969 }
5970
5971 lowpc += baseaddr;
5972 highpc += baseaddr;
5973
5974 /* If we have any template arguments, then we must allocate a
5975 different sort of symbol. */
5976 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5977 {
5978 if (child_die->tag == DW_TAG_template_type_param
5979 || child_die->tag == DW_TAG_template_value_param)
5980 {
5981 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5982 struct template_symbol);
5983 templ_func->base.is_cplus_template_function = 1;
5984 break;
5985 }
5986 }
5987
5988 new = push_context (0, lowpc);
5989 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5990 (struct symbol *) templ_func);
5991
5992 /* If there is a location expression for DW_AT_frame_base, record
5993 it. */
5994 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5995 if (attr)
5996 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5997 expression is being recorded directly in the function's symbol
5998 and not in a separate frame-base object. I guess this hack is
5999 to avoid adding some sort of frame-base adjunct/annex to the
6000 function's symbol :-(. The problem with doing this is that it
6001 results in a function symbol with a location expression that
6002 has nothing to do with the location of the function, ouch! The
6003 relationship should be: a function's symbol has-a frame base; a
6004 frame-base has-a location expression. */
6005 dwarf2_symbol_mark_computed (attr, new->name, cu);
6006
6007 cu->list_in_scope = &local_symbols;
6008
6009 if (die->child != NULL)
6010 {
6011 child_die = die->child;
6012 while (child_die && child_die->tag)
6013 {
6014 if (child_die->tag == DW_TAG_template_type_param
6015 || child_die->tag == DW_TAG_template_value_param)
6016 {
6017 struct symbol *arg = new_symbol (child_die, NULL, cu);
6018
6019 if (arg != NULL)
6020 VEC_safe_push (symbolp, template_args, arg);
6021 }
6022 else
6023 process_die (child_die, cu);
6024 child_die = sibling_die (child_die);
6025 }
6026 }
6027
6028 inherit_abstract_dies (die, cu);
6029
6030 /* If we have a DW_AT_specification, we might need to import using
6031 directives from the context of the specification DIE. See the
6032 comment in determine_prefix. */
6033 if (cu->language == language_cplus
6034 && dwarf2_attr (die, DW_AT_specification, cu))
6035 {
6036 struct dwarf2_cu *spec_cu = cu;
6037 struct die_info *spec_die = die_specification (die, &spec_cu);
6038
6039 while (spec_die)
6040 {
6041 child_die = spec_die->child;
6042 while (child_die && child_die->tag)
6043 {
6044 if (child_die->tag == DW_TAG_imported_module)
6045 process_die (child_die, spec_cu);
6046 child_die = sibling_die (child_die);
6047 }
6048
6049 /* In some cases, GCC generates specification DIEs that
6050 themselves contain DW_AT_specification attributes. */
6051 spec_die = die_specification (spec_die, &spec_cu);
6052 }
6053 }
6054
6055 new = pop_context ();
6056 /* Make a block for the local symbols within. */
6057 block = finish_block (new->name, &local_symbols, new->old_blocks,
6058 lowpc, highpc, objfile);
6059
6060 /* For C++, set the block's scope. */
6061 if (cu->language == language_cplus || cu->language == language_fortran)
6062 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6063 determine_prefix (die, cu),
6064 processing_has_namespace_info);
6065
6066 /* If we have address ranges, record them. */
6067 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6068
6069 /* Attach template arguments to function. */
6070 if (! VEC_empty (symbolp, template_args))
6071 {
6072 gdb_assert (templ_func != NULL);
6073
6074 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6075 templ_func->template_arguments
6076 = obstack_alloc (&objfile->objfile_obstack,
6077 (templ_func->n_template_arguments
6078 * sizeof (struct symbol *)));
6079 memcpy (templ_func->template_arguments,
6080 VEC_address (symbolp, template_args),
6081 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6082 VEC_free (symbolp, template_args);
6083 }
6084
6085 /* In C++, we can have functions nested inside functions (e.g., when
6086 a function declares a class that has methods). This means that
6087 when we finish processing a function scope, we may need to go
6088 back to building a containing block's symbol lists. */
6089 local_symbols = new->locals;
6090 param_symbols = new->params;
6091 using_directives = new->using_directives;
6092
6093 /* If we've finished processing a top-level function, subsequent
6094 symbols go in the file symbol list. */
6095 if (outermost_context_p ())
6096 cu->list_in_scope = &file_symbols;
6097 }
6098
6099 /* Process all the DIES contained within a lexical block scope. Start
6100 a new scope, process the dies, and then close the scope. */
6101
6102 static void
6103 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6104 {
6105 struct objfile *objfile = cu->objfile;
6106 struct context_stack *new;
6107 CORE_ADDR lowpc, highpc;
6108 struct die_info *child_die;
6109 CORE_ADDR baseaddr;
6110
6111 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6112
6113 /* Ignore blocks with missing or invalid low and high pc attributes. */
6114 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6115 as multiple lexical blocks? Handling children in a sane way would
6116 be nasty. Might be easier to properly extend generic blocks to
6117 describe ranges. */
6118 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6119 return;
6120 lowpc += baseaddr;
6121 highpc += baseaddr;
6122
6123 push_context (0, lowpc);
6124 if (die->child != NULL)
6125 {
6126 child_die = die->child;
6127 while (child_die && child_die->tag)
6128 {
6129 process_die (child_die, cu);
6130 child_die = sibling_die (child_die);
6131 }
6132 }
6133 new = pop_context ();
6134
6135 if (local_symbols != NULL || using_directives != NULL)
6136 {
6137 struct block *block
6138 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6139 highpc, objfile);
6140
6141 /* Note that recording ranges after traversing children, as we
6142 do here, means that recording a parent's ranges entails
6143 walking across all its children's ranges as they appear in
6144 the address map, which is quadratic behavior.
6145
6146 It would be nicer to record the parent's ranges before
6147 traversing its children, simply overriding whatever you find
6148 there. But since we don't even decide whether to create a
6149 block until after we've traversed its children, that's hard
6150 to do. */
6151 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6152 }
6153 local_symbols = new->locals;
6154 using_directives = new->using_directives;
6155 }
6156
6157 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6158
6159 static void
6160 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6161 {
6162 struct objfile *objfile = cu->objfile;
6163 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6164 CORE_ADDR pc, baseaddr;
6165 struct attribute *attr;
6166 struct call_site *call_site, call_site_local;
6167 void **slot;
6168 int nparams;
6169 struct die_info *child_die;
6170
6171 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6172
6173 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6174 if (!attr)
6175 {
6176 complaint (&symfile_complaints,
6177 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6178 "DIE 0x%x [in module %s]"),
6179 die->offset, objfile->name);
6180 return;
6181 }
6182 pc = DW_ADDR (attr) + baseaddr;
6183
6184 if (cu->call_site_htab == NULL)
6185 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6186 NULL, &objfile->objfile_obstack,
6187 hashtab_obstack_allocate, NULL);
6188 call_site_local.pc = pc;
6189 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6190 if (*slot != NULL)
6191 {
6192 complaint (&symfile_complaints,
6193 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6194 "DIE 0x%x [in module %s]"),
6195 paddress (gdbarch, pc), die->offset, objfile->name);
6196 return;
6197 }
6198
6199 /* Count parameters at the caller. */
6200
6201 nparams = 0;
6202 for (child_die = die->child; child_die && child_die->tag;
6203 child_die = sibling_die (child_die))
6204 {
6205 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6206 {
6207 complaint (&symfile_complaints,
6208 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6209 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6210 child_die->tag, child_die->offset, objfile->name);
6211 continue;
6212 }
6213
6214 nparams++;
6215 }
6216
6217 call_site = obstack_alloc (&objfile->objfile_obstack,
6218 (sizeof (*call_site)
6219 + (sizeof (*call_site->parameter)
6220 * (nparams - 1))));
6221 *slot = call_site;
6222 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6223 call_site->pc = pc;
6224
6225 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6226 {
6227 struct die_info *func_die;
6228
6229 /* Skip also over DW_TAG_inlined_subroutine. */
6230 for (func_die = die->parent;
6231 func_die && func_die->tag != DW_TAG_subprogram
6232 && func_die->tag != DW_TAG_subroutine_type;
6233 func_die = func_die->parent);
6234
6235 /* DW_AT_GNU_all_call_sites is a superset
6236 of DW_AT_GNU_all_tail_call_sites. */
6237 if (func_die
6238 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6239 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6240 {
6241 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6242 not complete. But keep CALL_SITE for look ups via call_site_htab,
6243 both the initial caller containing the real return address PC and
6244 the final callee containing the current PC of a chain of tail
6245 calls do not need to have the tail call list complete. But any
6246 function candidate for a virtual tail call frame searched via
6247 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6248 determined unambiguously. */
6249 }
6250 else
6251 {
6252 struct type *func_type = NULL;
6253
6254 if (func_die)
6255 func_type = get_die_type (func_die, cu);
6256 if (func_type != NULL)
6257 {
6258 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6259
6260 /* Enlist this call site to the function. */
6261 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6262 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6263 }
6264 else
6265 complaint (&symfile_complaints,
6266 _("Cannot find function owning DW_TAG_GNU_call_site "
6267 "DIE 0x%x [in module %s]"),
6268 die->offset, objfile->name);
6269 }
6270 }
6271
6272 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6273 if (attr == NULL)
6274 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6275 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6276 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6277 /* Keep NULL DWARF_BLOCK. */;
6278 else if (attr_form_is_block (attr))
6279 {
6280 struct dwarf2_locexpr_baton *dlbaton;
6281
6282 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6283 dlbaton->data = DW_BLOCK (attr)->data;
6284 dlbaton->size = DW_BLOCK (attr)->size;
6285 dlbaton->per_cu = cu->per_cu;
6286
6287 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6288 }
6289 else if (is_ref_attr (attr))
6290 {
6291 struct dwarf2_cu *target_cu = cu;
6292 struct die_info *target_die;
6293
6294 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6295 gdb_assert (target_cu->objfile == objfile);
6296 if (die_is_declaration (target_die, target_cu))
6297 {
6298 const char *target_physname;
6299
6300 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6301 if (target_physname == NULL)
6302 complaint (&symfile_complaints,
6303 _("DW_AT_GNU_call_site_target target DIE has invalid "
6304 "physname, for referencing DIE 0x%x [in module %s]"),
6305 die->offset, objfile->name);
6306 else
6307 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6308 }
6309 else
6310 {
6311 CORE_ADDR lowpc;
6312
6313 /* DW_AT_entry_pc should be preferred. */
6314 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6315 complaint (&symfile_complaints,
6316 _("DW_AT_GNU_call_site_target target DIE has invalid "
6317 "low pc, for referencing DIE 0x%x [in module %s]"),
6318 die->offset, objfile->name);
6319 else
6320 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6321 }
6322 }
6323 else
6324 complaint (&symfile_complaints,
6325 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6326 "block nor reference, for DIE 0x%x [in module %s]"),
6327 die->offset, objfile->name);
6328
6329 call_site->per_cu = cu->per_cu;
6330
6331 for (child_die = die->child;
6332 child_die && child_die->tag;
6333 child_die = sibling_die (child_die))
6334 {
6335 struct dwarf2_locexpr_baton *dlbaton;
6336 struct call_site_parameter *parameter;
6337
6338 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6339 {
6340 /* Already printed the complaint above. */
6341 continue;
6342 }
6343
6344 gdb_assert (call_site->parameter_count < nparams);
6345 parameter = &call_site->parameter[call_site->parameter_count];
6346
6347 /* DW_AT_location specifies the register number. Value of the data
6348 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6349
6350 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6351 if (!attr || !attr_form_is_block (attr))
6352 {
6353 complaint (&symfile_complaints,
6354 _("No DW_FORM_block* DW_AT_location for "
6355 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6356 child_die->offset, objfile->name);
6357 continue;
6358 }
6359 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6360 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6361 if (parameter->dwarf_reg == -1
6362 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6363 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6364 &parameter->fb_offset))
6365 {
6366 complaint (&symfile_complaints,
6367 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6368 "for DW_FORM_block* DW_AT_location for "
6369 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6370 child_die->offset, objfile->name);
6371 continue;
6372 }
6373
6374 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6375 if (!attr_form_is_block (attr))
6376 {
6377 complaint (&symfile_complaints,
6378 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6379 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6380 child_die->offset, objfile->name);
6381 continue;
6382 }
6383 parameter->value = DW_BLOCK (attr)->data;
6384 parameter->value_size = DW_BLOCK (attr)->size;
6385
6386 /* Parameters are not pre-cleared by memset above. */
6387 parameter->data_value = NULL;
6388 parameter->data_value_size = 0;
6389 call_site->parameter_count++;
6390
6391 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6392 if (attr)
6393 {
6394 if (!attr_form_is_block (attr))
6395 complaint (&symfile_complaints,
6396 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6397 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6398 child_die->offset, objfile->name);
6399 else
6400 {
6401 parameter->data_value = DW_BLOCK (attr)->data;
6402 parameter->data_value_size = DW_BLOCK (attr)->size;
6403 }
6404 }
6405 }
6406 }
6407
6408 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6409 Return 1 if the attributes are present and valid, otherwise, return 0.
6410 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6411
6412 static int
6413 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6414 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6415 struct partial_symtab *ranges_pst)
6416 {
6417 struct objfile *objfile = cu->objfile;
6418 struct comp_unit_head *cu_header = &cu->header;
6419 bfd *obfd = objfile->obfd;
6420 unsigned int addr_size = cu_header->addr_size;
6421 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6422 /* Base address selection entry. */
6423 CORE_ADDR base;
6424 int found_base;
6425 unsigned int dummy;
6426 gdb_byte *buffer;
6427 CORE_ADDR marker;
6428 int low_set;
6429 CORE_ADDR low = 0;
6430 CORE_ADDR high = 0;
6431 CORE_ADDR baseaddr;
6432
6433 found_base = cu->base_known;
6434 base = cu->base_address;
6435
6436 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6437 if (offset >= dwarf2_per_objfile->ranges.size)
6438 {
6439 complaint (&symfile_complaints,
6440 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6441 offset);
6442 return 0;
6443 }
6444 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6445
6446 /* Read in the largest possible address. */
6447 marker = read_address (obfd, buffer, cu, &dummy);
6448 if ((marker & mask) == mask)
6449 {
6450 /* If we found the largest possible address, then
6451 read the base address. */
6452 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6453 buffer += 2 * addr_size;
6454 offset += 2 * addr_size;
6455 found_base = 1;
6456 }
6457
6458 low_set = 0;
6459
6460 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6461
6462 while (1)
6463 {
6464 CORE_ADDR range_beginning, range_end;
6465
6466 range_beginning = read_address (obfd, buffer, cu, &dummy);
6467 buffer += addr_size;
6468 range_end = read_address (obfd, buffer, cu, &dummy);
6469 buffer += addr_size;
6470 offset += 2 * addr_size;
6471
6472 /* An end of list marker is a pair of zero addresses. */
6473 if (range_beginning == 0 && range_end == 0)
6474 /* Found the end of list entry. */
6475 break;
6476
6477 /* Each base address selection entry is a pair of 2 values.
6478 The first is the largest possible address, the second is
6479 the base address. Check for a base address here. */
6480 if ((range_beginning & mask) == mask)
6481 {
6482 /* If we found the largest possible address, then
6483 read the base address. */
6484 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6485 found_base = 1;
6486 continue;
6487 }
6488
6489 if (!found_base)
6490 {
6491 /* We have no valid base address for the ranges
6492 data. */
6493 complaint (&symfile_complaints,
6494 _("Invalid .debug_ranges data (no base address)"));
6495 return 0;
6496 }
6497
6498 if (range_beginning > range_end)
6499 {
6500 /* Inverted range entries are invalid. */
6501 complaint (&symfile_complaints,
6502 _("Invalid .debug_ranges data (inverted range)"));
6503 return 0;
6504 }
6505
6506 /* Empty range entries have no effect. */
6507 if (range_beginning == range_end)
6508 continue;
6509
6510 range_beginning += base;
6511 range_end += base;
6512
6513 if (ranges_pst != NULL)
6514 addrmap_set_empty (objfile->psymtabs_addrmap,
6515 range_beginning + baseaddr,
6516 range_end - 1 + baseaddr,
6517 ranges_pst);
6518
6519 /* FIXME: This is recording everything as a low-high
6520 segment of consecutive addresses. We should have a
6521 data structure for discontiguous block ranges
6522 instead. */
6523 if (! low_set)
6524 {
6525 low = range_beginning;
6526 high = range_end;
6527 low_set = 1;
6528 }
6529 else
6530 {
6531 if (range_beginning < low)
6532 low = range_beginning;
6533 if (range_end > high)
6534 high = range_end;
6535 }
6536 }
6537
6538 if (! low_set)
6539 /* If the first entry is an end-of-list marker, the range
6540 describes an empty scope, i.e. no instructions. */
6541 return 0;
6542
6543 if (low_return)
6544 *low_return = low;
6545 if (high_return)
6546 *high_return = high;
6547 return 1;
6548 }
6549
6550 /* Get low and high pc attributes from a die. Return 1 if the attributes
6551 are present and valid, otherwise, return 0. Return -1 if the range is
6552 discontinuous, i.e. derived from DW_AT_ranges information. */
6553 static int
6554 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6555 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6556 struct partial_symtab *pst)
6557 {
6558 struct attribute *attr;
6559 CORE_ADDR low = 0;
6560 CORE_ADDR high = 0;
6561 int ret = 0;
6562
6563 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6564 if (attr)
6565 {
6566 high = DW_ADDR (attr);
6567 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6568 if (attr)
6569 low = DW_ADDR (attr);
6570 else
6571 /* Found high w/o low attribute. */
6572 return 0;
6573
6574 /* Found consecutive range of addresses. */
6575 ret = 1;
6576 }
6577 else
6578 {
6579 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6580 if (attr != NULL)
6581 {
6582 /* Value of the DW_AT_ranges attribute is the offset in the
6583 .debug_ranges section. */
6584 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6585 return 0;
6586 /* Found discontinuous range of addresses. */
6587 ret = -1;
6588 }
6589 }
6590
6591 /* read_partial_die has also the strict LOW < HIGH requirement. */
6592 if (high <= low)
6593 return 0;
6594
6595 /* When using the GNU linker, .gnu.linkonce. sections are used to
6596 eliminate duplicate copies of functions and vtables and such.
6597 The linker will arbitrarily choose one and discard the others.
6598 The AT_*_pc values for such functions refer to local labels in
6599 these sections. If the section from that file was discarded, the
6600 labels are not in the output, so the relocs get a value of 0.
6601 If this is a discarded function, mark the pc bounds as invalid,
6602 so that GDB will ignore it. */
6603 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6604 return 0;
6605
6606 *lowpc = low;
6607 if (highpc)
6608 *highpc = high;
6609 return ret;
6610 }
6611
6612 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6613 its low and high PC addresses. Do nothing if these addresses could not
6614 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6615 and HIGHPC to the high address if greater than HIGHPC. */
6616
6617 static void
6618 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6619 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6620 struct dwarf2_cu *cu)
6621 {
6622 CORE_ADDR low, high;
6623 struct die_info *child = die->child;
6624
6625 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6626 {
6627 *lowpc = min (*lowpc, low);
6628 *highpc = max (*highpc, high);
6629 }
6630
6631 /* If the language does not allow nested subprograms (either inside
6632 subprograms or lexical blocks), we're done. */
6633 if (cu->language != language_ada)
6634 return;
6635
6636 /* Check all the children of the given DIE. If it contains nested
6637 subprograms, then check their pc bounds. Likewise, we need to
6638 check lexical blocks as well, as they may also contain subprogram
6639 definitions. */
6640 while (child && child->tag)
6641 {
6642 if (child->tag == DW_TAG_subprogram
6643 || child->tag == DW_TAG_lexical_block)
6644 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6645 child = sibling_die (child);
6646 }
6647 }
6648
6649 /* Get the low and high pc's represented by the scope DIE, and store
6650 them in *LOWPC and *HIGHPC. If the correct values can't be
6651 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6652
6653 static void
6654 get_scope_pc_bounds (struct die_info *die,
6655 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6656 struct dwarf2_cu *cu)
6657 {
6658 CORE_ADDR best_low = (CORE_ADDR) -1;
6659 CORE_ADDR best_high = (CORE_ADDR) 0;
6660 CORE_ADDR current_low, current_high;
6661
6662 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6663 {
6664 best_low = current_low;
6665 best_high = current_high;
6666 }
6667 else
6668 {
6669 struct die_info *child = die->child;
6670
6671 while (child && child->tag)
6672 {
6673 switch (child->tag) {
6674 case DW_TAG_subprogram:
6675 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6676 break;
6677 case DW_TAG_namespace:
6678 case DW_TAG_module:
6679 /* FIXME: carlton/2004-01-16: Should we do this for
6680 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6681 that current GCC's always emit the DIEs corresponding
6682 to definitions of methods of classes as children of a
6683 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6684 the DIEs giving the declarations, which could be
6685 anywhere). But I don't see any reason why the
6686 standards says that they have to be there. */
6687 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6688
6689 if (current_low != ((CORE_ADDR) -1))
6690 {
6691 best_low = min (best_low, current_low);
6692 best_high = max (best_high, current_high);
6693 }
6694 break;
6695 default:
6696 /* Ignore. */
6697 break;
6698 }
6699
6700 child = sibling_die (child);
6701 }
6702 }
6703
6704 *lowpc = best_low;
6705 *highpc = best_high;
6706 }
6707
6708 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6709 in DIE. */
6710 static void
6711 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6712 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6713 {
6714 struct objfile *objfile = cu->objfile;
6715 struct attribute *attr;
6716
6717 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6718 if (attr)
6719 {
6720 CORE_ADDR high = DW_ADDR (attr);
6721
6722 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6723 if (attr)
6724 {
6725 CORE_ADDR low = DW_ADDR (attr);
6726
6727 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6728 }
6729 }
6730
6731 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6732 if (attr)
6733 {
6734 bfd *obfd = objfile->obfd;
6735
6736 /* The value of the DW_AT_ranges attribute is the offset of the
6737 address range list in the .debug_ranges section. */
6738 unsigned long offset = DW_UNSND (attr);
6739 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6740
6741 /* For some target architectures, but not others, the
6742 read_address function sign-extends the addresses it returns.
6743 To recognize base address selection entries, we need a
6744 mask. */
6745 unsigned int addr_size = cu->header.addr_size;
6746 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6747
6748 /* The base address, to which the next pair is relative. Note
6749 that this 'base' is a DWARF concept: most entries in a range
6750 list are relative, to reduce the number of relocs against the
6751 debugging information. This is separate from this function's
6752 'baseaddr' argument, which GDB uses to relocate debugging
6753 information from a shared library based on the address at
6754 which the library was loaded. */
6755 CORE_ADDR base = cu->base_address;
6756 int base_known = cu->base_known;
6757
6758 gdb_assert (dwarf2_per_objfile->ranges.readin);
6759 if (offset >= dwarf2_per_objfile->ranges.size)
6760 {
6761 complaint (&symfile_complaints,
6762 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6763 offset);
6764 return;
6765 }
6766
6767 for (;;)
6768 {
6769 unsigned int bytes_read;
6770 CORE_ADDR start, end;
6771
6772 start = read_address (obfd, buffer, cu, &bytes_read);
6773 buffer += bytes_read;
6774 end = read_address (obfd, buffer, cu, &bytes_read);
6775 buffer += bytes_read;
6776
6777 /* Did we find the end of the range list? */
6778 if (start == 0 && end == 0)
6779 break;
6780
6781 /* Did we find a base address selection entry? */
6782 else if ((start & base_select_mask) == base_select_mask)
6783 {
6784 base = end;
6785 base_known = 1;
6786 }
6787
6788 /* We found an ordinary address range. */
6789 else
6790 {
6791 if (!base_known)
6792 {
6793 complaint (&symfile_complaints,
6794 _("Invalid .debug_ranges data "
6795 "(no base address)"));
6796 return;
6797 }
6798
6799 if (start > end)
6800 {
6801 /* Inverted range entries are invalid. */
6802 complaint (&symfile_complaints,
6803 _("Invalid .debug_ranges data "
6804 "(inverted range)"));
6805 return;
6806 }
6807
6808 /* Empty range entries have no effect. */
6809 if (start == end)
6810 continue;
6811
6812 record_block_range (block,
6813 baseaddr + base + start,
6814 baseaddr + base + end - 1);
6815 }
6816 }
6817 }
6818 }
6819
6820 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6821 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6822 during 4.6.0 experimental. */
6823
6824 static int
6825 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6826 {
6827 const char *cs;
6828 int major, minor, release;
6829
6830 if (cu->producer == NULL)
6831 {
6832 /* For unknown compilers expect their behavior is DWARF version
6833 compliant.
6834
6835 GCC started to support .debug_types sections by -gdwarf-4 since
6836 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6837 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6838 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6839 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6840
6841 return 0;
6842 }
6843
6844 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6845
6846 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6847 {
6848 /* For non-GCC compilers expect their behavior is DWARF version
6849 compliant. */
6850
6851 return 0;
6852 }
6853 cs = &cu->producer[strlen ("GNU ")];
6854 while (*cs && !isdigit (*cs))
6855 cs++;
6856 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6857 {
6858 /* Not recognized as GCC. */
6859
6860 return 0;
6861 }
6862
6863 return major < 4 || (major == 4 && minor < 6);
6864 }
6865
6866 /* Return the default accessibility type if it is not overriden by
6867 DW_AT_accessibility. */
6868
6869 static enum dwarf_access_attribute
6870 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6871 {
6872 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6873 {
6874 /* The default DWARF 2 accessibility for members is public, the default
6875 accessibility for inheritance is private. */
6876
6877 if (die->tag != DW_TAG_inheritance)
6878 return DW_ACCESS_public;
6879 else
6880 return DW_ACCESS_private;
6881 }
6882 else
6883 {
6884 /* DWARF 3+ defines the default accessibility a different way. The same
6885 rules apply now for DW_TAG_inheritance as for the members and it only
6886 depends on the container kind. */
6887
6888 if (die->parent->tag == DW_TAG_class_type)
6889 return DW_ACCESS_private;
6890 else
6891 return DW_ACCESS_public;
6892 }
6893 }
6894
6895 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6896 offset. If the attribute was not found return 0, otherwise return
6897 1. If it was found but could not properly be handled, set *OFFSET
6898 to 0. */
6899
6900 static int
6901 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6902 LONGEST *offset)
6903 {
6904 struct attribute *attr;
6905
6906 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6907 if (attr != NULL)
6908 {
6909 *offset = 0;
6910
6911 /* Note that we do not check for a section offset first here.
6912 This is because DW_AT_data_member_location is new in DWARF 4,
6913 so if we see it, we can assume that a constant form is really
6914 a constant and not a section offset. */
6915 if (attr_form_is_constant (attr))
6916 *offset = dwarf2_get_attr_constant_value (attr, 0);
6917 else if (attr_form_is_section_offset (attr))
6918 dwarf2_complex_location_expr_complaint ();
6919 else if (attr_form_is_block (attr))
6920 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6921 else
6922 dwarf2_complex_location_expr_complaint ();
6923
6924 return 1;
6925 }
6926
6927 return 0;
6928 }
6929
6930 /* Add an aggregate field to the field list. */
6931
6932 static void
6933 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6934 struct dwarf2_cu *cu)
6935 {
6936 struct objfile *objfile = cu->objfile;
6937 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6938 struct nextfield *new_field;
6939 struct attribute *attr;
6940 struct field *fp;
6941 char *fieldname = "";
6942
6943 /* Allocate a new field list entry and link it in. */
6944 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6945 make_cleanup (xfree, new_field);
6946 memset (new_field, 0, sizeof (struct nextfield));
6947
6948 if (die->tag == DW_TAG_inheritance)
6949 {
6950 new_field->next = fip->baseclasses;
6951 fip->baseclasses = new_field;
6952 }
6953 else
6954 {
6955 new_field->next = fip->fields;
6956 fip->fields = new_field;
6957 }
6958 fip->nfields++;
6959
6960 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6961 if (attr)
6962 new_field->accessibility = DW_UNSND (attr);
6963 else
6964 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6965 if (new_field->accessibility != DW_ACCESS_public)
6966 fip->non_public_fields = 1;
6967
6968 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6969 if (attr)
6970 new_field->virtuality = DW_UNSND (attr);
6971 else
6972 new_field->virtuality = DW_VIRTUALITY_none;
6973
6974 fp = &new_field->field;
6975
6976 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6977 {
6978 LONGEST offset;
6979
6980 /* Data member other than a C++ static data member. */
6981
6982 /* Get type of field. */
6983 fp->type = die_type (die, cu);
6984
6985 SET_FIELD_BITPOS (*fp, 0);
6986
6987 /* Get bit size of field (zero if none). */
6988 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6989 if (attr)
6990 {
6991 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6992 }
6993 else
6994 {
6995 FIELD_BITSIZE (*fp) = 0;
6996 }
6997
6998 /* Get bit offset of field. */
6999 if (handle_data_member_location (die, cu, &offset))
7000 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7001 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7002 if (attr)
7003 {
7004 if (gdbarch_bits_big_endian (gdbarch))
7005 {
7006 /* For big endian bits, the DW_AT_bit_offset gives the
7007 additional bit offset from the MSB of the containing
7008 anonymous object to the MSB of the field. We don't
7009 have to do anything special since we don't need to
7010 know the size of the anonymous object. */
7011 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7012 }
7013 else
7014 {
7015 /* For little endian bits, compute the bit offset to the
7016 MSB of the anonymous object, subtract off the number of
7017 bits from the MSB of the field to the MSB of the
7018 object, and then subtract off the number of bits of
7019 the field itself. The result is the bit offset of
7020 the LSB of the field. */
7021 int anonymous_size;
7022 int bit_offset = DW_UNSND (attr);
7023
7024 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7025 if (attr)
7026 {
7027 /* The size of the anonymous object containing
7028 the bit field is explicit, so use the
7029 indicated size (in bytes). */
7030 anonymous_size = DW_UNSND (attr);
7031 }
7032 else
7033 {
7034 /* The size of the anonymous object containing
7035 the bit field must be inferred from the type
7036 attribute of the data member containing the
7037 bit field. */
7038 anonymous_size = TYPE_LENGTH (fp->type);
7039 }
7040 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7041 - bit_offset - FIELD_BITSIZE (*fp);
7042 }
7043 }
7044
7045 /* Get name of field. */
7046 fieldname = dwarf2_name (die, cu);
7047 if (fieldname == NULL)
7048 fieldname = "";
7049
7050 /* The name is already allocated along with this objfile, so we don't
7051 need to duplicate it for the type. */
7052 fp->name = fieldname;
7053
7054 /* Change accessibility for artificial fields (e.g. virtual table
7055 pointer or virtual base class pointer) to private. */
7056 if (dwarf2_attr (die, DW_AT_artificial, cu))
7057 {
7058 FIELD_ARTIFICIAL (*fp) = 1;
7059 new_field->accessibility = DW_ACCESS_private;
7060 fip->non_public_fields = 1;
7061 }
7062 }
7063 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7064 {
7065 /* C++ static member. */
7066
7067 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7068 is a declaration, but all versions of G++ as of this writing
7069 (so through at least 3.2.1) incorrectly generate
7070 DW_TAG_variable tags. */
7071
7072 const char *physname;
7073
7074 /* Get name of field. */
7075 fieldname = dwarf2_name (die, cu);
7076 if (fieldname == NULL)
7077 return;
7078
7079 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7080 if (attr
7081 /* Only create a symbol if this is an external value.
7082 new_symbol checks this and puts the value in the global symbol
7083 table, which we want. If it is not external, new_symbol
7084 will try to put the value in cu->list_in_scope which is wrong. */
7085 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7086 {
7087 /* A static const member, not much different than an enum as far as
7088 we're concerned, except that we can support more types. */
7089 new_symbol (die, NULL, cu);
7090 }
7091
7092 /* Get physical name. */
7093 physname = dwarf2_physname (fieldname, die, cu);
7094
7095 /* The name is already allocated along with this objfile, so we don't
7096 need to duplicate it for the type. */
7097 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7098 FIELD_TYPE (*fp) = die_type (die, cu);
7099 FIELD_NAME (*fp) = fieldname;
7100 }
7101 else if (die->tag == DW_TAG_inheritance)
7102 {
7103 LONGEST offset;
7104
7105 /* C++ base class field. */
7106 if (handle_data_member_location (die, cu, &offset))
7107 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7108 FIELD_BITSIZE (*fp) = 0;
7109 FIELD_TYPE (*fp) = die_type (die, cu);
7110 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7111 fip->nbaseclasses++;
7112 }
7113 }
7114
7115 /* Add a typedef defined in the scope of the FIP's class. */
7116
7117 static void
7118 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7119 struct dwarf2_cu *cu)
7120 {
7121 struct objfile *objfile = cu->objfile;
7122 struct typedef_field_list *new_field;
7123 struct attribute *attr;
7124 struct typedef_field *fp;
7125 char *fieldname = "";
7126
7127 /* Allocate a new field list entry and link it in. */
7128 new_field = xzalloc (sizeof (*new_field));
7129 make_cleanup (xfree, new_field);
7130
7131 gdb_assert (die->tag == DW_TAG_typedef);
7132
7133 fp = &new_field->field;
7134
7135 /* Get name of field. */
7136 fp->name = dwarf2_name (die, cu);
7137 if (fp->name == NULL)
7138 return;
7139
7140 fp->type = read_type_die (die, cu);
7141
7142 new_field->next = fip->typedef_field_list;
7143 fip->typedef_field_list = new_field;
7144 fip->typedef_field_list_count++;
7145 }
7146
7147 /* Create the vector of fields, and attach it to the type. */
7148
7149 static void
7150 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7151 struct dwarf2_cu *cu)
7152 {
7153 int nfields = fip->nfields;
7154
7155 /* Record the field count, allocate space for the array of fields,
7156 and create blank accessibility bitfields if necessary. */
7157 TYPE_NFIELDS (type) = nfields;
7158 TYPE_FIELDS (type) = (struct field *)
7159 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7160 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7161
7162 if (fip->non_public_fields && cu->language != language_ada)
7163 {
7164 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7165
7166 TYPE_FIELD_PRIVATE_BITS (type) =
7167 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7168 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7169
7170 TYPE_FIELD_PROTECTED_BITS (type) =
7171 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7172 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7173
7174 TYPE_FIELD_IGNORE_BITS (type) =
7175 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7176 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7177 }
7178
7179 /* If the type has baseclasses, allocate and clear a bit vector for
7180 TYPE_FIELD_VIRTUAL_BITS. */
7181 if (fip->nbaseclasses && cu->language != language_ada)
7182 {
7183 int num_bytes = B_BYTES (fip->nbaseclasses);
7184 unsigned char *pointer;
7185
7186 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7187 pointer = TYPE_ALLOC (type, num_bytes);
7188 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7189 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7190 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7191 }
7192
7193 /* Copy the saved-up fields into the field vector. Start from the head of
7194 the list, adding to the tail of the field array, so that they end up in
7195 the same order in the array in which they were added to the list. */
7196 while (nfields-- > 0)
7197 {
7198 struct nextfield *fieldp;
7199
7200 if (fip->fields)
7201 {
7202 fieldp = fip->fields;
7203 fip->fields = fieldp->next;
7204 }
7205 else
7206 {
7207 fieldp = fip->baseclasses;
7208 fip->baseclasses = fieldp->next;
7209 }
7210
7211 TYPE_FIELD (type, nfields) = fieldp->field;
7212 switch (fieldp->accessibility)
7213 {
7214 case DW_ACCESS_private:
7215 if (cu->language != language_ada)
7216 SET_TYPE_FIELD_PRIVATE (type, nfields);
7217 break;
7218
7219 case DW_ACCESS_protected:
7220 if (cu->language != language_ada)
7221 SET_TYPE_FIELD_PROTECTED (type, nfields);
7222 break;
7223
7224 case DW_ACCESS_public:
7225 break;
7226
7227 default:
7228 /* Unknown accessibility. Complain and treat it as public. */
7229 {
7230 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7231 fieldp->accessibility);
7232 }
7233 break;
7234 }
7235 if (nfields < fip->nbaseclasses)
7236 {
7237 switch (fieldp->virtuality)
7238 {
7239 case DW_VIRTUALITY_virtual:
7240 case DW_VIRTUALITY_pure_virtual:
7241 if (cu->language == language_ada)
7242 error (_("unexpected virtuality in component of Ada type"));
7243 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7244 break;
7245 }
7246 }
7247 }
7248 }
7249
7250 /* Add a member function to the proper fieldlist. */
7251
7252 static void
7253 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7254 struct type *type, struct dwarf2_cu *cu)
7255 {
7256 struct objfile *objfile = cu->objfile;
7257 struct attribute *attr;
7258 struct fnfieldlist *flp;
7259 int i;
7260 struct fn_field *fnp;
7261 char *fieldname;
7262 struct nextfnfield *new_fnfield;
7263 struct type *this_type;
7264 enum dwarf_access_attribute accessibility;
7265
7266 if (cu->language == language_ada)
7267 error (_("unexpected member function in Ada type"));
7268
7269 /* Get name of member function. */
7270 fieldname = dwarf2_name (die, cu);
7271 if (fieldname == NULL)
7272 return;
7273
7274 /* Look up member function name in fieldlist. */
7275 for (i = 0; i < fip->nfnfields; i++)
7276 {
7277 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7278 break;
7279 }
7280
7281 /* Create new list element if necessary. */
7282 if (i < fip->nfnfields)
7283 flp = &fip->fnfieldlists[i];
7284 else
7285 {
7286 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7287 {
7288 fip->fnfieldlists = (struct fnfieldlist *)
7289 xrealloc (fip->fnfieldlists,
7290 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7291 * sizeof (struct fnfieldlist));
7292 if (fip->nfnfields == 0)
7293 make_cleanup (free_current_contents, &fip->fnfieldlists);
7294 }
7295 flp = &fip->fnfieldlists[fip->nfnfields];
7296 flp->name = fieldname;
7297 flp->length = 0;
7298 flp->head = NULL;
7299 i = fip->nfnfields++;
7300 }
7301
7302 /* Create a new member function field and chain it to the field list
7303 entry. */
7304 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7305 make_cleanup (xfree, new_fnfield);
7306 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7307 new_fnfield->next = flp->head;
7308 flp->head = new_fnfield;
7309 flp->length++;
7310
7311 /* Fill in the member function field info. */
7312 fnp = &new_fnfield->fnfield;
7313
7314 /* Delay processing of the physname until later. */
7315 if (cu->language == language_cplus || cu->language == language_java)
7316 {
7317 add_to_method_list (type, i, flp->length - 1, fieldname,
7318 die, cu);
7319 }
7320 else
7321 {
7322 const char *physname = dwarf2_physname (fieldname, die, cu);
7323 fnp->physname = physname ? physname : "";
7324 }
7325
7326 fnp->type = alloc_type (objfile);
7327 this_type = read_type_die (die, cu);
7328 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7329 {
7330 int nparams = TYPE_NFIELDS (this_type);
7331
7332 /* TYPE is the domain of this method, and THIS_TYPE is the type
7333 of the method itself (TYPE_CODE_METHOD). */
7334 smash_to_method_type (fnp->type, type,
7335 TYPE_TARGET_TYPE (this_type),
7336 TYPE_FIELDS (this_type),
7337 TYPE_NFIELDS (this_type),
7338 TYPE_VARARGS (this_type));
7339
7340 /* Handle static member functions.
7341 Dwarf2 has no clean way to discern C++ static and non-static
7342 member functions. G++ helps GDB by marking the first
7343 parameter for non-static member functions (which is the this
7344 pointer) as artificial. We obtain this information from
7345 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7346 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7347 fnp->voffset = VOFFSET_STATIC;
7348 }
7349 else
7350 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7351 dwarf2_full_name (fieldname, die, cu));
7352
7353 /* Get fcontext from DW_AT_containing_type if present. */
7354 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7355 fnp->fcontext = die_containing_type (die, cu);
7356
7357 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7358 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7359
7360 /* Get accessibility. */
7361 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7362 if (attr)
7363 accessibility = DW_UNSND (attr);
7364 else
7365 accessibility = dwarf2_default_access_attribute (die, cu);
7366 switch (accessibility)
7367 {
7368 case DW_ACCESS_private:
7369 fnp->is_private = 1;
7370 break;
7371 case DW_ACCESS_protected:
7372 fnp->is_protected = 1;
7373 break;
7374 }
7375
7376 /* Check for artificial methods. */
7377 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7378 if (attr && DW_UNSND (attr) != 0)
7379 fnp->is_artificial = 1;
7380
7381 /* Get index in virtual function table if it is a virtual member
7382 function. For older versions of GCC, this is an offset in the
7383 appropriate virtual table, as specified by DW_AT_containing_type.
7384 For everyone else, it is an expression to be evaluated relative
7385 to the object address. */
7386
7387 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7388 if (attr)
7389 {
7390 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7391 {
7392 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7393 {
7394 /* Old-style GCC. */
7395 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7396 }
7397 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7398 || (DW_BLOCK (attr)->size > 1
7399 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7400 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7401 {
7402 struct dwarf_block blk;
7403 int offset;
7404
7405 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7406 ? 1 : 2);
7407 blk.size = DW_BLOCK (attr)->size - offset;
7408 blk.data = DW_BLOCK (attr)->data + offset;
7409 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7410 if ((fnp->voffset % cu->header.addr_size) != 0)
7411 dwarf2_complex_location_expr_complaint ();
7412 else
7413 fnp->voffset /= cu->header.addr_size;
7414 fnp->voffset += 2;
7415 }
7416 else
7417 dwarf2_complex_location_expr_complaint ();
7418
7419 if (!fnp->fcontext)
7420 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7421 }
7422 else if (attr_form_is_section_offset (attr))
7423 {
7424 dwarf2_complex_location_expr_complaint ();
7425 }
7426 else
7427 {
7428 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7429 fieldname);
7430 }
7431 }
7432 else
7433 {
7434 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7435 if (attr && DW_UNSND (attr))
7436 {
7437 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7438 complaint (&symfile_complaints,
7439 _("Member function \"%s\" (offset %d) is virtual "
7440 "but the vtable offset is not specified"),
7441 fieldname, die->offset);
7442 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7443 TYPE_CPLUS_DYNAMIC (type) = 1;
7444 }
7445 }
7446 }
7447
7448 /* Create the vector of member function fields, and attach it to the type. */
7449
7450 static void
7451 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7452 struct dwarf2_cu *cu)
7453 {
7454 struct fnfieldlist *flp;
7455 int i;
7456
7457 if (cu->language == language_ada)
7458 error (_("unexpected member functions in Ada type"));
7459
7460 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7461 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7462 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7463
7464 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7465 {
7466 struct nextfnfield *nfp = flp->head;
7467 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7468 int k;
7469
7470 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7471 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7472 fn_flp->fn_fields = (struct fn_field *)
7473 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7474 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7475 fn_flp->fn_fields[k] = nfp->fnfield;
7476 }
7477
7478 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7479 }
7480
7481 /* Returns non-zero if NAME is the name of a vtable member in CU's
7482 language, zero otherwise. */
7483 static int
7484 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7485 {
7486 static const char vptr[] = "_vptr";
7487 static const char vtable[] = "vtable";
7488
7489 /* Look for the C++ and Java forms of the vtable. */
7490 if ((cu->language == language_java
7491 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7492 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7493 && is_cplus_marker (name[sizeof (vptr) - 1])))
7494 return 1;
7495
7496 return 0;
7497 }
7498
7499 /* GCC outputs unnamed structures that are really pointers to member
7500 functions, with the ABI-specified layout. If TYPE describes
7501 such a structure, smash it into a member function type.
7502
7503 GCC shouldn't do this; it should just output pointer to member DIEs.
7504 This is GCC PR debug/28767. */
7505
7506 static void
7507 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7508 {
7509 struct type *pfn_type, *domain_type, *new_type;
7510
7511 /* Check for a structure with no name and two children. */
7512 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7513 return;
7514
7515 /* Check for __pfn and __delta members. */
7516 if (TYPE_FIELD_NAME (type, 0) == NULL
7517 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7518 || TYPE_FIELD_NAME (type, 1) == NULL
7519 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7520 return;
7521
7522 /* Find the type of the method. */
7523 pfn_type = TYPE_FIELD_TYPE (type, 0);
7524 if (pfn_type == NULL
7525 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7526 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7527 return;
7528
7529 /* Look for the "this" argument. */
7530 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7531 if (TYPE_NFIELDS (pfn_type) == 0
7532 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7533 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7534 return;
7535
7536 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7537 new_type = alloc_type (objfile);
7538 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7539 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7540 TYPE_VARARGS (pfn_type));
7541 smash_to_methodptr_type (type, new_type);
7542 }
7543
7544 /* Called when we find the DIE that starts a structure or union scope
7545 (definition) to create a type for the structure or union. Fill in
7546 the type's name and general properties; the members will not be
7547 processed until process_structure_type.
7548
7549 NOTE: we need to call these functions regardless of whether or not the
7550 DIE has a DW_AT_name attribute, since it might be an anonymous
7551 structure or union. This gets the type entered into our set of
7552 user defined types.
7553
7554 However, if the structure is incomplete (an opaque struct/union)
7555 then suppress creating a symbol table entry for it since gdb only
7556 wants to find the one with the complete definition. Note that if
7557 it is complete, we just call new_symbol, which does it's own
7558 checking about whether the struct/union is anonymous or not (and
7559 suppresses creating a symbol table entry itself). */
7560
7561 static struct type *
7562 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7563 {
7564 struct objfile *objfile = cu->objfile;
7565 struct type *type;
7566 struct attribute *attr;
7567 char *name;
7568
7569 /* If the definition of this type lives in .debug_types, read that type.
7570 Don't follow DW_AT_specification though, that will take us back up
7571 the chain and we want to go down. */
7572 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7573 if (attr)
7574 {
7575 struct dwarf2_cu *type_cu = cu;
7576 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7577
7578 /* We could just recurse on read_structure_type, but we need to call
7579 get_die_type to ensure only one type for this DIE is created.
7580 This is important, for example, because for c++ classes we need
7581 TYPE_NAME set which is only done by new_symbol. Blech. */
7582 type = read_type_die (type_die, type_cu);
7583
7584 /* TYPE_CU may not be the same as CU.
7585 Ensure TYPE is recorded in CU's type_hash table. */
7586 return set_die_type (die, type, cu);
7587 }
7588
7589 type = alloc_type (objfile);
7590 INIT_CPLUS_SPECIFIC (type);
7591
7592 name = dwarf2_name (die, cu);
7593 if (name != NULL)
7594 {
7595 if (cu->language == language_cplus
7596 || cu->language == language_java)
7597 {
7598 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7599
7600 /* dwarf2_full_name might have already finished building the DIE's
7601 type. If so, there is no need to continue. */
7602 if (get_die_type (die, cu) != NULL)
7603 return get_die_type (die, cu);
7604
7605 TYPE_TAG_NAME (type) = full_name;
7606 if (die->tag == DW_TAG_structure_type
7607 || die->tag == DW_TAG_class_type)
7608 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7609 }
7610 else
7611 {
7612 /* The name is already allocated along with this objfile, so
7613 we don't need to duplicate it for the type. */
7614 TYPE_TAG_NAME (type) = (char *) name;
7615 if (die->tag == DW_TAG_class_type)
7616 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7617 }
7618 }
7619
7620 if (die->tag == DW_TAG_structure_type)
7621 {
7622 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7623 }
7624 else if (die->tag == DW_TAG_union_type)
7625 {
7626 TYPE_CODE (type) = TYPE_CODE_UNION;
7627 }
7628 else
7629 {
7630 TYPE_CODE (type) = TYPE_CODE_CLASS;
7631 }
7632
7633 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7634 TYPE_DECLARED_CLASS (type) = 1;
7635
7636 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7637 if (attr)
7638 {
7639 TYPE_LENGTH (type) = DW_UNSND (attr);
7640 }
7641 else
7642 {
7643 TYPE_LENGTH (type) = 0;
7644 }
7645
7646 TYPE_STUB_SUPPORTED (type) = 1;
7647 if (die_is_declaration (die, cu))
7648 TYPE_STUB (type) = 1;
7649 else if (attr == NULL && die->child == NULL
7650 && producer_is_realview (cu->producer))
7651 /* RealView does not output the required DW_AT_declaration
7652 on incomplete types. */
7653 TYPE_STUB (type) = 1;
7654
7655 /* We need to add the type field to the die immediately so we don't
7656 infinitely recurse when dealing with pointers to the structure
7657 type within the structure itself. */
7658 set_die_type (die, type, cu);
7659
7660 /* set_die_type should be already done. */
7661 set_descriptive_type (type, die, cu);
7662
7663 return type;
7664 }
7665
7666 /* Finish creating a structure or union type, including filling in
7667 its members and creating a symbol for it. */
7668
7669 static void
7670 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7671 {
7672 struct objfile *objfile = cu->objfile;
7673 struct die_info *child_die = die->child;
7674 struct type *type;
7675
7676 type = get_die_type (die, cu);
7677 if (type == NULL)
7678 type = read_structure_type (die, cu);
7679
7680 if (die->child != NULL && ! die_is_declaration (die, cu))
7681 {
7682 struct field_info fi;
7683 struct die_info *child_die;
7684 VEC (symbolp) *template_args = NULL;
7685 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7686
7687 memset (&fi, 0, sizeof (struct field_info));
7688
7689 child_die = die->child;
7690
7691 while (child_die && child_die->tag)
7692 {
7693 if (child_die->tag == DW_TAG_member
7694 || child_die->tag == DW_TAG_variable)
7695 {
7696 /* NOTE: carlton/2002-11-05: A C++ static data member
7697 should be a DW_TAG_member that is a declaration, but
7698 all versions of G++ as of this writing (so through at
7699 least 3.2.1) incorrectly generate DW_TAG_variable
7700 tags for them instead. */
7701 dwarf2_add_field (&fi, child_die, cu);
7702 }
7703 else if (child_die->tag == DW_TAG_subprogram)
7704 {
7705 /* C++ member function. */
7706 dwarf2_add_member_fn (&fi, child_die, type, cu);
7707 }
7708 else if (child_die->tag == DW_TAG_inheritance)
7709 {
7710 /* C++ base class field. */
7711 dwarf2_add_field (&fi, child_die, cu);
7712 }
7713 else if (child_die->tag == DW_TAG_typedef)
7714 dwarf2_add_typedef (&fi, child_die, cu);
7715 else if (child_die->tag == DW_TAG_template_type_param
7716 || child_die->tag == DW_TAG_template_value_param)
7717 {
7718 struct symbol *arg = new_symbol (child_die, NULL, cu);
7719
7720 if (arg != NULL)
7721 VEC_safe_push (symbolp, template_args, arg);
7722 }
7723
7724 child_die = sibling_die (child_die);
7725 }
7726
7727 /* Attach template arguments to type. */
7728 if (! VEC_empty (symbolp, template_args))
7729 {
7730 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7731 TYPE_N_TEMPLATE_ARGUMENTS (type)
7732 = VEC_length (symbolp, template_args);
7733 TYPE_TEMPLATE_ARGUMENTS (type)
7734 = obstack_alloc (&objfile->objfile_obstack,
7735 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7736 * sizeof (struct symbol *)));
7737 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7738 VEC_address (symbolp, template_args),
7739 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7740 * sizeof (struct symbol *)));
7741 VEC_free (symbolp, template_args);
7742 }
7743
7744 /* Attach fields and member functions to the type. */
7745 if (fi.nfields)
7746 dwarf2_attach_fields_to_type (&fi, type, cu);
7747 if (fi.nfnfields)
7748 {
7749 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7750
7751 /* Get the type which refers to the base class (possibly this
7752 class itself) which contains the vtable pointer for the current
7753 class from the DW_AT_containing_type attribute. This use of
7754 DW_AT_containing_type is a GNU extension. */
7755
7756 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7757 {
7758 struct type *t = die_containing_type (die, cu);
7759
7760 TYPE_VPTR_BASETYPE (type) = t;
7761 if (type == t)
7762 {
7763 int i;
7764
7765 /* Our own class provides vtbl ptr. */
7766 for (i = TYPE_NFIELDS (t) - 1;
7767 i >= TYPE_N_BASECLASSES (t);
7768 --i)
7769 {
7770 const char *fieldname = TYPE_FIELD_NAME (t, i);
7771
7772 if (is_vtable_name (fieldname, cu))
7773 {
7774 TYPE_VPTR_FIELDNO (type) = i;
7775 break;
7776 }
7777 }
7778
7779 /* Complain if virtual function table field not found. */
7780 if (i < TYPE_N_BASECLASSES (t))
7781 complaint (&symfile_complaints,
7782 _("virtual function table pointer "
7783 "not found when defining class '%s'"),
7784 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7785 "");
7786 }
7787 else
7788 {
7789 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7790 }
7791 }
7792 else if (cu->producer
7793 && strncmp (cu->producer,
7794 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7795 {
7796 /* The IBM XLC compiler does not provide direct indication
7797 of the containing type, but the vtable pointer is
7798 always named __vfp. */
7799
7800 int i;
7801
7802 for (i = TYPE_NFIELDS (type) - 1;
7803 i >= TYPE_N_BASECLASSES (type);
7804 --i)
7805 {
7806 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7807 {
7808 TYPE_VPTR_FIELDNO (type) = i;
7809 TYPE_VPTR_BASETYPE (type) = type;
7810 break;
7811 }
7812 }
7813 }
7814 }
7815
7816 /* Copy fi.typedef_field_list linked list elements content into the
7817 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7818 if (fi.typedef_field_list)
7819 {
7820 int i = fi.typedef_field_list_count;
7821
7822 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7823 TYPE_TYPEDEF_FIELD_ARRAY (type)
7824 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7825 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7826
7827 /* Reverse the list order to keep the debug info elements order. */
7828 while (--i >= 0)
7829 {
7830 struct typedef_field *dest, *src;
7831
7832 dest = &TYPE_TYPEDEF_FIELD (type, i);
7833 src = &fi.typedef_field_list->field;
7834 fi.typedef_field_list = fi.typedef_field_list->next;
7835 *dest = *src;
7836 }
7837 }
7838
7839 do_cleanups (back_to);
7840
7841 if (HAVE_CPLUS_STRUCT (type))
7842 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7843 }
7844
7845 quirk_gcc_member_function_pointer (type, objfile);
7846
7847 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7848 snapshots) has been known to create a die giving a declaration
7849 for a class that has, as a child, a die giving a definition for a
7850 nested class. So we have to process our children even if the
7851 current die is a declaration. Normally, of course, a declaration
7852 won't have any children at all. */
7853
7854 while (child_die != NULL && child_die->tag)
7855 {
7856 if (child_die->tag == DW_TAG_member
7857 || child_die->tag == DW_TAG_variable
7858 || child_die->tag == DW_TAG_inheritance
7859 || child_die->tag == DW_TAG_template_value_param
7860 || child_die->tag == DW_TAG_template_type_param)
7861 {
7862 /* Do nothing. */
7863 }
7864 else
7865 process_die (child_die, cu);
7866
7867 child_die = sibling_die (child_die);
7868 }
7869
7870 /* Do not consider external references. According to the DWARF standard,
7871 these DIEs are identified by the fact that they have no byte_size
7872 attribute, and a declaration attribute. */
7873 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7874 || !die_is_declaration (die, cu))
7875 new_symbol (die, type, cu);
7876 }
7877
7878 /* Given a DW_AT_enumeration_type die, set its type. We do not
7879 complete the type's fields yet, or create any symbols. */
7880
7881 static struct type *
7882 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7883 {
7884 struct objfile *objfile = cu->objfile;
7885 struct type *type;
7886 struct attribute *attr;
7887 const char *name;
7888
7889 /* If the definition of this type lives in .debug_types, read that type.
7890 Don't follow DW_AT_specification though, that will take us back up
7891 the chain and we want to go down. */
7892 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7893 if (attr)
7894 {
7895 struct dwarf2_cu *type_cu = cu;
7896 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7897
7898 type = read_type_die (type_die, type_cu);
7899
7900 /* TYPE_CU may not be the same as CU.
7901 Ensure TYPE is recorded in CU's type_hash table. */
7902 return set_die_type (die, type, cu);
7903 }
7904
7905 type = alloc_type (objfile);
7906
7907 TYPE_CODE (type) = TYPE_CODE_ENUM;
7908 name = dwarf2_full_name (NULL, die, cu);
7909 if (name != NULL)
7910 TYPE_TAG_NAME (type) = (char *) name;
7911
7912 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7913 if (attr)
7914 {
7915 TYPE_LENGTH (type) = DW_UNSND (attr);
7916 }
7917 else
7918 {
7919 TYPE_LENGTH (type) = 0;
7920 }
7921
7922 /* The enumeration DIE can be incomplete. In Ada, any type can be
7923 declared as private in the package spec, and then defined only
7924 inside the package body. Such types are known as Taft Amendment
7925 Types. When another package uses such a type, an incomplete DIE
7926 may be generated by the compiler. */
7927 if (die_is_declaration (die, cu))
7928 TYPE_STUB (type) = 1;
7929
7930 return set_die_type (die, type, cu);
7931 }
7932
7933 /* Given a pointer to a die which begins an enumeration, process all
7934 the dies that define the members of the enumeration, and create the
7935 symbol for the enumeration type.
7936
7937 NOTE: We reverse the order of the element list. */
7938
7939 static void
7940 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7941 {
7942 struct type *this_type;
7943
7944 this_type = get_die_type (die, cu);
7945 if (this_type == NULL)
7946 this_type = read_enumeration_type (die, cu);
7947
7948 if (die->child != NULL)
7949 {
7950 struct die_info *child_die;
7951 struct symbol *sym;
7952 struct field *fields = NULL;
7953 int num_fields = 0;
7954 int unsigned_enum = 1;
7955 char *name;
7956 int flag_enum = 1;
7957 ULONGEST mask = 0;
7958
7959 child_die = die->child;
7960 while (child_die && child_die->tag)
7961 {
7962 if (child_die->tag != DW_TAG_enumerator)
7963 {
7964 process_die (child_die, cu);
7965 }
7966 else
7967 {
7968 name = dwarf2_name (child_die, cu);
7969 if (name)
7970 {
7971 sym = new_symbol (child_die, this_type, cu);
7972 if (SYMBOL_VALUE (sym) < 0)
7973 {
7974 unsigned_enum = 0;
7975 flag_enum = 0;
7976 }
7977 else if ((mask & SYMBOL_VALUE (sym)) != 0)
7978 flag_enum = 0;
7979 else
7980 mask |= SYMBOL_VALUE (sym);
7981
7982 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7983 {
7984 fields = (struct field *)
7985 xrealloc (fields,
7986 (num_fields + DW_FIELD_ALLOC_CHUNK)
7987 * sizeof (struct field));
7988 }
7989
7990 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7991 FIELD_TYPE (fields[num_fields]) = NULL;
7992 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7993 FIELD_BITSIZE (fields[num_fields]) = 0;
7994
7995 num_fields++;
7996 }
7997 }
7998
7999 child_die = sibling_die (child_die);
8000 }
8001
8002 if (num_fields)
8003 {
8004 TYPE_NFIELDS (this_type) = num_fields;
8005 TYPE_FIELDS (this_type) = (struct field *)
8006 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8007 memcpy (TYPE_FIELDS (this_type), fields,
8008 sizeof (struct field) * num_fields);
8009 xfree (fields);
8010 }
8011 if (unsigned_enum)
8012 TYPE_UNSIGNED (this_type) = 1;
8013 if (flag_enum)
8014 TYPE_FLAG_ENUM (this_type) = 1;
8015 }
8016
8017 /* If we are reading an enum from a .debug_types unit, and the enum
8018 is a declaration, and the enum is not the signatured type in the
8019 unit, then we do not want to add a symbol for it. Adding a
8020 symbol would in some cases obscure the true definition of the
8021 enum, giving users an incomplete type when the definition is
8022 actually available. Note that we do not want to do this for all
8023 enums which are just declarations, because C++0x allows forward
8024 enum declarations. */
8025 if (cu->per_cu->debug_types_section
8026 && die_is_declaration (die, cu))
8027 {
8028 struct signatured_type *type_sig;
8029
8030 type_sig
8031 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8032 cu->per_cu->debug_types_section,
8033 cu->per_cu->offset);
8034 if (type_sig->per_cu.offset + type_sig->type_offset
8035 != die->offset)
8036 return;
8037 }
8038
8039 new_symbol (die, this_type, cu);
8040 }
8041
8042 /* Extract all information from a DW_TAG_array_type DIE and put it in
8043 the DIE's type field. For now, this only handles one dimensional
8044 arrays. */
8045
8046 static struct type *
8047 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8048 {
8049 struct objfile *objfile = cu->objfile;
8050 struct die_info *child_die;
8051 struct type *type;
8052 struct type *element_type, *range_type, *index_type;
8053 struct type **range_types = NULL;
8054 struct attribute *attr;
8055 int ndim = 0;
8056 struct cleanup *back_to;
8057 char *name;
8058
8059 element_type = die_type (die, cu);
8060
8061 /* The die_type call above may have already set the type for this DIE. */
8062 type = get_die_type (die, cu);
8063 if (type)
8064 return type;
8065
8066 /* Irix 6.2 native cc creates array types without children for
8067 arrays with unspecified length. */
8068 if (die->child == NULL)
8069 {
8070 index_type = objfile_type (objfile)->builtin_int;
8071 range_type = create_range_type (NULL, index_type, 0, -1);
8072 type = create_array_type (NULL, element_type, range_type);
8073 return set_die_type (die, type, cu);
8074 }
8075
8076 back_to = make_cleanup (null_cleanup, NULL);
8077 child_die = die->child;
8078 while (child_die && child_die->tag)
8079 {
8080 if (child_die->tag == DW_TAG_subrange_type)
8081 {
8082 struct type *child_type = read_type_die (child_die, cu);
8083
8084 if (child_type != NULL)
8085 {
8086 /* The range type was succesfully read. Save it for the
8087 array type creation. */
8088 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8089 {
8090 range_types = (struct type **)
8091 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8092 * sizeof (struct type *));
8093 if (ndim == 0)
8094 make_cleanup (free_current_contents, &range_types);
8095 }
8096 range_types[ndim++] = child_type;
8097 }
8098 }
8099 child_die = sibling_die (child_die);
8100 }
8101
8102 /* Dwarf2 dimensions are output from left to right, create the
8103 necessary array types in backwards order. */
8104
8105 type = element_type;
8106
8107 if (read_array_order (die, cu) == DW_ORD_col_major)
8108 {
8109 int i = 0;
8110
8111 while (i < ndim)
8112 type = create_array_type (NULL, type, range_types[i++]);
8113 }
8114 else
8115 {
8116 while (ndim-- > 0)
8117 type = create_array_type (NULL, type, range_types[ndim]);
8118 }
8119
8120 /* Understand Dwarf2 support for vector types (like they occur on
8121 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8122 array type. This is not part of the Dwarf2/3 standard yet, but a
8123 custom vendor extension. The main difference between a regular
8124 array and the vector variant is that vectors are passed by value
8125 to functions. */
8126 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8127 if (attr)
8128 make_vector_type (type);
8129
8130 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8131 implementation may choose to implement triple vectors using this
8132 attribute. */
8133 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8134 if (attr)
8135 {
8136 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8137 TYPE_LENGTH (type) = DW_UNSND (attr);
8138 else
8139 complaint (&symfile_complaints,
8140 _("DW_AT_byte_size for array type smaller "
8141 "than the total size of elements"));
8142 }
8143
8144 name = dwarf2_name (die, cu);
8145 if (name)
8146 TYPE_NAME (type) = name;
8147
8148 /* Install the type in the die. */
8149 set_die_type (die, type, cu);
8150
8151 /* set_die_type should be already done. */
8152 set_descriptive_type (type, die, cu);
8153
8154 do_cleanups (back_to);
8155
8156 return type;
8157 }
8158
8159 static enum dwarf_array_dim_ordering
8160 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8161 {
8162 struct attribute *attr;
8163
8164 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8165
8166 if (attr) return DW_SND (attr);
8167
8168 /* GNU F77 is a special case, as at 08/2004 array type info is the
8169 opposite order to the dwarf2 specification, but data is still
8170 laid out as per normal fortran.
8171
8172 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8173 version checking. */
8174
8175 if (cu->language == language_fortran
8176 && cu->producer && strstr (cu->producer, "GNU F77"))
8177 {
8178 return DW_ORD_row_major;
8179 }
8180
8181 switch (cu->language_defn->la_array_ordering)
8182 {
8183 case array_column_major:
8184 return DW_ORD_col_major;
8185 case array_row_major:
8186 default:
8187 return DW_ORD_row_major;
8188 };
8189 }
8190
8191 /* Extract all information from a DW_TAG_set_type DIE and put it in
8192 the DIE's type field. */
8193
8194 static struct type *
8195 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8196 {
8197 struct type *domain_type, *set_type;
8198 struct attribute *attr;
8199
8200 domain_type = die_type (die, cu);
8201
8202 /* The die_type call above may have already set the type for this DIE. */
8203 set_type = get_die_type (die, cu);
8204 if (set_type)
8205 return set_type;
8206
8207 set_type = create_set_type (NULL, domain_type);
8208
8209 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8210 if (attr)
8211 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8212
8213 return set_die_type (die, set_type, cu);
8214 }
8215
8216 /* First cut: install each common block member as a global variable. */
8217
8218 static void
8219 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8220 {
8221 struct die_info *child_die;
8222 struct attribute *attr;
8223 struct symbol *sym;
8224 CORE_ADDR base = (CORE_ADDR) 0;
8225
8226 attr = dwarf2_attr (die, DW_AT_location, cu);
8227 if (attr)
8228 {
8229 /* Support the .debug_loc offsets. */
8230 if (attr_form_is_block (attr))
8231 {
8232 base = decode_locdesc (DW_BLOCK (attr), cu);
8233 }
8234 else if (attr_form_is_section_offset (attr))
8235 {
8236 dwarf2_complex_location_expr_complaint ();
8237 }
8238 else
8239 {
8240 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8241 "common block member");
8242 }
8243 }
8244 if (die->child != NULL)
8245 {
8246 child_die = die->child;
8247 while (child_die && child_die->tag)
8248 {
8249 LONGEST offset;
8250
8251 sym = new_symbol (child_die, NULL, cu);
8252 if (sym != NULL
8253 && handle_data_member_location (child_die, cu, &offset))
8254 {
8255 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8256 add_symbol_to_list (sym, &global_symbols);
8257 }
8258 child_die = sibling_die (child_die);
8259 }
8260 }
8261 }
8262
8263 /* Create a type for a C++ namespace. */
8264
8265 static struct type *
8266 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8267 {
8268 struct objfile *objfile = cu->objfile;
8269 const char *previous_prefix, *name;
8270 int is_anonymous;
8271 struct type *type;
8272
8273 /* For extensions, reuse the type of the original namespace. */
8274 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8275 {
8276 struct die_info *ext_die;
8277 struct dwarf2_cu *ext_cu = cu;
8278
8279 ext_die = dwarf2_extension (die, &ext_cu);
8280 type = read_type_die (ext_die, ext_cu);
8281
8282 /* EXT_CU may not be the same as CU.
8283 Ensure TYPE is recorded in CU's type_hash table. */
8284 return set_die_type (die, type, cu);
8285 }
8286
8287 name = namespace_name (die, &is_anonymous, cu);
8288
8289 /* Now build the name of the current namespace. */
8290
8291 previous_prefix = determine_prefix (die, cu);
8292 if (previous_prefix[0] != '\0')
8293 name = typename_concat (&objfile->objfile_obstack,
8294 previous_prefix, name, 0, cu);
8295
8296 /* Create the type. */
8297 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8298 objfile);
8299 TYPE_NAME (type) = (char *) name;
8300 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8301
8302 return set_die_type (die, type, cu);
8303 }
8304
8305 /* Read a C++ namespace. */
8306
8307 static void
8308 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8309 {
8310 struct objfile *objfile = cu->objfile;
8311 int is_anonymous;
8312
8313 /* Add a symbol associated to this if we haven't seen the namespace
8314 before. Also, add a using directive if it's an anonymous
8315 namespace. */
8316
8317 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8318 {
8319 struct type *type;
8320
8321 type = read_type_die (die, cu);
8322 new_symbol (die, type, cu);
8323
8324 namespace_name (die, &is_anonymous, cu);
8325 if (is_anonymous)
8326 {
8327 const char *previous_prefix = determine_prefix (die, cu);
8328
8329 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8330 NULL, NULL, &objfile->objfile_obstack);
8331 }
8332 }
8333
8334 if (die->child != NULL)
8335 {
8336 struct die_info *child_die = die->child;
8337
8338 while (child_die && child_die->tag)
8339 {
8340 process_die (child_die, cu);
8341 child_die = sibling_die (child_die);
8342 }
8343 }
8344 }
8345
8346 /* Read a Fortran module as type. This DIE can be only a declaration used for
8347 imported module. Still we need that type as local Fortran "use ... only"
8348 declaration imports depend on the created type in determine_prefix. */
8349
8350 static struct type *
8351 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8352 {
8353 struct objfile *objfile = cu->objfile;
8354 char *module_name;
8355 struct type *type;
8356
8357 module_name = dwarf2_name (die, cu);
8358 if (!module_name)
8359 complaint (&symfile_complaints,
8360 _("DW_TAG_module has no name, offset 0x%x"),
8361 die->offset);
8362 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8363
8364 /* determine_prefix uses TYPE_TAG_NAME. */
8365 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8366
8367 return set_die_type (die, type, cu);
8368 }
8369
8370 /* Read a Fortran module. */
8371
8372 static void
8373 read_module (struct die_info *die, struct dwarf2_cu *cu)
8374 {
8375 struct die_info *child_die = die->child;
8376
8377 while (child_die && child_die->tag)
8378 {
8379 process_die (child_die, cu);
8380 child_die = sibling_die (child_die);
8381 }
8382 }
8383
8384 /* Return the name of the namespace represented by DIE. Set
8385 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8386 namespace. */
8387
8388 static const char *
8389 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8390 {
8391 struct die_info *current_die;
8392 const char *name = NULL;
8393
8394 /* Loop through the extensions until we find a name. */
8395
8396 for (current_die = die;
8397 current_die != NULL;
8398 current_die = dwarf2_extension (die, &cu))
8399 {
8400 name = dwarf2_name (current_die, cu);
8401 if (name != NULL)
8402 break;
8403 }
8404
8405 /* Is it an anonymous namespace? */
8406
8407 *is_anonymous = (name == NULL);
8408 if (*is_anonymous)
8409 name = CP_ANONYMOUS_NAMESPACE_STR;
8410
8411 return name;
8412 }
8413
8414 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8415 the user defined type vector. */
8416
8417 static struct type *
8418 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8419 {
8420 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8421 struct comp_unit_head *cu_header = &cu->header;
8422 struct type *type;
8423 struct attribute *attr_byte_size;
8424 struct attribute *attr_address_class;
8425 int byte_size, addr_class;
8426 struct type *target_type;
8427
8428 target_type = die_type (die, cu);
8429
8430 /* The die_type call above may have already set the type for this DIE. */
8431 type = get_die_type (die, cu);
8432 if (type)
8433 return type;
8434
8435 type = lookup_pointer_type (target_type);
8436
8437 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8438 if (attr_byte_size)
8439 byte_size = DW_UNSND (attr_byte_size);
8440 else
8441 byte_size = cu_header->addr_size;
8442
8443 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8444 if (attr_address_class)
8445 addr_class = DW_UNSND (attr_address_class);
8446 else
8447 addr_class = DW_ADDR_none;
8448
8449 /* If the pointer size or address class is different than the
8450 default, create a type variant marked as such and set the
8451 length accordingly. */
8452 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8453 {
8454 if (gdbarch_address_class_type_flags_p (gdbarch))
8455 {
8456 int type_flags;
8457
8458 type_flags = gdbarch_address_class_type_flags
8459 (gdbarch, byte_size, addr_class);
8460 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8461 == 0);
8462 type = make_type_with_address_space (type, type_flags);
8463 }
8464 else if (TYPE_LENGTH (type) != byte_size)
8465 {
8466 complaint (&symfile_complaints,
8467 _("invalid pointer size %d"), byte_size);
8468 }
8469 else
8470 {
8471 /* Should we also complain about unhandled address classes? */
8472 }
8473 }
8474
8475 TYPE_LENGTH (type) = byte_size;
8476 return set_die_type (die, type, cu);
8477 }
8478
8479 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8480 the user defined type vector. */
8481
8482 static struct type *
8483 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8484 {
8485 struct type *type;
8486 struct type *to_type;
8487 struct type *domain;
8488
8489 to_type = die_type (die, cu);
8490 domain = die_containing_type (die, cu);
8491
8492 /* The calls above may have already set the type for this DIE. */
8493 type = get_die_type (die, cu);
8494 if (type)
8495 return type;
8496
8497 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8498 type = lookup_methodptr_type (to_type);
8499 else
8500 type = lookup_memberptr_type (to_type, domain);
8501
8502 return set_die_type (die, type, cu);
8503 }
8504
8505 /* Extract all information from a DW_TAG_reference_type DIE and add to
8506 the user defined type vector. */
8507
8508 static struct type *
8509 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8510 {
8511 struct comp_unit_head *cu_header = &cu->header;
8512 struct type *type, *target_type;
8513 struct attribute *attr;
8514
8515 target_type = die_type (die, cu);
8516
8517 /* The die_type call above may have already set the type for this DIE. */
8518 type = get_die_type (die, cu);
8519 if (type)
8520 return type;
8521
8522 type = lookup_reference_type (target_type);
8523 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8524 if (attr)
8525 {
8526 TYPE_LENGTH (type) = DW_UNSND (attr);
8527 }
8528 else
8529 {
8530 TYPE_LENGTH (type) = cu_header->addr_size;
8531 }
8532 return set_die_type (die, type, cu);
8533 }
8534
8535 static struct type *
8536 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8537 {
8538 struct type *base_type, *cv_type;
8539
8540 base_type = die_type (die, cu);
8541
8542 /* The die_type call above may have already set the type for this DIE. */
8543 cv_type = get_die_type (die, cu);
8544 if (cv_type)
8545 return cv_type;
8546
8547 /* In case the const qualifier is applied to an array type, the element type
8548 is so qualified, not the array type (section 6.7.3 of C99). */
8549 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8550 {
8551 struct type *el_type, *inner_array;
8552
8553 base_type = copy_type (base_type);
8554 inner_array = base_type;
8555
8556 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8557 {
8558 TYPE_TARGET_TYPE (inner_array) =
8559 copy_type (TYPE_TARGET_TYPE (inner_array));
8560 inner_array = TYPE_TARGET_TYPE (inner_array);
8561 }
8562
8563 el_type = TYPE_TARGET_TYPE (inner_array);
8564 TYPE_TARGET_TYPE (inner_array) =
8565 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8566
8567 return set_die_type (die, base_type, cu);
8568 }
8569
8570 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8571 return set_die_type (die, cv_type, cu);
8572 }
8573
8574 static struct type *
8575 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8576 {
8577 struct type *base_type, *cv_type;
8578
8579 base_type = die_type (die, cu);
8580
8581 /* The die_type call above may have already set the type for this DIE. */
8582 cv_type = get_die_type (die, cu);
8583 if (cv_type)
8584 return cv_type;
8585
8586 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8587 return set_die_type (die, cv_type, cu);
8588 }
8589
8590 /* Extract all information from a DW_TAG_string_type DIE and add to
8591 the user defined type vector. It isn't really a user defined type,
8592 but it behaves like one, with other DIE's using an AT_user_def_type
8593 attribute to reference it. */
8594
8595 static struct type *
8596 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8597 {
8598 struct objfile *objfile = cu->objfile;
8599 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8600 struct type *type, *range_type, *index_type, *char_type;
8601 struct attribute *attr;
8602 unsigned int length;
8603
8604 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8605 if (attr)
8606 {
8607 length = DW_UNSND (attr);
8608 }
8609 else
8610 {
8611 /* Check for the DW_AT_byte_size attribute. */
8612 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8613 if (attr)
8614 {
8615 length = DW_UNSND (attr);
8616 }
8617 else
8618 {
8619 length = 1;
8620 }
8621 }
8622
8623 index_type = objfile_type (objfile)->builtin_int;
8624 range_type = create_range_type (NULL, index_type, 1, length);
8625 char_type = language_string_char_type (cu->language_defn, gdbarch);
8626 type = create_string_type (NULL, char_type, range_type);
8627
8628 return set_die_type (die, type, cu);
8629 }
8630
8631 /* Handle DIES due to C code like:
8632
8633 struct foo
8634 {
8635 int (*funcp)(int a, long l);
8636 int b;
8637 };
8638
8639 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8640
8641 static struct type *
8642 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8643 {
8644 struct objfile *objfile = cu->objfile;
8645 struct type *type; /* Type that this function returns. */
8646 struct type *ftype; /* Function that returns above type. */
8647 struct attribute *attr;
8648
8649 type = die_type (die, cu);
8650
8651 /* The die_type call above may have already set the type for this DIE. */
8652 ftype = get_die_type (die, cu);
8653 if (ftype)
8654 return ftype;
8655
8656 ftype = lookup_function_type (type);
8657
8658 /* All functions in C++, Pascal and Java have prototypes. */
8659 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8660 if ((attr && (DW_UNSND (attr) != 0))
8661 || cu->language == language_cplus
8662 || cu->language == language_java
8663 || cu->language == language_pascal)
8664 TYPE_PROTOTYPED (ftype) = 1;
8665 else if (producer_is_realview (cu->producer))
8666 /* RealView does not emit DW_AT_prototyped. We can not
8667 distinguish prototyped and unprototyped functions; default to
8668 prototyped, since that is more common in modern code (and
8669 RealView warns about unprototyped functions). */
8670 TYPE_PROTOTYPED (ftype) = 1;
8671
8672 /* Store the calling convention in the type if it's available in
8673 the subroutine die. Otherwise set the calling convention to
8674 the default value DW_CC_normal. */
8675 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8676 if (attr)
8677 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8678 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8679 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8680 else
8681 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8682
8683 /* We need to add the subroutine type to the die immediately so
8684 we don't infinitely recurse when dealing with parameters
8685 declared as the same subroutine type. */
8686 set_die_type (die, ftype, cu);
8687
8688 if (die->child != NULL)
8689 {
8690 struct type *void_type = objfile_type (objfile)->builtin_void;
8691 struct die_info *child_die;
8692 int nparams, iparams;
8693
8694 /* Count the number of parameters.
8695 FIXME: GDB currently ignores vararg functions, but knows about
8696 vararg member functions. */
8697 nparams = 0;
8698 child_die = die->child;
8699 while (child_die && child_die->tag)
8700 {
8701 if (child_die->tag == DW_TAG_formal_parameter)
8702 nparams++;
8703 else if (child_die->tag == DW_TAG_unspecified_parameters)
8704 TYPE_VARARGS (ftype) = 1;
8705 child_die = sibling_die (child_die);
8706 }
8707
8708 /* Allocate storage for parameters and fill them in. */
8709 TYPE_NFIELDS (ftype) = nparams;
8710 TYPE_FIELDS (ftype) = (struct field *)
8711 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8712
8713 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8714 even if we error out during the parameters reading below. */
8715 for (iparams = 0; iparams < nparams; iparams++)
8716 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8717
8718 iparams = 0;
8719 child_die = die->child;
8720 while (child_die && child_die->tag)
8721 {
8722 if (child_die->tag == DW_TAG_formal_parameter)
8723 {
8724 struct type *arg_type;
8725
8726 /* DWARF version 2 has no clean way to discern C++
8727 static and non-static member functions. G++ helps
8728 GDB by marking the first parameter for non-static
8729 member functions (which is the this pointer) as
8730 artificial. We pass this information to
8731 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8732
8733 DWARF version 3 added DW_AT_object_pointer, which GCC
8734 4.5 does not yet generate. */
8735 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8736 if (attr)
8737 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8738 else
8739 {
8740 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8741
8742 /* GCC/43521: In java, the formal parameter
8743 "this" is sometimes not marked with DW_AT_artificial. */
8744 if (cu->language == language_java)
8745 {
8746 const char *name = dwarf2_name (child_die, cu);
8747
8748 if (name && !strcmp (name, "this"))
8749 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8750 }
8751 }
8752 arg_type = die_type (child_die, cu);
8753
8754 /* RealView does not mark THIS as const, which the testsuite
8755 expects. GCC marks THIS as const in method definitions,
8756 but not in the class specifications (GCC PR 43053). */
8757 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8758 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8759 {
8760 int is_this = 0;
8761 struct dwarf2_cu *arg_cu = cu;
8762 const char *name = dwarf2_name (child_die, cu);
8763
8764 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8765 if (attr)
8766 {
8767 /* If the compiler emits this, use it. */
8768 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8769 is_this = 1;
8770 }
8771 else if (name && strcmp (name, "this") == 0)
8772 /* Function definitions will have the argument names. */
8773 is_this = 1;
8774 else if (name == NULL && iparams == 0)
8775 /* Declarations may not have the names, so like
8776 elsewhere in GDB, assume an artificial first
8777 argument is "this". */
8778 is_this = 1;
8779
8780 if (is_this)
8781 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8782 arg_type, 0);
8783 }
8784
8785 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8786 iparams++;
8787 }
8788 child_die = sibling_die (child_die);
8789 }
8790 }
8791
8792 return ftype;
8793 }
8794
8795 static struct type *
8796 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8797 {
8798 struct objfile *objfile = cu->objfile;
8799 const char *name = NULL;
8800 struct type *this_type, *target_type;
8801
8802 name = dwarf2_full_name (NULL, die, cu);
8803 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8804 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8805 TYPE_NAME (this_type) = (char *) name;
8806 set_die_type (die, this_type, cu);
8807 target_type = die_type (die, cu);
8808 if (target_type != this_type)
8809 TYPE_TARGET_TYPE (this_type) = target_type;
8810 else
8811 {
8812 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8813 spec and cause infinite loops in GDB. */
8814 complaint (&symfile_complaints,
8815 _("Self-referential DW_TAG_typedef "
8816 "- DIE at 0x%x [in module %s]"),
8817 die->offset, objfile->name);
8818 TYPE_TARGET_TYPE (this_type) = NULL;
8819 }
8820 return this_type;
8821 }
8822
8823 /* Find a representation of a given base type and install
8824 it in the TYPE field of the die. */
8825
8826 static struct type *
8827 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8828 {
8829 struct objfile *objfile = cu->objfile;
8830 struct type *type;
8831 struct attribute *attr;
8832 int encoding = 0, size = 0;
8833 char *name;
8834 enum type_code code = TYPE_CODE_INT;
8835 int type_flags = 0;
8836 struct type *target_type = NULL;
8837
8838 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8839 if (attr)
8840 {
8841 encoding = DW_UNSND (attr);
8842 }
8843 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8844 if (attr)
8845 {
8846 size = DW_UNSND (attr);
8847 }
8848 name = dwarf2_name (die, cu);
8849 if (!name)
8850 {
8851 complaint (&symfile_complaints,
8852 _("DW_AT_name missing from DW_TAG_base_type"));
8853 }
8854
8855 switch (encoding)
8856 {
8857 case DW_ATE_address:
8858 /* Turn DW_ATE_address into a void * pointer. */
8859 code = TYPE_CODE_PTR;
8860 type_flags |= TYPE_FLAG_UNSIGNED;
8861 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8862 break;
8863 case DW_ATE_boolean:
8864 code = TYPE_CODE_BOOL;
8865 type_flags |= TYPE_FLAG_UNSIGNED;
8866 break;
8867 case DW_ATE_complex_float:
8868 code = TYPE_CODE_COMPLEX;
8869 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8870 break;
8871 case DW_ATE_decimal_float:
8872 code = TYPE_CODE_DECFLOAT;
8873 break;
8874 case DW_ATE_float:
8875 code = TYPE_CODE_FLT;
8876 break;
8877 case DW_ATE_signed:
8878 break;
8879 case DW_ATE_unsigned:
8880 type_flags |= TYPE_FLAG_UNSIGNED;
8881 if (cu->language == language_fortran
8882 && name
8883 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8884 code = TYPE_CODE_CHAR;
8885 break;
8886 case DW_ATE_signed_char:
8887 if (cu->language == language_ada || cu->language == language_m2
8888 || cu->language == language_pascal
8889 || cu->language == language_fortran)
8890 code = TYPE_CODE_CHAR;
8891 break;
8892 case DW_ATE_unsigned_char:
8893 if (cu->language == language_ada || cu->language == language_m2
8894 || cu->language == language_pascal
8895 || cu->language == language_fortran)
8896 code = TYPE_CODE_CHAR;
8897 type_flags |= TYPE_FLAG_UNSIGNED;
8898 break;
8899 case DW_ATE_UTF:
8900 /* We just treat this as an integer and then recognize the
8901 type by name elsewhere. */
8902 break;
8903
8904 default:
8905 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8906 dwarf_type_encoding_name (encoding));
8907 break;
8908 }
8909
8910 type = init_type (code, size, type_flags, NULL, objfile);
8911 TYPE_NAME (type) = name;
8912 TYPE_TARGET_TYPE (type) = target_type;
8913
8914 if (name && strcmp (name, "char") == 0)
8915 TYPE_NOSIGN (type) = 1;
8916
8917 return set_die_type (die, type, cu);
8918 }
8919
8920 /* Read the given DW_AT_subrange DIE. */
8921
8922 static struct type *
8923 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8924 {
8925 struct type *base_type;
8926 struct type *range_type;
8927 struct attribute *attr;
8928 LONGEST low = 0;
8929 LONGEST high = -1;
8930 char *name;
8931 LONGEST negative_mask;
8932
8933 base_type = die_type (die, cu);
8934 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8935 check_typedef (base_type);
8936
8937 /* The die_type call above may have already set the type for this DIE. */
8938 range_type = get_die_type (die, cu);
8939 if (range_type)
8940 return range_type;
8941
8942 if (cu->language == language_fortran)
8943 {
8944 /* FORTRAN implies a lower bound of 1, if not given. */
8945 low = 1;
8946 }
8947
8948 /* FIXME: For variable sized arrays either of these could be
8949 a variable rather than a constant value. We'll allow it,
8950 but we don't know how to handle it. */
8951 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8952 if (attr)
8953 low = dwarf2_get_attr_constant_value (attr, 0);
8954
8955 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8956 if (attr)
8957 {
8958 if (attr_form_is_block (attr) || is_ref_attr (attr))
8959 {
8960 /* GCC encodes arrays with unspecified or dynamic length
8961 with a DW_FORM_block1 attribute or a reference attribute.
8962 FIXME: GDB does not yet know how to handle dynamic
8963 arrays properly, treat them as arrays with unspecified
8964 length for now.
8965
8966 FIXME: jimb/2003-09-22: GDB does not really know
8967 how to handle arrays of unspecified length
8968 either; we just represent them as zero-length
8969 arrays. Choose an appropriate upper bound given
8970 the lower bound we've computed above. */
8971 high = low - 1;
8972 }
8973 else
8974 high = dwarf2_get_attr_constant_value (attr, 1);
8975 }
8976 else
8977 {
8978 attr = dwarf2_attr (die, DW_AT_count, cu);
8979 if (attr)
8980 {
8981 int count = dwarf2_get_attr_constant_value (attr, 1);
8982 high = low + count - 1;
8983 }
8984 else
8985 {
8986 /* Unspecified array length. */
8987 high = low - 1;
8988 }
8989 }
8990
8991 /* Dwarf-2 specifications explicitly allows to create subrange types
8992 without specifying a base type.
8993 In that case, the base type must be set to the type of
8994 the lower bound, upper bound or count, in that order, if any of these
8995 three attributes references an object that has a type.
8996 If no base type is found, the Dwarf-2 specifications say that
8997 a signed integer type of size equal to the size of an address should
8998 be used.
8999 For the following C code: `extern char gdb_int [];'
9000 GCC produces an empty range DIE.
9001 FIXME: muller/2010-05-28: Possible references to object for low bound,
9002 high bound or count are not yet handled by this code. */
9003 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9004 {
9005 struct objfile *objfile = cu->objfile;
9006 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9007 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9008 struct type *int_type = objfile_type (objfile)->builtin_int;
9009
9010 /* Test "int", "long int", and "long long int" objfile types,
9011 and select the first one having a size above or equal to the
9012 architecture address size. */
9013 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9014 base_type = int_type;
9015 else
9016 {
9017 int_type = objfile_type (objfile)->builtin_long;
9018 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9019 base_type = int_type;
9020 else
9021 {
9022 int_type = objfile_type (objfile)->builtin_long_long;
9023 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9024 base_type = int_type;
9025 }
9026 }
9027 }
9028
9029 negative_mask =
9030 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9031 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9032 low |= negative_mask;
9033 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9034 high |= negative_mask;
9035
9036 range_type = create_range_type (NULL, base_type, low, high);
9037
9038 /* Mark arrays with dynamic length at least as an array of unspecified
9039 length. GDB could check the boundary but before it gets implemented at
9040 least allow accessing the array elements. */
9041 if (attr && attr_form_is_block (attr))
9042 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9043
9044 /* Ada expects an empty array on no boundary attributes. */
9045 if (attr == NULL && cu->language != language_ada)
9046 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9047
9048 name = dwarf2_name (die, cu);
9049 if (name)
9050 TYPE_NAME (range_type) = name;
9051
9052 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9053 if (attr)
9054 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9055
9056 set_die_type (die, range_type, cu);
9057
9058 /* set_die_type should be already done. */
9059 set_descriptive_type (range_type, die, cu);
9060
9061 return range_type;
9062 }
9063
9064 static struct type *
9065 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9066 {
9067 struct type *type;
9068
9069 /* For now, we only support the C meaning of an unspecified type: void. */
9070
9071 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9072 TYPE_NAME (type) = dwarf2_name (die, cu);
9073
9074 return set_die_type (die, type, cu);
9075 }
9076
9077 /* Trivial hash function for die_info: the hash value of a DIE
9078 is its offset in .debug_info for this objfile. */
9079
9080 static hashval_t
9081 die_hash (const void *item)
9082 {
9083 const struct die_info *die = item;
9084
9085 return die->offset;
9086 }
9087
9088 /* Trivial comparison function for die_info structures: two DIEs
9089 are equal if they have the same offset. */
9090
9091 static int
9092 die_eq (const void *item_lhs, const void *item_rhs)
9093 {
9094 const struct die_info *die_lhs = item_lhs;
9095 const struct die_info *die_rhs = item_rhs;
9096
9097 return die_lhs->offset == die_rhs->offset;
9098 }
9099
9100 /* Read a whole compilation unit into a linked list of dies. */
9101
9102 static struct die_info *
9103 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9104 {
9105 struct die_reader_specs reader_specs;
9106 int read_abbrevs = 0;
9107 struct cleanup *back_to = NULL;
9108 struct die_info *die;
9109
9110 if (cu->dwarf2_abbrevs == NULL)
9111 {
9112 dwarf2_read_abbrevs (cu);
9113 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9114 read_abbrevs = 1;
9115 }
9116
9117 gdb_assert (cu->die_hash == NULL);
9118 cu->die_hash
9119 = htab_create_alloc_ex (cu->header.length / 12,
9120 die_hash,
9121 die_eq,
9122 NULL,
9123 &cu->comp_unit_obstack,
9124 hashtab_obstack_allocate,
9125 dummy_obstack_deallocate);
9126
9127 init_cu_die_reader (&reader_specs, cu);
9128
9129 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9130
9131 if (read_abbrevs)
9132 do_cleanups (back_to);
9133
9134 return die;
9135 }
9136
9137 /* Main entry point for reading a DIE and all children.
9138 Read the DIE and dump it if requested. */
9139
9140 static struct die_info *
9141 read_die_and_children (const struct die_reader_specs *reader,
9142 gdb_byte *info_ptr,
9143 gdb_byte **new_info_ptr,
9144 struct die_info *parent)
9145 {
9146 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9147 new_info_ptr, parent);
9148
9149 if (dwarf2_die_debug)
9150 {
9151 fprintf_unfiltered (gdb_stdlog,
9152 "\nRead die from %s of %s:\n",
9153 (reader->cu->per_cu->debug_types_section
9154 ? ".debug_types"
9155 : ".debug_info"),
9156 reader->abfd->filename);
9157 dump_die (result, dwarf2_die_debug);
9158 }
9159
9160 return result;
9161 }
9162
9163 /* Read a single die and all its descendents. Set the die's sibling
9164 field to NULL; set other fields in the die correctly, and set all
9165 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9166 location of the info_ptr after reading all of those dies. PARENT
9167 is the parent of the die in question. */
9168
9169 static struct die_info *
9170 read_die_and_children_1 (const struct die_reader_specs *reader,
9171 gdb_byte *info_ptr,
9172 gdb_byte **new_info_ptr,
9173 struct die_info *parent)
9174 {
9175 struct die_info *die;
9176 gdb_byte *cur_ptr;
9177 int has_children;
9178
9179 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9180 if (die == NULL)
9181 {
9182 *new_info_ptr = cur_ptr;
9183 return NULL;
9184 }
9185 store_in_ref_table (die, reader->cu);
9186
9187 if (has_children)
9188 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9189 else
9190 {
9191 die->child = NULL;
9192 *new_info_ptr = cur_ptr;
9193 }
9194
9195 die->sibling = NULL;
9196 die->parent = parent;
9197 return die;
9198 }
9199
9200 /* Read a die, all of its descendents, and all of its siblings; set
9201 all of the fields of all of the dies correctly. Arguments are as
9202 in read_die_and_children. */
9203
9204 static struct die_info *
9205 read_die_and_siblings (const struct die_reader_specs *reader,
9206 gdb_byte *info_ptr,
9207 gdb_byte **new_info_ptr,
9208 struct die_info *parent)
9209 {
9210 struct die_info *first_die, *last_sibling;
9211 gdb_byte *cur_ptr;
9212
9213 cur_ptr = info_ptr;
9214 first_die = last_sibling = NULL;
9215
9216 while (1)
9217 {
9218 struct die_info *die
9219 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9220
9221 if (die == NULL)
9222 {
9223 *new_info_ptr = cur_ptr;
9224 return first_die;
9225 }
9226
9227 if (!first_die)
9228 first_die = die;
9229 else
9230 last_sibling->sibling = die;
9231
9232 last_sibling = die;
9233 }
9234 }
9235
9236 /* Read the die from the .debug_info section buffer. Set DIEP to
9237 point to a newly allocated die with its information, except for its
9238 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9239 whether the die has children or not. */
9240
9241 static gdb_byte *
9242 read_full_die (const struct die_reader_specs *reader,
9243 struct die_info **diep, gdb_byte *info_ptr,
9244 int *has_children)
9245 {
9246 unsigned int abbrev_number, bytes_read, i, offset;
9247 struct abbrev_info *abbrev;
9248 struct die_info *die;
9249 struct dwarf2_cu *cu = reader->cu;
9250 bfd *abfd = reader->abfd;
9251
9252 offset = info_ptr - reader->buffer;
9253 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9254 info_ptr += bytes_read;
9255 if (!abbrev_number)
9256 {
9257 *diep = NULL;
9258 *has_children = 0;
9259 return info_ptr;
9260 }
9261
9262 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9263 if (!abbrev)
9264 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9265 abbrev_number,
9266 bfd_get_filename (abfd));
9267
9268 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9269 die->offset = offset;
9270 die->tag = abbrev->tag;
9271 die->abbrev = abbrev_number;
9272
9273 die->num_attrs = abbrev->num_attrs;
9274
9275 for (i = 0; i < abbrev->num_attrs; ++i)
9276 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9277 abfd, info_ptr, cu);
9278
9279 *diep = die;
9280 *has_children = abbrev->has_children;
9281 return info_ptr;
9282 }
9283
9284 /* In DWARF version 2, the description of the debugging information is
9285 stored in a separate .debug_abbrev section. Before we read any
9286 dies from a section we read in all abbreviations and install them
9287 in a hash table. This function also sets flags in CU describing
9288 the data found in the abbrev table. */
9289
9290 static void
9291 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9292 {
9293 bfd *abfd = cu->objfile->obfd;
9294 struct comp_unit_head *cu_header = &cu->header;
9295 gdb_byte *abbrev_ptr;
9296 struct abbrev_info *cur_abbrev;
9297 unsigned int abbrev_number, bytes_read, abbrev_name;
9298 unsigned int abbrev_form, hash_number;
9299 struct attr_abbrev *cur_attrs;
9300 unsigned int allocated_attrs;
9301
9302 /* Initialize dwarf2 abbrevs. */
9303 obstack_init (&cu->abbrev_obstack);
9304 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9305 (ABBREV_HASH_SIZE
9306 * sizeof (struct abbrev_info *)));
9307 memset (cu->dwarf2_abbrevs, 0,
9308 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9309
9310 dwarf2_read_section (dwarf2_per_objfile->objfile,
9311 &dwarf2_per_objfile->abbrev);
9312 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9313 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9314 abbrev_ptr += bytes_read;
9315
9316 allocated_attrs = ATTR_ALLOC_CHUNK;
9317 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9318
9319 /* Loop until we reach an abbrev number of 0. */
9320 while (abbrev_number)
9321 {
9322 cur_abbrev = dwarf_alloc_abbrev (cu);
9323
9324 /* read in abbrev header */
9325 cur_abbrev->number = abbrev_number;
9326 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9327 abbrev_ptr += bytes_read;
9328 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9329 abbrev_ptr += 1;
9330
9331 /* now read in declarations */
9332 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9333 abbrev_ptr += bytes_read;
9334 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9335 abbrev_ptr += bytes_read;
9336 while (abbrev_name)
9337 {
9338 if (cur_abbrev->num_attrs == allocated_attrs)
9339 {
9340 allocated_attrs += ATTR_ALLOC_CHUNK;
9341 cur_attrs
9342 = xrealloc (cur_attrs, (allocated_attrs
9343 * sizeof (struct attr_abbrev)));
9344 }
9345
9346 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9347 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9348 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9349 abbrev_ptr += bytes_read;
9350 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9351 abbrev_ptr += bytes_read;
9352 }
9353
9354 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9355 (cur_abbrev->num_attrs
9356 * sizeof (struct attr_abbrev)));
9357 memcpy (cur_abbrev->attrs, cur_attrs,
9358 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9359
9360 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9361 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9362 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9363
9364 /* Get next abbreviation.
9365 Under Irix6 the abbreviations for a compilation unit are not
9366 always properly terminated with an abbrev number of 0.
9367 Exit loop if we encounter an abbreviation which we have
9368 already read (which means we are about to read the abbreviations
9369 for the next compile unit) or if the end of the abbreviation
9370 table is reached. */
9371 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9372 >= dwarf2_per_objfile->abbrev.size)
9373 break;
9374 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9375 abbrev_ptr += bytes_read;
9376 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9377 break;
9378 }
9379
9380 xfree (cur_attrs);
9381 }
9382
9383 /* Release the memory used by the abbrev table for a compilation unit. */
9384
9385 static void
9386 dwarf2_free_abbrev_table (void *ptr_to_cu)
9387 {
9388 struct dwarf2_cu *cu = ptr_to_cu;
9389
9390 obstack_free (&cu->abbrev_obstack, NULL);
9391 cu->dwarf2_abbrevs = NULL;
9392 }
9393
9394 /* Lookup an abbrev_info structure in the abbrev hash table. */
9395
9396 static struct abbrev_info *
9397 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9398 {
9399 unsigned int hash_number;
9400 struct abbrev_info *abbrev;
9401
9402 hash_number = number % ABBREV_HASH_SIZE;
9403 abbrev = cu->dwarf2_abbrevs[hash_number];
9404
9405 while (abbrev)
9406 {
9407 if (abbrev->number == number)
9408 return abbrev;
9409 else
9410 abbrev = abbrev->next;
9411 }
9412 return NULL;
9413 }
9414
9415 /* Returns nonzero if TAG represents a type that we might generate a partial
9416 symbol for. */
9417
9418 static int
9419 is_type_tag_for_partial (int tag)
9420 {
9421 switch (tag)
9422 {
9423 #if 0
9424 /* Some types that would be reasonable to generate partial symbols for,
9425 that we don't at present. */
9426 case DW_TAG_array_type:
9427 case DW_TAG_file_type:
9428 case DW_TAG_ptr_to_member_type:
9429 case DW_TAG_set_type:
9430 case DW_TAG_string_type:
9431 case DW_TAG_subroutine_type:
9432 #endif
9433 case DW_TAG_base_type:
9434 case DW_TAG_class_type:
9435 case DW_TAG_interface_type:
9436 case DW_TAG_enumeration_type:
9437 case DW_TAG_structure_type:
9438 case DW_TAG_subrange_type:
9439 case DW_TAG_typedef:
9440 case DW_TAG_union_type:
9441 return 1;
9442 default:
9443 return 0;
9444 }
9445 }
9446
9447 /* Load all DIEs that are interesting for partial symbols into memory. */
9448
9449 static struct partial_die_info *
9450 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9451 int building_psymtab, struct dwarf2_cu *cu)
9452 {
9453 struct objfile *objfile = cu->objfile;
9454 struct partial_die_info *part_die;
9455 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9456 struct abbrev_info *abbrev;
9457 unsigned int bytes_read;
9458 unsigned int load_all = 0;
9459
9460 int nesting_level = 1;
9461
9462 parent_die = NULL;
9463 last_die = NULL;
9464
9465 if (cu->per_cu && cu->per_cu->load_all_dies)
9466 load_all = 1;
9467
9468 cu->partial_dies
9469 = htab_create_alloc_ex (cu->header.length / 12,
9470 partial_die_hash,
9471 partial_die_eq,
9472 NULL,
9473 &cu->comp_unit_obstack,
9474 hashtab_obstack_allocate,
9475 dummy_obstack_deallocate);
9476
9477 part_die = obstack_alloc (&cu->comp_unit_obstack,
9478 sizeof (struct partial_die_info));
9479
9480 while (1)
9481 {
9482 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9483
9484 /* A NULL abbrev means the end of a series of children. */
9485 if (abbrev == NULL)
9486 {
9487 if (--nesting_level == 0)
9488 {
9489 /* PART_DIE was probably the last thing allocated on the
9490 comp_unit_obstack, so we could call obstack_free
9491 here. We don't do that because the waste is small,
9492 and will be cleaned up when we're done with this
9493 compilation unit. This way, we're also more robust
9494 against other users of the comp_unit_obstack. */
9495 return first_die;
9496 }
9497 info_ptr += bytes_read;
9498 last_die = parent_die;
9499 parent_die = parent_die->die_parent;
9500 continue;
9501 }
9502
9503 /* Check for template arguments. We never save these; if
9504 they're seen, we just mark the parent, and go on our way. */
9505 if (parent_die != NULL
9506 && cu->language == language_cplus
9507 && (abbrev->tag == DW_TAG_template_type_param
9508 || abbrev->tag == DW_TAG_template_value_param))
9509 {
9510 parent_die->has_template_arguments = 1;
9511
9512 if (!load_all)
9513 {
9514 /* We don't need a partial DIE for the template argument. */
9515 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9516 cu);
9517 continue;
9518 }
9519 }
9520
9521 /* We only recurse into subprograms looking for template arguments.
9522 Skip their other children. */
9523 if (!load_all
9524 && cu->language == language_cplus
9525 && parent_die != NULL
9526 && parent_die->tag == DW_TAG_subprogram)
9527 {
9528 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9529 continue;
9530 }
9531
9532 /* Check whether this DIE is interesting enough to save. Normally
9533 we would not be interested in members here, but there may be
9534 later variables referencing them via DW_AT_specification (for
9535 static members). */
9536 if (!load_all
9537 && !is_type_tag_for_partial (abbrev->tag)
9538 && abbrev->tag != DW_TAG_constant
9539 && abbrev->tag != DW_TAG_enumerator
9540 && abbrev->tag != DW_TAG_subprogram
9541 && abbrev->tag != DW_TAG_lexical_block
9542 && abbrev->tag != DW_TAG_variable
9543 && abbrev->tag != DW_TAG_namespace
9544 && abbrev->tag != DW_TAG_module
9545 && abbrev->tag != DW_TAG_member)
9546 {
9547 /* Otherwise we skip to the next sibling, if any. */
9548 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9549 continue;
9550 }
9551
9552 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9553 buffer, info_ptr, cu);
9554
9555 /* This two-pass algorithm for processing partial symbols has a
9556 high cost in cache pressure. Thus, handle some simple cases
9557 here which cover the majority of C partial symbols. DIEs
9558 which neither have specification tags in them, nor could have
9559 specification tags elsewhere pointing at them, can simply be
9560 processed and discarded.
9561
9562 This segment is also optional; scan_partial_symbols and
9563 add_partial_symbol will handle these DIEs if we chain
9564 them in normally. When compilers which do not emit large
9565 quantities of duplicate debug information are more common,
9566 this code can probably be removed. */
9567
9568 /* Any complete simple types at the top level (pretty much all
9569 of them, for a language without namespaces), can be processed
9570 directly. */
9571 if (parent_die == NULL
9572 && part_die->has_specification == 0
9573 && part_die->is_declaration == 0
9574 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9575 || part_die->tag == DW_TAG_base_type
9576 || part_die->tag == DW_TAG_subrange_type))
9577 {
9578 if (building_psymtab && part_die->name != NULL)
9579 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9580 VAR_DOMAIN, LOC_TYPEDEF,
9581 &objfile->static_psymbols,
9582 0, (CORE_ADDR) 0, cu->language, objfile);
9583 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9584 continue;
9585 }
9586
9587 /* The exception for DW_TAG_typedef with has_children above is
9588 a workaround of GCC PR debug/47510. In the case of this complaint
9589 type_name_no_tag_or_error will error on such types later.
9590
9591 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9592 it could not find the child DIEs referenced later, this is checked
9593 above. In correct DWARF DW_TAG_typedef should have no children. */
9594
9595 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9596 complaint (&symfile_complaints,
9597 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9598 "- DIE at 0x%x [in module %s]"),
9599 part_die->offset, objfile->name);
9600
9601 /* If we're at the second level, and we're an enumerator, and
9602 our parent has no specification (meaning possibly lives in a
9603 namespace elsewhere), then we can add the partial symbol now
9604 instead of queueing it. */
9605 if (part_die->tag == DW_TAG_enumerator
9606 && parent_die != NULL
9607 && parent_die->die_parent == NULL
9608 && parent_die->tag == DW_TAG_enumeration_type
9609 && parent_die->has_specification == 0)
9610 {
9611 if (part_die->name == NULL)
9612 complaint (&symfile_complaints,
9613 _("malformed enumerator DIE ignored"));
9614 else if (building_psymtab)
9615 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9616 VAR_DOMAIN, LOC_CONST,
9617 (cu->language == language_cplus
9618 || cu->language == language_java)
9619 ? &objfile->global_psymbols
9620 : &objfile->static_psymbols,
9621 0, (CORE_ADDR) 0, cu->language, objfile);
9622
9623 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9624 continue;
9625 }
9626
9627 /* We'll save this DIE so link it in. */
9628 part_die->die_parent = parent_die;
9629 part_die->die_sibling = NULL;
9630 part_die->die_child = NULL;
9631
9632 if (last_die && last_die == parent_die)
9633 last_die->die_child = part_die;
9634 else if (last_die)
9635 last_die->die_sibling = part_die;
9636
9637 last_die = part_die;
9638
9639 if (first_die == NULL)
9640 first_die = part_die;
9641
9642 /* Maybe add the DIE to the hash table. Not all DIEs that we
9643 find interesting need to be in the hash table, because we
9644 also have the parent/sibling/child chains; only those that we
9645 might refer to by offset later during partial symbol reading.
9646
9647 For now this means things that might have be the target of a
9648 DW_AT_specification, DW_AT_abstract_origin, or
9649 DW_AT_extension. DW_AT_extension will refer only to
9650 namespaces; DW_AT_abstract_origin refers to functions (and
9651 many things under the function DIE, but we do not recurse
9652 into function DIEs during partial symbol reading) and
9653 possibly variables as well; DW_AT_specification refers to
9654 declarations. Declarations ought to have the DW_AT_declaration
9655 flag. It happens that GCC forgets to put it in sometimes, but
9656 only for functions, not for types.
9657
9658 Adding more things than necessary to the hash table is harmless
9659 except for the performance cost. Adding too few will result in
9660 wasted time in find_partial_die, when we reread the compilation
9661 unit with load_all_dies set. */
9662
9663 if (load_all
9664 || abbrev->tag == DW_TAG_constant
9665 || abbrev->tag == DW_TAG_subprogram
9666 || abbrev->tag == DW_TAG_variable
9667 || abbrev->tag == DW_TAG_namespace
9668 || part_die->is_declaration)
9669 {
9670 void **slot;
9671
9672 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9673 part_die->offset, INSERT);
9674 *slot = part_die;
9675 }
9676
9677 part_die = obstack_alloc (&cu->comp_unit_obstack,
9678 sizeof (struct partial_die_info));
9679
9680 /* For some DIEs we want to follow their children (if any). For C
9681 we have no reason to follow the children of structures; for other
9682 languages we have to, so that we can get at method physnames
9683 to infer fully qualified class names, for DW_AT_specification,
9684 and for C++ template arguments. For C++, we also look one level
9685 inside functions to find template arguments (if the name of the
9686 function does not already contain the template arguments).
9687
9688 For Ada, we need to scan the children of subprograms and lexical
9689 blocks as well because Ada allows the definition of nested
9690 entities that could be interesting for the debugger, such as
9691 nested subprograms for instance. */
9692 if (last_die->has_children
9693 && (load_all
9694 || last_die->tag == DW_TAG_namespace
9695 || last_die->tag == DW_TAG_module
9696 || last_die->tag == DW_TAG_enumeration_type
9697 || (cu->language == language_cplus
9698 && last_die->tag == DW_TAG_subprogram
9699 && (last_die->name == NULL
9700 || strchr (last_die->name, '<') == NULL))
9701 || (cu->language != language_c
9702 && (last_die->tag == DW_TAG_class_type
9703 || last_die->tag == DW_TAG_interface_type
9704 || last_die->tag == DW_TAG_structure_type
9705 || last_die->tag == DW_TAG_union_type))
9706 || (cu->language == language_ada
9707 && (last_die->tag == DW_TAG_subprogram
9708 || last_die->tag == DW_TAG_lexical_block))))
9709 {
9710 nesting_level++;
9711 parent_die = last_die;
9712 continue;
9713 }
9714
9715 /* Otherwise we skip to the next sibling, if any. */
9716 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9717
9718 /* Back to the top, do it again. */
9719 }
9720 }
9721
9722 /* Read a minimal amount of information into the minimal die structure. */
9723
9724 static gdb_byte *
9725 read_partial_die (struct partial_die_info *part_die,
9726 struct abbrev_info *abbrev,
9727 unsigned int abbrev_len, bfd *abfd,
9728 gdb_byte *buffer, gdb_byte *info_ptr,
9729 struct dwarf2_cu *cu)
9730 {
9731 struct objfile *objfile = cu->objfile;
9732 unsigned int i;
9733 struct attribute attr;
9734 int has_low_pc_attr = 0;
9735 int has_high_pc_attr = 0;
9736
9737 memset (part_die, 0, sizeof (struct partial_die_info));
9738
9739 part_die->offset = info_ptr - buffer;
9740
9741 info_ptr += abbrev_len;
9742
9743 if (abbrev == NULL)
9744 return info_ptr;
9745
9746 part_die->tag = abbrev->tag;
9747 part_die->has_children = abbrev->has_children;
9748
9749 for (i = 0; i < abbrev->num_attrs; ++i)
9750 {
9751 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9752
9753 /* Store the data if it is of an attribute we want to keep in a
9754 partial symbol table. */
9755 switch (attr.name)
9756 {
9757 case DW_AT_name:
9758 switch (part_die->tag)
9759 {
9760 case DW_TAG_compile_unit:
9761 case DW_TAG_type_unit:
9762 /* Compilation units have a DW_AT_name that is a filename, not
9763 a source language identifier. */
9764 case DW_TAG_enumeration_type:
9765 case DW_TAG_enumerator:
9766 /* These tags always have simple identifiers already; no need
9767 to canonicalize them. */
9768 part_die->name = DW_STRING (&attr);
9769 break;
9770 default:
9771 part_die->name
9772 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9773 &objfile->objfile_obstack);
9774 break;
9775 }
9776 break;
9777 case DW_AT_linkage_name:
9778 case DW_AT_MIPS_linkage_name:
9779 /* Note that both forms of linkage name might appear. We
9780 assume they will be the same, and we only store the last
9781 one we see. */
9782 if (cu->language == language_ada)
9783 part_die->name = DW_STRING (&attr);
9784 part_die->linkage_name = DW_STRING (&attr);
9785 break;
9786 case DW_AT_low_pc:
9787 has_low_pc_attr = 1;
9788 part_die->lowpc = DW_ADDR (&attr);
9789 break;
9790 case DW_AT_high_pc:
9791 has_high_pc_attr = 1;
9792 part_die->highpc = DW_ADDR (&attr);
9793 break;
9794 case DW_AT_location:
9795 /* Support the .debug_loc offsets. */
9796 if (attr_form_is_block (&attr))
9797 {
9798 part_die->locdesc = DW_BLOCK (&attr);
9799 }
9800 else if (attr_form_is_section_offset (&attr))
9801 {
9802 dwarf2_complex_location_expr_complaint ();
9803 }
9804 else
9805 {
9806 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9807 "partial symbol information");
9808 }
9809 break;
9810 case DW_AT_external:
9811 part_die->is_external = DW_UNSND (&attr);
9812 break;
9813 case DW_AT_declaration:
9814 part_die->is_declaration = DW_UNSND (&attr);
9815 break;
9816 case DW_AT_type:
9817 part_die->has_type = 1;
9818 break;
9819 case DW_AT_abstract_origin:
9820 case DW_AT_specification:
9821 case DW_AT_extension:
9822 part_die->has_specification = 1;
9823 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9824 break;
9825 case DW_AT_sibling:
9826 /* Ignore absolute siblings, they might point outside of
9827 the current compile unit. */
9828 if (attr.form == DW_FORM_ref_addr)
9829 complaint (&symfile_complaints,
9830 _("ignoring absolute DW_AT_sibling"));
9831 else
9832 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9833 break;
9834 case DW_AT_byte_size:
9835 part_die->has_byte_size = 1;
9836 break;
9837 case DW_AT_calling_convention:
9838 /* DWARF doesn't provide a way to identify a program's source-level
9839 entry point. DW_AT_calling_convention attributes are only meant
9840 to describe functions' calling conventions.
9841
9842 However, because it's a necessary piece of information in
9843 Fortran, and because DW_CC_program is the only piece of debugging
9844 information whose definition refers to a 'main program' at all,
9845 several compilers have begun marking Fortran main programs with
9846 DW_CC_program --- even when those functions use the standard
9847 calling conventions.
9848
9849 So until DWARF specifies a way to provide this information and
9850 compilers pick up the new representation, we'll support this
9851 practice. */
9852 if (DW_UNSND (&attr) == DW_CC_program
9853 && cu->language == language_fortran)
9854 {
9855 set_main_name (part_die->name);
9856
9857 /* As this DIE has a static linkage the name would be difficult
9858 to look up later. */
9859 language_of_main = language_fortran;
9860 }
9861 break;
9862 default:
9863 break;
9864 }
9865 }
9866
9867 if (has_low_pc_attr && has_high_pc_attr)
9868 {
9869 /* When using the GNU linker, .gnu.linkonce. sections are used to
9870 eliminate duplicate copies of functions and vtables and such.
9871 The linker will arbitrarily choose one and discard the others.
9872 The AT_*_pc values for such functions refer to local labels in
9873 these sections. If the section from that file was discarded, the
9874 labels are not in the output, so the relocs get a value of 0.
9875 If this is a discarded function, mark the pc bounds as invalid,
9876 so that GDB will ignore it. */
9877 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9878 {
9879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9880
9881 complaint (&symfile_complaints,
9882 _("DW_AT_low_pc %s is zero "
9883 "for DIE at 0x%x [in module %s]"),
9884 paddress (gdbarch, part_die->lowpc),
9885 part_die->offset, objfile->name);
9886 }
9887 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9888 else if (part_die->lowpc >= part_die->highpc)
9889 {
9890 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9891
9892 complaint (&symfile_complaints,
9893 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9894 "for DIE at 0x%x [in module %s]"),
9895 paddress (gdbarch, part_die->lowpc),
9896 paddress (gdbarch, part_die->highpc),
9897 part_die->offset, objfile->name);
9898 }
9899 else
9900 part_die->has_pc_info = 1;
9901 }
9902
9903 return info_ptr;
9904 }
9905
9906 /* Find a cached partial DIE at OFFSET in CU. */
9907
9908 static struct partial_die_info *
9909 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9910 {
9911 struct partial_die_info *lookup_die = NULL;
9912 struct partial_die_info part_die;
9913
9914 part_die.offset = offset;
9915 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9916
9917 return lookup_die;
9918 }
9919
9920 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9921 except in the case of .debug_types DIEs which do not reference
9922 outside their CU (they do however referencing other types via
9923 DW_FORM_ref_sig8). */
9924
9925 static struct partial_die_info *
9926 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9927 {
9928 struct objfile *objfile = cu->objfile;
9929 struct dwarf2_per_cu_data *per_cu = NULL;
9930 struct partial_die_info *pd = NULL;
9931
9932 if (cu->per_cu->debug_types_section)
9933 {
9934 pd = find_partial_die_in_comp_unit (offset, cu);
9935 if (pd != NULL)
9936 return pd;
9937 goto not_found;
9938 }
9939
9940 if (offset_in_cu_p (&cu->header, offset))
9941 {
9942 pd = find_partial_die_in_comp_unit (offset, cu);
9943 if (pd != NULL)
9944 return pd;
9945 }
9946
9947 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9948
9949 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9950 load_partial_comp_unit (per_cu);
9951
9952 per_cu->cu->last_used = 0;
9953 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9954
9955 if (pd == NULL && per_cu->load_all_dies == 0)
9956 {
9957 struct cleanup *back_to;
9958 struct partial_die_info comp_unit_die;
9959 struct abbrev_info *abbrev;
9960 unsigned int bytes_read;
9961 char *info_ptr;
9962
9963 per_cu->load_all_dies = 1;
9964
9965 /* Re-read the DIEs. */
9966 back_to = make_cleanup (null_cleanup, 0);
9967 if (per_cu->cu->dwarf2_abbrevs == NULL)
9968 {
9969 dwarf2_read_abbrevs (per_cu->cu);
9970 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9971 }
9972 info_ptr = (dwarf2_per_objfile->info.buffer
9973 + per_cu->cu->header.offset
9974 + per_cu->cu->header.first_die_offset);
9975 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9976 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9977 objfile->obfd,
9978 dwarf2_per_objfile->info.buffer, info_ptr,
9979 per_cu->cu);
9980 if (comp_unit_die.has_children)
9981 load_partial_dies (objfile->obfd,
9982 dwarf2_per_objfile->info.buffer, info_ptr,
9983 0, per_cu->cu);
9984 do_cleanups (back_to);
9985
9986 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9987 }
9988
9989 not_found:
9990
9991 if (pd == NULL)
9992 internal_error (__FILE__, __LINE__,
9993 _("could not find partial DIE 0x%x "
9994 "in cache [from module %s]\n"),
9995 offset, bfd_get_filename (objfile->obfd));
9996 return pd;
9997 }
9998
9999 /* See if we can figure out if the class lives in a namespace. We do
10000 this by looking for a member function; its demangled name will
10001 contain namespace info, if there is any. */
10002
10003 static void
10004 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10005 struct dwarf2_cu *cu)
10006 {
10007 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10008 what template types look like, because the demangler
10009 frequently doesn't give the same name as the debug info. We
10010 could fix this by only using the demangled name to get the
10011 prefix (but see comment in read_structure_type). */
10012
10013 struct partial_die_info *real_pdi;
10014 struct partial_die_info *child_pdi;
10015
10016 /* If this DIE (this DIE's specification, if any) has a parent, then
10017 we should not do this. We'll prepend the parent's fully qualified
10018 name when we create the partial symbol. */
10019
10020 real_pdi = struct_pdi;
10021 while (real_pdi->has_specification)
10022 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10023
10024 if (real_pdi->die_parent != NULL)
10025 return;
10026
10027 for (child_pdi = struct_pdi->die_child;
10028 child_pdi != NULL;
10029 child_pdi = child_pdi->die_sibling)
10030 {
10031 if (child_pdi->tag == DW_TAG_subprogram
10032 && child_pdi->linkage_name != NULL)
10033 {
10034 char *actual_class_name
10035 = language_class_name_from_physname (cu->language_defn,
10036 child_pdi->linkage_name);
10037 if (actual_class_name != NULL)
10038 {
10039 struct_pdi->name
10040 = obsavestring (actual_class_name,
10041 strlen (actual_class_name),
10042 &cu->objfile->objfile_obstack);
10043 xfree (actual_class_name);
10044 }
10045 break;
10046 }
10047 }
10048 }
10049
10050 /* Adjust PART_DIE before generating a symbol for it. This function
10051 may set the is_external flag or change the DIE's name. */
10052
10053 static void
10054 fixup_partial_die (struct partial_die_info *part_die,
10055 struct dwarf2_cu *cu)
10056 {
10057 /* Once we've fixed up a die, there's no point in doing so again.
10058 This also avoids a memory leak if we were to call
10059 guess_partial_die_structure_name multiple times. */
10060 if (part_die->fixup_called)
10061 return;
10062
10063 /* If we found a reference attribute and the DIE has no name, try
10064 to find a name in the referred to DIE. */
10065
10066 if (part_die->name == NULL && part_die->has_specification)
10067 {
10068 struct partial_die_info *spec_die;
10069
10070 spec_die = find_partial_die (part_die->spec_offset, cu);
10071
10072 fixup_partial_die (spec_die, cu);
10073
10074 if (spec_die->name)
10075 {
10076 part_die->name = spec_die->name;
10077
10078 /* Copy DW_AT_external attribute if it is set. */
10079 if (spec_die->is_external)
10080 part_die->is_external = spec_die->is_external;
10081 }
10082 }
10083
10084 /* Set default names for some unnamed DIEs. */
10085
10086 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10087 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10088
10089 /* If there is no parent die to provide a namespace, and there are
10090 children, see if we can determine the namespace from their linkage
10091 name. */
10092 if (cu->language == language_cplus
10093 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10094 && part_die->die_parent == NULL
10095 && part_die->has_children
10096 && (part_die->tag == DW_TAG_class_type
10097 || part_die->tag == DW_TAG_structure_type
10098 || part_die->tag == DW_TAG_union_type))
10099 guess_partial_die_structure_name (part_die, cu);
10100
10101 /* GCC might emit a nameless struct or union that has a linkage
10102 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10103 if (part_die->name == NULL
10104 && (part_die->tag == DW_TAG_class_type
10105 || part_die->tag == DW_TAG_interface_type
10106 || part_die->tag == DW_TAG_structure_type
10107 || part_die->tag == DW_TAG_union_type)
10108 && part_die->linkage_name != NULL)
10109 {
10110 char *demangled;
10111
10112 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10113 if (demangled)
10114 {
10115 const char *base;
10116
10117 /* Strip any leading namespaces/classes, keep only the base name.
10118 DW_AT_name for named DIEs does not contain the prefixes. */
10119 base = strrchr (demangled, ':');
10120 if (base && base > demangled && base[-1] == ':')
10121 base++;
10122 else
10123 base = demangled;
10124
10125 part_die->name = obsavestring (base, strlen (base),
10126 &cu->objfile->objfile_obstack);
10127 xfree (demangled);
10128 }
10129 }
10130
10131 part_die->fixup_called = 1;
10132 }
10133
10134 /* Read an attribute value described by an attribute form. */
10135
10136 static gdb_byte *
10137 read_attribute_value (struct attribute *attr, unsigned form,
10138 bfd *abfd, gdb_byte *info_ptr,
10139 struct dwarf2_cu *cu)
10140 {
10141 struct comp_unit_head *cu_header = &cu->header;
10142 unsigned int bytes_read;
10143 struct dwarf_block *blk;
10144
10145 attr->form = form;
10146 switch (form)
10147 {
10148 case DW_FORM_ref_addr:
10149 if (cu->header.version == 2)
10150 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10151 else
10152 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10153 &cu->header, &bytes_read);
10154 info_ptr += bytes_read;
10155 break;
10156 case DW_FORM_addr:
10157 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10158 info_ptr += bytes_read;
10159 break;
10160 case DW_FORM_block2:
10161 blk = dwarf_alloc_block (cu);
10162 blk->size = read_2_bytes (abfd, info_ptr);
10163 info_ptr += 2;
10164 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10165 info_ptr += blk->size;
10166 DW_BLOCK (attr) = blk;
10167 break;
10168 case DW_FORM_block4:
10169 blk = dwarf_alloc_block (cu);
10170 blk->size = read_4_bytes (abfd, info_ptr);
10171 info_ptr += 4;
10172 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10173 info_ptr += blk->size;
10174 DW_BLOCK (attr) = blk;
10175 break;
10176 case DW_FORM_data2:
10177 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10178 info_ptr += 2;
10179 break;
10180 case DW_FORM_data4:
10181 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10182 info_ptr += 4;
10183 break;
10184 case DW_FORM_data8:
10185 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10186 info_ptr += 8;
10187 break;
10188 case DW_FORM_sec_offset:
10189 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10190 info_ptr += bytes_read;
10191 break;
10192 case DW_FORM_string:
10193 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10194 DW_STRING_IS_CANONICAL (attr) = 0;
10195 info_ptr += bytes_read;
10196 break;
10197 case DW_FORM_strp:
10198 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10199 &bytes_read);
10200 DW_STRING_IS_CANONICAL (attr) = 0;
10201 info_ptr += bytes_read;
10202 break;
10203 case DW_FORM_exprloc:
10204 case DW_FORM_block:
10205 blk = dwarf_alloc_block (cu);
10206 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10207 info_ptr += bytes_read;
10208 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10209 info_ptr += blk->size;
10210 DW_BLOCK (attr) = blk;
10211 break;
10212 case DW_FORM_block1:
10213 blk = dwarf_alloc_block (cu);
10214 blk->size = read_1_byte (abfd, info_ptr);
10215 info_ptr += 1;
10216 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10217 info_ptr += blk->size;
10218 DW_BLOCK (attr) = blk;
10219 break;
10220 case DW_FORM_data1:
10221 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10222 info_ptr += 1;
10223 break;
10224 case DW_FORM_flag:
10225 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10226 info_ptr += 1;
10227 break;
10228 case DW_FORM_flag_present:
10229 DW_UNSND (attr) = 1;
10230 break;
10231 case DW_FORM_sdata:
10232 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10233 info_ptr += bytes_read;
10234 break;
10235 case DW_FORM_udata:
10236 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10237 info_ptr += bytes_read;
10238 break;
10239 case DW_FORM_ref1:
10240 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10241 info_ptr += 1;
10242 break;
10243 case DW_FORM_ref2:
10244 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10245 info_ptr += 2;
10246 break;
10247 case DW_FORM_ref4:
10248 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10249 info_ptr += 4;
10250 break;
10251 case DW_FORM_ref8:
10252 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10253 info_ptr += 8;
10254 break;
10255 case DW_FORM_ref_sig8:
10256 /* Convert the signature to something we can record in DW_UNSND
10257 for later lookup.
10258 NOTE: This is NULL if the type wasn't found. */
10259 DW_SIGNATURED_TYPE (attr) =
10260 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10261 info_ptr += 8;
10262 break;
10263 case DW_FORM_ref_udata:
10264 DW_ADDR (attr) = (cu->header.offset
10265 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10266 info_ptr += bytes_read;
10267 break;
10268 case DW_FORM_indirect:
10269 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10270 info_ptr += bytes_read;
10271 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10272 break;
10273 default:
10274 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10275 dwarf_form_name (form),
10276 bfd_get_filename (abfd));
10277 }
10278
10279 /* We have seen instances where the compiler tried to emit a byte
10280 size attribute of -1 which ended up being encoded as an unsigned
10281 0xffffffff. Although 0xffffffff is technically a valid size value,
10282 an object of this size seems pretty unlikely so we can relatively
10283 safely treat these cases as if the size attribute was invalid and
10284 treat them as zero by default. */
10285 if (attr->name == DW_AT_byte_size
10286 && form == DW_FORM_data4
10287 && DW_UNSND (attr) >= 0xffffffff)
10288 {
10289 complaint
10290 (&symfile_complaints,
10291 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10292 hex_string (DW_UNSND (attr)));
10293 DW_UNSND (attr) = 0;
10294 }
10295
10296 return info_ptr;
10297 }
10298
10299 /* Read an attribute described by an abbreviated attribute. */
10300
10301 static gdb_byte *
10302 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10303 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10304 {
10305 attr->name = abbrev->name;
10306 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10307 }
10308
10309 /* Read dwarf information from a buffer. */
10310
10311 static unsigned int
10312 read_1_byte (bfd *abfd, gdb_byte *buf)
10313 {
10314 return bfd_get_8 (abfd, buf);
10315 }
10316
10317 static int
10318 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10319 {
10320 return bfd_get_signed_8 (abfd, buf);
10321 }
10322
10323 static unsigned int
10324 read_2_bytes (bfd *abfd, gdb_byte *buf)
10325 {
10326 return bfd_get_16 (abfd, buf);
10327 }
10328
10329 static int
10330 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10331 {
10332 return bfd_get_signed_16 (abfd, buf);
10333 }
10334
10335 static unsigned int
10336 read_4_bytes (bfd *abfd, gdb_byte *buf)
10337 {
10338 return bfd_get_32 (abfd, buf);
10339 }
10340
10341 static int
10342 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10343 {
10344 return bfd_get_signed_32 (abfd, buf);
10345 }
10346
10347 static ULONGEST
10348 read_8_bytes (bfd *abfd, gdb_byte *buf)
10349 {
10350 return bfd_get_64 (abfd, buf);
10351 }
10352
10353 static CORE_ADDR
10354 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10355 unsigned int *bytes_read)
10356 {
10357 struct comp_unit_head *cu_header = &cu->header;
10358 CORE_ADDR retval = 0;
10359
10360 if (cu_header->signed_addr_p)
10361 {
10362 switch (cu_header->addr_size)
10363 {
10364 case 2:
10365 retval = bfd_get_signed_16 (abfd, buf);
10366 break;
10367 case 4:
10368 retval = bfd_get_signed_32 (abfd, buf);
10369 break;
10370 case 8:
10371 retval = bfd_get_signed_64 (abfd, buf);
10372 break;
10373 default:
10374 internal_error (__FILE__, __LINE__,
10375 _("read_address: bad switch, signed [in module %s]"),
10376 bfd_get_filename (abfd));
10377 }
10378 }
10379 else
10380 {
10381 switch (cu_header->addr_size)
10382 {
10383 case 2:
10384 retval = bfd_get_16 (abfd, buf);
10385 break;
10386 case 4:
10387 retval = bfd_get_32 (abfd, buf);
10388 break;
10389 case 8:
10390 retval = bfd_get_64 (abfd, buf);
10391 break;
10392 default:
10393 internal_error (__FILE__, __LINE__,
10394 _("read_address: bad switch, "
10395 "unsigned [in module %s]"),
10396 bfd_get_filename (abfd));
10397 }
10398 }
10399
10400 *bytes_read = cu_header->addr_size;
10401 return retval;
10402 }
10403
10404 /* Read the initial length from a section. The (draft) DWARF 3
10405 specification allows the initial length to take up either 4 bytes
10406 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10407 bytes describe the length and all offsets will be 8 bytes in length
10408 instead of 4.
10409
10410 An older, non-standard 64-bit format is also handled by this
10411 function. The older format in question stores the initial length
10412 as an 8-byte quantity without an escape value. Lengths greater
10413 than 2^32 aren't very common which means that the initial 4 bytes
10414 is almost always zero. Since a length value of zero doesn't make
10415 sense for the 32-bit format, this initial zero can be considered to
10416 be an escape value which indicates the presence of the older 64-bit
10417 format. As written, the code can't detect (old format) lengths
10418 greater than 4GB. If it becomes necessary to handle lengths
10419 somewhat larger than 4GB, we could allow other small values (such
10420 as the non-sensical values of 1, 2, and 3) to also be used as
10421 escape values indicating the presence of the old format.
10422
10423 The value returned via bytes_read should be used to increment the
10424 relevant pointer after calling read_initial_length().
10425
10426 [ Note: read_initial_length() and read_offset() are based on the
10427 document entitled "DWARF Debugging Information Format", revision
10428 3, draft 8, dated November 19, 2001. This document was obtained
10429 from:
10430
10431 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10432
10433 This document is only a draft and is subject to change. (So beware.)
10434
10435 Details regarding the older, non-standard 64-bit format were
10436 determined empirically by examining 64-bit ELF files produced by
10437 the SGI toolchain on an IRIX 6.5 machine.
10438
10439 - Kevin, July 16, 2002
10440 ] */
10441
10442 static LONGEST
10443 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10444 {
10445 LONGEST length = bfd_get_32 (abfd, buf);
10446
10447 if (length == 0xffffffff)
10448 {
10449 length = bfd_get_64 (abfd, buf + 4);
10450 *bytes_read = 12;
10451 }
10452 else if (length == 0)
10453 {
10454 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10455 length = bfd_get_64 (abfd, buf);
10456 *bytes_read = 8;
10457 }
10458 else
10459 {
10460 *bytes_read = 4;
10461 }
10462
10463 return length;
10464 }
10465
10466 /* Cover function for read_initial_length.
10467 Returns the length of the object at BUF, and stores the size of the
10468 initial length in *BYTES_READ and stores the size that offsets will be in
10469 *OFFSET_SIZE.
10470 If the initial length size is not equivalent to that specified in
10471 CU_HEADER then issue a complaint.
10472 This is useful when reading non-comp-unit headers. */
10473
10474 static LONGEST
10475 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10476 const struct comp_unit_head *cu_header,
10477 unsigned int *bytes_read,
10478 unsigned int *offset_size)
10479 {
10480 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10481
10482 gdb_assert (cu_header->initial_length_size == 4
10483 || cu_header->initial_length_size == 8
10484 || cu_header->initial_length_size == 12);
10485
10486 if (cu_header->initial_length_size != *bytes_read)
10487 complaint (&symfile_complaints,
10488 _("intermixed 32-bit and 64-bit DWARF sections"));
10489
10490 *offset_size = (*bytes_read == 4) ? 4 : 8;
10491 return length;
10492 }
10493
10494 /* Read an offset from the data stream. The size of the offset is
10495 given by cu_header->offset_size. */
10496
10497 static LONGEST
10498 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10499 unsigned int *bytes_read)
10500 {
10501 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10502
10503 *bytes_read = cu_header->offset_size;
10504 return offset;
10505 }
10506
10507 /* Read an offset from the data stream. */
10508
10509 static LONGEST
10510 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10511 {
10512 LONGEST retval = 0;
10513
10514 switch (offset_size)
10515 {
10516 case 4:
10517 retval = bfd_get_32 (abfd, buf);
10518 break;
10519 case 8:
10520 retval = bfd_get_64 (abfd, buf);
10521 break;
10522 default:
10523 internal_error (__FILE__, __LINE__,
10524 _("read_offset_1: bad switch [in module %s]"),
10525 bfd_get_filename (abfd));
10526 }
10527
10528 return retval;
10529 }
10530
10531 static gdb_byte *
10532 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10533 {
10534 /* If the size of a host char is 8 bits, we can return a pointer
10535 to the buffer, otherwise we have to copy the data to a buffer
10536 allocated on the temporary obstack. */
10537 gdb_assert (HOST_CHAR_BIT == 8);
10538 return buf;
10539 }
10540
10541 static char *
10542 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10543 {
10544 /* If the size of a host char is 8 bits, we can return a pointer
10545 to the string, otherwise we have to copy the string to a buffer
10546 allocated on the temporary obstack. */
10547 gdb_assert (HOST_CHAR_BIT == 8);
10548 if (*buf == '\0')
10549 {
10550 *bytes_read_ptr = 1;
10551 return NULL;
10552 }
10553 *bytes_read_ptr = strlen ((char *) buf) + 1;
10554 return (char *) buf;
10555 }
10556
10557 static char *
10558 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10559 {
10560 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10561 if (dwarf2_per_objfile->str.buffer == NULL)
10562 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10563 bfd_get_filename (abfd));
10564 if (str_offset >= dwarf2_per_objfile->str.size)
10565 error (_("DW_FORM_strp pointing outside of "
10566 ".debug_str section [in module %s]"),
10567 bfd_get_filename (abfd));
10568 gdb_assert (HOST_CHAR_BIT == 8);
10569 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10570 return NULL;
10571 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10572 }
10573
10574 static char *
10575 read_indirect_string (bfd *abfd, gdb_byte *buf,
10576 const struct comp_unit_head *cu_header,
10577 unsigned int *bytes_read_ptr)
10578 {
10579 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10580
10581 return read_indirect_string_at_offset (abfd, str_offset);
10582 }
10583
10584 static unsigned long
10585 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10586 {
10587 unsigned long result;
10588 unsigned int num_read;
10589 int i, shift;
10590 unsigned char byte;
10591
10592 result = 0;
10593 shift = 0;
10594 num_read = 0;
10595 i = 0;
10596 while (1)
10597 {
10598 byte = bfd_get_8 (abfd, buf);
10599 buf++;
10600 num_read++;
10601 result |= ((unsigned long)(byte & 127) << shift);
10602 if ((byte & 128) == 0)
10603 {
10604 break;
10605 }
10606 shift += 7;
10607 }
10608 *bytes_read_ptr = num_read;
10609 return result;
10610 }
10611
10612 static long
10613 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10614 {
10615 long result;
10616 int i, shift, num_read;
10617 unsigned char byte;
10618
10619 result = 0;
10620 shift = 0;
10621 num_read = 0;
10622 i = 0;
10623 while (1)
10624 {
10625 byte = bfd_get_8 (abfd, buf);
10626 buf++;
10627 num_read++;
10628 result |= ((long)(byte & 127) << shift);
10629 shift += 7;
10630 if ((byte & 128) == 0)
10631 {
10632 break;
10633 }
10634 }
10635 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10636 result |= -(((long)1) << shift);
10637 *bytes_read_ptr = num_read;
10638 return result;
10639 }
10640
10641 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10642
10643 static gdb_byte *
10644 skip_leb128 (bfd *abfd, gdb_byte *buf)
10645 {
10646 int byte;
10647
10648 while (1)
10649 {
10650 byte = bfd_get_8 (abfd, buf);
10651 buf++;
10652 if ((byte & 128) == 0)
10653 return buf;
10654 }
10655 }
10656
10657 static void
10658 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10659 {
10660 switch (lang)
10661 {
10662 case DW_LANG_C89:
10663 case DW_LANG_C99:
10664 case DW_LANG_C:
10665 cu->language = language_c;
10666 break;
10667 case DW_LANG_C_plus_plus:
10668 cu->language = language_cplus;
10669 break;
10670 case DW_LANG_D:
10671 cu->language = language_d;
10672 break;
10673 case DW_LANG_Fortran77:
10674 case DW_LANG_Fortran90:
10675 case DW_LANG_Fortran95:
10676 cu->language = language_fortran;
10677 break;
10678 case DW_LANG_Mips_Assembler:
10679 cu->language = language_asm;
10680 break;
10681 case DW_LANG_Java:
10682 cu->language = language_java;
10683 break;
10684 case DW_LANG_Ada83:
10685 case DW_LANG_Ada95:
10686 cu->language = language_ada;
10687 break;
10688 case DW_LANG_Modula2:
10689 cu->language = language_m2;
10690 break;
10691 case DW_LANG_Pascal83:
10692 cu->language = language_pascal;
10693 break;
10694 case DW_LANG_ObjC:
10695 cu->language = language_objc;
10696 break;
10697 case DW_LANG_Cobol74:
10698 case DW_LANG_Cobol85:
10699 default:
10700 cu->language = language_minimal;
10701 break;
10702 }
10703 cu->language_defn = language_def (cu->language);
10704 }
10705
10706 /* Return the named attribute or NULL if not there. */
10707
10708 static struct attribute *
10709 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10710 {
10711 for (;;)
10712 {
10713 unsigned int i;
10714 struct attribute *spec = NULL;
10715
10716 for (i = 0; i < die->num_attrs; ++i)
10717 {
10718 if (die->attrs[i].name == name)
10719 return &die->attrs[i];
10720 if (die->attrs[i].name == DW_AT_specification
10721 || die->attrs[i].name == DW_AT_abstract_origin)
10722 spec = &die->attrs[i];
10723 }
10724
10725 if (!spec)
10726 break;
10727
10728 die = follow_die_ref (die, spec, &cu);
10729 }
10730
10731 return NULL;
10732 }
10733
10734 /* Return the named attribute or NULL if not there,
10735 but do not follow DW_AT_specification, etc.
10736 This is for use in contexts where we're reading .debug_types dies.
10737 Following DW_AT_specification, DW_AT_abstract_origin will take us
10738 back up the chain, and we want to go down. */
10739
10740 static struct attribute *
10741 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10742 struct dwarf2_cu *cu)
10743 {
10744 unsigned int i;
10745
10746 for (i = 0; i < die->num_attrs; ++i)
10747 if (die->attrs[i].name == name)
10748 return &die->attrs[i];
10749
10750 return NULL;
10751 }
10752
10753 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10754 and holds a non-zero value. This function should only be used for
10755 DW_FORM_flag or DW_FORM_flag_present attributes. */
10756
10757 static int
10758 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10759 {
10760 struct attribute *attr = dwarf2_attr (die, name, cu);
10761
10762 return (attr && DW_UNSND (attr));
10763 }
10764
10765 static int
10766 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10767 {
10768 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10769 which value is non-zero. However, we have to be careful with
10770 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10771 (via dwarf2_flag_true_p) follows this attribute. So we may
10772 end up accidently finding a declaration attribute that belongs
10773 to a different DIE referenced by the specification attribute,
10774 even though the given DIE does not have a declaration attribute. */
10775 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10776 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10777 }
10778
10779 /* Return the die giving the specification for DIE, if there is
10780 one. *SPEC_CU is the CU containing DIE on input, and the CU
10781 containing the return value on output. If there is no
10782 specification, but there is an abstract origin, that is
10783 returned. */
10784
10785 static struct die_info *
10786 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10787 {
10788 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10789 *spec_cu);
10790
10791 if (spec_attr == NULL)
10792 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10793
10794 if (spec_attr == NULL)
10795 return NULL;
10796 else
10797 return follow_die_ref (die, spec_attr, spec_cu);
10798 }
10799
10800 /* Free the line_header structure *LH, and any arrays and strings it
10801 refers to.
10802 NOTE: This is also used as a "cleanup" function. */
10803
10804 static void
10805 free_line_header (struct line_header *lh)
10806 {
10807 if (lh->standard_opcode_lengths)
10808 xfree (lh->standard_opcode_lengths);
10809
10810 /* Remember that all the lh->file_names[i].name pointers are
10811 pointers into debug_line_buffer, and don't need to be freed. */
10812 if (lh->file_names)
10813 xfree (lh->file_names);
10814
10815 /* Similarly for the include directory names. */
10816 if (lh->include_dirs)
10817 xfree (lh->include_dirs);
10818
10819 xfree (lh);
10820 }
10821
10822 /* Add an entry to LH's include directory table. */
10823
10824 static void
10825 add_include_dir (struct line_header *lh, char *include_dir)
10826 {
10827 /* Grow the array if necessary. */
10828 if (lh->include_dirs_size == 0)
10829 {
10830 lh->include_dirs_size = 1; /* for testing */
10831 lh->include_dirs = xmalloc (lh->include_dirs_size
10832 * sizeof (*lh->include_dirs));
10833 }
10834 else if (lh->num_include_dirs >= lh->include_dirs_size)
10835 {
10836 lh->include_dirs_size *= 2;
10837 lh->include_dirs = xrealloc (lh->include_dirs,
10838 (lh->include_dirs_size
10839 * sizeof (*lh->include_dirs)));
10840 }
10841
10842 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10843 }
10844
10845 /* Add an entry to LH's file name table. */
10846
10847 static void
10848 add_file_name (struct line_header *lh,
10849 char *name,
10850 unsigned int dir_index,
10851 unsigned int mod_time,
10852 unsigned int length)
10853 {
10854 struct file_entry *fe;
10855
10856 /* Grow the array if necessary. */
10857 if (lh->file_names_size == 0)
10858 {
10859 lh->file_names_size = 1; /* for testing */
10860 lh->file_names = xmalloc (lh->file_names_size
10861 * sizeof (*lh->file_names));
10862 }
10863 else if (lh->num_file_names >= lh->file_names_size)
10864 {
10865 lh->file_names_size *= 2;
10866 lh->file_names = xrealloc (lh->file_names,
10867 (lh->file_names_size
10868 * sizeof (*lh->file_names)));
10869 }
10870
10871 fe = &lh->file_names[lh->num_file_names++];
10872 fe->name = name;
10873 fe->dir_index = dir_index;
10874 fe->mod_time = mod_time;
10875 fe->length = length;
10876 fe->included_p = 0;
10877 fe->symtab = NULL;
10878 }
10879
10880 /* Read the statement program header starting at OFFSET in
10881 .debug_line, according to the endianness of ABFD. Return a pointer
10882 to a struct line_header, allocated using xmalloc.
10883
10884 NOTE: the strings in the include directory and file name tables of
10885 the returned object point into debug_line_buffer, and must not be
10886 freed. */
10887
10888 static struct line_header *
10889 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10890 struct dwarf2_cu *cu)
10891 {
10892 struct cleanup *back_to;
10893 struct line_header *lh;
10894 gdb_byte *line_ptr;
10895 unsigned int bytes_read, offset_size;
10896 int i;
10897 char *cur_dir, *cur_file;
10898
10899 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10900 if (dwarf2_per_objfile->line.buffer == NULL)
10901 {
10902 complaint (&symfile_complaints, _("missing .debug_line section"));
10903 return 0;
10904 }
10905
10906 /* Make sure that at least there's room for the total_length field.
10907 That could be 12 bytes long, but we're just going to fudge that. */
10908 if (offset + 4 >= dwarf2_per_objfile->line.size)
10909 {
10910 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10911 return 0;
10912 }
10913
10914 lh = xmalloc (sizeof (*lh));
10915 memset (lh, 0, sizeof (*lh));
10916 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10917 (void *) lh);
10918
10919 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10920
10921 /* Read in the header. */
10922 lh->total_length =
10923 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10924 &bytes_read, &offset_size);
10925 line_ptr += bytes_read;
10926 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10927 + dwarf2_per_objfile->line.size))
10928 {
10929 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10930 return 0;
10931 }
10932 lh->statement_program_end = line_ptr + lh->total_length;
10933 lh->version = read_2_bytes (abfd, line_ptr);
10934 line_ptr += 2;
10935 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10936 line_ptr += offset_size;
10937 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10938 line_ptr += 1;
10939 if (lh->version >= 4)
10940 {
10941 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10942 line_ptr += 1;
10943 }
10944 else
10945 lh->maximum_ops_per_instruction = 1;
10946
10947 if (lh->maximum_ops_per_instruction == 0)
10948 {
10949 lh->maximum_ops_per_instruction = 1;
10950 complaint (&symfile_complaints,
10951 _("invalid maximum_ops_per_instruction "
10952 "in `.debug_line' section"));
10953 }
10954
10955 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10956 line_ptr += 1;
10957 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10958 line_ptr += 1;
10959 lh->line_range = read_1_byte (abfd, line_ptr);
10960 line_ptr += 1;
10961 lh->opcode_base = read_1_byte (abfd, line_ptr);
10962 line_ptr += 1;
10963 lh->standard_opcode_lengths
10964 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10965
10966 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10967 for (i = 1; i < lh->opcode_base; ++i)
10968 {
10969 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10970 line_ptr += 1;
10971 }
10972
10973 /* Read directory table. */
10974 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10975 {
10976 line_ptr += bytes_read;
10977 add_include_dir (lh, cur_dir);
10978 }
10979 line_ptr += bytes_read;
10980
10981 /* Read file name table. */
10982 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10983 {
10984 unsigned int dir_index, mod_time, length;
10985
10986 line_ptr += bytes_read;
10987 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10988 line_ptr += bytes_read;
10989 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10990 line_ptr += bytes_read;
10991 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10992 line_ptr += bytes_read;
10993
10994 add_file_name (lh, cur_file, dir_index, mod_time, length);
10995 }
10996 line_ptr += bytes_read;
10997 lh->statement_program_start = line_ptr;
10998
10999 if (line_ptr > (dwarf2_per_objfile->line.buffer
11000 + dwarf2_per_objfile->line.size))
11001 complaint (&symfile_complaints,
11002 _("line number info header doesn't "
11003 "fit in `.debug_line' section"));
11004
11005 discard_cleanups (back_to);
11006 return lh;
11007 }
11008
11009 /* Subroutine of dwarf_decode_lines to simplify it.
11010 Return the file name of the psymtab for included file FILE_INDEX
11011 in line header LH of PST.
11012 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11013 If space for the result is malloc'd, it will be freed by a cleanup.
11014 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11015
11016 static char *
11017 psymtab_include_file_name (const struct line_header *lh, int file_index,
11018 const struct partial_symtab *pst,
11019 const char *comp_dir)
11020 {
11021 const struct file_entry fe = lh->file_names [file_index];
11022 char *include_name = fe.name;
11023 char *include_name_to_compare = include_name;
11024 char *dir_name = NULL;
11025 const char *pst_filename;
11026 char *copied_name = NULL;
11027 int file_is_pst;
11028
11029 if (fe.dir_index)
11030 dir_name = lh->include_dirs[fe.dir_index - 1];
11031
11032 if (!IS_ABSOLUTE_PATH (include_name)
11033 && (dir_name != NULL || comp_dir != NULL))
11034 {
11035 /* Avoid creating a duplicate psymtab for PST.
11036 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11037 Before we do the comparison, however, we need to account
11038 for DIR_NAME and COMP_DIR.
11039 First prepend dir_name (if non-NULL). If we still don't
11040 have an absolute path prepend comp_dir (if non-NULL).
11041 However, the directory we record in the include-file's
11042 psymtab does not contain COMP_DIR (to match the
11043 corresponding symtab(s)).
11044
11045 Example:
11046
11047 bash$ cd /tmp
11048 bash$ gcc -g ./hello.c
11049 include_name = "hello.c"
11050 dir_name = "."
11051 DW_AT_comp_dir = comp_dir = "/tmp"
11052 DW_AT_name = "./hello.c" */
11053
11054 if (dir_name != NULL)
11055 {
11056 include_name = concat (dir_name, SLASH_STRING,
11057 include_name, (char *)NULL);
11058 include_name_to_compare = include_name;
11059 make_cleanup (xfree, include_name);
11060 }
11061 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11062 {
11063 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11064 include_name, (char *)NULL);
11065 }
11066 }
11067
11068 pst_filename = pst->filename;
11069 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11070 {
11071 copied_name = concat (pst->dirname, SLASH_STRING,
11072 pst_filename, (char *)NULL);
11073 pst_filename = copied_name;
11074 }
11075
11076 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11077
11078 if (include_name_to_compare != include_name)
11079 xfree (include_name_to_compare);
11080 if (copied_name != NULL)
11081 xfree (copied_name);
11082
11083 if (file_is_pst)
11084 return NULL;
11085 return include_name;
11086 }
11087
11088 /* Ignore this record_line request. */
11089
11090 static void
11091 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11092 {
11093 return;
11094 }
11095
11096 /* Subroutine of dwarf_decode_lines to simplify it.
11097 Process the line number information in LH. */
11098
11099 static void
11100 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11101 struct dwarf2_cu *cu, struct partial_symtab *pst)
11102 {
11103 gdb_byte *line_ptr, *extended_end;
11104 gdb_byte *line_end;
11105 unsigned int bytes_read, extended_len;
11106 unsigned char op_code, extended_op, adj_opcode;
11107 CORE_ADDR baseaddr;
11108 struct objfile *objfile = cu->objfile;
11109 bfd *abfd = objfile->obfd;
11110 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11111 const int decode_for_pst_p = (pst != NULL);
11112 struct subfile *last_subfile = NULL;
11113 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11114 = record_line;
11115
11116 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11117
11118 line_ptr = lh->statement_program_start;
11119 line_end = lh->statement_program_end;
11120
11121 /* Read the statement sequences until there's nothing left. */
11122 while (line_ptr < line_end)
11123 {
11124 /* state machine registers */
11125 CORE_ADDR address = 0;
11126 unsigned int file = 1;
11127 unsigned int line = 1;
11128 unsigned int column = 0;
11129 int is_stmt = lh->default_is_stmt;
11130 int basic_block = 0;
11131 int end_sequence = 0;
11132 CORE_ADDR addr;
11133 unsigned char op_index = 0;
11134
11135 if (!decode_for_pst_p && lh->num_file_names >= file)
11136 {
11137 /* Start a subfile for the current file of the state machine. */
11138 /* lh->include_dirs and lh->file_names are 0-based, but the
11139 directory and file name numbers in the statement program
11140 are 1-based. */
11141 struct file_entry *fe = &lh->file_names[file - 1];
11142 char *dir = NULL;
11143
11144 if (fe->dir_index)
11145 dir = lh->include_dirs[fe->dir_index - 1];
11146
11147 dwarf2_start_subfile (fe->name, dir, comp_dir);
11148 }
11149
11150 /* Decode the table. */
11151 while (!end_sequence)
11152 {
11153 op_code = read_1_byte (abfd, line_ptr);
11154 line_ptr += 1;
11155 if (line_ptr > line_end)
11156 {
11157 dwarf2_debug_line_missing_end_sequence_complaint ();
11158 break;
11159 }
11160
11161 if (op_code >= lh->opcode_base)
11162 {
11163 /* Special operand. */
11164 adj_opcode = op_code - lh->opcode_base;
11165 address += (((op_index + (adj_opcode / lh->line_range))
11166 / lh->maximum_ops_per_instruction)
11167 * lh->minimum_instruction_length);
11168 op_index = ((op_index + (adj_opcode / lh->line_range))
11169 % lh->maximum_ops_per_instruction);
11170 line += lh->line_base + (adj_opcode % lh->line_range);
11171 if (lh->num_file_names < file || file == 0)
11172 dwarf2_debug_line_missing_file_complaint ();
11173 /* For now we ignore lines not starting on an
11174 instruction boundary. */
11175 else if (op_index == 0)
11176 {
11177 lh->file_names[file - 1].included_p = 1;
11178 if (!decode_for_pst_p && is_stmt)
11179 {
11180 if (last_subfile != current_subfile)
11181 {
11182 addr = gdbarch_addr_bits_remove (gdbarch, address);
11183 if (last_subfile)
11184 (*p_record_line) (last_subfile, 0, addr);
11185 last_subfile = current_subfile;
11186 }
11187 /* Append row to matrix using current values. */
11188 addr = gdbarch_addr_bits_remove (gdbarch, address);
11189 (*p_record_line) (current_subfile, line, addr);
11190 }
11191 }
11192 basic_block = 0;
11193 }
11194 else switch (op_code)
11195 {
11196 case DW_LNS_extended_op:
11197 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11198 &bytes_read);
11199 line_ptr += bytes_read;
11200 extended_end = line_ptr + extended_len;
11201 extended_op = read_1_byte (abfd, line_ptr);
11202 line_ptr += 1;
11203 switch (extended_op)
11204 {
11205 case DW_LNE_end_sequence:
11206 p_record_line = record_line;
11207 end_sequence = 1;
11208 break;
11209 case DW_LNE_set_address:
11210 address = read_address (abfd, line_ptr, cu, &bytes_read);
11211
11212 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11213 {
11214 /* This line table is for a function which has been
11215 GCd by the linker. Ignore it. PR gdb/12528 */
11216
11217 long line_offset
11218 = line_ptr - dwarf2_per_objfile->line.buffer;
11219
11220 complaint (&symfile_complaints,
11221 _(".debug_line address at offset 0x%lx is 0 "
11222 "[in module %s]"),
11223 line_offset, objfile->name);
11224 p_record_line = noop_record_line;
11225 }
11226
11227 op_index = 0;
11228 line_ptr += bytes_read;
11229 address += baseaddr;
11230 break;
11231 case DW_LNE_define_file:
11232 {
11233 char *cur_file;
11234 unsigned int dir_index, mod_time, length;
11235
11236 cur_file = read_direct_string (abfd, line_ptr,
11237 &bytes_read);
11238 line_ptr += bytes_read;
11239 dir_index =
11240 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11241 line_ptr += bytes_read;
11242 mod_time =
11243 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11244 line_ptr += bytes_read;
11245 length =
11246 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11247 line_ptr += bytes_read;
11248 add_file_name (lh, cur_file, dir_index, mod_time, length);
11249 }
11250 break;
11251 case DW_LNE_set_discriminator:
11252 /* The discriminator is not interesting to the debugger;
11253 just ignore it. */
11254 line_ptr = extended_end;
11255 break;
11256 default:
11257 complaint (&symfile_complaints,
11258 _("mangled .debug_line section"));
11259 return;
11260 }
11261 /* Make sure that we parsed the extended op correctly. If e.g.
11262 we expected a different address size than the producer used,
11263 we may have read the wrong number of bytes. */
11264 if (line_ptr != extended_end)
11265 {
11266 complaint (&symfile_complaints,
11267 _("mangled .debug_line section"));
11268 return;
11269 }
11270 break;
11271 case DW_LNS_copy:
11272 if (lh->num_file_names < file || file == 0)
11273 dwarf2_debug_line_missing_file_complaint ();
11274 else
11275 {
11276 lh->file_names[file - 1].included_p = 1;
11277 if (!decode_for_pst_p && is_stmt)
11278 {
11279 if (last_subfile != current_subfile)
11280 {
11281 addr = gdbarch_addr_bits_remove (gdbarch, address);
11282 if (last_subfile)
11283 (*p_record_line) (last_subfile, 0, addr);
11284 last_subfile = current_subfile;
11285 }
11286 addr = gdbarch_addr_bits_remove (gdbarch, address);
11287 (*p_record_line) (current_subfile, line, addr);
11288 }
11289 }
11290 basic_block = 0;
11291 break;
11292 case DW_LNS_advance_pc:
11293 {
11294 CORE_ADDR adjust
11295 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11296
11297 address += (((op_index + adjust)
11298 / lh->maximum_ops_per_instruction)
11299 * lh->minimum_instruction_length);
11300 op_index = ((op_index + adjust)
11301 % lh->maximum_ops_per_instruction);
11302 line_ptr += bytes_read;
11303 }
11304 break;
11305 case DW_LNS_advance_line:
11306 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11307 line_ptr += bytes_read;
11308 break;
11309 case DW_LNS_set_file:
11310 {
11311 /* The arrays lh->include_dirs and lh->file_names are
11312 0-based, but the directory and file name numbers in
11313 the statement program are 1-based. */
11314 struct file_entry *fe;
11315 char *dir = NULL;
11316
11317 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11318 line_ptr += bytes_read;
11319 if (lh->num_file_names < file || file == 0)
11320 dwarf2_debug_line_missing_file_complaint ();
11321 else
11322 {
11323 fe = &lh->file_names[file - 1];
11324 if (fe->dir_index)
11325 dir = lh->include_dirs[fe->dir_index - 1];
11326 if (!decode_for_pst_p)
11327 {
11328 last_subfile = current_subfile;
11329 dwarf2_start_subfile (fe->name, dir, comp_dir);
11330 }
11331 }
11332 }
11333 break;
11334 case DW_LNS_set_column:
11335 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11336 line_ptr += bytes_read;
11337 break;
11338 case DW_LNS_negate_stmt:
11339 is_stmt = (!is_stmt);
11340 break;
11341 case DW_LNS_set_basic_block:
11342 basic_block = 1;
11343 break;
11344 /* Add to the address register of the state machine the
11345 address increment value corresponding to special opcode
11346 255. I.e., this value is scaled by the minimum
11347 instruction length since special opcode 255 would have
11348 scaled the increment. */
11349 case DW_LNS_const_add_pc:
11350 {
11351 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
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 }
11359 break;
11360 case DW_LNS_fixed_advance_pc:
11361 address += read_2_bytes (abfd, line_ptr);
11362 op_index = 0;
11363 line_ptr += 2;
11364 break;
11365 default:
11366 {
11367 /* Unknown standard opcode, ignore it. */
11368 int i;
11369
11370 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11371 {
11372 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11373 line_ptr += bytes_read;
11374 }
11375 }
11376 }
11377 }
11378 if (lh->num_file_names < file || file == 0)
11379 dwarf2_debug_line_missing_file_complaint ();
11380 else
11381 {
11382 lh->file_names[file - 1].included_p = 1;
11383 if (!decode_for_pst_p)
11384 {
11385 addr = gdbarch_addr_bits_remove (gdbarch, address);
11386 (*p_record_line) (current_subfile, 0, addr);
11387 }
11388 }
11389 }
11390 }
11391
11392 /* Decode the Line Number Program (LNP) for the given line_header
11393 structure and CU. The actual information extracted and the type
11394 of structures created from the LNP depends on the value of PST.
11395
11396 1. If PST is NULL, then this procedure uses the data from the program
11397 to create all necessary symbol tables, and their linetables.
11398
11399 2. If PST is not NULL, this procedure reads the program to determine
11400 the list of files included by the unit represented by PST, and
11401 builds all the associated partial symbol tables.
11402
11403 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11404 It is used for relative paths in the line table.
11405 NOTE: When processing partial symtabs (pst != NULL),
11406 comp_dir == pst->dirname.
11407
11408 NOTE: It is important that psymtabs have the same file name (via strcmp)
11409 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11410 symtab we don't use it in the name of the psymtabs we create.
11411 E.g. expand_line_sal requires this when finding psymtabs to expand.
11412 A good testcase for this is mb-inline.exp. */
11413
11414 static void
11415 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11416 struct dwarf2_cu *cu, struct partial_symtab *pst,
11417 int want_line_info)
11418 {
11419 struct objfile *objfile = cu->objfile;
11420 const int decode_for_pst_p = (pst != NULL);
11421 struct subfile *first_subfile = current_subfile;
11422
11423 if (want_line_info)
11424 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
11425
11426 if (decode_for_pst_p)
11427 {
11428 int file_index;
11429
11430 /* Now that we're done scanning the Line Header Program, we can
11431 create the psymtab of each included file. */
11432 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11433 if (lh->file_names[file_index].included_p == 1)
11434 {
11435 char *include_name =
11436 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11437 if (include_name != NULL)
11438 dwarf2_create_include_psymtab (include_name, pst, objfile);
11439 }
11440 }
11441 else
11442 {
11443 /* Make sure a symtab is created for every file, even files
11444 which contain only variables (i.e. no code with associated
11445 line numbers). */
11446 int i;
11447
11448 for (i = 0; i < lh->num_file_names; i++)
11449 {
11450 char *dir = NULL;
11451 struct file_entry *fe;
11452
11453 fe = &lh->file_names[i];
11454 if (fe->dir_index)
11455 dir = lh->include_dirs[fe->dir_index - 1];
11456 dwarf2_start_subfile (fe->name, dir, comp_dir);
11457
11458 /* Skip the main file; we don't need it, and it must be
11459 allocated last, so that it will show up before the
11460 non-primary symtabs in the objfile's symtab list. */
11461 if (current_subfile == first_subfile)
11462 continue;
11463
11464 if (current_subfile->symtab == NULL)
11465 current_subfile->symtab = allocate_symtab (current_subfile->name,
11466 objfile);
11467 fe->symtab = current_subfile->symtab;
11468 }
11469 }
11470 }
11471
11472 /* Start a subfile for DWARF. FILENAME is the name of the file and
11473 DIRNAME the name of the source directory which contains FILENAME
11474 or NULL if not known. COMP_DIR is the compilation directory for the
11475 linetable's compilation unit or NULL if not known.
11476 This routine tries to keep line numbers from identical absolute and
11477 relative file names in a common subfile.
11478
11479 Using the `list' example from the GDB testsuite, which resides in
11480 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11481 of /srcdir/list0.c yields the following debugging information for list0.c:
11482
11483 DW_AT_name: /srcdir/list0.c
11484 DW_AT_comp_dir: /compdir
11485 files.files[0].name: list0.h
11486 files.files[0].dir: /srcdir
11487 files.files[1].name: list0.c
11488 files.files[1].dir: /srcdir
11489
11490 The line number information for list0.c has to end up in a single
11491 subfile, so that `break /srcdir/list0.c:1' works as expected.
11492 start_subfile will ensure that this happens provided that we pass the
11493 concatenation of files.files[1].dir and files.files[1].name as the
11494 subfile's name. */
11495
11496 static void
11497 dwarf2_start_subfile (char *filename, const char *dirname,
11498 const char *comp_dir)
11499 {
11500 char *fullname;
11501
11502 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11503 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11504 second argument to start_subfile. To be consistent, we do the
11505 same here. In order not to lose the line information directory,
11506 we concatenate it to the filename when it makes sense.
11507 Note that the Dwarf3 standard says (speaking of filenames in line
11508 information): ``The directory index is ignored for file names
11509 that represent full path names''. Thus ignoring dirname in the
11510 `else' branch below isn't an issue. */
11511
11512 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11513 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11514 else
11515 fullname = filename;
11516
11517 start_subfile (fullname, comp_dir);
11518
11519 if (fullname != filename)
11520 xfree (fullname);
11521 }
11522
11523 static void
11524 var_decode_location (struct attribute *attr, struct symbol *sym,
11525 struct dwarf2_cu *cu)
11526 {
11527 struct objfile *objfile = cu->objfile;
11528 struct comp_unit_head *cu_header = &cu->header;
11529
11530 /* NOTE drow/2003-01-30: There used to be a comment and some special
11531 code here to turn a symbol with DW_AT_external and a
11532 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11533 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11534 with some versions of binutils) where shared libraries could have
11535 relocations against symbols in their debug information - the
11536 minimal symbol would have the right address, but the debug info
11537 would not. It's no longer necessary, because we will explicitly
11538 apply relocations when we read in the debug information now. */
11539
11540 /* A DW_AT_location attribute with no contents indicates that a
11541 variable has been optimized away. */
11542 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11543 {
11544 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11545 return;
11546 }
11547
11548 /* Handle one degenerate form of location expression specially, to
11549 preserve GDB's previous behavior when section offsets are
11550 specified. If this is just a DW_OP_addr then mark this symbol
11551 as LOC_STATIC. */
11552
11553 if (attr_form_is_block (attr)
11554 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11555 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11556 {
11557 unsigned int dummy;
11558
11559 SYMBOL_VALUE_ADDRESS (sym) =
11560 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11561 SYMBOL_CLASS (sym) = LOC_STATIC;
11562 fixup_symbol_section (sym, objfile);
11563 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11564 SYMBOL_SECTION (sym));
11565 return;
11566 }
11567
11568 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11569 expression evaluator, and use LOC_COMPUTED only when necessary
11570 (i.e. when the value of a register or memory location is
11571 referenced, or a thread-local block, etc.). Then again, it might
11572 not be worthwhile. I'm assuming that it isn't unless performance
11573 or memory numbers show me otherwise. */
11574
11575 dwarf2_symbol_mark_computed (attr, sym, cu);
11576 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11577
11578 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11579 cu->has_loclist = 1;
11580 }
11581
11582 /* Given a pointer to a DWARF information entry, figure out if we need
11583 to make a symbol table entry for it, and if so, create a new entry
11584 and return a pointer to it.
11585 If TYPE is NULL, determine symbol type from the die, otherwise
11586 used the passed type.
11587 If SPACE is not NULL, use it to hold the new symbol. If it is
11588 NULL, allocate a new symbol on the objfile's obstack. */
11589
11590 static struct symbol *
11591 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11592 struct symbol *space)
11593 {
11594 struct objfile *objfile = cu->objfile;
11595 struct symbol *sym = NULL;
11596 char *name;
11597 struct attribute *attr = NULL;
11598 struct attribute *attr2 = NULL;
11599 CORE_ADDR baseaddr;
11600 struct pending **list_to_add = NULL;
11601
11602 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11603
11604 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11605
11606 name = dwarf2_name (die, cu);
11607 if (name)
11608 {
11609 const char *linkagename;
11610 int suppress_add = 0;
11611
11612 if (space)
11613 sym = space;
11614 else
11615 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11616 OBJSTAT (objfile, n_syms++);
11617
11618 /* Cache this symbol's name and the name's demangled form (if any). */
11619 SYMBOL_SET_LANGUAGE (sym, cu->language);
11620 linkagename = dwarf2_physname (name, die, cu);
11621 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11622
11623 /* Fortran does not have mangling standard and the mangling does differ
11624 between gfortran, iFort etc. */
11625 if (cu->language == language_fortran
11626 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11627 symbol_set_demangled_name (&(sym->ginfo),
11628 (char *) dwarf2_full_name (name, die, cu),
11629 NULL);
11630
11631 /* Default assumptions.
11632 Use the passed type or decode it from the die. */
11633 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11634 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11635 if (type != NULL)
11636 SYMBOL_TYPE (sym) = type;
11637 else
11638 SYMBOL_TYPE (sym) = die_type (die, cu);
11639 attr = dwarf2_attr (die,
11640 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11641 cu);
11642 if (attr)
11643 {
11644 SYMBOL_LINE (sym) = DW_UNSND (attr);
11645 }
11646
11647 attr = dwarf2_attr (die,
11648 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11649 cu);
11650 if (attr)
11651 {
11652 int file_index = DW_UNSND (attr);
11653
11654 if (cu->line_header == NULL
11655 || file_index > cu->line_header->num_file_names)
11656 complaint (&symfile_complaints,
11657 _("file index out of range"));
11658 else if (file_index > 0)
11659 {
11660 struct file_entry *fe;
11661
11662 fe = &cu->line_header->file_names[file_index - 1];
11663 SYMBOL_SYMTAB (sym) = fe->symtab;
11664 }
11665 }
11666
11667 switch (die->tag)
11668 {
11669 case DW_TAG_label:
11670 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11671 if (attr)
11672 {
11673 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11674 }
11675 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11676 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11677 SYMBOL_CLASS (sym) = LOC_LABEL;
11678 add_symbol_to_list (sym, cu->list_in_scope);
11679 break;
11680 case DW_TAG_subprogram:
11681 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11682 finish_block. */
11683 SYMBOL_CLASS (sym) = LOC_BLOCK;
11684 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11685 if ((attr2 && (DW_UNSND (attr2) != 0))
11686 || cu->language == language_ada)
11687 {
11688 /* Subprograms marked external are stored as a global symbol.
11689 Ada subprograms, whether marked external or not, are always
11690 stored as a global symbol, because we want to be able to
11691 access them globally. For instance, we want to be able
11692 to break on a nested subprogram without having to
11693 specify the context. */
11694 list_to_add = &global_symbols;
11695 }
11696 else
11697 {
11698 list_to_add = cu->list_in_scope;
11699 }
11700 break;
11701 case DW_TAG_inlined_subroutine:
11702 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11703 finish_block. */
11704 SYMBOL_CLASS (sym) = LOC_BLOCK;
11705 SYMBOL_INLINED (sym) = 1;
11706 /* Do not add the symbol to any lists. It will be found via
11707 BLOCK_FUNCTION from the blockvector. */
11708 break;
11709 case DW_TAG_template_value_param:
11710 suppress_add = 1;
11711 /* Fall through. */
11712 case DW_TAG_constant:
11713 case DW_TAG_variable:
11714 case DW_TAG_member:
11715 /* Compilation with minimal debug info may result in
11716 variables with missing type entries. Change the
11717 misleading `void' type to something sensible. */
11718 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11719 SYMBOL_TYPE (sym)
11720 = objfile_type (objfile)->nodebug_data_symbol;
11721
11722 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11723 /* In the case of DW_TAG_member, we should only be called for
11724 static const members. */
11725 if (die->tag == DW_TAG_member)
11726 {
11727 /* dwarf2_add_field uses die_is_declaration,
11728 so we do the same. */
11729 gdb_assert (die_is_declaration (die, cu));
11730 gdb_assert (attr);
11731 }
11732 if (attr)
11733 {
11734 dwarf2_const_value (attr, sym, cu);
11735 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11736 if (!suppress_add)
11737 {
11738 if (attr2 && (DW_UNSND (attr2) != 0))
11739 list_to_add = &global_symbols;
11740 else
11741 list_to_add = cu->list_in_scope;
11742 }
11743 break;
11744 }
11745 attr = dwarf2_attr (die, DW_AT_location, cu);
11746 if (attr)
11747 {
11748 var_decode_location (attr, sym, cu);
11749 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11750 if (SYMBOL_CLASS (sym) == LOC_STATIC
11751 && SYMBOL_VALUE_ADDRESS (sym) == 0
11752 && !dwarf2_per_objfile->has_section_at_zero)
11753 {
11754 /* When a static variable is eliminated by the linker,
11755 the corresponding debug information is not stripped
11756 out, but the variable address is set to null;
11757 do not add such variables into symbol table. */
11758 }
11759 else if (attr2 && (DW_UNSND (attr2) != 0))
11760 {
11761 /* Workaround gfortran PR debug/40040 - it uses
11762 DW_AT_location for variables in -fPIC libraries which may
11763 get overriden by other libraries/executable and get
11764 a different address. Resolve it by the minimal symbol
11765 which may come from inferior's executable using copy
11766 relocation. Make this workaround only for gfortran as for
11767 other compilers GDB cannot guess the minimal symbol
11768 Fortran mangling kind. */
11769 if (cu->language == language_fortran && die->parent
11770 && die->parent->tag == DW_TAG_module
11771 && cu->producer
11772 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11773 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11774
11775 /* A variable with DW_AT_external is never static,
11776 but it may be block-scoped. */
11777 list_to_add = (cu->list_in_scope == &file_symbols
11778 ? &global_symbols : cu->list_in_scope);
11779 }
11780 else
11781 list_to_add = cu->list_in_scope;
11782 }
11783 else
11784 {
11785 /* We do not know the address of this symbol.
11786 If it is an external symbol and we have type information
11787 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11788 The address of the variable will then be determined from
11789 the minimal symbol table whenever the variable is
11790 referenced. */
11791 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11792 if (attr2 && (DW_UNSND (attr2) != 0)
11793 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11794 {
11795 /* A variable with DW_AT_external is never static, but it
11796 may be block-scoped. */
11797 list_to_add = (cu->list_in_scope == &file_symbols
11798 ? &global_symbols : cu->list_in_scope);
11799
11800 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11801 }
11802 else if (!die_is_declaration (die, cu))
11803 {
11804 /* Use the default LOC_OPTIMIZED_OUT class. */
11805 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11806 if (!suppress_add)
11807 list_to_add = cu->list_in_scope;
11808 }
11809 }
11810 break;
11811 case DW_TAG_formal_parameter:
11812 /* If we are inside a function, mark this as an argument. If
11813 not, we might be looking at an argument to an inlined function
11814 when we do not have enough information to show inlined frames;
11815 pretend it's a local variable in that case so that the user can
11816 still see it. */
11817 if (context_stack_depth > 0
11818 && context_stack[context_stack_depth - 1].name != NULL)
11819 SYMBOL_IS_ARGUMENT (sym) = 1;
11820 attr = dwarf2_attr (die, DW_AT_location, cu);
11821 if (attr)
11822 {
11823 var_decode_location (attr, sym, cu);
11824 }
11825 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11826 if (attr)
11827 {
11828 dwarf2_const_value (attr, sym, cu);
11829 }
11830
11831 list_to_add = cu->list_in_scope;
11832 break;
11833 case DW_TAG_unspecified_parameters:
11834 /* From varargs functions; gdb doesn't seem to have any
11835 interest in this information, so just ignore it for now.
11836 (FIXME?) */
11837 break;
11838 case DW_TAG_template_type_param:
11839 suppress_add = 1;
11840 /* Fall through. */
11841 case DW_TAG_class_type:
11842 case DW_TAG_interface_type:
11843 case DW_TAG_structure_type:
11844 case DW_TAG_union_type:
11845 case DW_TAG_set_type:
11846 case DW_TAG_enumeration_type:
11847 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11848 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11849
11850 {
11851 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11852 really ever be static objects: otherwise, if you try
11853 to, say, break of a class's method and you're in a file
11854 which doesn't mention that class, it won't work unless
11855 the check for all static symbols in lookup_symbol_aux
11856 saves you. See the OtherFileClass tests in
11857 gdb.c++/namespace.exp. */
11858
11859 if (!suppress_add)
11860 {
11861 list_to_add = (cu->list_in_scope == &file_symbols
11862 && (cu->language == language_cplus
11863 || cu->language == language_java)
11864 ? &global_symbols : cu->list_in_scope);
11865
11866 /* The semantics of C++ state that "struct foo {
11867 ... }" also defines a typedef for "foo". A Java
11868 class declaration also defines a typedef for the
11869 class. */
11870 if (cu->language == language_cplus
11871 || cu->language == language_java
11872 || cu->language == language_ada)
11873 {
11874 /* The symbol's name is already allocated along
11875 with this objfile, so we don't need to
11876 duplicate it for the type. */
11877 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11878 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11879 }
11880 }
11881 }
11882 break;
11883 case DW_TAG_typedef:
11884 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11885 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11886 list_to_add = cu->list_in_scope;
11887 break;
11888 case DW_TAG_base_type:
11889 case DW_TAG_subrange_type:
11890 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11891 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11892 list_to_add = cu->list_in_scope;
11893 break;
11894 case DW_TAG_enumerator:
11895 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11896 if (attr)
11897 {
11898 dwarf2_const_value (attr, sym, cu);
11899 }
11900 {
11901 /* NOTE: carlton/2003-11-10: See comment above in the
11902 DW_TAG_class_type, etc. block. */
11903
11904 list_to_add = (cu->list_in_scope == &file_symbols
11905 && (cu->language == language_cplus
11906 || cu->language == language_java)
11907 ? &global_symbols : cu->list_in_scope);
11908 }
11909 break;
11910 case DW_TAG_namespace:
11911 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11912 list_to_add = &global_symbols;
11913 break;
11914 default:
11915 /* Not a tag we recognize. Hopefully we aren't processing
11916 trash data, but since we must specifically ignore things
11917 we don't recognize, there is nothing else we should do at
11918 this point. */
11919 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11920 dwarf_tag_name (die->tag));
11921 break;
11922 }
11923
11924 if (suppress_add)
11925 {
11926 sym->hash_next = objfile->template_symbols;
11927 objfile->template_symbols = sym;
11928 list_to_add = NULL;
11929 }
11930
11931 if (list_to_add != NULL)
11932 add_symbol_to_list (sym, list_to_add);
11933
11934 /* For the benefit of old versions of GCC, check for anonymous
11935 namespaces based on the demangled name. */
11936 if (!processing_has_namespace_info
11937 && cu->language == language_cplus)
11938 cp_scan_for_anonymous_namespaces (sym, objfile);
11939 }
11940 return (sym);
11941 }
11942
11943 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11944
11945 static struct symbol *
11946 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11947 {
11948 return new_symbol_full (die, type, cu, NULL);
11949 }
11950
11951 /* Given an attr with a DW_FORM_dataN value in host byte order,
11952 zero-extend it as appropriate for the symbol's type. The DWARF
11953 standard (v4) is not entirely clear about the meaning of using
11954 DW_FORM_dataN for a constant with a signed type, where the type is
11955 wider than the data. The conclusion of a discussion on the DWARF
11956 list was that this is unspecified. We choose to always zero-extend
11957 because that is the interpretation long in use by GCC. */
11958
11959 static gdb_byte *
11960 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11961 const char *name, struct obstack *obstack,
11962 struct dwarf2_cu *cu, long *value, int bits)
11963 {
11964 struct objfile *objfile = cu->objfile;
11965 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11966 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11967 LONGEST l = DW_UNSND (attr);
11968
11969 if (bits < sizeof (*value) * 8)
11970 {
11971 l &= ((LONGEST) 1 << bits) - 1;
11972 *value = l;
11973 }
11974 else if (bits == sizeof (*value) * 8)
11975 *value = l;
11976 else
11977 {
11978 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11979 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11980 return bytes;
11981 }
11982
11983 return NULL;
11984 }
11985
11986 /* Read a constant value from an attribute. Either set *VALUE, or if
11987 the value does not fit in *VALUE, set *BYTES - either already
11988 allocated on the objfile obstack, or newly allocated on OBSTACK,
11989 or, set *BATON, if we translated the constant to a location
11990 expression. */
11991
11992 static void
11993 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11994 const char *name, struct obstack *obstack,
11995 struct dwarf2_cu *cu,
11996 long *value, gdb_byte **bytes,
11997 struct dwarf2_locexpr_baton **baton)
11998 {
11999 struct objfile *objfile = cu->objfile;
12000 struct comp_unit_head *cu_header = &cu->header;
12001 struct dwarf_block *blk;
12002 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12003 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12004
12005 *value = 0;
12006 *bytes = NULL;
12007 *baton = NULL;
12008
12009 switch (attr->form)
12010 {
12011 case DW_FORM_addr:
12012 {
12013 gdb_byte *data;
12014
12015 if (TYPE_LENGTH (type) != cu_header->addr_size)
12016 dwarf2_const_value_length_mismatch_complaint (name,
12017 cu_header->addr_size,
12018 TYPE_LENGTH (type));
12019 /* Symbols of this form are reasonably rare, so we just
12020 piggyback on the existing location code rather than writing
12021 a new implementation of symbol_computed_ops. */
12022 *baton = obstack_alloc (&objfile->objfile_obstack,
12023 sizeof (struct dwarf2_locexpr_baton));
12024 (*baton)->per_cu = cu->per_cu;
12025 gdb_assert ((*baton)->per_cu);
12026
12027 (*baton)->size = 2 + cu_header->addr_size;
12028 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12029 (*baton)->data = data;
12030
12031 data[0] = DW_OP_addr;
12032 store_unsigned_integer (&data[1], cu_header->addr_size,
12033 byte_order, DW_ADDR (attr));
12034 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12035 }
12036 break;
12037 case DW_FORM_string:
12038 case DW_FORM_strp:
12039 /* DW_STRING is already allocated on the objfile obstack, point
12040 directly to it. */
12041 *bytes = (gdb_byte *) DW_STRING (attr);
12042 break;
12043 case DW_FORM_block1:
12044 case DW_FORM_block2:
12045 case DW_FORM_block4:
12046 case DW_FORM_block:
12047 case DW_FORM_exprloc:
12048 blk = DW_BLOCK (attr);
12049 if (TYPE_LENGTH (type) != blk->size)
12050 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12051 TYPE_LENGTH (type));
12052 *bytes = blk->data;
12053 break;
12054
12055 /* The DW_AT_const_value attributes are supposed to carry the
12056 symbol's value "represented as it would be on the target
12057 architecture." By the time we get here, it's already been
12058 converted to host endianness, so we just need to sign- or
12059 zero-extend it as appropriate. */
12060 case DW_FORM_data1:
12061 *bytes = dwarf2_const_value_data (attr, type, name,
12062 obstack, cu, value, 8);
12063 break;
12064 case DW_FORM_data2:
12065 *bytes = dwarf2_const_value_data (attr, type, name,
12066 obstack, cu, value, 16);
12067 break;
12068 case DW_FORM_data4:
12069 *bytes = dwarf2_const_value_data (attr, type, name,
12070 obstack, cu, value, 32);
12071 break;
12072 case DW_FORM_data8:
12073 *bytes = dwarf2_const_value_data (attr, type, name,
12074 obstack, cu, value, 64);
12075 break;
12076
12077 case DW_FORM_sdata:
12078 *value = DW_SND (attr);
12079 break;
12080
12081 case DW_FORM_udata:
12082 *value = DW_UNSND (attr);
12083 break;
12084
12085 default:
12086 complaint (&symfile_complaints,
12087 _("unsupported const value attribute form: '%s'"),
12088 dwarf_form_name (attr->form));
12089 *value = 0;
12090 break;
12091 }
12092 }
12093
12094
12095 /* Copy constant value from an attribute to a symbol. */
12096
12097 static void
12098 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12099 struct dwarf2_cu *cu)
12100 {
12101 struct objfile *objfile = cu->objfile;
12102 struct comp_unit_head *cu_header = &cu->header;
12103 long value;
12104 gdb_byte *bytes;
12105 struct dwarf2_locexpr_baton *baton;
12106
12107 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12108 SYMBOL_PRINT_NAME (sym),
12109 &objfile->objfile_obstack, cu,
12110 &value, &bytes, &baton);
12111
12112 if (baton != NULL)
12113 {
12114 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12115 SYMBOL_LOCATION_BATON (sym) = baton;
12116 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12117 }
12118 else if (bytes != NULL)
12119 {
12120 SYMBOL_VALUE_BYTES (sym) = bytes;
12121 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12122 }
12123 else
12124 {
12125 SYMBOL_VALUE (sym) = value;
12126 SYMBOL_CLASS (sym) = LOC_CONST;
12127 }
12128 }
12129
12130 /* Return the type of the die in question using its DW_AT_type attribute. */
12131
12132 static struct type *
12133 die_type (struct die_info *die, struct dwarf2_cu *cu)
12134 {
12135 struct attribute *type_attr;
12136
12137 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12138 if (!type_attr)
12139 {
12140 /* A missing DW_AT_type represents a void type. */
12141 return objfile_type (cu->objfile)->builtin_void;
12142 }
12143
12144 return lookup_die_type (die, type_attr, cu);
12145 }
12146
12147 /* True iff CU's producer generates GNAT Ada auxiliary information
12148 that allows to find parallel types through that information instead
12149 of having to do expensive parallel lookups by type name. */
12150
12151 static int
12152 need_gnat_info (struct dwarf2_cu *cu)
12153 {
12154 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12155 of GNAT produces this auxiliary information, without any indication
12156 that it is produced. Part of enhancing the FSF version of GNAT
12157 to produce that information will be to put in place an indicator
12158 that we can use in order to determine whether the descriptive type
12159 info is available or not. One suggestion that has been made is
12160 to use a new attribute, attached to the CU die. For now, assume
12161 that the descriptive type info is not available. */
12162 return 0;
12163 }
12164
12165 /* Return the auxiliary type of the die in question using its
12166 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12167 attribute is not present. */
12168
12169 static struct type *
12170 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12171 {
12172 struct attribute *type_attr;
12173
12174 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12175 if (!type_attr)
12176 return NULL;
12177
12178 return lookup_die_type (die, type_attr, cu);
12179 }
12180
12181 /* If DIE has a descriptive_type attribute, then set the TYPE's
12182 descriptive type accordingly. */
12183
12184 static void
12185 set_descriptive_type (struct type *type, struct die_info *die,
12186 struct dwarf2_cu *cu)
12187 {
12188 struct type *descriptive_type = die_descriptive_type (die, cu);
12189
12190 if (descriptive_type)
12191 {
12192 ALLOCATE_GNAT_AUX_TYPE (type);
12193 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12194 }
12195 }
12196
12197 /* Return the containing type of the die in question using its
12198 DW_AT_containing_type attribute. */
12199
12200 static struct type *
12201 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12202 {
12203 struct attribute *type_attr;
12204
12205 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12206 if (!type_attr)
12207 error (_("Dwarf Error: Problem turning containing type into gdb type "
12208 "[in module %s]"), cu->objfile->name);
12209
12210 return lookup_die_type (die, type_attr, cu);
12211 }
12212
12213 /* Look up the type of DIE in CU using its type attribute ATTR.
12214 If there is no type substitute an error marker. */
12215
12216 static struct type *
12217 lookup_die_type (struct die_info *die, struct attribute *attr,
12218 struct dwarf2_cu *cu)
12219 {
12220 struct objfile *objfile = cu->objfile;
12221 struct type *this_type;
12222
12223 /* First see if we have it cached. */
12224
12225 if (is_ref_attr (attr))
12226 {
12227 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12228
12229 this_type = get_die_type_at_offset (offset, cu->per_cu);
12230 }
12231 else if (attr->form == DW_FORM_ref_sig8)
12232 {
12233 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12234 struct dwarf2_cu *sig_cu;
12235 unsigned int offset;
12236
12237 /* sig_type will be NULL if the signatured type is missing from
12238 the debug info. */
12239 if (sig_type == NULL)
12240 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12241 "at 0x%x [in module %s]"),
12242 die->offset, objfile->name);
12243
12244 gdb_assert (sig_type->per_cu.debug_types_section);
12245 offset = sig_type->per_cu.offset + sig_type->type_offset;
12246 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12247 }
12248 else
12249 {
12250 dump_die_for_error (die);
12251 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12252 dwarf_attr_name (attr->name), objfile->name);
12253 }
12254
12255 /* If not cached we need to read it in. */
12256
12257 if (this_type == NULL)
12258 {
12259 struct die_info *type_die;
12260 struct dwarf2_cu *type_cu = cu;
12261
12262 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12263 /* If the type is cached, we should have found it above. */
12264 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12265 this_type = read_type_die_1 (type_die, type_cu);
12266 }
12267
12268 /* If we still don't have a type use an error marker. */
12269
12270 if (this_type == NULL)
12271 {
12272 char *message, *saved;
12273
12274 /* read_type_die already issued a complaint. */
12275 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12276 objfile->name,
12277 cu->header.offset,
12278 die->offset);
12279 saved = obstack_copy0 (&objfile->objfile_obstack,
12280 message, strlen (message));
12281 xfree (message);
12282
12283 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12284 }
12285
12286 return this_type;
12287 }
12288
12289 /* Return the type in DIE, CU.
12290 Returns NULL for invalid types.
12291
12292 This first does a lookup in the appropriate type_hash table,
12293 and only reads the die in if necessary.
12294
12295 NOTE: This can be called when reading in partial or full symbols. */
12296
12297 static struct type *
12298 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12299 {
12300 struct type *this_type;
12301
12302 this_type = get_die_type (die, cu);
12303 if (this_type)
12304 return this_type;
12305
12306 return read_type_die_1 (die, cu);
12307 }
12308
12309 /* Read the type in DIE, CU.
12310 Returns NULL for invalid types. */
12311
12312 static struct type *
12313 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12314 {
12315 struct type *this_type = NULL;
12316
12317 switch (die->tag)
12318 {
12319 case DW_TAG_class_type:
12320 case DW_TAG_interface_type:
12321 case DW_TAG_structure_type:
12322 case DW_TAG_union_type:
12323 this_type = read_structure_type (die, cu);
12324 break;
12325 case DW_TAG_enumeration_type:
12326 this_type = read_enumeration_type (die, cu);
12327 break;
12328 case DW_TAG_subprogram:
12329 case DW_TAG_subroutine_type:
12330 case DW_TAG_inlined_subroutine:
12331 this_type = read_subroutine_type (die, cu);
12332 break;
12333 case DW_TAG_array_type:
12334 this_type = read_array_type (die, cu);
12335 break;
12336 case DW_TAG_set_type:
12337 this_type = read_set_type (die, cu);
12338 break;
12339 case DW_TAG_pointer_type:
12340 this_type = read_tag_pointer_type (die, cu);
12341 break;
12342 case DW_TAG_ptr_to_member_type:
12343 this_type = read_tag_ptr_to_member_type (die, cu);
12344 break;
12345 case DW_TAG_reference_type:
12346 this_type = read_tag_reference_type (die, cu);
12347 break;
12348 case DW_TAG_const_type:
12349 this_type = read_tag_const_type (die, cu);
12350 break;
12351 case DW_TAG_volatile_type:
12352 this_type = read_tag_volatile_type (die, cu);
12353 break;
12354 case DW_TAG_string_type:
12355 this_type = read_tag_string_type (die, cu);
12356 break;
12357 case DW_TAG_typedef:
12358 this_type = read_typedef (die, cu);
12359 break;
12360 case DW_TAG_subrange_type:
12361 this_type = read_subrange_type (die, cu);
12362 break;
12363 case DW_TAG_base_type:
12364 this_type = read_base_type (die, cu);
12365 break;
12366 case DW_TAG_unspecified_type:
12367 this_type = read_unspecified_type (die, cu);
12368 break;
12369 case DW_TAG_namespace:
12370 this_type = read_namespace_type (die, cu);
12371 break;
12372 case DW_TAG_module:
12373 this_type = read_module_type (die, cu);
12374 break;
12375 default:
12376 complaint (&symfile_complaints,
12377 _("unexpected tag in read_type_die: '%s'"),
12378 dwarf_tag_name (die->tag));
12379 break;
12380 }
12381
12382 return this_type;
12383 }
12384
12385 /* See if we can figure out if the class lives in a namespace. We do
12386 this by looking for a member function; its demangled name will
12387 contain namespace info, if there is any.
12388 Return the computed name or NULL.
12389 Space for the result is allocated on the objfile's obstack.
12390 This is the full-die version of guess_partial_die_structure_name.
12391 In this case we know DIE has no useful parent. */
12392
12393 static char *
12394 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12395 {
12396 struct die_info *spec_die;
12397 struct dwarf2_cu *spec_cu;
12398 struct die_info *child;
12399
12400 spec_cu = cu;
12401 spec_die = die_specification (die, &spec_cu);
12402 if (spec_die != NULL)
12403 {
12404 die = spec_die;
12405 cu = spec_cu;
12406 }
12407
12408 for (child = die->child;
12409 child != NULL;
12410 child = child->sibling)
12411 {
12412 if (child->tag == DW_TAG_subprogram)
12413 {
12414 struct attribute *attr;
12415
12416 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12417 if (attr == NULL)
12418 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12419 if (attr != NULL)
12420 {
12421 char *actual_name
12422 = language_class_name_from_physname (cu->language_defn,
12423 DW_STRING (attr));
12424 char *name = NULL;
12425
12426 if (actual_name != NULL)
12427 {
12428 char *die_name = dwarf2_name (die, cu);
12429
12430 if (die_name != NULL
12431 && strcmp (die_name, actual_name) != 0)
12432 {
12433 /* Strip off the class name from the full name.
12434 We want the prefix. */
12435 int die_name_len = strlen (die_name);
12436 int actual_name_len = strlen (actual_name);
12437
12438 /* Test for '::' as a sanity check. */
12439 if (actual_name_len > die_name_len + 2
12440 && actual_name[actual_name_len
12441 - die_name_len - 1] == ':')
12442 name =
12443 obsavestring (actual_name,
12444 actual_name_len - die_name_len - 2,
12445 &cu->objfile->objfile_obstack);
12446 }
12447 }
12448 xfree (actual_name);
12449 return name;
12450 }
12451 }
12452 }
12453
12454 return NULL;
12455 }
12456
12457 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12458 prefix part in such case. See
12459 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12460
12461 static char *
12462 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12463 {
12464 struct attribute *attr;
12465 char *base;
12466
12467 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12468 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12469 return NULL;
12470
12471 attr = dwarf2_attr (die, DW_AT_name, cu);
12472 if (attr != NULL && DW_STRING (attr) != NULL)
12473 return NULL;
12474
12475 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12476 if (attr == NULL)
12477 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12478 if (attr == NULL || DW_STRING (attr) == NULL)
12479 return NULL;
12480
12481 /* dwarf2_name had to be already called. */
12482 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12483
12484 /* Strip the base name, keep any leading namespaces/classes. */
12485 base = strrchr (DW_STRING (attr), ':');
12486 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12487 return "";
12488
12489 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12490 &cu->objfile->objfile_obstack);
12491 }
12492
12493 /* Return the name of the namespace/class that DIE is defined within,
12494 or "" if we can't tell. The caller should not xfree the result.
12495
12496 For example, if we're within the method foo() in the following
12497 code:
12498
12499 namespace N {
12500 class C {
12501 void foo () {
12502 }
12503 };
12504 }
12505
12506 then determine_prefix on foo's die will return "N::C". */
12507
12508 static const char *
12509 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12510 {
12511 struct die_info *parent, *spec_die;
12512 struct dwarf2_cu *spec_cu;
12513 struct type *parent_type;
12514 char *retval;
12515
12516 if (cu->language != language_cplus && cu->language != language_java
12517 && cu->language != language_fortran)
12518 return "";
12519
12520 retval = anonymous_struct_prefix (die, cu);
12521 if (retval)
12522 return retval;
12523
12524 /* We have to be careful in the presence of DW_AT_specification.
12525 For example, with GCC 3.4, given the code
12526
12527 namespace N {
12528 void foo() {
12529 // Definition of N::foo.
12530 }
12531 }
12532
12533 then we'll have a tree of DIEs like this:
12534
12535 1: DW_TAG_compile_unit
12536 2: DW_TAG_namespace // N
12537 3: DW_TAG_subprogram // declaration of N::foo
12538 4: DW_TAG_subprogram // definition of N::foo
12539 DW_AT_specification // refers to die #3
12540
12541 Thus, when processing die #4, we have to pretend that we're in
12542 the context of its DW_AT_specification, namely the contex of die
12543 #3. */
12544 spec_cu = cu;
12545 spec_die = die_specification (die, &spec_cu);
12546 if (spec_die == NULL)
12547 parent = die->parent;
12548 else
12549 {
12550 parent = spec_die->parent;
12551 cu = spec_cu;
12552 }
12553
12554 if (parent == NULL)
12555 return "";
12556 else if (parent->building_fullname)
12557 {
12558 const char *name;
12559 const char *parent_name;
12560
12561 /* It has been seen on RealView 2.2 built binaries,
12562 DW_TAG_template_type_param types actually _defined_ as
12563 children of the parent class:
12564
12565 enum E {};
12566 template class <class Enum> Class{};
12567 Class<enum E> class_e;
12568
12569 1: DW_TAG_class_type (Class)
12570 2: DW_TAG_enumeration_type (E)
12571 3: DW_TAG_enumerator (enum1:0)
12572 3: DW_TAG_enumerator (enum2:1)
12573 ...
12574 2: DW_TAG_template_type_param
12575 DW_AT_type DW_FORM_ref_udata (E)
12576
12577 Besides being broken debug info, it can put GDB into an
12578 infinite loop. Consider:
12579
12580 When we're building the full name for Class<E>, we'll start
12581 at Class, and go look over its template type parameters,
12582 finding E. We'll then try to build the full name of E, and
12583 reach here. We're now trying to build the full name of E,
12584 and look over the parent DIE for containing scope. In the
12585 broken case, if we followed the parent DIE of E, we'd again
12586 find Class, and once again go look at its template type
12587 arguments, etc., etc. Simply don't consider such parent die
12588 as source-level parent of this die (it can't be, the language
12589 doesn't allow it), and break the loop here. */
12590 name = dwarf2_name (die, cu);
12591 parent_name = dwarf2_name (parent, cu);
12592 complaint (&symfile_complaints,
12593 _("template param type '%s' defined within parent '%s'"),
12594 name ? name : "<unknown>",
12595 parent_name ? parent_name : "<unknown>");
12596 return "";
12597 }
12598 else
12599 switch (parent->tag)
12600 {
12601 case DW_TAG_namespace:
12602 parent_type = read_type_die (parent, cu);
12603 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12604 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12605 Work around this problem here. */
12606 if (cu->language == language_cplus
12607 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12608 return "";
12609 /* We give a name to even anonymous namespaces. */
12610 return TYPE_TAG_NAME (parent_type);
12611 case DW_TAG_class_type:
12612 case DW_TAG_interface_type:
12613 case DW_TAG_structure_type:
12614 case DW_TAG_union_type:
12615 case DW_TAG_module:
12616 parent_type = read_type_die (parent, cu);
12617 if (TYPE_TAG_NAME (parent_type) != NULL)
12618 return TYPE_TAG_NAME (parent_type);
12619 else
12620 /* An anonymous structure is only allowed non-static data
12621 members; no typedefs, no member functions, et cetera.
12622 So it does not need a prefix. */
12623 return "";
12624 case DW_TAG_compile_unit:
12625 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12626 if (cu->language == language_cplus
12627 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12628 && die->child != NULL
12629 && (die->tag == DW_TAG_class_type
12630 || die->tag == DW_TAG_structure_type
12631 || die->tag == DW_TAG_union_type))
12632 {
12633 char *name = guess_full_die_structure_name (die, cu);
12634 if (name != NULL)
12635 return name;
12636 }
12637 return "";
12638 default:
12639 return determine_prefix (parent, cu);
12640 }
12641 }
12642
12643 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12644 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12645 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12646 an obconcat, otherwise allocate storage for the result. The CU argument is
12647 used to determine the language and hence, the appropriate separator. */
12648
12649 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12650
12651 static char *
12652 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12653 int physname, struct dwarf2_cu *cu)
12654 {
12655 const char *lead = "";
12656 const char *sep;
12657
12658 if (suffix == NULL || suffix[0] == '\0'
12659 || prefix == NULL || prefix[0] == '\0')
12660 sep = "";
12661 else if (cu->language == language_java)
12662 sep = ".";
12663 else if (cu->language == language_fortran && physname)
12664 {
12665 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12666 DW_AT_MIPS_linkage_name is preferred and used instead. */
12667
12668 lead = "__";
12669 sep = "_MOD_";
12670 }
12671 else
12672 sep = "::";
12673
12674 if (prefix == NULL)
12675 prefix = "";
12676 if (suffix == NULL)
12677 suffix = "";
12678
12679 if (obs == NULL)
12680 {
12681 char *retval
12682 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12683
12684 strcpy (retval, lead);
12685 strcat (retval, prefix);
12686 strcat (retval, sep);
12687 strcat (retval, suffix);
12688 return retval;
12689 }
12690 else
12691 {
12692 /* We have an obstack. */
12693 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12694 }
12695 }
12696
12697 /* Return sibling of die, NULL if no sibling. */
12698
12699 static struct die_info *
12700 sibling_die (struct die_info *die)
12701 {
12702 return die->sibling;
12703 }
12704
12705 /* Get name of a die, return NULL if not found. */
12706
12707 static char *
12708 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12709 struct obstack *obstack)
12710 {
12711 if (name && cu->language == language_cplus)
12712 {
12713 char *canon_name = cp_canonicalize_string (name);
12714
12715 if (canon_name != NULL)
12716 {
12717 if (strcmp (canon_name, name) != 0)
12718 name = obsavestring (canon_name, strlen (canon_name),
12719 obstack);
12720 xfree (canon_name);
12721 }
12722 }
12723
12724 return name;
12725 }
12726
12727 /* Get name of a die, return NULL if not found. */
12728
12729 static char *
12730 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12731 {
12732 struct attribute *attr;
12733
12734 attr = dwarf2_attr (die, DW_AT_name, cu);
12735 if ((!attr || !DW_STRING (attr))
12736 && die->tag != DW_TAG_class_type
12737 && die->tag != DW_TAG_interface_type
12738 && die->tag != DW_TAG_structure_type
12739 && die->tag != DW_TAG_union_type)
12740 return NULL;
12741
12742 switch (die->tag)
12743 {
12744 case DW_TAG_compile_unit:
12745 /* Compilation units have a DW_AT_name that is a filename, not
12746 a source language identifier. */
12747 case DW_TAG_enumeration_type:
12748 case DW_TAG_enumerator:
12749 /* These tags always have simple identifiers already; no need
12750 to canonicalize them. */
12751 return DW_STRING (attr);
12752
12753 case DW_TAG_subprogram:
12754 /* Java constructors will all be named "<init>", so return
12755 the class name when we see this special case. */
12756 if (cu->language == language_java
12757 && DW_STRING (attr) != NULL
12758 && strcmp (DW_STRING (attr), "<init>") == 0)
12759 {
12760 struct dwarf2_cu *spec_cu = cu;
12761 struct die_info *spec_die;
12762
12763 /* GCJ will output '<init>' for Java constructor names.
12764 For this special case, return the name of the parent class. */
12765
12766 /* GCJ may output suprogram DIEs with AT_specification set.
12767 If so, use the name of the specified DIE. */
12768 spec_die = die_specification (die, &spec_cu);
12769 if (spec_die != NULL)
12770 return dwarf2_name (spec_die, spec_cu);
12771
12772 do
12773 {
12774 die = die->parent;
12775 if (die->tag == DW_TAG_class_type)
12776 return dwarf2_name (die, cu);
12777 }
12778 while (die->tag != DW_TAG_compile_unit);
12779 }
12780 break;
12781
12782 case DW_TAG_class_type:
12783 case DW_TAG_interface_type:
12784 case DW_TAG_structure_type:
12785 case DW_TAG_union_type:
12786 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12787 structures or unions. These were of the form "._%d" in GCC 4.1,
12788 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12789 and GCC 4.4. We work around this problem by ignoring these. */
12790 if (attr && DW_STRING (attr)
12791 && (strncmp (DW_STRING (attr), "._", 2) == 0
12792 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12793 return NULL;
12794
12795 /* GCC might emit a nameless typedef that has a linkage name. See
12796 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12797 if (!attr || DW_STRING (attr) == NULL)
12798 {
12799 char *demangled = NULL;
12800
12801 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12802 if (attr == NULL)
12803 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12804
12805 if (attr == NULL || DW_STRING (attr) == NULL)
12806 return NULL;
12807
12808 /* Avoid demangling DW_STRING (attr) the second time on a second
12809 call for the same DIE. */
12810 if (!DW_STRING_IS_CANONICAL (attr))
12811 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12812
12813 if (demangled)
12814 {
12815 char *base;
12816
12817 /* FIXME: we already did this for the partial symbol... */
12818 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12819 &cu->objfile->objfile_obstack);
12820 DW_STRING_IS_CANONICAL (attr) = 1;
12821 xfree (demangled);
12822
12823 /* Strip any leading namespaces/classes, keep only the base name.
12824 DW_AT_name for named DIEs does not contain the prefixes. */
12825 base = strrchr (DW_STRING (attr), ':');
12826 if (base && base > DW_STRING (attr) && base[-1] == ':')
12827 return &base[1];
12828 else
12829 return DW_STRING (attr);
12830 }
12831 }
12832 break;
12833
12834 default:
12835 break;
12836 }
12837
12838 if (!DW_STRING_IS_CANONICAL (attr))
12839 {
12840 DW_STRING (attr)
12841 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12842 &cu->objfile->objfile_obstack);
12843 DW_STRING_IS_CANONICAL (attr) = 1;
12844 }
12845 return DW_STRING (attr);
12846 }
12847
12848 /* Return the die that this die in an extension of, or NULL if there
12849 is none. *EXT_CU is the CU containing DIE on input, and the CU
12850 containing the return value on output. */
12851
12852 static struct die_info *
12853 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12854 {
12855 struct attribute *attr;
12856
12857 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12858 if (attr == NULL)
12859 return NULL;
12860
12861 return follow_die_ref (die, attr, ext_cu);
12862 }
12863
12864 /* Convert a DIE tag into its string name. */
12865
12866 static char *
12867 dwarf_tag_name (unsigned tag)
12868 {
12869 switch (tag)
12870 {
12871 case DW_TAG_padding:
12872 return "DW_TAG_padding";
12873 case DW_TAG_array_type:
12874 return "DW_TAG_array_type";
12875 case DW_TAG_class_type:
12876 return "DW_TAG_class_type";
12877 case DW_TAG_entry_point:
12878 return "DW_TAG_entry_point";
12879 case DW_TAG_enumeration_type:
12880 return "DW_TAG_enumeration_type";
12881 case DW_TAG_formal_parameter:
12882 return "DW_TAG_formal_parameter";
12883 case DW_TAG_imported_declaration:
12884 return "DW_TAG_imported_declaration";
12885 case DW_TAG_label:
12886 return "DW_TAG_label";
12887 case DW_TAG_lexical_block:
12888 return "DW_TAG_lexical_block";
12889 case DW_TAG_member:
12890 return "DW_TAG_member";
12891 case DW_TAG_pointer_type:
12892 return "DW_TAG_pointer_type";
12893 case DW_TAG_reference_type:
12894 return "DW_TAG_reference_type";
12895 case DW_TAG_compile_unit:
12896 return "DW_TAG_compile_unit";
12897 case DW_TAG_string_type:
12898 return "DW_TAG_string_type";
12899 case DW_TAG_structure_type:
12900 return "DW_TAG_structure_type";
12901 case DW_TAG_subroutine_type:
12902 return "DW_TAG_subroutine_type";
12903 case DW_TAG_typedef:
12904 return "DW_TAG_typedef";
12905 case DW_TAG_union_type:
12906 return "DW_TAG_union_type";
12907 case DW_TAG_unspecified_parameters:
12908 return "DW_TAG_unspecified_parameters";
12909 case DW_TAG_variant:
12910 return "DW_TAG_variant";
12911 case DW_TAG_common_block:
12912 return "DW_TAG_common_block";
12913 case DW_TAG_common_inclusion:
12914 return "DW_TAG_common_inclusion";
12915 case DW_TAG_inheritance:
12916 return "DW_TAG_inheritance";
12917 case DW_TAG_inlined_subroutine:
12918 return "DW_TAG_inlined_subroutine";
12919 case DW_TAG_module:
12920 return "DW_TAG_module";
12921 case DW_TAG_ptr_to_member_type:
12922 return "DW_TAG_ptr_to_member_type";
12923 case DW_TAG_set_type:
12924 return "DW_TAG_set_type";
12925 case DW_TAG_subrange_type:
12926 return "DW_TAG_subrange_type";
12927 case DW_TAG_with_stmt:
12928 return "DW_TAG_with_stmt";
12929 case DW_TAG_access_declaration:
12930 return "DW_TAG_access_declaration";
12931 case DW_TAG_base_type:
12932 return "DW_TAG_base_type";
12933 case DW_TAG_catch_block:
12934 return "DW_TAG_catch_block";
12935 case DW_TAG_const_type:
12936 return "DW_TAG_const_type";
12937 case DW_TAG_constant:
12938 return "DW_TAG_constant";
12939 case DW_TAG_enumerator:
12940 return "DW_TAG_enumerator";
12941 case DW_TAG_file_type:
12942 return "DW_TAG_file_type";
12943 case DW_TAG_friend:
12944 return "DW_TAG_friend";
12945 case DW_TAG_namelist:
12946 return "DW_TAG_namelist";
12947 case DW_TAG_namelist_item:
12948 return "DW_TAG_namelist_item";
12949 case DW_TAG_packed_type:
12950 return "DW_TAG_packed_type";
12951 case DW_TAG_subprogram:
12952 return "DW_TAG_subprogram";
12953 case DW_TAG_template_type_param:
12954 return "DW_TAG_template_type_param";
12955 case DW_TAG_template_value_param:
12956 return "DW_TAG_template_value_param";
12957 case DW_TAG_thrown_type:
12958 return "DW_TAG_thrown_type";
12959 case DW_TAG_try_block:
12960 return "DW_TAG_try_block";
12961 case DW_TAG_variant_part:
12962 return "DW_TAG_variant_part";
12963 case DW_TAG_variable:
12964 return "DW_TAG_variable";
12965 case DW_TAG_volatile_type:
12966 return "DW_TAG_volatile_type";
12967 case DW_TAG_dwarf_procedure:
12968 return "DW_TAG_dwarf_procedure";
12969 case DW_TAG_restrict_type:
12970 return "DW_TAG_restrict_type";
12971 case DW_TAG_interface_type:
12972 return "DW_TAG_interface_type";
12973 case DW_TAG_namespace:
12974 return "DW_TAG_namespace";
12975 case DW_TAG_imported_module:
12976 return "DW_TAG_imported_module";
12977 case DW_TAG_unspecified_type:
12978 return "DW_TAG_unspecified_type";
12979 case DW_TAG_partial_unit:
12980 return "DW_TAG_partial_unit";
12981 case DW_TAG_imported_unit:
12982 return "DW_TAG_imported_unit";
12983 case DW_TAG_condition:
12984 return "DW_TAG_condition";
12985 case DW_TAG_shared_type:
12986 return "DW_TAG_shared_type";
12987 case DW_TAG_type_unit:
12988 return "DW_TAG_type_unit";
12989 case DW_TAG_MIPS_loop:
12990 return "DW_TAG_MIPS_loop";
12991 case DW_TAG_HP_array_descriptor:
12992 return "DW_TAG_HP_array_descriptor";
12993 case DW_TAG_format_label:
12994 return "DW_TAG_format_label";
12995 case DW_TAG_function_template:
12996 return "DW_TAG_function_template";
12997 case DW_TAG_class_template:
12998 return "DW_TAG_class_template";
12999 case DW_TAG_GNU_BINCL:
13000 return "DW_TAG_GNU_BINCL";
13001 case DW_TAG_GNU_EINCL:
13002 return "DW_TAG_GNU_EINCL";
13003 case DW_TAG_upc_shared_type:
13004 return "DW_TAG_upc_shared_type";
13005 case DW_TAG_upc_strict_type:
13006 return "DW_TAG_upc_strict_type";
13007 case DW_TAG_upc_relaxed_type:
13008 return "DW_TAG_upc_relaxed_type";
13009 case DW_TAG_PGI_kanji_type:
13010 return "DW_TAG_PGI_kanji_type";
13011 case DW_TAG_PGI_interface_block:
13012 return "DW_TAG_PGI_interface_block";
13013 case DW_TAG_GNU_call_site:
13014 return "DW_TAG_GNU_call_site";
13015 default:
13016 return "DW_TAG_<unknown>";
13017 }
13018 }
13019
13020 /* Convert a DWARF attribute code into its string name. */
13021
13022 static char *
13023 dwarf_attr_name (unsigned attr)
13024 {
13025 switch (attr)
13026 {
13027 case DW_AT_sibling:
13028 return "DW_AT_sibling";
13029 case DW_AT_location:
13030 return "DW_AT_location";
13031 case DW_AT_name:
13032 return "DW_AT_name";
13033 case DW_AT_ordering:
13034 return "DW_AT_ordering";
13035 case DW_AT_subscr_data:
13036 return "DW_AT_subscr_data";
13037 case DW_AT_byte_size:
13038 return "DW_AT_byte_size";
13039 case DW_AT_bit_offset:
13040 return "DW_AT_bit_offset";
13041 case DW_AT_bit_size:
13042 return "DW_AT_bit_size";
13043 case DW_AT_element_list:
13044 return "DW_AT_element_list";
13045 case DW_AT_stmt_list:
13046 return "DW_AT_stmt_list";
13047 case DW_AT_low_pc:
13048 return "DW_AT_low_pc";
13049 case DW_AT_high_pc:
13050 return "DW_AT_high_pc";
13051 case DW_AT_language:
13052 return "DW_AT_language";
13053 case DW_AT_member:
13054 return "DW_AT_member";
13055 case DW_AT_discr:
13056 return "DW_AT_discr";
13057 case DW_AT_discr_value:
13058 return "DW_AT_discr_value";
13059 case DW_AT_visibility:
13060 return "DW_AT_visibility";
13061 case DW_AT_import:
13062 return "DW_AT_import";
13063 case DW_AT_string_length:
13064 return "DW_AT_string_length";
13065 case DW_AT_common_reference:
13066 return "DW_AT_common_reference";
13067 case DW_AT_comp_dir:
13068 return "DW_AT_comp_dir";
13069 case DW_AT_const_value:
13070 return "DW_AT_const_value";
13071 case DW_AT_containing_type:
13072 return "DW_AT_containing_type";
13073 case DW_AT_default_value:
13074 return "DW_AT_default_value";
13075 case DW_AT_inline:
13076 return "DW_AT_inline";
13077 case DW_AT_is_optional:
13078 return "DW_AT_is_optional";
13079 case DW_AT_lower_bound:
13080 return "DW_AT_lower_bound";
13081 case DW_AT_producer:
13082 return "DW_AT_producer";
13083 case DW_AT_prototyped:
13084 return "DW_AT_prototyped";
13085 case DW_AT_return_addr:
13086 return "DW_AT_return_addr";
13087 case DW_AT_start_scope:
13088 return "DW_AT_start_scope";
13089 case DW_AT_bit_stride:
13090 return "DW_AT_bit_stride";
13091 case DW_AT_upper_bound:
13092 return "DW_AT_upper_bound";
13093 case DW_AT_abstract_origin:
13094 return "DW_AT_abstract_origin";
13095 case DW_AT_accessibility:
13096 return "DW_AT_accessibility";
13097 case DW_AT_address_class:
13098 return "DW_AT_address_class";
13099 case DW_AT_artificial:
13100 return "DW_AT_artificial";
13101 case DW_AT_base_types:
13102 return "DW_AT_base_types";
13103 case DW_AT_calling_convention:
13104 return "DW_AT_calling_convention";
13105 case DW_AT_count:
13106 return "DW_AT_count";
13107 case DW_AT_data_member_location:
13108 return "DW_AT_data_member_location";
13109 case DW_AT_decl_column:
13110 return "DW_AT_decl_column";
13111 case DW_AT_decl_file:
13112 return "DW_AT_decl_file";
13113 case DW_AT_decl_line:
13114 return "DW_AT_decl_line";
13115 case DW_AT_declaration:
13116 return "DW_AT_declaration";
13117 case DW_AT_discr_list:
13118 return "DW_AT_discr_list";
13119 case DW_AT_encoding:
13120 return "DW_AT_encoding";
13121 case DW_AT_external:
13122 return "DW_AT_external";
13123 case DW_AT_frame_base:
13124 return "DW_AT_frame_base";
13125 case DW_AT_friend:
13126 return "DW_AT_friend";
13127 case DW_AT_identifier_case:
13128 return "DW_AT_identifier_case";
13129 case DW_AT_macro_info:
13130 return "DW_AT_macro_info";
13131 case DW_AT_namelist_items:
13132 return "DW_AT_namelist_items";
13133 case DW_AT_priority:
13134 return "DW_AT_priority";
13135 case DW_AT_segment:
13136 return "DW_AT_segment";
13137 case DW_AT_specification:
13138 return "DW_AT_specification";
13139 case DW_AT_static_link:
13140 return "DW_AT_static_link";
13141 case DW_AT_type:
13142 return "DW_AT_type";
13143 case DW_AT_use_location:
13144 return "DW_AT_use_location";
13145 case DW_AT_variable_parameter:
13146 return "DW_AT_variable_parameter";
13147 case DW_AT_virtuality:
13148 return "DW_AT_virtuality";
13149 case DW_AT_vtable_elem_location:
13150 return "DW_AT_vtable_elem_location";
13151 /* DWARF 3 values. */
13152 case DW_AT_allocated:
13153 return "DW_AT_allocated";
13154 case DW_AT_associated:
13155 return "DW_AT_associated";
13156 case DW_AT_data_location:
13157 return "DW_AT_data_location";
13158 case DW_AT_byte_stride:
13159 return "DW_AT_byte_stride";
13160 case DW_AT_entry_pc:
13161 return "DW_AT_entry_pc";
13162 case DW_AT_use_UTF8:
13163 return "DW_AT_use_UTF8";
13164 case DW_AT_extension:
13165 return "DW_AT_extension";
13166 case DW_AT_ranges:
13167 return "DW_AT_ranges";
13168 case DW_AT_trampoline:
13169 return "DW_AT_trampoline";
13170 case DW_AT_call_column:
13171 return "DW_AT_call_column";
13172 case DW_AT_call_file:
13173 return "DW_AT_call_file";
13174 case DW_AT_call_line:
13175 return "DW_AT_call_line";
13176 case DW_AT_description:
13177 return "DW_AT_description";
13178 case DW_AT_binary_scale:
13179 return "DW_AT_binary_scale";
13180 case DW_AT_decimal_scale:
13181 return "DW_AT_decimal_scale";
13182 case DW_AT_small:
13183 return "DW_AT_small";
13184 case DW_AT_decimal_sign:
13185 return "DW_AT_decimal_sign";
13186 case DW_AT_digit_count:
13187 return "DW_AT_digit_count";
13188 case DW_AT_picture_string:
13189 return "DW_AT_picture_string";
13190 case DW_AT_mutable:
13191 return "DW_AT_mutable";
13192 case DW_AT_threads_scaled:
13193 return "DW_AT_threads_scaled";
13194 case DW_AT_explicit:
13195 return "DW_AT_explicit";
13196 case DW_AT_object_pointer:
13197 return "DW_AT_object_pointer";
13198 case DW_AT_endianity:
13199 return "DW_AT_endianity";
13200 case DW_AT_elemental:
13201 return "DW_AT_elemental";
13202 case DW_AT_pure:
13203 return "DW_AT_pure";
13204 case DW_AT_recursive:
13205 return "DW_AT_recursive";
13206 /* DWARF 4 values. */
13207 case DW_AT_signature:
13208 return "DW_AT_signature";
13209 case DW_AT_linkage_name:
13210 return "DW_AT_linkage_name";
13211 /* SGI/MIPS extensions. */
13212 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13213 case DW_AT_MIPS_fde:
13214 return "DW_AT_MIPS_fde";
13215 #endif
13216 case DW_AT_MIPS_loop_begin:
13217 return "DW_AT_MIPS_loop_begin";
13218 case DW_AT_MIPS_tail_loop_begin:
13219 return "DW_AT_MIPS_tail_loop_begin";
13220 case DW_AT_MIPS_epilog_begin:
13221 return "DW_AT_MIPS_epilog_begin";
13222 case DW_AT_MIPS_loop_unroll_factor:
13223 return "DW_AT_MIPS_loop_unroll_factor";
13224 case DW_AT_MIPS_software_pipeline_depth:
13225 return "DW_AT_MIPS_software_pipeline_depth";
13226 case DW_AT_MIPS_linkage_name:
13227 return "DW_AT_MIPS_linkage_name";
13228 case DW_AT_MIPS_stride:
13229 return "DW_AT_MIPS_stride";
13230 case DW_AT_MIPS_abstract_name:
13231 return "DW_AT_MIPS_abstract_name";
13232 case DW_AT_MIPS_clone_origin:
13233 return "DW_AT_MIPS_clone_origin";
13234 case DW_AT_MIPS_has_inlines:
13235 return "DW_AT_MIPS_has_inlines";
13236 /* HP extensions. */
13237 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13238 case DW_AT_HP_block_index:
13239 return "DW_AT_HP_block_index";
13240 #endif
13241 case DW_AT_HP_unmodifiable:
13242 return "DW_AT_HP_unmodifiable";
13243 case DW_AT_HP_actuals_stmt_list:
13244 return "DW_AT_HP_actuals_stmt_list";
13245 case DW_AT_HP_proc_per_section:
13246 return "DW_AT_HP_proc_per_section";
13247 case DW_AT_HP_raw_data_ptr:
13248 return "DW_AT_HP_raw_data_ptr";
13249 case DW_AT_HP_pass_by_reference:
13250 return "DW_AT_HP_pass_by_reference";
13251 case DW_AT_HP_opt_level:
13252 return "DW_AT_HP_opt_level";
13253 case DW_AT_HP_prof_version_id:
13254 return "DW_AT_HP_prof_version_id";
13255 case DW_AT_HP_opt_flags:
13256 return "DW_AT_HP_opt_flags";
13257 case DW_AT_HP_cold_region_low_pc:
13258 return "DW_AT_HP_cold_region_low_pc";
13259 case DW_AT_HP_cold_region_high_pc:
13260 return "DW_AT_HP_cold_region_high_pc";
13261 case DW_AT_HP_all_variables_modifiable:
13262 return "DW_AT_HP_all_variables_modifiable";
13263 case DW_AT_HP_linkage_name:
13264 return "DW_AT_HP_linkage_name";
13265 case DW_AT_HP_prof_flags:
13266 return "DW_AT_HP_prof_flags";
13267 /* GNU extensions. */
13268 case DW_AT_sf_names:
13269 return "DW_AT_sf_names";
13270 case DW_AT_src_info:
13271 return "DW_AT_src_info";
13272 case DW_AT_mac_info:
13273 return "DW_AT_mac_info";
13274 case DW_AT_src_coords:
13275 return "DW_AT_src_coords";
13276 case DW_AT_body_begin:
13277 return "DW_AT_body_begin";
13278 case DW_AT_body_end:
13279 return "DW_AT_body_end";
13280 case DW_AT_GNU_vector:
13281 return "DW_AT_GNU_vector";
13282 case DW_AT_GNU_odr_signature:
13283 return "DW_AT_GNU_odr_signature";
13284 /* VMS extensions. */
13285 case DW_AT_VMS_rtnbeg_pd_address:
13286 return "DW_AT_VMS_rtnbeg_pd_address";
13287 /* UPC extension. */
13288 case DW_AT_upc_threads_scaled:
13289 return "DW_AT_upc_threads_scaled";
13290 /* PGI (STMicroelectronics) extensions. */
13291 case DW_AT_PGI_lbase:
13292 return "DW_AT_PGI_lbase";
13293 case DW_AT_PGI_soffset:
13294 return "DW_AT_PGI_soffset";
13295 case DW_AT_PGI_lstride:
13296 return "DW_AT_PGI_lstride";
13297 default:
13298 return "DW_AT_<unknown>";
13299 }
13300 }
13301
13302 /* Convert a DWARF value form code into its string name. */
13303
13304 static char *
13305 dwarf_form_name (unsigned form)
13306 {
13307 switch (form)
13308 {
13309 case DW_FORM_addr:
13310 return "DW_FORM_addr";
13311 case DW_FORM_block2:
13312 return "DW_FORM_block2";
13313 case DW_FORM_block4:
13314 return "DW_FORM_block4";
13315 case DW_FORM_data2:
13316 return "DW_FORM_data2";
13317 case DW_FORM_data4:
13318 return "DW_FORM_data4";
13319 case DW_FORM_data8:
13320 return "DW_FORM_data8";
13321 case DW_FORM_string:
13322 return "DW_FORM_string";
13323 case DW_FORM_block:
13324 return "DW_FORM_block";
13325 case DW_FORM_block1:
13326 return "DW_FORM_block1";
13327 case DW_FORM_data1:
13328 return "DW_FORM_data1";
13329 case DW_FORM_flag:
13330 return "DW_FORM_flag";
13331 case DW_FORM_sdata:
13332 return "DW_FORM_sdata";
13333 case DW_FORM_strp:
13334 return "DW_FORM_strp";
13335 case DW_FORM_udata:
13336 return "DW_FORM_udata";
13337 case DW_FORM_ref_addr:
13338 return "DW_FORM_ref_addr";
13339 case DW_FORM_ref1:
13340 return "DW_FORM_ref1";
13341 case DW_FORM_ref2:
13342 return "DW_FORM_ref2";
13343 case DW_FORM_ref4:
13344 return "DW_FORM_ref4";
13345 case DW_FORM_ref8:
13346 return "DW_FORM_ref8";
13347 case DW_FORM_ref_udata:
13348 return "DW_FORM_ref_udata";
13349 case DW_FORM_indirect:
13350 return "DW_FORM_indirect";
13351 case DW_FORM_sec_offset:
13352 return "DW_FORM_sec_offset";
13353 case DW_FORM_exprloc:
13354 return "DW_FORM_exprloc";
13355 case DW_FORM_flag_present:
13356 return "DW_FORM_flag_present";
13357 case DW_FORM_ref_sig8:
13358 return "DW_FORM_ref_sig8";
13359 default:
13360 return "DW_FORM_<unknown>";
13361 }
13362 }
13363
13364 /* Convert a DWARF stack opcode into its string name. */
13365
13366 const char *
13367 dwarf_stack_op_name (unsigned op)
13368 {
13369 switch (op)
13370 {
13371 case DW_OP_addr:
13372 return "DW_OP_addr";
13373 case DW_OP_deref:
13374 return "DW_OP_deref";
13375 case DW_OP_const1u:
13376 return "DW_OP_const1u";
13377 case DW_OP_const1s:
13378 return "DW_OP_const1s";
13379 case DW_OP_const2u:
13380 return "DW_OP_const2u";
13381 case DW_OP_const2s:
13382 return "DW_OP_const2s";
13383 case DW_OP_const4u:
13384 return "DW_OP_const4u";
13385 case DW_OP_const4s:
13386 return "DW_OP_const4s";
13387 case DW_OP_const8u:
13388 return "DW_OP_const8u";
13389 case DW_OP_const8s:
13390 return "DW_OP_const8s";
13391 case DW_OP_constu:
13392 return "DW_OP_constu";
13393 case DW_OP_consts:
13394 return "DW_OP_consts";
13395 case DW_OP_dup:
13396 return "DW_OP_dup";
13397 case DW_OP_drop:
13398 return "DW_OP_drop";
13399 case DW_OP_over:
13400 return "DW_OP_over";
13401 case DW_OP_pick:
13402 return "DW_OP_pick";
13403 case DW_OP_swap:
13404 return "DW_OP_swap";
13405 case DW_OP_rot:
13406 return "DW_OP_rot";
13407 case DW_OP_xderef:
13408 return "DW_OP_xderef";
13409 case DW_OP_abs:
13410 return "DW_OP_abs";
13411 case DW_OP_and:
13412 return "DW_OP_and";
13413 case DW_OP_div:
13414 return "DW_OP_div";
13415 case DW_OP_minus:
13416 return "DW_OP_minus";
13417 case DW_OP_mod:
13418 return "DW_OP_mod";
13419 case DW_OP_mul:
13420 return "DW_OP_mul";
13421 case DW_OP_neg:
13422 return "DW_OP_neg";
13423 case DW_OP_not:
13424 return "DW_OP_not";
13425 case DW_OP_or:
13426 return "DW_OP_or";
13427 case DW_OP_plus:
13428 return "DW_OP_plus";
13429 case DW_OP_plus_uconst:
13430 return "DW_OP_plus_uconst";
13431 case DW_OP_shl:
13432 return "DW_OP_shl";
13433 case DW_OP_shr:
13434 return "DW_OP_shr";
13435 case DW_OP_shra:
13436 return "DW_OP_shra";
13437 case DW_OP_xor:
13438 return "DW_OP_xor";
13439 case DW_OP_bra:
13440 return "DW_OP_bra";
13441 case DW_OP_eq:
13442 return "DW_OP_eq";
13443 case DW_OP_ge:
13444 return "DW_OP_ge";
13445 case DW_OP_gt:
13446 return "DW_OP_gt";
13447 case DW_OP_le:
13448 return "DW_OP_le";
13449 case DW_OP_lt:
13450 return "DW_OP_lt";
13451 case DW_OP_ne:
13452 return "DW_OP_ne";
13453 case DW_OP_skip:
13454 return "DW_OP_skip";
13455 case DW_OP_lit0:
13456 return "DW_OP_lit0";
13457 case DW_OP_lit1:
13458 return "DW_OP_lit1";
13459 case DW_OP_lit2:
13460 return "DW_OP_lit2";
13461 case DW_OP_lit3:
13462 return "DW_OP_lit3";
13463 case DW_OP_lit4:
13464 return "DW_OP_lit4";
13465 case DW_OP_lit5:
13466 return "DW_OP_lit5";
13467 case DW_OP_lit6:
13468 return "DW_OP_lit6";
13469 case DW_OP_lit7:
13470 return "DW_OP_lit7";
13471 case DW_OP_lit8:
13472 return "DW_OP_lit8";
13473 case DW_OP_lit9:
13474 return "DW_OP_lit9";
13475 case DW_OP_lit10:
13476 return "DW_OP_lit10";
13477 case DW_OP_lit11:
13478 return "DW_OP_lit11";
13479 case DW_OP_lit12:
13480 return "DW_OP_lit12";
13481 case DW_OP_lit13:
13482 return "DW_OP_lit13";
13483 case DW_OP_lit14:
13484 return "DW_OP_lit14";
13485 case DW_OP_lit15:
13486 return "DW_OP_lit15";
13487 case DW_OP_lit16:
13488 return "DW_OP_lit16";
13489 case DW_OP_lit17:
13490 return "DW_OP_lit17";
13491 case DW_OP_lit18:
13492 return "DW_OP_lit18";
13493 case DW_OP_lit19:
13494 return "DW_OP_lit19";
13495 case DW_OP_lit20:
13496 return "DW_OP_lit20";
13497 case DW_OP_lit21:
13498 return "DW_OP_lit21";
13499 case DW_OP_lit22:
13500 return "DW_OP_lit22";
13501 case DW_OP_lit23:
13502 return "DW_OP_lit23";
13503 case DW_OP_lit24:
13504 return "DW_OP_lit24";
13505 case DW_OP_lit25:
13506 return "DW_OP_lit25";
13507 case DW_OP_lit26:
13508 return "DW_OP_lit26";
13509 case DW_OP_lit27:
13510 return "DW_OP_lit27";
13511 case DW_OP_lit28:
13512 return "DW_OP_lit28";
13513 case DW_OP_lit29:
13514 return "DW_OP_lit29";
13515 case DW_OP_lit30:
13516 return "DW_OP_lit30";
13517 case DW_OP_lit31:
13518 return "DW_OP_lit31";
13519 case DW_OP_reg0:
13520 return "DW_OP_reg0";
13521 case DW_OP_reg1:
13522 return "DW_OP_reg1";
13523 case DW_OP_reg2:
13524 return "DW_OP_reg2";
13525 case DW_OP_reg3:
13526 return "DW_OP_reg3";
13527 case DW_OP_reg4:
13528 return "DW_OP_reg4";
13529 case DW_OP_reg5:
13530 return "DW_OP_reg5";
13531 case DW_OP_reg6:
13532 return "DW_OP_reg6";
13533 case DW_OP_reg7:
13534 return "DW_OP_reg7";
13535 case DW_OP_reg8:
13536 return "DW_OP_reg8";
13537 case DW_OP_reg9:
13538 return "DW_OP_reg9";
13539 case DW_OP_reg10:
13540 return "DW_OP_reg10";
13541 case DW_OP_reg11:
13542 return "DW_OP_reg11";
13543 case DW_OP_reg12:
13544 return "DW_OP_reg12";
13545 case DW_OP_reg13:
13546 return "DW_OP_reg13";
13547 case DW_OP_reg14:
13548 return "DW_OP_reg14";
13549 case DW_OP_reg15:
13550 return "DW_OP_reg15";
13551 case DW_OP_reg16:
13552 return "DW_OP_reg16";
13553 case DW_OP_reg17:
13554 return "DW_OP_reg17";
13555 case DW_OP_reg18:
13556 return "DW_OP_reg18";
13557 case DW_OP_reg19:
13558 return "DW_OP_reg19";
13559 case DW_OP_reg20:
13560 return "DW_OP_reg20";
13561 case DW_OP_reg21:
13562 return "DW_OP_reg21";
13563 case DW_OP_reg22:
13564 return "DW_OP_reg22";
13565 case DW_OP_reg23:
13566 return "DW_OP_reg23";
13567 case DW_OP_reg24:
13568 return "DW_OP_reg24";
13569 case DW_OP_reg25:
13570 return "DW_OP_reg25";
13571 case DW_OP_reg26:
13572 return "DW_OP_reg26";
13573 case DW_OP_reg27:
13574 return "DW_OP_reg27";
13575 case DW_OP_reg28:
13576 return "DW_OP_reg28";
13577 case DW_OP_reg29:
13578 return "DW_OP_reg29";
13579 case DW_OP_reg30:
13580 return "DW_OP_reg30";
13581 case DW_OP_reg31:
13582 return "DW_OP_reg31";
13583 case DW_OP_breg0:
13584 return "DW_OP_breg0";
13585 case DW_OP_breg1:
13586 return "DW_OP_breg1";
13587 case DW_OP_breg2:
13588 return "DW_OP_breg2";
13589 case DW_OP_breg3:
13590 return "DW_OP_breg3";
13591 case DW_OP_breg4:
13592 return "DW_OP_breg4";
13593 case DW_OP_breg5:
13594 return "DW_OP_breg5";
13595 case DW_OP_breg6:
13596 return "DW_OP_breg6";
13597 case DW_OP_breg7:
13598 return "DW_OP_breg7";
13599 case DW_OP_breg8:
13600 return "DW_OP_breg8";
13601 case DW_OP_breg9:
13602 return "DW_OP_breg9";
13603 case DW_OP_breg10:
13604 return "DW_OP_breg10";
13605 case DW_OP_breg11:
13606 return "DW_OP_breg11";
13607 case DW_OP_breg12:
13608 return "DW_OP_breg12";
13609 case DW_OP_breg13:
13610 return "DW_OP_breg13";
13611 case DW_OP_breg14:
13612 return "DW_OP_breg14";
13613 case DW_OP_breg15:
13614 return "DW_OP_breg15";
13615 case DW_OP_breg16:
13616 return "DW_OP_breg16";
13617 case DW_OP_breg17:
13618 return "DW_OP_breg17";
13619 case DW_OP_breg18:
13620 return "DW_OP_breg18";
13621 case DW_OP_breg19:
13622 return "DW_OP_breg19";
13623 case DW_OP_breg20:
13624 return "DW_OP_breg20";
13625 case DW_OP_breg21:
13626 return "DW_OP_breg21";
13627 case DW_OP_breg22:
13628 return "DW_OP_breg22";
13629 case DW_OP_breg23:
13630 return "DW_OP_breg23";
13631 case DW_OP_breg24:
13632 return "DW_OP_breg24";
13633 case DW_OP_breg25:
13634 return "DW_OP_breg25";
13635 case DW_OP_breg26:
13636 return "DW_OP_breg26";
13637 case DW_OP_breg27:
13638 return "DW_OP_breg27";
13639 case DW_OP_breg28:
13640 return "DW_OP_breg28";
13641 case DW_OP_breg29:
13642 return "DW_OP_breg29";
13643 case DW_OP_breg30:
13644 return "DW_OP_breg30";
13645 case DW_OP_breg31:
13646 return "DW_OP_breg31";
13647 case DW_OP_regx:
13648 return "DW_OP_regx";
13649 case DW_OP_fbreg:
13650 return "DW_OP_fbreg";
13651 case DW_OP_bregx:
13652 return "DW_OP_bregx";
13653 case DW_OP_piece:
13654 return "DW_OP_piece";
13655 case DW_OP_deref_size:
13656 return "DW_OP_deref_size";
13657 case DW_OP_xderef_size:
13658 return "DW_OP_xderef_size";
13659 case DW_OP_nop:
13660 return "DW_OP_nop";
13661 /* DWARF 3 extensions. */
13662 case DW_OP_push_object_address:
13663 return "DW_OP_push_object_address";
13664 case DW_OP_call2:
13665 return "DW_OP_call2";
13666 case DW_OP_call4:
13667 return "DW_OP_call4";
13668 case DW_OP_call_ref:
13669 return "DW_OP_call_ref";
13670 case DW_OP_form_tls_address:
13671 return "DW_OP_form_tls_address";
13672 case DW_OP_call_frame_cfa:
13673 return "DW_OP_call_frame_cfa";
13674 case DW_OP_bit_piece:
13675 return "DW_OP_bit_piece";
13676 /* DWARF 4 extensions. */
13677 case DW_OP_implicit_value:
13678 return "DW_OP_implicit_value";
13679 case DW_OP_stack_value:
13680 return "DW_OP_stack_value";
13681 /* GNU extensions. */
13682 case DW_OP_GNU_push_tls_address:
13683 return "DW_OP_GNU_push_tls_address";
13684 case DW_OP_GNU_uninit:
13685 return "DW_OP_GNU_uninit";
13686 case DW_OP_GNU_implicit_pointer:
13687 return "DW_OP_GNU_implicit_pointer";
13688 case DW_OP_GNU_entry_value:
13689 return "DW_OP_GNU_entry_value";
13690 case DW_OP_GNU_const_type:
13691 return "DW_OP_GNU_const_type";
13692 case DW_OP_GNU_regval_type:
13693 return "DW_OP_GNU_regval_type";
13694 case DW_OP_GNU_deref_type:
13695 return "DW_OP_GNU_deref_type";
13696 case DW_OP_GNU_convert:
13697 return "DW_OP_GNU_convert";
13698 case DW_OP_GNU_reinterpret:
13699 return "DW_OP_GNU_reinterpret";
13700 default:
13701 return NULL;
13702 }
13703 }
13704
13705 static char *
13706 dwarf_bool_name (unsigned mybool)
13707 {
13708 if (mybool)
13709 return "TRUE";
13710 else
13711 return "FALSE";
13712 }
13713
13714 /* Convert a DWARF type code into its string name. */
13715
13716 static char *
13717 dwarf_type_encoding_name (unsigned enc)
13718 {
13719 switch (enc)
13720 {
13721 case DW_ATE_void:
13722 return "DW_ATE_void";
13723 case DW_ATE_address:
13724 return "DW_ATE_address";
13725 case DW_ATE_boolean:
13726 return "DW_ATE_boolean";
13727 case DW_ATE_complex_float:
13728 return "DW_ATE_complex_float";
13729 case DW_ATE_float:
13730 return "DW_ATE_float";
13731 case DW_ATE_signed:
13732 return "DW_ATE_signed";
13733 case DW_ATE_signed_char:
13734 return "DW_ATE_signed_char";
13735 case DW_ATE_unsigned:
13736 return "DW_ATE_unsigned";
13737 case DW_ATE_unsigned_char:
13738 return "DW_ATE_unsigned_char";
13739 /* DWARF 3. */
13740 case DW_ATE_imaginary_float:
13741 return "DW_ATE_imaginary_float";
13742 case DW_ATE_packed_decimal:
13743 return "DW_ATE_packed_decimal";
13744 case DW_ATE_numeric_string:
13745 return "DW_ATE_numeric_string";
13746 case DW_ATE_edited:
13747 return "DW_ATE_edited";
13748 case DW_ATE_signed_fixed:
13749 return "DW_ATE_signed_fixed";
13750 case DW_ATE_unsigned_fixed:
13751 return "DW_ATE_unsigned_fixed";
13752 case DW_ATE_decimal_float:
13753 return "DW_ATE_decimal_float";
13754 /* DWARF 4. */
13755 case DW_ATE_UTF:
13756 return "DW_ATE_UTF";
13757 /* HP extensions. */
13758 case DW_ATE_HP_float80:
13759 return "DW_ATE_HP_float80";
13760 case DW_ATE_HP_complex_float80:
13761 return "DW_ATE_HP_complex_float80";
13762 case DW_ATE_HP_float128:
13763 return "DW_ATE_HP_float128";
13764 case DW_ATE_HP_complex_float128:
13765 return "DW_ATE_HP_complex_float128";
13766 case DW_ATE_HP_floathpintel:
13767 return "DW_ATE_HP_floathpintel";
13768 case DW_ATE_HP_imaginary_float80:
13769 return "DW_ATE_HP_imaginary_float80";
13770 case DW_ATE_HP_imaginary_float128:
13771 return "DW_ATE_HP_imaginary_float128";
13772 default:
13773 return "DW_ATE_<unknown>";
13774 }
13775 }
13776
13777 /* Convert a DWARF call frame info operation to its string name. */
13778
13779 #if 0
13780 static char *
13781 dwarf_cfi_name (unsigned cfi_opc)
13782 {
13783 switch (cfi_opc)
13784 {
13785 case DW_CFA_advance_loc:
13786 return "DW_CFA_advance_loc";
13787 case DW_CFA_offset:
13788 return "DW_CFA_offset";
13789 case DW_CFA_restore:
13790 return "DW_CFA_restore";
13791 case DW_CFA_nop:
13792 return "DW_CFA_nop";
13793 case DW_CFA_set_loc:
13794 return "DW_CFA_set_loc";
13795 case DW_CFA_advance_loc1:
13796 return "DW_CFA_advance_loc1";
13797 case DW_CFA_advance_loc2:
13798 return "DW_CFA_advance_loc2";
13799 case DW_CFA_advance_loc4:
13800 return "DW_CFA_advance_loc4";
13801 case DW_CFA_offset_extended:
13802 return "DW_CFA_offset_extended";
13803 case DW_CFA_restore_extended:
13804 return "DW_CFA_restore_extended";
13805 case DW_CFA_undefined:
13806 return "DW_CFA_undefined";
13807 case DW_CFA_same_value:
13808 return "DW_CFA_same_value";
13809 case DW_CFA_register:
13810 return "DW_CFA_register";
13811 case DW_CFA_remember_state:
13812 return "DW_CFA_remember_state";
13813 case DW_CFA_restore_state:
13814 return "DW_CFA_restore_state";
13815 case DW_CFA_def_cfa:
13816 return "DW_CFA_def_cfa";
13817 case DW_CFA_def_cfa_register:
13818 return "DW_CFA_def_cfa_register";
13819 case DW_CFA_def_cfa_offset:
13820 return "DW_CFA_def_cfa_offset";
13821 /* DWARF 3. */
13822 case DW_CFA_def_cfa_expression:
13823 return "DW_CFA_def_cfa_expression";
13824 case DW_CFA_expression:
13825 return "DW_CFA_expression";
13826 case DW_CFA_offset_extended_sf:
13827 return "DW_CFA_offset_extended_sf";
13828 case DW_CFA_def_cfa_sf:
13829 return "DW_CFA_def_cfa_sf";
13830 case DW_CFA_def_cfa_offset_sf:
13831 return "DW_CFA_def_cfa_offset_sf";
13832 case DW_CFA_val_offset:
13833 return "DW_CFA_val_offset";
13834 case DW_CFA_val_offset_sf:
13835 return "DW_CFA_val_offset_sf";
13836 case DW_CFA_val_expression:
13837 return "DW_CFA_val_expression";
13838 /* SGI/MIPS specific. */
13839 case DW_CFA_MIPS_advance_loc8:
13840 return "DW_CFA_MIPS_advance_loc8";
13841 /* GNU extensions. */
13842 case DW_CFA_GNU_window_save:
13843 return "DW_CFA_GNU_window_save";
13844 case DW_CFA_GNU_args_size:
13845 return "DW_CFA_GNU_args_size";
13846 case DW_CFA_GNU_negative_offset_extended:
13847 return "DW_CFA_GNU_negative_offset_extended";
13848 default:
13849 return "DW_CFA_<unknown>";
13850 }
13851 }
13852 #endif
13853
13854 static void
13855 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13856 {
13857 unsigned int i;
13858
13859 print_spaces (indent, f);
13860 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13861 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13862
13863 if (die->parent != NULL)
13864 {
13865 print_spaces (indent, f);
13866 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13867 die->parent->offset);
13868 }
13869
13870 print_spaces (indent, f);
13871 fprintf_unfiltered (f, " has children: %s\n",
13872 dwarf_bool_name (die->child != NULL));
13873
13874 print_spaces (indent, f);
13875 fprintf_unfiltered (f, " attributes:\n");
13876
13877 for (i = 0; i < die->num_attrs; ++i)
13878 {
13879 print_spaces (indent, f);
13880 fprintf_unfiltered (f, " %s (%s) ",
13881 dwarf_attr_name (die->attrs[i].name),
13882 dwarf_form_name (die->attrs[i].form));
13883
13884 switch (die->attrs[i].form)
13885 {
13886 case DW_FORM_ref_addr:
13887 case DW_FORM_addr:
13888 fprintf_unfiltered (f, "address: ");
13889 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13890 break;
13891 case DW_FORM_block2:
13892 case DW_FORM_block4:
13893 case DW_FORM_block:
13894 case DW_FORM_block1:
13895 fprintf_unfiltered (f, "block: size %d",
13896 DW_BLOCK (&die->attrs[i])->size);
13897 break;
13898 case DW_FORM_exprloc:
13899 fprintf_unfiltered (f, "expression: size %u",
13900 DW_BLOCK (&die->attrs[i])->size);
13901 break;
13902 case DW_FORM_ref1:
13903 case DW_FORM_ref2:
13904 case DW_FORM_ref4:
13905 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13906 (long) (DW_ADDR (&die->attrs[i])));
13907 break;
13908 case DW_FORM_data1:
13909 case DW_FORM_data2:
13910 case DW_FORM_data4:
13911 case DW_FORM_data8:
13912 case DW_FORM_udata:
13913 case DW_FORM_sdata:
13914 fprintf_unfiltered (f, "constant: %s",
13915 pulongest (DW_UNSND (&die->attrs[i])));
13916 break;
13917 case DW_FORM_sec_offset:
13918 fprintf_unfiltered (f, "section offset: %s",
13919 pulongest (DW_UNSND (&die->attrs[i])));
13920 break;
13921 case DW_FORM_ref_sig8:
13922 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13923 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13924 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13925 else
13926 fprintf_unfiltered (f, "signatured type, offset: unknown");
13927 break;
13928 case DW_FORM_string:
13929 case DW_FORM_strp:
13930 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13931 DW_STRING (&die->attrs[i])
13932 ? DW_STRING (&die->attrs[i]) : "",
13933 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13934 break;
13935 case DW_FORM_flag:
13936 if (DW_UNSND (&die->attrs[i]))
13937 fprintf_unfiltered (f, "flag: TRUE");
13938 else
13939 fprintf_unfiltered (f, "flag: FALSE");
13940 break;
13941 case DW_FORM_flag_present:
13942 fprintf_unfiltered (f, "flag: TRUE");
13943 break;
13944 case DW_FORM_indirect:
13945 /* The reader will have reduced the indirect form to
13946 the "base form" so this form should not occur. */
13947 fprintf_unfiltered (f,
13948 "unexpected attribute form: DW_FORM_indirect");
13949 break;
13950 default:
13951 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13952 die->attrs[i].form);
13953 break;
13954 }
13955 fprintf_unfiltered (f, "\n");
13956 }
13957 }
13958
13959 static void
13960 dump_die_for_error (struct die_info *die)
13961 {
13962 dump_die_shallow (gdb_stderr, 0, die);
13963 }
13964
13965 static void
13966 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13967 {
13968 int indent = level * 4;
13969
13970 gdb_assert (die != NULL);
13971
13972 if (level >= max_level)
13973 return;
13974
13975 dump_die_shallow (f, indent, die);
13976
13977 if (die->child != NULL)
13978 {
13979 print_spaces (indent, f);
13980 fprintf_unfiltered (f, " Children:");
13981 if (level + 1 < max_level)
13982 {
13983 fprintf_unfiltered (f, "\n");
13984 dump_die_1 (f, level + 1, max_level, die->child);
13985 }
13986 else
13987 {
13988 fprintf_unfiltered (f,
13989 " [not printed, max nesting level reached]\n");
13990 }
13991 }
13992
13993 if (die->sibling != NULL && level > 0)
13994 {
13995 dump_die_1 (f, level, max_level, die->sibling);
13996 }
13997 }
13998
13999 /* This is called from the pdie macro in gdbinit.in.
14000 It's not static so gcc will keep a copy callable from gdb. */
14001
14002 void
14003 dump_die (struct die_info *die, int max_level)
14004 {
14005 dump_die_1 (gdb_stdlog, 0, max_level, die);
14006 }
14007
14008 static void
14009 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14010 {
14011 void **slot;
14012
14013 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14014
14015 *slot = die;
14016 }
14017
14018 static int
14019 is_ref_attr (struct attribute *attr)
14020 {
14021 switch (attr->form)
14022 {
14023 case DW_FORM_ref_addr:
14024 case DW_FORM_ref1:
14025 case DW_FORM_ref2:
14026 case DW_FORM_ref4:
14027 case DW_FORM_ref8:
14028 case DW_FORM_ref_udata:
14029 return 1;
14030 default:
14031 return 0;
14032 }
14033 }
14034
14035 static unsigned int
14036 dwarf2_get_ref_die_offset (struct attribute *attr)
14037 {
14038 if (is_ref_attr (attr))
14039 return DW_ADDR (attr);
14040
14041 complaint (&symfile_complaints,
14042 _("unsupported die ref attribute form: '%s'"),
14043 dwarf_form_name (attr->form));
14044 return 0;
14045 }
14046
14047 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14048 * the value held by the attribute is not constant. */
14049
14050 static LONGEST
14051 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14052 {
14053 if (attr->form == DW_FORM_sdata)
14054 return DW_SND (attr);
14055 else if (attr->form == DW_FORM_udata
14056 || attr->form == DW_FORM_data1
14057 || attr->form == DW_FORM_data2
14058 || attr->form == DW_FORM_data4
14059 || attr->form == DW_FORM_data8)
14060 return DW_UNSND (attr);
14061 else
14062 {
14063 complaint (&symfile_complaints,
14064 _("Attribute value is not a constant (%s)"),
14065 dwarf_form_name (attr->form));
14066 return default_value;
14067 }
14068 }
14069
14070 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14071 unit and add it to our queue.
14072 The result is non-zero if PER_CU was queued, otherwise the result is zero
14073 meaning either PER_CU is already queued or it is already loaded. */
14074
14075 static int
14076 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14077 struct dwarf2_per_cu_data *per_cu)
14078 {
14079 /* We may arrive here during partial symbol reading, if we need full
14080 DIEs to process an unusual case (e.g. template arguments). Do
14081 not queue PER_CU, just tell our caller to load its DIEs. */
14082 if (dwarf2_per_objfile->reading_partial_symbols)
14083 {
14084 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14085 return 1;
14086 return 0;
14087 }
14088
14089 /* Mark the dependence relation so that we don't flush PER_CU
14090 too early. */
14091 dwarf2_add_dependence (this_cu, per_cu);
14092
14093 /* If it's already on the queue, we have nothing to do. */
14094 if (per_cu->queued)
14095 return 0;
14096
14097 /* If the compilation unit is already loaded, just mark it as
14098 used. */
14099 if (per_cu->cu != NULL)
14100 {
14101 per_cu->cu->last_used = 0;
14102 return 0;
14103 }
14104
14105 /* Add it to the queue. */
14106 queue_comp_unit (per_cu);
14107
14108 return 1;
14109 }
14110
14111 /* Follow reference or signature attribute ATTR of SRC_DIE.
14112 On entry *REF_CU is the CU of SRC_DIE.
14113 On exit *REF_CU is the CU of the result. */
14114
14115 static struct die_info *
14116 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14117 struct dwarf2_cu **ref_cu)
14118 {
14119 struct die_info *die;
14120
14121 if (is_ref_attr (attr))
14122 die = follow_die_ref (src_die, attr, ref_cu);
14123 else if (attr->form == DW_FORM_ref_sig8)
14124 die = follow_die_sig (src_die, attr, ref_cu);
14125 else
14126 {
14127 dump_die_for_error (src_die);
14128 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14129 (*ref_cu)->objfile->name);
14130 }
14131
14132 return die;
14133 }
14134
14135 /* Follow reference OFFSET.
14136 On entry *REF_CU is the CU of the source die referencing OFFSET.
14137 On exit *REF_CU is the CU of the result.
14138 Returns NULL if OFFSET is invalid. */
14139
14140 static struct die_info *
14141 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14142 {
14143 struct die_info temp_die;
14144 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14145
14146 gdb_assert (cu->per_cu != NULL);
14147
14148 target_cu = cu;
14149
14150 if (cu->per_cu->debug_types_section)
14151 {
14152 /* .debug_types CUs cannot reference anything outside their CU.
14153 If they need to, they have to reference a signatured type via
14154 DW_FORM_ref_sig8. */
14155 if (! offset_in_cu_p (&cu->header, offset))
14156 return NULL;
14157 }
14158 else if (! offset_in_cu_p (&cu->header, offset))
14159 {
14160 struct dwarf2_per_cu_data *per_cu;
14161
14162 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14163
14164 /* If necessary, add it to the queue and load its DIEs. */
14165 if (maybe_queue_comp_unit (cu, per_cu))
14166 load_full_comp_unit (per_cu);
14167
14168 target_cu = per_cu->cu;
14169 }
14170 else if (cu->dies == NULL)
14171 {
14172 /* We're loading full DIEs during partial symbol reading. */
14173 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14174 load_full_comp_unit (cu->per_cu);
14175 }
14176
14177 *ref_cu = target_cu;
14178 temp_die.offset = offset;
14179 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14180 }
14181
14182 /* Follow reference attribute ATTR of SRC_DIE.
14183 On entry *REF_CU is the CU of SRC_DIE.
14184 On exit *REF_CU is the CU of the result. */
14185
14186 static struct die_info *
14187 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14188 struct dwarf2_cu **ref_cu)
14189 {
14190 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14191 struct dwarf2_cu *cu = *ref_cu;
14192 struct die_info *die;
14193
14194 die = follow_die_offset (offset, ref_cu);
14195 if (!die)
14196 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14197 "at 0x%x [in module %s]"),
14198 offset, src_die->offset, cu->objfile->name);
14199
14200 return die;
14201 }
14202
14203 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14204 Returned value is intended for DW_OP_call*. Returned
14205 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14206
14207 struct dwarf2_locexpr_baton
14208 dwarf2_fetch_die_location_block (unsigned int offset_in_cu,
14209 struct dwarf2_per_cu_data *per_cu,
14210 CORE_ADDR (*get_frame_pc) (void *baton),
14211 void *baton)
14212 {
14213 unsigned int offset = per_cu->offset + offset_in_cu;
14214 struct dwarf2_cu *cu;
14215 struct die_info *die;
14216 struct attribute *attr;
14217 struct dwarf2_locexpr_baton retval;
14218
14219 dw2_setup (per_cu->objfile);
14220
14221 if (per_cu->cu == NULL)
14222 load_cu (per_cu);
14223 cu = per_cu->cu;
14224
14225 die = follow_die_offset (offset, &cu);
14226 if (!die)
14227 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14228 offset, per_cu->objfile->name);
14229
14230 attr = dwarf2_attr (die, DW_AT_location, cu);
14231 if (!attr)
14232 {
14233 /* DWARF: "If there is no such attribute, then there is no effect.".
14234 DATA is ignored if SIZE is 0. */
14235
14236 retval.data = NULL;
14237 retval.size = 0;
14238 }
14239 else if (attr_form_is_section_offset (attr))
14240 {
14241 struct dwarf2_loclist_baton loclist_baton;
14242 CORE_ADDR pc = (*get_frame_pc) (baton);
14243 size_t size;
14244
14245 fill_in_loclist_baton (cu, &loclist_baton, attr);
14246
14247 retval.data = dwarf2_find_location_expression (&loclist_baton,
14248 &size, pc);
14249 retval.size = size;
14250 }
14251 else
14252 {
14253 if (!attr_form_is_block (attr))
14254 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14255 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14256 offset, per_cu->objfile->name);
14257
14258 retval.data = DW_BLOCK (attr)->data;
14259 retval.size = DW_BLOCK (attr)->size;
14260 }
14261 retval.per_cu = cu->per_cu;
14262
14263 age_cached_comp_units ();
14264
14265 return retval;
14266 }
14267
14268 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14269 PER_CU. */
14270
14271 struct type *
14272 dwarf2_get_die_type (unsigned int die_offset,
14273 struct dwarf2_per_cu_data *per_cu)
14274 {
14275 dw2_setup (per_cu->objfile);
14276 return get_die_type_at_offset (per_cu->offset + die_offset, per_cu);
14277 }
14278
14279 /* Follow the signature attribute ATTR in SRC_DIE.
14280 On entry *REF_CU is the CU of SRC_DIE.
14281 On exit *REF_CU is the CU of the result. */
14282
14283 static struct die_info *
14284 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14285 struct dwarf2_cu **ref_cu)
14286 {
14287 struct objfile *objfile = (*ref_cu)->objfile;
14288 struct die_info temp_die;
14289 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14290 struct dwarf2_cu *sig_cu;
14291 struct die_info *die;
14292
14293 /* sig_type will be NULL if the signatured type is missing from
14294 the debug info. */
14295 if (sig_type == NULL)
14296 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14297 "at 0x%x [in module %s]"),
14298 src_die->offset, objfile->name);
14299
14300 /* If necessary, add it to the queue and load its DIEs. */
14301
14302 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14303 read_signatured_type (sig_type);
14304
14305 gdb_assert (sig_type->per_cu.cu != NULL);
14306
14307 sig_cu = sig_type->per_cu.cu;
14308 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14309 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14310 if (die)
14311 {
14312 *ref_cu = sig_cu;
14313 return die;
14314 }
14315
14316 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14317 "from DIE at 0x%x [in module %s]"),
14318 sig_type->type_offset, src_die->offset, objfile->name);
14319 }
14320
14321 /* Given an offset of a signatured type, return its signatured_type. */
14322
14323 static struct signatured_type *
14324 lookup_signatured_type_at_offset (struct objfile *objfile,
14325 struct dwarf2_section_info *section,
14326 unsigned int offset)
14327 {
14328 gdb_byte *info_ptr = section->buffer + offset;
14329 unsigned int length, initial_length_size;
14330 unsigned int sig_offset;
14331 struct signatured_type find_entry, *type_sig;
14332
14333 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14334 sig_offset = (initial_length_size
14335 + 2 /*version*/
14336 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14337 + 1 /*address_size*/);
14338 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14339 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14340
14341 /* This is only used to lookup previously recorded types.
14342 If we didn't find it, it's our bug. */
14343 gdb_assert (type_sig != NULL);
14344 gdb_assert (offset == type_sig->per_cu.offset);
14345
14346 return type_sig;
14347 }
14348
14349 /* Load the DIEs associated with type unit PER_CU into memory. */
14350
14351 static void
14352 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14353 {
14354 struct objfile *objfile = per_cu->objfile;
14355 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14356 unsigned int offset = per_cu->offset;
14357 struct signatured_type *type_sig;
14358
14359 dwarf2_read_section (objfile, sect);
14360
14361 /* We have the section offset, but we need the signature to do the
14362 hash table lookup. */
14363 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14364 the signature to assert we found the right one.
14365 Ok, but it's a lot of work. We should simplify things so any needed
14366 assert doesn't require all this clumsiness. */
14367 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14368
14369 gdb_assert (type_sig->per_cu.cu == NULL);
14370
14371 read_signatured_type (type_sig);
14372
14373 gdb_assert (type_sig->per_cu.cu != NULL);
14374 }
14375
14376 /* Read in a signatured type and build its CU and DIEs. */
14377
14378 static void
14379 read_signatured_type (struct signatured_type *type_sig)
14380 {
14381 struct objfile *objfile = type_sig->per_cu.objfile;
14382 gdb_byte *types_ptr;
14383 struct die_reader_specs reader_specs;
14384 struct dwarf2_cu *cu;
14385 ULONGEST signature;
14386 struct cleanup *back_to, *free_cu_cleanup;
14387 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14388
14389 dwarf2_read_section (objfile, section);
14390 types_ptr = section->buffer + type_sig->per_cu.offset;
14391
14392 gdb_assert (type_sig->per_cu.cu == NULL);
14393
14394 cu = xmalloc (sizeof (*cu));
14395 init_one_comp_unit (cu, &type_sig->per_cu);
14396
14397 /* If an error occurs while loading, release our storage. */
14398 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14399
14400 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14401 &signature, NULL);
14402 gdb_assert (signature == type_sig->signature);
14403
14404 cu->die_hash
14405 = htab_create_alloc_ex (cu->header.length / 12,
14406 die_hash,
14407 die_eq,
14408 NULL,
14409 &cu->comp_unit_obstack,
14410 hashtab_obstack_allocate,
14411 dummy_obstack_deallocate);
14412
14413 dwarf2_read_abbrevs (cu);
14414 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14415
14416 init_cu_die_reader (&reader_specs, cu);
14417
14418 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14419 NULL /*parent*/);
14420
14421 /* We try not to read any attributes in this function, because not
14422 all CUs needed for references have been loaded yet, and symbol
14423 table processing isn't initialized. But we have to set the CU language,
14424 or we won't be able to build types correctly. */
14425 prepare_one_comp_unit (cu, cu->dies);
14426
14427 do_cleanups (back_to);
14428
14429 /* We've successfully allocated this compilation unit. Let our caller
14430 clean it up when finished with it. */
14431 discard_cleanups (free_cu_cleanup);
14432
14433 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14434 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14435 }
14436
14437 /* Decode simple location descriptions.
14438 Given a pointer to a dwarf block that defines a location, compute
14439 the location and return the value.
14440
14441 NOTE drow/2003-11-18: This function is called in two situations
14442 now: for the address of static or global variables (partial symbols
14443 only) and for offsets into structures which are expected to be
14444 (more or less) constant. The partial symbol case should go away,
14445 and only the constant case should remain. That will let this
14446 function complain more accurately. A few special modes are allowed
14447 without complaint for global variables (for instance, global
14448 register values and thread-local values).
14449
14450 A location description containing no operations indicates that the
14451 object is optimized out. The return value is 0 for that case.
14452 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14453 callers will only want a very basic result and this can become a
14454 complaint.
14455
14456 Note that stack[0] is unused except as a default error return. */
14457
14458 static CORE_ADDR
14459 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14460 {
14461 struct objfile *objfile = cu->objfile;
14462 int i;
14463 int size = blk->size;
14464 gdb_byte *data = blk->data;
14465 CORE_ADDR stack[64];
14466 int stacki;
14467 unsigned int bytes_read, unsnd;
14468 gdb_byte op;
14469
14470 i = 0;
14471 stacki = 0;
14472 stack[stacki] = 0;
14473 stack[++stacki] = 0;
14474
14475 while (i < size)
14476 {
14477 op = data[i++];
14478 switch (op)
14479 {
14480 case DW_OP_lit0:
14481 case DW_OP_lit1:
14482 case DW_OP_lit2:
14483 case DW_OP_lit3:
14484 case DW_OP_lit4:
14485 case DW_OP_lit5:
14486 case DW_OP_lit6:
14487 case DW_OP_lit7:
14488 case DW_OP_lit8:
14489 case DW_OP_lit9:
14490 case DW_OP_lit10:
14491 case DW_OP_lit11:
14492 case DW_OP_lit12:
14493 case DW_OP_lit13:
14494 case DW_OP_lit14:
14495 case DW_OP_lit15:
14496 case DW_OP_lit16:
14497 case DW_OP_lit17:
14498 case DW_OP_lit18:
14499 case DW_OP_lit19:
14500 case DW_OP_lit20:
14501 case DW_OP_lit21:
14502 case DW_OP_lit22:
14503 case DW_OP_lit23:
14504 case DW_OP_lit24:
14505 case DW_OP_lit25:
14506 case DW_OP_lit26:
14507 case DW_OP_lit27:
14508 case DW_OP_lit28:
14509 case DW_OP_lit29:
14510 case DW_OP_lit30:
14511 case DW_OP_lit31:
14512 stack[++stacki] = op - DW_OP_lit0;
14513 break;
14514
14515 case DW_OP_reg0:
14516 case DW_OP_reg1:
14517 case DW_OP_reg2:
14518 case DW_OP_reg3:
14519 case DW_OP_reg4:
14520 case DW_OP_reg5:
14521 case DW_OP_reg6:
14522 case DW_OP_reg7:
14523 case DW_OP_reg8:
14524 case DW_OP_reg9:
14525 case DW_OP_reg10:
14526 case DW_OP_reg11:
14527 case DW_OP_reg12:
14528 case DW_OP_reg13:
14529 case DW_OP_reg14:
14530 case DW_OP_reg15:
14531 case DW_OP_reg16:
14532 case DW_OP_reg17:
14533 case DW_OP_reg18:
14534 case DW_OP_reg19:
14535 case DW_OP_reg20:
14536 case DW_OP_reg21:
14537 case DW_OP_reg22:
14538 case DW_OP_reg23:
14539 case DW_OP_reg24:
14540 case DW_OP_reg25:
14541 case DW_OP_reg26:
14542 case DW_OP_reg27:
14543 case DW_OP_reg28:
14544 case DW_OP_reg29:
14545 case DW_OP_reg30:
14546 case DW_OP_reg31:
14547 stack[++stacki] = op - DW_OP_reg0;
14548 if (i < size)
14549 dwarf2_complex_location_expr_complaint ();
14550 break;
14551
14552 case DW_OP_regx:
14553 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14554 i += bytes_read;
14555 stack[++stacki] = unsnd;
14556 if (i < size)
14557 dwarf2_complex_location_expr_complaint ();
14558 break;
14559
14560 case DW_OP_addr:
14561 stack[++stacki] = read_address (objfile->obfd, &data[i],
14562 cu, &bytes_read);
14563 i += bytes_read;
14564 break;
14565
14566 case DW_OP_const1u:
14567 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14568 i += 1;
14569 break;
14570
14571 case DW_OP_const1s:
14572 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14573 i += 1;
14574 break;
14575
14576 case DW_OP_const2u:
14577 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14578 i += 2;
14579 break;
14580
14581 case DW_OP_const2s:
14582 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14583 i += 2;
14584 break;
14585
14586 case DW_OP_const4u:
14587 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14588 i += 4;
14589 break;
14590
14591 case DW_OP_const4s:
14592 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14593 i += 4;
14594 break;
14595
14596 case DW_OP_const8u:
14597 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14598 i += 8;
14599 break;
14600
14601 case DW_OP_constu:
14602 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14603 &bytes_read);
14604 i += bytes_read;
14605 break;
14606
14607 case DW_OP_consts:
14608 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14609 i += bytes_read;
14610 break;
14611
14612 case DW_OP_dup:
14613 stack[stacki + 1] = stack[stacki];
14614 stacki++;
14615 break;
14616
14617 case DW_OP_plus:
14618 stack[stacki - 1] += stack[stacki];
14619 stacki--;
14620 break;
14621
14622 case DW_OP_plus_uconst:
14623 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14624 &bytes_read);
14625 i += bytes_read;
14626 break;
14627
14628 case DW_OP_minus:
14629 stack[stacki - 1] -= stack[stacki];
14630 stacki--;
14631 break;
14632
14633 case DW_OP_deref:
14634 /* If we're not the last op, then we definitely can't encode
14635 this using GDB's address_class enum. This is valid for partial
14636 global symbols, although the variable's address will be bogus
14637 in the psymtab. */
14638 if (i < size)
14639 dwarf2_complex_location_expr_complaint ();
14640 break;
14641
14642 case DW_OP_GNU_push_tls_address:
14643 /* The top of the stack has the offset from the beginning
14644 of the thread control block at which the variable is located. */
14645 /* Nothing should follow this operator, so the top of stack would
14646 be returned. */
14647 /* This is valid for partial global symbols, but the variable's
14648 address will be bogus in the psymtab. Make it always at least
14649 non-zero to not look as a variable garbage collected by linker
14650 which have DW_OP_addr 0. */
14651 if (i < size)
14652 dwarf2_complex_location_expr_complaint ();
14653 stack[stacki]++;
14654 break;
14655
14656 case DW_OP_GNU_uninit:
14657 break;
14658
14659 default:
14660 {
14661 const char *name = dwarf_stack_op_name (op);
14662
14663 if (name)
14664 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14665 name);
14666 else
14667 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14668 op);
14669 }
14670
14671 return (stack[stacki]);
14672 }
14673
14674 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14675 outside of the allocated space. Also enforce minimum>0. */
14676 if (stacki >= ARRAY_SIZE (stack) - 1)
14677 {
14678 complaint (&symfile_complaints,
14679 _("location description stack overflow"));
14680 return 0;
14681 }
14682
14683 if (stacki <= 0)
14684 {
14685 complaint (&symfile_complaints,
14686 _("location description stack underflow"));
14687 return 0;
14688 }
14689 }
14690 return (stack[stacki]);
14691 }
14692
14693 /* memory allocation interface */
14694
14695 static struct dwarf_block *
14696 dwarf_alloc_block (struct dwarf2_cu *cu)
14697 {
14698 struct dwarf_block *blk;
14699
14700 blk = (struct dwarf_block *)
14701 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14702 return (blk);
14703 }
14704
14705 static struct abbrev_info *
14706 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14707 {
14708 struct abbrev_info *abbrev;
14709
14710 abbrev = (struct abbrev_info *)
14711 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14712 memset (abbrev, 0, sizeof (struct abbrev_info));
14713 return (abbrev);
14714 }
14715
14716 static struct die_info *
14717 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14718 {
14719 struct die_info *die;
14720 size_t size = sizeof (struct die_info);
14721
14722 if (num_attrs > 1)
14723 size += (num_attrs - 1) * sizeof (struct attribute);
14724
14725 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14726 memset (die, 0, sizeof (struct die_info));
14727 return (die);
14728 }
14729
14730 \f
14731 /* Macro support. */
14732
14733 /* Return the full name of file number I in *LH's file name table.
14734 Use COMP_DIR as the name of the current directory of the
14735 compilation. The result is allocated using xmalloc; the caller is
14736 responsible for freeing it. */
14737 static char *
14738 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14739 {
14740 /* Is the file number a valid index into the line header's file name
14741 table? Remember that file numbers start with one, not zero. */
14742 if (1 <= file && file <= lh->num_file_names)
14743 {
14744 struct file_entry *fe = &lh->file_names[file - 1];
14745
14746 if (IS_ABSOLUTE_PATH (fe->name))
14747 return xstrdup (fe->name);
14748 else
14749 {
14750 const char *dir;
14751 int dir_len;
14752 char *full_name;
14753
14754 if (fe->dir_index)
14755 dir = lh->include_dirs[fe->dir_index - 1];
14756 else
14757 dir = comp_dir;
14758
14759 if (dir)
14760 {
14761 dir_len = strlen (dir);
14762 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14763 strcpy (full_name, dir);
14764 full_name[dir_len] = '/';
14765 strcpy (full_name + dir_len + 1, fe->name);
14766 return full_name;
14767 }
14768 else
14769 return xstrdup (fe->name);
14770 }
14771 }
14772 else
14773 {
14774 /* The compiler produced a bogus file number. We can at least
14775 record the macro definitions made in the file, even if we
14776 won't be able to find the file by name. */
14777 char fake_name[80];
14778
14779 sprintf (fake_name, "<bad macro file number %d>", file);
14780
14781 complaint (&symfile_complaints,
14782 _("bad file number in macro information (%d)"),
14783 file);
14784
14785 return xstrdup (fake_name);
14786 }
14787 }
14788
14789
14790 static struct macro_source_file *
14791 macro_start_file (int file, int line,
14792 struct macro_source_file *current_file,
14793 const char *comp_dir,
14794 struct line_header *lh, struct objfile *objfile)
14795 {
14796 /* The full name of this source file. */
14797 char *full_name = file_full_name (file, lh, comp_dir);
14798
14799 /* We don't create a macro table for this compilation unit
14800 at all until we actually get a filename. */
14801 if (! pending_macros)
14802 pending_macros = new_macro_table (&objfile->objfile_obstack,
14803 objfile->macro_cache);
14804
14805 if (! current_file)
14806 /* If we have no current file, then this must be the start_file
14807 directive for the compilation unit's main source file. */
14808 current_file = macro_set_main (pending_macros, full_name);
14809 else
14810 current_file = macro_include (current_file, line, full_name);
14811
14812 xfree (full_name);
14813
14814 return current_file;
14815 }
14816
14817
14818 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14819 followed by a null byte. */
14820 static char *
14821 copy_string (const char *buf, int len)
14822 {
14823 char *s = xmalloc (len + 1);
14824
14825 memcpy (s, buf, len);
14826 s[len] = '\0';
14827 return s;
14828 }
14829
14830
14831 static const char *
14832 consume_improper_spaces (const char *p, const char *body)
14833 {
14834 if (*p == ' ')
14835 {
14836 complaint (&symfile_complaints,
14837 _("macro definition contains spaces "
14838 "in formal argument list:\n`%s'"),
14839 body);
14840
14841 while (*p == ' ')
14842 p++;
14843 }
14844
14845 return p;
14846 }
14847
14848
14849 static void
14850 parse_macro_definition (struct macro_source_file *file, int line,
14851 const char *body)
14852 {
14853 const char *p;
14854
14855 /* The body string takes one of two forms. For object-like macro
14856 definitions, it should be:
14857
14858 <macro name> " " <definition>
14859
14860 For function-like macro definitions, it should be:
14861
14862 <macro name> "() " <definition>
14863 or
14864 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14865
14866 Spaces may appear only where explicitly indicated, and in the
14867 <definition>.
14868
14869 The Dwarf 2 spec says that an object-like macro's name is always
14870 followed by a space, but versions of GCC around March 2002 omit
14871 the space when the macro's definition is the empty string.
14872
14873 The Dwarf 2 spec says that there should be no spaces between the
14874 formal arguments in a function-like macro's formal argument list,
14875 but versions of GCC around March 2002 include spaces after the
14876 commas. */
14877
14878
14879 /* Find the extent of the macro name. The macro name is terminated
14880 by either a space or null character (for an object-like macro) or
14881 an opening paren (for a function-like macro). */
14882 for (p = body; *p; p++)
14883 if (*p == ' ' || *p == '(')
14884 break;
14885
14886 if (*p == ' ' || *p == '\0')
14887 {
14888 /* It's an object-like macro. */
14889 int name_len = p - body;
14890 char *name = copy_string (body, name_len);
14891 const char *replacement;
14892
14893 if (*p == ' ')
14894 replacement = body + name_len + 1;
14895 else
14896 {
14897 dwarf2_macro_malformed_definition_complaint (body);
14898 replacement = body + name_len;
14899 }
14900
14901 macro_define_object (file, line, name, replacement);
14902
14903 xfree (name);
14904 }
14905 else if (*p == '(')
14906 {
14907 /* It's a function-like macro. */
14908 char *name = copy_string (body, p - body);
14909 int argc = 0;
14910 int argv_size = 1;
14911 char **argv = xmalloc (argv_size * sizeof (*argv));
14912
14913 p++;
14914
14915 p = consume_improper_spaces (p, body);
14916
14917 /* Parse the formal argument list. */
14918 while (*p && *p != ')')
14919 {
14920 /* Find the extent of the current argument name. */
14921 const char *arg_start = p;
14922
14923 while (*p && *p != ',' && *p != ')' && *p != ' ')
14924 p++;
14925
14926 if (! *p || p == arg_start)
14927 dwarf2_macro_malformed_definition_complaint (body);
14928 else
14929 {
14930 /* Make sure argv has room for the new argument. */
14931 if (argc >= argv_size)
14932 {
14933 argv_size *= 2;
14934 argv = xrealloc (argv, argv_size * sizeof (*argv));
14935 }
14936
14937 argv[argc++] = copy_string (arg_start, p - arg_start);
14938 }
14939
14940 p = consume_improper_spaces (p, body);
14941
14942 /* Consume the comma, if present. */
14943 if (*p == ',')
14944 {
14945 p++;
14946
14947 p = consume_improper_spaces (p, body);
14948 }
14949 }
14950
14951 if (*p == ')')
14952 {
14953 p++;
14954
14955 if (*p == ' ')
14956 /* Perfectly formed definition, no complaints. */
14957 macro_define_function (file, line, name,
14958 argc, (const char **) argv,
14959 p + 1);
14960 else if (*p == '\0')
14961 {
14962 /* Complain, but do define it. */
14963 dwarf2_macro_malformed_definition_complaint (body);
14964 macro_define_function (file, line, name,
14965 argc, (const char **) argv,
14966 p);
14967 }
14968 else
14969 /* Just complain. */
14970 dwarf2_macro_malformed_definition_complaint (body);
14971 }
14972 else
14973 /* Just complain. */
14974 dwarf2_macro_malformed_definition_complaint (body);
14975
14976 xfree (name);
14977 {
14978 int i;
14979
14980 for (i = 0; i < argc; i++)
14981 xfree (argv[i]);
14982 }
14983 xfree (argv);
14984 }
14985 else
14986 dwarf2_macro_malformed_definition_complaint (body);
14987 }
14988
14989 /* Skip some bytes from BYTES according to the form given in FORM.
14990 Returns the new pointer. */
14991
14992 static gdb_byte *
14993 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14994 enum dwarf_form form,
14995 unsigned int offset_size,
14996 struct dwarf2_section_info *section)
14997 {
14998 unsigned int bytes_read;
14999
15000 switch (form)
15001 {
15002 case DW_FORM_data1:
15003 case DW_FORM_flag:
15004 ++bytes;
15005 break;
15006
15007 case DW_FORM_data2:
15008 bytes += 2;
15009 break;
15010
15011 case DW_FORM_data4:
15012 bytes += 4;
15013 break;
15014
15015 case DW_FORM_data8:
15016 bytes += 8;
15017 break;
15018
15019 case DW_FORM_string:
15020 read_direct_string (abfd, bytes, &bytes_read);
15021 bytes += bytes_read;
15022 break;
15023
15024 case DW_FORM_sec_offset:
15025 case DW_FORM_strp:
15026 bytes += offset_size;
15027 break;
15028
15029 case DW_FORM_block:
15030 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15031 bytes += bytes_read;
15032 break;
15033
15034 case DW_FORM_block1:
15035 bytes += 1 + read_1_byte (abfd, bytes);
15036 break;
15037 case DW_FORM_block2:
15038 bytes += 2 + read_2_bytes (abfd, bytes);
15039 break;
15040 case DW_FORM_block4:
15041 bytes += 4 + read_4_bytes (abfd, bytes);
15042 break;
15043
15044 case DW_FORM_sdata:
15045 case DW_FORM_udata:
15046 bytes = skip_leb128 (abfd, bytes);
15047 break;
15048
15049 default:
15050 {
15051 complain:
15052 complaint (&symfile_complaints,
15053 _("invalid form 0x%x in `%s'"),
15054 form,
15055 section->asection->name);
15056 return NULL;
15057 }
15058 }
15059
15060 return bytes;
15061 }
15062
15063 /* A helper for dwarf_decode_macros that handles skipping an unknown
15064 opcode. Returns an updated pointer to the macro data buffer; or,
15065 on error, issues a complaint and returns NULL. */
15066
15067 static gdb_byte *
15068 skip_unknown_opcode (unsigned int opcode,
15069 gdb_byte **opcode_definitions,
15070 gdb_byte *mac_ptr,
15071 bfd *abfd,
15072 unsigned int offset_size,
15073 struct dwarf2_section_info *section)
15074 {
15075 unsigned int bytes_read, i;
15076 unsigned long arg;
15077 gdb_byte *defn;
15078
15079 if (opcode_definitions[opcode] == NULL)
15080 {
15081 complaint (&symfile_complaints,
15082 _("unrecognized DW_MACFINO opcode 0x%x"),
15083 opcode);
15084 return NULL;
15085 }
15086
15087 defn = opcode_definitions[opcode];
15088 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15089 defn += bytes_read;
15090
15091 for (i = 0; i < arg; ++i)
15092 {
15093 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15094 if (mac_ptr == NULL)
15095 {
15096 /* skip_form_bytes already issued the complaint. */
15097 return NULL;
15098 }
15099 }
15100
15101 return mac_ptr;
15102 }
15103
15104 /* A helper function which parses the header of a macro section.
15105 If the macro section is the extended (for now called "GNU") type,
15106 then this updates *OFFSET_SIZE. Returns a pointer to just after
15107 the header, or issues a complaint and returns NULL on error. */
15108
15109 static gdb_byte *
15110 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15111 bfd *abfd,
15112 gdb_byte *mac_ptr,
15113 unsigned int *offset_size,
15114 int section_is_gnu)
15115 {
15116 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15117
15118 if (section_is_gnu)
15119 {
15120 unsigned int version, flags;
15121
15122 version = read_2_bytes (abfd, mac_ptr);
15123 if (version != 4)
15124 {
15125 complaint (&symfile_complaints,
15126 _("unrecognized version `%d' in .debug_macro section"),
15127 version);
15128 return NULL;
15129 }
15130 mac_ptr += 2;
15131
15132 flags = read_1_byte (abfd, mac_ptr);
15133 ++mac_ptr;
15134 *offset_size = (flags & 1) ? 8 : 4;
15135
15136 if ((flags & 2) != 0)
15137 /* We don't need the line table offset. */
15138 mac_ptr += *offset_size;
15139
15140 /* Vendor opcode descriptions. */
15141 if ((flags & 4) != 0)
15142 {
15143 unsigned int i, count;
15144
15145 count = read_1_byte (abfd, mac_ptr);
15146 ++mac_ptr;
15147 for (i = 0; i < count; ++i)
15148 {
15149 unsigned int opcode, bytes_read;
15150 unsigned long arg;
15151
15152 opcode = read_1_byte (abfd, mac_ptr);
15153 ++mac_ptr;
15154 opcode_definitions[opcode] = mac_ptr;
15155 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15156 mac_ptr += bytes_read;
15157 mac_ptr += arg;
15158 }
15159 }
15160 }
15161
15162 return mac_ptr;
15163 }
15164
15165 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15166 including DW_MACRO_GNU_transparent_include. */
15167
15168 static void
15169 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15170 struct macro_source_file *current_file,
15171 struct line_header *lh, char *comp_dir,
15172 struct dwarf2_section_info *section,
15173 int section_is_gnu,
15174 unsigned int offset_size,
15175 struct objfile *objfile,
15176 htab_t include_hash)
15177 {
15178 enum dwarf_macro_record_type macinfo_type;
15179 int at_commandline;
15180 gdb_byte *opcode_definitions[256];
15181
15182 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15183 &offset_size, section_is_gnu);
15184 if (mac_ptr == NULL)
15185 {
15186 /* We already issued a complaint. */
15187 return;
15188 }
15189
15190 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15191 GDB is still reading the definitions from command line. First
15192 DW_MACINFO_start_file will need to be ignored as it was already executed
15193 to create CURRENT_FILE for the main source holding also the command line
15194 definitions. On first met DW_MACINFO_start_file this flag is reset to
15195 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15196
15197 at_commandline = 1;
15198
15199 do
15200 {
15201 /* Do we at least have room for a macinfo type byte? */
15202 if (mac_ptr >= mac_end)
15203 {
15204 dwarf2_macros_too_long_complaint (section);
15205 break;
15206 }
15207
15208 macinfo_type = read_1_byte (abfd, mac_ptr);
15209 mac_ptr++;
15210
15211 /* Note that we rely on the fact that the corresponding GNU and
15212 DWARF constants are the same. */
15213 switch (macinfo_type)
15214 {
15215 /* A zero macinfo type indicates the end of the macro
15216 information. */
15217 case 0:
15218 break;
15219
15220 case DW_MACRO_GNU_define:
15221 case DW_MACRO_GNU_undef:
15222 case DW_MACRO_GNU_define_indirect:
15223 case DW_MACRO_GNU_undef_indirect:
15224 {
15225 unsigned int bytes_read;
15226 int line;
15227 char *body;
15228 int is_define;
15229
15230 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15231 mac_ptr += bytes_read;
15232
15233 if (macinfo_type == DW_MACRO_GNU_define
15234 || macinfo_type == DW_MACRO_GNU_undef)
15235 {
15236 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15237 mac_ptr += bytes_read;
15238 }
15239 else
15240 {
15241 LONGEST str_offset;
15242
15243 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15244 mac_ptr += offset_size;
15245
15246 body = read_indirect_string_at_offset (abfd, str_offset);
15247 }
15248
15249 is_define = (macinfo_type == DW_MACRO_GNU_define
15250 || macinfo_type == DW_MACRO_GNU_define_indirect);
15251 if (! current_file)
15252 {
15253 /* DWARF violation as no main source is present. */
15254 complaint (&symfile_complaints,
15255 _("debug info with no main source gives macro %s "
15256 "on line %d: %s"),
15257 is_define ? _("definition") : _("undefinition"),
15258 line, body);
15259 break;
15260 }
15261 if ((line == 0 && !at_commandline)
15262 || (line != 0 && at_commandline))
15263 complaint (&symfile_complaints,
15264 _("debug info gives %s macro %s with %s line %d: %s"),
15265 at_commandline ? _("command-line") : _("in-file"),
15266 is_define ? _("definition") : _("undefinition"),
15267 line == 0 ? _("zero") : _("non-zero"), line, body);
15268
15269 if (is_define)
15270 parse_macro_definition (current_file, line, body);
15271 else
15272 {
15273 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15274 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15275 macro_undef (current_file, line, body);
15276 }
15277 }
15278 break;
15279
15280 case DW_MACRO_GNU_start_file:
15281 {
15282 unsigned int bytes_read;
15283 int line, file;
15284
15285 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15286 mac_ptr += bytes_read;
15287 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15288 mac_ptr += bytes_read;
15289
15290 if ((line == 0 && !at_commandline)
15291 || (line != 0 && at_commandline))
15292 complaint (&symfile_complaints,
15293 _("debug info gives source %d included "
15294 "from %s at %s line %d"),
15295 file, at_commandline ? _("command-line") : _("file"),
15296 line == 0 ? _("zero") : _("non-zero"), line);
15297
15298 if (at_commandline)
15299 {
15300 /* This DW_MACRO_GNU_start_file was executed in the
15301 pass one. */
15302 at_commandline = 0;
15303 }
15304 else
15305 current_file = macro_start_file (file, line,
15306 current_file, comp_dir,
15307 lh, objfile);
15308 }
15309 break;
15310
15311 case DW_MACRO_GNU_end_file:
15312 if (! current_file)
15313 complaint (&symfile_complaints,
15314 _("macro debug info has an unmatched "
15315 "`close_file' directive"));
15316 else
15317 {
15318 current_file = current_file->included_by;
15319 if (! current_file)
15320 {
15321 enum dwarf_macro_record_type next_type;
15322
15323 /* GCC circa March 2002 doesn't produce the zero
15324 type byte marking the end of the compilation
15325 unit. Complain if it's not there, but exit no
15326 matter what. */
15327
15328 /* Do we at least have room for a macinfo type byte? */
15329 if (mac_ptr >= mac_end)
15330 {
15331 dwarf2_macros_too_long_complaint (section);
15332 return;
15333 }
15334
15335 /* We don't increment mac_ptr here, so this is just
15336 a look-ahead. */
15337 next_type = read_1_byte (abfd, mac_ptr);
15338 if (next_type != 0)
15339 complaint (&symfile_complaints,
15340 _("no terminating 0-type entry for "
15341 "macros in `.debug_macinfo' section"));
15342
15343 return;
15344 }
15345 }
15346 break;
15347
15348 case DW_MACRO_GNU_transparent_include:
15349 {
15350 LONGEST offset;
15351 void **slot;
15352
15353 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15354 mac_ptr += offset_size;
15355
15356 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15357 if (*slot != NULL)
15358 {
15359 /* This has actually happened; see
15360 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15361 complaint (&symfile_complaints,
15362 _("recursive DW_MACRO_GNU_transparent_include in "
15363 ".debug_macro section"));
15364 }
15365 else
15366 {
15367 *slot = mac_ptr;
15368
15369 dwarf_decode_macro_bytes (abfd,
15370 section->buffer + offset,
15371 mac_end, current_file,
15372 lh, comp_dir,
15373 section, section_is_gnu,
15374 offset_size, objfile, include_hash);
15375
15376 htab_remove_elt (include_hash, mac_ptr);
15377 }
15378 }
15379 break;
15380
15381 case DW_MACINFO_vendor_ext:
15382 if (!section_is_gnu)
15383 {
15384 unsigned int bytes_read;
15385 int constant;
15386
15387 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15388 mac_ptr += bytes_read;
15389 read_direct_string (abfd, mac_ptr, &bytes_read);
15390 mac_ptr += bytes_read;
15391
15392 /* We don't recognize any vendor extensions. */
15393 break;
15394 }
15395 /* FALLTHROUGH */
15396
15397 default:
15398 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15399 mac_ptr, abfd, offset_size,
15400 section);
15401 if (mac_ptr == NULL)
15402 return;
15403 break;
15404 }
15405 } while (macinfo_type != 0);
15406 }
15407
15408 static void
15409 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15410 char *comp_dir, bfd *abfd,
15411 struct dwarf2_cu *cu,
15412 struct dwarf2_section_info *section,
15413 int section_is_gnu)
15414 {
15415 struct objfile *objfile = dwarf2_per_objfile->objfile;
15416 gdb_byte *mac_ptr, *mac_end;
15417 struct macro_source_file *current_file = 0;
15418 enum dwarf_macro_record_type macinfo_type;
15419 unsigned int offset_size = cu->header.offset_size;
15420 gdb_byte *opcode_definitions[256];
15421 struct cleanup *cleanup;
15422 htab_t include_hash;
15423 void **slot;
15424
15425 dwarf2_read_section (objfile, section);
15426 if (section->buffer == NULL)
15427 {
15428 complaint (&symfile_complaints, _("missing %s section"),
15429 section->asection->name);
15430 return;
15431 }
15432
15433 /* First pass: Find the name of the base filename.
15434 This filename is needed in order to process all macros whose definition
15435 (or undefinition) comes from the command line. These macros are defined
15436 before the first DW_MACINFO_start_file entry, and yet still need to be
15437 associated to the base file.
15438
15439 To determine the base file name, we scan the macro definitions until we
15440 reach the first DW_MACINFO_start_file entry. We then initialize
15441 CURRENT_FILE accordingly so that any macro definition found before the
15442 first DW_MACINFO_start_file can still be associated to the base file. */
15443
15444 mac_ptr = section->buffer + offset;
15445 mac_end = section->buffer + section->size;
15446
15447 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15448 &offset_size, section_is_gnu);
15449 if (mac_ptr == NULL)
15450 {
15451 /* We already issued a complaint. */
15452 return;
15453 }
15454
15455 do
15456 {
15457 /* Do we at least have room for a macinfo type byte? */
15458 if (mac_ptr >= mac_end)
15459 {
15460 /* Complaint is printed during the second pass as GDB will probably
15461 stop the first pass earlier upon finding
15462 DW_MACINFO_start_file. */
15463 break;
15464 }
15465
15466 macinfo_type = read_1_byte (abfd, mac_ptr);
15467 mac_ptr++;
15468
15469 /* Note that we rely on the fact that the corresponding GNU and
15470 DWARF constants are the same. */
15471 switch (macinfo_type)
15472 {
15473 /* A zero macinfo type indicates the end of the macro
15474 information. */
15475 case 0:
15476 break;
15477
15478 case DW_MACRO_GNU_define:
15479 case DW_MACRO_GNU_undef:
15480 /* Only skip the data by MAC_PTR. */
15481 {
15482 unsigned int bytes_read;
15483
15484 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15485 mac_ptr += bytes_read;
15486 read_direct_string (abfd, mac_ptr, &bytes_read);
15487 mac_ptr += bytes_read;
15488 }
15489 break;
15490
15491 case DW_MACRO_GNU_start_file:
15492 {
15493 unsigned int bytes_read;
15494 int line, file;
15495
15496 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15497 mac_ptr += bytes_read;
15498 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15499 mac_ptr += bytes_read;
15500
15501 current_file = macro_start_file (file, line, current_file,
15502 comp_dir, lh, objfile);
15503 }
15504 break;
15505
15506 case DW_MACRO_GNU_end_file:
15507 /* No data to skip by MAC_PTR. */
15508 break;
15509
15510 case DW_MACRO_GNU_define_indirect:
15511 case DW_MACRO_GNU_undef_indirect:
15512 {
15513 unsigned int bytes_read;
15514
15515 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15516 mac_ptr += bytes_read;
15517 mac_ptr += offset_size;
15518 }
15519 break;
15520
15521 case DW_MACRO_GNU_transparent_include:
15522 /* Note that, according to the spec, a transparent include
15523 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15524 skip this opcode. */
15525 mac_ptr += offset_size;
15526 break;
15527
15528 case DW_MACINFO_vendor_ext:
15529 /* Only skip the data by MAC_PTR. */
15530 if (!section_is_gnu)
15531 {
15532 unsigned int bytes_read;
15533
15534 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15535 mac_ptr += bytes_read;
15536 read_direct_string (abfd, mac_ptr, &bytes_read);
15537 mac_ptr += bytes_read;
15538 }
15539 /* FALLTHROUGH */
15540
15541 default:
15542 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15543 mac_ptr, abfd, offset_size,
15544 section);
15545 if (mac_ptr == NULL)
15546 return;
15547 break;
15548 }
15549 } while (macinfo_type != 0 && current_file == NULL);
15550
15551 /* Second pass: Process all entries.
15552
15553 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15554 command-line macro definitions/undefinitions. This flag is unset when we
15555 reach the first DW_MACINFO_start_file entry. */
15556
15557 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15558 NULL, xcalloc, xfree);
15559 cleanup = make_cleanup_htab_delete (include_hash);
15560 mac_ptr = section->buffer + offset;
15561 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15562 *slot = mac_ptr;
15563 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
15564 current_file, lh, comp_dir, section, section_is_gnu,
15565 offset_size, objfile, include_hash);
15566 do_cleanups (cleanup);
15567 }
15568
15569 /* Check if the attribute's form is a DW_FORM_block*
15570 if so return true else false. */
15571 static int
15572 attr_form_is_block (struct attribute *attr)
15573 {
15574 return (attr == NULL ? 0 :
15575 attr->form == DW_FORM_block1
15576 || attr->form == DW_FORM_block2
15577 || attr->form == DW_FORM_block4
15578 || attr->form == DW_FORM_block
15579 || attr->form == DW_FORM_exprloc);
15580 }
15581
15582 /* Return non-zero if ATTR's value is a section offset --- classes
15583 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15584 You may use DW_UNSND (attr) to retrieve such offsets.
15585
15586 Section 7.5.4, "Attribute Encodings", explains that no attribute
15587 may have a value that belongs to more than one of these classes; it
15588 would be ambiguous if we did, because we use the same forms for all
15589 of them. */
15590 static int
15591 attr_form_is_section_offset (struct attribute *attr)
15592 {
15593 return (attr->form == DW_FORM_data4
15594 || attr->form == DW_FORM_data8
15595 || attr->form == DW_FORM_sec_offset);
15596 }
15597
15598
15599 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15600 zero otherwise. When this function returns true, you can apply
15601 dwarf2_get_attr_constant_value to it.
15602
15603 However, note that for some attributes you must check
15604 attr_form_is_section_offset before using this test. DW_FORM_data4
15605 and DW_FORM_data8 are members of both the constant class, and of
15606 the classes that contain offsets into other debug sections
15607 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15608 that, if an attribute's can be either a constant or one of the
15609 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15610 taken as section offsets, not constants. */
15611 static int
15612 attr_form_is_constant (struct attribute *attr)
15613 {
15614 switch (attr->form)
15615 {
15616 case DW_FORM_sdata:
15617 case DW_FORM_udata:
15618 case DW_FORM_data1:
15619 case DW_FORM_data2:
15620 case DW_FORM_data4:
15621 case DW_FORM_data8:
15622 return 1;
15623 default:
15624 return 0;
15625 }
15626 }
15627
15628 /* A helper function that fills in a dwarf2_loclist_baton. */
15629
15630 static void
15631 fill_in_loclist_baton (struct dwarf2_cu *cu,
15632 struct dwarf2_loclist_baton *baton,
15633 struct attribute *attr)
15634 {
15635 dwarf2_read_section (dwarf2_per_objfile->objfile,
15636 &dwarf2_per_objfile->loc);
15637
15638 baton->per_cu = cu->per_cu;
15639 gdb_assert (baton->per_cu);
15640 /* We don't know how long the location list is, but make sure we
15641 don't run off the edge of the section. */
15642 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15643 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15644 baton->base_address = cu->base_address;
15645 }
15646
15647 static void
15648 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15649 struct dwarf2_cu *cu)
15650 {
15651 struct objfile *objfile = dwarf2_per_objfile->objfile;
15652
15653 if (attr_form_is_section_offset (attr)
15654 /* ".debug_loc" may not exist at all, or the offset may be outside
15655 the section. If so, fall through to the complaint in the
15656 other branch. */
15657 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15658 &dwarf2_per_objfile->loc))
15659 {
15660 struct dwarf2_loclist_baton *baton;
15661
15662 baton = obstack_alloc (&objfile->objfile_obstack,
15663 sizeof (struct dwarf2_loclist_baton));
15664
15665 fill_in_loclist_baton (cu, baton, attr);
15666
15667 if (cu->base_known == 0)
15668 complaint (&symfile_complaints,
15669 _("Location list used without "
15670 "specifying the CU base address."));
15671
15672 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15673 SYMBOL_LOCATION_BATON (sym) = baton;
15674 }
15675 else
15676 {
15677 struct dwarf2_locexpr_baton *baton;
15678
15679 baton = obstack_alloc (&objfile->objfile_obstack,
15680 sizeof (struct dwarf2_locexpr_baton));
15681 baton->per_cu = cu->per_cu;
15682 gdb_assert (baton->per_cu);
15683
15684 if (attr_form_is_block (attr))
15685 {
15686 /* Note that we're just copying the block's data pointer
15687 here, not the actual data. We're still pointing into the
15688 info_buffer for SYM's objfile; right now we never release
15689 that buffer, but when we do clean up properly this may
15690 need to change. */
15691 baton->size = DW_BLOCK (attr)->size;
15692 baton->data = DW_BLOCK (attr)->data;
15693 }
15694 else
15695 {
15696 dwarf2_invalid_attrib_class_complaint ("location description",
15697 SYMBOL_NATURAL_NAME (sym));
15698 baton->size = 0;
15699 }
15700
15701 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15702 SYMBOL_LOCATION_BATON (sym) = baton;
15703 }
15704 }
15705
15706 /* Return the OBJFILE associated with the compilation unit CU. If CU
15707 came from a separate debuginfo file, then the master objfile is
15708 returned. */
15709
15710 struct objfile *
15711 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15712 {
15713 struct objfile *objfile = per_cu->objfile;
15714
15715 /* Return the master objfile, so that we can report and look up the
15716 correct file containing this variable. */
15717 if (objfile->separate_debug_objfile_backlink)
15718 objfile = objfile->separate_debug_objfile_backlink;
15719
15720 return objfile;
15721 }
15722
15723 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15724 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15725 CU_HEADERP first. */
15726
15727 static const struct comp_unit_head *
15728 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15729 struct dwarf2_per_cu_data *per_cu)
15730 {
15731 struct objfile *objfile;
15732 struct dwarf2_per_objfile *per_objfile;
15733 gdb_byte *info_ptr;
15734
15735 if (per_cu->cu)
15736 return &per_cu->cu->header;
15737
15738 objfile = per_cu->objfile;
15739 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15740 info_ptr = per_objfile->info.buffer + per_cu->offset;
15741
15742 memset (cu_headerp, 0, sizeof (*cu_headerp));
15743 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15744
15745 return cu_headerp;
15746 }
15747
15748 /* Return the address size given in the compilation unit header for CU. */
15749
15750 int
15751 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15752 {
15753 struct comp_unit_head cu_header_local;
15754 const struct comp_unit_head *cu_headerp;
15755
15756 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15757
15758 return cu_headerp->addr_size;
15759 }
15760
15761 /* Return the offset size given in the compilation unit header for CU. */
15762
15763 int
15764 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15765 {
15766 struct comp_unit_head cu_header_local;
15767 const struct comp_unit_head *cu_headerp;
15768
15769 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15770
15771 return cu_headerp->offset_size;
15772 }
15773
15774 /* See its dwarf2loc.h declaration. */
15775
15776 int
15777 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15778 {
15779 struct comp_unit_head cu_header_local;
15780 const struct comp_unit_head *cu_headerp;
15781
15782 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15783
15784 if (cu_headerp->version == 2)
15785 return cu_headerp->addr_size;
15786 else
15787 return cu_headerp->offset_size;
15788 }
15789
15790 /* Return the text offset of the CU. The returned offset comes from
15791 this CU's objfile. If this objfile came from a separate debuginfo
15792 file, then the offset may be different from the corresponding
15793 offset in the parent objfile. */
15794
15795 CORE_ADDR
15796 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15797 {
15798 struct objfile *objfile = per_cu->objfile;
15799
15800 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15801 }
15802
15803 /* Locate the .debug_info compilation unit from CU's objfile which contains
15804 the DIE at OFFSET. Raises an error on failure. */
15805
15806 static struct dwarf2_per_cu_data *
15807 dwarf2_find_containing_comp_unit (unsigned int offset,
15808 struct objfile *objfile)
15809 {
15810 struct dwarf2_per_cu_data *this_cu;
15811 int low, high;
15812
15813 low = 0;
15814 high = dwarf2_per_objfile->n_comp_units - 1;
15815 while (high > low)
15816 {
15817 int mid = low + (high - low) / 2;
15818
15819 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15820 high = mid;
15821 else
15822 low = mid + 1;
15823 }
15824 gdb_assert (low == high);
15825 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15826 {
15827 if (low == 0)
15828 error (_("Dwarf Error: could not find partial DIE containing "
15829 "offset 0x%lx [in module %s]"),
15830 (long) offset, bfd_get_filename (objfile->obfd));
15831
15832 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15833 return dwarf2_per_objfile->all_comp_units[low-1];
15834 }
15835 else
15836 {
15837 this_cu = dwarf2_per_objfile->all_comp_units[low];
15838 if (low == dwarf2_per_objfile->n_comp_units - 1
15839 && offset >= this_cu->offset + this_cu->length)
15840 error (_("invalid dwarf2 offset %u"), offset);
15841 gdb_assert (offset < this_cu->offset + this_cu->length);
15842 return this_cu;
15843 }
15844 }
15845
15846 /* Initialize dwarf2_cu CU, owned by PER_CU. */
15847
15848 static void
15849 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15850 {
15851 memset (cu, 0, sizeof (*cu));
15852 per_cu->cu = cu;
15853 cu->per_cu = per_cu;
15854 cu->objfile = per_cu->objfile;
15855 obstack_init (&cu->comp_unit_obstack);
15856 }
15857
15858 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15859
15860 static void
15861 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15862 {
15863 struct attribute *attr;
15864
15865 /* Set the language we're debugging. */
15866 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15867 if (attr)
15868 set_cu_language (DW_UNSND (attr), cu);
15869 else
15870 {
15871 cu->language = language_minimal;
15872 cu->language_defn = language_def (cu->language);
15873 }
15874 }
15875
15876 /* Release one cached compilation unit, CU. We unlink it from the tree
15877 of compilation units, but we don't remove it from the read_in_chain;
15878 the caller is responsible for that.
15879 NOTE: DATA is a void * because this function is also used as a
15880 cleanup routine. */
15881
15882 static void
15883 free_heap_comp_unit (void *data)
15884 {
15885 struct dwarf2_cu *cu = data;
15886
15887 gdb_assert (cu->per_cu != NULL);
15888 cu->per_cu->cu = NULL;
15889 cu->per_cu = NULL;
15890
15891 obstack_free (&cu->comp_unit_obstack, NULL);
15892
15893 xfree (cu);
15894 }
15895
15896 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15897 when we're finished with it. We can't free the pointer itself, but be
15898 sure to unlink it from the cache. Also release any associated storage
15899 and perform cache maintenance.
15900
15901 Only used during partial symbol parsing. */
15902
15903 static void
15904 free_stack_comp_unit (void *data)
15905 {
15906 struct dwarf2_cu *cu = data;
15907
15908 gdb_assert (cu->per_cu != NULL);
15909 cu->per_cu->cu = NULL;
15910 cu->per_cu = NULL;
15911
15912 obstack_free (&cu->comp_unit_obstack, NULL);
15913 cu->partial_dies = NULL;
15914
15915 /* The previous code only did this if per_cu != NULL.
15916 But that would always succeed, so now we just unconditionally do
15917 the aging. This seems like the wrong place to do such aging,
15918 but cleaning that up is left for later. */
15919 age_cached_comp_units ();
15920 }
15921
15922 /* Free all cached compilation units. */
15923
15924 static void
15925 free_cached_comp_units (void *data)
15926 {
15927 struct dwarf2_per_cu_data *per_cu, **last_chain;
15928
15929 per_cu = dwarf2_per_objfile->read_in_chain;
15930 last_chain = &dwarf2_per_objfile->read_in_chain;
15931 while (per_cu != NULL)
15932 {
15933 struct dwarf2_per_cu_data *next_cu;
15934
15935 next_cu = per_cu->cu->read_in_chain;
15936
15937 free_heap_comp_unit (per_cu->cu);
15938 *last_chain = next_cu;
15939
15940 per_cu = next_cu;
15941 }
15942 }
15943
15944 /* Increase the age counter on each cached compilation unit, and free
15945 any that are too old. */
15946
15947 static void
15948 age_cached_comp_units (void)
15949 {
15950 struct dwarf2_per_cu_data *per_cu, **last_chain;
15951
15952 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15953 per_cu = dwarf2_per_objfile->read_in_chain;
15954 while (per_cu != NULL)
15955 {
15956 per_cu->cu->last_used ++;
15957 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15958 dwarf2_mark (per_cu->cu);
15959 per_cu = per_cu->cu->read_in_chain;
15960 }
15961
15962 per_cu = dwarf2_per_objfile->read_in_chain;
15963 last_chain = &dwarf2_per_objfile->read_in_chain;
15964 while (per_cu != NULL)
15965 {
15966 struct dwarf2_per_cu_data *next_cu;
15967
15968 next_cu = per_cu->cu->read_in_chain;
15969
15970 if (!per_cu->cu->mark)
15971 {
15972 free_heap_comp_unit (per_cu->cu);
15973 *last_chain = next_cu;
15974 }
15975 else
15976 last_chain = &per_cu->cu->read_in_chain;
15977
15978 per_cu = next_cu;
15979 }
15980 }
15981
15982 /* Remove a single compilation unit from the cache. */
15983
15984 static void
15985 free_one_cached_comp_unit (void *target_cu)
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 if (per_cu->cu == target_cu)
15998 {
15999 free_heap_comp_unit (per_cu->cu);
16000 *last_chain = next_cu;
16001 break;
16002 }
16003 else
16004 last_chain = &per_cu->cu->read_in_chain;
16005
16006 per_cu = next_cu;
16007 }
16008 }
16009
16010 /* Release all extra memory associated with OBJFILE. */
16011
16012 void
16013 dwarf2_free_objfile (struct objfile *objfile)
16014 {
16015 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16016
16017 if (dwarf2_per_objfile == NULL)
16018 return;
16019
16020 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16021 free_cached_comp_units (NULL);
16022
16023 if (dwarf2_per_objfile->quick_file_names_table)
16024 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16025
16026 /* Everything else should be on the objfile obstack. */
16027 }
16028
16029 /* A pair of DIE offset and GDB type pointer. We store these
16030 in a hash table separate from the DIEs, and preserve them
16031 when the DIEs are flushed out of cache. */
16032
16033 struct dwarf2_offset_and_type
16034 {
16035 unsigned int offset;
16036 struct type *type;
16037 };
16038
16039 /* Hash function for a dwarf2_offset_and_type. */
16040
16041 static hashval_t
16042 offset_and_type_hash (const void *item)
16043 {
16044 const struct dwarf2_offset_and_type *ofs = item;
16045
16046 return ofs->offset;
16047 }
16048
16049 /* Equality function for a dwarf2_offset_and_type. */
16050
16051 static int
16052 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16053 {
16054 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16055 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16056
16057 return ofs_lhs->offset == ofs_rhs->offset;
16058 }
16059
16060 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16061 table if necessary. For convenience, return TYPE.
16062
16063 The DIEs reading must have careful ordering to:
16064 * Not cause infite loops trying to read in DIEs as a prerequisite for
16065 reading current DIE.
16066 * Not trying to dereference contents of still incompletely read in types
16067 while reading in other DIEs.
16068 * Enable referencing still incompletely read in types just by a pointer to
16069 the type without accessing its fields.
16070
16071 Therefore caller should follow these rules:
16072 * Try to fetch any prerequisite types we may need to build this DIE type
16073 before building the type and calling set_die_type.
16074 * After building type call set_die_type for current DIE as soon as
16075 possible before fetching more types to complete the current type.
16076 * Make the type as complete as possible before fetching more types. */
16077
16078 static struct type *
16079 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16080 {
16081 struct dwarf2_offset_and_type **slot, ofs;
16082 struct objfile *objfile = cu->objfile;
16083 htab_t *type_hash_ptr;
16084
16085 /* For Ada types, make sure that the gnat-specific data is always
16086 initialized (if not already set). There are a few types where
16087 we should not be doing so, because the type-specific area is
16088 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16089 where the type-specific area is used to store the floatformat).
16090 But this is not a problem, because the gnat-specific information
16091 is actually not needed for these types. */
16092 if (need_gnat_info (cu)
16093 && TYPE_CODE (type) != TYPE_CODE_FUNC
16094 && TYPE_CODE (type) != TYPE_CODE_FLT
16095 && !HAVE_GNAT_AUX_INFO (type))
16096 INIT_GNAT_SPECIFIC (type);
16097
16098 if (cu->per_cu->debug_types_section)
16099 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16100 else
16101 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16102
16103 if (*type_hash_ptr == NULL)
16104 {
16105 *type_hash_ptr
16106 = htab_create_alloc_ex (127,
16107 offset_and_type_hash,
16108 offset_and_type_eq,
16109 NULL,
16110 &objfile->objfile_obstack,
16111 hashtab_obstack_allocate,
16112 dummy_obstack_deallocate);
16113 }
16114
16115 ofs.offset = die->offset;
16116 ofs.type = type;
16117 slot = (struct dwarf2_offset_and_type **)
16118 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16119 if (*slot)
16120 complaint (&symfile_complaints,
16121 _("A problem internal to GDB: DIE 0x%x has type already set"),
16122 die->offset);
16123 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16124 **slot = ofs;
16125 return type;
16126 }
16127
16128 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16129 table, or return NULL if the die does not have a saved type. */
16130
16131 static struct type *
16132 get_die_type_at_offset (unsigned int offset,
16133 struct dwarf2_per_cu_data *per_cu)
16134 {
16135 struct dwarf2_offset_and_type *slot, ofs;
16136 htab_t type_hash;
16137
16138 if (per_cu->debug_types_section)
16139 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16140 else
16141 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16142 if (type_hash == NULL)
16143 return NULL;
16144
16145 ofs.offset = offset;
16146 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16147 if (slot)
16148 return slot->type;
16149 else
16150 return NULL;
16151 }
16152
16153 /* Look up the type for DIE in the appropriate type_hash table,
16154 or return NULL if DIE does not have a saved type. */
16155
16156 static struct type *
16157 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16158 {
16159 return get_die_type_at_offset (die->offset, cu->per_cu);
16160 }
16161
16162 /* Add a dependence relationship from CU to REF_PER_CU. */
16163
16164 static void
16165 dwarf2_add_dependence (struct dwarf2_cu *cu,
16166 struct dwarf2_per_cu_data *ref_per_cu)
16167 {
16168 void **slot;
16169
16170 if (cu->dependencies == NULL)
16171 cu->dependencies
16172 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16173 NULL, &cu->comp_unit_obstack,
16174 hashtab_obstack_allocate,
16175 dummy_obstack_deallocate);
16176
16177 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16178 if (*slot == NULL)
16179 *slot = ref_per_cu;
16180 }
16181
16182 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16183 Set the mark field in every compilation unit in the
16184 cache that we must keep because we are keeping CU. */
16185
16186 static int
16187 dwarf2_mark_helper (void **slot, void *data)
16188 {
16189 struct dwarf2_per_cu_data *per_cu;
16190
16191 per_cu = (struct dwarf2_per_cu_data *) *slot;
16192
16193 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16194 reading of the chain. As such dependencies remain valid it is not much
16195 useful to track and undo them during QUIT cleanups. */
16196 if (per_cu->cu == NULL)
16197 return 1;
16198
16199 if (per_cu->cu->mark)
16200 return 1;
16201 per_cu->cu->mark = 1;
16202
16203 if (per_cu->cu->dependencies != NULL)
16204 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16205
16206 return 1;
16207 }
16208
16209 /* Set the mark field in CU and in every other compilation unit in the
16210 cache that we must keep because we are keeping CU. */
16211
16212 static void
16213 dwarf2_mark (struct dwarf2_cu *cu)
16214 {
16215 if (cu->mark)
16216 return;
16217 cu->mark = 1;
16218 if (cu->dependencies != NULL)
16219 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16220 }
16221
16222 static void
16223 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16224 {
16225 while (per_cu)
16226 {
16227 per_cu->cu->mark = 0;
16228 per_cu = per_cu->cu->read_in_chain;
16229 }
16230 }
16231
16232 /* Trivial hash function for partial_die_info: the hash value of a DIE
16233 is its offset in .debug_info for this objfile. */
16234
16235 static hashval_t
16236 partial_die_hash (const void *item)
16237 {
16238 const struct partial_die_info *part_die = item;
16239
16240 return part_die->offset;
16241 }
16242
16243 /* Trivial comparison function for partial_die_info structures: two DIEs
16244 are equal if they have the same offset. */
16245
16246 static int
16247 partial_die_eq (const void *item_lhs, const void *item_rhs)
16248 {
16249 const struct partial_die_info *part_die_lhs = item_lhs;
16250 const struct partial_die_info *part_die_rhs = item_rhs;
16251
16252 return part_die_lhs->offset == part_die_rhs->offset;
16253 }
16254
16255 static struct cmd_list_element *set_dwarf2_cmdlist;
16256 static struct cmd_list_element *show_dwarf2_cmdlist;
16257
16258 static void
16259 set_dwarf2_cmd (char *args, int from_tty)
16260 {
16261 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16262 }
16263
16264 static void
16265 show_dwarf2_cmd (char *args, int from_tty)
16266 {
16267 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16268 }
16269
16270 /* If section described by INFO was mmapped, munmap it now. */
16271
16272 static void
16273 munmap_section_buffer (struct dwarf2_section_info *info)
16274 {
16275 if (info->map_addr != NULL)
16276 {
16277 #ifdef HAVE_MMAP
16278 int res;
16279
16280 res = munmap (info->map_addr, info->map_len);
16281 gdb_assert (res == 0);
16282 #else
16283 /* Without HAVE_MMAP, we should never be here to begin with. */
16284 gdb_assert_not_reached ("no mmap support");
16285 #endif
16286 }
16287 }
16288
16289 /* munmap debug sections for OBJFILE, if necessary. */
16290
16291 static void
16292 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16293 {
16294 struct dwarf2_per_objfile *data = d;
16295 int ix;
16296 struct dwarf2_section_info *section;
16297
16298 /* This is sorted according to the order they're defined in to make it easier
16299 to keep in sync. */
16300 munmap_section_buffer (&data->info);
16301 munmap_section_buffer (&data->abbrev);
16302 munmap_section_buffer (&data->line);
16303 munmap_section_buffer (&data->loc);
16304 munmap_section_buffer (&data->macinfo);
16305 munmap_section_buffer (&data->macro);
16306 munmap_section_buffer (&data->str);
16307 munmap_section_buffer (&data->ranges);
16308 munmap_section_buffer (&data->frame);
16309 munmap_section_buffer (&data->eh_frame);
16310 munmap_section_buffer (&data->gdb_index);
16311
16312 for (ix = 0;
16313 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16314 ++ix)
16315 munmap_section_buffer (section);
16316
16317 VEC_free (dwarf2_section_info_def, data->types);
16318 }
16319
16320 \f
16321 /* The "save gdb-index" command. */
16322
16323 /* The contents of the hash table we create when building the string
16324 table. */
16325 struct strtab_entry
16326 {
16327 offset_type offset;
16328 const char *str;
16329 };
16330
16331 /* Hash function for a strtab_entry.
16332
16333 Function is used only during write_hash_table so no index format backward
16334 compatibility is needed. */
16335
16336 static hashval_t
16337 hash_strtab_entry (const void *e)
16338 {
16339 const struct strtab_entry *entry = e;
16340 return mapped_index_string_hash (INT_MAX, entry->str);
16341 }
16342
16343 /* Equality function for a strtab_entry. */
16344
16345 static int
16346 eq_strtab_entry (const void *a, const void *b)
16347 {
16348 const struct strtab_entry *ea = a;
16349 const struct strtab_entry *eb = b;
16350 return !strcmp (ea->str, eb->str);
16351 }
16352
16353 /* Create a strtab_entry hash table. */
16354
16355 static htab_t
16356 create_strtab (void)
16357 {
16358 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16359 xfree, xcalloc, xfree);
16360 }
16361
16362 /* Add a string to the constant pool. Return the string's offset in
16363 host order. */
16364
16365 static offset_type
16366 add_string (htab_t table, struct obstack *cpool, const char *str)
16367 {
16368 void **slot;
16369 struct strtab_entry entry;
16370 struct strtab_entry *result;
16371
16372 entry.str = str;
16373 slot = htab_find_slot (table, &entry, INSERT);
16374 if (*slot)
16375 result = *slot;
16376 else
16377 {
16378 result = XNEW (struct strtab_entry);
16379 result->offset = obstack_object_size (cpool);
16380 result->str = str;
16381 obstack_grow_str0 (cpool, str);
16382 *slot = result;
16383 }
16384 return result->offset;
16385 }
16386
16387 /* An entry in the symbol table. */
16388 struct symtab_index_entry
16389 {
16390 /* The name of the symbol. */
16391 const char *name;
16392 /* The offset of the name in the constant pool. */
16393 offset_type index_offset;
16394 /* A sorted vector of the indices of all the CUs that hold an object
16395 of this name. */
16396 VEC (offset_type) *cu_indices;
16397 };
16398
16399 /* The symbol table. This is a power-of-2-sized hash table. */
16400 struct mapped_symtab
16401 {
16402 offset_type n_elements;
16403 offset_type size;
16404 struct symtab_index_entry **data;
16405 };
16406
16407 /* Hash function for a symtab_index_entry. */
16408
16409 static hashval_t
16410 hash_symtab_entry (const void *e)
16411 {
16412 const struct symtab_index_entry *entry = e;
16413 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16414 sizeof (offset_type) * VEC_length (offset_type,
16415 entry->cu_indices),
16416 0);
16417 }
16418
16419 /* Equality function for a symtab_index_entry. */
16420
16421 static int
16422 eq_symtab_entry (const void *a, const void *b)
16423 {
16424 const struct symtab_index_entry *ea = a;
16425 const struct symtab_index_entry *eb = b;
16426 int len = VEC_length (offset_type, ea->cu_indices);
16427 if (len != VEC_length (offset_type, eb->cu_indices))
16428 return 0;
16429 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16430 VEC_address (offset_type, eb->cu_indices),
16431 sizeof (offset_type) * len);
16432 }
16433
16434 /* Destroy a symtab_index_entry. */
16435
16436 static void
16437 delete_symtab_entry (void *p)
16438 {
16439 struct symtab_index_entry *entry = p;
16440 VEC_free (offset_type, entry->cu_indices);
16441 xfree (entry);
16442 }
16443
16444 /* Create a hash table holding symtab_index_entry objects. */
16445
16446 static htab_t
16447 create_symbol_hash_table (void)
16448 {
16449 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16450 delete_symtab_entry, xcalloc, xfree);
16451 }
16452
16453 /* Create a new mapped symtab object. */
16454
16455 static struct mapped_symtab *
16456 create_mapped_symtab (void)
16457 {
16458 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16459 symtab->n_elements = 0;
16460 symtab->size = 1024;
16461 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16462 return symtab;
16463 }
16464
16465 /* Destroy a mapped_symtab. */
16466
16467 static void
16468 cleanup_mapped_symtab (void *p)
16469 {
16470 struct mapped_symtab *symtab = p;
16471 /* The contents of the array are freed when the other hash table is
16472 destroyed. */
16473 xfree (symtab->data);
16474 xfree (symtab);
16475 }
16476
16477 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16478 the slot.
16479
16480 Function is used only during write_hash_table so no index format backward
16481 compatibility is needed. */
16482
16483 static struct symtab_index_entry **
16484 find_slot (struct mapped_symtab *symtab, const char *name)
16485 {
16486 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16487
16488 index = hash & (symtab->size - 1);
16489 step = ((hash * 17) & (symtab->size - 1)) | 1;
16490
16491 for (;;)
16492 {
16493 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16494 return &symtab->data[index];
16495 index = (index + step) & (symtab->size - 1);
16496 }
16497 }
16498
16499 /* Expand SYMTAB's hash table. */
16500
16501 static void
16502 hash_expand (struct mapped_symtab *symtab)
16503 {
16504 offset_type old_size = symtab->size;
16505 offset_type i;
16506 struct symtab_index_entry **old_entries = symtab->data;
16507
16508 symtab->size *= 2;
16509 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16510
16511 for (i = 0; i < old_size; ++i)
16512 {
16513 if (old_entries[i])
16514 {
16515 struct symtab_index_entry **slot = find_slot (symtab,
16516 old_entries[i]->name);
16517 *slot = old_entries[i];
16518 }
16519 }
16520
16521 xfree (old_entries);
16522 }
16523
16524 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16525 is the index of the CU in which the symbol appears. */
16526
16527 static void
16528 add_index_entry (struct mapped_symtab *symtab, const char *name,
16529 offset_type cu_index)
16530 {
16531 struct symtab_index_entry **slot;
16532
16533 ++symtab->n_elements;
16534 if (4 * symtab->n_elements / 3 >= symtab->size)
16535 hash_expand (symtab);
16536
16537 slot = find_slot (symtab, name);
16538 if (!*slot)
16539 {
16540 *slot = XNEW (struct symtab_index_entry);
16541 (*slot)->name = name;
16542 (*slot)->cu_indices = NULL;
16543 }
16544 /* Don't push an index twice. Due to how we add entries we only
16545 have to check the last one. */
16546 if (VEC_empty (offset_type, (*slot)->cu_indices)
16547 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16548 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16549 }
16550
16551 /* Add a vector of indices to the constant pool. */
16552
16553 static offset_type
16554 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16555 struct symtab_index_entry *entry)
16556 {
16557 void **slot;
16558
16559 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16560 if (!*slot)
16561 {
16562 offset_type len = VEC_length (offset_type, entry->cu_indices);
16563 offset_type val = MAYBE_SWAP (len);
16564 offset_type iter;
16565 int i;
16566
16567 *slot = entry;
16568 entry->index_offset = obstack_object_size (cpool);
16569
16570 obstack_grow (cpool, &val, sizeof (val));
16571 for (i = 0;
16572 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16573 ++i)
16574 {
16575 val = MAYBE_SWAP (iter);
16576 obstack_grow (cpool, &val, sizeof (val));
16577 }
16578 }
16579 else
16580 {
16581 struct symtab_index_entry *old_entry = *slot;
16582 entry->index_offset = old_entry->index_offset;
16583 entry = old_entry;
16584 }
16585 return entry->index_offset;
16586 }
16587
16588 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16589 constant pool entries going into the obstack CPOOL. */
16590
16591 static void
16592 write_hash_table (struct mapped_symtab *symtab,
16593 struct obstack *output, struct obstack *cpool)
16594 {
16595 offset_type i;
16596 htab_t symbol_hash_table;
16597 htab_t str_table;
16598
16599 symbol_hash_table = create_symbol_hash_table ();
16600 str_table = create_strtab ();
16601
16602 /* We add all the index vectors to the constant pool first, to
16603 ensure alignment is ok. */
16604 for (i = 0; i < symtab->size; ++i)
16605 {
16606 if (symtab->data[i])
16607 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16608 }
16609
16610 /* Now write out the hash table. */
16611 for (i = 0; i < symtab->size; ++i)
16612 {
16613 offset_type str_off, vec_off;
16614
16615 if (symtab->data[i])
16616 {
16617 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16618 vec_off = symtab->data[i]->index_offset;
16619 }
16620 else
16621 {
16622 /* While 0 is a valid constant pool index, it is not valid
16623 to have 0 for both offsets. */
16624 str_off = 0;
16625 vec_off = 0;
16626 }
16627
16628 str_off = MAYBE_SWAP (str_off);
16629 vec_off = MAYBE_SWAP (vec_off);
16630
16631 obstack_grow (output, &str_off, sizeof (str_off));
16632 obstack_grow (output, &vec_off, sizeof (vec_off));
16633 }
16634
16635 htab_delete (str_table);
16636 htab_delete (symbol_hash_table);
16637 }
16638
16639 /* Struct to map psymtab to CU index in the index file. */
16640 struct psymtab_cu_index_map
16641 {
16642 struct partial_symtab *psymtab;
16643 unsigned int cu_index;
16644 };
16645
16646 static hashval_t
16647 hash_psymtab_cu_index (const void *item)
16648 {
16649 const struct psymtab_cu_index_map *map = item;
16650
16651 return htab_hash_pointer (map->psymtab);
16652 }
16653
16654 static int
16655 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16656 {
16657 const struct psymtab_cu_index_map *lhs = item_lhs;
16658 const struct psymtab_cu_index_map *rhs = item_rhs;
16659
16660 return lhs->psymtab == rhs->psymtab;
16661 }
16662
16663 /* Helper struct for building the address table. */
16664 struct addrmap_index_data
16665 {
16666 struct objfile *objfile;
16667 struct obstack *addr_obstack;
16668 htab_t cu_index_htab;
16669
16670 /* Non-zero if the previous_* fields are valid.
16671 We can't write an entry until we see the next entry (since it is only then
16672 that we know the end of the entry). */
16673 int previous_valid;
16674 /* Index of the CU in the table of all CUs in the index file. */
16675 unsigned int previous_cu_index;
16676 /* Start address of the CU. */
16677 CORE_ADDR previous_cu_start;
16678 };
16679
16680 /* Write an address entry to OBSTACK. */
16681
16682 static void
16683 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16684 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16685 {
16686 offset_type cu_index_to_write;
16687 char addr[8];
16688 CORE_ADDR baseaddr;
16689
16690 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16691
16692 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16693 obstack_grow (obstack, addr, 8);
16694 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16695 obstack_grow (obstack, addr, 8);
16696 cu_index_to_write = MAYBE_SWAP (cu_index);
16697 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16698 }
16699
16700 /* Worker function for traversing an addrmap to build the address table. */
16701
16702 static int
16703 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16704 {
16705 struct addrmap_index_data *data = datap;
16706 struct partial_symtab *pst = obj;
16707 offset_type cu_index;
16708 void **slot;
16709
16710 if (data->previous_valid)
16711 add_address_entry (data->objfile, data->addr_obstack,
16712 data->previous_cu_start, start_addr,
16713 data->previous_cu_index);
16714
16715 data->previous_cu_start = start_addr;
16716 if (pst != NULL)
16717 {
16718 struct psymtab_cu_index_map find_map, *map;
16719 find_map.psymtab = pst;
16720 map = htab_find (data->cu_index_htab, &find_map);
16721 gdb_assert (map != NULL);
16722 data->previous_cu_index = map->cu_index;
16723 data->previous_valid = 1;
16724 }
16725 else
16726 data->previous_valid = 0;
16727
16728 return 0;
16729 }
16730
16731 /* Write OBJFILE's address map to OBSTACK.
16732 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16733 in the index file. */
16734
16735 static void
16736 write_address_map (struct objfile *objfile, struct obstack *obstack,
16737 htab_t cu_index_htab)
16738 {
16739 struct addrmap_index_data addrmap_index_data;
16740
16741 /* When writing the address table, we have to cope with the fact that
16742 the addrmap iterator only provides the start of a region; we have to
16743 wait until the next invocation to get the start of the next region. */
16744
16745 addrmap_index_data.objfile = objfile;
16746 addrmap_index_data.addr_obstack = obstack;
16747 addrmap_index_data.cu_index_htab = cu_index_htab;
16748 addrmap_index_data.previous_valid = 0;
16749
16750 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16751 &addrmap_index_data);
16752
16753 /* It's highly unlikely the last entry (end address = 0xff...ff)
16754 is valid, but we should still handle it.
16755 The end address is recorded as the start of the next region, but that
16756 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16757 anyway. */
16758 if (addrmap_index_data.previous_valid)
16759 add_address_entry (objfile, obstack,
16760 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16761 addrmap_index_data.previous_cu_index);
16762 }
16763
16764 /* Add a list of partial symbols to SYMTAB. */
16765
16766 static void
16767 write_psymbols (struct mapped_symtab *symtab,
16768 htab_t psyms_seen,
16769 struct partial_symbol **psymp,
16770 int count,
16771 offset_type cu_index,
16772 int is_static)
16773 {
16774 for (; count-- > 0; ++psymp)
16775 {
16776 void **slot, *lookup;
16777
16778 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16779 error (_("Ada is not currently supported by the index"));
16780
16781 /* We only want to add a given psymbol once. However, we also
16782 want to account for whether it is global or static. So, we
16783 may add it twice, using slightly different values. */
16784 if (is_static)
16785 {
16786 uintptr_t val = 1 | (uintptr_t) *psymp;
16787
16788 lookup = (void *) val;
16789 }
16790 else
16791 lookup = *psymp;
16792
16793 /* Only add a given psymbol once. */
16794 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16795 if (!*slot)
16796 {
16797 *slot = lookup;
16798 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16799 }
16800 }
16801 }
16802
16803 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16804 exception if there is an error. */
16805
16806 static void
16807 write_obstack (FILE *file, struct obstack *obstack)
16808 {
16809 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16810 file)
16811 != obstack_object_size (obstack))
16812 error (_("couldn't data write to file"));
16813 }
16814
16815 /* Unlink a file if the argument is not NULL. */
16816
16817 static void
16818 unlink_if_set (void *p)
16819 {
16820 char **filename = p;
16821 if (*filename)
16822 unlink (*filename);
16823 }
16824
16825 /* A helper struct used when iterating over debug_types. */
16826 struct signatured_type_index_data
16827 {
16828 struct objfile *objfile;
16829 struct mapped_symtab *symtab;
16830 struct obstack *types_list;
16831 htab_t psyms_seen;
16832 int cu_index;
16833 };
16834
16835 /* A helper function that writes a single signatured_type to an
16836 obstack. */
16837
16838 static int
16839 write_one_signatured_type (void **slot, void *d)
16840 {
16841 struct signatured_type_index_data *info = d;
16842 struct signatured_type *entry = (struct signatured_type *) *slot;
16843 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16844 struct partial_symtab *psymtab = per_cu->v.psymtab;
16845 gdb_byte val[8];
16846
16847 write_psymbols (info->symtab,
16848 info->psyms_seen,
16849 info->objfile->global_psymbols.list
16850 + psymtab->globals_offset,
16851 psymtab->n_global_syms, info->cu_index,
16852 0);
16853 write_psymbols (info->symtab,
16854 info->psyms_seen,
16855 info->objfile->static_psymbols.list
16856 + psymtab->statics_offset,
16857 psymtab->n_static_syms, info->cu_index,
16858 1);
16859
16860 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16861 obstack_grow (info->types_list, val, 8);
16862 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16863 obstack_grow (info->types_list, val, 8);
16864 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16865 obstack_grow (info->types_list, val, 8);
16866
16867 ++info->cu_index;
16868
16869 return 1;
16870 }
16871
16872 /* Create an index file for OBJFILE in the directory DIR. */
16873
16874 static void
16875 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16876 {
16877 struct cleanup *cleanup;
16878 char *filename, *cleanup_filename;
16879 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16880 struct obstack cu_list, types_cu_list;
16881 int i;
16882 FILE *out_file;
16883 struct mapped_symtab *symtab;
16884 offset_type val, size_of_contents, total_len;
16885 struct stat st;
16886 char buf[8];
16887 htab_t psyms_seen;
16888 htab_t cu_index_htab;
16889 struct psymtab_cu_index_map *psymtab_cu_index_map;
16890
16891 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16892 return;
16893
16894 if (dwarf2_per_objfile->using_index)
16895 error (_("Cannot use an index to create the index"));
16896
16897 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16898 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16899
16900 if (stat (objfile->name, &st) < 0)
16901 perror_with_name (objfile->name);
16902
16903 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16904 INDEX_SUFFIX, (char *) NULL);
16905 cleanup = make_cleanup (xfree, filename);
16906
16907 out_file = fopen (filename, "wb");
16908 if (!out_file)
16909 error (_("Can't open `%s' for writing"), filename);
16910
16911 cleanup_filename = filename;
16912 make_cleanup (unlink_if_set, &cleanup_filename);
16913
16914 symtab = create_mapped_symtab ();
16915 make_cleanup (cleanup_mapped_symtab, symtab);
16916
16917 obstack_init (&addr_obstack);
16918 make_cleanup_obstack_free (&addr_obstack);
16919
16920 obstack_init (&cu_list);
16921 make_cleanup_obstack_free (&cu_list);
16922
16923 obstack_init (&types_cu_list);
16924 make_cleanup_obstack_free (&types_cu_list);
16925
16926 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16927 NULL, xcalloc, xfree);
16928 make_cleanup_htab_delete (psyms_seen);
16929
16930 /* While we're scanning CU's create a table that maps a psymtab pointer
16931 (which is what addrmap records) to its index (which is what is recorded
16932 in the index file). This will later be needed to write the address
16933 table. */
16934 cu_index_htab = htab_create_alloc (100,
16935 hash_psymtab_cu_index,
16936 eq_psymtab_cu_index,
16937 NULL, xcalloc, xfree);
16938 make_cleanup_htab_delete (cu_index_htab);
16939 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16940 xmalloc (sizeof (struct psymtab_cu_index_map)
16941 * dwarf2_per_objfile->n_comp_units);
16942 make_cleanup (xfree, psymtab_cu_index_map);
16943
16944 /* The CU list is already sorted, so we don't need to do additional
16945 work here. Also, the debug_types entries do not appear in
16946 all_comp_units, but only in their own hash table. */
16947 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16948 {
16949 struct dwarf2_per_cu_data *per_cu
16950 = dwarf2_per_objfile->all_comp_units[i];
16951 struct partial_symtab *psymtab = per_cu->v.psymtab;
16952 gdb_byte val[8];
16953 struct psymtab_cu_index_map *map;
16954 void **slot;
16955
16956 write_psymbols (symtab,
16957 psyms_seen,
16958 objfile->global_psymbols.list + psymtab->globals_offset,
16959 psymtab->n_global_syms, i,
16960 0);
16961 write_psymbols (symtab,
16962 psyms_seen,
16963 objfile->static_psymbols.list + psymtab->statics_offset,
16964 psymtab->n_static_syms, i,
16965 1);
16966
16967 map = &psymtab_cu_index_map[i];
16968 map->psymtab = psymtab;
16969 map->cu_index = i;
16970 slot = htab_find_slot (cu_index_htab, map, INSERT);
16971 gdb_assert (slot != NULL);
16972 gdb_assert (*slot == NULL);
16973 *slot = map;
16974
16975 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16976 obstack_grow (&cu_list, val, 8);
16977 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16978 obstack_grow (&cu_list, val, 8);
16979 }
16980
16981 /* Dump the address map. */
16982 write_address_map (objfile, &addr_obstack, cu_index_htab);
16983
16984 /* Write out the .debug_type entries, if any. */
16985 if (dwarf2_per_objfile->signatured_types)
16986 {
16987 struct signatured_type_index_data sig_data;
16988
16989 sig_data.objfile = objfile;
16990 sig_data.symtab = symtab;
16991 sig_data.types_list = &types_cu_list;
16992 sig_data.psyms_seen = psyms_seen;
16993 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16994 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16995 write_one_signatured_type, &sig_data);
16996 }
16997
16998 obstack_init (&constant_pool);
16999 make_cleanup_obstack_free (&constant_pool);
17000 obstack_init (&symtab_obstack);
17001 make_cleanup_obstack_free (&symtab_obstack);
17002 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17003
17004 obstack_init (&contents);
17005 make_cleanup_obstack_free (&contents);
17006 size_of_contents = 6 * sizeof (offset_type);
17007 total_len = size_of_contents;
17008
17009 /* The version number. */
17010 val = MAYBE_SWAP (5);
17011 obstack_grow (&contents, &val, sizeof (val));
17012
17013 /* The offset of the CU list from the start of the file. */
17014 val = MAYBE_SWAP (total_len);
17015 obstack_grow (&contents, &val, sizeof (val));
17016 total_len += obstack_object_size (&cu_list);
17017
17018 /* The offset of the types CU list from the start of the file. */
17019 val = MAYBE_SWAP (total_len);
17020 obstack_grow (&contents, &val, sizeof (val));
17021 total_len += obstack_object_size (&types_cu_list);
17022
17023 /* The offset of the address table from the start of the file. */
17024 val = MAYBE_SWAP (total_len);
17025 obstack_grow (&contents, &val, sizeof (val));
17026 total_len += obstack_object_size (&addr_obstack);
17027
17028 /* The offset of the symbol table from the start of the file. */
17029 val = MAYBE_SWAP (total_len);
17030 obstack_grow (&contents, &val, sizeof (val));
17031 total_len += obstack_object_size (&symtab_obstack);
17032
17033 /* The offset of the constant pool from the start of the file. */
17034 val = MAYBE_SWAP (total_len);
17035 obstack_grow (&contents, &val, sizeof (val));
17036 total_len += obstack_object_size (&constant_pool);
17037
17038 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17039
17040 write_obstack (out_file, &contents);
17041 write_obstack (out_file, &cu_list);
17042 write_obstack (out_file, &types_cu_list);
17043 write_obstack (out_file, &addr_obstack);
17044 write_obstack (out_file, &symtab_obstack);
17045 write_obstack (out_file, &constant_pool);
17046
17047 fclose (out_file);
17048
17049 /* We want to keep the file, so we set cleanup_filename to NULL
17050 here. See unlink_if_set. */
17051 cleanup_filename = NULL;
17052
17053 do_cleanups (cleanup);
17054 }
17055
17056 /* Implementation of the `save gdb-index' command.
17057
17058 Note that the file format used by this command is documented in the
17059 GDB manual. Any changes here must be documented there. */
17060
17061 static void
17062 save_gdb_index_command (char *arg, int from_tty)
17063 {
17064 struct objfile *objfile;
17065
17066 if (!arg || !*arg)
17067 error (_("usage: save gdb-index DIRECTORY"));
17068
17069 ALL_OBJFILES (objfile)
17070 {
17071 struct stat st;
17072
17073 /* If the objfile does not correspond to an actual file, skip it. */
17074 if (stat (objfile->name, &st) < 0)
17075 continue;
17076
17077 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17078 if (dwarf2_per_objfile)
17079 {
17080 volatile struct gdb_exception except;
17081
17082 TRY_CATCH (except, RETURN_MASK_ERROR)
17083 {
17084 write_psymtabs_to_index (objfile, arg);
17085 }
17086 if (except.reason < 0)
17087 exception_fprintf (gdb_stderr, except,
17088 _("Error while writing index for `%s': "),
17089 objfile->name);
17090 }
17091 }
17092 }
17093
17094 \f
17095
17096 int dwarf2_always_disassemble;
17097
17098 static void
17099 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17100 struct cmd_list_element *c, const char *value)
17101 {
17102 fprintf_filtered (file,
17103 _("Whether to always disassemble "
17104 "DWARF expressions is %s.\n"),
17105 value);
17106 }
17107
17108 static void
17109 show_check_physname (struct ui_file *file, int from_tty,
17110 struct cmd_list_element *c, const char *value)
17111 {
17112 fprintf_filtered (file,
17113 _("Whether to check \"physname\" is %s.\n"),
17114 value);
17115 }
17116
17117 void _initialize_dwarf2_read (void);
17118
17119 void
17120 _initialize_dwarf2_read (void)
17121 {
17122 struct cmd_list_element *c;
17123
17124 dwarf2_objfile_data_key
17125 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17126
17127 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17128 Set DWARF 2 specific variables.\n\
17129 Configure DWARF 2 variables such as the cache size"),
17130 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17131 0/*allow-unknown*/, &maintenance_set_cmdlist);
17132
17133 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17134 Show DWARF 2 specific variables\n\
17135 Show DWARF 2 variables such as the cache size"),
17136 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17137 0/*allow-unknown*/, &maintenance_show_cmdlist);
17138
17139 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17140 &dwarf2_max_cache_age, _("\
17141 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17142 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17143 A higher limit means that cached compilation units will be stored\n\
17144 in memory longer, and more total memory will be used. Zero disables\n\
17145 caching, which can slow down startup."),
17146 NULL,
17147 show_dwarf2_max_cache_age,
17148 &set_dwarf2_cmdlist,
17149 &show_dwarf2_cmdlist);
17150
17151 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17152 &dwarf2_always_disassemble, _("\
17153 Set whether `info address' always disassembles DWARF expressions."), _("\
17154 Show whether `info address' always disassembles DWARF expressions."), _("\
17155 When enabled, DWARF expressions are always printed in an assembly-like\n\
17156 syntax. When disabled, expressions will be printed in a more\n\
17157 conversational style, when possible."),
17158 NULL,
17159 show_dwarf2_always_disassemble,
17160 &set_dwarf2_cmdlist,
17161 &show_dwarf2_cmdlist);
17162
17163 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17164 Set debugging of the dwarf2 DIE reader."), _("\
17165 Show debugging of the dwarf2 DIE reader."), _("\
17166 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17167 The value is the maximum depth to print."),
17168 NULL,
17169 NULL,
17170 &setdebuglist, &showdebuglist);
17171
17172 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17173 Set cross-checking of \"physname\" code against demangler."), _("\
17174 Show cross-checking of \"physname\" code against demangler."), _("\
17175 When enabled, GDB's internal \"physname\" code is checked against\n\
17176 the demangler."),
17177 NULL, show_check_physname,
17178 &setdebuglist, &showdebuglist);
17179
17180 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17181 _("\
17182 Save a gdb-index file.\n\
17183 Usage: save gdb-index DIRECTORY"),
17184 &save_cmdlist);
17185 set_cmd_completer (c, filename_completer);
17186 }