* dwarf2read.c: Move FIXME from dwarf2_build_psymtabs_hard
[deliverable/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 /* A list 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_comp_units;
177
178 /* The .debug_types-related CUs. */
179 struct dwarf2_per_cu_data **type_comp_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 flag will be set if this compilation unit might include
382 inter-compilation-unit references. */
383 unsigned int has_form_ref_addr : 1;
384
385 /* This flag will be set if this compilation unit includes any
386 DW_TAG_namespace DIEs. If we know that there are explicit
387 DIEs for namespaces, we don't need to try to infer them
388 from mangled names. */
389 unsigned int has_namespace_info : 1;
390
391 /* This CU references .debug_loc. See the symtab->locations_valid field.
392 This test is imperfect as there may exist optimized debug code not using
393 any location list and still facing inlining issues if handled as
394 unoptimized code. For a future better test see GCC PR other/32998. */
395 unsigned int has_loclist : 1;
396 };
397
398 /* Persistent data held for a compilation unit, even when not
399 processing it. We put a pointer to this structure in the
400 read_symtab_private field of the psymtab. */
401
402 struct dwarf2_per_cu_data
403 {
404 /* The start offset and length of this compilation unit. 2**29-1
405 bytes should suffice to store the length of any compilation unit
406 - if it doesn't, GDB will fall over anyway.
407 NOTE: Unlike comp_unit_head.length, this length includes
408 initial_length_size. */
409 unsigned int offset;
410 unsigned int length : 29;
411
412 /* Flag indicating this compilation unit will be read in before
413 any of the current compilation units are processed. */
414 unsigned int queued : 1;
415
416 /* This flag will be set if we need to load absolutely all DIEs
417 for this compilation unit, instead of just the ones we think
418 are interesting. It gets set if we look for a DIE in the
419 hash table and don't find it. */
420 unsigned int load_all_dies : 1;
421
422 /* Non-null if this CU is from .debug_types; in which case it points
423 to the section. Otherwise it's from .debug_info. */
424 struct dwarf2_section_info *debug_types_section;
425
426 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
427 of the CU cache it gets reset to NULL again. */
428 struct dwarf2_cu *cu;
429
430 /* The corresponding objfile.
431 Normally we can get the objfile from dwarf2_per_objfile.
432 However we can enter this file with just a "per_cu" handle. */
433 struct objfile *objfile;
434
435 /* When using partial symbol tables, the 'psymtab' field is active.
436 Otherwise the 'quick' field is active. */
437 union
438 {
439 /* The partial symbol table associated with this compilation unit,
440 or NULL for partial units (which do not have an associated
441 symtab). */
442 struct partial_symtab *psymtab;
443
444 /* Data needed by the "quick" functions. */
445 struct dwarf2_per_cu_quick_data *quick;
446 } v;
447 };
448
449 /* Entry in the signatured_types hash table. */
450
451 struct signatured_type
452 {
453 ULONGEST signature;
454
455 /* Offset in .debug_types of the type defined by this TU. */
456 unsigned int type_offset;
457
458 /* The CU(/TU) of this type. */
459 struct dwarf2_per_cu_data per_cu;
460 };
461
462 /* Struct used to pass misc. parameters to read_die_and_children, et
463 al. which are used for both .debug_info and .debug_types dies.
464 All parameters here are unchanging for the life of the call. This
465 struct exists to abstract away the constant parameters of die
466 reading. */
467
468 struct die_reader_specs
469 {
470 /* The bfd of this objfile. */
471 bfd* abfd;
472
473 /* The CU of the DIE we are parsing. */
474 struct dwarf2_cu *cu;
475
476 /* Pointer to start of section buffer.
477 This is either the start of .debug_info or .debug_types. */
478 const gdb_byte *buffer;
479 };
480
481 /* The line number information for a compilation unit (found in the
482 .debug_line section) begins with a "statement program header",
483 which contains the following information. */
484 struct line_header
485 {
486 unsigned int total_length;
487 unsigned short version;
488 unsigned int header_length;
489 unsigned char minimum_instruction_length;
490 unsigned char maximum_ops_per_instruction;
491 unsigned char default_is_stmt;
492 int line_base;
493 unsigned char line_range;
494 unsigned char opcode_base;
495
496 /* standard_opcode_lengths[i] is the number of operands for the
497 standard opcode whose value is i. This means that
498 standard_opcode_lengths[0] is unused, and the last meaningful
499 element is standard_opcode_lengths[opcode_base - 1]. */
500 unsigned char *standard_opcode_lengths;
501
502 /* The include_directories table. NOTE! These strings are not
503 allocated with xmalloc; instead, they are pointers into
504 debug_line_buffer. If you try to free them, `free' will get
505 indigestion. */
506 unsigned int num_include_dirs, include_dirs_size;
507 char **include_dirs;
508
509 /* The file_names table. NOTE! These strings are not allocated
510 with xmalloc; instead, they are pointers into debug_line_buffer.
511 Don't try to free them directly. */
512 unsigned int num_file_names, file_names_size;
513 struct file_entry
514 {
515 char *name;
516 unsigned int dir_index;
517 unsigned int mod_time;
518 unsigned int length;
519 int included_p; /* Non-zero if referenced by the Line Number Program. */
520 struct symtab *symtab; /* The associated symbol table, if any. */
521 } *file_names;
522
523 /* The start and end of the statement program following this
524 header. These point into dwarf2_per_objfile->line_buffer. */
525 gdb_byte *statement_program_start, *statement_program_end;
526 };
527
528 /* When we construct a partial symbol table entry we only
529 need this much information. */
530 struct partial_die_info
531 {
532 /* Offset of this DIE. */
533 unsigned int offset;
534
535 /* DWARF-2 tag for this DIE. */
536 ENUM_BITFIELD(dwarf_tag) tag : 16;
537
538 /* Assorted flags describing the data found in this DIE. */
539 unsigned int has_children : 1;
540 unsigned int is_external : 1;
541 unsigned int is_declaration : 1;
542 unsigned int has_type : 1;
543 unsigned int has_specification : 1;
544 unsigned int has_pc_info : 1;
545
546 /* Flag set if the SCOPE field of this structure has been
547 computed. */
548 unsigned int scope_set : 1;
549
550 /* Flag set if the DIE has a byte_size attribute. */
551 unsigned int has_byte_size : 1;
552
553 /* Flag set if any of the DIE's children are template arguments. */
554 unsigned int has_template_arguments : 1;
555
556 /* Flag set if fixup_partial_die has been called on this die. */
557 unsigned int fixup_called : 1;
558
559 /* The name of this DIE. Normally the value of DW_AT_name, but
560 sometimes a default name for unnamed DIEs. */
561 char *name;
562
563 /* The linkage name, if present. */
564 const char *linkage_name;
565
566 /* The scope to prepend to our children. This is generally
567 allocated on the comp_unit_obstack, so will disappear
568 when this compilation unit leaves the cache. */
569 char *scope;
570
571 /* The location description associated with this DIE, if any. */
572 struct dwarf_block *locdesc;
573
574 /* If HAS_PC_INFO, the PC range associated with this DIE. */
575 CORE_ADDR lowpc;
576 CORE_ADDR highpc;
577
578 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
579 DW_AT_sibling, if any. */
580 /* NOTE: This member isn't strictly necessary, read_partial_die could
581 return DW_AT_sibling values to its caller load_partial_dies. */
582 gdb_byte *sibling;
583
584 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
585 DW_AT_specification (or DW_AT_abstract_origin or
586 DW_AT_extension). */
587 unsigned int spec_offset;
588
589 /* Pointers to this DIE's parent, first child, and next sibling,
590 if any. */
591 struct partial_die_info *die_parent, *die_child, *die_sibling;
592 };
593
594 /* This data structure holds the information of an abbrev. */
595 struct abbrev_info
596 {
597 unsigned int number; /* number identifying abbrev */
598 enum dwarf_tag tag; /* dwarf tag */
599 unsigned short has_children; /* boolean */
600 unsigned short num_attrs; /* number of attributes */
601 struct attr_abbrev *attrs; /* an array of attribute descriptions */
602 struct abbrev_info *next; /* next in chain */
603 };
604
605 struct attr_abbrev
606 {
607 ENUM_BITFIELD(dwarf_attribute) name : 16;
608 ENUM_BITFIELD(dwarf_form) form : 16;
609 };
610
611 /* Attributes have a name and a value. */
612 struct attribute
613 {
614 ENUM_BITFIELD(dwarf_attribute) name : 16;
615 ENUM_BITFIELD(dwarf_form) form : 15;
616
617 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
618 field should be in u.str (existing only for DW_STRING) but it is kept
619 here for better struct attribute alignment. */
620 unsigned int string_is_canonical : 1;
621
622 union
623 {
624 char *str;
625 struct dwarf_block *blk;
626 ULONGEST unsnd;
627 LONGEST snd;
628 CORE_ADDR addr;
629 struct signatured_type *signatured_type;
630 }
631 u;
632 };
633
634 /* This data structure holds a complete die structure. */
635 struct die_info
636 {
637 /* DWARF-2 tag for this DIE. */
638 ENUM_BITFIELD(dwarf_tag) tag : 16;
639
640 /* Number of attributes */
641 unsigned char num_attrs;
642
643 /* True if we're presently building the full type name for the
644 type derived from this DIE. */
645 unsigned char building_fullname : 1;
646
647 /* Abbrev number */
648 unsigned int abbrev;
649
650 /* Offset in .debug_info or .debug_types section. */
651 unsigned int offset;
652
653 /* The dies in a compilation unit form an n-ary tree. PARENT
654 points to this die's parent; CHILD points to the first child of
655 this node; and all the children of a given node are chained
656 together via their SIBLING fields. */
657 struct die_info *child; /* Its first child, if any. */
658 struct die_info *sibling; /* Its next sibling, if any. */
659 struct die_info *parent; /* Its parent, if any. */
660
661 /* An array of attributes, with NUM_ATTRS elements. There may be
662 zero, but it's not common and zero-sized arrays are not
663 sufficiently portable C. */
664 struct attribute attrs[1];
665 };
666
667 /* Get at parts of an attribute structure. */
668
669 #define DW_STRING(attr) ((attr)->u.str)
670 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
671 #define DW_UNSND(attr) ((attr)->u.unsnd)
672 #define DW_BLOCK(attr) ((attr)->u.blk)
673 #define DW_SND(attr) ((attr)->u.snd)
674 #define DW_ADDR(attr) ((attr)->u.addr)
675 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
676
677 /* Blocks are a bunch of untyped bytes. */
678 struct dwarf_block
679 {
680 unsigned int size;
681
682 /* Valid only if SIZE is not zero. */
683 gdb_byte *data;
684 };
685
686 #ifndef ATTR_ALLOC_CHUNK
687 #define ATTR_ALLOC_CHUNK 4
688 #endif
689
690 /* Allocate fields for structs, unions and enums in this size. */
691 #ifndef DW_FIELD_ALLOC_CHUNK
692 #define DW_FIELD_ALLOC_CHUNK 4
693 #endif
694
695 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
696 but this would require a corresponding change in unpack_field_as_long
697 and friends. */
698 static int bits_per_byte = 8;
699
700 /* The routines that read and process dies for a C struct or C++ class
701 pass lists of data member fields and lists of member function fields
702 in an instance of a field_info structure, as defined below. */
703 struct field_info
704 {
705 /* List of data member and baseclasses fields. */
706 struct nextfield
707 {
708 struct nextfield *next;
709 int accessibility;
710 int virtuality;
711 struct field field;
712 }
713 *fields, *baseclasses;
714
715 /* Number of fields (including baseclasses). */
716 int nfields;
717
718 /* Number of baseclasses. */
719 int nbaseclasses;
720
721 /* Set if the accesibility of one of the fields is not public. */
722 int non_public_fields;
723
724 /* Member function fields array, entries are allocated in the order they
725 are encountered in the object file. */
726 struct nextfnfield
727 {
728 struct nextfnfield *next;
729 struct fn_field fnfield;
730 }
731 *fnfields;
732
733 /* Member function fieldlist array, contains name of possibly overloaded
734 member function, number of overloaded member functions and a pointer
735 to the head of the member function field chain. */
736 struct fnfieldlist
737 {
738 char *name;
739 int length;
740 struct nextfnfield *head;
741 }
742 *fnfieldlists;
743
744 /* Number of entries in the fnfieldlists array. */
745 int nfnfields;
746
747 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
748 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
749 struct typedef_field_list
750 {
751 struct typedef_field field;
752 struct typedef_field_list *next;
753 }
754 *typedef_field_list;
755 unsigned typedef_field_list_count;
756 };
757
758 /* One item on the queue of compilation units to read in full symbols
759 for. */
760 struct dwarf2_queue_item
761 {
762 struct dwarf2_per_cu_data *per_cu;
763 struct dwarf2_queue_item *next;
764 };
765
766 /* The current queue. */
767 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
768
769 /* Loaded secondary compilation units are kept in memory until they
770 have not been referenced for the processing of this many
771 compilation units. Set this to zero to disable caching. Cache
772 sizes of up to at least twenty will improve startup time for
773 typical inter-CU-reference binaries, at an obvious memory cost. */
774 static int dwarf2_max_cache_age = 5;
775 static void
776 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
777 struct cmd_list_element *c, const char *value)
778 {
779 fprintf_filtered (file, _("The upper bound on the age of cached "
780 "dwarf2 compilation units is %s.\n"),
781 value);
782 }
783
784
785 /* Various complaints about symbol reading that don't abort the process. */
786
787 static void
788 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
789 {
790 complaint (&symfile_complaints,
791 _("statement list doesn't fit in .debug_line section"));
792 }
793
794 static void
795 dwarf2_debug_line_missing_file_complaint (void)
796 {
797 complaint (&symfile_complaints,
798 _(".debug_line section has line data without a file"));
799 }
800
801 static void
802 dwarf2_debug_line_missing_end_sequence_complaint (void)
803 {
804 complaint (&symfile_complaints,
805 _(".debug_line section has line "
806 "program sequence without an end"));
807 }
808
809 static void
810 dwarf2_complex_location_expr_complaint (void)
811 {
812 complaint (&symfile_complaints, _("location expression too complex"));
813 }
814
815 static void
816 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
817 int arg3)
818 {
819 complaint (&symfile_complaints,
820 _("const value length mismatch for '%s', got %d, expected %d"),
821 arg1, arg2, arg3);
822 }
823
824 static void
825 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
826 {
827 complaint (&symfile_complaints,
828 _("macro info runs off end of `%s' section"),
829 section->asection->name);
830 }
831
832 static void
833 dwarf2_macro_malformed_definition_complaint (const char *arg1)
834 {
835 complaint (&symfile_complaints,
836 _("macro debug info contains a "
837 "malformed macro definition:\n`%s'"),
838 arg1);
839 }
840
841 static void
842 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
843 {
844 complaint (&symfile_complaints,
845 _("invalid attribute class or form for '%s' in '%s'"),
846 arg1, arg2);
847 }
848
849 /* local function prototypes */
850
851 static void dwarf2_locate_sections (bfd *, asection *, void *);
852
853 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
854 struct objfile *);
855
856 static void dwarf2_find_base_address (struct die_info *die,
857 struct dwarf2_cu *cu);
858
859 static void dwarf2_build_psymtabs_hard (struct objfile *);
860
861 static void scan_partial_symbols (struct partial_die_info *,
862 CORE_ADDR *, CORE_ADDR *,
863 int, struct dwarf2_cu *);
864
865 static void add_partial_symbol (struct partial_die_info *,
866 struct dwarf2_cu *);
867
868 static void add_partial_namespace (struct partial_die_info *pdi,
869 CORE_ADDR *lowpc, CORE_ADDR *highpc,
870 int need_pc, struct dwarf2_cu *cu);
871
872 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
873 CORE_ADDR *highpc, int need_pc,
874 struct dwarf2_cu *cu);
875
876 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
877 struct dwarf2_cu *cu);
878
879 static void add_partial_subprogram (struct partial_die_info *pdi,
880 CORE_ADDR *lowpc, CORE_ADDR *highpc,
881 int need_pc, struct dwarf2_cu *cu);
882
883 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
884 gdb_byte *buffer, gdb_byte *info_ptr,
885 bfd *abfd, struct dwarf2_cu *cu);
886
887 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
888
889 static void psymtab_to_symtab_1 (struct partial_symtab *);
890
891 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
892
893 static void dwarf2_free_abbrev_table (void *);
894
895 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
896
897 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
898 struct dwarf2_cu *);
899
900 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
901 struct dwarf2_cu *);
902
903 static struct partial_die_info *load_partial_dies (bfd *,
904 gdb_byte *, gdb_byte *,
905 int, struct dwarf2_cu *);
906
907 static gdb_byte *read_partial_die (struct partial_die_info *,
908 struct abbrev_info *abbrev,
909 unsigned int, bfd *,
910 gdb_byte *, gdb_byte *,
911 struct dwarf2_cu *);
912
913 static struct partial_die_info *find_partial_die (unsigned int,
914 struct dwarf2_cu *);
915
916 static void fixup_partial_die (struct partial_die_info *,
917 struct dwarf2_cu *);
918
919 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
920 bfd *, gdb_byte *, struct dwarf2_cu *);
921
922 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
923 bfd *, gdb_byte *, struct dwarf2_cu *);
924
925 static unsigned int read_1_byte (bfd *, gdb_byte *);
926
927 static int read_1_signed_byte (bfd *, gdb_byte *);
928
929 static unsigned int read_2_bytes (bfd *, gdb_byte *);
930
931 static unsigned int read_4_bytes (bfd *, gdb_byte *);
932
933 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
934
935 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
936 unsigned int *);
937
938 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
939
940 static LONGEST read_checked_initial_length_and_offset
941 (bfd *, gdb_byte *, const struct comp_unit_head *,
942 unsigned int *, unsigned int *);
943
944 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
945 unsigned int *);
946
947 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
948
949 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
950
951 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
952
953 static char *read_indirect_string (bfd *, gdb_byte *,
954 const struct comp_unit_head *,
955 unsigned int *);
956
957 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
958
959 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
960
961 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
962
963 static void set_cu_language (unsigned int, struct dwarf2_cu *);
964
965 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
966 struct dwarf2_cu *);
967
968 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
969 unsigned int,
970 struct dwarf2_cu *);
971
972 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
973 struct dwarf2_cu *cu);
974
975 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
976
977 static struct die_info *die_specification (struct die_info *die,
978 struct dwarf2_cu **);
979
980 static void free_line_header (struct line_header *lh);
981
982 static void add_file_name (struct line_header *, char *, unsigned int,
983 unsigned int, unsigned int);
984
985 static struct line_header *(dwarf_decode_line_header
986 (unsigned int offset,
987 bfd *abfd, struct dwarf2_cu *cu));
988
989 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
990 struct dwarf2_cu *, struct partial_symtab *);
991
992 static void dwarf2_start_subfile (char *, const char *, const char *);
993
994 static struct symbol *new_symbol (struct die_info *, struct type *,
995 struct dwarf2_cu *);
996
997 static struct symbol *new_symbol_full (struct die_info *, struct type *,
998 struct dwarf2_cu *, struct symbol *);
999
1000 static void dwarf2_const_value (struct attribute *, struct symbol *,
1001 struct dwarf2_cu *);
1002
1003 static void dwarf2_const_value_attr (struct attribute *attr,
1004 struct type *type,
1005 const char *name,
1006 struct obstack *obstack,
1007 struct dwarf2_cu *cu, long *value,
1008 gdb_byte **bytes,
1009 struct dwarf2_locexpr_baton **baton);
1010
1011 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1012
1013 static int need_gnat_info (struct dwarf2_cu *);
1014
1015 static struct type *die_descriptive_type (struct die_info *,
1016 struct dwarf2_cu *);
1017
1018 static void set_descriptive_type (struct type *, struct die_info *,
1019 struct dwarf2_cu *);
1020
1021 static struct type *die_containing_type (struct die_info *,
1022 struct dwarf2_cu *);
1023
1024 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1025 struct dwarf2_cu *);
1026
1027 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1028
1029 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1030
1031 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1032
1033 static char *typename_concat (struct obstack *obs, const char *prefix,
1034 const char *suffix, int physname,
1035 struct dwarf2_cu *cu);
1036
1037 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1038
1039 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1040
1041 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1042
1043 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1044
1045 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1046
1047 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1048 struct dwarf2_cu *, struct partial_symtab *);
1049
1050 static int dwarf2_get_pc_bounds (struct die_info *,
1051 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1052 struct partial_symtab *);
1053
1054 static void get_scope_pc_bounds (struct die_info *,
1055 CORE_ADDR *, CORE_ADDR *,
1056 struct dwarf2_cu *);
1057
1058 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1059 CORE_ADDR, struct dwarf2_cu *);
1060
1061 static void dwarf2_add_field (struct field_info *, struct die_info *,
1062 struct dwarf2_cu *);
1063
1064 static void dwarf2_attach_fields_to_type (struct field_info *,
1065 struct type *, struct dwarf2_cu *);
1066
1067 static void dwarf2_add_member_fn (struct field_info *,
1068 struct die_info *, struct type *,
1069 struct dwarf2_cu *);
1070
1071 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1072 struct type *,
1073 struct dwarf2_cu *);
1074
1075 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1076
1077 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1078
1079 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1080
1081 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1082
1083 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1084
1085 static struct type *read_module_type (struct die_info *die,
1086 struct dwarf2_cu *cu);
1087
1088 static const char *namespace_name (struct die_info *die,
1089 int *is_anonymous, struct dwarf2_cu *);
1090
1091 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1092
1093 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1094
1095 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1096 struct dwarf2_cu *);
1097
1098 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1099
1100 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1101 gdb_byte *info_ptr,
1102 gdb_byte **new_info_ptr,
1103 struct die_info *parent);
1104
1105 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1106 gdb_byte *info_ptr,
1107 gdb_byte **new_info_ptr,
1108 struct die_info *parent);
1109
1110 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1111 gdb_byte *info_ptr,
1112 gdb_byte **new_info_ptr,
1113 struct die_info *parent);
1114
1115 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1116 struct die_info **, gdb_byte *,
1117 int *);
1118
1119 static void process_die (struct die_info *, struct dwarf2_cu *);
1120
1121 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1122 struct obstack *);
1123
1124 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1125
1126 static const char *dwarf2_full_name (char *name,
1127 struct die_info *die,
1128 struct dwarf2_cu *cu);
1129
1130 static struct die_info *dwarf2_extension (struct die_info *die,
1131 struct dwarf2_cu **);
1132
1133 static char *dwarf_tag_name (unsigned int);
1134
1135 static char *dwarf_attr_name (unsigned int);
1136
1137 static char *dwarf_form_name (unsigned int);
1138
1139 static char *dwarf_bool_name (unsigned int);
1140
1141 static char *dwarf_type_encoding_name (unsigned int);
1142
1143 #if 0
1144 static char *dwarf_cfi_name (unsigned int);
1145 #endif
1146
1147 static struct die_info *sibling_die (struct die_info *);
1148
1149 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1150
1151 static void dump_die_for_error (struct die_info *);
1152
1153 static void dump_die_1 (struct ui_file *, int level, int max_level,
1154 struct die_info *);
1155
1156 /*static*/ void dump_die (struct die_info *, int max_level);
1157
1158 static void store_in_ref_table (struct die_info *,
1159 struct dwarf2_cu *);
1160
1161 static int is_ref_attr (struct attribute *);
1162
1163 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1164
1165 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1166
1167 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1168 struct attribute *,
1169 struct dwarf2_cu **);
1170
1171 static struct die_info *follow_die_ref (struct die_info *,
1172 struct attribute *,
1173 struct dwarf2_cu **);
1174
1175 static struct die_info *follow_die_sig (struct die_info *,
1176 struct attribute *,
1177 struct dwarf2_cu **);
1178
1179 static struct signatured_type *lookup_signatured_type_at_offset
1180 (struct objfile *objfile,
1181 struct dwarf2_section_info *section,
1182 unsigned int offset);
1183
1184 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1185
1186 static void read_signatured_type (struct signatured_type *type_sig);
1187
1188 /* memory allocation interface */
1189
1190 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1191
1192 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1193
1194 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1195
1196 static void dwarf_decode_macros (struct line_header *, unsigned int,
1197 char *, bfd *, struct dwarf2_cu *,
1198 struct dwarf2_section_info *,
1199 int);
1200
1201 static int attr_form_is_block (struct attribute *);
1202
1203 static int attr_form_is_section_offset (struct attribute *);
1204
1205 static int attr_form_is_constant (struct attribute *);
1206
1207 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1208 struct dwarf2_loclist_baton *baton,
1209 struct attribute *attr);
1210
1211 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1212 struct symbol *sym,
1213 struct dwarf2_cu *cu);
1214
1215 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1216 struct abbrev_info *abbrev,
1217 struct dwarf2_cu *cu);
1218
1219 static void free_stack_comp_unit (void *);
1220
1221 static hashval_t partial_die_hash (const void *item);
1222
1223 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1224
1225 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1226 (unsigned int offset, struct objfile *objfile);
1227
1228 static void init_one_comp_unit (struct dwarf2_cu *cu,
1229 struct dwarf2_per_cu_data *per_cu);
1230
1231 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1232 struct die_info *comp_unit_die);
1233
1234 static void free_heap_comp_unit (void *);
1235
1236 static void free_cached_comp_units (void *);
1237
1238 static void age_cached_comp_units (void);
1239
1240 static void free_one_cached_comp_unit (void *);
1241
1242 static struct type *set_die_type (struct die_info *, struct type *,
1243 struct dwarf2_cu *);
1244
1245 static void create_all_comp_units (struct objfile *);
1246
1247 static int create_debug_types_hash_table (struct objfile *objfile);
1248
1249 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1250
1251 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1252
1253 static void dwarf2_add_dependence (struct dwarf2_cu *,
1254 struct dwarf2_per_cu_data *);
1255
1256 static void dwarf2_mark (struct dwarf2_cu *);
1257
1258 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1259
1260 static struct type *get_die_type_at_offset (unsigned int,
1261 struct dwarf2_per_cu_data *per_cu);
1262
1263 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1264
1265 static void dwarf2_release_queue (void *dummy);
1266
1267 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1268
1269 static void process_queue (void);
1270
1271 static void find_file_and_directory (struct die_info *die,
1272 struct dwarf2_cu *cu,
1273 char **name, char **comp_dir);
1274
1275 static char *file_full_name (int file, struct line_header *lh,
1276 const char *comp_dir);
1277
1278 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1279 gdb_byte *info_ptr,
1280 gdb_byte *buffer,
1281 unsigned int buffer_size,
1282 bfd *abfd,
1283 int is_debug_types_section);
1284
1285 static void init_cu_die_reader (struct die_reader_specs *reader,
1286 struct dwarf2_cu *cu);
1287
1288 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1289
1290 #if WORDS_BIGENDIAN
1291
1292 /* Convert VALUE between big- and little-endian. */
1293 static offset_type
1294 byte_swap (offset_type value)
1295 {
1296 offset_type result;
1297
1298 result = (value & 0xff) << 24;
1299 result |= (value & 0xff00) << 8;
1300 result |= (value & 0xff0000) >> 8;
1301 result |= (value & 0xff000000) >> 24;
1302 return result;
1303 }
1304
1305 #define MAYBE_SWAP(V) byte_swap (V)
1306
1307 #else
1308 #define MAYBE_SWAP(V) (V)
1309 #endif /* WORDS_BIGENDIAN */
1310
1311 /* The suffix for an index file. */
1312 #define INDEX_SUFFIX ".gdb-index"
1313
1314 static const char *dwarf2_physname (char *name, struct die_info *die,
1315 struct dwarf2_cu *cu);
1316
1317 /* Try to locate the sections we need for DWARF 2 debugging
1318 information and return true if we have enough to do something.
1319 NAMES points to the dwarf2 section names, or is NULL if the standard
1320 ELF names are used. */
1321
1322 int
1323 dwarf2_has_info (struct objfile *objfile,
1324 const struct dwarf2_debug_sections *names)
1325 {
1326 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1327 if (!dwarf2_per_objfile)
1328 {
1329 /* Initialize per-objfile state. */
1330 struct dwarf2_per_objfile *data
1331 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1332
1333 memset (data, 0, sizeof (*data));
1334 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1335 dwarf2_per_objfile = data;
1336
1337 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1338 (void *) names);
1339 dwarf2_per_objfile->objfile = objfile;
1340 }
1341 return (dwarf2_per_objfile->info.asection != NULL
1342 && dwarf2_per_objfile->abbrev.asection != NULL);
1343 }
1344
1345 /* When loading sections, we look either for uncompressed section or for
1346 compressed section names. */
1347
1348 static int
1349 section_is_p (const char *section_name,
1350 const struct dwarf2_section_names *names)
1351 {
1352 if (names->normal != NULL
1353 && strcmp (section_name, names->normal) == 0)
1354 return 1;
1355 if (names->compressed != NULL
1356 && strcmp (section_name, names->compressed) == 0)
1357 return 1;
1358 return 0;
1359 }
1360
1361 /* This function is mapped across the sections and remembers the
1362 offset and size of each of the debugging sections we are interested
1363 in. */
1364
1365 static void
1366 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1367 {
1368 const struct dwarf2_debug_sections *names;
1369
1370 if (vnames == NULL)
1371 names = &dwarf2_elf_names;
1372 else
1373 names = (const struct dwarf2_debug_sections *) vnames;
1374
1375 if (section_is_p (sectp->name, &names->info))
1376 {
1377 dwarf2_per_objfile->info.asection = sectp;
1378 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1379 }
1380 else if (section_is_p (sectp->name, &names->abbrev))
1381 {
1382 dwarf2_per_objfile->abbrev.asection = sectp;
1383 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1384 }
1385 else if (section_is_p (sectp->name, &names->line))
1386 {
1387 dwarf2_per_objfile->line.asection = sectp;
1388 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1389 }
1390 else if (section_is_p (sectp->name, &names->loc))
1391 {
1392 dwarf2_per_objfile->loc.asection = sectp;
1393 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1394 }
1395 else if (section_is_p (sectp->name, &names->macinfo))
1396 {
1397 dwarf2_per_objfile->macinfo.asection = sectp;
1398 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1399 }
1400 else if (section_is_p (sectp->name, &names->macro))
1401 {
1402 dwarf2_per_objfile->macro.asection = sectp;
1403 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1404 }
1405 else if (section_is_p (sectp->name, &names->str))
1406 {
1407 dwarf2_per_objfile->str.asection = sectp;
1408 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1409 }
1410 else if (section_is_p (sectp->name, &names->frame))
1411 {
1412 dwarf2_per_objfile->frame.asection = sectp;
1413 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1414 }
1415 else if (section_is_p (sectp->name, &names->eh_frame))
1416 {
1417 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1418
1419 if (aflag & SEC_HAS_CONTENTS)
1420 {
1421 dwarf2_per_objfile->eh_frame.asection = sectp;
1422 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1423 }
1424 }
1425 else if (section_is_p (sectp->name, &names->ranges))
1426 {
1427 dwarf2_per_objfile->ranges.asection = sectp;
1428 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1429 }
1430 else if (section_is_p (sectp->name, &names->types))
1431 {
1432 struct dwarf2_section_info type_section;
1433
1434 memset (&type_section, 0, sizeof (type_section));
1435 type_section.asection = sectp;
1436 type_section.size = bfd_get_section_size (sectp);
1437
1438 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1439 &type_section);
1440 }
1441 else if (section_is_p (sectp->name, &names->gdb_index))
1442 {
1443 dwarf2_per_objfile->gdb_index.asection = sectp;
1444 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1445 }
1446
1447 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1448 && bfd_section_vma (abfd, sectp) == 0)
1449 dwarf2_per_objfile->has_section_at_zero = 1;
1450 }
1451
1452 /* Decompress a section that was compressed using zlib. Store the
1453 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1454
1455 static void
1456 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1457 gdb_byte **outbuf, bfd_size_type *outsize)
1458 {
1459 bfd *abfd = objfile->obfd;
1460 #ifndef HAVE_ZLIB_H
1461 error (_("Support for zlib-compressed DWARF data (from '%s') "
1462 "is disabled in this copy of GDB"),
1463 bfd_get_filename (abfd));
1464 #else
1465 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1466 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1467 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1468 bfd_size_type uncompressed_size;
1469 gdb_byte *uncompressed_buffer;
1470 z_stream strm;
1471 int rc;
1472 int header_size = 12;
1473
1474 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1475 || bfd_bread (compressed_buffer,
1476 compressed_size, abfd) != compressed_size)
1477 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1478 bfd_get_filename (abfd));
1479
1480 /* Read the zlib header. In this case, it should be "ZLIB" followed
1481 by the uncompressed section size, 8 bytes in big-endian order. */
1482 if (compressed_size < header_size
1483 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1484 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1485 bfd_get_filename (abfd));
1486 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1493 uncompressed_size += compressed_buffer[11];
1494
1495 /* It is possible the section consists of several compressed
1496 buffers concatenated together, so we uncompress in a loop. */
1497 strm.zalloc = NULL;
1498 strm.zfree = NULL;
1499 strm.opaque = NULL;
1500 strm.avail_in = compressed_size - header_size;
1501 strm.next_in = (Bytef*) compressed_buffer + header_size;
1502 strm.avail_out = uncompressed_size;
1503 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1504 uncompressed_size);
1505 rc = inflateInit (&strm);
1506 while (strm.avail_in > 0)
1507 {
1508 if (rc != Z_OK)
1509 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1510 bfd_get_filename (abfd), rc);
1511 strm.next_out = ((Bytef*) uncompressed_buffer
1512 + (uncompressed_size - strm.avail_out));
1513 rc = inflate (&strm, Z_FINISH);
1514 if (rc != Z_STREAM_END)
1515 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1516 bfd_get_filename (abfd), rc);
1517 rc = inflateReset (&strm);
1518 }
1519 rc = inflateEnd (&strm);
1520 if (rc != Z_OK
1521 || strm.avail_out != 0)
1522 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1523 bfd_get_filename (abfd), rc);
1524
1525 do_cleanups (cleanup);
1526 *outbuf = uncompressed_buffer;
1527 *outsize = uncompressed_size;
1528 #endif
1529 }
1530
1531 /* A helper function that decides whether a section is empty. */
1532
1533 static int
1534 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1535 {
1536 return info->asection == NULL || info->size == 0;
1537 }
1538
1539 /* Read the contents of the section INFO from object file specified by
1540 OBJFILE, store info about the section into INFO.
1541 If the section is compressed, uncompress it before returning. */
1542
1543 static void
1544 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1545 {
1546 bfd *abfd = objfile->obfd;
1547 asection *sectp = info->asection;
1548 gdb_byte *buf, *retbuf;
1549 unsigned char header[4];
1550
1551 if (info->readin)
1552 return;
1553 info->buffer = NULL;
1554 info->map_addr = NULL;
1555 info->readin = 1;
1556
1557 if (dwarf2_section_empty_p (info))
1558 return;
1559
1560 /* Check if the file has a 4-byte header indicating compression. */
1561 if (info->size > sizeof (header)
1562 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1563 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1564 {
1565 /* Upon decompression, update the buffer and its size. */
1566 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1567 {
1568 zlib_decompress_section (objfile, sectp, &info->buffer,
1569 &info->size);
1570 return;
1571 }
1572 }
1573
1574 #ifdef HAVE_MMAP
1575 if (pagesize == 0)
1576 pagesize = getpagesize ();
1577
1578 /* Only try to mmap sections which are large enough: we don't want to
1579 waste space due to fragmentation. Also, only try mmap for sections
1580 without relocations. */
1581
1582 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1583 {
1584 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1585 MAP_PRIVATE, sectp->filepos,
1586 &info->map_addr, &info->map_len);
1587
1588 if ((caddr_t)info->buffer != MAP_FAILED)
1589 {
1590 #if HAVE_POSIX_MADVISE
1591 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1592 #endif
1593 return;
1594 }
1595 }
1596 #endif
1597
1598 /* If we get here, we are a normal, not-compressed section. */
1599 info->buffer = buf
1600 = obstack_alloc (&objfile->objfile_obstack, info->size);
1601
1602 /* When debugging .o files, we may need to apply relocations; see
1603 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1604 We never compress sections in .o files, so we only need to
1605 try this when the section is not compressed. */
1606 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1607 if (retbuf != NULL)
1608 {
1609 info->buffer = retbuf;
1610 return;
1611 }
1612
1613 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1614 || bfd_bread (buf, info->size, abfd) != info->size)
1615 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1616 bfd_get_filename (abfd));
1617 }
1618
1619 /* A helper function that returns the size of a section in a safe way.
1620 If you are positive that the section has been read before using the
1621 size, then it is safe to refer to the dwarf2_section_info object's
1622 "size" field directly. In other cases, you must call this
1623 function, because for compressed sections the size field is not set
1624 correctly until the section has been read. */
1625
1626 static bfd_size_type
1627 dwarf2_section_size (struct objfile *objfile,
1628 struct dwarf2_section_info *info)
1629 {
1630 if (!info->readin)
1631 dwarf2_read_section (objfile, info);
1632 return info->size;
1633 }
1634
1635 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1636 SECTION_NAME. */
1637
1638 void
1639 dwarf2_get_section_info (struct objfile *objfile,
1640 enum dwarf2_section_enum sect,
1641 asection **sectp, gdb_byte **bufp,
1642 bfd_size_type *sizep)
1643 {
1644 struct dwarf2_per_objfile *data
1645 = objfile_data (objfile, dwarf2_objfile_data_key);
1646 struct dwarf2_section_info *info;
1647
1648 /* We may see an objfile without any DWARF, in which case we just
1649 return nothing. */
1650 if (data == NULL)
1651 {
1652 *sectp = NULL;
1653 *bufp = NULL;
1654 *sizep = 0;
1655 return;
1656 }
1657 switch (sect)
1658 {
1659 case DWARF2_DEBUG_FRAME:
1660 info = &data->frame;
1661 break;
1662 case DWARF2_EH_FRAME:
1663 info = &data->eh_frame;
1664 break;
1665 default:
1666 gdb_assert_not_reached ("unexpected section");
1667 }
1668
1669 dwarf2_read_section (objfile, info);
1670
1671 *sectp = info->asection;
1672 *bufp = info->buffer;
1673 *sizep = info->size;
1674 }
1675
1676 \f
1677 /* DWARF quick_symbols_functions support. */
1678
1679 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1680 unique line tables, so we maintain a separate table of all .debug_line
1681 derived entries to support the sharing.
1682 All the quick functions need is the list of file names. We discard the
1683 line_header when we're done and don't need to record it here. */
1684 struct quick_file_names
1685 {
1686 /* The offset in .debug_line of the line table. We hash on this. */
1687 unsigned int offset;
1688
1689 /* The number of entries in file_names, real_names. */
1690 unsigned int num_file_names;
1691
1692 /* The file names from the line table, after being run through
1693 file_full_name. */
1694 const char **file_names;
1695
1696 /* The file names from the line table after being run through
1697 gdb_realpath. These are computed lazily. */
1698 const char **real_names;
1699 };
1700
1701 /* When using the index (and thus not using psymtabs), each CU has an
1702 object of this type. This is used to hold information needed by
1703 the various "quick" methods. */
1704 struct dwarf2_per_cu_quick_data
1705 {
1706 /* The file table. This can be NULL if there was no file table
1707 or it's currently not read in.
1708 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1709 struct quick_file_names *file_names;
1710
1711 /* The corresponding symbol table. This is NULL if symbols for this
1712 CU have not yet been read. */
1713 struct symtab *symtab;
1714
1715 /* A temporary mark bit used when iterating over all CUs in
1716 expand_symtabs_matching. */
1717 unsigned int mark : 1;
1718
1719 /* True if we've tried to read the file table and found there isn't one.
1720 There will be no point in trying to read it again next time. */
1721 unsigned int no_file_data : 1;
1722 };
1723
1724 /* Hash function for a quick_file_names. */
1725
1726 static hashval_t
1727 hash_file_name_entry (const void *e)
1728 {
1729 const struct quick_file_names *file_data = e;
1730
1731 return file_data->offset;
1732 }
1733
1734 /* Equality function for a quick_file_names. */
1735
1736 static int
1737 eq_file_name_entry (const void *a, const void *b)
1738 {
1739 const struct quick_file_names *ea = a;
1740 const struct quick_file_names *eb = b;
1741
1742 return ea->offset == eb->offset;
1743 }
1744
1745 /* Delete function for a quick_file_names. */
1746
1747 static void
1748 delete_file_name_entry (void *e)
1749 {
1750 struct quick_file_names *file_data = e;
1751 int i;
1752
1753 for (i = 0; i < file_data->num_file_names; ++i)
1754 {
1755 xfree ((void*) file_data->file_names[i]);
1756 if (file_data->real_names)
1757 xfree ((void*) file_data->real_names[i]);
1758 }
1759
1760 /* The space for the struct itself lives on objfile_obstack,
1761 so we don't free it here. */
1762 }
1763
1764 /* Create a quick_file_names hash table. */
1765
1766 static htab_t
1767 create_quick_file_names_table (unsigned int nr_initial_entries)
1768 {
1769 return htab_create_alloc (nr_initial_entries,
1770 hash_file_name_entry, eq_file_name_entry,
1771 delete_file_name_entry, xcalloc, xfree);
1772 }
1773
1774 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1775 have to be created afterwards. You should call age_cached_comp_units after
1776 processing PER_CU->CU. dw2_setup must have been already called. */
1777
1778 static void
1779 load_cu (struct dwarf2_per_cu_data *per_cu)
1780 {
1781 if (per_cu->debug_types_section)
1782 load_full_type_unit (per_cu);
1783 else
1784 load_full_comp_unit (per_cu);
1785
1786 gdb_assert (per_cu->cu != NULL);
1787
1788 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1789 }
1790
1791 /* Read in the symbols for PER_CU. */
1792
1793 static void
1794 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1795 {
1796 struct cleanup *back_to;
1797
1798 back_to = make_cleanup (dwarf2_release_queue, NULL);
1799
1800 queue_comp_unit (per_cu);
1801
1802 load_cu (per_cu);
1803
1804 process_queue ();
1805
1806 /* Age the cache, releasing compilation units that have not
1807 been used recently. */
1808 age_cached_comp_units ();
1809
1810 do_cleanups (back_to);
1811 }
1812
1813 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1814 the objfile from which this CU came. Returns the resulting symbol
1815 table. */
1816
1817 static struct symtab *
1818 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1819 {
1820 if (!per_cu->v.quick->symtab)
1821 {
1822 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1823 increment_reading_symtab ();
1824 dw2_do_instantiate_symtab (per_cu);
1825 do_cleanups (back_to);
1826 }
1827 return per_cu->v.quick->symtab;
1828 }
1829
1830 /* Return the CU given its index. */
1831
1832 static struct dwarf2_per_cu_data *
1833 dw2_get_cu (int index)
1834 {
1835 if (index >= dwarf2_per_objfile->n_comp_units)
1836 {
1837 index -= dwarf2_per_objfile->n_comp_units;
1838 return dwarf2_per_objfile->type_comp_units[index];
1839 }
1840 return dwarf2_per_objfile->all_comp_units[index];
1841 }
1842
1843 /* A helper function that knows how to read a 64-bit value in a way
1844 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1845 otherwise. */
1846
1847 static int
1848 extract_cu_value (const char *bytes, ULONGEST *result)
1849 {
1850 if (sizeof (ULONGEST) < 8)
1851 {
1852 int i;
1853
1854 /* Ignore the upper 4 bytes if they are all zero. */
1855 for (i = 0; i < 4; ++i)
1856 if (bytes[i + 4] != 0)
1857 return 0;
1858
1859 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1860 }
1861 else
1862 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1863 return 1;
1864 }
1865
1866 /* Read the CU list from the mapped index, and use it to create all
1867 the CU objects for this objfile. Return 0 if something went wrong,
1868 1 if everything went ok. */
1869
1870 static int
1871 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1872 offset_type cu_list_elements)
1873 {
1874 offset_type i;
1875
1876 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1877 dwarf2_per_objfile->all_comp_units
1878 = obstack_alloc (&objfile->objfile_obstack,
1879 dwarf2_per_objfile->n_comp_units
1880 * sizeof (struct dwarf2_per_cu_data *));
1881
1882 for (i = 0; i < cu_list_elements; i += 2)
1883 {
1884 struct dwarf2_per_cu_data *the_cu;
1885 ULONGEST offset, length;
1886
1887 if (!extract_cu_value (cu_list, &offset)
1888 || !extract_cu_value (cu_list + 8, &length))
1889 return 0;
1890 cu_list += 2 * 8;
1891
1892 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1893 struct dwarf2_per_cu_data);
1894 the_cu->offset = offset;
1895 the_cu->length = length;
1896 the_cu->objfile = objfile;
1897 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1898 struct dwarf2_per_cu_quick_data);
1899 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1900 }
1901
1902 return 1;
1903 }
1904
1905 /* Create the signatured type hash table from the index. */
1906
1907 static int
1908 create_signatured_type_table_from_index (struct objfile *objfile,
1909 struct dwarf2_section_info *section,
1910 const gdb_byte *bytes,
1911 offset_type elements)
1912 {
1913 offset_type i;
1914 htab_t sig_types_hash;
1915
1916 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1917 dwarf2_per_objfile->type_comp_units
1918 = obstack_alloc (&objfile->objfile_obstack,
1919 dwarf2_per_objfile->n_type_comp_units
1920 * sizeof (struct dwarf2_per_cu_data *));
1921
1922 sig_types_hash = allocate_signatured_type_table (objfile);
1923
1924 for (i = 0; i < elements; i += 3)
1925 {
1926 struct signatured_type *type_sig;
1927 ULONGEST offset, type_offset, signature;
1928 void **slot;
1929
1930 if (!extract_cu_value (bytes, &offset)
1931 || !extract_cu_value (bytes + 8, &type_offset))
1932 return 0;
1933 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1934 bytes += 3 * 8;
1935
1936 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1937 struct signatured_type);
1938 type_sig->signature = signature;
1939 type_sig->type_offset = type_offset;
1940 type_sig->per_cu.debug_types_section = section;
1941 type_sig->per_cu.offset = offset;
1942 type_sig->per_cu.objfile = objfile;
1943 type_sig->per_cu.v.quick
1944 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1945 struct dwarf2_per_cu_quick_data);
1946
1947 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1948 *slot = type_sig;
1949
1950 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1951 }
1952
1953 dwarf2_per_objfile->signatured_types = sig_types_hash;
1954
1955 return 1;
1956 }
1957
1958 /* Read the address map data from the mapped index, and use it to
1959 populate the objfile's psymtabs_addrmap. */
1960
1961 static void
1962 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1963 {
1964 const gdb_byte *iter, *end;
1965 struct obstack temp_obstack;
1966 struct addrmap *mutable_map;
1967 struct cleanup *cleanup;
1968 CORE_ADDR baseaddr;
1969
1970 obstack_init (&temp_obstack);
1971 cleanup = make_cleanup_obstack_free (&temp_obstack);
1972 mutable_map = addrmap_create_mutable (&temp_obstack);
1973
1974 iter = index->address_table;
1975 end = iter + index->address_table_size;
1976
1977 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1978
1979 while (iter < end)
1980 {
1981 ULONGEST hi, lo, cu_index;
1982 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1983 iter += 8;
1984 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985 iter += 8;
1986 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1987 iter += 4;
1988
1989 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1990 dw2_get_cu (cu_index));
1991 }
1992
1993 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1994 &objfile->objfile_obstack);
1995 do_cleanups (cleanup);
1996 }
1997
1998 /* The hash function for strings in the mapped index. This is the same as
1999 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2000 implementation. This is necessary because the hash function is tied to the
2001 format of the mapped index file. The hash values do not have to match with
2002 SYMBOL_HASH_NEXT.
2003
2004 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2005
2006 static hashval_t
2007 mapped_index_string_hash (int index_version, const void *p)
2008 {
2009 const unsigned char *str = (const unsigned char *) p;
2010 hashval_t r = 0;
2011 unsigned char c;
2012
2013 while ((c = *str++) != 0)
2014 {
2015 if (index_version >= 5)
2016 c = tolower (c);
2017 r = r * 67 + c - 113;
2018 }
2019
2020 return r;
2021 }
2022
2023 /* Find a slot in the mapped index INDEX for the object named NAME.
2024 If NAME is found, set *VEC_OUT to point to the CU vector in the
2025 constant pool and return 1. If NAME cannot be found, return 0. */
2026
2027 static int
2028 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2029 offset_type **vec_out)
2030 {
2031 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2032 offset_type hash;
2033 offset_type slot, step;
2034 int (*cmp) (const char *, const char *);
2035
2036 if (current_language->la_language == language_cplus
2037 || current_language->la_language == language_java
2038 || current_language->la_language == language_fortran)
2039 {
2040 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2041 not contain any. */
2042 const char *paren = strchr (name, '(');
2043
2044 if (paren)
2045 {
2046 char *dup;
2047
2048 dup = xmalloc (paren - name + 1);
2049 memcpy (dup, name, paren - name);
2050 dup[paren - name] = 0;
2051
2052 make_cleanup (xfree, dup);
2053 name = dup;
2054 }
2055 }
2056
2057 /* Index version 4 did not support case insensitive searches. But the
2058 indexes for case insensitive languages are built in lowercase, therefore
2059 simulate our NAME being searched is also lowercased. */
2060 hash = mapped_index_string_hash ((index->version == 4
2061 && case_sensitivity == case_sensitive_off
2062 ? 5 : index->version),
2063 name);
2064
2065 slot = hash & (index->symbol_table_slots - 1);
2066 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2067 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2068
2069 for (;;)
2070 {
2071 /* Convert a slot number to an offset into the table. */
2072 offset_type i = 2 * slot;
2073 const char *str;
2074 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2075 {
2076 do_cleanups (back_to);
2077 return 0;
2078 }
2079
2080 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2081 if (!cmp (name, str))
2082 {
2083 *vec_out = (offset_type *) (index->constant_pool
2084 + MAYBE_SWAP (index->symbol_table[i + 1]));
2085 do_cleanups (back_to);
2086 return 1;
2087 }
2088
2089 slot = (slot + step) & (index->symbol_table_slots - 1);
2090 }
2091 }
2092
2093 /* Read the index file. If everything went ok, initialize the "quick"
2094 elements of all the CUs and return 1. Otherwise, return 0. */
2095
2096 static int
2097 dwarf2_read_index (struct objfile *objfile)
2098 {
2099 char *addr;
2100 struct mapped_index *map;
2101 offset_type *metadata;
2102 const gdb_byte *cu_list;
2103 const gdb_byte *types_list = NULL;
2104 offset_type version, cu_list_elements;
2105 offset_type types_list_elements = 0;
2106 int i;
2107
2108 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2109 return 0;
2110
2111 /* Older elfutils strip versions could keep the section in the main
2112 executable while splitting it for the separate debug info file. */
2113 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2114 & SEC_HAS_CONTENTS) == 0)
2115 return 0;
2116
2117 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2118
2119 addr = dwarf2_per_objfile->gdb_index.buffer;
2120 /* Version check. */
2121 version = MAYBE_SWAP (*(offset_type *) addr);
2122 /* Versions earlier than 3 emitted every copy of a psymbol. This
2123 causes the index to behave very poorly for certain requests. Version 3
2124 contained incomplete addrmap. So, it seems better to just ignore such
2125 indices. Index version 4 uses a different hash function than index
2126 version 5 and later. */
2127 if (version < 4)
2128 return 0;
2129 /* Indexes with higher version than the one supported by GDB may be no
2130 longer backward compatible. */
2131 if (version > 5)
2132 return 0;
2133
2134 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2135 map->version = version;
2136 map->total_size = dwarf2_per_objfile->gdb_index.size;
2137
2138 metadata = (offset_type *) (addr + sizeof (offset_type));
2139
2140 i = 0;
2141 cu_list = addr + MAYBE_SWAP (metadata[i]);
2142 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2143 / 8);
2144 ++i;
2145
2146 types_list = addr + MAYBE_SWAP (metadata[i]);
2147 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2148 - MAYBE_SWAP (metadata[i]))
2149 / 8);
2150 ++i;
2151
2152 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2153 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2154 - MAYBE_SWAP (metadata[i]));
2155 ++i;
2156
2157 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2158 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2159 - MAYBE_SWAP (metadata[i]))
2160 / (2 * sizeof (offset_type)));
2161 ++i;
2162
2163 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2164
2165 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2166 return 0;
2167
2168 if (types_list_elements)
2169 {
2170 struct dwarf2_section_info *section;
2171
2172 /* We can only handle a single .debug_types when we have an
2173 index. */
2174 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2175 return 0;
2176
2177 section = VEC_index (dwarf2_section_info_def,
2178 dwarf2_per_objfile->types, 0);
2179
2180 if (!create_signatured_type_table_from_index (objfile, section,
2181 types_list,
2182 types_list_elements))
2183 return 0;
2184 }
2185
2186 create_addrmap_from_index (objfile, map);
2187
2188 dwarf2_per_objfile->index_table = map;
2189 dwarf2_per_objfile->using_index = 1;
2190 dwarf2_per_objfile->quick_file_names_table =
2191 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2192
2193 return 1;
2194 }
2195
2196 /* A helper for the "quick" functions which sets the global
2197 dwarf2_per_objfile according to OBJFILE. */
2198
2199 static void
2200 dw2_setup (struct objfile *objfile)
2201 {
2202 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2203 gdb_assert (dwarf2_per_objfile);
2204 }
2205
2206 /* A helper for the "quick" functions which attempts to read the line
2207 table for THIS_CU. */
2208
2209 static struct quick_file_names *
2210 dw2_get_file_names (struct objfile *objfile,
2211 struct dwarf2_per_cu_data *this_cu)
2212 {
2213 bfd *abfd = objfile->obfd;
2214 struct line_header *lh;
2215 struct attribute *attr;
2216 struct cleanup *cleanups;
2217 struct die_info *comp_unit_die;
2218 struct dwarf2_section_info* sec;
2219 gdb_byte *info_ptr, *buffer;
2220 int has_children, i;
2221 struct dwarf2_cu cu;
2222 unsigned int bytes_read, buffer_size;
2223 struct die_reader_specs reader_specs;
2224 char *name, *comp_dir;
2225 void **slot;
2226 struct quick_file_names *qfn;
2227 unsigned int line_offset;
2228
2229 if (this_cu->v.quick->file_names != NULL)
2230 return this_cu->v.quick->file_names;
2231 /* If we know there is no line data, no point in looking again. */
2232 if (this_cu->v.quick->no_file_data)
2233 return NULL;
2234
2235 init_one_comp_unit (&cu, this_cu);
2236 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2237
2238 if (this_cu->debug_types_section)
2239 sec = this_cu->debug_types_section;
2240 else
2241 sec = &dwarf2_per_objfile->info;
2242 dwarf2_read_section (objfile, sec);
2243 buffer_size = sec->size;
2244 buffer = sec->buffer;
2245 info_ptr = buffer + this_cu->offset;
2246
2247 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2248 buffer, buffer_size,
2249 abfd,
2250 this_cu->debug_types_section != NULL);
2251
2252 /* Skip dummy compilation units. */
2253 if (info_ptr >= buffer + buffer_size
2254 || peek_abbrev_code (abfd, info_ptr) == 0)
2255 {
2256 do_cleanups (cleanups);
2257 return NULL;
2258 }
2259
2260 dwarf2_read_abbrevs (&cu);
2261 make_cleanup (dwarf2_free_abbrev_table, &cu);
2262
2263 init_cu_die_reader (&reader_specs, &cu);
2264 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2265 &has_children);
2266
2267 lh = NULL;
2268 slot = NULL;
2269 line_offset = 0;
2270 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2271 if (attr)
2272 {
2273 struct quick_file_names find_entry;
2274
2275 line_offset = DW_UNSND (attr);
2276
2277 /* We may have already read in this line header (TU line header sharing).
2278 If we have we're done. */
2279 find_entry.offset = line_offset;
2280 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2281 &find_entry, INSERT);
2282 if (*slot != NULL)
2283 {
2284 do_cleanups (cleanups);
2285 this_cu->v.quick->file_names = *slot;
2286 return *slot;
2287 }
2288
2289 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2290 }
2291 if (lh == NULL)
2292 {
2293 do_cleanups (cleanups);
2294 this_cu->v.quick->no_file_data = 1;
2295 return NULL;
2296 }
2297
2298 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2299 qfn->offset = line_offset;
2300 gdb_assert (slot != NULL);
2301 *slot = qfn;
2302
2303 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2304
2305 qfn->num_file_names = lh->num_file_names;
2306 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2307 lh->num_file_names * sizeof (char *));
2308 for (i = 0; i < lh->num_file_names; ++i)
2309 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2310 qfn->real_names = NULL;
2311
2312 free_line_header (lh);
2313 do_cleanups (cleanups);
2314
2315 this_cu->v.quick->file_names = qfn;
2316 return qfn;
2317 }
2318
2319 /* A helper for the "quick" functions which computes and caches the
2320 real path for a given file name from the line table. */
2321
2322 static const char *
2323 dw2_get_real_path (struct objfile *objfile,
2324 struct quick_file_names *qfn, int index)
2325 {
2326 if (qfn->real_names == NULL)
2327 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2328 qfn->num_file_names, sizeof (char *));
2329
2330 if (qfn->real_names[index] == NULL)
2331 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2332
2333 return qfn->real_names[index];
2334 }
2335
2336 static struct symtab *
2337 dw2_find_last_source_symtab (struct objfile *objfile)
2338 {
2339 int index;
2340
2341 dw2_setup (objfile);
2342 index = dwarf2_per_objfile->n_comp_units - 1;
2343 return dw2_instantiate_symtab (dw2_get_cu (index));
2344 }
2345
2346 /* Traversal function for dw2_forget_cached_source_info. */
2347
2348 static int
2349 dw2_free_cached_file_names (void **slot, void *info)
2350 {
2351 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2352
2353 if (file_data->real_names)
2354 {
2355 int i;
2356
2357 for (i = 0; i < file_data->num_file_names; ++i)
2358 {
2359 xfree ((void*) file_data->real_names[i]);
2360 file_data->real_names[i] = NULL;
2361 }
2362 }
2363
2364 return 1;
2365 }
2366
2367 static void
2368 dw2_forget_cached_source_info (struct objfile *objfile)
2369 {
2370 dw2_setup (objfile);
2371
2372 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2373 dw2_free_cached_file_names, NULL);
2374 }
2375
2376 /* Helper function for dw2_map_symtabs_matching_filename that expands
2377 the symtabs and calls the iterator. */
2378
2379 static int
2380 dw2_map_expand_apply (struct objfile *objfile,
2381 struct dwarf2_per_cu_data *per_cu,
2382 const char *name,
2383 const char *full_path, const char *real_path,
2384 int (*callback) (struct symtab *, void *),
2385 void *data)
2386 {
2387 struct symtab *last_made = objfile->symtabs;
2388
2389 /* Don't visit already-expanded CUs. */
2390 if (per_cu->v.quick->symtab)
2391 return 0;
2392
2393 /* This may expand more than one symtab, and we want to iterate over
2394 all of them. */
2395 dw2_instantiate_symtab (per_cu);
2396
2397 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2398 objfile->symtabs, last_made);
2399 }
2400
2401 /* Implementation of the map_symtabs_matching_filename method. */
2402
2403 static int
2404 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2405 const char *full_path, const char *real_path,
2406 int (*callback) (struct symtab *, void *),
2407 void *data)
2408 {
2409 int i;
2410 const char *name_basename = lbasename (name);
2411 int check_basename = name_basename == name;
2412 struct dwarf2_per_cu_data *base_cu = NULL;
2413
2414 dw2_setup (objfile);
2415
2416 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2417 + dwarf2_per_objfile->n_type_comp_units); ++i)
2418 {
2419 int j;
2420 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2421 struct quick_file_names *file_data;
2422
2423 /* We only need to look at symtabs not already expanded. */
2424 if (per_cu->v.quick->symtab)
2425 continue;
2426
2427 file_data = dw2_get_file_names (objfile, per_cu);
2428 if (file_data == NULL)
2429 continue;
2430
2431 for (j = 0; j < file_data->num_file_names; ++j)
2432 {
2433 const char *this_name = file_data->file_names[j];
2434
2435 if (FILENAME_CMP (name, this_name) == 0)
2436 {
2437 if (dw2_map_expand_apply (objfile, per_cu,
2438 name, full_path, real_path,
2439 callback, data))
2440 return 1;
2441 }
2442
2443 if (check_basename && ! base_cu
2444 && FILENAME_CMP (lbasename (this_name), name) == 0)
2445 base_cu = per_cu;
2446
2447 /* Before we invoke realpath, which can get expensive when many
2448 files are involved, do a quick comparison of the basenames. */
2449 if (! basenames_may_differ
2450 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2451 continue;
2452
2453 if (full_path != NULL)
2454 {
2455 const char *this_real_name = dw2_get_real_path (objfile,
2456 file_data, j);
2457
2458 if (this_real_name != NULL
2459 && FILENAME_CMP (full_path, this_real_name) == 0)
2460 {
2461 if (dw2_map_expand_apply (objfile, per_cu,
2462 name, full_path, real_path,
2463 callback, data))
2464 return 1;
2465 }
2466 }
2467
2468 if (real_path != NULL)
2469 {
2470 const char *this_real_name = dw2_get_real_path (objfile,
2471 file_data, j);
2472
2473 if (this_real_name != NULL
2474 && FILENAME_CMP (real_path, this_real_name) == 0)
2475 {
2476 if (dw2_map_expand_apply (objfile, per_cu,
2477 name, full_path, real_path,
2478 callback, data))
2479 return 1;
2480 }
2481 }
2482 }
2483 }
2484
2485 if (base_cu)
2486 {
2487 if (dw2_map_expand_apply (objfile, base_cu,
2488 name, full_path, real_path,
2489 callback, data))
2490 return 1;
2491 }
2492
2493 return 0;
2494 }
2495
2496 static struct symtab *
2497 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2498 const char *name, domain_enum domain)
2499 {
2500 /* We do all the work in the pre_expand_symtabs_matching hook
2501 instead. */
2502 return NULL;
2503 }
2504
2505 /* A helper function that expands all symtabs that hold an object
2506 named NAME. */
2507
2508 static void
2509 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2510 {
2511 dw2_setup (objfile);
2512
2513 /* index_table is NULL if OBJF_READNOW. */
2514 if (dwarf2_per_objfile->index_table)
2515 {
2516 offset_type *vec;
2517
2518 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2519 name, &vec))
2520 {
2521 offset_type i, len = MAYBE_SWAP (*vec);
2522 for (i = 0; i < len; ++i)
2523 {
2524 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2525 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2526
2527 dw2_instantiate_symtab (per_cu);
2528 }
2529 }
2530 }
2531 }
2532
2533 static void
2534 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2535 enum block_enum block_kind, const char *name,
2536 domain_enum domain)
2537 {
2538 dw2_do_expand_symtabs_matching (objfile, name);
2539 }
2540
2541 static void
2542 dw2_print_stats (struct objfile *objfile)
2543 {
2544 int i, count;
2545
2546 dw2_setup (objfile);
2547 count = 0;
2548 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2549 + dwarf2_per_objfile->n_type_comp_units); ++i)
2550 {
2551 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2552
2553 if (!per_cu->v.quick->symtab)
2554 ++count;
2555 }
2556 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2557 }
2558
2559 static void
2560 dw2_dump (struct objfile *objfile)
2561 {
2562 /* Nothing worth printing. */
2563 }
2564
2565 static void
2566 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2567 struct section_offsets *delta)
2568 {
2569 /* There's nothing to relocate here. */
2570 }
2571
2572 static void
2573 dw2_expand_symtabs_for_function (struct objfile *objfile,
2574 const char *func_name)
2575 {
2576 dw2_do_expand_symtabs_matching (objfile, func_name);
2577 }
2578
2579 static void
2580 dw2_expand_all_symtabs (struct objfile *objfile)
2581 {
2582 int i;
2583
2584 dw2_setup (objfile);
2585
2586 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2587 + dwarf2_per_objfile->n_type_comp_units); ++i)
2588 {
2589 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2590
2591 dw2_instantiate_symtab (per_cu);
2592 }
2593 }
2594
2595 static void
2596 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2597 const char *filename)
2598 {
2599 int i;
2600
2601 dw2_setup (objfile);
2602
2603 /* We don't need to consider type units here.
2604 This is only called for examining code, e.g. expand_line_sal.
2605 There can be an order of magnitude (or more) more type units
2606 than comp units, and we avoid them if we can. */
2607
2608 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2609 {
2610 int j;
2611 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2612 struct quick_file_names *file_data;
2613
2614 /* We only need to look at symtabs not already expanded. */
2615 if (per_cu->v.quick->symtab)
2616 continue;
2617
2618 file_data = dw2_get_file_names (objfile, per_cu);
2619 if (file_data == NULL)
2620 continue;
2621
2622 for (j = 0; j < file_data->num_file_names; ++j)
2623 {
2624 const char *this_name = file_data->file_names[j];
2625 if (FILENAME_CMP (this_name, filename) == 0)
2626 {
2627 dw2_instantiate_symtab (per_cu);
2628 break;
2629 }
2630 }
2631 }
2632 }
2633
2634 static const char *
2635 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2636 {
2637 struct dwarf2_per_cu_data *per_cu;
2638 offset_type *vec;
2639 struct quick_file_names *file_data;
2640
2641 dw2_setup (objfile);
2642
2643 /* index_table is NULL if OBJF_READNOW. */
2644 if (!dwarf2_per_objfile->index_table)
2645 {
2646 struct symtab *s;
2647
2648 ALL_OBJFILE_SYMTABS (objfile, s)
2649 if (s->primary)
2650 {
2651 struct blockvector *bv = BLOCKVECTOR (s);
2652 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2653 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2654
2655 if (sym)
2656 return sym->symtab->filename;
2657 }
2658 return NULL;
2659 }
2660
2661 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2662 name, &vec))
2663 return NULL;
2664
2665 /* Note that this just looks at the very first one named NAME -- but
2666 actually we are looking for a function. find_main_filename
2667 should be rewritten so that it doesn't require a custom hook. It
2668 could just use the ordinary symbol tables. */
2669 /* vec[0] is the length, which must always be >0. */
2670 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2671
2672 file_data = dw2_get_file_names (objfile, per_cu);
2673 if (file_data == NULL)
2674 return NULL;
2675
2676 return file_data->file_names[file_data->num_file_names - 1];
2677 }
2678
2679 static void
2680 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2681 struct objfile *objfile, int global,
2682 int (*callback) (struct block *,
2683 struct symbol *, void *),
2684 void *data, symbol_compare_ftype *match,
2685 symbol_compare_ftype *ordered_compare)
2686 {
2687 /* Currently unimplemented; used for Ada. The function can be called if the
2688 current language is Ada for a non-Ada objfile using GNU index. As Ada
2689 does not look for non-Ada symbols this function should just return. */
2690 }
2691
2692 static void
2693 dw2_expand_symtabs_matching
2694 (struct objfile *objfile,
2695 int (*file_matcher) (const char *, void *),
2696 int (*name_matcher) (const struct language_defn *, const char *, void *),
2697 enum search_domain kind,
2698 void *data)
2699 {
2700 int i;
2701 offset_type iter;
2702 struct mapped_index *index;
2703
2704 dw2_setup (objfile);
2705
2706 /* index_table is NULL if OBJF_READNOW. */
2707 if (!dwarf2_per_objfile->index_table)
2708 return;
2709 index = dwarf2_per_objfile->index_table;
2710
2711 if (file_matcher != NULL)
2712 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2713 + dwarf2_per_objfile->n_type_comp_units); ++i)
2714 {
2715 int j;
2716 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2717 struct quick_file_names *file_data;
2718
2719 per_cu->v.quick->mark = 0;
2720
2721 /* We only need to look at symtabs not already expanded. */
2722 if (per_cu->v.quick->symtab)
2723 continue;
2724
2725 file_data = dw2_get_file_names (objfile, per_cu);
2726 if (file_data == NULL)
2727 continue;
2728
2729 for (j = 0; j < file_data->num_file_names; ++j)
2730 {
2731 if (file_matcher (file_data->file_names[j], data))
2732 {
2733 per_cu->v.quick->mark = 1;
2734 break;
2735 }
2736 }
2737 }
2738
2739 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2740 {
2741 offset_type idx = 2 * iter;
2742 const char *name;
2743 offset_type *vec, vec_len, vec_idx;
2744
2745 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2746 continue;
2747
2748 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2749
2750 if (! (*name_matcher) (current_language, name, data))
2751 continue;
2752
2753 /* The name was matched, now expand corresponding CUs that were
2754 marked. */
2755 vec = (offset_type *) (index->constant_pool
2756 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2757 vec_len = MAYBE_SWAP (vec[0]);
2758 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2759 {
2760 struct dwarf2_per_cu_data *per_cu;
2761
2762 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2763 if (file_matcher == NULL || per_cu->v.quick->mark)
2764 dw2_instantiate_symtab (per_cu);
2765 }
2766 }
2767 }
2768
2769 static struct symtab *
2770 dw2_find_pc_sect_symtab (struct objfile *objfile,
2771 struct minimal_symbol *msymbol,
2772 CORE_ADDR pc,
2773 struct obj_section *section,
2774 int warn_if_readin)
2775 {
2776 struct dwarf2_per_cu_data *data;
2777
2778 dw2_setup (objfile);
2779
2780 if (!objfile->psymtabs_addrmap)
2781 return NULL;
2782
2783 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2784 if (!data)
2785 return NULL;
2786
2787 if (warn_if_readin && data->v.quick->symtab)
2788 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2789 paddress (get_objfile_arch (objfile), pc));
2790
2791 return dw2_instantiate_symtab (data);
2792 }
2793
2794 static void
2795 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2796 void *data, int need_fullname)
2797 {
2798 int i;
2799
2800 dw2_setup (objfile);
2801
2802 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2803 + dwarf2_per_objfile->n_type_comp_units); ++i)
2804 {
2805 int j;
2806 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2807 struct quick_file_names *file_data;
2808
2809 /* We only need to look at symtabs not already expanded. */
2810 if (per_cu->v.quick->symtab)
2811 continue;
2812
2813 file_data = dw2_get_file_names (objfile, per_cu);
2814 if (file_data == NULL)
2815 continue;
2816
2817 for (j = 0; j < file_data->num_file_names; ++j)
2818 {
2819 const char *this_real_name;
2820
2821 if (need_fullname)
2822 this_real_name = dw2_get_real_path (objfile, file_data, j);
2823 else
2824 this_real_name = NULL;
2825 (*fun) (file_data->file_names[j], this_real_name, data);
2826 }
2827 }
2828 }
2829
2830 static int
2831 dw2_has_symbols (struct objfile *objfile)
2832 {
2833 return 1;
2834 }
2835
2836 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2837 {
2838 dw2_has_symbols,
2839 dw2_find_last_source_symtab,
2840 dw2_forget_cached_source_info,
2841 dw2_map_symtabs_matching_filename,
2842 dw2_lookup_symbol,
2843 dw2_pre_expand_symtabs_matching,
2844 dw2_print_stats,
2845 dw2_dump,
2846 dw2_relocate,
2847 dw2_expand_symtabs_for_function,
2848 dw2_expand_all_symtabs,
2849 dw2_expand_symtabs_with_filename,
2850 dw2_find_symbol_file,
2851 dw2_map_matching_symbols,
2852 dw2_expand_symtabs_matching,
2853 dw2_find_pc_sect_symtab,
2854 dw2_map_symbol_filenames
2855 };
2856
2857 /* Initialize for reading DWARF for this objfile. Return 0 if this
2858 file will use psymtabs, or 1 if using the GNU index. */
2859
2860 int
2861 dwarf2_initialize_objfile (struct objfile *objfile)
2862 {
2863 /* If we're about to read full symbols, don't bother with the
2864 indices. In this case we also don't care if some other debug
2865 format is making psymtabs, because they are all about to be
2866 expanded anyway. */
2867 if ((objfile->flags & OBJF_READNOW))
2868 {
2869 int i;
2870
2871 dwarf2_per_objfile->using_index = 1;
2872 create_all_comp_units (objfile);
2873 create_debug_types_hash_table (objfile);
2874 dwarf2_per_objfile->quick_file_names_table =
2875 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2876
2877 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2878 + dwarf2_per_objfile->n_type_comp_units); ++i)
2879 {
2880 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2881
2882 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2883 struct dwarf2_per_cu_quick_data);
2884 }
2885
2886 /* Return 1 so that gdb sees the "quick" functions. However,
2887 these functions will be no-ops because we will have expanded
2888 all symtabs. */
2889 return 1;
2890 }
2891
2892 if (dwarf2_read_index (objfile))
2893 return 1;
2894
2895 return 0;
2896 }
2897
2898 \f
2899
2900 /* Build a partial symbol table. */
2901
2902 void
2903 dwarf2_build_psymtabs (struct objfile *objfile)
2904 {
2905 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2906 {
2907 init_psymbol_list (objfile, 1024);
2908 }
2909
2910 dwarf2_build_psymtabs_hard (objfile);
2911 }
2912
2913 /* Return TRUE if OFFSET is within CU_HEADER. */
2914
2915 static inline int
2916 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2917 {
2918 unsigned int bottom = cu_header->offset;
2919 unsigned int top = (cu_header->offset
2920 + cu_header->length
2921 + cu_header->initial_length_size);
2922
2923 return (offset >= bottom && offset < top);
2924 }
2925
2926 /* Read in the comp unit header information from the debug_info at info_ptr.
2927 NOTE: This leaves members offset, first_die_offset to be filled in
2928 by the caller. */
2929
2930 static gdb_byte *
2931 read_comp_unit_head (struct comp_unit_head *cu_header,
2932 gdb_byte *info_ptr, bfd *abfd)
2933 {
2934 int signed_addr;
2935 unsigned int bytes_read;
2936
2937 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2938 cu_header->initial_length_size = bytes_read;
2939 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2940 info_ptr += bytes_read;
2941 cu_header->version = read_2_bytes (abfd, info_ptr);
2942 info_ptr += 2;
2943 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2944 &bytes_read);
2945 info_ptr += bytes_read;
2946 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2947 info_ptr += 1;
2948 signed_addr = bfd_get_sign_extend_vma (abfd);
2949 if (signed_addr < 0)
2950 internal_error (__FILE__, __LINE__,
2951 _("read_comp_unit_head: dwarf from non elf file"));
2952 cu_header->signed_addr_p = signed_addr;
2953
2954 return info_ptr;
2955 }
2956
2957 /* Read in a CU header and perform some basic error checking. */
2958
2959 static gdb_byte *
2960 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2961 gdb_byte *buffer, unsigned int buffer_size,
2962 bfd *abfd, int is_debug_types_section)
2963 {
2964 gdb_byte *beg_of_comp_unit = info_ptr;
2965
2966 header->offset = beg_of_comp_unit - buffer;
2967
2968 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2969
2970 /* If we're reading a type unit, skip over the signature and
2971 type_offset fields. */
2972 if (is_debug_types_section)
2973 info_ptr += 8 /*signature*/ + header->offset_size;
2974
2975 header->first_die_offset = info_ptr - beg_of_comp_unit;
2976
2977 if (header->version != 2 && header->version != 3 && header->version != 4)
2978 error (_("Dwarf Error: wrong version in compilation unit header "
2979 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2980 bfd_get_filename (abfd));
2981
2982 if (header->abbrev_offset
2983 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2984 &dwarf2_per_objfile->abbrev))
2985 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2986 "(offset 0x%lx + 6) [in module %s]"),
2987 (long) header->abbrev_offset,
2988 (long) (beg_of_comp_unit - buffer),
2989 bfd_get_filename (abfd));
2990
2991 if (beg_of_comp_unit + header->length + header->initial_length_size
2992 > buffer + buffer_size)
2993 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2994 "(offset 0x%lx + 0) [in module %s]"),
2995 (long) header->length,
2996 (long) (beg_of_comp_unit - buffer),
2997 bfd_get_filename (abfd));
2998
2999 return info_ptr;
3000 }
3001
3002 /* Read in the types comp unit header information from .debug_types entry at
3003 types_ptr. The result is a pointer to one past the end of the header. */
3004
3005 static gdb_byte *
3006 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3007 struct dwarf2_section_info *section,
3008 ULONGEST *signature,
3009 gdb_byte *types_ptr, bfd *abfd)
3010 {
3011 gdb_byte *initial_types_ptr = types_ptr;
3012
3013 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3014 cu_header->offset = types_ptr - section->buffer;
3015
3016 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3017
3018 *signature = read_8_bytes (abfd, types_ptr);
3019 types_ptr += 8;
3020 types_ptr += cu_header->offset_size;
3021 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3022
3023 return types_ptr;
3024 }
3025
3026 /* Allocate a new partial symtab for file named NAME and mark this new
3027 partial symtab as being an include of PST. */
3028
3029 static void
3030 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3031 struct objfile *objfile)
3032 {
3033 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3034
3035 subpst->section_offsets = pst->section_offsets;
3036 subpst->textlow = 0;
3037 subpst->texthigh = 0;
3038
3039 subpst->dependencies = (struct partial_symtab **)
3040 obstack_alloc (&objfile->objfile_obstack,
3041 sizeof (struct partial_symtab *));
3042 subpst->dependencies[0] = pst;
3043 subpst->number_of_dependencies = 1;
3044
3045 subpst->globals_offset = 0;
3046 subpst->n_global_syms = 0;
3047 subpst->statics_offset = 0;
3048 subpst->n_static_syms = 0;
3049 subpst->symtab = NULL;
3050 subpst->read_symtab = pst->read_symtab;
3051 subpst->readin = 0;
3052
3053 /* No private part is necessary for include psymtabs. This property
3054 can be used to differentiate between such include psymtabs and
3055 the regular ones. */
3056 subpst->read_symtab_private = NULL;
3057 }
3058
3059 /* Read the Line Number Program data and extract the list of files
3060 included by the source file represented by PST. Build an include
3061 partial symtab for each of these included files. */
3062
3063 static void
3064 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3065 struct die_info *die,
3066 struct partial_symtab *pst)
3067 {
3068 struct objfile *objfile = cu->objfile;
3069 bfd *abfd = objfile->obfd;
3070 struct line_header *lh = NULL;
3071 struct attribute *attr;
3072
3073 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3074 if (attr)
3075 {
3076 unsigned int line_offset = DW_UNSND (attr);
3077
3078 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3079 }
3080 if (lh == NULL)
3081 return; /* No linetable, so no includes. */
3082
3083 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3084 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3085
3086 free_line_header (lh);
3087 }
3088
3089 static hashval_t
3090 hash_type_signature (const void *item)
3091 {
3092 const struct signatured_type *type_sig = item;
3093
3094 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3095 return type_sig->signature;
3096 }
3097
3098 static int
3099 eq_type_signature (const void *item_lhs, const void *item_rhs)
3100 {
3101 const struct signatured_type *lhs = item_lhs;
3102 const struct signatured_type *rhs = item_rhs;
3103
3104 return lhs->signature == rhs->signature;
3105 }
3106
3107 /* Allocate a hash table for signatured types. */
3108
3109 static htab_t
3110 allocate_signatured_type_table (struct objfile *objfile)
3111 {
3112 return htab_create_alloc_ex (41,
3113 hash_type_signature,
3114 eq_type_signature,
3115 NULL,
3116 &objfile->objfile_obstack,
3117 hashtab_obstack_allocate,
3118 dummy_obstack_deallocate);
3119 }
3120
3121 /* A helper function to add a signatured type CU to a list. */
3122
3123 static int
3124 add_signatured_type_cu_to_list (void **slot, void *datum)
3125 {
3126 struct signatured_type *sigt = *slot;
3127 struct dwarf2_per_cu_data ***datap = datum;
3128
3129 **datap = &sigt->per_cu;
3130 ++*datap;
3131
3132 return 1;
3133 }
3134
3135 /* Create the hash table of all entries in the .debug_types section.
3136 The result is zero if there is an error (e.g. missing .debug_types section),
3137 otherwise non-zero. */
3138
3139 static int
3140 create_debug_types_hash_table (struct objfile *objfile)
3141 {
3142 htab_t types_htab = NULL;
3143 struct dwarf2_per_cu_data **iter;
3144 int ix;
3145 struct dwarf2_section_info *section;
3146
3147 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3148 {
3149 dwarf2_per_objfile->signatured_types = NULL;
3150 return 0;
3151 }
3152
3153 for (ix = 0;
3154 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3155 ix, section);
3156 ++ix)
3157 {
3158 gdb_byte *info_ptr, *end_ptr;
3159
3160 dwarf2_read_section (objfile, section);
3161 info_ptr = section->buffer;
3162
3163 if (info_ptr == NULL)
3164 continue;
3165
3166 if (types_htab == NULL)
3167 types_htab = allocate_signatured_type_table (objfile);
3168
3169 if (dwarf2_die_debug)
3170 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3171
3172 end_ptr = info_ptr + section->size;
3173 while (info_ptr < end_ptr)
3174 {
3175 unsigned int offset;
3176 unsigned int offset_size;
3177 unsigned int type_offset;
3178 unsigned int length, initial_length_size;
3179 unsigned short version;
3180 ULONGEST signature;
3181 struct signatured_type *type_sig;
3182 void **slot;
3183 gdb_byte *ptr = info_ptr;
3184
3185 offset = ptr - section->buffer;
3186
3187 /* We need to read the type's signature in order to build the hash
3188 table, but we don't need to read anything else just yet. */
3189
3190 /* Sanity check to ensure entire cu is present. */
3191 length = read_initial_length (objfile->obfd, ptr,
3192 &initial_length_size);
3193 if (ptr + length + initial_length_size > end_ptr)
3194 {
3195 complaint (&symfile_complaints,
3196 _("debug type entry runs off end "
3197 "of `.debug_types' section, ignored"));
3198 break;
3199 }
3200
3201 offset_size = initial_length_size == 4 ? 4 : 8;
3202 ptr += initial_length_size;
3203 version = bfd_get_16 (objfile->obfd, ptr);
3204 ptr += 2;
3205 ptr += offset_size; /* abbrev offset */
3206 ptr += 1; /* address size */
3207 signature = bfd_get_64 (objfile->obfd, ptr);
3208 ptr += 8;
3209 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3210 ptr += offset_size;
3211
3212 /* Skip dummy type units. */
3213 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3214 {
3215 info_ptr = info_ptr + initial_length_size + length;
3216 continue;
3217 }
3218
3219 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3220 memset (type_sig, 0, sizeof (*type_sig));
3221 type_sig->signature = signature;
3222 type_sig->type_offset = type_offset;
3223 type_sig->per_cu.objfile = objfile;
3224 type_sig->per_cu.debug_types_section = section;
3225 type_sig->per_cu.offset = offset;
3226
3227 slot = htab_find_slot (types_htab, type_sig, INSERT);
3228 gdb_assert (slot != NULL);
3229 if (*slot != NULL)
3230 {
3231 const struct signatured_type *dup_sig = *slot;
3232
3233 complaint (&symfile_complaints,
3234 _("debug type entry at offset 0x%x is duplicate to the "
3235 "entry at offset 0x%x, signature 0x%s"),
3236 offset, dup_sig->per_cu.offset,
3237 phex (signature, sizeof (signature)));
3238 gdb_assert (signature == dup_sig->signature);
3239 }
3240 *slot = type_sig;
3241
3242 if (dwarf2_die_debug)
3243 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3244 offset, phex (signature, sizeof (signature)));
3245
3246 info_ptr = info_ptr + initial_length_size + length;
3247 }
3248 }
3249
3250 dwarf2_per_objfile->signatured_types = types_htab;
3251
3252 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3253 dwarf2_per_objfile->type_comp_units
3254 = obstack_alloc (&objfile->objfile_obstack,
3255 dwarf2_per_objfile->n_type_comp_units
3256 * sizeof (struct dwarf2_per_cu_data *));
3257 iter = &dwarf2_per_objfile->type_comp_units[0];
3258 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3259 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3260 == dwarf2_per_objfile->n_type_comp_units);
3261
3262 return 1;
3263 }
3264
3265 /* Lookup a signature based type.
3266 Returns NULL if SIG is not present in the table. */
3267
3268 static struct signatured_type *
3269 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3270 {
3271 struct signatured_type find_entry, *entry;
3272
3273 if (dwarf2_per_objfile->signatured_types == NULL)
3274 {
3275 complaint (&symfile_complaints,
3276 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3277 return 0;
3278 }
3279
3280 find_entry.signature = sig;
3281 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3282 return entry;
3283 }
3284
3285 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3286
3287 static void
3288 init_cu_die_reader (struct die_reader_specs *reader,
3289 struct dwarf2_cu *cu)
3290 {
3291 reader->abfd = cu->objfile->obfd;
3292 reader->cu = cu;
3293 if (cu->per_cu->debug_types_section)
3294 {
3295 gdb_assert (cu->per_cu->debug_types_section->readin);
3296 reader->buffer = cu->per_cu->debug_types_section->buffer;
3297 }
3298 else
3299 {
3300 gdb_assert (dwarf2_per_objfile->info.readin);
3301 reader->buffer = dwarf2_per_objfile->info.buffer;
3302 }
3303 }
3304
3305 /* Find the base address of the compilation unit for range lists and
3306 location lists. It will normally be specified by DW_AT_low_pc.
3307 In DWARF-3 draft 4, the base address could be overridden by
3308 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3309 compilation units with discontinuous ranges. */
3310
3311 static void
3312 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3313 {
3314 struct attribute *attr;
3315
3316 cu->base_known = 0;
3317 cu->base_address = 0;
3318
3319 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3320 if (attr)
3321 {
3322 cu->base_address = DW_ADDR (attr);
3323 cu->base_known = 1;
3324 }
3325 else
3326 {
3327 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3328 if (attr)
3329 {
3330 cu->base_address = DW_ADDR (attr);
3331 cu->base_known = 1;
3332 }
3333 }
3334 }
3335
3336 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3337 to combine the common parts.
3338 Process compilation unit THIS_CU for a psymtab.
3339 SECTION is the section the CU/TU comes from,
3340 either .debug_info or .debug_types. */
3341
3342 void
3343 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3344 struct dwarf2_section_info *section,
3345 int is_debug_types_section)
3346 {
3347 struct objfile *objfile = this_cu->objfile;
3348 bfd *abfd = objfile->obfd;
3349 gdb_byte *buffer = section->buffer;
3350 gdb_byte *info_ptr = buffer + this_cu->offset;
3351 unsigned int buffer_size = section->size;
3352 gdb_byte *beg_of_comp_unit = info_ptr;
3353 struct die_info *comp_unit_die;
3354 struct partial_symtab *pst;
3355 CORE_ADDR baseaddr;
3356 struct cleanup *back_to_inner;
3357 struct dwarf2_cu cu;
3358 int has_children, has_pc_info;
3359 struct attribute *attr;
3360 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3361 struct die_reader_specs reader_specs;
3362 const char *filename;
3363
3364 /* If this compilation unit was already read in, free the
3365 cached copy in order to read it in again. This is
3366 necessary because we skipped some symbols when we first
3367 read in the compilation unit (see load_partial_dies).
3368 This problem could be avoided, but the benefit is
3369 unclear. */
3370 if (this_cu->cu != NULL)
3371 free_one_cached_comp_unit (this_cu->cu);
3372
3373 /* Note that this is a pointer to our stack frame, being
3374 added to a global data structure. It will be cleaned up
3375 in free_stack_comp_unit when we finish with this
3376 compilation unit. */
3377 init_one_comp_unit (&cu, this_cu);
3378 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3379
3380 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3381 buffer, buffer_size,
3382 abfd,
3383 is_debug_types_section);
3384
3385 /* Skip dummy compilation units. */
3386 if (info_ptr >= buffer + buffer_size
3387 || peek_abbrev_code (abfd, info_ptr) == 0)
3388 {
3389 do_cleanups (back_to_inner);
3390 return;
3391 }
3392
3393 cu.list_in_scope = &file_symbols;
3394
3395 /* Read the abbrevs for this compilation unit into a table. */
3396 dwarf2_read_abbrevs (&cu);
3397 make_cleanup (dwarf2_free_abbrev_table, &cu);
3398
3399 /* Read the compilation unit die. */
3400 init_cu_die_reader (&reader_specs, &cu);
3401 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3402 &has_children);
3403
3404 if (is_debug_types_section)
3405 {
3406 /* LENGTH has not been set yet for type units. */
3407 gdb_assert (this_cu->offset == cu.header.offset);
3408 this_cu->length = cu.header.length + cu.header.initial_length_size;
3409 }
3410 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3411 {
3412 do_cleanups (back_to_inner);
3413 return;
3414 }
3415
3416 prepare_one_comp_unit (&cu, comp_unit_die);
3417
3418 /* Allocate a new partial symbol table structure. */
3419 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3420 if (attr == NULL || !DW_STRING (attr))
3421 filename = "";
3422 else
3423 filename = DW_STRING (attr);
3424 pst = start_psymtab_common (objfile, objfile->section_offsets,
3425 filename,
3426 /* TEXTLOW and TEXTHIGH are set below. */
3427 0,
3428 objfile->global_psymbols.next,
3429 objfile->static_psymbols.next);
3430 pst->psymtabs_addrmap_supported = 1;
3431
3432 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3433 if (attr != NULL)
3434 pst->dirname = DW_STRING (attr);
3435
3436 pst->read_symtab_private = this_cu;
3437
3438 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3439
3440 /* Store the function that reads in the rest of the symbol table. */
3441 pst->read_symtab = dwarf2_psymtab_to_symtab;
3442
3443 this_cu->v.psymtab = pst;
3444
3445 dwarf2_find_base_address (comp_unit_die, &cu);
3446
3447 /* Possibly set the default values of LOWPC and HIGHPC from
3448 `DW_AT_ranges'. */
3449 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3450 &best_highpc, &cu, pst);
3451 if (has_pc_info == 1 && best_lowpc < best_highpc)
3452 /* Store the contiguous range if it is not empty; it can be empty for
3453 CUs with no code. */
3454 addrmap_set_empty (objfile->psymtabs_addrmap,
3455 best_lowpc + baseaddr,
3456 best_highpc + baseaddr - 1, pst);
3457
3458 /* Check if comp unit has_children.
3459 If so, read the rest of the partial symbols from this comp unit.
3460 If not, there's no more debug_info for this comp unit. */
3461 if (has_children)
3462 {
3463 struct partial_die_info *first_die;
3464 CORE_ADDR lowpc, highpc;
3465
3466 lowpc = ((CORE_ADDR) -1);
3467 highpc = ((CORE_ADDR) 0);
3468
3469 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3470
3471 scan_partial_symbols (first_die, &lowpc, &highpc,
3472 ! has_pc_info, &cu);
3473
3474 /* If we didn't find a lowpc, set it to highpc to avoid
3475 complaints from `maint check'. */
3476 if (lowpc == ((CORE_ADDR) -1))
3477 lowpc = highpc;
3478
3479 /* If the compilation unit didn't have an explicit address range,
3480 then use the information extracted from its child dies. */
3481 if (! has_pc_info)
3482 {
3483 best_lowpc = lowpc;
3484 best_highpc = highpc;
3485 }
3486 }
3487 pst->textlow = best_lowpc + baseaddr;
3488 pst->texthigh = best_highpc + baseaddr;
3489
3490 pst->n_global_syms = objfile->global_psymbols.next -
3491 (objfile->global_psymbols.list + pst->globals_offset);
3492 pst->n_static_syms = objfile->static_psymbols.next -
3493 (objfile->static_psymbols.list + pst->statics_offset);
3494 sort_pst_symbols (pst);
3495
3496 if (is_debug_types_section)
3497 {
3498 /* It's not clear we want to do anything with stmt lists here.
3499 Waiting to see what gcc ultimately does. */
3500 }
3501 else
3502 {
3503 /* Get the list of files included in the current compilation unit,
3504 and build a psymtab for each of them. */
3505 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3506 }
3507
3508 do_cleanups (back_to_inner);
3509 }
3510
3511 /* Traversal function for htab_traverse_noresize.
3512 Process one .debug_types comp-unit. */
3513
3514 static int
3515 process_type_comp_unit (void **slot, void *info)
3516 {
3517 struct signatured_type *entry = (struct signatured_type *) *slot;
3518 struct dwarf2_per_cu_data *this_cu;
3519
3520 gdb_assert (info == NULL);
3521 this_cu = &entry->per_cu;
3522
3523 gdb_assert (this_cu->debug_types_section->readin);
3524 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3525
3526 return 1;
3527 }
3528
3529 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3530 Build partial symbol tables for the .debug_types comp-units. */
3531
3532 static void
3533 build_type_psymtabs (struct objfile *objfile)
3534 {
3535 if (! create_debug_types_hash_table (objfile))
3536 return;
3537
3538 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3539 process_type_comp_unit, NULL);
3540 }
3541
3542 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3543
3544 static void
3545 psymtabs_addrmap_cleanup (void *o)
3546 {
3547 struct objfile *objfile = o;
3548
3549 objfile->psymtabs_addrmap = NULL;
3550 }
3551
3552 /* Build the partial symbol table by doing a quick pass through the
3553 .debug_info and .debug_abbrev sections. */
3554
3555 static void
3556 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3557 {
3558 struct cleanup *back_to, *addrmap_cleanup;
3559 struct obstack temp_obstack;
3560 int i;
3561
3562 dwarf2_per_objfile->reading_partial_symbols = 1;
3563
3564 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3565
3566 /* Any cached compilation units will be linked by the per-objfile
3567 read_in_chain. Make sure to free them when we're done. */
3568 back_to = make_cleanup (free_cached_comp_units, NULL);
3569
3570 build_type_psymtabs (objfile);
3571
3572 create_all_comp_units (objfile);
3573
3574 /* Create a temporary address map on a temporary obstack. We later
3575 copy this to the final obstack. */
3576 obstack_init (&temp_obstack);
3577 make_cleanup_obstack_free (&temp_obstack);
3578 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3579 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3580
3581 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3582 {
3583 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3584
3585 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3586 }
3587
3588 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3589 &objfile->objfile_obstack);
3590 discard_cleanups (addrmap_cleanup);
3591
3592 do_cleanups (back_to);
3593 }
3594
3595 /* Load the partial DIEs for a secondary CU into memory. */
3596
3597 static void
3598 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3599 {
3600 struct objfile *objfile = this_cu->objfile;
3601 bfd *abfd = objfile->obfd;
3602 gdb_byte *info_ptr;
3603 struct die_info *comp_unit_die;
3604 struct dwarf2_cu *cu;
3605 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3606 int has_children;
3607 struct die_reader_specs reader_specs;
3608 int read_cu = 0;
3609
3610 gdb_assert (! this_cu->debug_types_section);
3611
3612 gdb_assert (dwarf2_per_objfile->info.readin);
3613 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3614
3615 if (this_cu->cu == NULL)
3616 {
3617 cu = xmalloc (sizeof (*cu));
3618 init_one_comp_unit (cu, this_cu);
3619
3620 read_cu = 1;
3621
3622 /* If an error occurs while loading, release our storage. */
3623 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3624
3625 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3626 dwarf2_per_objfile->info.buffer,
3627 dwarf2_per_objfile->info.size,
3628 abfd, 0);
3629
3630 /* Skip dummy compilation units. */
3631 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3632 + dwarf2_per_objfile->info.size)
3633 || peek_abbrev_code (abfd, info_ptr) == 0)
3634 {
3635 do_cleanups (free_cu_cleanup);
3636 return;
3637 }
3638
3639 /* Link this CU into read_in_chain. */
3640 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3641 dwarf2_per_objfile->read_in_chain = this_cu;
3642 }
3643 else
3644 {
3645 cu = this_cu->cu;
3646 info_ptr += cu->header.first_die_offset;
3647 }
3648
3649 /* Read the abbrevs for this compilation unit into a table. */
3650 gdb_assert (cu->dwarf2_abbrevs == NULL);
3651 dwarf2_read_abbrevs (cu);
3652 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3653
3654 /* Read the compilation unit die. */
3655 init_cu_die_reader (&reader_specs, cu);
3656 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3657 &has_children);
3658
3659 prepare_one_comp_unit (cu, comp_unit_die);
3660
3661 /* Check if comp unit has_children.
3662 If so, read the rest of the partial symbols from this comp unit.
3663 If not, there's no more debug_info for this comp unit. */
3664 if (has_children)
3665 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3666
3667 do_cleanups (free_abbrevs_cleanup);
3668
3669 if (read_cu)
3670 {
3671 /* We've successfully allocated this compilation unit. Let our
3672 caller clean it up when finished with it. */
3673 discard_cleanups (free_cu_cleanup);
3674 }
3675 }
3676
3677 /* Create a list of all compilation units in OBJFILE.
3678 This is only done for -readnow and building partial symtabs. */
3679
3680 static void
3681 create_all_comp_units (struct objfile *objfile)
3682 {
3683 int n_allocated;
3684 int n_comp_units;
3685 struct dwarf2_per_cu_data **all_comp_units;
3686 gdb_byte *info_ptr;
3687
3688 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3689 info_ptr = dwarf2_per_objfile->info.buffer;
3690
3691 n_comp_units = 0;
3692 n_allocated = 10;
3693 all_comp_units = xmalloc (n_allocated
3694 * sizeof (struct dwarf2_per_cu_data *));
3695
3696 while (info_ptr < dwarf2_per_objfile->info.buffer
3697 + dwarf2_per_objfile->info.size)
3698 {
3699 unsigned int length, initial_length_size;
3700 struct dwarf2_per_cu_data *this_cu;
3701 unsigned int offset;
3702
3703 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3704
3705 /* Read just enough information to find out where the next
3706 compilation unit is. */
3707 length = read_initial_length (objfile->obfd, info_ptr,
3708 &initial_length_size);
3709
3710 /* Save the compilation unit for later lookup. */
3711 this_cu = obstack_alloc (&objfile->objfile_obstack,
3712 sizeof (struct dwarf2_per_cu_data));
3713 memset (this_cu, 0, sizeof (*this_cu));
3714 this_cu->offset = offset;
3715 this_cu->length = length + initial_length_size;
3716 this_cu->objfile = objfile;
3717
3718 if (n_comp_units == n_allocated)
3719 {
3720 n_allocated *= 2;
3721 all_comp_units = xrealloc (all_comp_units,
3722 n_allocated
3723 * sizeof (struct dwarf2_per_cu_data *));
3724 }
3725 all_comp_units[n_comp_units++] = this_cu;
3726
3727 info_ptr = info_ptr + this_cu->length;
3728 }
3729
3730 dwarf2_per_objfile->all_comp_units
3731 = obstack_alloc (&objfile->objfile_obstack,
3732 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3733 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3734 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3735 xfree (all_comp_units);
3736 dwarf2_per_objfile->n_comp_units = n_comp_units;
3737 }
3738
3739 /* Process all loaded DIEs for compilation unit CU, starting at
3740 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3741 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3742 DW_AT_ranges). If NEED_PC is set, then this function will set
3743 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3744 and record the covered ranges in the addrmap. */
3745
3746 static void
3747 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3748 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3749 {
3750 struct partial_die_info *pdi;
3751
3752 /* Now, march along the PDI's, descending into ones which have
3753 interesting children but skipping the children of the other ones,
3754 until we reach the end of the compilation unit. */
3755
3756 pdi = first_die;
3757
3758 while (pdi != NULL)
3759 {
3760 fixup_partial_die (pdi, cu);
3761
3762 /* Anonymous namespaces or modules have no name but have interesting
3763 children, so we need to look at them. Ditto for anonymous
3764 enums. */
3765
3766 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3767 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3768 {
3769 switch (pdi->tag)
3770 {
3771 case DW_TAG_subprogram:
3772 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3773 break;
3774 case DW_TAG_constant:
3775 case DW_TAG_variable:
3776 case DW_TAG_typedef:
3777 case DW_TAG_union_type:
3778 if (!pdi->is_declaration)
3779 {
3780 add_partial_symbol (pdi, cu);
3781 }
3782 break;
3783 case DW_TAG_class_type:
3784 case DW_TAG_interface_type:
3785 case DW_TAG_structure_type:
3786 if (!pdi->is_declaration)
3787 {
3788 add_partial_symbol (pdi, cu);
3789 }
3790 break;
3791 case DW_TAG_enumeration_type:
3792 if (!pdi->is_declaration)
3793 add_partial_enumeration (pdi, cu);
3794 break;
3795 case DW_TAG_base_type:
3796 case DW_TAG_subrange_type:
3797 /* File scope base type definitions are added to the partial
3798 symbol table. */
3799 add_partial_symbol (pdi, cu);
3800 break;
3801 case DW_TAG_namespace:
3802 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3803 break;
3804 case DW_TAG_module:
3805 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3806 break;
3807 default:
3808 break;
3809 }
3810 }
3811
3812 /* If the die has a sibling, skip to the sibling. */
3813
3814 pdi = pdi->die_sibling;
3815 }
3816 }
3817
3818 /* Functions used to compute the fully scoped name of a partial DIE.
3819
3820 Normally, this is simple. For C++, the parent DIE's fully scoped
3821 name is concatenated with "::" and the partial DIE's name. For
3822 Java, the same thing occurs except that "." is used instead of "::".
3823 Enumerators are an exception; they use the scope of their parent
3824 enumeration type, i.e. the name of the enumeration type is not
3825 prepended to the enumerator.
3826
3827 There are two complexities. One is DW_AT_specification; in this
3828 case "parent" means the parent of the target of the specification,
3829 instead of the direct parent of the DIE. The other is compilers
3830 which do not emit DW_TAG_namespace; in this case we try to guess
3831 the fully qualified name of structure types from their members'
3832 linkage names. This must be done using the DIE's children rather
3833 than the children of any DW_AT_specification target. We only need
3834 to do this for structures at the top level, i.e. if the target of
3835 any DW_AT_specification (if any; otherwise the DIE itself) does not
3836 have a parent. */
3837
3838 /* Compute the scope prefix associated with PDI's parent, in
3839 compilation unit CU. The result will be allocated on CU's
3840 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3841 field. NULL is returned if no prefix is necessary. */
3842 static char *
3843 partial_die_parent_scope (struct partial_die_info *pdi,
3844 struct dwarf2_cu *cu)
3845 {
3846 char *grandparent_scope;
3847 struct partial_die_info *parent, *real_pdi;
3848
3849 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3850 then this means the parent of the specification DIE. */
3851
3852 real_pdi = pdi;
3853 while (real_pdi->has_specification)
3854 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3855
3856 parent = real_pdi->die_parent;
3857 if (parent == NULL)
3858 return NULL;
3859
3860 if (parent->scope_set)
3861 return parent->scope;
3862
3863 fixup_partial_die (parent, cu);
3864
3865 grandparent_scope = partial_die_parent_scope (parent, cu);
3866
3867 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3868 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3869 Work around this problem here. */
3870 if (cu->language == language_cplus
3871 && parent->tag == DW_TAG_namespace
3872 && strcmp (parent->name, "::") == 0
3873 && grandparent_scope == NULL)
3874 {
3875 parent->scope = NULL;
3876 parent->scope_set = 1;
3877 return NULL;
3878 }
3879
3880 if (pdi->tag == DW_TAG_enumerator)
3881 /* Enumerators should not get the name of the enumeration as a prefix. */
3882 parent->scope = grandparent_scope;
3883 else if (parent->tag == DW_TAG_namespace
3884 || parent->tag == DW_TAG_module
3885 || parent->tag == DW_TAG_structure_type
3886 || parent->tag == DW_TAG_class_type
3887 || parent->tag == DW_TAG_interface_type
3888 || parent->tag == DW_TAG_union_type
3889 || parent->tag == DW_TAG_enumeration_type)
3890 {
3891 if (grandparent_scope == NULL)
3892 parent->scope = parent->name;
3893 else
3894 parent->scope = typename_concat (&cu->comp_unit_obstack,
3895 grandparent_scope,
3896 parent->name, 0, cu);
3897 }
3898 else
3899 {
3900 /* FIXME drow/2004-04-01: What should we be doing with
3901 function-local names? For partial symbols, we should probably be
3902 ignoring them. */
3903 complaint (&symfile_complaints,
3904 _("unhandled containing DIE tag %d for DIE at %d"),
3905 parent->tag, pdi->offset);
3906 parent->scope = grandparent_scope;
3907 }
3908
3909 parent->scope_set = 1;
3910 return parent->scope;
3911 }
3912
3913 /* Return the fully scoped name associated with PDI, from compilation unit
3914 CU. The result will be allocated with malloc. */
3915 static char *
3916 partial_die_full_name (struct partial_die_info *pdi,
3917 struct dwarf2_cu *cu)
3918 {
3919 char *parent_scope;
3920
3921 /* If this is a template instantiation, we can not work out the
3922 template arguments from partial DIEs. So, unfortunately, we have
3923 to go through the full DIEs. At least any work we do building
3924 types here will be reused if full symbols are loaded later. */
3925 if (pdi->has_template_arguments)
3926 {
3927 fixup_partial_die (pdi, cu);
3928
3929 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3930 {
3931 struct die_info *die;
3932 struct attribute attr;
3933 struct dwarf2_cu *ref_cu = cu;
3934
3935 attr.name = 0;
3936 attr.form = DW_FORM_ref_addr;
3937 attr.u.addr = pdi->offset;
3938 die = follow_die_ref (NULL, &attr, &ref_cu);
3939
3940 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3941 }
3942 }
3943
3944 parent_scope = partial_die_parent_scope (pdi, cu);
3945 if (parent_scope == NULL)
3946 return NULL;
3947 else
3948 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3949 }
3950
3951 static void
3952 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3953 {
3954 struct objfile *objfile = cu->objfile;
3955 CORE_ADDR addr = 0;
3956 char *actual_name = NULL;
3957 const struct partial_symbol *psym = NULL;
3958 CORE_ADDR baseaddr;
3959 int built_actual_name = 0;
3960
3961 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3962
3963 actual_name = partial_die_full_name (pdi, cu);
3964 if (actual_name)
3965 built_actual_name = 1;
3966
3967 if (actual_name == NULL)
3968 actual_name = pdi->name;
3969
3970 switch (pdi->tag)
3971 {
3972 case DW_TAG_subprogram:
3973 if (pdi->is_external || cu->language == language_ada)
3974 {
3975 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3976 of the global scope. But in Ada, we want to be able to access
3977 nested procedures globally. So all Ada subprograms are stored
3978 in the global scope. */
3979 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3980 mst_text, objfile); */
3981 add_psymbol_to_list (actual_name, strlen (actual_name),
3982 built_actual_name,
3983 VAR_DOMAIN, LOC_BLOCK,
3984 &objfile->global_psymbols,
3985 0, pdi->lowpc + baseaddr,
3986 cu->language, objfile);
3987 }
3988 else
3989 {
3990 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3991 mst_file_text, objfile); */
3992 add_psymbol_to_list (actual_name, strlen (actual_name),
3993 built_actual_name,
3994 VAR_DOMAIN, LOC_BLOCK,
3995 &objfile->static_psymbols,
3996 0, pdi->lowpc + baseaddr,
3997 cu->language, objfile);
3998 }
3999 break;
4000 case DW_TAG_constant:
4001 {
4002 struct psymbol_allocation_list *list;
4003
4004 if (pdi->is_external)
4005 list = &objfile->global_psymbols;
4006 else
4007 list = &objfile->static_psymbols;
4008 add_psymbol_to_list (actual_name, strlen (actual_name),
4009 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4010 list, 0, 0, cu->language, objfile);
4011 }
4012 break;
4013 case DW_TAG_variable:
4014 if (pdi->locdesc)
4015 addr = decode_locdesc (pdi->locdesc, cu);
4016
4017 if (pdi->locdesc
4018 && addr == 0
4019 && !dwarf2_per_objfile->has_section_at_zero)
4020 {
4021 /* A global or static variable may also have been stripped
4022 out by the linker if unused, in which case its address
4023 will be nullified; do not add such variables into partial
4024 symbol table then. */
4025 }
4026 else if (pdi->is_external)
4027 {
4028 /* Global Variable.
4029 Don't enter into the minimal symbol tables as there is
4030 a minimal symbol table entry from the ELF symbols already.
4031 Enter into partial symbol table if it has a location
4032 descriptor or a type.
4033 If the location descriptor is missing, new_symbol will create
4034 a LOC_UNRESOLVED symbol, the address of the variable will then
4035 be determined from the minimal symbol table whenever the variable
4036 is referenced.
4037 The address for the partial symbol table entry is not
4038 used by GDB, but it comes in handy for debugging partial symbol
4039 table building. */
4040
4041 if (pdi->locdesc || pdi->has_type)
4042 add_psymbol_to_list (actual_name, strlen (actual_name),
4043 built_actual_name,
4044 VAR_DOMAIN, LOC_STATIC,
4045 &objfile->global_psymbols,
4046 0, addr + baseaddr,
4047 cu->language, objfile);
4048 }
4049 else
4050 {
4051 /* Static Variable. Skip symbols without location descriptors. */
4052 if (pdi->locdesc == NULL)
4053 {
4054 if (built_actual_name)
4055 xfree (actual_name);
4056 return;
4057 }
4058 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4059 mst_file_data, objfile); */
4060 add_psymbol_to_list (actual_name, strlen (actual_name),
4061 built_actual_name,
4062 VAR_DOMAIN, LOC_STATIC,
4063 &objfile->static_psymbols,
4064 0, addr + baseaddr,
4065 cu->language, objfile);
4066 }
4067 break;
4068 case DW_TAG_typedef:
4069 case DW_TAG_base_type:
4070 case DW_TAG_subrange_type:
4071 add_psymbol_to_list (actual_name, strlen (actual_name),
4072 built_actual_name,
4073 VAR_DOMAIN, LOC_TYPEDEF,
4074 &objfile->static_psymbols,
4075 0, (CORE_ADDR) 0, cu->language, objfile);
4076 break;
4077 case DW_TAG_namespace:
4078 add_psymbol_to_list (actual_name, strlen (actual_name),
4079 built_actual_name,
4080 VAR_DOMAIN, LOC_TYPEDEF,
4081 &objfile->global_psymbols,
4082 0, (CORE_ADDR) 0, cu->language, objfile);
4083 break;
4084 case DW_TAG_class_type:
4085 case DW_TAG_interface_type:
4086 case DW_TAG_structure_type:
4087 case DW_TAG_union_type:
4088 case DW_TAG_enumeration_type:
4089 /* Skip external references. The DWARF standard says in the section
4090 about "Structure, Union, and Class Type Entries": "An incomplete
4091 structure, union or class type is represented by a structure,
4092 union or class entry that does not have a byte size attribute
4093 and that has a DW_AT_declaration attribute." */
4094 if (!pdi->has_byte_size && pdi->is_declaration)
4095 {
4096 if (built_actual_name)
4097 xfree (actual_name);
4098 return;
4099 }
4100
4101 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4102 static vs. global. */
4103 add_psymbol_to_list (actual_name, strlen (actual_name),
4104 built_actual_name,
4105 STRUCT_DOMAIN, LOC_TYPEDEF,
4106 (cu->language == language_cplus
4107 || cu->language == language_java)
4108 ? &objfile->global_psymbols
4109 : &objfile->static_psymbols,
4110 0, (CORE_ADDR) 0, cu->language, objfile);
4111
4112 break;
4113 case DW_TAG_enumerator:
4114 add_psymbol_to_list (actual_name, strlen (actual_name),
4115 built_actual_name,
4116 VAR_DOMAIN, LOC_CONST,
4117 (cu->language == language_cplus
4118 || cu->language == language_java)
4119 ? &objfile->global_psymbols
4120 : &objfile->static_psymbols,
4121 0, (CORE_ADDR) 0, cu->language, objfile);
4122 break;
4123 default:
4124 break;
4125 }
4126
4127 if (built_actual_name)
4128 xfree (actual_name);
4129 }
4130
4131 /* Read a partial die corresponding to a namespace; also, add a symbol
4132 corresponding to that namespace to the symbol table. NAMESPACE is
4133 the name of the enclosing namespace. */
4134
4135 static void
4136 add_partial_namespace (struct partial_die_info *pdi,
4137 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4138 int need_pc, struct dwarf2_cu *cu)
4139 {
4140 /* Add a symbol for the namespace. */
4141
4142 add_partial_symbol (pdi, cu);
4143
4144 /* Now scan partial symbols in that namespace. */
4145
4146 if (pdi->has_children)
4147 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4148 }
4149
4150 /* Read a partial die corresponding to a Fortran module. */
4151
4152 static void
4153 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4154 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4155 {
4156 /* Now scan partial symbols in that module. */
4157
4158 if (pdi->has_children)
4159 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4160 }
4161
4162 /* Read a partial die corresponding to a subprogram and create a partial
4163 symbol for that subprogram. When the CU language allows it, this
4164 routine also defines a partial symbol for each nested subprogram
4165 that this subprogram contains.
4166
4167 DIE my also be a lexical block, in which case we simply search
4168 recursively for suprograms defined inside that lexical block.
4169 Again, this is only performed when the CU language allows this
4170 type of definitions. */
4171
4172 static void
4173 add_partial_subprogram (struct partial_die_info *pdi,
4174 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4175 int need_pc, struct dwarf2_cu *cu)
4176 {
4177 if (pdi->tag == DW_TAG_subprogram)
4178 {
4179 if (pdi->has_pc_info)
4180 {
4181 if (pdi->lowpc < *lowpc)
4182 *lowpc = pdi->lowpc;
4183 if (pdi->highpc > *highpc)
4184 *highpc = pdi->highpc;
4185 if (need_pc)
4186 {
4187 CORE_ADDR baseaddr;
4188 struct objfile *objfile = cu->objfile;
4189
4190 baseaddr = ANOFFSET (objfile->section_offsets,
4191 SECT_OFF_TEXT (objfile));
4192 addrmap_set_empty (objfile->psymtabs_addrmap,
4193 pdi->lowpc + baseaddr,
4194 pdi->highpc - 1 + baseaddr,
4195 cu->per_cu->v.psymtab);
4196 }
4197 if (!pdi->is_declaration)
4198 /* Ignore subprogram DIEs that do not have a name, they are
4199 illegal. Do not emit a complaint at this point, we will
4200 do so when we convert this psymtab into a symtab. */
4201 if (pdi->name)
4202 add_partial_symbol (pdi, cu);
4203 }
4204 }
4205
4206 if (! pdi->has_children)
4207 return;
4208
4209 if (cu->language == language_ada)
4210 {
4211 pdi = pdi->die_child;
4212 while (pdi != NULL)
4213 {
4214 fixup_partial_die (pdi, cu);
4215 if (pdi->tag == DW_TAG_subprogram
4216 || pdi->tag == DW_TAG_lexical_block)
4217 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4218 pdi = pdi->die_sibling;
4219 }
4220 }
4221 }
4222
4223 /* Read a partial die corresponding to an enumeration type. */
4224
4225 static void
4226 add_partial_enumeration (struct partial_die_info *enum_pdi,
4227 struct dwarf2_cu *cu)
4228 {
4229 struct partial_die_info *pdi;
4230
4231 if (enum_pdi->name != NULL)
4232 add_partial_symbol (enum_pdi, cu);
4233
4234 pdi = enum_pdi->die_child;
4235 while (pdi)
4236 {
4237 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4238 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4239 else
4240 add_partial_symbol (pdi, cu);
4241 pdi = pdi->die_sibling;
4242 }
4243 }
4244
4245 /* Return the initial uleb128 in the die at INFO_PTR. */
4246
4247 static unsigned int
4248 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4249 {
4250 unsigned int bytes_read;
4251
4252 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4253 }
4254
4255 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4256 Return the corresponding abbrev, or NULL if the number is zero (indicating
4257 an empty DIE). In either case *BYTES_READ will be set to the length of
4258 the initial number. */
4259
4260 static struct abbrev_info *
4261 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4262 struct dwarf2_cu *cu)
4263 {
4264 bfd *abfd = cu->objfile->obfd;
4265 unsigned int abbrev_number;
4266 struct abbrev_info *abbrev;
4267
4268 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4269
4270 if (abbrev_number == 0)
4271 return NULL;
4272
4273 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4274 if (!abbrev)
4275 {
4276 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4277 abbrev_number, bfd_get_filename (abfd));
4278 }
4279
4280 return abbrev;
4281 }
4282
4283 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4284 Returns a pointer to the end of a series of DIEs, terminated by an empty
4285 DIE. Any children of the skipped DIEs will also be skipped. */
4286
4287 static gdb_byte *
4288 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4289 {
4290 struct abbrev_info *abbrev;
4291 unsigned int bytes_read;
4292
4293 while (1)
4294 {
4295 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4296 if (abbrev == NULL)
4297 return info_ptr + bytes_read;
4298 else
4299 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4300 }
4301 }
4302
4303 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4304 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4305 abbrev corresponding to that skipped uleb128 should be passed in
4306 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4307 children. */
4308
4309 static gdb_byte *
4310 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4311 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4312 {
4313 unsigned int bytes_read;
4314 struct attribute attr;
4315 bfd *abfd = cu->objfile->obfd;
4316 unsigned int form, i;
4317
4318 for (i = 0; i < abbrev->num_attrs; i++)
4319 {
4320 /* The only abbrev we care about is DW_AT_sibling. */
4321 if (abbrev->attrs[i].name == DW_AT_sibling)
4322 {
4323 read_attribute (&attr, &abbrev->attrs[i],
4324 abfd, info_ptr, cu);
4325 if (attr.form == DW_FORM_ref_addr)
4326 complaint (&symfile_complaints,
4327 _("ignoring absolute DW_AT_sibling"));
4328 else
4329 return buffer + dwarf2_get_ref_die_offset (&attr);
4330 }
4331
4332 /* If it isn't DW_AT_sibling, skip this attribute. */
4333 form = abbrev->attrs[i].form;
4334 skip_attribute:
4335 switch (form)
4336 {
4337 case DW_FORM_ref_addr:
4338 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4339 and later it is offset sized. */
4340 if (cu->header.version == 2)
4341 info_ptr += cu->header.addr_size;
4342 else
4343 info_ptr += cu->header.offset_size;
4344 break;
4345 case DW_FORM_addr:
4346 info_ptr += cu->header.addr_size;
4347 break;
4348 case DW_FORM_data1:
4349 case DW_FORM_ref1:
4350 case DW_FORM_flag:
4351 info_ptr += 1;
4352 break;
4353 case DW_FORM_flag_present:
4354 break;
4355 case DW_FORM_data2:
4356 case DW_FORM_ref2:
4357 info_ptr += 2;
4358 break;
4359 case DW_FORM_data4:
4360 case DW_FORM_ref4:
4361 info_ptr += 4;
4362 break;
4363 case DW_FORM_data8:
4364 case DW_FORM_ref8:
4365 case DW_FORM_ref_sig8:
4366 info_ptr += 8;
4367 break;
4368 case DW_FORM_string:
4369 read_direct_string (abfd, info_ptr, &bytes_read);
4370 info_ptr += bytes_read;
4371 break;
4372 case DW_FORM_sec_offset:
4373 case DW_FORM_strp:
4374 info_ptr += cu->header.offset_size;
4375 break;
4376 case DW_FORM_exprloc:
4377 case DW_FORM_block:
4378 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4379 info_ptr += bytes_read;
4380 break;
4381 case DW_FORM_block1:
4382 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4383 break;
4384 case DW_FORM_block2:
4385 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4386 break;
4387 case DW_FORM_block4:
4388 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4389 break;
4390 case DW_FORM_sdata:
4391 case DW_FORM_udata:
4392 case DW_FORM_ref_udata:
4393 info_ptr = skip_leb128 (abfd, info_ptr);
4394 break;
4395 case DW_FORM_indirect:
4396 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4397 info_ptr += bytes_read;
4398 /* We need to continue parsing from here, so just go back to
4399 the top. */
4400 goto skip_attribute;
4401
4402 default:
4403 error (_("Dwarf Error: Cannot handle %s "
4404 "in DWARF reader [in module %s]"),
4405 dwarf_form_name (form),
4406 bfd_get_filename (abfd));
4407 }
4408 }
4409
4410 if (abbrev->has_children)
4411 return skip_children (buffer, info_ptr, cu);
4412 else
4413 return info_ptr;
4414 }
4415
4416 /* Locate ORIG_PDI's sibling.
4417 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4418 in BUFFER. */
4419
4420 static gdb_byte *
4421 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4422 gdb_byte *buffer, gdb_byte *info_ptr,
4423 bfd *abfd, struct dwarf2_cu *cu)
4424 {
4425 /* Do we know the sibling already? */
4426
4427 if (orig_pdi->sibling)
4428 return orig_pdi->sibling;
4429
4430 /* Are there any children to deal with? */
4431
4432 if (!orig_pdi->has_children)
4433 return info_ptr;
4434
4435 /* Skip the children the long way. */
4436
4437 return skip_children (buffer, info_ptr, cu);
4438 }
4439
4440 /* Expand this partial symbol table into a full symbol table. */
4441
4442 static void
4443 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4444 {
4445 if (pst != NULL)
4446 {
4447 if (pst->readin)
4448 {
4449 warning (_("bug: psymtab for %s is already read in."),
4450 pst->filename);
4451 }
4452 else
4453 {
4454 if (info_verbose)
4455 {
4456 printf_filtered (_("Reading in symbols for %s..."),
4457 pst->filename);
4458 gdb_flush (gdb_stdout);
4459 }
4460
4461 /* Restore our global data. */
4462 dwarf2_per_objfile = objfile_data (pst->objfile,
4463 dwarf2_objfile_data_key);
4464
4465 /* If this psymtab is constructed from a debug-only objfile, the
4466 has_section_at_zero flag will not necessarily be correct. We
4467 can get the correct value for this flag by looking at the data
4468 associated with the (presumably stripped) associated objfile. */
4469 if (pst->objfile->separate_debug_objfile_backlink)
4470 {
4471 struct dwarf2_per_objfile *dpo_backlink
4472 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4473 dwarf2_objfile_data_key);
4474
4475 dwarf2_per_objfile->has_section_at_zero
4476 = dpo_backlink->has_section_at_zero;
4477 }
4478
4479 dwarf2_per_objfile->reading_partial_symbols = 0;
4480
4481 psymtab_to_symtab_1 (pst);
4482
4483 /* Finish up the debug error message. */
4484 if (info_verbose)
4485 printf_filtered (_("done.\n"));
4486 }
4487 }
4488 }
4489 \f
4490 /* Reading in full CUs. */
4491
4492 /* Add PER_CU to the queue. */
4493
4494 static void
4495 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4496 {
4497 struct dwarf2_queue_item *item;
4498
4499 per_cu->queued = 1;
4500 item = xmalloc (sizeof (*item));
4501 item->per_cu = per_cu;
4502 item->next = NULL;
4503
4504 if (dwarf2_queue == NULL)
4505 dwarf2_queue = item;
4506 else
4507 dwarf2_queue_tail->next = item;
4508
4509 dwarf2_queue_tail = item;
4510 }
4511
4512 /* Process the queue. */
4513
4514 static void
4515 process_queue (void)
4516 {
4517 struct dwarf2_queue_item *item, *next_item;
4518
4519 /* The queue starts out with one item, but following a DIE reference
4520 may load a new CU, adding it to the end of the queue. */
4521 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4522 {
4523 if (dwarf2_per_objfile->using_index
4524 ? !item->per_cu->v.quick->symtab
4525 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4526 process_full_comp_unit (item->per_cu);
4527
4528 item->per_cu->queued = 0;
4529 next_item = item->next;
4530 xfree (item);
4531 }
4532
4533 dwarf2_queue_tail = NULL;
4534 }
4535
4536 /* Free all allocated queue entries. This function only releases anything if
4537 an error was thrown; if the queue was processed then it would have been
4538 freed as we went along. */
4539
4540 static void
4541 dwarf2_release_queue (void *dummy)
4542 {
4543 struct dwarf2_queue_item *item, *last;
4544
4545 item = dwarf2_queue;
4546 while (item)
4547 {
4548 /* Anything still marked queued is likely to be in an
4549 inconsistent state, so discard it. */
4550 if (item->per_cu->queued)
4551 {
4552 if (item->per_cu->cu != NULL)
4553 free_one_cached_comp_unit (item->per_cu->cu);
4554 item->per_cu->queued = 0;
4555 }
4556
4557 last = item;
4558 item = item->next;
4559 xfree (last);
4560 }
4561
4562 dwarf2_queue = dwarf2_queue_tail = NULL;
4563 }
4564
4565 /* Read in full symbols for PST, and anything it depends on. */
4566
4567 static void
4568 psymtab_to_symtab_1 (struct partial_symtab *pst)
4569 {
4570 struct dwarf2_per_cu_data *per_cu;
4571 struct cleanup *back_to;
4572 int i;
4573
4574 for (i = 0; i < pst->number_of_dependencies; i++)
4575 if (!pst->dependencies[i]->readin)
4576 {
4577 /* Inform about additional files that need to be read in. */
4578 if (info_verbose)
4579 {
4580 /* FIXME: i18n: Need to make this a single string. */
4581 fputs_filtered (" ", gdb_stdout);
4582 wrap_here ("");
4583 fputs_filtered ("and ", gdb_stdout);
4584 wrap_here ("");
4585 printf_filtered ("%s...", pst->dependencies[i]->filename);
4586 wrap_here (""); /* Flush output. */
4587 gdb_flush (gdb_stdout);
4588 }
4589 psymtab_to_symtab_1 (pst->dependencies[i]);
4590 }
4591
4592 per_cu = pst->read_symtab_private;
4593
4594 if (per_cu == NULL)
4595 {
4596 /* It's an include file, no symbols to read for it.
4597 Everything is in the parent symtab. */
4598 pst->readin = 1;
4599 return;
4600 }
4601
4602 dw2_do_instantiate_symtab (per_cu);
4603 }
4604
4605 /* Load the DIEs associated with PER_CU into memory. */
4606
4607 static void
4608 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4609 {
4610 struct objfile *objfile = per_cu->objfile;
4611 bfd *abfd = objfile->obfd;
4612 struct dwarf2_cu *cu;
4613 unsigned int offset;
4614 gdb_byte *info_ptr, *beg_of_comp_unit;
4615 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4616 struct attribute *attr;
4617 int read_cu = 0;
4618
4619 gdb_assert (! per_cu->debug_types_section);
4620
4621 /* Set local variables from the partial symbol table info. */
4622 offset = per_cu->offset;
4623
4624 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4625 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4626 beg_of_comp_unit = info_ptr;
4627
4628 if (per_cu->cu == NULL)
4629 {
4630 cu = xmalloc (sizeof (*cu));
4631 init_one_comp_unit (cu, per_cu);
4632
4633 read_cu = 1;
4634
4635 /* If an error occurs while loading, release our storage. */
4636 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4637
4638 /* Read in the comp_unit header. */
4639 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4640
4641 /* Skip dummy compilation units. */
4642 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4643 + dwarf2_per_objfile->info.size)
4644 || peek_abbrev_code (abfd, info_ptr) == 0)
4645 {
4646 do_cleanups (free_cu_cleanup);
4647 return;
4648 }
4649
4650 /* Complete the cu_header. */
4651 cu->header.offset = offset;
4652 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4653
4654 /* Read the abbrevs for this compilation unit. */
4655 dwarf2_read_abbrevs (cu);
4656 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4657
4658 /* Link this CU into read_in_chain. */
4659 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4660 dwarf2_per_objfile->read_in_chain = per_cu;
4661 }
4662 else
4663 {
4664 cu = per_cu->cu;
4665 info_ptr += cu->header.first_die_offset;
4666 }
4667
4668 cu->dies = read_comp_unit (info_ptr, cu);
4669
4670 /* We try not to read any attributes in this function, because not
4671 all CUs needed for references have been loaded yet, and symbol
4672 table processing isn't initialized. But we have to set the CU language,
4673 or we won't be able to build types correctly. */
4674 prepare_one_comp_unit (cu, cu->dies);
4675
4676 /* Similarly, if we do not read the producer, we can not apply
4677 producer-specific interpretation. */
4678 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4679 if (attr)
4680 cu->producer = DW_STRING (attr);
4681
4682 if (read_cu)
4683 {
4684 do_cleanups (free_abbrevs_cleanup);
4685
4686 /* We've successfully allocated this compilation unit. Let our
4687 caller clean it up when finished with it. */
4688 discard_cleanups (free_cu_cleanup);
4689 }
4690 }
4691
4692 /* Add a DIE to the delayed physname list. */
4693
4694 static void
4695 add_to_method_list (struct type *type, int fnfield_index, int index,
4696 const char *name, struct die_info *die,
4697 struct dwarf2_cu *cu)
4698 {
4699 struct delayed_method_info mi;
4700 mi.type = type;
4701 mi.fnfield_index = fnfield_index;
4702 mi.index = index;
4703 mi.name = name;
4704 mi.die = die;
4705 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4706 }
4707
4708 /* A cleanup for freeing the delayed method list. */
4709
4710 static void
4711 free_delayed_list (void *ptr)
4712 {
4713 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4714 if (cu->method_list != NULL)
4715 {
4716 VEC_free (delayed_method_info, cu->method_list);
4717 cu->method_list = NULL;
4718 }
4719 }
4720
4721 /* Compute the physnames of any methods on the CU's method list.
4722
4723 The computation of method physnames is delayed in order to avoid the
4724 (bad) condition that one of the method's formal parameters is of an as yet
4725 incomplete type. */
4726
4727 static void
4728 compute_delayed_physnames (struct dwarf2_cu *cu)
4729 {
4730 int i;
4731 struct delayed_method_info *mi;
4732 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4733 {
4734 const char *physname;
4735 struct fn_fieldlist *fn_flp
4736 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4737 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4738 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4739 }
4740 }
4741
4742 /* Generate full symbol information for PER_CU, whose DIEs have
4743 already been loaded into memory. */
4744
4745 static void
4746 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4747 {
4748 struct dwarf2_cu *cu = per_cu->cu;
4749 struct objfile *objfile = per_cu->objfile;
4750 CORE_ADDR lowpc, highpc;
4751 struct symtab *symtab;
4752 struct cleanup *back_to, *delayed_list_cleanup;
4753 CORE_ADDR baseaddr;
4754
4755 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4756
4757 buildsym_init ();
4758 back_to = make_cleanup (really_free_pendings, NULL);
4759 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4760
4761 cu->list_in_scope = &file_symbols;
4762
4763 /* Do line number decoding in read_file_scope () */
4764 process_die (cu->dies, cu);
4765
4766 /* Now that we have processed all the DIEs in the CU, all the types
4767 should be complete, and it should now be safe to compute all of the
4768 physnames. */
4769 compute_delayed_physnames (cu);
4770 do_cleanups (delayed_list_cleanup);
4771
4772 /* Some compilers don't define a DW_AT_high_pc attribute for the
4773 compilation unit. If the DW_AT_high_pc is missing, synthesize
4774 it, by scanning the DIE's below the compilation unit. */
4775 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4776
4777 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4778
4779 if (symtab != NULL)
4780 {
4781 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4782
4783 /* Set symtab language to language from DW_AT_language. If the
4784 compilation is from a C file generated by language preprocessors, do
4785 not set the language if it was already deduced by start_subfile. */
4786 if (!(cu->language == language_c && symtab->language != language_c))
4787 symtab->language = cu->language;
4788
4789 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4790 produce DW_AT_location with location lists but it can be possibly
4791 invalid without -fvar-tracking.
4792
4793 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4794 needed, it would be wrong due to missing DW_AT_producer there.
4795
4796 Still one can confuse GDB by using non-standard GCC compilation
4797 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4798 */
4799 if (cu->has_loclist && gcc_4_minor >= 0)
4800 symtab->locations_valid = 1;
4801
4802 if (gcc_4_minor >= 5)
4803 symtab->epilogue_unwind_valid = 1;
4804
4805 symtab->call_site_htab = cu->call_site_htab;
4806 }
4807
4808 if (dwarf2_per_objfile->using_index)
4809 per_cu->v.quick->symtab = symtab;
4810 else
4811 {
4812 struct partial_symtab *pst = per_cu->v.psymtab;
4813 pst->symtab = symtab;
4814 pst->readin = 1;
4815 }
4816
4817 do_cleanups (back_to);
4818 }
4819
4820 /* Process a die and its children. */
4821
4822 static void
4823 process_die (struct die_info *die, struct dwarf2_cu *cu)
4824 {
4825 switch (die->tag)
4826 {
4827 case DW_TAG_padding:
4828 break;
4829 case DW_TAG_compile_unit:
4830 read_file_scope (die, cu);
4831 break;
4832 case DW_TAG_type_unit:
4833 read_type_unit_scope (die, cu);
4834 break;
4835 case DW_TAG_subprogram:
4836 case DW_TAG_inlined_subroutine:
4837 read_func_scope (die, cu);
4838 break;
4839 case DW_TAG_lexical_block:
4840 case DW_TAG_try_block:
4841 case DW_TAG_catch_block:
4842 read_lexical_block_scope (die, cu);
4843 break;
4844 case DW_TAG_GNU_call_site:
4845 read_call_site_scope (die, cu);
4846 break;
4847 case DW_TAG_class_type:
4848 case DW_TAG_interface_type:
4849 case DW_TAG_structure_type:
4850 case DW_TAG_union_type:
4851 process_structure_scope (die, cu);
4852 break;
4853 case DW_TAG_enumeration_type:
4854 process_enumeration_scope (die, cu);
4855 break;
4856
4857 /* These dies have a type, but processing them does not create
4858 a symbol or recurse to process the children. Therefore we can
4859 read them on-demand through read_type_die. */
4860 case DW_TAG_subroutine_type:
4861 case DW_TAG_set_type:
4862 case DW_TAG_array_type:
4863 case DW_TAG_pointer_type:
4864 case DW_TAG_ptr_to_member_type:
4865 case DW_TAG_reference_type:
4866 case DW_TAG_string_type:
4867 break;
4868
4869 case DW_TAG_base_type:
4870 case DW_TAG_subrange_type:
4871 case DW_TAG_typedef:
4872 /* Add a typedef symbol for the type definition, if it has a
4873 DW_AT_name. */
4874 new_symbol (die, read_type_die (die, cu), cu);
4875 break;
4876 case DW_TAG_common_block:
4877 read_common_block (die, cu);
4878 break;
4879 case DW_TAG_common_inclusion:
4880 break;
4881 case DW_TAG_namespace:
4882 processing_has_namespace_info = 1;
4883 read_namespace (die, cu);
4884 break;
4885 case DW_TAG_module:
4886 processing_has_namespace_info = 1;
4887 read_module (die, cu);
4888 break;
4889 case DW_TAG_imported_declaration:
4890 case DW_TAG_imported_module:
4891 processing_has_namespace_info = 1;
4892 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4893 || cu->language != language_fortran))
4894 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4895 dwarf_tag_name (die->tag));
4896 read_import_statement (die, cu);
4897 break;
4898 default:
4899 new_symbol (die, NULL, cu);
4900 break;
4901 }
4902 }
4903
4904 /* A helper function for dwarf2_compute_name which determines whether DIE
4905 needs to have the name of the scope prepended to the name listed in the
4906 die. */
4907
4908 static int
4909 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4910 {
4911 struct attribute *attr;
4912
4913 switch (die->tag)
4914 {
4915 case DW_TAG_namespace:
4916 case DW_TAG_typedef:
4917 case DW_TAG_class_type:
4918 case DW_TAG_interface_type:
4919 case DW_TAG_structure_type:
4920 case DW_TAG_union_type:
4921 case DW_TAG_enumeration_type:
4922 case DW_TAG_enumerator:
4923 case DW_TAG_subprogram:
4924 case DW_TAG_member:
4925 return 1;
4926
4927 case DW_TAG_variable:
4928 case DW_TAG_constant:
4929 /* We only need to prefix "globally" visible variables. These include
4930 any variable marked with DW_AT_external or any variable that
4931 lives in a namespace. [Variables in anonymous namespaces
4932 require prefixing, but they are not DW_AT_external.] */
4933
4934 if (dwarf2_attr (die, DW_AT_specification, cu))
4935 {
4936 struct dwarf2_cu *spec_cu = cu;
4937
4938 return die_needs_namespace (die_specification (die, &spec_cu),
4939 spec_cu);
4940 }
4941
4942 attr = dwarf2_attr (die, DW_AT_external, cu);
4943 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4944 && die->parent->tag != DW_TAG_module)
4945 return 0;
4946 /* A variable in a lexical block of some kind does not need a
4947 namespace, even though in C++ such variables may be external
4948 and have a mangled name. */
4949 if (die->parent->tag == DW_TAG_lexical_block
4950 || die->parent->tag == DW_TAG_try_block
4951 || die->parent->tag == DW_TAG_catch_block
4952 || die->parent->tag == DW_TAG_subprogram)
4953 return 0;
4954 return 1;
4955
4956 default:
4957 return 0;
4958 }
4959 }
4960
4961 /* Retrieve the last character from a mem_file. */
4962
4963 static void
4964 do_ui_file_peek_last (void *object, const char *buffer, long length)
4965 {
4966 char *last_char_p = (char *) object;
4967
4968 if (length > 0)
4969 *last_char_p = buffer[length - 1];
4970 }
4971
4972 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4973 compute the physname for the object, which include a method's
4974 formal parameters (C++/Java) and return type (Java).
4975
4976 For Ada, return the DIE's linkage name rather than the fully qualified
4977 name. PHYSNAME is ignored..
4978
4979 The result is allocated on the objfile_obstack and canonicalized. */
4980
4981 static const char *
4982 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4983 int physname)
4984 {
4985 struct objfile *objfile = cu->objfile;
4986
4987 if (name == NULL)
4988 name = dwarf2_name (die, cu);
4989
4990 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4991 compute it by typename_concat inside GDB. */
4992 if (cu->language == language_ada
4993 || (cu->language == language_fortran && physname))
4994 {
4995 /* For Ada unit, we prefer the linkage name over the name, as
4996 the former contains the exported name, which the user expects
4997 to be able to reference. Ideally, we want the user to be able
4998 to reference this entity using either natural or linkage name,
4999 but we haven't started looking at this enhancement yet. */
5000 struct attribute *attr;
5001
5002 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5003 if (attr == NULL)
5004 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5005 if (attr && DW_STRING (attr))
5006 return DW_STRING (attr);
5007 }
5008
5009 /* These are the only languages we know how to qualify names in. */
5010 if (name != NULL
5011 && (cu->language == language_cplus || cu->language == language_java
5012 || cu->language == language_fortran))
5013 {
5014 if (die_needs_namespace (die, cu))
5015 {
5016 long length;
5017 char *prefix;
5018 struct ui_file *buf;
5019
5020 prefix = determine_prefix (die, cu);
5021 buf = mem_fileopen ();
5022 if (*prefix != '\0')
5023 {
5024 char *prefixed_name = typename_concat (NULL, prefix, name,
5025 physname, cu);
5026
5027 fputs_unfiltered (prefixed_name, buf);
5028 xfree (prefixed_name);
5029 }
5030 else
5031 fputs_unfiltered (name, buf);
5032
5033 /* Template parameters may be specified in the DIE's DW_AT_name, or
5034 as children with DW_TAG_template_type_param or
5035 DW_TAG_value_type_param. If the latter, add them to the name
5036 here. If the name already has template parameters, then
5037 skip this step; some versions of GCC emit both, and
5038 it is more efficient to use the pre-computed name.
5039
5040 Something to keep in mind about this process: it is very
5041 unlikely, or in some cases downright impossible, to produce
5042 something that will match the mangled name of a function.
5043 If the definition of the function has the same debug info,
5044 we should be able to match up with it anyway. But fallbacks
5045 using the minimal symbol, for instance to find a method
5046 implemented in a stripped copy of libstdc++, will not work.
5047 If we do not have debug info for the definition, we will have to
5048 match them up some other way.
5049
5050 When we do name matching there is a related problem with function
5051 templates; two instantiated function templates are allowed to
5052 differ only by their return types, which we do not add here. */
5053
5054 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5055 {
5056 struct attribute *attr;
5057 struct die_info *child;
5058 int first = 1;
5059
5060 die->building_fullname = 1;
5061
5062 for (child = die->child; child != NULL; child = child->sibling)
5063 {
5064 struct type *type;
5065 long value;
5066 gdb_byte *bytes;
5067 struct dwarf2_locexpr_baton *baton;
5068 struct value *v;
5069
5070 if (child->tag != DW_TAG_template_type_param
5071 && child->tag != DW_TAG_template_value_param)
5072 continue;
5073
5074 if (first)
5075 {
5076 fputs_unfiltered ("<", buf);
5077 first = 0;
5078 }
5079 else
5080 fputs_unfiltered (", ", buf);
5081
5082 attr = dwarf2_attr (child, DW_AT_type, cu);
5083 if (attr == NULL)
5084 {
5085 complaint (&symfile_complaints,
5086 _("template parameter missing DW_AT_type"));
5087 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5088 continue;
5089 }
5090 type = die_type (child, cu);
5091
5092 if (child->tag == DW_TAG_template_type_param)
5093 {
5094 c_print_type (type, "", buf, -1, 0);
5095 continue;
5096 }
5097
5098 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5099 if (attr == NULL)
5100 {
5101 complaint (&symfile_complaints,
5102 _("template parameter missing "
5103 "DW_AT_const_value"));
5104 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5105 continue;
5106 }
5107
5108 dwarf2_const_value_attr (attr, type, name,
5109 &cu->comp_unit_obstack, cu,
5110 &value, &bytes, &baton);
5111
5112 if (TYPE_NOSIGN (type))
5113 /* GDB prints characters as NUMBER 'CHAR'. If that's
5114 changed, this can use value_print instead. */
5115 c_printchar (value, type, buf);
5116 else
5117 {
5118 struct value_print_options opts;
5119
5120 if (baton != NULL)
5121 v = dwarf2_evaluate_loc_desc (type, NULL,
5122 baton->data,
5123 baton->size,
5124 baton->per_cu);
5125 else if (bytes != NULL)
5126 {
5127 v = allocate_value (type);
5128 memcpy (value_contents_writeable (v), bytes,
5129 TYPE_LENGTH (type));
5130 }
5131 else
5132 v = value_from_longest (type, value);
5133
5134 /* Specify decimal so that we do not depend on
5135 the radix. */
5136 get_formatted_print_options (&opts, 'd');
5137 opts.raw = 1;
5138 value_print (v, buf, &opts);
5139 release_value (v);
5140 value_free (v);
5141 }
5142 }
5143
5144 die->building_fullname = 0;
5145
5146 if (!first)
5147 {
5148 /* Close the argument list, with a space if necessary
5149 (nested templates). */
5150 char last_char = '\0';
5151 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5152 if (last_char == '>')
5153 fputs_unfiltered (" >", buf);
5154 else
5155 fputs_unfiltered (">", buf);
5156 }
5157 }
5158
5159 /* For Java and C++ methods, append formal parameter type
5160 information, if PHYSNAME. */
5161
5162 if (physname && die->tag == DW_TAG_subprogram
5163 && (cu->language == language_cplus
5164 || cu->language == language_java))
5165 {
5166 struct type *type = read_type_die (die, cu);
5167
5168 c_type_print_args (type, buf, 1, cu->language);
5169
5170 if (cu->language == language_java)
5171 {
5172 /* For java, we must append the return type to method
5173 names. */
5174 if (die->tag == DW_TAG_subprogram)
5175 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5176 0, 0);
5177 }
5178 else if (cu->language == language_cplus)
5179 {
5180 /* Assume that an artificial first parameter is
5181 "this", but do not crash if it is not. RealView
5182 marks unnamed (and thus unused) parameters as
5183 artificial; there is no way to differentiate
5184 the two cases. */
5185 if (TYPE_NFIELDS (type) > 0
5186 && TYPE_FIELD_ARTIFICIAL (type, 0)
5187 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5188 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5189 0))))
5190 fputs_unfiltered (" const", buf);
5191 }
5192 }
5193
5194 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5195 &length);
5196 ui_file_delete (buf);
5197
5198 if (cu->language == language_cplus)
5199 {
5200 char *cname
5201 = dwarf2_canonicalize_name (name, cu,
5202 &objfile->objfile_obstack);
5203
5204 if (cname != NULL)
5205 name = cname;
5206 }
5207 }
5208 }
5209
5210 return name;
5211 }
5212
5213 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5214 If scope qualifiers are appropriate they will be added. The result
5215 will be allocated on the objfile_obstack, or NULL if the DIE does
5216 not have a name. NAME may either be from a previous call to
5217 dwarf2_name or NULL.
5218
5219 The output string will be canonicalized (if C++/Java). */
5220
5221 static const char *
5222 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5223 {
5224 return dwarf2_compute_name (name, die, cu, 0);
5225 }
5226
5227 /* Construct a physname for the given DIE in CU. NAME may either be
5228 from a previous call to dwarf2_name or NULL. The result will be
5229 allocated on the objfile_objstack or NULL if the DIE does not have a
5230 name.
5231
5232 The output string will be canonicalized (if C++/Java). */
5233
5234 static const char *
5235 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5236 {
5237 struct objfile *objfile = cu->objfile;
5238 struct attribute *attr;
5239 const char *retval, *mangled = NULL, *canon = NULL;
5240 struct cleanup *back_to;
5241 int need_copy = 1;
5242
5243 /* In this case dwarf2_compute_name is just a shortcut not building anything
5244 on its own. */
5245 if (!die_needs_namespace (die, cu))
5246 return dwarf2_compute_name (name, die, cu, 1);
5247
5248 back_to = make_cleanup (null_cleanup, NULL);
5249
5250 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5251 if (!attr)
5252 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5253
5254 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5255 has computed. */
5256 if (attr && DW_STRING (attr))
5257 {
5258 char *demangled;
5259
5260 mangled = DW_STRING (attr);
5261
5262 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5263 type. It is easier for GDB users to search for such functions as
5264 `name(params)' than `long name(params)'. In such case the minimal
5265 symbol names do not match the full symbol names but for template
5266 functions there is never a need to look up their definition from their
5267 declaration so the only disadvantage remains the minimal symbol
5268 variant `long name(params)' does not have the proper inferior type.
5269 */
5270
5271 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5272 | (cu->language == language_java
5273 ? DMGL_JAVA | DMGL_RET_POSTFIX
5274 : DMGL_RET_DROP)));
5275 if (demangled)
5276 {
5277 make_cleanup (xfree, demangled);
5278 canon = demangled;
5279 }
5280 else
5281 {
5282 canon = mangled;
5283 need_copy = 0;
5284 }
5285 }
5286
5287 if (canon == NULL || check_physname)
5288 {
5289 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5290
5291 if (canon != NULL && strcmp (physname, canon) != 0)
5292 {
5293 /* It may not mean a bug in GDB. The compiler could also
5294 compute DW_AT_linkage_name incorrectly. But in such case
5295 GDB would need to be bug-to-bug compatible. */
5296
5297 complaint (&symfile_complaints,
5298 _("Computed physname <%s> does not match demangled <%s> "
5299 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5300 physname, canon, mangled, die->offset, objfile->name);
5301
5302 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5303 is available here - over computed PHYSNAME. It is safer
5304 against both buggy GDB and buggy compilers. */
5305
5306 retval = canon;
5307 }
5308 else
5309 {
5310 retval = physname;
5311 need_copy = 0;
5312 }
5313 }
5314 else
5315 retval = canon;
5316
5317 if (need_copy)
5318 retval = obsavestring (retval, strlen (retval),
5319 &objfile->objfile_obstack);
5320
5321 do_cleanups (back_to);
5322 return retval;
5323 }
5324
5325 /* Read the import statement specified by the given die and record it. */
5326
5327 static void
5328 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5329 {
5330 struct objfile *objfile = cu->objfile;
5331 struct attribute *import_attr;
5332 struct die_info *imported_die, *child_die;
5333 struct dwarf2_cu *imported_cu;
5334 const char *imported_name;
5335 const char *imported_name_prefix;
5336 const char *canonical_name;
5337 const char *import_alias;
5338 const char *imported_declaration = NULL;
5339 const char *import_prefix;
5340 VEC (const_char_ptr) *excludes = NULL;
5341 struct cleanup *cleanups;
5342
5343 char *temp;
5344
5345 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5346 if (import_attr == NULL)
5347 {
5348 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5349 dwarf_tag_name (die->tag));
5350 return;
5351 }
5352
5353 imported_cu = cu;
5354 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5355 imported_name = dwarf2_name (imported_die, imported_cu);
5356 if (imported_name == NULL)
5357 {
5358 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5359
5360 The import in the following code:
5361 namespace A
5362 {
5363 typedef int B;
5364 }
5365
5366 int main ()
5367 {
5368 using A::B;
5369 B b;
5370 return b;
5371 }
5372
5373 ...
5374 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5375 <52> DW_AT_decl_file : 1
5376 <53> DW_AT_decl_line : 6
5377 <54> DW_AT_import : <0x75>
5378 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5379 <59> DW_AT_name : B
5380 <5b> DW_AT_decl_file : 1
5381 <5c> DW_AT_decl_line : 2
5382 <5d> DW_AT_type : <0x6e>
5383 ...
5384 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5385 <76> DW_AT_byte_size : 4
5386 <77> DW_AT_encoding : 5 (signed)
5387
5388 imports the wrong die ( 0x75 instead of 0x58 ).
5389 This case will be ignored until the gcc bug is fixed. */
5390 return;
5391 }
5392
5393 /* Figure out the local name after import. */
5394 import_alias = dwarf2_name (die, cu);
5395
5396 /* Figure out where the statement is being imported to. */
5397 import_prefix = determine_prefix (die, cu);
5398
5399 /* Figure out what the scope of the imported die is and prepend it
5400 to the name of the imported die. */
5401 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5402
5403 if (imported_die->tag != DW_TAG_namespace
5404 && imported_die->tag != DW_TAG_module)
5405 {
5406 imported_declaration = imported_name;
5407 canonical_name = imported_name_prefix;
5408 }
5409 else if (strlen (imported_name_prefix) > 0)
5410 {
5411 temp = alloca (strlen (imported_name_prefix)
5412 + 2 + strlen (imported_name) + 1);
5413 strcpy (temp, imported_name_prefix);
5414 strcat (temp, "::");
5415 strcat (temp, imported_name);
5416 canonical_name = temp;
5417 }
5418 else
5419 canonical_name = imported_name;
5420
5421 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5422
5423 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5424 for (child_die = die->child; child_die && child_die->tag;
5425 child_die = sibling_die (child_die))
5426 {
5427 /* DWARF-4: A Fortran use statement with a “rename list” may be
5428 represented by an imported module entry with an import attribute
5429 referring to the module and owned entries corresponding to those
5430 entities that are renamed as part of being imported. */
5431
5432 if (child_die->tag != DW_TAG_imported_declaration)
5433 {
5434 complaint (&symfile_complaints,
5435 _("child DW_TAG_imported_declaration expected "
5436 "- DIE at 0x%x [in module %s]"),
5437 child_die->offset, objfile->name);
5438 continue;
5439 }
5440
5441 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5442 if (import_attr == NULL)
5443 {
5444 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5445 dwarf_tag_name (child_die->tag));
5446 continue;
5447 }
5448
5449 imported_cu = cu;
5450 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5451 &imported_cu);
5452 imported_name = dwarf2_name (imported_die, imported_cu);
5453 if (imported_name == NULL)
5454 {
5455 complaint (&symfile_complaints,
5456 _("child DW_TAG_imported_declaration has unknown "
5457 "imported name - DIE at 0x%x [in module %s]"),
5458 child_die->offset, objfile->name);
5459 continue;
5460 }
5461
5462 VEC_safe_push (const_char_ptr, excludes, imported_name);
5463
5464 process_die (child_die, cu);
5465 }
5466
5467 cp_add_using_directive (import_prefix,
5468 canonical_name,
5469 import_alias,
5470 imported_declaration,
5471 excludes,
5472 &objfile->objfile_obstack);
5473
5474 do_cleanups (cleanups);
5475 }
5476
5477 /* Cleanup function for read_file_scope. */
5478
5479 static void
5480 free_cu_line_header (void *arg)
5481 {
5482 struct dwarf2_cu *cu = arg;
5483
5484 free_line_header (cu->line_header);
5485 cu->line_header = NULL;
5486 }
5487
5488 static void
5489 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5490 char **name, char **comp_dir)
5491 {
5492 struct attribute *attr;
5493
5494 *name = NULL;
5495 *comp_dir = NULL;
5496
5497 /* Find the filename. Do not use dwarf2_name here, since the filename
5498 is not a source language identifier. */
5499 attr = dwarf2_attr (die, DW_AT_name, cu);
5500 if (attr)
5501 {
5502 *name = DW_STRING (attr);
5503 }
5504
5505 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5506 if (attr)
5507 *comp_dir = DW_STRING (attr);
5508 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5509 {
5510 *comp_dir = ldirname (*name);
5511 if (*comp_dir != NULL)
5512 make_cleanup (xfree, *comp_dir);
5513 }
5514 if (*comp_dir != NULL)
5515 {
5516 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5517 directory, get rid of it. */
5518 char *cp = strchr (*comp_dir, ':');
5519
5520 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5521 *comp_dir = cp + 1;
5522 }
5523
5524 if (*name == NULL)
5525 *name = "<unknown>";
5526 }
5527
5528 /* Handle DW_AT_stmt_list for a compilation unit. */
5529
5530 static void
5531 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5532 const char *comp_dir)
5533 {
5534 struct attribute *attr;
5535 struct objfile *objfile = cu->objfile;
5536 bfd *abfd = objfile->obfd;
5537
5538 /* Decode line number information if present. We do this before
5539 processing child DIEs, so that the line header table is available
5540 for DW_AT_decl_file. */
5541 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5542 if (attr)
5543 {
5544 unsigned int line_offset = DW_UNSND (attr);
5545 struct line_header *line_header
5546 = dwarf_decode_line_header (line_offset, abfd, cu);
5547
5548 if (line_header)
5549 {
5550 cu->line_header = line_header;
5551 make_cleanup (free_cu_line_header, cu);
5552 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5553 }
5554 }
5555 }
5556
5557 /* Process DW_TAG_compile_unit. */
5558
5559 static void
5560 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5561 {
5562 struct objfile *objfile = cu->objfile;
5563 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5564 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5565 CORE_ADDR highpc = ((CORE_ADDR) 0);
5566 struct attribute *attr;
5567 char *name = NULL;
5568 char *comp_dir = NULL;
5569 struct die_info *child_die;
5570 bfd *abfd = objfile->obfd;
5571 CORE_ADDR baseaddr;
5572
5573 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5574
5575 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5576
5577 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5578 from finish_block. */
5579 if (lowpc == ((CORE_ADDR) -1))
5580 lowpc = highpc;
5581 lowpc += baseaddr;
5582 highpc += baseaddr;
5583
5584 find_file_and_directory (die, cu, &name, &comp_dir);
5585
5586 attr = dwarf2_attr (die, DW_AT_language, cu);
5587 if (attr)
5588 {
5589 set_cu_language (DW_UNSND (attr), cu);
5590 }
5591
5592 attr = dwarf2_attr (die, DW_AT_producer, cu);
5593 if (attr)
5594 cu->producer = DW_STRING (attr);
5595
5596 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5597 standardised yet. As a workaround for the language detection we fall
5598 back to the DW_AT_producer string. */
5599 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5600 cu->language = language_opencl;
5601
5602 /* We assume that we're processing GCC output. */
5603 processing_gcc_compilation = 2;
5604
5605 processing_has_namespace_info = 0;
5606
5607 start_symtab (name, comp_dir, lowpc);
5608 record_debugformat ("DWARF 2");
5609 record_producer (cu->producer);
5610
5611 handle_DW_AT_stmt_list (die, cu, comp_dir);
5612
5613 /* Process all dies in compilation unit. */
5614 if (die->child != NULL)
5615 {
5616 child_die = die->child;
5617 while (child_die && child_die->tag)
5618 {
5619 process_die (child_die, cu);
5620 child_die = sibling_die (child_die);
5621 }
5622 }
5623
5624 /* Decode macro information, if present. Dwarf 2 macro information
5625 refers to information in the line number info statement program
5626 header, so we can only read it if we've read the header
5627 successfully. */
5628 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5629 if (attr && cu->line_header)
5630 {
5631 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5632 complaint (&symfile_complaints,
5633 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5634
5635 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5636 comp_dir, abfd, cu,
5637 &dwarf2_per_objfile->macro, 1);
5638 }
5639 else
5640 {
5641 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5642 if (attr && cu->line_header)
5643 {
5644 unsigned int macro_offset = DW_UNSND (attr);
5645
5646 dwarf_decode_macros (cu->line_header, macro_offset,
5647 comp_dir, abfd, cu,
5648 &dwarf2_per_objfile->macinfo, 0);
5649 }
5650 }
5651
5652 do_cleanups (back_to);
5653 }
5654
5655 /* Process DW_TAG_type_unit.
5656 For TUs we want to skip the first top level sibling if it's not the
5657 actual type being defined by this TU. In this case the first top
5658 level sibling is there to provide context only. */
5659
5660 static void
5661 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5662 {
5663 struct objfile *objfile = cu->objfile;
5664 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5665 CORE_ADDR lowpc;
5666 struct attribute *attr;
5667 char *name = NULL;
5668 char *comp_dir = NULL;
5669 struct die_info *child_die;
5670 bfd *abfd = objfile->obfd;
5671
5672 /* start_symtab needs a low pc, but we don't really have one.
5673 Do what read_file_scope would do in the absence of such info. */
5674 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5675
5676 /* Find the filename. Do not use dwarf2_name here, since the filename
5677 is not a source language identifier. */
5678 attr = dwarf2_attr (die, DW_AT_name, cu);
5679 if (attr)
5680 name = DW_STRING (attr);
5681
5682 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5683 if (attr)
5684 comp_dir = DW_STRING (attr);
5685 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5686 {
5687 comp_dir = ldirname (name);
5688 if (comp_dir != NULL)
5689 make_cleanup (xfree, comp_dir);
5690 }
5691
5692 if (name == NULL)
5693 name = "<unknown>";
5694
5695 attr = dwarf2_attr (die, DW_AT_language, cu);
5696 if (attr)
5697 set_cu_language (DW_UNSND (attr), cu);
5698
5699 /* This isn't technically needed today. It is done for symmetry
5700 with read_file_scope. */
5701 attr = dwarf2_attr (die, DW_AT_producer, cu);
5702 if (attr)
5703 cu->producer = DW_STRING (attr);
5704
5705 /* We assume that we're processing GCC output. */
5706 processing_gcc_compilation = 2;
5707
5708 processing_has_namespace_info = 0;
5709
5710 start_symtab (name, comp_dir, lowpc);
5711 record_debugformat ("DWARF 2");
5712 record_producer (cu->producer);
5713
5714 handle_DW_AT_stmt_list (die, cu, comp_dir);
5715
5716 /* Process the dies in the type unit. */
5717 if (die->child == NULL)
5718 {
5719 dump_die_for_error (die);
5720 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5721 bfd_get_filename (abfd));
5722 }
5723
5724 child_die = die->child;
5725
5726 while (child_die && child_die->tag)
5727 {
5728 process_die (child_die, cu);
5729
5730 child_die = sibling_die (child_die);
5731 }
5732
5733 do_cleanups (back_to);
5734 }
5735
5736 /* qsort helper for inherit_abstract_dies. */
5737
5738 static int
5739 unsigned_int_compar (const void *ap, const void *bp)
5740 {
5741 unsigned int a = *(unsigned int *) ap;
5742 unsigned int b = *(unsigned int *) bp;
5743
5744 return (a > b) - (b > a);
5745 }
5746
5747 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5748 Inherit only the children of the DW_AT_abstract_origin DIE not being
5749 already referenced by DW_AT_abstract_origin from the children of the
5750 current DIE. */
5751
5752 static void
5753 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5754 {
5755 struct die_info *child_die;
5756 unsigned die_children_count;
5757 /* CU offsets which were referenced by children of the current DIE. */
5758 unsigned *offsets;
5759 unsigned *offsets_end, *offsetp;
5760 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5761 struct die_info *origin_die;
5762 /* Iterator of the ORIGIN_DIE children. */
5763 struct die_info *origin_child_die;
5764 struct cleanup *cleanups;
5765 struct attribute *attr;
5766 struct dwarf2_cu *origin_cu;
5767 struct pending **origin_previous_list_in_scope;
5768
5769 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5770 if (!attr)
5771 return;
5772
5773 /* Note that following die references may follow to a die in a
5774 different cu. */
5775
5776 origin_cu = cu;
5777 origin_die = follow_die_ref (die, attr, &origin_cu);
5778
5779 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5780 symbols in. */
5781 origin_previous_list_in_scope = origin_cu->list_in_scope;
5782 origin_cu->list_in_scope = cu->list_in_scope;
5783
5784 if (die->tag != origin_die->tag
5785 && !(die->tag == DW_TAG_inlined_subroutine
5786 && origin_die->tag == DW_TAG_subprogram))
5787 complaint (&symfile_complaints,
5788 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5789 die->offset, origin_die->offset);
5790
5791 child_die = die->child;
5792 die_children_count = 0;
5793 while (child_die && child_die->tag)
5794 {
5795 child_die = sibling_die (child_die);
5796 die_children_count++;
5797 }
5798 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5799 cleanups = make_cleanup (xfree, offsets);
5800
5801 offsets_end = offsets;
5802 child_die = die->child;
5803 while (child_die && child_die->tag)
5804 {
5805 /* For each CHILD_DIE, find the corresponding child of
5806 ORIGIN_DIE. If there is more than one layer of
5807 DW_AT_abstract_origin, follow them all; there shouldn't be,
5808 but GCC versions at least through 4.4 generate this (GCC PR
5809 40573). */
5810 struct die_info *child_origin_die = child_die;
5811 struct dwarf2_cu *child_origin_cu = cu;
5812
5813 while (1)
5814 {
5815 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5816 child_origin_cu);
5817 if (attr == NULL)
5818 break;
5819 child_origin_die = follow_die_ref (child_origin_die, attr,
5820 &child_origin_cu);
5821 }
5822
5823 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5824 counterpart may exist. */
5825 if (child_origin_die != child_die)
5826 {
5827 if (child_die->tag != child_origin_die->tag
5828 && !(child_die->tag == DW_TAG_inlined_subroutine
5829 && child_origin_die->tag == DW_TAG_subprogram))
5830 complaint (&symfile_complaints,
5831 _("Child DIE 0x%x and its abstract origin 0x%x have "
5832 "different tags"), child_die->offset,
5833 child_origin_die->offset);
5834 if (child_origin_die->parent != origin_die)
5835 complaint (&symfile_complaints,
5836 _("Child DIE 0x%x and its abstract origin 0x%x have "
5837 "different parents"), child_die->offset,
5838 child_origin_die->offset);
5839 else
5840 *offsets_end++ = child_origin_die->offset;
5841 }
5842 child_die = sibling_die (child_die);
5843 }
5844 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5845 unsigned_int_compar);
5846 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5847 if (offsetp[-1] == *offsetp)
5848 complaint (&symfile_complaints,
5849 _("Multiple children of DIE 0x%x refer "
5850 "to DIE 0x%x as their abstract origin"),
5851 die->offset, *offsetp);
5852
5853 offsetp = offsets;
5854 origin_child_die = origin_die->child;
5855 while (origin_child_die && origin_child_die->tag)
5856 {
5857 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5858 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5859 offsetp++;
5860 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5861 {
5862 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5863 process_die (origin_child_die, origin_cu);
5864 }
5865 origin_child_die = sibling_die (origin_child_die);
5866 }
5867 origin_cu->list_in_scope = origin_previous_list_in_scope;
5868
5869 do_cleanups (cleanups);
5870 }
5871
5872 static void
5873 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5874 {
5875 struct objfile *objfile = cu->objfile;
5876 struct context_stack *new;
5877 CORE_ADDR lowpc;
5878 CORE_ADDR highpc;
5879 struct die_info *child_die;
5880 struct attribute *attr, *call_line, *call_file;
5881 char *name;
5882 CORE_ADDR baseaddr;
5883 struct block *block;
5884 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5885 VEC (symbolp) *template_args = NULL;
5886 struct template_symbol *templ_func = NULL;
5887
5888 if (inlined_func)
5889 {
5890 /* If we do not have call site information, we can't show the
5891 caller of this inlined function. That's too confusing, so
5892 only use the scope for local variables. */
5893 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5894 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5895 if (call_line == NULL || call_file == NULL)
5896 {
5897 read_lexical_block_scope (die, cu);
5898 return;
5899 }
5900 }
5901
5902 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5903
5904 name = dwarf2_name (die, cu);
5905
5906 /* Ignore functions with missing or empty names. These are actually
5907 illegal according to the DWARF standard. */
5908 if (name == NULL)
5909 {
5910 complaint (&symfile_complaints,
5911 _("missing name for subprogram DIE at %d"), die->offset);
5912 return;
5913 }
5914
5915 /* Ignore functions with missing or invalid low and high pc attributes. */
5916 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5917 {
5918 attr = dwarf2_attr (die, DW_AT_external, cu);
5919 if (!attr || !DW_UNSND (attr))
5920 complaint (&symfile_complaints,
5921 _("cannot get low and high bounds "
5922 "for subprogram DIE at %d"),
5923 die->offset);
5924 return;
5925 }
5926
5927 lowpc += baseaddr;
5928 highpc += baseaddr;
5929
5930 /* If we have any template arguments, then we must allocate a
5931 different sort of symbol. */
5932 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5933 {
5934 if (child_die->tag == DW_TAG_template_type_param
5935 || child_die->tag == DW_TAG_template_value_param)
5936 {
5937 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5938 struct template_symbol);
5939 templ_func->base.is_cplus_template_function = 1;
5940 break;
5941 }
5942 }
5943
5944 new = push_context (0, lowpc);
5945 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5946 (struct symbol *) templ_func);
5947
5948 /* If there is a location expression for DW_AT_frame_base, record
5949 it. */
5950 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5951 if (attr)
5952 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5953 expression is being recorded directly in the function's symbol
5954 and not in a separate frame-base object. I guess this hack is
5955 to avoid adding some sort of frame-base adjunct/annex to the
5956 function's symbol :-(. The problem with doing this is that it
5957 results in a function symbol with a location expression that
5958 has nothing to do with the location of the function, ouch! The
5959 relationship should be: a function's symbol has-a frame base; a
5960 frame-base has-a location expression. */
5961 dwarf2_symbol_mark_computed (attr, new->name, cu);
5962
5963 cu->list_in_scope = &local_symbols;
5964
5965 if (die->child != NULL)
5966 {
5967 child_die = die->child;
5968 while (child_die && child_die->tag)
5969 {
5970 if (child_die->tag == DW_TAG_template_type_param
5971 || child_die->tag == DW_TAG_template_value_param)
5972 {
5973 struct symbol *arg = new_symbol (child_die, NULL, cu);
5974
5975 if (arg != NULL)
5976 VEC_safe_push (symbolp, template_args, arg);
5977 }
5978 else
5979 process_die (child_die, cu);
5980 child_die = sibling_die (child_die);
5981 }
5982 }
5983
5984 inherit_abstract_dies (die, cu);
5985
5986 /* If we have a DW_AT_specification, we might need to import using
5987 directives from the context of the specification DIE. See the
5988 comment in determine_prefix. */
5989 if (cu->language == language_cplus
5990 && dwarf2_attr (die, DW_AT_specification, cu))
5991 {
5992 struct dwarf2_cu *spec_cu = cu;
5993 struct die_info *spec_die = die_specification (die, &spec_cu);
5994
5995 while (spec_die)
5996 {
5997 child_die = spec_die->child;
5998 while (child_die && child_die->tag)
5999 {
6000 if (child_die->tag == DW_TAG_imported_module)
6001 process_die (child_die, spec_cu);
6002 child_die = sibling_die (child_die);
6003 }
6004
6005 /* In some cases, GCC generates specification DIEs that
6006 themselves contain DW_AT_specification attributes. */
6007 spec_die = die_specification (spec_die, &spec_cu);
6008 }
6009 }
6010
6011 new = pop_context ();
6012 /* Make a block for the local symbols within. */
6013 block = finish_block (new->name, &local_symbols, new->old_blocks,
6014 lowpc, highpc, objfile);
6015
6016 /* For C++, set the block's scope. */
6017 if (cu->language == language_cplus || cu->language == language_fortran)
6018 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6019 determine_prefix (die, cu),
6020 processing_has_namespace_info);
6021
6022 /* If we have address ranges, record them. */
6023 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6024
6025 /* Attach template arguments to function. */
6026 if (! VEC_empty (symbolp, template_args))
6027 {
6028 gdb_assert (templ_func != NULL);
6029
6030 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6031 templ_func->template_arguments
6032 = obstack_alloc (&objfile->objfile_obstack,
6033 (templ_func->n_template_arguments
6034 * sizeof (struct symbol *)));
6035 memcpy (templ_func->template_arguments,
6036 VEC_address (symbolp, template_args),
6037 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6038 VEC_free (symbolp, template_args);
6039 }
6040
6041 /* In C++, we can have functions nested inside functions (e.g., when
6042 a function declares a class that has methods). This means that
6043 when we finish processing a function scope, we may need to go
6044 back to building a containing block's symbol lists. */
6045 local_symbols = new->locals;
6046 param_symbols = new->params;
6047 using_directives = new->using_directives;
6048
6049 /* If we've finished processing a top-level function, subsequent
6050 symbols go in the file symbol list. */
6051 if (outermost_context_p ())
6052 cu->list_in_scope = &file_symbols;
6053 }
6054
6055 /* Process all the DIES contained within a lexical block scope. Start
6056 a new scope, process the dies, and then close the scope. */
6057
6058 static void
6059 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6060 {
6061 struct objfile *objfile = cu->objfile;
6062 struct context_stack *new;
6063 CORE_ADDR lowpc, highpc;
6064 struct die_info *child_die;
6065 CORE_ADDR baseaddr;
6066
6067 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6068
6069 /* Ignore blocks with missing or invalid low and high pc attributes. */
6070 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6071 as multiple lexical blocks? Handling children in a sane way would
6072 be nasty. Might be easier to properly extend generic blocks to
6073 describe ranges. */
6074 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6075 return;
6076 lowpc += baseaddr;
6077 highpc += baseaddr;
6078
6079 push_context (0, lowpc);
6080 if (die->child != NULL)
6081 {
6082 child_die = die->child;
6083 while (child_die && child_die->tag)
6084 {
6085 process_die (child_die, cu);
6086 child_die = sibling_die (child_die);
6087 }
6088 }
6089 new = pop_context ();
6090
6091 if (local_symbols != NULL || using_directives != NULL)
6092 {
6093 struct block *block
6094 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6095 highpc, objfile);
6096
6097 /* Note that recording ranges after traversing children, as we
6098 do here, means that recording a parent's ranges entails
6099 walking across all its children's ranges as they appear in
6100 the address map, which is quadratic behavior.
6101
6102 It would be nicer to record the parent's ranges before
6103 traversing its children, simply overriding whatever you find
6104 there. But since we don't even decide whether to create a
6105 block until after we've traversed its children, that's hard
6106 to do. */
6107 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6108 }
6109 local_symbols = new->locals;
6110 using_directives = new->using_directives;
6111 }
6112
6113 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6114
6115 static void
6116 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6117 {
6118 struct objfile *objfile = cu->objfile;
6119 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6120 CORE_ADDR pc, baseaddr;
6121 struct attribute *attr;
6122 struct call_site *call_site, call_site_local;
6123 void **slot;
6124 int nparams;
6125 struct die_info *child_die;
6126
6127 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6128
6129 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6130 if (!attr)
6131 {
6132 complaint (&symfile_complaints,
6133 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6134 "DIE 0x%x [in module %s]"),
6135 die->offset, objfile->name);
6136 return;
6137 }
6138 pc = DW_ADDR (attr) + baseaddr;
6139
6140 if (cu->call_site_htab == NULL)
6141 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6142 NULL, &objfile->objfile_obstack,
6143 hashtab_obstack_allocate, NULL);
6144 call_site_local.pc = pc;
6145 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6146 if (*slot != NULL)
6147 {
6148 complaint (&symfile_complaints,
6149 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6150 "DIE 0x%x [in module %s]"),
6151 paddress (gdbarch, pc), die->offset, objfile->name);
6152 return;
6153 }
6154
6155 /* Count parameters at the caller. */
6156
6157 nparams = 0;
6158 for (child_die = die->child; child_die && child_die->tag;
6159 child_die = sibling_die (child_die))
6160 {
6161 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6162 {
6163 complaint (&symfile_complaints,
6164 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6165 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6166 child_die->tag, child_die->offset, objfile->name);
6167 continue;
6168 }
6169
6170 nparams++;
6171 }
6172
6173 call_site = obstack_alloc (&objfile->objfile_obstack,
6174 (sizeof (*call_site)
6175 + (sizeof (*call_site->parameter)
6176 * (nparams - 1))));
6177 *slot = call_site;
6178 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6179 call_site->pc = pc;
6180
6181 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6182 {
6183 struct die_info *func_die;
6184
6185 /* Skip also over DW_TAG_inlined_subroutine. */
6186 for (func_die = die->parent;
6187 func_die && func_die->tag != DW_TAG_subprogram
6188 && func_die->tag != DW_TAG_subroutine_type;
6189 func_die = func_die->parent);
6190
6191 /* DW_AT_GNU_all_call_sites is a superset
6192 of DW_AT_GNU_all_tail_call_sites. */
6193 if (func_die
6194 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6195 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6196 {
6197 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6198 not complete. But keep CALL_SITE for look ups via call_site_htab,
6199 both the initial caller containing the real return address PC and
6200 the final callee containing the current PC of a chain of tail
6201 calls do not need to have the tail call list complete. But any
6202 function candidate for a virtual tail call frame searched via
6203 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6204 determined unambiguously. */
6205 }
6206 else
6207 {
6208 struct type *func_type = NULL;
6209
6210 if (func_die)
6211 func_type = get_die_type (func_die, cu);
6212 if (func_type != NULL)
6213 {
6214 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6215
6216 /* Enlist this call site to the function. */
6217 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6218 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6219 }
6220 else
6221 complaint (&symfile_complaints,
6222 _("Cannot find function owning DW_TAG_GNU_call_site "
6223 "DIE 0x%x [in module %s]"),
6224 die->offset, objfile->name);
6225 }
6226 }
6227
6228 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6229 if (attr == NULL)
6230 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6231 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6232 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6233 /* Keep NULL DWARF_BLOCK. */;
6234 else if (attr_form_is_block (attr))
6235 {
6236 struct dwarf2_locexpr_baton *dlbaton;
6237
6238 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6239 dlbaton->data = DW_BLOCK (attr)->data;
6240 dlbaton->size = DW_BLOCK (attr)->size;
6241 dlbaton->per_cu = cu->per_cu;
6242
6243 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6244 }
6245 else if (is_ref_attr (attr))
6246 {
6247 struct dwarf2_cu *target_cu = cu;
6248 struct die_info *target_die;
6249
6250 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6251 gdb_assert (target_cu->objfile == objfile);
6252 if (die_is_declaration (target_die, target_cu))
6253 {
6254 const char *target_physname;
6255
6256 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6257 if (target_physname == NULL)
6258 complaint (&symfile_complaints,
6259 _("DW_AT_GNU_call_site_target target DIE has invalid "
6260 "physname, for referencing DIE 0x%x [in module %s]"),
6261 die->offset, objfile->name);
6262 else
6263 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6264 }
6265 else
6266 {
6267 CORE_ADDR lowpc;
6268
6269 /* DW_AT_entry_pc should be preferred. */
6270 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6271 complaint (&symfile_complaints,
6272 _("DW_AT_GNU_call_site_target target DIE has invalid "
6273 "low pc, for referencing DIE 0x%x [in module %s]"),
6274 die->offset, objfile->name);
6275 else
6276 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6277 }
6278 }
6279 else
6280 complaint (&symfile_complaints,
6281 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6282 "block nor reference, for DIE 0x%x [in module %s]"),
6283 die->offset, objfile->name);
6284
6285 call_site->per_cu = cu->per_cu;
6286
6287 for (child_die = die->child;
6288 child_die && child_die->tag;
6289 child_die = sibling_die (child_die))
6290 {
6291 struct dwarf2_locexpr_baton *dlbaton;
6292 struct call_site_parameter *parameter;
6293
6294 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6295 {
6296 /* Already printed the complaint above. */
6297 continue;
6298 }
6299
6300 gdb_assert (call_site->parameter_count < nparams);
6301 parameter = &call_site->parameter[call_site->parameter_count];
6302
6303 /* DW_AT_location specifies the register number. Value of the data
6304 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6305
6306 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6307 if (!attr || !attr_form_is_block (attr))
6308 {
6309 complaint (&symfile_complaints,
6310 _("No DW_FORM_block* DW_AT_location for "
6311 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6312 child_die->offset, objfile->name);
6313 continue;
6314 }
6315 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6316 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6317 if (parameter->dwarf_reg == -1
6318 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6319 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6320 &parameter->fb_offset))
6321 {
6322 complaint (&symfile_complaints,
6323 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6324 "for DW_FORM_block* DW_AT_location for "
6325 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6326 child_die->offset, objfile->name);
6327 continue;
6328 }
6329
6330 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6331 if (!attr_form_is_block (attr))
6332 {
6333 complaint (&symfile_complaints,
6334 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6335 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6336 child_die->offset, objfile->name);
6337 continue;
6338 }
6339 parameter->value = DW_BLOCK (attr)->data;
6340 parameter->value_size = DW_BLOCK (attr)->size;
6341
6342 /* Parameters are not pre-cleared by memset above. */
6343 parameter->data_value = NULL;
6344 parameter->data_value_size = 0;
6345 call_site->parameter_count++;
6346
6347 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6348 if (attr)
6349 {
6350 if (!attr_form_is_block (attr))
6351 complaint (&symfile_complaints,
6352 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6353 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6354 child_die->offset, objfile->name);
6355 else
6356 {
6357 parameter->data_value = DW_BLOCK (attr)->data;
6358 parameter->data_value_size = DW_BLOCK (attr)->size;
6359 }
6360 }
6361 }
6362 }
6363
6364 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6365 Return 1 if the attributes are present and valid, otherwise, return 0.
6366 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6367
6368 static int
6369 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6370 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6371 struct partial_symtab *ranges_pst)
6372 {
6373 struct objfile *objfile = cu->objfile;
6374 struct comp_unit_head *cu_header = &cu->header;
6375 bfd *obfd = objfile->obfd;
6376 unsigned int addr_size = cu_header->addr_size;
6377 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6378 /* Base address selection entry. */
6379 CORE_ADDR base;
6380 int found_base;
6381 unsigned int dummy;
6382 gdb_byte *buffer;
6383 CORE_ADDR marker;
6384 int low_set;
6385 CORE_ADDR low = 0;
6386 CORE_ADDR high = 0;
6387 CORE_ADDR baseaddr;
6388
6389 found_base = cu->base_known;
6390 base = cu->base_address;
6391
6392 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6393 if (offset >= dwarf2_per_objfile->ranges.size)
6394 {
6395 complaint (&symfile_complaints,
6396 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6397 offset);
6398 return 0;
6399 }
6400 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6401
6402 /* Read in the largest possible address. */
6403 marker = read_address (obfd, buffer, cu, &dummy);
6404 if ((marker & mask) == mask)
6405 {
6406 /* If we found the largest possible address, then
6407 read the base address. */
6408 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6409 buffer += 2 * addr_size;
6410 offset += 2 * addr_size;
6411 found_base = 1;
6412 }
6413
6414 low_set = 0;
6415
6416 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6417
6418 while (1)
6419 {
6420 CORE_ADDR range_beginning, range_end;
6421
6422 range_beginning = read_address (obfd, buffer, cu, &dummy);
6423 buffer += addr_size;
6424 range_end = read_address (obfd, buffer, cu, &dummy);
6425 buffer += addr_size;
6426 offset += 2 * addr_size;
6427
6428 /* An end of list marker is a pair of zero addresses. */
6429 if (range_beginning == 0 && range_end == 0)
6430 /* Found the end of list entry. */
6431 break;
6432
6433 /* Each base address selection entry is a pair of 2 values.
6434 The first is the largest possible address, the second is
6435 the base address. Check for a base address here. */
6436 if ((range_beginning & mask) == mask)
6437 {
6438 /* If we found the largest possible address, then
6439 read the base address. */
6440 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6441 found_base = 1;
6442 continue;
6443 }
6444
6445 if (!found_base)
6446 {
6447 /* We have no valid base address for the ranges
6448 data. */
6449 complaint (&symfile_complaints,
6450 _("Invalid .debug_ranges data (no base address)"));
6451 return 0;
6452 }
6453
6454 if (range_beginning > range_end)
6455 {
6456 /* Inverted range entries are invalid. */
6457 complaint (&symfile_complaints,
6458 _("Invalid .debug_ranges data (inverted range)"));
6459 return 0;
6460 }
6461
6462 /* Empty range entries have no effect. */
6463 if (range_beginning == range_end)
6464 continue;
6465
6466 range_beginning += base;
6467 range_end += base;
6468
6469 if (ranges_pst != NULL)
6470 addrmap_set_empty (objfile->psymtabs_addrmap,
6471 range_beginning + baseaddr,
6472 range_end - 1 + baseaddr,
6473 ranges_pst);
6474
6475 /* FIXME: This is recording everything as a low-high
6476 segment of consecutive addresses. We should have a
6477 data structure for discontiguous block ranges
6478 instead. */
6479 if (! low_set)
6480 {
6481 low = range_beginning;
6482 high = range_end;
6483 low_set = 1;
6484 }
6485 else
6486 {
6487 if (range_beginning < low)
6488 low = range_beginning;
6489 if (range_end > high)
6490 high = range_end;
6491 }
6492 }
6493
6494 if (! low_set)
6495 /* If the first entry is an end-of-list marker, the range
6496 describes an empty scope, i.e. no instructions. */
6497 return 0;
6498
6499 if (low_return)
6500 *low_return = low;
6501 if (high_return)
6502 *high_return = high;
6503 return 1;
6504 }
6505
6506 /* Get low and high pc attributes from a die. Return 1 if the attributes
6507 are present and valid, otherwise, return 0. Return -1 if the range is
6508 discontinuous, i.e. derived from DW_AT_ranges information. */
6509 static int
6510 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6511 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6512 struct partial_symtab *pst)
6513 {
6514 struct attribute *attr;
6515 CORE_ADDR low = 0;
6516 CORE_ADDR high = 0;
6517 int ret = 0;
6518
6519 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6520 if (attr)
6521 {
6522 high = DW_ADDR (attr);
6523 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6524 if (attr)
6525 low = DW_ADDR (attr);
6526 else
6527 /* Found high w/o low attribute. */
6528 return 0;
6529
6530 /* Found consecutive range of addresses. */
6531 ret = 1;
6532 }
6533 else
6534 {
6535 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6536 if (attr != NULL)
6537 {
6538 /* Value of the DW_AT_ranges attribute is the offset in the
6539 .debug_ranges section. */
6540 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6541 return 0;
6542 /* Found discontinuous range of addresses. */
6543 ret = -1;
6544 }
6545 }
6546
6547 /* read_partial_die has also the strict LOW < HIGH requirement. */
6548 if (high <= low)
6549 return 0;
6550
6551 /* When using the GNU linker, .gnu.linkonce. sections are used to
6552 eliminate duplicate copies of functions and vtables and such.
6553 The linker will arbitrarily choose one and discard the others.
6554 The AT_*_pc values for such functions refer to local labels in
6555 these sections. If the section from that file was discarded, the
6556 labels are not in the output, so the relocs get a value of 0.
6557 If this is a discarded function, mark the pc bounds as invalid,
6558 so that GDB will ignore it. */
6559 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6560 return 0;
6561
6562 *lowpc = low;
6563 if (highpc)
6564 *highpc = high;
6565 return ret;
6566 }
6567
6568 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6569 its low and high PC addresses. Do nothing if these addresses could not
6570 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6571 and HIGHPC to the high address if greater than HIGHPC. */
6572
6573 static void
6574 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6575 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6576 struct dwarf2_cu *cu)
6577 {
6578 CORE_ADDR low, high;
6579 struct die_info *child = die->child;
6580
6581 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6582 {
6583 *lowpc = min (*lowpc, low);
6584 *highpc = max (*highpc, high);
6585 }
6586
6587 /* If the language does not allow nested subprograms (either inside
6588 subprograms or lexical blocks), we're done. */
6589 if (cu->language != language_ada)
6590 return;
6591
6592 /* Check all the children of the given DIE. If it contains nested
6593 subprograms, then check their pc bounds. Likewise, we need to
6594 check lexical blocks as well, as they may also contain subprogram
6595 definitions. */
6596 while (child && child->tag)
6597 {
6598 if (child->tag == DW_TAG_subprogram
6599 || child->tag == DW_TAG_lexical_block)
6600 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6601 child = sibling_die (child);
6602 }
6603 }
6604
6605 /* Get the low and high pc's represented by the scope DIE, and store
6606 them in *LOWPC and *HIGHPC. If the correct values can't be
6607 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6608
6609 static void
6610 get_scope_pc_bounds (struct die_info *die,
6611 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6612 struct dwarf2_cu *cu)
6613 {
6614 CORE_ADDR best_low = (CORE_ADDR) -1;
6615 CORE_ADDR best_high = (CORE_ADDR) 0;
6616 CORE_ADDR current_low, current_high;
6617
6618 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6619 {
6620 best_low = current_low;
6621 best_high = current_high;
6622 }
6623 else
6624 {
6625 struct die_info *child = die->child;
6626
6627 while (child && child->tag)
6628 {
6629 switch (child->tag) {
6630 case DW_TAG_subprogram:
6631 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6632 break;
6633 case DW_TAG_namespace:
6634 case DW_TAG_module:
6635 /* FIXME: carlton/2004-01-16: Should we do this for
6636 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6637 that current GCC's always emit the DIEs corresponding
6638 to definitions of methods of classes as children of a
6639 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6640 the DIEs giving the declarations, which could be
6641 anywhere). But I don't see any reason why the
6642 standards says that they have to be there. */
6643 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6644
6645 if (current_low != ((CORE_ADDR) -1))
6646 {
6647 best_low = min (best_low, current_low);
6648 best_high = max (best_high, current_high);
6649 }
6650 break;
6651 default:
6652 /* Ignore. */
6653 break;
6654 }
6655
6656 child = sibling_die (child);
6657 }
6658 }
6659
6660 *lowpc = best_low;
6661 *highpc = best_high;
6662 }
6663
6664 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6665 in DIE. */
6666 static void
6667 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6668 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6669 {
6670 struct objfile *objfile = cu->objfile;
6671 struct attribute *attr;
6672
6673 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6674 if (attr)
6675 {
6676 CORE_ADDR high = DW_ADDR (attr);
6677
6678 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6679 if (attr)
6680 {
6681 CORE_ADDR low = DW_ADDR (attr);
6682
6683 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6684 }
6685 }
6686
6687 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6688 if (attr)
6689 {
6690 bfd *obfd = objfile->obfd;
6691
6692 /* The value of the DW_AT_ranges attribute is the offset of the
6693 address range list in the .debug_ranges section. */
6694 unsigned long offset = DW_UNSND (attr);
6695 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6696
6697 /* For some target architectures, but not others, the
6698 read_address function sign-extends the addresses it returns.
6699 To recognize base address selection entries, we need a
6700 mask. */
6701 unsigned int addr_size = cu->header.addr_size;
6702 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6703
6704 /* The base address, to which the next pair is relative. Note
6705 that this 'base' is a DWARF concept: most entries in a range
6706 list are relative, to reduce the number of relocs against the
6707 debugging information. This is separate from this function's
6708 'baseaddr' argument, which GDB uses to relocate debugging
6709 information from a shared library based on the address at
6710 which the library was loaded. */
6711 CORE_ADDR base = cu->base_address;
6712 int base_known = cu->base_known;
6713
6714 gdb_assert (dwarf2_per_objfile->ranges.readin);
6715 if (offset >= dwarf2_per_objfile->ranges.size)
6716 {
6717 complaint (&symfile_complaints,
6718 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6719 offset);
6720 return;
6721 }
6722
6723 for (;;)
6724 {
6725 unsigned int bytes_read;
6726 CORE_ADDR start, end;
6727
6728 start = read_address (obfd, buffer, cu, &bytes_read);
6729 buffer += bytes_read;
6730 end = read_address (obfd, buffer, cu, &bytes_read);
6731 buffer += bytes_read;
6732
6733 /* Did we find the end of the range list? */
6734 if (start == 0 && end == 0)
6735 break;
6736
6737 /* Did we find a base address selection entry? */
6738 else if ((start & base_select_mask) == base_select_mask)
6739 {
6740 base = end;
6741 base_known = 1;
6742 }
6743
6744 /* We found an ordinary address range. */
6745 else
6746 {
6747 if (!base_known)
6748 {
6749 complaint (&symfile_complaints,
6750 _("Invalid .debug_ranges data "
6751 "(no base address)"));
6752 return;
6753 }
6754
6755 if (start > end)
6756 {
6757 /* Inverted range entries are invalid. */
6758 complaint (&symfile_complaints,
6759 _("Invalid .debug_ranges data "
6760 "(inverted range)"));
6761 return;
6762 }
6763
6764 /* Empty range entries have no effect. */
6765 if (start == end)
6766 continue;
6767
6768 record_block_range (block,
6769 baseaddr + base + start,
6770 baseaddr + base + end - 1);
6771 }
6772 }
6773 }
6774 }
6775
6776 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6777 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6778 during 4.6.0 experimental. */
6779
6780 static int
6781 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6782 {
6783 const char *cs;
6784 int major, minor, release;
6785
6786 if (cu->producer == NULL)
6787 {
6788 /* For unknown compilers expect their behavior is DWARF version
6789 compliant.
6790
6791 GCC started to support .debug_types sections by -gdwarf-4 since
6792 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6793 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6794 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6795 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6796
6797 return 0;
6798 }
6799
6800 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6801
6802 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6803 {
6804 /* For non-GCC compilers expect their behavior is DWARF version
6805 compliant. */
6806
6807 return 0;
6808 }
6809 cs = &cu->producer[strlen ("GNU ")];
6810 while (*cs && !isdigit (*cs))
6811 cs++;
6812 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6813 {
6814 /* Not recognized as GCC. */
6815
6816 return 0;
6817 }
6818
6819 return major < 4 || (major == 4 && minor < 6);
6820 }
6821
6822 /* Return the default accessibility type if it is not overriden by
6823 DW_AT_accessibility. */
6824
6825 static enum dwarf_access_attribute
6826 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6827 {
6828 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6829 {
6830 /* The default DWARF 2 accessibility for members is public, the default
6831 accessibility for inheritance is private. */
6832
6833 if (die->tag != DW_TAG_inheritance)
6834 return DW_ACCESS_public;
6835 else
6836 return DW_ACCESS_private;
6837 }
6838 else
6839 {
6840 /* DWARF 3+ defines the default accessibility a different way. The same
6841 rules apply now for DW_TAG_inheritance as for the members and it only
6842 depends on the container kind. */
6843
6844 if (die->parent->tag == DW_TAG_class_type)
6845 return DW_ACCESS_private;
6846 else
6847 return DW_ACCESS_public;
6848 }
6849 }
6850
6851 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6852 offset. If the attribute was not found return 0, otherwise return
6853 1. If it was found but could not properly be handled, set *OFFSET
6854 to 0. */
6855
6856 static int
6857 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6858 LONGEST *offset)
6859 {
6860 struct attribute *attr;
6861
6862 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6863 if (attr != NULL)
6864 {
6865 *offset = 0;
6866
6867 /* Note that we do not check for a section offset first here.
6868 This is because DW_AT_data_member_location is new in DWARF 4,
6869 so if we see it, we can assume that a constant form is really
6870 a constant and not a section offset. */
6871 if (attr_form_is_constant (attr))
6872 *offset = dwarf2_get_attr_constant_value (attr, 0);
6873 else if (attr_form_is_section_offset (attr))
6874 dwarf2_complex_location_expr_complaint ();
6875 else if (attr_form_is_block (attr))
6876 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6877 else
6878 dwarf2_complex_location_expr_complaint ();
6879
6880 return 1;
6881 }
6882
6883 return 0;
6884 }
6885
6886 /* Add an aggregate field to the field list. */
6887
6888 static void
6889 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6890 struct dwarf2_cu *cu)
6891 {
6892 struct objfile *objfile = cu->objfile;
6893 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6894 struct nextfield *new_field;
6895 struct attribute *attr;
6896 struct field *fp;
6897 char *fieldname = "";
6898
6899 /* Allocate a new field list entry and link it in. */
6900 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6901 make_cleanup (xfree, new_field);
6902 memset (new_field, 0, sizeof (struct nextfield));
6903
6904 if (die->tag == DW_TAG_inheritance)
6905 {
6906 new_field->next = fip->baseclasses;
6907 fip->baseclasses = new_field;
6908 }
6909 else
6910 {
6911 new_field->next = fip->fields;
6912 fip->fields = new_field;
6913 }
6914 fip->nfields++;
6915
6916 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6917 if (attr)
6918 new_field->accessibility = DW_UNSND (attr);
6919 else
6920 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6921 if (new_field->accessibility != DW_ACCESS_public)
6922 fip->non_public_fields = 1;
6923
6924 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6925 if (attr)
6926 new_field->virtuality = DW_UNSND (attr);
6927 else
6928 new_field->virtuality = DW_VIRTUALITY_none;
6929
6930 fp = &new_field->field;
6931
6932 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6933 {
6934 LONGEST offset;
6935
6936 /* Data member other than a C++ static data member. */
6937
6938 /* Get type of field. */
6939 fp->type = die_type (die, cu);
6940
6941 SET_FIELD_BITPOS (*fp, 0);
6942
6943 /* Get bit size of field (zero if none). */
6944 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6945 if (attr)
6946 {
6947 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6948 }
6949 else
6950 {
6951 FIELD_BITSIZE (*fp) = 0;
6952 }
6953
6954 /* Get bit offset of field. */
6955 if (handle_data_member_location (die, cu, &offset))
6956 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6957 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6958 if (attr)
6959 {
6960 if (gdbarch_bits_big_endian (gdbarch))
6961 {
6962 /* For big endian bits, the DW_AT_bit_offset gives the
6963 additional bit offset from the MSB of the containing
6964 anonymous object to the MSB of the field. We don't
6965 have to do anything special since we don't need to
6966 know the size of the anonymous object. */
6967 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6968 }
6969 else
6970 {
6971 /* For little endian bits, compute the bit offset to the
6972 MSB of the anonymous object, subtract off the number of
6973 bits from the MSB of the field to the MSB of the
6974 object, and then subtract off the number of bits of
6975 the field itself. The result is the bit offset of
6976 the LSB of the field. */
6977 int anonymous_size;
6978 int bit_offset = DW_UNSND (attr);
6979
6980 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6981 if (attr)
6982 {
6983 /* The size of the anonymous object containing
6984 the bit field is explicit, so use the
6985 indicated size (in bytes). */
6986 anonymous_size = DW_UNSND (attr);
6987 }
6988 else
6989 {
6990 /* The size of the anonymous object containing
6991 the bit field must be inferred from the type
6992 attribute of the data member containing the
6993 bit field. */
6994 anonymous_size = TYPE_LENGTH (fp->type);
6995 }
6996 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6997 - bit_offset - FIELD_BITSIZE (*fp);
6998 }
6999 }
7000
7001 /* Get name of field. */
7002 fieldname = dwarf2_name (die, cu);
7003 if (fieldname == NULL)
7004 fieldname = "";
7005
7006 /* The name is already allocated along with this objfile, so we don't
7007 need to duplicate it for the type. */
7008 fp->name = fieldname;
7009
7010 /* Change accessibility for artificial fields (e.g. virtual table
7011 pointer or virtual base class pointer) to private. */
7012 if (dwarf2_attr (die, DW_AT_artificial, cu))
7013 {
7014 FIELD_ARTIFICIAL (*fp) = 1;
7015 new_field->accessibility = DW_ACCESS_private;
7016 fip->non_public_fields = 1;
7017 }
7018 }
7019 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7020 {
7021 /* C++ static member. */
7022
7023 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7024 is a declaration, but all versions of G++ as of this writing
7025 (so through at least 3.2.1) incorrectly generate
7026 DW_TAG_variable tags. */
7027
7028 const char *physname;
7029
7030 /* Get name of field. */
7031 fieldname = dwarf2_name (die, cu);
7032 if (fieldname == NULL)
7033 return;
7034
7035 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7036 if (attr
7037 /* Only create a symbol if this is an external value.
7038 new_symbol checks this and puts the value in the global symbol
7039 table, which we want. If it is not external, new_symbol
7040 will try to put the value in cu->list_in_scope which is wrong. */
7041 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7042 {
7043 /* A static const member, not much different than an enum as far as
7044 we're concerned, except that we can support more types. */
7045 new_symbol (die, NULL, cu);
7046 }
7047
7048 /* Get physical name. */
7049 physname = dwarf2_physname (fieldname, die, cu);
7050
7051 /* The name is already allocated along with this objfile, so we don't
7052 need to duplicate it for the type. */
7053 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7054 FIELD_TYPE (*fp) = die_type (die, cu);
7055 FIELD_NAME (*fp) = fieldname;
7056 }
7057 else if (die->tag == DW_TAG_inheritance)
7058 {
7059 LONGEST offset;
7060
7061 /* C++ base class field. */
7062 if (handle_data_member_location (die, cu, &offset))
7063 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7064 FIELD_BITSIZE (*fp) = 0;
7065 FIELD_TYPE (*fp) = die_type (die, cu);
7066 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7067 fip->nbaseclasses++;
7068 }
7069 }
7070
7071 /* Add a typedef defined in the scope of the FIP's class. */
7072
7073 static void
7074 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7075 struct dwarf2_cu *cu)
7076 {
7077 struct objfile *objfile = cu->objfile;
7078 struct typedef_field_list *new_field;
7079 struct attribute *attr;
7080 struct typedef_field *fp;
7081 char *fieldname = "";
7082
7083 /* Allocate a new field list entry and link it in. */
7084 new_field = xzalloc (sizeof (*new_field));
7085 make_cleanup (xfree, new_field);
7086
7087 gdb_assert (die->tag == DW_TAG_typedef);
7088
7089 fp = &new_field->field;
7090
7091 /* Get name of field. */
7092 fp->name = dwarf2_name (die, cu);
7093 if (fp->name == NULL)
7094 return;
7095
7096 fp->type = read_type_die (die, cu);
7097
7098 new_field->next = fip->typedef_field_list;
7099 fip->typedef_field_list = new_field;
7100 fip->typedef_field_list_count++;
7101 }
7102
7103 /* Create the vector of fields, and attach it to the type. */
7104
7105 static void
7106 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7107 struct dwarf2_cu *cu)
7108 {
7109 int nfields = fip->nfields;
7110
7111 /* Record the field count, allocate space for the array of fields,
7112 and create blank accessibility bitfields if necessary. */
7113 TYPE_NFIELDS (type) = nfields;
7114 TYPE_FIELDS (type) = (struct field *)
7115 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7116 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7117
7118 if (fip->non_public_fields && cu->language != language_ada)
7119 {
7120 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7121
7122 TYPE_FIELD_PRIVATE_BITS (type) =
7123 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7124 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7125
7126 TYPE_FIELD_PROTECTED_BITS (type) =
7127 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7128 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7129
7130 TYPE_FIELD_IGNORE_BITS (type) =
7131 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7132 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7133 }
7134
7135 /* If the type has baseclasses, allocate and clear a bit vector for
7136 TYPE_FIELD_VIRTUAL_BITS. */
7137 if (fip->nbaseclasses && cu->language != language_ada)
7138 {
7139 int num_bytes = B_BYTES (fip->nbaseclasses);
7140 unsigned char *pointer;
7141
7142 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7143 pointer = TYPE_ALLOC (type, num_bytes);
7144 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7145 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7146 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7147 }
7148
7149 /* Copy the saved-up fields into the field vector. Start from the head of
7150 the list, adding to the tail of the field array, so that they end up in
7151 the same order in the array in which they were added to the list. */
7152 while (nfields-- > 0)
7153 {
7154 struct nextfield *fieldp;
7155
7156 if (fip->fields)
7157 {
7158 fieldp = fip->fields;
7159 fip->fields = fieldp->next;
7160 }
7161 else
7162 {
7163 fieldp = fip->baseclasses;
7164 fip->baseclasses = fieldp->next;
7165 }
7166
7167 TYPE_FIELD (type, nfields) = fieldp->field;
7168 switch (fieldp->accessibility)
7169 {
7170 case DW_ACCESS_private:
7171 if (cu->language != language_ada)
7172 SET_TYPE_FIELD_PRIVATE (type, nfields);
7173 break;
7174
7175 case DW_ACCESS_protected:
7176 if (cu->language != language_ada)
7177 SET_TYPE_FIELD_PROTECTED (type, nfields);
7178 break;
7179
7180 case DW_ACCESS_public:
7181 break;
7182
7183 default:
7184 /* Unknown accessibility. Complain and treat it as public. */
7185 {
7186 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7187 fieldp->accessibility);
7188 }
7189 break;
7190 }
7191 if (nfields < fip->nbaseclasses)
7192 {
7193 switch (fieldp->virtuality)
7194 {
7195 case DW_VIRTUALITY_virtual:
7196 case DW_VIRTUALITY_pure_virtual:
7197 if (cu->language == language_ada)
7198 error (_("unexpected virtuality in component of Ada type"));
7199 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7200 break;
7201 }
7202 }
7203 }
7204 }
7205
7206 /* Add a member function to the proper fieldlist. */
7207
7208 static void
7209 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7210 struct type *type, struct dwarf2_cu *cu)
7211 {
7212 struct objfile *objfile = cu->objfile;
7213 struct attribute *attr;
7214 struct fnfieldlist *flp;
7215 int i;
7216 struct fn_field *fnp;
7217 char *fieldname;
7218 struct nextfnfield *new_fnfield;
7219 struct type *this_type;
7220 enum dwarf_access_attribute accessibility;
7221
7222 if (cu->language == language_ada)
7223 error (_("unexpected member function in Ada type"));
7224
7225 /* Get name of member function. */
7226 fieldname = dwarf2_name (die, cu);
7227 if (fieldname == NULL)
7228 return;
7229
7230 /* Look up member function name in fieldlist. */
7231 for (i = 0; i < fip->nfnfields; i++)
7232 {
7233 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7234 break;
7235 }
7236
7237 /* Create new list element if necessary. */
7238 if (i < fip->nfnfields)
7239 flp = &fip->fnfieldlists[i];
7240 else
7241 {
7242 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7243 {
7244 fip->fnfieldlists = (struct fnfieldlist *)
7245 xrealloc (fip->fnfieldlists,
7246 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7247 * sizeof (struct fnfieldlist));
7248 if (fip->nfnfields == 0)
7249 make_cleanup (free_current_contents, &fip->fnfieldlists);
7250 }
7251 flp = &fip->fnfieldlists[fip->nfnfields];
7252 flp->name = fieldname;
7253 flp->length = 0;
7254 flp->head = NULL;
7255 i = fip->nfnfields++;
7256 }
7257
7258 /* Create a new member function field and chain it to the field list
7259 entry. */
7260 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7261 make_cleanup (xfree, new_fnfield);
7262 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7263 new_fnfield->next = flp->head;
7264 flp->head = new_fnfield;
7265 flp->length++;
7266
7267 /* Fill in the member function field info. */
7268 fnp = &new_fnfield->fnfield;
7269
7270 /* Delay processing of the physname until later. */
7271 if (cu->language == language_cplus || cu->language == language_java)
7272 {
7273 add_to_method_list (type, i, flp->length - 1, fieldname,
7274 die, cu);
7275 }
7276 else
7277 {
7278 const char *physname = dwarf2_physname (fieldname, die, cu);
7279 fnp->physname = physname ? physname : "";
7280 }
7281
7282 fnp->type = alloc_type (objfile);
7283 this_type = read_type_die (die, cu);
7284 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7285 {
7286 int nparams = TYPE_NFIELDS (this_type);
7287
7288 /* TYPE is the domain of this method, and THIS_TYPE is the type
7289 of the method itself (TYPE_CODE_METHOD). */
7290 smash_to_method_type (fnp->type, type,
7291 TYPE_TARGET_TYPE (this_type),
7292 TYPE_FIELDS (this_type),
7293 TYPE_NFIELDS (this_type),
7294 TYPE_VARARGS (this_type));
7295
7296 /* Handle static member functions.
7297 Dwarf2 has no clean way to discern C++ static and non-static
7298 member functions. G++ helps GDB by marking the first
7299 parameter for non-static member functions (which is the this
7300 pointer) as artificial. We obtain this information from
7301 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7302 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7303 fnp->voffset = VOFFSET_STATIC;
7304 }
7305 else
7306 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7307 dwarf2_full_name (fieldname, die, cu));
7308
7309 /* Get fcontext from DW_AT_containing_type if present. */
7310 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7311 fnp->fcontext = die_containing_type (die, cu);
7312
7313 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7314 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7315
7316 /* Get accessibility. */
7317 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7318 if (attr)
7319 accessibility = DW_UNSND (attr);
7320 else
7321 accessibility = dwarf2_default_access_attribute (die, cu);
7322 switch (accessibility)
7323 {
7324 case DW_ACCESS_private:
7325 fnp->is_private = 1;
7326 break;
7327 case DW_ACCESS_protected:
7328 fnp->is_protected = 1;
7329 break;
7330 }
7331
7332 /* Check for artificial methods. */
7333 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7334 if (attr && DW_UNSND (attr) != 0)
7335 fnp->is_artificial = 1;
7336
7337 /* Get index in virtual function table if it is a virtual member
7338 function. For older versions of GCC, this is an offset in the
7339 appropriate virtual table, as specified by DW_AT_containing_type.
7340 For everyone else, it is an expression to be evaluated relative
7341 to the object address. */
7342
7343 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7344 if (attr)
7345 {
7346 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7347 {
7348 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7349 {
7350 /* Old-style GCC. */
7351 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7352 }
7353 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7354 || (DW_BLOCK (attr)->size > 1
7355 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7356 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7357 {
7358 struct dwarf_block blk;
7359 int offset;
7360
7361 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7362 ? 1 : 2);
7363 blk.size = DW_BLOCK (attr)->size - offset;
7364 blk.data = DW_BLOCK (attr)->data + offset;
7365 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7366 if ((fnp->voffset % cu->header.addr_size) != 0)
7367 dwarf2_complex_location_expr_complaint ();
7368 else
7369 fnp->voffset /= cu->header.addr_size;
7370 fnp->voffset += 2;
7371 }
7372 else
7373 dwarf2_complex_location_expr_complaint ();
7374
7375 if (!fnp->fcontext)
7376 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7377 }
7378 else if (attr_form_is_section_offset (attr))
7379 {
7380 dwarf2_complex_location_expr_complaint ();
7381 }
7382 else
7383 {
7384 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7385 fieldname);
7386 }
7387 }
7388 else
7389 {
7390 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7391 if (attr && DW_UNSND (attr))
7392 {
7393 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7394 complaint (&symfile_complaints,
7395 _("Member function \"%s\" (offset %d) is virtual "
7396 "but the vtable offset is not specified"),
7397 fieldname, die->offset);
7398 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7399 TYPE_CPLUS_DYNAMIC (type) = 1;
7400 }
7401 }
7402 }
7403
7404 /* Create the vector of member function fields, and attach it to the type. */
7405
7406 static void
7407 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7408 struct dwarf2_cu *cu)
7409 {
7410 struct fnfieldlist *flp;
7411 int total_length = 0;
7412 int i;
7413
7414 if (cu->language == language_ada)
7415 error (_("unexpected member functions in Ada type"));
7416
7417 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7418 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7419 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7420
7421 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7422 {
7423 struct nextfnfield *nfp = flp->head;
7424 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7425 int k;
7426
7427 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7428 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7429 fn_flp->fn_fields = (struct fn_field *)
7430 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7431 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7432 fn_flp->fn_fields[k] = nfp->fnfield;
7433
7434 total_length += flp->length;
7435 }
7436
7437 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7438 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7439 }
7440
7441 /* Returns non-zero if NAME is the name of a vtable member in CU's
7442 language, zero otherwise. */
7443 static int
7444 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7445 {
7446 static const char vptr[] = "_vptr";
7447 static const char vtable[] = "vtable";
7448
7449 /* Look for the C++ and Java forms of the vtable. */
7450 if ((cu->language == language_java
7451 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7452 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7453 && is_cplus_marker (name[sizeof (vptr) - 1])))
7454 return 1;
7455
7456 return 0;
7457 }
7458
7459 /* GCC outputs unnamed structures that are really pointers to member
7460 functions, with the ABI-specified layout. If TYPE describes
7461 such a structure, smash it into a member function type.
7462
7463 GCC shouldn't do this; it should just output pointer to member DIEs.
7464 This is GCC PR debug/28767. */
7465
7466 static void
7467 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7468 {
7469 struct type *pfn_type, *domain_type, *new_type;
7470
7471 /* Check for a structure with no name and two children. */
7472 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7473 return;
7474
7475 /* Check for __pfn and __delta members. */
7476 if (TYPE_FIELD_NAME (type, 0) == NULL
7477 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7478 || TYPE_FIELD_NAME (type, 1) == NULL
7479 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7480 return;
7481
7482 /* Find the type of the method. */
7483 pfn_type = TYPE_FIELD_TYPE (type, 0);
7484 if (pfn_type == NULL
7485 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7486 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7487 return;
7488
7489 /* Look for the "this" argument. */
7490 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7491 if (TYPE_NFIELDS (pfn_type) == 0
7492 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7493 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7494 return;
7495
7496 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7497 new_type = alloc_type (objfile);
7498 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7499 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7500 TYPE_VARARGS (pfn_type));
7501 smash_to_methodptr_type (type, new_type);
7502 }
7503
7504 /* Called when we find the DIE that starts a structure or union scope
7505 (definition) to create a type for the structure or union. Fill in
7506 the type's name and general properties; the members will not be
7507 processed until process_structure_type.
7508
7509 NOTE: we need to call these functions regardless of whether or not the
7510 DIE has a DW_AT_name attribute, since it might be an anonymous
7511 structure or union. This gets the type entered into our set of
7512 user defined types.
7513
7514 However, if the structure is incomplete (an opaque struct/union)
7515 then suppress creating a symbol table entry for it since gdb only
7516 wants to find the one with the complete definition. Note that if
7517 it is complete, we just call new_symbol, which does it's own
7518 checking about whether the struct/union is anonymous or not (and
7519 suppresses creating a symbol table entry itself). */
7520
7521 static struct type *
7522 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7523 {
7524 struct objfile *objfile = cu->objfile;
7525 struct type *type;
7526 struct attribute *attr;
7527 char *name;
7528
7529 /* If the definition of this type lives in .debug_types, read that type.
7530 Don't follow DW_AT_specification though, that will take us back up
7531 the chain and we want to go down. */
7532 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7533 if (attr)
7534 {
7535 struct dwarf2_cu *type_cu = cu;
7536 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7537
7538 /* We could just recurse on read_structure_type, but we need to call
7539 get_die_type to ensure only one type for this DIE is created.
7540 This is important, for example, because for c++ classes we need
7541 TYPE_NAME set which is only done by new_symbol. Blech. */
7542 type = read_type_die (type_die, type_cu);
7543
7544 /* TYPE_CU may not be the same as CU.
7545 Ensure TYPE is recorded in CU's type_hash table. */
7546 return set_die_type (die, type, cu);
7547 }
7548
7549 type = alloc_type (objfile);
7550 INIT_CPLUS_SPECIFIC (type);
7551
7552 name = dwarf2_name (die, cu);
7553 if (name != NULL)
7554 {
7555 if (cu->language == language_cplus
7556 || cu->language == language_java)
7557 {
7558 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7559
7560 /* dwarf2_full_name might have already finished building the DIE's
7561 type. If so, there is no need to continue. */
7562 if (get_die_type (die, cu) != NULL)
7563 return get_die_type (die, cu);
7564
7565 TYPE_TAG_NAME (type) = full_name;
7566 if (die->tag == DW_TAG_structure_type
7567 || die->tag == DW_TAG_class_type)
7568 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7569 }
7570 else
7571 {
7572 /* The name is already allocated along with this objfile, so
7573 we don't need to duplicate it for the type. */
7574 TYPE_TAG_NAME (type) = (char *) name;
7575 if (die->tag == DW_TAG_class_type)
7576 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7577 }
7578 }
7579
7580 if (die->tag == DW_TAG_structure_type)
7581 {
7582 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7583 }
7584 else if (die->tag == DW_TAG_union_type)
7585 {
7586 TYPE_CODE (type) = TYPE_CODE_UNION;
7587 }
7588 else
7589 {
7590 TYPE_CODE (type) = TYPE_CODE_CLASS;
7591 }
7592
7593 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7594 TYPE_DECLARED_CLASS (type) = 1;
7595
7596 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7597 if (attr)
7598 {
7599 TYPE_LENGTH (type) = DW_UNSND (attr);
7600 }
7601 else
7602 {
7603 TYPE_LENGTH (type) = 0;
7604 }
7605
7606 TYPE_STUB_SUPPORTED (type) = 1;
7607 if (die_is_declaration (die, cu))
7608 TYPE_STUB (type) = 1;
7609 else if (attr == NULL && die->child == NULL
7610 && producer_is_realview (cu->producer))
7611 /* RealView does not output the required DW_AT_declaration
7612 on incomplete types. */
7613 TYPE_STUB (type) = 1;
7614
7615 /* We need to add the type field to the die immediately so we don't
7616 infinitely recurse when dealing with pointers to the structure
7617 type within the structure itself. */
7618 set_die_type (die, type, cu);
7619
7620 /* set_die_type should be already done. */
7621 set_descriptive_type (type, die, cu);
7622
7623 return type;
7624 }
7625
7626 /* Finish creating a structure or union type, including filling in
7627 its members and creating a symbol for it. */
7628
7629 static void
7630 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7631 {
7632 struct objfile *objfile = cu->objfile;
7633 struct die_info *child_die = die->child;
7634 struct type *type;
7635
7636 type = get_die_type (die, cu);
7637 if (type == NULL)
7638 type = read_structure_type (die, cu);
7639
7640 if (die->child != NULL && ! die_is_declaration (die, cu))
7641 {
7642 struct field_info fi;
7643 struct die_info *child_die;
7644 VEC (symbolp) *template_args = NULL;
7645 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7646
7647 memset (&fi, 0, sizeof (struct field_info));
7648
7649 child_die = die->child;
7650
7651 while (child_die && child_die->tag)
7652 {
7653 if (child_die->tag == DW_TAG_member
7654 || child_die->tag == DW_TAG_variable)
7655 {
7656 /* NOTE: carlton/2002-11-05: A C++ static data member
7657 should be a DW_TAG_member that is a declaration, but
7658 all versions of G++ as of this writing (so through at
7659 least 3.2.1) incorrectly generate DW_TAG_variable
7660 tags for them instead. */
7661 dwarf2_add_field (&fi, child_die, cu);
7662 }
7663 else if (child_die->tag == DW_TAG_subprogram)
7664 {
7665 /* C++ member function. */
7666 dwarf2_add_member_fn (&fi, child_die, type, cu);
7667 }
7668 else if (child_die->tag == DW_TAG_inheritance)
7669 {
7670 /* C++ base class field. */
7671 dwarf2_add_field (&fi, child_die, cu);
7672 }
7673 else if (child_die->tag == DW_TAG_typedef)
7674 dwarf2_add_typedef (&fi, child_die, cu);
7675 else if (child_die->tag == DW_TAG_template_type_param
7676 || child_die->tag == DW_TAG_template_value_param)
7677 {
7678 struct symbol *arg = new_symbol (child_die, NULL, cu);
7679
7680 if (arg != NULL)
7681 VEC_safe_push (symbolp, template_args, arg);
7682 }
7683
7684 child_die = sibling_die (child_die);
7685 }
7686
7687 /* Attach template arguments to type. */
7688 if (! VEC_empty (symbolp, template_args))
7689 {
7690 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7691 TYPE_N_TEMPLATE_ARGUMENTS (type)
7692 = VEC_length (symbolp, template_args);
7693 TYPE_TEMPLATE_ARGUMENTS (type)
7694 = obstack_alloc (&objfile->objfile_obstack,
7695 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7696 * sizeof (struct symbol *)));
7697 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7698 VEC_address (symbolp, template_args),
7699 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7700 * sizeof (struct symbol *)));
7701 VEC_free (symbolp, template_args);
7702 }
7703
7704 /* Attach fields and member functions to the type. */
7705 if (fi.nfields)
7706 dwarf2_attach_fields_to_type (&fi, type, cu);
7707 if (fi.nfnfields)
7708 {
7709 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7710
7711 /* Get the type which refers to the base class (possibly this
7712 class itself) which contains the vtable pointer for the current
7713 class from the DW_AT_containing_type attribute. This use of
7714 DW_AT_containing_type is a GNU extension. */
7715
7716 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7717 {
7718 struct type *t = die_containing_type (die, cu);
7719
7720 TYPE_VPTR_BASETYPE (type) = t;
7721 if (type == t)
7722 {
7723 int i;
7724
7725 /* Our own class provides vtbl ptr. */
7726 for (i = TYPE_NFIELDS (t) - 1;
7727 i >= TYPE_N_BASECLASSES (t);
7728 --i)
7729 {
7730 char *fieldname = TYPE_FIELD_NAME (t, i);
7731
7732 if (is_vtable_name (fieldname, cu))
7733 {
7734 TYPE_VPTR_FIELDNO (type) = i;
7735 break;
7736 }
7737 }
7738
7739 /* Complain if virtual function table field not found. */
7740 if (i < TYPE_N_BASECLASSES (t))
7741 complaint (&symfile_complaints,
7742 _("virtual function table pointer "
7743 "not found when defining class '%s'"),
7744 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7745 "");
7746 }
7747 else
7748 {
7749 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7750 }
7751 }
7752 else if (cu->producer
7753 && strncmp (cu->producer,
7754 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7755 {
7756 /* The IBM XLC compiler does not provide direct indication
7757 of the containing type, but the vtable pointer is
7758 always named __vfp. */
7759
7760 int i;
7761
7762 for (i = TYPE_NFIELDS (type) - 1;
7763 i >= TYPE_N_BASECLASSES (type);
7764 --i)
7765 {
7766 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7767 {
7768 TYPE_VPTR_FIELDNO (type) = i;
7769 TYPE_VPTR_BASETYPE (type) = type;
7770 break;
7771 }
7772 }
7773 }
7774 }
7775
7776 /* Copy fi.typedef_field_list linked list elements content into the
7777 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7778 if (fi.typedef_field_list)
7779 {
7780 int i = fi.typedef_field_list_count;
7781
7782 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7783 TYPE_TYPEDEF_FIELD_ARRAY (type)
7784 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7785 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7786
7787 /* Reverse the list order to keep the debug info elements order. */
7788 while (--i >= 0)
7789 {
7790 struct typedef_field *dest, *src;
7791
7792 dest = &TYPE_TYPEDEF_FIELD (type, i);
7793 src = &fi.typedef_field_list->field;
7794 fi.typedef_field_list = fi.typedef_field_list->next;
7795 *dest = *src;
7796 }
7797 }
7798
7799 do_cleanups (back_to);
7800
7801 if (HAVE_CPLUS_STRUCT (type))
7802 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7803 }
7804
7805 quirk_gcc_member_function_pointer (type, objfile);
7806
7807 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7808 snapshots) has been known to create a die giving a declaration
7809 for a class that has, as a child, a die giving a definition for a
7810 nested class. So we have to process our children even if the
7811 current die is a declaration. Normally, of course, a declaration
7812 won't have any children at all. */
7813
7814 while (child_die != NULL && child_die->tag)
7815 {
7816 if (child_die->tag == DW_TAG_member
7817 || child_die->tag == DW_TAG_variable
7818 || child_die->tag == DW_TAG_inheritance
7819 || child_die->tag == DW_TAG_template_value_param
7820 || child_die->tag == DW_TAG_template_type_param)
7821 {
7822 /* Do nothing. */
7823 }
7824 else
7825 process_die (child_die, cu);
7826
7827 child_die = sibling_die (child_die);
7828 }
7829
7830 /* Do not consider external references. According to the DWARF standard,
7831 these DIEs are identified by the fact that they have no byte_size
7832 attribute, and a declaration attribute. */
7833 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7834 || !die_is_declaration (die, cu))
7835 new_symbol (die, type, cu);
7836 }
7837
7838 /* Given a DW_AT_enumeration_type die, set its type. We do not
7839 complete the type's fields yet, or create any symbols. */
7840
7841 static struct type *
7842 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7843 {
7844 struct objfile *objfile = cu->objfile;
7845 struct type *type;
7846 struct attribute *attr;
7847 const char *name;
7848
7849 /* If the definition of this type lives in .debug_types, read that type.
7850 Don't follow DW_AT_specification though, that will take us back up
7851 the chain and we want to go down. */
7852 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7853 if (attr)
7854 {
7855 struct dwarf2_cu *type_cu = cu;
7856 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7857
7858 type = read_type_die (type_die, type_cu);
7859
7860 /* TYPE_CU may not be the same as CU.
7861 Ensure TYPE is recorded in CU's type_hash table. */
7862 return set_die_type (die, type, cu);
7863 }
7864
7865 type = alloc_type (objfile);
7866
7867 TYPE_CODE (type) = TYPE_CODE_ENUM;
7868 name = dwarf2_full_name (NULL, die, cu);
7869 if (name != NULL)
7870 TYPE_TAG_NAME (type) = (char *) name;
7871
7872 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7873 if (attr)
7874 {
7875 TYPE_LENGTH (type) = DW_UNSND (attr);
7876 }
7877 else
7878 {
7879 TYPE_LENGTH (type) = 0;
7880 }
7881
7882 /* The enumeration DIE can be incomplete. In Ada, any type can be
7883 declared as private in the package spec, and then defined only
7884 inside the package body. Such types are known as Taft Amendment
7885 Types. When another package uses such a type, an incomplete DIE
7886 may be generated by the compiler. */
7887 if (die_is_declaration (die, cu))
7888 TYPE_STUB (type) = 1;
7889
7890 return set_die_type (die, type, cu);
7891 }
7892
7893 /* Given a pointer to a die which begins an enumeration, process all
7894 the dies that define the members of the enumeration, and create the
7895 symbol for the enumeration type.
7896
7897 NOTE: We reverse the order of the element list. */
7898
7899 static void
7900 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7901 {
7902 struct type *this_type;
7903
7904 this_type = get_die_type (die, cu);
7905 if (this_type == NULL)
7906 this_type = read_enumeration_type (die, cu);
7907
7908 if (die->child != NULL)
7909 {
7910 struct die_info *child_die;
7911 struct symbol *sym;
7912 struct field *fields = NULL;
7913 int num_fields = 0;
7914 int unsigned_enum = 1;
7915 char *name;
7916
7917 child_die = die->child;
7918 while (child_die && child_die->tag)
7919 {
7920 if (child_die->tag != DW_TAG_enumerator)
7921 {
7922 process_die (child_die, cu);
7923 }
7924 else
7925 {
7926 name = dwarf2_name (child_die, cu);
7927 if (name)
7928 {
7929 sym = new_symbol (child_die, this_type, cu);
7930 if (SYMBOL_VALUE (sym) < 0)
7931 unsigned_enum = 0;
7932
7933 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7934 {
7935 fields = (struct field *)
7936 xrealloc (fields,
7937 (num_fields + DW_FIELD_ALLOC_CHUNK)
7938 * sizeof (struct field));
7939 }
7940
7941 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7942 FIELD_TYPE (fields[num_fields]) = NULL;
7943 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7944 FIELD_BITSIZE (fields[num_fields]) = 0;
7945
7946 num_fields++;
7947 }
7948 }
7949
7950 child_die = sibling_die (child_die);
7951 }
7952
7953 if (num_fields)
7954 {
7955 TYPE_NFIELDS (this_type) = num_fields;
7956 TYPE_FIELDS (this_type) = (struct field *)
7957 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7958 memcpy (TYPE_FIELDS (this_type), fields,
7959 sizeof (struct field) * num_fields);
7960 xfree (fields);
7961 }
7962 if (unsigned_enum)
7963 TYPE_UNSIGNED (this_type) = 1;
7964 }
7965
7966 /* If we are reading an enum from a .debug_types unit, and the enum
7967 is a declaration, and the enum is not the signatured type in the
7968 unit, then we do not want to add a symbol for it. Adding a
7969 symbol would in some cases obscure the true definition of the
7970 enum, giving users an incomplete type when the definition is
7971 actually available. Note that we do not want to do this for all
7972 enums which are just declarations, because C++0x allows forward
7973 enum declarations. */
7974 if (cu->per_cu->debug_types_section
7975 && die_is_declaration (die, cu))
7976 {
7977 struct signatured_type *type_sig;
7978
7979 type_sig
7980 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7981 cu->per_cu->debug_types_section,
7982 cu->per_cu->offset);
7983 if (type_sig->type_offset != die->offset)
7984 return;
7985 }
7986
7987 new_symbol (die, this_type, cu);
7988 }
7989
7990 /* Extract all information from a DW_TAG_array_type DIE and put it in
7991 the DIE's type field. For now, this only handles one dimensional
7992 arrays. */
7993
7994 static struct type *
7995 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7996 {
7997 struct objfile *objfile = cu->objfile;
7998 struct die_info *child_die;
7999 struct type *type;
8000 struct type *element_type, *range_type, *index_type;
8001 struct type **range_types = NULL;
8002 struct attribute *attr;
8003 int ndim = 0;
8004 struct cleanup *back_to;
8005 char *name;
8006
8007 element_type = die_type (die, cu);
8008
8009 /* The die_type call above may have already set the type for this DIE. */
8010 type = get_die_type (die, cu);
8011 if (type)
8012 return type;
8013
8014 /* Irix 6.2 native cc creates array types without children for
8015 arrays with unspecified length. */
8016 if (die->child == NULL)
8017 {
8018 index_type = objfile_type (objfile)->builtin_int;
8019 range_type = create_range_type (NULL, index_type, 0, -1);
8020 type = create_array_type (NULL, element_type, range_type);
8021 return set_die_type (die, type, cu);
8022 }
8023
8024 back_to = make_cleanup (null_cleanup, NULL);
8025 child_die = die->child;
8026 while (child_die && child_die->tag)
8027 {
8028 if (child_die->tag == DW_TAG_subrange_type)
8029 {
8030 struct type *child_type = read_type_die (child_die, cu);
8031
8032 if (child_type != NULL)
8033 {
8034 /* The range type was succesfully read. Save it for the
8035 array type creation. */
8036 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8037 {
8038 range_types = (struct type **)
8039 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8040 * sizeof (struct type *));
8041 if (ndim == 0)
8042 make_cleanup (free_current_contents, &range_types);
8043 }
8044 range_types[ndim++] = child_type;
8045 }
8046 }
8047 child_die = sibling_die (child_die);
8048 }
8049
8050 /* Dwarf2 dimensions are output from left to right, create the
8051 necessary array types in backwards order. */
8052
8053 type = element_type;
8054
8055 if (read_array_order (die, cu) == DW_ORD_col_major)
8056 {
8057 int i = 0;
8058
8059 while (i < ndim)
8060 type = create_array_type (NULL, type, range_types[i++]);
8061 }
8062 else
8063 {
8064 while (ndim-- > 0)
8065 type = create_array_type (NULL, type, range_types[ndim]);
8066 }
8067
8068 /* Understand Dwarf2 support for vector types (like they occur on
8069 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8070 array type. This is not part of the Dwarf2/3 standard yet, but a
8071 custom vendor extension. The main difference between a regular
8072 array and the vector variant is that vectors are passed by value
8073 to functions. */
8074 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8075 if (attr)
8076 make_vector_type (type);
8077
8078 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8079 implementation may choose to implement triple vectors using this
8080 attribute. */
8081 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8082 if (attr)
8083 {
8084 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8085 TYPE_LENGTH (type) = DW_UNSND (attr);
8086 else
8087 complaint (&symfile_complaints,
8088 _("DW_AT_byte_size for array type smaller "
8089 "than the total size of elements"));
8090 }
8091
8092 name = dwarf2_name (die, cu);
8093 if (name)
8094 TYPE_NAME (type) = name;
8095
8096 /* Install the type in the die. */
8097 set_die_type (die, type, cu);
8098
8099 /* set_die_type should be already done. */
8100 set_descriptive_type (type, die, cu);
8101
8102 do_cleanups (back_to);
8103
8104 return type;
8105 }
8106
8107 static enum dwarf_array_dim_ordering
8108 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8109 {
8110 struct attribute *attr;
8111
8112 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8113
8114 if (attr) return DW_SND (attr);
8115
8116 /* GNU F77 is a special case, as at 08/2004 array type info is the
8117 opposite order to the dwarf2 specification, but data is still
8118 laid out as per normal fortran.
8119
8120 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8121 version checking. */
8122
8123 if (cu->language == language_fortran
8124 && cu->producer && strstr (cu->producer, "GNU F77"))
8125 {
8126 return DW_ORD_row_major;
8127 }
8128
8129 switch (cu->language_defn->la_array_ordering)
8130 {
8131 case array_column_major:
8132 return DW_ORD_col_major;
8133 case array_row_major:
8134 default:
8135 return DW_ORD_row_major;
8136 };
8137 }
8138
8139 /* Extract all information from a DW_TAG_set_type DIE and put it in
8140 the DIE's type field. */
8141
8142 static struct type *
8143 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8144 {
8145 struct type *domain_type, *set_type;
8146 struct attribute *attr;
8147
8148 domain_type = die_type (die, cu);
8149
8150 /* The die_type call above may have already set the type for this DIE. */
8151 set_type = get_die_type (die, cu);
8152 if (set_type)
8153 return set_type;
8154
8155 set_type = create_set_type (NULL, domain_type);
8156
8157 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8158 if (attr)
8159 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8160
8161 return set_die_type (die, set_type, cu);
8162 }
8163
8164 /* First cut: install each common block member as a global variable. */
8165
8166 static void
8167 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8168 {
8169 struct die_info *child_die;
8170 struct attribute *attr;
8171 struct symbol *sym;
8172 CORE_ADDR base = (CORE_ADDR) 0;
8173
8174 attr = dwarf2_attr (die, DW_AT_location, cu);
8175 if (attr)
8176 {
8177 /* Support the .debug_loc offsets. */
8178 if (attr_form_is_block (attr))
8179 {
8180 base = decode_locdesc (DW_BLOCK (attr), cu);
8181 }
8182 else if (attr_form_is_section_offset (attr))
8183 {
8184 dwarf2_complex_location_expr_complaint ();
8185 }
8186 else
8187 {
8188 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8189 "common block member");
8190 }
8191 }
8192 if (die->child != NULL)
8193 {
8194 child_die = die->child;
8195 while (child_die && child_die->tag)
8196 {
8197 LONGEST offset;
8198
8199 sym = new_symbol (child_die, NULL, cu);
8200 if (sym != NULL
8201 && handle_data_member_location (child_die, cu, &offset))
8202 {
8203 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8204 add_symbol_to_list (sym, &global_symbols);
8205 }
8206 child_die = sibling_die (child_die);
8207 }
8208 }
8209 }
8210
8211 /* Create a type for a C++ namespace. */
8212
8213 static struct type *
8214 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8215 {
8216 struct objfile *objfile = cu->objfile;
8217 const char *previous_prefix, *name;
8218 int is_anonymous;
8219 struct type *type;
8220
8221 /* For extensions, reuse the type of the original namespace. */
8222 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8223 {
8224 struct die_info *ext_die;
8225 struct dwarf2_cu *ext_cu = cu;
8226
8227 ext_die = dwarf2_extension (die, &ext_cu);
8228 type = read_type_die (ext_die, ext_cu);
8229
8230 /* EXT_CU may not be the same as CU.
8231 Ensure TYPE is recorded in CU's type_hash table. */
8232 return set_die_type (die, type, cu);
8233 }
8234
8235 name = namespace_name (die, &is_anonymous, cu);
8236
8237 /* Now build the name of the current namespace. */
8238
8239 previous_prefix = determine_prefix (die, cu);
8240 if (previous_prefix[0] != '\0')
8241 name = typename_concat (&objfile->objfile_obstack,
8242 previous_prefix, name, 0, cu);
8243
8244 /* Create the type. */
8245 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8246 objfile);
8247 TYPE_NAME (type) = (char *) name;
8248 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8249
8250 return set_die_type (die, type, cu);
8251 }
8252
8253 /* Read a C++ namespace. */
8254
8255 static void
8256 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8257 {
8258 struct objfile *objfile = cu->objfile;
8259 int is_anonymous;
8260
8261 /* Add a symbol associated to this if we haven't seen the namespace
8262 before. Also, add a using directive if it's an anonymous
8263 namespace. */
8264
8265 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8266 {
8267 struct type *type;
8268
8269 type = read_type_die (die, cu);
8270 new_symbol (die, type, cu);
8271
8272 namespace_name (die, &is_anonymous, cu);
8273 if (is_anonymous)
8274 {
8275 const char *previous_prefix = determine_prefix (die, cu);
8276
8277 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8278 NULL, NULL, &objfile->objfile_obstack);
8279 }
8280 }
8281
8282 if (die->child != NULL)
8283 {
8284 struct die_info *child_die = die->child;
8285
8286 while (child_die && child_die->tag)
8287 {
8288 process_die (child_die, cu);
8289 child_die = sibling_die (child_die);
8290 }
8291 }
8292 }
8293
8294 /* Read a Fortran module as type. This DIE can be only a declaration used for
8295 imported module. Still we need that type as local Fortran "use ... only"
8296 declaration imports depend on the created type in determine_prefix. */
8297
8298 static struct type *
8299 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8300 {
8301 struct objfile *objfile = cu->objfile;
8302 char *module_name;
8303 struct type *type;
8304
8305 module_name = dwarf2_name (die, cu);
8306 if (!module_name)
8307 complaint (&symfile_complaints,
8308 _("DW_TAG_module has no name, offset 0x%x"),
8309 die->offset);
8310 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8311
8312 /* determine_prefix uses TYPE_TAG_NAME. */
8313 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8314
8315 return set_die_type (die, type, cu);
8316 }
8317
8318 /* Read a Fortran module. */
8319
8320 static void
8321 read_module (struct die_info *die, struct dwarf2_cu *cu)
8322 {
8323 struct die_info *child_die = die->child;
8324
8325 while (child_die && child_die->tag)
8326 {
8327 process_die (child_die, cu);
8328 child_die = sibling_die (child_die);
8329 }
8330 }
8331
8332 /* Return the name of the namespace represented by DIE. Set
8333 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8334 namespace. */
8335
8336 static const char *
8337 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8338 {
8339 struct die_info *current_die;
8340 const char *name = NULL;
8341
8342 /* Loop through the extensions until we find a name. */
8343
8344 for (current_die = die;
8345 current_die != NULL;
8346 current_die = dwarf2_extension (die, &cu))
8347 {
8348 name = dwarf2_name (current_die, cu);
8349 if (name != NULL)
8350 break;
8351 }
8352
8353 /* Is it an anonymous namespace? */
8354
8355 *is_anonymous = (name == NULL);
8356 if (*is_anonymous)
8357 name = CP_ANONYMOUS_NAMESPACE_STR;
8358
8359 return name;
8360 }
8361
8362 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8363 the user defined type vector. */
8364
8365 static struct type *
8366 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8367 {
8368 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8369 struct comp_unit_head *cu_header = &cu->header;
8370 struct type *type;
8371 struct attribute *attr_byte_size;
8372 struct attribute *attr_address_class;
8373 int byte_size, addr_class;
8374 struct type *target_type;
8375
8376 target_type = die_type (die, cu);
8377
8378 /* The die_type call above may have already set the type for this DIE. */
8379 type = get_die_type (die, cu);
8380 if (type)
8381 return type;
8382
8383 type = lookup_pointer_type (target_type);
8384
8385 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8386 if (attr_byte_size)
8387 byte_size = DW_UNSND (attr_byte_size);
8388 else
8389 byte_size = cu_header->addr_size;
8390
8391 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8392 if (attr_address_class)
8393 addr_class = DW_UNSND (attr_address_class);
8394 else
8395 addr_class = DW_ADDR_none;
8396
8397 /* If the pointer size or address class is different than the
8398 default, create a type variant marked as such and set the
8399 length accordingly. */
8400 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8401 {
8402 if (gdbarch_address_class_type_flags_p (gdbarch))
8403 {
8404 int type_flags;
8405
8406 type_flags = gdbarch_address_class_type_flags
8407 (gdbarch, byte_size, addr_class);
8408 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8409 == 0);
8410 type = make_type_with_address_space (type, type_flags);
8411 }
8412 else if (TYPE_LENGTH (type) != byte_size)
8413 {
8414 complaint (&symfile_complaints,
8415 _("invalid pointer size %d"), byte_size);
8416 }
8417 else
8418 {
8419 /* Should we also complain about unhandled address classes? */
8420 }
8421 }
8422
8423 TYPE_LENGTH (type) = byte_size;
8424 return set_die_type (die, type, cu);
8425 }
8426
8427 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8428 the user defined type vector. */
8429
8430 static struct type *
8431 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8432 {
8433 struct type *type;
8434 struct type *to_type;
8435 struct type *domain;
8436
8437 to_type = die_type (die, cu);
8438 domain = die_containing_type (die, cu);
8439
8440 /* The calls above may have already set the type for this DIE. */
8441 type = get_die_type (die, cu);
8442 if (type)
8443 return type;
8444
8445 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8446 type = lookup_methodptr_type (to_type);
8447 else
8448 type = lookup_memberptr_type (to_type, domain);
8449
8450 return set_die_type (die, type, cu);
8451 }
8452
8453 /* Extract all information from a DW_TAG_reference_type DIE and add to
8454 the user defined type vector. */
8455
8456 static struct type *
8457 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8458 {
8459 struct comp_unit_head *cu_header = &cu->header;
8460 struct type *type, *target_type;
8461 struct attribute *attr;
8462
8463 target_type = die_type (die, cu);
8464
8465 /* The die_type call above may have already set the type for this DIE. */
8466 type = get_die_type (die, cu);
8467 if (type)
8468 return type;
8469
8470 type = lookup_reference_type (target_type);
8471 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8472 if (attr)
8473 {
8474 TYPE_LENGTH (type) = DW_UNSND (attr);
8475 }
8476 else
8477 {
8478 TYPE_LENGTH (type) = cu_header->addr_size;
8479 }
8480 return set_die_type (die, type, cu);
8481 }
8482
8483 static struct type *
8484 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8485 {
8486 struct type *base_type, *cv_type;
8487
8488 base_type = die_type (die, cu);
8489
8490 /* The die_type call above may have already set the type for this DIE. */
8491 cv_type = get_die_type (die, cu);
8492 if (cv_type)
8493 return cv_type;
8494
8495 /* In case the const qualifier is applied to an array type, the element type
8496 is so qualified, not the array type (section 6.7.3 of C99). */
8497 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8498 {
8499 struct type *el_type, *inner_array;
8500
8501 base_type = copy_type (base_type);
8502 inner_array = base_type;
8503
8504 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8505 {
8506 TYPE_TARGET_TYPE (inner_array) =
8507 copy_type (TYPE_TARGET_TYPE (inner_array));
8508 inner_array = TYPE_TARGET_TYPE (inner_array);
8509 }
8510
8511 el_type = TYPE_TARGET_TYPE (inner_array);
8512 TYPE_TARGET_TYPE (inner_array) =
8513 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8514
8515 return set_die_type (die, base_type, cu);
8516 }
8517
8518 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8519 return set_die_type (die, cv_type, cu);
8520 }
8521
8522 static struct type *
8523 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8524 {
8525 struct type *base_type, *cv_type;
8526
8527 base_type = die_type (die, cu);
8528
8529 /* The die_type call above may have already set the type for this DIE. */
8530 cv_type = get_die_type (die, cu);
8531 if (cv_type)
8532 return cv_type;
8533
8534 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8535 return set_die_type (die, cv_type, cu);
8536 }
8537
8538 /* Extract all information from a DW_TAG_string_type DIE and add to
8539 the user defined type vector. It isn't really a user defined type,
8540 but it behaves like one, with other DIE's using an AT_user_def_type
8541 attribute to reference it. */
8542
8543 static struct type *
8544 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8545 {
8546 struct objfile *objfile = cu->objfile;
8547 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8548 struct type *type, *range_type, *index_type, *char_type;
8549 struct attribute *attr;
8550 unsigned int length;
8551
8552 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8553 if (attr)
8554 {
8555 length = DW_UNSND (attr);
8556 }
8557 else
8558 {
8559 /* Check for the DW_AT_byte_size attribute. */
8560 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8561 if (attr)
8562 {
8563 length = DW_UNSND (attr);
8564 }
8565 else
8566 {
8567 length = 1;
8568 }
8569 }
8570
8571 index_type = objfile_type (objfile)->builtin_int;
8572 range_type = create_range_type (NULL, index_type, 1, length);
8573 char_type = language_string_char_type (cu->language_defn, gdbarch);
8574 type = create_string_type (NULL, char_type, range_type);
8575
8576 return set_die_type (die, type, cu);
8577 }
8578
8579 /* Handle DIES due to C code like:
8580
8581 struct foo
8582 {
8583 int (*funcp)(int a, long l);
8584 int b;
8585 };
8586
8587 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8588
8589 static struct type *
8590 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8591 {
8592 struct objfile *objfile = cu->objfile;
8593 struct type *type; /* Type that this function returns. */
8594 struct type *ftype; /* Function that returns above type. */
8595 struct attribute *attr;
8596
8597 type = die_type (die, cu);
8598
8599 /* The die_type call above may have already set the type for this DIE. */
8600 ftype = get_die_type (die, cu);
8601 if (ftype)
8602 return ftype;
8603
8604 ftype = lookup_function_type (type);
8605
8606 /* All functions in C++, Pascal and Java have prototypes. */
8607 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8608 if ((attr && (DW_UNSND (attr) != 0))
8609 || cu->language == language_cplus
8610 || cu->language == language_java
8611 || cu->language == language_pascal)
8612 TYPE_PROTOTYPED (ftype) = 1;
8613 else if (producer_is_realview (cu->producer))
8614 /* RealView does not emit DW_AT_prototyped. We can not
8615 distinguish prototyped and unprototyped functions; default to
8616 prototyped, since that is more common in modern code (and
8617 RealView warns about unprototyped functions). */
8618 TYPE_PROTOTYPED (ftype) = 1;
8619
8620 /* Store the calling convention in the type if it's available in
8621 the subroutine die. Otherwise set the calling convention to
8622 the default value DW_CC_normal. */
8623 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8624 if (attr)
8625 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8626 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8627 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8628 else
8629 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8630
8631 /* We need to add the subroutine type to the die immediately so
8632 we don't infinitely recurse when dealing with parameters
8633 declared as the same subroutine type. */
8634 set_die_type (die, ftype, cu);
8635
8636 if (die->child != NULL)
8637 {
8638 struct type *void_type = objfile_type (objfile)->builtin_void;
8639 struct die_info *child_die;
8640 int nparams, iparams;
8641
8642 /* Count the number of parameters.
8643 FIXME: GDB currently ignores vararg functions, but knows about
8644 vararg member functions. */
8645 nparams = 0;
8646 child_die = die->child;
8647 while (child_die && child_die->tag)
8648 {
8649 if (child_die->tag == DW_TAG_formal_parameter)
8650 nparams++;
8651 else if (child_die->tag == DW_TAG_unspecified_parameters)
8652 TYPE_VARARGS (ftype) = 1;
8653 child_die = sibling_die (child_die);
8654 }
8655
8656 /* Allocate storage for parameters and fill them in. */
8657 TYPE_NFIELDS (ftype) = nparams;
8658 TYPE_FIELDS (ftype) = (struct field *)
8659 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8660
8661 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8662 even if we error out during the parameters reading below. */
8663 for (iparams = 0; iparams < nparams; iparams++)
8664 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8665
8666 iparams = 0;
8667 child_die = die->child;
8668 while (child_die && child_die->tag)
8669 {
8670 if (child_die->tag == DW_TAG_formal_parameter)
8671 {
8672 struct type *arg_type;
8673
8674 /* DWARF version 2 has no clean way to discern C++
8675 static and non-static member functions. G++ helps
8676 GDB by marking the first parameter for non-static
8677 member functions (which is the this pointer) as
8678 artificial. We pass this information to
8679 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8680
8681 DWARF version 3 added DW_AT_object_pointer, which GCC
8682 4.5 does not yet generate. */
8683 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8684 if (attr)
8685 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8686 else
8687 {
8688 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8689
8690 /* GCC/43521: In java, the formal parameter
8691 "this" is sometimes not marked with DW_AT_artificial. */
8692 if (cu->language == language_java)
8693 {
8694 const char *name = dwarf2_name (child_die, cu);
8695
8696 if (name && !strcmp (name, "this"))
8697 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8698 }
8699 }
8700 arg_type = die_type (child_die, cu);
8701
8702 /* RealView does not mark THIS as const, which the testsuite
8703 expects. GCC marks THIS as const in method definitions,
8704 but not in the class specifications (GCC PR 43053). */
8705 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8706 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8707 {
8708 int is_this = 0;
8709 struct dwarf2_cu *arg_cu = cu;
8710 const char *name = dwarf2_name (child_die, cu);
8711
8712 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8713 if (attr)
8714 {
8715 /* If the compiler emits this, use it. */
8716 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8717 is_this = 1;
8718 }
8719 else if (name && strcmp (name, "this") == 0)
8720 /* Function definitions will have the argument names. */
8721 is_this = 1;
8722 else if (name == NULL && iparams == 0)
8723 /* Declarations may not have the names, so like
8724 elsewhere in GDB, assume an artificial first
8725 argument is "this". */
8726 is_this = 1;
8727
8728 if (is_this)
8729 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8730 arg_type, 0);
8731 }
8732
8733 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8734 iparams++;
8735 }
8736 child_die = sibling_die (child_die);
8737 }
8738 }
8739
8740 return ftype;
8741 }
8742
8743 static struct type *
8744 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8745 {
8746 struct objfile *objfile = cu->objfile;
8747 const char *name = NULL;
8748 struct type *this_type, *target_type;
8749
8750 name = dwarf2_full_name (NULL, die, cu);
8751 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8752 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8753 TYPE_NAME (this_type) = (char *) name;
8754 set_die_type (die, this_type, cu);
8755 target_type = die_type (die, cu);
8756 if (target_type != this_type)
8757 TYPE_TARGET_TYPE (this_type) = target_type;
8758 else
8759 {
8760 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8761 spec and cause infinite loops in GDB. */
8762 complaint (&symfile_complaints,
8763 _("Self-referential DW_TAG_typedef "
8764 "- DIE at 0x%x [in module %s]"),
8765 die->offset, objfile->name);
8766 TYPE_TARGET_TYPE (this_type) = NULL;
8767 }
8768 return this_type;
8769 }
8770
8771 /* Find a representation of a given base type and install
8772 it in the TYPE field of the die. */
8773
8774 static struct type *
8775 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8776 {
8777 struct objfile *objfile = cu->objfile;
8778 struct type *type;
8779 struct attribute *attr;
8780 int encoding = 0, size = 0;
8781 char *name;
8782 enum type_code code = TYPE_CODE_INT;
8783 int type_flags = 0;
8784 struct type *target_type = NULL;
8785
8786 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8787 if (attr)
8788 {
8789 encoding = DW_UNSND (attr);
8790 }
8791 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8792 if (attr)
8793 {
8794 size = DW_UNSND (attr);
8795 }
8796 name = dwarf2_name (die, cu);
8797 if (!name)
8798 {
8799 complaint (&symfile_complaints,
8800 _("DW_AT_name missing from DW_TAG_base_type"));
8801 }
8802
8803 switch (encoding)
8804 {
8805 case DW_ATE_address:
8806 /* Turn DW_ATE_address into a void * pointer. */
8807 code = TYPE_CODE_PTR;
8808 type_flags |= TYPE_FLAG_UNSIGNED;
8809 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8810 break;
8811 case DW_ATE_boolean:
8812 code = TYPE_CODE_BOOL;
8813 type_flags |= TYPE_FLAG_UNSIGNED;
8814 break;
8815 case DW_ATE_complex_float:
8816 code = TYPE_CODE_COMPLEX;
8817 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8818 break;
8819 case DW_ATE_decimal_float:
8820 code = TYPE_CODE_DECFLOAT;
8821 break;
8822 case DW_ATE_float:
8823 code = TYPE_CODE_FLT;
8824 break;
8825 case DW_ATE_signed:
8826 break;
8827 case DW_ATE_unsigned:
8828 type_flags |= TYPE_FLAG_UNSIGNED;
8829 if (cu->language == language_fortran
8830 && name
8831 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8832 code = TYPE_CODE_CHAR;
8833 break;
8834 case DW_ATE_signed_char:
8835 if (cu->language == language_ada || cu->language == language_m2
8836 || cu->language == language_pascal
8837 || cu->language == language_fortran)
8838 code = TYPE_CODE_CHAR;
8839 break;
8840 case DW_ATE_unsigned_char:
8841 if (cu->language == language_ada || cu->language == language_m2
8842 || cu->language == language_pascal
8843 || cu->language == language_fortran)
8844 code = TYPE_CODE_CHAR;
8845 type_flags |= TYPE_FLAG_UNSIGNED;
8846 break;
8847 case DW_ATE_UTF:
8848 /* We just treat this as an integer and then recognize the
8849 type by name elsewhere. */
8850 break;
8851
8852 default:
8853 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8854 dwarf_type_encoding_name (encoding));
8855 break;
8856 }
8857
8858 type = init_type (code, size, type_flags, NULL, objfile);
8859 TYPE_NAME (type) = name;
8860 TYPE_TARGET_TYPE (type) = target_type;
8861
8862 if (name && strcmp (name, "char") == 0)
8863 TYPE_NOSIGN (type) = 1;
8864
8865 return set_die_type (die, type, cu);
8866 }
8867
8868 /* Read the given DW_AT_subrange DIE. */
8869
8870 static struct type *
8871 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8872 {
8873 struct type *base_type;
8874 struct type *range_type;
8875 struct attribute *attr;
8876 LONGEST low = 0;
8877 LONGEST high = -1;
8878 char *name;
8879 LONGEST negative_mask;
8880
8881 base_type = die_type (die, cu);
8882 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8883 check_typedef (base_type);
8884
8885 /* The die_type call above may have already set the type for this DIE. */
8886 range_type = get_die_type (die, cu);
8887 if (range_type)
8888 return range_type;
8889
8890 if (cu->language == language_fortran)
8891 {
8892 /* FORTRAN implies a lower bound of 1, if not given. */
8893 low = 1;
8894 }
8895
8896 /* FIXME: For variable sized arrays either of these could be
8897 a variable rather than a constant value. We'll allow it,
8898 but we don't know how to handle it. */
8899 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8900 if (attr)
8901 low = dwarf2_get_attr_constant_value (attr, 0);
8902
8903 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8904 if (attr)
8905 {
8906 if (attr_form_is_block (attr) || is_ref_attr (attr))
8907 {
8908 /* GCC encodes arrays with unspecified or dynamic length
8909 with a DW_FORM_block1 attribute or a reference attribute.
8910 FIXME: GDB does not yet know how to handle dynamic
8911 arrays properly, treat them as arrays with unspecified
8912 length for now.
8913
8914 FIXME: jimb/2003-09-22: GDB does not really know
8915 how to handle arrays of unspecified length
8916 either; we just represent them as zero-length
8917 arrays. Choose an appropriate upper bound given
8918 the lower bound we've computed above. */
8919 high = low - 1;
8920 }
8921 else
8922 high = dwarf2_get_attr_constant_value (attr, 1);
8923 }
8924 else
8925 {
8926 attr = dwarf2_attr (die, DW_AT_count, cu);
8927 if (attr)
8928 {
8929 int count = dwarf2_get_attr_constant_value (attr, 1);
8930 high = low + count - 1;
8931 }
8932 else
8933 {
8934 /* Unspecified array length. */
8935 high = low - 1;
8936 }
8937 }
8938
8939 /* Dwarf-2 specifications explicitly allows to create subrange types
8940 without specifying a base type.
8941 In that case, the base type must be set to the type of
8942 the lower bound, upper bound or count, in that order, if any of these
8943 three attributes references an object that has a type.
8944 If no base type is found, the Dwarf-2 specifications say that
8945 a signed integer type of size equal to the size of an address should
8946 be used.
8947 For the following C code: `extern char gdb_int [];'
8948 GCC produces an empty range DIE.
8949 FIXME: muller/2010-05-28: Possible references to object for low bound,
8950 high bound or count are not yet handled by this code. */
8951 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8952 {
8953 struct objfile *objfile = cu->objfile;
8954 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8955 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8956 struct type *int_type = objfile_type (objfile)->builtin_int;
8957
8958 /* Test "int", "long int", and "long long int" objfile types,
8959 and select the first one having a size above or equal to the
8960 architecture address size. */
8961 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8962 base_type = int_type;
8963 else
8964 {
8965 int_type = objfile_type (objfile)->builtin_long;
8966 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8967 base_type = int_type;
8968 else
8969 {
8970 int_type = objfile_type (objfile)->builtin_long_long;
8971 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8972 base_type = int_type;
8973 }
8974 }
8975 }
8976
8977 negative_mask =
8978 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8979 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8980 low |= negative_mask;
8981 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8982 high |= negative_mask;
8983
8984 range_type = create_range_type (NULL, base_type, low, high);
8985
8986 /* Mark arrays with dynamic length at least as an array of unspecified
8987 length. GDB could check the boundary but before it gets implemented at
8988 least allow accessing the array elements. */
8989 if (attr && attr_form_is_block (attr))
8990 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8991
8992 /* Ada expects an empty array on no boundary attributes. */
8993 if (attr == NULL && cu->language != language_ada)
8994 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8995
8996 name = dwarf2_name (die, cu);
8997 if (name)
8998 TYPE_NAME (range_type) = name;
8999
9000 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9001 if (attr)
9002 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9003
9004 set_die_type (die, range_type, cu);
9005
9006 /* set_die_type should be already done. */
9007 set_descriptive_type (range_type, die, cu);
9008
9009 return range_type;
9010 }
9011
9012 static struct type *
9013 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9014 {
9015 struct type *type;
9016
9017 /* For now, we only support the C meaning of an unspecified type: void. */
9018
9019 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9020 TYPE_NAME (type) = dwarf2_name (die, cu);
9021
9022 return set_die_type (die, type, cu);
9023 }
9024
9025 /* Trivial hash function for die_info: the hash value of a DIE
9026 is its offset in .debug_info for this objfile. */
9027
9028 static hashval_t
9029 die_hash (const void *item)
9030 {
9031 const struct die_info *die = item;
9032
9033 return die->offset;
9034 }
9035
9036 /* Trivial comparison function for die_info structures: two DIEs
9037 are equal if they have the same offset. */
9038
9039 static int
9040 die_eq (const void *item_lhs, const void *item_rhs)
9041 {
9042 const struct die_info *die_lhs = item_lhs;
9043 const struct die_info *die_rhs = item_rhs;
9044
9045 return die_lhs->offset == die_rhs->offset;
9046 }
9047
9048 /* Read a whole compilation unit into a linked list of dies. */
9049
9050 static struct die_info *
9051 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9052 {
9053 struct die_reader_specs reader_specs;
9054 int read_abbrevs = 0;
9055 struct cleanup *back_to = NULL;
9056 struct die_info *die;
9057
9058 if (cu->dwarf2_abbrevs == NULL)
9059 {
9060 dwarf2_read_abbrevs (cu);
9061 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9062 read_abbrevs = 1;
9063 }
9064
9065 gdb_assert (cu->die_hash == NULL);
9066 cu->die_hash
9067 = htab_create_alloc_ex (cu->header.length / 12,
9068 die_hash,
9069 die_eq,
9070 NULL,
9071 &cu->comp_unit_obstack,
9072 hashtab_obstack_allocate,
9073 dummy_obstack_deallocate);
9074
9075 init_cu_die_reader (&reader_specs, cu);
9076
9077 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9078
9079 if (read_abbrevs)
9080 do_cleanups (back_to);
9081
9082 return die;
9083 }
9084
9085 /* Main entry point for reading a DIE and all children.
9086 Read the DIE and dump it if requested. */
9087
9088 static struct die_info *
9089 read_die_and_children (const struct die_reader_specs *reader,
9090 gdb_byte *info_ptr,
9091 gdb_byte **new_info_ptr,
9092 struct die_info *parent)
9093 {
9094 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9095 new_info_ptr, parent);
9096
9097 if (dwarf2_die_debug)
9098 {
9099 fprintf_unfiltered (gdb_stdlog,
9100 "\nRead die from %s of %s:\n",
9101 (reader->cu->per_cu->debug_types_section
9102 ? ".debug_types"
9103 : ".debug_info"),
9104 reader->abfd->filename);
9105 dump_die (result, dwarf2_die_debug);
9106 }
9107
9108 return result;
9109 }
9110
9111 /* Read a single die and all its descendents. Set the die's sibling
9112 field to NULL; set other fields in the die correctly, and set all
9113 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9114 location of the info_ptr after reading all of those dies. PARENT
9115 is the parent of the die in question. */
9116
9117 static struct die_info *
9118 read_die_and_children_1 (const struct die_reader_specs *reader,
9119 gdb_byte *info_ptr,
9120 gdb_byte **new_info_ptr,
9121 struct die_info *parent)
9122 {
9123 struct die_info *die;
9124 gdb_byte *cur_ptr;
9125 int has_children;
9126
9127 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9128 if (die == NULL)
9129 {
9130 *new_info_ptr = cur_ptr;
9131 return NULL;
9132 }
9133 store_in_ref_table (die, reader->cu);
9134
9135 if (has_children)
9136 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9137 else
9138 {
9139 die->child = NULL;
9140 *new_info_ptr = cur_ptr;
9141 }
9142
9143 die->sibling = NULL;
9144 die->parent = parent;
9145 return die;
9146 }
9147
9148 /* Read a die, all of its descendents, and all of its siblings; set
9149 all of the fields of all of the dies correctly. Arguments are as
9150 in read_die_and_children. */
9151
9152 static struct die_info *
9153 read_die_and_siblings (const struct die_reader_specs *reader,
9154 gdb_byte *info_ptr,
9155 gdb_byte **new_info_ptr,
9156 struct die_info *parent)
9157 {
9158 struct die_info *first_die, *last_sibling;
9159 gdb_byte *cur_ptr;
9160
9161 cur_ptr = info_ptr;
9162 first_die = last_sibling = NULL;
9163
9164 while (1)
9165 {
9166 struct die_info *die
9167 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9168
9169 if (die == NULL)
9170 {
9171 *new_info_ptr = cur_ptr;
9172 return first_die;
9173 }
9174
9175 if (!first_die)
9176 first_die = die;
9177 else
9178 last_sibling->sibling = die;
9179
9180 last_sibling = die;
9181 }
9182 }
9183
9184 /* Read the die from the .debug_info section buffer. Set DIEP to
9185 point to a newly allocated die with its information, except for its
9186 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9187 whether the die has children or not. */
9188
9189 static gdb_byte *
9190 read_full_die (const struct die_reader_specs *reader,
9191 struct die_info **diep, gdb_byte *info_ptr,
9192 int *has_children)
9193 {
9194 unsigned int abbrev_number, bytes_read, i, offset;
9195 struct abbrev_info *abbrev;
9196 struct die_info *die;
9197 struct dwarf2_cu *cu = reader->cu;
9198 bfd *abfd = reader->abfd;
9199
9200 offset = info_ptr - reader->buffer;
9201 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9202 info_ptr += bytes_read;
9203 if (!abbrev_number)
9204 {
9205 *diep = NULL;
9206 *has_children = 0;
9207 return info_ptr;
9208 }
9209
9210 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9211 if (!abbrev)
9212 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9213 abbrev_number,
9214 bfd_get_filename (abfd));
9215
9216 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9217 die->offset = offset;
9218 die->tag = abbrev->tag;
9219 die->abbrev = abbrev_number;
9220
9221 die->num_attrs = abbrev->num_attrs;
9222
9223 for (i = 0; i < abbrev->num_attrs; ++i)
9224 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9225 abfd, info_ptr, cu);
9226
9227 *diep = die;
9228 *has_children = abbrev->has_children;
9229 return info_ptr;
9230 }
9231
9232 /* In DWARF version 2, the description of the debugging information is
9233 stored in a separate .debug_abbrev section. Before we read any
9234 dies from a section we read in all abbreviations and install them
9235 in a hash table. This function also sets flags in CU describing
9236 the data found in the abbrev table. */
9237
9238 static void
9239 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9240 {
9241 bfd *abfd = cu->objfile->obfd;
9242 struct comp_unit_head *cu_header = &cu->header;
9243 gdb_byte *abbrev_ptr;
9244 struct abbrev_info *cur_abbrev;
9245 unsigned int abbrev_number, bytes_read, abbrev_name;
9246 unsigned int abbrev_form, hash_number;
9247 struct attr_abbrev *cur_attrs;
9248 unsigned int allocated_attrs;
9249
9250 /* Initialize dwarf2 abbrevs. */
9251 obstack_init (&cu->abbrev_obstack);
9252 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9253 (ABBREV_HASH_SIZE
9254 * sizeof (struct abbrev_info *)));
9255 memset (cu->dwarf2_abbrevs, 0,
9256 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9257
9258 dwarf2_read_section (dwarf2_per_objfile->objfile,
9259 &dwarf2_per_objfile->abbrev);
9260 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9261 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9262 abbrev_ptr += bytes_read;
9263
9264 allocated_attrs = ATTR_ALLOC_CHUNK;
9265 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9266
9267 /* Loop until we reach an abbrev number of 0. */
9268 while (abbrev_number)
9269 {
9270 cur_abbrev = dwarf_alloc_abbrev (cu);
9271
9272 /* read in abbrev header */
9273 cur_abbrev->number = abbrev_number;
9274 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9275 abbrev_ptr += bytes_read;
9276 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9277 abbrev_ptr += 1;
9278
9279 if (cur_abbrev->tag == DW_TAG_namespace)
9280 cu->has_namespace_info = 1;
9281
9282 /* now read in declarations */
9283 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9284 abbrev_ptr += bytes_read;
9285 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9286 abbrev_ptr += bytes_read;
9287 while (abbrev_name)
9288 {
9289 if (cur_abbrev->num_attrs == allocated_attrs)
9290 {
9291 allocated_attrs += ATTR_ALLOC_CHUNK;
9292 cur_attrs
9293 = xrealloc (cur_attrs, (allocated_attrs
9294 * sizeof (struct attr_abbrev)));
9295 }
9296
9297 /* Record whether this compilation unit might have
9298 inter-compilation-unit references. If we don't know what form
9299 this attribute will have, then it might potentially be a
9300 DW_FORM_ref_addr, so we conservatively expect inter-CU
9301 references. */
9302
9303 if (abbrev_form == DW_FORM_ref_addr
9304 || abbrev_form == DW_FORM_indirect)
9305 cu->has_form_ref_addr = 1;
9306
9307 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9308 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9309 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9310 abbrev_ptr += bytes_read;
9311 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9312 abbrev_ptr += bytes_read;
9313 }
9314
9315 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9316 (cur_abbrev->num_attrs
9317 * sizeof (struct attr_abbrev)));
9318 memcpy (cur_abbrev->attrs, cur_attrs,
9319 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9320
9321 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9322 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9323 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9324
9325 /* Get next abbreviation.
9326 Under Irix6 the abbreviations for a compilation unit are not
9327 always properly terminated with an abbrev number of 0.
9328 Exit loop if we encounter an abbreviation which we have
9329 already read (which means we are about to read the abbreviations
9330 for the next compile unit) or if the end of the abbreviation
9331 table is reached. */
9332 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9333 >= dwarf2_per_objfile->abbrev.size)
9334 break;
9335 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9336 abbrev_ptr += bytes_read;
9337 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9338 break;
9339 }
9340
9341 xfree (cur_attrs);
9342 }
9343
9344 /* Release the memory used by the abbrev table for a compilation unit. */
9345
9346 static void
9347 dwarf2_free_abbrev_table (void *ptr_to_cu)
9348 {
9349 struct dwarf2_cu *cu = ptr_to_cu;
9350
9351 obstack_free (&cu->abbrev_obstack, NULL);
9352 cu->dwarf2_abbrevs = NULL;
9353 }
9354
9355 /* Lookup an abbrev_info structure in the abbrev hash table. */
9356
9357 static struct abbrev_info *
9358 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9359 {
9360 unsigned int hash_number;
9361 struct abbrev_info *abbrev;
9362
9363 hash_number = number % ABBREV_HASH_SIZE;
9364 abbrev = cu->dwarf2_abbrevs[hash_number];
9365
9366 while (abbrev)
9367 {
9368 if (abbrev->number == number)
9369 return abbrev;
9370 else
9371 abbrev = abbrev->next;
9372 }
9373 return NULL;
9374 }
9375
9376 /* Returns nonzero if TAG represents a type that we might generate a partial
9377 symbol for. */
9378
9379 static int
9380 is_type_tag_for_partial (int tag)
9381 {
9382 switch (tag)
9383 {
9384 #if 0
9385 /* Some types that would be reasonable to generate partial symbols for,
9386 that we don't at present. */
9387 case DW_TAG_array_type:
9388 case DW_TAG_file_type:
9389 case DW_TAG_ptr_to_member_type:
9390 case DW_TAG_set_type:
9391 case DW_TAG_string_type:
9392 case DW_TAG_subroutine_type:
9393 #endif
9394 case DW_TAG_base_type:
9395 case DW_TAG_class_type:
9396 case DW_TAG_interface_type:
9397 case DW_TAG_enumeration_type:
9398 case DW_TAG_structure_type:
9399 case DW_TAG_subrange_type:
9400 case DW_TAG_typedef:
9401 case DW_TAG_union_type:
9402 return 1;
9403 default:
9404 return 0;
9405 }
9406 }
9407
9408 /* Load all DIEs that are interesting for partial symbols into memory. */
9409
9410 static struct partial_die_info *
9411 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9412 int building_psymtab, struct dwarf2_cu *cu)
9413 {
9414 struct objfile *objfile = cu->objfile;
9415 struct partial_die_info *part_die;
9416 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9417 struct abbrev_info *abbrev;
9418 unsigned int bytes_read;
9419 unsigned int load_all = 0;
9420
9421 int nesting_level = 1;
9422
9423 parent_die = NULL;
9424 last_die = NULL;
9425
9426 if (cu->per_cu && cu->per_cu->load_all_dies)
9427 load_all = 1;
9428
9429 cu->partial_dies
9430 = htab_create_alloc_ex (cu->header.length / 12,
9431 partial_die_hash,
9432 partial_die_eq,
9433 NULL,
9434 &cu->comp_unit_obstack,
9435 hashtab_obstack_allocate,
9436 dummy_obstack_deallocate);
9437
9438 part_die = obstack_alloc (&cu->comp_unit_obstack,
9439 sizeof (struct partial_die_info));
9440
9441 while (1)
9442 {
9443 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9444
9445 /* A NULL abbrev means the end of a series of children. */
9446 if (abbrev == NULL)
9447 {
9448 if (--nesting_level == 0)
9449 {
9450 /* PART_DIE was probably the last thing allocated on the
9451 comp_unit_obstack, so we could call obstack_free
9452 here. We don't do that because the waste is small,
9453 and will be cleaned up when we're done with this
9454 compilation unit. This way, we're also more robust
9455 against other users of the comp_unit_obstack. */
9456 return first_die;
9457 }
9458 info_ptr += bytes_read;
9459 last_die = parent_die;
9460 parent_die = parent_die->die_parent;
9461 continue;
9462 }
9463
9464 /* Check for template arguments. We never save these; if
9465 they're seen, we just mark the parent, and go on our way. */
9466 if (parent_die != NULL
9467 && cu->language == language_cplus
9468 && (abbrev->tag == DW_TAG_template_type_param
9469 || abbrev->tag == DW_TAG_template_value_param))
9470 {
9471 parent_die->has_template_arguments = 1;
9472
9473 if (!load_all)
9474 {
9475 /* We don't need a partial DIE for the template argument. */
9476 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9477 cu);
9478 continue;
9479 }
9480 }
9481
9482 /* We only recurse into subprograms looking for template arguments.
9483 Skip their other children. */
9484 if (!load_all
9485 && cu->language == language_cplus
9486 && parent_die != NULL
9487 && parent_die->tag == DW_TAG_subprogram)
9488 {
9489 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9490 continue;
9491 }
9492
9493 /* Check whether this DIE is interesting enough to save. Normally
9494 we would not be interested in members here, but there may be
9495 later variables referencing them via DW_AT_specification (for
9496 static members). */
9497 if (!load_all
9498 && !is_type_tag_for_partial (abbrev->tag)
9499 && abbrev->tag != DW_TAG_constant
9500 && abbrev->tag != DW_TAG_enumerator
9501 && abbrev->tag != DW_TAG_subprogram
9502 && abbrev->tag != DW_TAG_lexical_block
9503 && abbrev->tag != DW_TAG_variable
9504 && abbrev->tag != DW_TAG_namespace
9505 && abbrev->tag != DW_TAG_module
9506 && abbrev->tag != DW_TAG_member)
9507 {
9508 /* Otherwise we skip to the next sibling, if any. */
9509 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9510 continue;
9511 }
9512
9513 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9514 buffer, info_ptr, cu);
9515
9516 /* This two-pass algorithm for processing partial symbols has a
9517 high cost in cache pressure. Thus, handle some simple cases
9518 here which cover the majority of C partial symbols. DIEs
9519 which neither have specification tags in them, nor could have
9520 specification tags elsewhere pointing at them, can simply be
9521 processed and discarded.
9522
9523 This segment is also optional; scan_partial_symbols and
9524 add_partial_symbol will handle these DIEs if we chain
9525 them in normally. When compilers which do not emit large
9526 quantities of duplicate debug information are more common,
9527 this code can probably be removed. */
9528
9529 /* Any complete simple types at the top level (pretty much all
9530 of them, for a language without namespaces), can be processed
9531 directly. */
9532 if (parent_die == NULL
9533 && part_die->has_specification == 0
9534 && part_die->is_declaration == 0
9535 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9536 || part_die->tag == DW_TAG_base_type
9537 || part_die->tag == DW_TAG_subrange_type))
9538 {
9539 if (building_psymtab && part_die->name != NULL)
9540 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9541 VAR_DOMAIN, LOC_TYPEDEF,
9542 &objfile->static_psymbols,
9543 0, (CORE_ADDR) 0, cu->language, objfile);
9544 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9545 continue;
9546 }
9547
9548 /* The exception for DW_TAG_typedef with has_children above is
9549 a workaround of GCC PR debug/47510. In the case of this complaint
9550 type_name_no_tag_or_error will error on such types later.
9551
9552 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9553 it could not find the child DIEs referenced later, this is checked
9554 above. In correct DWARF DW_TAG_typedef should have no children. */
9555
9556 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9557 complaint (&symfile_complaints,
9558 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9559 "- DIE at 0x%x [in module %s]"),
9560 part_die->offset, objfile->name);
9561
9562 /* If we're at the second level, and we're an enumerator, and
9563 our parent has no specification (meaning possibly lives in a
9564 namespace elsewhere), then we can add the partial symbol now
9565 instead of queueing it. */
9566 if (part_die->tag == DW_TAG_enumerator
9567 && parent_die != NULL
9568 && parent_die->die_parent == NULL
9569 && parent_die->tag == DW_TAG_enumeration_type
9570 && parent_die->has_specification == 0)
9571 {
9572 if (part_die->name == NULL)
9573 complaint (&symfile_complaints,
9574 _("malformed enumerator DIE ignored"));
9575 else if (building_psymtab)
9576 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9577 VAR_DOMAIN, LOC_CONST,
9578 (cu->language == language_cplus
9579 || cu->language == language_java)
9580 ? &objfile->global_psymbols
9581 : &objfile->static_psymbols,
9582 0, (CORE_ADDR) 0, cu->language, objfile);
9583
9584 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9585 continue;
9586 }
9587
9588 /* We'll save this DIE so link it in. */
9589 part_die->die_parent = parent_die;
9590 part_die->die_sibling = NULL;
9591 part_die->die_child = NULL;
9592
9593 if (last_die && last_die == parent_die)
9594 last_die->die_child = part_die;
9595 else if (last_die)
9596 last_die->die_sibling = part_die;
9597
9598 last_die = part_die;
9599
9600 if (first_die == NULL)
9601 first_die = part_die;
9602
9603 /* Maybe add the DIE to the hash table. Not all DIEs that we
9604 find interesting need to be in the hash table, because we
9605 also have the parent/sibling/child chains; only those that we
9606 might refer to by offset later during partial symbol reading.
9607
9608 For now this means things that might have be the target of a
9609 DW_AT_specification, DW_AT_abstract_origin, or
9610 DW_AT_extension. DW_AT_extension will refer only to
9611 namespaces; DW_AT_abstract_origin refers to functions (and
9612 many things under the function DIE, but we do not recurse
9613 into function DIEs during partial symbol reading) and
9614 possibly variables as well; DW_AT_specification refers to
9615 declarations. Declarations ought to have the DW_AT_declaration
9616 flag. It happens that GCC forgets to put it in sometimes, but
9617 only for functions, not for types.
9618
9619 Adding more things than necessary to the hash table is harmless
9620 except for the performance cost. Adding too few will result in
9621 wasted time in find_partial_die, when we reread the compilation
9622 unit with load_all_dies set. */
9623
9624 if (load_all
9625 || abbrev->tag == DW_TAG_constant
9626 || abbrev->tag == DW_TAG_subprogram
9627 || abbrev->tag == DW_TAG_variable
9628 || abbrev->tag == DW_TAG_namespace
9629 || part_die->is_declaration)
9630 {
9631 void **slot;
9632
9633 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9634 part_die->offset, INSERT);
9635 *slot = part_die;
9636 }
9637
9638 part_die = obstack_alloc (&cu->comp_unit_obstack,
9639 sizeof (struct partial_die_info));
9640
9641 /* For some DIEs we want to follow their children (if any). For C
9642 we have no reason to follow the children of structures; for other
9643 languages we have to, so that we can get at method physnames
9644 to infer fully qualified class names, for DW_AT_specification,
9645 and for C++ template arguments. For C++, we also look one level
9646 inside functions to find template arguments (if the name of the
9647 function does not already contain the template arguments).
9648
9649 For Ada, we need to scan the children of subprograms and lexical
9650 blocks as well because Ada allows the definition of nested
9651 entities that could be interesting for the debugger, such as
9652 nested subprograms for instance. */
9653 if (last_die->has_children
9654 && (load_all
9655 || last_die->tag == DW_TAG_namespace
9656 || last_die->tag == DW_TAG_module
9657 || last_die->tag == DW_TAG_enumeration_type
9658 || (cu->language == language_cplus
9659 && last_die->tag == DW_TAG_subprogram
9660 && (last_die->name == NULL
9661 || strchr (last_die->name, '<') == NULL))
9662 || (cu->language != language_c
9663 && (last_die->tag == DW_TAG_class_type
9664 || last_die->tag == DW_TAG_interface_type
9665 || last_die->tag == DW_TAG_structure_type
9666 || last_die->tag == DW_TAG_union_type))
9667 || (cu->language == language_ada
9668 && (last_die->tag == DW_TAG_subprogram
9669 || last_die->tag == DW_TAG_lexical_block))))
9670 {
9671 nesting_level++;
9672 parent_die = last_die;
9673 continue;
9674 }
9675
9676 /* Otherwise we skip to the next sibling, if any. */
9677 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9678
9679 /* Back to the top, do it again. */
9680 }
9681 }
9682
9683 /* Read a minimal amount of information into the minimal die structure. */
9684
9685 static gdb_byte *
9686 read_partial_die (struct partial_die_info *part_die,
9687 struct abbrev_info *abbrev,
9688 unsigned int abbrev_len, bfd *abfd,
9689 gdb_byte *buffer, gdb_byte *info_ptr,
9690 struct dwarf2_cu *cu)
9691 {
9692 struct objfile *objfile = cu->objfile;
9693 unsigned int i;
9694 struct attribute attr;
9695 int has_low_pc_attr = 0;
9696 int has_high_pc_attr = 0;
9697
9698 memset (part_die, 0, sizeof (struct partial_die_info));
9699
9700 part_die->offset = info_ptr - buffer;
9701
9702 info_ptr += abbrev_len;
9703
9704 if (abbrev == NULL)
9705 return info_ptr;
9706
9707 part_die->tag = abbrev->tag;
9708 part_die->has_children = abbrev->has_children;
9709
9710 for (i = 0; i < abbrev->num_attrs; ++i)
9711 {
9712 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9713
9714 /* Store the data if it is of an attribute we want to keep in a
9715 partial symbol table. */
9716 switch (attr.name)
9717 {
9718 case DW_AT_name:
9719 switch (part_die->tag)
9720 {
9721 case DW_TAG_compile_unit:
9722 case DW_TAG_type_unit:
9723 /* Compilation units have a DW_AT_name that is a filename, not
9724 a source language identifier. */
9725 case DW_TAG_enumeration_type:
9726 case DW_TAG_enumerator:
9727 /* These tags always have simple identifiers already; no need
9728 to canonicalize them. */
9729 part_die->name = DW_STRING (&attr);
9730 break;
9731 default:
9732 part_die->name
9733 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9734 &objfile->objfile_obstack);
9735 break;
9736 }
9737 break;
9738 case DW_AT_linkage_name:
9739 case DW_AT_MIPS_linkage_name:
9740 /* Note that both forms of linkage name might appear. We
9741 assume they will be the same, and we only store the last
9742 one we see. */
9743 if (cu->language == language_ada)
9744 part_die->name = DW_STRING (&attr);
9745 part_die->linkage_name = DW_STRING (&attr);
9746 break;
9747 case DW_AT_low_pc:
9748 has_low_pc_attr = 1;
9749 part_die->lowpc = DW_ADDR (&attr);
9750 break;
9751 case DW_AT_high_pc:
9752 has_high_pc_attr = 1;
9753 part_die->highpc = DW_ADDR (&attr);
9754 break;
9755 case DW_AT_location:
9756 /* Support the .debug_loc offsets. */
9757 if (attr_form_is_block (&attr))
9758 {
9759 part_die->locdesc = DW_BLOCK (&attr);
9760 }
9761 else if (attr_form_is_section_offset (&attr))
9762 {
9763 dwarf2_complex_location_expr_complaint ();
9764 }
9765 else
9766 {
9767 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9768 "partial symbol information");
9769 }
9770 break;
9771 case DW_AT_external:
9772 part_die->is_external = DW_UNSND (&attr);
9773 break;
9774 case DW_AT_declaration:
9775 part_die->is_declaration = DW_UNSND (&attr);
9776 break;
9777 case DW_AT_type:
9778 part_die->has_type = 1;
9779 break;
9780 case DW_AT_abstract_origin:
9781 case DW_AT_specification:
9782 case DW_AT_extension:
9783 part_die->has_specification = 1;
9784 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9785 break;
9786 case DW_AT_sibling:
9787 /* Ignore absolute siblings, they might point outside of
9788 the current compile unit. */
9789 if (attr.form == DW_FORM_ref_addr)
9790 complaint (&symfile_complaints,
9791 _("ignoring absolute DW_AT_sibling"));
9792 else
9793 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9794 break;
9795 case DW_AT_byte_size:
9796 part_die->has_byte_size = 1;
9797 break;
9798 case DW_AT_calling_convention:
9799 /* DWARF doesn't provide a way to identify a program's source-level
9800 entry point. DW_AT_calling_convention attributes are only meant
9801 to describe functions' calling conventions.
9802
9803 However, because it's a necessary piece of information in
9804 Fortran, and because DW_CC_program is the only piece of debugging
9805 information whose definition refers to a 'main program' at all,
9806 several compilers have begun marking Fortran main programs with
9807 DW_CC_program --- even when those functions use the standard
9808 calling conventions.
9809
9810 So until DWARF specifies a way to provide this information and
9811 compilers pick up the new representation, we'll support this
9812 practice. */
9813 if (DW_UNSND (&attr) == DW_CC_program
9814 && cu->language == language_fortran)
9815 {
9816 set_main_name (part_die->name);
9817
9818 /* As this DIE has a static linkage the name would be difficult
9819 to look up later. */
9820 language_of_main = language_fortran;
9821 }
9822 break;
9823 default:
9824 break;
9825 }
9826 }
9827
9828 if (has_low_pc_attr && has_high_pc_attr)
9829 {
9830 /* When using the GNU linker, .gnu.linkonce. sections are used to
9831 eliminate duplicate copies of functions and vtables and such.
9832 The linker will arbitrarily choose one and discard the others.
9833 The AT_*_pc values for such functions refer to local labels in
9834 these sections. If the section from that file was discarded, the
9835 labels are not in the output, so the relocs get a value of 0.
9836 If this is a discarded function, mark the pc bounds as invalid,
9837 so that GDB will ignore it. */
9838 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9839 {
9840 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9841
9842 complaint (&symfile_complaints,
9843 _("DW_AT_low_pc %s is zero "
9844 "for DIE at 0x%x [in module %s]"),
9845 paddress (gdbarch, part_die->lowpc),
9846 part_die->offset, objfile->name);
9847 }
9848 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9849 else if (part_die->lowpc >= part_die->highpc)
9850 {
9851 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9852
9853 complaint (&symfile_complaints,
9854 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9855 "for DIE at 0x%x [in module %s]"),
9856 paddress (gdbarch, part_die->lowpc),
9857 paddress (gdbarch, part_die->highpc),
9858 part_die->offset, objfile->name);
9859 }
9860 else
9861 part_die->has_pc_info = 1;
9862 }
9863
9864 return info_ptr;
9865 }
9866
9867 /* Find a cached partial DIE at OFFSET in CU. */
9868
9869 static struct partial_die_info *
9870 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9871 {
9872 struct partial_die_info *lookup_die = NULL;
9873 struct partial_die_info part_die;
9874
9875 part_die.offset = offset;
9876 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9877
9878 return lookup_die;
9879 }
9880
9881 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9882 except in the case of .debug_types DIEs which do not reference
9883 outside their CU (they do however referencing other types via
9884 DW_FORM_ref_sig8). */
9885
9886 static struct partial_die_info *
9887 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9888 {
9889 struct objfile *objfile = cu->objfile;
9890 struct dwarf2_per_cu_data *per_cu = NULL;
9891 struct partial_die_info *pd = NULL;
9892
9893 if (cu->per_cu->debug_types_section)
9894 {
9895 pd = find_partial_die_in_comp_unit (offset, cu);
9896 if (pd != NULL)
9897 return pd;
9898 goto not_found;
9899 }
9900
9901 if (offset_in_cu_p (&cu->header, offset))
9902 {
9903 pd = find_partial_die_in_comp_unit (offset, cu);
9904 if (pd != NULL)
9905 return pd;
9906 }
9907
9908 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9909
9910 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9911 load_partial_comp_unit (per_cu);
9912
9913 per_cu->cu->last_used = 0;
9914 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9915
9916 if (pd == NULL && per_cu->load_all_dies == 0)
9917 {
9918 struct cleanup *back_to;
9919 struct partial_die_info comp_unit_die;
9920 struct abbrev_info *abbrev;
9921 unsigned int bytes_read;
9922 char *info_ptr;
9923
9924 per_cu->load_all_dies = 1;
9925
9926 /* Re-read the DIEs. */
9927 back_to = make_cleanup (null_cleanup, 0);
9928 if (per_cu->cu->dwarf2_abbrevs == NULL)
9929 {
9930 dwarf2_read_abbrevs (per_cu->cu);
9931 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9932 }
9933 info_ptr = (dwarf2_per_objfile->info.buffer
9934 + per_cu->cu->header.offset
9935 + per_cu->cu->header.first_die_offset);
9936 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9937 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9938 objfile->obfd,
9939 dwarf2_per_objfile->info.buffer, info_ptr,
9940 per_cu->cu);
9941 if (comp_unit_die.has_children)
9942 load_partial_dies (objfile->obfd,
9943 dwarf2_per_objfile->info.buffer, info_ptr,
9944 0, per_cu->cu);
9945 do_cleanups (back_to);
9946
9947 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9948 }
9949
9950 not_found:
9951
9952 if (pd == NULL)
9953 internal_error (__FILE__, __LINE__,
9954 _("could not find partial DIE 0x%x "
9955 "in cache [from module %s]\n"),
9956 offset, bfd_get_filename (objfile->obfd));
9957 return pd;
9958 }
9959
9960 /* See if we can figure out if the class lives in a namespace. We do
9961 this by looking for a member function; its demangled name will
9962 contain namespace info, if there is any. */
9963
9964 static void
9965 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9966 struct dwarf2_cu *cu)
9967 {
9968 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9969 what template types look like, because the demangler
9970 frequently doesn't give the same name as the debug info. We
9971 could fix this by only using the demangled name to get the
9972 prefix (but see comment in read_structure_type). */
9973
9974 struct partial_die_info *real_pdi;
9975 struct partial_die_info *child_pdi;
9976
9977 /* If this DIE (this DIE's specification, if any) has a parent, then
9978 we should not do this. We'll prepend the parent's fully qualified
9979 name when we create the partial symbol. */
9980
9981 real_pdi = struct_pdi;
9982 while (real_pdi->has_specification)
9983 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9984
9985 if (real_pdi->die_parent != NULL)
9986 return;
9987
9988 for (child_pdi = struct_pdi->die_child;
9989 child_pdi != NULL;
9990 child_pdi = child_pdi->die_sibling)
9991 {
9992 if (child_pdi->tag == DW_TAG_subprogram
9993 && child_pdi->linkage_name != NULL)
9994 {
9995 char *actual_class_name
9996 = language_class_name_from_physname (cu->language_defn,
9997 child_pdi->linkage_name);
9998 if (actual_class_name != NULL)
9999 {
10000 struct_pdi->name
10001 = obsavestring (actual_class_name,
10002 strlen (actual_class_name),
10003 &cu->objfile->objfile_obstack);
10004 xfree (actual_class_name);
10005 }
10006 break;
10007 }
10008 }
10009 }
10010
10011 /* Adjust PART_DIE before generating a symbol for it. This function
10012 may set the is_external flag or change the DIE's name. */
10013
10014 static void
10015 fixup_partial_die (struct partial_die_info *part_die,
10016 struct dwarf2_cu *cu)
10017 {
10018 /* Once we've fixed up a die, there's no point in doing so again.
10019 This also avoids a memory leak if we were to call
10020 guess_partial_die_structure_name multiple times. */
10021 if (part_die->fixup_called)
10022 return;
10023
10024 /* If we found a reference attribute and the DIE has no name, try
10025 to find a name in the referred to DIE. */
10026
10027 if (part_die->name == NULL && part_die->has_specification)
10028 {
10029 struct partial_die_info *spec_die;
10030
10031 spec_die = find_partial_die (part_die->spec_offset, cu);
10032
10033 fixup_partial_die (spec_die, cu);
10034
10035 if (spec_die->name)
10036 {
10037 part_die->name = spec_die->name;
10038
10039 /* Copy DW_AT_external attribute if it is set. */
10040 if (spec_die->is_external)
10041 part_die->is_external = spec_die->is_external;
10042 }
10043 }
10044
10045 /* Set default names for some unnamed DIEs. */
10046
10047 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10048 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10049
10050 /* If there is no parent die to provide a namespace, and there are
10051 children, see if we can determine the namespace from their linkage
10052 name.
10053 NOTE: We need to do this even if cu->has_namespace_info != 0.
10054 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10055 if (cu->language == language_cplus
10056 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10057 && part_die->die_parent == NULL
10058 && part_die->has_children
10059 && (part_die->tag == DW_TAG_class_type
10060 || part_die->tag == DW_TAG_structure_type
10061 || part_die->tag == DW_TAG_union_type))
10062 guess_partial_die_structure_name (part_die, cu);
10063
10064 /* GCC might emit a nameless struct or union that has a linkage
10065 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10066 if (part_die->name == NULL
10067 && (part_die->tag == DW_TAG_class_type
10068 || part_die->tag == DW_TAG_interface_type
10069 || part_die->tag == DW_TAG_structure_type
10070 || part_die->tag == DW_TAG_union_type)
10071 && part_die->linkage_name != NULL)
10072 {
10073 char *demangled;
10074
10075 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10076 if (demangled)
10077 {
10078 const char *base;
10079
10080 /* Strip any leading namespaces/classes, keep only the base name.
10081 DW_AT_name for named DIEs does not contain the prefixes. */
10082 base = strrchr (demangled, ':');
10083 if (base && base > demangled && base[-1] == ':')
10084 base++;
10085 else
10086 base = demangled;
10087
10088 part_die->name = obsavestring (base, strlen (base),
10089 &cu->objfile->objfile_obstack);
10090 xfree (demangled);
10091 }
10092 }
10093
10094 part_die->fixup_called = 1;
10095 }
10096
10097 /* Read an attribute value described by an attribute form. */
10098
10099 static gdb_byte *
10100 read_attribute_value (struct attribute *attr, unsigned form,
10101 bfd *abfd, gdb_byte *info_ptr,
10102 struct dwarf2_cu *cu)
10103 {
10104 struct comp_unit_head *cu_header = &cu->header;
10105 unsigned int bytes_read;
10106 struct dwarf_block *blk;
10107
10108 attr->form = form;
10109 switch (form)
10110 {
10111 case DW_FORM_ref_addr:
10112 if (cu->header.version == 2)
10113 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10114 else
10115 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10116 &cu->header, &bytes_read);
10117 info_ptr += bytes_read;
10118 break;
10119 case DW_FORM_addr:
10120 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10121 info_ptr += bytes_read;
10122 break;
10123 case DW_FORM_block2:
10124 blk = dwarf_alloc_block (cu);
10125 blk->size = read_2_bytes (abfd, info_ptr);
10126 info_ptr += 2;
10127 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10128 info_ptr += blk->size;
10129 DW_BLOCK (attr) = blk;
10130 break;
10131 case DW_FORM_block4:
10132 blk = dwarf_alloc_block (cu);
10133 blk->size = read_4_bytes (abfd, info_ptr);
10134 info_ptr += 4;
10135 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10136 info_ptr += blk->size;
10137 DW_BLOCK (attr) = blk;
10138 break;
10139 case DW_FORM_data2:
10140 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10141 info_ptr += 2;
10142 break;
10143 case DW_FORM_data4:
10144 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10145 info_ptr += 4;
10146 break;
10147 case DW_FORM_data8:
10148 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10149 info_ptr += 8;
10150 break;
10151 case DW_FORM_sec_offset:
10152 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10153 info_ptr += bytes_read;
10154 break;
10155 case DW_FORM_string:
10156 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10157 DW_STRING_IS_CANONICAL (attr) = 0;
10158 info_ptr += bytes_read;
10159 break;
10160 case DW_FORM_strp:
10161 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10162 &bytes_read);
10163 DW_STRING_IS_CANONICAL (attr) = 0;
10164 info_ptr += bytes_read;
10165 break;
10166 case DW_FORM_exprloc:
10167 case DW_FORM_block:
10168 blk = dwarf_alloc_block (cu);
10169 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10170 info_ptr += bytes_read;
10171 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10172 info_ptr += blk->size;
10173 DW_BLOCK (attr) = blk;
10174 break;
10175 case DW_FORM_block1:
10176 blk = dwarf_alloc_block (cu);
10177 blk->size = read_1_byte (abfd, info_ptr);
10178 info_ptr += 1;
10179 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10180 info_ptr += blk->size;
10181 DW_BLOCK (attr) = blk;
10182 break;
10183 case DW_FORM_data1:
10184 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10185 info_ptr += 1;
10186 break;
10187 case DW_FORM_flag:
10188 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10189 info_ptr += 1;
10190 break;
10191 case DW_FORM_flag_present:
10192 DW_UNSND (attr) = 1;
10193 break;
10194 case DW_FORM_sdata:
10195 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10196 info_ptr += bytes_read;
10197 break;
10198 case DW_FORM_udata:
10199 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10200 info_ptr += bytes_read;
10201 break;
10202 case DW_FORM_ref1:
10203 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10204 info_ptr += 1;
10205 break;
10206 case DW_FORM_ref2:
10207 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10208 info_ptr += 2;
10209 break;
10210 case DW_FORM_ref4:
10211 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10212 info_ptr += 4;
10213 break;
10214 case DW_FORM_ref8:
10215 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10216 info_ptr += 8;
10217 break;
10218 case DW_FORM_ref_sig8:
10219 /* Convert the signature to something we can record in DW_UNSND
10220 for later lookup.
10221 NOTE: This is NULL if the type wasn't found. */
10222 DW_SIGNATURED_TYPE (attr) =
10223 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10224 info_ptr += 8;
10225 break;
10226 case DW_FORM_ref_udata:
10227 DW_ADDR (attr) = (cu->header.offset
10228 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10229 info_ptr += bytes_read;
10230 break;
10231 case DW_FORM_indirect:
10232 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10233 info_ptr += bytes_read;
10234 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10235 break;
10236 default:
10237 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10238 dwarf_form_name (form),
10239 bfd_get_filename (abfd));
10240 }
10241
10242 /* We have seen instances where the compiler tried to emit a byte
10243 size attribute of -1 which ended up being encoded as an unsigned
10244 0xffffffff. Although 0xffffffff is technically a valid size value,
10245 an object of this size seems pretty unlikely so we can relatively
10246 safely treat these cases as if the size attribute was invalid and
10247 treat them as zero by default. */
10248 if (attr->name == DW_AT_byte_size
10249 && form == DW_FORM_data4
10250 && DW_UNSND (attr) >= 0xffffffff)
10251 {
10252 complaint
10253 (&symfile_complaints,
10254 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10255 hex_string (DW_UNSND (attr)));
10256 DW_UNSND (attr) = 0;
10257 }
10258
10259 return info_ptr;
10260 }
10261
10262 /* Read an attribute described by an abbreviated attribute. */
10263
10264 static gdb_byte *
10265 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10266 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10267 {
10268 attr->name = abbrev->name;
10269 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10270 }
10271
10272 /* Read dwarf information from a buffer. */
10273
10274 static unsigned int
10275 read_1_byte (bfd *abfd, gdb_byte *buf)
10276 {
10277 return bfd_get_8 (abfd, buf);
10278 }
10279
10280 static int
10281 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10282 {
10283 return bfd_get_signed_8 (abfd, buf);
10284 }
10285
10286 static unsigned int
10287 read_2_bytes (bfd *abfd, gdb_byte *buf)
10288 {
10289 return bfd_get_16 (abfd, buf);
10290 }
10291
10292 static int
10293 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10294 {
10295 return bfd_get_signed_16 (abfd, buf);
10296 }
10297
10298 static unsigned int
10299 read_4_bytes (bfd *abfd, gdb_byte *buf)
10300 {
10301 return bfd_get_32 (abfd, buf);
10302 }
10303
10304 static int
10305 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10306 {
10307 return bfd_get_signed_32 (abfd, buf);
10308 }
10309
10310 static ULONGEST
10311 read_8_bytes (bfd *abfd, gdb_byte *buf)
10312 {
10313 return bfd_get_64 (abfd, buf);
10314 }
10315
10316 static CORE_ADDR
10317 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10318 unsigned int *bytes_read)
10319 {
10320 struct comp_unit_head *cu_header = &cu->header;
10321 CORE_ADDR retval = 0;
10322
10323 if (cu_header->signed_addr_p)
10324 {
10325 switch (cu_header->addr_size)
10326 {
10327 case 2:
10328 retval = bfd_get_signed_16 (abfd, buf);
10329 break;
10330 case 4:
10331 retval = bfd_get_signed_32 (abfd, buf);
10332 break;
10333 case 8:
10334 retval = bfd_get_signed_64 (abfd, buf);
10335 break;
10336 default:
10337 internal_error (__FILE__, __LINE__,
10338 _("read_address: bad switch, signed [in module %s]"),
10339 bfd_get_filename (abfd));
10340 }
10341 }
10342 else
10343 {
10344 switch (cu_header->addr_size)
10345 {
10346 case 2:
10347 retval = bfd_get_16 (abfd, buf);
10348 break;
10349 case 4:
10350 retval = bfd_get_32 (abfd, buf);
10351 break;
10352 case 8:
10353 retval = bfd_get_64 (abfd, buf);
10354 break;
10355 default:
10356 internal_error (__FILE__, __LINE__,
10357 _("read_address: bad switch, "
10358 "unsigned [in module %s]"),
10359 bfd_get_filename (abfd));
10360 }
10361 }
10362
10363 *bytes_read = cu_header->addr_size;
10364 return retval;
10365 }
10366
10367 /* Read the initial length from a section. The (draft) DWARF 3
10368 specification allows the initial length to take up either 4 bytes
10369 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10370 bytes describe the length and all offsets will be 8 bytes in length
10371 instead of 4.
10372
10373 An older, non-standard 64-bit format is also handled by this
10374 function. The older format in question stores the initial length
10375 as an 8-byte quantity without an escape value. Lengths greater
10376 than 2^32 aren't very common which means that the initial 4 bytes
10377 is almost always zero. Since a length value of zero doesn't make
10378 sense for the 32-bit format, this initial zero can be considered to
10379 be an escape value which indicates the presence of the older 64-bit
10380 format. As written, the code can't detect (old format) lengths
10381 greater than 4GB. If it becomes necessary to handle lengths
10382 somewhat larger than 4GB, we could allow other small values (such
10383 as the non-sensical values of 1, 2, and 3) to also be used as
10384 escape values indicating the presence of the old format.
10385
10386 The value returned via bytes_read should be used to increment the
10387 relevant pointer after calling read_initial_length().
10388
10389 [ Note: read_initial_length() and read_offset() are based on the
10390 document entitled "DWARF Debugging Information Format", revision
10391 3, draft 8, dated November 19, 2001. This document was obtained
10392 from:
10393
10394 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10395
10396 This document is only a draft and is subject to change. (So beware.)
10397
10398 Details regarding the older, non-standard 64-bit format were
10399 determined empirically by examining 64-bit ELF files produced by
10400 the SGI toolchain on an IRIX 6.5 machine.
10401
10402 - Kevin, July 16, 2002
10403 ] */
10404
10405 static LONGEST
10406 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10407 {
10408 LONGEST length = bfd_get_32 (abfd, buf);
10409
10410 if (length == 0xffffffff)
10411 {
10412 length = bfd_get_64 (abfd, buf + 4);
10413 *bytes_read = 12;
10414 }
10415 else if (length == 0)
10416 {
10417 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10418 length = bfd_get_64 (abfd, buf);
10419 *bytes_read = 8;
10420 }
10421 else
10422 {
10423 *bytes_read = 4;
10424 }
10425
10426 return length;
10427 }
10428
10429 /* Cover function for read_initial_length.
10430 Returns the length of the object at BUF, and stores the size of the
10431 initial length in *BYTES_READ and stores the size that offsets will be in
10432 *OFFSET_SIZE.
10433 If the initial length size is not equivalent to that specified in
10434 CU_HEADER then issue a complaint.
10435 This is useful when reading non-comp-unit headers. */
10436
10437 static LONGEST
10438 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10439 const struct comp_unit_head *cu_header,
10440 unsigned int *bytes_read,
10441 unsigned int *offset_size)
10442 {
10443 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10444
10445 gdb_assert (cu_header->initial_length_size == 4
10446 || cu_header->initial_length_size == 8
10447 || cu_header->initial_length_size == 12);
10448
10449 if (cu_header->initial_length_size != *bytes_read)
10450 complaint (&symfile_complaints,
10451 _("intermixed 32-bit and 64-bit DWARF sections"));
10452
10453 *offset_size = (*bytes_read == 4) ? 4 : 8;
10454 return length;
10455 }
10456
10457 /* Read an offset from the data stream. The size of the offset is
10458 given by cu_header->offset_size. */
10459
10460 static LONGEST
10461 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10462 unsigned int *bytes_read)
10463 {
10464 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10465
10466 *bytes_read = cu_header->offset_size;
10467 return offset;
10468 }
10469
10470 /* Read an offset from the data stream. */
10471
10472 static LONGEST
10473 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10474 {
10475 LONGEST retval = 0;
10476
10477 switch (offset_size)
10478 {
10479 case 4:
10480 retval = bfd_get_32 (abfd, buf);
10481 break;
10482 case 8:
10483 retval = bfd_get_64 (abfd, buf);
10484 break;
10485 default:
10486 internal_error (__FILE__, __LINE__,
10487 _("read_offset_1: bad switch [in module %s]"),
10488 bfd_get_filename (abfd));
10489 }
10490
10491 return retval;
10492 }
10493
10494 static gdb_byte *
10495 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10496 {
10497 /* If the size of a host char is 8 bits, we can return a pointer
10498 to the buffer, otherwise we have to copy the data to a buffer
10499 allocated on the temporary obstack. */
10500 gdb_assert (HOST_CHAR_BIT == 8);
10501 return buf;
10502 }
10503
10504 static char *
10505 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10506 {
10507 /* If the size of a host char is 8 bits, we can return a pointer
10508 to the string, otherwise we have to copy the string to a buffer
10509 allocated on the temporary obstack. */
10510 gdb_assert (HOST_CHAR_BIT == 8);
10511 if (*buf == '\0')
10512 {
10513 *bytes_read_ptr = 1;
10514 return NULL;
10515 }
10516 *bytes_read_ptr = strlen ((char *) buf) + 1;
10517 return (char *) buf;
10518 }
10519
10520 static char *
10521 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10522 {
10523 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10524 if (dwarf2_per_objfile->str.buffer == NULL)
10525 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10526 bfd_get_filename (abfd));
10527 if (str_offset >= dwarf2_per_objfile->str.size)
10528 error (_("DW_FORM_strp pointing outside of "
10529 ".debug_str section [in module %s]"),
10530 bfd_get_filename (abfd));
10531 gdb_assert (HOST_CHAR_BIT == 8);
10532 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10533 return NULL;
10534 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10535 }
10536
10537 static char *
10538 read_indirect_string (bfd *abfd, gdb_byte *buf,
10539 const struct comp_unit_head *cu_header,
10540 unsigned int *bytes_read_ptr)
10541 {
10542 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10543
10544 return read_indirect_string_at_offset (abfd, str_offset);
10545 }
10546
10547 static unsigned long
10548 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10549 {
10550 unsigned long result;
10551 unsigned int num_read;
10552 int i, shift;
10553 unsigned char byte;
10554
10555 result = 0;
10556 shift = 0;
10557 num_read = 0;
10558 i = 0;
10559 while (1)
10560 {
10561 byte = bfd_get_8 (abfd, buf);
10562 buf++;
10563 num_read++;
10564 result |= ((unsigned long)(byte & 127) << shift);
10565 if ((byte & 128) == 0)
10566 {
10567 break;
10568 }
10569 shift += 7;
10570 }
10571 *bytes_read_ptr = num_read;
10572 return result;
10573 }
10574
10575 static long
10576 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10577 {
10578 long result;
10579 int i, shift, num_read;
10580 unsigned char byte;
10581
10582 result = 0;
10583 shift = 0;
10584 num_read = 0;
10585 i = 0;
10586 while (1)
10587 {
10588 byte = bfd_get_8 (abfd, buf);
10589 buf++;
10590 num_read++;
10591 result |= ((long)(byte & 127) << shift);
10592 shift += 7;
10593 if ((byte & 128) == 0)
10594 {
10595 break;
10596 }
10597 }
10598 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10599 result |= -(((long)1) << shift);
10600 *bytes_read_ptr = num_read;
10601 return result;
10602 }
10603
10604 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10605
10606 static gdb_byte *
10607 skip_leb128 (bfd *abfd, gdb_byte *buf)
10608 {
10609 int byte;
10610
10611 while (1)
10612 {
10613 byte = bfd_get_8 (abfd, buf);
10614 buf++;
10615 if ((byte & 128) == 0)
10616 return buf;
10617 }
10618 }
10619
10620 static void
10621 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10622 {
10623 switch (lang)
10624 {
10625 case DW_LANG_C89:
10626 case DW_LANG_C99:
10627 case DW_LANG_C:
10628 cu->language = language_c;
10629 break;
10630 case DW_LANG_C_plus_plus:
10631 cu->language = language_cplus;
10632 break;
10633 case DW_LANG_D:
10634 cu->language = language_d;
10635 break;
10636 case DW_LANG_Fortran77:
10637 case DW_LANG_Fortran90:
10638 case DW_LANG_Fortran95:
10639 cu->language = language_fortran;
10640 break;
10641 case DW_LANG_Mips_Assembler:
10642 cu->language = language_asm;
10643 break;
10644 case DW_LANG_Java:
10645 cu->language = language_java;
10646 break;
10647 case DW_LANG_Ada83:
10648 case DW_LANG_Ada95:
10649 cu->language = language_ada;
10650 break;
10651 case DW_LANG_Modula2:
10652 cu->language = language_m2;
10653 break;
10654 case DW_LANG_Pascal83:
10655 cu->language = language_pascal;
10656 break;
10657 case DW_LANG_ObjC:
10658 cu->language = language_objc;
10659 break;
10660 case DW_LANG_Cobol74:
10661 case DW_LANG_Cobol85:
10662 default:
10663 cu->language = language_minimal;
10664 break;
10665 }
10666 cu->language_defn = language_def (cu->language);
10667 }
10668
10669 /* Return the named attribute or NULL if not there. */
10670
10671 static struct attribute *
10672 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10673 {
10674 unsigned int i;
10675 struct attribute *spec = NULL;
10676
10677 for (i = 0; i < die->num_attrs; ++i)
10678 {
10679 if (die->attrs[i].name == name)
10680 return &die->attrs[i];
10681 if (die->attrs[i].name == DW_AT_specification
10682 || die->attrs[i].name == DW_AT_abstract_origin)
10683 spec = &die->attrs[i];
10684 }
10685
10686 if (spec)
10687 {
10688 die = follow_die_ref (die, spec, &cu);
10689 return dwarf2_attr (die, name, cu);
10690 }
10691
10692 return NULL;
10693 }
10694
10695 /* Return the named attribute or NULL if not there,
10696 but do not follow DW_AT_specification, etc.
10697 This is for use in contexts where we're reading .debug_types dies.
10698 Following DW_AT_specification, DW_AT_abstract_origin will take us
10699 back up the chain, and we want to go down. */
10700
10701 static struct attribute *
10702 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10703 struct dwarf2_cu *cu)
10704 {
10705 unsigned int i;
10706
10707 for (i = 0; i < die->num_attrs; ++i)
10708 if (die->attrs[i].name == name)
10709 return &die->attrs[i];
10710
10711 return NULL;
10712 }
10713
10714 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10715 and holds a non-zero value. This function should only be used for
10716 DW_FORM_flag or DW_FORM_flag_present attributes. */
10717
10718 static int
10719 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10720 {
10721 struct attribute *attr = dwarf2_attr (die, name, cu);
10722
10723 return (attr && DW_UNSND (attr));
10724 }
10725
10726 static int
10727 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10728 {
10729 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10730 which value is non-zero. However, we have to be careful with
10731 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10732 (via dwarf2_flag_true_p) follows this attribute. So we may
10733 end up accidently finding a declaration attribute that belongs
10734 to a different DIE referenced by the specification attribute,
10735 even though the given DIE does not have a declaration attribute. */
10736 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10737 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10738 }
10739
10740 /* Return the die giving the specification for DIE, if there is
10741 one. *SPEC_CU is the CU containing DIE on input, and the CU
10742 containing the return value on output. If there is no
10743 specification, but there is an abstract origin, that is
10744 returned. */
10745
10746 static struct die_info *
10747 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10748 {
10749 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10750 *spec_cu);
10751
10752 if (spec_attr == NULL)
10753 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10754
10755 if (spec_attr == NULL)
10756 return NULL;
10757 else
10758 return follow_die_ref (die, spec_attr, spec_cu);
10759 }
10760
10761 /* Free the line_header structure *LH, and any arrays and strings it
10762 refers to.
10763 NOTE: This is also used as a "cleanup" function. */
10764
10765 static void
10766 free_line_header (struct line_header *lh)
10767 {
10768 if (lh->standard_opcode_lengths)
10769 xfree (lh->standard_opcode_lengths);
10770
10771 /* Remember that all the lh->file_names[i].name pointers are
10772 pointers into debug_line_buffer, and don't need to be freed. */
10773 if (lh->file_names)
10774 xfree (lh->file_names);
10775
10776 /* Similarly for the include directory names. */
10777 if (lh->include_dirs)
10778 xfree (lh->include_dirs);
10779
10780 xfree (lh);
10781 }
10782
10783 /* Add an entry to LH's include directory table. */
10784
10785 static void
10786 add_include_dir (struct line_header *lh, char *include_dir)
10787 {
10788 /* Grow the array if necessary. */
10789 if (lh->include_dirs_size == 0)
10790 {
10791 lh->include_dirs_size = 1; /* for testing */
10792 lh->include_dirs = xmalloc (lh->include_dirs_size
10793 * sizeof (*lh->include_dirs));
10794 }
10795 else if (lh->num_include_dirs >= lh->include_dirs_size)
10796 {
10797 lh->include_dirs_size *= 2;
10798 lh->include_dirs = xrealloc (lh->include_dirs,
10799 (lh->include_dirs_size
10800 * sizeof (*lh->include_dirs)));
10801 }
10802
10803 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10804 }
10805
10806 /* Add an entry to LH's file name table. */
10807
10808 static void
10809 add_file_name (struct line_header *lh,
10810 char *name,
10811 unsigned int dir_index,
10812 unsigned int mod_time,
10813 unsigned int length)
10814 {
10815 struct file_entry *fe;
10816
10817 /* Grow the array if necessary. */
10818 if (lh->file_names_size == 0)
10819 {
10820 lh->file_names_size = 1; /* for testing */
10821 lh->file_names = xmalloc (lh->file_names_size
10822 * sizeof (*lh->file_names));
10823 }
10824 else if (lh->num_file_names >= lh->file_names_size)
10825 {
10826 lh->file_names_size *= 2;
10827 lh->file_names = xrealloc (lh->file_names,
10828 (lh->file_names_size
10829 * sizeof (*lh->file_names)));
10830 }
10831
10832 fe = &lh->file_names[lh->num_file_names++];
10833 fe->name = name;
10834 fe->dir_index = dir_index;
10835 fe->mod_time = mod_time;
10836 fe->length = length;
10837 fe->included_p = 0;
10838 fe->symtab = NULL;
10839 }
10840
10841 /* Read the statement program header starting at OFFSET in
10842 .debug_line, according to the endianness of ABFD. Return a pointer
10843 to a struct line_header, allocated using xmalloc.
10844
10845 NOTE: the strings in the include directory and file name tables of
10846 the returned object point into debug_line_buffer, and must not be
10847 freed. */
10848
10849 static struct line_header *
10850 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10851 struct dwarf2_cu *cu)
10852 {
10853 struct cleanup *back_to;
10854 struct line_header *lh;
10855 gdb_byte *line_ptr;
10856 unsigned int bytes_read, offset_size;
10857 int i;
10858 char *cur_dir, *cur_file;
10859
10860 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10861 if (dwarf2_per_objfile->line.buffer == NULL)
10862 {
10863 complaint (&symfile_complaints, _("missing .debug_line section"));
10864 return 0;
10865 }
10866
10867 /* Make sure that at least there's room for the total_length field.
10868 That could be 12 bytes long, but we're just going to fudge that. */
10869 if (offset + 4 >= dwarf2_per_objfile->line.size)
10870 {
10871 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10872 return 0;
10873 }
10874
10875 lh = xmalloc (sizeof (*lh));
10876 memset (lh, 0, sizeof (*lh));
10877 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10878 (void *) lh);
10879
10880 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10881
10882 /* Read in the header. */
10883 lh->total_length =
10884 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10885 &bytes_read, &offset_size);
10886 line_ptr += bytes_read;
10887 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10888 + dwarf2_per_objfile->line.size))
10889 {
10890 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10891 return 0;
10892 }
10893 lh->statement_program_end = line_ptr + lh->total_length;
10894 lh->version = read_2_bytes (abfd, line_ptr);
10895 line_ptr += 2;
10896 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10897 line_ptr += offset_size;
10898 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10899 line_ptr += 1;
10900 if (lh->version >= 4)
10901 {
10902 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10903 line_ptr += 1;
10904 }
10905 else
10906 lh->maximum_ops_per_instruction = 1;
10907
10908 if (lh->maximum_ops_per_instruction == 0)
10909 {
10910 lh->maximum_ops_per_instruction = 1;
10911 complaint (&symfile_complaints,
10912 _("invalid maximum_ops_per_instruction "
10913 "in `.debug_line' section"));
10914 }
10915
10916 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10917 line_ptr += 1;
10918 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10919 line_ptr += 1;
10920 lh->line_range = read_1_byte (abfd, line_ptr);
10921 line_ptr += 1;
10922 lh->opcode_base = read_1_byte (abfd, line_ptr);
10923 line_ptr += 1;
10924 lh->standard_opcode_lengths
10925 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10926
10927 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10928 for (i = 1; i < lh->opcode_base; ++i)
10929 {
10930 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10931 line_ptr += 1;
10932 }
10933
10934 /* Read directory table. */
10935 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10936 {
10937 line_ptr += bytes_read;
10938 add_include_dir (lh, cur_dir);
10939 }
10940 line_ptr += bytes_read;
10941
10942 /* Read file name table. */
10943 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10944 {
10945 unsigned int dir_index, mod_time, length;
10946
10947 line_ptr += bytes_read;
10948 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10949 line_ptr += bytes_read;
10950 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10951 line_ptr += bytes_read;
10952 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10953 line_ptr += bytes_read;
10954
10955 add_file_name (lh, cur_file, dir_index, mod_time, length);
10956 }
10957 line_ptr += bytes_read;
10958 lh->statement_program_start = line_ptr;
10959
10960 if (line_ptr > (dwarf2_per_objfile->line.buffer
10961 + dwarf2_per_objfile->line.size))
10962 complaint (&symfile_complaints,
10963 _("line number info header doesn't "
10964 "fit in `.debug_line' section"));
10965
10966 discard_cleanups (back_to);
10967 return lh;
10968 }
10969
10970 /* Subroutine of dwarf_decode_lines to simplify it.
10971 Return the file name of the psymtab for included file FILE_INDEX
10972 in line header LH of PST.
10973 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10974 If space for the result is malloc'd, it will be freed by a cleanup.
10975 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10976
10977 static char *
10978 psymtab_include_file_name (const struct line_header *lh, int file_index,
10979 const struct partial_symtab *pst,
10980 const char *comp_dir)
10981 {
10982 const struct file_entry fe = lh->file_names [file_index];
10983 char *include_name = fe.name;
10984 char *include_name_to_compare = include_name;
10985 char *dir_name = NULL;
10986 const char *pst_filename;
10987 char *copied_name = NULL;
10988 int file_is_pst;
10989
10990 if (fe.dir_index)
10991 dir_name = lh->include_dirs[fe.dir_index - 1];
10992
10993 if (!IS_ABSOLUTE_PATH (include_name)
10994 && (dir_name != NULL || comp_dir != NULL))
10995 {
10996 /* Avoid creating a duplicate psymtab for PST.
10997 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10998 Before we do the comparison, however, we need to account
10999 for DIR_NAME and COMP_DIR.
11000 First prepend dir_name (if non-NULL). If we still don't
11001 have an absolute path prepend comp_dir (if non-NULL).
11002 However, the directory we record in the include-file's
11003 psymtab does not contain COMP_DIR (to match the
11004 corresponding symtab(s)).
11005
11006 Example:
11007
11008 bash$ cd /tmp
11009 bash$ gcc -g ./hello.c
11010 include_name = "hello.c"
11011 dir_name = "."
11012 DW_AT_comp_dir = comp_dir = "/tmp"
11013 DW_AT_name = "./hello.c" */
11014
11015 if (dir_name != NULL)
11016 {
11017 include_name = concat (dir_name, SLASH_STRING,
11018 include_name, (char *)NULL);
11019 include_name_to_compare = include_name;
11020 make_cleanup (xfree, include_name);
11021 }
11022 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11023 {
11024 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11025 include_name, (char *)NULL);
11026 }
11027 }
11028
11029 pst_filename = pst->filename;
11030 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11031 {
11032 copied_name = concat (pst->dirname, SLASH_STRING,
11033 pst_filename, (char *)NULL);
11034 pst_filename = copied_name;
11035 }
11036
11037 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11038
11039 if (include_name_to_compare != include_name)
11040 xfree (include_name_to_compare);
11041 if (copied_name != NULL)
11042 xfree (copied_name);
11043
11044 if (file_is_pst)
11045 return NULL;
11046 return include_name;
11047 }
11048
11049 /* Ignore this record_line request. */
11050
11051 static void
11052 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11053 {
11054 return;
11055 }
11056
11057 /* Decode the Line Number Program (LNP) for the given line_header
11058 structure and CU. The actual information extracted and the type
11059 of structures created from the LNP depends on the value of PST.
11060
11061 1. If PST is NULL, then this procedure uses the data from the program
11062 to create all necessary symbol tables, and their linetables.
11063
11064 2. If PST is not NULL, this procedure reads the program to determine
11065 the list of files included by the unit represented by PST, and
11066 builds all the associated partial symbol tables.
11067
11068 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11069 It is used for relative paths in the line table.
11070 NOTE: When processing partial symtabs (pst != NULL),
11071 comp_dir == pst->dirname.
11072
11073 NOTE: It is important that psymtabs have the same file name (via strcmp)
11074 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11075 symtab we don't use it in the name of the psymtabs we create.
11076 E.g. expand_line_sal requires this when finding psymtabs to expand.
11077 A good testcase for this is mb-inline.exp. */
11078
11079 static void
11080 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11081 struct dwarf2_cu *cu, struct partial_symtab *pst)
11082 {
11083 gdb_byte *line_ptr, *extended_end;
11084 gdb_byte *line_end;
11085 unsigned int bytes_read, extended_len;
11086 unsigned char op_code, extended_op, adj_opcode;
11087 CORE_ADDR baseaddr;
11088 struct objfile *objfile = cu->objfile;
11089 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11090 const int decode_for_pst_p = (pst != NULL);
11091 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11092 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11093 = record_line;
11094
11095 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11096
11097 line_ptr = lh->statement_program_start;
11098 line_end = lh->statement_program_end;
11099
11100 /* Read the statement sequences until there's nothing left. */
11101 while (line_ptr < line_end)
11102 {
11103 /* state machine registers */
11104 CORE_ADDR address = 0;
11105 unsigned int file = 1;
11106 unsigned int line = 1;
11107 unsigned int column = 0;
11108 int is_stmt = lh->default_is_stmt;
11109 int basic_block = 0;
11110 int end_sequence = 0;
11111 CORE_ADDR addr;
11112 unsigned char op_index = 0;
11113
11114 if (!decode_for_pst_p && lh->num_file_names >= file)
11115 {
11116 /* Start a subfile for the current file of the state machine. */
11117 /* lh->include_dirs and lh->file_names are 0-based, but the
11118 directory and file name numbers in the statement program
11119 are 1-based. */
11120 struct file_entry *fe = &lh->file_names[file - 1];
11121 char *dir = NULL;
11122
11123 if (fe->dir_index)
11124 dir = lh->include_dirs[fe->dir_index - 1];
11125
11126 dwarf2_start_subfile (fe->name, dir, comp_dir);
11127 }
11128
11129 /* Decode the table. */
11130 while (!end_sequence)
11131 {
11132 op_code = read_1_byte (abfd, line_ptr);
11133 line_ptr += 1;
11134 if (line_ptr > line_end)
11135 {
11136 dwarf2_debug_line_missing_end_sequence_complaint ();
11137 break;
11138 }
11139
11140 if (op_code >= lh->opcode_base)
11141 {
11142 /* Special operand. */
11143 adj_opcode = op_code - lh->opcode_base;
11144 address += (((op_index + (adj_opcode / lh->line_range))
11145 / lh->maximum_ops_per_instruction)
11146 * lh->minimum_instruction_length);
11147 op_index = ((op_index + (adj_opcode / lh->line_range))
11148 % lh->maximum_ops_per_instruction);
11149 line += lh->line_base + (adj_opcode % lh->line_range);
11150 if (lh->num_file_names < file || file == 0)
11151 dwarf2_debug_line_missing_file_complaint ();
11152 /* For now we ignore lines not starting on an
11153 instruction boundary. */
11154 else if (op_index == 0)
11155 {
11156 lh->file_names[file - 1].included_p = 1;
11157 if (!decode_for_pst_p && is_stmt)
11158 {
11159 if (last_subfile != current_subfile)
11160 {
11161 addr = gdbarch_addr_bits_remove (gdbarch, address);
11162 if (last_subfile)
11163 (*p_record_line) (last_subfile, 0, addr);
11164 last_subfile = current_subfile;
11165 }
11166 /* Append row to matrix using current values. */
11167 addr = gdbarch_addr_bits_remove (gdbarch, address);
11168 (*p_record_line) (current_subfile, line, addr);
11169 }
11170 }
11171 basic_block = 0;
11172 }
11173 else switch (op_code)
11174 {
11175 case DW_LNS_extended_op:
11176 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11177 &bytes_read);
11178 line_ptr += bytes_read;
11179 extended_end = line_ptr + extended_len;
11180 extended_op = read_1_byte (abfd, line_ptr);
11181 line_ptr += 1;
11182 switch (extended_op)
11183 {
11184 case DW_LNE_end_sequence:
11185 p_record_line = record_line;
11186 end_sequence = 1;
11187 break;
11188 case DW_LNE_set_address:
11189 address = read_address (abfd, line_ptr, cu, &bytes_read);
11190
11191 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11192 {
11193 /* This line table is for a function which has been
11194 GCd by the linker. Ignore it. PR gdb/12528 */
11195
11196 long line_offset
11197 = line_ptr - dwarf2_per_objfile->line.buffer;
11198
11199 complaint (&symfile_complaints,
11200 _(".debug_line address at offset 0x%lx is 0 "
11201 "[in module %s]"),
11202 line_offset, objfile->name);
11203 p_record_line = noop_record_line;
11204 }
11205
11206 op_index = 0;
11207 line_ptr += bytes_read;
11208 address += baseaddr;
11209 break;
11210 case DW_LNE_define_file:
11211 {
11212 char *cur_file;
11213 unsigned int dir_index, mod_time, length;
11214
11215 cur_file = read_direct_string (abfd, line_ptr,
11216 &bytes_read);
11217 line_ptr += bytes_read;
11218 dir_index =
11219 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11220 line_ptr += bytes_read;
11221 mod_time =
11222 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11223 line_ptr += bytes_read;
11224 length =
11225 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11226 line_ptr += bytes_read;
11227 add_file_name (lh, cur_file, dir_index, mod_time, length);
11228 }
11229 break;
11230 case DW_LNE_set_discriminator:
11231 /* The discriminator is not interesting to the debugger;
11232 just ignore it. */
11233 line_ptr = extended_end;
11234 break;
11235 default:
11236 complaint (&symfile_complaints,
11237 _("mangled .debug_line section"));
11238 return;
11239 }
11240 /* Make sure that we parsed the extended op correctly. If e.g.
11241 we expected a different address size than the producer used,
11242 we may have read the wrong number of bytes. */
11243 if (line_ptr != extended_end)
11244 {
11245 complaint (&symfile_complaints,
11246 _("mangled .debug_line section"));
11247 return;
11248 }
11249 break;
11250 case DW_LNS_copy:
11251 if (lh->num_file_names < file || file == 0)
11252 dwarf2_debug_line_missing_file_complaint ();
11253 else
11254 {
11255 lh->file_names[file - 1].included_p = 1;
11256 if (!decode_for_pst_p && is_stmt)
11257 {
11258 if (last_subfile != current_subfile)
11259 {
11260 addr = gdbarch_addr_bits_remove (gdbarch, address);
11261 if (last_subfile)
11262 (*p_record_line) (last_subfile, 0, addr);
11263 last_subfile = current_subfile;
11264 }
11265 addr = gdbarch_addr_bits_remove (gdbarch, address);
11266 (*p_record_line) (current_subfile, line, addr);
11267 }
11268 }
11269 basic_block = 0;
11270 break;
11271 case DW_LNS_advance_pc:
11272 {
11273 CORE_ADDR adjust
11274 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11275
11276 address += (((op_index + adjust)
11277 / lh->maximum_ops_per_instruction)
11278 * lh->minimum_instruction_length);
11279 op_index = ((op_index + adjust)
11280 % lh->maximum_ops_per_instruction);
11281 line_ptr += bytes_read;
11282 }
11283 break;
11284 case DW_LNS_advance_line:
11285 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11286 line_ptr += bytes_read;
11287 break;
11288 case DW_LNS_set_file:
11289 {
11290 /* The arrays lh->include_dirs and lh->file_names are
11291 0-based, but the directory and file name numbers in
11292 the statement program are 1-based. */
11293 struct file_entry *fe;
11294 char *dir = NULL;
11295
11296 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11297 line_ptr += bytes_read;
11298 if (lh->num_file_names < file || file == 0)
11299 dwarf2_debug_line_missing_file_complaint ();
11300 else
11301 {
11302 fe = &lh->file_names[file - 1];
11303 if (fe->dir_index)
11304 dir = lh->include_dirs[fe->dir_index - 1];
11305 if (!decode_for_pst_p)
11306 {
11307 last_subfile = current_subfile;
11308 dwarf2_start_subfile (fe->name, dir, comp_dir);
11309 }
11310 }
11311 }
11312 break;
11313 case DW_LNS_set_column:
11314 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11315 line_ptr += bytes_read;
11316 break;
11317 case DW_LNS_negate_stmt:
11318 is_stmt = (!is_stmt);
11319 break;
11320 case DW_LNS_set_basic_block:
11321 basic_block = 1;
11322 break;
11323 /* Add to the address register of the state machine the
11324 address increment value corresponding to special opcode
11325 255. I.e., this value is scaled by the minimum
11326 instruction length since special opcode 255 would have
11327 scaled the increment. */
11328 case DW_LNS_const_add_pc:
11329 {
11330 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11331
11332 address += (((op_index + adjust)
11333 / lh->maximum_ops_per_instruction)
11334 * lh->minimum_instruction_length);
11335 op_index = ((op_index + adjust)
11336 % lh->maximum_ops_per_instruction);
11337 }
11338 break;
11339 case DW_LNS_fixed_advance_pc:
11340 address += read_2_bytes (abfd, line_ptr);
11341 op_index = 0;
11342 line_ptr += 2;
11343 break;
11344 default:
11345 {
11346 /* Unknown standard opcode, ignore it. */
11347 int i;
11348
11349 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11350 {
11351 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11352 line_ptr += bytes_read;
11353 }
11354 }
11355 }
11356 }
11357 if (lh->num_file_names < file || file == 0)
11358 dwarf2_debug_line_missing_file_complaint ();
11359 else
11360 {
11361 lh->file_names[file - 1].included_p = 1;
11362 if (!decode_for_pst_p)
11363 {
11364 addr = gdbarch_addr_bits_remove (gdbarch, address);
11365 (*p_record_line) (current_subfile, 0, addr);
11366 }
11367 }
11368 }
11369
11370 if (decode_for_pst_p)
11371 {
11372 int file_index;
11373
11374 /* Now that we're done scanning the Line Header Program, we can
11375 create the psymtab of each included file. */
11376 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11377 if (lh->file_names[file_index].included_p == 1)
11378 {
11379 char *include_name =
11380 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11381 if (include_name != NULL)
11382 dwarf2_create_include_psymtab (include_name, pst, objfile);
11383 }
11384 }
11385 else
11386 {
11387 /* Make sure a symtab is created for every file, even files
11388 which contain only variables (i.e. no code with associated
11389 line numbers). */
11390
11391 int i;
11392 struct file_entry *fe;
11393
11394 for (i = 0; i < lh->num_file_names; i++)
11395 {
11396 char *dir = NULL;
11397
11398 fe = &lh->file_names[i];
11399 if (fe->dir_index)
11400 dir = lh->include_dirs[fe->dir_index - 1];
11401 dwarf2_start_subfile (fe->name, dir, comp_dir);
11402
11403 /* Skip the main file; we don't need it, and it must be
11404 allocated last, so that it will show up before the
11405 non-primary symtabs in the objfile's symtab list. */
11406 if (current_subfile == first_subfile)
11407 continue;
11408
11409 if (current_subfile->symtab == NULL)
11410 current_subfile->symtab = allocate_symtab (current_subfile->name,
11411 objfile);
11412 fe->symtab = current_subfile->symtab;
11413 }
11414 }
11415 }
11416
11417 /* Start a subfile for DWARF. FILENAME is the name of the file and
11418 DIRNAME the name of the source directory which contains FILENAME
11419 or NULL if not known. COMP_DIR is the compilation directory for the
11420 linetable's compilation unit or NULL if not known.
11421 This routine tries to keep line numbers from identical absolute and
11422 relative file names in a common subfile.
11423
11424 Using the `list' example from the GDB testsuite, which resides in
11425 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11426 of /srcdir/list0.c yields the following debugging information for list0.c:
11427
11428 DW_AT_name: /srcdir/list0.c
11429 DW_AT_comp_dir: /compdir
11430 files.files[0].name: list0.h
11431 files.files[0].dir: /srcdir
11432 files.files[1].name: list0.c
11433 files.files[1].dir: /srcdir
11434
11435 The line number information for list0.c has to end up in a single
11436 subfile, so that `break /srcdir/list0.c:1' works as expected.
11437 start_subfile will ensure that this happens provided that we pass the
11438 concatenation of files.files[1].dir and files.files[1].name as the
11439 subfile's name. */
11440
11441 static void
11442 dwarf2_start_subfile (char *filename, const char *dirname,
11443 const char *comp_dir)
11444 {
11445 char *fullname;
11446
11447 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11448 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11449 second argument to start_subfile. To be consistent, we do the
11450 same here. In order not to lose the line information directory,
11451 we concatenate it to the filename when it makes sense.
11452 Note that the Dwarf3 standard says (speaking of filenames in line
11453 information): ``The directory index is ignored for file names
11454 that represent full path names''. Thus ignoring dirname in the
11455 `else' branch below isn't an issue. */
11456
11457 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11458 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11459 else
11460 fullname = filename;
11461
11462 start_subfile (fullname, comp_dir);
11463
11464 if (fullname != filename)
11465 xfree (fullname);
11466 }
11467
11468 static void
11469 var_decode_location (struct attribute *attr, struct symbol *sym,
11470 struct dwarf2_cu *cu)
11471 {
11472 struct objfile *objfile = cu->objfile;
11473 struct comp_unit_head *cu_header = &cu->header;
11474
11475 /* NOTE drow/2003-01-30: There used to be a comment and some special
11476 code here to turn a symbol with DW_AT_external and a
11477 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11478 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11479 with some versions of binutils) where shared libraries could have
11480 relocations against symbols in their debug information - the
11481 minimal symbol would have the right address, but the debug info
11482 would not. It's no longer necessary, because we will explicitly
11483 apply relocations when we read in the debug information now. */
11484
11485 /* A DW_AT_location attribute with no contents indicates that a
11486 variable has been optimized away. */
11487 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11488 {
11489 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11490 return;
11491 }
11492
11493 /* Handle one degenerate form of location expression specially, to
11494 preserve GDB's previous behavior when section offsets are
11495 specified. If this is just a DW_OP_addr then mark this symbol
11496 as LOC_STATIC. */
11497
11498 if (attr_form_is_block (attr)
11499 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11500 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11501 {
11502 unsigned int dummy;
11503
11504 SYMBOL_VALUE_ADDRESS (sym) =
11505 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11506 SYMBOL_CLASS (sym) = LOC_STATIC;
11507 fixup_symbol_section (sym, objfile);
11508 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11509 SYMBOL_SECTION (sym));
11510 return;
11511 }
11512
11513 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11514 expression evaluator, and use LOC_COMPUTED only when necessary
11515 (i.e. when the value of a register or memory location is
11516 referenced, or a thread-local block, etc.). Then again, it might
11517 not be worthwhile. I'm assuming that it isn't unless performance
11518 or memory numbers show me otherwise. */
11519
11520 dwarf2_symbol_mark_computed (attr, sym, cu);
11521 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11522
11523 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11524 cu->has_loclist = 1;
11525 }
11526
11527 /* Given a pointer to a DWARF information entry, figure out if we need
11528 to make a symbol table entry for it, and if so, create a new entry
11529 and return a pointer to it.
11530 If TYPE is NULL, determine symbol type from the die, otherwise
11531 used the passed type.
11532 If SPACE is not NULL, use it to hold the new symbol. If it is
11533 NULL, allocate a new symbol on the objfile's obstack. */
11534
11535 static struct symbol *
11536 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11537 struct symbol *space)
11538 {
11539 struct objfile *objfile = cu->objfile;
11540 struct symbol *sym = NULL;
11541 char *name;
11542 struct attribute *attr = NULL;
11543 struct attribute *attr2 = NULL;
11544 CORE_ADDR baseaddr;
11545 struct pending **list_to_add = NULL;
11546
11547 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11548
11549 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11550
11551 name = dwarf2_name (die, cu);
11552 if (name)
11553 {
11554 const char *linkagename;
11555 int suppress_add = 0;
11556
11557 if (space)
11558 sym = space;
11559 else
11560 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11561 OBJSTAT (objfile, n_syms++);
11562
11563 /* Cache this symbol's name and the name's demangled form (if any). */
11564 SYMBOL_SET_LANGUAGE (sym, cu->language);
11565 linkagename = dwarf2_physname (name, die, cu);
11566 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11567
11568 /* Fortran does not have mangling standard and the mangling does differ
11569 between gfortran, iFort etc. */
11570 if (cu->language == language_fortran
11571 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11572 symbol_set_demangled_name (&(sym->ginfo),
11573 (char *) dwarf2_full_name (name, die, cu),
11574 NULL);
11575
11576 /* Default assumptions.
11577 Use the passed type or decode it from the die. */
11578 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11579 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11580 if (type != NULL)
11581 SYMBOL_TYPE (sym) = type;
11582 else
11583 SYMBOL_TYPE (sym) = die_type (die, cu);
11584 attr = dwarf2_attr (die,
11585 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11586 cu);
11587 if (attr)
11588 {
11589 SYMBOL_LINE (sym) = DW_UNSND (attr);
11590 }
11591
11592 attr = dwarf2_attr (die,
11593 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11594 cu);
11595 if (attr)
11596 {
11597 int file_index = DW_UNSND (attr);
11598
11599 if (cu->line_header == NULL
11600 || file_index > cu->line_header->num_file_names)
11601 complaint (&symfile_complaints,
11602 _("file index out of range"));
11603 else if (file_index > 0)
11604 {
11605 struct file_entry *fe;
11606
11607 fe = &cu->line_header->file_names[file_index - 1];
11608 SYMBOL_SYMTAB (sym) = fe->symtab;
11609 }
11610 }
11611
11612 switch (die->tag)
11613 {
11614 case DW_TAG_label:
11615 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11616 if (attr)
11617 {
11618 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11619 }
11620 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11621 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11622 SYMBOL_CLASS (sym) = LOC_LABEL;
11623 add_symbol_to_list (sym, cu->list_in_scope);
11624 break;
11625 case DW_TAG_subprogram:
11626 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11627 finish_block. */
11628 SYMBOL_CLASS (sym) = LOC_BLOCK;
11629 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11630 if ((attr2 && (DW_UNSND (attr2) != 0))
11631 || cu->language == language_ada)
11632 {
11633 /* Subprograms marked external are stored as a global symbol.
11634 Ada subprograms, whether marked external or not, are always
11635 stored as a global symbol, because we want to be able to
11636 access them globally. For instance, we want to be able
11637 to break on a nested subprogram without having to
11638 specify the context. */
11639 list_to_add = &global_symbols;
11640 }
11641 else
11642 {
11643 list_to_add = cu->list_in_scope;
11644 }
11645 break;
11646 case DW_TAG_inlined_subroutine:
11647 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11648 finish_block. */
11649 SYMBOL_CLASS (sym) = LOC_BLOCK;
11650 SYMBOL_INLINED (sym) = 1;
11651 /* Do not add the symbol to any lists. It will be found via
11652 BLOCK_FUNCTION from the blockvector. */
11653 break;
11654 case DW_TAG_template_value_param:
11655 suppress_add = 1;
11656 /* Fall through. */
11657 case DW_TAG_constant:
11658 case DW_TAG_variable:
11659 case DW_TAG_member:
11660 /* Compilation with minimal debug info may result in
11661 variables with missing type entries. Change the
11662 misleading `void' type to something sensible. */
11663 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11664 SYMBOL_TYPE (sym)
11665 = objfile_type (objfile)->nodebug_data_symbol;
11666
11667 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11668 /* In the case of DW_TAG_member, we should only be called for
11669 static const members. */
11670 if (die->tag == DW_TAG_member)
11671 {
11672 /* dwarf2_add_field uses die_is_declaration,
11673 so we do the same. */
11674 gdb_assert (die_is_declaration (die, cu));
11675 gdb_assert (attr);
11676 }
11677 if (attr)
11678 {
11679 dwarf2_const_value (attr, sym, cu);
11680 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11681 if (!suppress_add)
11682 {
11683 if (attr2 && (DW_UNSND (attr2) != 0))
11684 list_to_add = &global_symbols;
11685 else
11686 list_to_add = cu->list_in_scope;
11687 }
11688 break;
11689 }
11690 attr = dwarf2_attr (die, DW_AT_location, cu);
11691 if (attr)
11692 {
11693 var_decode_location (attr, sym, cu);
11694 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11695 if (SYMBOL_CLASS (sym) == LOC_STATIC
11696 && SYMBOL_VALUE_ADDRESS (sym) == 0
11697 && !dwarf2_per_objfile->has_section_at_zero)
11698 {
11699 /* When a static variable is eliminated by the linker,
11700 the corresponding debug information is not stripped
11701 out, but the variable address is set to null;
11702 do not add such variables into symbol table. */
11703 }
11704 else if (attr2 && (DW_UNSND (attr2) != 0))
11705 {
11706 /* Workaround gfortran PR debug/40040 - it uses
11707 DW_AT_location for variables in -fPIC libraries which may
11708 get overriden by other libraries/executable and get
11709 a different address. Resolve it by the minimal symbol
11710 which may come from inferior's executable using copy
11711 relocation. Make this workaround only for gfortran as for
11712 other compilers GDB cannot guess the minimal symbol
11713 Fortran mangling kind. */
11714 if (cu->language == language_fortran && die->parent
11715 && die->parent->tag == DW_TAG_module
11716 && cu->producer
11717 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11718 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11719
11720 /* A variable with DW_AT_external is never static,
11721 but it may be block-scoped. */
11722 list_to_add = (cu->list_in_scope == &file_symbols
11723 ? &global_symbols : cu->list_in_scope);
11724 }
11725 else
11726 list_to_add = cu->list_in_scope;
11727 }
11728 else
11729 {
11730 /* We do not know the address of this symbol.
11731 If it is an external symbol and we have type information
11732 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11733 The address of the variable will then be determined from
11734 the minimal symbol table whenever the variable is
11735 referenced. */
11736 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11737 if (attr2 && (DW_UNSND (attr2) != 0)
11738 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11739 {
11740 /* A variable with DW_AT_external is never static, but it
11741 may be block-scoped. */
11742 list_to_add = (cu->list_in_scope == &file_symbols
11743 ? &global_symbols : cu->list_in_scope);
11744
11745 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11746 }
11747 else if (!die_is_declaration (die, cu))
11748 {
11749 /* Use the default LOC_OPTIMIZED_OUT class. */
11750 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11751 if (!suppress_add)
11752 list_to_add = cu->list_in_scope;
11753 }
11754 }
11755 break;
11756 case DW_TAG_formal_parameter:
11757 /* If we are inside a function, mark this as an argument. If
11758 not, we might be looking at an argument to an inlined function
11759 when we do not have enough information to show inlined frames;
11760 pretend it's a local variable in that case so that the user can
11761 still see it. */
11762 if (context_stack_depth > 0
11763 && context_stack[context_stack_depth - 1].name != NULL)
11764 SYMBOL_IS_ARGUMENT (sym) = 1;
11765 attr = dwarf2_attr (die, DW_AT_location, cu);
11766 if (attr)
11767 {
11768 var_decode_location (attr, sym, cu);
11769 }
11770 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11771 if (attr)
11772 {
11773 dwarf2_const_value (attr, sym, cu);
11774 }
11775
11776 list_to_add = cu->list_in_scope;
11777 break;
11778 case DW_TAG_unspecified_parameters:
11779 /* From varargs functions; gdb doesn't seem to have any
11780 interest in this information, so just ignore it for now.
11781 (FIXME?) */
11782 break;
11783 case DW_TAG_template_type_param:
11784 suppress_add = 1;
11785 /* Fall through. */
11786 case DW_TAG_class_type:
11787 case DW_TAG_interface_type:
11788 case DW_TAG_structure_type:
11789 case DW_TAG_union_type:
11790 case DW_TAG_set_type:
11791 case DW_TAG_enumeration_type:
11792 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11793 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11794
11795 {
11796 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11797 really ever be static objects: otherwise, if you try
11798 to, say, break of a class's method and you're in a file
11799 which doesn't mention that class, it won't work unless
11800 the check for all static symbols in lookup_symbol_aux
11801 saves you. See the OtherFileClass tests in
11802 gdb.c++/namespace.exp. */
11803
11804 if (!suppress_add)
11805 {
11806 list_to_add = (cu->list_in_scope == &file_symbols
11807 && (cu->language == language_cplus
11808 || cu->language == language_java)
11809 ? &global_symbols : cu->list_in_scope);
11810
11811 /* The semantics of C++ state that "struct foo {
11812 ... }" also defines a typedef for "foo". A Java
11813 class declaration also defines a typedef for the
11814 class. */
11815 if (cu->language == language_cplus
11816 || cu->language == language_java
11817 || cu->language == language_ada)
11818 {
11819 /* The symbol's name is already allocated along
11820 with this objfile, so we don't need to
11821 duplicate it for the type. */
11822 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11823 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11824 }
11825 }
11826 }
11827 break;
11828 case DW_TAG_typedef:
11829 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11830 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11831 list_to_add = cu->list_in_scope;
11832 break;
11833 case DW_TAG_base_type:
11834 case DW_TAG_subrange_type:
11835 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11836 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11837 list_to_add = cu->list_in_scope;
11838 break;
11839 case DW_TAG_enumerator:
11840 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11841 if (attr)
11842 {
11843 dwarf2_const_value (attr, sym, cu);
11844 }
11845 {
11846 /* NOTE: carlton/2003-11-10: See comment above in the
11847 DW_TAG_class_type, etc. block. */
11848
11849 list_to_add = (cu->list_in_scope == &file_symbols
11850 && (cu->language == language_cplus
11851 || cu->language == language_java)
11852 ? &global_symbols : cu->list_in_scope);
11853 }
11854 break;
11855 case DW_TAG_namespace:
11856 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11857 list_to_add = &global_symbols;
11858 break;
11859 default:
11860 /* Not a tag we recognize. Hopefully we aren't processing
11861 trash data, but since we must specifically ignore things
11862 we don't recognize, there is nothing else we should do at
11863 this point. */
11864 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11865 dwarf_tag_name (die->tag));
11866 break;
11867 }
11868
11869 if (suppress_add)
11870 {
11871 sym->hash_next = objfile->template_symbols;
11872 objfile->template_symbols = sym;
11873 list_to_add = NULL;
11874 }
11875
11876 if (list_to_add != NULL)
11877 add_symbol_to_list (sym, list_to_add);
11878
11879 /* For the benefit of old versions of GCC, check for anonymous
11880 namespaces based on the demangled name. */
11881 if (!processing_has_namespace_info
11882 && cu->language == language_cplus)
11883 cp_scan_for_anonymous_namespaces (sym, objfile);
11884 }
11885 return (sym);
11886 }
11887
11888 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11889
11890 static struct symbol *
11891 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11892 {
11893 return new_symbol_full (die, type, cu, NULL);
11894 }
11895
11896 /* Given an attr with a DW_FORM_dataN value in host byte order,
11897 zero-extend it as appropriate for the symbol's type. The DWARF
11898 standard (v4) is not entirely clear about the meaning of using
11899 DW_FORM_dataN for a constant with a signed type, where the type is
11900 wider than the data. The conclusion of a discussion on the DWARF
11901 list was that this is unspecified. We choose to always zero-extend
11902 because that is the interpretation long in use by GCC. */
11903
11904 static gdb_byte *
11905 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11906 const char *name, struct obstack *obstack,
11907 struct dwarf2_cu *cu, long *value, int bits)
11908 {
11909 struct objfile *objfile = cu->objfile;
11910 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11911 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11912 LONGEST l = DW_UNSND (attr);
11913
11914 if (bits < sizeof (*value) * 8)
11915 {
11916 l &= ((LONGEST) 1 << bits) - 1;
11917 *value = l;
11918 }
11919 else if (bits == sizeof (*value) * 8)
11920 *value = l;
11921 else
11922 {
11923 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11924 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11925 return bytes;
11926 }
11927
11928 return NULL;
11929 }
11930
11931 /* Read a constant value from an attribute. Either set *VALUE, or if
11932 the value does not fit in *VALUE, set *BYTES - either already
11933 allocated on the objfile obstack, or newly allocated on OBSTACK,
11934 or, set *BATON, if we translated the constant to a location
11935 expression. */
11936
11937 static void
11938 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11939 const char *name, struct obstack *obstack,
11940 struct dwarf2_cu *cu,
11941 long *value, gdb_byte **bytes,
11942 struct dwarf2_locexpr_baton **baton)
11943 {
11944 struct objfile *objfile = cu->objfile;
11945 struct comp_unit_head *cu_header = &cu->header;
11946 struct dwarf_block *blk;
11947 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11948 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11949
11950 *value = 0;
11951 *bytes = NULL;
11952 *baton = NULL;
11953
11954 switch (attr->form)
11955 {
11956 case DW_FORM_addr:
11957 {
11958 gdb_byte *data;
11959
11960 if (TYPE_LENGTH (type) != cu_header->addr_size)
11961 dwarf2_const_value_length_mismatch_complaint (name,
11962 cu_header->addr_size,
11963 TYPE_LENGTH (type));
11964 /* Symbols of this form are reasonably rare, so we just
11965 piggyback on the existing location code rather than writing
11966 a new implementation of symbol_computed_ops. */
11967 *baton = obstack_alloc (&objfile->objfile_obstack,
11968 sizeof (struct dwarf2_locexpr_baton));
11969 (*baton)->per_cu = cu->per_cu;
11970 gdb_assert ((*baton)->per_cu);
11971
11972 (*baton)->size = 2 + cu_header->addr_size;
11973 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11974 (*baton)->data = data;
11975
11976 data[0] = DW_OP_addr;
11977 store_unsigned_integer (&data[1], cu_header->addr_size,
11978 byte_order, DW_ADDR (attr));
11979 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11980 }
11981 break;
11982 case DW_FORM_string:
11983 case DW_FORM_strp:
11984 /* DW_STRING is already allocated on the objfile obstack, point
11985 directly to it. */
11986 *bytes = (gdb_byte *) DW_STRING (attr);
11987 break;
11988 case DW_FORM_block1:
11989 case DW_FORM_block2:
11990 case DW_FORM_block4:
11991 case DW_FORM_block:
11992 case DW_FORM_exprloc:
11993 blk = DW_BLOCK (attr);
11994 if (TYPE_LENGTH (type) != blk->size)
11995 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11996 TYPE_LENGTH (type));
11997 *bytes = blk->data;
11998 break;
11999
12000 /* The DW_AT_const_value attributes are supposed to carry the
12001 symbol's value "represented as it would be on the target
12002 architecture." By the time we get here, it's already been
12003 converted to host endianness, so we just need to sign- or
12004 zero-extend it as appropriate. */
12005 case DW_FORM_data1:
12006 *bytes = dwarf2_const_value_data (attr, type, name,
12007 obstack, cu, value, 8);
12008 break;
12009 case DW_FORM_data2:
12010 *bytes = dwarf2_const_value_data (attr, type, name,
12011 obstack, cu, value, 16);
12012 break;
12013 case DW_FORM_data4:
12014 *bytes = dwarf2_const_value_data (attr, type, name,
12015 obstack, cu, value, 32);
12016 break;
12017 case DW_FORM_data8:
12018 *bytes = dwarf2_const_value_data (attr, type, name,
12019 obstack, cu, value, 64);
12020 break;
12021
12022 case DW_FORM_sdata:
12023 *value = DW_SND (attr);
12024 break;
12025
12026 case DW_FORM_udata:
12027 *value = DW_UNSND (attr);
12028 break;
12029
12030 default:
12031 complaint (&symfile_complaints,
12032 _("unsupported const value attribute form: '%s'"),
12033 dwarf_form_name (attr->form));
12034 *value = 0;
12035 break;
12036 }
12037 }
12038
12039
12040 /* Copy constant value from an attribute to a symbol. */
12041
12042 static void
12043 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12044 struct dwarf2_cu *cu)
12045 {
12046 struct objfile *objfile = cu->objfile;
12047 struct comp_unit_head *cu_header = &cu->header;
12048 long value;
12049 gdb_byte *bytes;
12050 struct dwarf2_locexpr_baton *baton;
12051
12052 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12053 SYMBOL_PRINT_NAME (sym),
12054 &objfile->objfile_obstack, cu,
12055 &value, &bytes, &baton);
12056
12057 if (baton != NULL)
12058 {
12059 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12060 SYMBOL_LOCATION_BATON (sym) = baton;
12061 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12062 }
12063 else if (bytes != NULL)
12064 {
12065 SYMBOL_VALUE_BYTES (sym) = bytes;
12066 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12067 }
12068 else
12069 {
12070 SYMBOL_VALUE (sym) = value;
12071 SYMBOL_CLASS (sym) = LOC_CONST;
12072 }
12073 }
12074
12075 /* Return the type of the die in question using its DW_AT_type attribute. */
12076
12077 static struct type *
12078 die_type (struct die_info *die, struct dwarf2_cu *cu)
12079 {
12080 struct attribute *type_attr;
12081
12082 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12083 if (!type_attr)
12084 {
12085 /* A missing DW_AT_type represents a void type. */
12086 return objfile_type (cu->objfile)->builtin_void;
12087 }
12088
12089 return lookup_die_type (die, type_attr, cu);
12090 }
12091
12092 /* True iff CU's producer generates GNAT Ada auxiliary information
12093 that allows to find parallel types through that information instead
12094 of having to do expensive parallel lookups by type name. */
12095
12096 static int
12097 need_gnat_info (struct dwarf2_cu *cu)
12098 {
12099 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12100 of GNAT produces this auxiliary information, without any indication
12101 that it is produced. Part of enhancing the FSF version of GNAT
12102 to produce that information will be to put in place an indicator
12103 that we can use in order to determine whether the descriptive type
12104 info is available or not. One suggestion that has been made is
12105 to use a new attribute, attached to the CU die. For now, assume
12106 that the descriptive type info is not available. */
12107 return 0;
12108 }
12109
12110 /* Return the auxiliary type of the die in question using its
12111 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12112 attribute is not present. */
12113
12114 static struct type *
12115 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12116 {
12117 struct attribute *type_attr;
12118
12119 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12120 if (!type_attr)
12121 return NULL;
12122
12123 return lookup_die_type (die, type_attr, cu);
12124 }
12125
12126 /* If DIE has a descriptive_type attribute, then set the TYPE's
12127 descriptive type accordingly. */
12128
12129 static void
12130 set_descriptive_type (struct type *type, struct die_info *die,
12131 struct dwarf2_cu *cu)
12132 {
12133 struct type *descriptive_type = die_descriptive_type (die, cu);
12134
12135 if (descriptive_type)
12136 {
12137 ALLOCATE_GNAT_AUX_TYPE (type);
12138 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12139 }
12140 }
12141
12142 /* Return the containing type of the die in question using its
12143 DW_AT_containing_type attribute. */
12144
12145 static struct type *
12146 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12147 {
12148 struct attribute *type_attr;
12149
12150 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12151 if (!type_attr)
12152 error (_("Dwarf Error: Problem turning containing type into gdb type "
12153 "[in module %s]"), cu->objfile->name);
12154
12155 return lookup_die_type (die, type_attr, cu);
12156 }
12157
12158 /* Look up the type of DIE in CU using its type attribute ATTR.
12159 If there is no type substitute an error marker. */
12160
12161 static struct type *
12162 lookup_die_type (struct die_info *die, struct attribute *attr,
12163 struct dwarf2_cu *cu)
12164 {
12165 struct objfile *objfile = cu->objfile;
12166 struct type *this_type;
12167
12168 /* First see if we have it cached. */
12169
12170 if (is_ref_attr (attr))
12171 {
12172 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12173
12174 this_type = get_die_type_at_offset (offset, cu->per_cu);
12175 }
12176 else if (attr->form == DW_FORM_ref_sig8)
12177 {
12178 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12179 struct dwarf2_cu *sig_cu;
12180 unsigned int offset;
12181
12182 /* sig_type will be NULL if the signatured type is missing from
12183 the debug info. */
12184 if (sig_type == NULL)
12185 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12186 "at 0x%x [in module %s]"),
12187 die->offset, objfile->name);
12188
12189 gdb_assert (sig_type->per_cu.debug_types_section);
12190 offset = sig_type->per_cu.offset + sig_type->type_offset;
12191 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12192 }
12193 else
12194 {
12195 dump_die_for_error (die);
12196 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12197 dwarf_attr_name (attr->name), objfile->name);
12198 }
12199
12200 /* If not cached we need to read it in. */
12201
12202 if (this_type == NULL)
12203 {
12204 struct die_info *type_die;
12205 struct dwarf2_cu *type_cu = cu;
12206
12207 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12208 /* If the type is cached, we should have found it above. */
12209 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12210 this_type = read_type_die_1 (type_die, type_cu);
12211 }
12212
12213 /* If we still don't have a type use an error marker. */
12214
12215 if (this_type == NULL)
12216 {
12217 char *message, *saved;
12218
12219 /* read_type_die already issued a complaint. */
12220 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12221 objfile->name,
12222 cu->header.offset,
12223 die->offset);
12224 saved = obstack_copy0 (&objfile->objfile_obstack,
12225 message, strlen (message));
12226 xfree (message);
12227
12228 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12229 }
12230
12231 return this_type;
12232 }
12233
12234 /* Return the type in DIE, CU.
12235 Returns NULL for invalid types.
12236
12237 This first does a lookup in the appropriate type_hash table,
12238 and only reads the die in if necessary.
12239
12240 NOTE: This can be called when reading in partial or full symbols. */
12241
12242 static struct type *
12243 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12244 {
12245 struct type *this_type;
12246
12247 this_type = get_die_type (die, cu);
12248 if (this_type)
12249 return this_type;
12250
12251 return read_type_die_1 (die, cu);
12252 }
12253
12254 /* Read the type in DIE, CU.
12255 Returns NULL for invalid types. */
12256
12257 static struct type *
12258 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12259 {
12260 struct type *this_type = NULL;
12261
12262 switch (die->tag)
12263 {
12264 case DW_TAG_class_type:
12265 case DW_TAG_interface_type:
12266 case DW_TAG_structure_type:
12267 case DW_TAG_union_type:
12268 this_type = read_structure_type (die, cu);
12269 break;
12270 case DW_TAG_enumeration_type:
12271 this_type = read_enumeration_type (die, cu);
12272 break;
12273 case DW_TAG_subprogram:
12274 case DW_TAG_subroutine_type:
12275 case DW_TAG_inlined_subroutine:
12276 this_type = read_subroutine_type (die, cu);
12277 break;
12278 case DW_TAG_array_type:
12279 this_type = read_array_type (die, cu);
12280 break;
12281 case DW_TAG_set_type:
12282 this_type = read_set_type (die, cu);
12283 break;
12284 case DW_TAG_pointer_type:
12285 this_type = read_tag_pointer_type (die, cu);
12286 break;
12287 case DW_TAG_ptr_to_member_type:
12288 this_type = read_tag_ptr_to_member_type (die, cu);
12289 break;
12290 case DW_TAG_reference_type:
12291 this_type = read_tag_reference_type (die, cu);
12292 break;
12293 case DW_TAG_const_type:
12294 this_type = read_tag_const_type (die, cu);
12295 break;
12296 case DW_TAG_volatile_type:
12297 this_type = read_tag_volatile_type (die, cu);
12298 break;
12299 case DW_TAG_string_type:
12300 this_type = read_tag_string_type (die, cu);
12301 break;
12302 case DW_TAG_typedef:
12303 this_type = read_typedef (die, cu);
12304 break;
12305 case DW_TAG_subrange_type:
12306 this_type = read_subrange_type (die, cu);
12307 break;
12308 case DW_TAG_base_type:
12309 this_type = read_base_type (die, cu);
12310 break;
12311 case DW_TAG_unspecified_type:
12312 this_type = read_unspecified_type (die, cu);
12313 break;
12314 case DW_TAG_namespace:
12315 this_type = read_namespace_type (die, cu);
12316 break;
12317 case DW_TAG_module:
12318 this_type = read_module_type (die, cu);
12319 break;
12320 default:
12321 complaint (&symfile_complaints,
12322 _("unexpected tag in read_type_die: '%s'"),
12323 dwarf_tag_name (die->tag));
12324 break;
12325 }
12326
12327 return this_type;
12328 }
12329
12330 /* See if we can figure out if the class lives in a namespace. We do
12331 this by looking for a member function; its demangled name will
12332 contain namespace info, if there is any.
12333 Return the computed name or NULL.
12334 Space for the result is allocated on the objfile's obstack.
12335 This is the full-die version of guess_partial_die_structure_name.
12336 In this case we know DIE has no useful parent. */
12337
12338 static char *
12339 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12340 {
12341 struct die_info *spec_die;
12342 struct dwarf2_cu *spec_cu;
12343 struct die_info *child;
12344
12345 spec_cu = cu;
12346 spec_die = die_specification (die, &spec_cu);
12347 if (spec_die != NULL)
12348 {
12349 die = spec_die;
12350 cu = spec_cu;
12351 }
12352
12353 for (child = die->child;
12354 child != NULL;
12355 child = child->sibling)
12356 {
12357 if (child->tag == DW_TAG_subprogram)
12358 {
12359 struct attribute *attr;
12360
12361 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12362 if (attr == NULL)
12363 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12364 if (attr != NULL)
12365 {
12366 char *actual_name
12367 = language_class_name_from_physname (cu->language_defn,
12368 DW_STRING (attr));
12369 char *name = NULL;
12370
12371 if (actual_name != NULL)
12372 {
12373 char *die_name = dwarf2_name (die, cu);
12374
12375 if (die_name != NULL
12376 && strcmp (die_name, actual_name) != 0)
12377 {
12378 /* Strip off the class name from the full name.
12379 We want the prefix. */
12380 int die_name_len = strlen (die_name);
12381 int actual_name_len = strlen (actual_name);
12382
12383 /* Test for '::' as a sanity check. */
12384 if (actual_name_len > die_name_len + 2
12385 && actual_name[actual_name_len
12386 - die_name_len - 1] == ':')
12387 name =
12388 obsavestring (actual_name,
12389 actual_name_len - die_name_len - 2,
12390 &cu->objfile->objfile_obstack);
12391 }
12392 }
12393 xfree (actual_name);
12394 return name;
12395 }
12396 }
12397 }
12398
12399 return NULL;
12400 }
12401
12402 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12403 prefix part in such case. See
12404 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12405
12406 static char *
12407 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12408 {
12409 struct attribute *attr;
12410 char *base;
12411
12412 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12413 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12414 return NULL;
12415
12416 attr = dwarf2_attr (die, DW_AT_name, cu);
12417 if (attr != NULL && DW_STRING (attr) != NULL)
12418 return NULL;
12419
12420 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12421 if (attr == NULL)
12422 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12423 if (attr == NULL || DW_STRING (attr) == NULL)
12424 return NULL;
12425
12426 /* dwarf2_name had to be already called. */
12427 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12428
12429 /* Strip the base name, keep any leading namespaces/classes. */
12430 base = strrchr (DW_STRING (attr), ':');
12431 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12432 return "";
12433
12434 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12435 &cu->objfile->objfile_obstack);
12436 }
12437
12438 /* Return the name of the namespace/class that DIE is defined within,
12439 or "" if we can't tell. The caller should not xfree the result.
12440
12441 For example, if we're within the method foo() in the following
12442 code:
12443
12444 namespace N {
12445 class C {
12446 void foo () {
12447 }
12448 };
12449 }
12450
12451 then determine_prefix on foo's die will return "N::C". */
12452
12453 static char *
12454 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12455 {
12456 struct die_info *parent, *spec_die;
12457 struct dwarf2_cu *spec_cu;
12458 struct type *parent_type;
12459 char *retval;
12460
12461 if (cu->language != language_cplus && cu->language != language_java
12462 && cu->language != language_fortran)
12463 return "";
12464
12465 retval = anonymous_struct_prefix (die, cu);
12466 if (retval)
12467 return retval;
12468
12469 /* We have to be careful in the presence of DW_AT_specification.
12470 For example, with GCC 3.4, given the code
12471
12472 namespace N {
12473 void foo() {
12474 // Definition of N::foo.
12475 }
12476 }
12477
12478 then we'll have a tree of DIEs like this:
12479
12480 1: DW_TAG_compile_unit
12481 2: DW_TAG_namespace // N
12482 3: DW_TAG_subprogram // declaration of N::foo
12483 4: DW_TAG_subprogram // definition of N::foo
12484 DW_AT_specification // refers to die #3
12485
12486 Thus, when processing die #4, we have to pretend that we're in
12487 the context of its DW_AT_specification, namely the contex of die
12488 #3. */
12489 spec_cu = cu;
12490 spec_die = die_specification (die, &spec_cu);
12491 if (spec_die == NULL)
12492 parent = die->parent;
12493 else
12494 {
12495 parent = spec_die->parent;
12496 cu = spec_cu;
12497 }
12498
12499 if (parent == NULL)
12500 return "";
12501 else if (parent->building_fullname)
12502 {
12503 const char *name;
12504 const char *parent_name;
12505
12506 /* It has been seen on RealView 2.2 built binaries,
12507 DW_TAG_template_type_param types actually _defined_ as
12508 children of the parent class:
12509
12510 enum E {};
12511 template class <class Enum> Class{};
12512 Class<enum E> class_e;
12513
12514 1: DW_TAG_class_type (Class)
12515 2: DW_TAG_enumeration_type (E)
12516 3: DW_TAG_enumerator (enum1:0)
12517 3: DW_TAG_enumerator (enum2:1)
12518 ...
12519 2: DW_TAG_template_type_param
12520 DW_AT_type DW_FORM_ref_udata (E)
12521
12522 Besides being broken debug info, it can put GDB into an
12523 infinite loop. Consider:
12524
12525 When we're building the full name for Class<E>, we'll start
12526 at Class, and go look over its template type parameters,
12527 finding E. We'll then try to build the full name of E, and
12528 reach here. We're now trying to build the full name of E,
12529 and look over the parent DIE for containing scope. In the
12530 broken case, if we followed the parent DIE of E, we'd again
12531 find Class, and once again go look at its template type
12532 arguments, etc., etc. Simply don't consider such parent die
12533 as source-level parent of this die (it can't be, the language
12534 doesn't allow it), and break the loop here. */
12535 name = dwarf2_name (die, cu);
12536 parent_name = dwarf2_name (parent, cu);
12537 complaint (&symfile_complaints,
12538 _("template param type '%s' defined within parent '%s'"),
12539 name ? name : "<unknown>",
12540 parent_name ? parent_name : "<unknown>");
12541 return "";
12542 }
12543 else
12544 switch (parent->tag)
12545 {
12546 case DW_TAG_namespace:
12547 parent_type = read_type_die (parent, cu);
12548 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12549 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12550 Work around this problem here. */
12551 if (cu->language == language_cplus
12552 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12553 return "";
12554 /* We give a name to even anonymous namespaces. */
12555 return TYPE_TAG_NAME (parent_type);
12556 case DW_TAG_class_type:
12557 case DW_TAG_interface_type:
12558 case DW_TAG_structure_type:
12559 case DW_TAG_union_type:
12560 case DW_TAG_module:
12561 parent_type = read_type_die (parent, cu);
12562 if (TYPE_TAG_NAME (parent_type) != NULL)
12563 return TYPE_TAG_NAME (parent_type);
12564 else
12565 /* An anonymous structure is only allowed non-static data
12566 members; no typedefs, no member functions, et cetera.
12567 So it does not need a prefix. */
12568 return "";
12569 case DW_TAG_compile_unit:
12570 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12571 if (cu->language == language_cplus
12572 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12573 && die->child != NULL
12574 && (die->tag == DW_TAG_class_type
12575 || die->tag == DW_TAG_structure_type
12576 || die->tag == DW_TAG_union_type))
12577 {
12578 char *name = guess_full_die_structure_name (die, cu);
12579 if (name != NULL)
12580 return name;
12581 }
12582 return "";
12583 default:
12584 return determine_prefix (parent, cu);
12585 }
12586 }
12587
12588 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12589 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12590 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12591 an obconcat, otherwise allocate storage for the result. The CU argument is
12592 used to determine the language and hence, the appropriate separator. */
12593
12594 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12595
12596 static char *
12597 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12598 int physname, struct dwarf2_cu *cu)
12599 {
12600 const char *lead = "";
12601 const char *sep;
12602
12603 if (suffix == NULL || suffix[0] == '\0'
12604 || prefix == NULL || prefix[0] == '\0')
12605 sep = "";
12606 else if (cu->language == language_java)
12607 sep = ".";
12608 else if (cu->language == language_fortran && physname)
12609 {
12610 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12611 DW_AT_MIPS_linkage_name is preferred and used instead. */
12612
12613 lead = "__";
12614 sep = "_MOD_";
12615 }
12616 else
12617 sep = "::";
12618
12619 if (prefix == NULL)
12620 prefix = "";
12621 if (suffix == NULL)
12622 suffix = "";
12623
12624 if (obs == NULL)
12625 {
12626 char *retval
12627 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12628
12629 strcpy (retval, lead);
12630 strcat (retval, prefix);
12631 strcat (retval, sep);
12632 strcat (retval, suffix);
12633 return retval;
12634 }
12635 else
12636 {
12637 /* We have an obstack. */
12638 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12639 }
12640 }
12641
12642 /* Return sibling of die, NULL if no sibling. */
12643
12644 static struct die_info *
12645 sibling_die (struct die_info *die)
12646 {
12647 return die->sibling;
12648 }
12649
12650 /* Get name of a die, return NULL if not found. */
12651
12652 static char *
12653 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12654 struct obstack *obstack)
12655 {
12656 if (name && cu->language == language_cplus)
12657 {
12658 char *canon_name = cp_canonicalize_string (name);
12659
12660 if (canon_name != NULL)
12661 {
12662 if (strcmp (canon_name, name) != 0)
12663 name = obsavestring (canon_name, strlen (canon_name),
12664 obstack);
12665 xfree (canon_name);
12666 }
12667 }
12668
12669 return name;
12670 }
12671
12672 /* Get name of a die, return NULL if not found. */
12673
12674 static char *
12675 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12676 {
12677 struct attribute *attr;
12678
12679 attr = dwarf2_attr (die, DW_AT_name, cu);
12680 if ((!attr || !DW_STRING (attr))
12681 && die->tag != DW_TAG_class_type
12682 && die->tag != DW_TAG_interface_type
12683 && die->tag != DW_TAG_structure_type
12684 && die->tag != DW_TAG_union_type)
12685 return NULL;
12686
12687 switch (die->tag)
12688 {
12689 case DW_TAG_compile_unit:
12690 /* Compilation units have a DW_AT_name that is a filename, not
12691 a source language identifier. */
12692 case DW_TAG_enumeration_type:
12693 case DW_TAG_enumerator:
12694 /* These tags always have simple identifiers already; no need
12695 to canonicalize them. */
12696 return DW_STRING (attr);
12697
12698 case DW_TAG_subprogram:
12699 /* Java constructors will all be named "<init>", so return
12700 the class name when we see this special case. */
12701 if (cu->language == language_java
12702 && DW_STRING (attr) != NULL
12703 && strcmp (DW_STRING (attr), "<init>") == 0)
12704 {
12705 struct dwarf2_cu *spec_cu = cu;
12706 struct die_info *spec_die;
12707
12708 /* GCJ will output '<init>' for Java constructor names.
12709 For this special case, return the name of the parent class. */
12710
12711 /* GCJ may output suprogram DIEs with AT_specification set.
12712 If so, use the name of the specified DIE. */
12713 spec_die = die_specification (die, &spec_cu);
12714 if (spec_die != NULL)
12715 return dwarf2_name (spec_die, spec_cu);
12716
12717 do
12718 {
12719 die = die->parent;
12720 if (die->tag == DW_TAG_class_type)
12721 return dwarf2_name (die, cu);
12722 }
12723 while (die->tag != DW_TAG_compile_unit);
12724 }
12725 break;
12726
12727 case DW_TAG_class_type:
12728 case DW_TAG_interface_type:
12729 case DW_TAG_structure_type:
12730 case DW_TAG_union_type:
12731 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12732 structures or unions. These were of the form "._%d" in GCC 4.1,
12733 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12734 and GCC 4.4. We work around this problem by ignoring these. */
12735 if (attr && DW_STRING (attr)
12736 && (strncmp (DW_STRING (attr), "._", 2) == 0
12737 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12738 return NULL;
12739
12740 /* GCC might emit a nameless typedef that has a linkage name. See
12741 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12742 if (!attr || DW_STRING (attr) == NULL)
12743 {
12744 char *demangled = NULL;
12745
12746 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12747 if (attr == NULL)
12748 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12749
12750 if (attr == NULL || DW_STRING (attr) == NULL)
12751 return NULL;
12752
12753 /* Avoid demangling DW_STRING (attr) the second time on a second
12754 call for the same DIE. */
12755 if (!DW_STRING_IS_CANONICAL (attr))
12756 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12757
12758 if (demangled)
12759 {
12760 char *base;
12761
12762 /* FIXME: we already did this for the partial symbol... */
12763 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12764 &cu->objfile->objfile_obstack);
12765 DW_STRING_IS_CANONICAL (attr) = 1;
12766 xfree (demangled);
12767
12768 /* Strip any leading namespaces/classes, keep only the base name.
12769 DW_AT_name for named DIEs does not contain the prefixes. */
12770 base = strrchr (DW_STRING (attr), ':');
12771 if (base && base > DW_STRING (attr) && base[-1] == ':')
12772 return &base[1];
12773 else
12774 return DW_STRING (attr);
12775 }
12776 }
12777 break;
12778
12779 default:
12780 break;
12781 }
12782
12783 if (!DW_STRING_IS_CANONICAL (attr))
12784 {
12785 DW_STRING (attr)
12786 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12787 &cu->objfile->objfile_obstack);
12788 DW_STRING_IS_CANONICAL (attr) = 1;
12789 }
12790 return DW_STRING (attr);
12791 }
12792
12793 /* Return the die that this die in an extension of, or NULL if there
12794 is none. *EXT_CU is the CU containing DIE on input, and the CU
12795 containing the return value on output. */
12796
12797 static struct die_info *
12798 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12799 {
12800 struct attribute *attr;
12801
12802 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12803 if (attr == NULL)
12804 return NULL;
12805
12806 return follow_die_ref (die, attr, ext_cu);
12807 }
12808
12809 /* Convert a DIE tag into its string name. */
12810
12811 static char *
12812 dwarf_tag_name (unsigned tag)
12813 {
12814 switch (tag)
12815 {
12816 case DW_TAG_padding:
12817 return "DW_TAG_padding";
12818 case DW_TAG_array_type:
12819 return "DW_TAG_array_type";
12820 case DW_TAG_class_type:
12821 return "DW_TAG_class_type";
12822 case DW_TAG_entry_point:
12823 return "DW_TAG_entry_point";
12824 case DW_TAG_enumeration_type:
12825 return "DW_TAG_enumeration_type";
12826 case DW_TAG_formal_parameter:
12827 return "DW_TAG_formal_parameter";
12828 case DW_TAG_imported_declaration:
12829 return "DW_TAG_imported_declaration";
12830 case DW_TAG_label:
12831 return "DW_TAG_label";
12832 case DW_TAG_lexical_block:
12833 return "DW_TAG_lexical_block";
12834 case DW_TAG_member:
12835 return "DW_TAG_member";
12836 case DW_TAG_pointer_type:
12837 return "DW_TAG_pointer_type";
12838 case DW_TAG_reference_type:
12839 return "DW_TAG_reference_type";
12840 case DW_TAG_compile_unit:
12841 return "DW_TAG_compile_unit";
12842 case DW_TAG_string_type:
12843 return "DW_TAG_string_type";
12844 case DW_TAG_structure_type:
12845 return "DW_TAG_structure_type";
12846 case DW_TAG_subroutine_type:
12847 return "DW_TAG_subroutine_type";
12848 case DW_TAG_typedef:
12849 return "DW_TAG_typedef";
12850 case DW_TAG_union_type:
12851 return "DW_TAG_union_type";
12852 case DW_TAG_unspecified_parameters:
12853 return "DW_TAG_unspecified_parameters";
12854 case DW_TAG_variant:
12855 return "DW_TAG_variant";
12856 case DW_TAG_common_block:
12857 return "DW_TAG_common_block";
12858 case DW_TAG_common_inclusion:
12859 return "DW_TAG_common_inclusion";
12860 case DW_TAG_inheritance:
12861 return "DW_TAG_inheritance";
12862 case DW_TAG_inlined_subroutine:
12863 return "DW_TAG_inlined_subroutine";
12864 case DW_TAG_module:
12865 return "DW_TAG_module";
12866 case DW_TAG_ptr_to_member_type:
12867 return "DW_TAG_ptr_to_member_type";
12868 case DW_TAG_set_type:
12869 return "DW_TAG_set_type";
12870 case DW_TAG_subrange_type:
12871 return "DW_TAG_subrange_type";
12872 case DW_TAG_with_stmt:
12873 return "DW_TAG_with_stmt";
12874 case DW_TAG_access_declaration:
12875 return "DW_TAG_access_declaration";
12876 case DW_TAG_base_type:
12877 return "DW_TAG_base_type";
12878 case DW_TAG_catch_block:
12879 return "DW_TAG_catch_block";
12880 case DW_TAG_const_type:
12881 return "DW_TAG_const_type";
12882 case DW_TAG_constant:
12883 return "DW_TAG_constant";
12884 case DW_TAG_enumerator:
12885 return "DW_TAG_enumerator";
12886 case DW_TAG_file_type:
12887 return "DW_TAG_file_type";
12888 case DW_TAG_friend:
12889 return "DW_TAG_friend";
12890 case DW_TAG_namelist:
12891 return "DW_TAG_namelist";
12892 case DW_TAG_namelist_item:
12893 return "DW_TAG_namelist_item";
12894 case DW_TAG_packed_type:
12895 return "DW_TAG_packed_type";
12896 case DW_TAG_subprogram:
12897 return "DW_TAG_subprogram";
12898 case DW_TAG_template_type_param:
12899 return "DW_TAG_template_type_param";
12900 case DW_TAG_template_value_param:
12901 return "DW_TAG_template_value_param";
12902 case DW_TAG_thrown_type:
12903 return "DW_TAG_thrown_type";
12904 case DW_TAG_try_block:
12905 return "DW_TAG_try_block";
12906 case DW_TAG_variant_part:
12907 return "DW_TAG_variant_part";
12908 case DW_TAG_variable:
12909 return "DW_TAG_variable";
12910 case DW_TAG_volatile_type:
12911 return "DW_TAG_volatile_type";
12912 case DW_TAG_dwarf_procedure:
12913 return "DW_TAG_dwarf_procedure";
12914 case DW_TAG_restrict_type:
12915 return "DW_TAG_restrict_type";
12916 case DW_TAG_interface_type:
12917 return "DW_TAG_interface_type";
12918 case DW_TAG_namespace:
12919 return "DW_TAG_namespace";
12920 case DW_TAG_imported_module:
12921 return "DW_TAG_imported_module";
12922 case DW_TAG_unspecified_type:
12923 return "DW_TAG_unspecified_type";
12924 case DW_TAG_partial_unit:
12925 return "DW_TAG_partial_unit";
12926 case DW_TAG_imported_unit:
12927 return "DW_TAG_imported_unit";
12928 case DW_TAG_condition:
12929 return "DW_TAG_condition";
12930 case DW_TAG_shared_type:
12931 return "DW_TAG_shared_type";
12932 case DW_TAG_type_unit:
12933 return "DW_TAG_type_unit";
12934 case DW_TAG_MIPS_loop:
12935 return "DW_TAG_MIPS_loop";
12936 case DW_TAG_HP_array_descriptor:
12937 return "DW_TAG_HP_array_descriptor";
12938 case DW_TAG_format_label:
12939 return "DW_TAG_format_label";
12940 case DW_TAG_function_template:
12941 return "DW_TAG_function_template";
12942 case DW_TAG_class_template:
12943 return "DW_TAG_class_template";
12944 case DW_TAG_GNU_BINCL:
12945 return "DW_TAG_GNU_BINCL";
12946 case DW_TAG_GNU_EINCL:
12947 return "DW_TAG_GNU_EINCL";
12948 case DW_TAG_upc_shared_type:
12949 return "DW_TAG_upc_shared_type";
12950 case DW_TAG_upc_strict_type:
12951 return "DW_TAG_upc_strict_type";
12952 case DW_TAG_upc_relaxed_type:
12953 return "DW_TAG_upc_relaxed_type";
12954 case DW_TAG_PGI_kanji_type:
12955 return "DW_TAG_PGI_kanji_type";
12956 case DW_TAG_PGI_interface_block:
12957 return "DW_TAG_PGI_interface_block";
12958 case DW_TAG_GNU_call_site:
12959 return "DW_TAG_GNU_call_site";
12960 default:
12961 return "DW_TAG_<unknown>";
12962 }
12963 }
12964
12965 /* Convert a DWARF attribute code into its string name. */
12966
12967 static char *
12968 dwarf_attr_name (unsigned attr)
12969 {
12970 switch (attr)
12971 {
12972 case DW_AT_sibling:
12973 return "DW_AT_sibling";
12974 case DW_AT_location:
12975 return "DW_AT_location";
12976 case DW_AT_name:
12977 return "DW_AT_name";
12978 case DW_AT_ordering:
12979 return "DW_AT_ordering";
12980 case DW_AT_subscr_data:
12981 return "DW_AT_subscr_data";
12982 case DW_AT_byte_size:
12983 return "DW_AT_byte_size";
12984 case DW_AT_bit_offset:
12985 return "DW_AT_bit_offset";
12986 case DW_AT_bit_size:
12987 return "DW_AT_bit_size";
12988 case DW_AT_element_list:
12989 return "DW_AT_element_list";
12990 case DW_AT_stmt_list:
12991 return "DW_AT_stmt_list";
12992 case DW_AT_low_pc:
12993 return "DW_AT_low_pc";
12994 case DW_AT_high_pc:
12995 return "DW_AT_high_pc";
12996 case DW_AT_language:
12997 return "DW_AT_language";
12998 case DW_AT_member:
12999 return "DW_AT_member";
13000 case DW_AT_discr:
13001 return "DW_AT_discr";
13002 case DW_AT_discr_value:
13003 return "DW_AT_discr_value";
13004 case DW_AT_visibility:
13005 return "DW_AT_visibility";
13006 case DW_AT_import:
13007 return "DW_AT_import";
13008 case DW_AT_string_length:
13009 return "DW_AT_string_length";
13010 case DW_AT_common_reference:
13011 return "DW_AT_common_reference";
13012 case DW_AT_comp_dir:
13013 return "DW_AT_comp_dir";
13014 case DW_AT_const_value:
13015 return "DW_AT_const_value";
13016 case DW_AT_containing_type:
13017 return "DW_AT_containing_type";
13018 case DW_AT_default_value:
13019 return "DW_AT_default_value";
13020 case DW_AT_inline:
13021 return "DW_AT_inline";
13022 case DW_AT_is_optional:
13023 return "DW_AT_is_optional";
13024 case DW_AT_lower_bound:
13025 return "DW_AT_lower_bound";
13026 case DW_AT_producer:
13027 return "DW_AT_producer";
13028 case DW_AT_prototyped:
13029 return "DW_AT_prototyped";
13030 case DW_AT_return_addr:
13031 return "DW_AT_return_addr";
13032 case DW_AT_start_scope:
13033 return "DW_AT_start_scope";
13034 case DW_AT_bit_stride:
13035 return "DW_AT_bit_stride";
13036 case DW_AT_upper_bound:
13037 return "DW_AT_upper_bound";
13038 case DW_AT_abstract_origin:
13039 return "DW_AT_abstract_origin";
13040 case DW_AT_accessibility:
13041 return "DW_AT_accessibility";
13042 case DW_AT_address_class:
13043 return "DW_AT_address_class";
13044 case DW_AT_artificial:
13045 return "DW_AT_artificial";
13046 case DW_AT_base_types:
13047 return "DW_AT_base_types";
13048 case DW_AT_calling_convention:
13049 return "DW_AT_calling_convention";
13050 case DW_AT_count:
13051 return "DW_AT_count";
13052 case DW_AT_data_member_location:
13053 return "DW_AT_data_member_location";
13054 case DW_AT_decl_column:
13055 return "DW_AT_decl_column";
13056 case DW_AT_decl_file:
13057 return "DW_AT_decl_file";
13058 case DW_AT_decl_line:
13059 return "DW_AT_decl_line";
13060 case DW_AT_declaration:
13061 return "DW_AT_declaration";
13062 case DW_AT_discr_list:
13063 return "DW_AT_discr_list";
13064 case DW_AT_encoding:
13065 return "DW_AT_encoding";
13066 case DW_AT_external:
13067 return "DW_AT_external";
13068 case DW_AT_frame_base:
13069 return "DW_AT_frame_base";
13070 case DW_AT_friend:
13071 return "DW_AT_friend";
13072 case DW_AT_identifier_case:
13073 return "DW_AT_identifier_case";
13074 case DW_AT_macro_info:
13075 return "DW_AT_macro_info";
13076 case DW_AT_namelist_items:
13077 return "DW_AT_namelist_items";
13078 case DW_AT_priority:
13079 return "DW_AT_priority";
13080 case DW_AT_segment:
13081 return "DW_AT_segment";
13082 case DW_AT_specification:
13083 return "DW_AT_specification";
13084 case DW_AT_static_link:
13085 return "DW_AT_static_link";
13086 case DW_AT_type:
13087 return "DW_AT_type";
13088 case DW_AT_use_location:
13089 return "DW_AT_use_location";
13090 case DW_AT_variable_parameter:
13091 return "DW_AT_variable_parameter";
13092 case DW_AT_virtuality:
13093 return "DW_AT_virtuality";
13094 case DW_AT_vtable_elem_location:
13095 return "DW_AT_vtable_elem_location";
13096 /* DWARF 3 values. */
13097 case DW_AT_allocated:
13098 return "DW_AT_allocated";
13099 case DW_AT_associated:
13100 return "DW_AT_associated";
13101 case DW_AT_data_location:
13102 return "DW_AT_data_location";
13103 case DW_AT_byte_stride:
13104 return "DW_AT_byte_stride";
13105 case DW_AT_entry_pc:
13106 return "DW_AT_entry_pc";
13107 case DW_AT_use_UTF8:
13108 return "DW_AT_use_UTF8";
13109 case DW_AT_extension:
13110 return "DW_AT_extension";
13111 case DW_AT_ranges:
13112 return "DW_AT_ranges";
13113 case DW_AT_trampoline:
13114 return "DW_AT_trampoline";
13115 case DW_AT_call_column:
13116 return "DW_AT_call_column";
13117 case DW_AT_call_file:
13118 return "DW_AT_call_file";
13119 case DW_AT_call_line:
13120 return "DW_AT_call_line";
13121 case DW_AT_description:
13122 return "DW_AT_description";
13123 case DW_AT_binary_scale:
13124 return "DW_AT_binary_scale";
13125 case DW_AT_decimal_scale:
13126 return "DW_AT_decimal_scale";
13127 case DW_AT_small:
13128 return "DW_AT_small";
13129 case DW_AT_decimal_sign:
13130 return "DW_AT_decimal_sign";
13131 case DW_AT_digit_count:
13132 return "DW_AT_digit_count";
13133 case DW_AT_picture_string:
13134 return "DW_AT_picture_string";
13135 case DW_AT_mutable:
13136 return "DW_AT_mutable";
13137 case DW_AT_threads_scaled:
13138 return "DW_AT_threads_scaled";
13139 case DW_AT_explicit:
13140 return "DW_AT_explicit";
13141 case DW_AT_object_pointer:
13142 return "DW_AT_object_pointer";
13143 case DW_AT_endianity:
13144 return "DW_AT_endianity";
13145 case DW_AT_elemental:
13146 return "DW_AT_elemental";
13147 case DW_AT_pure:
13148 return "DW_AT_pure";
13149 case DW_AT_recursive:
13150 return "DW_AT_recursive";
13151 /* DWARF 4 values. */
13152 case DW_AT_signature:
13153 return "DW_AT_signature";
13154 case DW_AT_linkage_name:
13155 return "DW_AT_linkage_name";
13156 /* SGI/MIPS extensions. */
13157 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13158 case DW_AT_MIPS_fde:
13159 return "DW_AT_MIPS_fde";
13160 #endif
13161 case DW_AT_MIPS_loop_begin:
13162 return "DW_AT_MIPS_loop_begin";
13163 case DW_AT_MIPS_tail_loop_begin:
13164 return "DW_AT_MIPS_tail_loop_begin";
13165 case DW_AT_MIPS_epilog_begin:
13166 return "DW_AT_MIPS_epilog_begin";
13167 case DW_AT_MIPS_loop_unroll_factor:
13168 return "DW_AT_MIPS_loop_unroll_factor";
13169 case DW_AT_MIPS_software_pipeline_depth:
13170 return "DW_AT_MIPS_software_pipeline_depth";
13171 case DW_AT_MIPS_linkage_name:
13172 return "DW_AT_MIPS_linkage_name";
13173 case DW_AT_MIPS_stride:
13174 return "DW_AT_MIPS_stride";
13175 case DW_AT_MIPS_abstract_name:
13176 return "DW_AT_MIPS_abstract_name";
13177 case DW_AT_MIPS_clone_origin:
13178 return "DW_AT_MIPS_clone_origin";
13179 case DW_AT_MIPS_has_inlines:
13180 return "DW_AT_MIPS_has_inlines";
13181 /* HP extensions. */
13182 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13183 case DW_AT_HP_block_index:
13184 return "DW_AT_HP_block_index";
13185 #endif
13186 case DW_AT_HP_unmodifiable:
13187 return "DW_AT_HP_unmodifiable";
13188 case DW_AT_HP_actuals_stmt_list:
13189 return "DW_AT_HP_actuals_stmt_list";
13190 case DW_AT_HP_proc_per_section:
13191 return "DW_AT_HP_proc_per_section";
13192 case DW_AT_HP_raw_data_ptr:
13193 return "DW_AT_HP_raw_data_ptr";
13194 case DW_AT_HP_pass_by_reference:
13195 return "DW_AT_HP_pass_by_reference";
13196 case DW_AT_HP_opt_level:
13197 return "DW_AT_HP_opt_level";
13198 case DW_AT_HP_prof_version_id:
13199 return "DW_AT_HP_prof_version_id";
13200 case DW_AT_HP_opt_flags:
13201 return "DW_AT_HP_opt_flags";
13202 case DW_AT_HP_cold_region_low_pc:
13203 return "DW_AT_HP_cold_region_low_pc";
13204 case DW_AT_HP_cold_region_high_pc:
13205 return "DW_AT_HP_cold_region_high_pc";
13206 case DW_AT_HP_all_variables_modifiable:
13207 return "DW_AT_HP_all_variables_modifiable";
13208 case DW_AT_HP_linkage_name:
13209 return "DW_AT_HP_linkage_name";
13210 case DW_AT_HP_prof_flags:
13211 return "DW_AT_HP_prof_flags";
13212 /* GNU extensions. */
13213 case DW_AT_sf_names:
13214 return "DW_AT_sf_names";
13215 case DW_AT_src_info:
13216 return "DW_AT_src_info";
13217 case DW_AT_mac_info:
13218 return "DW_AT_mac_info";
13219 case DW_AT_src_coords:
13220 return "DW_AT_src_coords";
13221 case DW_AT_body_begin:
13222 return "DW_AT_body_begin";
13223 case DW_AT_body_end:
13224 return "DW_AT_body_end";
13225 case DW_AT_GNU_vector:
13226 return "DW_AT_GNU_vector";
13227 case DW_AT_GNU_odr_signature:
13228 return "DW_AT_GNU_odr_signature";
13229 /* VMS extensions. */
13230 case DW_AT_VMS_rtnbeg_pd_address:
13231 return "DW_AT_VMS_rtnbeg_pd_address";
13232 /* UPC extension. */
13233 case DW_AT_upc_threads_scaled:
13234 return "DW_AT_upc_threads_scaled";
13235 /* PGI (STMicroelectronics) extensions. */
13236 case DW_AT_PGI_lbase:
13237 return "DW_AT_PGI_lbase";
13238 case DW_AT_PGI_soffset:
13239 return "DW_AT_PGI_soffset";
13240 case DW_AT_PGI_lstride:
13241 return "DW_AT_PGI_lstride";
13242 default:
13243 return "DW_AT_<unknown>";
13244 }
13245 }
13246
13247 /* Convert a DWARF value form code into its string name. */
13248
13249 static char *
13250 dwarf_form_name (unsigned form)
13251 {
13252 switch (form)
13253 {
13254 case DW_FORM_addr:
13255 return "DW_FORM_addr";
13256 case DW_FORM_block2:
13257 return "DW_FORM_block2";
13258 case DW_FORM_block4:
13259 return "DW_FORM_block4";
13260 case DW_FORM_data2:
13261 return "DW_FORM_data2";
13262 case DW_FORM_data4:
13263 return "DW_FORM_data4";
13264 case DW_FORM_data8:
13265 return "DW_FORM_data8";
13266 case DW_FORM_string:
13267 return "DW_FORM_string";
13268 case DW_FORM_block:
13269 return "DW_FORM_block";
13270 case DW_FORM_block1:
13271 return "DW_FORM_block1";
13272 case DW_FORM_data1:
13273 return "DW_FORM_data1";
13274 case DW_FORM_flag:
13275 return "DW_FORM_flag";
13276 case DW_FORM_sdata:
13277 return "DW_FORM_sdata";
13278 case DW_FORM_strp:
13279 return "DW_FORM_strp";
13280 case DW_FORM_udata:
13281 return "DW_FORM_udata";
13282 case DW_FORM_ref_addr:
13283 return "DW_FORM_ref_addr";
13284 case DW_FORM_ref1:
13285 return "DW_FORM_ref1";
13286 case DW_FORM_ref2:
13287 return "DW_FORM_ref2";
13288 case DW_FORM_ref4:
13289 return "DW_FORM_ref4";
13290 case DW_FORM_ref8:
13291 return "DW_FORM_ref8";
13292 case DW_FORM_ref_udata:
13293 return "DW_FORM_ref_udata";
13294 case DW_FORM_indirect:
13295 return "DW_FORM_indirect";
13296 case DW_FORM_sec_offset:
13297 return "DW_FORM_sec_offset";
13298 case DW_FORM_exprloc:
13299 return "DW_FORM_exprloc";
13300 case DW_FORM_flag_present:
13301 return "DW_FORM_flag_present";
13302 case DW_FORM_ref_sig8:
13303 return "DW_FORM_ref_sig8";
13304 default:
13305 return "DW_FORM_<unknown>";
13306 }
13307 }
13308
13309 /* Convert a DWARF stack opcode into its string name. */
13310
13311 const char *
13312 dwarf_stack_op_name (unsigned op)
13313 {
13314 switch (op)
13315 {
13316 case DW_OP_addr:
13317 return "DW_OP_addr";
13318 case DW_OP_deref:
13319 return "DW_OP_deref";
13320 case DW_OP_const1u:
13321 return "DW_OP_const1u";
13322 case DW_OP_const1s:
13323 return "DW_OP_const1s";
13324 case DW_OP_const2u:
13325 return "DW_OP_const2u";
13326 case DW_OP_const2s:
13327 return "DW_OP_const2s";
13328 case DW_OP_const4u:
13329 return "DW_OP_const4u";
13330 case DW_OP_const4s:
13331 return "DW_OP_const4s";
13332 case DW_OP_const8u:
13333 return "DW_OP_const8u";
13334 case DW_OP_const8s:
13335 return "DW_OP_const8s";
13336 case DW_OP_constu:
13337 return "DW_OP_constu";
13338 case DW_OP_consts:
13339 return "DW_OP_consts";
13340 case DW_OP_dup:
13341 return "DW_OP_dup";
13342 case DW_OP_drop:
13343 return "DW_OP_drop";
13344 case DW_OP_over:
13345 return "DW_OP_over";
13346 case DW_OP_pick:
13347 return "DW_OP_pick";
13348 case DW_OP_swap:
13349 return "DW_OP_swap";
13350 case DW_OP_rot:
13351 return "DW_OP_rot";
13352 case DW_OP_xderef:
13353 return "DW_OP_xderef";
13354 case DW_OP_abs:
13355 return "DW_OP_abs";
13356 case DW_OP_and:
13357 return "DW_OP_and";
13358 case DW_OP_div:
13359 return "DW_OP_div";
13360 case DW_OP_minus:
13361 return "DW_OP_minus";
13362 case DW_OP_mod:
13363 return "DW_OP_mod";
13364 case DW_OP_mul:
13365 return "DW_OP_mul";
13366 case DW_OP_neg:
13367 return "DW_OP_neg";
13368 case DW_OP_not:
13369 return "DW_OP_not";
13370 case DW_OP_or:
13371 return "DW_OP_or";
13372 case DW_OP_plus:
13373 return "DW_OP_plus";
13374 case DW_OP_plus_uconst:
13375 return "DW_OP_plus_uconst";
13376 case DW_OP_shl:
13377 return "DW_OP_shl";
13378 case DW_OP_shr:
13379 return "DW_OP_shr";
13380 case DW_OP_shra:
13381 return "DW_OP_shra";
13382 case DW_OP_xor:
13383 return "DW_OP_xor";
13384 case DW_OP_bra:
13385 return "DW_OP_bra";
13386 case DW_OP_eq:
13387 return "DW_OP_eq";
13388 case DW_OP_ge:
13389 return "DW_OP_ge";
13390 case DW_OP_gt:
13391 return "DW_OP_gt";
13392 case DW_OP_le:
13393 return "DW_OP_le";
13394 case DW_OP_lt:
13395 return "DW_OP_lt";
13396 case DW_OP_ne:
13397 return "DW_OP_ne";
13398 case DW_OP_skip:
13399 return "DW_OP_skip";
13400 case DW_OP_lit0:
13401 return "DW_OP_lit0";
13402 case DW_OP_lit1:
13403 return "DW_OP_lit1";
13404 case DW_OP_lit2:
13405 return "DW_OP_lit2";
13406 case DW_OP_lit3:
13407 return "DW_OP_lit3";
13408 case DW_OP_lit4:
13409 return "DW_OP_lit4";
13410 case DW_OP_lit5:
13411 return "DW_OP_lit5";
13412 case DW_OP_lit6:
13413 return "DW_OP_lit6";
13414 case DW_OP_lit7:
13415 return "DW_OP_lit7";
13416 case DW_OP_lit8:
13417 return "DW_OP_lit8";
13418 case DW_OP_lit9:
13419 return "DW_OP_lit9";
13420 case DW_OP_lit10:
13421 return "DW_OP_lit10";
13422 case DW_OP_lit11:
13423 return "DW_OP_lit11";
13424 case DW_OP_lit12:
13425 return "DW_OP_lit12";
13426 case DW_OP_lit13:
13427 return "DW_OP_lit13";
13428 case DW_OP_lit14:
13429 return "DW_OP_lit14";
13430 case DW_OP_lit15:
13431 return "DW_OP_lit15";
13432 case DW_OP_lit16:
13433 return "DW_OP_lit16";
13434 case DW_OP_lit17:
13435 return "DW_OP_lit17";
13436 case DW_OP_lit18:
13437 return "DW_OP_lit18";
13438 case DW_OP_lit19:
13439 return "DW_OP_lit19";
13440 case DW_OP_lit20:
13441 return "DW_OP_lit20";
13442 case DW_OP_lit21:
13443 return "DW_OP_lit21";
13444 case DW_OP_lit22:
13445 return "DW_OP_lit22";
13446 case DW_OP_lit23:
13447 return "DW_OP_lit23";
13448 case DW_OP_lit24:
13449 return "DW_OP_lit24";
13450 case DW_OP_lit25:
13451 return "DW_OP_lit25";
13452 case DW_OP_lit26:
13453 return "DW_OP_lit26";
13454 case DW_OP_lit27:
13455 return "DW_OP_lit27";
13456 case DW_OP_lit28:
13457 return "DW_OP_lit28";
13458 case DW_OP_lit29:
13459 return "DW_OP_lit29";
13460 case DW_OP_lit30:
13461 return "DW_OP_lit30";
13462 case DW_OP_lit31:
13463 return "DW_OP_lit31";
13464 case DW_OP_reg0:
13465 return "DW_OP_reg0";
13466 case DW_OP_reg1:
13467 return "DW_OP_reg1";
13468 case DW_OP_reg2:
13469 return "DW_OP_reg2";
13470 case DW_OP_reg3:
13471 return "DW_OP_reg3";
13472 case DW_OP_reg4:
13473 return "DW_OP_reg4";
13474 case DW_OP_reg5:
13475 return "DW_OP_reg5";
13476 case DW_OP_reg6:
13477 return "DW_OP_reg6";
13478 case DW_OP_reg7:
13479 return "DW_OP_reg7";
13480 case DW_OP_reg8:
13481 return "DW_OP_reg8";
13482 case DW_OP_reg9:
13483 return "DW_OP_reg9";
13484 case DW_OP_reg10:
13485 return "DW_OP_reg10";
13486 case DW_OP_reg11:
13487 return "DW_OP_reg11";
13488 case DW_OP_reg12:
13489 return "DW_OP_reg12";
13490 case DW_OP_reg13:
13491 return "DW_OP_reg13";
13492 case DW_OP_reg14:
13493 return "DW_OP_reg14";
13494 case DW_OP_reg15:
13495 return "DW_OP_reg15";
13496 case DW_OP_reg16:
13497 return "DW_OP_reg16";
13498 case DW_OP_reg17:
13499 return "DW_OP_reg17";
13500 case DW_OP_reg18:
13501 return "DW_OP_reg18";
13502 case DW_OP_reg19:
13503 return "DW_OP_reg19";
13504 case DW_OP_reg20:
13505 return "DW_OP_reg20";
13506 case DW_OP_reg21:
13507 return "DW_OP_reg21";
13508 case DW_OP_reg22:
13509 return "DW_OP_reg22";
13510 case DW_OP_reg23:
13511 return "DW_OP_reg23";
13512 case DW_OP_reg24:
13513 return "DW_OP_reg24";
13514 case DW_OP_reg25:
13515 return "DW_OP_reg25";
13516 case DW_OP_reg26:
13517 return "DW_OP_reg26";
13518 case DW_OP_reg27:
13519 return "DW_OP_reg27";
13520 case DW_OP_reg28:
13521 return "DW_OP_reg28";
13522 case DW_OP_reg29:
13523 return "DW_OP_reg29";
13524 case DW_OP_reg30:
13525 return "DW_OP_reg30";
13526 case DW_OP_reg31:
13527 return "DW_OP_reg31";
13528 case DW_OP_breg0:
13529 return "DW_OP_breg0";
13530 case DW_OP_breg1:
13531 return "DW_OP_breg1";
13532 case DW_OP_breg2:
13533 return "DW_OP_breg2";
13534 case DW_OP_breg3:
13535 return "DW_OP_breg3";
13536 case DW_OP_breg4:
13537 return "DW_OP_breg4";
13538 case DW_OP_breg5:
13539 return "DW_OP_breg5";
13540 case DW_OP_breg6:
13541 return "DW_OP_breg6";
13542 case DW_OP_breg7:
13543 return "DW_OP_breg7";
13544 case DW_OP_breg8:
13545 return "DW_OP_breg8";
13546 case DW_OP_breg9:
13547 return "DW_OP_breg9";
13548 case DW_OP_breg10:
13549 return "DW_OP_breg10";
13550 case DW_OP_breg11:
13551 return "DW_OP_breg11";
13552 case DW_OP_breg12:
13553 return "DW_OP_breg12";
13554 case DW_OP_breg13:
13555 return "DW_OP_breg13";
13556 case DW_OP_breg14:
13557 return "DW_OP_breg14";
13558 case DW_OP_breg15:
13559 return "DW_OP_breg15";
13560 case DW_OP_breg16:
13561 return "DW_OP_breg16";
13562 case DW_OP_breg17:
13563 return "DW_OP_breg17";
13564 case DW_OP_breg18:
13565 return "DW_OP_breg18";
13566 case DW_OP_breg19:
13567 return "DW_OP_breg19";
13568 case DW_OP_breg20:
13569 return "DW_OP_breg20";
13570 case DW_OP_breg21:
13571 return "DW_OP_breg21";
13572 case DW_OP_breg22:
13573 return "DW_OP_breg22";
13574 case DW_OP_breg23:
13575 return "DW_OP_breg23";
13576 case DW_OP_breg24:
13577 return "DW_OP_breg24";
13578 case DW_OP_breg25:
13579 return "DW_OP_breg25";
13580 case DW_OP_breg26:
13581 return "DW_OP_breg26";
13582 case DW_OP_breg27:
13583 return "DW_OP_breg27";
13584 case DW_OP_breg28:
13585 return "DW_OP_breg28";
13586 case DW_OP_breg29:
13587 return "DW_OP_breg29";
13588 case DW_OP_breg30:
13589 return "DW_OP_breg30";
13590 case DW_OP_breg31:
13591 return "DW_OP_breg31";
13592 case DW_OP_regx:
13593 return "DW_OP_regx";
13594 case DW_OP_fbreg:
13595 return "DW_OP_fbreg";
13596 case DW_OP_bregx:
13597 return "DW_OP_bregx";
13598 case DW_OP_piece:
13599 return "DW_OP_piece";
13600 case DW_OP_deref_size:
13601 return "DW_OP_deref_size";
13602 case DW_OP_xderef_size:
13603 return "DW_OP_xderef_size";
13604 case DW_OP_nop:
13605 return "DW_OP_nop";
13606 /* DWARF 3 extensions. */
13607 case DW_OP_push_object_address:
13608 return "DW_OP_push_object_address";
13609 case DW_OP_call2:
13610 return "DW_OP_call2";
13611 case DW_OP_call4:
13612 return "DW_OP_call4";
13613 case DW_OP_call_ref:
13614 return "DW_OP_call_ref";
13615 case DW_OP_form_tls_address:
13616 return "DW_OP_form_tls_address";
13617 case DW_OP_call_frame_cfa:
13618 return "DW_OP_call_frame_cfa";
13619 case DW_OP_bit_piece:
13620 return "DW_OP_bit_piece";
13621 /* DWARF 4 extensions. */
13622 case DW_OP_implicit_value:
13623 return "DW_OP_implicit_value";
13624 case DW_OP_stack_value:
13625 return "DW_OP_stack_value";
13626 /* GNU extensions. */
13627 case DW_OP_GNU_push_tls_address:
13628 return "DW_OP_GNU_push_tls_address";
13629 case DW_OP_GNU_uninit:
13630 return "DW_OP_GNU_uninit";
13631 case DW_OP_GNU_implicit_pointer:
13632 return "DW_OP_GNU_implicit_pointer";
13633 case DW_OP_GNU_entry_value:
13634 return "DW_OP_GNU_entry_value";
13635 case DW_OP_GNU_const_type:
13636 return "DW_OP_GNU_const_type";
13637 case DW_OP_GNU_regval_type:
13638 return "DW_OP_GNU_regval_type";
13639 case DW_OP_GNU_deref_type:
13640 return "DW_OP_GNU_deref_type";
13641 case DW_OP_GNU_convert:
13642 return "DW_OP_GNU_convert";
13643 case DW_OP_GNU_reinterpret:
13644 return "DW_OP_GNU_reinterpret";
13645 default:
13646 return NULL;
13647 }
13648 }
13649
13650 static char *
13651 dwarf_bool_name (unsigned mybool)
13652 {
13653 if (mybool)
13654 return "TRUE";
13655 else
13656 return "FALSE";
13657 }
13658
13659 /* Convert a DWARF type code into its string name. */
13660
13661 static char *
13662 dwarf_type_encoding_name (unsigned enc)
13663 {
13664 switch (enc)
13665 {
13666 case DW_ATE_void:
13667 return "DW_ATE_void";
13668 case DW_ATE_address:
13669 return "DW_ATE_address";
13670 case DW_ATE_boolean:
13671 return "DW_ATE_boolean";
13672 case DW_ATE_complex_float:
13673 return "DW_ATE_complex_float";
13674 case DW_ATE_float:
13675 return "DW_ATE_float";
13676 case DW_ATE_signed:
13677 return "DW_ATE_signed";
13678 case DW_ATE_signed_char:
13679 return "DW_ATE_signed_char";
13680 case DW_ATE_unsigned:
13681 return "DW_ATE_unsigned";
13682 case DW_ATE_unsigned_char:
13683 return "DW_ATE_unsigned_char";
13684 /* DWARF 3. */
13685 case DW_ATE_imaginary_float:
13686 return "DW_ATE_imaginary_float";
13687 case DW_ATE_packed_decimal:
13688 return "DW_ATE_packed_decimal";
13689 case DW_ATE_numeric_string:
13690 return "DW_ATE_numeric_string";
13691 case DW_ATE_edited:
13692 return "DW_ATE_edited";
13693 case DW_ATE_signed_fixed:
13694 return "DW_ATE_signed_fixed";
13695 case DW_ATE_unsigned_fixed:
13696 return "DW_ATE_unsigned_fixed";
13697 case DW_ATE_decimal_float:
13698 return "DW_ATE_decimal_float";
13699 /* DWARF 4. */
13700 case DW_ATE_UTF:
13701 return "DW_ATE_UTF";
13702 /* HP extensions. */
13703 case DW_ATE_HP_float80:
13704 return "DW_ATE_HP_float80";
13705 case DW_ATE_HP_complex_float80:
13706 return "DW_ATE_HP_complex_float80";
13707 case DW_ATE_HP_float128:
13708 return "DW_ATE_HP_float128";
13709 case DW_ATE_HP_complex_float128:
13710 return "DW_ATE_HP_complex_float128";
13711 case DW_ATE_HP_floathpintel:
13712 return "DW_ATE_HP_floathpintel";
13713 case DW_ATE_HP_imaginary_float80:
13714 return "DW_ATE_HP_imaginary_float80";
13715 case DW_ATE_HP_imaginary_float128:
13716 return "DW_ATE_HP_imaginary_float128";
13717 default:
13718 return "DW_ATE_<unknown>";
13719 }
13720 }
13721
13722 /* Convert a DWARF call frame info operation to its string name. */
13723
13724 #if 0
13725 static char *
13726 dwarf_cfi_name (unsigned cfi_opc)
13727 {
13728 switch (cfi_opc)
13729 {
13730 case DW_CFA_advance_loc:
13731 return "DW_CFA_advance_loc";
13732 case DW_CFA_offset:
13733 return "DW_CFA_offset";
13734 case DW_CFA_restore:
13735 return "DW_CFA_restore";
13736 case DW_CFA_nop:
13737 return "DW_CFA_nop";
13738 case DW_CFA_set_loc:
13739 return "DW_CFA_set_loc";
13740 case DW_CFA_advance_loc1:
13741 return "DW_CFA_advance_loc1";
13742 case DW_CFA_advance_loc2:
13743 return "DW_CFA_advance_loc2";
13744 case DW_CFA_advance_loc4:
13745 return "DW_CFA_advance_loc4";
13746 case DW_CFA_offset_extended:
13747 return "DW_CFA_offset_extended";
13748 case DW_CFA_restore_extended:
13749 return "DW_CFA_restore_extended";
13750 case DW_CFA_undefined:
13751 return "DW_CFA_undefined";
13752 case DW_CFA_same_value:
13753 return "DW_CFA_same_value";
13754 case DW_CFA_register:
13755 return "DW_CFA_register";
13756 case DW_CFA_remember_state:
13757 return "DW_CFA_remember_state";
13758 case DW_CFA_restore_state:
13759 return "DW_CFA_restore_state";
13760 case DW_CFA_def_cfa:
13761 return "DW_CFA_def_cfa";
13762 case DW_CFA_def_cfa_register:
13763 return "DW_CFA_def_cfa_register";
13764 case DW_CFA_def_cfa_offset:
13765 return "DW_CFA_def_cfa_offset";
13766 /* DWARF 3. */
13767 case DW_CFA_def_cfa_expression:
13768 return "DW_CFA_def_cfa_expression";
13769 case DW_CFA_expression:
13770 return "DW_CFA_expression";
13771 case DW_CFA_offset_extended_sf:
13772 return "DW_CFA_offset_extended_sf";
13773 case DW_CFA_def_cfa_sf:
13774 return "DW_CFA_def_cfa_sf";
13775 case DW_CFA_def_cfa_offset_sf:
13776 return "DW_CFA_def_cfa_offset_sf";
13777 case DW_CFA_val_offset:
13778 return "DW_CFA_val_offset";
13779 case DW_CFA_val_offset_sf:
13780 return "DW_CFA_val_offset_sf";
13781 case DW_CFA_val_expression:
13782 return "DW_CFA_val_expression";
13783 /* SGI/MIPS specific. */
13784 case DW_CFA_MIPS_advance_loc8:
13785 return "DW_CFA_MIPS_advance_loc8";
13786 /* GNU extensions. */
13787 case DW_CFA_GNU_window_save:
13788 return "DW_CFA_GNU_window_save";
13789 case DW_CFA_GNU_args_size:
13790 return "DW_CFA_GNU_args_size";
13791 case DW_CFA_GNU_negative_offset_extended:
13792 return "DW_CFA_GNU_negative_offset_extended";
13793 default:
13794 return "DW_CFA_<unknown>";
13795 }
13796 }
13797 #endif
13798
13799 static void
13800 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13801 {
13802 unsigned int i;
13803
13804 print_spaces (indent, f);
13805 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13806 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13807
13808 if (die->parent != NULL)
13809 {
13810 print_spaces (indent, f);
13811 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13812 die->parent->offset);
13813 }
13814
13815 print_spaces (indent, f);
13816 fprintf_unfiltered (f, " has children: %s\n",
13817 dwarf_bool_name (die->child != NULL));
13818
13819 print_spaces (indent, f);
13820 fprintf_unfiltered (f, " attributes:\n");
13821
13822 for (i = 0; i < die->num_attrs; ++i)
13823 {
13824 print_spaces (indent, f);
13825 fprintf_unfiltered (f, " %s (%s) ",
13826 dwarf_attr_name (die->attrs[i].name),
13827 dwarf_form_name (die->attrs[i].form));
13828
13829 switch (die->attrs[i].form)
13830 {
13831 case DW_FORM_ref_addr:
13832 case DW_FORM_addr:
13833 fprintf_unfiltered (f, "address: ");
13834 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13835 break;
13836 case DW_FORM_block2:
13837 case DW_FORM_block4:
13838 case DW_FORM_block:
13839 case DW_FORM_block1:
13840 fprintf_unfiltered (f, "block: size %d",
13841 DW_BLOCK (&die->attrs[i])->size);
13842 break;
13843 case DW_FORM_exprloc:
13844 fprintf_unfiltered (f, "expression: size %u",
13845 DW_BLOCK (&die->attrs[i])->size);
13846 break;
13847 case DW_FORM_ref1:
13848 case DW_FORM_ref2:
13849 case DW_FORM_ref4:
13850 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13851 (long) (DW_ADDR (&die->attrs[i])));
13852 break;
13853 case DW_FORM_data1:
13854 case DW_FORM_data2:
13855 case DW_FORM_data4:
13856 case DW_FORM_data8:
13857 case DW_FORM_udata:
13858 case DW_FORM_sdata:
13859 fprintf_unfiltered (f, "constant: %s",
13860 pulongest (DW_UNSND (&die->attrs[i])));
13861 break;
13862 case DW_FORM_sec_offset:
13863 fprintf_unfiltered (f, "section offset: %s",
13864 pulongest (DW_UNSND (&die->attrs[i])));
13865 break;
13866 case DW_FORM_ref_sig8:
13867 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13868 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13869 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13870 else
13871 fprintf_unfiltered (f, "signatured type, offset: unknown");
13872 break;
13873 case DW_FORM_string:
13874 case DW_FORM_strp:
13875 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13876 DW_STRING (&die->attrs[i])
13877 ? DW_STRING (&die->attrs[i]) : "",
13878 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13879 break;
13880 case DW_FORM_flag:
13881 if (DW_UNSND (&die->attrs[i]))
13882 fprintf_unfiltered (f, "flag: TRUE");
13883 else
13884 fprintf_unfiltered (f, "flag: FALSE");
13885 break;
13886 case DW_FORM_flag_present:
13887 fprintf_unfiltered (f, "flag: TRUE");
13888 break;
13889 case DW_FORM_indirect:
13890 /* The reader will have reduced the indirect form to
13891 the "base form" so this form should not occur. */
13892 fprintf_unfiltered (f,
13893 "unexpected attribute form: DW_FORM_indirect");
13894 break;
13895 default:
13896 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13897 die->attrs[i].form);
13898 break;
13899 }
13900 fprintf_unfiltered (f, "\n");
13901 }
13902 }
13903
13904 static void
13905 dump_die_for_error (struct die_info *die)
13906 {
13907 dump_die_shallow (gdb_stderr, 0, die);
13908 }
13909
13910 static void
13911 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13912 {
13913 int indent = level * 4;
13914
13915 gdb_assert (die != NULL);
13916
13917 if (level >= max_level)
13918 return;
13919
13920 dump_die_shallow (f, indent, die);
13921
13922 if (die->child != NULL)
13923 {
13924 print_spaces (indent, f);
13925 fprintf_unfiltered (f, " Children:");
13926 if (level + 1 < max_level)
13927 {
13928 fprintf_unfiltered (f, "\n");
13929 dump_die_1 (f, level + 1, max_level, die->child);
13930 }
13931 else
13932 {
13933 fprintf_unfiltered (f,
13934 " [not printed, max nesting level reached]\n");
13935 }
13936 }
13937
13938 if (die->sibling != NULL && level > 0)
13939 {
13940 dump_die_1 (f, level, max_level, die->sibling);
13941 }
13942 }
13943
13944 /* This is called from the pdie macro in gdbinit.in.
13945 It's not static so gcc will keep a copy callable from gdb. */
13946
13947 void
13948 dump_die (struct die_info *die, int max_level)
13949 {
13950 dump_die_1 (gdb_stdlog, 0, max_level, die);
13951 }
13952
13953 static void
13954 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13955 {
13956 void **slot;
13957
13958 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13959
13960 *slot = die;
13961 }
13962
13963 static int
13964 is_ref_attr (struct attribute *attr)
13965 {
13966 switch (attr->form)
13967 {
13968 case DW_FORM_ref_addr:
13969 case DW_FORM_ref1:
13970 case DW_FORM_ref2:
13971 case DW_FORM_ref4:
13972 case DW_FORM_ref8:
13973 case DW_FORM_ref_udata:
13974 return 1;
13975 default:
13976 return 0;
13977 }
13978 }
13979
13980 static unsigned int
13981 dwarf2_get_ref_die_offset (struct attribute *attr)
13982 {
13983 if (is_ref_attr (attr))
13984 return DW_ADDR (attr);
13985
13986 complaint (&symfile_complaints,
13987 _("unsupported die ref attribute form: '%s'"),
13988 dwarf_form_name (attr->form));
13989 return 0;
13990 }
13991
13992 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13993 * the value held by the attribute is not constant. */
13994
13995 static LONGEST
13996 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13997 {
13998 if (attr->form == DW_FORM_sdata)
13999 return DW_SND (attr);
14000 else if (attr->form == DW_FORM_udata
14001 || attr->form == DW_FORM_data1
14002 || attr->form == DW_FORM_data2
14003 || attr->form == DW_FORM_data4
14004 || attr->form == DW_FORM_data8)
14005 return DW_UNSND (attr);
14006 else
14007 {
14008 complaint (&symfile_complaints,
14009 _("Attribute value is not a constant (%s)"),
14010 dwarf_form_name (attr->form));
14011 return default_value;
14012 }
14013 }
14014
14015 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14016 unit and add it to our queue.
14017 The result is non-zero if PER_CU was queued, otherwise the result is zero
14018 meaning either PER_CU is already queued or it is already loaded. */
14019
14020 static int
14021 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14022 struct dwarf2_per_cu_data *per_cu)
14023 {
14024 /* We may arrive here during partial symbol reading, if we need full
14025 DIEs to process an unusual case (e.g. template arguments). Do
14026 not queue PER_CU, just tell our caller to load its DIEs. */
14027 if (dwarf2_per_objfile->reading_partial_symbols)
14028 {
14029 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14030 return 1;
14031 return 0;
14032 }
14033
14034 /* Mark the dependence relation so that we don't flush PER_CU
14035 too early. */
14036 dwarf2_add_dependence (this_cu, per_cu);
14037
14038 /* If it's already on the queue, we have nothing to do. */
14039 if (per_cu->queued)
14040 return 0;
14041
14042 /* If the compilation unit is already loaded, just mark it as
14043 used. */
14044 if (per_cu->cu != NULL)
14045 {
14046 per_cu->cu->last_used = 0;
14047 return 0;
14048 }
14049
14050 /* Add it to the queue. */
14051 queue_comp_unit (per_cu);
14052
14053 return 1;
14054 }
14055
14056 /* Follow reference or signature attribute ATTR of SRC_DIE.
14057 On entry *REF_CU is the CU of SRC_DIE.
14058 On exit *REF_CU is the CU of the result. */
14059
14060 static struct die_info *
14061 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14062 struct dwarf2_cu **ref_cu)
14063 {
14064 struct die_info *die;
14065
14066 if (is_ref_attr (attr))
14067 die = follow_die_ref (src_die, attr, ref_cu);
14068 else if (attr->form == DW_FORM_ref_sig8)
14069 die = follow_die_sig (src_die, attr, ref_cu);
14070 else
14071 {
14072 dump_die_for_error (src_die);
14073 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14074 (*ref_cu)->objfile->name);
14075 }
14076
14077 return die;
14078 }
14079
14080 /* Follow reference OFFSET.
14081 On entry *REF_CU is the CU of the source die referencing OFFSET.
14082 On exit *REF_CU is the CU of the result.
14083 Returns NULL if OFFSET is invalid. */
14084
14085 static struct die_info *
14086 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14087 {
14088 struct die_info temp_die;
14089 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14090
14091 gdb_assert (cu->per_cu != NULL);
14092
14093 target_cu = cu;
14094
14095 if (cu->per_cu->debug_types_section)
14096 {
14097 /* .debug_types CUs cannot reference anything outside their CU.
14098 If they need to, they have to reference a signatured type via
14099 DW_FORM_ref_sig8. */
14100 if (! offset_in_cu_p (&cu->header, offset))
14101 return NULL;
14102 }
14103 else if (! offset_in_cu_p (&cu->header, offset))
14104 {
14105 struct dwarf2_per_cu_data *per_cu;
14106
14107 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14108
14109 /* If necessary, add it to the queue and load its DIEs. */
14110 if (maybe_queue_comp_unit (cu, per_cu))
14111 load_full_comp_unit (per_cu);
14112
14113 target_cu = per_cu->cu;
14114 }
14115 else if (cu->dies == NULL)
14116 {
14117 /* We're loading full DIEs during partial symbol reading. */
14118 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14119 load_full_comp_unit (cu->per_cu);
14120 }
14121
14122 *ref_cu = target_cu;
14123 temp_die.offset = offset;
14124 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14125 }
14126
14127 /* Follow reference attribute ATTR of SRC_DIE.
14128 On entry *REF_CU is the CU of SRC_DIE.
14129 On exit *REF_CU is the CU of the result. */
14130
14131 static struct die_info *
14132 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14133 struct dwarf2_cu **ref_cu)
14134 {
14135 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14136 struct dwarf2_cu *cu = *ref_cu;
14137 struct die_info *die;
14138
14139 die = follow_die_offset (offset, ref_cu);
14140 if (!die)
14141 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14142 "at 0x%x [in module %s]"),
14143 offset, src_die->offset, cu->objfile->name);
14144
14145 return die;
14146 }
14147
14148 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14149 Returned value is intended for DW_OP_call*. Returned
14150 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14151
14152 struct dwarf2_locexpr_baton
14153 dwarf2_fetch_die_location_block (unsigned int offset,
14154 struct dwarf2_per_cu_data *per_cu,
14155 CORE_ADDR (*get_frame_pc) (void *baton),
14156 void *baton)
14157 {
14158 struct dwarf2_cu *cu;
14159 struct die_info *die;
14160 struct attribute *attr;
14161 struct dwarf2_locexpr_baton retval;
14162
14163 dw2_setup (per_cu->objfile);
14164
14165 if (per_cu->cu == NULL)
14166 load_cu (per_cu);
14167 cu = per_cu->cu;
14168
14169 die = follow_die_offset (offset, &cu);
14170 if (!die)
14171 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14172 offset, per_cu->objfile->name);
14173
14174 attr = dwarf2_attr (die, DW_AT_location, cu);
14175 if (!attr)
14176 {
14177 /* DWARF: "If there is no such attribute, then there is no effect.".
14178 DATA is ignored if SIZE is 0. */
14179
14180 retval.data = NULL;
14181 retval.size = 0;
14182 }
14183 else if (attr_form_is_section_offset (attr))
14184 {
14185 struct dwarf2_loclist_baton loclist_baton;
14186 CORE_ADDR pc = (*get_frame_pc) (baton);
14187 size_t size;
14188
14189 fill_in_loclist_baton (cu, &loclist_baton, attr);
14190
14191 retval.data = dwarf2_find_location_expression (&loclist_baton,
14192 &size, pc);
14193 retval.size = size;
14194 }
14195 else
14196 {
14197 if (!attr_form_is_block (attr))
14198 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14199 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14200 offset, per_cu->objfile->name);
14201
14202 retval.data = DW_BLOCK (attr)->data;
14203 retval.size = DW_BLOCK (attr)->size;
14204 }
14205 retval.per_cu = cu->per_cu;
14206
14207 age_cached_comp_units ();
14208
14209 return retval;
14210 }
14211
14212 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14213 PER_CU. */
14214
14215 struct type *
14216 dwarf2_get_die_type (unsigned int die_offset,
14217 struct dwarf2_per_cu_data *per_cu)
14218 {
14219 dw2_setup (per_cu->objfile);
14220 return get_die_type_at_offset (die_offset, per_cu);
14221 }
14222
14223 /* Follow the signature attribute ATTR in SRC_DIE.
14224 On entry *REF_CU is the CU of SRC_DIE.
14225 On exit *REF_CU is the CU of the result. */
14226
14227 static struct die_info *
14228 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14229 struct dwarf2_cu **ref_cu)
14230 {
14231 struct objfile *objfile = (*ref_cu)->objfile;
14232 struct die_info temp_die;
14233 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14234 struct dwarf2_cu *sig_cu;
14235 struct die_info *die;
14236
14237 /* sig_type will be NULL if the signatured type is missing from
14238 the debug info. */
14239 if (sig_type == NULL)
14240 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14241 "at 0x%x [in module %s]"),
14242 src_die->offset, objfile->name);
14243
14244 /* If necessary, add it to the queue and load its DIEs. */
14245
14246 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14247 read_signatured_type (sig_type);
14248
14249 gdb_assert (sig_type->per_cu.cu != NULL);
14250
14251 sig_cu = sig_type->per_cu.cu;
14252 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14253 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14254 if (die)
14255 {
14256 *ref_cu = sig_cu;
14257 return die;
14258 }
14259
14260 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14261 "from DIE at 0x%x [in module %s]"),
14262 sig_type->type_offset, src_die->offset, objfile->name);
14263 }
14264
14265 /* Given an offset of a signatured type, return its signatured_type. */
14266
14267 static struct signatured_type *
14268 lookup_signatured_type_at_offset (struct objfile *objfile,
14269 struct dwarf2_section_info *section,
14270 unsigned int offset)
14271 {
14272 gdb_byte *info_ptr = section->buffer + offset;
14273 unsigned int length, initial_length_size;
14274 unsigned int sig_offset;
14275 struct signatured_type find_entry, *type_sig;
14276
14277 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14278 sig_offset = (initial_length_size
14279 + 2 /*version*/
14280 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14281 + 1 /*address_size*/);
14282 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14283 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14284
14285 /* This is only used to lookup previously recorded types.
14286 If we didn't find it, it's our bug. */
14287 gdb_assert (type_sig != NULL);
14288 gdb_assert (offset == type_sig->per_cu.offset);
14289
14290 return type_sig;
14291 }
14292
14293 /* Load the DIEs associated with type unit PER_CU into memory. */
14294
14295 static void
14296 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14297 {
14298 struct objfile *objfile = per_cu->objfile;
14299 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14300 unsigned int offset = per_cu->offset;
14301 struct signatured_type *type_sig;
14302
14303 dwarf2_read_section (objfile, sect);
14304
14305 /* We have the section offset, but we need the signature to do the
14306 hash table lookup. */
14307 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14308 the signature to assert we found the right one.
14309 Ok, but it's a lot of work. We should simplify things so any needed
14310 assert doesn't require all this clumsiness. */
14311 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14312
14313 gdb_assert (type_sig->per_cu.cu == NULL);
14314
14315 read_signatured_type (type_sig);
14316
14317 gdb_assert (type_sig->per_cu.cu != NULL);
14318 }
14319
14320 /* Read in a signatured type and build its CU and DIEs. */
14321
14322 static void
14323 read_signatured_type (struct signatured_type *type_sig)
14324 {
14325 struct objfile *objfile = type_sig->per_cu.objfile;
14326 gdb_byte *types_ptr;
14327 struct die_reader_specs reader_specs;
14328 struct dwarf2_cu *cu;
14329 ULONGEST signature;
14330 struct cleanup *back_to, *free_cu_cleanup;
14331 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14332
14333 dwarf2_read_section (objfile, section);
14334 types_ptr = section->buffer + type_sig->per_cu.offset;
14335
14336 gdb_assert (type_sig->per_cu.cu == NULL);
14337
14338 cu = xmalloc (sizeof (*cu));
14339 init_one_comp_unit (cu, &type_sig->per_cu);
14340
14341 /* If an error occurs while loading, release our storage. */
14342 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14343
14344 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14345 types_ptr, objfile->obfd);
14346 gdb_assert (signature == type_sig->signature);
14347
14348 cu->die_hash
14349 = htab_create_alloc_ex (cu->header.length / 12,
14350 die_hash,
14351 die_eq,
14352 NULL,
14353 &cu->comp_unit_obstack,
14354 hashtab_obstack_allocate,
14355 dummy_obstack_deallocate);
14356
14357 dwarf2_read_abbrevs (cu);
14358 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14359
14360 init_cu_die_reader (&reader_specs, cu);
14361
14362 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14363 NULL /*parent*/);
14364
14365 /* We try not to read any attributes in this function, because not
14366 all CUs needed for references have been loaded yet, and symbol
14367 table processing isn't initialized. But we have to set the CU language,
14368 or we won't be able to build types correctly. */
14369 prepare_one_comp_unit (cu, cu->dies);
14370
14371 do_cleanups (back_to);
14372
14373 /* We've successfully allocated this compilation unit. Let our caller
14374 clean it up when finished with it. */
14375 discard_cleanups (free_cu_cleanup);
14376
14377 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14378 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14379 }
14380
14381 /* Decode simple location descriptions.
14382 Given a pointer to a dwarf block that defines a location, compute
14383 the location and return the value.
14384
14385 NOTE drow/2003-11-18: This function is called in two situations
14386 now: for the address of static or global variables (partial symbols
14387 only) and for offsets into structures which are expected to be
14388 (more or less) constant. The partial symbol case should go away,
14389 and only the constant case should remain. That will let this
14390 function complain more accurately. A few special modes are allowed
14391 without complaint for global variables (for instance, global
14392 register values and thread-local values).
14393
14394 A location description containing no operations indicates that the
14395 object is optimized out. The return value is 0 for that case.
14396 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14397 callers will only want a very basic result and this can become a
14398 complaint.
14399
14400 Note that stack[0] is unused except as a default error return. */
14401
14402 static CORE_ADDR
14403 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14404 {
14405 struct objfile *objfile = cu->objfile;
14406 int i;
14407 int size = blk->size;
14408 gdb_byte *data = blk->data;
14409 CORE_ADDR stack[64];
14410 int stacki;
14411 unsigned int bytes_read, unsnd;
14412 gdb_byte op;
14413
14414 i = 0;
14415 stacki = 0;
14416 stack[stacki] = 0;
14417 stack[++stacki] = 0;
14418
14419 while (i < size)
14420 {
14421 op = data[i++];
14422 switch (op)
14423 {
14424 case DW_OP_lit0:
14425 case DW_OP_lit1:
14426 case DW_OP_lit2:
14427 case DW_OP_lit3:
14428 case DW_OP_lit4:
14429 case DW_OP_lit5:
14430 case DW_OP_lit6:
14431 case DW_OP_lit7:
14432 case DW_OP_lit8:
14433 case DW_OP_lit9:
14434 case DW_OP_lit10:
14435 case DW_OP_lit11:
14436 case DW_OP_lit12:
14437 case DW_OP_lit13:
14438 case DW_OP_lit14:
14439 case DW_OP_lit15:
14440 case DW_OP_lit16:
14441 case DW_OP_lit17:
14442 case DW_OP_lit18:
14443 case DW_OP_lit19:
14444 case DW_OP_lit20:
14445 case DW_OP_lit21:
14446 case DW_OP_lit22:
14447 case DW_OP_lit23:
14448 case DW_OP_lit24:
14449 case DW_OP_lit25:
14450 case DW_OP_lit26:
14451 case DW_OP_lit27:
14452 case DW_OP_lit28:
14453 case DW_OP_lit29:
14454 case DW_OP_lit30:
14455 case DW_OP_lit31:
14456 stack[++stacki] = op - DW_OP_lit0;
14457 break;
14458
14459 case DW_OP_reg0:
14460 case DW_OP_reg1:
14461 case DW_OP_reg2:
14462 case DW_OP_reg3:
14463 case DW_OP_reg4:
14464 case DW_OP_reg5:
14465 case DW_OP_reg6:
14466 case DW_OP_reg7:
14467 case DW_OP_reg8:
14468 case DW_OP_reg9:
14469 case DW_OP_reg10:
14470 case DW_OP_reg11:
14471 case DW_OP_reg12:
14472 case DW_OP_reg13:
14473 case DW_OP_reg14:
14474 case DW_OP_reg15:
14475 case DW_OP_reg16:
14476 case DW_OP_reg17:
14477 case DW_OP_reg18:
14478 case DW_OP_reg19:
14479 case DW_OP_reg20:
14480 case DW_OP_reg21:
14481 case DW_OP_reg22:
14482 case DW_OP_reg23:
14483 case DW_OP_reg24:
14484 case DW_OP_reg25:
14485 case DW_OP_reg26:
14486 case DW_OP_reg27:
14487 case DW_OP_reg28:
14488 case DW_OP_reg29:
14489 case DW_OP_reg30:
14490 case DW_OP_reg31:
14491 stack[++stacki] = op - DW_OP_reg0;
14492 if (i < size)
14493 dwarf2_complex_location_expr_complaint ();
14494 break;
14495
14496 case DW_OP_regx:
14497 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14498 i += bytes_read;
14499 stack[++stacki] = unsnd;
14500 if (i < size)
14501 dwarf2_complex_location_expr_complaint ();
14502 break;
14503
14504 case DW_OP_addr:
14505 stack[++stacki] = read_address (objfile->obfd, &data[i],
14506 cu, &bytes_read);
14507 i += bytes_read;
14508 break;
14509
14510 case DW_OP_const1u:
14511 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14512 i += 1;
14513 break;
14514
14515 case DW_OP_const1s:
14516 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14517 i += 1;
14518 break;
14519
14520 case DW_OP_const2u:
14521 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14522 i += 2;
14523 break;
14524
14525 case DW_OP_const2s:
14526 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14527 i += 2;
14528 break;
14529
14530 case DW_OP_const4u:
14531 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14532 i += 4;
14533 break;
14534
14535 case DW_OP_const4s:
14536 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14537 i += 4;
14538 break;
14539
14540 case DW_OP_const8u:
14541 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14542 i += 8;
14543 break;
14544
14545 case DW_OP_constu:
14546 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14547 &bytes_read);
14548 i += bytes_read;
14549 break;
14550
14551 case DW_OP_consts:
14552 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14553 i += bytes_read;
14554 break;
14555
14556 case DW_OP_dup:
14557 stack[stacki + 1] = stack[stacki];
14558 stacki++;
14559 break;
14560
14561 case DW_OP_plus:
14562 stack[stacki - 1] += stack[stacki];
14563 stacki--;
14564 break;
14565
14566 case DW_OP_plus_uconst:
14567 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14568 &bytes_read);
14569 i += bytes_read;
14570 break;
14571
14572 case DW_OP_minus:
14573 stack[stacki - 1] -= stack[stacki];
14574 stacki--;
14575 break;
14576
14577 case DW_OP_deref:
14578 /* If we're not the last op, then we definitely can't encode
14579 this using GDB's address_class enum. This is valid for partial
14580 global symbols, although the variable's address will be bogus
14581 in the psymtab. */
14582 if (i < size)
14583 dwarf2_complex_location_expr_complaint ();
14584 break;
14585
14586 case DW_OP_GNU_push_tls_address:
14587 /* The top of the stack has the offset from the beginning
14588 of the thread control block at which the variable is located. */
14589 /* Nothing should follow this operator, so the top of stack would
14590 be returned. */
14591 /* This is valid for partial global symbols, but the variable's
14592 address will be bogus in the psymtab. Make it always at least
14593 non-zero to not look as a variable garbage collected by linker
14594 which have DW_OP_addr 0. */
14595 if (i < size)
14596 dwarf2_complex_location_expr_complaint ();
14597 stack[stacki]++;
14598 break;
14599
14600 case DW_OP_GNU_uninit:
14601 break;
14602
14603 default:
14604 {
14605 const char *name = dwarf_stack_op_name (op);
14606
14607 if (name)
14608 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14609 name);
14610 else
14611 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14612 op);
14613 }
14614
14615 return (stack[stacki]);
14616 }
14617
14618 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14619 outside of the allocated space. Also enforce minimum>0. */
14620 if (stacki >= ARRAY_SIZE (stack) - 1)
14621 {
14622 complaint (&symfile_complaints,
14623 _("location description stack overflow"));
14624 return 0;
14625 }
14626
14627 if (stacki <= 0)
14628 {
14629 complaint (&symfile_complaints,
14630 _("location description stack underflow"));
14631 return 0;
14632 }
14633 }
14634 return (stack[stacki]);
14635 }
14636
14637 /* memory allocation interface */
14638
14639 static struct dwarf_block *
14640 dwarf_alloc_block (struct dwarf2_cu *cu)
14641 {
14642 struct dwarf_block *blk;
14643
14644 blk = (struct dwarf_block *)
14645 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14646 return (blk);
14647 }
14648
14649 static struct abbrev_info *
14650 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14651 {
14652 struct abbrev_info *abbrev;
14653
14654 abbrev = (struct abbrev_info *)
14655 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14656 memset (abbrev, 0, sizeof (struct abbrev_info));
14657 return (abbrev);
14658 }
14659
14660 static struct die_info *
14661 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14662 {
14663 struct die_info *die;
14664 size_t size = sizeof (struct die_info);
14665
14666 if (num_attrs > 1)
14667 size += (num_attrs - 1) * sizeof (struct attribute);
14668
14669 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14670 memset (die, 0, sizeof (struct die_info));
14671 return (die);
14672 }
14673
14674 \f
14675 /* Macro support. */
14676
14677 /* Return the full name of file number I in *LH's file name table.
14678 Use COMP_DIR as the name of the current directory of the
14679 compilation. The result is allocated using xmalloc; the caller is
14680 responsible for freeing it. */
14681 static char *
14682 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14683 {
14684 /* Is the file number a valid index into the line header's file name
14685 table? Remember that file numbers start with one, not zero. */
14686 if (1 <= file && file <= lh->num_file_names)
14687 {
14688 struct file_entry *fe = &lh->file_names[file - 1];
14689
14690 if (IS_ABSOLUTE_PATH (fe->name))
14691 return xstrdup (fe->name);
14692 else
14693 {
14694 const char *dir;
14695 int dir_len;
14696 char *full_name;
14697
14698 if (fe->dir_index)
14699 dir = lh->include_dirs[fe->dir_index - 1];
14700 else
14701 dir = comp_dir;
14702
14703 if (dir)
14704 {
14705 dir_len = strlen (dir);
14706 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14707 strcpy (full_name, dir);
14708 full_name[dir_len] = '/';
14709 strcpy (full_name + dir_len + 1, fe->name);
14710 return full_name;
14711 }
14712 else
14713 return xstrdup (fe->name);
14714 }
14715 }
14716 else
14717 {
14718 /* The compiler produced a bogus file number. We can at least
14719 record the macro definitions made in the file, even if we
14720 won't be able to find the file by name. */
14721 char fake_name[80];
14722
14723 sprintf (fake_name, "<bad macro file number %d>", file);
14724
14725 complaint (&symfile_complaints,
14726 _("bad file number in macro information (%d)"),
14727 file);
14728
14729 return xstrdup (fake_name);
14730 }
14731 }
14732
14733
14734 static struct macro_source_file *
14735 macro_start_file (int file, int line,
14736 struct macro_source_file *current_file,
14737 const char *comp_dir,
14738 struct line_header *lh, struct objfile *objfile)
14739 {
14740 /* The full name of this source file. */
14741 char *full_name = file_full_name (file, lh, comp_dir);
14742
14743 /* We don't create a macro table for this compilation unit
14744 at all until we actually get a filename. */
14745 if (! pending_macros)
14746 pending_macros = new_macro_table (&objfile->objfile_obstack,
14747 objfile->macro_cache);
14748
14749 if (! current_file)
14750 /* If we have no current file, then this must be the start_file
14751 directive for the compilation unit's main source file. */
14752 current_file = macro_set_main (pending_macros, full_name);
14753 else
14754 current_file = macro_include (current_file, line, full_name);
14755
14756 xfree (full_name);
14757
14758 return current_file;
14759 }
14760
14761
14762 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14763 followed by a null byte. */
14764 static char *
14765 copy_string (const char *buf, int len)
14766 {
14767 char *s = xmalloc (len + 1);
14768
14769 memcpy (s, buf, len);
14770 s[len] = '\0';
14771 return s;
14772 }
14773
14774
14775 static const char *
14776 consume_improper_spaces (const char *p, const char *body)
14777 {
14778 if (*p == ' ')
14779 {
14780 complaint (&symfile_complaints,
14781 _("macro definition contains spaces "
14782 "in formal argument list:\n`%s'"),
14783 body);
14784
14785 while (*p == ' ')
14786 p++;
14787 }
14788
14789 return p;
14790 }
14791
14792
14793 static void
14794 parse_macro_definition (struct macro_source_file *file, int line,
14795 const char *body)
14796 {
14797 const char *p;
14798
14799 /* The body string takes one of two forms. For object-like macro
14800 definitions, it should be:
14801
14802 <macro name> " " <definition>
14803
14804 For function-like macro definitions, it should be:
14805
14806 <macro name> "() " <definition>
14807 or
14808 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14809
14810 Spaces may appear only where explicitly indicated, and in the
14811 <definition>.
14812
14813 The Dwarf 2 spec says that an object-like macro's name is always
14814 followed by a space, but versions of GCC around March 2002 omit
14815 the space when the macro's definition is the empty string.
14816
14817 The Dwarf 2 spec says that there should be no spaces between the
14818 formal arguments in a function-like macro's formal argument list,
14819 but versions of GCC around March 2002 include spaces after the
14820 commas. */
14821
14822
14823 /* Find the extent of the macro name. The macro name is terminated
14824 by either a space or null character (for an object-like macro) or
14825 an opening paren (for a function-like macro). */
14826 for (p = body; *p; p++)
14827 if (*p == ' ' || *p == '(')
14828 break;
14829
14830 if (*p == ' ' || *p == '\0')
14831 {
14832 /* It's an object-like macro. */
14833 int name_len = p - body;
14834 char *name = copy_string (body, name_len);
14835 const char *replacement;
14836
14837 if (*p == ' ')
14838 replacement = body + name_len + 1;
14839 else
14840 {
14841 dwarf2_macro_malformed_definition_complaint (body);
14842 replacement = body + name_len;
14843 }
14844
14845 macro_define_object (file, line, name, replacement);
14846
14847 xfree (name);
14848 }
14849 else if (*p == '(')
14850 {
14851 /* It's a function-like macro. */
14852 char *name = copy_string (body, p - body);
14853 int argc = 0;
14854 int argv_size = 1;
14855 char **argv = xmalloc (argv_size * sizeof (*argv));
14856
14857 p++;
14858
14859 p = consume_improper_spaces (p, body);
14860
14861 /* Parse the formal argument list. */
14862 while (*p && *p != ')')
14863 {
14864 /* Find the extent of the current argument name. */
14865 const char *arg_start = p;
14866
14867 while (*p && *p != ',' && *p != ')' && *p != ' ')
14868 p++;
14869
14870 if (! *p || p == arg_start)
14871 dwarf2_macro_malformed_definition_complaint (body);
14872 else
14873 {
14874 /* Make sure argv has room for the new argument. */
14875 if (argc >= argv_size)
14876 {
14877 argv_size *= 2;
14878 argv = xrealloc (argv, argv_size * sizeof (*argv));
14879 }
14880
14881 argv[argc++] = copy_string (arg_start, p - arg_start);
14882 }
14883
14884 p = consume_improper_spaces (p, body);
14885
14886 /* Consume the comma, if present. */
14887 if (*p == ',')
14888 {
14889 p++;
14890
14891 p = consume_improper_spaces (p, body);
14892 }
14893 }
14894
14895 if (*p == ')')
14896 {
14897 p++;
14898
14899 if (*p == ' ')
14900 /* Perfectly formed definition, no complaints. */
14901 macro_define_function (file, line, name,
14902 argc, (const char **) argv,
14903 p + 1);
14904 else if (*p == '\0')
14905 {
14906 /* Complain, but do define it. */
14907 dwarf2_macro_malformed_definition_complaint (body);
14908 macro_define_function (file, line, name,
14909 argc, (const char **) argv,
14910 p);
14911 }
14912 else
14913 /* Just complain. */
14914 dwarf2_macro_malformed_definition_complaint (body);
14915 }
14916 else
14917 /* Just complain. */
14918 dwarf2_macro_malformed_definition_complaint (body);
14919
14920 xfree (name);
14921 {
14922 int i;
14923
14924 for (i = 0; i < argc; i++)
14925 xfree (argv[i]);
14926 }
14927 xfree (argv);
14928 }
14929 else
14930 dwarf2_macro_malformed_definition_complaint (body);
14931 }
14932
14933 /* Skip some bytes from BYTES according to the form given in FORM.
14934 Returns the new pointer. */
14935
14936 static gdb_byte *
14937 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14938 enum dwarf_form form,
14939 unsigned int offset_size,
14940 struct dwarf2_section_info *section)
14941 {
14942 unsigned int bytes_read;
14943
14944 switch (form)
14945 {
14946 case DW_FORM_data1:
14947 case DW_FORM_flag:
14948 ++bytes;
14949 break;
14950
14951 case DW_FORM_data2:
14952 bytes += 2;
14953 break;
14954
14955 case DW_FORM_data4:
14956 bytes += 4;
14957 break;
14958
14959 case DW_FORM_data8:
14960 bytes += 8;
14961 break;
14962
14963 case DW_FORM_string:
14964 read_direct_string (abfd, bytes, &bytes_read);
14965 bytes += bytes_read;
14966 break;
14967
14968 case DW_FORM_sec_offset:
14969 case DW_FORM_strp:
14970 bytes += offset_size;
14971 break;
14972
14973 case DW_FORM_block:
14974 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14975 bytes += bytes_read;
14976 break;
14977
14978 case DW_FORM_block1:
14979 bytes += 1 + read_1_byte (abfd, bytes);
14980 break;
14981 case DW_FORM_block2:
14982 bytes += 2 + read_2_bytes (abfd, bytes);
14983 break;
14984 case DW_FORM_block4:
14985 bytes += 4 + read_4_bytes (abfd, bytes);
14986 break;
14987
14988 case DW_FORM_sdata:
14989 case DW_FORM_udata:
14990 bytes = skip_leb128 (abfd, bytes);
14991 break;
14992
14993 default:
14994 {
14995 complain:
14996 complaint (&symfile_complaints,
14997 _("invalid form 0x%x in `%s'"),
14998 form,
14999 section->asection->name);
15000 return NULL;
15001 }
15002 }
15003
15004 return bytes;
15005 }
15006
15007 /* A helper for dwarf_decode_macros that handles skipping an unknown
15008 opcode. Returns an updated pointer to the macro data buffer; or,
15009 on error, issues a complaint and returns NULL. */
15010
15011 static gdb_byte *
15012 skip_unknown_opcode (unsigned int opcode,
15013 gdb_byte **opcode_definitions,
15014 gdb_byte *mac_ptr,
15015 bfd *abfd,
15016 unsigned int offset_size,
15017 struct dwarf2_section_info *section)
15018 {
15019 unsigned int bytes_read, i;
15020 unsigned long arg;
15021 gdb_byte *defn;
15022
15023 if (opcode_definitions[opcode] == NULL)
15024 {
15025 complaint (&symfile_complaints,
15026 _("unrecognized DW_MACFINO opcode 0x%x"),
15027 opcode);
15028 return NULL;
15029 }
15030
15031 defn = opcode_definitions[opcode];
15032 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15033 defn += bytes_read;
15034
15035 for (i = 0; i < arg; ++i)
15036 {
15037 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15038 if (mac_ptr == NULL)
15039 {
15040 /* skip_form_bytes already issued the complaint. */
15041 return NULL;
15042 }
15043 }
15044
15045 return mac_ptr;
15046 }
15047
15048 /* A helper function which parses the header of a macro section.
15049 If the macro section is the extended (for now called "GNU") type,
15050 then this updates *OFFSET_SIZE. Returns a pointer to just after
15051 the header, or issues a complaint and returns NULL on error. */
15052
15053 static gdb_byte *
15054 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15055 bfd *abfd,
15056 gdb_byte *mac_ptr,
15057 unsigned int *offset_size,
15058 int section_is_gnu)
15059 {
15060 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15061
15062 if (section_is_gnu)
15063 {
15064 unsigned int version, flags;
15065
15066 version = read_2_bytes (abfd, mac_ptr);
15067 if (version != 4)
15068 {
15069 complaint (&symfile_complaints,
15070 _("unrecognized version `%d' in .debug_macro section"),
15071 version);
15072 return NULL;
15073 }
15074 mac_ptr += 2;
15075
15076 flags = read_1_byte (abfd, mac_ptr);
15077 ++mac_ptr;
15078 *offset_size = (flags & 1) ? 8 : 4;
15079
15080 if ((flags & 2) != 0)
15081 /* We don't need the line table offset. */
15082 mac_ptr += *offset_size;
15083
15084 /* Vendor opcode descriptions. */
15085 if ((flags & 4) != 0)
15086 {
15087 unsigned int i, count;
15088
15089 count = read_1_byte (abfd, mac_ptr);
15090 ++mac_ptr;
15091 for (i = 0; i < count; ++i)
15092 {
15093 unsigned int opcode, bytes_read;
15094 unsigned long arg;
15095
15096 opcode = read_1_byte (abfd, mac_ptr);
15097 ++mac_ptr;
15098 opcode_definitions[opcode] = mac_ptr;
15099 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15100 mac_ptr += bytes_read;
15101 mac_ptr += arg;
15102 }
15103 }
15104 }
15105
15106 return mac_ptr;
15107 }
15108
15109 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15110 including DW_GNU_MACINFO_transparent_include. */
15111
15112 static void
15113 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15114 struct macro_source_file *current_file,
15115 struct line_header *lh, char *comp_dir,
15116 struct dwarf2_section_info *section,
15117 int section_is_gnu,
15118 unsigned int offset_size,
15119 struct objfile *objfile)
15120 {
15121 enum dwarf_macro_record_type macinfo_type;
15122 int at_commandline;
15123 gdb_byte *opcode_definitions[256];
15124
15125 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15126 &offset_size, section_is_gnu);
15127 if (mac_ptr == NULL)
15128 {
15129 /* We already issued a complaint. */
15130 return;
15131 }
15132
15133 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15134 GDB is still reading the definitions from command line. First
15135 DW_MACINFO_start_file will need to be ignored as it was already executed
15136 to create CURRENT_FILE for the main source holding also the command line
15137 definitions. On first met DW_MACINFO_start_file this flag is reset to
15138 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15139
15140 at_commandline = 1;
15141
15142 do
15143 {
15144 /* Do we at least have room for a macinfo type byte? */
15145 if (mac_ptr >= mac_end)
15146 {
15147 dwarf2_macros_too_long_complaint (section);
15148 break;
15149 }
15150
15151 macinfo_type = read_1_byte (abfd, mac_ptr);
15152 mac_ptr++;
15153
15154 /* Note that we rely on the fact that the corresponding GNU and
15155 DWARF constants are the same. */
15156 switch (macinfo_type)
15157 {
15158 /* A zero macinfo type indicates the end of the macro
15159 information. */
15160 case 0:
15161 break;
15162
15163 case DW_MACRO_GNU_define:
15164 case DW_MACRO_GNU_undef:
15165 case DW_MACRO_GNU_define_indirect:
15166 case DW_MACRO_GNU_undef_indirect:
15167 {
15168 unsigned int bytes_read;
15169 int line;
15170 char *body;
15171 int is_define;
15172
15173 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15174 mac_ptr += bytes_read;
15175
15176 if (macinfo_type == DW_MACRO_GNU_define
15177 || macinfo_type == DW_MACRO_GNU_undef)
15178 {
15179 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15180 mac_ptr += bytes_read;
15181 }
15182 else
15183 {
15184 LONGEST str_offset;
15185
15186 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15187 mac_ptr += offset_size;
15188
15189 body = read_indirect_string_at_offset (abfd, str_offset);
15190 }
15191
15192 is_define = (macinfo_type == DW_MACRO_GNU_define
15193 || macinfo_type == DW_MACRO_GNU_define_indirect);
15194 if (! current_file)
15195 {
15196 /* DWARF violation as no main source is present. */
15197 complaint (&symfile_complaints,
15198 _("debug info with no main source gives macro %s "
15199 "on line %d: %s"),
15200 is_define ? _("definition") : _("undefinition"),
15201 line, body);
15202 break;
15203 }
15204 if ((line == 0 && !at_commandline)
15205 || (line != 0 && at_commandline))
15206 complaint (&symfile_complaints,
15207 _("debug info gives %s macro %s with %s line %d: %s"),
15208 at_commandline ? _("command-line") : _("in-file"),
15209 is_define ? _("definition") : _("undefinition"),
15210 line == 0 ? _("zero") : _("non-zero"), line, body);
15211
15212 if (is_define)
15213 parse_macro_definition (current_file, line, body);
15214 else
15215 {
15216 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15217 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15218 macro_undef (current_file, line, body);
15219 }
15220 }
15221 break;
15222
15223 case DW_MACRO_GNU_start_file:
15224 {
15225 unsigned int bytes_read;
15226 int line, file;
15227
15228 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15229 mac_ptr += bytes_read;
15230 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15231 mac_ptr += bytes_read;
15232
15233 if ((line == 0 && !at_commandline)
15234 || (line != 0 && at_commandline))
15235 complaint (&symfile_complaints,
15236 _("debug info gives source %d included "
15237 "from %s at %s line %d"),
15238 file, at_commandline ? _("command-line") : _("file"),
15239 line == 0 ? _("zero") : _("non-zero"), line);
15240
15241 if (at_commandline)
15242 {
15243 /* This DW_MACRO_GNU_start_file was executed in the
15244 pass one. */
15245 at_commandline = 0;
15246 }
15247 else
15248 current_file = macro_start_file (file, line,
15249 current_file, comp_dir,
15250 lh, objfile);
15251 }
15252 break;
15253
15254 case DW_MACRO_GNU_end_file:
15255 if (! current_file)
15256 complaint (&symfile_complaints,
15257 _("macro debug info has an unmatched "
15258 "`close_file' directive"));
15259 else
15260 {
15261 current_file = current_file->included_by;
15262 if (! current_file)
15263 {
15264 enum dwarf_macro_record_type next_type;
15265
15266 /* GCC circa March 2002 doesn't produce the zero
15267 type byte marking the end of the compilation
15268 unit. Complain if it's not there, but exit no
15269 matter what. */
15270
15271 /* Do we at least have room for a macinfo type byte? */
15272 if (mac_ptr >= mac_end)
15273 {
15274 dwarf2_macros_too_long_complaint (section);
15275 return;
15276 }
15277
15278 /* We don't increment mac_ptr here, so this is just
15279 a look-ahead. */
15280 next_type = read_1_byte (abfd, mac_ptr);
15281 if (next_type != 0)
15282 complaint (&symfile_complaints,
15283 _("no terminating 0-type entry for "
15284 "macros in `.debug_macinfo' section"));
15285
15286 return;
15287 }
15288 }
15289 break;
15290
15291 case DW_MACRO_GNU_transparent_include:
15292 {
15293 LONGEST offset;
15294
15295 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15296 mac_ptr += offset_size;
15297
15298 dwarf_decode_macro_bytes (abfd,
15299 section->buffer + offset,
15300 mac_end, current_file,
15301 lh, comp_dir,
15302 section, section_is_gnu,
15303 offset_size, objfile);
15304 }
15305 break;
15306
15307 case DW_MACINFO_vendor_ext:
15308 if (!section_is_gnu)
15309 {
15310 unsigned int bytes_read;
15311 int constant;
15312
15313 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15314 mac_ptr += bytes_read;
15315 read_direct_string (abfd, mac_ptr, &bytes_read);
15316 mac_ptr += bytes_read;
15317
15318 /* We don't recognize any vendor extensions. */
15319 break;
15320 }
15321 /* FALLTHROUGH */
15322
15323 default:
15324 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15325 mac_ptr, abfd, offset_size,
15326 section);
15327 if (mac_ptr == NULL)
15328 return;
15329 break;
15330 }
15331 } while (macinfo_type != 0);
15332 }
15333
15334 static void
15335 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15336 char *comp_dir, bfd *abfd,
15337 struct dwarf2_cu *cu,
15338 struct dwarf2_section_info *section,
15339 int section_is_gnu)
15340 {
15341 struct objfile *objfile = dwarf2_per_objfile->objfile;
15342 gdb_byte *mac_ptr, *mac_end;
15343 struct macro_source_file *current_file = 0;
15344 enum dwarf_macro_record_type macinfo_type;
15345 unsigned int offset_size = cu->header.offset_size;
15346 gdb_byte *opcode_definitions[256];
15347
15348 dwarf2_read_section (objfile, section);
15349 if (section->buffer == NULL)
15350 {
15351 complaint (&symfile_complaints, _("missing %s section"),
15352 section->asection->name);
15353 return;
15354 }
15355
15356 /* First pass: Find the name of the base filename.
15357 This filename is needed in order to process all macros whose definition
15358 (or undefinition) comes from the command line. These macros are defined
15359 before the first DW_MACINFO_start_file entry, and yet still need to be
15360 associated to the base file.
15361
15362 To determine the base file name, we scan the macro definitions until we
15363 reach the first DW_MACINFO_start_file entry. We then initialize
15364 CURRENT_FILE accordingly so that any macro definition found before the
15365 first DW_MACINFO_start_file can still be associated to the base file. */
15366
15367 mac_ptr = section->buffer + offset;
15368 mac_end = section->buffer + section->size;
15369
15370 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15371 &offset_size, section_is_gnu);
15372 if (mac_ptr == NULL)
15373 {
15374 /* We already issued a complaint. */
15375 return;
15376 }
15377
15378 do
15379 {
15380 /* Do we at least have room for a macinfo type byte? */
15381 if (mac_ptr >= mac_end)
15382 {
15383 /* Complaint is printed during the second pass as GDB will probably
15384 stop the first pass earlier upon finding
15385 DW_MACINFO_start_file. */
15386 break;
15387 }
15388
15389 macinfo_type = read_1_byte (abfd, mac_ptr);
15390 mac_ptr++;
15391
15392 /* Note that we rely on the fact that the corresponding GNU and
15393 DWARF constants are the same. */
15394 switch (macinfo_type)
15395 {
15396 /* A zero macinfo type indicates the end of the macro
15397 information. */
15398 case 0:
15399 break;
15400
15401 case DW_MACRO_GNU_define:
15402 case DW_MACRO_GNU_undef:
15403 /* Only skip the data by MAC_PTR. */
15404 {
15405 unsigned int bytes_read;
15406
15407 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15408 mac_ptr += bytes_read;
15409 read_direct_string (abfd, mac_ptr, &bytes_read);
15410 mac_ptr += bytes_read;
15411 }
15412 break;
15413
15414 case DW_MACRO_GNU_start_file:
15415 {
15416 unsigned int bytes_read;
15417 int line, file;
15418
15419 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15420 mac_ptr += bytes_read;
15421 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15422 mac_ptr += bytes_read;
15423
15424 current_file = macro_start_file (file, line, current_file,
15425 comp_dir, lh, objfile);
15426 }
15427 break;
15428
15429 case DW_MACRO_GNU_end_file:
15430 /* No data to skip by MAC_PTR. */
15431 break;
15432
15433 case DW_MACRO_GNU_define_indirect:
15434 case DW_MACRO_GNU_undef_indirect:
15435 {
15436 unsigned int bytes_read;
15437
15438 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15439 mac_ptr += bytes_read;
15440 mac_ptr += offset_size;
15441 }
15442 break;
15443
15444 case DW_MACRO_GNU_transparent_include:
15445 /* Note that, according to the spec, a transparent include
15446 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15447 skip this opcode. */
15448 mac_ptr += offset_size;
15449 break;
15450
15451 case DW_MACINFO_vendor_ext:
15452 /* Only skip the data by MAC_PTR. */
15453 if (!section_is_gnu)
15454 {
15455 unsigned int bytes_read;
15456
15457 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15458 mac_ptr += bytes_read;
15459 read_direct_string (abfd, mac_ptr, &bytes_read);
15460 mac_ptr += bytes_read;
15461 }
15462 /* FALLTHROUGH */
15463
15464 default:
15465 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15466 mac_ptr, abfd, offset_size,
15467 section);
15468 if (mac_ptr == NULL)
15469 return;
15470 break;
15471 }
15472 } while (macinfo_type != 0 && current_file == NULL);
15473
15474 /* Second pass: Process all entries.
15475
15476 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15477 command-line macro definitions/undefinitions. This flag is unset when we
15478 reach the first DW_MACINFO_start_file entry. */
15479
15480 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15481 current_file, lh, comp_dir, section, section_is_gnu,
15482 offset_size, objfile);
15483 }
15484
15485 /* Check if the attribute's form is a DW_FORM_block*
15486 if so return true else false. */
15487 static int
15488 attr_form_is_block (struct attribute *attr)
15489 {
15490 return (attr == NULL ? 0 :
15491 attr->form == DW_FORM_block1
15492 || attr->form == DW_FORM_block2
15493 || attr->form == DW_FORM_block4
15494 || attr->form == DW_FORM_block
15495 || attr->form == DW_FORM_exprloc);
15496 }
15497
15498 /* Return non-zero if ATTR's value is a section offset --- classes
15499 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15500 You may use DW_UNSND (attr) to retrieve such offsets.
15501
15502 Section 7.5.4, "Attribute Encodings", explains that no attribute
15503 may have a value that belongs to more than one of these classes; it
15504 would be ambiguous if we did, because we use the same forms for all
15505 of them. */
15506 static int
15507 attr_form_is_section_offset (struct attribute *attr)
15508 {
15509 return (attr->form == DW_FORM_data4
15510 || attr->form == DW_FORM_data8
15511 || attr->form == DW_FORM_sec_offset);
15512 }
15513
15514
15515 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15516 zero otherwise. When this function returns true, you can apply
15517 dwarf2_get_attr_constant_value to it.
15518
15519 However, note that for some attributes you must check
15520 attr_form_is_section_offset before using this test. DW_FORM_data4
15521 and DW_FORM_data8 are members of both the constant class, and of
15522 the classes that contain offsets into other debug sections
15523 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15524 that, if an attribute's can be either a constant or one of the
15525 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15526 taken as section offsets, not constants. */
15527 static int
15528 attr_form_is_constant (struct attribute *attr)
15529 {
15530 switch (attr->form)
15531 {
15532 case DW_FORM_sdata:
15533 case DW_FORM_udata:
15534 case DW_FORM_data1:
15535 case DW_FORM_data2:
15536 case DW_FORM_data4:
15537 case DW_FORM_data8:
15538 return 1;
15539 default:
15540 return 0;
15541 }
15542 }
15543
15544 /* A helper function that fills in a dwarf2_loclist_baton. */
15545
15546 static void
15547 fill_in_loclist_baton (struct dwarf2_cu *cu,
15548 struct dwarf2_loclist_baton *baton,
15549 struct attribute *attr)
15550 {
15551 dwarf2_read_section (dwarf2_per_objfile->objfile,
15552 &dwarf2_per_objfile->loc);
15553
15554 baton->per_cu = cu->per_cu;
15555 gdb_assert (baton->per_cu);
15556 /* We don't know how long the location list is, but make sure we
15557 don't run off the edge of the section. */
15558 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15559 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15560 baton->base_address = cu->base_address;
15561 }
15562
15563 static void
15564 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15565 struct dwarf2_cu *cu)
15566 {
15567 struct objfile *objfile = dwarf2_per_objfile->objfile;
15568
15569 if (attr_form_is_section_offset (attr)
15570 /* ".debug_loc" may not exist at all, or the offset may be outside
15571 the section. If so, fall through to the complaint in the
15572 other branch. */
15573 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15574 &dwarf2_per_objfile->loc))
15575 {
15576 struct dwarf2_loclist_baton *baton;
15577
15578 baton = obstack_alloc (&objfile->objfile_obstack,
15579 sizeof (struct dwarf2_loclist_baton));
15580
15581 fill_in_loclist_baton (cu, baton, attr);
15582
15583 if (cu->base_known == 0)
15584 complaint (&symfile_complaints,
15585 _("Location list used without "
15586 "specifying the CU base address."));
15587
15588 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15589 SYMBOL_LOCATION_BATON (sym) = baton;
15590 }
15591 else
15592 {
15593 struct dwarf2_locexpr_baton *baton;
15594
15595 baton = obstack_alloc (&objfile->objfile_obstack,
15596 sizeof (struct dwarf2_locexpr_baton));
15597 baton->per_cu = cu->per_cu;
15598 gdb_assert (baton->per_cu);
15599
15600 if (attr_form_is_block (attr))
15601 {
15602 /* Note that we're just copying the block's data pointer
15603 here, not the actual data. We're still pointing into the
15604 info_buffer for SYM's objfile; right now we never release
15605 that buffer, but when we do clean up properly this may
15606 need to change. */
15607 baton->size = DW_BLOCK (attr)->size;
15608 baton->data = DW_BLOCK (attr)->data;
15609 }
15610 else
15611 {
15612 dwarf2_invalid_attrib_class_complaint ("location description",
15613 SYMBOL_NATURAL_NAME (sym));
15614 baton->size = 0;
15615 }
15616
15617 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15618 SYMBOL_LOCATION_BATON (sym) = baton;
15619 }
15620 }
15621
15622 /* Return the OBJFILE associated with the compilation unit CU. If CU
15623 came from a separate debuginfo file, then the master objfile is
15624 returned. */
15625
15626 struct objfile *
15627 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15628 {
15629 struct objfile *objfile = per_cu->objfile;
15630
15631 /* Return the master objfile, so that we can report and look up the
15632 correct file containing this variable. */
15633 if (objfile->separate_debug_objfile_backlink)
15634 objfile = objfile->separate_debug_objfile_backlink;
15635
15636 return objfile;
15637 }
15638
15639 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15640 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15641 CU_HEADERP first. */
15642
15643 static const struct comp_unit_head *
15644 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15645 struct dwarf2_per_cu_data *per_cu)
15646 {
15647 struct objfile *objfile;
15648 struct dwarf2_per_objfile *per_objfile;
15649 gdb_byte *info_ptr;
15650
15651 if (per_cu->cu)
15652 return &per_cu->cu->header;
15653
15654 objfile = per_cu->objfile;
15655 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15656 info_ptr = per_objfile->info.buffer + per_cu->offset;
15657
15658 memset (cu_headerp, 0, sizeof (*cu_headerp));
15659 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15660
15661 return cu_headerp;
15662 }
15663
15664 /* Return the address size given in the compilation unit header for CU. */
15665
15666 int
15667 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15668 {
15669 struct comp_unit_head cu_header_local;
15670 const struct comp_unit_head *cu_headerp;
15671
15672 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15673
15674 return cu_headerp->addr_size;
15675 }
15676
15677 /* Return the offset size given in the compilation unit header for CU. */
15678
15679 int
15680 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15681 {
15682 struct comp_unit_head cu_header_local;
15683 const struct comp_unit_head *cu_headerp;
15684
15685 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15686
15687 return cu_headerp->offset_size;
15688 }
15689
15690 /* See its dwarf2loc.h declaration. */
15691
15692 int
15693 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15694 {
15695 struct comp_unit_head cu_header_local;
15696 const struct comp_unit_head *cu_headerp;
15697
15698 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15699
15700 if (cu_headerp->version == 2)
15701 return cu_headerp->addr_size;
15702 else
15703 return cu_headerp->offset_size;
15704 }
15705
15706 /* Return the text offset of the CU. The returned offset comes from
15707 this CU's objfile. If this objfile came from a separate debuginfo
15708 file, then the offset may be different from the corresponding
15709 offset in the parent objfile. */
15710
15711 CORE_ADDR
15712 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15713 {
15714 struct objfile *objfile = per_cu->objfile;
15715
15716 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15717 }
15718
15719 /* Locate the .debug_info compilation unit from CU's objfile which contains
15720 the DIE at OFFSET. Raises an error on failure. */
15721
15722 static struct dwarf2_per_cu_data *
15723 dwarf2_find_containing_comp_unit (unsigned int offset,
15724 struct objfile *objfile)
15725 {
15726 struct dwarf2_per_cu_data *this_cu;
15727 int low, high;
15728
15729 low = 0;
15730 high = dwarf2_per_objfile->n_comp_units - 1;
15731 while (high > low)
15732 {
15733 int mid = low + (high - low) / 2;
15734
15735 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15736 high = mid;
15737 else
15738 low = mid + 1;
15739 }
15740 gdb_assert (low == high);
15741 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15742 {
15743 if (low == 0)
15744 error (_("Dwarf Error: could not find partial DIE containing "
15745 "offset 0x%lx [in module %s]"),
15746 (long) offset, bfd_get_filename (objfile->obfd));
15747
15748 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15749 return dwarf2_per_objfile->all_comp_units[low-1];
15750 }
15751 else
15752 {
15753 this_cu = dwarf2_per_objfile->all_comp_units[low];
15754 if (low == dwarf2_per_objfile->n_comp_units - 1
15755 && offset >= this_cu->offset + this_cu->length)
15756 error (_("invalid dwarf2 offset %u"), offset);
15757 gdb_assert (offset < this_cu->offset + this_cu->length);
15758 return this_cu;
15759 }
15760 }
15761
15762 /* Initialize dwarf2_cu CU, owned by PER_CU. */
15763
15764 static void
15765 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15766 {
15767 memset (cu, 0, sizeof (*cu));
15768 per_cu->cu = cu;
15769 cu->per_cu = per_cu;
15770 cu->objfile = per_cu->objfile;
15771 obstack_init (&cu->comp_unit_obstack);
15772 }
15773
15774 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15775
15776 static void
15777 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15778 {
15779 struct attribute *attr;
15780
15781 /* Set the language we're debugging. */
15782 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15783 if (attr)
15784 set_cu_language (DW_UNSND (attr), cu);
15785 else
15786 {
15787 cu->language = language_minimal;
15788 cu->language_defn = language_def (cu->language);
15789 }
15790 }
15791
15792 /* Release one cached compilation unit, CU. We unlink it from the tree
15793 of compilation units, but we don't remove it from the read_in_chain;
15794 the caller is responsible for that.
15795 NOTE: DATA is a void * because this function is also used as a
15796 cleanup routine. */
15797
15798 static void
15799 free_heap_comp_unit (void *data)
15800 {
15801 struct dwarf2_cu *cu = data;
15802
15803 gdb_assert (cu->per_cu != NULL);
15804 cu->per_cu->cu = NULL;
15805 cu->per_cu = NULL;
15806
15807 obstack_free (&cu->comp_unit_obstack, NULL);
15808
15809 xfree (cu);
15810 }
15811
15812 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15813 when we're finished with it. We can't free the pointer itself, but be
15814 sure to unlink it from the cache. Also release any associated storage
15815 and perform cache maintenance.
15816
15817 Only used during partial symbol parsing. */
15818
15819 static void
15820 free_stack_comp_unit (void *data)
15821 {
15822 struct dwarf2_cu *cu = data;
15823
15824 gdb_assert (cu->per_cu != NULL);
15825 cu->per_cu->cu = NULL;
15826 cu->per_cu = NULL;
15827
15828 obstack_free (&cu->comp_unit_obstack, NULL);
15829 cu->partial_dies = NULL;
15830
15831 /* The previous code only did this if per_cu != NULL.
15832 But that would always succeed, so now we just unconditionally do
15833 the aging. This seems like the wrong place to do such aging,
15834 but cleaning that up is left for later. */
15835 age_cached_comp_units ();
15836 }
15837
15838 /* Free all cached compilation units. */
15839
15840 static void
15841 free_cached_comp_units (void *data)
15842 {
15843 struct dwarf2_per_cu_data *per_cu, **last_chain;
15844
15845 per_cu = dwarf2_per_objfile->read_in_chain;
15846 last_chain = &dwarf2_per_objfile->read_in_chain;
15847 while (per_cu != NULL)
15848 {
15849 struct dwarf2_per_cu_data *next_cu;
15850
15851 next_cu = per_cu->cu->read_in_chain;
15852
15853 free_heap_comp_unit (per_cu->cu);
15854 *last_chain = next_cu;
15855
15856 per_cu = next_cu;
15857 }
15858 }
15859
15860 /* Increase the age counter on each cached compilation unit, and free
15861 any that are too old. */
15862
15863 static void
15864 age_cached_comp_units (void)
15865 {
15866 struct dwarf2_per_cu_data *per_cu, **last_chain;
15867
15868 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15869 per_cu = dwarf2_per_objfile->read_in_chain;
15870 while (per_cu != NULL)
15871 {
15872 per_cu->cu->last_used ++;
15873 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15874 dwarf2_mark (per_cu->cu);
15875 per_cu = per_cu->cu->read_in_chain;
15876 }
15877
15878 per_cu = dwarf2_per_objfile->read_in_chain;
15879 last_chain = &dwarf2_per_objfile->read_in_chain;
15880 while (per_cu != NULL)
15881 {
15882 struct dwarf2_per_cu_data *next_cu;
15883
15884 next_cu = per_cu->cu->read_in_chain;
15885
15886 if (!per_cu->cu->mark)
15887 {
15888 free_heap_comp_unit (per_cu->cu);
15889 *last_chain = next_cu;
15890 }
15891 else
15892 last_chain = &per_cu->cu->read_in_chain;
15893
15894 per_cu = next_cu;
15895 }
15896 }
15897
15898 /* Remove a single compilation unit from the cache. */
15899
15900 static void
15901 free_one_cached_comp_unit (void *target_cu)
15902 {
15903 struct dwarf2_per_cu_data *per_cu, **last_chain;
15904
15905 per_cu = dwarf2_per_objfile->read_in_chain;
15906 last_chain = &dwarf2_per_objfile->read_in_chain;
15907 while (per_cu != NULL)
15908 {
15909 struct dwarf2_per_cu_data *next_cu;
15910
15911 next_cu = per_cu->cu->read_in_chain;
15912
15913 if (per_cu->cu == target_cu)
15914 {
15915 free_heap_comp_unit (per_cu->cu);
15916 *last_chain = next_cu;
15917 break;
15918 }
15919 else
15920 last_chain = &per_cu->cu->read_in_chain;
15921
15922 per_cu = next_cu;
15923 }
15924 }
15925
15926 /* Release all extra memory associated with OBJFILE. */
15927
15928 void
15929 dwarf2_free_objfile (struct objfile *objfile)
15930 {
15931 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15932
15933 if (dwarf2_per_objfile == NULL)
15934 return;
15935
15936 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15937 free_cached_comp_units (NULL);
15938
15939 if (dwarf2_per_objfile->quick_file_names_table)
15940 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15941
15942 /* Everything else should be on the objfile obstack. */
15943 }
15944
15945 /* A pair of DIE offset and GDB type pointer. We store these
15946 in a hash table separate from the DIEs, and preserve them
15947 when the DIEs are flushed out of cache. */
15948
15949 struct dwarf2_offset_and_type
15950 {
15951 unsigned int offset;
15952 struct type *type;
15953 };
15954
15955 /* Hash function for a dwarf2_offset_and_type. */
15956
15957 static hashval_t
15958 offset_and_type_hash (const void *item)
15959 {
15960 const struct dwarf2_offset_and_type *ofs = item;
15961
15962 return ofs->offset;
15963 }
15964
15965 /* Equality function for a dwarf2_offset_and_type. */
15966
15967 static int
15968 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15969 {
15970 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15971 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15972
15973 return ofs_lhs->offset == ofs_rhs->offset;
15974 }
15975
15976 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15977 table if necessary. For convenience, return TYPE.
15978
15979 The DIEs reading must have careful ordering to:
15980 * Not cause infite loops trying to read in DIEs as a prerequisite for
15981 reading current DIE.
15982 * Not trying to dereference contents of still incompletely read in types
15983 while reading in other DIEs.
15984 * Enable referencing still incompletely read in types just by a pointer to
15985 the type without accessing its fields.
15986
15987 Therefore caller should follow these rules:
15988 * Try to fetch any prerequisite types we may need to build this DIE type
15989 before building the type and calling set_die_type.
15990 * After building type call set_die_type for current DIE as soon as
15991 possible before fetching more types to complete the current type.
15992 * Make the type as complete as possible before fetching more types. */
15993
15994 static struct type *
15995 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15996 {
15997 struct dwarf2_offset_and_type **slot, ofs;
15998 struct objfile *objfile = cu->objfile;
15999 htab_t *type_hash_ptr;
16000
16001 /* For Ada types, make sure that the gnat-specific data is always
16002 initialized (if not already set). There are a few types where
16003 we should not be doing so, because the type-specific area is
16004 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16005 where the type-specific area is used to store the floatformat).
16006 But this is not a problem, because the gnat-specific information
16007 is actually not needed for these types. */
16008 if (need_gnat_info (cu)
16009 && TYPE_CODE (type) != TYPE_CODE_FUNC
16010 && TYPE_CODE (type) != TYPE_CODE_FLT
16011 && !HAVE_GNAT_AUX_INFO (type))
16012 INIT_GNAT_SPECIFIC (type);
16013
16014 if (cu->per_cu->debug_types_section)
16015 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16016 else
16017 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16018
16019 if (*type_hash_ptr == NULL)
16020 {
16021 *type_hash_ptr
16022 = htab_create_alloc_ex (127,
16023 offset_and_type_hash,
16024 offset_and_type_eq,
16025 NULL,
16026 &objfile->objfile_obstack,
16027 hashtab_obstack_allocate,
16028 dummy_obstack_deallocate);
16029 }
16030
16031 ofs.offset = die->offset;
16032 ofs.type = type;
16033 slot = (struct dwarf2_offset_and_type **)
16034 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16035 if (*slot)
16036 complaint (&symfile_complaints,
16037 _("A problem internal to GDB: DIE 0x%x has type already set"),
16038 die->offset);
16039 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16040 **slot = ofs;
16041 return type;
16042 }
16043
16044 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16045 table, or return NULL if the die does not have a saved type. */
16046
16047 static struct type *
16048 get_die_type_at_offset (unsigned int offset,
16049 struct dwarf2_per_cu_data *per_cu)
16050 {
16051 struct dwarf2_offset_and_type *slot, ofs;
16052 htab_t type_hash;
16053
16054 if (per_cu->debug_types_section)
16055 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16056 else
16057 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16058 if (type_hash == NULL)
16059 return NULL;
16060
16061 ofs.offset = offset;
16062 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16063 if (slot)
16064 return slot->type;
16065 else
16066 return NULL;
16067 }
16068
16069 /* Look up the type for DIE in the appropriate type_hash table,
16070 or return NULL if DIE does not have a saved type. */
16071
16072 static struct type *
16073 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16074 {
16075 return get_die_type_at_offset (die->offset, cu->per_cu);
16076 }
16077
16078 /* Add a dependence relationship from CU to REF_PER_CU. */
16079
16080 static void
16081 dwarf2_add_dependence (struct dwarf2_cu *cu,
16082 struct dwarf2_per_cu_data *ref_per_cu)
16083 {
16084 void **slot;
16085
16086 if (cu->dependencies == NULL)
16087 cu->dependencies
16088 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16089 NULL, &cu->comp_unit_obstack,
16090 hashtab_obstack_allocate,
16091 dummy_obstack_deallocate);
16092
16093 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16094 if (*slot == NULL)
16095 *slot = ref_per_cu;
16096 }
16097
16098 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16099 Set the mark field in every compilation unit in the
16100 cache that we must keep because we are keeping CU. */
16101
16102 static int
16103 dwarf2_mark_helper (void **slot, void *data)
16104 {
16105 struct dwarf2_per_cu_data *per_cu;
16106
16107 per_cu = (struct dwarf2_per_cu_data *) *slot;
16108
16109 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16110 reading of the chain. As such dependencies remain valid it is not much
16111 useful to track and undo them during QUIT cleanups. */
16112 if (per_cu->cu == NULL)
16113 return 1;
16114
16115 if (per_cu->cu->mark)
16116 return 1;
16117 per_cu->cu->mark = 1;
16118
16119 if (per_cu->cu->dependencies != NULL)
16120 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16121
16122 return 1;
16123 }
16124
16125 /* Set the mark field in CU and in every other compilation unit in the
16126 cache that we must keep because we are keeping CU. */
16127
16128 static void
16129 dwarf2_mark (struct dwarf2_cu *cu)
16130 {
16131 if (cu->mark)
16132 return;
16133 cu->mark = 1;
16134 if (cu->dependencies != NULL)
16135 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16136 }
16137
16138 static void
16139 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16140 {
16141 while (per_cu)
16142 {
16143 per_cu->cu->mark = 0;
16144 per_cu = per_cu->cu->read_in_chain;
16145 }
16146 }
16147
16148 /* Trivial hash function for partial_die_info: the hash value of a DIE
16149 is its offset in .debug_info for this objfile. */
16150
16151 static hashval_t
16152 partial_die_hash (const void *item)
16153 {
16154 const struct partial_die_info *part_die = item;
16155
16156 return part_die->offset;
16157 }
16158
16159 /* Trivial comparison function for partial_die_info structures: two DIEs
16160 are equal if they have the same offset. */
16161
16162 static int
16163 partial_die_eq (const void *item_lhs, const void *item_rhs)
16164 {
16165 const struct partial_die_info *part_die_lhs = item_lhs;
16166 const struct partial_die_info *part_die_rhs = item_rhs;
16167
16168 return part_die_lhs->offset == part_die_rhs->offset;
16169 }
16170
16171 static struct cmd_list_element *set_dwarf2_cmdlist;
16172 static struct cmd_list_element *show_dwarf2_cmdlist;
16173
16174 static void
16175 set_dwarf2_cmd (char *args, int from_tty)
16176 {
16177 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16178 }
16179
16180 static void
16181 show_dwarf2_cmd (char *args, int from_tty)
16182 {
16183 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16184 }
16185
16186 /* If section described by INFO was mmapped, munmap it now. */
16187
16188 static void
16189 munmap_section_buffer (struct dwarf2_section_info *info)
16190 {
16191 if (info->map_addr != NULL)
16192 {
16193 #ifdef HAVE_MMAP
16194 int res;
16195
16196 res = munmap (info->map_addr, info->map_len);
16197 gdb_assert (res == 0);
16198 #else
16199 /* Without HAVE_MMAP, we should never be here to begin with. */
16200 gdb_assert_not_reached ("no mmap support");
16201 #endif
16202 }
16203 }
16204
16205 /* munmap debug sections for OBJFILE, if necessary. */
16206
16207 static void
16208 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16209 {
16210 struct dwarf2_per_objfile *data = d;
16211 int ix;
16212 struct dwarf2_section_info *section;
16213
16214 /* This is sorted according to the order they're defined in to make it easier
16215 to keep in sync. */
16216 munmap_section_buffer (&data->info);
16217 munmap_section_buffer (&data->abbrev);
16218 munmap_section_buffer (&data->line);
16219 munmap_section_buffer (&data->loc);
16220 munmap_section_buffer (&data->macinfo);
16221 munmap_section_buffer (&data->macro);
16222 munmap_section_buffer (&data->str);
16223 munmap_section_buffer (&data->ranges);
16224 munmap_section_buffer (&data->frame);
16225 munmap_section_buffer (&data->eh_frame);
16226 munmap_section_buffer (&data->gdb_index);
16227
16228 for (ix = 0;
16229 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16230 ++ix)
16231 munmap_section_buffer (section);
16232
16233 VEC_free (dwarf2_section_info_def, data->types);
16234 }
16235
16236 \f
16237 /* The "save gdb-index" command. */
16238
16239 /* The contents of the hash table we create when building the string
16240 table. */
16241 struct strtab_entry
16242 {
16243 offset_type offset;
16244 const char *str;
16245 };
16246
16247 /* Hash function for a strtab_entry.
16248
16249 Function is used only during write_hash_table so no index format backward
16250 compatibility is needed. */
16251
16252 static hashval_t
16253 hash_strtab_entry (const void *e)
16254 {
16255 const struct strtab_entry *entry = e;
16256 return mapped_index_string_hash (INT_MAX, entry->str);
16257 }
16258
16259 /* Equality function for a strtab_entry. */
16260
16261 static int
16262 eq_strtab_entry (const void *a, const void *b)
16263 {
16264 const struct strtab_entry *ea = a;
16265 const struct strtab_entry *eb = b;
16266 return !strcmp (ea->str, eb->str);
16267 }
16268
16269 /* Create a strtab_entry hash table. */
16270
16271 static htab_t
16272 create_strtab (void)
16273 {
16274 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16275 xfree, xcalloc, xfree);
16276 }
16277
16278 /* Add a string to the constant pool. Return the string's offset in
16279 host order. */
16280
16281 static offset_type
16282 add_string (htab_t table, struct obstack *cpool, const char *str)
16283 {
16284 void **slot;
16285 struct strtab_entry entry;
16286 struct strtab_entry *result;
16287
16288 entry.str = str;
16289 slot = htab_find_slot (table, &entry, INSERT);
16290 if (*slot)
16291 result = *slot;
16292 else
16293 {
16294 result = XNEW (struct strtab_entry);
16295 result->offset = obstack_object_size (cpool);
16296 result->str = str;
16297 obstack_grow_str0 (cpool, str);
16298 *slot = result;
16299 }
16300 return result->offset;
16301 }
16302
16303 /* An entry in the symbol table. */
16304 struct symtab_index_entry
16305 {
16306 /* The name of the symbol. */
16307 const char *name;
16308 /* The offset of the name in the constant pool. */
16309 offset_type index_offset;
16310 /* A sorted vector of the indices of all the CUs that hold an object
16311 of this name. */
16312 VEC (offset_type) *cu_indices;
16313 };
16314
16315 /* The symbol table. This is a power-of-2-sized hash table. */
16316 struct mapped_symtab
16317 {
16318 offset_type n_elements;
16319 offset_type size;
16320 struct symtab_index_entry **data;
16321 };
16322
16323 /* Hash function for a symtab_index_entry. */
16324
16325 static hashval_t
16326 hash_symtab_entry (const void *e)
16327 {
16328 const struct symtab_index_entry *entry = e;
16329 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16330 sizeof (offset_type) * VEC_length (offset_type,
16331 entry->cu_indices),
16332 0);
16333 }
16334
16335 /* Equality function for a symtab_index_entry. */
16336
16337 static int
16338 eq_symtab_entry (const void *a, const void *b)
16339 {
16340 const struct symtab_index_entry *ea = a;
16341 const struct symtab_index_entry *eb = b;
16342 int len = VEC_length (offset_type, ea->cu_indices);
16343 if (len != VEC_length (offset_type, eb->cu_indices))
16344 return 0;
16345 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16346 VEC_address (offset_type, eb->cu_indices),
16347 sizeof (offset_type) * len);
16348 }
16349
16350 /* Destroy a symtab_index_entry. */
16351
16352 static void
16353 delete_symtab_entry (void *p)
16354 {
16355 struct symtab_index_entry *entry = p;
16356 VEC_free (offset_type, entry->cu_indices);
16357 xfree (entry);
16358 }
16359
16360 /* Create a hash table holding symtab_index_entry objects. */
16361
16362 static htab_t
16363 create_symbol_hash_table (void)
16364 {
16365 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16366 delete_symtab_entry, xcalloc, xfree);
16367 }
16368
16369 /* Create a new mapped symtab object. */
16370
16371 static struct mapped_symtab *
16372 create_mapped_symtab (void)
16373 {
16374 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16375 symtab->n_elements = 0;
16376 symtab->size = 1024;
16377 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16378 return symtab;
16379 }
16380
16381 /* Destroy a mapped_symtab. */
16382
16383 static void
16384 cleanup_mapped_symtab (void *p)
16385 {
16386 struct mapped_symtab *symtab = p;
16387 /* The contents of the array are freed when the other hash table is
16388 destroyed. */
16389 xfree (symtab->data);
16390 xfree (symtab);
16391 }
16392
16393 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16394 the slot.
16395
16396 Function is used only during write_hash_table so no index format backward
16397 compatibility is needed. */
16398
16399 static struct symtab_index_entry **
16400 find_slot (struct mapped_symtab *symtab, const char *name)
16401 {
16402 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16403
16404 index = hash & (symtab->size - 1);
16405 step = ((hash * 17) & (symtab->size - 1)) | 1;
16406
16407 for (;;)
16408 {
16409 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16410 return &symtab->data[index];
16411 index = (index + step) & (symtab->size - 1);
16412 }
16413 }
16414
16415 /* Expand SYMTAB's hash table. */
16416
16417 static void
16418 hash_expand (struct mapped_symtab *symtab)
16419 {
16420 offset_type old_size = symtab->size;
16421 offset_type i;
16422 struct symtab_index_entry **old_entries = symtab->data;
16423
16424 symtab->size *= 2;
16425 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16426
16427 for (i = 0; i < old_size; ++i)
16428 {
16429 if (old_entries[i])
16430 {
16431 struct symtab_index_entry **slot = find_slot (symtab,
16432 old_entries[i]->name);
16433 *slot = old_entries[i];
16434 }
16435 }
16436
16437 xfree (old_entries);
16438 }
16439
16440 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16441 is the index of the CU in which the symbol appears. */
16442
16443 static void
16444 add_index_entry (struct mapped_symtab *symtab, const char *name,
16445 offset_type cu_index)
16446 {
16447 struct symtab_index_entry **slot;
16448
16449 ++symtab->n_elements;
16450 if (4 * symtab->n_elements / 3 >= symtab->size)
16451 hash_expand (symtab);
16452
16453 slot = find_slot (symtab, name);
16454 if (!*slot)
16455 {
16456 *slot = XNEW (struct symtab_index_entry);
16457 (*slot)->name = name;
16458 (*slot)->cu_indices = NULL;
16459 }
16460 /* Don't push an index twice. Due to how we add entries we only
16461 have to check the last one. */
16462 if (VEC_empty (offset_type, (*slot)->cu_indices)
16463 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16464 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16465 }
16466
16467 /* Add a vector of indices to the constant pool. */
16468
16469 static offset_type
16470 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16471 struct symtab_index_entry *entry)
16472 {
16473 void **slot;
16474
16475 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16476 if (!*slot)
16477 {
16478 offset_type len = VEC_length (offset_type, entry->cu_indices);
16479 offset_type val = MAYBE_SWAP (len);
16480 offset_type iter;
16481 int i;
16482
16483 *slot = entry;
16484 entry->index_offset = obstack_object_size (cpool);
16485
16486 obstack_grow (cpool, &val, sizeof (val));
16487 for (i = 0;
16488 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16489 ++i)
16490 {
16491 val = MAYBE_SWAP (iter);
16492 obstack_grow (cpool, &val, sizeof (val));
16493 }
16494 }
16495 else
16496 {
16497 struct symtab_index_entry *old_entry = *slot;
16498 entry->index_offset = old_entry->index_offset;
16499 entry = old_entry;
16500 }
16501 return entry->index_offset;
16502 }
16503
16504 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16505 constant pool entries going into the obstack CPOOL. */
16506
16507 static void
16508 write_hash_table (struct mapped_symtab *symtab,
16509 struct obstack *output, struct obstack *cpool)
16510 {
16511 offset_type i;
16512 htab_t symbol_hash_table;
16513 htab_t str_table;
16514
16515 symbol_hash_table = create_symbol_hash_table ();
16516 str_table = create_strtab ();
16517
16518 /* We add all the index vectors to the constant pool first, to
16519 ensure alignment is ok. */
16520 for (i = 0; i < symtab->size; ++i)
16521 {
16522 if (symtab->data[i])
16523 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16524 }
16525
16526 /* Now write out the hash table. */
16527 for (i = 0; i < symtab->size; ++i)
16528 {
16529 offset_type str_off, vec_off;
16530
16531 if (symtab->data[i])
16532 {
16533 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16534 vec_off = symtab->data[i]->index_offset;
16535 }
16536 else
16537 {
16538 /* While 0 is a valid constant pool index, it is not valid
16539 to have 0 for both offsets. */
16540 str_off = 0;
16541 vec_off = 0;
16542 }
16543
16544 str_off = MAYBE_SWAP (str_off);
16545 vec_off = MAYBE_SWAP (vec_off);
16546
16547 obstack_grow (output, &str_off, sizeof (str_off));
16548 obstack_grow (output, &vec_off, sizeof (vec_off));
16549 }
16550
16551 htab_delete (str_table);
16552 htab_delete (symbol_hash_table);
16553 }
16554
16555 /* Struct to map psymtab to CU index in the index file. */
16556 struct psymtab_cu_index_map
16557 {
16558 struct partial_symtab *psymtab;
16559 unsigned int cu_index;
16560 };
16561
16562 static hashval_t
16563 hash_psymtab_cu_index (const void *item)
16564 {
16565 const struct psymtab_cu_index_map *map = item;
16566
16567 return htab_hash_pointer (map->psymtab);
16568 }
16569
16570 static int
16571 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16572 {
16573 const struct psymtab_cu_index_map *lhs = item_lhs;
16574 const struct psymtab_cu_index_map *rhs = item_rhs;
16575
16576 return lhs->psymtab == rhs->psymtab;
16577 }
16578
16579 /* Helper struct for building the address table. */
16580 struct addrmap_index_data
16581 {
16582 struct objfile *objfile;
16583 struct obstack *addr_obstack;
16584 htab_t cu_index_htab;
16585
16586 /* Non-zero if the previous_* fields are valid.
16587 We can't write an entry until we see the next entry (since it is only then
16588 that we know the end of the entry). */
16589 int previous_valid;
16590 /* Index of the CU in the table of all CUs in the index file. */
16591 unsigned int previous_cu_index;
16592 /* Start address of the CU. */
16593 CORE_ADDR previous_cu_start;
16594 };
16595
16596 /* Write an address entry to OBSTACK. */
16597
16598 static void
16599 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16600 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16601 {
16602 offset_type cu_index_to_write;
16603 char addr[8];
16604 CORE_ADDR baseaddr;
16605
16606 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16607
16608 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16609 obstack_grow (obstack, addr, 8);
16610 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16611 obstack_grow (obstack, addr, 8);
16612 cu_index_to_write = MAYBE_SWAP (cu_index);
16613 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16614 }
16615
16616 /* Worker function for traversing an addrmap to build the address table. */
16617
16618 static int
16619 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16620 {
16621 struct addrmap_index_data *data = datap;
16622 struct partial_symtab *pst = obj;
16623 offset_type cu_index;
16624 void **slot;
16625
16626 if (data->previous_valid)
16627 add_address_entry (data->objfile, data->addr_obstack,
16628 data->previous_cu_start, start_addr,
16629 data->previous_cu_index);
16630
16631 data->previous_cu_start = start_addr;
16632 if (pst != NULL)
16633 {
16634 struct psymtab_cu_index_map find_map, *map;
16635 find_map.psymtab = pst;
16636 map = htab_find (data->cu_index_htab, &find_map);
16637 gdb_assert (map != NULL);
16638 data->previous_cu_index = map->cu_index;
16639 data->previous_valid = 1;
16640 }
16641 else
16642 data->previous_valid = 0;
16643
16644 return 0;
16645 }
16646
16647 /* Write OBJFILE's address map to OBSTACK.
16648 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16649 in the index file. */
16650
16651 static void
16652 write_address_map (struct objfile *objfile, struct obstack *obstack,
16653 htab_t cu_index_htab)
16654 {
16655 struct addrmap_index_data addrmap_index_data;
16656
16657 /* When writing the address table, we have to cope with the fact that
16658 the addrmap iterator only provides the start of a region; we have to
16659 wait until the next invocation to get the start of the next region. */
16660
16661 addrmap_index_data.objfile = objfile;
16662 addrmap_index_data.addr_obstack = obstack;
16663 addrmap_index_data.cu_index_htab = cu_index_htab;
16664 addrmap_index_data.previous_valid = 0;
16665
16666 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16667 &addrmap_index_data);
16668
16669 /* It's highly unlikely the last entry (end address = 0xff...ff)
16670 is valid, but we should still handle it.
16671 The end address is recorded as the start of the next region, but that
16672 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16673 anyway. */
16674 if (addrmap_index_data.previous_valid)
16675 add_address_entry (objfile, obstack,
16676 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16677 addrmap_index_data.previous_cu_index);
16678 }
16679
16680 /* Add a list of partial symbols to SYMTAB. */
16681
16682 static void
16683 write_psymbols (struct mapped_symtab *symtab,
16684 htab_t psyms_seen,
16685 struct partial_symbol **psymp,
16686 int count,
16687 offset_type cu_index,
16688 int is_static)
16689 {
16690 for (; count-- > 0; ++psymp)
16691 {
16692 void **slot, *lookup;
16693
16694 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16695 error (_("Ada is not currently supported by the index"));
16696
16697 /* We only want to add a given psymbol once. However, we also
16698 want to account for whether it is global or static. So, we
16699 may add it twice, using slightly different values. */
16700 if (is_static)
16701 {
16702 uintptr_t val = 1 | (uintptr_t) *psymp;
16703
16704 lookup = (void *) val;
16705 }
16706 else
16707 lookup = *psymp;
16708
16709 /* Only add a given psymbol once. */
16710 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16711 if (!*slot)
16712 {
16713 *slot = lookup;
16714 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16715 }
16716 }
16717 }
16718
16719 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16720 exception if there is an error. */
16721
16722 static void
16723 write_obstack (FILE *file, struct obstack *obstack)
16724 {
16725 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16726 file)
16727 != obstack_object_size (obstack))
16728 error (_("couldn't data write to file"));
16729 }
16730
16731 /* Unlink a file if the argument is not NULL. */
16732
16733 static void
16734 unlink_if_set (void *p)
16735 {
16736 char **filename = p;
16737 if (*filename)
16738 unlink (*filename);
16739 }
16740
16741 /* A helper struct used when iterating over debug_types. */
16742 struct signatured_type_index_data
16743 {
16744 struct objfile *objfile;
16745 struct mapped_symtab *symtab;
16746 struct obstack *types_list;
16747 htab_t psyms_seen;
16748 int cu_index;
16749 };
16750
16751 /* A helper function that writes a single signatured_type to an
16752 obstack. */
16753
16754 static int
16755 write_one_signatured_type (void **slot, void *d)
16756 {
16757 struct signatured_type_index_data *info = d;
16758 struct signatured_type *entry = (struct signatured_type *) *slot;
16759 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16760 struct partial_symtab *psymtab = per_cu->v.psymtab;
16761 gdb_byte val[8];
16762
16763 write_psymbols (info->symtab,
16764 info->psyms_seen,
16765 info->objfile->global_psymbols.list
16766 + psymtab->globals_offset,
16767 psymtab->n_global_syms, info->cu_index,
16768 0);
16769 write_psymbols (info->symtab,
16770 info->psyms_seen,
16771 info->objfile->static_psymbols.list
16772 + psymtab->statics_offset,
16773 psymtab->n_static_syms, info->cu_index,
16774 1);
16775
16776 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16777 obstack_grow (info->types_list, val, 8);
16778 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16779 obstack_grow (info->types_list, val, 8);
16780 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16781 obstack_grow (info->types_list, val, 8);
16782
16783 ++info->cu_index;
16784
16785 return 1;
16786 }
16787
16788 /* Create an index file for OBJFILE in the directory DIR. */
16789
16790 static void
16791 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16792 {
16793 struct cleanup *cleanup;
16794 char *filename, *cleanup_filename;
16795 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16796 struct obstack cu_list, types_cu_list;
16797 int i;
16798 FILE *out_file;
16799 struct mapped_symtab *symtab;
16800 offset_type val, size_of_contents, total_len;
16801 struct stat st;
16802 char buf[8];
16803 htab_t psyms_seen;
16804 htab_t cu_index_htab;
16805 struct psymtab_cu_index_map *psymtab_cu_index_map;
16806
16807 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16808 return;
16809
16810 if (dwarf2_per_objfile->using_index)
16811 error (_("Cannot use an index to create the index"));
16812
16813 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16814 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16815
16816 if (stat (objfile->name, &st) < 0)
16817 perror_with_name (objfile->name);
16818
16819 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16820 INDEX_SUFFIX, (char *) NULL);
16821 cleanup = make_cleanup (xfree, filename);
16822
16823 out_file = fopen (filename, "wb");
16824 if (!out_file)
16825 error (_("Can't open `%s' for writing"), filename);
16826
16827 cleanup_filename = filename;
16828 make_cleanup (unlink_if_set, &cleanup_filename);
16829
16830 symtab = create_mapped_symtab ();
16831 make_cleanup (cleanup_mapped_symtab, symtab);
16832
16833 obstack_init (&addr_obstack);
16834 make_cleanup_obstack_free (&addr_obstack);
16835
16836 obstack_init (&cu_list);
16837 make_cleanup_obstack_free (&cu_list);
16838
16839 obstack_init (&types_cu_list);
16840 make_cleanup_obstack_free (&types_cu_list);
16841
16842 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16843 NULL, xcalloc, xfree);
16844 make_cleanup_htab_delete (psyms_seen);
16845
16846 /* While we're scanning CU's create a table that maps a psymtab pointer
16847 (which is what addrmap records) to its index (which is what is recorded
16848 in the index file). This will later be needed to write the address
16849 table. */
16850 cu_index_htab = htab_create_alloc (100,
16851 hash_psymtab_cu_index,
16852 eq_psymtab_cu_index,
16853 NULL, xcalloc, xfree);
16854 make_cleanup_htab_delete (cu_index_htab);
16855 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16856 xmalloc (sizeof (struct psymtab_cu_index_map)
16857 * dwarf2_per_objfile->n_comp_units);
16858 make_cleanup (xfree, psymtab_cu_index_map);
16859
16860 /* The CU list is already sorted, so we don't need to do additional
16861 work here. Also, the debug_types entries do not appear in
16862 all_comp_units, but only in their own hash table. */
16863 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16864 {
16865 struct dwarf2_per_cu_data *per_cu
16866 = dwarf2_per_objfile->all_comp_units[i];
16867 struct partial_symtab *psymtab = per_cu->v.psymtab;
16868 gdb_byte val[8];
16869 struct psymtab_cu_index_map *map;
16870 void **slot;
16871
16872 write_psymbols (symtab,
16873 psyms_seen,
16874 objfile->global_psymbols.list + psymtab->globals_offset,
16875 psymtab->n_global_syms, i,
16876 0);
16877 write_psymbols (symtab,
16878 psyms_seen,
16879 objfile->static_psymbols.list + psymtab->statics_offset,
16880 psymtab->n_static_syms, i,
16881 1);
16882
16883 map = &psymtab_cu_index_map[i];
16884 map->psymtab = psymtab;
16885 map->cu_index = i;
16886 slot = htab_find_slot (cu_index_htab, map, INSERT);
16887 gdb_assert (slot != NULL);
16888 gdb_assert (*slot == NULL);
16889 *slot = map;
16890
16891 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16892 obstack_grow (&cu_list, val, 8);
16893 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16894 obstack_grow (&cu_list, val, 8);
16895 }
16896
16897 /* Dump the address map. */
16898 write_address_map (objfile, &addr_obstack, cu_index_htab);
16899
16900 /* Write out the .debug_type entries, if any. */
16901 if (dwarf2_per_objfile->signatured_types)
16902 {
16903 struct signatured_type_index_data sig_data;
16904
16905 sig_data.objfile = objfile;
16906 sig_data.symtab = symtab;
16907 sig_data.types_list = &types_cu_list;
16908 sig_data.psyms_seen = psyms_seen;
16909 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16910 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16911 write_one_signatured_type, &sig_data);
16912 }
16913
16914 obstack_init (&constant_pool);
16915 make_cleanup_obstack_free (&constant_pool);
16916 obstack_init (&symtab_obstack);
16917 make_cleanup_obstack_free (&symtab_obstack);
16918 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16919
16920 obstack_init (&contents);
16921 make_cleanup_obstack_free (&contents);
16922 size_of_contents = 6 * sizeof (offset_type);
16923 total_len = size_of_contents;
16924
16925 /* The version number. */
16926 val = MAYBE_SWAP (5);
16927 obstack_grow (&contents, &val, sizeof (val));
16928
16929 /* The offset of the CU list from the start of the file. */
16930 val = MAYBE_SWAP (total_len);
16931 obstack_grow (&contents, &val, sizeof (val));
16932 total_len += obstack_object_size (&cu_list);
16933
16934 /* The offset of the types CU list from the start of the file. */
16935 val = MAYBE_SWAP (total_len);
16936 obstack_grow (&contents, &val, sizeof (val));
16937 total_len += obstack_object_size (&types_cu_list);
16938
16939 /* The offset of the address table from the start of the file. */
16940 val = MAYBE_SWAP (total_len);
16941 obstack_grow (&contents, &val, sizeof (val));
16942 total_len += obstack_object_size (&addr_obstack);
16943
16944 /* The offset of the symbol table from the start of the file. */
16945 val = MAYBE_SWAP (total_len);
16946 obstack_grow (&contents, &val, sizeof (val));
16947 total_len += obstack_object_size (&symtab_obstack);
16948
16949 /* The offset of the constant pool from the start of the file. */
16950 val = MAYBE_SWAP (total_len);
16951 obstack_grow (&contents, &val, sizeof (val));
16952 total_len += obstack_object_size (&constant_pool);
16953
16954 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16955
16956 write_obstack (out_file, &contents);
16957 write_obstack (out_file, &cu_list);
16958 write_obstack (out_file, &types_cu_list);
16959 write_obstack (out_file, &addr_obstack);
16960 write_obstack (out_file, &symtab_obstack);
16961 write_obstack (out_file, &constant_pool);
16962
16963 fclose (out_file);
16964
16965 /* We want to keep the file, so we set cleanup_filename to NULL
16966 here. See unlink_if_set. */
16967 cleanup_filename = NULL;
16968
16969 do_cleanups (cleanup);
16970 }
16971
16972 /* Implementation of the `save gdb-index' command.
16973
16974 Note that the file format used by this command is documented in the
16975 GDB manual. Any changes here must be documented there. */
16976
16977 static void
16978 save_gdb_index_command (char *arg, int from_tty)
16979 {
16980 struct objfile *objfile;
16981
16982 if (!arg || !*arg)
16983 error (_("usage: save gdb-index DIRECTORY"));
16984
16985 ALL_OBJFILES (objfile)
16986 {
16987 struct stat st;
16988
16989 /* If the objfile does not correspond to an actual file, skip it. */
16990 if (stat (objfile->name, &st) < 0)
16991 continue;
16992
16993 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16994 if (dwarf2_per_objfile)
16995 {
16996 volatile struct gdb_exception except;
16997
16998 TRY_CATCH (except, RETURN_MASK_ERROR)
16999 {
17000 write_psymtabs_to_index (objfile, arg);
17001 }
17002 if (except.reason < 0)
17003 exception_fprintf (gdb_stderr, except,
17004 _("Error while writing index for `%s': "),
17005 objfile->name);
17006 }
17007 }
17008 }
17009
17010 \f
17011
17012 int dwarf2_always_disassemble;
17013
17014 static void
17015 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17016 struct cmd_list_element *c, const char *value)
17017 {
17018 fprintf_filtered (file,
17019 _("Whether to always disassemble "
17020 "DWARF expressions is %s.\n"),
17021 value);
17022 }
17023
17024 static void
17025 show_check_physname (struct ui_file *file, int from_tty,
17026 struct cmd_list_element *c, const char *value)
17027 {
17028 fprintf_filtered (file,
17029 _("Whether to check \"physname\" is %s.\n"),
17030 value);
17031 }
17032
17033 void _initialize_dwarf2_read (void);
17034
17035 void
17036 _initialize_dwarf2_read (void)
17037 {
17038 struct cmd_list_element *c;
17039
17040 dwarf2_objfile_data_key
17041 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17042
17043 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17044 Set DWARF 2 specific variables.\n\
17045 Configure DWARF 2 variables such as the cache size"),
17046 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17047 0/*allow-unknown*/, &maintenance_set_cmdlist);
17048
17049 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17050 Show DWARF 2 specific variables\n\
17051 Show DWARF 2 variables such as the cache size"),
17052 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17053 0/*allow-unknown*/, &maintenance_show_cmdlist);
17054
17055 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17056 &dwarf2_max_cache_age, _("\
17057 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17058 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17059 A higher limit means that cached compilation units will be stored\n\
17060 in memory longer, and more total memory will be used. Zero disables\n\
17061 caching, which can slow down startup."),
17062 NULL,
17063 show_dwarf2_max_cache_age,
17064 &set_dwarf2_cmdlist,
17065 &show_dwarf2_cmdlist);
17066
17067 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17068 &dwarf2_always_disassemble, _("\
17069 Set whether `info address' always disassembles DWARF expressions."), _("\
17070 Show whether `info address' always disassembles DWARF expressions."), _("\
17071 When enabled, DWARF expressions are always printed in an assembly-like\n\
17072 syntax. When disabled, expressions will be printed in a more\n\
17073 conversational style, when possible."),
17074 NULL,
17075 show_dwarf2_always_disassemble,
17076 &set_dwarf2_cmdlist,
17077 &show_dwarf2_cmdlist);
17078
17079 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17080 Set debugging of the dwarf2 DIE reader."), _("\
17081 Show debugging of the dwarf2 DIE reader."), _("\
17082 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17083 The value is the maximum depth to print."),
17084 NULL,
17085 NULL,
17086 &setdebuglist, &showdebuglist);
17087
17088 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17089 Set cross-checking of \"physname\" code against demangler."), _("\
17090 Show cross-checking of \"physname\" code against demangler."), _("\
17091 When enabled, GDB's internal \"physname\" code is checked against\n\
17092 the demangler."),
17093 NULL, show_check_physname,
17094 &setdebuglist, &showdebuglist);
17095
17096 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17097 _("\
17098 Save a gdb-index file.\n\
17099 Usage: save gdb-index DIRECTORY"),
17100 &save_cmdlist);
17101 set_cmd_completer (c, filename_completer);
17102 }
This page took 0.407731 seconds and 5 git commands to generate.