* dwarf2read.c (dwarf2_per_cu_data): Remove outdated comment.
[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 #include "defs.h"
28 #include "bfd.h"
29 #include "symtab.h"
30 #include "gdbtypes.h"
31 #include "objfiles.h"
32 #include "dwarf2.h"
33 #include "buildsym.h"
34 #include "demangle.h"
35 #include "gdb-demangle.h"
36 #include "expression.h"
37 #include "filenames.h" /* for DOSish file names */
38 #include "macrotab.h"
39 #include "language.h"
40 #include "complaints.h"
41 #include "bcache.h"
42 #include "dwarf2expr.h"
43 #include "dwarf2loc.h"
44 #include "cp-support.h"
45 #include "hashtab.h"
46 #include "command.h"
47 #include "gdbcmd.h"
48 #include "block.h"
49 #include "addrmap.h"
50 #include "typeprint.h"
51 #include "jv-lang.h"
52 #include "psympriv.h"
53 #include "exceptions.h"
54 #include "gdb_stat.h"
55 #include "completer.h"
56 #include "vec.h"
57 #include "c-lang.h"
58 #include "valprint.h"
59 #include <ctype.h>
60
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82 typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98 typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in. */
119 static int dwarf2_die_debug = 0;
120
121 /* When non-zero, cross-check physname against demangler. */
122 static int check_physname = 0;
123
124 static int pagesize;
125
126 /* When set, the file that we're processing is known to have debugging
127 info for C++ namespaces. GCC 3.3.x did not produce this information,
128 but later versions do. */
129
130 static int processing_has_namespace_info;
131
132 static const struct objfile_data *dwarf2_objfile_data_key;
133
134 struct dwarf2_section_info
135 {
136 asection *asection;
137 gdb_byte *buffer;
138 bfd_size_type size;
139 /* Not NULL if the section was actually mmapped. */
140 void *map_addr;
141 /* Page aligned size of mmapped area. */
142 bfd_size_type map_len;
143 /* True if we have tried to read this section. */
144 int readin;
145 };
146
147 typedef struct dwarf2_section_info dwarf2_section_info_def;
148 DEF_VEC_O (dwarf2_section_info_def);
149
150 /* All offsets in the index are of this type. It must be
151 architecture-independent. */
152 typedef uint32_t offset_type;
153
154 DEF_VEC_I (offset_type);
155
156 /* A description of the mapped index. The file format is described in
157 a comment by the code that writes the index. */
158 struct mapped_index
159 {
160 /* Index data format version. */
161 int version;
162
163 /* The total length of the buffer. */
164 off_t total_size;
165
166 /* A pointer to the address table data. */
167 const gdb_byte *address_table;
168
169 /* Size of the address table data in bytes. */
170 offset_type address_table_size;
171
172 /* The symbol table, implemented as a hash table. */
173 const offset_type *symbol_table;
174
175 /* Size in slots, each slot is 2 offset_types. */
176 offset_type symbol_table_slots;
177
178 /* A pointer to the constant pool. */
179 const char *constant_pool;
180 };
181
182 /* Collection of data recorded per objfile.
183 This hangs off of dwarf2_objfile_data_key. */
184
185 struct dwarf2_per_objfile
186 {
187 struct dwarf2_section_info info;
188 struct dwarf2_section_info abbrev;
189 struct dwarf2_section_info line;
190 struct dwarf2_section_info loc;
191 struct dwarf2_section_info macinfo;
192 struct dwarf2_section_info macro;
193 struct dwarf2_section_info str;
194 struct dwarf2_section_info ranges;
195 struct dwarf2_section_info frame;
196 struct dwarf2_section_info eh_frame;
197 struct dwarf2_section_info gdb_index;
198
199 VEC (dwarf2_section_info_def) *types;
200
201 /* Back link. */
202 struct objfile *objfile;
203
204 /* A list of all the compilation units. This is used to locate
205 the target compilation unit of a particular reference. */
206 struct dwarf2_per_cu_data **all_comp_units;
207
208 /* The number of compilation units in ALL_COMP_UNITS. */
209 int n_comp_units;
210
211 /* The number of .debug_types-related CUs. */
212 int n_type_comp_units;
213
214 /* The .debug_types-related CUs. */
215 struct dwarf2_per_cu_data **type_comp_units;
216
217 /* A chain of compilation units that are currently read in, so that
218 they can be freed later. */
219 struct dwarf2_per_cu_data *read_in_chain;
220
221 /* A table mapping .debug_types signatures to its signatured_type entry.
222 This is NULL if the .debug_types section hasn't been read in yet. */
223 htab_t signatured_types;
224
225 /* A flag indicating wether this objfile has a section loaded at a
226 VMA of 0. */
227 int has_section_at_zero;
228
229 /* True if we are using the mapped index,
230 or we are faking it for OBJF_READNOW's sake. */
231 unsigned char using_index;
232
233 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
234 struct mapped_index *index_table;
235
236 /* When using index_table, this keeps track of all quick_file_names entries.
237 TUs can share line table entries with CUs or other TUs, and there can be
238 a lot more TUs than unique line tables, so we maintain a separate table
239 of all line table entries to support the sharing. */
240 htab_t quick_file_names_table;
241
242 /* Set during partial symbol reading, to prevent queueing of full
243 symbols. */
244 int reading_partial_symbols;
245
246 /* Table mapping type .debug_info DIE offsets to types.
247 This is NULL if not allocated yet.
248 It (currently) makes sense to allocate debug_types_type_hash lazily.
249 To keep things simple we allocate both lazily. */
250 htab_t debug_info_type_hash;
251
252 /* Table mapping type .debug_types DIE offsets to types.
253 This is NULL if not allocated yet. */
254 htab_t debug_types_type_hash;
255 };
256
257 static struct dwarf2_per_objfile *dwarf2_per_objfile;
258
259 /* Default names of the debugging sections. */
260
261 /* Note that if the debugging section has been compressed, it might
262 have a name like .zdebug_info. */
263
264 static const struct dwarf2_debug_sections dwarf2_elf_names =
265 {
266 { ".debug_info", ".zdebug_info" },
267 { ".debug_abbrev", ".zdebug_abbrev" },
268 { ".debug_line", ".zdebug_line" },
269 { ".debug_loc", ".zdebug_loc" },
270 { ".debug_macinfo", ".zdebug_macinfo" },
271 { ".debug_macro", ".zdebug_macro" },
272 { ".debug_str", ".zdebug_str" },
273 { ".debug_ranges", ".zdebug_ranges" },
274 { ".debug_types", ".zdebug_types" },
275 { ".debug_frame", ".zdebug_frame" },
276 { ".eh_frame", NULL },
277 { ".gdb_index", ".zgdb_index" },
278 23
279 };
280
281 /* local data types */
282
283 /* We hold several abbreviation tables in memory at the same time. */
284 #ifndef ABBREV_HASH_SIZE
285 #define ABBREV_HASH_SIZE 121
286 #endif
287
288 /* The data in a compilation unit header, after target2host
289 translation, looks like this. */
290 struct comp_unit_head
291 {
292 unsigned int length;
293 short version;
294 unsigned char addr_size;
295 unsigned char signed_addr_p;
296 unsigned int abbrev_offset;
297
298 /* Size of file offsets; either 4 or 8. */
299 unsigned int offset_size;
300
301 /* Size of the length field; either 4 or 12. */
302 unsigned int initial_length_size;
303
304 /* Offset to the first byte of this compilation unit header in the
305 .debug_info section, for resolving relative reference dies. */
306 unsigned int offset;
307
308 /* Offset to first die in this cu from the start of the cu.
309 This will be the first byte following the compilation unit header. */
310 unsigned int first_die_offset;
311 };
312
313 /* Type used for delaying computation of method physnames.
314 See comments for compute_delayed_physnames. */
315 struct delayed_method_info
316 {
317 /* The type to which the method is attached, i.e., its parent class. */
318 struct type *type;
319
320 /* The index of the method in the type's function fieldlists. */
321 int fnfield_index;
322
323 /* The index of the method in the fieldlist. */
324 int index;
325
326 /* The name of the DIE. */
327 const char *name;
328
329 /* The DIE associated with this method. */
330 struct die_info *die;
331 };
332
333 typedef struct delayed_method_info delayed_method_info;
334 DEF_VEC_O (delayed_method_info);
335
336 /* Internal state when decoding a particular compilation unit. */
337 struct dwarf2_cu
338 {
339 /* The objfile containing this compilation unit. */
340 struct objfile *objfile;
341
342 /* The header of the compilation unit. */
343 struct comp_unit_head header;
344
345 /* Base address of this compilation unit. */
346 CORE_ADDR base_address;
347
348 /* Non-zero if base_address has been set. */
349 int base_known;
350
351 /* The language we are debugging. */
352 enum language language;
353 const struct language_defn *language_defn;
354
355 const char *producer;
356
357 /* The generic symbol table building routines have separate lists for
358 file scope symbols and all all other scopes (local scopes). So
359 we need to select the right one to pass to add_symbol_to_list().
360 We do it by keeping a pointer to the correct list in list_in_scope.
361
362 FIXME: The original dwarf code just treated the file scope as the
363 first local scope, and all other local scopes as nested local
364 scopes, and worked fine. Check to see if we really need to
365 distinguish these in buildsym.c. */
366 struct pending **list_in_scope;
367
368 /* DWARF abbreviation table associated with this compilation unit. */
369 struct abbrev_info **dwarf2_abbrevs;
370
371 /* Storage for the abbrev table. */
372 struct obstack abbrev_obstack;
373
374 /* Hash table holding all the loaded partial DIEs. */
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
393 /* A hash table of die offsets for following references. */
394 htab_t die_hash;
395
396 /* Full DIEs if read in. */
397 struct die_info *dies;
398
399 /* A set of pointers to dwarf2_per_cu_data objects for compilation
400 units referenced by this one. Only set during full symbol processing;
401 partial symbol tables do not have dependencies. */
402 htab_t dependencies;
403
404 /* Header data from the line table, during full symbol processing. */
405 struct line_header *line_header;
406
407 /* A list of methods which need to have physnames computed
408 after all type information has been read. */
409 VEC (delayed_method_info) *method_list;
410
411 /* To be copied to symtab->call_site_htab. */
412 htab_t call_site_htab;
413
414 /* Mark used when releasing cached dies. */
415 unsigned int mark : 1;
416
417 /* This flag will be set if this compilation unit might include
418 inter-compilation-unit references. */
419 unsigned int has_form_ref_addr : 1;
420
421 /* This flag will be set if this compilation unit includes any
422 DW_TAG_namespace DIEs. If we know that there are explicit
423 DIEs for namespaces, we don't need to try to infer them
424 from mangled names. */
425 unsigned int has_namespace_info : 1;
426
427 /* This CU references .debug_loc. See the symtab->locations_valid field.
428 This test is imperfect as there may exist optimized debug code not using
429 any location list and still facing inlining issues if handled as
430 unoptimized code. For a future better test see GCC PR other/32998. */
431 unsigned int has_loclist : 1;
432 };
433
434 /* Persistent data held for a compilation unit, even when not
435 processing it. We put a pointer to this structure in the
436 read_symtab_private field of the psymtab. */
437
438 struct dwarf2_per_cu_data
439 {
440 /* The start offset and length of this compilation unit. 2**29-1
441 bytes should suffice to store the length of any compilation unit
442 - if it doesn't, GDB will fall over anyway.
443 NOTE: Unlike comp_unit_head.length, this length includes
444 initial_length_size. */
445 unsigned int offset;
446 unsigned int length : 29;
447
448 /* Flag indicating this compilation unit will be read in before
449 any of the current compilation units are processed. */
450 unsigned int queued : 1;
451
452 /* This flag will be set if we need to load absolutely all DIEs
453 for this compilation unit, instead of just the ones we think
454 are interesting. It gets set if we look for a DIE in the
455 hash table and don't find it. */
456 unsigned int load_all_dies : 1;
457
458 /* Non-null if this CU is from .debug_types; in which case it points
459 to the section. Otherwise it's from .debug_info. */
460 struct dwarf2_section_info *debug_types_section;
461
462 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
463 of the CU cache it gets reset to NULL again. */
464 struct dwarf2_cu *cu;
465
466 /* The corresponding objfile.
467 Normally we can get the objfile from dwarf2_per_objfile.
468 However we can enter this file with just a "per_cu" handle. */
469 struct objfile *objfile;
470
471 /* When using partial symbol tables, the 'psymtab' field is active.
472 Otherwise the 'quick' field is active. */
473 union
474 {
475 /* The partial symbol table associated with this compilation unit,
476 or NULL for partial units (which do not have an associated
477 symtab). */
478 struct partial_symtab *psymtab;
479
480 /* Data needed by the "quick" functions. */
481 struct dwarf2_per_cu_quick_data *quick;
482 } v;
483 };
484
485 /* Entry in the signatured_types hash table. */
486
487 struct signatured_type
488 {
489 ULONGEST signature;
490
491 /* Offset in .debug_types of the type defined by this TU. */
492 unsigned int type_offset;
493
494 /* The CU(/TU) of this type. */
495 struct dwarf2_per_cu_data per_cu;
496 };
497
498 /* Struct used to pass misc. parameters to read_die_and_children, et
499 al. which are used for both .debug_info and .debug_types dies.
500 All parameters here are unchanging for the life of the call. This
501 struct exists to abstract away the constant parameters of die
502 reading. */
503
504 struct die_reader_specs
505 {
506 /* The bfd of this objfile. */
507 bfd* abfd;
508
509 /* The CU of the DIE we are parsing. */
510 struct dwarf2_cu *cu;
511
512 /* Pointer to start of section buffer.
513 This is either the start of .debug_info or .debug_types. */
514 const gdb_byte *buffer;
515 };
516
517 /* The line number information for a compilation unit (found in the
518 .debug_line section) begins with a "statement program header",
519 which contains the following information. */
520 struct line_header
521 {
522 unsigned int total_length;
523 unsigned short version;
524 unsigned int header_length;
525 unsigned char minimum_instruction_length;
526 unsigned char maximum_ops_per_instruction;
527 unsigned char default_is_stmt;
528 int line_base;
529 unsigned char line_range;
530 unsigned char opcode_base;
531
532 /* standard_opcode_lengths[i] is the number of operands for the
533 standard opcode whose value is i. This means that
534 standard_opcode_lengths[0] is unused, and the last meaningful
535 element is standard_opcode_lengths[opcode_base - 1]. */
536 unsigned char *standard_opcode_lengths;
537
538 /* The include_directories table. NOTE! These strings are not
539 allocated with xmalloc; instead, they are pointers into
540 debug_line_buffer. If you try to free them, `free' will get
541 indigestion. */
542 unsigned int num_include_dirs, include_dirs_size;
543 char **include_dirs;
544
545 /* The file_names table. NOTE! These strings are not allocated
546 with xmalloc; instead, they are pointers into debug_line_buffer.
547 Don't try to free them directly. */
548 unsigned int num_file_names, file_names_size;
549 struct file_entry
550 {
551 char *name;
552 unsigned int dir_index;
553 unsigned int mod_time;
554 unsigned int length;
555 int included_p; /* Non-zero if referenced by the Line Number Program. */
556 struct symtab *symtab; /* The associated symbol table, if any. */
557 } *file_names;
558
559 /* The start and end of the statement program following this
560 header. These point into dwarf2_per_objfile->line_buffer. */
561 gdb_byte *statement_program_start, *statement_program_end;
562 };
563
564 /* When we construct a partial symbol table entry we only
565 need this much information. */
566 struct partial_die_info
567 {
568 /* Offset of this DIE. */
569 unsigned int offset;
570
571 /* DWARF-2 tag for this DIE. */
572 ENUM_BITFIELD(dwarf_tag) tag : 16;
573
574 /* Assorted flags describing the data found in this DIE. */
575 unsigned int has_children : 1;
576 unsigned int is_external : 1;
577 unsigned int is_declaration : 1;
578 unsigned int has_type : 1;
579 unsigned int has_specification : 1;
580 unsigned int has_pc_info : 1;
581
582 /* Flag set if the SCOPE field of this structure has been
583 computed. */
584 unsigned int scope_set : 1;
585
586 /* Flag set if the DIE has a byte_size attribute. */
587 unsigned int has_byte_size : 1;
588
589 /* Flag set if any of the DIE's children are template arguments. */
590 unsigned int has_template_arguments : 1;
591
592 /* Flag set if fixup_partial_die has been called on this die. */
593 unsigned int fixup_called : 1;
594
595 /* The name of this DIE. Normally the value of DW_AT_name, but
596 sometimes a default name for unnamed DIEs. */
597 char *name;
598
599 /* The linkage name, if present. */
600 const char *linkage_name;
601
602 /* The scope to prepend to our children. This is generally
603 allocated on the comp_unit_obstack, so will disappear
604 when this compilation unit leaves the cache. */
605 char *scope;
606
607 /* The location description associated with this DIE, if any. */
608 struct dwarf_block *locdesc;
609
610 /* If HAS_PC_INFO, the PC range associated with this DIE. */
611 CORE_ADDR lowpc;
612 CORE_ADDR highpc;
613
614 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
615 DW_AT_sibling, if any. */
616 /* NOTE: This member isn't strictly necessary, read_partial_die could
617 return DW_AT_sibling values to its caller load_partial_dies. */
618 gdb_byte *sibling;
619
620 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
621 DW_AT_specification (or DW_AT_abstract_origin or
622 DW_AT_extension). */
623 unsigned int spec_offset;
624
625 /* Pointers to this DIE's parent, first child, and next sibling,
626 if any. */
627 struct partial_die_info *die_parent, *die_child, *die_sibling;
628 };
629
630 /* This data structure holds the information of an abbrev. */
631 struct abbrev_info
632 {
633 unsigned int number; /* number identifying abbrev */
634 enum dwarf_tag tag; /* dwarf tag */
635 unsigned short has_children; /* boolean */
636 unsigned short num_attrs; /* number of attributes */
637 struct attr_abbrev *attrs; /* an array of attribute descriptions */
638 struct abbrev_info *next; /* next in chain */
639 };
640
641 struct attr_abbrev
642 {
643 ENUM_BITFIELD(dwarf_attribute) name : 16;
644 ENUM_BITFIELD(dwarf_form) form : 16;
645 };
646
647 /* Attributes have a name and a value. */
648 struct attribute
649 {
650 ENUM_BITFIELD(dwarf_attribute) name : 16;
651 ENUM_BITFIELD(dwarf_form) form : 15;
652
653 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
654 field should be in u.str (existing only for DW_STRING) but it is kept
655 here for better struct attribute alignment. */
656 unsigned int string_is_canonical : 1;
657
658 union
659 {
660 char *str;
661 struct dwarf_block *blk;
662 ULONGEST unsnd;
663 LONGEST snd;
664 CORE_ADDR addr;
665 struct signatured_type *signatured_type;
666 }
667 u;
668 };
669
670 /* This data structure holds a complete die structure. */
671 struct die_info
672 {
673 /* DWARF-2 tag for this DIE. */
674 ENUM_BITFIELD(dwarf_tag) tag : 16;
675
676 /* Number of attributes */
677 unsigned char num_attrs;
678
679 /* True if we're presently building the full type name for the
680 type derived from this DIE. */
681 unsigned char building_fullname : 1;
682
683 /* Abbrev number */
684 unsigned int abbrev;
685
686 /* Offset in .debug_info or .debug_types section. */
687 unsigned int offset;
688
689 /* The dies in a compilation unit form an n-ary tree. PARENT
690 points to this die's parent; CHILD points to the first child of
691 this node; and all the children of a given node are chained
692 together via their SIBLING fields. */
693 struct die_info *child; /* Its first child, if any. */
694 struct die_info *sibling; /* Its next sibling, if any. */
695 struct die_info *parent; /* Its parent, if any. */
696
697 /* An array of attributes, with NUM_ATTRS elements. There may be
698 zero, but it's not common and zero-sized arrays are not
699 sufficiently portable C. */
700 struct attribute attrs[1];
701 };
702
703 /* Get at parts of an attribute structure. */
704
705 #define DW_STRING(attr) ((attr)->u.str)
706 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
707 #define DW_UNSND(attr) ((attr)->u.unsnd)
708 #define DW_BLOCK(attr) ((attr)->u.blk)
709 #define DW_SND(attr) ((attr)->u.snd)
710 #define DW_ADDR(attr) ((attr)->u.addr)
711 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
712
713 /* Blocks are a bunch of untyped bytes. */
714 struct dwarf_block
715 {
716 unsigned int size;
717
718 /* Valid only if SIZE is not zero. */
719 gdb_byte *data;
720 };
721
722 #ifndef ATTR_ALLOC_CHUNK
723 #define ATTR_ALLOC_CHUNK 4
724 #endif
725
726 /* Allocate fields for structs, unions and enums in this size. */
727 #ifndef DW_FIELD_ALLOC_CHUNK
728 #define DW_FIELD_ALLOC_CHUNK 4
729 #endif
730
731 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
732 but this would require a corresponding change in unpack_field_as_long
733 and friends. */
734 static int bits_per_byte = 8;
735
736 /* The routines that read and process dies for a C struct or C++ class
737 pass lists of data member fields and lists of member function fields
738 in an instance of a field_info structure, as defined below. */
739 struct field_info
740 {
741 /* List of data member and baseclasses fields. */
742 struct nextfield
743 {
744 struct nextfield *next;
745 int accessibility;
746 int virtuality;
747 struct field field;
748 }
749 *fields, *baseclasses;
750
751 /* Number of fields (including baseclasses). */
752 int nfields;
753
754 /* Number of baseclasses. */
755 int nbaseclasses;
756
757 /* Set if the accesibility of one of the fields is not public. */
758 int non_public_fields;
759
760 /* Member function fields array, entries are allocated in the order they
761 are encountered in the object file. */
762 struct nextfnfield
763 {
764 struct nextfnfield *next;
765 struct fn_field fnfield;
766 }
767 *fnfields;
768
769 /* Member function fieldlist array, contains name of possibly overloaded
770 member function, number of overloaded member functions and a pointer
771 to the head of the member function field chain. */
772 struct fnfieldlist
773 {
774 char *name;
775 int length;
776 struct nextfnfield *head;
777 }
778 *fnfieldlists;
779
780 /* Number of entries in the fnfieldlists array. */
781 int nfnfields;
782
783 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
784 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
785 struct typedef_field_list
786 {
787 struct typedef_field field;
788 struct typedef_field_list *next;
789 }
790 *typedef_field_list;
791 unsigned typedef_field_list_count;
792 };
793
794 /* One item on the queue of compilation units to read in full symbols
795 for. */
796 struct dwarf2_queue_item
797 {
798 struct dwarf2_per_cu_data *per_cu;
799 struct dwarf2_queue_item *next;
800 };
801
802 /* The current queue. */
803 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
804
805 /* Loaded secondary compilation units are kept in memory until they
806 have not been referenced for the processing of this many
807 compilation units. Set this to zero to disable caching. Cache
808 sizes of up to at least twenty will improve startup time for
809 typical inter-CU-reference binaries, at an obvious memory cost. */
810 static int dwarf2_max_cache_age = 5;
811 static void
812 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
813 struct cmd_list_element *c, const char *value)
814 {
815 fprintf_filtered (file, _("The upper bound on the age of cached "
816 "dwarf2 compilation units is %s.\n"),
817 value);
818 }
819
820
821 /* Various complaints about symbol reading that don't abort the process. */
822
823 static void
824 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
825 {
826 complaint (&symfile_complaints,
827 _("statement list doesn't fit in .debug_line section"));
828 }
829
830 static void
831 dwarf2_debug_line_missing_file_complaint (void)
832 {
833 complaint (&symfile_complaints,
834 _(".debug_line section has line data without a file"));
835 }
836
837 static void
838 dwarf2_debug_line_missing_end_sequence_complaint (void)
839 {
840 complaint (&symfile_complaints,
841 _(".debug_line section has line "
842 "program sequence without an end"));
843 }
844
845 static void
846 dwarf2_complex_location_expr_complaint (void)
847 {
848 complaint (&symfile_complaints, _("location expression too complex"));
849 }
850
851 static void
852 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
853 int arg3)
854 {
855 complaint (&symfile_complaints,
856 _("const value length mismatch for '%s', got %d, expected %d"),
857 arg1, arg2, arg3);
858 }
859
860 static void
861 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
862 {
863 complaint (&symfile_complaints,
864 _("macro info runs off end of `%s' section"),
865 section->asection->name);
866 }
867
868 static void
869 dwarf2_macro_malformed_definition_complaint (const char *arg1)
870 {
871 complaint (&symfile_complaints,
872 _("macro debug info contains a "
873 "malformed macro definition:\n`%s'"),
874 arg1);
875 }
876
877 static void
878 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
879 {
880 complaint (&symfile_complaints,
881 _("invalid attribute class or form for '%s' in '%s'"),
882 arg1, arg2);
883 }
884
885 /* local function prototypes */
886
887 static void dwarf2_locate_sections (bfd *, asection *, void *);
888
889 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
890 struct objfile *);
891
892 static void dwarf2_find_base_address (struct die_info *die,
893 struct dwarf2_cu *cu);
894
895 static void dwarf2_build_psymtabs_hard (struct objfile *);
896
897 static void scan_partial_symbols (struct partial_die_info *,
898 CORE_ADDR *, CORE_ADDR *,
899 int, struct dwarf2_cu *);
900
901 static void add_partial_symbol (struct partial_die_info *,
902 struct dwarf2_cu *);
903
904 static void add_partial_namespace (struct partial_die_info *pdi,
905 CORE_ADDR *lowpc, CORE_ADDR *highpc,
906 int need_pc, struct dwarf2_cu *cu);
907
908 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
909 CORE_ADDR *highpc, int need_pc,
910 struct dwarf2_cu *cu);
911
912 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
913 struct dwarf2_cu *cu);
914
915 static void add_partial_subprogram (struct partial_die_info *pdi,
916 CORE_ADDR *lowpc, CORE_ADDR *highpc,
917 int need_pc, struct dwarf2_cu *cu);
918
919 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
920 gdb_byte *buffer, gdb_byte *info_ptr,
921 bfd *abfd, struct dwarf2_cu *cu);
922
923 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
924
925 static void psymtab_to_symtab_1 (struct partial_symtab *);
926
927 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
928
929 static void dwarf2_free_abbrev_table (void *);
930
931 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
932
933 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
934 struct dwarf2_cu *);
935
936 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
937 struct dwarf2_cu *);
938
939 static struct partial_die_info *load_partial_dies (bfd *,
940 gdb_byte *, gdb_byte *,
941 int, struct dwarf2_cu *);
942
943 static gdb_byte *read_partial_die (struct partial_die_info *,
944 struct abbrev_info *abbrev,
945 unsigned int, bfd *,
946 gdb_byte *, gdb_byte *,
947 struct dwarf2_cu *);
948
949 static struct partial_die_info *find_partial_die (unsigned int,
950 struct dwarf2_cu *);
951
952 static void fixup_partial_die (struct partial_die_info *,
953 struct dwarf2_cu *);
954
955 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
956 bfd *, gdb_byte *, struct dwarf2_cu *);
957
958 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
959 bfd *, gdb_byte *, struct dwarf2_cu *);
960
961 static unsigned int read_1_byte (bfd *, gdb_byte *);
962
963 static int read_1_signed_byte (bfd *, gdb_byte *);
964
965 static unsigned int read_2_bytes (bfd *, gdb_byte *);
966
967 static unsigned int read_4_bytes (bfd *, gdb_byte *);
968
969 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
970
971 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
972 unsigned int *);
973
974 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
975
976 static LONGEST read_checked_initial_length_and_offset
977 (bfd *, gdb_byte *, const struct comp_unit_head *,
978 unsigned int *, unsigned int *);
979
980 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
981 unsigned int *);
982
983 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
984
985 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
986
987 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
988
989 static char *read_indirect_string (bfd *, gdb_byte *,
990 const struct comp_unit_head *,
991 unsigned int *);
992
993 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
994
995 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
996
997 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
998
999 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1000
1001 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1002 struct dwarf2_cu *);
1003
1004 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1005 unsigned int,
1006 struct dwarf2_cu *);
1007
1008 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1009 struct dwarf2_cu *cu);
1010
1011 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1012
1013 static struct die_info *die_specification (struct die_info *die,
1014 struct dwarf2_cu **);
1015
1016 static void free_line_header (struct line_header *lh);
1017
1018 static void add_file_name (struct line_header *, char *, unsigned int,
1019 unsigned int, unsigned int);
1020
1021 static struct line_header *(dwarf_decode_line_header
1022 (unsigned int offset,
1023 bfd *abfd, struct dwarf2_cu *cu));
1024
1025 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1026 struct dwarf2_cu *, struct partial_symtab *);
1027
1028 static void dwarf2_start_subfile (char *, const char *, const char *);
1029
1030 static struct symbol *new_symbol (struct die_info *, struct type *,
1031 struct dwarf2_cu *);
1032
1033 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1034 struct dwarf2_cu *, struct symbol *);
1035
1036 static void dwarf2_const_value (struct attribute *, struct symbol *,
1037 struct dwarf2_cu *);
1038
1039 static void dwarf2_const_value_attr (struct attribute *attr,
1040 struct type *type,
1041 const char *name,
1042 struct obstack *obstack,
1043 struct dwarf2_cu *cu, long *value,
1044 gdb_byte **bytes,
1045 struct dwarf2_locexpr_baton **baton);
1046
1047 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1048
1049 static int need_gnat_info (struct dwarf2_cu *);
1050
1051 static struct type *die_descriptive_type (struct die_info *,
1052 struct dwarf2_cu *);
1053
1054 static void set_descriptive_type (struct type *, struct die_info *,
1055 struct dwarf2_cu *);
1056
1057 static struct type *die_containing_type (struct die_info *,
1058 struct dwarf2_cu *);
1059
1060 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1061 struct dwarf2_cu *);
1062
1063 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1064
1065 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1066
1067 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1068
1069 static char *typename_concat (struct obstack *obs, const char *prefix,
1070 const char *suffix, int physname,
1071 struct dwarf2_cu *cu);
1072
1073 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1074
1075 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1076
1077 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1078
1079 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1080
1081 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1082
1083 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1084 struct dwarf2_cu *, struct partial_symtab *);
1085
1086 static int dwarf2_get_pc_bounds (struct die_info *,
1087 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1088 struct partial_symtab *);
1089
1090 static void get_scope_pc_bounds (struct die_info *,
1091 CORE_ADDR *, CORE_ADDR *,
1092 struct dwarf2_cu *);
1093
1094 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1095 CORE_ADDR, struct dwarf2_cu *);
1096
1097 static void dwarf2_add_field (struct field_info *, struct die_info *,
1098 struct dwarf2_cu *);
1099
1100 static void dwarf2_attach_fields_to_type (struct field_info *,
1101 struct type *, struct dwarf2_cu *);
1102
1103 static void dwarf2_add_member_fn (struct field_info *,
1104 struct die_info *, struct type *,
1105 struct dwarf2_cu *);
1106
1107 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1108 struct type *,
1109 struct dwarf2_cu *);
1110
1111 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1112
1113 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1114
1115 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1116
1117 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1118
1119 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1120
1121 static struct type *read_module_type (struct die_info *die,
1122 struct dwarf2_cu *cu);
1123
1124 static const char *namespace_name (struct die_info *die,
1125 int *is_anonymous, struct dwarf2_cu *);
1126
1127 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1128
1129 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1130
1131 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1132 struct dwarf2_cu *);
1133
1134 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1135
1136 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1137 gdb_byte *info_ptr,
1138 gdb_byte **new_info_ptr,
1139 struct die_info *parent);
1140
1141 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1142 gdb_byte *info_ptr,
1143 gdb_byte **new_info_ptr,
1144 struct die_info *parent);
1145
1146 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1147 gdb_byte *info_ptr,
1148 gdb_byte **new_info_ptr,
1149 struct die_info *parent);
1150
1151 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1152 struct die_info **, gdb_byte *,
1153 int *);
1154
1155 static void process_die (struct die_info *, struct dwarf2_cu *);
1156
1157 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1158 struct obstack *);
1159
1160 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1161
1162 static const char *dwarf2_full_name (char *name,
1163 struct die_info *die,
1164 struct dwarf2_cu *cu);
1165
1166 static struct die_info *dwarf2_extension (struct die_info *die,
1167 struct dwarf2_cu **);
1168
1169 static char *dwarf_tag_name (unsigned int);
1170
1171 static char *dwarf_attr_name (unsigned int);
1172
1173 static char *dwarf_form_name (unsigned int);
1174
1175 static char *dwarf_bool_name (unsigned int);
1176
1177 static char *dwarf_type_encoding_name (unsigned int);
1178
1179 #if 0
1180 static char *dwarf_cfi_name (unsigned int);
1181 #endif
1182
1183 static struct die_info *sibling_die (struct die_info *);
1184
1185 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1186
1187 static void dump_die_for_error (struct die_info *);
1188
1189 static void dump_die_1 (struct ui_file *, int level, int max_level,
1190 struct die_info *);
1191
1192 /*static*/ void dump_die (struct die_info *, int max_level);
1193
1194 static void store_in_ref_table (struct die_info *,
1195 struct dwarf2_cu *);
1196
1197 static int is_ref_attr (struct attribute *);
1198
1199 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1200
1201 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1202
1203 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1204 struct attribute *,
1205 struct dwarf2_cu **);
1206
1207 static struct die_info *follow_die_ref (struct die_info *,
1208 struct attribute *,
1209 struct dwarf2_cu **);
1210
1211 static struct die_info *follow_die_sig (struct die_info *,
1212 struct attribute *,
1213 struct dwarf2_cu **);
1214
1215 static struct signatured_type *lookup_signatured_type_at_offset
1216 (struct objfile *objfile,
1217 struct dwarf2_section_info *section,
1218 unsigned int offset);
1219
1220 static void read_signatured_type_at_offset (struct objfile *objfile,
1221 struct dwarf2_section_info *sect,
1222 unsigned int offset);
1223
1224 static void read_signatured_type (struct signatured_type *type_sig);
1225
1226 /* memory allocation interface */
1227
1228 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1229
1230 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1231
1232 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1233
1234 static void dwarf_decode_macros (struct line_header *, unsigned int,
1235 char *, bfd *, struct dwarf2_cu *,
1236 struct dwarf2_section_info *,
1237 int);
1238
1239 static int attr_form_is_block (struct attribute *);
1240
1241 static int attr_form_is_section_offset (struct attribute *);
1242
1243 static int attr_form_is_constant (struct attribute *);
1244
1245 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1246 struct dwarf2_loclist_baton *baton,
1247 struct attribute *attr);
1248
1249 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1250 struct symbol *sym,
1251 struct dwarf2_cu *cu);
1252
1253 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1254 struct abbrev_info *abbrev,
1255 struct dwarf2_cu *cu);
1256
1257 static void free_stack_comp_unit (void *);
1258
1259 static hashval_t partial_die_hash (const void *item);
1260
1261 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1262
1263 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1264 (unsigned int offset, struct objfile *objfile);
1265
1266 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1267 (unsigned int offset, struct objfile *objfile);
1268
1269 static void init_one_comp_unit (struct dwarf2_cu *cu,
1270 struct dwarf2_per_cu_data *per_cu);
1271
1272 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1273 struct die_info *comp_unit_die);
1274
1275 static void free_heap_comp_unit (void *);
1276
1277 static void free_cached_comp_units (void *);
1278
1279 static void age_cached_comp_units (void);
1280
1281 static void free_one_cached_comp_unit (void *);
1282
1283 static struct type *set_die_type (struct die_info *, struct type *,
1284 struct dwarf2_cu *);
1285
1286 static void create_all_comp_units (struct objfile *);
1287
1288 static int create_debug_types_hash_table (struct objfile *objfile);
1289
1290 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1291
1292 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1293
1294 static void dwarf2_add_dependence (struct dwarf2_cu *,
1295 struct dwarf2_per_cu_data *);
1296
1297 static void dwarf2_mark (struct dwarf2_cu *);
1298
1299 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1300
1301 static struct type *get_die_type_at_offset (unsigned int,
1302 struct dwarf2_per_cu_data *per_cu);
1303
1304 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1305
1306 static void dwarf2_release_queue (void *dummy);
1307
1308 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1309
1310 static void process_queue (void);
1311
1312 static void find_file_and_directory (struct die_info *die,
1313 struct dwarf2_cu *cu,
1314 char **name, char **comp_dir);
1315
1316 static char *file_full_name (int file, struct line_header *lh,
1317 const char *comp_dir);
1318
1319 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1320 gdb_byte *info_ptr,
1321 gdb_byte *buffer,
1322 unsigned int buffer_size,
1323 bfd *abfd,
1324 int is_debug_types_section);
1325
1326 static void init_cu_die_reader (struct die_reader_specs *reader,
1327 struct dwarf2_cu *cu);
1328
1329 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1330
1331 #if WORDS_BIGENDIAN
1332
1333 /* Convert VALUE between big- and little-endian. */
1334 static offset_type
1335 byte_swap (offset_type value)
1336 {
1337 offset_type result;
1338
1339 result = (value & 0xff) << 24;
1340 result |= (value & 0xff00) << 8;
1341 result |= (value & 0xff0000) >> 8;
1342 result |= (value & 0xff000000) >> 24;
1343 return result;
1344 }
1345
1346 #define MAYBE_SWAP(V) byte_swap (V)
1347
1348 #else
1349 #define MAYBE_SWAP(V) (V)
1350 #endif /* WORDS_BIGENDIAN */
1351
1352 /* The suffix for an index file. */
1353 #define INDEX_SUFFIX ".gdb-index"
1354
1355 static const char *dwarf2_physname (char *name, struct die_info *die,
1356 struct dwarf2_cu *cu);
1357
1358 /* Try to locate the sections we need for DWARF 2 debugging
1359 information and return true if we have enough to do something.
1360 NAMES points to the dwarf2 section names, or is NULL if the standard
1361 ELF names are used. */
1362
1363 int
1364 dwarf2_has_info (struct objfile *objfile,
1365 const struct dwarf2_debug_sections *names)
1366 {
1367 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1368 if (!dwarf2_per_objfile)
1369 {
1370 /* Initialize per-objfile state. */
1371 struct dwarf2_per_objfile *data
1372 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1373
1374 memset (data, 0, sizeof (*data));
1375 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1376 dwarf2_per_objfile = data;
1377
1378 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1379 (void *) names);
1380 dwarf2_per_objfile->objfile = objfile;
1381 }
1382 return (dwarf2_per_objfile->info.asection != NULL
1383 && dwarf2_per_objfile->abbrev.asection != NULL);
1384 }
1385
1386 /* When loading sections, we look either for uncompressed section or for
1387 compressed section names. */
1388
1389 static int
1390 section_is_p (const char *section_name,
1391 const struct dwarf2_section_names *names)
1392 {
1393 if (names->normal != NULL
1394 && strcmp (section_name, names->normal) == 0)
1395 return 1;
1396 if (names->compressed != NULL
1397 && strcmp (section_name, names->compressed) == 0)
1398 return 1;
1399 return 0;
1400 }
1401
1402 /* This function is mapped across the sections and remembers the
1403 offset and size of each of the debugging sections we are interested
1404 in. */
1405
1406 static void
1407 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1408 {
1409 const struct dwarf2_debug_sections *names;
1410
1411 if (vnames == NULL)
1412 names = &dwarf2_elf_names;
1413 else
1414 names = (const struct dwarf2_debug_sections *) vnames;
1415
1416 if (section_is_p (sectp->name, &names->info))
1417 {
1418 dwarf2_per_objfile->info.asection = sectp;
1419 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1420 }
1421 else if (section_is_p (sectp->name, &names->abbrev))
1422 {
1423 dwarf2_per_objfile->abbrev.asection = sectp;
1424 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1425 }
1426 else if (section_is_p (sectp->name, &names->line))
1427 {
1428 dwarf2_per_objfile->line.asection = sectp;
1429 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1430 }
1431 else if (section_is_p (sectp->name, &names->loc))
1432 {
1433 dwarf2_per_objfile->loc.asection = sectp;
1434 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1435 }
1436 else if (section_is_p (sectp->name, &names->macinfo))
1437 {
1438 dwarf2_per_objfile->macinfo.asection = sectp;
1439 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1440 }
1441 else if (section_is_p (sectp->name, &names->macro))
1442 {
1443 dwarf2_per_objfile->macro.asection = sectp;
1444 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1445 }
1446 else if (section_is_p (sectp->name, &names->str))
1447 {
1448 dwarf2_per_objfile->str.asection = sectp;
1449 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1450 }
1451 else if (section_is_p (sectp->name, &names->frame))
1452 {
1453 dwarf2_per_objfile->frame.asection = sectp;
1454 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1455 }
1456 else if (section_is_p (sectp->name, &names->eh_frame))
1457 {
1458 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1459
1460 if (aflag & SEC_HAS_CONTENTS)
1461 {
1462 dwarf2_per_objfile->eh_frame.asection = sectp;
1463 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1464 }
1465 }
1466 else if (section_is_p (sectp->name, &names->ranges))
1467 {
1468 dwarf2_per_objfile->ranges.asection = sectp;
1469 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1470 }
1471 else if (section_is_p (sectp->name, &names->types))
1472 {
1473 struct dwarf2_section_info type_section;
1474
1475 memset (&type_section, 0, sizeof (type_section));
1476 type_section.asection = sectp;
1477 type_section.size = bfd_get_section_size (sectp);
1478
1479 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1480 &type_section);
1481 }
1482 else if (section_is_p (sectp->name, &names->gdb_index))
1483 {
1484 dwarf2_per_objfile->gdb_index.asection = sectp;
1485 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1486 }
1487
1488 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1489 && bfd_section_vma (abfd, sectp) == 0)
1490 dwarf2_per_objfile->has_section_at_zero = 1;
1491 }
1492
1493 /* Decompress a section that was compressed using zlib. Store the
1494 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1495
1496 static void
1497 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1498 gdb_byte **outbuf, bfd_size_type *outsize)
1499 {
1500 bfd *abfd = objfile->obfd;
1501 #ifndef HAVE_ZLIB_H
1502 error (_("Support for zlib-compressed DWARF data (from '%s') "
1503 "is disabled in this copy of GDB"),
1504 bfd_get_filename (abfd));
1505 #else
1506 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1507 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1508 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1509 bfd_size_type uncompressed_size;
1510 gdb_byte *uncompressed_buffer;
1511 z_stream strm;
1512 int rc;
1513 int header_size = 12;
1514
1515 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1516 || bfd_bread (compressed_buffer,
1517 compressed_size, abfd) != compressed_size)
1518 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1519 bfd_get_filename (abfd));
1520
1521 /* Read the zlib header. In this case, it should be "ZLIB" followed
1522 by the uncompressed section size, 8 bytes in big-endian order. */
1523 if (compressed_size < header_size
1524 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1525 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1526 bfd_get_filename (abfd));
1527 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1528 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1529 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1530 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1531 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1532 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1533 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1534 uncompressed_size += compressed_buffer[11];
1535
1536 /* It is possible the section consists of several compressed
1537 buffers concatenated together, so we uncompress in a loop. */
1538 strm.zalloc = NULL;
1539 strm.zfree = NULL;
1540 strm.opaque = NULL;
1541 strm.avail_in = compressed_size - header_size;
1542 strm.next_in = (Bytef*) compressed_buffer + header_size;
1543 strm.avail_out = uncompressed_size;
1544 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1545 uncompressed_size);
1546 rc = inflateInit (&strm);
1547 while (strm.avail_in > 0)
1548 {
1549 if (rc != Z_OK)
1550 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1551 bfd_get_filename (abfd), rc);
1552 strm.next_out = ((Bytef*) uncompressed_buffer
1553 + (uncompressed_size - strm.avail_out));
1554 rc = inflate (&strm, Z_FINISH);
1555 if (rc != Z_STREAM_END)
1556 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1557 bfd_get_filename (abfd), rc);
1558 rc = inflateReset (&strm);
1559 }
1560 rc = inflateEnd (&strm);
1561 if (rc != Z_OK
1562 || strm.avail_out != 0)
1563 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1564 bfd_get_filename (abfd), rc);
1565
1566 do_cleanups (cleanup);
1567 *outbuf = uncompressed_buffer;
1568 *outsize = uncompressed_size;
1569 #endif
1570 }
1571
1572 /* A helper function that decides whether a section is empty. */
1573
1574 static int
1575 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1576 {
1577 return info->asection == NULL || info->size == 0;
1578 }
1579
1580 /* Read the contents of the section INFO from object file specified by
1581 OBJFILE, store info about the section into INFO.
1582 If the section is compressed, uncompress it before returning. */
1583
1584 static void
1585 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1586 {
1587 bfd *abfd = objfile->obfd;
1588 asection *sectp = info->asection;
1589 gdb_byte *buf, *retbuf;
1590 unsigned char header[4];
1591
1592 if (info->readin)
1593 return;
1594 info->buffer = NULL;
1595 info->map_addr = NULL;
1596 info->readin = 1;
1597
1598 if (dwarf2_section_empty_p (info))
1599 return;
1600
1601 /* Check if the file has a 4-byte header indicating compression. */
1602 if (info->size > sizeof (header)
1603 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1604 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1605 {
1606 /* Upon decompression, update the buffer and its size. */
1607 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1608 {
1609 zlib_decompress_section (objfile, sectp, &info->buffer,
1610 &info->size);
1611 return;
1612 }
1613 }
1614
1615 #ifdef HAVE_MMAP
1616 if (pagesize == 0)
1617 pagesize = getpagesize ();
1618
1619 /* Only try to mmap sections which are large enough: we don't want to
1620 waste space due to fragmentation. Also, only try mmap for sections
1621 without relocations. */
1622
1623 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1624 {
1625 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1626 MAP_PRIVATE, sectp->filepos,
1627 &info->map_addr, &info->map_len);
1628
1629 if ((caddr_t)info->buffer != MAP_FAILED)
1630 {
1631 #if HAVE_POSIX_MADVISE
1632 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1633 #endif
1634 return;
1635 }
1636 }
1637 #endif
1638
1639 /* If we get here, we are a normal, not-compressed section. */
1640 info->buffer = buf
1641 = obstack_alloc (&objfile->objfile_obstack, info->size);
1642
1643 /* When debugging .o files, we may need to apply relocations; see
1644 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1645 We never compress sections in .o files, so we only need to
1646 try this when the section is not compressed. */
1647 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1648 if (retbuf != NULL)
1649 {
1650 info->buffer = retbuf;
1651 return;
1652 }
1653
1654 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1655 || bfd_bread (buf, info->size, abfd) != info->size)
1656 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1657 bfd_get_filename (abfd));
1658 }
1659
1660 /* A helper function that returns the size of a section in a safe way.
1661 If you are positive that the section has been read before using the
1662 size, then it is safe to refer to the dwarf2_section_info object's
1663 "size" field directly. In other cases, you must call this
1664 function, because for compressed sections the size field is not set
1665 correctly until the section has been read. */
1666
1667 static bfd_size_type
1668 dwarf2_section_size (struct objfile *objfile,
1669 struct dwarf2_section_info *info)
1670 {
1671 if (!info->readin)
1672 dwarf2_read_section (objfile, info);
1673 return info->size;
1674 }
1675
1676 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1677 SECTION_NAME. */
1678
1679 void
1680 dwarf2_get_section_info (struct objfile *objfile,
1681 enum dwarf2_section_enum sect,
1682 asection **sectp, gdb_byte **bufp,
1683 bfd_size_type *sizep)
1684 {
1685 struct dwarf2_per_objfile *data
1686 = objfile_data (objfile, dwarf2_objfile_data_key);
1687 struct dwarf2_section_info *info;
1688
1689 /* We may see an objfile without any DWARF, in which case we just
1690 return nothing. */
1691 if (data == NULL)
1692 {
1693 *sectp = NULL;
1694 *bufp = NULL;
1695 *sizep = 0;
1696 return;
1697 }
1698 switch (sect)
1699 {
1700 case DWARF2_DEBUG_FRAME:
1701 info = &data->frame;
1702 break;
1703 case DWARF2_EH_FRAME:
1704 info = &data->eh_frame;
1705 break;
1706 default:
1707 gdb_assert_not_reached ("unexpected section");
1708 }
1709
1710 dwarf2_read_section (objfile, info);
1711
1712 *sectp = info->asection;
1713 *bufp = info->buffer;
1714 *sizep = info->size;
1715 }
1716
1717 \f
1718 /* DWARF quick_symbols_functions support. */
1719
1720 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1721 unique line tables, so we maintain a separate table of all .debug_line
1722 derived entries to support the sharing.
1723 All the quick functions need is the list of file names. We discard the
1724 line_header when we're done and don't need to record it here. */
1725 struct quick_file_names
1726 {
1727 /* The offset in .debug_line of the line table. We hash on this. */
1728 unsigned int offset;
1729
1730 /* The number of entries in file_names, real_names. */
1731 unsigned int num_file_names;
1732
1733 /* The file names from the line table, after being run through
1734 file_full_name. */
1735 const char **file_names;
1736
1737 /* The file names from the line table after being run through
1738 gdb_realpath. These are computed lazily. */
1739 const char **real_names;
1740 };
1741
1742 /* When using the index (and thus not using psymtabs), each CU has an
1743 object of this type. This is used to hold information needed by
1744 the various "quick" methods. */
1745 struct dwarf2_per_cu_quick_data
1746 {
1747 /* The file table. This can be NULL if there was no file table
1748 or it's currently not read in.
1749 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1750 struct quick_file_names *file_names;
1751
1752 /* The corresponding symbol table. This is NULL if symbols for this
1753 CU have not yet been read. */
1754 struct symtab *symtab;
1755
1756 /* A temporary mark bit used when iterating over all CUs in
1757 expand_symtabs_matching. */
1758 unsigned int mark : 1;
1759
1760 /* True if we've tried to read the file table and found there isn't one.
1761 There will be no point in trying to read it again next time. */
1762 unsigned int no_file_data : 1;
1763 };
1764
1765 /* Hash function for a quick_file_names. */
1766
1767 static hashval_t
1768 hash_file_name_entry (const void *e)
1769 {
1770 const struct quick_file_names *file_data = e;
1771
1772 return file_data->offset;
1773 }
1774
1775 /* Equality function for a quick_file_names. */
1776
1777 static int
1778 eq_file_name_entry (const void *a, const void *b)
1779 {
1780 const struct quick_file_names *ea = a;
1781 const struct quick_file_names *eb = b;
1782
1783 return ea->offset == eb->offset;
1784 }
1785
1786 /* Delete function for a quick_file_names. */
1787
1788 static void
1789 delete_file_name_entry (void *e)
1790 {
1791 struct quick_file_names *file_data = e;
1792 int i;
1793
1794 for (i = 0; i < file_data->num_file_names; ++i)
1795 {
1796 xfree ((void*) file_data->file_names[i]);
1797 if (file_data->real_names)
1798 xfree ((void*) file_data->real_names[i]);
1799 }
1800
1801 /* The space for the struct itself lives on objfile_obstack,
1802 so we don't free it here. */
1803 }
1804
1805 /* Create a quick_file_names hash table. */
1806
1807 static htab_t
1808 create_quick_file_names_table (unsigned int nr_initial_entries)
1809 {
1810 return htab_create_alloc (nr_initial_entries,
1811 hash_file_name_entry, eq_file_name_entry,
1812 delete_file_name_entry, xcalloc, xfree);
1813 }
1814
1815 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1816 have to be created afterwards. You should call age_cached_comp_units after
1817 processing PER_CU->CU. dw2_setup must have been already called. */
1818
1819 static void
1820 load_cu (struct dwarf2_per_cu_data *per_cu)
1821 {
1822 if (per_cu->debug_types_section)
1823 read_signatured_type_at_offset (per_cu->objfile,
1824 per_cu->debug_types_section,
1825 per_cu->offset);
1826 else
1827 load_full_comp_unit (per_cu);
1828
1829 gdb_assert (per_cu->cu != NULL);
1830
1831 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1832 }
1833
1834 /* Read in the symbols for PER_CU. */
1835
1836 static void
1837 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1838 {
1839 struct cleanup *back_to;
1840
1841 back_to = make_cleanup (dwarf2_release_queue, NULL);
1842
1843 queue_comp_unit (per_cu);
1844
1845 load_cu (per_cu);
1846
1847 process_queue ();
1848
1849 /* Age the cache, releasing compilation units that have not
1850 been used recently. */
1851 age_cached_comp_units ();
1852
1853 do_cleanups (back_to);
1854 }
1855
1856 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1857 the objfile from which this CU came. Returns the resulting symbol
1858 table. */
1859
1860 static struct symtab *
1861 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1862 {
1863 if (!per_cu->v.quick->symtab)
1864 {
1865 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1866 increment_reading_symtab ();
1867 dw2_do_instantiate_symtab (per_cu);
1868 do_cleanups (back_to);
1869 }
1870 return per_cu->v.quick->symtab;
1871 }
1872
1873 /* Return the CU given its index. */
1874
1875 static struct dwarf2_per_cu_data *
1876 dw2_get_cu (int index)
1877 {
1878 if (index >= dwarf2_per_objfile->n_comp_units)
1879 {
1880 index -= dwarf2_per_objfile->n_comp_units;
1881 return dwarf2_per_objfile->type_comp_units[index];
1882 }
1883 return dwarf2_per_objfile->all_comp_units[index];
1884 }
1885
1886 /* A helper function that knows how to read a 64-bit value in a way
1887 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1888 otherwise. */
1889
1890 static int
1891 extract_cu_value (const char *bytes, ULONGEST *result)
1892 {
1893 if (sizeof (ULONGEST) < 8)
1894 {
1895 int i;
1896
1897 /* Ignore the upper 4 bytes if they are all zero. */
1898 for (i = 0; i < 4; ++i)
1899 if (bytes[i + 4] != 0)
1900 return 0;
1901
1902 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1903 }
1904 else
1905 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1906 return 1;
1907 }
1908
1909 /* Read the CU list from the mapped index, and use it to create all
1910 the CU objects for this objfile. Return 0 if something went wrong,
1911 1 if everything went ok. */
1912
1913 static int
1914 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1915 offset_type cu_list_elements)
1916 {
1917 offset_type i;
1918
1919 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1920 dwarf2_per_objfile->all_comp_units
1921 = obstack_alloc (&objfile->objfile_obstack,
1922 dwarf2_per_objfile->n_comp_units
1923 * sizeof (struct dwarf2_per_cu_data *));
1924
1925 for (i = 0; i < cu_list_elements; i += 2)
1926 {
1927 struct dwarf2_per_cu_data *the_cu;
1928 ULONGEST offset, length;
1929
1930 if (!extract_cu_value (cu_list, &offset)
1931 || !extract_cu_value (cu_list + 8, &length))
1932 return 0;
1933 cu_list += 2 * 8;
1934
1935 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1936 struct dwarf2_per_cu_data);
1937 the_cu->offset = offset;
1938 the_cu->length = length;
1939 the_cu->objfile = objfile;
1940 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1941 struct dwarf2_per_cu_quick_data);
1942 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1943 }
1944
1945 return 1;
1946 }
1947
1948 /* Create the signatured type hash table from the index. */
1949
1950 static int
1951 create_signatured_type_table_from_index (struct objfile *objfile,
1952 struct dwarf2_section_info *section,
1953 const gdb_byte *bytes,
1954 offset_type elements)
1955 {
1956 offset_type i;
1957 htab_t sig_types_hash;
1958
1959 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1960 dwarf2_per_objfile->type_comp_units
1961 = obstack_alloc (&objfile->objfile_obstack,
1962 dwarf2_per_objfile->n_type_comp_units
1963 * sizeof (struct dwarf2_per_cu_data *));
1964
1965 sig_types_hash = allocate_signatured_type_table (objfile);
1966
1967 for (i = 0; i < elements; i += 3)
1968 {
1969 struct signatured_type *type_sig;
1970 ULONGEST offset, type_offset, signature;
1971 void **slot;
1972
1973 if (!extract_cu_value (bytes, &offset)
1974 || !extract_cu_value (bytes + 8, &type_offset))
1975 return 0;
1976 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1977 bytes += 3 * 8;
1978
1979 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1980 struct signatured_type);
1981 type_sig->signature = signature;
1982 type_sig->type_offset = type_offset;
1983 type_sig->per_cu.debug_types_section = section;
1984 type_sig->per_cu.offset = offset;
1985 type_sig->per_cu.objfile = objfile;
1986 type_sig->per_cu.v.quick
1987 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1988 struct dwarf2_per_cu_quick_data);
1989
1990 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1991 *slot = type_sig;
1992
1993 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1994 }
1995
1996 dwarf2_per_objfile->signatured_types = sig_types_hash;
1997
1998 return 1;
1999 }
2000
2001 /* Read the address map data from the mapped index, and use it to
2002 populate the objfile's psymtabs_addrmap. */
2003
2004 static void
2005 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2006 {
2007 const gdb_byte *iter, *end;
2008 struct obstack temp_obstack;
2009 struct addrmap *mutable_map;
2010 struct cleanup *cleanup;
2011 CORE_ADDR baseaddr;
2012
2013 obstack_init (&temp_obstack);
2014 cleanup = make_cleanup_obstack_free (&temp_obstack);
2015 mutable_map = addrmap_create_mutable (&temp_obstack);
2016
2017 iter = index->address_table;
2018 end = iter + index->address_table_size;
2019
2020 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2021
2022 while (iter < end)
2023 {
2024 ULONGEST hi, lo, cu_index;
2025 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2026 iter += 8;
2027 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2028 iter += 8;
2029 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2030 iter += 4;
2031
2032 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2033 dw2_get_cu (cu_index));
2034 }
2035
2036 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2037 &objfile->objfile_obstack);
2038 do_cleanups (cleanup);
2039 }
2040
2041 /* The hash function for strings in the mapped index. This is the same as
2042 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2043 implementation. This is necessary because the hash function is tied to the
2044 format of the mapped index file. The hash values do not have to match with
2045 SYMBOL_HASH_NEXT.
2046
2047 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2048
2049 static hashval_t
2050 mapped_index_string_hash (int index_version, const void *p)
2051 {
2052 const unsigned char *str = (const unsigned char *) p;
2053 hashval_t r = 0;
2054 unsigned char c;
2055
2056 while ((c = *str++) != 0)
2057 {
2058 if (index_version >= 5)
2059 c = tolower (c);
2060 r = r * 67 + c - 113;
2061 }
2062
2063 return r;
2064 }
2065
2066 /* Find a slot in the mapped index INDEX for the object named NAME.
2067 If NAME is found, set *VEC_OUT to point to the CU vector in the
2068 constant pool and return 1. If NAME cannot be found, return 0. */
2069
2070 static int
2071 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2072 offset_type **vec_out)
2073 {
2074 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2075 offset_type hash;
2076 offset_type slot, step;
2077 int (*cmp) (const char *, const char *);
2078
2079 if (current_language->la_language == language_cplus
2080 || current_language->la_language == language_java
2081 || current_language->la_language == language_fortran)
2082 {
2083 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2084 not contain any. */
2085 const char *paren = strchr (name, '(');
2086
2087 if (paren)
2088 {
2089 char *dup;
2090
2091 dup = xmalloc (paren - name + 1);
2092 memcpy (dup, name, paren - name);
2093 dup[paren - name] = 0;
2094
2095 make_cleanup (xfree, dup);
2096 name = dup;
2097 }
2098 }
2099
2100 /* Index version 4 did not support case insensitive searches. But the
2101 indexes for case insensitive languages are built in lowercase, therefore
2102 simulate our NAME being searched is also lowercased. */
2103 hash = mapped_index_string_hash ((index->version == 4
2104 && case_sensitivity == case_sensitive_off
2105 ? 5 : index->version),
2106 name);
2107
2108 slot = hash & (index->symbol_table_slots - 1);
2109 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2110 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2111
2112 for (;;)
2113 {
2114 /* Convert a slot number to an offset into the table. */
2115 offset_type i = 2 * slot;
2116 const char *str;
2117 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2118 {
2119 do_cleanups (back_to);
2120 return 0;
2121 }
2122
2123 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2124 if (!cmp (name, str))
2125 {
2126 *vec_out = (offset_type *) (index->constant_pool
2127 + MAYBE_SWAP (index->symbol_table[i + 1]));
2128 do_cleanups (back_to);
2129 return 1;
2130 }
2131
2132 slot = (slot + step) & (index->symbol_table_slots - 1);
2133 }
2134 }
2135
2136 /* Read the index file. If everything went ok, initialize the "quick"
2137 elements of all the CUs and return 1. Otherwise, return 0. */
2138
2139 static int
2140 dwarf2_read_index (struct objfile *objfile)
2141 {
2142 char *addr;
2143 struct mapped_index *map;
2144 offset_type *metadata;
2145 const gdb_byte *cu_list;
2146 const gdb_byte *types_list = NULL;
2147 offset_type version, cu_list_elements;
2148 offset_type types_list_elements = 0;
2149 int i;
2150
2151 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2152 return 0;
2153
2154 /* Older elfutils strip versions could keep the section in the main
2155 executable while splitting it for the separate debug info file. */
2156 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2157 & SEC_HAS_CONTENTS) == 0)
2158 return 0;
2159
2160 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2161
2162 addr = dwarf2_per_objfile->gdb_index.buffer;
2163 /* Version check. */
2164 version = MAYBE_SWAP (*(offset_type *) addr);
2165 /* Versions earlier than 3 emitted every copy of a psymbol. This
2166 causes the index to behave very poorly for certain requests. Version 3
2167 contained incomplete addrmap. So, it seems better to just ignore such
2168 indices. Index version 4 uses a different hash function than index
2169 version 5 and later. */
2170 if (version < 4)
2171 return 0;
2172 /* Indexes with higher version than the one supported by GDB may be no
2173 longer backward compatible. */
2174 if (version > 5)
2175 return 0;
2176
2177 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2178 map->version = version;
2179 map->total_size = dwarf2_per_objfile->gdb_index.size;
2180
2181 metadata = (offset_type *) (addr + sizeof (offset_type));
2182
2183 i = 0;
2184 cu_list = addr + MAYBE_SWAP (metadata[i]);
2185 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2186 / 8);
2187 ++i;
2188
2189 types_list = addr + MAYBE_SWAP (metadata[i]);
2190 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2191 - MAYBE_SWAP (metadata[i]))
2192 / 8);
2193 ++i;
2194
2195 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2196 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2197 - MAYBE_SWAP (metadata[i]));
2198 ++i;
2199
2200 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2201 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2202 - MAYBE_SWAP (metadata[i]))
2203 / (2 * sizeof (offset_type)));
2204 ++i;
2205
2206 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2207
2208 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2209 return 0;
2210
2211 if (types_list_elements)
2212 {
2213 struct dwarf2_section_info *section;
2214
2215 /* We can only handle a single .debug_types when we have an
2216 index. */
2217 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2218 return 0;
2219
2220 section = VEC_index (dwarf2_section_info_def,
2221 dwarf2_per_objfile->types, 0);
2222
2223 if (!create_signatured_type_table_from_index (objfile, section,
2224 types_list,
2225 types_list_elements))
2226 return 0;
2227 }
2228
2229 create_addrmap_from_index (objfile, map);
2230
2231 dwarf2_per_objfile->index_table = map;
2232 dwarf2_per_objfile->using_index = 1;
2233 dwarf2_per_objfile->quick_file_names_table =
2234 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2235
2236 return 1;
2237 }
2238
2239 /* A helper for the "quick" functions which sets the global
2240 dwarf2_per_objfile according to OBJFILE. */
2241
2242 static void
2243 dw2_setup (struct objfile *objfile)
2244 {
2245 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2246 gdb_assert (dwarf2_per_objfile);
2247 }
2248
2249 /* A helper for the "quick" functions which attempts to read the line
2250 table for THIS_CU. */
2251
2252 static struct quick_file_names *
2253 dw2_get_file_names (struct objfile *objfile,
2254 struct dwarf2_per_cu_data *this_cu)
2255 {
2256 bfd *abfd = objfile->obfd;
2257 struct line_header *lh;
2258 struct attribute *attr;
2259 struct cleanup *cleanups;
2260 struct die_info *comp_unit_die;
2261 struct dwarf2_section_info* sec;
2262 gdb_byte *info_ptr, *buffer;
2263 int has_children, i;
2264 struct dwarf2_cu cu;
2265 unsigned int bytes_read, buffer_size;
2266 struct die_reader_specs reader_specs;
2267 char *name, *comp_dir;
2268 void **slot;
2269 struct quick_file_names *qfn;
2270 unsigned int line_offset;
2271
2272 if (this_cu->v.quick->file_names != NULL)
2273 return this_cu->v.quick->file_names;
2274 /* If we know there is no line data, no point in looking again. */
2275 if (this_cu->v.quick->no_file_data)
2276 return NULL;
2277
2278 init_one_comp_unit (&cu, this_cu);
2279 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2280
2281 if (this_cu->debug_types_section)
2282 sec = this_cu->debug_types_section;
2283 else
2284 sec = &dwarf2_per_objfile->info;
2285 dwarf2_read_section (objfile, sec);
2286 buffer_size = sec->size;
2287 buffer = sec->buffer;
2288 info_ptr = buffer + this_cu->offset;
2289
2290 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2291 buffer, buffer_size,
2292 abfd,
2293 this_cu->debug_types_section != NULL);
2294
2295 /* Skip dummy compilation units. */
2296 if (info_ptr >= buffer + buffer_size
2297 || peek_abbrev_code (abfd, info_ptr) == 0)
2298 {
2299 do_cleanups (cleanups);
2300 return NULL;
2301 }
2302
2303 dwarf2_read_abbrevs (abfd, &cu);
2304 make_cleanup (dwarf2_free_abbrev_table, &cu);
2305
2306 init_cu_die_reader (&reader_specs, &cu);
2307 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2308 &has_children);
2309
2310 lh = NULL;
2311 slot = NULL;
2312 line_offset = 0;
2313 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2314 if (attr)
2315 {
2316 struct quick_file_names find_entry;
2317
2318 line_offset = DW_UNSND (attr);
2319
2320 /* We may have already read in this line header (TU line header sharing).
2321 If we have we're done. */
2322 find_entry.offset = line_offset;
2323 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2324 &find_entry, INSERT);
2325 if (*slot != NULL)
2326 {
2327 do_cleanups (cleanups);
2328 this_cu->v.quick->file_names = *slot;
2329 return *slot;
2330 }
2331
2332 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2333 }
2334 if (lh == NULL)
2335 {
2336 do_cleanups (cleanups);
2337 this_cu->v.quick->no_file_data = 1;
2338 return NULL;
2339 }
2340
2341 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2342 qfn->offset = line_offset;
2343 gdb_assert (slot != NULL);
2344 *slot = qfn;
2345
2346 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2347
2348 qfn->num_file_names = lh->num_file_names;
2349 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2350 lh->num_file_names * sizeof (char *));
2351 for (i = 0; i < lh->num_file_names; ++i)
2352 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2353 qfn->real_names = NULL;
2354
2355 free_line_header (lh);
2356 do_cleanups (cleanups);
2357
2358 this_cu->v.quick->file_names = qfn;
2359 return qfn;
2360 }
2361
2362 /* A helper for the "quick" functions which computes and caches the
2363 real path for a given file name from the line table. */
2364
2365 static const char *
2366 dw2_get_real_path (struct objfile *objfile,
2367 struct quick_file_names *qfn, int index)
2368 {
2369 if (qfn->real_names == NULL)
2370 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2371 qfn->num_file_names, sizeof (char *));
2372
2373 if (qfn->real_names[index] == NULL)
2374 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2375
2376 return qfn->real_names[index];
2377 }
2378
2379 static struct symtab *
2380 dw2_find_last_source_symtab (struct objfile *objfile)
2381 {
2382 int index;
2383
2384 dw2_setup (objfile);
2385 index = dwarf2_per_objfile->n_comp_units - 1;
2386 return dw2_instantiate_symtab (dw2_get_cu (index));
2387 }
2388
2389 /* Traversal function for dw2_forget_cached_source_info. */
2390
2391 static int
2392 dw2_free_cached_file_names (void **slot, void *info)
2393 {
2394 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2395
2396 if (file_data->real_names)
2397 {
2398 int i;
2399
2400 for (i = 0; i < file_data->num_file_names; ++i)
2401 {
2402 xfree ((void*) file_data->real_names[i]);
2403 file_data->real_names[i] = NULL;
2404 }
2405 }
2406
2407 return 1;
2408 }
2409
2410 static void
2411 dw2_forget_cached_source_info (struct objfile *objfile)
2412 {
2413 dw2_setup (objfile);
2414
2415 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2416 dw2_free_cached_file_names, NULL);
2417 }
2418
2419 /* Helper function for dw2_map_symtabs_matching_filename that expands
2420 the symtabs and calls the iterator. */
2421
2422 static int
2423 dw2_map_expand_apply (struct objfile *objfile,
2424 struct dwarf2_per_cu_data *per_cu,
2425 const char *name,
2426 const char *full_path, const char *real_path,
2427 int (*callback) (struct symtab *, void *),
2428 void *data)
2429 {
2430 struct symtab *last_made = objfile->symtabs;
2431
2432 /* Don't visit already-expanded CUs. */
2433 if (per_cu->v.quick->symtab)
2434 return 0;
2435
2436 /* This may expand more than one symtab, and we want to iterate over
2437 all of them. */
2438 dw2_instantiate_symtab (per_cu);
2439
2440 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2441 objfile->symtabs, last_made);
2442 }
2443
2444 /* Implementation of the map_symtabs_matching_filename method. */
2445
2446 static int
2447 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2448 const char *full_path, const char *real_path,
2449 int (*callback) (struct symtab *, void *),
2450 void *data)
2451 {
2452 int i;
2453 const char *name_basename = lbasename (name);
2454 int check_basename = name_basename == name;
2455 struct dwarf2_per_cu_data *base_cu = NULL;
2456
2457 dw2_setup (objfile);
2458
2459 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2460 + dwarf2_per_objfile->n_type_comp_units); ++i)
2461 {
2462 int j;
2463 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2464 struct quick_file_names *file_data;
2465
2466 /* We only need to look at symtabs not already expanded. */
2467 if (per_cu->v.quick->symtab)
2468 continue;
2469
2470 file_data = dw2_get_file_names (objfile, per_cu);
2471 if (file_data == NULL)
2472 continue;
2473
2474 for (j = 0; j < file_data->num_file_names; ++j)
2475 {
2476 const char *this_name = file_data->file_names[j];
2477
2478 if (FILENAME_CMP (name, this_name) == 0)
2479 {
2480 if (dw2_map_expand_apply (objfile, per_cu,
2481 name, full_path, real_path,
2482 callback, data))
2483 return 1;
2484 }
2485
2486 if (check_basename && ! base_cu
2487 && FILENAME_CMP (lbasename (this_name), name) == 0)
2488 base_cu = per_cu;
2489
2490 /* Before we invoke realpath, which can get expensive when many
2491 files are involved, do a quick comparison of the basenames. */
2492 if (! basenames_may_differ
2493 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2494 continue;
2495
2496 if (full_path != NULL)
2497 {
2498 const char *this_real_name = dw2_get_real_path (objfile,
2499 file_data, j);
2500
2501 if (this_real_name != NULL
2502 && FILENAME_CMP (full_path, this_real_name) == 0)
2503 {
2504 if (dw2_map_expand_apply (objfile, per_cu,
2505 name, full_path, real_path,
2506 callback, data))
2507 return 1;
2508 }
2509 }
2510
2511 if (real_path != NULL)
2512 {
2513 const char *this_real_name = dw2_get_real_path (objfile,
2514 file_data, j);
2515
2516 if (this_real_name != NULL
2517 && FILENAME_CMP (real_path, this_real_name) == 0)
2518 {
2519 if (dw2_map_expand_apply (objfile, per_cu,
2520 name, full_path, real_path,
2521 callback, data))
2522 return 1;
2523 }
2524 }
2525 }
2526 }
2527
2528 if (base_cu)
2529 {
2530 if (dw2_map_expand_apply (objfile, base_cu,
2531 name, full_path, real_path,
2532 callback, data))
2533 return 1;
2534 }
2535
2536 return 0;
2537 }
2538
2539 static struct symtab *
2540 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2541 const char *name, domain_enum domain)
2542 {
2543 /* We do all the work in the pre_expand_symtabs_matching hook
2544 instead. */
2545 return NULL;
2546 }
2547
2548 /* A helper function that expands all symtabs that hold an object
2549 named NAME. */
2550
2551 static void
2552 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2553 {
2554 dw2_setup (objfile);
2555
2556 /* index_table is NULL if OBJF_READNOW. */
2557 if (dwarf2_per_objfile->index_table)
2558 {
2559 offset_type *vec;
2560
2561 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2562 name, &vec))
2563 {
2564 offset_type i, len = MAYBE_SWAP (*vec);
2565 for (i = 0; i < len; ++i)
2566 {
2567 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2568 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2569
2570 dw2_instantiate_symtab (per_cu);
2571 }
2572 }
2573 }
2574 }
2575
2576 static void
2577 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2578 enum block_enum block_kind, const char *name,
2579 domain_enum domain)
2580 {
2581 dw2_do_expand_symtabs_matching (objfile, name);
2582 }
2583
2584 static void
2585 dw2_print_stats (struct objfile *objfile)
2586 {
2587 int i, count;
2588
2589 dw2_setup (objfile);
2590 count = 0;
2591 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2592 + dwarf2_per_objfile->n_type_comp_units); ++i)
2593 {
2594 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2595
2596 if (!per_cu->v.quick->symtab)
2597 ++count;
2598 }
2599 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2600 }
2601
2602 static void
2603 dw2_dump (struct objfile *objfile)
2604 {
2605 /* Nothing worth printing. */
2606 }
2607
2608 static void
2609 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2610 struct section_offsets *delta)
2611 {
2612 /* There's nothing to relocate here. */
2613 }
2614
2615 static void
2616 dw2_expand_symtabs_for_function (struct objfile *objfile,
2617 const char *func_name)
2618 {
2619 dw2_do_expand_symtabs_matching (objfile, func_name);
2620 }
2621
2622 static void
2623 dw2_expand_all_symtabs (struct objfile *objfile)
2624 {
2625 int i;
2626
2627 dw2_setup (objfile);
2628
2629 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2630 + dwarf2_per_objfile->n_type_comp_units); ++i)
2631 {
2632 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2633
2634 dw2_instantiate_symtab (per_cu);
2635 }
2636 }
2637
2638 static void
2639 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2640 const char *filename)
2641 {
2642 int i;
2643
2644 dw2_setup (objfile);
2645
2646 /* We don't need to consider type units here.
2647 This is only called for examining code, e.g. expand_line_sal.
2648 There can be an order of magnitude (or more) more type units
2649 than comp units, and we avoid them if we can. */
2650
2651 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2652 {
2653 int j;
2654 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2655 struct quick_file_names *file_data;
2656
2657 /* We only need to look at symtabs not already expanded. */
2658 if (per_cu->v.quick->symtab)
2659 continue;
2660
2661 file_data = dw2_get_file_names (objfile, per_cu);
2662 if (file_data == NULL)
2663 continue;
2664
2665 for (j = 0; j < file_data->num_file_names; ++j)
2666 {
2667 const char *this_name = file_data->file_names[j];
2668 if (FILENAME_CMP (this_name, filename) == 0)
2669 {
2670 dw2_instantiate_symtab (per_cu);
2671 break;
2672 }
2673 }
2674 }
2675 }
2676
2677 static const char *
2678 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2679 {
2680 struct dwarf2_per_cu_data *per_cu;
2681 offset_type *vec;
2682 struct quick_file_names *file_data;
2683
2684 dw2_setup (objfile);
2685
2686 /* index_table is NULL if OBJF_READNOW. */
2687 if (!dwarf2_per_objfile->index_table)
2688 {
2689 struct symtab *s;
2690
2691 ALL_OBJFILE_SYMTABS (objfile, s)
2692 if (s->primary)
2693 {
2694 struct blockvector *bv = BLOCKVECTOR (s);
2695 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2696 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2697
2698 if (sym)
2699 return sym->symtab->filename;
2700 }
2701 return NULL;
2702 }
2703
2704 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2705 name, &vec))
2706 return NULL;
2707
2708 /* Note that this just looks at the very first one named NAME -- but
2709 actually we are looking for a function. find_main_filename
2710 should be rewritten so that it doesn't require a custom hook. It
2711 could just use the ordinary symbol tables. */
2712 /* vec[0] is the length, which must always be >0. */
2713 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2714
2715 file_data = dw2_get_file_names (objfile, per_cu);
2716 if (file_data == NULL)
2717 return NULL;
2718
2719 return file_data->file_names[file_data->num_file_names - 1];
2720 }
2721
2722 static void
2723 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2724 struct objfile *objfile, int global,
2725 int (*callback) (struct block *,
2726 struct symbol *, void *),
2727 void *data, symbol_compare_ftype *match,
2728 symbol_compare_ftype *ordered_compare)
2729 {
2730 /* Currently unimplemented; used for Ada. The function can be called if the
2731 current language is Ada for a non-Ada objfile using GNU index. As Ada
2732 does not look for non-Ada symbols this function should just return. */
2733 }
2734
2735 static void
2736 dw2_expand_symtabs_matching
2737 (struct objfile *objfile,
2738 int (*file_matcher) (const char *, void *),
2739 int (*name_matcher) (const struct language_defn *, const char *, void *),
2740 enum search_domain kind,
2741 void *data)
2742 {
2743 int i;
2744 offset_type iter;
2745 struct mapped_index *index;
2746
2747 dw2_setup (objfile);
2748
2749 /* index_table is NULL if OBJF_READNOW. */
2750 if (!dwarf2_per_objfile->index_table)
2751 return;
2752 index = dwarf2_per_objfile->index_table;
2753
2754 if (file_matcher != NULL)
2755 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2756 + dwarf2_per_objfile->n_type_comp_units); ++i)
2757 {
2758 int j;
2759 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2760 struct quick_file_names *file_data;
2761
2762 per_cu->v.quick->mark = 0;
2763
2764 /* We only need to look at symtabs not already expanded. */
2765 if (per_cu->v.quick->symtab)
2766 continue;
2767
2768 file_data = dw2_get_file_names (objfile, per_cu);
2769 if (file_data == NULL)
2770 continue;
2771
2772 for (j = 0; j < file_data->num_file_names; ++j)
2773 {
2774 if (file_matcher (file_data->file_names[j], data))
2775 {
2776 per_cu->v.quick->mark = 1;
2777 break;
2778 }
2779 }
2780 }
2781
2782 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2783 {
2784 offset_type idx = 2 * iter;
2785 const char *name;
2786 offset_type *vec, vec_len, vec_idx;
2787
2788 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2789 continue;
2790
2791 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2792
2793 if (! (*name_matcher) (current_language, name, data))
2794 continue;
2795
2796 /* The name was matched, now expand corresponding CUs that were
2797 marked. */
2798 vec = (offset_type *) (index->constant_pool
2799 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2800 vec_len = MAYBE_SWAP (vec[0]);
2801 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2802 {
2803 struct dwarf2_per_cu_data *per_cu;
2804
2805 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2806 if (file_matcher == NULL || per_cu->v.quick->mark)
2807 dw2_instantiate_symtab (per_cu);
2808 }
2809 }
2810 }
2811
2812 static struct symtab *
2813 dw2_find_pc_sect_symtab (struct objfile *objfile,
2814 struct minimal_symbol *msymbol,
2815 CORE_ADDR pc,
2816 struct obj_section *section,
2817 int warn_if_readin)
2818 {
2819 struct dwarf2_per_cu_data *data;
2820
2821 dw2_setup (objfile);
2822
2823 if (!objfile->psymtabs_addrmap)
2824 return NULL;
2825
2826 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2827 if (!data)
2828 return NULL;
2829
2830 if (warn_if_readin && data->v.quick->symtab)
2831 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2832 paddress (get_objfile_arch (objfile), pc));
2833
2834 return dw2_instantiate_symtab (data);
2835 }
2836
2837 static void
2838 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2839 void *data, int need_fullname)
2840 {
2841 int i;
2842
2843 dw2_setup (objfile);
2844
2845 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2846 + dwarf2_per_objfile->n_type_comp_units); ++i)
2847 {
2848 int j;
2849 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2850 struct quick_file_names *file_data;
2851
2852 /* We only need to look at symtabs not already expanded. */
2853 if (per_cu->v.quick->symtab)
2854 continue;
2855
2856 file_data = dw2_get_file_names (objfile, per_cu);
2857 if (file_data == NULL)
2858 continue;
2859
2860 for (j = 0; j < file_data->num_file_names; ++j)
2861 {
2862 const char *this_real_name;
2863
2864 if (need_fullname)
2865 this_real_name = dw2_get_real_path (objfile, file_data, j);
2866 else
2867 this_real_name = NULL;
2868 (*fun) (file_data->file_names[j], this_real_name, data);
2869 }
2870 }
2871 }
2872
2873 static int
2874 dw2_has_symbols (struct objfile *objfile)
2875 {
2876 return 1;
2877 }
2878
2879 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2880 {
2881 dw2_has_symbols,
2882 dw2_find_last_source_symtab,
2883 dw2_forget_cached_source_info,
2884 dw2_map_symtabs_matching_filename,
2885 dw2_lookup_symbol,
2886 dw2_pre_expand_symtabs_matching,
2887 dw2_print_stats,
2888 dw2_dump,
2889 dw2_relocate,
2890 dw2_expand_symtabs_for_function,
2891 dw2_expand_all_symtabs,
2892 dw2_expand_symtabs_with_filename,
2893 dw2_find_symbol_file,
2894 dw2_map_matching_symbols,
2895 dw2_expand_symtabs_matching,
2896 dw2_find_pc_sect_symtab,
2897 dw2_map_symbol_filenames
2898 };
2899
2900 /* Initialize for reading DWARF for this objfile. Return 0 if this
2901 file will use psymtabs, or 1 if using the GNU index. */
2902
2903 int
2904 dwarf2_initialize_objfile (struct objfile *objfile)
2905 {
2906 /* If we're about to read full symbols, don't bother with the
2907 indices. In this case we also don't care if some other debug
2908 format is making psymtabs, because they are all about to be
2909 expanded anyway. */
2910 if ((objfile->flags & OBJF_READNOW))
2911 {
2912 int i;
2913
2914 dwarf2_per_objfile->using_index = 1;
2915 create_all_comp_units (objfile);
2916 create_debug_types_hash_table (objfile);
2917 dwarf2_per_objfile->quick_file_names_table =
2918 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2919
2920 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2921 + dwarf2_per_objfile->n_type_comp_units); ++i)
2922 {
2923 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2924
2925 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2926 struct dwarf2_per_cu_quick_data);
2927 }
2928
2929 /* Return 1 so that gdb sees the "quick" functions. However,
2930 these functions will be no-ops because we will have expanded
2931 all symtabs. */
2932 return 1;
2933 }
2934
2935 if (dwarf2_read_index (objfile))
2936 return 1;
2937
2938 return 0;
2939 }
2940
2941 \f
2942
2943 /* Build a partial symbol table. */
2944
2945 void
2946 dwarf2_build_psymtabs (struct objfile *objfile)
2947 {
2948 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2949 {
2950 init_psymbol_list (objfile, 1024);
2951 }
2952
2953 dwarf2_build_psymtabs_hard (objfile);
2954 }
2955
2956 /* Return TRUE if OFFSET is within CU_HEADER. */
2957
2958 static inline int
2959 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2960 {
2961 unsigned int bottom = cu_header->offset;
2962 unsigned int top = (cu_header->offset
2963 + cu_header->length
2964 + cu_header->initial_length_size);
2965
2966 return (offset >= bottom && offset < top);
2967 }
2968
2969 /* Read in the comp unit header information from the debug_info at info_ptr.
2970 NOTE: This leaves members offset, first_die_offset to be filled in
2971 by the caller. */
2972
2973 static gdb_byte *
2974 read_comp_unit_head (struct comp_unit_head *cu_header,
2975 gdb_byte *info_ptr, bfd *abfd)
2976 {
2977 int signed_addr;
2978 unsigned int bytes_read;
2979
2980 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2981 cu_header->initial_length_size = bytes_read;
2982 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2983 info_ptr += bytes_read;
2984 cu_header->version = read_2_bytes (abfd, info_ptr);
2985 info_ptr += 2;
2986 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2987 &bytes_read);
2988 info_ptr += bytes_read;
2989 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2990 info_ptr += 1;
2991 signed_addr = bfd_get_sign_extend_vma (abfd);
2992 if (signed_addr < 0)
2993 internal_error (__FILE__, __LINE__,
2994 _("read_comp_unit_head: dwarf from non elf file"));
2995 cu_header->signed_addr_p = signed_addr;
2996
2997 return info_ptr;
2998 }
2999
3000 /* Read in a CU header and perform some basic error checking. */
3001
3002 static gdb_byte *
3003 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
3004 gdb_byte *buffer, unsigned int buffer_size,
3005 bfd *abfd, int is_debug_types_section)
3006 {
3007 gdb_byte *beg_of_comp_unit = info_ptr;
3008
3009 header->offset = beg_of_comp_unit - buffer;
3010
3011 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3012
3013 /* If we're reading a type unit, skip over the signature and
3014 type_offset fields. */
3015 if (is_debug_types_section)
3016 info_ptr += 8 /*signature*/ + header->offset_size;
3017
3018 header->first_die_offset = info_ptr - beg_of_comp_unit;
3019
3020 if (header->version != 2 && header->version != 3 && header->version != 4)
3021 error (_("Dwarf Error: wrong version in compilation unit header "
3022 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3023 bfd_get_filename (abfd));
3024
3025 if (header->abbrev_offset
3026 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3027 &dwarf2_per_objfile->abbrev))
3028 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3029 "(offset 0x%lx + 6) [in module %s]"),
3030 (long) header->abbrev_offset,
3031 (long) (beg_of_comp_unit - buffer),
3032 bfd_get_filename (abfd));
3033
3034 if (beg_of_comp_unit + header->length + header->initial_length_size
3035 > buffer + buffer_size)
3036 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3037 "(offset 0x%lx + 0) [in module %s]"),
3038 (long) header->length,
3039 (long) (beg_of_comp_unit - buffer),
3040 bfd_get_filename (abfd));
3041
3042 return info_ptr;
3043 }
3044
3045 /* Read in the types comp unit header information from .debug_types entry at
3046 types_ptr. The result is a pointer to one past the end of the header. */
3047
3048 static gdb_byte *
3049 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3050 struct dwarf2_section_info *section,
3051 ULONGEST *signature,
3052 gdb_byte *types_ptr, bfd *abfd)
3053 {
3054 gdb_byte *initial_types_ptr = types_ptr;
3055
3056 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3057 cu_header->offset = types_ptr - section->buffer;
3058
3059 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3060
3061 *signature = read_8_bytes (abfd, types_ptr);
3062 types_ptr += 8;
3063 types_ptr += cu_header->offset_size;
3064 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3065
3066 return types_ptr;
3067 }
3068
3069 /* Allocate a new partial symtab for file named NAME and mark this new
3070 partial symtab as being an include of PST. */
3071
3072 static void
3073 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3074 struct objfile *objfile)
3075 {
3076 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3077
3078 subpst->section_offsets = pst->section_offsets;
3079 subpst->textlow = 0;
3080 subpst->texthigh = 0;
3081
3082 subpst->dependencies = (struct partial_symtab **)
3083 obstack_alloc (&objfile->objfile_obstack,
3084 sizeof (struct partial_symtab *));
3085 subpst->dependencies[0] = pst;
3086 subpst->number_of_dependencies = 1;
3087
3088 subpst->globals_offset = 0;
3089 subpst->n_global_syms = 0;
3090 subpst->statics_offset = 0;
3091 subpst->n_static_syms = 0;
3092 subpst->symtab = NULL;
3093 subpst->read_symtab = pst->read_symtab;
3094 subpst->readin = 0;
3095
3096 /* No private part is necessary for include psymtabs. This property
3097 can be used to differentiate between such include psymtabs and
3098 the regular ones. */
3099 subpst->read_symtab_private = NULL;
3100 }
3101
3102 /* Read the Line Number Program data and extract the list of files
3103 included by the source file represented by PST. Build an include
3104 partial symtab for each of these included files. */
3105
3106 static void
3107 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3108 struct die_info *die,
3109 struct partial_symtab *pst)
3110 {
3111 struct objfile *objfile = cu->objfile;
3112 bfd *abfd = objfile->obfd;
3113 struct line_header *lh = NULL;
3114 struct attribute *attr;
3115
3116 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3117 if (attr)
3118 {
3119 unsigned int line_offset = DW_UNSND (attr);
3120
3121 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3122 }
3123 if (lh == NULL)
3124 return; /* No linetable, so no includes. */
3125
3126 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3127 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3128
3129 free_line_header (lh);
3130 }
3131
3132 static hashval_t
3133 hash_type_signature (const void *item)
3134 {
3135 const struct signatured_type *type_sig = item;
3136
3137 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3138 return type_sig->signature;
3139 }
3140
3141 static int
3142 eq_type_signature (const void *item_lhs, const void *item_rhs)
3143 {
3144 const struct signatured_type *lhs = item_lhs;
3145 const struct signatured_type *rhs = item_rhs;
3146
3147 return lhs->signature == rhs->signature;
3148 }
3149
3150 /* Allocate a hash table for signatured types. */
3151
3152 static htab_t
3153 allocate_signatured_type_table (struct objfile *objfile)
3154 {
3155 return htab_create_alloc_ex (41,
3156 hash_type_signature,
3157 eq_type_signature,
3158 NULL,
3159 &objfile->objfile_obstack,
3160 hashtab_obstack_allocate,
3161 dummy_obstack_deallocate);
3162 }
3163
3164 /* A helper function to add a signatured type CU to a list. */
3165
3166 static int
3167 add_signatured_type_cu_to_list (void **slot, void *datum)
3168 {
3169 struct signatured_type *sigt = *slot;
3170 struct dwarf2_per_cu_data ***datap = datum;
3171
3172 **datap = &sigt->per_cu;
3173 ++*datap;
3174
3175 return 1;
3176 }
3177
3178 /* Create the hash table of all entries in the .debug_types section.
3179 The result is zero if there is an error (e.g. missing .debug_types section),
3180 otherwise non-zero. */
3181
3182 static int
3183 create_debug_types_hash_table (struct objfile *objfile)
3184 {
3185 htab_t types_htab = NULL;
3186 struct dwarf2_per_cu_data **iter;
3187 int ix;
3188 struct dwarf2_section_info *section;
3189
3190 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3191 {
3192 dwarf2_per_objfile->signatured_types = NULL;
3193 return 0;
3194 }
3195
3196 for (ix = 0;
3197 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3198 ix, section);
3199 ++ix)
3200 {
3201 gdb_byte *info_ptr, *end_ptr;
3202
3203 dwarf2_read_section (objfile, section);
3204 info_ptr = section->buffer;
3205
3206 if (info_ptr == NULL)
3207 continue;
3208
3209 if (types_htab == NULL)
3210 types_htab = allocate_signatured_type_table (objfile);
3211
3212 if (dwarf2_die_debug)
3213 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3214
3215 end_ptr = info_ptr + section->size;
3216 while (info_ptr < end_ptr)
3217 {
3218 unsigned int offset;
3219 unsigned int offset_size;
3220 unsigned int type_offset;
3221 unsigned int length, initial_length_size;
3222 unsigned short version;
3223 ULONGEST signature;
3224 struct signatured_type *type_sig;
3225 void **slot;
3226 gdb_byte *ptr = info_ptr;
3227
3228 offset = ptr - section->buffer;
3229
3230 /* We need to read the type's signature in order to build the hash
3231 table, but we don't need to read anything else just yet. */
3232
3233 /* Sanity check to ensure entire cu is present. */
3234 length = read_initial_length (objfile->obfd, ptr,
3235 &initial_length_size);
3236 if (ptr + length + initial_length_size > end_ptr)
3237 {
3238 complaint (&symfile_complaints,
3239 _("debug type entry runs off end "
3240 "of `.debug_types' section, ignored"));
3241 break;
3242 }
3243
3244 offset_size = initial_length_size == 4 ? 4 : 8;
3245 ptr += initial_length_size;
3246 version = bfd_get_16 (objfile->obfd, ptr);
3247 ptr += 2;
3248 ptr += offset_size; /* abbrev offset */
3249 ptr += 1; /* address size */
3250 signature = bfd_get_64 (objfile->obfd, ptr);
3251 ptr += 8;
3252 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3253 ptr += offset_size;
3254
3255 /* Skip dummy type units. */
3256 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3257 {
3258 info_ptr = info_ptr + initial_length_size + length;
3259 continue;
3260 }
3261
3262 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3263 memset (type_sig, 0, sizeof (*type_sig));
3264 type_sig->signature = signature;
3265 type_sig->type_offset = type_offset;
3266 type_sig->per_cu.objfile = objfile;
3267 type_sig->per_cu.debug_types_section = section;
3268 type_sig->per_cu.offset = offset;
3269
3270 slot = htab_find_slot (types_htab, type_sig, INSERT);
3271 gdb_assert (slot != NULL);
3272 if (*slot != NULL)
3273 {
3274 const struct signatured_type *dup_sig = *slot;
3275
3276 complaint (&symfile_complaints,
3277 _("debug type entry at offset 0x%x is duplicate to the "
3278 "entry at offset 0x%x, signature 0x%s"),
3279 offset, dup_sig->per_cu.offset,
3280 phex (signature, sizeof (signature)));
3281 gdb_assert (signature == dup_sig->signature);
3282 }
3283 *slot = type_sig;
3284
3285 if (dwarf2_die_debug)
3286 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3287 offset, phex (signature, sizeof (signature)));
3288
3289 info_ptr = info_ptr + initial_length_size + length;
3290 }
3291 }
3292
3293 dwarf2_per_objfile->signatured_types = types_htab;
3294
3295 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3296 dwarf2_per_objfile->type_comp_units
3297 = obstack_alloc (&objfile->objfile_obstack,
3298 dwarf2_per_objfile->n_type_comp_units
3299 * sizeof (struct dwarf2_per_cu_data *));
3300 iter = &dwarf2_per_objfile->type_comp_units[0];
3301 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3302 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3303 == dwarf2_per_objfile->n_type_comp_units);
3304
3305 return 1;
3306 }
3307
3308 /* Lookup a signature based type.
3309 Returns NULL if SIG is not present in the table. */
3310
3311 static struct signatured_type *
3312 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3313 {
3314 struct signatured_type find_entry, *entry;
3315
3316 if (dwarf2_per_objfile->signatured_types == NULL)
3317 {
3318 complaint (&symfile_complaints,
3319 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3320 return 0;
3321 }
3322
3323 find_entry.signature = sig;
3324 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3325 return entry;
3326 }
3327
3328 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3329
3330 static void
3331 init_cu_die_reader (struct die_reader_specs *reader,
3332 struct dwarf2_cu *cu)
3333 {
3334 reader->abfd = cu->objfile->obfd;
3335 reader->cu = cu;
3336 if (cu->per_cu->debug_types_section)
3337 {
3338 gdb_assert (cu->per_cu->debug_types_section->readin);
3339 reader->buffer = cu->per_cu->debug_types_section->buffer;
3340 }
3341 else
3342 {
3343 gdb_assert (dwarf2_per_objfile->info.readin);
3344 reader->buffer = dwarf2_per_objfile->info.buffer;
3345 }
3346 }
3347
3348 /* Find the base address of the compilation unit for range lists and
3349 location lists. It will normally be specified by DW_AT_low_pc.
3350 In DWARF-3 draft 4, the base address could be overridden by
3351 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3352 compilation units with discontinuous ranges. */
3353
3354 static void
3355 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3356 {
3357 struct attribute *attr;
3358
3359 cu->base_known = 0;
3360 cu->base_address = 0;
3361
3362 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3363 if (attr)
3364 {
3365 cu->base_address = DW_ADDR (attr);
3366 cu->base_known = 1;
3367 }
3368 else
3369 {
3370 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3371 if (attr)
3372 {
3373 cu->base_address = DW_ADDR (attr);
3374 cu->base_known = 1;
3375 }
3376 }
3377 }
3378
3379 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3380 to combine the common parts.
3381 Process a compilation unit for a psymtab.
3382 BUFFER is a pointer to the beginning of the dwarf section buffer,
3383 either .debug_info or debug_types.
3384 INFO_PTR is a pointer to the start of the CU.
3385 Returns a pointer to the next CU. */
3386
3387 static gdb_byte *
3388 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3389 gdb_byte *buffer, gdb_byte *info_ptr,
3390 unsigned int buffer_size)
3391 {
3392 struct objfile *objfile = this_cu->objfile;
3393 bfd *abfd = objfile->obfd;
3394 gdb_byte *beg_of_comp_unit = info_ptr;
3395 struct die_info *comp_unit_die;
3396 struct partial_symtab *pst;
3397 CORE_ADDR baseaddr;
3398 struct cleanup *back_to_inner;
3399 struct dwarf2_cu cu;
3400 int has_children, has_pc_info;
3401 struct attribute *attr;
3402 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3403 struct die_reader_specs reader_specs;
3404 const char *filename;
3405
3406 /* If this compilation unit was already read in, free the
3407 cached copy in order to read it in again. This is
3408 necessary because we skipped some symbols when we first
3409 read in the compilation unit (see load_partial_dies).
3410 This problem could be avoided, but the benefit is
3411 unclear. */
3412 if (this_cu->cu != NULL)
3413 free_one_cached_comp_unit (this_cu->cu);
3414
3415 /* Note that this is a pointer to our stack frame, being
3416 added to a global data structure. It will be cleaned up
3417 in free_stack_comp_unit when we finish with this
3418 compilation unit. */
3419 init_one_comp_unit (&cu, this_cu);
3420 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3421
3422 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3423 buffer, buffer_size,
3424 abfd,
3425 this_cu->debug_types_section != NULL);
3426
3427 /* Skip dummy compilation units. */
3428 if (info_ptr >= buffer + buffer_size
3429 || peek_abbrev_code (abfd, info_ptr) == 0)
3430 {
3431 info_ptr = (beg_of_comp_unit + cu.header.length
3432 + cu.header.initial_length_size);
3433 do_cleanups (back_to_inner);
3434 return info_ptr;
3435 }
3436
3437 cu.list_in_scope = &file_symbols;
3438
3439 /* Read the abbrevs for this compilation unit into a table. */
3440 dwarf2_read_abbrevs (abfd, &cu);
3441 make_cleanup (dwarf2_free_abbrev_table, &cu);
3442
3443 /* Read the compilation unit die. */
3444 init_cu_die_reader (&reader_specs, &cu);
3445 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3446 &has_children);
3447
3448 if (this_cu->debug_types_section)
3449 {
3450 /* LENGTH has not been set yet for type units. */
3451 gdb_assert (this_cu->offset == cu.header.offset);
3452 this_cu->length = cu.header.length + cu.header.initial_length_size;
3453 }
3454 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3455 {
3456 info_ptr = (beg_of_comp_unit + cu.header.length
3457 + cu.header.initial_length_size);
3458 do_cleanups (back_to_inner);
3459 return info_ptr;
3460 }
3461
3462 prepare_one_comp_unit (&cu, comp_unit_die);
3463
3464 /* Allocate a new partial symbol table structure. */
3465 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3466 if (attr == NULL || !DW_STRING (attr))
3467 filename = "";
3468 else
3469 filename = DW_STRING (attr);
3470 pst = start_psymtab_common (objfile, objfile->section_offsets,
3471 filename,
3472 /* TEXTLOW and TEXTHIGH are set below. */
3473 0,
3474 objfile->global_psymbols.next,
3475 objfile->static_psymbols.next);
3476 pst->psymtabs_addrmap_supported = 1;
3477
3478 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3479 if (attr != NULL)
3480 pst->dirname = DW_STRING (attr);
3481
3482 pst->read_symtab_private = this_cu;
3483
3484 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3485
3486 /* Store the function that reads in the rest of the symbol table. */
3487 pst->read_symtab = dwarf2_psymtab_to_symtab;
3488
3489 this_cu->v.psymtab = pst;
3490
3491 dwarf2_find_base_address (comp_unit_die, &cu);
3492
3493 /* Possibly set the default values of LOWPC and HIGHPC from
3494 `DW_AT_ranges'. */
3495 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3496 &best_highpc, &cu, pst);
3497 if (has_pc_info == 1 && best_lowpc < best_highpc)
3498 /* Store the contiguous range if it is not empty; it can be empty for
3499 CUs with no code. */
3500 addrmap_set_empty (objfile->psymtabs_addrmap,
3501 best_lowpc + baseaddr,
3502 best_highpc + baseaddr - 1, pst);
3503
3504 /* Check if comp unit has_children.
3505 If so, read the rest of the partial symbols from this comp unit.
3506 If not, there's no more debug_info for this comp unit. */
3507 if (has_children)
3508 {
3509 struct partial_die_info *first_die;
3510 CORE_ADDR lowpc, highpc;
3511
3512 lowpc = ((CORE_ADDR) -1);
3513 highpc = ((CORE_ADDR) 0);
3514
3515 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3516
3517 scan_partial_symbols (first_die, &lowpc, &highpc,
3518 ! has_pc_info, &cu);
3519
3520 /* If we didn't find a lowpc, set it to highpc to avoid
3521 complaints from `maint check'. */
3522 if (lowpc == ((CORE_ADDR) -1))
3523 lowpc = highpc;
3524
3525 /* If the compilation unit didn't have an explicit address range,
3526 then use the information extracted from its child dies. */
3527 if (! has_pc_info)
3528 {
3529 best_lowpc = lowpc;
3530 best_highpc = highpc;
3531 }
3532 }
3533 pst->textlow = best_lowpc + baseaddr;
3534 pst->texthigh = best_highpc + baseaddr;
3535
3536 pst->n_global_syms = objfile->global_psymbols.next -
3537 (objfile->global_psymbols.list + pst->globals_offset);
3538 pst->n_static_syms = objfile->static_psymbols.next -
3539 (objfile->static_psymbols.list + pst->statics_offset);
3540 sort_pst_symbols (pst);
3541
3542 info_ptr = (beg_of_comp_unit + cu.header.length
3543 + cu.header.initial_length_size);
3544
3545 if (this_cu->debug_types_section)
3546 {
3547 /* It's not clear we want to do anything with stmt lists here.
3548 Waiting to see what gcc ultimately does. */
3549 }
3550 else
3551 {
3552 /* Get the list of files included in the current compilation unit,
3553 and build a psymtab for each of them. */
3554 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3555 }
3556
3557 do_cleanups (back_to_inner);
3558
3559 return info_ptr;
3560 }
3561
3562 /* Traversal function for htab_traverse_noresize.
3563 Process one .debug_types comp-unit. */
3564
3565 static int
3566 process_type_comp_unit (void **slot, void *info)
3567 {
3568 struct signatured_type *entry = (struct signatured_type *) *slot;
3569 struct dwarf2_per_cu_data *this_cu;
3570
3571 gdb_assert (info == NULL);
3572 this_cu = &entry->per_cu;
3573
3574 gdb_assert (this_cu->debug_types_section->readin);
3575 process_psymtab_comp_unit (this_cu,
3576 this_cu->debug_types_section->buffer,
3577 (this_cu->debug_types_section->buffer
3578 + this_cu->offset),
3579 this_cu->debug_types_section->size);
3580
3581 return 1;
3582 }
3583
3584 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3585 Build partial symbol tables for the .debug_types comp-units. */
3586
3587 static void
3588 build_type_psymtabs (struct objfile *objfile)
3589 {
3590 if (! create_debug_types_hash_table (objfile))
3591 return;
3592
3593 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3594 process_type_comp_unit, NULL);
3595 }
3596
3597 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3598
3599 static void
3600 psymtabs_addrmap_cleanup (void *o)
3601 {
3602 struct objfile *objfile = o;
3603
3604 objfile->psymtabs_addrmap = NULL;
3605 }
3606
3607 /* Build the partial symbol table by doing a quick pass through the
3608 .debug_info and .debug_abbrev sections. */
3609
3610 static void
3611 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3612 {
3613 gdb_byte *info_ptr;
3614 struct cleanup *back_to, *addrmap_cleanup;
3615 struct obstack temp_obstack;
3616
3617 dwarf2_per_objfile->reading_partial_symbols = 1;
3618
3619 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3620 info_ptr = dwarf2_per_objfile->info.buffer;
3621
3622 /* Any cached compilation units will be linked by the per-objfile
3623 read_in_chain. Make sure to free them when we're done. */
3624 back_to = make_cleanup (free_cached_comp_units, NULL);
3625
3626 build_type_psymtabs (objfile);
3627
3628 create_all_comp_units (objfile);
3629
3630 /* Create a temporary address map on a temporary obstack. We later
3631 copy this to the final obstack. */
3632 obstack_init (&temp_obstack);
3633 make_cleanup_obstack_free (&temp_obstack);
3634 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3635 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3636
3637 /* Since the objects we're extracting from .debug_info vary in
3638 length, only the individual functions to extract them (like
3639 read_comp_unit_head and load_partial_die) can really know whether
3640 the buffer is large enough to hold another complete object.
3641
3642 At the moment, they don't actually check that. If .debug_info
3643 holds just one extra byte after the last compilation unit's dies,
3644 then read_comp_unit_head will happily read off the end of the
3645 buffer. read_partial_die is similarly casual. Those functions
3646 should be fixed.
3647
3648 For this loop condition, simply checking whether there's any data
3649 left at all should be sufficient. */
3650
3651 while (info_ptr < (dwarf2_per_objfile->info.buffer
3652 + dwarf2_per_objfile->info.size))
3653 {
3654 struct dwarf2_per_cu_data *this_cu;
3655
3656 this_cu = dwarf2_find_comp_unit (info_ptr
3657 - dwarf2_per_objfile->info.buffer,
3658 objfile);
3659
3660 info_ptr = process_psymtab_comp_unit (this_cu,
3661 dwarf2_per_objfile->info.buffer,
3662 info_ptr,
3663 dwarf2_per_objfile->info.size);
3664 }
3665
3666 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3667 &objfile->objfile_obstack);
3668 discard_cleanups (addrmap_cleanup);
3669
3670 do_cleanups (back_to);
3671 }
3672
3673 /* Load the partial DIEs for a secondary CU into memory. */
3674
3675 static void
3676 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3677 {
3678 struct objfile *objfile = this_cu->objfile;
3679 bfd *abfd = objfile->obfd;
3680 gdb_byte *info_ptr;
3681 struct die_info *comp_unit_die;
3682 struct dwarf2_cu *cu;
3683 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3684 int has_children;
3685 struct die_reader_specs reader_specs;
3686 int read_cu = 0;
3687
3688 gdb_assert (! this_cu->debug_types_section);
3689
3690 gdb_assert (dwarf2_per_objfile->info.readin);
3691 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3692
3693 if (this_cu->cu == NULL)
3694 {
3695 cu = xmalloc (sizeof (*cu));
3696 init_one_comp_unit (cu, this_cu);
3697
3698 read_cu = 1;
3699
3700 /* If an error occurs while loading, release our storage. */
3701 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3702
3703 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3704 dwarf2_per_objfile->info.buffer,
3705 dwarf2_per_objfile->info.size,
3706 abfd, 0);
3707
3708 /* Skip dummy compilation units. */
3709 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3710 + dwarf2_per_objfile->info.size)
3711 || peek_abbrev_code (abfd, info_ptr) == 0)
3712 {
3713 do_cleanups (free_cu_cleanup);
3714 return;
3715 }
3716
3717 /* Link this CU into read_in_chain. */
3718 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3719 dwarf2_per_objfile->read_in_chain = this_cu;
3720 }
3721 else
3722 {
3723 cu = this_cu->cu;
3724 info_ptr += cu->header.first_die_offset;
3725 }
3726
3727 /* Read the abbrevs for this compilation unit into a table. */
3728 gdb_assert (cu->dwarf2_abbrevs == NULL);
3729 dwarf2_read_abbrevs (abfd, cu);
3730 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3731
3732 /* Read the compilation unit die. */
3733 init_cu_die_reader (&reader_specs, cu);
3734 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3735 &has_children);
3736
3737 prepare_one_comp_unit (cu, comp_unit_die);
3738
3739 /* Check if comp unit has_children.
3740 If so, read the rest of the partial symbols from this comp unit.
3741 If not, there's no more debug_info for this comp unit. */
3742 if (has_children)
3743 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3744
3745 do_cleanups (free_abbrevs_cleanup);
3746
3747 if (read_cu)
3748 {
3749 /* We've successfully allocated this compilation unit. Let our
3750 caller clean it up when finished with it. */
3751 discard_cleanups (free_cu_cleanup);
3752 }
3753 }
3754
3755 /* Create a list of all compilation units in OBJFILE.
3756 This is only done for -readnow and building partial symtabs. */
3757
3758 static void
3759 create_all_comp_units (struct objfile *objfile)
3760 {
3761 int n_allocated;
3762 int n_comp_units;
3763 struct dwarf2_per_cu_data **all_comp_units;
3764 gdb_byte *info_ptr;
3765
3766 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3767 info_ptr = dwarf2_per_objfile->info.buffer;
3768
3769 n_comp_units = 0;
3770 n_allocated = 10;
3771 all_comp_units = xmalloc (n_allocated
3772 * sizeof (struct dwarf2_per_cu_data *));
3773
3774 while (info_ptr < dwarf2_per_objfile->info.buffer
3775 + dwarf2_per_objfile->info.size)
3776 {
3777 unsigned int length, initial_length_size;
3778 struct dwarf2_per_cu_data *this_cu;
3779 unsigned int offset;
3780
3781 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3782
3783 /* Read just enough information to find out where the next
3784 compilation unit is. */
3785 length = read_initial_length (objfile->obfd, info_ptr,
3786 &initial_length_size);
3787
3788 /* Save the compilation unit for later lookup. */
3789 this_cu = obstack_alloc (&objfile->objfile_obstack,
3790 sizeof (struct dwarf2_per_cu_data));
3791 memset (this_cu, 0, sizeof (*this_cu));
3792 this_cu->offset = offset;
3793 this_cu->length = length + initial_length_size;
3794 this_cu->objfile = objfile;
3795
3796 if (n_comp_units == n_allocated)
3797 {
3798 n_allocated *= 2;
3799 all_comp_units = xrealloc (all_comp_units,
3800 n_allocated
3801 * sizeof (struct dwarf2_per_cu_data *));
3802 }
3803 all_comp_units[n_comp_units++] = this_cu;
3804
3805 info_ptr = info_ptr + this_cu->length;
3806 }
3807
3808 dwarf2_per_objfile->all_comp_units
3809 = obstack_alloc (&objfile->objfile_obstack,
3810 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3811 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3812 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3813 xfree (all_comp_units);
3814 dwarf2_per_objfile->n_comp_units = n_comp_units;
3815 }
3816
3817 /* Process all loaded DIEs for compilation unit CU, starting at
3818 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3819 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3820 DW_AT_ranges). If NEED_PC is set, then this function will set
3821 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3822 and record the covered ranges in the addrmap. */
3823
3824 static void
3825 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3826 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3827 {
3828 struct partial_die_info *pdi;
3829
3830 /* Now, march along the PDI's, descending into ones which have
3831 interesting children but skipping the children of the other ones,
3832 until we reach the end of the compilation unit. */
3833
3834 pdi = first_die;
3835
3836 while (pdi != NULL)
3837 {
3838 fixup_partial_die (pdi, cu);
3839
3840 /* Anonymous namespaces or modules have no name but have interesting
3841 children, so we need to look at them. Ditto for anonymous
3842 enums. */
3843
3844 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3845 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3846 {
3847 switch (pdi->tag)
3848 {
3849 case DW_TAG_subprogram:
3850 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3851 break;
3852 case DW_TAG_constant:
3853 case DW_TAG_variable:
3854 case DW_TAG_typedef:
3855 case DW_TAG_union_type:
3856 if (!pdi->is_declaration)
3857 {
3858 add_partial_symbol (pdi, cu);
3859 }
3860 break;
3861 case DW_TAG_class_type:
3862 case DW_TAG_interface_type:
3863 case DW_TAG_structure_type:
3864 if (!pdi->is_declaration)
3865 {
3866 add_partial_symbol (pdi, cu);
3867 }
3868 break;
3869 case DW_TAG_enumeration_type:
3870 if (!pdi->is_declaration)
3871 add_partial_enumeration (pdi, cu);
3872 break;
3873 case DW_TAG_base_type:
3874 case DW_TAG_subrange_type:
3875 /* File scope base type definitions are added to the partial
3876 symbol table. */
3877 add_partial_symbol (pdi, cu);
3878 break;
3879 case DW_TAG_namespace:
3880 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3881 break;
3882 case DW_TAG_module:
3883 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3884 break;
3885 default:
3886 break;
3887 }
3888 }
3889
3890 /* If the die has a sibling, skip to the sibling. */
3891
3892 pdi = pdi->die_sibling;
3893 }
3894 }
3895
3896 /* Functions used to compute the fully scoped name of a partial DIE.
3897
3898 Normally, this is simple. For C++, the parent DIE's fully scoped
3899 name is concatenated with "::" and the partial DIE's name. For
3900 Java, the same thing occurs except that "." is used instead of "::".
3901 Enumerators are an exception; they use the scope of their parent
3902 enumeration type, i.e. the name of the enumeration type is not
3903 prepended to the enumerator.
3904
3905 There are two complexities. One is DW_AT_specification; in this
3906 case "parent" means the parent of the target of the specification,
3907 instead of the direct parent of the DIE. The other is compilers
3908 which do not emit DW_TAG_namespace; in this case we try to guess
3909 the fully qualified name of structure types from their members'
3910 linkage names. This must be done using the DIE's children rather
3911 than the children of any DW_AT_specification target. We only need
3912 to do this for structures at the top level, i.e. if the target of
3913 any DW_AT_specification (if any; otherwise the DIE itself) does not
3914 have a parent. */
3915
3916 /* Compute the scope prefix associated with PDI's parent, in
3917 compilation unit CU. The result will be allocated on CU's
3918 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3919 field. NULL is returned if no prefix is necessary. */
3920 static char *
3921 partial_die_parent_scope (struct partial_die_info *pdi,
3922 struct dwarf2_cu *cu)
3923 {
3924 char *grandparent_scope;
3925 struct partial_die_info *parent, *real_pdi;
3926
3927 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3928 then this means the parent of the specification DIE. */
3929
3930 real_pdi = pdi;
3931 while (real_pdi->has_specification)
3932 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3933
3934 parent = real_pdi->die_parent;
3935 if (parent == NULL)
3936 return NULL;
3937
3938 if (parent->scope_set)
3939 return parent->scope;
3940
3941 fixup_partial_die (parent, cu);
3942
3943 grandparent_scope = partial_die_parent_scope (parent, cu);
3944
3945 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3946 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3947 Work around this problem here. */
3948 if (cu->language == language_cplus
3949 && parent->tag == DW_TAG_namespace
3950 && strcmp (parent->name, "::") == 0
3951 && grandparent_scope == NULL)
3952 {
3953 parent->scope = NULL;
3954 parent->scope_set = 1;
3955 return NULL;
3956 }
3957
3958 if (pdi->tag == DW_TAG_enumerator)
3959 /* Enumerators should not get the name of the enumeration as a prefix. */
3960 parent->scope = grandparent_scope;
3961 else if (parent->tag == DW_TAG_namespace
3962 || parent->tag == DW_TAG_module
3963 || parent->tag == DW_TAG_structure_type
3964 || parent->tag == DW_TAG_class_type
3965 || parent->tag == DW_TAG_interface_type
3966 || parent->tag == DW_TAG_union_type
3967 || parent->tag == DW_TAG_enumeration_type)
3968 {
3969 if (grandparent_scope == NULL)
3970 parent->scope = parent->name;
3971 else
3972 parent->scope = typename_concat (&cu->comp_unit_obstack,
3973 grandparent_scope,
3974 parent->name, 0, cu);
3975 }
3976 else
3977 {
3978 /* FIXME drow/2004-04-01: What should we be doing with
3979 function-local names? For partial symbols, we should probably be
3980 ignoring them. */
3981 complaint (&symfile_complaints,
3982 _("unhandled containing DIE tag %d for DIE at %d"),
3983 parent->tag, pdi->offset);
3984 parent->scope = grandparent_scope;
3985 }
3986
3987 parent->scope_set = 1;
3988 return parent->scope;
3989 }
3990
3991 /* Return the fully scoped name associated with PDI, from compilation unit
3992 CU. The result will be allocated with malloc. */
3993 static char *
3994 partial_die_full_name (struct partial_die_info *pdi,
3995 struct dwarf2_cu *cu)
3996 {
3997 char *parent_scope;
3998
3999 /* If this is a template instantiation, we can not work out the
4000 template arguments from partial DIEs. So, unfortunately, we have
4001 to go through the full DIEs. At least any work we do building
4002 types here will be reused if full symbols are loaded later. */
4003 if (pdi->has_template_arguments)
4004 {
4005 fixup_partial_die (pdi, cu);
4006
4007 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4008 {
4009 struct die_info *die;
4010 struct attribute attr;
4011 struct dwarf2_cu *ref_cu = cu;
4012
4013 attr.name = 0;
4014 attr.form = DW_FORM_ref_addr;
4015 attr.u.addr = pdi->offset;
4016 die = follow_die_ref (NULL, &attr, &ref_cu);
4017
4018 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4019 }
4020 }
4021
4022 parent_scope = partial_die_parent_scope (pdi, cu);
4023 if (parent_scope == NULL)
4024 return NULL;
4025 else
4026 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4027 }
4028
4029 static void
4030 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4031 {
4032 struct objfile *objfile = cu->objfile;
4033 CORE_ADDR addr = 0;
4034 char *actual_name = NULL;
4035 const struct partial_symbol *psym = NULL;
4036 CORE_ADDR baseaddr;
4037 int built_actual_name = 0;
4038
4039 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4040
4041 actual_name = partial_die_full_name (pdi, cu);
4042 if (actual_name)
4043 built_actual_name = 1;
4044
4045 if (actual_name == NULL)
4046 actual_name = pdi->name;
4047
4048 switch (pdi->tag)
4049 {
4050 case DW_TAG_subprogram:
4051 if (pdi->is_external || cu->language == language_ada)
4052 {
4053 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4054 of the global scope. But in Ada, we want to be able to access
4055 nested procedures globally. So all Ada subprograms are stored
4056 in the global scope. */
4057 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4058 mst_text, objfile); */
4059 add_psymbol_to_list (actual_name, strlen (actual_name),
4060 built_actual_name,
4061 VAR_DOMAIN, LOC_BLOCK,
4062 &objfile->global_psymbols,
4063 0, pdi->lowpc + baseaddr,
4064 cu->language, objfile);
4065 }
4066 else
4067 {
4068 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4069 mst_file_text, objfile); */
4070 add_psymbol_to_list (actual_name, strlen (actual_name),
4071 built_actual_name,
4072 VAR_DOMAIN, LOC_BLOCK,
4073 &objfile->static_psymbols,
4074 0, pdi->lowpc + baseaddr,
4075 cu->language, objfile);
4076 }
4077 break;
4078 case DW_TAG_constant:
4079 {
4080 struct psymbol_allocation_list *list;
4081
4082 if (pdi->is_external)
4083 list = &objfile->global_psymbols;
4084 else
4085 list = &objfile->static_psymbols;
4086 add_psymbol_to_list (actual_name, strlen (actual_name),
4087 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4088 list, 0, 0, cu->language, objfile);
4089 }
4090 break;
4091 case DW_TAG_variable:
4092 if (pdi->locdesc)
4093 addr = decode_locdesc (pdi->locdesc, cu);
4094
4095 if (pdi->locdesc
4096 && addr == 0
4097 && !dwarf2_per_objfile->has_section_at_zero)
4098 {
4099 /* A global or static variable may also have been stripped
4100 out by the linker if unused, in which case its address
4101 will be nullified; do not add such variables into partial
4102 symbol table then. */
4103 }
4104 else if (pdi->is_external)
4105 {
4106 /* Global Variable.
4107 Don't enter into the minimal symbol tables as there is
4108 a minimal symbol table entry from the ELF symbols already.
4109 Enter into partial symbol table if it has a location
4110 descriptor or a type.
4111 If the location descriptor is missing, new_symbol will create
4112 a LOC_UNRESOLVED symbol, the address of the variable will then
4113 be determined from the minimal symbol table whenever the variable
4114 is referenced.
4115 The address for the partial symbol table entry is not
4116 used by GDB, but it comes in handy for debugging partial symbol
4117 table building. */
4118
4119 if (pdi->locdesc || pdi->has_type)
4120 add_psymbol_to_list (actual_name, strlen (actual_name),
4121 built_actual_name,
4122 VAR_DOMAIN, LOC_STATIC,
4123 &objfile->global_psymbols,
4124 0, addr + baseaddr,
4125 cu->language, objfile);
4126 }
4127 else
4128 {
4129 /* Static Variable. Skip symbols without location descriptors. */
4130 if (pdi->locdesc == NULL)
4131 {
4132 if (built_actual_name)
4133 xfree (actual_name);
4134 return;
4135 }
4136 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4137 mst_file_data, objfile); */
4138 add_psymbol_to_list (actual_name, strlen (actual_name),
4139 built_actual_name,
4140 VAR_DOMAIN, LOC_STATIC,
4141 &objfile->static_psymbols,
4142 0, addr + baseaddr,
4143 cu->language, objfile);
4144 }
4145 break;
4146 case DW_TAG_typedef:
4147 case DW_TAG_base_type:
4148 case DW_TAG_subrange_type:
4149 add_psymbol_to_list (actual_name, strlen (actual_name),
4150 built_actual_name,
4151 VAR_DOMAIN, LOC_TYPEDEF,
4152 &objfile->static_psymbols,
4153 0, (CORE_ADDR) 0, cu->language, objfile);
4154 break;
4155 case DW_TAG_namespace:
4156 add_psymbol_to_list (actual_name, strlen (actual_name),
4157 built_actual_name,
4158 VAR_DOMAIN, LOC_TYPEDEF,
4159 &objfile->global_psymbols,
4160 0, (CORE_ADDR) 0, cu->language, objfile);
4161 break;
4162 case DW_TAG_class_type:
4163 case DW_TAG_interface_type:
4164 case DW_TAG_structure_type:
4165 case DW_TAG_union_type:
4166 case DW_TAG_enumeration_type:
4167 /* Skip external references. The DWARF standard says in the section
4168 about "Structure, Union, and Class Type Entries": "An incomplete
4169 structure, union or class type is represented by a structure,
4170 union or class entry that does not have a byte size attribute
4171 and that has a DW_AT_declaration attribute." */
4172 if (!pdi->has_byte_size && pdi->is_declaration)
4173 {
4174 if (built_actual_name)
4175 xfree (actual_name);
4176 return;
4177 }
4178
4179 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4180 static vs. global. */
4181 add_psymbol_to_list (actual_name, strlen (actual_name),
4182 built_actual_name,
4183 STRUCT_DOMAIN, LOC_TYPEDEF,
4184 (cu->language == language_cplus
4185 || cu->language == language_java)
4186 ? &objfile->global_psymbols
4187 : &objfile->static_psymbols,
4188 0, (CORE_ADDR) 0, cu->language, objfile);
4189
4190 break;
4191 case DW_TAG_enumerator:
4192 add_psymbol_to_list (actual_name, strlen (actual_name),
4193 built_actual_name,
4194 VAR_DOMAIN, LOC_CONST,
4195 (cu->language == language_cplus
4196 || cu->language == language_java)
4197 ? &objfile->global_psymbols
4198 : &objfile->static_psymbols,
4199 0, (CORE_ADDR) 0, cu->language, objfile);
4200 break;
4201 default:
4202 break;
4203 }
4204
4205 if (built_actual_name)
4206 xfree (actual_name);
4207 }
4208
4209 /* Read a partial die corresponding to a namespace; also, add a symbol
4210 corresponding to that namespace to the symbol table. NAMESPACE is
4211 the name of the enclosing namespace. */
4212
4213 static void
4214 add_partial_namespace (struct partial_die_info *pdi,
4215 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4216 int need_pc, struct dwarf2_cu *cu)
4217 {
4218 /* Add a symbol for the namespace. */
4219
4220 add_partial_symbol (pdi, cu);
4221
4222 /* Now scan partial symbols in that namespace. */
4223
4224 if (pdi->has_children)
4225 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4226 }
4227
4228 /* Read a partial die corresponding to a Fortran module. */
4229
4230 static void
4231 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4232 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4233 {
4234 /* Now scan partial symbols in that module. */
4235
4236 if (pdi->has_children)
4237 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4238 }
4239
4240 /* Read a partial die corresponding to a subprogram and create a partial
4241 symbol for that subprogram. When the CU language allows it, this
4242 routine also defines a partial symbol for each nested subprogram
4243 that this subprogram contains.
4244
4245 DIE my also be a lexical block, in which case we simply search
4246 recursively for suprograms defined inside that lexical block.
4247 Again, this is only performed when the CU language allows this
4248 type of definitions. */
4249
4250 static void
4251 add_partial_subprogram (struct partial_die_info *pdi,
4252 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4253 int need_pc, struct dwarf2_cu *cu)
4254 {
4255 if (pdi->tag == DW_TAG_subprogram)
4256 {
4257 if (pdi->has_pc_info)
4258 {
4259 if (pdi->lowpc < *lowpc)
4260 *lowpc = pdi->lowpc;
4261 if (pdi->highpc > *highpc)
4262 *highpc = pdi->highpc;
4263 if (need_pc)
4264 {
4265 CORE_ADDR baseaddr;
4266 struct objfile *objfile = cu->objfile;
4267
4268 baseaddr = ANOFFSET (objfile->section_offsets,
4269 SECT_OFF_TEXT (objfile));
4270 addrmap_set_empty (objfile->psymtabs_addrmap,
4271 pdi->lowpc + baseaddr,
4272 pdi->highpc - 1 + baseaddr,
4273 cu->per_cu->v.psymtab);
4274 }
4275 if (!pdi->is_declaration)
4276 /* Ignore subprogram DIEs that do not have a name, they are
4277 illegal. Do not emit a complaint at this point, we will
4278 do so when we convert this psymtab into a symtab. */
4279 if (pdi->name)
4280 add_partial_symbol (pdi, cu);
4281 }
4282 }
4283
4284 if (! pdi->has_children)
4285 return;
4286
4287 if (cu->language == language_ada)
4288 {
4289 pdi = pdi->die_child;
4290 while (pdi != NULL)
4291 {
4292 fixup_partial_die (pdi, cu);
4293 if (pdi->tag == DW_TAG_subprogram
4294 || pdi->tag == DW_TAG_lexical_block)
4295 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4296 pdi = pdi->die_sibling;
4297 }
4298 }
4299 }
4300
4301 /* Read a partial die corresponding to an enumeration type. */
4302
4303 static void
4304 add_partial_enumeration (struct partial_die_info *enum_pdi,
4305 struct dwarf2_cu *cu)
4306 {
4307 struct partial_die_info *pdi;
4308
4309 if (enum_pdi->name != NULL)
4310 add_partial_symbol (enum_pdi, cu);
4311
4312 pdi = enum_pdi->die_child;
4313 while (pdi)
4314 {
4315 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4316 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4317 else
4318 add_partial_symbol (pdi, cu);
4319 pdi = pdi->die_sibling;
4320 }
4321 }
4322
4323 /* Return the initial uleb128 in the die at INFO_PTR. */
4324
4325 static unsigned int
4326 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4327 {
4328 unsigned int bytes_read;
4329
4330 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4331 }
4332
4333 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4334 Return the corresponding abbrev, or NULL if the number is zero (indicating
4335 an empty DIE). In either case *BYTES_READ will be set to the length of
4336 the initial number. */
4337
4338 static struct abbrev_info *
4339 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4340 struct dwarf2_cu *cu)
4341 {
4342 bfd *abfd = cu->objfile->obfd;
4343 unsigned int abbrev_number;
4344 struct abbrev_info *abbrev;
4345
4346 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4347
4348 if (abbrev_number == 0)
4349 return NULL;
4350
4351 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4352 if (!abbrev)
4353 {
4354 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4355 abbrev_number, bfd_get_filename (abfd));
4356 }
4357
4358 return abbrev;
4359 }
4360
4361 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4362 Returns a pointer to the end of a series of DIEs, terminated by an empty
4363 DIE. Any children of the skipped DIEs will also be skipped. */
4364
4365 static gdb_byte *
4366 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4367 {
4368 struct abbrev_info *abbrev;
4369 unsigned int bytes_read;
4370
4371 while (1)
4372 {
4373 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4374 if (abbrev == NULL)
4375 return info_ptr + bytes_read;
4376 else
4377 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4378 }
4379 }
4380
4381 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4382 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4383 abbrev corresponding to that skipped uleb128 should be passed in
4384 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4385 children. */
4386
4387 static gdb_byte *
4388 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4389 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4390 {
4391 unsigned int bytes_read;
4392 struct attribute attr;
4393 bfd *abfd = cu->objfile->obfd;
4394 unsigned int form, i;
4395
4396 for (i = 0; i < abbrev->num_attrs; i++)
4397 {
4398 /* The only abbrev we care about is DW_AT_sibling. */
4399 if (abbrev->attrs[i].name == DW_AT_sibling)
4400 {
4401 read_attribute (&attr, &abbrev->attrs[i],
4402 abfd, info_ptr, cu);
4403 if (attr.form == DW_FORM_ref_addr)
4404 complaint (&symfile_complaints,
4405 _("ignoring absolute DW_AT_sibling"));
4406 else
4407 return buffer + dwarf2_get_ref_die_offset (&attr);
4408 }
4409
4410 /* If it isn't DW_AT_sibling, skip this attribute. */
4411 form = abbrev->attrs[i].form;
4412 skip_attribute:
4413 switch (form)
4414 {
4415 case DW_FORM_ref_addr:
4416 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4417 and later it is offset sized. */
4418 if (cu->header.version == 2)
4419 info_ptr += cu->header.addr_size;
4420 else
4421 info_ptr += cu->header.offset_size;
4422 break;
4423 case DW_FORM_addr:
4424 info_ptr += cu->header.addr_size;
4425 break;
4426 case DW_FORM_data1:
4427 case DW_FORM_ref1:
4428 case DW_FORM_flag:
4429 info_ptr += 1;
4430 break;
4431 case DW_FORM_flag_present:
4432 break;
4433 case DW_FORM_data2:
4434 case DW_FORM_ref2:
4435 info_ptr += 2;
4436 break;
4437 case DW_FORM_data4:
4438 case DW_FORM_ref4:
4439 info_ptr += 4;
4440 break;
4441 case DW_FORM_data8:
4442 case DW_FORM_ref8:
4443 case DW_FORM_ref_sig8:
4444 info_ptr += 8;
4445 break;
4446 case DW_FORM_string:
4447 read_direct_string (abfd, info_ptr, &bytes_read);
4448 info_ptr += bytes_read;
4449 break;
4450 case DW_FORM_sec_offset:
4451 case DW_FORM_strp:
4452 info_ptr += cu->header.offset_size;
4453 break;
4454 case DW_FORM_exprloc:
4455 case DW_FORM_block:
4456 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4457 info_ptr += bytes_read;
4458 break;
4459 case DW_FORM_block1:
4460 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4461 break;
4462 case DW_FORM_block2:
4463 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4464 break;
4465 case DW_FORM_block4:
4466 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4467 break;
4468 case DW_FORM_sdata:
4469 case DW_FORM_udata:
4470 case DW_FORM_ref_udata:
4471 info_ptr = skip_leb128 (abfd, info_ptr);
4472 break;
4473 case DW_FORM_indirect:
4474 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4475 info_ptr += bytes_read;
4476 /* We need to continue parsing from here, so just go back to
4477 the top. */
4478 goto skip_attribute;
4479
4480 default:
4481 error (_("Dwarf Error: Cannot handle %s "
4482 "in DWARF reader [in module %s]"),
4483 dwarf_form_name (form),
4484 bfd_get_filename (abfd));
4485 }
4486 }
4487
4488 if (abbrev->has_children)
4489 return skip_children (buffer, info_ptr, cu);
4490 else
4491 return info_ptr;
4492 }
4493
4494 /* Locate ORIG_PDI's sibling.
4495 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4496 in BUFFER. */
4497
4498 static gdb_byte *
4499 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4500 gdb_byte *buffer, gdb_byte *info_ptr,
4501 bfd *abfd, struct dwarf2_cu *cu)
4502 {
4503 /* Do we know the sibling already? */
4504
4505 if (orig_pdi->sibling)
4506 return orig_pdi->sibling;
4507
4508 /* Are there any children to deal with? */
4509
4510 if (!orig_pdi->has_children)
4511 return info_ptr;
4512
4513 /* Skip the children the long way. */
4514
4515 return skip_children (buffer, info_ptr, cu);
4516 }
4517
4518 /* Expand this partial symbol table into a full symbol table. */
4519
4520 static void
4521 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4522 {
4523 if (pst != NULL)
4524 {
4525 if (pst->readin)
4526 {
4527 warning (_("bug: psymtab for %s is already read in."),
4528 pst->filename);
4529 }
4530 else
4531 {
4532 if (info_verbose)
4533 {
4534 printf_filtered (_("Reading in symbols for %s..."),
4535 pst->filename);
4536 gdb_flush (gdb_stdout);
4537 }
4538
4539 /* Restore our global data. */
4540 dwarf2_per_objfile = objfile_data (pst->objfile,
4541 dwarf2_objfile_data_key);
4542
4543 /* If this psymtab is constructed from a debug-only objfile, the
4544 has_section_at_zero flag will not necessarily be correct. We
4545 can get the correct value for this flag by looking at the data
4546 associated with the (presumably stripped) associated objfile. */
4547 if (pst->objfile->separate_debug_objfile_backlink)
4548 {
4549 struct dwarf2_per_objfile *dpo_backlink
4550 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4551 dwarf2_objfile_data_key);
4552
4553 dwarf2_per_objfile->has_section_at_zero
4554 = dpo_backlink->has_section_at_zero;
4555 }
4556
4557 dwarf2_per_objfile->reading_partial_symbols = 0;
4558
4559 psymtab_to_symtab_1 (pst);
4560
4561 /* Finish up the debug error message. */
4562 if (info_verbose)
4563 printf_filtered (_("done.\n"));
4564 }
4565 }
4566 }
4567 \f
4568 /* Reading in full CUs. */
4569
4570 /* Add PER_CU to the queue. */
4571
4572 static void
4573 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4574 {
4575 struct dwarf2_queue_item *item;
4576
4577 per_cu->queued = 1;
4578 item = xmalloc (sizeof (*item));
4579 item->per_cu = per_cu;
4580 item->next = NULL;
4581
4582 if (dwarf2_queue == NULL)
4583 dwarf2_queue = item;
4584 else
4585 dwarf2_queue_tail->next = item;
4586
4587 dwarf2_queue_tail = item;
4588 }
4589
4590 /* Process the queue. */
4591
4592 static void
4593 process_queue (void)
4594 {
4595 struct dwarf2_queue_item *item, *next_item;
4596
4597 /* The queue starts out with one item, but following a DIE reference
4598 may load a new CU, adding it to the end of the queue. */
4599 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4600 {
4601 if (dwarf2_per_objfile->using_index
4602 ? !item->per_cu->v.quick->symtab
4603 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4604 process_full_comp_unit (item->per_cu);
4605
4606 item->per_cu->queued = 0;
4607 next_item = item->next;
4608 xfree (item);
4609 }
4610
4611 dwarf2_queue_tail = NULL;
4612 }
4613
4614 /* Free all allocated queue entries. This function only releases anything if
4615 an error was thrown; if the queue was processed then it would have been
4616 freed as we went along. */
4617
4618 static void
4619 dwarf2_release_queue (void *dummy)
4620 {
4621 struct dwarf2_queue_item *item, *last;
4622
4623 item = dwarf2_queue;
4624 while (item)
4625 {
4626 /* Anything still marked queued is likely to be in an
4627 inconsistent state, so discard it. */
4628 if (item->per_cu->queued)
4629 {
4630 if (item->per_cu->cu != NULL)
4631 free_one_cached_comp_unit (item->per_cu->cu);
4632 item->per_cu->queued = 0;
4633 }
4634
4635 last = item;
4636 item = item->next;
4637 xfree (last);
4638 }
4639
4640 dwarf2_queue = dwarf2_queue_tail = NULL;
4641 }
4642
4643 /* Read in full symbols for PST, and anything it depends on. */
4644
4645 static void
4646 psymtab_to_symtab_1 (struct partial_symtab *pst)
4647 {
4648 struct dwarf2_per_cu_data *per_cu;
4649 struct cleanup *back_to;
4650 int i;
4651
4652 for (i = 0; i < pst->number_of_dependencies; i++)
4653 if (!pst->dependencies[i]->readin)
4654 {
4655 /* Inform about additional files that need to be read in. */
4656 if (info_verbose)
4657 {
4658 /* FIXME: i18n: Need to make this a single string. */
4659 fputs_filtered (" ", gdb_stdout);
4660 wrap_here ("");
4661 fputs_filtered ("and ", gdb_stdout);
4662 wrap_here ("");
4663 printf_filtered ("%s...", pst->dependencies[i]->filename);
4664 wrap_here (""); /* Flush output. */
4665 gdb_flush (gdb_stdout);
4666 }
4667 psymtab_to_symtab_1 (pst->dependencies[i]);
4668 }
4669
4670 per_cu = pst->read_symtab_private;
4671
4672 if (per_cu == NULL)
4673 {
4674 /* It's an include file, no symbols to read for it.
4675 Everything is in the parent symtab. */
4676 pst->readin = 1;
4677 return;
4678 }
4679
4680 dw2_do_instantiate_symtab (per_cu);
4681 }
4682
4683 /* Load the DIEs associated with PER_CU into memory. */
4684
4685 static void
4686 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4687 {
4688 struct objfile *objfile = per_cu->objfile;
4689 bfd *abfd = objfile->obfd;
4690 struct dwarf2_cu *cu;
4691 unsigned int offset;
4692 gdb_byte *info_ptr, *beg_of_comp_unit;
4693 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4694 struct attribute *attr;
4695 int read_cu = 0;
4696
4697 gdb_assert (! per_cu->debug_types_section);
4698
4699 /* Set local variables from the partial symbol table info. */
4700 offset = per_cu->offset;
4701
4702 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4703 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4704 beg_of_comp_unit = info_ptr;
4705
4706 if (per_cu->cu == NULL)
4707 {
4708 cu = xmalloc (sizeof (*cu));
4709 init_one_comp_unit (cu, per_cu);
4710
4711 read_cu = 1;
4712
4713 /* If an error occurs while loading, release our storage. */
4714 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4715
4716 /* Read in the comp_unit header. */
4717 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4718
4719 /* Skip dummy compilation units. */
4720 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4721 + dwarf2_per_objfile->info.size)
4722 || peek_abbrev_code (abfd, info_ptr) == 0)
4723 {
4724 do_cleanups (free_cu_cleanup);
4725 return;
4726 }
4727
4728 /* Complete the cu_header. */
4729 cu->header.offset = offset;
4730 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4731
4732 /* Read the abbrevs for this compilation unit. */
4733 dwarf2_read_abbrevs (abfd, cu);
4734 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4735
4736 /* Link this CU into read_in_chain. */
4737 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4738 dwarf2_per_objfile->read_in_chain = per_cu;
4739 }
4740 else
4741 {
4742 cu = per_cu->cu;
4743 info_ptr += cu->header.first_die_offset;
4744 }
4745
4746 cu->dies = read_comp_unit (info_ptr, cu);
4747
4748 /* We try not to read any attributes in this function, because not
4749 all CUs needed for references have been loaded yet, and symbol
4750 table processing isn't initialized. But we have to set the CU language,
4751 or we won't be able to build types correctly. */
4752 prepare_one_comp_unit (cu, cu->dies);
4753
4754 /* Similarly, if we do not read the producer, we can not apply
4755 producer-specific interpretation. */
4756 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4757 if (attr)
4758 cu->producer = DW_STRING (attr);
4759
4760 if (read_cu)
4761 {
4762 do_cleanups (free_abbrevs_cleanup);
4763
4764 /* We've successfully allocated this compilation unit. Let our
4765 caller clean it up when finished with it. */
4766 discard_cleanups (free_cu_cleanup);
4767 }
4768 }
4769
4770 /* Add a DIE to the delayed physname list. */
4771
4772 static void
4773 add_to_method_list (struct type *type, int fnfield_index, int index,
4774 const char *name, struct die_info *die,
4775 struct dwarf2_cu *cu)
4776 {
4777 struct delayed_method_info mi;
4778 mi.type = type;
4779 mi.fnfield_index = fnfield_index;
4780 mi.index = index;
4781 mi.name = name;
4782 mi.die = die;
4783 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4784 }
4785
4786 /* A cleanup for freeing the delayed method list. */
4787
4788 static void
4789 free_delayed_list (void *ptr)
4790 {
4791 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4792 if (cu->method_list != NULL)
4793 {
4794 VEC_free (delayed_method_info, cu->method_list);
4795 cu->method_list = NULL;
4796 }
4797 }
4798
4799 /* Compute the physnames of any methods on the CU's method list.
4800
4801 The computation of method physnames is delayed in order to avoid the
4802 (bad) condition that one of the method's formal parameters is of an as yet
4803 incomplete type. */
4804
4805 static void
4806 compute_delayed_physnames (struct dwarf2_cu *cu)
4807 {
4808 int i;
4809 struct delayed_method_info *mi;
4810 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4811 {
4812 const char *physname;
4813 struct fn_fieldlist *fn_flp
4814 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4815 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4816 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4817 }
4818 }
4819
4820 /* Generate full symbol information for PER_CU, whose DIEs have
4821 already been loaded into memory. */
4822
4823 static void
4824 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4825 {
4826 struct dwarf2_cu *cu = per_cu->cu;
4827 struct objfile *objfile = per_cu->objfile;
4828 CORE_ADDR lowpc, highpc;
4829 struct symtab *symtab;
4830 struct cleanup *back_to, *delayed_list_cleanup;
4831 CORE_ADDR baseaddr;
4832
4833 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4834
4835 buildsym_init ();
4836 back_to = make_cleanup (really_free_pendings, NULL);
4837 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4838
4839 cu->list_in_scope = &file_symbols;
4840
4841 /* Do line number decoding in read_file_scope () */
4842 process_die (cu->dies, cu);
4843
4844 /* Now that we have processed all the DIEs in the CU, all the types
4845 should be complete, and it should now be safe to compute all of the
4846 physnames. */
4847 compute_delayed_physnames (cu);
4848 do_cleanups (delayed_list_cleanup);
4849
4850 /* Some compilers don't define a DW_AT_high_pc attribute for the
4851 compilation unit. If the DW_AT_high_pc is missing, synthesize
4852 it, by scanning the DIE's below the compilation unit. */
4853 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4854
4855 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4856
4857 if (symtab != NULL)
4858 {
4859 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4860
4861 /* Set symtab language to language from DW_AT_language. If the
4862 compilation is from a C file generated by language preprocessors, do
4863 not set the language if it was already deduced by start_subfile. */
4864 if (!(cu->language == language_c && symtab->language != language_c))
4865 symtab->language = cu->language;
4866
4867 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4868 produce DW_AT_location with location lists but it can be possibly
4869 invalid without -fvar-tracking.
4870
4871 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4872 needed, it would be wrong due to missing DW_AT_producer there.
4873
4874 Still one can confuse GDB by using non-standard GCC compilation
4875 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4876 */
4877 if (cu->has_loclist && gcc_4_minor >= 0)
4878 symtab->locations_valid = 1;
4879
4880 if (gcc_4_minor >= 5)
4881 symtab->epilogue_unwind_valid = 1;
4882
4883 symtab->call_site_htab = cu->call_site_htab;
4884 }
4885
4886 if (dwarf2_per_objfile->using_index)
4887 per_cu->v.quick->symtab = symtab;
4888 else
4889 {
4890 struct partial_symtab *pst = per_cu->v.psymtab;
4891 pst->symtab = symtab;
4892 pst->readin = 1;
4893 }
4894
4895 do_cleanups (back_to);
4896 }
4897
4898 /* Process a die and its children. */
4899
4900 static void
4901 process_die (struct die_info *die, struct dwarf2_cu *cu)
4902 {
4903 switch (die->tag)
4904 {
4905 case DW_TAG_padding:
4906 break;
4907 case DW_TAG_compile_unit:
4908 read_file_scope (die, cu);
4909 break;
4910 case DW_TAG_type_unit:
4911 read_type_unit_scope (die, cu);
4912 break;
4913 case DW_TAG_subprogram:
4914 case DW_TAG_inlined_subroutine:
4915 read_func_scope (die, cu);
4916 break;
4917 case DW_TAG_lexical_block:
4918 case DW_TAG_try_block:
4919 case DW_TAG_catch_block:
4920 read_lexical_block_scope (die, cu);
4921 break;
4922 case DW_TAG_GNU_call_site:
4923 read_call_site_scope (die, cu);
4924 break;
4925 case DW_TAG_class_type:
4926 case DW_TAG_interface_type:
4927 case DW_TAG_structure_type:
4928 case DW_TAG_union_type:
4929 process_structure_scope (die, cu);
4930 break;
4931 case DW_TAG_enumeration_type:
4932 process_enumeration_scope (die, cu);
4933 break;
4934
4935 /* These dies have a type, but processing them does not create
4936 a symbol or recurse to process the children. Therefore we can
4937 read them on-demand through read_type_die. */
4938 case DW_TAG_subroutine_type:
4939 case DW_TAG_set_type:
4940 case DW_TAG_array_type:
4941 case DW_TAG_pointer_type:
4942 case DW_TAG_ptr_to_member_type:
4943 case DW_TAG_reference_type:
4944 case DW_TAG_string_type:
4945 break;
4946
4947 case DW_TAG_base_type:
4948 case DW_TAG_subrange_type:
4949 case DW_TAG_typedef:
4950 /* Add a typedef symbol for the type definition, if it has a
4951 DW_AT_name. */
4952 new_symbol (die, read_type_die (die, cu), cu);
4953 break;
4954 case DW_TAG_common_block:
4955 read_common_block (die, cu);
4956 break;
4957 case DW_TAG_common_inclusion:
4958 break;
4959 case DW_TAG_namespace:
4960 processing_has_namespace_info = 1;
4961 read_namespace (die, cu);
4962 break;
4963 case DW_TAG_module:
4964 processing_has_namespace_info = 1;
4965 read_module (die, cu);
4966 break;
4967 case DW_TAG_imported_declaration:
4968 case DW_TAG_imported_module:
4969 processing_has_namespace_info = 1;
4970 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4971 || cu->language != language_fortran))
4972 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4973 dwarf_tag_name (die->tag));
4974 read_import_statement (die, cu);
4975 break;
4976 default:
4977 new_symbol (die, NULL, cu);
4978 break;
4979 }
4980 }
4981
4982 /* A helper function for dwarf2_compute_name which determines whether DIE
4983 needs to have the name of the scope prepended to the name listed in the
4984 die. */
4985
4986 static int
4987 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4988 {
4989 struct attribute *attr;
4990
4991 switch (die->tag)
4992 {
4993 case DW_TAG_namespace:
4994 case DW_TAG_typedef:
4995 case DW_TAG_class_type:
4996 case DW_TAG_interface_type:
4997 case DW_TAG_structure_type:
4998 case DW_TAG_union_type:
4999 case DW_TAG_enumeration_type:
5000 case DW_TAG_enumerator:
5001 case DW_TAG_subprogram:
5002 case DW_TAG_member:
5003 return 1;
5004
5005 case DW_TAG_variable:
5006 case DW_TAG_constant:
5007 /* We only need to prefix "globally" visible variables. These include
5008 any variable marked with DW_AT_external or any variable that
5009 lives in a namespace. [Variables in anonymous namespaces
5010 require prefixing, but they are not DW_AT_external.] */
5011
5012 if (dwarf2_attr (die, DW_AT_specification, cu))
5013 {
5014 struct dwarf2_cu *spec_cu = cu;
5015
5016 return die_needs_namespace (die_specification (die, &spec_cu),
5017 spec_cu);
5018 }
5019
5020 attr = dwarf2_attr (die, DW_AT_external, cu);
5021 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5022 && die->parent->tag != DW_TAG_module)
5023 return 0;
5024 /* A variable in a lexical block of some kind does not need a
5025 namespace, even though in C++ such variables may be external
5026 and have a mangled name. */
5027 if (die->parent->tag == DW_TAG_lexical_block
5028 || die->parent->tag == DW_TAG_try_block
5029 || die->parent->tag == DW_TAG_catch_block
5030 || die->parent->tag == DW_TAG_subprogram)
5031 return 0;
5032 return 1;
5033
5034 default:
5035 return 0;
5036 }
5037 }
5038
5039 /* Retrieve the last character from a mem_file. */
5040
5041 static void
5042 do_ui_file_peek_last (void *object, const char *buffer, long length)
5043 {
5044 char *last_char_p = (char *) object;
5045
5046 if (length > 0)
5047 *last_char_p = buffer[length - 1];
5048 }
5049
5050 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5051 compute the physname for the object, which include a method's
5052 formal parameters (C++/Java) and return type (Java).
5053
5054 For Ada, return the DIE's linkage name rather than the fully qualified
5055 name. PHYSNAME is ignored..
5056
5057 The result is allocated on the objfile_obstack and canonicalized. */
5058
5059 static const char *
5060 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5061 int physname)
5062 {
5063 struct objfile *objfile = cu->objfile;
5064
5065 if (name == NULL)
5066 name = dwarf2_name (die, cu);
5067
5068 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5069 compute it by typename_concat inside GDB. */
5070 if (cu->language == language_ada
5071 || (cu->language == language_fortran && physname))
5072 {
5073 /* For Ada unit, we prefer the linkage name over the name, as
5074 the former contains the exported name, which the user expects
5075 to be able to reference. Ideally, we want the user to be able
5076 to reference this entity using either natural or linkage name,
5077 but we haven't started looking at this enhancement yet. */
5078 struct attribute *attr;
5079
5080 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5081 if (attr == NULL)
5082 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5083 if (attr && DW_STRING (attr))
5084 return DW_STRING (attr);
5085 }
5086
5087 /* These are the only languages we know how to qualify names in. */
5088 if (name != NULL
5089 && (cu->language == language_cplus || cu->language == language_java
5090 || cu->language == language_fortran))
5091 {
5092 if (die_needs_namespace (die, cu))
5093 {
5094 long length;
5095 char *prefix;
5096 struct ui_file *buf;
5097
5098 prefix = determine_prefix (die, cu);
5099 buf = mem_fileopen ();
5100 if (*prefix != '\0')
5101 {
5102 char *prefixed_name = typename_concat (NULL, prefix, name,
5103 physname, cu);
5104
5105 fputs_unfiltered (prefixed_name, buf);
5106 xfree (prefixed_name);
5107 }
5108 else
5109 fputs_unfiltered (name, buf);
5110
5111 /* Template parameters may be specified in the DIE's DW_AT_name, or
5112 as children with DW_TAG_template_type_param or
5113 DW_TAG_value_type_param. If the latter, add them to the name
5114 here. If the name already has template parameters, then
5115 skip this step; some versions of GCC emit both, and
5116 it is more efficient to use the pre-computed name.
5117
5118 Something to keep in mind about this process: it is very
5119 unlikely, or in some cases downright impossible, to produce
5120 something that will match the mangled name of a function.
5121 If the definition of the function has the same debug info,
5122 we should be able to match up with it anyway. But fallbacks
5123 using the minimal symbol, for instance to find a method
5124 implemented in a stripped copy of libstdc++, will not work.
5125 If we do not have debug info for the definition, we will have to
5126 match them up some other way.
5127
5128 When we do name matching there is a related problem with function
5129 templates; two instantiated function templates are allowed to
5130 differ only by their return types, which we do not add here. */
5131
5132 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5133 {
5134 struct attribute *attr;
5135 struct die_info *child;
5136 int first = 1;
5137
5138 die->building_fullname = 1;
5139
5140 for (child = die->child; child != NULL; child = child->sibling)
5141 {
5142 struct type *type;
5143 long value;
5144 gdb_byte *bytes;
5145 struct dwarf2_locexpr_baton *baton;
5146 struct value *v;
5147
5148 if (child->tag != DW_TAG_template_type_param
5149 && child->tag != DW_TAG_template_value_param)
5150 continue;
5151
5152 if (first)
5153 {
5154 fputs_unfiltered ("<", buf);
5155 first = 0;
5156 }
5157 else
5158 fputs_unfiltered (", ", buf);
5159
5160 attr = dwarf2_attr (child, DW_AT_type, cu);
5161 if (attr == NULL)
5162 {
5163 complaint (&symfile_complaints,
5164 _("template parameter missing DW_AT_type"));
5165 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5166 continue;
5167 }
5168 type = die_type (child, cu);
5169
5170 if (child->tag == DW_TAG_template_type_param)
5171 {
5172 c_print_type (type, "", buf, -1, 0);
5173 continue;
5174 }
5175
5176 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5177 if (attr == NULL)
5178 {
5179 complaint (&symfile_complaints,
5180 _("template parameter missing "
5181 "DW_AT_const_value"));
5182 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5183 continue;
5184 }
5185
5186 dwarf2_const_value_attr (attr, type, name,
5187 &cu->comp_unit_obstack, cu,
5188 &value, &bytes, &baton);
5189
5190 if (TYPE_NOSIGN (type))
5191 /* GDB prints characters as NUMBER 'CHAR'. If that's
5192 changed, this can use value_print instead. */
5193 c_printchar (value, type, buf);
5194 else
5195 {
5196 struct value_print_options opts;
5197
5198 if (baton != NULL)
5199 v = dwarf2_evaluate_loc_desc (type, NULL,
5200 baton->data,
5201 baton->size,
5202 baton->per_cu);
5203 else if (bytes != NULL)
5204 {
5205 v = allocate_value (type);
5206 memcpy (value_contents_writeable (v), bytes,
5207 TYPE_LENGTH (type));
5208 }
5209 else
5210 v = value_from_longest (type, value);
5211
5212 /* Specify decimal so that we do not depend on
5213 the radix. */
5214 get_formatted_print_options (&opts, 'd');
5215 opts.raw = 1;
5216 value_print (v, buf, &opts);
5217 release_value (v);
5218 value_free (v);
5219 }
5220 }
5221
5222 die->building_fullname = 0;
5223
5224 if (!first)
5225 {
5226 /* Close the argument list, with a space if necessary
5227 (nested templates). */
5228 char last_char = '\0';
5229 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5230 if (last_char == '>')
5231 fputs_unfiltered (" >", buf);
5232 else
5233 fputs_unfiltered (">", buf);
5234 }
5235 }
5236
5237 /* For Java and C++ methods, append formal parameter type
5238 information, if PHYSNAME. */
5239
5240 if (physname && die->tag == DW_TAG_subprogram
5241 && (cu->language == language_cplus
5242 || cu->language == language_java))
5243 {
5244 struct type *type = read_type_die (die, cu);
5245
5246 c_type_print_args (type, buf, 1, cu->language);
5247
5248 if (cu->language == language_java)
5249 {
5250 /* For java, we must append the return type to method
5251 names. */
5252 if (die->tag == DW_TAG_subprogram)
5253 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5254 0, 0);
5255 }
5256 else if (cu->language == language_cplus)
5257 {
5258 /* Assume that an artificial first parameter is
5259 "this", but do not crash if it is not. RealView
5260 marks unnamed (and thus unused) parameters as
5261 artificial; there is no way to differentiate
5262 the two cases. */
5263 if (TYPE_NFIELDS (type) > 0
5264 && TYPE_FIELD_ARTIFICIAL (type, 0)
5265 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5266 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5267 0))))
5268 fputs_unfiltered (" const", buf);
5269 }
5270 }
5271
5272 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5273 &length);
5274 ui_file_delete (buf);
5275
5276 if (cu->language == language_cplus)
5277 {
5278 char *cname
5279 = dwarf2_canonicalize_name (name, cu,
5280 &objfile->objfile_obstack);
5281
5282 if (cname != NULL)
5283 name = cname;
5284 }
5285 }
5286 }
5287
5288 return name;
5289 }
5290
5291 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5292 If scope qualifiers are appropriate they will be added. The result
5293 will be allocated on the objfile_obstack, or NULL if the DIE does
5294 not have a name. NAME may either be from a previous call to
5295 dwarf2_name or NULL.
5296
5297 The output string will be canonicalized (if C++/Java). */
5298
5299 static const char *
5300 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5301 {
5302 return dwarf2_compute_name (name, die, cu, 0);
5303 }
5304
5305 /* Construct a physname for the given DIE in CU. NAME may either be
5306 from a previous call to dwarf2_name or NULL. The result will be
5307 allocated on the objfile_objstack or NULL if the DIE does not have a
5308 name.
5309
5310 The output string will be canonicalized (if C++/Java). */
5311
5312 static const char *
5313 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5314 {
5315 struct objfile *objfile = cu->objfile;
5316 struct attribute *attr;
5317 const char *retval, *mangled = NULL, *canon = NULL;
5318 struct cleanup *back_to;
5319 int need_copy = 1;
5320
5321 /* In this case dwarf2_compute_name is just a shortcut not building anything
5322 on its own. */
5323 if (!die_needs_namespace (die, cu))
5324 return dwarf2_compute_name (name, die, cu, 1);
5325
5326 back_to = make_cleanup (null_cleanup, NULL);
5327
5328 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5329 if (!attr)
5330 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5331
5332 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5333 has computed. */
5334 if (attr && DW_STRING (attr))
5335 {
5336 char *demangled;
5337
5338 mangled = DW_STRING (attr);
5339
5340 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5341 type. It is easier for GDB users to search for such functions as
5342 `name(params)' than `long name(params)'. In such case the minimal
5343 symbol names do not match the full symbol names but for template
5344 functions there is never a need to look up their definition from their
5345 declaration so the only disadvantage remains the minimal symbol
5346 variant `long name(params)' does not have the proper inferior type.
5347 */
5348
5349 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5350 | (cu->language == language_java
5351 ? DMGL_JAVA | DMGL_RET_POSTFIX
5352 : DMGL_RET_DROP)));
5353 if (demangled)
5354 {
5355 make_cleanup (xfree, demangled);
5356 canon = demangled;
5357 }
5358 else
5359 {
5360 canon = mangled;
5361 need_copy = 0;
5362 }
5363 }
5364
5365 if (canon == NULL || check_physname)
5366 {
5367 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5368
5369 if (canon != NULL && strcmp (physname, canon) != 0)
5370 {
5371 /* It may not mean a bug in GDB. The compiler could also
5372 compute DW_AT_linkage_name incorrectly. But in such case
5373 GDB would need to be bug-to-bug compatible. */
5374
5375 complaint (&symfile_complaints,
5376 _("Computed physname <%s> does not match demangled <%s> "
5377 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5378 physname, canon, mangled, die->offset, objfile->name);
5379
5380 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5381 is available here - over computed PHYSNAME. It is safer
5382 against both buggy GDB and buggy compilers. */
5383
5384 retval = canon;
5385 }
5386 else
5387 {
5388 retval = physname;
5389 need_copy = 0;
5390 }
5391 }
5392 else
5393 retval = canon;
5394
5395 if (need_copy)
5396 retval = obsavestring (retval, strlen (retval),
5397 &objfile->objfile_obstack);
5398
5399 do_cleanups (back_to);
5400 return retval;
5401 }
5402
5403 /* Read the import statement specified by the given die and record it. */
5404
5405 static void
5406 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5407 {
5408 struct objfile *objfile = cu->objfile;
5409 struct attribute *import_attr;
5410 struct die_info *imported_die, *child_die;
5411 struct dwarf2_cu *imported_cu;
5412 const char *imported_name;
5413 const char *imported_name_prefix;
5414 const char *canonical_name;
5415 const char *import_alias;
5416 const char *imported_declaration = NULL;
5417 const char *import_prefix;
5418 VEC (const_char_ptr) *excludes = NULL;
5419 struct cleanup *cleanups;
5420
5421 char *temp;
5422
5423 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5424 if (import_attr == NULL)
5425 {
5426 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5427 dwarf_tag_name (die->tag));
5428 return;
5429 }
5430
5431 imported_cu = cu;
5432 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5433 imported_name = dwarf2_name (imported_die, imported_cu);
5434 if (imported_name == NULL)
5435 {
5436 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5437
5438 The import in the following code:
5439 namespace A
5440 {
5441 typedef int B;
5442 }
5443
5444 int main ()
5445 {
5446 using A::B;
5447 B b;
5448 return b;
5449 }
5450
5451 ...
5452 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5453 <52> DW_AT_decl_file : 1
5454 <53> DW_AT_decl_line : 6
5455 <54> DW_AT_import : <0x75>
5456 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5457 <59> DW_AT_name : B
5458 <5b> DW_AT_decl_file : 1
5459 <5c> DW_AT_decl_line : 2
5460 <5d> DW_AT_type : <0x6e>
5461 ...
5462 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5463 <76> DW_AT_byte_size : 4
5464 <77> DW_AT_encoding : 5 (signed)
5465
5466 imports the wrong die ( 0x75 instead of 0x58 ).
5467 This case will be ignored until the gcc bug is fixed. */
5468 return;
5469 }
5470
5471 /* Figure out the local name after import. */
5472 import_alias = dwarf2_name (die, cu);
5473
5474 /* Figure out where the statement is being imported to. */
5475 import_prefix = determine_prefix (die, cu);
5476
5477 /* Figure out what the scope of the imported die is and prepend it
5478 to the name of the imported die. */
5479 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5480
5481 if (imported_die->tag != DW_TAG_namespace
5482 && imported_die->tag != DW_TAG_module)
5483 {
5484 imported_declaration = imported_name;
5485 canonical_name = imported_name_prefix;
5486 }
5487 else if (strlen (imported_name_prefix) > 0)
5488 {
5489 temp = alloca (strlen (imported_name_prefix)
5490 + 2 + strlen (imported_name) + 1);
5491 strcpy (temp, imported_name_prefix);
5492 strcat (temp, "::");
5493 strcat (temp, imported_name);
5494 canonical_name = temp;
5495 }
5496 else
5497 canonical_name = imported_name;
5498
5499 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5500
5501 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5502 for (child_die = die->child; child_die && child_die->tag;
5503 child_die = sibling_die (child_die))
5504 {
5505 /* DWARF-4: A Fortran use statement with a “rename list” may be
5506 represented by an imported module entry with an import attribute
5507 referring to the module and owned entries corresponding to those
5508 entities that are renamed as part of being imported. */
5509
5510 if (child_die->tag != DW_TAG_imported_declaration)
5511 {
5512 complaint (&symfile_complaints,
5513 _("child DW_TAG_imported_declaration expected "
5514 "- DIE at 0x%x [in module %s]"),
5515 child_die->offset, objfile->name);
5516 continue;
5517 }
5518
5519 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5520 if (import_attr == NULL)
5521 {
5522 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5523 dwarf_tag_name (child_die->tag));
5524 continue;
5525 }
5526
5527 imported_cu = cu;
5528 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5529 &imported_cu);
5530 imported_name = dwarf2_name (imported_die, imported_cu);
5531 if (imported_name == NULL)
5532 {
5533 complaint (&symfile_complaints,
5534 _("child DW_TAG_imported_declaration has unknown "
5535 "imported name - DIE at 0x%x [in module %s]"),
5536 child_die->offset, objfile->name);
5537 continue;
5538 }
5539
5540 VEC_safe_push (const_char_ptr, excludes, imported_name);
5541
5542 process_die (child_die, cu);
5543 }
5544
5545 cp_add_using_directive (import_prefix,
5546 canonical_name,
5547 import_alias,
5548 imported_declaration,
5549 excludes,
5550 &objfile->objfile_obstack);
5551
5552 do_cleanups (cleanups);
5553 }
5554
5555 /* Cleanup function for read_file_scope. */
5556
5557 static void
5558 free_cu_line_header (void *arg)
5559 {
5560 struct dwarf2_cu *cu = arg;
5561
5562 free_line_header (cu->line_header);
5563 cu->line_header = NULL;
5564 }
5565
5566 static void
5567 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5568 char **name, char **comp_dir)
5569 {
5570 struct attribute *attr;
5571
5572 *name = NULL;
5573 *comp_dir = NULL;
5574
5575 /* Find the filename. Do not use dwarf2_name here, since the filename
5576 is not a source language identifier. */
5577 attr = dwarf2_attr (die, DW_AT_name, cu);
5578 if (attr)
5579 {
5580 *name = DW_STRING (attr);
5581 }
5582
5583 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5584 if (attr)
5585 *comp_dir = DW_STRING (attr);
5586 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5587 {
5588 *comp_dir = ldirname (*name);
5589 if (*comp_dir != NULL)
5590 make_cleanup (xfree, *comp_dir);
5591 }
5592 if (*comp_dir != NULL)
5593 {
5594 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5595 directory, get rid of it. */
5596 char *cp = strchr (*comp_dir, ':');
5597
5598 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5599 *comp_dir = cp + 1;
5600 }
5601
5602 if (*name == NULL)
5603 *name = "<unknown>";
5604 }
5605
5606 /* Handle DW_AT_stmt_list for a compilation unit. */
5607
5608 static void
5609 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5610 const char *comp_dir)
5611 {
5612 struct attribute *attr;
5613 struct objfile *objfile = cu->objfile;
5614 bfd *abfd = objfile->obfd;
5615
5616 /* Decode line number information if present. We do this before
5617 processing child DIEs, so that the line header table is available
5618 for DW_AT_decl_file. */
5619 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5620 if (attr)
5621 {
5622 unsigned int line_offset = DW_UNSND (attr);
5623 struct line_header *line_header
5624 = dwarf_decode_line_header (line_offset, abfd, cu);
5625
5626 if (line_header)
5627 {
5628 cu->line_header = line_header;
5629 make_cleanup (free_cu_line_header, cu);
5630 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5631 }
5632 }
5633 }
5634
5635 /* Process DW_TAG_compile_unit. */
5636
5637 static void
5638 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5639 {
5640 struct objfile *objfile = cu->objfile;
5641 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5642 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5643 CORE_ADDR highpc = ((CORE_ADDR) 0);
5644 struct attribute *attr;
5645 char *name = NULL;
5646 char *comp_dir = NULL;
5647 struct die_info *child_die;
5648 bfd *abfd = objfile->obfd;
5649 CORE_ADDR baseaddr;
5650
5651 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5652
5653 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5654
5655 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5656 from finish_block. */
5657 if (lowpc == ((CORE_ADDR) -1))
5658 lowpc = highpc;
5659 lowpc += baseaddr;
5660 highpc += baseaddr;
5661
5662 find_file_and_directory (die, cu, &name, &comp_dir);
5663
5664 attr = dwarf2_attr (die, DW_AT_language, cu);
5665 if (attr)
5666 {
5667 set_cu_language (DW_UNSND (attr), cu);
5668 }
5669
5670 attr = dwarf2_attr (die, DW_AT_producer, cu);
5671 if (attr)
5672 cu->producer = DW_STRING (attr);
5673
5674 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5675 standardised yet. As a workaround for the language detection we fall
5676 back to the DW_AT_producer string. */
5677 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5678 cu->language = language_opencl;
5679
5680 /* We assume that we're processing GCC output. */
5681 processing_gcc_compilation = 2;
5682
5683 processing_has_namespace_info = 0;
5684
5685 start_symtab (name, comp_dir, lowpc);
5686 record_debugformat ("DWARF 2");
5687 record_producer (cu->producer);
5688
5689 handle_DW_AT_stmt_list (die, cu, comp_dir);
5690
5691 /* Process all dies in compilation unit. */
5692 if (die->child != NULL)
5693 {
5694 child_die = die->child;
5695 while (child_die && child_die->tag)
5696 {
5697 process_die (child_die, cu);
5698 child_die = sibling_die (child_die);
5699 }
5700 }
5701
5702 /* Decode macro information, if present. Dwarf 2 macro information
5703 refers to information in the line number info statement program
5704 header, so we can only read it if we've read the header
5705 successfully. */
5706 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5707 if (attr && cu->line_header)
5708 {
5709 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5710 complaint (&symfile_complaints,
5711 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5712
5713 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5714 comp_dir, abfd, cu,
5715 &dwarf2_per_objfile->macro, 1);
5716 }
5717 else
5718 {
5719 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5720 if (attr && cu->line_header)
5721 {
5722 unsigned int macro_offset = DW_UNSND (attr);
5723
5724 dwarf_decode_macros (cu->line_header, macro_offset,
5725 comp_dir, abfd, cu,
5726 &dwarf2_per_objfile->macinfo, 0);
5727 }
5728 }
5729
5730 do_cleanups (back_to);
5731 }
5732
5733 /* Process DW_TAG_type_unit.
5734 For TUs we want to skip the first top level sibling if it's not the
5735 actual type being defined by this TU. In this case the first top
5736 level sibling is there to provide context only. */
5737
5738 static void
5739 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5740 {
5741 struct objfile *objfile = cu->objfile;
5742 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5743 CORE_ADDR lowpc;
5744 struct attribute *attr;
5745 char *name = NULL;
5746 char *comp_dir = NULL;
5747 struct die_info *child_die;
5748 bfd *abfd = objfile->obfd;
5749
5750 /* start_symtab needs a low pc, but we don't really have one.
5751 Do what read_file_scope would do in the absence of such info. */
5752 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5753
5754 /* Find the filename. Do not use dwarf2_name here, since the filename
5755 is not a source language identifier. */
5756 attr = dwarf2_attr (die, DW_AT_name, cu);
5757 if (attr)
5758 name = DW_STRING (attr);
5759
5760 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5761 if (attr)
5762 comp_dir = DW_STRING (attr);
5763 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5764 {
5765 comp_dir = ldirname (name);
5766 if (comp_dir != NULL)
5767 make_cleanup (xfree, comp_dir);
5768 }
5769
5770 if (name == NULL)
5771 name = "<unknown>";
5772
5773 attr = dwarf2_attr (die, DW_AT_language, cu);
5774 if (attr)
5775 set_cu_language (DW_UNSND (attr), cu);
5776
5777 /* This isn't technically needed today. It is done for symmetry
5778 with read_file_scope. */
5779 attr = dwarf2_attr (die, DW_AT_producer, cu);
5780 if (attr)
5781 cu->producer = DW_STRING (attr);
5782
5783 /* We assume that we're processing GCC output. */
5784 processing_gcc_compilation = 2;
5785
5786 processing_has_namespace_info = 0;
5787
5788 start_symtab (name, comp_dir, lowpc);
5789 record_debugformat ("DWARF 2");
5790 record_producer (cu->producer);
5791
5792 handle_DW_AT_stmt_list (die, cu, comp_dir);
5793
5794 /* Process the dies in the type unit. */
5795 if (die->child == NULL)
5796 {
5797 dump_die_for_error (die);
5798 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5799 bfd_get_filename (abfd));
5800 }
5801
5802 child_die = die->child;
5803
5804 while (child_die && child_die->tag)
5805 {
5806 process_die (child_die, cu);
5807
5808 child_die = sibling_die (child_die);
5809 }
5810
5811 do_cleanups (back_to);
5812 }
5813
5814 /* qsort helper for inherit_abstract_dies. */
5815
5816 static int
5817 unsigned_int_compar (const void *ap, const void *bp)
5818 {
5819 unsigned int a = *(unsigned int *) ap;
5820 unsigned int b = *(unsigned int *) bp;
5821
5822 return (a > b) - (b > a);
5823 }
5824
5825 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5826 Inherit only the children of the DW_AT_abstract_origin DIE not being
5827 already referenced by DW_AT_abstract_origin from the children of the
5828 current DIE. */
5829
5830 static void
5831 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5832 {
5833 struct die_info *child_die;
5834 unsigned die_children_count;
5835 /* CU offsets which were referenced by children of the current DIE. */
5836 unsigned *offsets;
5837 unsigned *offsets_end, *offsetp;
5838 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5839 struct die_info *origin_die;
5840 /* Iterator of the ORIGIN_DIE children. */
5841 struct die_info *origin_child_die;
5842 struct cleanup *cleanups;
5843 struct attribute *attr;
5844 struct dwarf2_cu *origin_cu;
5845 struct pending **origin_previous_list_in_scope;
5846
5847 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5848 if (!attr)
5849 return;
5850
5851 /* Note that following die references may follow to a die in a
5852 different cu. */
5853
5854 origin_cu = cu;
5855 origin_die = follow_die_ref (die, attr, &origin_cu);
5856
5857 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5858 symbols in. */
5859 origin_previous_list_in_scope = origin_cu->list_in_scope;
5860 origin_cu->list_in_scope = cu->list_in_scope;
5861
5862 if (die->tag != origin_die->tag
5863 && !(die->tag == DW_TAG_inlined_subroutine
5864 && origin_die->tag == DW_TAG_subprogram))
5865 complaint (&symfile_complaints,
5866 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5867 die->offset, origin_die->offset);
5868
5869 child_die = die->child;
5870 die_children_count = 0;
5871 while (child_die && child_die->tag)
5872 {
5873 child_die = sibling_die (child_die);
5874 die_children_count++;
5875 }
5876 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5877 cleanups = make_cleanup (xfree, offsets);
5878
5879 offsets_end = offsets;
5880 child_die = die->child;
5881 while (child_die && child_die->tag)
5882 {
5883 /* For each CHILD_DIE, find the corresponding child of
5884 ORIGIN_DIE. If there is more than one layer of
5885 DW_AT_abstract_origin, follow them all; there shouldn't be,
5886 but GCC versions at least through 4.4 generate this (GCC PR
5887 40573). */
5888 struct die_info *child_origin_die = child_die;
5889 struct dwarf2_cu *child_origin_cu = cu;
5890
5891 while (1)
5892 {
5893 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5894 child_origin_cu);
5895 if (attr == NULL)
5896 break;
5897 child_origin_die = follow_die_ref (child_origin_die, attr,
5898 &child_origin_cu);
5899 }
5900
5901 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5902 counterpart may exist. */
5903 if (child_origin_die != child_die)
5904 {
5905 if (child_die->tag != child_origin_die->tag
5906 && !(child_die->tag == DW_TAG_inlined_subroutine
5907 && child_origin_die->tag == DW_TAG_subprogram))
5908 complaint (&symfile_complaints,
5909 _("Child DIE 0x%x and its abstract origin 0x%x have "
5910 "different tags"), child_die->offset,
5911 child_origin_die->offset);
5912 if (child_origin_die->parent != origin_die)
5913 complaint (&symfile_complaints,
5914 _("Child DIE 0x%x and its abstract origin 0x%x have "
5915 "different parents"), child_die->offset,
5916 child_origin_die->offset);
5917 else
5918 *offsets_end++ = child_origin_die->offset;
5919 }
5920 child_die = sibling_die (child_die);
5921 }
5922 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5923 unsigned_int_compar);
5924 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5925 if (offsetp[-1] == *offsetp)
5926 complaint (&symfile_complaints,
5927 _("Multiple children of DIE 0x%x refer "
5928 "to DIE 0x%x as their abstract origin"),
5929 die->offset, *offsetp);
5930
5931 offsetp = offsets;
5932 origin_child_die = origin_die->child;
5933 while (origin_child_die && origin_child_die->tag)
5934 {
5935 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5936 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5937 offsetp++;
5938 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5939 {
5940 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5941 process_die (origin_child_die, origin_cu);
5942 }
5943 origin_child_die = sibling_die (origin_child_die);
5944 }
5945 origin_cu->list_in_scope = origin_previous_list_in_scope;
5946
5947 do_cleanups (cleanups);
5948 }
5949
5950 static void
5951 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5952 {
5953 struct objfile *objfile = cu->objfile;
5954 struct context_stack *new;
5955 CORE_ADDR lowpc;
5956 CORE_ADDR highpc;
5957 struct die_info *child_die;
5958 struct attribute *attr, *call_line, *call_file;
5959 char *name;
5960 CORE_ADDR baseaddr;
5961 struct block *block;
5962 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5963 VEC (symbolp) *template_args = NULL;
5964 struct template_symbol *templ_func = NULL;
5965
5966 if (inlined_func)
5967 {
5968 /* If we do not have call site information, we can't show the
5969 caller of this inlined function. That's too confusing, so
5970 only use the scope for local variables. */
5971 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5972 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5973 if (call_line == NULL || call_file == NULL)
5974 {
5975 read_lexical_block_scope (die, cu);
5976 return;
5977 }
5978 }
5979
5980 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5981
5982 name = dwarf2_name (die, cu);
5983
5984 /* Ignore functions with missing or empty names. These are actually
5985 illegal according to the DWARF standard. */
5986 if (name == NULL)
5987 {
5988 complaint (&symfile_complaints,
5989 _("missing name for subprogram DIE at %d"), die->offset);
5990 return;
5991 }
5992
5993 /* Ignore functions with missing or invalid low and high pc attributes. */
5994 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5995 {
5996 attr = dwarf2_attr (die, DW_AT_external, cu);
5997 if (!attr || !DW_UNSND (attr))
5998 complaint (&symfile_complaints,
5999 _("cannot get low and high bounds "
6000 "for subprogram DIE at %d"),
6001 die->offset);
6002 return;
6003 }
6004
6005 lowpc += baseaddr;
6006 highpc += baseaddr;
6007
6008 /* If we have any template arguments, then we must allocate a
6009 different sort of symbol. */
6010 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6011 {
6012 if (child_die->tag == DW_TAG_template_type_param
6013 || child_die->tag == DW_TAG_template_value_param)
6014 {
6015 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6016 struct template_symbol);
6017 templ_func->base.is_cplus_template_function = 1;
6018 break;
6019 }
6020 }
6021
6022 new = push_context (0, lowpc);
6023 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6024 (struct symbol *) templ_func);
6025
6026 /* If there is a location expression for DW_AT_frame_base, record
6027 it. */
6028 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6029 if (attr)
6030 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6031 expression is being recorded directly in the function's symbol
6032 and not in a separate frame-base object. I guess this hack is
6033 to avoid adding some sort of frame-base adjunct/annex to the
6034 function's symbol :-(. The problem with doing this is that it
6035 results in a function symbol with a location expression that
6036 has nothing to do with the location of the function, ouch! The
6037 relationship should be: a function's symbol has-a frame base; a
6038 frame-base has-a location expression. */
6039 dwarf2_symbol_mark_computed (attr, new->name, cu);
6040
6041 cu->list_in_scope = &local_symbols;
6042
6043 if (die->child != NULL)
6044 {
6045 child_die = die->child;
6046 while (child_die && child_die->tag)
6047 {
6048 if (child_die->tag == DW_TAG_template_type_param
6049 || child_die->tag == DW_TAG_template_value_param)
6050 {
6051 struct symbol *arg = new_symbol (child_die, NULL, cu);
6052
6053 if (arg != NULL)
6054 VEC_safe_push (symbolp, template_args, arg);
6055 }
6056 else
6057 process_die (child_die, cu);
6058 child_die = sibling_die (child_die);
6059 }
6060 }
6061
6062 inherit_abstract_dies (die, cu);
6063
6064 /* If we have a DW_AT_specification, we might need to import using
6065 directives from the context of the specification DIE. See the
6066 comment in determine_prefix. */
6067 if (cu->language == language_cplus
6068 && dwarf2_attr (die, DW_AT_specification, cu))
6069 {
6070 struct dwarf2_cu *spec_cu = cu;
6071 struct die_info *spec_die = die_specification (die, &spec_cu);
6072
6073 while (spec_die)
6074 {
6075 child_die = spec_die->child;
6076 while (child_die && child_die->tag)
6077 {
6078 if (child_die->tag == DW_TAG_imported_module)
6079 process_die (child_die, spec_cu);
6080 child_die = sibling_die (child_die);
6081 }
6082
6083 /* In some cases, GCC generates specification DIEs that
6084 themselves contain DW_AT_specification attributes. */
6085 spec_die = die_specification (spec_die, &spec_cu);
6086 }
6087 }
6088
6089 new = pop_context ();
6090 /* Make a block for the local symbols within. */
6091 block = finish_block (new->name, &local_symbols, new->old_blocks,
6092 lowpc, highpc, objfile);
6093
6094 /* For C++, set the block's scope. */
6095 if (cu->language == language_cplus || cu->language == language_fortran)
6096 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6097 determine_prefix (die, cu),
6098 processing_has_namespace_info);
6099
6100 /* If we have address ranges, record them. */
6101 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6102
6103 /* Attach template arguments to function. */
6104 if (! VEC_empty (symbolp, template_args))
6105 {
6106 gdb_assert (templ_func != NULL);
6107
6108 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6109 templ_func->template_arguments
6110 = obstack_alloc (&objfile->objfile_obstack,
6111 (templ_func->n_template_arguments
6112 * sizeof (struct symbol *)));
6113 memcpy (templ_func->template_arguments,
6114 VEC_address (symbolp, template_args),
6115 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6116 VEC_free (symbolp, template_args);
6117 }
6118
6119 /* In C++, we can have functions nested inside functions (e.g., when
6120 a function declares a class that has methods). This means that
6121 when we finish processing a function scope, we may need to go
6122 back to building a containing block's symbol lists. */
6123 local_symbols = new->locals;
6124 param_symbols = new->params;
6125 using_directives = new->using_directives;
6126
6127 /* If we've finished processing a top-level function, subsequent
6128 symbols go in the file symbol list. */
6129 if (outermost_context_p ())
6130 cu->list_in_scope = &file_symbols;
6131 }
6132
6133 /* Process all the DIES contained within a lexical block scope. Start
6134 a new scope, process the dies, and then close the scope. */
6135
6136 static void
6137 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6138 {
6139 struct objfile *objfile = cu->objfile;
6140 struct context_stack *new;
6141 CORE_ADDR lowpc, highpc;
6142 struct die_info *child_die;
6143 CORE_ADDR baseaddr;
6144
6145 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6146
6147 /* Ignore blocks with missing or invalid low and high pc attributes. */
6148 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6149 as multiple lexical blocks? Handling children in a sane way would
6150 be nasty. Might be easier to properly extend generic blocks to
6151 describe ranges. */
6152 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6153 return;
6154 lowpc += baseaddr;
6155 highpc += baseaddr;
6156
6157 push_context (0, lowpc);
6158 if (die->child != NULL)
6159 {
6160 child_die = die->child;
6161 while (child_die && child_die->tag)
6162 {
6163 process_die (child_die, cu);
6164 child_die = sibling_die (child_die);
6165 }
6166 }
6167 new = pop_context ();
6168
6169 if (local_symbols != NULL || using_directives != NULL)
6170 {
6171 struct block *block
6172 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6173 highpc, objfile);
6174
6175 /* Note that recording ranges after traversing children, as we
6176 do here, means that recording a parent's ranges entails
6177 walking across all its children's ranges as they appear in
6178 the address map, which is quadratic behavior.
6179
6180 It would be nicer to record the parent's ranges before
6181 traversing its children, simply overriding whatever you find
6182 there. But since we don't even decide whether to create a
6183 block until after we've traversed its children, that's hard
6184 to do. */
6185 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6186 }
6187 local_symbols = new->locals;
6188 using_directives = new->using_directives;
6189 }
6190
6191 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6192
6193 static void
6194 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6195 {
6196 struct objfile *objfile = cu->objfile;
6197 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6198 CORE_ADDR pc, baseaddr;
6199 struct attribute *attr;
6200 struct call_site *call_site, call_site_local;
6201 void **slot;
6202 int nparams;
6203 struct die_info *child_die;
6204
6205 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6206
6207 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6208 if (!attr)
6209 {
6210 complaint (&symfile_complaints,
6211 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6212 "DIE 0x%x [in module %s]"),
6213 die->offset, objfile->name);
6214 return;
6215 }
6216 pc = DW_ADDR (attr) + baseaddr;
6217
6218 if (cu->call_site_htab == NULL)
6219 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6220 NULL, &objfile->objfile_obstack,
6221 hashtab_obstack_allocate, NULL);
6222 call_site_local.pc = pc;
6223 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6224 if (*slot != NULL)
6225 {
6226 complaint (&symfile_complaints,
6227 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6228 "DIE 0x%x [in module %s]"),
6229 paddress (gdbarch, pc), die->offset, objfile->name);
6230 return;
6231 }
6232
6233 /* Count parameters at the caller. */
6234
6235 nparams = 0;
6236 for (child_die = die->child; child_die && child_die->tag;
6237 child_die = sibling_die (child_die))
6238 {
6239 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6240 {
6241 complaint (&symfile_complaints,
6242 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6243 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6244 child_die->tag, child_die->offset, objfile->name);
6245 continue;
6246 }
6247
6248 nparams++;
6249 }
6250
6251 call_site = obstack_alloc (&objfile->objfile_obstack,
6252 (sizeof (*call_site)
6253 + (sizeof (*call_site->parameter)
6254 * (nparams - 1))));
6255 *slot = call_site;
6256 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6257 call_site->pc = pc;
6258
6259 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6260 {
6261 struct die_info *func_die;
6262
6263 /* Skip also over DW_TAG_inlined_subroutine. */
6264 for (func_die = die->parent;
6265 func_die && func_die->tag != DW_TAG_subprogram
6266 && func_die->tag != DW_TAG_subroutine_type;
6267 func_die = func_die->parent);
6268
6269 /* DW_AT_GNU_all_call_sites is a superset
6270 of DW_AT_GNU_all_tail_call_sites. */
6271 if (func_die
6272 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6273 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6274 {
6275 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6276 not complete. But keep CALL_SITE for look ups via call_site_htab,
6277 both the initial caller containing the real return address PC and
6278 the final callee containing the current PC of a chain of tail
6279 calls do not need to have the tail call list complete. But any
6280 function candidate for a virtual tail call frame searched via
6281 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6282 determined unambiguously. */
6283 }
6284 else
6285 {
6286 struct type *func_type = NULL;
6287
6288 if (func_die)
6289 func_type = get_die_type (func_die, cu);
6290 if (func_type != NULL)
6291 {
6292 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6293
6294 /* Enlist this call site to the function. */
6295 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6296 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6297 }
6298 else
6299 complaint (&symfile_complaints,
6300 _("Cannot find function owning DW_TAG_GNU_call_site "
6301 "DIE 0x%x [in module %s]"),
6302 die->offset, objfile->name);
6303 }
6304 }
6305
6306 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6307 if (attr == NULL)
6308 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6309 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6310 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6311 /* Keep NULL DWARF_BLOCK. */;
6312 else if (attr_form_is_block (attr))
6313 {
6314 struct dwarf2_locexpr_baton *dlbaton;
6315
6316 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6317 dlbaton->data = DW_BLOCK (attr)->data;
6318 dlbaton->size = DW_BLOCK (attr)->size;
6319 dlbaton->per_cu = cu->per_cu;
6320
6321 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6322 }
6323 else if (is_ref_attr (attr))
6324 {
6325 struct dwarf2_cu *target_cu = cu;
6326 struct die_info *target_die;
6327
6328 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6329 gdb_assert (target_cu->objfile == objfile);
6330 if (die_is_declaration (target_die, target_cu))
6331 {
6332 const char *target_physname;
6333
6334 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6335 if (target_physname == NULL)
6336 complaint (&symfile_complaints,
6337 _("DW_AT_GNU_call_site_target target DIE has invalid "
6338 "physname, for referencing DIE 0x%x [in module %s]"),
6339 die->offset, objfile->name);
6340 else
6341 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6342 }
6343 else
6344 {
6345 CORE_ADDR lowpc;
6346
6347 /* DW_AT_entry_pc should be preferred. */
6348 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6349 complaint (&symfile_complaints,
6350 _("DW_AT_GNU_call_site_target target DIE has invalid "
6351 "low pc, for referencing DIE 0x%x [in module %s]"),
6352 die->offset, objfile->name);
6353 else
6354 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6355 }
6356 }
6357 else
6358 complaint (&symfile_complaints,
6359 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6360 "block nor reference, for DIE 0x%x [in module %s]"),
6361 die->offset, objfile->name);
6362
6363 call_site->per_cu = cu->per_cu;
6364
6365 for (child_die = die->child;
6366 child_die && child_die->tag;
6367 child_die = sibling_die (child_die))
6368 {
6369 struct dwarf2_locexpr_baton *dlbaton;
6370 struct call_site_parameter *parameter;
6371
6372 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6373 {
6374 /* Already printed the complaint above. */
6375 continue;
6376 }
6377
6378 gdb_assert (call_site->parameter_count < nparams);
6379 parameter = &call_site->parameter[call_site->parameter_count];
6380
6381 /* DW_AT_location specifies the register number. Value of the data
6382 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6383
6384 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6385 if (!attr || !attr_form_is_block (attr))
6386 {
6387 complaint (&symfile_complaints,
6388 _("No DW_FORM_block* DW_AT_location for "
6389 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6390 child_die->offset, objfile->name);
6391 continue;
6392 }
6393 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6394 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6395 if (parameter->dwarf_reg == -1
6396 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6397 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6398 &parameter->fb_offset))
6399 {
6400 complaint (&symfile_complaints,
6401 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6402 "for DW_FORM_block* DW_AT_location for "
6403 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6404 child_die->offset, objfile->name);
6405 continue;
6406 }
6407
6408 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6409 if (!attr_form_is_block (attr))
6410 {
6411 complaint (&symfile_complaints,
6412 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6413 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6414 child_die->offset, objfile->name);
6415 continue;
6416 }
6417 parameter->value = DW_BLOCK (attr)->data;
6418 parameter->value_size = DW_BLOCK (attr)->size;
6419
6420 /* Parameters are not pre-cleared by memset above. */
6421 parameter->data_value = NULL;
6422 parameter->data_value_size = 0;
6423 call_site->parameter_count++;
6424
6425 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6426 if (attr)
6427 {
6428 if (!attr_form_is_block (attr))
6429 complaint (&symfile_complaints,
6430 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6431 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6432 child_die->offset, objfile->name);
6433 else
6434 {
6435 parameter->data_value = DW_BLOCK (attr)->data;
6436 parameter->data_value_size = DW_BLOCK (attr)->size;
6437 }
6438 }
6439 }
6440 }
6441
6442 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6443 Return 1 if the attributes are present and valid, otherwise, return 0.
6444 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6445
6446 static int
6447 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6448 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6449 struct partial_symtab *ranges_pst)
6450 {
6451 struct objfile *objfile = cu->objfile;
6452 struct comp_unit_head *cu_header = &cu->header;
6453 bfd *obfd = objfile->obfd;
6454 unsigned int addr_size = cu_header->addr_size;
6455 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6456 /* Base address selection entry. */
6457 CORE_ADDR base;
6458 int found_base;
6459 unsigned int dummy;
6460 gdb_byte *buffer;
6461 CORE_ADDR marker;
6462 int low_set;
6463 CORE_ADDR low = 0;
6464 CORE_ADDR high = 0;
6465 CORE_ADDR baseaddr;
6466
6467 found_base = cu->base_known;
6468 base = cu->base_address;
6469
6470 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6471 if (offset >= dwarf2_per_objfile->ranges.size)
6472 {
6473 complaint (&symfile_complaints,
6474 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6475 offset);
6476 return 0;
6477 }
6478 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6479
6480 /* Read in the largest possible address. */
6481 marker = read_address (obfd, buffer, cu, &dummy);
6482 if ((marker & mask) == mask)
6483 {
6484 /* If we found the largest possible address, then
6485 read the base address. */
6486 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6487 buffer += 2 * addr_size;
6488 offset += 2 * addr_size;
6489 found_base = 1;
6490 }
6491
6492 low_set = 0;
6493
6494 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6495
6496 while (1)
6497 {
6498 CORE_ADDR range_beginning, range_end;
6499
6500 range_beginning = read_address (obfd, buffer, cu, &dummy);
6501 buffer += addr_size;
6502 range_end = read_address (obfd, buffer, cu, &dummy);
6503 buffer += addr_size;
6504 offset += 2 * addr_size;
6505
6506 /* An end of list marker is a pair of zero addresses. */
6507 if (range_beginning == 0 && range_end == 0)
6508 /* Found the end of list entry. */
6509 break;
6510
6511 /* Each base address selection entry is a pair of 2 values.
6512 The first is the largest possible address, the second is
6513 the base address. Check for a base address here. */
6514 if ((range_beginning & mask) == mask)
6515 {
6516 /* If we found the largest possible address, then
6517 read the base address. */
6518 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6519 found_base = 1;
6520 continue;
6521 }
6522
6523 if (!found_base)
6524 {
6525 /* We have no valid base address for the ranges
6526 data. */
6527 complaint (&symfile_complaints,
6528 _("Invalid .debug_ranges data (no base address)"));
6529 return 0;
6530 }
6531
6532 if (range_beginning > range_end)
6533 {
6534 /* Inverted range entries are invalid. */
6535 complaint (&symfile_complaints,
6536 _("Invalid .debug_ranges data (inverted range)"));
6537 return 0;
6538 }
6539
6540 /* Empty range entries have no effect. */
6541 if (range_beginning == range_end)
6542 continue;
6543
6544 range_beginning += base;
6545 range_end += base;
6546
6547 if (ranges_pst != NULL)
6548 addrmap_set_empty (objfile->psymtabs_addrmap,
6549 range_beginning + baseaddr,
6550 range_end - 1 + baseaddr,
6551 ranges_pst);
6552
6553 /* FIXME: This is recording everything as a low-high
6554 segment of consecutive addresses. We should have a
6555 data structure for discontiguous block ranges
6556 instead. */
6557 if (! low_set)
6558 {
6559 low = range_beginning;
6560 high = range_end;
6561 low_set = 1;
6562 }
6563 else
6564 {
6565 if (range_beginning < low)
6566 low = range_beginning;
6567 if (range_end > high)
6568 high = range_end;
6569 }
6570 }
6571
6572 if (! low_set)
6573 /* If the first entry is an end-of-list marker, the range
6574 describes an empty scope, i.e. no instructions. */
6575 return 0;
6576
6577 if (low_return)
6578 *low_return = low;
6579 if (high_return)
6580 *high_return = high;
6581 return 1;
6582 }
6583
6584 /* Get low and high pc attributes from a die. Return 1 if the attributes
6585 are present and valid, otherwise, return 0. Return -1 if the range is
6586 discontinuous, i.e. derived from DW_AT_ranges information. */
6587 static int
6588 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6589 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6590 struct partial_symtab *pst)
6591 {
6592 struct attribute *attr;
6593 CORE_ADDR low = 0;
6594 CORE_ADDR high = 0;
6595 int ret = 0;
6596
6597 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6598 if (attr)
6599 {
6600 high = DW_ADDR (attr);
6601 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6602 if (attr)
6603 low = DW_ADDR (attr);
6604 else
6605 /* Found high w/o low attribute. */
6606 return 0;
6607
6608 /* Found consecutive range of addresses. */
6609 ret = 1;
6610 }
6611 else
6612 {
6613 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6614 if (attr != NULL)
6615 {
6616 /* Value of the DW_AT_ranges attribute is the offset in the
6617 .debug_ranges section. */
6618 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6619 return 0;
6620 /* Found discontinuous range of addresses. */
6621 ret = -1;
6622 }
6623 }
6624
6625 /* read_partial_die has also the strict LOW < HIGH requirement. */
6626 if (high <= low)
6627 return 0;
6628
6629 /* When using the GNU linker, .gnu.linkonce. sections are used to
6630 eliminate duplicate copies of functions and vtables and such.
6631 The linker will arbitrarily choose one and discard the others.
6632 The AT_*_pc values for such functions refer to local labels in
6633 these sections. If the section from that file was discarded, the
6634 labels are not in the output, so the relocs get a value of 0.
6635 If this is a discarded function, mark the pc bounds as invalid,
6636 so that GDB will ignore it. */
6637 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6638 return 0;
6639
6640 *lowpc = low;
6641 if (highpc)
6642 *highpc = high;
6643 return ret;
6644 }
6645
6646 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6647 its low and high PC addresses. Do nothing if these addresses could not
6648 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6649 and HIGHPC to the high address if greater than HIGHPC. */
6650
6651 static void
6652 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6653 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6654 struct dwarf2_cu *cu)
6655 {
6656 CORE_ADDR low, high;
6657 struct die_info *child = die->child;
6658
6659 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6660 {
6661 *lowpc = min (*lowpc, low);
6662 *highpc = max (*highpc, high);
6663 }
6664
6665 /* If the language does not allow nested subprograms (either inside
6666 subprograms or lexical blocks), we're done. */
6667 if (cu->language != language_ada)
6668 return;
6669
6670 /* Check all the children of the given DIE. If it contains nested
6671 subprograms, then check their pc bounds. Likewise, we need to
6672 check lexical blocks as well, as they may also contain subprogram
6673 definitions. */
6674 while (child && child->tag)
6675 {
6676 if (child->tag == DW_TAG_subprogram
6677 || child->tag == DW_TAG_lexical_block)
6678 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6679 child = sibling_die (child);
6680 }
6681 }
6682
6683 /* Get the low and high pc's represented by the scope DIE, and store
6684 them in *LOWPC and *HIGHPC. If the correct values can't be
6685 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6686
6687 static void
6688 get_scope_pc_bounds (struct die_info *die,
6689 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6690 struct dwarf2_cu *cu)
6691 {
6692 CORE_ADDR best_low = (CORE_ADDR) -1;
6693 CORE_ADDR best_high = (CORE_ADDR) 0;
6694 CORE_ADDR current_low, current_high;
6695
6696 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6697 {
6698 best_low = current_low;
6699 best_high = current_high;
6700 }
6701 else
6702 {
6703 struct die_info *child = die->child;
6704
6705 while (child && child->tag)
6706 {
6707 switch (child->tag) {
6708 case DW_TAG_subprogram:
6709 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6710 break;
6711 case DW_TAG_namespace:
6712 case DW_TAG_module:
6713 /* FIXME: carlton/2004-01-16: Should we do this for
6714 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6715 that current GCC's always emit the DIEs corresponding
6716 to definitions of methods of classes as children of a
6717 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6718 the DIEs giving the declarations, which could be
6719 anywhere). But I don't see any reason why the
6720 standards says that they have to be there. */
6721 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6722
6723 if (current_low != ((CORE_ADDR) -1))
6724 {
6725 best_low = min (best_low, current_low);
6726 best_high = max (best_high, current_high);
6727 }
6728 break;
6729 default:
6730 /* Ignore. */
6731 break;
6732 }
6733
6734 child = sibling_die (child);
6735 }
6736 }
6737
6738 *lowpc = best_low;
6739 *highpc = best_high;
6740 }
6741
6742 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6743 in DIE. */
6744 static void
6745 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6746 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6747 {
6748 struct objfile *objfile = cu->objfile;
6749 struct attribute *attr;
6750
6751 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6752 if (attr)
6753 {
6754 CORE_ADDR high = DW_ADDR (attr);
6755
6756 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6757 if (attr)
6758 {
6759 CORE_ADDR low = DW_ADDR (attr);
6760
6761 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6762 }
6763 }
6764
6765 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6766 if (attr)
6767 {
6768 bfd *obfd = objfile->obfd;
6769
6770 /* The value of the DW_AT_ranges attribute is the offset of the
6771 address range list in the .debug_ranges section. */
6772 unsigned long offset = DW_UNSND (attr);
6773 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6774
6775 /* For some target architectures, but not others, the
6776 read_address function sign-extends the addresses it returns.
6777 To recognize base address selection entries, we need a
6778 mask. */
6779 unsigned int addr_size = cu->header.addr_size;
6780 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6781
6782 /* The base address, to which the next pair is relative. Note
6783 that this 'base' is a DWARF concept: most entries in a range
6784 list are relative, to reduce the number of relocs against the
6785 debugging information. This is separate from this function's
6786 'baseaddr' argument, which GDB uses to relocate debugging
6787 information from a shared library based on the address at
6788 which the library was loaded. */
6789 CORE_ADDR base = cu->base_address;
6790 int base_known = cu->base_known;
6791
6792 gdb_assert (dwarf2_per_objfile->ranges.readin);
6793 if (offset >= dwarf2_per_objfile->ranges.size)
6794 {
6795 complaint (&symfile_complaints,
6796 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6797 offset);
6798 return;
6799 }
6800
6801 for (;;)
6802 {
6803 unsigned int bytes_read;
6804 CORE_ADDR start, end;
6805
6806 start = read_address (obfd, buffer, cu, &bytes_read);
6807 buffer += bytes_read;
6808 end = read_address (obfd, buffer, cu, &bytes_read);
6809 buffer += bytes_read;
6810
6811 /* Did we find the end of the range list? */
6812 if (start == 0 && end == 0)
6813 break;
6814
6815 /* Did we find a base address selection entry? */
6816 else if ((start & base_select_mask) == base_select_mask)
6817 {
6818 base = end;
6819 base_known = 1;
6820 }
6821
6822 /* We found an ordinary address range. */
6823 else
6824 {
6825 if (!base_known)
6826 {
6827 complaint (&symfile_complaints,
6828 _("Invalid .debug_ranges data "
6829 "(no base address)"));
6830 return;
6831 }
6832
6833 if (start > end)
6834 {
6835 /* Inverted range entries are invalid. */
6836 complaint (&symfile_complaints,
6837 _("Invalid .debug_ranges data "
6838 "(inverted range)"));
6839 return;
6840 }
6841
6842 /* Empty range entries have no effect. */
6843 if (start == end)
6844 continue;
6845
6846 record_block_range (block,
6847 baseaddr + base + start,
6848 baseaddr + base + end - 1);
6849 }
6850 }
6851 }
6852 }
6853
6854 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6855 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6856 during 4.6.0 experimental. */
6857
6858 static int
6859 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6860 {
6861 const char *cs;
6862 int major, minor, release;
6863
6864 if (cu->producer == NULL)
6865 {
6866 /* For unknown compilers expect their behavior is DWARF version
6867 compliant.
6868
6869 GCC started to support .debug_types sections by -gdwarf-4 since
6870 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6871 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6872 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6873 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6874
6875 return 0;
6876 }
6877
6878 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6879
6880 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6881 {
6882 /* For non-GCC compilers expect their behavior is DWARF version
6883 compliant. */
6884
6885 return 0;
6886 }
6887 cs = &cu->producer[strlen ("GNU ")];
6888 while (*cs && !isdigit (*cs))
6889 cs++;
6890 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6891 {
6892 /* Not recognized as GCC. */
6893
6894 return 0;
6895 }
6896
6897 return major < 4 || (major == 4 && minor < 6);
6898 }
6899
6900 /* Return the default accessibility type if it is not overriden by
6901 DW_AT_accessibility. */
6902
6903 static enum dwarf_access_attribute
6904 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6905 {
6906 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6907 {
6908 /* The default DWARF 2 accessibility for members is public, the default
6909 accessibility for inheritance is private. */
6910
6911 if (die->tag != DW_TAG_inheritance)
6912 return DW_ACCESS_public;
6913 else
6914 return DW_ACCESS_private;
6915 }
6916 else
6917 {
6918 /* DWARF 3+ defines the default accessibility a different way. The same
6919 rules apply now for DW_TAG_inheritance as for the members and it only
6920 depends on the container kind. */
6921
6922 if (die->parent->tag == DW_TAG_class_type)
6923 return DW_ACCESS_private;
6924 else
6925 return DW_ACCESS_public;
6926 }
6927 }
6928
6929 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6930 offset. If the attribute was not found return 0, otherwise return
6931 1. If it was found but could not properly be handled, set *OFFSET
6932 to 0. */
6933
6934 static int
6935 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6936 LONGEST *offset)
6937 {
6938 struct attribute *attr;
6939
6940 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6941 if (attr != NULL)
6942 {
6943 *offset = 0;
6944
6945 /* Note that we do not check for a section offset first here.
6946 This is because DW_AT_data_member_location is new in DWARF 4,
6947 so if we see it, we can assume that a constant form is really
6948 a constant and not a section offset. */
6949 if (attr_form_is_constant (attr))
6950 *offset = dwarf2_get_attr_constant_value (attr, 0);
6951 else if (attr_form_is_section_offset (attr))
6952 dwarf2_complex_location_expr_complaint ();
6953 else if (attr_form_is_block (attr))
6954 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6955 else
6956 dwarf2_complex_location_expr_complaint ();
6957
6958 return 1;
6959 }
6960
6961 return 0;
6962 }
6963
6964 /* Add an aggregate field to the field list. */
6965
6966 static void
6967 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6968 struct dwarf2_cu *cu)
6969 {
6970 struct objfile *objfile = cu->objfile;
6971 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6972 struct nextfield *new_field;
6973 struct attribute *attr;
6974 struct field *fp;
6975 char *fieldname = "";
6976
6977 /* Allocate a new field list entry and link it in. */
6978 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6979 make_cleanup (xfree, new_field);
6980 memset (new_field, 0, sizeof (struct nextfield));
6981
6982 if (die->tag == DW_TAG_inheritance)
6983 {
6984 new_field->next = fip->baseclasses;
6985 fip->baseclasses = new_field;
6986 }
6987 else
6988 {
6989 new_field->next = fip->fields;
6990 fip->fields = new_field;
6991 }
6992 fip->nfields++;
6993
6994 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6995 if (attr)
6996 new_field->accessibility = DW_UNSND (attr);
6997 else
6998 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6999 if (new_field->accessibility != DW_ACCESS_public)
7000 fip->non_public_fields = 1;
7001
7002 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7003 if (attr)
7004 new_field->virtuality = DW_UNSND (attr);
7005 else
7006 new_field->virtuality = DW_VIRTUALITY_none;
7007
7008 fp = &new_field->field;
7009
7010 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7011 {
7012 LONGEST offset;
7013
7014 /* Data member other than a C++ static data member. */
7015
7016 /* Get type of field. */
7017 fp->type = die_type (die, cu);
7018
7019 SET_FIELD_BITPOS (*fp, 0);
7020
7021 /* Get bit size of field (zero if none). */
7022 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7023 if (attr)
7024 {
7025 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7026 }
7027 else
7028 {
7029 FIELD_BITSIZE (*fp) = 0;
7030 }
7031
7032 /* Get bit offset of field. */
7033 if (handle_data_member_location (die, cu, &offset))
7034 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7035 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7036 if (attr)
7037 {
7038 if (gdbarch_bits_big_endian (gdbarch))
7039 {
7040 /* For big endian bits, the DW_AT_bit_offset gives the
7041 additional bit offset from the MSB of the containing
7042 anonymous object to the MSB of the field. We don't
7043 have to do anything special since we don't need to
7044 know the size of the anonymous object. */
7045 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7046 }
7047 else
7048 {
7049 /* For little endian bits, compute the bit offset to the
7050 MSB of the anonymous object, subtract off the number of
7051 bits from the MSB of the field to the MSB of the
7052 object, and then subtract off the number of bits of
7053 the field itself. The result is the bit offset of
7054 the LSB of the field. */
7055 int anonymous_size;
7056 int bit_offset = DW_UNSND (attr);
7057
7058 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7059 if (attr)
7060 {
7061 /* The size of the anonymous object containing
7062 the bit field is explicit, so use the
7063 indicated size (in bytes). */
7064 anonymous_size = DW_UNSND (attr);
7065 }
7066 else
7067 {
7068 /* The size of the anonymous object containing
7069 the bit field must be inferred from the type
7070 attribute of the data member containing the
7071 bit field. */
7072 anonymous_size = TYPE_LENGTH (fp->type);
7073 }
7074 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7075 - bit_offset - FIELD_BITSIZE (*fp);
7076 }
7077 }
7078
7079 /* Get name of field. */
7080 fieldname = dwarf2_name (die, cu);
7081 if (fieldname == NULL)
7082 fieldname = "";
7083
7084 /* The name is already allocated along with this objfile, so we don't
7085 need to duplicate it for the type. */
7086 fp->name = fieldname;
7087
7088 /* Change accessibility for artificial fields (e.g. virtual table
7089 pointer or virtual base class pointer) to private. */
7090 if (dwarf2_attr (die, DW_AT_artificial, cu))
7091 {
7092 FIELD_ARTIFICIAL (*fp) = 1;
7093 new_field->accessibility = DW_ACCESS_private;
7094 fip->non_public_fields = 1;
7095 }
7096 }
7097 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7098 {
7099 /* C++ static member. */
7100
7101 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7102 is a declaration, but all versions of G++ as of this writing
7103 (so through at least 3.2.1) incorrectly generate
7104 DW_TAG_variable tags. */
7105
7106 const char *physname;
7107
7108 /* Get name of field. */
7109 fieldname = dwarf2_name (die, cu);
7110 if (fieldname == NULL)
7111 return;
7112
7113 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7114 if (attr
7115 /* Only create a symbol if this is an external value.
7116 new_symbol checks this and puts the value in the global symbol
7117 table, which we want. If it is not external, new_symbol
7118 will try to put the value in cu->list_in_scope which is wrong. */
7119 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7120 {
7121 /* A static const member, not much different than an enum as far as
7122 we're concerned, except that we can support more types. */
7123 new_symbol (die, NULL, cu);
7124 }
7125
7126 /* Get physical name. */
7127 physname = dwarf2_physname (fieldname, die, cu);
7128
7129 /* The name is already allocated along with this objfile, so we don't
7130 need to duplicate it for the type. */
7131 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7132 FIELD_TYPE (*fp) = die_type (die, cu);
7133 FIELD_NAME (*fp) = fieldname;
7134 }
7135 else if (die->tag == DW_TAG_inheritance)
7136 {
7137 LONGEST offset;
7138
7139 /* C++ base class field. */
7140 if (handle_data_member_location (die, cu, &offset))
7141 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7142 FIELD_BITSIZE (*fp) = 0;
7143 FIELD_TYPE (*fp) = die_type (die, cu);
7144 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7145 fip->nbaseclasses++;
7146 }
7147 }
7148
7149 /* Add a typedef defined in the scope of the FIP's class. */
7150
7151 static void
7152 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7153 struct dwarf2_cu *cu)
7154 {
7155 struct objfile *objfile = cu->objfile;
7156 struct typedef_field_list *new_field;
7157 struct attribute *attr;
7158 struct typedef_field *fp;
7159 char *fieldname = "";
7160
7161 /* Allocate a new field list entry and link it in. */
7162 new_field = xzalloc (sizeof (*new_field));
7163 make_cleanup (xfree, new_field);
7164
7165 gdb_assert (die->tag == DW_TAG_typedef);
7166
7167 fp = &new_field->field;
7168
7169 /* Get name of field. */
7170 fp->name = dwarf2_name (die, cu);
7171 if (fp->name == NULL)
7172 return;
7173
7174 fp->type = read_type_die (die, cu);
7175
7176 new_field->next = fip->typedef_field_list;
7177 fip->typedef_field_list = new_field;
7178 fip->typedef_field_list_count++;
7179 }
7180
7181 /* Create the vector of fields, and attach it to the type. */
7182
7183 static void
7184 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7185 struct dwarf2_cu *cu)
7186 {
7187 int nfields = fip->nfields;
7188
7189 /* Record the field count, allocate space for the array of fields,
7190 and create blank accessibility bitfields if necessary. */
7191 TYPE_NFIELDS (type) = nfields;
7192 TYPE_FIELDS (type) = (struct field *)
7193 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7194 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7195
7196 if (fip->non_public_fields && cu->language != language_ada)
7197 {
7198 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7199
7200 TYPE_FIELD_PRIVATE_BITS (type) =
7201 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7202 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7203
7204 TYPE_FIELD_PROTECTED_BITS (type) =
7205 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7206 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7207
7208 TYPE_FIELD_IGNORE_BITS (type) =
7209 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7210 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7211 }
7212
7213 /* If the type has baseclasses, allocate and clear a bit vector for
7214 TYPE_FIELD_VIRTUAL_BITS. */
7215 if (fip->nbaseclasses && cu->language != language_ada)
7216 {
7217 int num_bytes = B_BYTES (fip->nbaseclasses);
7218 unsigned char *pointer;
7219
7220 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7221 pointer = TYPE_ALLOC (type, num_bytes);
7222 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7223 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7224 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7225 }
7226
7227 /* Copy the saved-up fields into the field vector. Start from the head of
7228 the list, adding to the tail of the field array, so that they end up in
7229 the same order in the array in which they were added to the list. */
7230 while (nfields-- > 0)
7231 {
7232 struct nextfield *fieldp;
7233
7234 if (fip->fields)
7235 {
7236 fieldp = fip->fields;
7237 fip->fields = fieldp->next;
7238 }
7239 else
7240 {
7241 fieldp = fip->baseclasses;
7242 fip->baseclasses = fieldp->next;
7243 }
7244
7245 TYPE_FIELD (type, nfields) = fieldp->field;
7246 switch (fieldp->accessibility)
7247 {
7248 case DW_ACCESS_private:
7249 if (cu->language != language_ada)
7250 SET_TYPE_FIELD_PRIVATE (type, nfields);
7251 break;
7252
7253 case DW_ACCESS_protected:
7254 if (cu->language != language_ada)
7255 SET_TYPE_FIELD_PROTECTED (type, nfields);
7256 break;
7257
7258 case DW_ACCESS_public:
7259 break;
7260
7261 default:
7262 /* Unknown accessibility. Complain and treat it as public. */
7263 {
7264 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7265 fieldp->accessibility);
7266 }
7267 break;
7268 }
7269 if (nfields < fip->nbaseclasses)
7270 {
7271 switch (fieldp->virtuality)
7272 {
7273 case DW_VIRTUALITY_virtual:
7274 case DW_VIRTUALITY_pure_virtual:
7275 if (cu->language == language_ada)
7276 error (_("unexpected virtuality in component of Ada type"));
7277 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7278 break;
7279 }
7280 }
7281 }
7282 }
7283
7284 /* Add a member function to the proper fieldlist. */
7285
7286 static void
7287 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7288 struct type *type, struct dwarf2_cu *cu)
7289 {
7290 struct objfile *objfile = cu->objfile;
7291 struct attribute *attr;
7292 struct fnfieldlist *flp;
7293 int i;
7294 struct fn_field *fnp;
7295 char *fieldname;
7296 struct nextfnfield *new_fnfield;
7297 struct type *this_type;
7298 enum dwarf_access_attribute accessibility;
7299
7300 if (cu->language == language_ada)
7301 error (_("unexpected member function in Ada type"));
7302
7303 /* Get name of member function. */
7304 fieldname = dwarf2_name (die, cu);
7305 if (fieldname == NULL)
7306 return;
7307
7308 /* Look up member function name in fieldlist. */
7309 for (i = 0; i < fip->nfnfields; i++)
7310 {
7311 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7312 break;
7313 }
7314
7315 /* Create new list element if necessary. */
7316 if (i < fip->nfnfields)
7317 flp = &fip->fnfieldlists[i];
7318 else
7319 {
7320 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7321 {
7322 fip->fnfieldlists = (struct fnfieldlist *)
7323 xrealloc (fip->fnfieldlists,
7324 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7325 * sizeof (struct fnfieldlist));
7326 if (fip->nfnfields == 0)
7327 make_cleanup (free_current_contents, &fip->fnfieldlists);
7328 }
7329 flp = &fip->fnfieldlists[fip->nfnfields];
7330 flp->name = fieldname;
7331 flp->length = 0;
7332 flp->head = NULL;
7333 i = fip->nfnfields++;
7334 }
7335
7336 /* Create a new member function field and chain it to the field list
7337 entry. */
7338 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7339 make_cleanup (xfree, new_fnfield);
7340 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7341 new_fnfield->next = flp->head;
7342 flp->head = new_fnfield;
7343 flp->length++;
7344
7345 /* Fill in the member function field info. */
7346 fnp = &new_fnfield->fnfield;
7347
7348 /* Delay processing of the physname until later. */
7349 if (cu->language == language_cplus || cu->language == language_java)
7350 {
7351 add_to_method_list (type, i, flp->length - 1, fieldname,
7352 die, cu);
7353 }
7354 else
7355 {
7356 const char *physname = dwarf2_physname (fieldname, die, cu);
7357 fnp->physname = physname ? physname : "";
7358 }
7359
7360 fnp->type = alloc_type (objfile);
7361 this_type = read_type_die (die, cu);
7362 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7363 {
7364 int nparams = TYPE_NFIELDS (this_type);
7365
7366 /* TYPE is the domain of this method, and THIS_TYPE is the type
7367 of the method itself (TYPE_CODE_METHOD). */
7368 smash_to_method_type (fnp->type, type,
7369 TYPE_TARGET_TYPE (this_type),
7370 TYPE_FIELDS (this_type),
7371 TYPE_NFIELDS (this_type),
7372 TYPE_VARARGS (this_type));
7373
7374 /* Handle static member functions.
7375 Dwarf2 has no clean way to discern C++ static and non-static
7376 member functions. G++ helps GDB by marking the first
7377 parameter for non-static member functions (which is the this
7378 pointer) as artificial. We obtain this information from
7379 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7380 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7381 fnp->voffset = VOFFSET_STATIC;
7382 }
7383 else
7384 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7385 dwarf2_full_name (fieldname, die, cu));
7386
7387 /* Get fcontext from DW_AT_containing_type if present. */
7388 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7389 fnp->fcontext = die_containing_type (die, cu);
7390
7391 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7392 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7393
7394 /* Get accessibility. */
7395 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7396 if (attr)
7397 accessibility = DW_UNSND (attr);
7398 else
7399 accessibility = dwarf2_default_access_attribute (die, cu);
7400 switch (accessibility)
7401 {
7402 case DW_ACCESS_private:
7403 fnp->is_private = 1;
7404 break;
7405 case DW_ACCESS_protected:
7406 fnp->is_protected = 1;
7407 break;
7408 }
7409
7410 /* Check for artificial methods. */
7411 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7412 if (attr && DW_UNSND (attr) != 0)
7413 fnp->is_artificial = 1;
7414
7415 /* Get index in virtual function table if it is a virtual member
7416 function. For older versions of GCC, this is an offset in the
7417 appropriate virtual table, as specified by DW_AT_containing_type.
7418 For everyone else, it is an expression to be evaluated relative
7419 to the object address. */
7420
7421 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7422 if (attr)
7423 {
7424 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7425 {
7426 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7427 {
7428 /* Old-style GCC. */
7429 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7430 }
7431 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7432 || (DW_BLOCK (attr)->size > 1
7433 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7434 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7435 {
7436 struct dwarf_block blk;
7437 int offset;
7438
7439 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7440 ? 1 : 2);
7441 blk.size = DW_BLOCK (attr)->size - offset;
7442 blk.data = DW_BLOCK (attr)->data + offset;
7443 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7444 if ((fnp->voffset % cu->header.addr_size) != 0)
7445 dwarf2_complex_location_expr_complaint ();
7446 else
7447 fnp->voffset /= cu->header.addr_size;
7448 fnp->voffset += 2;
7449 }
7450 else
7451 dwarf2_complex_location_expr_complaint ();
7452
7453 if (!fnp->fcontext)
7454 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7455 }
7456 else if (attr_form_is_section_offset (attr))
7457 {
7458 dwarf2_complex_location_expr_complaint ();
7459 }
7460 else
7461 {
7462 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7463 fieldname);
7464 }
7465 }
7466 else
7467 {
7468 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7469 if (attr && DW_UNSND (attr))
7470 {
7471 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7472 complaint (&symfile_complaints,
7473 _("Member function \"%s\" (offset %d) is virtual "
7474 "but the vtable offset is not specified"),
7475 fieldname, die->offset);
7476 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7477 TYPE_CPLUS_DYNAMIC (type) = 1;
7478 }
7479 }
7480 }
7481
7482 /* Create the vector of member function fields, and attach it to the type. */
7483
7484 static void
7485 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7486 struct dwarf2_cu *cu)
7487 {
7488 struct fnfieldlist *flp;
7489 int total_length = 0;
7490 int i;
7491
7492 if (cu->language == language_ada)
7493 error (_("unexpected member functions in Ada type"));
7494
7495 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7496 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7497 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7498
7499 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7500 {
7501 struct nextfnfield *nfp = flp->head;
7502 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7503 int k;
7504
7505 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7506 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7507 fn_flp->fn_fields = (struct fn_field *)
7508 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7509 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7510 fn_flp->fn_fields[k] = nfp->fnfield;
7511
7512 total_length += flp->length;
7513 }
7514
7515 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7516 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7517 }
7518
7519 /* Returns non-zero if NAME is the name of a vtable member in CU's
7520 language, zero otherwise. */
7521 static int
7522 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7523 {
7524 static const char vptr[] = "_vptr";
7525 static const char vtable[] = "vtable";
7526
7527 /* Look for the C++ and Java forms of the vtable. */
7528 if ((cu->language == language_java
7529 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7530 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7531 && is_cplus_marker (name[sizeof (vptr) - 1])))
7532 return 1;
7533
7534 return 0;
7535 }
7536
7537 /* GCC outputs unnamed structures that are really pointers to member
7538 functions, with the ABI-specified layout. If TYPE describes
7539 such a structure, smash it into a member function type.
7540
7541 GCC shouldn't do this; it should just output pointer to member DIEs.
7542 This is GCC PR debug/28767. */
7543
7544 static void
7545 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7546 {
7547 struct type *pfn_type, *domain_type, *new_type;
7548
7549 /* Check for a structure with no name and two children. */
7550 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7551 return;
7552
7553 /* Check for __pfn and __delta members. */
7554 if (TYPE_FIELD_NAME (type, 0) == NULL
7555 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7556 || TYPE_FIELD_NAME (type, 1) == NULL
7557 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7558 return;
7559
7560 /* Find the type of the method. */
7561 pfn_type = TYPE_FIELD_TYPE (type, 0);
7562 if (pfn_type == NULL
7563 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7564 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7565 return;
7566
7567 /* Look for the "this" argument. */
7568 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7569 if (TYPE_NFIELDS (pfn_type) == 0
7570 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7571 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7572 return;
7573
7574 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7575 new_type = alloc_type (objfile);
7576 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7577 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7578 TYPE_VARARGS (pfn_type));
7579 smash_to_methodptr_type (type, new_type);
7580 }
7581
7582 /* Called when we find the DIE that starts a structure or union scope
7583 (definition) to create a type for the structure or union. Fill in
7584 the type's name and general properties; the members will not be
7585 processed until process_structure_type.
7586
7587 NOTE: we need to call these functions regardless of whether or not the
7588 DIE has a DW_AT_name attribute, since it might be an anonymous
7589 structure or union. This gets the type entered into our set of
7590 user defined types.
7591
7592 However, if the structure is incomplete (an opaque struct/union)
7593 then suppress creating a symbol table entry for it since gdb only
7594 wants to find the one with the complete definition. Note that if
7595 it is complete, we just call new_symbol, which does it's own
7596 checking about whether the struct/union is anonymous or not (and
7597 suppresses creating a symbol table entry itself). */
7598
7599 static struct type *
7600 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7601 {
7602 struct objfile *objfile = cu->objfile;
7603 struct type *type;
7604 struct attribute *attr;
7605 char *name;
7606
7607 /* If the definition of this type lives in .debug_types, read that type.
7608 Don't follow DW_AT_specification though, that will take us back up
7609 the chain and we want to go down. */
7610 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7611 if (attr)
7612 {
7613 struct dwarf2_cu *type_cu = cu;
7614 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7615
7616 /* We could just recurse on read_structure_type, but we need to call
7617 get_die_type to ensure only one type for this DIE is created.
7618 This is important, for example, because for c++ classes we need
7619 TYPE_NAME set which is only done by new_symbol. Blech. */
7620 type = read_type_die (type_die, type_cu);
7621
7622 /* TYPE_CU may not be the same as CU.
7623 Ensure TYPE is recorded in CU's type_hash table. */
7624 return set_die_type (die, type, cu);
7625 }
7626
7627 type = alloc_type (objfile);
7628 INIT_CPLUS_SPECIFIC (type);
7629
7630 name = dwarf2_name (die, cu);
7631 if (name != NULL)
7632 {
7633 if (cu->language == language_cplus
7634 || cu->language == language_java)
7635 {
7636 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7637
7638 /* dwarf2_full_name might have already finished building the DIE's
7639 type. If so, there is no need to continue. */
7640 if (get_die_type (die, cu) != NULL)
7641 return get_die_type (die, cu);
7642
7643 TYPE_TAG_NAME (type) = full_name;
7644 if (die->tag == DW_TAG_structure_type
7645 || die->tag == DW_TAG_class_type)
7646 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7647 }
7648 else
7649 {
7650 /* The name is already allocated along with this objfile, so
7651 we don't need to duplicate it for the type. */
7652 TYPE_TAG_NAME (type) = (char *) name;
7653 if (die->tag == DW_TAG_class_type)
7654 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7655 }
7656 }
7657
7658 if (die->tag == DW_TAG_structure_type)
7659 {
7660 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7661 }
7662 else if (die->tag == DW_TAG_union_type)
7663 {
7664 TYPE_CODE (type) = TYPE_CODE_UNION;
7665 }
7666 else
7667 {
7668 TYPE_CODE (type) = TYPE_CODE_CLASS;
7669 }
7670
7671 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7672 TYPE_DECLARED_CLASS (type) = 1;
7673
7674 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7675 if (attr)
7676 {
7677 TYPE_LENGTH (type) = DW_UNSND (attr);
7678 }
7679 else
7680 {
7681 TYPE_LENGTH (type) = 0;
7682 }
7683
7684 TYPE_STUB_SUPPORTED (type) = 1;
7685 if (die_is_declaration (die, cu))
7686 TYPE_STUB (type) = 1;
7687 else if (attr == NULL && die->child == NULL
7688 && producer_is_realview (cu->producer))
7689 /* RealView does not output the required DW_AT_declaration
7690 on incomplete types. */
7691 TYPE_STUB (type) = 1;
7692
7693 /* We need to add the type field to the die immediately so we don't
7694 infinitely recurse when dealing with pointers to the structure
7695 type within the structure itself. */
7696 set_die_type (die, type, cu);
7697
7698 /* set_die_type should be already done. */
7699 set_descriptive_type (type, die, cu);
7700
7701 return type;
7702 }
7703
7704 /* Finish creating a structure or union type, including filling in
7705 its members and creating a symbol for it. */
7706
7707 static void
7708 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7709 {
7710 struct objfile *objfile = cu->objfile;
7711 struct die_info *child_die = die->child;
7712 struct type *type;
7713
7714 type = get_die_type (die, cu);
7715 if (type == NULL)
7716 type = read_structure_type (die, cu);
7717
7718 if (die->child != NULL && ! die_is_declaration (die, cu))
7719 {
7720 struct field_info fi;
7721 struct die_info *child_die;
7722 VEC (symbolp) *template_args = NULL;
7723 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7724
7725 memset (&fi, 0, sizeof (struct field_info));
7726
7727 child_die = die->child;
7728
7729 while (child_die && child_die->tag)
7730 {
7731 if (child_die->tag == DW_TAG_member
7732 || child_die->tag == DW_TAG_variable)
7733 {
7734 /* NOTE: carlton/2002-11-05: A C++ static data member
7735 should be a DW_TAG_member that is a declaration, but
7736 all versions of G++ as of this writing (so through at
7737 least 3.2.1) incorrectly generate DW_TAG_variable
7738 tags for them instead. */
7739 dwarf2_add_field (&fi, child_die, cu);
7740 }
7741 else if (child_die->tag == DW_TAG_subprogram)
7742 {
7743 /* C++ member function. */
7744 dwarf2_add_member_fn (&fi, child_die, type, cu);
7745 }
7746 else if (child_die->tag == DW_TAG_inheritance)
7747 {
7748 /* C++ base class field. */
7749 dwarf2_add_field (&fi, child_die, cu);
7750 }
7751 else if (child_die->tag == DW_TAG_typedef)
7752 dwarf2_add_typedef (&fi, child_die, cu);
7753 else if (child_die->tag == DW_TAG_template_type_param
7754 || child_die->tag == DW_TAG_template_value_param)
7755 {
7756 struct symbol *arg = new_symbol (child_die, NULL, cu);
7757
7758 if (arg != NULL)
7759 VEC_safe_push (symbolp, template_args, arg);
7760 }
7761
7762 child_die = sibling_die (child_die);
7763 }
7764
7765 /* Attach template arguments to type. */
7766 if (! VEC_empty (symbolp, template_args))
7767 {
7768 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7769 TYPE_N_TEMPLATE_ARGUMENTS (type)
7770 = VEC_length (symbolp, template_args);
7771 TYPE_TEMPLATE_ARGUMENTS (type)
7772 = obstack_alloc (&objfile->objfile_obstack,
7773 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7774 * sizeof (struct symbol *)));
7775 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7776 VEC_address (symbolp, template_args),
7777 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7778 * sizeof (struct symbol *)));
7779 VEC_free (symbolp, template_args);
7780 }
7781
7782 /* Attach fields and member functions to the type. */
7783 if (fi.nfields)
7784 dwarf2_attach_fields_to_type (&fi, type, cu);
7785 if (fi.nfnfields)
7786 {
7787 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7788
7789 /* Get the type which refers to the base class (possibly this
7790 class itself) which contains the vtable pointer for the current
7791 class from the DW_AT_containing_type attribute. This use of
7792 DW_AT_containing_type is a GNU extension. */
7793
7794 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7795 {
7796 struct type *t = die_containing_type (die, cu);
7797
7798 TYPE_VPTR_BASETYPE (type) = t;
7799 if (type == t)
7800 {
7801 int i;
7802
7803 /* Our own class provides vtbl ptr. */
7804 for (i = TYPE_NFIELDS (t) - 1;
7805 i >= TYPE_N_BASECLASSES (t);
7806 --i)
7807 {
7808 char *fieldname = TYPE_FIELD_NAME (t, i);
7809
7810 if (is_vtable_name (fieldname, cu))
7811 {
7812 TYPE_VPTR_FIELDNO (type) = i;
7813 break;
7814 }
7815 }
7816
7817 /* Complain if virtual function table field not found. */
7818 if (i < TYPE_N_BASECLASSES (t))
7819 complaint (&symfile_complaints,
7820 _("virtual function table pointer "
7821 "not found when defining class '%s'"),
7822 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7823 "");
7824 }
7825 else
7826 {
7827 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7828 }
7829 }
7830 else if (cu->producer
7831 && strncmp (cu->producer,
7832 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7833 {
7834 /* The IBM XLC compiler does not provide direct indication
7835 of the containing type, but the vtable pointer is
7836 always named __vfp. */
7837
7838 int i;
7839
7840 for (i = TYPE_NFIELDS (type) - 1;
7841 i >= TYPE_N_BASECLASSES (type);
7842 --i)
7843 {
7844 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7845 {
7846 TYPE_VPTR_FIELDNO (type) = i;
7847 TYPE_VPTR_BASETYPE (type) = type;
7848 break;
7849 }
7850 }
7851 }
7852 }
7853
7854 /* Copy fi.typedef_field_list linked list elements content into the
7855 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7856 if (fi.typedef_field_list)
7857 {
7858 int i = fi.typedef_field_list_count;
7859
7860 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7861 TYPE_TYPEDEF_FIELD_ARRAY (type)
7862 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7863 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7864
7865 /* Reverse the list order to keep the debug info elements order. */
7866 while (--i >= 0)
7867 {
7868 struct typedef_field *dest, *src;
7869
7870 dest = &TYPE_TYPEDEF_FIELD (type, i);
7871 src = &fi.typedef_field_list->field;
7872 fi.typedef_field_list = fi.typedef_field_list->next;
7873 *dest = *src;
7874 }
7875 }
7876
7877 do_cleanups (back_to);
7878
7879 if (HAVE_CPLUS_STRUCT (type))
7880 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7881 }
7882
7883 quirk_gcc_member_function_pointer (type, objfile);
7884
7885 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7886 snapshots) has been known to create a die giving a declaration
7887 for a class that has, as a child, a die giving a definition for a
7888 nested class. So we have to process our children even if the
7889 current die is a declaration. Normally, of course, a declaration
7890 won't have any children at all. */
7891
7892 while (child_die != NULL && child_die->tag)
7893 {
7894 if (child_die->tag == DW_TAG_member
7895 || child_die->tag == DW_TAG_variable
7896 || child_die->tag == DW_TAG_inheritance
7897 || child_die->tag == DW_TAG_template_value_param
7898 || child_die->tag == DW_TAG_template_type_param)
7899 {
7900 /* Do nothing. */
7901 }
7902 else
7903 process_die (child_die, cu);
7904
7905 child_die = sibling_die (child_die);
7906 }
7907
7908 /* Do not consider external references. According to the DWARF standard,
7909 these DIEs are identified by the fact that they have no byte_size
7910 attribute, and a declaration attribute. */
7911 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7912 || !die_is_declaration (die, cu))
7913 new_symbol (die, type, cu);
7914 }
7915
7916 /* Given a DW_AT_enumeration_type die, set its type. We do not
7917 complete the type's fields yet, or create any symbols. */
7918
7919 static struct type *
7920 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7921 {
7922 struct objfile *objfile = cu->objfile;
7923 struct type *type;
7924 struct attribute *attr;
7925 const char *name;
7926
7927 /* If the definition of this type lives in .debug_types, read that type.
7928 Don't follow DW_AT_specification though, that will take us back up
7929 the chain and we want to go down. */
7930 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7931 if (attr)
7932 {
7933 struct dwarf2_cu *type_cu = cu;
7934 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7935
7936 type = read_type_die (type_die, type_cu);
7937
7938 /* TYPE_CU may not be the same as CU.
7939 Ensure TYPE is recorded in CU's type_hash table. */
7940 return set_die_type (die, type, cu);
7941 }
7942
7943 type = alloc_type (objfile);
7944
7945 TYPE_CODE (type) = TYPE_CODE_ENUM;
7946 name = dwarf2_full_name (NULL, die, cu);
7947 if (name != NULL)
7948 TYPE_TAG_NAME (type) = (char *) name;
7949
7950 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7951 if (attr)
7952 {
7953 TYPE_LENGTH (type) = DW_UNSND (attr);
7954 }
7955 else
7956 {
7957 TYPE_LENGTH (type) = 0;
7958 }
7959
7960 /* The enumeration DIE can be incomplete. In Ada, any type can be
7961 declared as private in the package spec, and then defined only
7962 inside the package body. Such types are known as Taft Amendment
7963 Types. When another package uses such a type, an incomplete DIE
7964 may be generated by the compiler. */
7965 if (die_is_declaration (die, cu))
7966 TYPE_STUB (type) = 1;
7967
7968 return set_die_type (die, type, cu);
7969 }
7970
7971 /* Given a pointer to a die which begins an enumeration, process all
7972 the dies that define the members of the enumeration, and create the
7973 symbol for the enumeration type.
7974
7975 NOTE: We reverse the order of the element list. */
7976
7977 static void
7978 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7979 {
7980 struct type *this_type;
7981
7982 this_type = get_die_type (die, cu);
7983 if (this_type == NULL)
7984 this_type = read_enumeration_type (die, cu);
7985
7986 if (die->child != NULL)
7987 {
7988 struct die_info *child_die;
7989 struct symbol *sym;
7990 struct field *fields = NULL;
7991 int num_fields = 0;
7992 int unsigned_enum = 1;
7993 char *name;
7994
7995 child_die = die->child;
7996 while (child_die && child_die->tag)
7997 {
7998 if (child_die->tag != DW_TAG_enumerator)
7999 {
8000 process_die (child_die, cu);
8001 }
8002 else
8003 {
8004 name = dwarf2_name (child_die, cu);
8005 if (name)
8006 {
8007 sym = new_symbol (child_die, this_type, cu);
8008 if (SYMBOL_VALUE (sym) < 0)
8009 unsigned_enum = 0;
8010
8011 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8012 {
8013 fields = (struct field *)
8014 xrealloc (fields,
8015 (num_fields + DW_FIELD_ALLOC_CHUNK)
8016 * sizeof (struct field));
8017 }
8018
8019 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8020 FIELD_TYPE (fields[num_fields]) = NULL;
8021 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8022 FIELD_BITSIZE (fields[num_fields]) = 0;
8023
8024 num_fields++;
8025 }
8026 }
8027
8028 child_die = sibling_die (child_die);
8029 }
8030
8031 if (num_fields)
8032 {
8033 TYPE_NFIELDS (this_type) = num_fields;
8034 TYPE_FIELDS (this_type) = (struct field *)
8035 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8036 memcpy (TYPE_FIELDS (this_type), fields,
8037 sizeof (struct field) * num_fields);
8038 xfree (fields);
8039 }
8040 if (unsigned_enum)
8041 TYPE_UNSIGNED (this_type) = 1;
8042 }
8043
8044 /* If we are reading an enum from a .debug_types unit, and the enum
8045 is a declaration, and the enum is not the signatured type in the
8046 unit, then we do not want to add a symbol for it. Adding a
8047 symbol would in some cases obscure the true definition of the
8048 enum, giving users an incomplete type when the definition is
8049 actually available. Note that we do not want to do this for all
8050 enums which are just declarations, because C++0x allows forward
8051 enum declarations. */
8052 if (cu->per_cu->debug_types_section
8053 && die_is_declaration (die, cu))
8054 {
8055 struct signatured_type *type_sig;
8056
8057 type_sig
8058 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8059 cu->per_cu->debug_types_section,
8060 cu->per_cu->offset);
8061 if (type_sig->type_offset != die->offset)
8062 return;
8063 }
8064
8065 new_symbol (die, this_type, cu);
8066 }
8067
8068 /* Extract all information from a DW_TAG_array_type DIE and put it in
8069 the DIE's type field. For now, this only handles one dimensional
8070 arrays. */
8071
8072 static struct type *
8073 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8074 {
8075 struct objfile *objfile = cu->objfile;
8076 struct die_info *child_die;
8077 struct type *type;
8078 struct type *element_type, *range_type, *index_type;
8079 struct type **range_types = NULL;
8080 struct attribute *attr;
8081 int ndim = 0;
8082 struct cleanup *back_to;
8083 char *name;
8084
8085 element_type = die_type (die, cu);
8086
8087 /* The die_type call above may have already set the type for this DIE. */
8088 type = get_die_type (die, cu);
8089 if (type)
8090 return type;
8091
8092 /* Irix 6.2 native cc creates array types without children for
8093 arrays with unspecified length. */
8094 if (die->child == NULL)
8095 {
8096 index_type = objfile_type (objfile)->builtin_int;
8097 range_type = create_range_type (NULL, index_type, 0, -1);
8098 type = create_array_type (NULL, element_type, range_type);
8099 return set_die_type (die, type, cu);
8100 }
8101
8102 back_to = make_cleanup (null_cleanup, NULL);
8103 child_die = die->child;
8104 while (child_die && child_die->tag)
8105 {
8106 if (child_die->tag == DW_TAG_subrange_type)
8107 {
8108 struct type *child_type = read_type_die (child_die, cu);
8109
8110 if (child_type != NULL)
8111 {
8112 /* The range type was succesfully read. Save it for the
8113 array type creation. */
8114 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8115 {
8116 range_types = (struct type **)
8117 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8118 * sizeof (struct type *));
8119 if (ndim == 0)
8120 make_cleanup (free_current_contents, &range_types);
8121 }
8122 range_types[ndim++] = child_type;
8123 }
8124 }
8125 child_die = sibling_die (child_die);
8126 }
8127
8128 /* Dwarf2 dimensions are output from left to right, create the
8129 necessary array types in backwards order. */
8130
8131 type = element_type;
8132
8133 if (read_array_order (die, cu) == DW_ORD_col_major)
8134 {
8135 int i = 0;
8136
8137 while (i < ndim)
8138 type = create_array_type (NULL, type, range_types[i++]);
8139 }
8140 else
8141 {
8142 while (ndim-- > 0)
8143 type = create_array_type (NULL, type, range_types[ndim]);
8144 }
8145
8146 /* Understand Dwarf2 support for vector types (like they occur on
8147 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8148 array type. This is not part of the Dwarf2/3 standard yet, but a
8149 custom vendor extension. The main difference between a regular
8150 array and the vector variant is that vectors are passed by value
8151 to functions. */
8152 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8153 if (attr)
8154 make_vector_type (type);
8155
8156 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8157 implementation may choose to implement triple vectors using this
8158 attribute. */
8159 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8160 if (attr)
8161 {
8162 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8163 TYPE_LENGTH (type) = DW_UNSND (attr);
8164 else
8165 complaint (&symfile_complaints,
8166 _("DW_AT_byte_size for array type smaller "
8167 "than the total size of elements"));
8168 }
8169
8170 name = dwarf2_name (die, cu);
8171 if (name)
8172 TYPE_NAME (type) = name;
8173
8174 /* Install the type in the die. */
8175 set_die_type (die, type, cu);
8176
8177 /* set_die_type should be already done. */
8178 set_descriptive_type (type, die, cu);
8179
8180 do_cleanups (back_to);
8181
8182 return type;
8183 }
8184
8185 static enum dwarf_array_dim_ordering
8186 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8187 {
8188 struct attribute *attr;
8189
8190 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8191
8192 if (attr) return DW_SND (attr);
8193
8194 /* GNU F77 is a special case, as at 08/2004 array type info is the
8195 opposite order to the dwarf2 specification, but data is still
8196 laid out as per normal fortran.
8197
8198 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8199 version checking. */
8200
8201 if (cu->language == language_fortran
8202 && cu->producer && strstr (cu->producer, "GNU F77"))
8203 {
8204 return DW_ORD_row_major;
8205 }
8206
8207 switch (cu->language_defn->la_array_ordering)
8208 {
8209 case array_column_major:
8210 return DW_ORD_col_major;
8211 case array_row_major:
8212 default:
8213 return DW_ORD_row_major;
8214 };
8215 }
8216
8217 /* Extract all information from a DW_TAG_set_type DIE and put it in
8218 the DIE's type field. */
8219
8220 static struct type *
8221 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8222 {
8223 struct type *domain_type, *set_type;
8224 struct attribute *attr;
8225
8226 domain_type = die_type (die, cu);
8227
8228 /* The die_type call above may have already set the type for this DIE. */
8229 set_type = get_die_type (die, cu);
8230 if (set_type)
8231 return set_type;
8232
8233 set_type = create_set_type (NULL, domain_type);
8234
8235 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8236 if (attr)
8237 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8238
8239 return set_die_type (die, set_type, cu);
8240 }
8241
8242 /* First cut: install each common block member as a global variable. */
8243
8244 static void
8245 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8246 {
8247 struct die_info *child_die;
8248 struct attribute *attr;
8249 struct symbol *sym;
8250 CORE_ADDR base = (CORE_ADDR) 0;
8251
8252 attr = dwarf2_attr (die, DW_AT_location, cu);
8253 if (attr)
8254 {
8255 /* Support the .debug_loc offsets. */
8256 if (attr_form_is_block (attr))
8257 {
8258 base = decode_locdesc (DW_BLOCK (attr), cu);
8259 }
8260 else if (attr_form_is_section_offset (attr))
8261 {
8262 dwarf2_complex_location_expr_complaint ();
8263 }
8264 else
8265 {
8266 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8267 "common block member");
8268 }
8269 }
8270 if (die->child != NULL)
8271 {
8272 child_die = die->child;
8273 while (child_die && child_die->tag)
8274 {
8275 LONGEST offset;
8276
8277 sym = new_symbol (child_die, NULL, cu);
8278 if (sym != NULL
8279 && handle_data_member_location (child_die, cu, &offset))
8280 {
8281 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8282 add_symbol_to_list (sym, &global_symbols);
8283 }
8284 child_die = sibling_die (child_die);
8285 }
8286 }
8287 }
8288
8289 /* Create a type for a C++ namespace. */
8290
8291 static struct type *
8292 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8293 {
8294 struct objfile *objfile = cu->objfile;
8295 const char *previous_prefix, *name;
8296 int is_anonymous;
8297 struct type *type;
8298
8299 /* For extensions, reuse the type of the original namespace. */
8300 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8301 {
8302 struct die_info *ext_die;
8303 struct dwarf2_cu *ext_cu = cu;
8304
8305 ext_die = dwarf2_extension (die, &ext_cu);
8306 type = read_type_die (ext_die, ext_cu);
8307
8308 /* EXT_CU may not be the same as CU.
8309 Ensure TYPE is recorded in CU's type_hash table. */
8310 return set_die_type (die, type, cu);
8311 }
8312
8313 name = namespace_name (die, &is_anonymous, cu);
8314
8315 /* Now build the name of the current namespace. */
8316
8317 previous_prefix = determine_prefix (die, cu);
8318 if (previous_prefix[0] != '\0')
8319 name = typename_concat (&objfile->objfile_obstack,
8320 previous_prefix, name, 0, cu);
8321
8322 /* Create the type. */
8323 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8324 objfile);
8325 TYPE_NAME (type) = (char *) name;
8326 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8327
8328 return set_die_type (die, type, cu);
8329 }
8330
8331 /* Read a C++ namespace. */
8332
8333 static void
8334 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8335 {
8336 struct objfile *objfile = cu->objfile;
8337 int is_anonymous;
8338
8339 /* Add a symbol associated to this if we haven't seen the namespace
8340 before. Also, add a using directive if it's an anonymous
8341 namespace. */
8342
8343 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8344 {
8345 struct type *type;
8346
8347 type = read_type_die (die, cu);
8348 new_symbol (die, type, cu);
8349
8350 namespace_name (die, &is_anonymous, cu);
8351 if (is_anonymous)
8352 {
8353 const char *previous_prefix = determine_prefix (die, cu);
8354
8355 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8356 NULL, NULL, &objfile->objfile_obstack);
8357 }
8358 }
8359
8360 if (die->child != NULL)
8361 {
8362 struct die_info *child_die = die->child;
8363
8364 while (child_die && child_die->tag)
8365 {
8366 process_die (child_die, cu);
8367 child_die = sibling_die (child_die);
8368 }
8369 }
8370 }
8371
8372 /* Read a Fortran module as type. This DIE can be only a declaration used for
8373 imported module. Still we need that type as local Fortran "use ... only"
8374 declaration imports depend on the created type in determine_prefix. */
8375
8376 static struct type *
8377 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8378 {
8379 struct objfile *objfile = cu->objfile;
8380 char *module_name;
8381 struct type *type;
8382
8383 module_name = dwarf2_name (die, cu);
8384 if (!module_name)
8385 complaint (&symfile_complaints,
8386 _("DW_TAG_module has no name, offset 0x%x"),
8387 die->offset);
8388 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8389
8390 /* determine_prefix uses TYPE_TAG_NAME. */
8391 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8392
8393 return set_die_type (die, type, cu);
8394 }
8395
8396 /* Read a Fortran module. */
8397
8398 static void
8399 read_module (struct die_info *die, struct dwarf2_cu *cu)
8400 {
8401 struct die_info *child_die = die->child;
8402
8403 while (child_die && child_die->tag)
8404 {
8405 process_die (child_die, cu);
8406 child_die = sibling_die (child_die);
8407 }
8408 }
8409
8410 /* Return the name of the namespace represented by DIE. Set
8411 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8412 namespace. */
8413
8414 static const char *
8415 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8416 {
8417 struct die_info *current_die;
8418 const char *name = NULL;
8419
8420 /* Loop through the extensions until we find a name. */
8421
8422 for (current_die = die;
8423 current_die != NULL;
8424 current_die = dwarf2_extension (die, &cu))
8425 {
8426 name = dwarf2_name (current_die, cu);
8427 if (name != NULL)
8428 break;
8429 }
8430
8431 /* Is it an anonymous namespace? */
8432
8433 *is_anonymous = (name == NULL);
8434 if (*is_anonymous)
8435 name = CP_ANONYMOUS_NAMESPACE_STR;
8436
8437 return name;
8438 }
8439
8440 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8441 the user defined type vector. */
8442
8443 static struct type *
8444 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8445 {
8446 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8447 struct comp_unit_head *cu_header = &cu->header;
8448 struct type *type;
8449 struct attribute *attr_byte_size;
8450 struct attribute *attr_address_class;
8451 int byte_size, addr_class;
8452 struct type *target_type;
8453
8454 target_type = die_type (die, cu);
8455
8456 /* The die_type call above may have already set the type for this DIE. */
8457 type = get_die_type (die, cu);
8458 if (type)
8459 return type;
8460
8461 type = lookup_pointer_type (target_type);
8462
8463 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8464 if (attr_byte_size)
8465 byte_size = DW_UNSND (attr_byte_size);
8466 else
8467 byte_size = cu_header->addr_size;
8468
8469 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8470 if (attr_address_class)
8471 addr_class = DW_UNSND (attr_address_class);
8472 else
8473 addr_class = DW_ADDR_none;
8474
8475 /* If the pointer size or address class is different than the
8476 default, create a type variant marked as such and set the
8477 length accordingly. */
8478 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8479 {
8480 if (gdbarch_address_class_type_flags_p (gdbarch))
8481 {
8482 int type_flags;
8483
8484 type_flags = gdbarch_address_class_type_flags
8485 (gdbarch, byte_size, addr_class);
8486 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8487 == 0);
8488 type = make_type_with_address_space (type, type_flags);
8489 }
8490 else if (TYPE_LENGTH (type) != byte_size)
8491 {
8492 complaint (&symfile_complaints,
8493 _("invalid pointer size %d"), byte_size);
8494 }
8495 else
8496 {
8497 /* Should we also complain about unhandled address classes? */
8498 }
8499 }
8500
8501 TYPE_LENGTH (type) = byte_size;
8502 return set_die_type (die, type, cu);
8503 }
8504
8505 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8506 the user defined type vector. */
8507
8508 static struct type *
8509 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8510 {
8511 struct type *type;
8512 struct type *to_type;
8513 struct type *domain;
8514
8515 to_type = die_type (die, cu);
8516 domain = die_containing_type (die, cu);
8517
8518 /* The calls above may have already set the type for this DIE. */
8519 type = get_die_type (die, cu);
8520 if (type)
8521 return type;
8522
8523 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8524 type = lookup_methodptr_type (to_type);
8525 else
8526 type = lookup_memberptr_type (to_type, domain);
8527
8528 return set_die_type (die, type, cu);
8529 }
8530
8531 /* Extract all information from a DW_TAG_reference_type DIE and add to
8532 the user defined type vector. */
8533
8534 static struct type *
8535 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8536 {
8537 struct comp_unit_head *cu_header = &cu->header;
8538 struct type *type, *target_type;
8539 struct attribute *attr;
8540
8541 target_type = die_type (die, cu);
8542
8543 /* The die_type call above may have already set the type for this DIE. */
8544 type = get_die_type (die, cu);
8545 if (type)
8546 return type;
8547
8548 type = lookup_reference_type (target_type);
8549 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8550 if (attr)
8551 {
8552 TYPE_LENGTH (type) = DW_UNSND (attr);
8553 }
8554 else
8555 {
8556 TYPE_LENGTH (type) = cu_header->addr_size;
8557 }
8558 return set_die_type (die, type, cu);
8559 }
8560
8561 static struct type *
8562 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8563 {
8564 struct type *base_type, *cv_type;
8565
8566 base_type = die_type (die, cu);
8567
8568 /* The die_type call above may have already set the type for this DIE. */
8569 cv_type = get_die_type (die, cu);
8570 if (cv_type)
8571 return cv_type;
8572
8573 /* In case the const qualifier is applied to an array type, the element type
8574 is so qualified, not the array type (section 6.7.3 of C99). */
8575 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8576 {
8577 struct type *el_type, *inner_array;
8578
8579 base_type = copy_type (base_type);
8580 inner_array = base_type;
8581
8582 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8583 {
8584 TYPE_TARGET_TYPE (inner_array) =
8585 copy_type (TYPE_TARGET_TYPE (inner_array));
8586 inner_array = TYPE_TARGET_TYPE (inner_array);
8587 }
8588
8589 el_type = TYPE_TARGET_TYPE (inner_array);
8590 TYPE_TARGET_TYPE (inner_array) =
8591 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8592
8593 return set_die_type (die, base_type, cu);
8594 }
8595
8596 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8597 return set_die_type (die, cv_type, cu);
8598 }
8599
8600 static struct type *
8601 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8602 {
8603 struct type *base_type, *cv_type;
8604
8605 base_type = die_type (die, cu);
8606
8607 /* The die_type call above may have already set the type for this DIE. */
8608 cv_type = get_die_type (die, cu);
8609 if (cv_type)
8610 return cv_type;
8611
8612 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8613 return set_die_type (die, cv_type, cu);
8614 }
8615
8616 /* Extract all information from a DW_TAG_string_type DIE and add to
8617 the user defined type vector. It isn't really a user defined type,
8618 but it behaves like one, with other DIE's using an AT_user_def_type
8619 attribute to reference it. */
8620
8621 static struct type *
8622 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8623 {
8624 struct objfile *objfile = cu->objfile;
8625 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8626 struct type *type, *range_type, *index_type, *char_type;
8627 struct attribute *attr;
8628 unsigned int length;
8629
8630 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8631 if (attr)
8632 {
8633 length = DW_UNSND (attr);
8634 }
8635 else
8636 {
8637 /* Check for the DW_AT_byte_size attribute. */
8638 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8639 if (attr)
8640 {
8641 length = DW_UNSND (attr);
8642 }
8643 else
8644 {
8645 length = 1;
8646 }
8647 }
8648
8649 index_type = objfile_type (objfile)->builtin_int;
8650 range_type = create_range_type (NULL, index_type, 1, length);
8651 char_type = language_string_char_type (cu->language_defn, gdbarch);
8652 type = create_string_type (NULL, char_type, range_type);
8653
8654 return set_die_type (die, type, cu);
8655 }
8656
8657 /* Handle DIES due to C code like:
8658
8659 struct foo
8660 {
8661 int (*funcp)(int a, long l);
8662 int b;
8663 };
8664
8665 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8666
8667 static struct type *
8668 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8669 {
8670 struct objfile *objfile = cu->objfile;
8671 struct type *type; /* Type that this function returns. */
8672 struct type *ftype; /* Function that returns above type. */
8673 struct attribute *attr;
8674
8675 type = die_type (die, cu);
8676
8677 /* The die_type call above may have already set the type for this DIE. */
8678 ftype = get_die_type (die, cu);
8679 if (ftype)
8680 return ftype;
8681
8682 ftype = lookup_function_type (type);
8683
8684 /* All functions in C++, Pascal and Java have prototypes. */
8685 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8686 if ((attr && (DW_UNSND (attr) != 0))
8687 || cu->language == language_cplus
8688 || cu->language == language_java
8689 || cu->language == language_pascal)
8690 TYPE_PROTOTYPED (ftype) = 1;
8691 else if (producer_is_realview (cu->producer))
8692 /* RealView does not emit DW_AT_prototyped. We can not
8693 distinguish prototyped and unprototyped functions; default to
8694 prototyped, since that is more common in modern code (and
8695 RealView warns about unprototyped functions). */
8696 TYPE_PROTOTYPED (ftype) = 1;
8697
8698 /* Store the calling convention in the type if it's available in
8699 the subroutine die. Otherwise set the calling convention to
8700 the default value DW_CC_normal. */
8701 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8702 if (attr)
8703 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8704 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8705 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8706 else
8707 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8708
8709 /* We need to add the subroutine type to the die immediately so
8710 we don't infinitely recurse when dealing with parameters
8711 declared as the same subroutine type. */
8712 set_die_type (die, ftype, cu);
8713
8714 if (die->child != NULL)
8715 {
8716 struct type *void_type = objfile_type (objfile)->builtin_void;
8717 struct die_info *child_die;
8718 int nparams, iparams;
8719
8720 /* Count the number of parameters.
8721 FIXME: GDB currently ignores vararg functions, but knows about
8722 vararg member functions. */
8723 nparams = 0;
8724 child_die = die->child;
8725 while (child_die && child_die->tag)
8726 {
8727 if (child_die->tag == DW_TAG_formal_parameter)
8728 nparams++;
8729 else if (child_die->tag == DW_TAG_unspecified_parameters)
8730 TYPE_VARARGS (ftype) = 1;
8731 child_die = sibling_die (child_die);
8732 }
8733
8734 /* Allocate storage for parameters and fill them in. */
8735 TYPE_NFIELDS (ftype) = nparams;
8736 TYPE_FIELDS (ftype) = (struct field *)
8737 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8738
8739 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8740 even if we error out during the parameters reading below. */
8741 for (iparams = 0; iparams < nparams; iparams++)
8742 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8743
8744 iparams = 0;
8745 child_die = die->child;
8746 while (child_die && child_die->tag)
8747 {
8748 if (child_die->tag == DW_TAG_formal_parameter)
8749 {
8750 struct type *arg_type;
8751
8752 /* DWARF version 2 has no clean way to discern C++
8753 static and non-static member functions. G++ helps
8754 GDB by marking the first parameter for non-static
8755 member functions (which is the this pointer) as
8756 artificial. We pass this information to
8757 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8758
8759 DWARF version 3 added DW_AT_object_pointer, which GCC
8760 4.5 does not yet generate. */
8761 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8762 if (attr)
8763 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8764 else
8765 {
8766 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8767
8768 /* GCC/43521: In java, the formal parameter
8769 "this" is sometimes not marked with DW_AT_artificial. */
8770 if (cu->language == language_java)
8771 {
8772 const char *name = dwarf2_name (child_die, cu);
8773
8774 if (name && !strcmp (name, "this"))
8775 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8776 }
8777 }
8778 arg_type = die_type (child_die, cu);
8779
8780 /* RealView does not mark THIS as const, which the testsuite
8781 expects. GCC marks THIS as const in method definitions,
8782 but not in the class specifications (GCC PR 43053). */
8783 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8784 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8785 {
8786 int is_this = 0;
8787 struct dwarf2_cu *arg_cu = cu;
8788 const char *name = dwarf2_name (child_die, cu);
8789
8790 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8791 if (attr)
8792 {
8793 /* If the compiler emits this, use it. */
8794 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8795 is_this = 1;
8796 }
8797 else if (name && strcmp (name, "this") == 0)
8798 /* Function definitions will have the argument names. */
8799 is_this = 1;
8800 else if (name == NULL && iparams == 0)
8801 /* Declarations may not have the names, so like
8802 elsewhere in GDB, assume an artificial first
8803 argument is "this". */
8804 is_this = 1;
8805
8806 if (is_this)
8807 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8808 arg_type, 0);
8809 }
8810
8811 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8812 iparams++;
8813 }
8814 child_die = sibling_die (child_die);
8815 }
8816 }
8817
8818 return ftype;
8819 }
8820
8821 static struct type *
8822 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8823 {
8824 struct objfile *objfile = cu->objfile;
8825 const char *name = NULL;
8826 struct type *this_type, *target_type;
8827
8828 name = dwarf2_full_name (NULL, die, cu);
8829 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8830 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8831 TYPE_NAME (this_type) = (char *) name;
8832 set_die_type (die, this_type, cu);
8833 target_type = die_type (die, cu);
8834 if (target_type != this_type)
8835 TYPE_TARGET_TYPE (this_type) = target_type;
8836 else
8837 {
8838 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8839 spec and cause infinite loops in GDB. */
8840 complaint (&symfile_complaints,
8841 _("Self-referential DW_TAG_typedef "
8842 "- DIE at 0x%x [in module %s]"),
8843 die->offset, objfile->name);
8844 TYPE_TARGET_TYPE (this_type) = NULL;
8845 }
8846 return this_type;
8847 }
8848
8849 /* Find a representation of a given base type and install
8850 it in the TYPE field of the die. */
8851
8852 static struct type *
8853 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8854 {
8855 struct objfile *objfile = cu->objfile;
8856 struct type *type;
8857 struct attribute *attr;
8858 int encoding = 0, size = 0;
8859 char *name;
8860 enum type_code code = TYPE_CODE_INT;
8861 int type_flags = 0;
8862 struct type *target_type = NULL;
8863
8864 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8865 if (attr)
8866 {
8867 encoding = DW_UNSND (attr);
8868 }
8869 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8870 if (attr)
8871 {
8872 size = DW_UNSND (attr);
8873 }
8874 name = dwarf2_name (die, cu);
8875 if (!name)
8876 {
8877 complaint (&symfile_complaints,
8878 _("DW_AT_name missing from DW_TAG_base_type"));
8879 }
8880
8881 switch (encoding)
8882 {
8883 case DW_ATE_address:
8884 /* Turn DW_ATE_address into a void * pointer. */
8885 code = TYPE_CODE_PTR;
8886 type_flags |= TYPE_FLAG_UNSIGNED;
8887 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8888 break;
8889 case DW_ATE_boolean:
8890 code = TYPE_CODE_BOOL;
8891 type_flags |= TYPE_FLAG_UNSIGNED;
8892 break;
8893 case DW_ATE_complex_float:
8894 code = TYPE_CODE_COMPLEX;
8895 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8896 break;
8897 case DW_ATE_decimal_float:
8898 code = TYPE_CODE_DECFLOAT;
8899 break;
8900 case DW_ATE_float:
8901 code = TYPE_CODE_FLT;
8902 break;
8903 case DW_ATE_signed:
8904 break;
8905 case DW_ATE_unsigned:
8906 type_flags |= TYPE_FLAG_UNSIGNED;
8907 if (cu->language == language_fortran
8908 && name
8909 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8910 code = TYPE_CODE_CHAR;
8911 break;
8912 case DW_ATE_signed_char:
8913 if (cu->language == language_ada || cu->language == language_m2
8914 || cu->language == language_pascal
8915 || cu->language == language_fortran)
8916 code = TYPE_CODE_CHAR;
8917 break;
8918 case DW_ATE_unsigned_char:
8919 if (cu->language == language_ada || cu->language == language_m2
8920 || cu->language == language_pascal
8921 || cu->language == language_fortran)
8922 code = TYPE_CODE_CHAR;
8923 type_flags |= TYPE_FLAG_UNSIGNED;
8924 break;
8925 case DW_ATE_UTF:
8926 /* We just treat this as an integer and then recognize the
8927 type by name elsewhere. */
8928 break;
8929
8930 default:
8931 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8932 dwarf_type_encoding_name (encoding));
8933 break;
8934 }
8935
8936 type = init_type (code, size, type_flags, NULL, objfile);
8937 TYPE_NAME (type) = name;
8938 TYPE_TARGET_TYPE (type) = target_type;
8939
8940 if (name && strcmp (name, "char") == 0)
8941 TYPE_NOSIGN (type) = 1;
8942
8943 return set_die_type (die, type, cu);
8944 }
8945
8946 /* Read the given DW_AT_subrange DIE. */
8947
8948 static struct type *
8949 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8950 {
8951 struct type *base_type;
8952 struct type *range_type;
8953 struct attribute *attr;
8954 LONGEST low = 0;
8955 LONGEST high = -1;
8956 char *name;
8957 LONGEST negative_mask;
8958
8959 base_type = die_type (die, cu);
8960 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8961 check_typedef (base_type);
8962
8963 /* The die_type call above may have already set the type for this DIE. */
8964 range_type = get_die_type (die, cu);
8965 if (range_type)
8966 return range_type;
8967
8968 if (cu->language == language_fortran)
8969 {
8970 /* FORTRAN implies a lower bound of 1, if not given. */
8971 low = 1;
8972 }
8973
8974 /* FIXME: For variable sized arrays either of these could be
8975 a variable rather than a constant value. We'll allow it,
8976 but we don't know how to handle it. */
8977 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8978 if (attr)
8979 low = dwarf2_get_attr_constant_value (attr, 0);
8980
8981 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8982 if (attr)
8983 {
8984 if (attr_form_is_block (attr) || is_ref_attr (attr))
8985 {
8986 /* GCC encodes arrays with unspecified or dynamic length
8987 with a DW_FORM_block1 attribute or a reference attribute.
8988 FIXME: GDB does not yet know how to handle dynamic
8989 arrays properly, treat them as arrays with unspecified
8990 length for now.
8991
8992 FIXME: jimb/2003-09-22: GDB does not really know
8993 how to handle arrays of unspecified length
8994 either; we just represent them as zero-length
8995 arrays. Choose an appropriate upper bound given
8996 the lower bound we've computed above. */
8997 high = low - 1;
8998 }
8999 else
9000 high = dwarf2_get_attr_constant_value (attr, 1);
9001 }
9002 else
9003 {
9004 attr = dwarf2_attr (die, DW_AT_count, cu);
9005 if (attr)
9006 {
9007 int count = dwarf2_get_attr_constant_value (attr, 1);
9008 high = low + count - 1;
9009 }
9010 else
9011 {
9012 /* Unspecified array length. */
9013 high = low - 1;
9014 }
9015 }
9016
9017 /* Dwarf-2 specifications explicitly allows to create subrange types
9018 without specifying a base type.
9019 In that case, the base type must be set to the type of
9020 the lower bound, upper bound or count, in that order, if any of these
9021 three attributes references an object that has a type.
9022 If no base type is found, the Dwarf-2 specifications say that
9023 a signed integer type of size equal to the size of an address should
9024 be used.
9025 For the following C code: `extern char gdb_int [];'
9026 GCC produces an empty range DIE.
9027 FIXME: muller/2010-05-28: Possible references to object for low bound,
9028 high bound or count are not yet handled by this code. */
9029 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9030 {
9031 struct objfile *objfile = cu->objfile;
9032 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9033 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9034 struct type *int_type = objfile_type (objfile)->builtin_int;
9035
9036 /* Test "int", "long int", and "long long int" objfile types,
9037 and select the first one having a size above or equal to the
9038 architecture address size. */
9039 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9040 base_type = int_type;
9041 else
9042 {
9043 int_type = objfile_type (objfile)->builtin_long;
9044 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9045 base_type = int_type;
9046 else
9047 {
9048 int_type = objfile_type (objfile)->builtin_long_long;
9049 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9050 base_type = int_type;
9051 }
9052 }
9053 }
9054
9055 negative_mask =
9056 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9057 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9058 low |= negative_mask;
9059 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9060 high |= negative_mask;
9061
9062 range_type = create_range_type (NULL, base_type, low, high);
9063
9064 /* Mark arrays with dynamic length at least as an array of unspecified
9065 length. GDB could check the boundary but before it gets implemented at
9066 least allow accessing the array elements. */
9067 if (attr && attr_form_is_block (attr))
9068 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9069
9070 /* Ada expects an empty array on no boundary attributes. */
9071 if (attr == NULL && cu->language != language_ada)
9072 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9073
9074 name = dwarf2_name (die, cu);
9075 if (name)
9076 TYPE_NAME (range_type) = name;
9077
9078 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9079 if (attr)
9080 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9081
9082 set_die_type (die, range_type, cu);
9083
9084 /* set_die_type should be already done. */
9085 set_descriptive_type (range_type, die, cu);
9086
9087 return range_type;
9088 }
9089
9090 static struct type *
9091 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9092 {
9093 struct type *type;
9094
9095 /* For now, we only support the C meaning of an unspecified type: void. */
9096
9097 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9098 TYPE_NAME (type) = dwarf2_name (die, cu);
9099
9100 return set_die_type (die, type, cu);
9101 }
9102
9103 /* Trivial hash function for die_info: the hash value of a DIE
9104 is its offset in .debug_info for this objfile. */
9105
9106 static hashval_t
9107 die_hash (const void *item)
9108 {
9109 const struct die_info *die = item;
9110
9111 return die->offset;
9112 }
9113
9114 /* Trivial comparison function for die_info structures: two DIEs
9115 are equal if they have the same offset. */
9116
9117 static int
9118 die_eq (const void *item_lhs, const void *item_rhs)
9119 {
9120 const struct die_info *die_lhs = item_lhs;
9121 const struct die_info *die_rhs = item_rhs;
9122
9123 return die_lhs->offset == die_rhs->offset;
9124 }
9125
9126 /* Read a whole compilation unit into a linked list of dies. */
9127
9128 static struct die_info *
9129 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9130 {
9131 struct die_reader_specs reader_specs;
9132 int read_abbrevs = 0;
9133 struct cleanup *back_to = NULL;
9134 struct die_info *die;
9135
9136 if (cu->dwarf2_abbrevs == NULL)
9137 {
9138 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9139 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9140 read_abbrevs = 1;
9141 }
9142
9143 gdb_assert (cu->die_hash == NULL);
9144 cu->die_hash
9145 = htab_create_alloc_ex (cu->header.length / 12,
9146 die_hash,
9147 die_eq,
9148 NULL,
9149 &cu->comp_unit_obstack,
9150 hashtab_obstack_allocate,
9151 dummy_obstack_deallocate);
9152
9153 init_cu_die_reader (&reader_specs, cu);
9154
9155 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9156
9157 if (read_abbrevs)
9158 do_cleanups (back_to);
9159
9160 return die;
9161 }
9162
9163 /* Main entry point for reading a DIE and all children.
9164 Read the DIE and dump it if requested. */
9165
9166 static struct die_info *
9167 read_die_and_children (const struct die_reader_specs *reader,
9168 gdb_byte *info_ptr,
9169 gdb_byte **new_info_ptr,
9170 struct die_info *parent)
9171 {
9172 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9173 new_info_ptr, parent);
9174
9175 if (dwarf2_die_debug)
9176 {
9177 fprintf_unfiltered (gdb_stdlog,
9178 "\nRead die from %s of %s:\n",
9179 (reader->cu->per_cu->debug_types_section
9180 ? ".debug_types"
9181 : ".debug_info"),
9182 reader->abfd->filename);
9183 dump_die (result, dwarf2_die_debug);
9184 }
9185
9186 return result;
9187 }
9188
9189 /* Read a single die and all its descendents. Set the die's sibling
9190 field to NULL; set other fields in the die correctly, and set all
9191 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9192 location of the info_ptr after reading all of those dies. PARENT
9193 is the parent of the die in question. */
9194
9195 static struct die_info *
9196 read_die_and_children_1 (const struct die_reader_specs *reader,
9197 gdb_byte *info_ptr,
9198 gdb_byte **new_info_ptr,
9199 struct die_info *parent)
9200 {
9201 struct die_info *die;
9202 gdb_byte *cur_ptr;
9203 int has_children;
9204
9205 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9206 if (die == NULL)
9207 {
9208 *new_info_ptr = cur_ptr;
9209 return NULL;
9210 }
9211 store_in_ref_table (die, reader->cu);
9212
9213 if (has_children)
9214 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9215 else
9216 {
9217 die->child = NULL;
9218 *new_info_ptr = cur_ptr;
9219 }
9220
9221 die->sibling = NULL;
9222 die->parent = parent;
9223 return die;
9224 }
9225
9226 /* Read a die, all of its descendents, and all of its siblings; set
9227 all of the fields of all of the dies correctly. Arguments are as
9228 in read_die_and_children. */
9229
9230 static struct die_info *
9231 read_die_and_siblings (const struct die_reader_specs *reader,
9232 gdb_byte *info_ptr,
9233 gdb_byte **new_info_ptr,
9234 struct die_info *parent)
9235 {
9236 struct die_info *first_die, *last_sibling;
9237 gdb_byte *cur_ptr;
9238
9239 cur_ptr = info_ptr;
9240 first_die = last_sibling = NULL;
9241
9242 while (1)
9243 {
9244 struct die_info *die
9245 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9246
9247 if (die == NULL)
9248 {
9249 *new_info_ptr = cur_ptr;
9250 return first_die;
9251 }
9252
9253 if (!first_die)
9254 first_die = die;
9255 else
9256 last_sibling->sibling = die;
9257
9258 last_sibling = die;
9259 }
9260 }
9261
9262 /* Read the die from the .debug_info section buffer. Set DIEP to
9263 point to a newly allocated die with its information, except for its
9264 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9265 whether the die has children or not. */
9266
9267 static gdb_byte *
9268 read_full_die (const struct die_reader_specs *reader,
9269 struct die_info **diep, gdb_byte *info_ptr,
9270 int *has_children)
9271 {
9272 unsigned int abbrev_number, bytes_read, i, offset;
9273 struct abbrev_info *abbrev;
9274 struct die_info *die;
9275 struct dwarf2_cu *cu = reader->cu;
9276 bfd *abfd = reader->abfd;
9277
9278 offset = info_ptr - reader->buffer;
9279 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9280 info_ptr += bytes_read;
9281 if (!abbrev_number)
9282 {
9283 *diep = NULL;
9284 *has_children = 0;
9285 return info_ptr;
9286 }
9287
9288 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9289 if (!abbrev)
9290 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9291 abbrev_number,
9292 bfd_get_filename (abfd));
9293
9294 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9295 die->offset = offset;
9296 die->tag = abbrev->tag;
9297 die->abbrev = abbrev_number;
9298
9299 die->num_attrs = abbrev->num_attrs;
9300
9301 for (i = 0; i < abbrev->num_attrs; ++i)
9302 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9303 abfd, info_ptr, cu);
9304
9305 *diep = die;
9306 *has_children = abbrev->has_children;
9307 return info_ptr;
9308 }
9309
9310 /* In DWARF version 2, the description of the debugging information is
9311 stored in a separate .debug_abbrev section. Before we read any
9312 dies from a section we read in all abbreviations and install them
9313 in a hash table. This function also sets flags in CU describing
9314 the data found in the abbrev table. */
9315
9316 static void
9317 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
9318 {
9319 struct comp_unit_head *cu_header = &cu->header;
9320 gdb_byte *abbrev_ptr;
9321 struct abbrev_info *cur_abbrev;
9322 unsigned int abbrev_number, bytes_read, abbrev_name;
9323 unsigned int abbrev_form, hash_number;
9324 struct attr_abbrev *cur_attrs;
9325 unsigned int allocated_attrs;
9326
9327 /* Initialize dwarf2 abbrevs. */
9328 obstack_init (&cu->abbrev_obstack);
9329 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9330 (ABBREV_HASH_SIZE
9331 * sizeof (struct abbrev_info *)));
9332 memset (cu->dwarf2_abbrevs, 0,
9333 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9334
9335 dwarf2_read_section (dwarf2_per_objfile->objfile,
9336 &dwarf2_per_objfile->abbrev);
9337 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9338 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9339 abbrev_ptr += bytes_read;
9340
9341 allocated_attrs = ATTR_ALLOC_CHUNK;
9342 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9343
9344 /* Loop until we reach an abbrev number of 0. */
9345 while (abbrev_number)
9346 {
9347 cur_abbrev = dwarf_alloc_abbrev (cu);
9348
9349 /* read in abbrev header */
9350 cur_abbrev->number = abbrev_number;
9351 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9352 abbrev_ptr += bytes_read;
9353 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9354 abbrev_ptr += 1;
9355
9356 if (cur_abbrev->tag == DW_TAG_namespace)
9357 cu->has_namespace_info = 1;
9358
9359 /* now read in declarations */
9360 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9361 abbrev_ptr += bytes_read;
9362 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9363 abbrev_ptr += bytes_read;
9364 while (abbrev_name)
9365 {
9366 if (cur_abbrev->num_attrs == allocated_attrs)
9367 {
9368 allocated_attrs += ATTR_ALLOC_CHUNK;
9369 cur_attrs
9370 = xrealloc (cur_attrs, (allocated_attrs
9371 * sizeof (struct attr_abbrev)));
9372 }
9373
9374 /* Record whether this compilation unit might have
9375 inter-compilation-unit references. If we don't know what form
9376 this attribute will have, then it might potentially be a
9377 DW_FORM_ref_addr, so we conservatively expect inter-CU
9378 references. */
9379
9380 if (abbrev_form == DW_FORM_ref_addr
9381 || abbrev_form == DW_FORM_indirect)
9382 cu->has_form_ref_addr = 1;
9383
9384 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9385 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9386 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9387 abbrev_ptr += bytes_read;
9388 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9389 abbrev_ptr += bytes_read;
9390 }
9391
9392 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9393 (cur_abbrev->num_attrs
9394 * sizeof (struct attr_abbrev)));
9395 memcpy (cur_abbrev->attrs, cur_attrs,
9396 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9397
9398 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9399 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9400 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9401
9402 /* Get next abbreviation.
9403 Under Irix6 the abbreviations for a compilation unit are not
9404 always properly terminated with an abbrev number of 0.
9405 Exit loop if we encounter an abbreviation which we have
9406 already read (which means we are about to read the abbreviations
9407 for the next compile unit) or if the end of the abbreviation
9408 table is reached. */
9409 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9410 >= dwarf2_per_objfile->abbrev.size)
9411 break;
9412 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9413 abbrev_ptr += bytes_read;
9414 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9415 break;
9416 }
9417
9418 xfree (cur_attrs);
9419 }
9420
9421 /* Release the memory used by the abbrev table for a compilation unit. */
9422
9423 static void
9424 dwarf2_free_abbrev_table (void *ptr_to_cu)
9425 {
9426 struct dwarf2_cu *cu = ptr_to_cu;
9427
9428 obstack_free (&cu->abbrev_obstack, NULL);
9429 cu->dwarf2_abbrevs = NULL;
9430 }
9431
9432 /* Lookup an abbrev_info structure in the abbrev hash table. */
9433
9434 static struct abbrev_info *
9435 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9436 {
9437 unsigned int hash_number;
9438 struct abbrev_info *abbrev;
9439
9440 hash_number = number % ABBREV_HASH_SIZE;
9441 abbrev = cu->dwarf2_abbrevs[hash_number];
9442
9443 while (abbrev)
9444 {
9445 if (abbrev->number == number)
9446 return abbrev;
9447 else
9448 abbrev = abbrev->next;
9449 }
9450 return NULL;
9451 }
9452
9453 /* Returns nonzero if TAG represents a type that we might generate a partial
9454 symbol for. */
9455
9456 static int
9457 is_type_tag_for_partial (int tag)
9458 {
9459 switch (tag)
9460 {
9461 #if 0
9462 /* Some types that would be reasonable to generate partial symbols for,
9463 that we don't at present. */
9464 case DW_TAG_array_type:
9465 case DW_TAG_file_type:
9466 case DW_TAG_ptr_to_member_type:
9467 case DW_TAG_set_type:
9468 case DW_TAG_string_type:
9469 case DW_TAG_subroutine_type:
9470 #endif
9471 case DW_TAG_base_type:
9472 case DW_TAG_class_type:
9473 case DW_TAG_interface_type:
9474 case DW_TAG_enumeration_type:
9475 case DW_TAG_structure_type:
9476 case DW_TAG_subrange_type:
9477 case DW_TAG_typedef:
9478 case DW_TAG_union_type:
9479 return 1;
9480 default:
9481 return 0;
9482 }
9483 }
9484
9485 /* Load all DIEs that are interesting for partial symbols into memory. */
9486
9487 static struct partial_die_info *
9488 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9489 int building_psymtab, struct dwarf2_cu *cu)
9490 {
9491 struct objfile *objfile = cu->objfile;
9492 struct partial_die_info *part_die;
9493 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9494 struct abbrev_info *abbrev;
9495 unsigned int bytes_read;
9496 unsigned int load_all = 0;
9497
9498 int nesting_level = 1;
9499
9500 parent_die = NULL;
9501 last_die = NULL;
9502
9503 if (cu->per_cu && cu->per_cu->load_all_dies)
9504 load_all = 1;
9505
9506 cu->partial_dies
9507 = htab_create_alloc_ex (cu->header.length / 12,
9508 partial_die_hash,
9509 partial_die_eq,
9510 NULL,
9511 &cu->comp_unit_obstack,
9512 hashtab_obstack_allocate,
9513 dummy_obstack_deallocate);
9514
9515 part_die = obstack_alloc (&cu->comp_unit_obstack,
9516 sizeof (struct partial_die_info));
9517
9518 while (1)
9519 {
9520 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9521
9522 /* A NULL abbrev means the end of a series of children. */
9523 if (abbrev == NULL)
9524 {
9525 if (--nesting_level == 0)
9526 {
9527 /* PART_DIE was probably the last thing allocated on the
9528 comp_unit_obstack, so we could call obstack_free
9529 here. We don't do that because the waste is small,
9530 and will be cleaned up when we're done with this
9531 compilation unit. This way, we're also more robust
9532 against other users of the comp_unit_obstack. */
9533 return first_die;
9534 }
9535 info_ptr += bytes_read;
9536 last_die = parent_die;
9537 parent_die = parent_die->die_parent;
9538 continue;
9539 }
9540
9541 /* Check for template arguments. We never save these; if
9542 they're seen, we just mark the parent, and go on our way. */
9543 if (parent_die != NULL
9544 && cu->language == language_cplus
9545 && (abbrev->tag == DW_TAG_template_type_param
9546 || abbrev->tag == DW_TAG_template_value_param))
9547 {
9548 parent_die->has_template_arguments = 1;
9549
9550 if (!load_all)
9551 {
9552 /* We don't need a partial DIE for the template argument. */
9553 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9554 cu);
9555 continue;
9556 }
9557 }
9558
9559 /* We only recurse into subprograms looking for template arguments.
9560 Skip their other children. */
9561 if (!load_all
9562 && cu->language == language_cplus
9563 && parent_die != NULL
9564 && parent_die->tag == DW_TAG_subprogram)
9565 {
9566 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9567 continue;
9568 }
9569
9570 /* Check whether this DIE is interesting enough to save. Normally
9571 we would not be interested in members here, but there may be
9572 later variables referencing them via DW_AT_specification (for
9573 static members). */
9574 if (!load_all
9575 && !is_type_tag_for_partial (abbrev->tag)
9576 && abbrev->tag != DW_TAG_constant
9577 && abbrev->tag != DW_TAG_enumerator
9578 && abbrev->tag != DW_TAG_subprogram
9579 && abbrev->tag != DW_TAG_lexical_block
9580 && abbrev->tag != DW_TAG_variable
9581 && abbrev->tag != DW_TAG_namespace
9582 && abbrev->tag != DW_TAG_module
9583 && abbrev->tag != DW_TAG_member)
9584 {
9585 /* Otherwise we skip to the next sibling, if any. */
9586 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9587 continue;
9588 }
9589
9590 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9591 buffer, info_ptr, cu);
9592
9593 /* This two-pass algorithm for processing partial symbols has a
9594 high cost in cache pressure. Thus, handle some simple cases
9595 here which cover the majority of C partial symbols. DIEs
9596 which neither have specification tags in them, nor could have
9597 specification tags elsewhere pointing at them, can simply be
9598 processed and discarded.
9599
9600 This segment is also optional; scan_partial_symbols and
9601 add_partial_symbol will handle these DIEs if we chain
9602 them in normally. When compilers which do not emit large
9603 quantities of duplicate debug information are more common,
9604 this code can probably be removed. */
9605
9606 /* Any complete simple types at the top level (pretty much all
9607 of them, for a language without namespaces), can be processed
9608 directly. */
9609 if (parent_die == NULL
9610 && part_die->has_specification == 0
9611 && part_die->is_declaration == 0
9612 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9613 || part_die->tag == DW_TAG_base_type
9614 || part_die->tag == DW_TAG_subrange_type))
9615 {
9616 if (building_psymtab && part_die->name != NULL)
9617 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9618 VAR_DOMAIN, LOC_TYPEDEF,
9619 &objfile->static_psymbols,
9620 0, (CORE_ADDR) 0, cu->language, objfile);
9621 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9622 continue;
9623 }
9624
9625 /* The exception for DW_TAG_typedef with has_children above is
9626 a workaround of GCC PR debug/47510. In the case of this complaint
9627 type_name_no_tag_or_error will error on such types later.
9628
9629 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9630 it could not find the child DIEs referenced later, this is checked
9631 above. In correct DWARF DW_TAG_typedef should have no children. */
9632
9633 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9634 complaint (&symfile_complaints,
9635 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9636 "- DIE at 0x%x [in module %s]"),
9637 part_die->offset, objfile->name);
9638
9639 /* If we're at the second level, and we're an enumerator, and
9640 our parent has no specification (meaning possibly lives in a
9641 namespace elsewhere), then we can add the partial symbol now
9642 instead of queueing it. */
9643 if (part_die->tag == DW_TAG_enumerator
9644 && parent_die != NULL
9645 && parent_die->die_parent == NULL
9646 && parent_die->tag == DW_TAG_enumeration_type
9647 && parent_die->has_specification == 0)
9648 {
9649 if (part_die->name == NULL)
9650 complaint (&symfile_complaints,
9651 _("malformed enumerator DIE ignored"));
9652 else if (building_psymtab)
9653 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9654 VAR_DOMAIN, LOC_CONST,
9655 (cu->language == language_cplus
9656 || cu->language == language_java)
9657 ? &objfile->global_psymbols
9658 : &objfile->static_psymbols,
9659 0, (CORE_ADDR) 0, cu->language, objfile);
9660
9661 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9662 continue;
9663 }
9664
9665 /* We'll save this DIE so link it in. */
9666 part_die->die_parent = parent_die;
9667 part_die->die_sibling = NULL;
9668 part_die->die_child = NULL;
9669
9670 if (last_die && last_die == parent_die)
9671 last_die->die_child = part_die;
9672 else if (last_die)
9673 last_die->die_sibling = part_die;
9674
9675 last_die = part_die;
9676
9677 if (first_die == NULL)
9678 first_die = part_die;
9679
9680 /* Maybe add the DIE to the hash table. Not all DIEs that we
9681 find interesting need to be in the hash table, because we
9682 also have the parent/sibling/child chains; only those that we
9683 might refer to by offset later during partial symbol reading.
9684
9685 For now this means things that might have be the target of a
9686 DW_AT_specification, DW_AT_abstract_origin, or
9687 DW_AT_extension. DW_AT_extension will refer only to
9688 namespaces; DW_AT_abstract_origin refers to functions (and
9689 many things under the function DIE, but we do not recurse
9690 into function DIEs during partial symbol reading) and
9691 possibly variables as well; DW_AT_specification refers to
9692 declarations. Declarations ought to have the DW_AT_declaration
9693 flag. It happens that GCC forgets to put it in sometimes, but
9694 only for functions, not for types.
9695
9696 Adding more things than necessary to the hash table is harmless
9697 except for the performance cost. Adding too few will result in
9698 wasted time in find_partial_die, when we reread the compilation
9699 unit with load_all_dies set. */
9700
9701 if (load_all
9702 || abbrev->tag == DW_TAG_constant
9703 || abbrev->tag == DW_TAG_subprogram
9704 || abbrev->tag == DW_TAG_variable
9705 || abbrev->tag == DW_TAG_namespace
9706 || part_die->is_declaration)
9707 {
9708 void **slot;
9709
9710 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9711 part_die->offset, INSERT);
9712 *slot = part_die;
9713 }
9714
9715 part_die = obstack_alloc (&cu->comp_unit_obstack,
9716 sizeof (struct partial_die_info));
9717
9718 /* For some DIEs we want to follow their children (if any). For C
9719 we have no reason to follow the children of structures; for other
9720 languages we have to, so that we can get at method physnames
9721 to infer fully qualified class names, for DW_AT_specification,
9722 and for C++ template arguments. For C++, we also look one level
9723 inside functions to find template arguments (if the name of the
9724 function does not already contain the template arguments).
9725
9726 For Ada, we need to scan the children of subprograms and lexical
9727 blocks as well because Ada allows the definition of nested
9728 entities that could be interesting for the debugger, such as
9729 nested subprograms for instance. */
9730 if (last_die->has_children
9731 && (load_all
9732 || last_die->tag == DW_TAG_namespace
9733 || last_die->tag == DW_TAG_module
9734 || last_die->tag == DW_TAG_enumeration_type
9735 || (cu->language == language_cplus
9736 && last_die->tag == DW_TAG_subprogram
9737 && (last_die->name == NULL
9738 || strchr (last_die->name, '<') == NULL))
9739 || (cu->language != language_c
9740 && (last_die->tag == DW_TAG_class_type
9741 || last_die->tag == DW_TAG_interface_type
9742 || last_die->tag == DW_TAG_structure_type
9743 || last_die->tag == DW_TAG_union_type))
9744 || (cu->language == language_ada
9745 && (last_die->tag == DW_TAG_subprogram
9746 || last_die->tag == DW_TAG_lexical_block))))
9747 {
9748 nesting_level++;
9749 parent_die = last_die;
9750 continue;
9751 }
9752
9753 /* Otherwise we skip to the next sibling, if any. */
9754 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9755
9756 /* Back to the top, do it again. */
9757 }
9758 }
9759
9760 /* Read a minimal amount of information into the minimal die structure. */
9761
9762 static gdb_byte *
9763 read_partial_die (struct partial_die_info *part_die,
9764 struct abbrev_info *abbrev,
9765 unsigned int abbrev_len, bfd *abfd,
9766 gdb_byte *buffer, gdb_byte *info_ptr,
9767 struct dwarf2_cu *cu)
9768 {
9769 struct objfile *objfile = cu->objfile;
9770 unsigned int i;
9771 struct attribute attr;
9772 int has_low_pc_attr = 0;
9773 int has_high_pc_attr = 0;
9774
9775 memset (part_die, 0, sizeof (struct partial_die_info));
9776
9777 part_die->offset = info_ptr - buffer;
9778
9779 info_ptr += abbrev_len;
9780
9781 if (abbrev == NULL)
9782 return info_ptr;
9783
9784 part_die->tag = abbrev->tag;
9785 part_die->has_children = abbrev->has_children;
9786
9787 for (i = 0; i < abbrev->num_attrs; ++i)
9788 {
9789 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9790
9791 /* Store the data if it is of an attribute we want to keep in a
9792 partial symbol table. */
9793 switch (attr.name)
9794 {
9795 case DW_AT_name:
9796 switch (part_die->tag)
9797 {
9798 case DW_TAG_compile_unit:
9799 case DW_TAG_type_unit:
9800 /* Compilation units have a DW_AT_name that is a filename, not
9801 a source language identifier. */
9802 case DW_TAG_enumeration_type:
9803 case DW_TAG_enumerator:
9804 /* These tags always have simple identifiers already; no need
9805 to canonicalize them. */
9806 part_die->name = DW_STRING (&attr);
9807 break;
9808 default:
9809 part_die->name
9810 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9811 &objfile->objfile_obstack);
9812 break;
9813 }
9814 break;
9815 case DW_AT_linkage_name:
9816 case DW_AT_MIPS_linkage_name:
9817 /* Note that both forms of linkage name might appear. We
9818 assume they will be the same, and we only store the last
9819 one we see. */
9820 if (cu->language == language_ada)
9821 part_die->name = DW_STRING (&attr);
9822 part_die->linkage_name = DW_STRING (&attr);
9823 break;
9824 case DW_AT_low_pc:
9825 has_low_pc_attr = 1;
9826 part_die->lowpc = DW_ADDR (&attr);
9827 break;
9828 case DW_AT_high_pc:
9829 has_high_pc_attr = 1;
9830 part_die->highpc = DW_ADDR (&attr);
9831 break;
9832 case DW_AT_location:
9833 /* Support the .debug_loc offsets. */
9834 if (attr_form_is_block (&attr))
9835 {
9836 part_die->locdesc = DW_BLOCK (&attr);
9837 }
9838 else if (attr_form_is_section_offset (&attr))
9839 {
9840 dwarf2_complex_location_expr_complaint ();
9841 }
9842 else
9843 {
9844 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9845 "partial symbol information");
9846 }
9847 break;
9848 case DW_AT_external:
9849 part_die->is_external = DW_UNSND (&attr);
9850 break;
9851 case DW_AT_declaration:
9852 part_die->is_declaration = DW_UNSND (&attr);
9853 break;
9854 case DW_AT_type:
9855 part_die->has_type = 1;
9856 break;
9857 case DW_AT_abstract_origin:
9858 case DW_AT_specification:
9859 case DW_AT_extension:
9860 part_die->has_specification = 1;
9861 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9862 break;
9863 case DW_AT_sibling:
9864 /* Ignore absolute siblings, they might point outside of
9865 the current compile unit. */
9866 if (attr.form == DW_FORM_ref_addr)
9867 complaint (&symfile_complaints,
9868 _("ignoring absolute DW_AT_sibling"));
9869 else
9870 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9871 break;
9872 case DW_AT_byte_size:
9873 part_die->has_byte_size = 1;
9874 break;
9875 case DW_AT_calling_convention:
9876 /* DWARF doesn't provide a way to identify a program's source-level
9877 entry point. DW_AT_calling_convention attributes are only meant
9878 to describe functions' calling conventions.
9879
9880 However, because it's a necessary piece of information in
9881 Fortran, and because DW_CC_program is the only piece of debugging
9882 information whose definition refers to a 'main program' at all,
9883 several compilers have begun marking Fortran main programs with
9884 DW_CC_program --- even when those functions use the standard
9885 calling conventions.
9886
9887 So until DWARF specifies a way to provide this information and
9888 compilers pick up the new representation, we'll support this
9889 practice. */
9890 if (DW_UNSND (&attr) == DW_CC_program
9891 && cu->language == language_fortran)
9892 {
9893 set_main_name (part_die->name);
9894
9895 /* As this DIE has a static linkage the name would be difficult
9896 to look up later. */
9897 language_of_main = language_fortran;
9898 }
9899 break;
9900 default:
9901 break;
9902 }
9903 }
9904
9905 if (has_low_pc_attr && has_high_pc_attr)
9906 {
9907 /* When using the GNU linker, .gnu.linkonce. sections are used to
9908 eliminate duplicate copies of functions and vtables and such.
9909 The linker will arbitrarily choose one and discard the others.
9910 The AT_*_pc values for such functions refer to local labels in
9911 these sections. If the section from that file was discarded, the
9912 labels are not in the output, so the relocs get a value of 0.
9913 If this is a discarded function, mark the pc bounds as invalid,
9914 so that GDB will ignore it. */
9915 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9916 {
9917 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9918
9919 complaint (&symfile_complaints,
9920 _("DW_AT_low_pc %s is zero "
9921 "for DIE at 0x%x [in module %s]"),
9922 paddress (gdbarch, part_die->lowpc),
9923 part_die->offset, objfile->name);
9924 }
9925 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9926 else if (part_die->lowpc >= part_die->highpc)
9927 {
9928 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9929
9930 complaint (&symfile_complaints,
9931 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9932 "for DIE at 0x%x [in module %s]"),
9933 paddress (gdbarch, part_die->lowpc),
9934 paddress (gdbarch, part_die->highpc),
9935 part_die->offset, objfile->name);
9936 }
9937 else
9938 part_die->has_pc_info = 1;
9939 }
9940
9941 return info_ptr;
9942 }
9943
9944 /* Find a cached partial DIE at OFFSET in CU. */
9945
9946 static struct partial_die_info *
9947 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9948 {
9949 struct partial_die_info *lookup_die = NULL;
9950 struct partial_die_info part_die;
9951
9952 part_die.offset = offset;
9953 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9954
9955 return lookup_die;
9956 }
9957
9958 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9959 except in the case of .debug_types DIEs which do not reference
9960 outside their CU (they do however referencing other types via
9961 DW_FORM_ref_sig8). */
9962
9963 static struct partial_die_info *
9964 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9965 {
9966 struct objfile *objfile = cu->objfile;
9967 struct dwarf2_per_cu_data *per_cu = NULL;
9968 struct partial_die_info *pd = NULL;
9969
9970 if (cu->per_cu->debug_types_section)
9971 {
9972 pd = find_partial_die_in_comp_unit (offset, cu);
9973 if (pd != NULL)
9974 return pd;
9975 goto not_found;
9976 }
9977
9978 if (offset_in_cu_p (&cu->header, offset))
9979 {
9980 pd = find_partial_die_in_comp_unit (offset, cu);
9981 if (pd != NULL)
9982 return pd;
9983 }
9984
9985 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9986
9987 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9988 load_partial_comp_unit (per_cu);
9989
9990 per_cu->cu->last_used = 0;
9991 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9992
9993 if (pd == NULL && per_cu->load_all_dies == 0)
9994 {
9995 struct cleanup *back_to;
9996 struct partial_die_info comp_unit_die;
9997 struct abbrev_info *abbrev;
9998 unsigned int bytes_read;
9999 char *info_ptr;
10000
10001 per_cu->load_all_dies = 1;
10002
10003 /* Re-read the DIEs. */
10004 back_to = make_cleanup (null_cleanup, 0);
10005 if (per_cu->cu->dwarf2_abbrevs == NULL)
10006 {
10007 dwarf2_read_abbrevs (objfile->obfd, per_cu->cu);
10008 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10009 }
10010 info_ptr = (dwarf2_per_objfile->info.buffer
10011 + per_cu->cu->header.offset
10012 + per_cu->cu->header.first_die_offset);
10013 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10014 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10015 objfile->obfd,
10016 dwarf2_per_objfile->info.buffer, info_ptr,
10017 per_cu->cu);
10018 if (comp_unit_die.has_children)
10019 load_partial_dies (objfile->obfd,
10020 dwarf2_per_objfile->info.buffer, info_ptr,
10021 0, per_cu->cu);
10022 do_cleanups (back_to);
10023
10024 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10025 }
10026
10027 not_found:
10028
10029 if (pd == NULL)
10030 internal_error (__FILE__, __LINE__,
10031 _("could not find partial DIE 0x%x "
10032 "in cache [from module %s]\n"),
10033 offset, bfd_get_filename (objfile->obfd));
10034 return pd;
10035 }
10036
10037 /* See if we can figure out if the class lives in a namespace. We do
10038 this by looking for a member function; its demangled name will
10039 contain namespace info, if there is any. */
10040
10041 static void
10042 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10043 struct dwarf2_cu *cu)
10044 {
10045 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10046 what template types look like, because the demangler
10047 frequently doesn't give the same name as the debug info. We
10048 could fix this by only using the demangled name to get the
10049 prefix (but see comment in read_structure_type). */
10050
10051 struct partial_die_info *real_pdi;
10052 struct partial_die_info *child_pdi;
10053
10054 /* If this DIE (this DIE's specification, if any) has a parent, then
10055 we should not do this. We'll prepend the parent's fully qualified
10056 name when we create the partial symbol. */
10057
10058 real_pdi = struct_pdi;
10059 while (real_pdi->has_specification)
10060 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10061
10062 if (real_pdi->die_parent != NULL)
10063 return;
10064
10065 for (child_pdi = struct_pdi->die_child;
10066 child_pdi != NULL;
10067 child_pdi = child_pdi->die_sibling)
10068 {
10069 if (child_pdi->tag == DW_TAG_subprogram
10070 && child_pdi->linkage_name != NULL)
10071 {
10072 char *actual_class_name
10073 = language_class_name_from_physname (cu->language_defn,
10074 child_pdi->linkage_name);
10075 if (actual_class_name != NULL)
10076 {
10077 struct_pdi->name
10078 = obsavestring (actual_class_name,
10079 strlen (actual_class_name),
10080 &cu->objfile->objfile_obstack);
10081 xfree (actual_class_name);
10082 }
10083 break;
10084 }
10085 }
10086 }
10087
10088 /* Adjust PART_DIE before generating a symbol for it. This function
10089 may set the is_external flag or change the DIE's name. */
10090
10091 static void
10092 fixup_partial_die (struct partial_die_info *part_die,
10093 struct dwarf2_cu *cu)
10094 {
10095 /* Once we've fixed up a die, there's no point in doing so again.
10096 This also avoids a memory leak if we were to call
10097 guess_partial_die_structure_name multiple times. */
10098 if (part_die->fixup_called)
10099 return;
10100
10101 /* If we found a reference attribute and the DIE has no name, try
10102 to find a name in the referred to DIE. */
10103
10104 if (part_die->name == NULL && part_die->has_specification)
10105 {
10106 struct partial_die_info *spec_die;
10107
10108 spec_die = find_partial_die (part_die->spec_offset, cu);
10109
10110 fixup_partial_die (spec_die, cu);
10111
10112 if (spec_die->name)
10113 {
10114 part_die->name = spec_die->name;
10115
10116 /* Copy DW_AT_external attribute if it is set. */
10117 if (spec_die->is_external)
10118 part_die->is_external = spec_die->is_external;
10119 }
10120 }
10121
10122 /* Set default names for some unnamed DIEs. */
10123
10124 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10125 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10126
10127 /* If there is no parent die to provide a namespace, and there are
10128 children, see if we can determine the namespace from their linkage
10129 name.
10130 NOTE: We need to do this even if cu->has_namespace_info != 0.
10131 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10132 if (cu->language == language_cplus
10133 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10134 && part_die->die_parent == NULL
10135 && part_die->has_children
10136 && (part_die->tag == DW_TAG_class_type
10137 || part_die->tag == DW_TAG_structure_type
10138 || part_die->tag == DW_TAG_union_type))
10139 guess_partial_die_structure_name (part_die, cu);
10140
10141 /* GCC might emit a nameless struct or union that has a linkage
10142 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10143 if (part_die->name == NULL
10144 && (part_die->tag == DW_TAG_class_type
10145 || part_die->tag == DW_TAG_interface_type
10146 || part_die->tag == DW_TAG_structure_type
10147 || part_die->tag == DW_TAG_union_type)
10148 && part_die->linkage_name != NULL)
10149 {
10150 char *demangled;
10151
10152 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10153 if (demangled)
10154 {
10155 const char *base;
10156
10157 /* Strip any leading namespaces/classes, keep only the base name.
10158 DW_AT_name for named DIEs does not contain the prefixes. */
10159 base = strrchr (demangled, ':');
10160 if (base && base > demangled && base[-1] == ':')
10161 base++;
10162 else
10163 base = demangled;
10164
10165 part_die->name = obsavestring (base, strlen (base),
10166 &cu->objfile->objfile_obstack);
10167 xfree (demangled);
10168 }
10169 }
10170
10171 part_die->fixup_called = 1;
10172 }
10173
10174 /* Read an attribute value described by an attribute form. */
10175
10176 static gdb_byte *
10177 read_attribute_value (struct attribute *attr, unsigned form,
10178 bfd *abfd, gdb_byte *info_ptr,
10179 struct dwarf2_cu *cu)
10180 {
10181 struct comp_unit_head *cu_header = &cu->header;
10182 unsigned int bytes_read;
10183 struct dwarf_block *blk;
10184
10185 attr->form = form;
10186 switch (form)
10187 {
10188 case DW_FORM_ref_addr:
10189 if (cu->header.version == 2)
10190 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10191 else
10192 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10193 &cu->header, &bytes_read);
10194 info_ptr += bytes_read;
10195 break;
10196 case DW_FORM_addr:
10197 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10198 info_ptr += bytes_read;
10199 break;
10200 case DW_FORM_block2:
10201 blk = dwarf_alloc_block (cu);
10202 blk->size = read_2_bytes (abfd, info_ptr);
10203 info_ptr += 2;
10204 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10205 info_ptr += blk->size;
10206 DW_BLOCK (attr) = blk;
10207 break;
10208 case DW_FORM_block4:
10209 blk = dwarf_alloc_block (cu);
10210 blk->size = read_4_bytes (abfd, info_ptr);
10211 info_ptr += 4;
10212 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10213 info_ptr += blk->size;
10214 DW_BLOCK (attr) = blk;
10215 break;
10216 case DW_FORM_data2:
10217 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10218 info_ptr += 2;
10219 break;
10220 case DW_FORM_data4:
10221 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10222 info_ptr += 4;
10223 break;
10224 case DW_FORM_data8:
10225 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10226 info_ptr += 8;
10227 break;
10228 case DW_FORM_sec_offset:
10229 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10230 info_ptr += bytes_read;
10231 break;
10232 case DW_FORM_string:
10233 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10234 DW_STRING_IS_CANONICAL (attr) = 0;
10235 info_ptr += bytes_read;
10236 break;
10237 case DW_FORM_strp:
10238 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10239 &bytes_read);
10240 DW_STRING_IS_CANONICAL (attr) = 0;
10241 info_ptr += bytes_read;
10242 break;
10243 case DW_FORM_exprloc:
10244 case DW_FORM_block:
10245 blk = dwarf_alloc_block (cu);
10246 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10247 info_ptr += bytes_read;
10248 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10249 info_ptr += blk->size;
10250 DW_BLOCK (attr) = blk;
10251 break;
10252 case DW_FORM_block1:
10253 blk = dwarf_alloc_block (cu);
10254 blk->size = read_1_byte (abfd, info_ptr);
10255 info_ptr += 1;
10256 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10257 info_ptr += blk->size;
10258 DW_BLOCK (attr) = blk;
10259 break;
10260 case DW_FORM_data1:
10261 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10262 info_ptr += 1;
10263 break;
10264 case DW_FORM_flag:
10265 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10266 info_ptr += 1;
10267 break;
10268 case DW_FORM_flag_present:
10269 DW_UNSND (attr) = 1;
10270 break;
10271 case DW_FORM_sdata:
10272 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10273 info_ptr += bytes_read;
10274 break;
10275 case DW_FORM_udata:
10276 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10277 info_ptr += bytes_read;
10278 break;
10279 case DW_FORM_ref1:
10280 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10281 info_ptr += 1;
10282 break;
10283 case DW_FORM_ref2:
10284 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10285 info_ptr += 2;
10286 break;
10287 case DW_FORM_ref4:
10288 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10289 info_ptr += 4;
10290 break;
10291 case DW_FORM_ref8:
10292 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10293 info_ptr += 8;
10294 break;
10295 case DW_FORM_ref_sig8:
10296 /* Convert the signature to something we can record in DW_UNSND
10297 for later lookup.
10298 NOTE: This is NULL if the type wasn't found. */
10299 DW_SIGNATURED_TYPE (attr) =
10300 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10301 info_ptr += 8;
10302 break;
10303 case DW_FORM_ref_udata:
10304 DW_ADDR (attr) = (cu->header.offset
10305 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10306 info_ptr += bytes_read;
10307 break;
10308 case DW_FORM_indirect:
10309 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10310 info_ptr += bytes_read;
10311 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10312 break;
10313 default:
10314 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10315 dwarf_form_name (form),
10316 bfd_get_filename (abfd));
10317 }
10318
10319 /* We have seen instances where the compiler tried to emit a byte
10320 size attribute of -1 which ended up being encoded as an unsigned
10321 0xffffffff. Although 0xffffffff is technically a valid size value,
10322 an object of this size seems pretty unlikely so we can relatively
10323 safely treat these cases as if the size attribute was invalid and
10324 treat them as zero by default. */
10325 if (attr->name == DW_AT_byte_size
10326 && form == DW_FORM_data4
10327 && DW_UNSND (attr) >= 0xffffffff)
10328 {
10329 complaint
10330 (&symfile_complaints,
10331 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10332 hex_string (DW_UNSND (attr)));
10333 DW_UNSND (attr) = 0;
10334 }
10335
10336 return info_ptr;
10337 }
10338
10339 /* Read an attribute described by an abbreviated attribute. */
10340
10341 static gdb_byte *
10342 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10343 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10344 {
10345 attr->name = abbrev->name;
10346 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10347 }
10348
10349 /* Read dwarf information from a buffer. */
10350
10351 static unsigned int
10352 read_1_byte (bfd *abfd, gdb_byte *buf)
10353 {
10354 return bfd_get_8 (abfd, buf);
10355 }
10356
10357 static int
10358 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10359 {
10360 return bfd_get_signed_8 (abfd, buf);
10361 }
10362
10363 static unsigned int
10364 read_2_bytes (bfd *abfd, gdb_byte *buf)
10365 {
10366 return bfd_get_16 (abfd, buf);
10367 }
10368
10369 static int
10370 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10371 {
10372 return bfd_get_signed_16 (abfd, buf);
10373 }
10374
10375 static unsigned int
10376 read_4_bytes (bfd *abfd, gdb_byte *buf)
10377 {
10378 return bfd_get_32 (abfd, buf);
10379 }
10380
10381 static int
10382 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10383 {
10384 return bfd_get_signed_32 (abfd, buf);
10385 }
10386
10387 static ULONGEST
10388 read_8_bytes (bfd *abfd, gdb_byte *buf)
10389 {
10390 return bfd_get_64 (abfd, buf);
10391 }
10392
10393 static CORE_ADDR
10394 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10395 unsigned int *bytes_read)
10396 {
10397 struct comp_unit_head *cu_header = &cu->header;
10398 CORE_ADDR retval = 0;
10399
10400 if (cu_header->signed_addr_p)
10401 {
10402 switch (cu_header->addr_size)
10403 {
10404 case 2:
10405 retval = bfd_get_signed_16 (abfd, buf);
10406 break;
10407 case 4:
10408 retval = bfd_get_signed_32 (abfd, buf);
10409 break;
10410 case 8:
10411 retval = bfd_get_signed_64 (abfd, buf);
10412 break;
10413 default:
10414 internal_error (__FILE__, __LINE__,
10415 _("read_address: bad switch, signed [in module %s]"),
10416 bfd_get_filename (abfd));
10417 }
10418 }
10419 else
10420 {
10421 switch (cu_header->addr_size)
10422 {
10423 case 2:
10424 retval = bfd_get_16 (abfd, buf);
10425 break;
10426 case 4:
10427 retval = bfd_get_32 (abfd, buf);
10428 break;
10429 case 8:
10430 retval = bfd_get_64 (abfd, buf);
10431 break;
10432 default:
10433 internal_error (__FILE__, __LINE__,
10434 _("read_address: bad switch, "
10435 "unsigned [in module %s]"),
10436 bfd_get_filename (abfd));
10437 }
10438 }
10439
10440 *bytes_read = cu_header->addr_size;
10441 return retval;
10442 }
10443
10444 /* Read the initial length from a section. The (draft) DWARF 3
10445 specification allows the initial length to take up either 4 bytes
10446 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10447 bytes describe the length and all offsets will be 8 bytes in length
10448 instead of 4.
10449
10450 An older, non-standard 64-bit format is also handled by this
10451 function. The older format in question stores the initial length
10452 as an 8-byte quantity without an escape value. Lengths greater
10453 than 2^32 aren't very common which means that the initial 4 bytes
10454 is almost always zero. Since a length value of zero doesn't make
10455 sense for the 32-bit format, this initial zero can be considered to
10456 be an escape value which indicates the presence of the older 64-bit
10457 format. As written, the code can't detect (old format) lengths
10458 greater than 4GB. If it becomes necessary to handle lengths
10459 somewhat larger than 4GB, we could allow other small values (such
10460 as the non-sensical values of 1, 2, and 3) to also be used as
10461 escape values indicating the presence of the old format.
10462
10463 The value returned via bytes_read should be used to increment the
10464 relevant pointer after calling read_initial_length().
10465
10466 [ Note: read_initial_length() and read_offset() are based on the
10467 document entitled "DWARF Debugging Information Format", revision
10468 3, draft 8, dated November 19, 2001. This document was obtained
10469 from:
10470
10471 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10472
10473 This document is only a draft and is subject to change. (So beware.)
10474
10475 Details regarding the older, non-standard 64-bit format were
10476 determined empirically by examining 64-bit ELF files produced by
10477 the SGI toolchain on an IRIX 6.5 machine.
10478
10479 - Kevin, July 16, 2002
10480 ] */
10481
10482 static LONGEST
10483 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10484 {
10485 LONGEST length = bfd_get_32 (abfd, buf);
10486
10487 if (length == 0xffffffff)
10488 {
10489 length = bfd_get_64 (abfd, buf + 4);
10490 *bytes_read = 12;
10491 }
10492 else if (length == 0)
10493 {
10494 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10495 length = bfd_get_64 (abfd, buf);
10496 *bytes_read = 8;
10497 }
10498 else
10499 {
10500 *bytes_read = 4;
10501 }
10502
10503 return length;
10504 }
10505
10506 /* Cover function for read_initial_length.
10507 Returns the length of the object at BUF, and stores the size of the
10508 initial length in *BYTES_READ and stores the size that offsets will be in
10509 *OFFSET_SIZE.
10510 If the initial length size is not equivalent to that specified in
10511 CU_HEADER then issue a complaint.
10512 This is useful when reading non-comp-unit headers. */
10513
10514 static LONGEST
10515 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10516 const struct comp_unit_head *cu_header,
10517 unsigned int *bytes_read,
10518 unsigned int *offset_size)
10519 {
10520 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10521
10522 gdb_assert (cu_header->initial_length_size == 4
10523 || cu_header->initial_length_size == 8
10524 || cu_header->initial_length_size == 12);
10525
10526 if (cu_header->initial_length_size != *bytes_read)
10527 complaint (&symfile_complaints,
10528 _("intermixed 32-bit and 64-bit DWARF sections"));
10529
10530 *offset_size = (*bytes_read == 4) ? 4 : 8;
10531 return length;
10532 }
10533
10534 /* Read an offset from the data stream. The size of the offset is
10535 given by cu_header->offset_size. */
10536
10537 static LONGEST
10538 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10539 unsigned int *bytes_read)
10540 {
10541 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10542
10543 *bytes_read = cu_header->offset_size;
10544 return offset;
10545 }
10546
10547 /* Read an offset from the data stream. */
10548
10549 static LONGEST
10550 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10551 {
10552 LONGEST retval = 0;
10553
10554 switch (offset_size)
10555 {
10556 case 4:
10557 retval = bfd_get_32 (abfd, buf);
10558 break;
10559 case 8:
10560 retval = bfd_get_64 (abfd, buf);
10561 break;
10562 default:
10563 internal_error (__FILE__, __LINE__,
10564 _("read_offset_1: bad switch [in module %s]"),
10565 bfd_get_filename (abfd));
10566 }
10567
10568 return retval;
10569 }
10570
10571 static gdb_byte *
10572 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10573 {
10574 /* If the size of a host char is 8 bits, we can return a pointer
10575 to the buffer, otherwise we have to copy the data to a buffer
10576 allocated on the temporary obstack. */
10577 gdb_assert (HOST_CHAR_BIT == 8);
10578 return buf;
10579 }
10580
10581 static char *
10582 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10583 {
10584 /* If the size of a host char is 8 bits, we can return a pointer
10585 to the string, otherwise we have to copy the string to a buffer
10586 allocated on the temporary obstack. */
10587 gdb_assert (HOST_CHAR_BIT == 8);
10588 if (*buf == '\0')
10589 {
10590 *bytes_read_ptr = 1;
10591 return NULL;
10592 }
10593 *bytes_read_ptr = strlen ((char *) buf) + 1;
10594 return (char *) buf;
10595 }
10596
10597 static char *
10598 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10599 {
10600 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10601 if (dwarf2_per_objfile->str.buffer == NULL)
10602 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10603 bfd_get_filename (abfd));
10604 if (str_offset >= dwarf2_per_objfile->str.size)
10605 error (_("DW_FORM_strp pointing outside of "
10606 ".debug_str section [in module %s]"),
10607 bfd_get_filename (abfd));
10608 gdb_assert (HOST_CHAR_BIT == 8);
10609 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10610 return NULL;
10611 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10612 }
10613
10614 static char *
10615 read_indirect_string (bfd *abfd, gdb_byte *buf,
10616 const struct comp_unit_head *cu_header,
10617 unsigned int *bytes_read_ptr)
10618 {
10619 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10620
10621 return read_indirect_string_at_offset (abfd, str_offset);
10622 }
10623
10624 static unsigned long
10625 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10626 {
10627 unsigned long result;
10628 unsigned int num_read;
10629 int i, shift;
10630 unsigned char byte;
10631
10632 result = 0;
10633 shift = 0;
10634 num_read = 0;
10635 i = 0;
10636 while (1)
10637 {
10638 byte = bfd_get_8 (abfd, buf);
10639 buf++;
10640 num_read++;
10641 result |= ((unsigned long)(byte & 127) << shift);
10642 if ((byte & 128) == 0)
10643 {
10644 break;
10645 }
10646 shift += 7;
10647 }
10648 *bytes_read_ptr = num_read;
10649 return result;
10650 }
10651
10652 static long
10653 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10654 {
10655 long result;
10656 int i, shift, num_read;
10657 unsigned char byte;
10658
10659 result = 0;
10660 shift = 0;
10661 num_read = 0;
10662 i = 0;
10663 while (1)
10664 {
10665 byte = bfd_get_8 (abfd, buf);
10666 buf++;
10667 num_read++;
10668 result |= ((long)(byte & 127) << shift);
10669 shift += 7;
10670 if ((byte & 128) == 0)
10671 {
10672 break;
10673 }
10674 }
10675 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10676 result |= -(((long)1) << shift);
10677 *bytes_read_ptr = num_read;
10678 return result;
10679 }
10680
10681 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10682
10683 static gdb_byte *
10684 skip_leb128 (bfd *abfd, gdb_byte *buf)
10685 {
10686 int byte;
10687
10688 while (1)
10689 {
10690 byte = bfd_get_8 (abfd, buf);
10691 buf++;
10692 if ((byte & 128) == 0)
10693 return buf;
10694 }
10695 }
10696
10697 static void
10698 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10699 {
10700 switch (lang)
10701 {
10702 case DW_LANG_C89:
10703 case DW_LANG_C99:
10704 case DW_LANG_C:
10705 cu->language = language_c;
10706 break;
10707 case DW_LANG_C_plus_plus:
10708 cu->language = language_cplus;
10709 break;
10710 case DW_LANG_D:
10711 cu->language = language_d;
10712 break;
10713 case DW_LANG_Fortran77:
10714 case DW_LANG_Fortran90:
10715 case DW_LANG_Fortran95:
10716 cu->language = language_fortran;
10717 break;
10718 case DW_LANG_Mips_Assembler:
10719 cu->language = language_asm;
10720 break;
10721 case DW_LANG_Java:
10722 cu->language = language_java;
10723 break;
10724 case DW_LANG_Ada83:
10725 case DW_LANG_Ada95:
10726 cu->language = language_ada;
10727 break;
10728 case DW_LANG_Modula2:
10729 cu->language = language_m2;
10730 break;
10731 case DW_LANG_Pascal83:
10732 cu->language = language_pascal;
10733 break;
10734 case DW_LANG_ObjC:
10735 cu->language = language_objc;
10736 break;
10737 case DW_LANG_Cobol74:
10738 case DW_LANG_Cobol85:
10739 default:
10740 cu->language = language_minimal;
10741 break;
10742 }
10743 cu->language_defn = language_def (cu->language);
10744 }
10745
10746 /* Return the named attribute or NULL if not there. */
10747
10748 static struct attribute *
10749 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10750 {
10751 unsigned int i;
10752 struct attribute *spec = NULL;
10753
10754 for (i = 0; i < die->num_attrs; ++i)
10755 {
10756 if (die->attrs[i].name == name)
10757 return &die->attrs[i];
10758 if (die->attrs[i].name == DW_AT_specification
10759 || die->attrs[i].name == DW_AT_abstract_origin)
10760 spec = &die->attrs[i];
10761 }
10762
10763 if (spec)
10764 {
10765 die = follow_die_ref (die, spec, &cu);
10766 return dwarf2_attr (die, name, cu);
10767 }
10768
10769 return NULL;
10770 }
10771
10772 /* Return the named attribute or NULL if not there,
10773 but do not follow DW_AT_specification, etc.
10774 This is for use in contexts where we're reading .debug_types dies.
10775 Following DW_AT_specification, DW_AT_abstract_origin will take us
10776 back up the chain, and we want to go down. */
10777
10778 static struct attribute *
10779 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10780 struct dwarf2_cu *cu)
10781 {
10782 unsigned int i;
10783
10784 for (i = 0; i < die->num_attrs; ++i)
10785 if (die->attrs[i].name == name)
10786 return &die->attrs[i];
10787
10788 return NULL;
10789 }
10790
10791 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10792 and holds a non-zero value. This function should only be used for
10793 DW_FORM_flag or DW_FORM_flag_present attributes. */
10794
10795 static int
10796 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10797 {
10798 struct attribute *attr = dwarf2_attr (die, name, cu);
10799
10800 return (attr && DW_UNSND (attr));
10801 }
10802
10803 static int
10804 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10805 {
10806 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10807 which value is non-zero. However, we have to be careful with
10808 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10809 (via dwarf2_flag_true_p) follows this attribute. So we may
10810 end up accidently finding a declaration attribute that belongs
10811 to a different DIE referenced by the specification attribute,
10812 even though the given DIE does not have a declaration attribute. */
10813 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10814 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10815 }
10816
10817 /* Return the die giving the specification for DIE, if there is
10818 one. *SPEC_CU is the CU containing DIE on input, and the CU
10819 containing the return value on output. If there is no
10820 specification, but there is an abstract origin, that is
10821 returned. */
10822
10823 static struct die_info *
10824 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10825 {
10826 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10827 *spec_cu);
10828
10829 if (spec_attr == NULL)
10830 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10831
10832 if (spec_attr == NULL)
10833 return NULL;
10834 else
10835 return follow_die_ref (die, spec_attr, spec_cu);
10836 }
10837
10838 /* Free the line_header structure *LH, and any arrays and strings it
10839 refers to.
10840 NOTE: This is also used as a "cleanup" function. */
10841
10842 static void
10843 free_line_header (struct line_header *lh)
10844 {
10845 if (lh->standard_opcode_lengths)
10846 xfree (lh->standard_opcode_lengths);
10847
10848 /* Remember that all the lh->file_names[i].name pointers are
10849 pointers into debug_line_buffer, and don't need to be freed. */
10850 if (lh->file_names)
10851 xfree (lh->file_names);
10852
10853 /* Similarly for the include directory names. */
10854 if (lh->include_dirs)
10855 xfree (lh->include_dirs);
10856
10857 xfree (lh);
10858 }
10859
10860 /* Add an entry to LH's include directory table. */
10861
10862 static void
10863 add_include_dir (struct line_header *lh, char *include_dir)
10864 {
10865 /* Grow the array if necessary. */
10866 if (lh->include_dirs_size == 0)
10867 {
10868 lh->include_dirs_size = 1; /* for testing */
10869 lh->include_dirs = xmalloc (lh->include_dirs_size
10870 * sizeof (*lh->include_dirs));
10871 }
10872 else if (lh->num_include_dirs >= lh->include_dirs_size)
10873 {
10874 lh->include_dirs_size *= 2;
10875 lh->include_dirs = xrealloc (lh->include_dirs,
10876 (lh->include_dirs_size
10877 * sizeof (*lh->include_dirs)));
10878 }
10879
10880 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10881 }
10882
10883 /* Add an entry to LH's file name table. */
10884
10885 static void
10886 add_file_name (struct line_header *lh,
10887 char *name,
10888 unsigned int dir_index,
10889 unsigned int mod_time,
10890 unsigned int length)
10891 {
10892 struct file_entry *fe;
10893
10894 /* Grow the array if necessary. */
10895 if (lh->file_names_size == 0)
10896 {
10897 lh->file_names_size = 1; /* for testing */
10898 lh->file_names = xmalloc (lh->file_names_size
10899 * sizeof (*lh->file_names));
10900 }
10901 else if (lh->num_file_names >= lh->file_names_size)
10902 {
10903 lh->file_names_size *= 2;
10904 lh->file_names = xrealloc (lh->file_names,
10905 (lh->file_names_size
10906 * sizeof (*lh->file_names)));
10907 }
10908
10909 fe = &lh->file_names[lh->num_file_names++];
10910 fe->name = name;
10911 fe->dir_index = dir_index;
10912 fe->mod_time = mod_time;
10913 fe->length = length;
10914 fe->included_p = 0;
10915 fe->symtab = NULL;
10916 }
10917
10918 /* Read the statement program header starting at OFFSET in
10919 .debug_line, according to the endianness of ABFD. Return a pointer
10920 to a struct line_header, allocated using xmalloc.
10921
10922 NOTE: the strings in the include directory and file name tables of
10923 the returned object point into debug_line_buffer, and must not be
10924 freed. */
10925
10926 static struct line_header *
10927 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10928 struct dwarf2_cu *cu)
10929 {
10930 struct cleanup *back_to;
10931 struct line_header *lh;
10932 gdb_byte *line_ptr;
10933 unsigned int bytes_read, offset_size;
10934 int i;
10935 char *cur_dir, *cur_file;
10936
10937 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10938 if (dwarf2_per_objfile->line.buffer == NULL)
10939 {
10940 complaint (&symfile_complaints, _("missing .debug_line section"));
10941 return 0;
10942 }
10943
10944 /* Make sure that at least there's room for the total_length field.
10945 That could be 12 bytes long, but we're just going to fudge that. */
10946 if (offset + 4 >= dwarf2_per_objfile->line.size)
10947 {
10948 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10949 return 0;
10950 }
10951
10952 lh = xmalloc (sizeof (*lh));
10953 memset (lh, 0, sizeof (*lh));
10954 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10955 (void *) lh);
10956
10957 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10958
10959 /* Read in the header. */
10960 lh->total_length =
10961 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10962 &bytes_read, &offset_size);
10963 line_ptr += bytes_read;
10964 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10965 + dwarf2_per_objfile->line.size))
10966 {
10967 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10968 return 0;
10969 }
10970 lh->statement_program_end = line_ptr + lh->total_length;
10971 lh->version = read_2_bytes (abfd, line_ptr);
10972 line_ptr += 2;
10973 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10974 line_ptr += offset_size;
10975 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10976 line_ptr += 1;
10977 if (lh->version >= 4)
10978 {
10979 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10980 line_ptr += 1;
10981 }
10982 else
10983 lh->maximum_ops_per_instruction = 1;
10984
10985 if (lh->maximum_ops_per_instruction == 0)
10986 {
10987 lh->maximum_ops_per_instruction = 1;
10988 complaint (&symfile_complaints,
10989 _("invalid maximum_ops_per_instruction "
10990 "in `.debug_line' section"));
10991 }
10992
10993 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10994 line_ptr += 1;
10995 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10996 line_ptr += 1;
10997 lh->line_range = read_1_byte (abfd, line_ptr);
10998 line_ptr += 1;
10999 lh->opcode_base = read_1_byte (abfd, line_ptr);
11000 line_ptr += 1;
11001 lh->standard_opcode_lengths
11002 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11003
11004 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11005 for (i = 1; i < lh->opcode_base; ++i)
11006 {
11007 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11008 line_ptr += 1;
11009 }
11010
11011 /* Read directory table. */
11012 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11013 {
11014 line_ptr += bytes_read;
11015 add_include_dir (lh, cur_dir);
11016 }
11017 line_ptr += bytes_read;
11018
11019 /* Read file name table. */
11020 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11021 {
11022 unsigned int dir_index, mod_time, length;
11023
11024 line_ptr += bytes_read;
11025 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11026 line_ptr += bytes_read;
11027 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11028 line_ptr += bytes_read;
11029 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11030 line_ptr += bytes_read;
11031
11032 add_file_name (lh, cur_file, dir_index, mod_time, length);
11033 }
11034 line_ptr += bytes_read;
11035 lh->statement_program_start = line_ptr;
11036
11037 if (line_ptr > (dwarf2_per_objfile->line.buffer
11038 + dwarf2_per_objfile->line.size))
11039 complaint (&symfile_complaints,
11040 _("line number info header doesn't "
11041 "fit in `.debug_line' section"));
11042
11043 discard_cleanups (back_to);
11044 return lh;
11045 }
11046
11047 /* Subroutine of dwarf_decode_lines to simplify it.
11048 Return the file name of the psymtab for included file FILE_INDEX
11049 in line header LH of PST.
11050 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11051 If space for the result is malloc'd, it will be freed by a cleanup.
11052 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11053
11054 static char *
11055 psymtab_include_file_name (const struct line_header *lh, int file_index,
11056 const struct partial_symtab *pst,
11057 const char *comp_dir)
11058 {
11059 const struct file_entry fe = lh->file_names [file_index];
11060 char *include_name = fe.name;
11061 char *include_name_to_compare = include_name;
11062 char *dir_name = NULL;
11063 const char *pst_filename;
11064 char *copied_name = NULL;
11065 int file_is_pst;
11066
11067 if (fe.dir_index)
11068 dir_name = lh->include_dirs[fe.dir_index - 1];
11069
11070 if (!IS_ABSOLUTE_PATH (include_name)
11071 && (dir_name != NULL || comp_dir != NULL))
11072 {
11073 /* Avoid creating a duplicate psymtab for PST.
11074 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11075 Before we do the comparison, however, we need to account
11076 for DIR_NAME and COMP_DIR.
11077 First prepend dir_name (if non-NULL). If we still don't
11078 have an absolute path prepend comp_dir (if non-NULL).
11079 However, the directory we record in the include-file's
11080 psymtab does not contain COMP_DIR (to match the
11081 corresponding symtab(s)).
11082
11083 Example:
11084
11085 bash$ cd /tmp
11086 bash$ gcc -g ./hello.c
11087 include_name = "hello.c"
11088 dir_name = "."
11089 DW_AT_comp_dir = comp_dir = "/tmp"
11090 DW_AT_name = "./hello.c" */
11091
11092 if (dir_name != NULL)
11093 {
11094 include_name = concat (dir_name, SLASH_STRING,
11095 include_name, (char *)NULL);
11096 include_name_to_compare = include_name;
11097 make_cleanup (xfree, include_name);
11098 }
11099 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11100 {
11101 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11102 include_name, (char *)NULL);
11103 }
11104 }
11105
11106 pst_filename = pst->filename;
11107 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11108 {
11109 copied_name = concat (pst->dirname, SLASH_STRING,
11110 pst_filename, (char *)NULL);
11111 pst_filename = copied_name;
11112 }
11113
11114 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11115
11116 if (include_name_to_compare != include_name)
11117 xfree (include_name_to_compare);
11118 if (copied_name != NULL)
11119 xfree (copied_name);
11120
11121 if (file_is_pst)
11122 return NULL;
11123 return include_name;
11124 }
11125
11126 /* Ignore this record_line request. */
11127
11128 static void
11129 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11130 {
11131 return;
11132 }
11133
11134 /* Decode the Line Number Program (LNP) for the given line_header
11135 structure and CU. The actual information extracted and the type
11136 of structures created from the LNP depends on the value of PST.
11137
11138 1. If PST is NULL, then this procedure uses the data from the program
11139 to create all necessary symbol tables, and their linetables.
11140
11141 2. If PST is not NULL, this procedure reads the program to determine
11142 the list of files included by the unit represented by PST, and
11143 builds all the associated partial symbol tables.
11144
11145 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11146 It is used for relative paths in the line table.
11147 NOTE: When processing partial symtabs (pst != NULL),
11148 comp_dir == pst->dirname.
11149
11150 NOTE: It is important that psymtabs have the same file name (via strcmp)
11151 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11152 symtab we don't use it in the name of the psymtabs we create.
11153 E.g. expand_line_sal requires this when finding psymtabs to expand.
11154 A good testcase for this is mb-inline.exp. */
11155
11156 static void
11157 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11158 struct dwarf2_cu *cu, struct partial_symtab *pst)
11159 {
11160 gdb_byte *line_ptr, *extended_end;
11161 gdb_byte *line_end;
11162 unsigned int bytes_read, extended_len;
11163 unsigned char op_code, extended_op, adj_opcode;
11164 CORE_ADDR baseaddr;
11165 struct objfile *objfile = cu->objfile;
11166 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11167 const int decode_for_pst_p = (pst != NULL);
11168 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11169 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11170 = record_line;
11171
11172 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11173
11174 line_ptr = lh->statement_program_start;
11175 line_end = lh->statement_program_end;
11176
11177 /* Read the statement sequences until there's nothing left. */
11178 while (line_ptr < line_end)
11179 {
11180 /* state machine registers */
11181 CORE_ADDR address = 0;
11182 unsigned int file = 1;
11183 unsigned int line = 1;
11184 unsigned int column = 0;
11185 int is_stmt = lh->default_is_stmt;
11186 int basic_block = 0;
11187 int end_sequence = 0;
11188 CORE_ADDR addr;
11189 unsigned char op_index = 0;
11190
11191 if (!decode_for_pst_p && lh->num_file_names >= file)
11192 {
11193 /* Start a subfile for the current file of the state machine. */
11194 /* lh->include_dirs and lh->file_names are 0-based, but the
11195 directory and file name numbers in the statement program
11196 are 1-based. */
11197 struct file_entry *fe = &lh->file_names[file - 1];
11198 char *dir = NULL;
11199
11200 if (fe->dir_index)
11201 dir = lh->include_dirs[fe->dir_index - 1];
11202
11203 dwarf2_start_subfile (fe->name, dir, comp_dir);
11204 }
11205
11206 /* Decode the table. */
11207 while (!end_sequence)
11208 {
11209 op_code = read_1_byte (abfd, line_ptr);
11210 line_ptr += 1;
11211 if (line_ptr > line_end)
11212 {
11213 dwarf2_debug_line_missing_end_sequence_complaint ();
11214 break;
11215 }
11216
11217 if (op_code >= lh->opcode_base)
11218 {
11219 /* Special operand. */
11220 adj_opcode = op_code - lh->opcode_base;
11221 address += (((op_index + (adj_opcode / lh->line_range))
11222 / lh->maximum_ops_per_instruction)
11223 * lh->minimum_instruction_length);
11224 op_index = ((op_index + (adj_opcode / lh->line_range))
11225 % lh->maximum_ops_per_instruction);
11226 line += lh->line_base + (adj_opcode % lh->line_range);
11227 if (lh->num_file_names < file || file == 0)
11228 dwarf2_debug_line_missing_file_complaint ();
11229 /* For now we ignore lines not starting on an
11230 instruction boundary. */
11231 else if (op_index == 0)
11232 {
11233 lh->file_names[file - 1].included_p = 1;
11234 if (!decode_for_pst_p && is_stmt)
11235 {
11236 if (last_subfile != current_subfile)
11237 {
11238 addr = gdbarch_addr_bits_remove (gdbarch, address);
11239 if (last_subfile)
11240 (*p_record_line) (last_subfile, 0, addr);
11241 last_subfile = current_subfile;
11242 }
11243 /* Append row to matrix using current values. */
11244 addr = gdbarch_addr_bits_remove (gdbarch, address);
11245 (*p_record_line) (current_subfile, line, addr);
11246 }
11247 }
11248 basic_block = 0;
11249 }
11250 else switch (op_code)
11251 {
11252 case DW_LNS_extended_op:
11253 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11254 &bytes_read);
11255 line_ptr += bytes_read;
11256 extended_end = line_ptr + extended_len;
11257 extended_op = read_1_byte (abfd, line_ptr);
11258 line_ptr += 1;
11259 switch (extended_op)
11260 {
11261 case DW_LNE_end_sequence:
11262 p_record_line = record_line;
11263 end_sequence = 1;
11264 break;
11265 case DW_LNE_set_address:
11266 address = read_address (abfd, line_ptr, cu, &bytes_read);
11267
11268 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11269 {
11270 /* This line table is for a function which has been
11271 GCd by the linker. Ignore it. PR gdb/12528 */
11272
11273 long line_offset
11274 = line_ptr - dwarf2_per_objfile->line.buffer;
11275
11276 complaint (&symfile_complaints,
11277 _(".debug_line address at offset 0x%lx is 0 "
11278 "[in module %s]"),
11279 line_offset, objfile->name);
11280 p_record_line = noop_record_line;
11281 }
11282
11283 op_index = 0;
11284 line_ptr += bytes_read;
11285 address += baseaddr;
11286 break;
11287 case DW_LNE_define_file:
11288 {
11289 char *cur_file;
11290 unsigned int dir_index, mod_time, length;
11291
11292 cur_file = read_direct_string (abfd, line_ptr,
11293 &bytes_read);
11294 line_ptr += bytes_read;
11295 dir_index =
11296 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11297 line_ptr += bytes_read;
11298 mod_time =
11299 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11300 line_ptr += bytes_read;
11301 length =
11302 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11303 line_ptr += bytes_read;
11304 add_file_name (lh, cur_file, dir_index, mod_time, length);
11305 }
11306 break;
11307 case DW_LNE_set_discriminator:
11308 /* The discriminator is not interesting to the debugger;
11309 just ignore it. */
11310 line_ptr = extended_end;
11311 break;
11312 default:
11313 complaint (&symfile_complaints,
11314 _("mangled .debug_line section"));
11315 return;
11316 }
11317 /* Make sure that we parsed the extended op correctly. If e.g.
11318 we expected a different address size than the producer used,
11319 we may have read the wrong number of bytes. */
11320 if (line_ptr != extended_end)
11321 {
11322 complaint (&symfile_complaints,
11323 _("mangled .debug_line section"));
11324 return;
11325 }
11326 break;
11327 case DW_LNS_copy:
11328 if (lh->num_file_names < file || file == 0)
11329 dwarf2_debug_line_missing_file_complaint ();
11330 else
11331 {
11332 lh->file_names[file - 1].included_p = 1;
11333 if (!decode_for_pst_p && is_stmt)
11334 {
11335 if (last_subfile != current_subfile)
11336 {
11337 addr = gdbarch_addr_bits_remove (gdbarch, address);
11338 if (last_subfile)
11339 (*p_record_line) (last_subfile, 0, addr);
11340 last_subfile = current_subfile;
11341 }
11342 addr = gdbarch_addr_bits_remove (gdbarch, address);
11343 (*p_record_line) (current_subfile, line, addr);
11344 }
11345 }
11346 basic_block = 0;
11347 break;
11348 case DW_LNS_advance_pc:
11349 {
11350 CORE_ADDR adjust
11351 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11352
11353 address += (((op_index + adjust)
11354 / lh->maximum_ops_per_instruction)
11355 * lh->minimum_instruction_length);
11356 op_index = ((op_index + adjust)
11357 % lh->maximum_ops_per_instruction);
11358 line_ptr += bytes_read;
11359 }
11360 break;
11361 case DW_LNS_advance_line:
11362 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11363 line_ptr += bytes_read;
11364 break;
11365 case DW_LNS_set_file:
11366 {
11367 /* The arrays lh->include_dirs and lh->file_names are
11368 0-based, but the directory and file name numbers in
11369 the statement program are 1-based. */
11370 struct file_entry *fe;
11371 char *dir = NULL;
11372
11373 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11374 line_ptr += bytes_read;
11375 if (lh->num_file_names < file || file == 0)
11376 dwarf2_debug_line_missing_file_complaint ();
11377 else
11378 {
11379 fe = &lh->file_names[file - 1];
11380 if (fe->dir_index)
11381 dir = lh->include_dirs[fe->dir_index - 1];
11382 if (!decode_for_pst_p)
11383 {
11384 last_subfile = current_subfile;
11385 dwarf2_start_subfile (fe->name, dir, comp_dir);
11386 }
11387 }
11388 }
11389 break;
11390 case DW_LNS_set_column:
11391 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11392 line_ptr += bytes_read;
11393 break;
11394 case DW_LNS_negate_stmt:
11395 is_stmt = (!is_stmt);
11396 break;
11397 case DW_LNS_set_basic_block:
11398 basic_block = 1;
11399 break;
11400 /* Add to the address register of the state machine the
11401 address increment value corresponding to special opcode
11402 255. I.e., this value is scaled by the minimum
11403 instruction length since special opcode 255 would have
11404 scaled the increment. */
11405 case DW_LNS_const_add_pc:
11406 {
11407 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11408
11409 address += (((op_index + adjust)
11410 / lh->maximum_ops_per_instruction)
11411 * lh->minimum_instruction_length);
11412 op_index = ((op_index + adjust)
11413 % lh->maximum_ops_per_instruction);
11414 }
11415 break;
11416 case DW_LNS_fixed_advance_pc:
11417 address += read_2_bytes (abfd, line_ptr);
11418 op_index = 0;
11419 line_ptr += 2;
11420 break;
11421 default:
11422 {
11423 /* Unknown standard opcode, ignore it. */
11424 int i;
11425
11426 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11427 {
11428 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11429 line_ptr += bytes_read;
11430 }
11431 }
11432 }
11433 }
11434 if (lh->num_file_names < file || file == 0)
11435 dwarf2_debug_line_missing_file_complaint ();
11436 else
11437 {
11438 lh->file_names[file - 1].included_p = 1;
11439 if (!decode_for_pst_p)
11440 {
11441 addr = gdbarch_addr_bits_remove (gdbarch, address);
11442 (*p_record_line) (current_subfile, 0, addr);
11443 }
11444 }
11445 }
11446
11447 if (decode_for_pst_p)
11448 {
11449 int file_index;
11450
11451 /* Now that we're done scanning the Line Header Program, we can
11452 create the psymtab of each included file. */
11453 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11454 if (lh->file_names[file_index].included_p == 1)
11455 {
11456 char *include_name =
11457 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11458 if (include_name != NULL)
11459 dwarf2_create_include_psymtab (include_name, pst, objfile);
11460 }
11461 }
11462 else
11463 {
11464 /* Make sure a symtab is created for every file, even files
11465 which contain only variables (i.e. no code with associated
11466 line numbers). */
11467
11468 int i;
11469 struct file_entry *fe;
11470
11471 for (i = 0; i < lh->num_file_names; i++)
11472 {
11473 char *dir = NULL;
11474
11475 fe = &lh->file_names[i];
11476 if (fe->dir_index)
11477 dir = lh->include_dirs[fe->dir_index - 1];
11478 dwarf2_start_subfile (fe->name, dir, comp_dir);
11479
11480 /* Skip the main file; we don't need it, and it must be
11481 allocated last, so that it will show up before the
11482 non-primary symtabs in the objfile's symtab list. */
11483 if (current_subfile == first_subfile)
11484 continue;
11485
11486 if (current_subfile->symtab == NULL)
11487 current_subfile->symtab = allocate_symtab (current_subfile->name,
11488 objfile);
11489 fe->symtab = current_subfile->symtab;
11490 }
11491 }
11492 }
11493
11494 /* Start a subfile for DWARF. FILENAME is the name of the file and
11495 DIRNAME the name of the source directory which contains FILENAME
11496 or NULL if not known. COMP_DIR is the compilation directory for the
11497 linetable's compilation unit or NULL if not known.
11498 This routine tries to keep line numbers from identical absolute and
11499 relative file names in a common subfile.
11500
11501 Using the `list' example from the GDB testsuite, which resides in
11502 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11503 of /srcdir/list0.c yields the following debugging information for list0.c:
11504
11505 DW_AT_name: /srcdir/list0.c
11506 DW_AT_comp_dir: /compdir
11507 files.files[0].name: list0.h
11508 files.files[0].dir: /srcdir
11509 files.files[1].name: list0.c
11510 files.files[1].dir: /srcdir
11511
11512 The line number information for list0.c has to end up in a single
11513 subfile, so that `break /srcdir/list0.c:1' works as expected.
11514 start_subfile will ensure that this happens provided that we pass the
11515 concatenation of files.files[1].dir and files.files[1].name as the
11516 subfile's name. */
11517
11518 static void
11519 dwarf2_start_subfile (char *filename, const char *dirname,
11520 const char *comp_dir)
11521 {
11522 char *fullname;
11523
11524 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11525 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11526 second argument to start_subfile. To be consistent, we do the
11527 same here. In order not to lose the line information directory,
11528 we concatenate it to the filename when it makes sense.
11529 Note that the Dwarf3 standard says (speaking of filenames in line
11530 information): ``The directory index is ignored for file names
11531 that represent full path names''. Thus ignoring dirname in the
11532 `else' branch below isn't an issue. */
11533
11534 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11535 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11536 else
11537 fullname = filename;
11538
11539 start_subfile (fullname, comp_dir);
11540
11541 if (fullname != filename)
11542 xfree (fullname);
11543 }
11544
11545 static void
11546 var_decode_location (struct attribute *attr, struct symbol *sym,
11547 struct dwarf2_cu *cu)
11548 {
11549 struct objfile *objfile = cu->objfile;
11550 struct comp_unit_head *cu_header = &cu->header;
11551
11552 /* NOTE drow/2003-01-30: There used to be a comment and some special
11553 code here to turn a symbol with DW_AT_external and a
11554 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11555 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11556 with some versions of binutils) where shared libraries could have
11557 relocations against symbols in their debug information - the
11558 minimal symbol would have the right address, but the debug info
11559 would not. It's no longer necessary, because we will explicitly
11560 apply relocations when we read in the debug information now. */
11561
11562 /* A DW_AT_location attribute with no contents indicates that a
11563 variable has been optimized away. */
11564 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11565 {
11566 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11567 return;
11568 }
11569
11570 /* Handle one degenerate form of location expression specially, to
11571 preserve GDB's previous behavior when section offsets are
11572 specified. If this is just a DW_OP_addr then mark this symbol
11573 as LOC_STATIC. */
11574
11575 if (attr_form_is_block (attr)
11576 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11577 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11578 {
11579 unsigned int dummy;
11580
11581 SYMBOL_VALUE_ADDRESS (sym) =
11582 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11583 SYMBOL_CLASS (sym) = LOC_STATIC;
11584 fixup_symbol_section (sym, objfile);
11585 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11586 SYMBOL_SECTION (sym));
11587 return;
11588 }
11589
11590 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11591 expression evaluator, and use LOC_COMPUTED only when necessary
11592 (i.e. when the value of a register or memory location is
11593 referenced, or a thread-local block, etc.). Then again, it might
11594 not be worthwhile. I'm assuming that it isn't unless performance
11595 or memory numbers show me otherwise. */
11596
11597 dwarf2_symbol_mark_computed (attr, sym, cu);
11598 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11599
11600 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11601 cu->has_loclist = 1;
11602 }
11603
11604 /* Given a pointer to a DWARF information entry, figure out if we need
11605 to make a symbol table entry for it, and if so, create a new entry
11606 and return a pointer to it.
11607 If TYPE is NULL, determine symbol type from the die, otherwise
11608 used the passed type.
11609 If SPACE is not NULL, use it to hold the new symbol. If it is
11610 NULL, allocate a new symbol on the objfile's obstack. */
11611
11612 static struct symbol *
11613 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11614 struct symbol *space)
11615 {
11616 struct objfile *objfile = cu->objfile;
11617 struct symbol *sym = NULL;
11618 char *name;
11619 struct attribute *attr = NULL;
11620 struct attribute *attr2 = NULL;
11621 CORE_ADDR baseaddr;
11622 struct pending **list_to_add = NULL;
11623
11624 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11625
11626 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11627
11628 name = dwarf2_name (die, cu);
11629 if (name)
11630 {
11631 const char *linkagename;
11632 int suppress_add = 0;
11633
11634 if (space)
11635 sym = space;
11636 else
11637 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11638 OBJSTAT (objfile, n_syms++);
11639
11640 /* Cache this symbol's name and the name's demangled form (if any). */
11641 SYMBOL_SET_LANGUAGE (sym, cu->language);
11642 linkagename = dwarf2_physname (name, die, cu);
11643 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11644
11645 /* Fortran does not have mangling standard and the mangling does differ
11646 between gfortran, iFort etc. */
11647 if (cu->language == language_fortran
11648 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11649 symbol_set_demangled_name (&(sym->ginfo),
11650 (char *) dwarf2_full_name (name, die, cu),
11651 NULL);
11652
11653 /* Default assumptions.
11654 Use the passed type or decode it from the die. */
11655 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11656 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11657 if (type != NULL)
11658 SYMBOL_TYPE (sym) = type;
11659 else
11660 SYMBOL_TYPE (sym) = die_type (die, cu);
11661 attr = dwarf2_attr (die,
11662 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11663 cu);
11664 if (attr)
11665 {
11666 SYMBOL_LINE (sym) = DW_UNSND (attr);
11667 }
11668
11669 attr = dwarf2_attr (die,
11670 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11671 cu);
11672 if (attr)
11673 {
11674 int file_index = DW_UNSND (attr);
11675
11676 if (cu->line_header == NULL
11677 || file_index > cu->line_header->num_file_names)
11678 complaint (&symfile_complaints,
11679 _("file index out of range"));
11680 else if (file_index > 0)
11681 {
11682 struct file_entry *fe;
11683
11684 fe = &cu->line_header->file_names[file_index - 1];
11685 SYMBOL_SYMTAB (sym) = fe->symtab;
11686 }
11687 }
11688
11689 switch (die->tag)
11690 {
11691 case DW_TAG_label:
11692 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11693 if (attr)
11694 {
11695 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11696 }
11697 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11698 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11699 SYMBOL_CLASS (sym) = LOC_LABEL;
11700 add_symbol_to_list (sym, cu->list_in_scope);
11701 break;
11702 case DW_TAG_subprogram:
11703 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11704 finish_block. */
11705 SYMBOL_CLASS (sym) = LOC_BLOCK;
11706 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11707 if ((attr2 && (DW_UNSND (attr2) != 0))
11708 || cu->language == language_ada)
11709 {
11710 /* Subprograms marked external are stored as a global symbol.
11711 Ada subprograms, whether marked external or not, are always
11712 stored as a global symbol, because we want to be able to
11713 access them globally. For instance, we want to be able
11714 to break on a nested subprogram without having to
11715 specify the context. */
11716 list_to_add = &global_symbols;
11717 }
11718 else
11719 {
11720 list_to_add = cu->list_in_scope;
11721 }
11722 break;
11723 case DW_TAG_inlined_subroutine:
11724 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11725 finish_block. */
11726 SYMBOL_CLASS (sym) = LOC_BLOCK;
11727 SYMBOL_INLINED (sym) = 1;
11728 /* Do not add the symbol to any lists. It will be found via
11729 BLOCK_FUNCTION from the blockvector. */
11730 break;
11731 case DW_TAG_template_value_param:
11732 suppress_add = 1;
11733 /* Fall through. */
11734 case DW_TAG_constant:
11735 case DW_TAG_variable:
11736 case DW_TAG_member:
11737 /* Compilation with minimal debug info may result in
11738 variables with missing type entries. Change the
11739 misleading `void' type to something sensible. */
11740 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11741 SYMBOL_TYPE (sym)
11742 = objfile_type (objfile)->nodebug_data_symbol;
11743
11744 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11745 /* In the case of DW_TAG_member, we should only be called for
11746 static const members. */
11747 if (die->tag == DW_TAG_member)
11748 {
11749 /* dwarf2_add_field uses die_is_declaration,
11750 so we do the same. */
11751 gdb_assert (die_is_declaration (die, cu));
11752 gdb_assert (attr);
11753 }
11754 if (attr)
11755 {
11756 dwarf2_const_value (attr, sym, cu);
11757 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11758 if (!suppress_add)
11759 {
11760 if (attr2 && (DW_UNSND (attr2) != 0))
11761 list_to_add = &global_symbols;
11762 else
11763 list_to_add = cu->list_in_scope;
11764 }
11765 break;
11766 }
11767 attr = dwarf2_attr (die, DW_AT_location, cu);
11768 if (attr)
11769 {
11770 var_decode_location (attr, sym, cu);
11771 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11772 if (SYMBOL_CLASS (sym) == LOC_STATIC
11773 && SYMBOL_VALUE_ADDRESS (sym) == 0
11774 && !dwarf2_per_objfile->has_section_at_zero)
11775 {
11776 /* When a static variable is eliminated by the linker,
11777 the corresponding debug information is not stripped
11778 out, but the variable address is set to null;
11779 do not add such variables into symbol table. */
11780 }
11781 else if (attr2 && (DW_UNSND (attr2) != 0))
11782 {
11783 /* Workaround gfortran PR debug/40040 - it uses
11784 DW_AT_location for variables in -fPIC libraries which may
11785 get overriden by other libraries/executable and get
11786 a different address. Resolve it by the minimal symbol
11787 which may come from inferior's executable using copy
11788 relocation. Make this workaround only for gfortran as for
11789 other compilers GDB cannot guess the minimal symbol
11790 Fortran mangling kind. */
11791 if (cu->language == language_fortran && die->parent
11792 && die->parent->tag == DW_TAG_module
11793 && cu->producer
11794 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11795 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11796
11797 /* A variable with DW_AT_external is never static,
11798 but it may be block-scoped. */
11799 list_to_add = (cu->list_in_scope == &file_symbols
11800 ? &global_symbols : cu->list_in_scope);
11801 }
11802 else
11803 list_to_add = cu->list_in_scope;
11804 }
11805 else
11806 {
11807 /* We do not know the address of this symbol.
11808 If it is an external symbol and we have type information
11809 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11810 The address of the variable will then be determined from
11811 the minimal symbol table whenever the variable is
11812 referenced. */
11813 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11814 if (attr2 && (DW_UNSND (attr2) != 0)
11815 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11816 {
11817 /* A variable with DW_AT_external is never static, but it
11818 may be block-scoped. */
11819 list_to_add = (cu->list_in_scope == &file_symbols
11820 ? &global_symbols : cu->list_in_scope);
11821
11822 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11823 }
11824 else if (!die_is_declaration (die, cu))
11825 {
11826 /* Use the default LOC_OPTIMIZED_OUT class. */
11827 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11828 if (!suppress_add)
11829 list_to_add = cu->list_in_scope;
11830 }
11831 }
11832 break;
11833 case DW_TAG_formal_parameter:
11834 /* If we are inside a function, mark this as an argument. If
11835 not, we might be looking at an argument to an inlined function
11836 when we do not have enough information to show inlined frames;
11837 pretend it's a local variable in that case so that the user can
11838 still see it. */
11839 if (context_stack_depth > 0
11840 && context_stack[context_stack_depth - 1].name != NULL)
11841 SYMBOL_IS_ARGUMENT (sym) = 1;
11842 attr = dwarf2_attr (die, DW_AT_location, cu);
11843 if (attr)
11844 {
11845 var_decode_location (attr, sym, cu);
11846 }
11847 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11848 if (attr)
11849 {
11850 dwarf2_const_value (attr, sym, cu);
11851 }
11852
11853 list_to_add = cu->list_in_scope;
11854 break;
11855 case DW_TAG_unspecified_parameters:
11856 /* From varargs functions; gdb doesn't seem to have any
11857 interest in this information, so just ignore it for now.
11858 (FIXME?) */
11859 break;
11860 case DW_TAG_template_type_param:
11861 suppress_add = 1;
11862 /* Fall through. */
11863 case DW_TAG_class_type:
11864 case DW_TAG_interface_type:
11865 case DW_TAG_structure_type:
11866 case DW_TAG_union_type:
11867 case DW_TAG_set_type:
11868 case DW_TAG_enumeration_type:
11869 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11870 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11871
11872 {
11873 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11874 really ever be static objects: otherwise, if you try
11875 to, say, break of a class's method and you're in a file
11876 which doesn't mention that class, it won't work unless
11877 the check for all static symbols in lookup_symbol_aux
11878 saves you. See the OtherFileClass tests in
11879 gdb.c++/namespace.exp. */
11880
11881 if (!suppress_add)
11882 {
11883 list_to_add = (cu->list_in_scope == &file_symbols
11884 && (cu->language == language_cplus
11885 || cu->language == language_java)
11886 ? &global_symbols : cu->list_in_scope);
11887
11888 /* The semantics of C++ state that "struct foo {
11889 ... }" also defines a typedef for "foo". A Java
11890 class declaration also defines a typedef for the
11891 class. */
11892 if (cu->language == language_cplus
11893 || cu->language == language_java
11894 || cu->language == language_ada)
11895 {
11896 /* The symbol's name is already allocated along
11897 with this objfile, so we don't need to
11898 duplicate it for the type. */
11899 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11900 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11901 }
11902 }
11903 }
11904 break;
11905 case DW_TAG_typedef:
11906 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11907 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11908 list_to_add = cu->list_in_scope;
11909 break;
11910 case DW_TAG_base_type:
11911 case DW_TAG_subrange_type:
11912 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11913 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11914 list_to_add = cu->list_in_scope;
11915 break;
11916 case DW_TAG_enumerator:
11917 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11918 if (attr)
11919 {
11920 dwarf2_const_value (attr, sym, cu);
11921 }
11922 {
11923 /* NOTE: carlton/2003-11-10: See comment above in the
11924 DW_TAG_class_type, etc. block. */
11925
11926 list_to_add = (cu->list_in_scope == &file_symbols
11927 && (cu->language == language_cplus
11928 || cu->language == language_java)
11929 ? &global_symbols : cu->list_in_scope);
11930 }
11931 break;
11932 case DW_TAG_namespace:
11933 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11934 list_to_add = &global_symbols;
11935 break;
11936 default:
11937 /* Not a tag we recognize. Hopefully we aren't processing
11938 trash data, but since we must specifically ignore things
11939 we don't recognize, there is nothing else we should do at
11940 this point. */
11941 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11942 dwarf_tag_name (die->tag));
11943 break;
11944 }
11945
11946 if (suppress_add)
11947 {
11948 sym->hash_next = objfile->template_symbols;
11949 objfile->template_symbols = sym;
11950 list_to_add = NULL;
11951 }
11952
11953 if (list_to_add != NULL)
11954 add_symbol_to_list (sym, list_to_add);
11955
11956 /* For the benefit of old versions of GCC, check for anonymous
11957 namespaces based on the demangled name. */
11958 if (!processing_has_namespace_info
11959 && cu->language == language_cplus)
11960 cp_scan_for_anonymous_namespaces (sym, objfile);
11961 }
11962 return (sym);
11963 }
11964
11965 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11966
11967 static struct symbol *
11968 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11969 {
11970 return new_symbol_full (die, type, cu, NULL);
11971 }
11972
11973 /* Given an attr with a DW_FORM_dataN value in host byte order,
11974 zero-extend it as appropriate for the symbol's type. The DWARF
11975 standard (v4) is not entirely clear about the meaning of using
11976 DW_FORM_dataN for a constant with a signed type, where the type is
11977 wider than the data. The conclusion of a discussion on the DWARF
11978 list was that this is unspecified. We choose to always zero-extend
11979 because that is the interpretation long in use by GCC. */
11980
11981 static gdb_byte *
11982 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11983 const char *name, struct obstack *obstack,
11984 struct dwarf2_cu *cu, long *value, int bits)
11985 {
11986 struct objfile *objfile = cu->objfile;
11987 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11988 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11989 LONGEST l = DW_UNSND (attr);
11990
11991 if (bits < sizeof (*value) * 8)
11992 {
11993 l &= ((LONGEST) 1 << bits) - 1;
11994 *value = l;
11995 }
11996 else if (bits == sizeof (*value) * 8)
11997 *value = l;
11998 else
11999 {
12000 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12001 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12002 return bytes;
12003 }
12004
12005 return NULL;
12006 }
12007
12008 /* Read a constant value from an attribute. Either set *VALUE, or if
12009 the value does not fit in *VALUE, set *BYTES - either already
12010 allocated on the objfile obstack, or newly allocated on OBSTACK,
12011 or, set *BATON, if we translated the constant to a location
12012 expression. */
12013
12014 static void
12015 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12016 const char *name, struct obstack *obstack,
12017 struct dwarf2_cu *cu,
12018 long *value, gdb_byte **bytes,
12019 struct dwarf2_locexpr_baton **baton)
12020 {
12021 struct objfile *objfile = cu->objfile;
12022 struct comp_unit_head *cu_header = &cu->header;
12023 struct dwarf_block *blk;
12024 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12025 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12026
12027 *value = 0;
12028 *bytes = NULL;
12029 *baton = NULL;
12030
12031 switch (attr->form)
12032 {
12033 case DW_FORM_addr:
12034 {
12035 gdb_byte *data;
12036
12037 if (TYPE_LENGTH (type) != cu_header->addr_size)
12038 dwarf2_const_value_length_mismatch_complaint (name,
12039 cu_header->addr_size,
12040 TYPE_LENGTH (type));
12041 /* Symbols of this form are reasonably rare, so we just
12042 piggyback on the existing location code rather than writing
12043 a new implementation of symbol_computed_ops. */
12044 *baton = obstack_alloc (&objfile->objfile_obstack,
12045 sizeof (struct dwarf2_locexpr_baton));
12046 (*baton)->per_cu = cu->per_cu;
12047 gdb_assert ((*baton)->per_cu);
12048
12049 (*baton)->size = 2 + cu_header->addr_size;
12050 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12051 (*baton)->data = data;
12052
12053 data[0] = DW_OP_addr;
12054 store_unsigned_integer (&data[1], cu_header->addr_size,
12055 byte_order, DW_ADDR (attr));
12056 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12057 }
12058 break;
12059 case DW_FORM_string:
12060 case DW_FORM_strp:
12061 /* DW_STRING is already allocated on the objfile obstack, point
12062 directly to it. */
12063 *bytes = (gdb_byte *) DW_STRING (attr);
12064 break;
12065 case DW_FORM_block1:
12066 case DW_FORM_block2:
12067 case DW_FORM_block4:
12068 case DW_FORM_block:
12069 case DW_FORM_exprloc:
12070 blk = DW_BLOCK (attr);
12071 if (TYPE_LENGTH (type) != blk->size)
12072 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12073 TYPE_LENGTH (type));
12074 *bytes = blk->data;
12075 break;
12076
12077 /* The DW_AT_const_value attributes are supposed to carry the
12078 symbol's value "represented as it would be on the target
12079 architecture." By the time we get here, it's already been
12080 converted to host endianness, so we just need to sign- or
12081 zero-extend it as appropriate. */
12082 case DW_FORM_data1:
12083 *bytes = dwarf2_const_value_data (attr, type, name,
12084 obstack, cu, value, 8);
12085 break;
12086 case DW_FORM_data2:
12087 *bytes = dwarf2_const_value_data (attr, type, name,
12088 obstack, cu, value, 16);
12089 break;
12090 case DW_FORM_data4:
12091 *bytes = dwarf2_const_value_data (attr, type, name,
12092 obstack, cu, value, 32);
12093 break;
12094 case DW_FORM_data8:
12095 *bytes = dwarf2_const_value_data (attr, type, name,
12096 obstack, cu, value, 64);
12097 break;
12098
12099 case DW_FORM_sdata:
12100 *value = DW_SND (attr);
12101 break;
12102
12103 case DW_FORM_udata:
12104 *value = DW_UNSND (attr);
12105 break;
12106
12107 default:
12108 complaint (&symfile_complaints,
12109 _("unsupported const value attribute form: '%s'"),
12110 dwarf_form_name (attr->form));
12111 *value = 0;
12112 break;
12113 }
12114 }
12115
12116
12117 /* Copy constant value from an attribute to a symbol. */
12118
12119 static void
12120 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12121 struct dwarf2_cu *cu)
12122 {
12123 struct objfile *objfile = cu->objfile;
12124 struct comp_unit_head *cu_header = &cu->header;
12125 long value;
12126 gdb_byte *bytes;
12127 struct dwarf2_locexpr_baton *baton;
12128
12129 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12130 SYMBOL_PRINT_NAME (sym),
12131 &objfile->objfile_obstack, cu,
12132 &value, &bytes, &baton);
12133
12134 if (baton != NULL)
12135 {
12136 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12137 SYMBOL_LOCATION_BATON (sym) = baton;
12138 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12139 }
12140 else if (bytes != NULL)
12141 {
12142 SYMBOL_VALUE_BYTES (sym) = bytes;
12143 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12144 }
12145 else
12146 {
12147 SYMBOL_VALUE (sym) = value;
12148 SYMBOL_CLASS (sym) = LOC_CONST;
12149 }
12150 }
12151
12152 /* Return the type of the die in question using its DW_AT_type attribute. */
12153
12154 static struct type *
12155 die_type (struct die_info *die, struct dwarf2_cu *cu)
12156 {
12157 struct attribute *type_attr;
12158
12159 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12160 if (!type_attr)
12161 {
12162 /* A missing DW_AT_type represents a void type. */
12163 return objfile_type (cu->objfile)->builtin_void;
12164 }
12165
12166 return lookup_die_type (die, type_attr, cu);
12167 }
12168
12169 /* True iff CU's producer generates GNAT Ada auxiliary information
12170 that allows to find parallel types through that information instead
12171 of having to do expensive parallel lookups by type name. */
12172
12173 static int
12174 need_gnat_info (struct dwarf2_cu *cu)
12175 {
12176 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12177 of GNAT produces this auxiliary information, without any indication
12178 that it is produced. Part of enhancing the FSF version of GNAT
12179 to produce that information will be to put in place an indicator
12180 that we can use in order to determine whether the descriptive type
12181 info is available or not. One suggestion that has been made is
12182 to use a new attribute, attached to the CU die. For now, assume
12183 that the descriptive type info is not available. */
12184 return 0;
12185 }
12186
12187 /* Return the auxiliary type of the die in question using its
12188 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12189 attribute is not present. */
12190
12191 static struct type *
12192 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12193 {
12194 struct attribute *type_attr;
12195
12196 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12197 if (!type_attr)
12198 return NULL;
12199
12200 return lookup_die_type (die, type_attr, cu);
12201 }
12202
12203 /* If DIE has a descriptive_type attribute, then set the TYPE's
12204 descriptive type accordingly. */
12205
12206 static void
12207 set_descriptive_type (struct type *type, struct die_info *die,
12208 struct dwarf2_cu *cu)
12209 {
12210 struct type *descriptive_type = die_descriptive_type (die, cu);
12211
12212 if (descriptive_type)
12213 {
12214 ALLOCATE_GNAT_AUX_TYPE (type);
12215 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12216 }
12217 }
12218
12219 /* Return the containing type of the die in question using its
12220 DW_AT_containing_type attribute. */
12221
12222 static struct type *
12223 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12224 {
12225 struct attribute *type_attr;
12226
12227 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12228 if (!type_attr)
12229 error (_("Dwarf Error: Problem turning containing type into gdb type "
12230 "[in module %s]"), cu->objfile->name);
12231
12232 return lookup_die_type (die, type_attr, cu);
12233 }
12234
12235 /* Look up the type of DIE in CU using its type attribute ATTR.
12236 If there is no type substitute an error marker. */
12237
12238 static struct type *
12239 lookup_die_type (struct die_info *die, struct attribute *attr,
12240 struct dwarf2_cu *cu)
12241 {
12242 struct objfile *objfile = cu->objfile;
12243 struct type *this_type;
12244
12245 /* First see if we have it cached. */
12246
12247 if (is_ref_attr (attr))
12248 {
12249 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12250
12251 this_type = get_die_type_at_offset (offset, cu->per_cu);
12252 }
12253 else if (attr->form == DW_FORM_ref_sig8)
12254 {
12255 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12256 struct dwarf2_cu *sig_cu;
12257 unsigned int offset;
12258
12259 /* sig_type will be NULL if the signatured type is missing from
12260 the debug info. */
12261 if (sig_type == NULL)
12262 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12263 "at 0x%x [in module %s]"),
12264 die->offset, objfile->name);
12265
12266 gdb_assert (sig_type->per_cu.debug_types_section);
12267 offset = sig_type->per_cu.offset + sig_type->type_offset;
12268 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12269 }
12270 else
12271 {
12272 dump_die_for_error (die);
12273 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12274 dwarf_attr_name (attr->name), objfile->name);
12275 }
12276
12277 /* If not cached we need to read it in. */
12278
12279 if (this_type == NULL)
12280 {
12281 struct die_info *type_die;
12282 struct dwarf2_cu *type_cu = cu;
12283
12284 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12285 /* If the type is cached, we should have found it above. */
12286 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12287 this_type = read_type_die_1 (type_die, type_cu);
12288 }
12289
12290 /* If we still don't have a type use an error marker. */
12291
12292 if (this_type == NULL)
12293 {
12294 char *message, *saved;
12295
12296 /* read_type_die already issued a complaint. */
12297 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12298 objfile->name,
12299 cu->header.offset,
12300 die->offset);
12301 saved = obstack_copy0 (&objfile->objfile_obstack,
12302 message, strlen (message));
12303 xfree (message);
12304
12305 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12306 }
12307
12308 return this_type;
12309 }
12310
12311 /* Return the type in DIE, CU.
12312 Returns NULL for invalid types.
12313
12314 This first does a lookup in the appropriate type_hash table,
12315 and only reads the die in if necessary.
12316
12317 NOTE: This can be called when reading in partial or full symbols. */
12318
12319 static struct type *
12320 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12321 {
12322 struct type *this_type;
12323
12324 this_type = get_die_type (die, cu);
12325 if (this_type)
12326 return this_type;
12327
12328 return read_type_die_1 (die, cu);
12329 }
12330
12331 /* Read the type in DIE, CU.
12332 Returns NULL for invalid types. */
12333
12334 static struct type *
12335 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12336 {
12337 struct type *this_type = NULL;
12338
12339 switch (die->tag)
12340 {
12341 case DW_TAG_class_type:
12342 case DW_TAG_interface_type:
12343 case DW_TAG_structure_type:
12344 case DW_TAG_union_type:
12345 this_type = read_structure_type (die, cu);
12346 break;
12347 case DW_TAG_enumeration_type:
12348 this_type = read_enumeration_type (die, cu);
12349 break;
12350 case DW_TAG_subprogram:
12351 case DW_TAG_subroutine_type:
12352 case DW_TAG_inlined_subroutine:
12353 this_type = read_subroutine_type (die, cu);
12354 break;
12355 case DW_TAG_array_type:
12356 this_type = read_array_type (die, cu);
12357 break;
12358 case DW_TAG_set_type:
12359 this_type = read_set_type (die, cu);
12360 break;
12361 case DW_TAG_pointer_type:
12362 this_type = read_tag_pointer_type (die, cu);
12363 break;
12364 case DW_TAG_ptr_to_member_type:
12365 this_type = read_tag_ptr_to_member_type (die, cu);
12366 break;
12367 case DW_TAG_reference_type:
12368 this_type = read_tag_reference_type (die, cu);
12369 break;
12370 case DW_TAG_const_type:
12371 this_type = read_tag_const_type (die, cu);
12372 break;
12373 case DW_TAG_volatile_type:
12374 this_type = read_tag_volatile_type (die, cu);
12375 break;
12376 case DW_TAG_string_type:
12377 this_type = read_tag_string_type (die, cu);
12378 break;
12379 case DW_TAG_typedef:
12380 this_type = read_typedef (die, cu);
12381 break;
12382 case DW_TAG_subrange_type:
12383 this_type = read_subrange_type (die, cu);
12384 break;
12385 case DW_TAG_base_type:
12386 this_type = read_base_type (die, cu);
12387 break;
12388 case DW_TAG_unspecified_type:
12389 this_type = read_unspecified_type (die, cu);
12390 break;
12391 case DW_TAG_namespace:
12392 this_type = read_namespace_type (die, cu);
12393 break;
12394 case DW_TAG_module:
12395 this_type = read_module_type (die, cu);
12396 break;
12397 default:
12398 complaint (&symfile_complaints,
12399 _("unexpected tag in read_type_die: '%s'"),
12400 dwarf_tag_name (die->tag));
12401 break;
12402 }
12403
12404 return this_type;
12405 }
12406
12407 /* See if we can figure out if the class lives in a namespace. We do
12408 this by looking for a member function; its demangled name will
12409 contain namespace info, if there is any.
12410 Return the computed name or NULL.
12411 Space for the result is allocated on the objfile's obstack.
12412 This is the full-die version of guess_partial_die_structure_name.
12413 In this case we know DIE has no useful parent. */
12414
12415 static char *
12416 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12417 {
12418 struct die_info *spec_die;
12419 struct dwarf2_cu *spec_cu;
12420 struct die_info *child;
12421
12422 spec_cu = cu;
12423 spec_die = die_specification (die, &spec_cu);
12424 if (spec_die != NULL)
12425 {
12426 die = spec_die;
12427 cu = spec_cu;
12428 }
12429
12430 for (child = die->child;
12431 child != NULL;
12432 child = child->sibling)
12433 {
12434 if (child->tag == DW_TAG_subprogram)
12435 {
12436 struct attribute *attr;
12437
12438 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12439 if (attr == NULL)
12440 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12441 if (attr != NULL)
12442 {
12443 char *actual_name
12444 = language_class_name_from_physname (cu->language_defn,
12445 DW_STRING (attr));
12446 char *name = NULL;
12447
12448 if (actual_name != NULL)
12449 {
12450 char *die_name = dwarf2_name (die, cu);
12451
12452 if (die_name != NULL
12453 && strcmp (die_name, actual_name) != 0)
12454 {
12455 /* Strip off the class name from the full name.
12456 We want the prefix. */
12457 int die_name_len = strlen (die_name);
12458 int actual_name_len = strlen (actual_name);
12459
12460 /* Test for '::' as a sanity check. */
12461 if (actual_name_len > die_name_len + 2
12462 && actual_name[actual_name_len
12463 - die_name_len - 1] == ':')
12464 name =
12465 obsavestring (actual_name,
12466 actual_name_len - die_name_len - 2,
12467 &cu->objfile->objfile_obstack);
12468 }
12469 }
12470 xfree (actual_name);
12471 return name;
12472 }
12473 }
12474 }
12475
12476 return NULL;
12477 }
12478
12479 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12480 prefix part in such case. See
12481 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12482
12483 static char *
12484 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12485 {
12486 struct attribute *attr;
12487 char *base;
12488
12489 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12490 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12491 return NULL;
12492
12493 attr = dwarf2_attr (die, DW_AT_name, cu);
12494 if (attr != NULL && DW_STRING (attr) != NULL)
12495 return NULL;
12496
12497 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12498 if (attr == NULL)
12499 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12500 if (attr == NULL || DW_STRING (attr) == NULL)
12501 return NULL;
12502
12503 /* dwarf2_name had to be already called. */
12504 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12505
12506 /* Strip the base name, keep any leading namespaces/classes. */
12507 base = strrchr (DW_STRING (attr), ':');
12508 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12509 return "";
12510
12511 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12512 &cu->objfile->objfile_obstack);
12513 }
12514
12515 /* Return the name of the namespace/class that DIE is defined within,
12516 or "" if we can't tell. The caller should not xfree the result.
12517
12518 For example, if we're within the method foo() in the following
12519 code:
12520
12521 namespace N {
12522 class C {
12523 void foo () {
12524 }
12525 };
12526 }
12527
12528 then determine_prefix on foo's die will return "N::C". */
12529
12530 static char *
12531 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12532 {
12533 struct die_info *parent, *spec_die;
12534 struct dwarf2_cu *spec_cu;
12535 struct type *parent_type;
12536 char *retval;
12537
12538 if (cu->language != language_cplus && cu->language != language_java
12539 && cu->language != language_fortran)
12540 return "";
12541
12542 retval = anonymous_struct_prefix (die, cu);
12543 if (retval)
12544 return retval;
12545
12546 /* We have to be careful in the presence of DW_AT_specification.
12547 For example, with GCC 3.4, given the code
12548
12549 namespace N {
12550 void foo() {
12551 // Definition of N::foo.
12552 }
12553 }
12554
12555 then we'll have a tree of DIEs like this:
12556
12557 1: DW_TAG_compile_unit
12558 2: DW_TAG_namespace // N
12559 3: DW_TAG_subprogram // declaration of N::foo
12560 4: DW_TAG_subprogram // definition of N::foo
12561 DW_AT_specification // refers to die #3
12562
12563 Thus, when processing die #4, we have to pretend that we're in
12564 the context of its DW_AT_specification, namely the contex of die
12565 #3. */
12566 spec_cu = cu;
12567 spec_die = die_specification (die, &spec_cu);
12568 if (spec_die == NULL)
12569 parent = die->parent;
12570 else
12571 {
12572 parent = spec_die->parent;
12573 cu = spec_cu;
12574 }
12575
12576 if (parent == NULL)
12577 return "";
12578 else if (parent->building_fullname)
12579 {
12580 const char *name;
12581 const char *parent_name;
12582
12583 /* It has been seen on RealView 2.2 built binaries,
12584 DW_TAG_template_type_param types actually _defined_ as
12585 children of the parent class:
12586
12587 enum E {};
12588 template class <class Enum> Class{};
12589 Class<enum E> class_e;
12590
12591 1: DW_TAG_class_type (Class)
12592 2: DW_TAG_enumeration_type (E)
12593 3: DW_TAG_enumerator (enum1:0)
12594 3: DW_TAG_enumerator (enum2:1)
12595 ...
12596 2: DW_TAG_template_type_param
12597 DW_AT_type DW_FORM_ref_udata (E)
12598
12599 Besides being broken debug info, it can put GDB into an
12600 infinite loop. Consider:
12601
12602 When we're building the full name for Class<E>, we'll start
12603 at Class, and go look over its template type parameters,
12604 finding E. We'll then try to build the full name of E, and
12605 reach here. We're now trying to build the full name of E,
12606 and look over the parent DIE for containing scope. In the
12607 broken case, if we followed the parent DIE of E, we'd again
12608 find Class, and once again go look at its template type
12609 arguments, etc., etc. Simply don't consider such parent die
12610 as source-level parent of this die (it can't be, the language
12611 doesn't allow it), and break the loop here. */
12612 name = dwarf2_name (die, cu);
12613 parent_name = dwarf2_name (parent, cu);
12614 complaint (&symfile_complaints,
12615 _("template param type '%s' defined within parent '%s'"),
12616 name ? name : "<unknown>",
12617 parent_name ? parent_name : "<unknown>");
12618 return "";
12619 }
12620 else
12621 switch (parent->tag)
12622 {
12623 case DW_TAG_namespace:
12624 parent_type = read_type_die (parent, cu);
12625 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12626 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12627 Work around this problem here. */
12628 if (cu->language == language_cplus
12629 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12630 return "";
12631 /* We give a name to even anonymous namespaces. */
12632 return TYPE_TAG_NAME (parent_type);
12633 case DW_TAG_class_type:
12634 case DW_TAG_interface_type:
12635 case DW_TAG_structure_type:
12636 case DW_TAG_union_type:
12637 case DW_TAG_module:
12638 parent_type = read_type_die (parent, cu);
12639 if (TYPE_TAG_NAME (parent_type) != NULL)
12640 return TYPE_TAG_NAME (parent_type);
12641 else
12642 /* An anonymous structure is only allowed non-static data
12643 members; no typedefs, no member functions, et cetera.
12644 So it does not need a prefix. */
12645 return "";
12646 case DW_TAG_compile_unit:
12647 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12648 if (cu->language == language_cplus
12649 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12650 && die->child != NULL
12651 && (die->tag == DW_TAG_class_type
12652 || die->tag == DW_TAG_structure_type
12653 || die->tag == DW_TAG_union_type))
12654 {
12655 char *name = guess_full_die_structure_name (die, cu);
12656 if (name != NULL)
12657 return name;
12658 }
12659 return "";
12660 default:
12661 return determine_prefix (parent, cu);
12662 }
12663 }
12664
12665 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12666 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12667 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12668 an obconcat, otherwise allocate storage for the result. The CU argument is
12669 used to determine the language and hence, the appropriate separator. */
12670
12671 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12672
12673 static char *
12674 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12675 int physname, struct dwarf2_cu *cu)
12676 {
12677 const char *lead = "";
12678 const char *sep;
12679
12680 if (suffix == NULL || suffix[0] == '\0'
12681 || prefix == NULL || prefix[0] == '\0')
12682 sep = "";
12683 else if (cu->language == language_java)
12684 sep = ".";
12685 else if (cu->language == language_fortran && physname)
12686 {
12687 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12688 DW_AT_MIPS_linkage_name is preferred and used instead. */
12689
12690 lead = "__";
12691 sep = "_MOD_";
12692 }
12693 else
12694 sep = "::";
12695
12696 if (prefix == NULL)
12697 prefix = "";
12698 if (suffix == NULL)
12699 suffix = "";
12700
12701 if (obs == NULL)
12702 {
12703 char *retval
12704 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12705
12706 strcpy (retval, lead);
12707 strcat (retval, prefix);
12708 strcat (retval, sep);
12709 strcat (retval, suffix);
12710 return retval;
12711 }
12712 else
12713 {
12714 /* We have an obstack. */
12715 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12716 }
12717 }
12718
12719 /* Return sibling of die, NULL if no sibling. */
12720
12721 static struct die_info *
12722 sibling_die (struct die_info *die)
12723 {
12724 return die->sibling;
12725 }
12726
12727 /* Get name of a die, return NULL if not found. */
12728
12729 static char *
12730 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12731 struct obstack *obstack)
12732 {
12733 if (name && cu->language == language_cplus)
12734 {
12735 char *canon_name = cp_canonicalize_string (name);
12736
12737 if (canon_name != NULL)
12738 {
12739 if (strcmp (canon_name, name) != 0)
12740 name = obsavestring (canon_name, strlen (canon_name),
12741 obstack);
12742 xfree (canon_name);
12743 }
12744 }
12745
12746 return name;
12747 }
12748
12749 /* Get name of a die, return NULL if not found. */
12750
12751 static char *
12752 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12753 {
12754 struct attribute *attr;
12755
12756 attr = dwarf2_attr (die, DW_AT_name, cu);
12757 if ((!attr || !DW_STRING (attr))
12758 && die->tag != DW_TAG_class_type
12759 && die->tag != DW_TAG_interface_type
12760 && die->tag != DW_TAG_structure_type
12761 && die->tag != DW_TAG_union_type)
12762 return NULL;
12763
12764 switch (die->tag)
12765 {
12766 case DW_TAG_compile_unit:
12767 /* Compilation units have a DW_AT_name that is a filename, not
12768 a source language identifier. */
12769 case DW_TAG_enumeration_type:
12770 case DW_TAG_enumerator:
12771 /* These tags always have simple identifiers already; no need
12772 to canonicalize them. */
12773 return DW_STRING (attr);
12774
12775 case DW_TAG_subprogram:
12776 /* Java constructors will all be named "<init>", so return
12777 the class name when we see this special case. */
12778 if (cu->language == language_java
12779 && DW_STRING (attr) != NULL
12780 && strcmp (DW_STRING (attr), "<init>") == 0)
12781 {
12782 struct dwarf2_cu *spec_cu = cu;
12783 struct die_info *spec_die;
12784
12785 /* GCJ will output '<init>' for Java constructor names.
12786 For this special case, return the name of the parent class. */
12787
12788 /* GCJ may output suprogram DIEs with AT_specification set.
12789 If so, use the name of the specified DIE. */
12790 spec_die = die_specification (die, &spec_cu);
12791 if (spec_die != NULL)
12792 return dwarf2_name (spec_die, spec_cu);
12793
12794 do
12795 {
12796 die = die->parent;
12797 if (die->tag == DW_TAG_class_type)
12798 return dwarf2_name (die, cu);
12799 }
12800 while (die->tag != DW_TAG_compile_unit);
12801 }
12802 break;
12803
12804 case DW_TAG_class_type:
12805 case DW_TAG_interface_type:
12806 case DW_TAG_structure_type:
12807 case DW_TAG_union_type:
12808 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12809 structures or unions. These were of the form "._%d" in GCC 4.1,
12810 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12811 and GCC 4.4. We work around this problem by ignoring these. */
12812 if (attr && DW_STRING (attr)
12813 && (strncmp (DW_STRING (attr), "._", 2) == 0
12814 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12815 return NULL;
12816
12817 /* GCC might emit a nameless typedef that has a linkage name. See
12818 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12819 if (!attr || DW_STRING (attr) == NULL)
12820 {
12821 char *demangled = NULL;
12822
12823 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12824 if (attr == NULL)
12825 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12826
12827 if (attr == NULL || DW_STRING (attr) == NULL)
12828 return NULL;
12829
12830 /* Avoid demangling DW_STRING (attr) the second time on a second
12831 call for the same DIE. */
12832 if (!DW_STRING_IS_CANONICAL (attr))
12833 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12834
12835 if (demangled)
12836 {
12837 char *base;
12838
12839 /* FIXME: we already did this for the partial symbol... */
12840 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12841 &cu->objfile->objfile_obstack);
12842 DW_STRING_IS_CANONICAL (attr) = 1;
12843 xfree (demangled);
12844
12845 /* Strip any leading namespaces/classes, keep only the base name.
12846 DW_AT_name for named DIEs does not contain the prefixes. */
12847 base = strrchr (DW_STRING (attr), ':');
12848 if (base && base > DW_STRING (attr) && base[-1] == ':')
12849 return &base[1];
12850 else
12851 return DW_STRING (attr);
12852 }
12853 }
12854 break;
12855
12856 default:
12857 break;
12858 }
12859
12860 if (!DW_STRING_IS_CANONICAL (attr))
12861 {
12862 DW_STRING (attr)
12863 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12864 &cu->objfile->objfile_obstack);
12865 DW_STRING_IS_CANONICAL (attr) = 1;
12866 }
12867 return DW_STRING (attr);
12868 }
12869
12870 /* Return the die that this die in an extension of, or NULL if there
12871 is none. *EXT_CU is the CU containing DIE on input, and the CU
12872 containing the return value on output. */
12873
12874 static struct die_info *
12875 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12876 {
12877 struct attribute *attr;
12878
12879 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12880 if (attr == NULL)
12881 return NULL;
12882
12883 return follow_die_ref (die, attr, ext_cu);
12884 }
12885
12886 /* Convert a DIE tag into its string name. */
12887
12888 static char *
12889 dwarf_tag_name (unsigned tag)
12890 {
12891 switch (tag)
12892 {
12893 case DW_TAG_padding:
12894 return "DW_TAG_padding";
12895 case DW_TAG_array_type:
12896 return "DW_TAG_array_type";
12897 case DW_TAG_class_type:
12898 return "DW_TAG_class_type";
12899 case DW_TAG_entry_point:
12900 return "DW_TAG_entry_point";
12901 case DW_TAG_enumeration_type:
12902 return "DW_TAG_enumeration_type";
12903 case DW_TAG_formal_parameter:
12904 return "DW_TAG_formal_parameter";
12905 case DW_TAG_imported_declaration:
12906 return "DW_TAG_imported_declaration";
12907 case DW_TAG_label:
12908 return "DW_TAG_label";
12909 case DW_TAG_lexical_block:
12910 return "DW_TAG_lexical_block";
12911 case DW_TAG_member:
12912 return "DW_TAG_member";
12913 case DW_TAG_pointer_type:
12914 return "DW_TAG_pointer_type";
12915 case DW_TAG_reference_type:
12916 return "DW_TAG_reference_type";
12917 case DW_TAG_compile_unit:
12918 return "DW_TAG_compile_unit";
12919 case DW_TAG_string_type:
12920 return "DW_TAG_string_type";
12921 case DW_TAG_structure_type:
12922 return "DW_TAG_structure_type";
12923 case DW_TAG_subroutine_type:
12924 return "DW_TAG_subroutine_type";
12925 case DW_TAG_typedef:
12926 return "DW_TAG_typedef";
12927 case DW_TAG_union_type:
12928 return "DW_TAG_union_type";
12929 case DW_TAG_unspecified_parameters:
12930 return "DW_TAG_unspecified_parameters";
12931 case DW_TAG_variant:
12932 return "DW_TAG_variant";
12933 case DW_TAG_common_block:
12934 return "DW_TAG_common_block";
12935 case DW_TAG_common_inclusion:
12936 return "DW_TAG_common_inclusion";
12937 case DW_TAG_inheritance:
12938 return "DW_TAG_inheritance";
12939 case DW_TAG_inlined_subroutine:
12940 return "DW_TAG_inlined_subroutine";
12941 case DW_TAG_module:
12942 return "DW_TAG_module";
12943 case DW_TAG_ptr_to_member_type:
12944 return "DW_TAG_ptr_to_member_type";
12945 case DW_TAG_set_type:
12946 return "DW_TAG_set_type";
12947 case DW_TAG_subrange_type:
12948 return "DW_TAG_subrange_type";
12949 case DW_TAG_with_stmt:
12950 return "DW_TAG_with_stmt";
12951 case DW_TAG_access_declaration:
12952 return "DW_TAG_access_declaration";
12953 case DW_TAG_base_type:
12954 return "DW_TAG_base_type";
12955 case DW_TAG_catch_block:
12956 return "DW_TAG_catch_block";
12957 case DW_TAG_const_type:
12958 return "DW_TAG_const_type";
12959 case DW_TAG_constant:
12960 return "DW_TAG_constant";
12961 case DW_TAG_enumerator:
12962 return "DW_TAG_enumerator";
12963 case DW_TAG_file_type:
12964 return "DW_TAG_file_type";
12965 case DW_TAG_friend:
12966 return "DW_TAG_friend";
12967 case DW_TAG_namelist:
12968 return "DW_TAG_namelist";
12969 case DW_TAG_namelist_item:
12970 return "DW_TAG_namelist_item";
12971 case DW_TAG_packed_type:
12972 return "DW_TAG_packed_type";
12973 case DW_TAG_subprogram:
12974 return "DW_TAG_subprogram";
12975 case DW_TAG_template_type_param:
12976 return "DW_TAG_template_type_param";
12977 case DW_TAG_template_value_param:
12978 return "DW_TAG_template_value_param";
12979 case DW_TAG_thrown_type:
12980 return "DW_TAG_thrown_type";
12981 case DW_TAG_try_block:
12982 return "DW_TAG_try_block";
12983 case DW_TAG_variant_part:
12984 return "DW_TAG_variant_part";
12985 case DW_TAG_variable:
12986 return "DW_TAG_variable";
12987 case DW_TAG_volatile_type:
12988 return "DW_TAG_volatile_type";
12989 case DW_TAG_dwarf_procedure:
12990 return "DW_TAG_dwarf_procedure";
12991 case DW_TAG_restrict_type:
12992 return "DW_TAG_restrict_type";
12993 case DW_TAG_interface_type:
12994 return "DW_TAG_interface_type";
12995 case DW_TAG_namespace:
12996 return "DW_TAG_namespace";
12997 case DW_TAG_imported_module:
12998 return "DW_TAG_imported_module";
12999 case DW_TAG_unspecified_type:
13000 return "DW_TAG_unspecified_type";
13001 case DW_TAG_partial_unit:
13002 return "DW_TAG_partial_unit";
13003 case DW_TAG_imported_unit:
13004 return "DW_TAG_imported_unit";
13005 case DW_TAG_condition:
13006 return "DW_TAG_condition";
13007 case DW_TAG_shared_type:
13008 return "DW_TAG_shared_type";
13009 case DW_TAG_type_unit:
13010 return "DW_TAG_type_unit";
13011 case DW_TAG_MIPS_loop:
13012 return "DW_TAG_MIPS_loop";
13013 case DW_TAG_HP_array_descriptor:
13014 return "DW_TAG_HP_array_descriptor";
13015 case DW_TAG_format_label:
13016 return "DW_TAG_format_label";
13017 case DW_TAG_function_template:
13018 return "DW_TAG_function_template";
13019 case DW_TAG_class_template:
13020 return "DW_TAG_class_template";
13021 case DW_TAG_GNU_BINCL:
13022 return "DW_TAG_GNU_BINCL";
13023 case DW_TAG_GNU_EINCL:
13024 return "DW_TAG_GNU_EINCL";
13025 case DW_TAG_upc_shared_type:
13026 return "DW_TAG_upc_shared_type";
13027 case DW_TAG_upc_strict_type:
13028 return "DW_TAG_upc_strict_type";
13029 case DW_TAG_upc_relaxed_type:
13030 return "DW_TAG_upc_relaxed_type";
13031 case DW_TAG_PGI_kanji_type:
13032 return "DW_TAG_PGI_kanji_type";
13033 case DW_TAG_PGI_interface_block:
13034 return "DW_TAG_PGI_interface_block";
13035 case DW_TAG_GNU_call_site:
13036 return "DW_TAG_GNU_call_site";
13037 default:
13038 return "DW_TAG_<unknown>";
13039 }
13040 }
13041
13042 /* Convert a DWARF attribute code into its string name. */
13043
13044 static char *
13045 dwarf_attr_name (unsigned attr)
13046 {
13047 switch (attr)
13048 {
13049 case DW_AT_sibling:
13050 return "DW_AT_sibling";
13051 case DW_AT_location:
13052 return "DW_AT_location";
13053 case DW_AT_name:
13054 return "DW_AT_name";
13055 case DW_AT_ordering:
13056 return "DW_AT_ordering";
13057 case DW_AT_subscr_data:
13058 return "DW_AT_subscr_data";
13059 case DW_AT_byte_size:
13060 return "DW_AT_byte_size";
13061 case DW_AT_bit_offset:
13062 return "DW_AT_bit_offset";
13063 case DW_AT_bit_size:
13064 return "DW_AT_bit_size";
13065 case DW_AT_element_list:
13066 return "DW_AT_element_list";
13067 case DW_AT_stmt_list:
13068 return "DW_AT_stmt_list";
13069 case DW_AT_low_pc:
13070 return "DW_AT_low_pc";
13071 case DW_AT_high_pc:
13072 return "DW_AT_high_pc";
13073 case DW_AT_language:
13074 return "DW_AT_language";
13075 case DW_AT_member:
13076 return "DW_AT_member";
13077 case DW_AT_discr:
13078 return "DW_AT_discr";
13079 case DW_AT_discr_value:
13080 return "DW_AT_discr_value";
13081 case DW_AT_visibility:
13082 return "DW_AT_visibility";
13083 case DW_AT_import:
13084 return "DW_AT_import";
13085 case DW_AT_string_length:
13086 return "DW_AT_string_length";
13087 case DW_AT_common_reference:
13088 return "DW_AT_common_reference";
13089 case DW_AT_comp_dir:
13090 return "DW_AT_comp_dir";
13091 case DW_AT_const_value:
13092 return "DW_AT_const_value";
13093 case DW_AT_containing_type:
13094 return "DW_AT_containing_type";
13095 case DW_AT_default_value:
13096 return "DW_AT_default_value";
13097 case DW_AT_inline:
13098 return "DW_AT_inline";
13099 case DW_AT_is_optional:
13100 return "DW_AT_is_optional";
13101 case DW_AT_lower_bound:
13102 return "DW_AT_lower_bound";
13103 case DW_AT_producer:
13104 return "DW_AT_producer";
13105 case DW_AT_prototyped:
13106 return "DW_AT_prototyped";
13107 case DW_AT_return_addr:
13108 return "DW_AT_return_addr";
13109 case DW_AT_start_scope:
13110 return "DW_AT_start_scope";
13111 case DW_AT_bit_stride:
13112 return "DW_AT_bit_stride";
13113 case DW_AT_upper_bound:
13114 return "DW_AT_upper_bound";
13115 case DW_AT_abstract_origin:
13116 return "DW_AT_abstract_origin";
13117 case DW_AT_accessibility:
13118 return "DW_AT_accessibility";
13119 case DW_AT_address_class:
13120 return "DW_AT_address_class";
13121 case DW_AT_artificial:
13122 return "DW_AT_artificial";
13123 case DW_AT_base_types:
13124 return "DW_AT_base_types";
13125 case DW_AT_calling_convention:
13126 return "DW_AT_calling_convention";
13127 case DW_AT_count:
13128 return "DW_AT_count";
13129 case DW_AT_data_member_location:
13130 return "DW_AT_data_member_location";
13131 case DW_AT_decl_column:
13132 return "DW_AT_decl_column";
13133 case DW_AT_decl_file:
13134 return "DW_AT_decl_file";
13135 case DW_AT_decl_line:
13136 return "DW_AT_decl_line";
13137 case DW_AT_declaration:
13138 return "DW_AT_declaration";
13139 case DW_AT_discr_list:
13140 return "DW_AT_discr_list";
13141 case DW_AT_encoding:
13142 return "DW_AT_encoding";
13143 case DW_AT_external:
13144 return "DW_AT_external";
13145 case DW_AT_frame_base:
13146 return "DW_AT_frame_base";
13147 case DW_AT_friend:
13148 return "DW_AT_friend";
13149 case DW_AT_identifier_case:
13150 return "DW_AT_identifier_case";
13151 case DW_AT_macro_info:
13152 return "DW_AT_macro_info";
13153 case DW_AT_namelist_items:
13154 return "DW_AT_namelist_items";
13155 case DW_AT_priority:
13156 return "DW_AT_priority";
13157 case DW_AT_segment:
13158 return "DW_AT_segment";
13159 case DW_AT_specification:
13160 return "DW_AT_specification";
13161 case DW_AT_static_link:
13162 return "DW_AT_static_link";
13163 case DW_AT_type:
13164 return "DW_AT_type";
13165 case DW_AT_use_location:
13166 return "DW_AT_use_location";
13167 case DW_AT_variable_parameter:
13168 return "DW_AT_variable_parameter";
13169 case DW_AT_virtuality:
13170 return "DW_AT_virtuality";
13171 case DW_AT_vtable_elem_location:
13172 return "DW_AT_vtable_elem_location";
13173 /* DWARF 3 values. */
13174 case DW_AT_allocated:
13175 return "DW_AT_allocated";
13176 case DW_AT_associated:
13177 return "DW_AT_associated";
13178 case DW_AT_data_location:
13179 return "DW_AT_data_location";
13180 case DW_AT_byte_stride:
13181 return "DW_AT_byte_stride";
13182 case DW_AT_entry_pc:
13183 return "DW_AT_entry_pc";
13184 case DW_AT_use_UTF8:
13185 return "DW_AT_use_UTF8";
13186 case DW_AT_extension:
13187 return "DW_AT_extension";
13188 case DW_AT_ranges:
13189 return "DW_AT_ranges";
13190 case DW_AT_trampoline:
13191 return "DW_AT_trampoline";
13192 case DW_AT_call_column:
13193 return "DW_AT_call_column";
13194 case DW_AT_call_file:
13195 return "DW_AT_call_file";
13196 case DW_AT_call_line:
13197 return "DW_AT_call_line";
13198 case DW_AT_description:
13199 return "DW_AT_description";
13200 case DW_AT_binary_scale:
13201 return "DW_AT_binary_scale";
13202 case DW_AT_decimal_scale:
13203 return "DW_AT_decimal_scale";
13204 case DW_AT_small:
13205 return "DW_AT_small";
13206 case DW_AT_decimal_sign:
13207 return "DW_AT_decimal_sign";
13208 case DW_AT_digit_count:
13209 return "DW_AT_digit_count";
13210 case DW_AT_picture_string:
13211 return "DW_AT_picture_string";
13212 case DW_AT_mutable:
13213 return "DW_AT_mutable";
13214 case DW_AT_threads_scaled:
13215 return "DW_AT_threads_scaled";
13216 case DW_AT_explicit:
13217 return "DW_AT_explicit";
13218 case DW_AT_object_pointer:
13219 return "DW_AT_object_pointer";
13220 case DW_AT_endianity:
13221 return "DW_AT_endianity";
13222 case DW_AT_elemental:
13223 return "DW_AT_elemental";
13224 case DW_AT_pure:
13225 return "DW_AT_pure";
13226 case DW_AT_recursive:
13227 return "DW_AT_recursive";
13228 /* DWARF 4 values. */
13229 case DW_AT_signature:
13230 return "DW_AT_signature";
13231 case DW_AT_linkage_name:
13232 return "DW_AT_linkage_name";
13233 /* SGI/MIPS extensions. */
13234 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13235 case DW_AT_MIPS_fde:
13236 return "DW_AT_MIPS_fde";
13237 #endif
13238 case DW_AT_MIPS_loop_begin:
13239 return "DW_AT_MIPS_loop_begin";
13240 case DW_AT_MIPS_tail_loop_begin:
13241 return "DW_AT_MIPS_tail_loop_begin";
13242 case DW_AT_MIPS_epilog_begin:
13243 return "DW_AT_MIPS_epilog_begin";
13244 case DW_AT_MIPS_loop_unroll_factor:
13245 return "DW_AT_MIPS_loop_unroll_factor";
13246 case DW_AT_MIPS_software_pipeline_depth:
13247 return "DW_AT_MIPS_software_pipeline_depth";
13248 case DW_AT_MIPS_linkage_name:
13249 return "DW_AT_MIPS_linkage_name";
13250 case DW_AT_MIPS_stride:
13251 return "DW_AT_MIPS_stride";
13252 case DW_AT_MIPS_abstract_name:
13253 return "DW_AT_MIPS_abstract_name";
13254 case DW_AT_MIPS_clone_origin:
13255 return "DW_AT_MIPS_clone_origin";
13256 case DW_AT_MIPS_has_inlines:
13257 return "DW_AT_MIPS_has_inlines";
13258 /* HP extensions. */
13259 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13260 case DW_AT_HP_block_index:
13261 return "DW_AT_HP_block_index";
13262 #endif
13263 case DW_AT_HP_unmodifiable:
13264 return "DW_AT_HP_unmodifiable";
13265 case DW_AT_HP_actuals_stmt_list:
13266 return "DW_AT_HP_actuals_stmt_list";
13267 case DW_AT_HP_proc_per_section:
13268 return "DW_AT_HP_proc_per_section";
13269 case DW_AT_HP_raw_data_ptr:
13270 return "DW_AT_HP_raw_data_ptr";
13271 case DW_AT_HP_pass_by_reference:
13272 return "DW_AT_HP_pass_by_reference";
13273 case DW_AT_HP_opt_level:
13274 return "DW_AT_HP_opt_level";
13275 case DW_AT_HP_prof_version_id:
13276 return "DW_AT_HP_prof_version_id";
13277 case DW_AT_HP_opt_flags:
13278 return "DW_AT_HP_opt_flags";
13279 case DW_AT_HP_cold_region_low_pc:
13280 return "DW_AT_HP_cold_region_low_pc";
13281 case DW_AT_HP_cold_region_high_pc:
13282 return "DW_AT_HP_cold_region_high_pc";
13283 case DW_AT_HP_all_variables_modifiable:
13284 return "DW_AT_HP_all_variables_modifiable";
13285 case DW_AT_HP_linkage_name:
13286 return "DW_AT_HP_linkage_name";
13287 case DW_AT_HP_prof_flags:
13288 return "DW_AT_HP_prof_flags";
13289 /* GNU extensions. */
13290 case DW_AT_sf_names:
13291 return "DW_AT_sf_names";
13292 case DW_AT_src_info:
13293 return "DW_AT_src_info";
13294 case DW_AT_mac_info:
13295 return "DW_AT_mac_info";
13296 case DW_AT_src_coords:
13297 return "DW_AT_src_coords";
13298 case DW_AT_body_begin:
13299 return "DW_AT_body_begin";
13300 case DW_AT_body_end:
13301 return "DW_AT_body_end";
13302 case DW_AT_GNU_vector:
13303 return "DW_AT_GNU_vector";
13304 case DW_AT_GNU_odr_signature:
13305 return "DW_AT_GNU_odr_signature";
13306 /* VMS extensions. */
13307 case DW_AT_VMS_rtnbeg_pd_address:
13308 return "DW_AT_VMS_rtnbeg_pd_address";
13309 /* UPC extension. */
13310 case DW_AT_upc_threads_scaled:
13311 return "DW_AT_upc_threads_scaled";
13312 /* PGI (STMicroelectronics) extensions. */
13313 case DW_AT_PGI_lbase:
13314 return "DW_AT_PGI_lbase";
13315 case DW_AT_PGI_soffset:
13316 return "DW_AT_PGI_soffset";
13317 case DW_AT_PGI_lstride:
13318 return "DW_AT_PGI_lstride";
13319 default:
13320 return "DW_AT_<unknown>";
13321 }
13322 }
13323
13324 /* Convert a DWARF value form code into its string name. */
13325
13326 static char *
13327 dwarf_form_name (unsigned form)
13328 {
13329 switch (form)
13330 {
13331 case DW_FORM_addr:
13332 return "DW_FORM_addr";
13333 case DW_FORM_block2:
13334 return "DW_FORM_block2";
13335 case DW_FORM_block4:
13336 return "DW_FORM_block4";
13337 case DW_FORM_data2:
13338 return "DW_FORM_data2";
13339 case DW_FORM_data4:
13340 return "DW_FORM_data4";
13341 case DW_FORM_data8:
13342 return "DW_FORM_data8";
13343 case DW_FORM_string:
13344 return "DW_FORM_string";
13345 case DW_FORM_block:
13346 return "DW_FORM_block";
13347 case DW_FORM_block1:
13348 return "DW_FORM_block1";
13349 case DW_FORM_data1:
13350 return "DW_FORM_data1";
13351 case DW_FORM_flag:
13352 return "DW_FORM_flag";
13353 case DW_FORM_sdata:
13354 return "DW_FORM_sdata";
13355 case DW_FORM_strp:
13356 return "DW_FORM_strp";
13357 case DW_FORM_udata:
13358 return "DW_FORM_udata";
13359 case DW_FORM_ref_addr:
13360 return "DW_FORM_ref_addr";
13361 case DW_FORM_ref1:
13362 return "DW_FORM_ref1";
13363 case DW_FORM_ref2:
13364 return "DW_FORM_ref2";
13365 case DW_FORM_ref4:
13366 return "DW_FORM_ref4";
13367 case DW_FORM_ref8:
13368 return "DW_FORM_ref8";
13369 case DW_FORM_ref_udata:
13370 return "DW_FORM_ref_udata";
13371 case DW_FORM_indirect:
13372 return "DW_FORM_indirect";
13373 case DW_FORM_sec_offset:
13374 return "DW_FORM_sec_offset";
13375 case DW_FORM_exprloc:
13376 return "DW_FORM_exprloc";
13377 case DW_FORM_flag_present:
13378 return "DW_FORM_flag_present";
13379 case DW_FORM_ref_sig8:
13380 return "DW_FORM_ref_sig8";
13381 default:
13382 return "DW_FORM_<unknown>";
13383 }
13384 }
13385
13386 /* Convert a DWARF stack opcode into its string name. */
13387
13388 const char *
13389 dwarf_stack_op_name (unsigned op)
13390 {
13391 switch (op)
13392 {
13393 case DW_OP_addr:
13394 return "DW_OP_addr";
13395 case DW_OP_deref:
13396 return "DW_OP_deref";
13397 case DW_OP_const1u:
13398 return "DW_OP_const1u";
13399 case DW_OP_const1s:
13400 return "DW_OP_const1s";
13401 case DW_OP_const2u:
13402 return "DW_OP_const2u";
13403 case DW_OP_const2s:
13404 return "DW_OP_const2s";
13405 case DW_OP_const4u:
13406 return "DW_OP_const4u";
13407 case DW_OP_const4s:
13408 return "DW_OP_const4s";
13409 case DW_OP_const8u:
13410 return "DW_OP_const8u";
13411 case DW_OP_const8s:
13412 return "DW_OP_const8s";
13413 case DW_OP_constu:
13414 return "DW_OP_constu";
13415 case DW_OP_consts:
13416 return "DW_OP_consts";
13417 case DW_OP_dup:
13418 return "DW_OP_dup";
13419 case DW_OP_drop:
13420 return "DW_OP_drop";
13421 case DW_OP_over:
13422 return "DW_OP_over";
13423 case DW_OP_pick:
13424 return "DW_OP_pick";
13425 case DW_OP_swap:
13426 return "DW_OP_swap";
13427 case DW_OP_rot:
13428 return "DW_OP_rot";
13429 case DW_OP_xderef:
13430 return "DW_OP_xderef";
13431 case DW_OP_abs:
13432 return "DW_OP_abs";
13433 case DW_OP_and:
13434 return "DW_OP_and";
13435 case DW_OP_div:
13436 return "DW_OP_div";
13437 case DW_OP_minus:
13438 return "DW_OP_minus";
13439 case DW_OP_mod:
13440 return "DW_OP_mod";
13441 case DW_OP_mul:
13442 return "DW_OP_mul";
13443 case DW_OP_neg:
13444 return "DW_OP_neg";
13445 case DW_OP_not:
13446 return "DW_OP_not";
13447 case DW_OP_or:
13448 return "DW_OP_or";
13449 case DW_OP_plus:
13450 return "DW_OP_plus";
13451 case DW_OP_plus_uconst:
13452 return "DW_OP_plus_uconst";
13453 case DW_OP_shl:
13454 return "DW_OP_shl";
13455 case DW_OP_shr:
13456 return "DW_OP_shr";
13457 case DW_OP_shra:
13458 return "DW_OP_shra";
13459 case DW_OP_xor:
13460 return "DW_OP_xor";
13461 case DW_OP_bra:
13462 return "DW_OP_bra";
13463 case DW_OP_eq:
13464 return "DW_OP_eq";
13465 case DW_OP_ge:
13466 return "DW_OP_ge";
13467 case DW_OP_gt:
13468 return "DW_OP_gt";
13469 case DW_OP_le:
13470 return "DW_OP_le";
13471 case DW_OP_lt:
13472 return "DW_OP_lt";
13473 case DW_OP_ne:
13474 return "DW_OP_ne";
13475 case DW_OP_skip:
13476 return "DW_OP_skip";
13477 case DW_OP_lit0:
13478 return "DW_OP_lit0";
13479 case DW_OP_lit1:
13480 return "DW_OP_lit1";
13481 case DW_OP_lit2:
13482 return "DW_OP_lit2";
13483 case DW_OP_lit3:
13484 return "DW_OP_lit3";
13485 case DW_OP_lit4:
13486 return "DW_OP_lit4";
13487 case DW_OP_lit5:
13488 return "DW_OP_lit5";
13489 case DW_OP_lit6:
13490 return "DW_OP_lit6";
13491 case DW_OP_lit7:
13492 return "DW_OP_lit7";
13493 case DW_OP_lit8:
13494 return "DW_OP_lit8";
13495 case DW_OP_lit9:
13496 return "DW_OP_lit9";
13497 case DW_OP_lit10:
13498 return "DW_OP_lit10";
13499 case DW_OP_lit11:
13500 return "DW_OP_lit11";
13501 case DW_OP_lit12:
13502 return "DW_OP_lit12";
13503 case DW_OP_lit13:
13504 return "DW_OP_lit13";
13505 case DW_OP_lit14:
13506 return "DW_OP_lit14";
13507 case DW_OP_lit15:
13508 return "DW_OP_lit15";
13509 case DW_OP_lit16:
13510 return "DW_OP_lit16";
13511 case DW_OP_lit17:
13512 return "DW_OP_lit17";
13513 case DW_OP_lit18:
13514 return "DW_OP_lit18";
13515 case DW_OP_lit19:
13516 return "DW_OP_lit19";
13517 case DW_OP_lit20:
13518 return "DW_OP_lit20";
13519 case DW_OP_lit21:
13520 return "DW_OP_lit21";
13521 case DW_OP_lit22:
13522 return "DW_OP_lit22";
13523 case DW_OP_lit23:
13524 return "DW_OP_lit23";
13525 case DW_OP_lit24:
13526 return "DW_OP_lit24";
13527 case DW_OP_lit25:
13528 return "DW_OP_lit25";
13529 case DW_OP_lit26:
13530 return "DW_OP_lit26";
13531 case DW_OP_lit27:
13532 return "DW_OP_lit27";
13533 case DW_OP_lit28:
13534 return "DW_OP_lit28";
13535 case DW_OP_lit29:
13536 return "DW_OP_lit29";
13537 case DW_OP_lit30:
13538 return "DW_OP_lit30";
13539 case DW_OP_lit31:
13540 return "DW_OP_lit31";
13541 case DW_OP_reg0:
13542 return "DW_OP_reg0";
13543 case DW_OP_reg1:
13544 return "DW_OP_reg1";
13545 case DW_OP_reg2:
13546 return "DW_OP_reg2";
13547 case DW_OP_reg3:
13548 return "DW_OP_reg3";
13549 case DW_OP_reg4:
13550 return "DW_OP_reg4";
13551 case DW_OP_reg5:
13552 return "DW_OP_reg5";
13553 case DW_OP_reg6:
13554 return "DW_OP_reg6";
13555 case DW_OP_reg7:
13556 return "DW_OP_reg7";
13557 case DW_OP_reg8:
13558 return "DW_OP_reg8";
13559 case DW_OP_reg9:
13560 return "DW_OP_reg9";
13561 case DW_OP_reg10:
13562 return "DW_OP_reg10";
13563 case DW_OP_reg11:
13564 return "DW_OP_reg11";
13565 case DW_OP_reg12:
13566 return "DW_OP_reg12";
13567 case DW_OP_reg13:
13568 return "DW_OP_reg13";
13569 case DW_OP_reg14:
13570 return "DW_OP_reg14";
13571 case DW_OP_reg15:
13572 return "DW_OP_reg15";
13573 case DW_OP_reg16:
13574 return "DW_OP_reg16";
13575 case DW_OP_reg17:
13576 return "DW_OP_reg17";
13577 case DW_OP_reg18:
13578 return "DW_OP_reg18";
13579 case DW_OP_reg19:
13580 return "DW_OP_reg19";
13581 case DW_OP_reg20:
13582 return "DW_OP_reg20";
13583 case DW_OP_reg21:
13584 return "DW_OP_reg21";
13585 case DW_OP_reg22:
13586 return "DW_OP_reg22";
13587 case DW_OP_reg23:
13588 return "DW_OP_reg23";
13589 case DW_OP_reg24:
13590 return "DW_OP_reg24";
13591 case DW_OP_reg25:
13592 return "DW_OP_reg25";
13593 case DW_OP_reg26:
13594 return "DW_OP_reg26";
13595 case DW_OP_reg27:
13596 return "DW_OP_reg27";
13597 case DW_OP_reg28:
13598 return "DW_OP_reg28";
13599 case DW_OP_reg29:
13600 return "DW_OP_reg29";
13601 case DW_OP_reg30:
13602 return "DW_OP_reg30";
13603 case DW_OP_reg31:
13604 return "DW_OP_reg31";
13605 case DW_OP_breg0:
13606 return "DW_OP_breg0";
13607 case DW_OP_breg1:
13608 return "DW_OP_breg1";
13609 case DW_OP_breg2:
13610 return "DW_OP_breg2";
13611 case DW_OP_breg3:
13612 return "DW_OP_breg3";
13613 case DW_OP_breg4:
13614 return "DW_OP_breg4";
13615 case DW_OP_breg5:
13616 return "DW_OP_breg5";
13617 case DW_OP_breg6:
13618 return "DW_OP_breg6";
13619 case DW_OP_breg7:
13620 return "DW_OP_breg7";
13621 case DW_OP_breg8:
13622 return "DW_OP_breg8";
13623 case DW_OP_breg9:
13624 return "DW_OP_breg9";
13625 case DW_OP_breg10:
13626 return "DW_OP_breg10";
13627 case DW_OP_breg11:
13628 return "DW_OP_breg11";
13629 case DW_OP_breg12:
13630 return "DW_OP_breg12";
13631 case DW_OP_breg13:
13632 return "DW_OP_breg13";
13633 case DW_OP_breg14:
13634 return "DW_OP_breg14";
13635 case DW_OP_breg15:
13636 return "DW_OP_breg15";
13637 case DW_OP_breg16:
13638 return "DW_OP_breg16";
13639 case DW_OP_breg17:
13640 return "DW_OP_breg17";
13641 case DW_OP_breg18:
13642 return "DW_OP_breg18";
13643 case DW_OP_breg19:
13644 return "DW_OP_breg19";
13645 case DW_OP_breg20:
13646 return "DW_OP_breg20";
13647 case DW_OP_breg21:
13648 return "DW_OP_breg21";
13649 case DW_OP_breg22:
13650 return "DW_OP_breg22";
13651 case DW_OP_breg23:
13652 return "DW_OP_breg23";
13653 case DW_OP_breg24:
13654 return "DW_OP_breg24";
13655 case DW_OP_breg25:
13656 return "DW_OP_breg25";
13657 case DW_OP_breg26:
13658 return "DW_OP_breg26";
13659 case DW_OP_breg27:
13660 return "DW_OP_breg27";
13661 case DW_OP_breg28:
13662 return "DW_OP_breg28";
13663 case DW_OP_breg29:
13664 return "DW_OP_breg29";
13665 case DW_OP_breg30:
13666 return "DW_OP_breg30";
13667 case DW_OP_breg31:
13668 return "DW_OP_breg31";
13669 case DW_OP_regx:
13670 return "DW_OP_regx";
13671 case DW_OP_fbreg:
13672 return "DW_OP_fbreg";
13673 case DW_OP_bregx:
13674 return "DW_OP_bregx";
13675 case DW_OP_piece:
13676 return "DW_OP_piece";
13677 case DW_OP_deref_size:
13678 return "DW_OP_deref_size";
13679 case DW_OP_xderef_size:
13680 return "DW_OP_xderef_size";
13681 case DW_OP_nop:
13682 return "DW_OP_nop";
13683 /* DWARF 3 extensions. */
13684 case DW_OP_push_object_address:
13685 return "DW_OP_push_object_address";
13686 case DW_OP_call2:
13687 return "DW_OP_call2";
13688 case DW_OP_call4:
13689 return "DW_OP_call4";
13690 case DW_OP_call_ref:
13691 return "DW_OP_call_ref";
13692 case DW_OP_form_tls_address:
13693 return "DW_OP_form_tls_address";
13694 case DW_OP_call_frame_cfa:
13695 return "DW_OP_call_frame_cfa";
13696 case DW_OP_bit_piece:
13697 return "DW_OP_bit_piece";
13698 /* DWARF 4 extensions. */
13699 case DW_OP_implicit_value:
13700 return "DW_OP_implicit_value";
13701 case DW_OP_stack_value:
13702 return "DW_OP_stack_value";
13703 /* GNU extensions. */
13704 case DW_OP_GNU_push_tls_address:
13705 return "DW_OP_GNU_push_tls_address";
13706 case DW_OP_GNU_uninit:
13707 return "DW_OP_GNU_uninit";
13708 case DW_OP_GNU_implicit_pointer:
13709 return "DW_OP_GNU_implicit_pointer";
13710 case DW_OP_GNU_entry_value:
13711 return "DW_OP_GNU_entry_value";
13712 case DW_OP_GNU_const_type:
13713 return "DW_OP_GNU_const_type";
13714 case DW_OP_GNU_regval_type:
13715 return "DW_OP_GNU_regval_type";
13716 case DW_OP_GNU_deref_type:
13717 return "DW_OP_GNU_deref_type";
13718 case DW_OP_GNU_convert:
13719 return "DW_OP_GNU_convert";
13720 case DW_OP_GNU_reinterpret:
13721 return "DW_OP_GNU_reinterpret";
13722 default:
13723 return NULL;
13724 }
13725 }
13726
13727 static char *
13728 dwarf_bool_name (unsigned mybool)
13729 {
13730 if (mybool)
13731 return "TRUE";
13732 else
13733 return "FALSE";
13734 }
13735
13736 /* Convert a DWARF type code into its string name. */
13737
13738 static char *
13739 dwarf_type_encoding_name (unsigned enc)
13740 {
13741 switch (enc)
13742 {
13743 case DW_ATE_void:
13744 return "DW_ATE_void";
13745 case DW_ATE_address:
13746 return "DW_ATE_address";
13747 case DW_ATE_boolean:
13748 return "DW_ATE_boolean";
13749 case DW_ATE_complex_float:
13750 return "DW_ATE_complex_float";
13751 case DW_ATE_float:
13752 return "DW_ATE_float";
13753 case DW_ATE_signed:
13754 return "DW_ATE_signed";
13755 case DW_ATE_signed_char:
13756 return "DW_ATE_signed_char";
13757 case DW_ATE_unsigned:
13758 return "DW_ATE_unsigned";
13759 case DW_ATE_unsigned_char:
13760 return "DW_ATE_unsigned_char";
13761 /* DWARF 3. */
13762 case DW_ATE_imaginary_float:
13763 return "DW_ATE_imaginary_float";
13764 case DW_ATE_packed_decimal:
13765 return "DW_ATE_packed_decimal";
13766 case DW_ATE_numeric_string:
13767 return "DW_ATE_numeric_string";
13768 case DW_ATE_edited:
13769 return "DW_ATE_edited";
13770 case DW_ATE_signed_fixed:
13771 return "DW_ATE_signed_fixed";
13772 case DW_ATE_unsigned_fixed:
13773 return "DW_ATE_unsigned_fixed";
13774 case DW_ATE_decimal_float:
13775 return "DW_ATE_decimal_float";
13776 /* DWARF 4. */
13777 case DW_ATE_UTF:
13778 return "DW_ATE_UTF";
13779 /* HP extensions. */
13780 case DW_ATE_HP_float80:
13781 return "DW_ATE_HP_float80";
13782 case DW_ATE_HP_complex_float80:
13783 return "DW_ATE_HP_complex_float80";
13784 case DW_ATE_HP_float128:
13785 return "DW_ATE_HP_float128";
13786 case DW_ATE_HP_complex_float128:
13787 return "DW_ATE_HP_complex_float128";
13788 case DW_ATE_HP_floathpintel:
13789 return "DW_ATE_HP_floathpintel";
13790 case DW_ATE_HP_imaginary_float80:
13791 return "DW_ATE_HP_imaginary_float80";
13792 case DW_ATE_HP_imaginary_float128:
13793 return "DW_ATE_HP_imaginary_float128";
13794 default:
13795 return "DW_ATE_<unknown>";
13796 }
13797 }
13798
13799 /* Convert a DWARF call frame info operation to its string name. */
13800
13801 #if 0
13802 static char *
13803 dwarf_cfi_name (unsigned cfi_opc)
13804 {
13805 switch (cfi_opc)
13806 {
13807 case DW_CFA_advance_loc:
13808 return "DW_CFA_advance_loc";
13809 case DW_CFA_offset:
13810 return "DW_CFA_offset";
13811 case DW_CFA_restore:
13812 return "DW_CFA_restore";
13813 case DW_CFA_nop:
13814 return "DW_CFA_nop";
13815 case DW_CFA_set_loc:
13816 return "DW_CFA_set_loc";
13817 case DW_CFA_advance_loc1:
13818 return "DW_CFA_advance_loc1";
13819 case DW_CFA_advance_loc2:
13820 return "DW_CFA_advance_loc2";
13821 case DW_CFA_advance_loc4:
13822 return "DW_CFA_advance_loc4";
13823 case DW_CFA_offset_extended:
13824 return "DW_CFA_offset_extended";
13825 case DW_CFA_restore_extended:
13826 return "DW_CFA_restore_extended";
13827 case DW_CFA_undefined:
13828 return "DW_CFA_undefined";
13829 case DW_CFA_same_value:
13830 return "DW_CFA_same_value";
13831 case DW_CFA_register:
13832 return "DW_CFA_register";
13833 case DW_CFA_remember_state:
13834 return "DW_CFA_remember_state";
13835 case DW_CFA_restore_state:
13836 return "DW_CFA_restore_state";
13837 case DW_CFA_def_cfa:
13838 return "DW_CFA_def_cfa";
13839 case DW_CFA_def_cfa_register:
13840 return "DW_CFA_def_cfa_register";
13841 case DW_CFA_def_cfa_offset:
13842 return "DW_CFA_def_cfa_offset";
13843 /* DWARF 3. */
13844 case DW_CFA_def_cfa_expression:
13845 return "DW_CFA_def_cfa_expression";
13846 case DW_CFA_expression:
13847 return "DW_CFA_expression";
13848 case DW_CFA_offset_extended_sf:
13849 return "DW_CFA_offset_extended_sf";
13850 case DW_CFA_def_cfa_sf:
13851 return "DW_CFA_def_cfa_sf";
13852 case DW_CFA_def_cfa_offset_sf:
13853 return "DW_CFA_def_cfa_offset_sf";
13854 case DW_CFA_val_offset:
13855 return "DW_CFA_val_offset";
13856 case DW_CFA_val_offset_sf:
13857 return "DW_CFA_val_offset_sf";
13858 case DW_CFA_val_expression:
13859 return "DW_CFA_val_expression";
13860 /* SGI/MIPS specific. */
13861 case DW_CFA_MIPS_advance_loc8:
13862 return "DW_CFA_MIPS_advance_loc8";
13863 /* GNU extensions. */
13864 case DW_CFA_GNU_window_save:
13865 return "DW_CFA_GNU_window_save";
13866 case DW_CFA_GNU_args_size:
13867 return "DW_CFA_GNU_args_size";
13868 case DW_CFA_GNU_negative_offset_extended:
13869 return "DW_CFA_GNU_negative_offset_extended";
13870 default:
13871 return "DW_CFA_<unknown>";
13872 }
13873 }
13874 #endif
13875
13876 static void
13877 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13878 {
13879 unsigned int i;
13880
13881 print_spaces (indent, f);
13882 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13883 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13884
13885 if (die->parent != NULL)
13886 {
13887 print_spaces (indent, f);
13888 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13889 die->parent->offset);
13890 }
13891
13892 print_spaces (indent, f);
13893 fprintf_unfiltered (f, " has children: %s\n",
13894 dwarf_bool_name (die->child != NULL));
13895
13896 print_spaces (indent, f);
13897 fprintf_unfiltered (f, " attributes:\n");
13898
13899 for (i = 0; i < die->num_attrs; ++i)
13900 {
13901 print_spaces (indent, f);
13902 fprintf_unfiltered (f, " %s (%s) ",
13903 dwarf_attr_name (die->attrs[i].name),
13904 dwarf_form_name (die->attrs[i].form));
13905
13906 switch (die->attrs[i].form)
13907 {
13908 case DW_FORM_ref_addr:
13909 case DW_FORM_addr:
13910 fprintf_unfiltered (f, "address: ");
13911 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13912 break;
13913 case DW_FORM_block2:
13914 case DW_FORM_block4:
13915 case DW_FORM_block:
13916 case DW_FORM_block1:
13917 fprintf_unfiltered (f, "block: size %d",
13918 DW_BLOCK (&die->attrs[i])->size);
13919 break;
13920 case DW_FORM_exprloc:
13921 fprintf_unfiltered (f, "expression: size %u",
13922 DW_BLOCK (&die->attrs[i])->size);
13923 break;
13924 case DW_FORM_ref1:
13925 case DW_FORM_ref2:
13926 case DW_FORM_ref4:
13927 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13928 (long) (DW_ADDR (&die->attrs[i])));
13929 break;
13930 case DW_FORM_data1:
13931 case DW_FORM_data2:
13932 case DW_FORM_data4:
13933 case DW_FORM_data8:
13934 case DW_FORM_udata:
13935 case DW_FORM_sdata:
13936 fprintf_unfiltered (f, "constant: %s",
13937 pulongest (DW_UNSND (&die->attrs[i])));
13938 break;
13939 case DW_FORM_sec_offset:
13940 fprintf_unfiltered (f, "section offset: %s",
13941 pulongest (DW_UNSND (&die->attrs[i])));
13942 break;
13943 case DW_FORM_ref_sig8:
13944 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13945 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13946 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13947 else
13948 fprintf_unfiltered (f, "signatured type, offset: unknown");
13949 break;
13950 case DW_FORM_string:
13951 case DW_FORM_strp:
13952 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13953 DW_STRING (&die->attrs[i])
13954 ? DW_STRING (&die->attrs[i]) : "",
13955 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13956 break;
13957 case DW_FORM_flag:
13958 if (DW_UNSND (&die->attrs[i]))
13959 fprintf_unfiltered (f, "flag: TRUE");
13960 else
13961 fprintf_unfiltered (f, "flag: FALSE");
13962 break;
13963 case DW_FORM_flag_present:
13964 fprintf_unfiltered (f, "flag: TRUE");
13965 break;
13966 case DW_FORM_indirect:
13967 /* The reader will have reduced the indirect form to
13968 the "base form" so this form should not occur. */
13969 fprintf_unfiltered (f,
13970 "unexpected attribute form: DW_FORM_indirect");
13971 break;
13972 default:
13973 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13974 die->attrs[i].form);
13975 break;
13976 }
13977 fprintf_unfiltered (f, "\n");
13978 }
13979 }
13980
13981 static void
13982 dump_die_for_error (struct die_info *die)
13983 {
13984 dump_die_shallow (gdb_stderr, 0, die);
13985 }
13986
13987 static void
13988 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13989 {
13990 int indent = level * 4;
13991
13992 gdb_assert (die != NULL);
13993
13994 if (level >= max_level)
13995 return;
13996
13997 dump_die_shallow (f, indent, die);
13998
13999 if (die->child != NULL)
14000 {
14001 print_spaces (indent, f);
14002 fprintf_unfiltered (f, " Children:");
14003 if (level + 1 < max_level)
14004 {
14005 fprintf_unfiltered (f, "\n");
14006 dump_die_1 (f, level + 1, max_level, die->child);
14007 }
14008 else
14009 {
14010 fprintf_unfiltered (f,
14011 " [not printed, max nesting level reached]\n");
14012 }
14013 }
14014
14015 if (die->sibling != NULL && level > 0)
14016 {
14017 dump_die_1 (f, level, max_level, die->sibling);
14018 }
14019 }
14020
14021 /* This is called from the pdie macro in gdbinit.in.
14022 It's not static so gcc will keep a copy callable from gdb. */
14023
14024 void
14025 dump_die (struct die_info *die, int max_level)
14026 {
14027 dump_die_1 (gdb_stdlog, 0, max_level, die);
14028 }
14029
14030 static void
14031 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14032 {
14033 void **slot;
14034
14035 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14036
14037 *slot = die;
14038 }
14039
14040 static int
14041 is_ref_attr (struct attribute *attr)
14042 {
14043 switch (attr->form)
14044 {
14045 case DW_FORM_ref_addr:
14046 case DW_FORM_ref1:
14047 case DW_FORM_ref2:
14048 case DW_FORM_ref4:
14049 case DW_FORM_ref8:
14050 case DW_FORM_ref_udata:
14051 return 1;
14052 default:
14053 return 0;
14054 }
14055 }
14056
14057 static unsigned int
14058 dwarf2_get_ref_die_offset (struct attribute *attr)
14059 {
14060 if (is_ref_attr (attr))
14061 return DW_ADDR (attr);
14062
14063 complaint (&symfile_complaints,
14064 _("unsupported die ref attribute form: '%s'"),
14065 dwarf_form_name (attr->form));
14066 return 0;
14067 }
14068
14069 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14070 * the value held by the attribute is not constant. */
14071
14072 static LONGEST
14073 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14074 {
14075 if (attr->form == DW_FORM_sdata)
14076 return DW_SND (attr);
14077 else if (attr->form == DW_FORM_udata
14078 || attr->form == DW_FORM_data1
14079 || attr->form == DW_FORM_data2
14080 || attr->form == DW_FORM_data4
14081 || attr->form == DW_FORM_data8)
14082 return DW_UNSND (attr);
14083 else
14084 {
14085 complaint (&symfile_complaints,
14086 _("Attribute value is not a constant (%s)"),
14087 dwarf_form_name (attr->form));
14088 return default_value;
14089 }
14090 }
14091
14092 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14093 unit and add it to our queue.
14094 The result is non-zero if PER_CU was queued, otherwise the result is zero
14095 meaning either PER_CU is already queued or it is already loaded. */
14096
14097 static int
14098 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14099 struct dwarf2_per_cu_data *per_cu)
14100 {
14101 /* We may arrive here during partial symbol reading, if we need full
14102 DIEs to process an unusual case (e.g. template arguments). Do
14103 not queue PER_CU, just tell our caller to load its DIEs. */
14104 if (dwarf2_per_objfile->reading_partial_symbols)
14105 {
14106 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14107 return 1;
14108 return 0;
14109 }
14110
14111 /* Mark the dependence relation so that we don't flush PER_CU
14112 too early. */
14113 dwarf2_add_dependence (this_cu, per_cu);
14114
14115 /* If it's already on the queue, we have nothing to do. */
14116 if (per_cu->queued)
14117 return 0;
14118
14119 /* If the compilation unit is already loaded, just mark it as
14120 used. */
14121 if (per_cu->cu != NULL)
14122 {
14123 per_cu->cu->last_used = 0;
14124 return 0;
14125 }
14126
14127 /* Add it to the queue. */
14128 queue_comp_unit (per_cu);
14129
14130 return 1;
14131 }
14132
14133 /* Follow reference or signature attribute ATTR of SRC_DIE.
14134 On entry *REF_CU is the CU of SRC_DIE.
14135 On exit *REF_CU is the CU of the result. */
14136
14137 static struct die_info *
14138 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14139 struct dwarf2_cu **ref_cu)
14140 {
14141 struct die_info *die;
14142
14143 if (is_ref_attr (attr))
14144 die = follow_die_ref (src_die, attr, ref_cu);
14145 else if (attr->form == DW_FORM_ref_sig8)
14146 die = follow_die_sig (src_die, attr, ref_cu);
14147 else
14148 {
14149 dump_die_for_error (src_die);
14150 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14151 (*ref_cu)->objfile->name);
14152 }
14153
14154 return die;
14155 }
14156
14157 /* Follow reference OFFSET.
14158 On entry *REF_CU is the CU of the source die referencing OFFSET.
14159 On exit *REF_CU is the CU of the result.
14160 Returns NULL if OFFSET is invalid. */
14161
14162 static struct die_info *
14163 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14164 {
14165 struct die_info temp_die;
14166 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14167
14168 gdb_assert (cu->per_cu != NULL);
14169
14170 target_cu = cu;
14171
14172 if (cu->per_cu->debug_types_section)
14173 {
14174 /* .debug_types CUs cannot reference anything outside their CU.
14175 If they need to, they have to reference a signatured type via
14176 DW_FORM_ref_sig8. */
14177 if (! offset_in_cu_p (&cu->header, offset))
14178 return NULL;
14179 }
14180 else if (! offset_in_cu_p (&cu->header, offset))
14181 {
14182 struct dwarf2_per_cu_data *per_cu;
14183
14184 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14185
14186 /* If necessary, add it to the queue and load its DIEs. */
14187 if (maybe_queue_comp_unit (cu, per_cu))
14188 load_full_comp_unit (per_cu);
14189
14190 target_cu = per_cu->cu;
14191 }
14192 else if (cu->dies == NULL)
14193 {
14194 /* We're loading full DIEs during partial symbol reading. */
14195 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14196 load_full_comp_unit (cu->per_cu);
14197 }
14198
14199 *ref_cu = target_cu;
14200 temp_die.offset = offset;
14201 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14202 }
14203
14204 /* Follow reference attribute ATTR of SRC_DIE.
14205 On entry *REF_CU is the CU of SRC_DIE.
14206 On exit *REF_CU is the CU of the result. */
14207
14208 static struct die_info *
14209 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14210 struct dwarf2_cu **ref_cu)
14211 {
14212 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14213 struct dwarf2_cu *cu = *ref_cu;
14214 struct die_info *die;
14215
14216 die = follow_die_offset (offset, ref_cu);
14217 if (!die)
14218 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14219 "at 0x%x [in module %s]"),
14220 offset, src_die->offset, cu->objfile->name);
14221
14222 return die;
14223 }
14224
14225 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14226 Returned value is intended for DW_OP_call*. Returned
14227 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14228
14229 struct dwarf2_locexpr_baton
14230 dwarf2_fetch_die_location_block (unsigned int offset,
14231 struct dwarf2_per_cu_data *per_cu,
14232 CORE_ADDR (*get_frame_pc) (void *baton),
14233 void *baton)
14234 {
14235 struct dwarf2_cu *cu;
14236 struct die_info *die;
14237 struct attribute *attr;
14238 struct dwarf2_locexpr_baton retval;
14239
14240 dw2_setup (per_cu->objfile);
14241
14242 if (per_cu->cu == NULL)
14243 load_cu (per_cu);
14244 cu = per_cu->cu;
14245
14246 die = follow_die_offset (offset, &cu);
14247 if (!die)
14248 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14249 offset, per_cu->objfile->name);
14250
14251 attr = dwarf2_attr (die, DW_AT_location, cu);
14252 if (!attr)
14253 {
14254 /* DWARF: "If there is no such attribute, then there is no effect.".
14255 DATA is ignored if SIZE is 0. */
14256
14257 retval.data = NULL;
14258 retval.size = 0;
14259 }
14260 else if (attr_form_is_section_offset (attr))
14261 {
14262 struct dwarf2_loclist_baton loclist_baton;
14263 CORE_ADDR pc = (*get_frame_pc) (baton);
14264 size_t size;
14265
14266 fill_in_loclist_baton (cu, &loclist_baton, attr);
14267
14268 retval.data = dwarf2_find_location_expression (&loclist_baton,
14269 &size, pc);
14270 retval.size = size;
14271 }
14272 else
14273 {
14274 if (!attr_form_is_block (attr))
14275 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14276 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14277 offset, per_cu->objfile->name);
14278
14279 retval.data = DW_BLOCK (attr)->data;
14280 retval.size = DW_BLOCK (attr)->size;
14281 }
14282 retval.per_cu = cu->per_cu;
14283
14284 age_cached_comp_units ();
14285
14286 return retval;
14287 }
14288
14289 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14290 PER_CU. */
14291
14292 struct type *
14293 dwarf2_get_die_type (unsigned int die_offset,
14294 struct dwarf2_per_cu_data *per_cu)
14295 {
14296 dw2_setup (per_cu->objfile);
14297 return get_die_type_at_offset (die_offset, per_cu);
14298 }
14299
14300 /* Follow the signature attribute ATTR in SRC_DIE.
14301 On entry *REF_CU is the CU of SRC_DIE.
14302 On exit *REF_CU is the CU of the result. */
14303
14304 static struct die_info *
14305 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14306 struct dwarf2_cu **ref_cu)
14307 {
14308 struct objfile *objfile = (*ref_cu)->objfile;
14309 struct die_info temp_die;
14310 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14311 struct dwarf2_cu *sig_cu;
14312 struct die_info *die;
14313
14314 /* sig_type will be NULL if the signatured type is missing from
14315 the debug info. */
14316 if (sig_type == NULL)
14317 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14318 "at 0x%x [in module %s]"),
14319 src_die->offset, objfile->name);
14320
14321 /* If necessary, add it to the queue and load its DIEs. */
14322
14323 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14324 read_signatured_type (sig_type);
14325
14326 gdb_assert (sig_type->per_cu.cu != NULL);
14327
14328 sig_cu = sig_type->per_cu.cu;
14329 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14330 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14331 if (die)
14332 {
14333 *ref_cu = sig_cu;
14334 return die;
14335 }
14336
14337 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14338 "from DIE at 0x%x [in module %s]"),
14339 sig_type->type_offset, src_die->offset, objfile->name);
14340 }
14341
14342 /* Given an offset of a signatured type, return its signatured_type. */
14343
14344 static struct signatured_type *
14345 lookup_signatured_type_at_offset (struct objfile *objfile,
14346 struct dwarf2_section_info *section,
14347 unsigned int offset)
14348 {
14349 gdb_byte *info_ptr = section->buffer + offset;
14350 unsigned int length, initial_length_size;
14351 unsigned int sig_offset;
14352 struct signatured_type find_entry, *type_sig;
14353
14354 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14355 sig_offset = (initial_length_size
14356 + 2 /*version*/
14357 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14358 + 1 /*address_size*/);
14359 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14360 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14361
14362 /* This is only used to lookup previously recorded types.
14363 If we didn't find it, it's our bug. */
14364 gdb_assert (type_sig != NULL);
14365 gdb_assert (offset == type_sig->per_cu.offset);
14366
14367 return type_sig;
14368 }
14369
14370 /* Read in signatured type at OFFSET and build its CU and die(s). */
14371
14372 static void
14373 read_signatured_type_at_offset (struct objfile *objfile,
14374 struct dwarf2_section_info *sect,
14375 unsigned int offset)
14376 {
14377 struct signatured_type *type_sig;
14378
14379 dwarf2_read_section (objfile, sect);
14380
14381 /* We have the section offset, but we need the signature to do the
14382 hash table lookup. */
14383 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14384
14385 gdb_assert (type_sig->per_cu.cu == NULL);
14386
14387 read_signatured_type (type_sig);
14388
14389 gdb_assert (type_sig->per_cu.cu != NULL);
14390 }
14391
14392 /* Read in a signatured type and build its CU and DIEs. */
14393
14394 static void
14395 read_signatured_type (struct signatured_type *type_sig)
14396 {
14397 struct objfile *objfile = type_sig->per_cu.objfile;
14398 gdb_byte *types_ptr;
14399 struct die_reader_specs reader_specs;
14400 struct dwarf2_cu *cu;
14401 ULONGEST signature;
14402 struct cleanup *back_to, *free_cu_cleanup;
14403 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14404
14405 dwarf2_read_section (objfile, section);
14406 types_ptr = section->buffer + type_sig->per_cu.offset;
14407
14408 gdb_assert (type_sig->per_cu.cu == NULL);
14409
14410 cu = xmalloc (sizeof (*cu));
14411 init_one_comp_unit (cu, &type_sig->per_cu);
14412
14413 /* If an error occurs while loading, release our storage. */
14414 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14415
14416 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14417 types_ptr, objfile->obfd);
14418 gdb_assert (signature == type_sig->signature);
14419
14420 cu->die_hash
14421 = htab_create_alloc_ex (cu->header.length / 12,
14422 die_hash,
14423 die_eq,
14424 NULL,
14425 &cu->comp_unit_obstack,
14426 hashtab_obstack_allocate,
14427 dummy_obstack_deallocate);
14428
14429 dwarf2_read_abbrevs (objfile->obfd, cu);
14430 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14431
14432 init_cu_die_reader (&reader_specs, cu);
14433
14434 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14435 NULL /*parent*/);
14436
14437 /* We try not to read any attributes in this function, because not
14438 all CUs needed for references have been loaded yet, and symbol
14439 table processing isn't initialized. But we have to set the CU language,
14440 or we won't be able to build types correctly. */
14441 prepare_one_comp_unit (cu, cu->dies);
14442
14443 do_cleanups (back_to);
14444
14445 /* We've successfully allocated this compilation unit. Let our caller
14446 clean it up when finished with it. */
14447 discard_cleanups (free_cu_cleanup);
14448
14449 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14450 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14451 }
14452
14453 /* Decode simple location descriptions.
14454 Given a pointer to a dwarf block that defines a location, compute
14455 the location and return the value.
14456
14457 NOTE drow/2003-11-18: This function is called in two situations
14458 now: for the address of static or global variables (partial symbols
14459 only) and for offsets into structures which are expected to be
14460 (more or less) constant. The partial symbol case should go away,
14461 and only the constant case should remain. That will let this
14462 function complain more accurately. A few special modes are allowed
14463 without complaint for global variables (for instance, global
14464 register values and thread-local values).
14465
14466 A location description containing no operations indicates that the
14467 object is optimized out. The return value is 0 for that case.
14468 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14469 callers will only want a very basic result and this can become a
14470 complaint.
14471
14472 Note that stack[0] is unused except as a default error return. */
14473
14474 static CORE_ADDR
14475 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14476 {
14477 struct objfile *objfile = cu->objfile;
14478 int i;
14479 int size = blk->size;
14480 gdb_byte *data = blk->data;
14481 CORE_ADDR stack[64];
14482 int stacki;
14483 unsigned int bytes_read, unsnd;
14484 gdb_byte op;
14485
14486 i = 0;
14487 stacki = 0;
14488 stack[stacki] = 0;
14489 stack[++stacki] = 0;
14490
14491 while (i < size)
14492 {
14493 op = data[i++];
14494 switch (op)
14495 {
14496 case DW_OP_lit0:
14497 case DW_OP_lit1:
14498 case DW_OP_lit2:
14499 case DW_OP_lit3:
14500 case DW_OP_lit4:
14501 case DW_OP_lit5:
14502 case DW_OP_lit6:
14503 case DW_OP_lit7:
14504 case DW_OP_lit8:
14505 case DW_OP_lit9:
14506 case DW_OP_lit10:
14507 case DW_OP_lit11:
14508 case DW_OP_lit12:
14509 case DW_OP_lit13:
14510 case DW_OP_lit14:
14511 case DW_OP_lit15:
14512 case DW_OP_lit16:
14513 case DW_OP_lit17:
14514 case DW_OP_lit18:
14515 case DW_OP_lit19:
14516 case DW_OP_lit20:
14517 case DW_OP_lit21:
14518 case DW_OP_lit22:
14519 case DW_OP_lit23:
14520 case DW_OP_lit24:
14521 case DW_OP_lit25:
14522 case DW_OP_lit26:
14523 case DW_OP_lit27:
14524 case DW_OP_lit28:
14525 case DW_OP_lit29:
14526 case DW_OP_lit30:
14527 case DW_OP_lit31:
14528 stack[++stacki] = op - DW_OP_lit0;
14529 break;
14530
14531 case DW_OP_reg0:
14532 case DW_OP_reg1:
14533 case DW_OP_reg2:
14534 case DW_OP_reg3:
14535 case DW_OP_reg4:
14536 case DW_OP_reg5:
14537 case DW_OP_reg6:
14538 case DW_OP_reg7:
14539 case DW_OP_reg8:
14540 case DW_OP_reg9:
14541 case DW_OP_reg10:
14542 case DW_OP_reg11:
14543 case DW_OP_reg12:
14544 case DW_OP_reg13:
14545 case DW_OP_reg14:
14546 case DW_OP_reg15:
14547 case DW_OP_reg16:
14548 case DW_OP_reg17:
14549 case DW_OP_reg18:
14550 case DW_OP_reg19:
14551 case DW_OP_reg20:
14552 case DW_OP_reg21:
14553 case DW_OP_reg22:
14554 case DW_OP_reg23:
14555 case DW_OP_reg24:
14556 case DW_OP_reg25:
14557 case DW_OP_reg26:
14558 case DW_OP_reg27:
14559 case DW_OP_reg28:
14560 case DW_OP_reg29:
14561 case DW_OP_reg30:
14562 case DW_OP_reg31:
14563 stack[++stacki] = op - DW_OP_reg0;
14564 if (i < size)
14565 dwarf2_complex_location_expr_complaint ();
14566 break;
14567
14568 case DW_OP_regx:
14569 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14570 i += bytes_read;
14571 stack[++stacki] = unsnd;
14572 if (i < size)
14573 dwarf2_complex_location_expr_complaint ();
14574 break;
14575
14576 case DW_OP_addr:
14577 stack[++stacki] = read_address (objfile->obfd, &data[i],
14578 cu, &bytes_read);
14579 i += bytes_read;
14580 break;
14581
14582 case DW_OP_const1u:
14583 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14584 i += 1;
14585 break;
14586
14587 case DW_OP_const1s:
14588 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14589 i += 1;
14590 break;
14591
14592 case DW_OP_const2u:
14593 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14594 i += 2;
14595 break;
14596
14597 case DW_OP_const2s:
14598 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14599 i += 2;
14600 break;
14601
14602 case DW_OP_const4u:
14603 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14604 i += 4;
14605 break;
14606
14607 case DW_OP_const4s:
14608 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14609 i += 4;
14610 break;
14611
14612 case DW_OP_const8u:
14613 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14614 i += 8;
14615 break;
14616
14617 case DW_OP_constu:
14618 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14619 &bytes_read);
14620 i += bytes_read;
14621 break;
14622
14623 case DW_OP_consts:
14624 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14625 i += bytes_read;
14626 break;
14627
14628 case DW_OP_dup:
14629 stack[stacki + 1] = stack[stacki];
14630 stacki++;
14631 break;
14632
14633 case DW_OP_plus:
14634 stack[stacki - 1] += stack[stacki];
14635 stacki--;
14636 break;
14637
14638 case DW_OP_plus_uconst:
14639 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14640 &bytes_read);
14641 i += bytes_read;
14642 break;
14643
14644 case DW_OP_minus:
14645 stack[stacki - 1] -= stack[stacki];
14646 stacki--;
14647 break;
14648
14649 case DW_OP_deref:
14650 /* If we're not the last op, then we definitely can't encode
14651 this using GDB's address_class enum. This is valid for partial
14652 global symbols, although the variable's address will be bogus
14653 in the psymtab. */
14654 if (i < size)
14655 dwarf2_complex_location_expr_complaint ();
14656 break;
14657
14658 case DW_OP_GNU_push_tls_address:
14659 /* The top of the stack has the offset from the beginning
14660 of the thread control block at which the variable is located. */
14661 /* Nothing should follow this operator, so the top of stack would
14662 be returned. */
14663 /* This is valid for partial global symbols, but the variable's
14664 address will be bogus in the psymtab. Make it always at least
14665 non-zero to not look as a variable garbage collected by linker
14666 which have DW_OP_addr 0. */
14667 if (i < size)
14668 dwarf2_complex_location_expr_complaint ();
14669 stack[stacki]++;
14670 break;
14671
14672 case DW_OP_GNU_uninit:
14673 break;
14674
14675 default:
14676 {
14677 const char *name = dwarf_stack_op_name (op);
14678
14679 if (name)
14680 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14681 name);
14682 else
14683 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14684 op);
14685 }
14686
14687 return (stack[stacki]);
14688 }
14689
14690 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14691 outside of the allocated space. Also enforce minimum>0. */
14692 if (stacki >= ARRAY_SIZE (stack) - 1)
14693 {
14694 complaint (&symfile_complaints,
14695 _("location description stack overflow"));
14696 return 0;
14697 }
14698
14699 if (stacki <= 0)
14700 {
14701 complaint (&symfile_complaints,
14702 _("location description stack underflow"));
14703 return 0;
14704 }
14705 }
14706 return (stack[stacki]);
14707 }
14708
14709 /* memory allocation interface */
14710
14711 static struct dwarf_block *
14712 dwarf_alloc_block (struct dwarf2_cu *cu)
14713 {
14714 struct dwarf_block *blk;
14715
14716 blk = (struct dwarf_block *)
14717 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14718 return (blk);
14719 }
14720
14721 static struct abbrev_info *
14722 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14723 {
14724 struct abbrev_info *abbrev;
14725
14726 abbrev = (struct abbrev_info *)
14727 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14728 memset (abbrev, 0, sizeof (struct abbrev_info));
14729 return (abbrev);
14730 }
14731
14732 static struct die_info *
14733 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14734 {
14735 struct die_info *die;
14736 size_t size = sizeof (struct die_info);
14737
14738 if (num_attrs > 1)
14739 size += (num_attrs - 1) * sizeof (struct attribute);
14740
14741 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14742 memset (die, 0, sizeof (struct die_info));
14743 return (die);
14744 }
14745
14746 \f
14747 /* Macro support. */
14748
14749 /* Return the full name of file number I in *LH's file name table.
14750 Use COMP_DIR as the name of the current directory of the
14751 compilation. The result is allocated using xmalloc; the caller is
14752 responsible for freeing it. */
14753 static char *
14754 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14755 {
14756 /* Is the file number a valid index into the line header's file name
14757 table? Remember that file numbers start with one, not zero. */
14758 if (1 <= file && file <= lh->num_file_names)
14759 {
14760 struct file_entry *fe = &lh->file_names[file - 1];
14761
14762 if (IS_ABSOLUTE_PATH (fe->name))
14763 return xstrdup (fe->name);
14764 else
14765 {
14766 const char *dir;
14767 int dir_len;
14768 char *full_name;
14769
14770 if (fe->dir_index)
14771 dir = lh->include_dirs[fe->dir_index - 1];
14772 else
14773 dir = comp_dir;
14774
14775 if (dir)
14776 {
14777 dir_len = strlen (dir);
14778 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14779 strcpy (full_name, dir);
14780 full_name[dir_len] = '/';
14781 strcpy (full_name + dir_len + 1, fe->name);
14782 return full_name;
14783 }
14784 else
14785 return xstrdup (fe->name);
14786 }
14787 }
14788 else
14789 {
14790 /* The compiler produced a bogus file number. We can at least
14791 record the macro definitions made in the file, even if we
14792 won't be able to find the file by name. */
14793 char fake_name[80];
14794
14795 sprintf (fake_name, "<bad macro file number %d>", file);
14796
14797 complaint (&symfile_complaints,
14798 _("bad file number in macro information (%d)"),
14799 file);
14800
14801 return xstrdup (fake_name);
14802 }
14803 }
14804
14805
14806 static struct macro_source_file *
14807 macro_start_file (int file, int line,
14808 struct macro_source_file *current_file,
14809 const char *comp_dir,
14810 struct line_header *lh, struct objfile *objfile)
14811 {
14812 /* The full name of this source file. */
14813 char *full_name = file_full_name (file, lh, comp_dir);
14814
14815 /* We don't create a macro table for this compilation unit
14816 at all until we actually get a filename. */
14817 if (! pending_macros)
14818 pending_macros = new_macro_table (&objfile->objfile_obstack,
14819 objfile->macro_cache);
14820
14821 if (! current_file)
14822 /* If we have no current file, then this must be the start_file
14823 directive for the compilation unit's main source file. */
14824 current_file = macro_set_main (pending_macros, full_name);
14825 else
14826 current_file = macro_include (current_file, line, full_name);
14827
14828 xfree (full_name);
14829
14830 return current_file;
14831 }
14832
14833
14834 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14835 followed by a null byte. */
14836 static char *
14837 copy_string (const char *buf, int len)
14838 {
14839 char *s = xmalloc (len + 1);
14840
14841 memcpy (s, buf, len);
14842 s[len] = '\0';
14843 return s;
14844 }
14845
14846
14847 static const char *
14848 consume_improper_spaces (const char *p, const char *body)
14849 {
14850 if (*p == ' ')
14851 {
14852 complaint (&symfile_complaints,
14853 _("macro definition contains spaces "
14854 "in formal argument list:\n`%s'"),
14855 body);
14856
14857 while (*p == ' ')
14858 p++;
14859 }
14860
14861 return p;
14862 }
14863
14864
14865 static void
14866 parse_macro_definition (struct macro_source_file *file, int line,
14867 const char *body)
14868 {
14869 const char *p;
14870
14871 /* The body string takes one of two forms. For object-like macro
14872 definitions, it should be:
14873
14874 <macro name> " " <definition>
14875
14876 For function-like macro definitions, it should be:
14877
14878 <macro name> "() " <definition>
14879 or
14880 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14881
14882 Spaces may appear only where explicitly indicated, and in the
14883 <definition>.
14884
14885 The Dwarf 2 spec says that an object-like macro's name is always
14886 followed by a space, but versions of GCC around March 2002 omit
14887 the space when the macro's definition is the empty string.
14888
14889 The Dwarf 2 spec says that there should be no spaces between the
14890 formal arguments in a function-like macro's formal argument list,
14891 but versions of GCC around March 2002 include spaces after the
14892 commas. */
14893
14894
14895 /* Find the extent of the macro name. The macro name is terminated
14896 by either a space or null character (for an object-like macro) or
14897 an opening paren (for a function-like macro). */
14898 for (p = body; *p; p++)
14899 if (*p == ' ' || *p == '(')
14900 break;
14901
14902 if (*p == ' ' || *p == '\0')
14903 {
14904 /* It's an object-like macro. */
14905 int name_len = p - body;
14906 char *name = copy_string (body, name_len);
14907 const char *replacement;
14908
14909 if (*p == ' ')
14910 replacement = body + name_len + 1;
14911 else
14912 {
14913 dwarf2_macro_malformed_definition_complaint (body);
14914 replacement = body + name_len;
14915 }
14916
14917 macro_define_object (file, line, name, replacement);
14918
14919 xfree (name);
14920 }
14921 else if (*p == '(')
14922 {
14923 /* It's a function-like macro. */
14924 char *name = copy_string (body, p - body);
14925 int argc = 0;
14926 int argv_size = 1;
14927 char **argv = xmalloc (argv_size * sizeof (*argv));
14928
14929 p++;
14930
14931 p = consume_improper_spaces (p, body);
14932
14933 /* Parse the formal argument list. */
14934 while (*p && *p != ')')
14935 {
14936 /* Find the extent of the current argument name. */
14937 const char *arg_start = p;
14938
14939 while (*p && *p != ',' && *p != ')' && *p != ' ')
14940 p++;
14941
14942 if (! *p || p == arg_start)
14943 dwarf2_macro_malformed_definition_complaint (body);
14944 else
14945 {
14946 /* Make sure argv has room for the new argument. */
14947 if (argc >= argv_size)
14948 {
14949 argv_size *= 2;
14950 argv = xrealloc (argv, argv_size * sizeof (*argv));
14951 }
14952
14953 argv[argc++] = copy_string (arg_start, p - arg_start);
14954 }
14955
14956 p = consume_improper_spaces (p, body);
14957
14958 /* Consume the comma, if present. */
14959 if (*p == ',')
14960 {
14961 p++;
14962
14963 p = consume_improper_spaces (p, body);
14964 }
14965 }
14966
14967 if (*p == ')')
14968 {
14969 p++;
14970
14971 if (*p == ' ')
14972 /* Perfectly formed definition, no complaints. */
14973 macro_define_function (file, line, name,
14974 argc, (const char **) argv,
14975 p + 1);
14976 else if (*p == '\0')
14977 {
14978 /* Complain, but do define it. */
14979 dwarf2_macro_malformed_definition_complaint (body);
14980 macro_define_function (file, line, name,
14981 argc, (const char **) argv,
14982 p);
14983 }
14984 else
14985 /* Just complain. */
14986 dwarf2_macro_malformed_definition_complaint (body);
14987 }
14988 else
14989 /* Just complain. */
14990 dwarf2_macro_malformed_definition_complaint (body);
14991
14992 xfree (name);
14993 {
14994 int i;
14995
14996 for (i = 0; i < argc; i++)
14997 xfree (argv[i]);
14998 }
14999 xfree (argv);
15000 }
15001 else
15002 dwarf2_macro_malformed_definition_complaint (body);
15003 }
15004
15005 /* Skip some bytes from BYTES according to the form given in FORM.
15006 Returns the new pointer. */
15007
15008 static gdb_byte *
15009 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15010 enum dwarf_form form,
15011 unsigned int offset_size,
15012 struct dwarf2_section_info *section)
15013 {
15014 unsigned int bytes_read;
15015
15016 switch (form)
15017 {
15018 case DW_FORM_data1:
15019 case DW_FORM_flag:
15020 ++bytes;
15021 break;
15022
15023 case DW_FORM_data2:
15024 bytes += 2;
15025 break;
15026
15027 case DW_FORM_data4:
15028 bytes += 4;
15029 break;
15030
15031 case DW_FORM_data8:
15032 bytes += 8;
15033 break;
15034
15035 case DW_FORM_string:
15036 read_direct_string (abfd, bytes, &bytes_read);
15037 bytes += bytes_read;
15038 break;
15039
15040 case DW_FORM_sec_offset:
15041 case DW_FORM_strp:
15042 bytes += offset_size;
15043 break;
15044
15045 case DW_FORM_block:
15046 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15047 bytes += bytes_read;
15048 break;
15049
15050 case DW_FORM_block1:
15051 bytes += 1 + read_1_byte (abfd, bytes);
15052 break;
15053 case DW_FORM_block2:
15054 bytes += 2 + read_2_bytes (abfd, bytes);
15055 break;
15056 case DW_FORM_block4:
15057 bytes += 4 + read_4_bytes (abfd, bytes);
15058 break;
15059
15060 case DW_FORM_sdata:
15061 case DW_FORM_udata:
15062 bytes = skip_leb128 (abfd, bytes);
15063 break;
15064
15065 default:
15066 {
15067 complain:
15068 complaint (&symfile_complaints,
15069 _("invalid form 0x%x in `%s'"),
15070 form,
15071 section->asection->name);
15072 return NULL;
15073 }
15074 }
15075
15076 return bytes;
15077 }
15078
15079 /* A helper for dwarf_decode_macros that handles skipping an unknown
15080 opcode. Returns an updated pointer to the macro data buffer; or,
15081 on error, issues a complaint and returns NULL. */
15082
15083 static gdb_byte *
15084 skip_unknown_opcode (unsigned int opcode,
15085 gdb_byte **opcode_definitions,
15086 gdb_byte *mac_ptr,
15087 bfd *abfd,
15088 unsigned int offset_size,
15089 struct dwarf2_section_info *section)
15090 {
15091 unsigned int bytes_read, i;
15092 unsigned long arg;
15093 gdb_byte *defn;
15094
15095 if (opcode_definitions[opcode] == NULL)
15096 {
15097 complaint (&symfile_complaints,
15098 _("unrecognized DW_MACFINO opcode 0x%x"),
15099 opcode);
15100 return NULL;
15101 }
15102
15103 defn = opcode_definitions[opcode];
15104 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15105 defn += bytes_read;
15106
15107 for (i = 0; i < arg; ++i)
15108 {
15109 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15110 if (mac_ptr == NULL)
15111 {
15112 /* skip_form_bytes already issued the complaint. */
15113 return NULL;
15114 }
15115 }
15116
15117 return mac_ptr;
15118 }
15119
15120 /* A helper function which parses the header of a macro section.
15121 If the macro section is the extended (for now called "GNU") type,
15122 then this updates *OFFSET_SIZE. Returns a pointer to just after
15123 the header, or issues a complaint and returns NULL on error. */
15124
15125 static gdb_byte *
15126 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15127 bfd *abfd,
15128 gdb_byte *mac_ptr,
15129 unsigned int *offset_size,
15130 int section_is_gnu)
15131 {
15132 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15133
15134 if (section_is_gnu)
15135 {
15136 unsigned int version, flags;
15137
15138 version = read_2_bytes (abfd, mac_ptr);
15139 if (version != 4)
15140 {
15141 complaint (&symfile_complaints,
15142 _("unrecognized version `%d' in .debug_macro section"),
15143 version);
15144 return NULL;
15145 }
15146 mac_ptr += 2;
15147
15148 flags = read_1_byte (abfd, mac_ptr);
15149 ++mac_ptr;
15150 *offset_size = (flags & 1) ? 8 : 4;
15151
15152 if ((flags & 2) != 0)
15153 /* We don't need the line table offset. */
15154 mac_ptr += *offset_size;
15155
15156 /* Vendor opcode descriptions. */
15157 if ((flags & 4) != 0)
15158 {
15159 unsigned int i, count;
15160
15161 count = read_1_byte (abfd, mac_ptr);
15162 ++mac_ptr;
15163 for (i = 0; i < count; ++i)
15164 {
15165 unsigned int opcode, bytes_read;
15166 unsigned long arg;
15167
15168 opcode = read_1_byte (abfd, mac_ptr);
15169 ++mac_ptr;
15170 opcode_definitions[opcode] = mac_ptr;
15171 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15172 mac_ptr += bytes_read;
15173 mac_ptr += arg;
15174 }
15175 }
15176 }
15177
15178 return mac_ptr;
15179 }
15180
15181 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15182 including DW_GNU_MACINFO_transparent_include. */
15183
15184 static void
15185 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15186 struct macro_source_file *current_file,
15187 struct line_header *lh, char *comp_dir,
15188 struct dwarf2_section_info *section,
15189 int section_is_gnu,
15190 unsigned int offset_size,
15191 struct objfile *objfile)
15192 {
15193 enum dwarf_macro_record_type macinfo_type;
15194 int at_commandline;
15195 gdb_byte *opcode_definitions[256];
15196
15197 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15198 &offset_size, section_is_gnu);
15199 if (mac_ptr == NULL)
15200 {
15201 /* We already issued a complaint. */
15202 return;
15203 }
15204
15205 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15206 GDB is still reading the definitions from command line. First
15207 DW_MACINFO_start_file will need to be ignored as it was already executed
15208 to create CURRENT_FILE for the main source holding also the command line
15209 definitions. On first met DW_MACINFO_start_file this flag is reset to
15210 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15211
15212 at_commandline = 1;
15213
15214 do
15215 {
15216 /* Do we at least have room for a macinfo type byte? */
15217 if (mac_ptr >= mac_end)
15218 {
15219 dwarf2_macros_too_long_complaint (section);
15220 break;
15221 }
15222
15223 macinfo_type = read_1_byte (abfd, mac_ptr);
15224 mac_ptr++;
15225
15226 /* Note that we rely on the fact that the corresponding GNU and
15227 DWARF constants are the same. */
15228 switch (macinfo_type)
15229 {
15230 /* A zero macinfo type indicates the end of the macro
15231 information. */
15232 case 0:
15233 break;
15234
15235 case DW_MACRO_GNU_define:
15236 case DW_MACRO_GNU_undef:
15237 case DW_MACRO_GNU_define_indirect:
15238 case DW_MACRO_GNU_undef_indirect:
15239 {
15240 unsigned int bytes_read;
15241 int line;
15242 char *body;
15243 int is_define;
15244
15245 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15246 mac_ptr += bytes_read;
15247
15248 if (macinfo_type == DW_MACRO_GNU_define
15249 || macinfo_type == DW_MACRO_GNU_undef)
15250 {
15251 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15252 mac_ptr += bytes_read;
15253 }
15254 else
15255 {
15256 LONGEST str_offset;
15257
15258 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15259 mac_ptr += offset_size;
15260
15261 body = read_indirect_string_at_offset (abfd, str_offset);
15262 }
15263
15264 is_define = (macinfo_type == DW_MACRO_GNU_define
15265 || macinfo_type == DW_MACRO_GNU_define_indirect);
15266 if (! current_file)
15267 {
15268 /* DWARF violation as no main source is present. */
15269 complaint (&symfile_complaints,
15270 _("debug info with no main source gives macro %s "
15271 "on line %d: %s"),
15272 is_define ? _("definition") : _("undefinition"),
15273 line, body);
15274 break;
15275 }
15276 if ((line == 0 && !at_commandline)
15277 || (line != 0 && at_commandline))
15278 complaint (&symfile_complaints,
15279 _("debug info gives %s macro %s with %s line %d: %s"),
15280 at_commandline ? _("command-line") : _("in-file"),
15281 is_define ? _("definition") : _("undefinition"),
15282 line == 0 ? _("zero") : _("non-zero"), line, body);
15283
15284 if (is_define)
15285 parse_macro_definition (current_file, line, body);
15286 else
15287 {
15288 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15289 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15290 macro_undef (current_file, line, body);
15291 }
15292 }
15293 break;
15294
15295 case DW_MACRO_GNU_start_file:
15296 {
15297 unsigned int bytes_read;
15298 int line, file;
15299
15300 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15301 mac_ptr += bytes_read;
15302 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15303 mac_ptr += bytes_read;
15304
15305 if ((line == 0 && !at_commandline)
15306 || (line != 0 && at_commandline))
15307 complaint (&symfile_complaints,
15308 _("debug info gives source %d included "
15309 "from %s at %s line %d"),
15310 file, at_commandline ? _("command-line") : _("file"),
15311 line == 0 ? _("zero") : _("non-zero"), line);
15312
15313 if (at_commandline)
15314 {
15315 /* This DW_MACRO_GNU_start_file was executed in the
15316 pass one. */
15317 at_commandline = 0;
15318 }
15319 else
15320 current_file = macro_start_file (file, line,
15321 current_file, comp_dir,
15322 lh, objfile);
15323 }
15324 break;
15325
15326 case DW_MACRO_GNU_end_file:
15327 if (! current_file)
15328 complaint (&symfile_complaints,
15329 _("macro debug info has an unmatched "
15330 "`close_file' directive"));
15331 else
15332 {
15333 current_file = current_file->included_by;
15334 if (! current_file)
15335 {
15336 enum dwarf_macro_record_type next_type;
15337
15338 /* GCC circa March 2002 doesn't produce the zero
15339 type byte marking the end of the compilation
15340 unit. Complain if it's not there, but exit no
15341 matter what. */
15342
15343 /* Do we at least have room for a macinfo type byte? */
15344 if (mac_ptr >= mac_end)
15345 {
15346 dwarf2_macros_too_long_complaint (section);
15347 return;
15348 }
15349
15350 /* We don't increment mac_ptr here, so this is just
15351 a look-ahead. */
15352 next_type = read_1_byte (abfd, mac_ptr);
15353 if (next_type != 0)
15354 complaint (&symfile_complaints,
15355 _("no terminating 0-type entry for "
15356 "macros in `.debug_macinfo' section"));
15357
15358 return;
15359 }
15360 }
15361 break;
15362
15363 case DW_MACRO_GNU_transparent_include:
15364 {
15365 LONGEST offset;
15366
15367 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15368 mac_ptr += offset_size;
15369
15370 dwarf_decode_macro_bytes (abfd,
15371 section->buffer + offset,
15372 mac_end, current_file,
15373 lh, comp_dir,
15374 section, section_is_gnu,
15375 offset_size, objfile);
15376 }
15377 break;
15378
15379 case DW_MACINFO_vendor_ext:
15380 if (!section_is_gnu)
15381 {
15382 unsigned int bytes_read;
15383 int constant;
15384
15385 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15386 mac_ptr += bytes_read;
15387 read_direct_string (abfd, mac_ptr, &bytes_read);
15388 mac_ptr += bytes_read;
15389
15390 /* We don't recognize any vendor extensions. */
15391 break;
15392 }
15393 /* FALLTHROUGH */
15394
15395 default:
15396 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15397 mac_ptr, abfd, offset_size,
15398 section);
15399 if (mac_ptr == NULL)
15400 return;
15401 break;
15402 }
15403 } while (macinfo_type != 0);
15404 }
15405
15406 static void
15407 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15408 char *comp_dir, bfd *abfd,
15409 struct dwarf2_cu *cu,
15410 struct dwarf2_section_info *section,
15411 int section_is_gnu)
15412 {
15413 struct objfile *objfile = dwarf2_per_objfile->objfile;
15414 gdb_byte *mac_ptr, *mac_end;
15415 struct macro_source_file *current_file = 0;
15416 enum dwarf_macro_record_type macinfo_type;
15417 unsigned int offset_size = cu->header.offset_size;
15418 gdb_byte *opcode_definitions[256];
15419
15420 dwarf2_read_section (objfile, section);
15421 if (section->buffer == NULL)
15422 {
15423 complaint (&symfile_complaints, _("missing %s section"),
15424 section->asection->name);
15425 return;
15426 }
15427
15428 /* First pass: Find the name of the base filename.
15429 This filename is needed in order to process all macros whose definition
15430 (or undefinition) comes from the command line. These macros are defined
15431 before the first DW_MACINFO_start_file entry, and yet still need to be
15432 associated to the base file.
15433
15434 To determine the base file name, we scan the macro definitions until we
15435 reach the first DW_MACINFO_start_file entry. We then initialize
15436 CURRENT_FILE accordingly so that any macro definition found before the
15437 first DW_MACINFO_start_file can still be associated to the base file. */
15438
15439 mac_ptr = section->buffer + offset;
15440 mac_end = section->buffer + section->size;
15441
15442 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15443 &offset_size, section_is_gnu);
15444 if (mac_ptr == NULL)
15445 {
15446 /* We already issued a complaint. */
15447 return;
15448 }
15449
15450 do
15451 {
15452 /* Do we at least have room for a macinfo type byte? */
15453 if (mac_ptr >= mac_end)
15454 {
15455 /* Complaint is printed during the second pass as GDB will probably
15456 stop the first pass earlier upon finding
15457 DW_MACINFO_start_file. */
15458 break;
15459 }
15460
15461 macinfo_type = read_1_byte (abfd, mac_ptr);
15462 mac_ptr++;
15463
15464 /* Note that we rely on the fact that the corresponding GNU and
15465 DWARF constants are the same. */
15466 switch (macinfo_type)
15467 {
15468 /* A zero macinfo type indicates the end of the macro
15469 information. */
15470 case 0:
15471 break;
15472
15473 case DW_MACRO_GNU_define:
15474 case DW_MACRO_GNU_undef:
15475 /* Only skip the data by MAC_PTR. */
15476 {
15477 unsigned int bytes_read;
15478
15479 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15480 mac_ptr += bytes_read;
15481 read_direct_string (abfd, mac_ptr, &bytes_read);
15482 mac_ptr += bytes_read;
15483 }
15484 break;
15485
15486 case DW_MACRO_GNU_start_file:
15487 {
15488 unsigned int bytes_read;
15489 int line, file;
15490
15491 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15492 mac_ptr += bytes_read;
15493 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15494 mac_ptr += bytes_read;
15495
15496 current_file = macro_start_file (file, line, current_file,
15497 comp_dir, lh, objfile);
15498 }
15499 break;
15500
15501 case DW_MACRO_GNU_end_file:
15502 /* No data to skip by MAC_PTR. */
15503 break;
15504
15505 case DW_MACRO_GNU_define_indirect:
15506 case DW_MACRO_GNU_undef_indirect:
15507 {
15508 unsigned int bytes_read;
15509
15510 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15511 mac_ptr += bytes_read;
15512 mac_ptr += offset_size;
15513 }
15514 break;
15515
15516 case DW_MACRO_GNU_transparent_include:
15517 /* Note that, according to the spec, a transparent include
15518 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15519 skip this opcode. */
15520 mac_ptr += offset_size;
15521 break;
15522
15523 case DW_MACINFO_vendor_ext:
15524 /* Only skip the data by MAC_PTR. */
15525 if (!section_is_gnu)
15526 {
15527 unsigned int bytes_read;
15528
15529 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15530 mac_ptr += bytes_read;
15531 read_direct_string (abfd, mac_ptr, &bytes_read);
15532 mac_ptr += bytes_read;
15533 }
15534 /* FALLTHROUGH */
15535
15536 default:
15537 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15538 mac_ptr, abfd, offset_size,
15539 section);
15540 if (mac_ptr == NULL)
15541 return;
15542 break;
15543 }
15544 } while (macinfo_type != 0 && current_file == NULL);
15545
15546 /* Second pass: Process all entries.
15547
15548 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15549 command-line macro definitions/undefinitions. This flag is unset when we
15550 reach the first DW_MACINFO_start_file entry. */
15551
15552 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15553 current_file, lh, comp_dir, section, section_is_gnu,
15554 offset_size, objfile);
15555 }
15556
15557 /* Check if the attribute's form is a DW_FORM_block*
15558 if so return true else false. */
15559 static int
15560 attr_form_is_block (struct attribute *attr)
15561 {
15562 return (attr == NULL ? 0 :
15563 attr->form == DW_FORM_block1
15564 || attr->form == DW_FORM_block2
15565 || attr->form == DW_FORM_block4
15566 || attr->form == DW_FORM_block
15567 || attr->form == DW_FORM_exprloc);
15568 }
15569
15570 /* Return non-zero if ATTR's value is a section offset --- classes
15571 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15572 You may use DW_UNSND (attr) to retrieve such offsets.
15573
15574 Section 7.5.4, "Attribute Encodings", explains that no attribute
15575 may have a value that belongs to more than one of these classes; it
15576 would be ambiguous if we did, because we use the same forms for all
15577 of them. */
15578 static int
15579 attr_form_is_section_offset (struct attribute *attr)
15580 {
15581 return (attr->form == DW_FORM_data4
15582 || attr->form == DW_FORM_data8
15583 || attr->form == DW_FORM_sec_offset);
15584 }
15585
15586
15587 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15588 zero otherwise. When this function returns true, you can apply
15589 dwarf2_get_attr_constant_value to it.
15590
15591 However, note that for some attributes you must check
15592 attr_form_is_section_offset before using this test. DW_FORM_data4
15593 and DW_FORM_data8 are members of both the constant class, and of
15594 the classes that contain offsets into other debug sections
15595 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15596 that, if an attribute's can be either a constant or one of the
15597 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15598 taken as section offsets, not constants. */
15599 static int
15600 attr_form_is_constant (struct attribute *attr)
15601 {
15602 switch (attr->form)
15603 {
15604 case DW_FORM_sdata:
15605 case DW_FORM_udata:
15606 case DW_FORM_data1:
15607 case DW_FORM_data2:
15608 case DW_FORM_data4:
15609 case DW_FORM_data8:
15610 return 1;
15611 default:
15612 return 0;
15613 }
15614 }
15615
15616 /* A helper function that fills in a dwarf2_loclist_baton. */
15617
15618 static void
15619 fill_in_loclist_baton (struct dwarf2_cu *cu,
15620 struct dwarf2_loclist_baton *baton,
15621 struct attribute *attr)
15622 {
15623 dwarf2_read_section (dwarf2_per_objfile->objfile,
15624 &dwarf2_per_objfile->loc);
15625
15626 baton->per_cu = cu->per_cu;
15627 gdb_assert (baton->per_cu);
15628 /* We don't know how long the location list is, but make sure we
15629 don't run off the edge of the section. */
15630 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15631 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15632 baton->base_address = cu->base_address;
15633 }
15634
15635 static void
15636 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15637 struct dwarf2_cu *cu)
15638 {
15639 struct objfile *objfile = dwarf2_per_objfile->objfile;
15640
15641 if (attr_form_is_section_offset (attr)
15642 /* ".debug_loc" may not exist at all, or the offset may be outside
15643 the section. If so, fall through to the complaint in the
15644 other branch. */
15645 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15646 &dwarf2_per_objfile->loc))
15647 {
15648 struct dwarf2_loclist_baton *baton;
15649
15650 baton = obstack_alloc (&objfile->objfile_obstack,
15651 sizeof (struct dwarf2_loclist_baton));
15652
15653 fill_in_loclist_baton (cu, baton, attr);
15654
15655 if (cu->base_known == 0)
15656 complaint (&symfile_complaints,
15657 _("Location list used without "
15658 "specifying the CU base address."));
15659
15660 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15661 SYMBOL_LOCATION_BATON (sym) = baton;
15662 }
15663 else
15664 {
15665 struct dwarf2_locexpr_baton *baton;
15666
15667 baton = obstack_alloc (&objfile->objfile_obstack,
15668 sizeof (struct dwarf2_locexpr_baton));
15669 baton->per_cu = cu->per_cu;
15670 gdb_assert (baton->per_cu);
15671
15672 if (attr_form_is_block (attr))
15673 {
15674 /* Note that we're just copying the block's data pointer
15675 here, not the actual data. We're still pointing into the
15676 info_buffer for SYM's objfile; right now we never release
15677 that buffer, but when we do clean up properly this may
15678 need to change. */
15679 baton->size = DW_BLOCK (attr)->size;
15680 baton->data = DW_BLOCK (attr)->data;
15681 }
15682 else
15683 {
15684 dwarf2_invalid_attrib_class_complaint ("location description",
15685 SYMBOL_NATURAL_NAME (sym));
15686 baton->size = 0;
15687 }
15688
15689 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15690 SYMBOL_LOCATION_BATON (sym) = baton;
15691 }
15692 }
15693
15694 /* Return the OBJFILE associated with the compilation unit CU. If CU
15695 came from a separate debuginfo file, then the master objfile is
15696 returned. */
15697
15698 struct objfile *
15699 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15700 {
15701 struct objfile *objfile = per_cu->objfile;
15702
15703 /* Return the master objfile, so that we can report and look up the
15704 correct file containing this variable. */
15705 if (objfile->separate_debug_objfile_backlink)
15706 objfile = objfile->separate_debug_objfile_backlink;
15707
15708 return objfile;
15709 }
15710
15711 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15712 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15713 CU_HEADERP first. */
15714
15715 static const struct comp_unit_head *
15716 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15717 struct dwarf2_per_cu_data *per_cu)
15718 {
15719 struct objfile *objfile;
15720 struct dwarf2_per_objfile *per_objfile;
15721 gdb_byte *info_ptr;
15722
15723 if (per_cu->cu)
15724 return &per_cu->cu->header;
15725
15726 objfile = per_cu->objfile;
15727 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15728 info_ptr = per_objfile->info.buffer + per_cu->offset;
15729
15730 memset (cu_headerp, 0, sizeof (*cu_headerp));
15731 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15732
15733 return cu_headerp;
15734 }
15735
15736 /* Return the address size given in the compilation unit header for CU. */
15737
15738 CORE_ADDR
15739 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15740 {
15741 struct comp_unit_head cu_header_local;
15742 const struct comp_unit_head *cu_headerp;
15743
15744 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15745
15746 return cu_headerp->addr_size;
15747 }
15748
15749 /* Return the offset size given in the compilation unit header for CU. */
15750
15751 int
15752 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15753 {
15754 struct comp_unit_head cu_header_local;
15755 const struct comp_unit_head *cu_headerp;
15756
15757 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15758
15759 return cu_headerp->offset_size;
15760 }
15761
15762 /* See its dwarf2loc.h declaration. */
15763
15764 int
15765 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15766 {
15767 struct comp_unit_head cu_header_local;
15768 const struct comp_unit_head *cu_headerp;
15769
15770 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15771
15772 if (cu_headerp->version == 2)
15773 return cu_headerp->addr_size;
15774 else
15775 return cu_headerp->offset_size;
15776 }
15777
15778 /* Return the text offset of the CU. The returned offset comes from
15779 this CU's objfile. If this objfile came from a separate debuginfo
15780 file, then the offset may be different from the corresponding
15781 offset in the parent objfile. */
15782
15783 CORE_ADDR
15784 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15785 {
15786 struct objfile *objfile = per_cu->objfile;
15787
15788 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15789 }
15790
15791 /* Locate the .debug_info compilation unit from CU's objfile which contains
15792 the DIE at OFFSET. Raises an error on failure. */
15793
15794 static struct dwarf2_per_cu_data *
15795 dwarf2_find_containing_comp_unit (unsigned int offset,
15796 struct objfile *objfile)
15797 {
15798 struct dwarf2_per_cu_data *this_cu;
15799 int low, high;
15800
15801 low = 0;
15802 high = dwarf2_per_objfile->n_comp_units - 1;
15803 while (high > low)
15804 {
15805 int mid = low + (high - low) / 2;
15806
15807 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15808 high = mid;
15809 else
15810 low = mid + 1;
15811 }
15812 gdb_assert (low == high);
15813 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15814 {
15815 if (low == 0)
15816 error (_("Dwarf Error: could not find partial DIE containing "
15817 "offset 0x%lx [in module %s]"),
15818 (long) offset, bfd_get_filename (objfile->obfd));
15819
15820 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15821 return dwarf2_per_objfile->all_comp_units[low-1];
15822 }
15823 else
15824 {
15825 this_cu = dwarf2_per_objfile->all_comp_units[low];
15826 if (low == dwarf2_per_objfile->n_comp_units - 1
15827 && offset >= this_cu->offset + this_cu->length)
15828 error (_("invalid dwarf2 offset %u"), offset);
15829 gdb_assert (offset < this_cu->offset + this_cu->length);
15830 return this_cu;
15831 }
15832 }
15833
15834 /* Locate the compilation unit from OBJFILE which is located at exactly
15835 OFFSET. Raises an error on failure. */
15836
15837 static struct dwarf2_per_cu_data *
15838 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15839 {
15840 struct dwarf2_per_cu_data *this_cu;
15841
15842 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15843 if (this_cu->offset != offset)
15844 error (_("no compilation unit with offset %u."), offset);
15845 return this_cu;
15846 }
15847
15848 /* Initialize dwarf2_cu CU, owned by PER_CU. */
15849
15850 static void
15851 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15852 {
15853 memset (cu, 0, sizeof (*cu));
15854 per_cu->cu = cu;
15855 cu->per_cu = per_cu;
15856 cu->objfile = per_cu->objfile;
15857 obstack_init (&cu->comp_unit_obstack);
15858 }
15859
15860 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15861
15862 static void
15863 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15864 {
15865 struct attribute *attr;
15866
15867 /* Set the language we're debugging. */
15868 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15869 if (attr)
15870 set_cu_language (DW_UNSND (attr), cu);
15871 else
15872 {
15873 cu->language = language_minimal;
15874 cu->language_defn = language_def (cu->language);
15875 }
15876 }
15877
15878 /* Release one cached compilation unit, CU. We unlink it from the tree
15879 of compilation units, but we don't remove it from the read_in_chain;
15880 the caller is responsible for that.
15881 NOTE: DATA is a void * because this function is also used as a
15882 cleanup routine. */
15883
15884 static void
15885 free_heap_comp_unit (void *data)
15886 {
15887 struct dwarf2_cu *cu = data;
15888
15889 gdb_assert (cu->per_cu != NULL);
15890 cu->per_cu->cu = NULL;
15891 cu->per_cu = NULL;
15892
15893 obstack_free (&cu->comp_unit_obstack, NULL);
15894
15895 xfree (cu);
15896 }
15897
15898 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15899 when we're finished with it. We can't free the pointer itself, but be
15900 sure to unlink it from the cache. Also release any associated storage
15901 and perform cache maintenance.
15902
15903 Only used during partial symbol parsing. */
15904
15905 static void
15906 free_stack_comp_unit (void *data)
15907 {
15908 struct dwarf2_cu *cu = data;
15909
15910 gdb_assert (cu->per_cu != NULL);
15911 cu->per_cu->cu = NULL;
15912 cu->per_cu = NULL;
15913
15914 obstack_free (&cu->comp_unit_obstack, NULL);
15915 cu->partial_dies = NULL;
15916
15917 /* The previous code only did this if per_cu != NULL.
15918 But that would always succeed, so now we just unconditionally do
15919 the aging. This seems like the wrong place to do such aging,
15920 but cleaning that up is left for later. */
15921 age_cached_comp_units ();
15922 }
15923
15924 /* Free all cached compilation units. */
15925
15926 static void
15927 free_cached_comp_units (void *data)
15928 {
15929 struct dwarf2_per_cu_data *per_cu, **last_chain;
15930
15931 per_cu = dwarf2_per_objfile->read_in_chain;
15932 last_chain = &dwarf2_per_objfile->read_in_chain;
15933 while (per_cu != NULL)
15934 {
15935 struct dwarf2_per_cu_data *next_cu;
15936
15937 next_cu = per_cu->cu->read_in_chain;
15938
15939 free_heap_comp_unit (per_cu->cu);
15940 *last_chain = next_cu;
15941
15942 per_cu = next_cu;
15943 }
15944 }
15945
15946 /* Increase the age counter on each cached compilation unit, and free
15947 any that are too old. */
15948
15949 static void
15950 age_cached_comp_units (void)
15951 {
15952 struct dwarf2_per_cu_data *per_cu, **last_chain;
15953
15954 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15955 per_cu = dwarf2_per_objfile->read_in_chain;
15956 while (per_cu != NULL)
15957 {
15958 per_cu->cu->last_used ++;
15959 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15960 dwarf2_mark (per_cu->cu);
15961 per_cu = per_cu->cu->read_in_chain;
15962 }
15963
15964 per_cu = dwarf2_per_objfile->read_in_chain;
15965 last_chain = &dwarf2_per_objfile->read_in_chain;
15966 while (per_cu != NULL)
15967 {
15968 struct dwarf2_per_cu_data *next_cu;
15969
15970 next_cu = per_cu->cu->read_in_chain;
15971
15972 if (!per_cu->cu->mark)
15973 {
15974 free_heap_comp_unit (per_cu->cu);
15975 *last_chain = next_cu;
15976 }
15977 else
15978 last_chain = &per_cu->cu->read_in_chain;
15979
15980 per_cu = next_cu;
15981 }
15982 }
15983
15984 /* Remove a single compilation unit from the cache. */
15985
15986 static void
15987 free_one_cached_comp_unit (void *target_cu)
15988 {
15989 struct dwarf2_per_cu_data *per_cu, **last_chain;
15990
15991 per_cu = dwarf2_per_objfile->read_in_chain;
15992 last_chain = &dwarf2_per_objfile->read_in_chain;
15993 while (per_cu != NULL)
15994 {
15995 struct dwarf2_per_cu_data *next_cu;
15996
15997 next_cu = per_cu->cu->read_in_chain;
15998
15999 if (per_cu->cu == target_cu)
16000 {
16001 free_heap_comp_unit (per_cu->cu);
16002 *last_chain = next_cu;
16003 break;
16004 }
16005 else
16006 last_chain = &per_cu->cu->read_in_chain;
16007
16008 per_cu = next_cu;
16009 }
16010 }
16011
16012 /* Release all extra memory associated with OBJFILE. */
16013
16014 void
16015 dwarf2_free_objfile (struct objfile *objfile)
16016 {
16017 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16018
16019 if (dwarf2_per_objfile == NULL)
16020 return;
16021
16022 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16023 free_cached_comp_units (NULL);
16024
16025 if (dwarf2_per_objfile->quick_file_names_table)
16026 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16027
16028 /* Everything else should be on the objfile obstack. */
16029 }
16030
16031 /* A pair of DIE offset and GDB type pointer. We store these
16032 in a hash table separate from the DIEs, and preserve them
16033 when the DIEs are flushed out of cache. */
16034
16035 struct dwarf2_offset_and_type
16036 {
16037 unsigned int offset;
16038 struct type *type;
16039 };
16040
16041 /* Hash function for a dwarf2_offset_and_type. */
16042
16043 static hashval_t
16044 offset_and_type_hash (const void *item)
16045 {
16046 const struct dwarf2_offset_and_type *ofs = item;
16047
16048 return ofs->offset;
16049 }
16050
16051 /* Equality function for a dwarf2_offset_and_type. */
16052
16053 static int
16054 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16055 {
16056 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16057 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16058
16059 return ofs_lhs->offset == ofs_rhs->offset;
16060 }
16061
16062 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16063 table if necessary. For convenience, return TYPE.
16064
16065 The DIEs reading must have careful ordering to:
16066 * Not cause infite loops trying to read in DIEs as a prerequisite for
16067 reading current DIE.
16068 * Not trying to dereference contents of still incompletely read in types
16069 while reading in other DIEs.
16070 * Enable referencing still incompletely read in types just by a pointer to
16071 the type without accessing its fields.
16072
16073 Therefore caller should follow these rules:
16074 * Try to fetch any prerequisite types we may need to build this DIE type
16075 before building the type and calling set_die_type.
16076 * After building type call set_die_type for current DIE as soon as
16077 possible before fetching more types to complete the current type.
16078 * Make the type as complete as possible before fetching more types. */
16079
16080 static struct type *
16081 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16082 {
16083 struct dwarf2_offset_and_type **slot, ofs;
16084 struct objfile *objfile = cu->objfile;
16085 htab_t *type_hash_ptr;
16086
16087 /* For Ada types, make sure that the gnat-specific data is always
16088 initialized (if not already set). There are a few types where
16089 we should not be doing so, because the type-specific area is
16090 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16091 where the type-specific area is used to store the floatformat).
16092 But this is not a problem, because the gnat-specific information
16093 is actually not needed for these types. */
16094 if (need_gnat_info (cu)
16095 && TYPE_CODE (type) != TYPE_CODE_FUNC
16096 && TYPE_CODE (type) != TYPE_CODE_FLT
16097 && !HAVE_GNAT_AUX_INFO (type))
16098 INIT_GNAT_SPECIFIC (type);
16099
16100 if (cu->per_cu->debug_types_section)
16101 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16102 else
16103 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16104
16105 if (*type_hash_ptr == NULL)
16106 {
16107 *type_hash_ptr
16108 = htab_create_alloc_ex (127,
16109 offset_and_type_hash,
16110 offset_and_type_eq,
16111 NULL,
16112 &objfile->objfile_obstack,
16113 hashtab_obstack_allocate,
16114 dummy_obstack_deallocate);
16115 }
16116
16117 ofs.offset = die->offset;
16118 ofs.type = type;
16119 slot = (struct dwarf2_offset_and_type **)
16120 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16121 if (*slot)
16122 complaint (&symfile_complaints,
16123 _("A problem internal to GDB: DIE 0x%x has type already set"),
16124 die->offset);
16125 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16126 **slot = ofs;
16127 return type;
16128 }
16129
16130 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16131 table, or return NULL if the die does not have a saved type. */
16132
16133 static struct type *
16134 get_die_type_at_offset (unsigned int offset,
16135 struct dwarf2_per_cu_data *per_cu)
16136 {
16137 struct dwarf2_offset_and_type *slot, ofs;
16138 htab_t type_hash;
16139
16140 if (per_cu->debug_types_section)
16141 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16142 else
16143 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16144 if (type_hash == NULL)
16145 return NULL;
16146
16147 ofs.offset = offset;
16148 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16149 if (slot)
16150 return slot->type;
16151 else
16152 return NULL;
16153 }
16154
16155 /* Look up the type for DIE in the appropriate type_hash table,
16156 or return NULL if DIE does not have a saved type. */
16157
16158 static struct type *
16159 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16160 {
16161 return get_die_type_at_offset (die->offset, cu->per_cu);
16162 }
16163
16164 /* Add a dependence relationship from CU to REF_PER_CU. */
16165
16166 static void
16167 dwarf2_add_dependence (struct dwarf2_cu *cu,
16168 struct dwarf2_per_cu_data *ref_per_cu)
16169 {
16170 void **slot;
16171
16172 if (cu->dependencies == NULL)
16173 cu->dependencies
16174 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16175 NULL, &cu->comp_unit_obstack,
16176 hashtab_obstack_allocate,
16177 dummy_obstack_deallocate);
16178
16179 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16180 if (*slot == NULL)
16181 *slot = ref_per_cu;
16182 }
16183
16184 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16185 Set the mark field in every compilation unit in the
16186 cache that we must keep because we are keeping CU. */
16187
16188 static int
16189 dwarf2_mark_helper (void **slot, void *data)
16190 {
16191 struct dwarf2_per_cu_data *per_cu;
16192
16193 per_cu = (struct dwarf2_per_cu_data *) *slot;
16194
16195 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16196 reading of the chain. As such dependencies remain valid it is not much
16197 useful to track and undo them during QUIT cleanups. */
16198 if (per_cu->cu == NULL)
16199 return 1;
16200
16201 if (per_cu->cu->mark)
16202 return 1;
16203 per_cu->cu->mark = 1;
16204
16205 if (per_cu->cu->dependencies != NULL)
16206 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16207
16208 return 1;
16209 }
16210
16211 /* Set the mark field in CU and in every other compilation unit in the
16212 cache that we must keep because we are keeping CU. */
16213
16214 static void
16215 dwarf2_mark (struct dwarf2_cu *cu)
16216 {
16217 if (cu->mark)
16218 return;
16219 cu->mark = 1;
16220 if (cu->dependencies != NULL)
16221 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16222 }
16223
16224 static void
16225 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16226 {
16227 while (per_cu)
16228 {
16229 per_cu->cu->mark = 0;
16230 per_cu = per_cu->cu->read_in_chain;
16231 }
16232 }
16233
16234 /* Trivial hash function for partial_die_info: the hash value of a DIE
16235 is its offset in .debug_info for this objfile. */
16236
16237 static hashval_t
16238 partial_die_hash (const void *item)
16239 {
16240 const struct partial_die_info *part_die = item;
16241
16242 return part_die->offset;
16243 }
16244
16245 /* Trivial comparison function for partial_die_info structures: two DIEs
16246 are equal if they have the same offset. */
16247
16248 static int
16249 partial_die_eq (const void *item_lhs, const void *item_rhs)
16250 {
16251 const struct partial_die_info *part_die_lhs = item_lhs;
16252 const struct partial_die_info *part_die_rhs = item_rhs;
16253
16254 return part_die_lhs->offset == part_die_rhs->offset;
16255 }
16256
16257 static struct cmd_list_element *set_dwarf2_cmdlist;
16258 static struct cmd_list_element *show_dwarf2_cmdlist;
16259
16260 static void
16261 set_dwarf2_cmd (char *args, int from_tty)
16262 {
16263 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16264 }
16265
16266 static void
16267 show_dwarf2_cmd (char *args, int from_tty)
16268 {
16269 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16270 }
16271
16272 /* If section described by INFO was mmapped, munmap it now. */
16273
16274 static void
16275 munmap_section_buffer (struct dwarf2_section_info *info)
16276 {
16277 if (info->map_addr != NULL)
16278 {
16279 #ifdef HAVE_MMAP
16280 int res;
16281
16282 res = munmap (info->map_addr, info->map_len);
16283 gdb_assert (res == 0);
16284 #else
16285 /* Without HAVE_MMAP, we should never be here to begin with. */
16286 gdb_assert_not_reached ("no mmap support");
16287 #endif
16288 }
16289 }
16290
16291 /* munmap debug sections for OBJFILE, if necessary. */
16292
16293 static void
16294 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16295 {
16296 struct dwarf2_per_objfile *data = d;
16297 int ix;
16298 struct dwarf2_section_info *section;
16299
16300 /* This is sorted according to the order they're defined in to make it easier
16301 to keep in sync. */
16302 munmap_section_buffer (&data->info);
16303 munmap_section_buffer (&data->abbrev);
16304 munmap_section_buffer (&data->line);
16305 munmap_section_buffer (&data->loc);
16306 munmap_section_buffer (&data->macinfo);
16307 munmap_section_buffer (&data->macro);
16308 munmap_section_buffer (&data->str);
16309 munmap_section_buffer (&data->ranges);
16310 munmap_section_buffer (&data->frame);
16311 munmap_section_buffer (&data->eh_frame);
16312 munmap_section_buffer (&data->gdb_index);
16313
16314 for (ix = 0;
16315 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16316 ++ix)
16317 munmap_section_buffer (section);
16318
16319 VEC_free (dwarf2_section_info_def, data->types);
16320 }
16321
16322 \f
16323 /* The "save gdb-index" command. */
16324
16325 /* The contents of the hash table we create when building the string
16326 table. */
16327 struct strtab_entry
16328 {
16329 offset_type offset;
16330 const char *str;
16331 };
16332
16333 /* Hash function for a strtab_entry.
16334
16335 Function is used only during write_hash_table so no index format backward
16336 compatibility is needed. */
16337
16338 static hashval_t
16339 hash_strtab_entry (const void *e)
16340 {
16341 const struct strtab_entry *entry = e;
16342 return mapped_index_string_hash (INT_MAX, entry->str);
16343 }
16344
16345 /* Equality function for a strtab_entry. */
16346
16347 static int
16348 eq_strtab_entry (const void *a, const void *b)
16349 {
16350 const struct strtab_entry *ea = a;
16351 const struct strtab_entry *eb = b;
16352 return !strcmp (ea->str, eb->str);
16353 }
16354
16355 /* Create a strtab_entry hash table. */
16356
16357 static htab_t
16358 create_strtab (void)
16359 {
16360 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16361 xfree, xcalloc, xfree);
16362 }
16363
16364 /* Add a string to the constant pool. Return the string's offset in
16365 host order. */
16366
16367 static offset_type
16368 add_string (htab_t table, struct obstack *cpool, const char *str)
16369 {
16370 void **slot;
16371 struct strtab_entry entry;
16372 struct strtab_entry *result;
16373
16374 entry.str = str;
16375 slot = htab_find_slot (table, &entry, INSERT);
16376 if (*slot)
16377 result = *slot;
16378 else
16379 {
16380 result = XNEW (struct strtab_entry);
16381 result->offset = obstack_object_size (cpool);
16382 result->str = str;
16383 obstack_grow_str0 (cpool, str);
16384 *slot = result;
16385 }
16386 return result->offset;
16387 }
16388
16389 /* An entry in the symbol table. */
16390 struct symtab_index_entry
16391 {
16392 /* The name of the symbol. */
16393 const char *name;
16394 /* The offset of the name in the constant pool. */
16395 offset_type index_offset;
16396 /* A sorted vector of the indices of all the CUs that hold an object
16397 of this name. */
16398 VEC (offset_type) *cu_indices;
16399 };
16400
16401 /* The symbol table. This is a power-of-2-sized hash table. */
16402 struct mapped_symtab
16403 {
16404 offset_type n_elements;
16405 offset_type size;
16406 struct symtab_index_entry **data;
16407 };
16408
16409 /* Hash function for a symtab_index_entry. */
16410
16411 static hashval_t
16412 hash_symtab_entry (const void *e)
16413 {
16414 const struct symtab_index_entry *entry = e;
16415 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16416 sizeof (offset_type) * VEC_length (offset_type,
16417 entry->cu_indices),
16418 0);
16419 }
16420
16421 /* Equality function for a symtab_index_entry. */
16422
16423 static int
16424 eq_symtab_entry (const void *a, const void *b)
16425 {
16426 const struct symtab_index_entry *ea = a;
16427 const struct symtab_index_entry *eb = b;
16428 int len = VEC_length (offset_type, ea->cu_indices);
16429 if (len != VEC_length (offset_type, eb->cu_indices))
16430 return 0;
16431 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16432 VEC_address (offset_type, eb->cu_indices),
16433 sizeof (offset_type) * len);
16434 }
16435
16436 /* Destroy a symtab_index_entry. */
16437
16438 static void
16439 delete_symtab_entry (void *p)
16440 {
16441 struct symtab_index_entry *entry = p;
16442 VEC_free (offset_type, entry->cu_indices);
16443 xfree (entry);
16444 }
16445
16446 /* Create a hash table holding symtab_index_entry objects. */
16447
16448 static htab_t
16449 create_symbol_hash_table (void)
16450 {
16451 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16452 delete_symtab_entry, xcalloc, xfree);
16453 }
16454
16455 /* Create a new mapped symtab object. */
16456
16457 static struct mapped_symtab *
16458 create_mapped_symtab (void)
16459 {
16460 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16461 symtab->n_elements = 0;
16462 symtab->size = 1024;
16463 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16464 return symtab;
16465 }
16466
16467 /* Destroy a mapped_symtab. */
16468
16469 static void
16470 cleanup_mapped_symtab (void *p)
16471 {
16472 struct mapped_symtab *symtab = p;
16473 /* The contents of the array are freed when the other hash table is
16474 destroyed. */
16475 xfree (symtab->data);
16476 xfree (symtab);
16477 }
16478
16479 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16480 the slot.
16481
16482 Function is used only during write_hash_table so no index format backward
16483 compatibility is needed. */
16484
16485 static struct symtab_index_entry **
16486 find_slot (struct mapped_symtab *symtab, const char *name)
16487 {
16488 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16489
16490 index = hash & (symtab->size - 1);
16491 step = ((hash * 17) & (symtab->size - 1)) | 1;
16492
16493 for (;;)
16494 {
16495 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16496 return &symtab->data[index];
16497 index = (index + step) & (symtab->size - 1);
16498 }
16499 }
16500
16501 /* Expand SYMTAB's hash table. */
16502
16503 static void
16504 hash_expand (struct mapped_symtab *symtab)
16505 {
16506 offset_type old_size = symtab->size;
16507 offset_type i;
16508 struct symtab_index_entry **old_entries = symtab->data;
16509
16510 symtab->size *= 2;
16511 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16512
16513 for (i = 0; i < old_size; ++i)
16514 {
16515 if (old_entries[i])
16516 {
16517 struct symtab_index_entry **slot = find_slot (symtab,
16518 old_entries[i]->name);
16519 *slot = old_entries[i];
16520 }
16521 }
16522
16523 xfree (old_entries);
16524 }
16525
16526 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16527 is the index of the CU in which the symbol appears. */
16528
16529 static void
16530 add_index_entry (struct mapped_symtab *symtab, const char *name,
16531 offset_type cu_index)
16532 {
16533 struct symtab_index_entry **slot;
16534
16535 ++symtab->n_elements;
16536 if (4 * symtab->n_elements / 3 >= symtab->size)
16537 hash_expand (symtab);
16538
16539 slot = find_slot (symtab, name);
16540 if (!*slot)
16541 {
16542 *slot = XNEW (struct symtab_index_entry);
16543 (*slot)->name = name;
16544 (*slot)->cu_indices = NULL;
16545 }
16546 /* Don't push an index twice. Due to how we add entries we only
16547 have to check the last one. */
16548 if (VEC_empty (offset_type, (*slot)->cu_indices)
16549 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16550 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16551 }
16552
16553 /* Add a vector of indices to the constant pool. */
16554
16555 static offset_type
16556 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16557 struct symtab_index_entry *entry)
16558 {
16559 void **slot;
16560
16561 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16562 if (!*slot)
16563 {
16564 offset_type len = VEC_length (offset_type, entry->cu_indices);
16565 offset_type val = MAYBE_SWAP (len);
16566 offset_type iter;
16567 int i;
16568
16569 *slot = entry;
16570 entry->index_offset = obstack_object_size (cpool);
16571
16572 obstack_grow (cpool, &val, sizeof (val));
16573 for (i = 0;
16574 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16575 ++i)
16576 {
16577 val = MAYBE_SWAP (iter);
16578 obstack_grow (cpool, &val, sizeof (val));
16579 }
16580 }
16581 else
16582 {
16583 struct symtab_index_entry *old_entry = *slot;
16584 entry->index_offset = old_entry->index_offset;
16585 entry = old_entry;
16586 }
16587 return entry->index_offset;
16588 }
16589
16590 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16591 constant pool entries going into the obstack CPOOL. */
16592
16593 static void
16594 write_hash_table (struct mapped_symtab *symtab,
16595 struct obstack *output, struct obstack *cpool)
16596 {
16597 offset_type i;
16598 htab_t symbol_hash_table;
16599 htab_t str_table;
16600
16601 symbol_hash_table = create_symbol_hash_table ();
16602 str_table = create_strtab ();
16603
16604 /* We add all the index vectors to the constant pool first, to
16605 ensure alignment is ok. */
16606 for (i = 0; i < symtab->size; ++i)
16607 {
16608 if (symtab->data[i])
16609 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16610 }
16611
16612 /* Now write out the hash table. */
16613 for (i = 0; i < symtab->size; ++i)
16614 {
16615 offset_type str_off, vec_off;
16616
16617 if (symtab->data[i])
16618 {
16619 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16620 vec_off = symtab->data[i]->index_offset;
16621 }
16622 else
16623 {
16624 /* While 0 is a valid constant pool index, it is not valid
16625 to have 0 for both offsets. */
16626 str_off = 0;
16627 vec_off = 0;
16628 }
16629
16630 str_off = MAYBE_SWAP (str_off);
16631 vec_off = MAYBE_SWAP (vec_off);
16632
16633 obstack_grow (output, &str_off, sizeof (str_off));
16634 obstack_grow (output, &vec_off, sizeof (vec_off));
16635 }
16636
16637 htab_delete (str_table);
16638 htab_delete (symbol_hash_table);
16639 }
16640
16641 /* Struct to map psymtab to CU index in the index file. */
16642 struct psymtab_cu_index_map
16643 {
16644 struct partial_symtab *psymtab;
16645 unsigned int cu_index;
16646 };
16647
16648 static hashval_t
16649 hash_psymtab_cu_index (const void *item)
16650 {
16651 const struct psymtab_cu_index_map *map = item;
16652
16653 return htab_hash_pointer (map->psymtab);
16654 }
16655
16656 static int
16657 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16658 {
16659 const struct psymtab_cu_index_map *lhs = item_lhs;
16660 const struct psymtab_cu_index_map *rhs = item_rhs;
16661
16662 return lhs->psymtab == rhs->psymtab;
16663 }
16664
16665 /* Helper struct for building the address table. */
16666 struct addrmap_index_data
16667 {
16668 struct objfile *objfile;
16669 struct obstack *addr_obstack;
16670 htab_t cu_index_htab;
16671
16672 /* Non-zero if the previous_* fields are valid.
16673 We can't write an entry until we see the next entry (since it is only then
16674 that we know the end of the entry). */
16675 int previous_valid;
16676 /* Index of the CU in the table of all CUs in the index file. */
16677 unsigned int previous_cu_index;
16678 /* Start address of the CU. */
16679 CORE_ADDR previous_cu_start;
16680 };
16681
16682 /* Write an address entry to OBSTACK. */
16683
16684 static void
16685 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16686 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16687 {
16688 offset_type cu_index_to_write;
16689 char addr[8];
16690 CORE_ADDR baseaddr;
16691
16692 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16693
16694 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16695 obstack_grow (obstack, addr, 8);
16696 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16697 obstack_grow (obstack, addr, 8);
16698 cu_index_to_write = MAYBE_SWAP (cu_index);
16699 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16700 }
16701
16702 /* Worker function for traversing an addrmap to build the address table. */
16703
16704 static int
16705 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16706 {
16707 struct addrmap_index_data *data = datap;
16708 struct partial_symtab *pst = obj;
16709 offset_type cu_index;
16710 void **slot;
16711
16712 if (data->previous_valid)
16713 add_address_entry (data->objfile, data->addr_obstack,
16714 data->previous_cu_start, start_addr,
16715 data->previous_cu_index);
16716
16717 data->previous_cu_start = start_addr;
16718 if (pst != NULL)
16719 {
16720 struct psymtab_cu_index_map find_map, *map;
16721 find_map.psymtab = pst;
16722 map = htab_find (data->cu_index_htab, &find_map);
16723 gdb_assert (map != NULL);
16724 data->previous_cu_index = map->cu_index;
16725 data->previous_valid = 1;
16726 }
16727 else
16728 data->previous_valid = 0;
16729
16730 return 0;
16731 }
16732
16733 /* Write OBJFILE's address map to OBSTACK.
16734 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16735 in the index file. */
16736
16737 static void
16738 write_address_map (struct objfile *objfile, struct obstack *obstack,
16739 htab_t cu_index_htab)
16740 {
16741 struct addrmap_index_data addrmap_index_data;
16742
16743 /* When writing the address table, we have to cope with the fact that
16744 the addrmap iterator only provides the start of a region; we have to
16745 wait until the next invocation to get the start of the next region. */
16746
16747 addrmap_index_data.objfile = objfile;
16748 addrmap_index_data.addr_obstack = obstack;
16749 addrmap_index_data.cu_index_htab = cu_index_htab;
16750 addrmap_index_data.previous_valid = 0;
16751
16752 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16753 &addrmap_index_data);
16754
16755 /* It's highly unlikely the last entry (end address = 0xff...ff)
16756 is valid, but we should still handle it.
16757 The end address is recorded as the start of the next region, but that
16758 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16759 anyway. */
16760 if (addrmap_index_data.previous_valid)
16761 add_address_entry (objfile, obstack,
16762 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16763 addrmap_index_data.previous_cu_index);
16764 }
16765
16766 /* Add a list of partial symbols to SYMTAB. */
16767
16768 static void
16769 write_psymbols (struct mapped_symtab *symtab,
16770 htab_t psyms_seen,
16771 struct partial_symbol **psymp,
16772 int count,
16773 offset_type cu_index,
16774 int is_static)
16775 {
16776 for (; count-- > 0; ++psymp)
16777 {
16778 void **slot, *lookup;
16779
16780 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16781 error (_("Ada is not currently supported by the index"));
16782
16783 /* We only want to add a given psymbol once. However, we also
16784 want to account for whether it is global or static. So, we
16785 may add it twice, using slightly different values. */
16786 if (is_static)
16787 {
16788 uintptr_t val = 1 | (uintptr_t) *psymp;
16789
16790 lookup = (void *) val;
16791 }
16792 else
16793 lookup = *psymp;
16794
16795 /* Only add a given psymbol once. */
16796 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16797 if (!*slot)
16798 {
16799 *slot = lookup;
16800 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16801 }
16802 }
16803 }
16804
16805 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16806 exception if there is an error. */
16807
16808 static void
16809 write_obstack (FILE *file, struct obstack *obstack)
16810 {
16811 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16812 file)
16813 != obstack_object_size (obstack))
16814 error (_("couldn't data write to file"));
16815 }
16816
16817 /* Unlink a file if the argument is not NULL. */
16818
16819 static void
16820 unlink_if_set (void *p)
16821 {
16822 char **filename = p;
16823 if (*filename)
16824 unlink (*filename);
16825 }
16826
16827 /* A helper struct used when iterating over debug_types. */
16828 struct signatured_type_index_data
16829 {
16830 struct objfile *objfile;
16831 struct mapped_symtab *symtab;
16832 struct obstack *types_list;
16833 htab_t psyms_seen;
16834 int cu_index;
16835 };
16836
16837 /* A helper function that writes a single signatured_type to an
16838 obstack. */
16839
16840 static int
16841 write_one_signatured_type (void **slot, void *d)
16842 {
16843 struct signatured_type_index_data *info = d;
16844 struct signatured_type *entry = (struct signatured_type *) *slot;
16845 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16846 struct partial_symtab *psymtab = per_cu->v.psymtab;
16847 gdb_byte val[8];
16848
16849 write_psymbols (info->symtab,
16850 info->psyms_seen,
16851 info->objfile->global_psymbols.list
16852 + psymtab->globals_offset,
16853 psymtab->n_global_syms, info->cu_index,
16854 0);
16855 write_psymbols (info->symtab,
16856 info->psyms_seen,
16857 info->objfile->static_psymbols.list
16858 + psymtab->statics_offset,
16859 psymtab->n_static_syms, info->cu_index,
16860 1);
16861
16862 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16863 obstack_grow (info->types_list, val, 8);
16864 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16865 obstack_grow (info->types_list, val, 8);
16866 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16867 obstack_grow (info->types_list, val, 8);
16868
16869 ++info->cu_index;
16870
16871 return 1;
16872 }
16873
16874 /* Create an index file for OBJFILE in the directory DIR. */
16875
16876 static void
16877 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16878 {
16879 struct cleanup *cleanup;
16880 char *filename, *cleanup_filename;
16881 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16882 struct obstack cu_list, types_cu_list;
16883 int i;
16884 FILE *out_file;
16885 struct mapped_symtab *symtab;
16886 offset_type val, size_of_contents, total_len;
16887 struct stat st;
16888 char buf[8];
16889 htab_t psyms_seen;
16890 htab_t cu_index_htab;
16891 struct psymtab_cu_index_map *psymtab_cu_index_map;
16892
16893 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16894 return;
16895
16896 if (dwarf2_per_objfile->using_index)
16897 error (_("Cannot use an index to create the index"));
16898
16899 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16900 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16901
16902 if (stat (objfile->name, &st) < 0)
16903 perror_with_name (objfile->name);
16904
16905 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16906 INDEX_SUFFIX, (char *) NULL);
16907 cleanup = make_cleanup (xfree, filename);
16908
16909 out_file = fopen (filename, "wb");
16910 if (!out_file)
16911 error (_("Can't open `%s' for writing"), filename);
16912
16913 cleanup_filename = filename;
16914 make_cleanup (unlink_if_set, &cleanup_filename);
16915
16916 symtab = create_mapped_symtab ();
16917 make_cleanup (cleanup_mapped_symtab, symtab);
16918
16919 obstack_init (&addr_obstack);
16920 make_cleanup_obstack_free (&addr_obstack);
16921
16922 obstack_init (&cu_list);
16923 make_cleanup_obstack_free (&cu_list);
16924
16925 obstack_init (&types_cu_list);
16926 make_cleanup_obstack_free (&types_cu_list);
16927
16928 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16929 NULL, xcalloc, xfree);
16930 make_cleanup_htab_delete (psyms_seen);
16931
16932 /* While we're scanning CU's create a table that maps a psymtab pointer
16933 (which is what addrmap records) to its index (which is what is recorded
16934 in the index file). This will later be needed to write the address
16935 table. */
16936 cu_index_htab = htab_create_alloc (100,
16937 hash_psymtab_cu_index,
16938 eq_psymtab_cu_index,
16939 NULL, xcalloc, xfree);
16940 make_cleanup_htab_delete (cu_index_htab);
16941 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16942 xmalloc (sizeof (struct psymtab_cu_index_map)
16943 * dwarf2_per_objfile->n_comp_units);
16944 make_cleanup (xfree, psymtab_cu_index_map);
16945
16946 /* The CU list is already sorted, so we don't need to do additional
16947 work here. Also, the debug_types entries do not appear in
16948 all_comp_units, but only in their own hash table. */
16949 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16950 {
16951 struct dwarf2_per_cu_data *per_cu
16952 = dwarf2_per_objfile->all_comp_units[i];
16953 struct partial_symtab *psymtab = per_cu->v.psymtab;
16954 gdb_byte val[8];
16955 struct psymtab_cu_index_map *map;
16956 void **slot;
16957
16958 write_psymbols (symtab,
16959 psyms_seen,
16960 objfile->global_psymbols.list + psymtab->globals_offset,
16961 psymtab->n_global_syms, i,
16962 0);
16963 write_psymbols (symtab,
16964 psyms_seen,
16965 objfile->static_psymbols.list + psymtab->statics_offset,
16966 psymtab->n_static_syms, i,
16967 1);
16968
16969 map = &psymtab_cu_index_map[i];
16970 map->psymtab = psymtab;
16971 map->cu_index = i;
16972 slot = htab_find_slot (cu_index_htab, map, INSERT);
16973 gdb_assert (slot != NULL);
16974 gdb_assert (*slot == NULL);
16975 *slot = map;
16976
16977 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16978 obstack_grow (&cu_list, val, 8);
16979 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16980 obstack_grow (&cu_list, val, 8);
16981 }
16982
16983 /* Dump the address map. */
16984 write_address_map (objfile, &addr_obstack, cu_index_htab);
16985
16986 /* Write out the .debug_type entries, if any. */
16987 if (dwarf2_per_objfile->signatured_types)
16988 {
16989 struct signatured_type_index_data sig_data;
16990
16991 sig_data.objfile = objfile;
16992 sig_data.symtab = symtab;
16993 sig_data.types_list = &types_cu_list;
16994 sig_data.psyms_seen = psyms_seen;
16995 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16996 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16997 write_one_signatured_type, &sig_data);
16998 }
16999
17000 obstack_init (&constant_pool);
17001 make_cleanup_obstack_free (&constant_pool);
17002 obstack_init (&symtab_obstack);
17003 make_cleanup_obstack_free (&symtab_obstack);
17004 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17005
17006 obstack_init (&contents);
17007 make_cleanup_obstack_free (&contents);
17008 size_of_contents = 6 * sizeof (offset_type);
17009 total_len = size_of_contents;
17010
17011 /* The version number. */
17012 val = MAYBE_SWAP (5);
17013 obstack_grow (&contents, &val, sizeof (val));
17014
17015 /* The offset of the CU list from the start of the file. */
17016 val = MAYBE_SWAP (total_len);
17017 obstack_grow (&contents, &val, sizeof (val));
17018 total_len += obstack_object_size (&cu_list);
17019
17020 /* The offset of the types CU list from the start of the file. */
17021 val = MAYBE_SWAP (total_len);
17022 obstack_grow (&contents, &val, sizeof (val));
17023 total_len += obstack_object_size (&types_cu_list);
17024
17025 /* The offset of the address table from the start of the file. */
17026 val = MAYBE_SWAP (total_len);
17027 obstack_grow (&contents, &val, sizeof (val));
17028 total_len += obstack_object_size (&addr_obstack);
17029
17030 /* The offset of the symbol table from the start of the file. */
17031 val = MAYBE_SWAP (total_len);
17032 obstack_grow (&contents, &val, sizeof (val));
17033 total_len += obstack_object_size (&symtab_obstack);
17034
17035 /* The offset of the constant pool from the start of the file. */
17036 val = MAYBE_SWAP (total_len);
17037 obstack_grow (&contents, &val, sizeof (val));
17038 total_len += obstack_object_size (&constant_pool);
17039
17040 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17041
17042 write_obstack (out_file, &contents);
17043 write_obstack (out_file, &cu_list);
17044 write_obstack (out_file, &types_cu_list);
17045 write_obstack (out_file, &addr_obstack);
17046 write_obstack (out_file, &symtab_obstack);
17047 write_obstack (out_file, &constant_pool);
17048
17049 fclose (out_file);
17050
17051 /* We want to keep the file, so we set cleanup_filename to NULL
17052 here. See unlink_if_set. */
17053 cleanup_filename = NULL;
17054
17055 do_cleanups (cleanup);
17056 }
17057
17058 /* Implementation of the `save gdb-index' command.
17059
17060 Note that the file format used by this command is documented in the
17061 GDB manual. Any changes here must be documented there. */
17062
17063 static void
17064 save_gdb_index_command (char *arg, int from_tty)
17065 {
17066 struct objfile *objfile;
17067
17068 if (!arg || !*arg)
17069 error (_("usage: save gdb-index DIRECTORY"));
17070
17071 ALL_OBJFILES (objfile)
17072 {
17073 struct stat st;
17074
17075 /* If the objfile does not correspond to an actual file, skip it. */
17076 if (stat (objfile->name, &st) < 0)
17077 continue;
17078
17079 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17080 if (dwarf2_per_objfile)
17081 {
17082 volatile struct gdb_exception except;
17083
17084 TRY_CATCH (except, RETURN_MASK_ERROR)
17085 {
17086 write_psymtabs_to_index (objfile, arg);
17087 }
17088 if (except.reason < 0)
17089 exception_fprintf (gdb_stderr, except,
17090 _("Error while writing index for `%s': "),
17091 objfile->name);
17092 }
17093 }
17094 }
17095
17096 \f
17097
17098 int dwarf2_always_disassemble;
17099
17100 static void
17101 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17102 struct cmd_list_element *c, const char *value)
17103 {
17104 fprintf_filtered (file,
17105 _("Whether to always disassemble "
17106 "DWARF expressions is %s.\n"),
17107 value);
17108 }
17109
17110 static void
17111 show_check_physname (struct ui_file *file, int from_tty,
17112 struct cmd_list_element *c, const char *value)
17113 {
17114 fprintf_filtered (file,
17115 _("Whether to check \"physname\" is %s.\n"),
17116 value);
17117 }
17118
17119 void _initialize_dwarf2_read (void);
17120
17121 void
17122 _initialize_dwarf2_read (void)
17123 {
17124 struct cmd_list_element *c;
17125
17126 dwarf2_objfile_data_key
17127 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17128
17129 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17130 Set DWARF 2 specific variables.\n\
17131 Configure DWARF 2 variables such as the cache size"),
17132 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17133 0/*allow-unknown*/, &maintenance_set_cmdlist);
17134
17135 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17136 Show DWARF 2 specific variables\n\
17137 Show DWARF 2 variables such as the cache size"),
17138 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17139 0/*allow-unknown*/, &maintenance_show_cmdlist);
17140
17141 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17142 &dwarf2_max_cache_age, _("\
17143 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17144 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17145 A higher limit means that cached compilation units will be stored\n\
17146 in memory longer, and more total memory will be used. Zero disables\n\
17147 caching, which can slow down startup."),
17148 NULL,
17149 show_dwarf2_max_cache_age,
17150 &set_dwarf2_cmdlist,
17151 &show_dwarf2_cmdlist);
17152
17153 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17154 &dwarf2_always_disassemble, _("\
17155 Set whether `info address' always disassembles DWARF expressions."), _("\
17156 Show whether `info address' always disassembles DWARF expressions."), _("\
17157 When enabled, DWARF expressions are always printed in an assembly-like\n\
17158 syntax. When disabled, expressions will be printed in a more\n\
17159 conversational style, when possible."),
17160 NULL,
17161 show_dwarf2_always_disassemble,
17162 &set_dwarf2_cmdlist,
17163 &show_dwarf2_cmdlist);
17164
17165 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17166 Set debugging of the dwarf2 DIE reader."), _("\
17167 Show debugging of the dwarf2 DIE reader."), _("\
17168 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17169 The value is the maximum depth to print."),
17170 NULL,
17171 NULL,
17172 &setdebuglist, &showdebuglist);
17173
17174 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17175 Set cross-checking of \"physname\" code against demangler."), _("\
17176 Show cross-checking of \"physname\" code against demangler."), _("\
17177 When enabled, GDB's internal \"physname\" code is checked against\n\
17178 the demangler."),
17179 NULL, show_check_physname,
17180 &setdebuglist, &showdebuglist);
17181
17182 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17183 _("\
17184 Save a gdb-index file.\n\
17185 Usage: save gdb-index DIRECTORY"),
17186 &save_cmdlist);
17187 set_cmd_completer (c, filename_completer);
17188 }
This page took 0.4739 seconds and 5 git commands to generate.