505f5768fd76690ca55a7441a6883cb14cfe3e7f
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 support.
13
14 This file is part of GDB.
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h" /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60 #include <ctype.h>
61
62 #include <fcntl.h>
63 #include "gdb_string.h"
64 #include "gdb_assert.h"
65 #include <sys/types.h>
66 #ifdef HAVE_ZLIB_H
67 #include <zlib.h>
68 #endif
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #ifndef MAP_FAILED
72 #define MAP_FAILED ((void *) -1)
73 #endif
74 #endif
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 #if 0
80 /* .debug_info header for a compilation unit
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83 typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92 _COMP_UNIT_HEADER;
93 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
94 #endif
95
96 /* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99 typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117 _STATEMENT_PROLOGUE;
118
119 /* When non-zero, dump DIEs after they are read in. */
120 static int dwarf2_die_debug = 0;
121
122 /* When non-zero, cross-check physname against demangler. */
123 static int check_physname = 0;
124
125 static int pagesize;
126
127 /* When set, the file that we're processing is known to have debugging
128 info for C++ namespaces. GCC 3.3.x did not produce this information,
129 but later versions do. */
130
131 static int processing_has_namespace_info;
132
133 static const struct objfile_data *dwarf2_objfile_data_key;
134
135 struct dwarf2_section_info
136 {
137 asection *asection;
138 gdb_byte *buffer;
139 bfd_size_type size;
140 /* Not NULL if the section was actually mmapped. */
141 void *map_addr;
142 /* Page aligned size of mmapped area. */
143 bfd_size_type map_len;
144 /* True if we have tried to read this section. */
145 int readin;
146 };
147
148 typedef struct dwarf2_section_info dwarf2_section_info_def;
149 DEF_VEC_O (dwarf2_section_info_def);
150
151 /* All offsets in the index are of this type. It must be
152 architecture-independent. */
153 typedef uint32_t offset_type;
154
155 DEF_VEC_I (offset_type);
156
157 /* A description of the mapped index. The file format is described in
158 a comment by the code that writes the index. */
159 struct mapped_index
160 {
161 /* Index data format version. */
162 int version;
163
164 /* The total length of the buffer. */
165 off_t total_size;
166
167 /* A pointer to the address table data. */
168 const gdb_byte *address_table;
169
170 /* Size of the address table data in bytes. */
171 offset_type address_table_size;
172
173 /* The symbol table, implemented as a hash table. */
174 const offset_type *symbol_table;
175
176 /* Size in slots, each slot is 2 offset_types. */
177 offset_type symbol_table_slots;
178
179 /* A pointer to the constant pool. */
180 const char *constant_pool;
181 };
182
183 struct dwarf2_per_objfile
184 {
185 struct dwarf2_section_info info;
186 struct dwarf2_section_info abbrev;
187 struct dwarf2_section_info line;
188 struct dwarf2_section_info loc;
189 struct dwarf2_section_info macinfo;
190 struct dwarf2_section_info macro;
191 struct dwarf2_section_info str;
192 struct dwarf2_section_info ranges;
193 struct dwarf2_section_info frame;
194 struct dwarf2_section_info eh_frame;
195 struct dwarf2_section_info gdb_index;
196
197 VEC (dwarf2_section_info_def) *types;
198
199 /* Back link. */
200 struct objfile *objfile;
201
202 /* A list of all the compilation units. This is used to locate
203 the target compilation unit of a particular reference. */
204 struct dwarf2_per_cu_data **all_comp_units;
205
206 /* The number of compilation units in ALL_COMP_UNITS. */
207 int n_comp_units;
208
209 /* The number of .debug_types-related CUs. */
210 int n_type_comp_units;
211
212 /* The .debug_types-related CUs. */
213 struct dwarf2_per_cu_data **type_comp_units;
214
215 /* A chain of compilation units that are currently read in, so that
216 they can be freed later. */
217 struct dwarf2_per_cu_data *read_in_chain;
218
219 /* A table mapping .debug_types signatures to its signatured_type entry.
220 This is NULL if the .debug_types section hasn't been read in yet. */
221 htab_t signatured_types;
222
223 /* A flag indicating wether this objfile has a section loaded at a
224 VMA of 0. */
225 int has_section_at_zero;
226
227 /* True if we are using the mapped index,
228 or we are faking it for OBJF_READNOW's sake. */
229 unsigned char using_index;
230
231 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
232 struct mapped_index *index_table;
233
234 /* When using index_table, this keeps track of all quick_file_names entries.
235 TUs can share line table entries with CUs or other TUs, and there can be
236 a lot more TUs than unique line tables, so we maintain a separate table
237 of all line table entries to support the sharing. */
238 htab_t quick_file_names_table;
239
240 /* Set during partial symbol reading, to prevent queueing of full
241 symbols. */
242 int reading_partial_symbols;
243
244 /* Table mapping type .debug_info DIE offsets to types.
245 This is NULL if not allocated yet.
246 It (currently) makes sense to allocate debug_types_type_hash lazily.
247 To keep things simple we allocate both lazily. */
248 htab_t debug_info_type_hash;
249
250 /* Table mapping type .debug_types DIE offsets to types.
251 This is NULL if not allocated yet. */
252 htab_t debug_types_type_hash;
253 };
254
255 static struct dwarf2_per_objfile *dwarf2_per_objfile;
256
257 /* Default names of the debugging sections. */
258
259 /* Note that if the debugging section has been compressed, it might
260 have a name like .zdebug_info. */
261
262 static const struct dwarf2_debug_sections dwarf2_elf_names = {
263 { ".debug_info", ".zdebug_info" },
264 { ".debug_abbrev", ".zdebug_abbrev" },
265 { ".debug_line", ".zdebug_line" },
266 { ".debug_loc", ".zdebug_loc" },
267 { ".debug_macinfo", ".zdebug_macinfo" },
268 { ".debug_macro", ".zdebug_macro" },
269 { ".debug_str", ".zdebug_str" },
270 { ".debug_ranges", ".zdebug_ranges" },
271 { ".debug_types", ".zdebug_types" },
272 { ".debug_frame", ".zdebug_frame" },
273 { ".eh_frame", NULL },
274 { ".gdb_index", ".zgdb_index" }
275 };
276
277 /* local data types */
278
279 /* We hold several abbreviation tables in memory at the same time. */
280 #ifndef ABBREV_HASH_SIZE
281 #define ABBREV_HASH_SIZE 121
282 #endif
283
284 /* The data in a compilation unit header, after target2host
285 translation, looks like this. */
286 struct comp_unit_head
287 {
288 unsigned int length;
289 short version;
290 unsigned char addr_size;
291 unsigned char signed_addr_p;
292 unsigned int abbrev_offset;
293
294 /* Size of file offsets; either 4 or 8. */
295 unsigned int offset_size;
296
297 /* Size of the length field; either 4 or 12. */
298 unsigned int initial_length_size;
299
300 /* Offset to the first byte of this compilation unit header in the
301 .debug_info section, for resolving relative reference dies. */
302 unsigned int offset;
303
304 /* Offset to first die in this cu from the start of the cu.
305 This will be the first byte following the compilation unit header. */
306 unsigned int first_die_offset;
307 };
308
309 /* Type used for delaying computation of method physnames.
310 See comments for compute_delayed_physnames. */
311 struct delayed_method_info
312 {
313 /* The type to which the method is attached, i.e., its parent class. */
314 struct type *type;
315
316 /* The index of the method in the type's function fieldlists. */
317 int fnfield_index;
318
319 /* The index of the method in the fieldlist. */
320 int index;
321
322 /* The name of the DIE. */
323 const char *name;
324
325 /* The DIE associated with this method. */
326 struct die_info *die;
327 };
328
329 typedef struct delayed_method_info delayed_method_info;
330 DEF_VEC_O (delayed_method_info);
331
332 /* Internal state when decoding a particular compilation unit. */
333 struct dwarf2_cu
334 {
335 /* The objfile containing this compilation unit. */
336 struct objfile *objfile;
337
338 /* The header of the compilation unit. */
339 struct comp_unit_head header;
340
341 /* Base address of this compilation unit. */
342 CORE_ADDR base_address;
343
344 /* Non-zero if base_address has been set. */
345 int base_known;
346
347 struct function_range *first_fn, *last_fn, *cached_fn;
348
349 /* The language we are debugging. */
350 enum language language;
351 const struct language_defn *language_defn;
352
353 const char *producer;
354
355 /* The generic symbol table building routines have separate lists for
356 file scope symbols and all all other scopes (local scopes). So
357 we need to select the right one to pass to add_symbol_to_list().
358 We do it by keeping a pointer to the correct list in list_in_scope.
359
360 FIXME: The original dwarf code just treated the file scope as the
361 first local scope, and all other local scopes as nested local
362 scopes, and worked fine. Check to see if we really need to
363 distinguish these in buildsym.c. */
364 struct pending **list_in_scope;
365
366 /* DWARF abbreviation table associated with this compilation unit. */
367 struct abbrev_info **dwarf2_abbrevs;
368
369 /* Storage for the abbrev table. */
370 struct obstack abbrev_obstack;
371
372 /* Hash table holding all the loaded partial DIEs. */
373 htab_t partial_dies;
374
375 /* Storage for things with the same lifetime as this read-in compilation
376 unit, including partial DIEs. */
377 struct obstack comp_unit_obstack;
378
379 /* When multiple dwarf2_cu structures are living in memory, this field
380 chains them all together, so that they can be released efficiently.
381 We will probably also want a generation counter so that most-recently-used
382 compilation units are cached... */
383 struct dwarf2_per_cu_data *read_in_chain;
384
385 /* Backchain to our per_cu entry if the tree has been built. */
386 struct dwarf2_per_cu_data *per_cu;
387
388 /* How many compilation units ago was this CU last referenced? */
389 int last_used;
390
391 /* A hash table of die offsets for following references. */
392 htab_t die_hash;
393
394 /* Full DIEs if read in. */
395 struct die_info *dies;
396
397 /* A set of pointers to dwarf2_per_cu_data objects for compilation
398 units referenced by this one. Only set during full symbol processing;
399 partial symbol tables do not have dependencies. */
400 htab_t dependencies;
401
402 /* Header data from the line table, during full symbol processing. */
403 struct line_header *line_header;
404
405 /* A list of methods which need to have physnames computed
406 after all type information has been read. */
407 VEC (delayed_method_info) *method_list;
408
409 /* Mark used when releasing cached dies. */
410 unsigned int mark : 1;
411
412 /* This flag will be set if this compilation unit might include
413 inter-compilation-unit references. */
414 unsigned int has_form_ref_addr : 1;
415
416 /* This flag will be set if this compilation unit includes any
417 DW_TAG_namespace DIEs. If we know that there are explicit
418 DIEs for namespaces, we don't need to try to infer them
419 from mangled names. */
420 unsigned int has_namespace_info : 1;
421
422 /* This CU references .debug_loc. See the symtab->locations_valid field.
423 This test is imperfect as there may exist optimized debug code not using
424 any location list and still facing inlining issues if handled as
425 unoptimized code. For a future better test see GCC PR other/32998. */
426
427 unsigned int has_loclist : 1;
428 };
429
430 /* Persistent data held for a compilation unit, even when not
431 processing it. We put a pointer to this structure in the
432 read_symtab_private field of the psymtab. If we encounter
433 inter-compilation-unit references, we also maintain a sorted
434 list of all compilation units. */
435
436 struct dwarf2_per_cu_data
437 {
438 /* The start offset and length of this compilation unit. 2**29-1
439 bytes should suffice to store the length of any compilation unit
440 - if it doesn't, GDB will fall over anyway.
441 NOTE: Unlike comp_unit_head.length, this length includes
442 initial_length_size. */
443 unsigned int offset;
444 unsigned int length : 29;
445
446 /* Flag indicating this compilation unit will be read in before
447 any of the current compilation units are processed. */
448 unsigned int queued : 1;
449
450 /* This flag will be set if we need to load absolutely all DIEs
451 for this compilation unit, instead of just the ones we think
452 are interesting. It gets set if we look for a DIE in the
453 hash table and don't find it. */
454 unsigned int load_all_dies : 1;
455
456 /* Non-null if this CU is from .debug_types; in which case it points
457 to the section. Otherwise it's from .debug_info. */
458 struct dwarf2_section_info *debug_type_section;
459
460 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
461 of the CU cache it gets reset to NULL again. */
462 struct dwarf2_cu *cu;
463
464 /* The corresponding objfile. */
465 struct objfile *objfile;
466
467 /* When using partial symbol tables, the 'psymtab' field is active.
468 Otherwise the 'quick' field is active. */
469 union
470 {
471 /* The partial symbol table associated with this compilation unit,
472 or NULL for partial units (which do not have an associated
473 symtab). */
474 struct partial_symtab *psymtab;
475
476 /* Data needed by the "quick" functions. */
477 struct dwarf2_per_cu_quick_data *quick;
478 } v;
479 };
480
481 /* Entry in the signatured_types hash table. */
482
483 struct signatured_type
484 {
485 ULONGEST signature;
486
487 /* Offset in .debug_types of the type defined by this TU. */
488 unsigned int type_offset;
489
490 /* The CU(/TU) of this type. */
491 struct dwarf2_per_cu_data per_cu;
492 };
493
494 /* Struct used to pass misc. parameters to read_die_and_children, et
495 al. which are used for both .debug_info and .debug_types dies.
496 All parameters here are unchanging for the life of the call. This
497 struct exists to abstract away the constant parameters of die
498 reading. */
499
500 struct die_reader_specs
501 {
502 /* The bfd of this objfile. */
503 bfd* abfd;
504
505 /* The CU of the DIE we are parsing. */
506 struct dwarf2_cu *cu;
507
508 /* Pointer to start of section buffer.
509 This is either the start of .debug_info or .debug_types. */
510 const gdb_byte *buffer;
511 };
512
513 /* The line number information for a compilation unit (found in the
514 .debug_line section) begins with a "statement program header",
515 which contains the following information. */
516 struct line_header
517 {
518 unsigned int total_length;
519 unsigned short version;
520 unsigned int header_length;
521 unsigned char minimum_instruction_length;
522 unsigned char maximum_ops_per_instruction;
523 unsigned char default_is_stmt;
524 int line_base;
525 unsigned char line_range;
526 unsigned char opcode_base;
527
528 /* standard_opcode_lengths[i] is the number of operands for the
529 standard opcode whose value is i. This means that
530 standard_opcode_lengths[0] is unused, and the last meaningful
531 element is standard_opcode_lengths[opcode_base - 1]. */
532 unsigned char *standard_opcode_lengths;
533
534 /* The include_directories table. NOTE! These strings are not
535 allocated with xmalloc; instead, they are pointers into
536 debug_line_buffer. If you try to free them, `free' will get
537 indigestion. */
538 unsigned int num_include_dirs, include_dirs_size;
539 char **include_dirs;
540
541 /* The file_names table. NOTE! These strings are not allocated
542 with xmalloc; instead, they are pointers into debug_line_buffer.
543 Don't try to free them directly. */
544 unsigned int num_file_names, file_names_size;
545 struct file_entry
546 {
547 char *name;
548 unsigned int dir_index;
549 unsigned int mod_time;
550 unsigned int length;
551 int included_p; /* Non-zero if referenced by the Line Number Program. */
552 struct symtab *symtab; /* The associated symbol table, if any. */
553 } *file_names;
554
555 /* The start and end of the statement program following this
556 header. These point into dwarf2_per_objfile->line_buffer. */
557 gdb_byte *statement_program_start, *statement_program_end;
558 };
559
560 /* When we construct a partial symbol table entry we only
561 need this much information. */
562 struct partial_die_info
563 {
564 /* Offset of this DIE. */
565 unsigned int offset;
566
567 /* DWARF-2 tag for this DIE. */
568 ENUM_BITFIELD(dwarf_tag) tag : 16;
569
570 /* Assorted flags describing the data found in this DIE. */
571 unsigned int has_children : 1;
572 unsigned int is_external : 1;
573 unsigned int is_declaration : 1;
574 unsigned int has_type : 1;
575 unsigned int has_specification : 1;
576 unsigned int has_pc_info : 1;
577
578 /* Flag set if the SCOPE field of this structure has been
579 computed. */
580 unsigned int scope_set : 1;
581
582 /* Flag set if the DIE has a byte_size attribute. */
583 unsigned int has_byte_size : 1;
584
585 /* Flag set if any of the DIE's children are template arguments. */
586 unsigned int has_template_arguments : 1;
587
588 /* Flag set if fixup_partial_die has been called on this die. */
589 unsigned int fixup_called : 1;
590
591 /* The name of this DIE. Normally the value of DW_AT_name, but
592 sometimes a default name for unnamed DIEs. */
593 char *name;
594
595 /* The linkage name, if present. */
596 const char *linkage_name;
597
598 /* The scope to prepend to our children. This is generally
599 allocated on the comp_unit_obstack, so will disappear
600 when this compilation unit leaves the cache. */
601 char *scope;
602
603 /* The location description associated with this DIE, if any. */
604 struct dwarf_block *locdesc;
605
606 /* If HAS_PC_INFO, the PC range associated with this DIE. */
607 CORE_ADDR lowpc;
608 CORE_ADDR highpc;
609
610 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
611 DW_AT_sibling, if any. */
612 /* NOTE: This member isn't strictly necessary, read_partial_die could
613 return DW_AT_sibling values to its caller load_partial_dies. */
614 gdb_byte *sibling;
615
616 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
617 DW_AT_specification (or DW_AT_abstract_origin or
618 DW_AT_extension). */
619 unsigned int spec_offset;
620
621 /* Pointers to this DIE's parent, first child, and next sibling,
622 if any. */
623 struct partial_die_info *die_parent, *die_child, *die_sibling;
624 };
625
626 /* This data structure holds the information of an abbrev. */
627 struct abbrev_info
628 {
629 unsigned int number; /* number identifying abbrev */
630 enum dwarf_tag tag; /* dwarf tag */
631 unsigned short has_children; /* boolean */
632 unsigned short num_attrs; /* number of attributes */
633 struct attr_abbrev *attrs; /* an array of attribute descriptions */
634 struct abbrev_info *next; /* next in chain */
635 };
636
637 struct attr_abbrev
638 {
639 ENUM_BITFIELD(dwarf_attribute) name : 16;
640 ENUM_BITFIELD(dwarf_form) form : 16;
641 };
642
643 /* Attributes have a name and a value. */
644 struct attribute
645 {
646 ENUM_BITFIELD(dwarf_attribute) name : 16;
647 ENUM_BITFIELD(dwarf_form) form : 15;
648
649 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
650 field should be in u.str (existing only for DW_STRING) but it is kept
651 here for better struct attribute alignment. */
652 unsigned int string_is_canonical : 1;
653
654 union
655 {
656 char *str;
657 struct dwarf_block *blk;
658 ULONGEST unsnd;
659 LONGEST snd;
660 CORE_ADDR addr;
661 struct signatured_type *signatured_type;
662 }
663 u;
664 };
665
666 /* This data structure holds a complete die structure. */
667 struct die_info
668 {
669 /* DWARF-2 tag for this DIE. */
670 ENUM_BITFIELD(dwarf_tag) tag : 16;
671
672 /* Number of attributes */
673 unsigned char num_attrs;
674
675 /* True if we're presently building the full type name for the
676 type derived from this DIE. */
677 unsigned char building_fullname : 1;
678
679 /* Abbrev number */
680 unsigned int abbrev;
681
682 /* Offset in .debug_info or .debug_types section. */
683 unsigned int offset;
684
685 /* The dies in a compilation unit form an n-ary tree. PARENT
686 points to this die's parent; CHILD points to the first child of
687 this node; and all the children of a given node are chained
688 together via their SIBLING fields. */
689 struct die_info *child; /* Its first child, if any. */
690 struct die_info *sibling; /* Its next sibling, if any. */
691 struct die_info *parent; /* Its parent, if any. */
692
693 /* An array of attributes, with NUM_ATTRS elements. There may be
694 zero, but it's not common and zero-sized arrays are not
695 sufficiently portable C. */
696 struct attribute attrs[1];
697 };
698
699 struct function_range
700 {
701 const char *name;
702 CORE_ADDR lowpc, highpc;
703 int seen_line;
704 struct function_range *next;
705 };
706
707 /* Get at parts of an attribute structure. */
708
709 #define DW_STRING(attr) ((attr)->u.str)
710 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
711 #define DW_UNSND(attr) ((attr)->u.unsnd)
712 #define DW_BLOCK(attr) ((attr)->u.blk)
713 #define DW_SND(attr) ((attr)->u.snd)
714 #define DW_ADDR(attr) ((attr)->u.addr)
715 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
716
717 /* Blocks are a bunch of untyped bytes. */
718 struct dwarf_block
719 {
720 unsigned int size;
721 gdb_byte *data;
722 };
723
724 #ifndef ATTR_ALLOC_CHUNK
725 #define ATTR_ALLOC_CHUNK 4
726 #endif
727
728 /* Allocate fields for structs, unions and enums in this size. */
729 #ifndef DW_FIELD_ALLOC_CHUNK
730 #define DW_FIELD_ALLOC_CHUNK 4
731 #endif
732
733 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
734 but this would require a corresponding change in unpack_field_as_long
735 and friends. */
736 static int bits_per_byte = 8;
737
738 /* The routines that read and process dies for a C struct or C++ class
739 pass lists of data member fields and lists of member function fields
740 in an instance of a field_info structure, as defined below. */
741 struct field_info
742 {
743 /* List of data member and baseclasses fields. */
744 struct nextfield
745 {
746 struct nextfield *next;
747 int accessibility;
748 int virtuality;
749 struct field field;
750 }
751 *fields, *baseclasses;
752
753 /* Number of fields (including baseclasses). */
754 int nfields;
755
756 /* Number of baseclasses. */
757 int nbaseclasses;
758
759 /* Set if the accesibility of one of the fields is not public. */
760 int non_public_fields;
761
762 /* Member function fields array, entries are allocated in the order they
763 are encountered in the object file. */
764 struct nextfnfield
765 {
766 struct nextfnfield *next;
767 struct fn_field fnfield;
768 }
769 *fnfields;
770
771 /* Member function fieldlist array, contains name of possibly overloaded
772 member function, number of overloaded member functions and a pointer
773 to the head of the member function field chain. */
774 struct fnfieldlist
775 {
776 char *name;
777 int length;
778 struct nextfnfield *head;
779 }
780 *fnfieldlists;
781
782 /* Number of entries in the fnfieldlists array. */
783 int nfnfields;
784
785 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
786 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
787 struct typedef_field_list
788 {
789 struct typedef_field field;
790 struct typedef_field_list *next;
791 }
792 *typedef_field_list;
793 unsigned typedef_field_list_count;
794 };
795
796 /* One item on the queue of compilation units to read in full symbols
797 for. */
798 struct dwarf2_queue_item
799 {
800 struct dwarf2_per_cu_data *per_cu;
801 struct dwarf2_queue_item *next;
802 };
803
804 /* The current queue. */
805 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
806
807 /* Loaded secondary compilation units are kept in memory until they
808 have not been referenced for the processing of this many
809 compilation units. Set this to zero to disable caching. Cache
810 sizes of up to at least twenty will improve startup time for
811 typical inter-CU-reference binaries, at an obvious memory cost. */
812 static int dwarf2_max_cache_age = 5;
813 static void
814 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
815 struct cmd_list_element *c, const char *value)
816 {
817 fprintf_filtered (file, _("The upper bound on the age of cached "
818 "dwarf2 compilation units is %s.\n"),
819 value);
820 }
821
822
823 /* Various complaints about symbol reading that don't abort the process. */
824
825 static void
826 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
827 {
828 complaint (&symfile_complaints,
829 _("statement list doesn't fit in .debug_line section"));
830 }
831
832 static void
833 dwarf2_debug_line_missing_file_complaint (void)
834 {
835 complaint (&symfile_complaints,
836 _(".debug_line section has line data without a file"));
837 }
838
839 static void
840 dwarf2_debug_line_missing_end_sequence_complaint (void)
841 {
842 complaint (&symfile_complaints,
843 _(".debug_line section has line "
844 "program sequence without an end"));
845 }
846
847 static void
848 dwarf2_complex_location_expr_complaint (void)
849 {
850 complaint (&symfile_complaints, _("location expression too complex"));
851 }
852
853 static void
854 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
855 int arg3)
856 {
857 complaint (&symfile_complaints,
858 _("const value length mismatch for '%s', got %d, expected %d"),
859 arg1, arg2, arg3);
860 }
861
862 static void
863 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
864 {
865 complaint (&symfile_complaints,
866 _("macro info runs off end of `%s' section"),
867 section->asection->name);
868 }
869
870 static void
871 dwarf2_macro_malformed_definition_complaint (const char *arg1)
872 {
873 complaint (&symfile_complaints,
874 _("macro debug info contains a "
875 "malformed macro definition:\n`%s'"),
876 arg1);
877 }
878
879 static void
880 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
881 {
882 complaint (&symfile_complaints,
883 _("invalid attribute class or form for '%s' in '%s'"),
884 arg1, arg2);
885 }
886
887 /* local function prototypes */
888
889 static void dwarf2_locate_sections (bfd *, asection *, void *);
890
891 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
892 struct objfile *);
893
894 static void dwarf2_find_base_address (struct die_info *die,
895 struct dwarf2_cu *cu);
896
897 static void dwarf2_build_psymtabs_hard (struct objfile *);
898
899 static void scan_partial_symbols (struct partial_die_info *,
900 CORE_ADDR *, CORE_ADDR *,
901 int, struct dwarf2_cu *);
902
903 static void add_partial_symbol (struct partial_die_info *,
904 struct dwarf2_cu *);
905
906 static void add_partial_namespace (struct partial_die_info *pdi,
907 CORE_ADDR *lowpc, CORE_ADDR *highpc,
908 int need_pc, struct dwarf2_cu *cu);
909
910 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
911 CORE_ADDR *highpc, int need_pc,
912 struct dwarf2_cu *cu);
913
914 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
915 struct dwarf2_cu *cu);
916
917 static void add_partial_subprogram (struct partial_die_info *pdi,
918 CORE_ADDR *lowpc, CORE_ADDR *highpc,
919 int need_pc, struct dwarf2_cu *cu);
920
921 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
922 gdb_byte *buffer, gdb_byte *info_ptr,
923 bfd *abfd, struct dwarf2_cu *cu);
924
925 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
926
927 static void psymtab_to_symtab_1 (struct partial_symtab *);
928
929 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
930
931 static void dwarf2_free_abbrev_table (void *);
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 int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1082 struct dwarf2_cu *, struct partial_symtab *);
1083
1084 static int dwarf2_get_pc_bounds (struct die_info *,
1085 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1086 struct partial_symtab *);
1087
1088 static void get_scope_pc_bounds (struct die_info *,
1089 CORE_ADDR *, CORE_ADDR *,
1090 struct dwarf2_cu *);
1091
1092 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1093 CORE_ADDR, struct dwarf2_cu *);
1094
1095 static void dwarf2_add_field (struct field_info *, struct die_info *,
1096 struct dwarf2_cu *);
1097
1098 static void dwarf2_attach_fields_to_type (struct field_info *,
1099 struct type *, struct dwarf2_cu *);
1100
1101 static void dwarf2_add_member_fn (struct field_info *,
1102 struct die_info *, struct type *,
1103 struct dwarf2_cu *);
1104
1105 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1106 struct type *,
1107 struct dwarf2_cu *);
1108
1109 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1110
1111 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1112
1113 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1114
1115 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1116
1117 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1118
1119 static struct type *read_module_type (struct die_info *die,
1120 struct dwarf2_cu *cu);
1121
1122 static const char *namespace_name (struct die_info *die,
1123 int *is_anonymous, struct dwarf2_cu *);
1124
1125 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1126
1127 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1128
1129 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1130 struct dwarf2_cu *);
1131
1132 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1133
1134 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1135 gdb_byte *info_ptr,
1136 gdb_byte **new_info_ptr,
1137 struct die_info *parent);
1138
1139 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1140 gdb_byte *info_ptr,
1141 gdb_byte **new_info_ptr,
1142 struct die_info *parent);
1143
1144 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1145 gdb_byte *info_ptr,
1146 gdb_byte **new_info_ptr,
1147 struct die_info *parent);
1148
1149 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1150 struct die_info **, gdb_byte *,
1151 int *);
1152
1153 static void process_die (struct die_info *, struct dwarf2_cu *);
1154
1155 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1156 struct obstack *);
1157
1158 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1159
1160 static const char *dwarf2_full_name (char *name,
1161 struct die_info *die,
1162 struct dwarf2_cu *cu);
1163
1164 static struct die_info *dwarf2_extension (struct die_info *die,
1165 struct dwarf2_cu **);
1166
1167 static char *dwarf_tag_name (unsigned int);
1168
1169 static char *dwarf_attr_name (unsigned int);
1170
1171 static char *dwarf_form_name (unsigned int);
1172
1173 static char *dwarf_bool_name (unsigned int);
1174
1175 static char *dwarf_type_encoding_name (unsigned int);
1176
1177 #if 0
1178 static char *dwarf_cfi_name (unsigned int);
1179 #endif
1180
1181 static struct die_info *sibling_die (struct die_info *);
1182
1183 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1184
1185 static void dump_die_for_error (struct die_info *);
1186
1187 static void dump_die_1 (struct ui_file *, int level, int max_level,
1188 struct die_info *);
1189
1190 /*static*/ void dump_die (struct die_info *, int max_level);
1191
1192 static void store_in_ref_table (struct die_info *,
1193 struct dwarf2_cu *);
1194
1195 static int is_ref_attr (struct attribute *);
1196
1197 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1198
1199 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1200
1201 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1202 struct attribute *,
1203 struct dwarf2_cu **);
1204
1205 static struct die_info *follow_die_ref (struct die_info *,
1206 struct attribute *,
1207 struct dwarf2_cu **);
1208
1209 static struct die_info *follow_die_sig (struct die_info *,
1210 struct attribute *,
1211 struct dwarf2_cu **);
1212
1213 static struct signatured_type *lookup_signatured_type_at_offset
1214 (struct objfile *objfile,
1215 struct dwarf2_section_info *section,
1216 unsigned int offset);
1217
1218 static void read_signatured_type_at_offset (struct objfile *objfile,
1219 struct dwarf2_section_info *sect,
1220 unsigned int offset);
1221
1222 static void read_signatured_type (struct objfile *,
1223 struct signatured_type *type_sig);
1224
1225 /* memory allocation interface */
1226
1227 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1228
1229 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1230
1231 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1232
1233 static void initialize_cu_func_list (struct dwarf2_cu *);
1234
1235 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1236 struct dwarf2_cu *);
1237
1238 static void dwarf_decode_macros (struct line_header *, unsigned int,
1239 char *, bfd *, struct dwarf2_cu *,
1240 struct dwarf2_section_info *,
1241 int);
1242
1243 static int attr_form_is_block (struct attribute *);
1244
1245 static int attr_form_is_section_offset (struct attribute *);
1246
1247 static int attr_form_is_constant (struct attribute *);
1248
1249 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1250 struct dwarf2_loclist_baton *baton,
1251 struct attribute *attr);
1252
1253 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1254 struct symbol *sym,
1255 struct dwarf2_cu *cu);
1256
1257 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1258 struct abbrev_info *abbrev,
1259 struct dwarf2_cu *cu);
1260
1261 static void free_stack_comp_unit (void *);
1262
1263 static hashval_t partial_die_hash (const void *item);
1264
1265 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1266
1267 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1268 (unsigned int offset, struct objfile *objfile);
1269
1270 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1271 (unsigned int offset, struct objfile *objfile);
1272
1273 static void init_one_comp_unit (struct dwarf2_cu *cu,
1274 struct objfile *objfile);
1275
1276 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1277 struct die_info *comp_unit_die);
1278
1279 static void free_one_comp_unit (void *);
1280
1281 static void free_cached_comp_units (void *);
1282
1283 static void age_cached_comp_units (void);
1284
1285 static void free_one_cached_comp_unit (void *);
1286
1287 static struct type *set_die_type (struct die_info *, struct type *,
1288 struct dwarf2_cu *);
1289
1290 static void create_all_comp_units (struct objfile *);
1291
1292 static int create_debug_types_hash_table (struct objfile *objfile);
1293
1294 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1295 struct objfile *);
1296
1297 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1298
1299 static void dwarf2_add_dependence (struct dwarf2_cu *,
1300 struct dwarf2_per_cu_data *);
1301
1302 static void dwarf2_mark (struct dwarf2_cu *);
1303
1304 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1305
1306 static struct type *get_die_type_at_offset (unsigned int,
1307 struct dwarf2_per_cu_data *per_cu);
1308
1309 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1310
1311 static void dwarf2_release_queue (void *dummy);
1312
1313 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1314 struct objfile *objfile);
1315
1316 static void process_queue (struct objfile *objfile);
1317
1318 static void find_file_and_directory (struct die_info *die,
1319 struct dwarf2_cu *cu,
1320 char **name, char **comp_dir);
1321
1322 static char *file_full_name (int file, struct line_header *lh,
1323 const char *comp_dir);
1324
1325 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1326 gdb_byte *info_ptr,
1327 gdb_byte *buffer,
1328 unsigned int buffer_size,
1329 bfd *abfd);
1330
1331 static void init_cu_die_reader (struct die_reader_specs *reader,
1332 struct dwarf2_cu *cu);
1333
1334 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1335
1336 #if WORDS_BIGENDIAN
1337
1338 /* Convert VALUE between big- and little-endian. */
1339 static offset_type
1340 byte_swap (offset_type value)
1341 {
1342 offset_type result;
1343
1344 result = (value & 0xff) << 24;
1345 result |= (value & 0xff00) << 8;
1346 result |= (value & 0xff0000) >> 8;
1347 result |= (value & 0xff000000) >> 24;
1348 return result;
1349 }
1350
1351 #define MAYBE_SWAP(V) byte_swap (V)
1352
1353 #else
1354 #define MAYBE_SWAP(V) (V)
1355 #endif /* WORDS_BIGENDIAN */
1356
1357 /* The suffix for an index file. */
1358 #define INDEX_SUFFIX ".gdb-index"
1359
1360 static const char *dwarf2_physname (char *name, struct die_info *die,
1361 struct dwarf2_cu *cu);
1362
1363 /* Try to locate the sections we need for DWARF 2 debugging
1364 information and return true if we have enough to do something.
1365 NAMES points to the dwarf2 section names, or is NULL if the standard
1366 ELF names are used. */
1367
1368 int
1369 dwarf2_has_info (struct objfile *objfile,
1370 const struct dwarf2_debug_sections *names)
1371 {
1372 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1373 if (!dwarf2_per_objfile)
1374 {
1375 /* Initialize per-objfile state. */
1376 struct dwarf2_per_objfile *data
1377 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1378
1379 memset (data, 0, sizeof (*data));
1380 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1381 dwarf2_per_objfile = data;
1382
1383 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1384 (void *) names);
1385 dwarf2_per_objfile->objfile = objfile;
1386 }
1387 return (dwarf2_per_objfile->info.asection != NULL
1388 && dwarf2_per_objfile->abbrev.asection != NULL);
1389 }
1390
1391 /* When loading sections, we look either for uncompressed section or for
1392 compressed section names. */
1393
1394 static int
1395 section_is_p (const char *section_name,
1396 const struct dwarf2_section_names *names)
1397 {
1398 if (names->normal != NULL
1399 && strcmp (section_name, names->normal) == 0)
1400 return 1;
1401 if (names->compressed != NULL
1402 && strcmp (section_name, names->compressed) == 0)
1403 return 1;
1404 return 0;
1405 }
1406
1407 /* This function is mapped across the sections and remembers the
1408 offset and size of each of the debugging sections we are interested
1409 in. */
1410
1411 static void
1412 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1413 {
1414 const struct dwarf2_debug_sections *names;
1415
1416 if (vnames == NULL)
1417 names = &dwarf2_elf_names;
1418 else
1419 names = (const struct dwarf2_debug_sections *) vnames;
1420
1421 if (section_is_p (sectp->name, &names->info))
1422 {
1423 dwarf2_per_objfile->info.asection = sectp;
1424 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1425 }
1426 else if (section_is_p (sectp->name, &names->abbrev))
1427 {
1428 dwarf2_per_objfile->abbrev.asection = sectp;
1429 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1430 }
1431 else if (section_is_p (sectp->name, &names->line))
1432 {
1433 dwarf2_per_objfile->line.asection = sectp;
1434 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1435 }
1436 else if (section_is_p (sectp->name, &names->loc))
1437 {
1438 dwarf2_per_objfile->loc.asection = sectp;
1439 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1440 }
1441 else if (section_is_p (sectp->name, &names->macinfo))
1442 {
1443 dwarf2_per_objfile->macinfo.asection = sectp;
1444 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1445 }
1446 else if (section_is_p (sectp->name, &names->macro))
1447 {
1448 dwarf2_per_objfile->macro.asection = sectp;
1449 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1450 }
1451 else if (section_is_p (sectp->name, &names->str))
1452 {
1453 dwarf2_per_objfile->str.asection = sectp;
1454 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1455 }
1456 else if (section_is_p (sectp->name, &names->frame))
1457 {
1458 dwarf2_per_objfile->frame.asection = sectp;
1459 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1460 }
1461 else if (section_is_p (sectp->name, &names->eh_frame))
1462 {
1463 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1464
1465 if (aflag & SEC_HAS_CONTENTS)
1466 {
1467 dwarf2_per_objfile->eh_frame.asection = sectp;
1468 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1469 }
1470 }
1471 else if (section_is_p (sectp->name, &names->ranges))
1472 {
1473 dwarf2_per_objfile->ranges.asection = sectp;
1474 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1475 }
1476 else if (section_is_p (sectp->name, &names->types))
1477 {
1478 struct dwarf2_section_info type_section;
1479
1480 memset (&type_section, 0, sizeof (type_section));
1481 type_section.asection = sectp;
1482 type_section.size = bfd_get_section_size (sectp);
1483
1484 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1485 &type_section);
1486 }
1487 else if (section_is_p (sectp->name, &names->gdb_index))
1488 {
1489 dwarf2_per_objfile->gdb_index.asection = sectp;
1490 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1491 }
1492
1493 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1494 && bfd_section_vma (abfd, sectp) == 0)
1495 dwarf2_per_objfile->has_section_at_zero = 1;
1496 }
1497
1498 /* Decompress a section that was compressed using zlib. Store the
1499 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1500
1501 static void
1502 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1503 gdb_byte **outbuf, bfd_size_type *outsize)
1504 {
1505 bfd *abfd = objfile->obfd;
1506 #ifndef HAVE_ZLIB_H
1507 error (_("Support for zlib-compressed DWARF data (from '%s') "
1508 "is disabled in this copy of GDB"),
1509 bfd_get_filename (abfd));
1510 #else
1511 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1512 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1513 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1514 bfd_size_type uncompressed_size;
1515 gdb_byte *uncompressed_buffer;
1516 z_stream strm;
1517 int rc;
1518 int header_size = 12;
1519
1520 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1521 || bfd_bread (compressed_buffer,
1522 compressed_size, abfd) != compressed_size)
1523 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1524 bfd_get_filename (abfd));
1525
1526 /* Read the zlib header. In this case, it should be "ZLIB" followed
1527 by the uncompressed section size, 8 bytes in big-endian order. */
1528 if (compressed_size < header_size
1529 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1530 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1531 bfd_get_filename (abfd));
1532 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1533 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1534 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1535 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1536 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1537 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1538 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1539 uncompressed_size += compressed_buffer[11];
1540
1541 /* It is possible the section consists of several compressed
1542 buffers concatenated together, so we uncompress in a loop. */
1543 strm.zalloc = NULL;
1544 strm.zfree = NULL;
1545 strm.opaque = NULL;
1546 strm.avail_in = compressed_size - header_size;
1547 strm.next_in = (Bytef*) compressed_buffer + header_size;
1548 strm.avail_out = uncompressed_size;
1549 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1550 uncompressed_size);
1551 rc = inflateInit (&strm);
1552 while (strm.avail_in > 0)
1553 {
1554 if (rc != Z_OK)
1555 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1556 bfd_get_filename (abfd), rc);
1557 strm.next_out = ((Bytef*) uncompressed_buffer
1558 + (uncompressed_size - strm.avail_out));
1559 rc = inflate (&strm, Z_FINISH);
1560 if (rc != Z_STREAM_END)
1561 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1562 bfd_get_filename (abfd), rc);
1563 rc = inflateReset (&strm);
1564 }
1565 rc = inflateEnd (&strm);
1566 if (rc != Z_OK
1567 || strm.avail_out != 0)
1568 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1569 bfd_get_filename (abfd), rc);
1570
1571 do_cleanups (cleanup);
1572 *outbuf = uncompressed_buffer;
1573 *outsize = uncompressed_size;
1574 #endif
1575 }
1576
1577 /* A helper function that decides whether a section is empty. */
1578
1579 static int
1580 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1581 {
1582 return info->asection == NULL || info->size == 0;
1583 }
1584
1585 /* Read the contents of the section SECTP from object file specified by
1586 OBJFILE, store info about the section into INFO.
1587 If the section is compressed, uncompress it before returning. */
1588
1589 static void
1590 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1591 {
1592 bfd *abfd = objfile->obfd;
1593 asection *sectp = info->asection;
1594 gdb_byte *buf, *retbuf;
1595 unsigned char header[4];
1596
1597 if (info->readin)
1598 return;
1599 info->buffer = NULL;
1600 info->map_addr = NULL;
1601 info->readin = 1;
1602
1603 if (dwarf2_section_empty_p (info))
1604 return;
1605
1606 /* Check if the file has a 4-byte header indicating compression. */
1607 if (info->size > sizeof (header)
1608 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1609 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1610 {
1611 /* Upon decompression, update the buffer and its size. */
1612 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1613 {
1614 zlib_decompress_section (objfile, sectp, &info->buffer,
1615 &info->size);
1616 return;
1617 }
1618 }
1619
1620 #ifdef HAVE_MMAP
1621 if (pagesize == 0)
1622 pagesize = getpagesize ();
1623
1624 /* Only try to mmap sections which are large enough: we don't want to
1625 waste space due to fragmentation. Also, only try mmap for sections
1626 without relocations. */
1627
1628 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1629 {
1630 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1631 MAP_PRIVATE, sectp->filepos,
1632 &info->map_addr, &info->map_len);
1633
1634 if ((caddr_t)info->buffer != MAP_FAILED)
1635 {
1636 #if HAVE_POSIX_MADVISE
1637 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1638 #endif
1639 return;
1640 }
1641 }
1642 #endif
1643
1644 /* If we get here, we are a normal, not-compressed section. */
1645 info->buffer = buf
1646 = obstack_alloc (&objfile->objfile_obstack, info->size);
1647
1648 /* When debugging .o files, we may need to apply relocations; see
1649 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1650 We never compress sections in .o files, so we only need to
1651 try this when the section is not compressed. */
1652 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1653 if (retbuf != NULL)
1654 {
1655 info->buffer = retbuf;
1656 return;
1657 }
1658
1659 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1660 || bfd_bread (buf, info->size, abfd) != info->size)
1661 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1662 bfd_get_filename (abfd));
1663 }
1664
1665 /* A helper function that returns the size of a section in a safe way.
1666 If you are positive that the section has been read before using the
1667 size, then it is safe to refer to the dwarf2_section_info object's
1668 "size" field directly. In other cases, you must call this
1669 function, because for compressed sections the size field is not set
1670 correctly until the section has been read. */
1671
1672 static bfd_size_type
1673 dwarf2_section_size (struct objfile *objfile,
1674 struct dwarf2_section_info *info)
1675 {
1676 if (!info->readin)
1677 dwarf2_read_section (objfile, info);
1678 return info->size;
1679 }
1680
1681 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1682 SECTION_NAME. */
1683
1684 void
1685 dwarf2_get_section_info (struct objfile *objfile,
1686 enum dwarf2_section_enum sect,
1687 asection **sectp, gdb_byte **bufp,
1688 bfd_size_type *sizep)
1689 {
1690 struct dwarf2_per_objfile *data
1691 = objfile_data (objfile, dwarf2_objfile_data_key);
1692 struct dwarf2_section_info *info;
1693
1694 /* We may see an objfile without any DWARF, in which case we just
1695 return nothing. */
1696 if (data == NULL)
1697 {
1698 *sectp = NULL;
1699 *bufp = NULL;
1700 *sizep = 0;
1701 return;
1702 }
1703 switch (sect)
1704 {
1705 case DWARF2_DEBUG_FRAME:
1706 info = &data->frame;
1707 break;
1708 case DWARF2_EH_FRAME:
1709 info = &data->eh_frame;
1710 break;
1711 default:
1712 gdb_assert_not_reached ("unexpected section");
1713 }
1714
1715 dwarf2_read_section (objfile, info);
1716
1717 *sectp = info->asection;
1718 *bufp = info->buffer;
1719 *sizep = info->size;
1720 }
1721
1722 \f
1723 /* DWARF quick_symbols_functions support. */
1724
1725 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1726 unique line tables, so we maintain a separate table of all .debug_line
1727 derived entries to support the sharing.
1728 All the quick functions need is the list of file names. We discard the
1729 line_header when we're done and don't need to record it here. */
1730 struct quick_file_names
1731 {
1732 /* The offset in .debug_line of the line table. We hash on this. */
1733 unsigned int offset;
1734
1735 /* The number of entries in file_names, real_names. */
1736 unsigned int num_file_names;
1737
1738 /* The file names from the line table, after being run through
1739 file_full_name. */
1740 const char **file_names;
1741
1742 /* The file names from the line table after being run through
1743 gdb_realpath. These are computed lazily. */
1744 const char **real_names;
1745 };
1746
1747 /* When using the index (and thus not using psymtabs), each CU has an
1748 object of this type. This is used to hold information needed by
1749 the various "quick" methods. */
1750 struct dwarf2_per_cu_quick_data
1751 {
1752 /* The file table. This can be NULL if there was no file table
1753 or it's currently not read in.
1754 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1755 struct quick_file_names *file_names;
1756
1757 /* The corresponding symbol table. This is NULL if symbols for this
1758 CU have not yet been read. */
1759 struct symtab *symtab;
1760
1761 /* A temporary mark bit used when iterating over all CUs in
1762 expand_symtabs_matching. */
1763 unsigned int mark : 1;
1764
1765 /* True if we've tried to read the file table and found there isn't one.
1766 There will be no point in trying to read it again next time. */
1767 unsigned int no_file_data : 1;
1768 };
1769
1770 /* Hash function for a quick_file_names. */
1771
1772 static hashval_t
1773 hash_file_name_entry (const void *e)
1774 {
1775 const struct quick_file_names *file_data = e;
1776
1777 return file_data->offset;
1778 }
1779
1780 /* Equality function for a quick_file_names. */
1781
1782 static int
1783 eq_file_name_entry (const void *a, const void *b)
1784 {
1785 const struct quick_file_names *ea = a;
1786 const struct quick_file_names *eb = b;
1787
1788 return ea->offset == eb->offset;
1789 }
1790
1791 /* Delete function for a quick_file_names. */
1792
1793 static void
1794 delete_file_name_entry (void *e)
1795 {
1796 struct quick_file_names *file_data = e;
1797 int i;
1798
1799 for (i = 0; i < file_data->num_file_names; ++i)
1800 {
1801 xfree ((void*) file_data->file_names[i]);
1802 if (file_data->real_names)
1803 xfree ((void*) file_data->real_names[i]);
1804 }
1805
1806 /* The space for the struct itself lives on objfile_obstack,
1807 so we don't free it here. */
1808 }
1809
1810 /* Create a quick_file_names hash table. */
1811
1812 static htab_t
1813 create_quick_file_names_table (unsigned int nr_initial_entries)
1814 {
1815 return htab_create_alloc (nr_initial_entries,
1816 hash_file_name_entry, eq_file_name_entry,
1817 delete_file_name_entry, xcalloc, xfree);
1818 }
1819
1820 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1821 have to be created afterwards. You should call age_cached_comp_units after
1822 processing PER_CU->CU. dw2_setup must have been already called. */
1823
1824 static void
1825 load_cu (struct dwarf2_per_cu_data *per_cu)
1826 {
1827 if (per_cu->debug_type_section)
1828 read_signatured_type_at_offset (per_cu->objfile,
1829 per_cu->debug_type_section,
1830 per_cu->offset);
1831 else
1832 load_full_comp_unit (per_cu, per_cu->objfile);
1833
1834 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1835
1836 gdb_assert (per_cu->cu != NULL);
1837 }
1838
1839 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1840 this CU came. */
1841
1842 static void
1843 dw2_do_instantiate_symtab (struct objfile *objfile,
1844 struct dwarf2_per_cu_data *per_cu)
1845 {
1846 struct cleanup *back_to;
1847
1848 back_to = make_cleanup (dwarf2_release_queue, NULL);
1849
1850 queue_comp_unit (per_cu, objfile);
1851
1852 load_cu (per_cu);
1853
1854 process_queue (objfile);
1855
1856 /* Age the cache, releasing compilation units that have not
1857 been used recently. */
1858 age_cached_comp_units ();
1859
1860 do_cleanups (back_to);
1861 }
1862
1863 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1864 the objfile from which this CU came. Returns the resulting symbol
1865 table. */
1866
1867 static struct symtab *
1868 dw2_instantiate_symtab (struct objfile *objfile,
1869 struct dwarf2_per_cu_data *per_cu)
1870 {
1871 if (!per_cu->v.quick->symtab)
1872 {
1873 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1874 increment_reading_symtab ();
1875 dw2_do_instantiate_symtab (objfile, per_cu);
1876 do_cleanups (back_to);
1877 }
1878 return per_cu->v.quick->symtab;
1879 }
1880
1881 /* Return the CU given its index. */
1882
1883 static struct dwarf2_per_cu_data *
1884 dw2_get_cu (int index)
1885 {
1886 if (index >= dwarf2_per_objfile->n_comp_units)
1887 {
1888 index -= dwarf2_per_objfile->n_comp_units;
1889 return dwarf2_per_objfile->type_comp_units[index];
1890 }
1891 return dwarf2_per_objfile->all_comp_units[index];
1892 }
1893
1894 /* A helper function that knows how to read a 64-bit value in a way
1895 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1896 otherwise. */
1897
1898 static int
1899 extract_cu_value (const char *bytes, ULONGEST *result)
1900 {
1901 if (sizeof (ULONGEST) < 8)
1902 {
1903 int i;
1904
1905 /* Ignore the upper 4 bytes if they are all zero. */
1906 for (i = 0; i < 4; ++i)
1907 if (bytes[i + 4] != 0)
1908 return 0;
1909
1910 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1911 }
1912 else
1913 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1914 return 1;
1915 }
1916
1917 /* Read the CU list from the mapped index, and use it to create all
1918 the CU objects for this objfile. Return 0 if something went wrong,
1919 1 if everything went ok. */
1920
1921 static int
1922 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1923 offset_type cu_list_elements)
1924 {
1925 offset_type i;
1926
1927 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1928 dwarf2_per_objfile->all_comp_units
1929 = obstack_alloc (&objfile->objfile_obstack,
1930 dwarf2_per_objfile->n_comp_units
1931 * sizeof (struct dwarf2_per_cu_data *));
1932
1933 for (i = 0; i < cu_list_elements; i += 2)
1934 {
1935 struct dwarf2_per_cu_data *the_cu;
1936 ULONGEST offset, length;
1937
1938 if (!extract_cu_value (cu_list, &offset)
1939 || !extract_cu_value (cu_list + 8, &length))
1940 return 0;
1941 cu_list += 2 * 8;
1942
1943 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1944 struct dwarf2_per_cu_data);
1945 the_cu->offset = offset;
1946 the_cu->length = length;
1947 the_cu->objfile = objfile;
1948 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1949 struct dwarf2_per_cu_quick_data);
1950 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1951 }
1952
1953 return 1;
1954 }
1955
1956 /* Create the signatured type hash table from the index. */
1957
1958 static int
1959 create_signatured_type_table_from_index (struct objfile *objfile,
1960 struct dwarf2_section_info *section,
1961 const gdb_byte *bytes,
1962 offset_type elements)
1963 {
1964 offset_type i;
1965 htab_t sig_types_hash;
1966
1967 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1968 dwarf2_per_objfile->type_comp_units
1969 = obstack_alloc (&objfile->objfile_obstack,
1970 dwarf2_per_objfile->n_type_comp_units
1971 * sizeof (struct dwarf2_per_cu_data *));
1972
1973 sig_types_hash = allocate_signatured_type_table (objfile);
1974
1975 for (i = 0; i < elements; i += 3)
1976 {
1977 struct signatured_type *type_sig;
1978 ULONGEST offset, type_offset, signature;
1979 void **slot;
1980
1981 if (!extract_cu_value (bytes, &offset)
1982 || !extract_cu_value (bytes + 8, &type_offset))
1983 return 0;
1984 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1985 bytes += 3 * 8;
1986
1987 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1988 struct signatured_type);
1989 type_sig->signature = signature;
1990 type_sig->type_offset = type_offset;
1991 type_sig->per_cu.debug_type_section = section;
1992 type_sig->per_cu.offset = offset;
1993 type_sig->per_cu.objfile = objfile;
1994 type_sig->per_cu.v.quick
1995 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1996 struct dwarf2_per_cu_quick_data);
1997
1998 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1999 *slot = type_sig;
2000
2001 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2002 }
2003
2004 dwarf2_per_objfile->signatured_types = sig_types_hash;
2005
2006 return 1;
2007 }
2008
2009 /* Read the address map data from the mapped index, and use it to
2010 populate the objfile's psymtabs_addrmap. */
2011
2012 static void
2013 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2014 {
2015 const gdb_byte *iter, *end;
2016 struct obstack temp_obstack;
2017 struct addrmap *mutable_map;
2018 struct cleanup *cleanup;
2019 CORE_ADDR baseaddr;
2020
2021 obstack_init (&temp_obstack);
2022 cleanup = make_cleanup_obstack_free (&temp_obstack);
2023 mutable_map = addrmap_create_mutable (&temp_obstack);
2024
2025 iter = index->address_table;
2026 end = iter + index->address_table_size;
2027
2028 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2029
2030 while (iter < end)
2031 {
2032 ULONGEST hi, lo, cu_index;
2033 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2034 iter += 8;
2035 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2036 iter += 8;
2037 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2038 iter += 4;
2039
2040 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2041 dw2_get_cu (cu_index));
2042 }
2043
2044 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2045 &objfile->objfile_obstack);
2046 do_cleanups (cleanup);
2047 }
2048
2049 /* The hash function for strings in the mapped index. This is the same as
2050 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2051 implementation. This is necessary because the hash function is tied to the
2052 format of the mapped index file. The hash values do not have to match with
2053 SYMBOL_HASH_NEXT.
2054
2055 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2056
2057 static hashval_t
2058 mapped_index_string_hash (int index_version, const void *p)
2059 {
2060 const unsigned char *str = (const unsigned char *) p;
2061 hashval_t r = 0;
2062 unsigned char c;
2063
2064 while ((c = *str++) != 0)
2065 {
2066 if (index_version >= 5)
2067 c = tolower (c);
2068 r = r * 67 + c - 113;
2069 }
2070
2071 return r;
2072 }
2073
2074 /* Find a slot in the mapped index INDEX for the object named NAME.
2075 If NAME is found, set *VEC_OUT to point to the CU vector in the
2076 constant pool and return 1. If NAME cannot be found, return 0. */
2077
2078 static int
2079 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2080 offset_type **vec_out)
2081 {
2082 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2083 offset_type hash;
2084 offset_type slot, step;
2085 int (*cmp) (const char *, const char *);
2086
2087 if (current_language->la_language == language_cplus
2088 || current_language->la_language == language_java
2089 || current_language->la_language == language_fortran)
2090 {
2091 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2092 not contain any. */
2093 const char *paren = strchr (name, '(');
2094
2095 if (paren)
2096 {
2097 char *dup;
2098
2099 dup = xmalloc (paren - name + 1);
2100 memcpy (dup, name, paren - name);
2101 dup[paren - name] = 0;
2102
2103 make_cleanup (xfree, dup);
2104 name = dup;
2105 }
2106 }
2107
2108 /* Index version 4 did not support case insensitive searches. But the
2109 indexes for case insensitive languages are built in lowercase, therefore
2110 simulate our NAME being searched is also lowercased. */
2111 hash = mapped_index_string_hash ((index->version == 4
2112 && case_sensitivity == case_sensitive_off
2113 ? 5 : index->version),
2114 name);
2115
2116 slot = hash & (index->symbol_table_slots - 1);
2117 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2118 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2119
2120 for (;;)
2121 {
2122 /* Convert a slot number to an offset into the table. */
2123 offset_type i = 2 * slot;
2124 const char *str;
2125 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2126 {
2127 do_cleanups (back_to);
2128 return 0;
2129 }
2130
2131 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2132 if (!cmp (name, str))
2133 {
2134 *vec_out = (offset_type *) (index->constant_pool
2135 + MAYBE_SWAP (index->symbol_table[i + 1]));
2136 do_cleanups (back_to);
2137 return 1;
2138 }
2139
2140 slot = (slot + step) & (index->symbol_table_slots - 1);
2141 }
2142 }
2143
2144 /* Read the index file. If everything went ok, initialize the "quick"
2145 elements of all the CUs and return 1. Otherwise, return 0. */
2146
2147 static int
2148 dwarf2_read_index (struct objfile *objfile)
2149 {
2150 char *addr;
2151 struct mapped_index *map;
2152 offset_type *metadata;
2153 const gdb_byte *cu_list;
2154 const gdb_byte *types_list = NULL;
2155 offset_type version, cu_list_elements;
2156 offset_type types_list_elements = 0;
2157 int i;
2158
2159 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2160 return 0;
2161
2162 /* Older elfutils strip versions could keep the section in the main
2163 executable while splitting it for the separate debug info file. */
2164 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2165 & SEC_HAS_CONTENTS) == 0)
2166 return 0;
2167
2168 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2169
2170 addr = dwarf2_per_objfile->gdb_index.buffer;
2171 /* Version check. */
2172 version = MAYBE_SWAP (*(offset_type *) addr);
2173 /* Versions earlier than 3 emitted every copy of a psymbol. This
2174 causes the index to behave very poorly for certain requests. Version 3
2175 contained incomplete addrmap. So, it seems better to just ignore such
2176 indices. Index version 4 uses a different hash function than index
2177 version 5 and later. */
2178 if (version < 4)
2179 return 0;
2180 /* Indexes with higher version than the one supported by GDB may be no
2181 longer backward compatible. */
2182 if (version > 5)
2183 return 0;
2184
2185 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2186 map->version = version;
2187 map->total_size = dwarf2_per_objfile->gdb_index.size;
2188
2189 metadata = (offset_type *) (addr + sizeof (offset_type));
2190
2191 i = 0;
2192 cu_list = addr + MAYBE_SWAP (metadata[i]);
2193 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2194 / 8);
2195 ++i;
2196
2197 types_list = addr + MAYBE_SWAP (metadata[i]);
2198 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2199 - MAYBE_SWAP (metadata[i]))
2200 / 8);
2201 ++i;
2202
2203 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2204 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2205 - MAYBE_SWAP (metadata[i]));
2206 ++i;
2207
2208 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2209 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2210 - MAYBE_SWAP (metadata[i]))
2211 / (2 * sizeof (offset_type)));
2212 ++i;
2213
2214 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2215
2216 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2217 return 0;
2218
2219 if (types_list_elements)
2220 {
2221 struct dwarf2_section_info *section;
2222
2223 /* We can only handle a single .debug_types when we have an
2224 index. */
2225 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2226 return 0;
2227
2228 section = VEC_index (dwarf2_section_info_def,
2229 dwarf2_per_objfile->types, 0);
2230
2231 if (!create_signatured_type_table_from_index (objfile, section,
2232 types_list,
2233 types_list_elements))
2234 return 0;
2235 }
2236
2237 create_addrmap_from_index (objfile, map);
2238
2239 dwarf2_per_objfile->index_table = map;
2240 dwarf2_per_objfile->using_index = 1;
2241 dwarf2_per_objfile->quick_file_names_table =
2242 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2243
2244 return 1;
2245 }
2246
2247 /* A helper for the "quick" functions which sets the global
2248 dwarf2_per_objfile according to OBJFILE. */
2249
2250 static void
2251 dw2_setup (struct objfile *objfile)
2252 {
2253 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2254 gdb_assert (dwarf2_per_objfile);
2255 }
2256
2257 /* A helper for the "quick" functions which attempts to read the line
2258 table for THIS_CU. */
2259
2260 static struct quick_file_names *
2261 dw2_get_file_names (struct objfile *objfile,
2262 struct dwarf2_per_cu_data *this_cu)
2263 {
2264 bfd *abfd = objfile->obfd;
2265 struct line_header *lh;
2266 struct attribute *attr;
2267 struct cleanup *cleanups;
2268 struct die_info *comp_unit_die;
2269 struct dwarf2_section_info* sec;
2270 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2271 int has_children, i;
2272 struct dwarf2_cu cu;
2273 unsigned int bytes_read, buffer_size;
2274 struct die_reader_specs reader_specs;
2275 char *name, *comp_dir;
2276 void **slot;
2277 struct quick_file_names *qfn;
2278 unsigned int line_offset;
2279
2280 if (this_cu->v.quick->file_names != NULL)
2281 return this_cu->v.quick->file_names;
2282 /* If we know there is no line data, no point in looking again. */
2283 if (this_cu->v.quick->no_file_data)
2284 return NULL;
2285
2286 init_one_comp_unit (&cu, objfile);
2287 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2288
2289 if (this_cu->debug_type_section)
2290 sec = this_cu->debug_type_section;
2291 else
2292 sec = &dwarf2_per_objfile->info;
2293 dwarf2_read_section (objfile, sec);
2294 buffer_size = sec->size;
2295 buffer = sec->buffer;
2296 info_ptr = buffer + this_cu->offset;
2297 beg_of_comp_unit = info_ptr;
2298
2299 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2300 buffer, buffer_size,
2301 abfd);
2302
2303 /* Complete the cu_header. */
2304 cu.header.offset = beg_of_comp_unit - buffer;
2305 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2306
2307 this_cu->cu = &cu;
2308 cu.per_cu = this_cu;
2309
2310 dwarf2_read_abbrevs (abfd, &cu);
2311 make_cleanup (dwarf2_free_abbrev_table, &cu);
2312
2313 if (this_cu->debug_type_section)
2314 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2315 init_cu_die_reader (&reader_specs, &cu);
2316 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2317 &has_children);
2318
2319 lh = NULL;
2320 slot = NULL;
2321 line_offset = 0;
2322 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2323 if (attr)
2324 {
2325 struct quick_file_names find_entry;
2326
2327 line_offset = DW_UNSND (attr);
2328
2329 /* We may have already read in this line header (TU line header sharing).
2330 If we have we're done. */
2331 find_entry.offset = line_offset;
2332 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2333 &find_entry, INSERT);
2334 if (*slot != NULL)
2335 {
2336 do_cleanups (cleanups);
2337 this_cu->v.quick->file_names = *slot;
2338 return *slot;
2339 }
2340
2341 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2342 }
2343 if (lh == NULL)
2344 {
2345 do_cleanups (cleanups);
2346 this_cu->v.quick->no_file_data = 1;
2347 return NULL;
2348 }
2349
2350 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2351 qfn->offset = line_offset;
2352 gdb_assert (slot != NULL);
2353 *slot = qfn;
2354
2355 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2356
2357 qfn->num_file_names = lh->num_file_names;
2358 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2359 lh->num_file_names * sizeof (char *));
2360 for (i = 0; i < lh->num_file_names; ++i)
2361 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2362 qfn->real_names = NULL;
2363
2364 free_line_header (lh);
2365 do_cleanups (cleanups);
2366
2367 this_cu->v.quick->file_names = qfn;
2368 return qfn;
2369 }
2370
2371 /* A helper for the "quick" functions which computes and caches the
2372 real path for a given file name from the line table. */
2373
2374 static const char *
2375 dw2_get_real_path (struct objfile *objfile,
2376 struct quick_file_names *qfn, int index)
2377 {
2378 if (qfn->real_names == NULL)
2379 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2380 qfn->num_file_names, sizeof (char *));
2381
2382 if (qfn->real_names[index] == NULL)
2383 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2384
2385 return qfn->real_names[index];
2386 }
2387
2388 static struct symtab *
2389 dw2_find_last_source_symtab (struct objfile *objfile)
2390 {
2391 int index;
2392
2393 dw2_setup (objfile);
2394 index = dwarf2_per_objfile->n_comp_units - 1;
2395 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2396 }
2397
2398 /* Traversal function for dw2_forget_cached_source_info. */
2399
2400 static int
2401 dw2_free_cached_file_names (void **slot, void *info)
2402 {
2403 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2404
2405 if (file_data->real_names)
2406 {
2407 int i;
2408
2409 for (i = 0; i < file_data->num_file_names; ++i)
2410 {
2411 xfree ((void*) file_data->real_names[i]);
2412 file_data->real_names[i] = NULL;
2413 }
2414 }
2415
2416 return 1;
2417 }
2418
2419 static void
2420 dw2_forget_cached_source_info (struct objfile *objfile)
2421 {
2422 dw2_setup (objfile);
2423
2424 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2425 dw2_free_cached_file_names, NULL);
2426 }
2427
2428 static int
2429 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2430 const char *full_path, const char *real_path,
2431 struct symtab **result)
2432 {
2433 int i;
2434 int check_basename = lbasename (name) == name;
2435 struct dwarf2_per_cu_data *base_cu = NULL;
2436
2437 dw2_setup (objfile);
2438
2439 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2440 + dwarf2_per_objfile->n_type_comp_units); ++i)
2441 {
2442 int j;
2443 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2444 struct quick_file_names *file_data;
2445
2446 if (per_cu->v.quick->symtab)
2447 continue;
2448
2449 file_data = dw2_get_file_names (objfile, per_cu);
2450 if (file_data == NULL)
2451 continue;
2452
2453 for (j = 0; j < file_data->num_file_names; ++j)
2454 {
2455 const char *this_name = file_data->file_names[j];
2456
2457 if (FILENAME_CMP (name, this_name) == 0)
2458 {
2459 *result = dw2_instantiate_symtab (objfile, per_cu);
2460 return 1;
2461 }
2462
2463 if (check_basename && ! base_cu
2464 && FILENAME_CMP (lbasename (this_name), name) == 0)
2465 base_cu = per_cu;
2466
2467 if (full_path != NULL)
2468 {
2469 const char *this_real_name = dw2_get_real_path (objfile,
2470 file_data, j);
2471
2472 if (this_real_name != NULL
2473 && FILENAME_CMP (full_path, this_real_name) == 0)
2474 {
2475 *result = dw2_instantiate_symtab (objfile, per_cu);
2476 return 1;
2477 }
2478 }
2479
2480 if (real_path != NULL)
2481 {
2482 const char *this_real_name = dw2_get_real_path (objfile,
2483 file_data, j);
2484
2485 if (this_real_name != NULL
2486 && FILENAME_CMP (real_path, this_real_name) == 0)
2487 {
2488 *result = dw2_instantiate_symtab (objfile, per_cu);
2489 return 1;
2490 }
2491 }
2492 }
2493 }
2494
2495 if (base_cu)
2496 {
2497 *result = dw2_instantiate_symtab (objfile, base_cu);
2498 return 1;
2499 }
2500
2501 return 0;
2502 }
2503
2504 static struct symtab *
2505 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2506 const char *name, domain_enum domain)
2507 {
2508 /* We do all the work in the pre_expand_symtabs_matching hook
2509 instead. */
2510 return NULL;
2511 }
2512
2513 /* A helper function that expands all symtabs that hold an object
2514 named NAME. */
2515
2516 static void
2517 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2518 {
2519 dw2_setup (objfile);
2520
2521 /* index_table is NULL if OBJF_READNOW. */
2522 if (dwarf2_per_objfile->index_table)
2523 {
2524 offset_type *vec;
2525
2526 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2527 name, &vec))
2528 {
2529 offset_type i, len = MAYBE_SWAP (*vec);
2530 for (i = 0; i < len; ++i)
2531 {
2532 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2533 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2534
2535 dw2_instantiate_symtab (objfile, per_cu);
2536 }
2537 }
2538 }
2539 }
2540
2541 static void
2542 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2543 enum block_enum block_kind, const char *name,
2544 domain_enum domain)
2545 {
2546 dw2_do_expand_symtabs_matching (objfile, name);
2547 }
2548
2549 static void
2550 dw2_print_stats (struct objfile *objfile)
2551 {
2552 int i, count;
2553
2554 dw2_setup (objfile);
2555 count = 0;
2556 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2557 + dwarf2_per_objfile->n_type_comp_units); ++i)
2558 {
2559 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2560
2561 if (!per_cu->v.quick->symtab)
2562 ++count;
2563 }
2564 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2565 }
2566
2567 static void
2568 dw2_dump (struct objfile *objfile)
2569 {
2570 /* Nothing worth printing. */
2571 }
2572
2573 static void
2574 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2575 struct section_offsets *delta)
2576 {
2577 /* There's nothing to relocate here. */
2578 }
2579
2580 static void
2581 dw2_expand_symtabs_for_function (struct objfile *objfile,
2582 const char *func_name)
2583 {
2584 dw2_do_expand_symtabs_matching (objfile, func_name);
2585 }
2586
2587 static void
2588 dw2_expand_all_symtabs (struct objfile *objfile)
2589 {
2590 int i;
2591
2592 dw2_setup (objfile);
2593
2594 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2595 + dwarf2_per_objfile->n_type_comp_units); ++i)
2596 {
2597 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2598
2599 dw2_instantiate_symtab (objfile, per_cu);
2600 }
2601 }
2602
2603 static void
2604 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2605 const char *filename)
2606 {
2607 int i;
2608
2609 dw2_setup (objfile);
2610
2611 /* We don't need to consider type units here.
2612 This is only called for examining code, e.g. expand_line_sal.
2613 There can be an order of magnitude (or more) more type units
2614 than comp units, and we avoid them if we can. */
2615
2616 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2617 {
2618 int j;
2619 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2620 struct quick_file_names *file_data;
2621
2622 if (per_cu->v.quick->symtab)
2623 continue;
2624
2625 file_data = dw2_get_file_names (objfile, per_cu);
2626 if (file_data == NULL)
2627 continue;
2628
2629 for (j = 0; j < file_data->num_file_names; ++j)
2630 {
2631 const char *this_name = file_data->file_names[j];
2632 if (FILENAME_CMP (this_name, filename) == 0)
2633 {
2634 dw2_instantiate_symtab (objfile, per_cu);
2635 break;
2636 }
2637 }
2638 }
2639 }
2640
2641 static const char *
2642 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2643 {
2644 struct dwarf2_per_cu_data *per_cu;
2645 offset_type *vec;
2646 struct quick_file_names *file_data;
2647
2648 dw2_setup (objfile);
2649
2650 /* index_table is NULL if OBJF_READNOW. */
2651 if (!dwarf2_per_objfile->index_table)
2652 return NULL;
2653
2654 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2655 name, &vec))
2656 return NULL;
2657
2658 /* Note that this just looks at the very first one named NAME -- but
2659 actually we are looking for a function. find_main_filename
2660 should be rewritten so that it doesn't require a custom hook. It
2661 could just use the ordinary symbol tables. */
2662 /* vec[0] is the length, which must always be >0. */
2663 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2664
2665 file_data = dw2_get_file_names (objfile, per_cu);
2666 if (file_data == NULL)
2667 return NULL;
2668
2669 return file_data->file_names[file_data->num_file_names - 1];
2670 }
2671
2672 static void
2673 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2674 struct objfile *objfile, int global,
2675 int (*callback) (struct block *,
2676 struct symbol *, void *),
2677 void *data, symbol_compare_ftype *match,
2678 symbol_compare_ftype *ordered_compare)
2679 {
2680 /* Currently unimplemented; used for Ada. The function can be called if the
2681 current language is Ada for a non-Ada objfile using GNU index. As Ada
2682 does not look for non-Ada symbols this function should just return. */
2683 }
2684
2685 static void
2686 dw2_expand_symtabs_matching (struct objfile *objfile,
2687 int (*file_matcher) (const char *, void *),
2688 int (*name_matcher) (const char *, void *),
2689 enum search_domain kind,
2690 void *data)
2691 {
2692 int i;
2693 offset_type iter;
2694 struct mapped_index *index;
2695
2696 dw2_setup (objfile);
2697
2698 /* index_table is NULL if OBJF_READNOW. */
2699 if (!dwarf2_per_objfile->index_table)
2700 return;
2701 index = dwarf2_per_objfile->index_table;
2702
2703 if (file_matcher != NULL)
2704 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2705 + dwarf2_per_objfile->n_type_comp_units); ++i)
2706 {
2707 int j;
2708 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2709 struct quick_file_names *file_data;
2710
2711 per_cu->v.quick->mark = 0;
2712 if (per_cu->v.quick->symtab)
2713 continue;
2714
2715 file_data = dw2_get_file_names (objfile, per_cu);
2716 if (file_data == NULL)
2717 continue;
2718
2719 for (j = 0; j < file_data->num_file_names; ++j)
2720 {
2721 if (file_matcher (file_data->file_names[j], data))
2722 {
2723 per_cu->v.quick->mark = 1;
2724 break;
2725 }
2726 }
2727 }
2728
2729 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2730 {
2731 offset_type idx = 2 * iter;
2732 const char *name;
2733 offset_type *vec, vec_len, vec_idx;
2734
2735 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2736 continue;
2737
2738 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2739
2740 if (! (*name_matcher) (name, data))
2741 continue;
2742
2743 /* The name was matched, now expand corresponding CUs that were
2744 marked. */
2745 vec = (offset_type *) (index->constant_pool
2746 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2747 vec_len = MAYBE_SWAP (vec[0]);
2748 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2749 {
2750 struct dwarf2_per_cu_data *per_cu;
2751
2752 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2753 if (file_matcher == NULL || per_cu->v.quick->mark)
2754 dw2_instantiate_symtab (objfile, per_cu);
2755 }
2756 }
2757 }
2758
2759 static struct symtab *
2760 dw2_find_pc_sect_symtab (struct objfile *objfile,
2761 struct minimal_symbol *msymbol,
2762 CORE_ADDR pc,
2763 struct obj_section *section,
2764 int warn_if_readin)
2765 {
2766 struct dwarf2_per_cu_data *data;
2767
2768 dw2_setup (objfile);
2769
2770 if (!objfile->psymtabs_addrmap)
2771 return NULL;
2772
2773 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2774 if (!data)
2775 return NULL;
2776
2777 if (warn_if_readin && data->v.quick->symtab)
2778 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2779 paddress (get_objfile_arch (objfile), pc));
2780
2781 return dw2_instantiate_symtab (objfile, data);
2782 }
2783
2784 static void
2785 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2786 void *data)
2787 {
2788 int i;
2789
2790 dw2_setup (objfile);
2791
2792 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2793 + dwarf2_per_objfile->n_type_comp_units); ++i)
2794 {
2795 int j;
2796 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2797 struct quick_file_names *file_data;
2798
2799 if (per_cu->v.quick->symtab)
2800 continue;
2801
2802 file_data = dw2_get_file_names (objfile, per_cu);
2803 if (file_data == NULL)
2804 continue;
2805
2806 for (j = 0; j < file_data->num_file_names; ++j)
2807 {
2808 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2809 j);
2810 (*fun) (file_data->file_names[j], this_real_name, data);
2811 }
2812 }
2813 }
2814
2815 static int
2816 dw2_has_symbols (struct objfile *objfile)
2817 {
2818 return 1;
2819 }
2820
2821 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2822 {
2823 dw2_has_symbols,
2824 dw2_find_last_source_symtab,
2825 dw2_forget_cached_source_info,
2826 dw2_lookup_symtab,
2827 dw2_lookup_symbol,
2828 dw2_pre_expand_symtabs_matching,
2829 dw2_print_stats,
2830 dw2_dump,
2831 dw2_relocate,
2832 dw2_expand_symtabs_for_function,
2833 dw2_expand_all_symtabs,
2834 dw2_expand_symtabs_with_filename,
2835 dw2_find_symbol_file,
2836 dw2_map_matching_symbols,
2837 dw2_expand_symtabs_matching,
2838 dw2_find_pc_sect_symtab,
2839 dw2_map_symbol_filenames
2840 };
2841
2842 /* Initialize for reading DWARF for this objfile. Return 0 if this
2843 file will use psymtabs, or 1 if using the GNU index. */
2844
2845 int
2846 dwarf2_initialize_objfile (struct objfile *objfile)
2847 {
2848 /* If we're about to read full symbols, don't bother with the
2849 indices. In this case we also don't care if some other debug
2850 format is making psymtabs, because they are all about to be
2851 expanded anyway. */
2852 if ((objfile->flags & OBJF_READNOW))
2853 {
2854 int i;
2855
2856 dwarf2_per_objfile->using_index = 1;
2857 create_all_comp_units (objfile);
2858 create_debug_types_hash_table (objfile);
2859 dwarf2_per_objfile->quick_file_names_table =
2860 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2861
2862 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2863 + dwarf2_per_objfile->n_type_comp_units); ++i)
2864 {
2865 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2866
2867 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2868 struct dwarf2_per_cu_quick_data);
2869 }
2870
2871 /* Return 1 so that gdb sees the "quick" functions. However,
2872 these functions will be no-ops because we will have expanded
2873 all symtabs. */
2874 return 1;
2875 }
2876
2877 if (dwarf2_read_index (objfile))
2878 return 1;
2879
2880 return 0;
2881 }
2882
2883 \f
2884
2885 /* Build a partial symbol table. */
2886
2887 void
2888 dwarf2_build_psymtabs (struct objfile *objfile)
2889 {
2890 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2891 {
2892 init_psymbol_list (objfile, 1024);
2893 }
2894
2895 dwarf2_build_psymtabs_hard (objfile);
2896 }
2897
2898 /* Return TRUE if OFFSET is within CU_HEADER. */
2899
2900 static inline int
2901 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2902 {
2903 unsigned int bottom = cu_header->offset;
2904 unsigned int top = (cu_header->offset
2905 + cu_header->length
2906 + cu_header->initial_length_size);
2907
2908 return (offset >= bottom && offset < top);
2909 }
2910
2911 /* Read in the comp unit header information from the debug_info at info_ptr.
2912 NOTE: This leaves members offset, first_die_offset to be filled in
2913 by the caller. */
2914
2915 static gdb_byte *
2916 read_comp_unit_head (struct comp_unit_head *cu_header,
2917 gdb_byte *info_ptr, bfd *abfd)
2918 {
2919 int signed_addr;
2920 unsigned int bytes_read;
2921
2922 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2923 cu_header->initial_length_size = bytes_read;
2924 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2925 info_ptr += bytes_read;
2926 cu_header->version = read_2_bytes (abfd, info_ptr);
2927 info_ptr += 2;
2928 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2929 &bytes_read);
2930 info_ptr += bytes_read;
2931 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2932 info_ptr += 1;
2933 signed_addr = bfd_get_sign_extend_vma (abfd);
2934 if (signed_addr < 0)
2935 internal_error (__FILE__, __LINE__,
2936 _("read_comp_unit_head: dwarf from non elf file"));
2937 cu_header->signed_addr_p = signed_addr;
2938
2939 return info_ptr;
2940 }
2941
2942 static gdb_byte *
2943 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2944 gdb_byte *buffer, unsigned int buffer_size,
2945 bfd *abfd)
2946 {
2947 gdb_byte *beg_of_comp_unit = info_ptr;
2948
2949 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2950
2951 if (header->version != 2 && header->version != 3 && header->version != 4)
2952 error (_("Dwarf Error: wrong version in compilation unit header "
2953 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2954 bfd_get_filename (abfd));
2955
2956 if (header->abbrev_offset
2957 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2958 &dwarf2_per_objfile->abbrev))
2959 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2960 "(offset 0x%lx + 6) [in module %s]"),
2961 (long) header->abbrev_offset,
2962 (long) (beg_of_comp_unit - buffer),
2963 bfd_get_filename (abfd));
2964
2965 if (beg_of_comp_unit + header->length + header->initial_length_size
2966 > buffer + buffer_size)
2967 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2968 "(offset 0x%lx + 0) [in module %s]"),
2969 (long) header->length,
2970 (long) (beg_of_comp_unit - buffer),
2971 bfd_get_filename (abfd));
2972
2973 return info_ptr;
2974 }
2975
2976 /* Read in the types comp unit header information from .debug_types entry at
2977 types_ptr. The result is a pointer to one past the end of the header. */
2978
2979 static gdb_byte *
2980 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2981 struct dwarf2_section_info *section,
2982 ULONGEST *signature,
2983 gdb_byte *types_ptr, bfd *abfd)
2984 {
2985 gdb_byte *initial_types_ptr = types_ptr;
2986
2987 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
2988 cu_header->offset = types_ptr - section->buffer;
2989
2990 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2991
2992 *signature = read_8_bytes (abfd, types_ptr);
2993 types_ptr += 8;
2994 types_ptr += cu_header->offset_size;
2995 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2996
2997 return types_ptr;
2998 }
2999
3000 /* Allocate a new partial symtab for file named NAME and mark this new
3001 partial symtab as being an include of PST. */
3002
3003 static void
3004 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3005 struct objfile *objfile)
3006 {
3007 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3008
3009 subpst->section_offsets = pst->section_offsets;
3010 subpst->textlow = 0;
3011 subpst->texthigh = 0;
3012
3013 subpst->dependencies = (struct partial_symtab **)
3014 obstack_alloc (&objfile->objfile_obstack,
3015 sizeof (struct partial_symtab *));
3016 subpst->dependencies[0] = pst;
3017 subpst->number_of_dependencies = 1;
3018
3019 subpst->globals_offset = 0;
3020 subpst->n_global_syms = 0;
3021 subpst->statics_offset = 0;
3022 subpst->n_static_syms = 0;
3023 subpst->symtab = NULL;
3024 subpst->read_symtab = pst->read_symtab;
3025 subpst->readin = 0;
3026
3027 /* No private part is necessary for include psymtabs. This property
3028 can be used to differentiate between such include psymtabs and
3029 the regular ones. */
3030 subpst->read_symtab_private = NULL;
3031 }
3032
3033 /* Read the Line Number Program data and extract the list of files
3034 included by the source file represented by PST. Build an include
3035 partial symtab for each of these included files. */
3036
3037 static void
3038 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3039 struct die_info *die,
3040 struct partial_symtab *pst)
3041 {
3042 struct objfile *objfile = cu->objfile;
3043 bfd *abfd = objfile->obfd;
3044 struct line_header *lh = NULL;
3045 struct attribute *attr;
3046
3047 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3048 if (attr)
3049 {
3050 unsigned int line_offset = DW_UNSND (attr);
3051
3052 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3053 }
3054 if (lh == NULL)
3055 return; /* No linetable, so no includes. */
3056
3057 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3058 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3059
3060 free_line_header (lh);
3061 }
3062
3063 static hashval_t
3064 hash_type_signature (const void *item)
3065 {
3066 const struct signatured_type *type_sig = item;
3067
3068 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3069 return type_sig->signature;
3070 }
3071
3072 static int
3073 eq_type_signature (const void *item_lhs, const void *item_rhs)
3074 {
3075 const struct signatured_type *lhs = item_lhs;
3076 const struct signatured_type *rhs = item_rhs;
3077
3078 return lhs->signature == rhs->signature;
3079 }
3080
3081 /* Allocate a hash table for signatured types. */
3082
3083 static htab_t
3084 allocate_signatured_type_table (struct objfile *objfile)
3085 {
3086 return htab_create_alloc_ex (41,
3087 hash_type_signature,
3088 eq_type_signature,
3089 NULL,
3090 &objfile->objfile_obstack,
3091 hashtab_obstack_allocate,
3092 dummy_obstack_deallocate);
3093 }
3094
3095 /* A helper function to add a signatured type CU to a list. */
3096
3097 static int
3098 add_signatured_type_cu_to_list (void **slot, void *datum)
3099 {
3100 struct signatured_type *sigt = *slot;
3101 struct dwarf2_per_cu_data ***datap = datum;
3102
3103 **datap = &sigt->per_cu;
3104 ++*datap;
3105
3106 return 1;
3107 }
3108
3109 /* Create the hash table of all entries in the .debug_types section.
3110 The result is zero if there is an error (e.g. missing .debug_types section),
3111 otherwise non-zero. */
3112
3113 static int
3114 create_debug_types_hash_table (struct objfile *objfile)
3115 {
3116 htab_t types_htab = NULL;
3117 struct dwarf2_per_cu_data **iter;
3118 int ix;
3119 struct dwarf2_section_info *section;
3120
3121 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3122 {
3123 dwarf2_per_objfile->signatured_types = NULL;
3124 return 0;
3125 }
3126
3127 for (ix = 0;
3128 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3129 ix, section);
3130 ++ix)
3131 {
3132 gdb_byte *info_ptr, *end_ptr;
3133
3134 dwarf2_read_section (objfile, section);
3135 info_ptr = section->buffer;
3136
3137 if (info_ptr == NULL)
3138 continue;
3139
3140 if (types_htab == NULL)
3141 types_htab = allocate_signatured_type_table (objfile);
3142
3143 if (dwarf2_die_debug)
3144 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3145
3146 end_ptr = info_ptr + section->size;
3147 while (info_ptr < end_ptr)
3148 {
3149 unsigned int offset;
3150 unsigned int offset_size;
3151 unsigned int type_offset;
3152 unsigned int length, initial_length_size;
3153 unsigned short version;
3154 ULONGEST signature;
3155 struct signatured_type *type_sig;
3156 void **slot;
3157 gdb_byte *ptr = info_ptr;
3158
3159 offset = ptr - section->buffer;
3160
3161 /* We need to read the type's signature in order to build the hash
3162 table, but we don't need to read anything else just yet. */
3163
3164 /* Sanity check to ensure entire cu is present. */
3165 length = read_initial_length (objfile->obfd, ptr,
3166 &initial_length_size);
3167 if (ptr + length + initial_length_size > end_ptr)
3168 {
3169 complaint (&symfile_complaints,
3170 _("debug type entry runs off end "
3171 "of `.debug_types' section, ignored"));
3172 break;
3173 }
3174
3175 offset_size = initial_length_size == 4 ? 4 : 8;
3176 ptr += initial_length_size;
3177 version = bfd_get_16 (objfile->obfd, ptr);
3178 ptr += 2;
3179 ptr += offset_size; /* abbrev offset */
3180 ptr += 1; /* address size */
3181 signature = bfd_get_64 (objfile->obfd, ptr);
3182 ptr += 8;
3183 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3184
3185 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3186 memset (type_sig, 0, sizeof (*type_sig));
3187 type_sig->signature = signature;
3188 type_sig->type_offset = type_offset;
3189 type_sig->per_cu.objfile = objfile;
3190 type_sig->per_cu.debug_type_section = section;
3191 type_sig->per_cu.offset = offset;
3192
3193 slot = htab_find_slot (types_htab, type_sig, INSERT);
3194 gdb_assert (slot != NULL);
3195 if (*slot != NULL)
3196 {
3197 const struct signatured_type *dup_sig = *slot;
3198
3199 complaint (&symfile_complaints,
3200 _("debug type entry at offset 0x%x is duplicate to the "
3201 "entry at offset 0x%x, signature 0x%s"),
3202 offset, dup_sig->per_cu.offset,
3203 phex (signature, sizeof (signature)));
3204 gdb_assert (signature == dup_sig->signature);
3205 }
3206 *slot = type_sig;
3207
3208 if (dwarf2_die_debug)
3209 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3210 offset, phex (signature, sizeof (signature)));
3211
3212 info_ptr = info_ptr + initial_length_size + length;
3213 }
3214 }
3215
3216 dwarf2_per_objfile->signatured_types = types_htab;
3217
3218 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3219 dwarf2_per_objfile->type_comp_units
3220 = obstack_alloc (&objfile->objfile_obstack,
3221 dwarf2_per_objfile->n_type_comp_units
3222 * sizeof (struct dwarf2_per_cu_data *));
3223 iter = &dwarf2_per_objfile->type_comp_units[0];
3224 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3225 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3226 == dwarf2_per_objfile->n_type_comp_units);
3227
3228 return 1;
3229 }
3230
3231 /* Lookup a signature based type.
3232 Returns NULL if SIG is not present in the table. */
3233
3234 static struct signatured_type *
3235 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3236 {
3237 struct signatured_type find_entry, *entry;
3238
3239 if (dwarf2_per_objfile->signatured_types == NULL)
3240 {
3241 complaint (&symfile_complaints,
3242 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3243 return 0;
3244 }
3245
3246 find_entry.signature = sig;
3247 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3248 return entry;
3249 }
3250
3251 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3252
3253 static void
3254 init_cu_die_reader (struct die_reader_specs *reader,
3255 struct dwarf2_cu *cu)
3256 {
3257 reader->abfd = cu->objfile->obfd;
3258 reader->cu = cu;
3259 if (cu->per_cu->debug_type_section)
3260 {
3261 gdb_assert (cu->per_cu->debug_type_section->readin);
3262 reader->buffer = cu->per_cu->debug_type_section->buffer;
3263 }
3264 else
3265 {
3266 gdb_assert (dwarf2_per_objfile->info.readin);
3267 reader->buffer = dwarf2_per_objfile->info.buffer;
3268 }
3269 }
3270
3271 /* Find the base address of the compilation unit for range lists and
3272 location lists. It will normally be specified by DW_AT_low_pc.
3273 In DWARF-3 draft 4, the base address could be overridden by
3274 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3275 compilation units with discontinuous ranges. */
3276
3277 static void
3278 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3279 {
3280 struct attribute *attr;
3281
3282 cu->base_known = 0;
3283 cu->base_address = 0;
3284
3285 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3286 if (attr)
3287 {
3288 cu->base_address = DW_ADDR (attr);
3289 cu->base_known = 1;
3290 }
3291 else
3292 {
3293 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3294 if (attr)
3295 {
3296 cu->base_address = DW_ADDR (attr);
3297 cu->base_known = 1;
3298 }
3299 }
3300 }
3301
3302 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3303 to combine the common parts.
3304 Process a compilation unit for a psymtab.
3305 BUFFER is a pointer to the beginning of the dwarf section buffer,
3306 either .debug_info or debug_types.
3307 INFO_PTR is a pointer to the start of the CU.
3308 Returns a pointer to the next CU. */
3309
3310 static gdb_byte *
3311 process_psymtab_comp_unit (struct objfile *objfile,
3312 struct dwarf2_per_cu_data *this_cu,
3313 gdb_byte *buffer, gdb_byte *info_ptr,
3314 unsigned int buffer_size)
3315 {
3316 bfd *abfd = objfile->obfd;
3317 gdb_byte *beg_of_comp_unit = info_ptr;
3318 struct die_info *comp_unit_die;
3319 struct partial_symtab *pst;
3320 CORE_ADDR baseaddr;
3321 struct cleanup *back_to_inner;
3322 struct dwarf2_cu cu;
3323 int has_children, has_pc_info;
3324 struct attribute *attr;
3325 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3326 struct die_reader_specs reader_specs;
3327 const char *filename;
3328
3329 init_one_comp_unit (&cu, objfile);
3330 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3331
3332 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3333 buffer, buffer_size,
3334 abfd);
3335
3336 /* Complete the cu_header. */
3337 cu.header.offset = beg_of_comp_unit - buffer;
3338 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3339
3340 cu.list_in_scope = &file_symbols;
3341
3342 /* If this compilation unit was already read in, free the
3343 cached copy in order to read it in again. This is
3344 necessary because we skipped some symbols when we first
3345 read in the compilation unit (see load_partial_dies).
3346 This problem could be avoided, but the benefit is
3347 unclear. */
3348 if (this_cu->cu != NULL)
3349 free_one_cached_comp_unit (this_cu->cu);
3350
3351 /* Note that this is a pointer to our stack frame, being
3352 added to a global data structure. It will be cleaned up
3353 in free_stack_comp_unit when we finish with this
3354 compilation unit. */
3355 this_cu->cu = &cu;
3356 cu.per_cu = this_cu;
3357
3358 /* Read the abbrevs for this compilation unit into a table. */
3359 dwarf2_read_abbrevs (abfd, &cu);
3360 make_cleanup (dwarf2_free_abbrev_table, &cu);
3361
3362 /* Read the compilation unit die. */
3363 if (this_cu->debug_type_section)
3364 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3365 init_cu_die_reader (&reader_specs, &cu);
3366 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3367 &has_children);
3368
3369 if (this_cu->debug_type_section)
3370 {
3371 /* LENGTH has not been set yet for type units. */
3372 gdb_assert (this_cu->offset == cu.header.offset);
3373 this_cu->length = cu.header.length + cu.header.initial_length_size;
3374 }
3375 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3376 {
3377 info_ptr = (beg_of_comp_unit + cu.header.length
3378 + cu.header.initial_length_size);
3379 do_cleanups (back_to_inner);
3380 return info_ptr;
3381 }
3382
3383 prepare_one_comp_unit (&cu, comp_unit_die);
3384
3385 /* Allocate a new partial symbol table structure. */
3386 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3387 if (attr == NULL || !DW_STRING (attr))
3388 filename = "";
3389 else
3390 filename = DW_STRING (attr);
3391 pst = start_psymtab_common (objfile, objfile->section_offsets,
3392 filename,
3393 /* TEXTLOW and TEXTHIGH are set below. */
3394 0,
3395 objfile->global_psymbols.next,
3396 objfile->static_psymbols.next);
3397
3398 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3399 if (attr != NULL)
3400 pst->dirname = DW_STRING (attr);
3401
3402 pst->read_symtab_private = this_cu;
3403
3404 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3405
3406 /* Store the function that reads in the rest of the symbol table. */
3407 pst->read_symtab = dwarf2_psymtab_to_symtab;
3408
3409 this_cu->v.psymtab = pst;
3410
3411 dwarf2_find_base_address (comp_unit_die, &cu);
3412
3413 /* Possibly set the default values of LOWPC and HIGHPC from
3414 `DW_AT_ranges'. */
3415 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3416 &best_highpc, &cu, pst);
3417 if (has_pc_info == 1 && best_lowpc < best_highpc)
3418 /* Store the contiguous range if it is not empty; it can be empty for
3419 CUs with no code. */
3420 addrmap_set_empty (objfile->psymtabs_addrmap,
3421 best_lowpc + baseaddr,
3422 best_highpc + baseaddr - 1, pst);
3423
3424 /* Check if comp unit has_children.
3425 If so, read the rest of the partial symbols from this comp unit.
3426 If not, there's no more debug_info for this comp unit. */
3427 if (has_children)
3428 {
3429 struct partial_die_info *first_die;
3430 CORE_ADDR lowpc, highpc;
3431
3432 lowpc = ((CORE_ADDR) -1);
3433 highpc = ((CORE_ADDR) 0);
3434
3435 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3436
3437 scan_partial_symbols (first_die, &lowpc, &highpc,
3438 ! has_pc_info, &cu);
3439
3440 /* If we didn't find a lowpc, set it to highpc to avoid
3441 complaints from `maint check'. */
3442 if (lowpc == ((CORE_ADDR) -1))
3443 lowpc = highpc;
3444
3445 /* If the compilation unit didn't have an explicit address range,
3446 then use the information extracted from its child dies. */
3447 if (! has_pc_info)
3448 {
3449 best_lowpc = lowpc;
3450 best_highpc = highpc;
3451 }
3452 }
3453 pst->textlow = best_lowpc + baseaddr;
3454 pst->texthigh = best_highpc + baseaddr;
3455
3456 pst->n_global_syms = objfile->global_psymbols.next -
3457 (objfile->global_psymbols.list + pst->globals_offset);
3458 pst->n_static_syms = objfile->static_psymbols.next -
3459 (objfile->static_psymbols.list + pst->statics_offset);
3460 sort_pst_symbols (pst);
3461
3462 info_ptr = (beg_of_comp_unit + cu.header.length
3463 + cu.header.initial_length_size);
3464
3465 if (this_cu->debug_type_section)
3466 {
3467 /* It's not clear we want to do anything with stmt lists here.
3468 Waiting to see what gcc ultimately does. */
3469 }
3470 else
3471 {
3472 /* Get the list of files included in the current compilation unit,
3473 and build a psymtab for each of them. */
3474 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3475 }
3476
3477 do_cleanups (back_to_inner);
3478
3479 return info_ptr;
3480 }
3481
3482 /* Traversal function for htab_traverse_noresize.
3483 Process one .debug_types comp-unit. */
3484
3485 static int
3486 process_type_comp_unit (void **slot, void *info)
3487 {
3488 struct signatured_type *entry = (struct signatured_type *) *slot;
3489 struct objfile *objfile = (struct objfile *) info;
3490 struct dwarf2_per_cu_data *this_cu;
3491
3492 this_cu = &entry->per_cu;
3493
3494 gdb_assert (this_cu->debug_type_section->readin);
3495 process_psymtab_comp_unit (objfile, this_cu,
3496 this_cu->debug_type_section->buffer,
3497 (this_cu->debug_type_section->buffer
3498 + this_cu->offset),
3499 this_cu->debug_type_section->size);
3500
3501 return 1;
3502 }
3503
3504 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3505 Build partial symbol tables for the .debug_types comp-units. */
3506
3507 static void
3508 build_type_psymtabs (struct objfile *objfile)
3509 {
3510 if (! create_debug_types_hash_table (objfile))
3511 return;
3512
3513 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3514 process_type_comp_unit, objfile);
3515 }
3516
3517 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3518
3519 static void
3520 psymtabs_addrmap_cleanup (void *o)
3521 {
3522 struct objfile *objfile = o;
3523
3524 objfile->psymtabs_addrmap = NULL;
3525 }
3526
3527 /* Build the partial symbol table by doing a quick pass through the
3528 .debug_info and .debug_abbrev sections. */
3529
3530 static void
3531 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3532 {
3533 gdb_byte *info_ptr;
3534 struct cleanup *back_to, *addrmap_cleanup;
3535 struct obstack temp_obstack;
3536
3537 dwarf2_per_objfile->reading_partial_symbols = 1;
3538
3539 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3540 info_ptr = dwarf2_per_objfile->info.buffer;
3541
3542 /* Any cached compilation units will be linked by the per-objfile
3543 read_in_chain. Make sure to free them when we're done. */
3544 back_to = make_cleanup (free_cached_comp_units, NULL);
3545
3546 build_type_psymtabs (objfile);
3547
3548 create_all_comp_units (objfile);
3549
3550 /* Create a temporary address map on a temporary obstack. We later
3551 copy this to the final obstack. */
3552 obstack_init (&temp_obstack);
3553 make_cleanup_obstack_free (&temp_obstack);
3554 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3555 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3556
3557 /* Since the objects we're extracting from .debug_info vary in
3558 length, only the individual functions to extract them (like
3559 read_comp_unit_head and load_partial_die) can really know whether
3560 the buffer is large enough to hold another complete object.
3561
3562 At the moment, they don't actually check that. If .debug_info
3563 holds just one extra byte after the last compilation unit's dies,
3564 then read_comp_unit_head will happily read off the end of the
3565 buffer. read_partial_die is similarly casual. Those functions
3566 should be fixed.
3567
3568 For this loop condition, simply checking whether there's any data
3569 left at all should be sufficient. */
3570
3571 while (info_ptr < (dwarf2_per_objfile->info.buffer
3572 + dwarf2_per_objfile->info.size))
3573 {
3574 struct dwarf2_per_cu_data *this_cu;
3575
3576 this_cu = dwarf2_find_comp_unit (info_ptr
3577 - dwarf2_per_objfile->info.buffer,
3578 objfile);
3579
3580 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3581 dwarf2_per_objfile->info.buffer,
3582 info_ptr,
3583 dwarf2_per_objfile->info.size);
3584 }
3585
3586 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3587 &objfile->objfile_obstack);
3588 discard_cleanups (addrmap_cleanup);
3589
3590 do_cleanups (back_to);
3591 }
3592
3593 /* Load the partial DIEs for a secondary CU into memory. */
3594
3595 static void
3596 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3597 struct objfile *objfile)
3598 {
3599 bfd *abfd = objfile->obfd;
3600 gdb_byte *info_ptr, *beg_of_comp_unit;
3601 struct die_info *comp_unit_die;
3602 struct dwarf2_cu *cu;
3603 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3604 int has_children;
3605 struct die_reader_specs reader_specs;
3606 int read_cu = 0;
3607
3608 gdb_assert (! this_cu->debug_type_section);
3609
3610 gdb_assert (dwarf2_per_objfile->info.readin);
3611 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3612 beg_of_comp_unit = info_ptr;
3613
3614 if (this_cu->cu == NULL)
3615 {
3616 cu = xmalloc (sizeof (*cu));
3617 init_one_comp_unit (cu, objfile);
3618
3619 read_cu = 1;
3620
3621 /* If an error occurs while loading, release our storage. */
3622 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3623
3624 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3625 dwarf2_per_objfile->info.buffer,
3626 dwarf2_per_objfile->info.size,
3627 abfd);
3628
3629 /* Complete the cu_header. */
3630 cu->header.offset = this_cu->offset;
3631 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3632
3633 /* Link this compilation unit into the compilation unit tree. */
3634 this_cu->cu = cu;
3635 cu->per_cu = this_cu;
3636
3637 /* Link this CU into read_in_chain. */
3638 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3639 dwarf2_per_objfile->read_in_chain = this_cu;
3640 }
3641 else
3642 {
3643 cu = this_cu->cu;
3644 info_ptr += cu->header.first_die_offset;
3645 }
3646
3647 /* Read the abbrevs for this compilation unit into a table. */
3648 gdb_assert (cu->dwarf2_abbrevs == NULL);
3649 dwarf2_read_abbrevs (abfd, cu);
3650 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3651
3652 /* Read the compilation unit die. */
3653 init_cu_die_reader (&reader_specs, cu);
3654 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3655 &has_children);
3656
3657 prepare_one_comp_unit (cu, comp_unit_die);
3658
3659 /* Check if comp unit has_children.
3660 If so, read the rest of the partial symbols from this comp unit.
3661 If not, there's no more debug_info for this comp unit. */
3662 if (has_children)
3663 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3664
3665 do_cleanups (free_abbrevs_cleanup);
3666
3667 if (read_cu)
3668 {
3669 /* We've successfully allocated this compilation unit. Let our
3670 caller clean it up when finished with it. */
3671 discard_cleanups (free_cu_cleanup);
3672 }
3673 }
3674
3675 /* Create a list of all compilation units in OBJFILE. We do this only
3676 if an inter-comp-unit reference is found; presumably if there is one,
3677 there will be many, and one will occur early in the .debug_info section.
3678 So there's no point in building this list incrementally. */
3679
3680 static void
3681 create_all_comp_units (struct objfile *objfile)
3682 {
3683 int n_allocated;
3684 int n_comp_units;
3685 struct dwarf2_per_cu_data **all_comp_units;
3686 gdb_byte *info_ptr;
3687
3688 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3689 info_ptr = dwarf2_per_objfile->info.buffer;
3690
3691 n_comp_units = 0;
3692 n_allocated = 10;
3693 all_comp_units = xmalloc (n_allocated
3694 * sizeof (struct dwarf2_per_cu_data *));
3695
3696 while (info_ptr < dwarf2_per_objfile->info.buffer
3697 + dwarf2_per_objfile->info.size)
3698 {
3699 unsigned int length, initial_length_size;
3700 struct dwarf2_per_cu_data *this_cu;
3701 unsigned int offset;
3702
3703 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3704
3705 /* Read just enough information to find out where the next
3706 compilation unit is. */
3707 length = read_initial_length (objfile->obfd, info_ptr,
3708 &initial_length_size);
3709
3710 /* Save the compilation unit for later lookup. */
3711 this_cu = obstack_alloc (&objfile->objfile_obstack,
3712 sizeof (struct dwarf2_per_cu_data));
3713 memset (this_cu, 0, sizeof (*this_cu));
3714 this_cu->offset = offset;
3715 this_cu->length = length + initial_length_size;
3716 this_cu->objfile = objfile;
3717
3718 if (n_comp_units == n_allocated)
3719 {
3720 n_allocated *= 2;
3721 all_comp_units = xrealloc (all_comp_units,
3722 n_allocated
3723 * sizeof (struct dwarf2_per_cu_data *));
3724 }
3725 all_comp_units[n_comp_units++] = this_cu;
3726
3727 info_ptr = info_ptr + this_cu->length;
3728 }
3729
3730 dwarf2_per_objfile->all_comp_units
3731 = obstack_alloc (&objfile->objfile_obstack,
3732 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3733 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3734 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3735 xfree (all_comp_units);
3736 dwarf2_per_objfile->n_comp_units = n_comp_units;
3737 }
3738
3739 /* Process all loaded DIEs for compilation unit CU, starting at
3740 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3741 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3742 DW_AT_ranges). If NEED_PC is set, then this function will set
3743 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3744 and record the covered ranges in the addrmap. */
3745
3746 static void
3747 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3748 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3749 {
3750 struct partial_die_info *pdi;
3751
3752 /* Now, march along the PDI's, descending into ones which have
3753 interesting children but skipping the children of the other ones,
3754 until we reach the end of the compilation unit. */
3755
3756 pdi = first_die;
3757
3758 while (pdi != NULL)
3759 {
3760 fixup_partial_die (pdi, cu);
3761
3762 /* Anonymous namespaces or modules have no name but have interesting
3763 children, so we need to look at them. Ditto for anonymous
3764 enums. */
3765
3766 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3767 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3768 {
3769 switch (pdi->tag)
3770 {
3771 case DW_TAG_subprogram:
3772 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3773 break;
3774 case DW_TAG_constant:
3775 case DW_TAG_variable:
3776 case DW_TAG_typedef:
3777 case DW_TAG_union_type:
3778 if (!pdi->is_declaration)
3779 {
3780 add_partial_symbol (pdi, cu);
3781 }
3782 break;
3783 case DW_TAG_class_type:
3784 case DW_TAG_interface_type:
3785 case DW_TAG_structure_type:
3786 if (!pdi->is_declaration)
3787 {
3788 add_partial_symbol (pdi, cu);
3789 }
3790 break;
3791 case DW_TAG_enumeration_type:
3792 if (!pdi->is_declaration)
3793 add_partial_enumeration (pdi, cu);
3794 break;
3795 case DW_TAG_base_type:
3796 case DW_TAG_subrange_type:
3797 /* File scope base type definitions are added to the partial
3798 symbol table. */
3799 add_partial_symbol (pdi, cu);
3800 break;
3801 case DW_TAG_namespace:
3802 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3803 break;
3804 case DW_TAG_module:
3805 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3806 break;
3807 default:
3808 break;
3809 }
3810 }
3811
3812 /* If the die has a sibling, skip to the sibling. */
3813
3814 pdi = pdi->die_sibling;
3815 }
3816 }
3817
3818 /* Functions used to compute the fully scoped name of a partial DIE.
3819
3820 Normally, this is simple. For C++, the parent DIE's fully scoped
3821 name is concatenated with "::" and the partial DIE's name. For
3822 Java, the same thing occurs except that "." is used instead of "::".
3823 Enumerators are an exception; they use the scope of their parent
3824 enumeration type, i.e. the name of the enumeration type is not
3825 prepended to the enumerator.
3826
3827 There are two complexities. One is DW_AT_specification; in this
3828 case "parent" means the parent of the target of the specification,
3829 instead of the direct parent of the DIE. The other is compilers
3830 which do not emit DW_TAG_namespace; in this case we try to guess
3831 the fully qualified name of structure types from their members'
3832 linkage names. This must be done using the DIE's children rather
3833 than the children of any DW_AT_specification target. We only need
3834 to do this for structures at the top level, i.e. if the target of
3835 any DW_AT_specification (if any; otherwise the DIE itself) does not
3836 have a parent. */
3837
3838 /* Compute the scope prefix associated with PDI's parent, in
3839 compilation unit CU. The result will be allocated on CU's
3840 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3841 field. NULL is returned if no prefix is necessary. */
3842 static char *
3843 partial_die_parent_scope (struct partial_die_info *pdi,
3844 struct dwarf2_cu *cu)
3845 {
3846 char *grandparent_scope;
3847 struct partial_die_info *parent, *real_pdi;
3848
3849 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3850 then this means the parent of the specification DIE. */
3851
3852 real_pdi = pdi;
3853 while (real_pdi->has_specification)
3854 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3855
3856 parent = real_pdi->die_parent;
3857 if (parent == NULL)
3858 return NULL;
3859
3860 if (parent->scope_set)
3861 return parent->scope;
3862
3863 fixup_partial_die (parent, cu);
3864
3865 grandparent_scope = partial_die_parent_scope (parent, cu);
3866
3867 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3868 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3869 Work around this problem here. */
3870 if (cu->language == language_cplus
3871 && parent->tag == DW_TAG_namespace
3872 && strcmp (parent->name, "::") == 0
3873 && grandparent_scope == NULL)
3874 {
3875 parent->scope = NULL;
3876 parent->scope_set = 1;
3877 return NULL;
3878 }
3879
3880 if (parent->tag == DW_TAG_namespace
3881 || parent->tag == DW_TAG_module
3882 || parent->tag == DW_TAG_structure_type
3883 || parent->tag == DW_TAG_class_type
3884 || parent->tag == DW_TAG_interface_type
3885 || parent->tag == DW_TAG_union_type
3886 || parent->tag == DW_TAG_enumeration_type)
3887 {
3888 if (grandparent_scope == NULL)
3889 parent->scope = parent->name;
3890 else
3891 parent->scope = typename_concat (&cu->comp_unit_obstack,
3892 grandparent_scope,
3893 parent->name, 0, cu);
3894 }
3895 else if (parent->tag == DW_TAG_enumerator)
3896 /* Enumerators should not get the name of the enumeration as a prefix. */
3897 parent->scope = grandparent_scope;
3898 else
3899 {
3900 /* FIXME drow/2004-04-01: What should we be doing with
3901 function-local names? For partial symbols, we should probably be
3902 ignoring them. */
3903 complaint (&symfile_complaints,
3904 _("unhandled containing DIE tag %d for DIE at %d"),
3905 parent->tag, pdi->offset);
3906 parent->scope = grandparent_scope;
3907 }
3908
3909 parent->scope_set = 1;
3910 return parent->scope;
3911 }
3912
3913 /* Return the fully scoped name associated with PDI, from compilation unit
3914 CU. The result will be allocated with malloc. */
3915 static char *
3916 partial_die_full_name (struct partial_die_info *pdi,
3917 struct dwarf2_cu *cu)
3918 {
3919 char *parent_scope;
3920
3921 /* If this is a template instantiation, we can not work out the
3922 template arguments from partial DIEs. So, unfortunately, we have
3923 to go through the full DIEs. At least any work we do building
3924 types here will be reused if full symbols are loaded later. */
3925 if (pdi->has_template_arguments)
3926 {
3927 fixup_partial_die (pdi, cu);
3928
3929 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3930 {
3931 struct die_info *die;
3932 struct attribute attr;
3933 struct dwarf2_cu *ref_cu = cu;
3934
3935 attr.name = 0;
3936 attr.form = DW_FORM_ref_addr;
3937 attr.u.addr = pdi->offset;
3938 die = follow_die_ref (NULL, &attr, &ref_cu);
3939
3940 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3941 }
3942 }
3943
3944 parent_scope = partial_die_parent_scope (pdi, cu);
3945 if (parent_scope == NULL)
3946 return NULL;
3947 else
3948 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3949 }
3950
3951 static void
3952 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3953 {
3954 struct objfile *objfile = cu->objfile;
3955 CORE_ADDR addr = 0;
3956 char *actual_name = NULL;
3957 const struct partial_symbol *psym = NULL;
3958 CORE_ADDR baseaddr;
3959 int built_actual_name = 0;
3960
3961 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3962
3963 actual_name = partial_die_full_name (pdi, cu);
3964 if (actual_name)
3965 built_actual_name = 1;
3966
3967 if (actual_name == NULL)
3968 actual_name = pdi->name;
3969
3970 switch (pdi->tag)
3971 {
3972 case DW_TAG_subprogram:
3973 if (pdi->is_external || cu->language == language_ada)
3974 {
3975 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3976 of the global scope. But in Ada, we want to be able to access
3977 nested procedures globally. So all Ada subprograms are stored
3978 in the global scope. */
3979 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3980 mst_text, objfile); */
3981 add_psymbol_to_list (actual_name, strlen (actual_name),
3982 built_actual_name,
3983 VAR_DOMAIN, LOC_BLOCK,
3984 &objfile->global_psymbols,
3985 0, pdi->lowpc + baseaddr,
3986 cu->language, objfile);
3987 }
3988 else
3989 {
3990 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3991 mst_file_text, objfile); */
3992 add_psymbol_to_list (actual_name, strlen (actual_name),
3993 built_actual_name,
3994 VAR_DOMAIN, LOC_BLOCK,
3995 &objfile->static_psymbols,
3996 0, pdi->lowpc + baseaddr,
3997 cu->language, objfile);
3998 }
3999 break;
4000 case DW_TAG_constant:
4001 {
4002 struct psymbol_allocation_list *list;
4003
4004 if (pdi->is_external)
4005 list = &objfile->global_psymbols;
4006 else
4007 list = &objfile->static_psymbols;
4008 add_psymbol_to_list (actual_name, strlen (actual_name),
4009 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4010 list, 0, 0, cu->language, objfile);
4011 }
4012 break;
4013 case DW_TAG_variable:
4014 if (pdi->locdesc)
4015 addr = decode_locdesc (pdi->locdesc, cu);
4016
4017 if (pdi->locdesc
4018 && addr == 0
4019 && !dwarf2_per_objfile->has_section_at_zero)
4020 {
4021 /* A global or static variable may also have been stripped
4022 out by the linker if unused, in which case its address
4023 will be nullified; do not add such variables into partial
4024 symbol table then. */
4025 }
4026 else if (pdi->is_external)
4027 {
4028 /* Global Variable.
4029 Don't enter into the minimal symbol tables as there is
4030 a minimal symbol table entry from the ELF symbols already.
4031 Enter into partial symbol table if it has a location
4032 descriptor or a type.
4033 If the location descriptor is missing, new_symbol will create
4034 a LOC_UNRESOLVED symbol, the address of the variable will then
4035 be determined from the minimal symbol table whenever the variable
4036 is referenced.
4037 The address for the partial symbol table entry is not
4038 used by GDB, but it comes in handy for debugging partial symbol
4039 table building. */
4040
4041 if (pdi->locdesc || pdi->has_type)
4042 add_psymbol_to_list (actual_name, strlen (actual_name),
4043 built_actual_name,
4044 VAR_DOMAIN, LOC_STATIC,
4045 &objfile->global_psymbols,
4046 0, addr + baseaddr,
4047 cu->language, objfile);
4048 }
4049 else
4050 {
4051 /* Static Variable. Skip symbols without location descriptors. */
4052 if (pdi->locdesc == NULL)
4053 {
4054 if (built_actual_name)
4055 xfree (actual_name);
4056 return;
4057 }
4058 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4059 mst_file_data, objfile); */
4060 add_psymbol_to_list (actual_name, strlen (actual_name),
4061 built_actual_name,
4062 VAR_DOMAIN, LOC_STATIC,
4063 &objfile->static_psymbols,
4064 0, addr + baseaddr,
4065 cu->language, objfile);
4066 }
4067 break;
4068 case DW_TAG_typedef:
4069 case DW_TAG_base_type:
4070 case DW_TAG_subrange_type:
4071 add_psymbol_to_list (actual_name, strlen (actual_name),
4072 built_actual_name,
4073 VAR_DOMAIN, LOC_TYPEDEF,
4074 &objfile->static_psymbols,
4075 0, (CORE_ADDR) 0, cu->language, objfile);
4076 break;
4077 case DW_TAG_namespace:
4078 add_psymbol_to_list (actual_name, strlen (actual_name),
4079 built_actual_name,
4080 VAR_DOMAIN, LOC_TYPEDEF,
4081 &objfile->global_psymbols,
4082 0, (CORE_ADDR) 0, cu->language, objfile);
4083 break;
4084 case DW_TAG_class_type:
4085 case DW_TAG_interface_type:
4086 case DW_TAG_structure_type:
4087 case DW_TAG_union_type:
4088 case DW_TAG_enumeration_type:
4089 /* Skip external references. The DWARF standard says in the section
4090 about "Structure, Union, and Class Type Entries": "An incomplete
4091 structure, union or class type is represented by a structure,
4092 union or class entry that does not have a byte size attribute
4093 and that has a DW_AT_declaration attribute." */
4094 if (!pdi->has_byte_size && pdi->is_declaration)
4095 {
4096 if (built_actual_name)
4097 xfree (actual_name);
4098 return;
4099 }
4100
4101 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4102 static vs. global. */
4103 add_psymbol_to_list (actual_name, strlen (actual_name),
4104 built_actual_name,
4105 STRUCT_DOMAIN, LOC_TYPEDEF,
4106 (cu->language == language_cplus
4107 || cu->language == language_java)
4108 ? &objfile->global_psymbols
4109 : &objfile->static_psymbols,
4110 0, (CORE_ADDR) 0, cu->language, objfile);
4111
4112 break;
4113 case DW_TAG_enumerator:
4114 add_psymbol_to_list (actual_name, strlen (actual_name),
4115 built_actual_name,
4116 VAR_DOMAIN, LOC_CONST,
4117 (cu->language == language_cplus
4118 || cu->language == language_java)
4119 ? &objfile->global_psymbols
4120 : &objfile->static_psymbols,
4121 0, (CORE_ADDR) 0, cu->language, objfile);
4122 break;
4123 default:
4124 break;
4125 }
4126
4127 if (built_actual_name)
4128 xfree (actual_name);
4129 }
4130
4131 /* Read a partial die corresponding to a namespace; also, add a symbol
4132 corresponding to that namespace to the symbol table. NAMESPACE is
4133 the name of the enclosing namespace. */
4134
4135 static void
4136 add_partial_namespace (struct partial_die_info *pdi,
4137 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4138 int need_pc, struct dwarf2_cu *cu)
4139 {
4140 /* Add a symbol for the namespace. */
4141
4142 add_partial_symbol (pdi, cu);
4143
4144 /* Now scan partial symbols in that namespace. */
4145
4146 if (pdi->has_children)
4147 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4148 }
4149
4150 /* Read a partial die corresponding to a Fortran module. */
4151
4152 static void
4153 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4154 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4155 {
4156 /* Now scan partial symbols in that module. */
4157
4158 if (pdi->has_children)
4159 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4160 }
4161
4162 /* Read a partial die corresponding to a subprogram and create a partial
4163 symbol for that subprogram. When the CU language allows it, this
4164 routine also defines a partial symbol for each nested subprogram
4165 that this subprogram contains.
4166
4167 DIE my also be a lexical block, in which case we simply search
4168 recursively for suprograms defined inside that lexical block.
4169 Again, this is only performed when the CU language allows this
4170 type of definitions. */
4171
4172 static void
4173 add_partial_subprogram (struct partial_die_info *pdi,
4174 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4175 int need_pc, struct dwarf2_cu *cu)
4176 {
4177 if (pdi->tag == DW_TAG_subprogram)
4178 {
4179 if (pdi->has_pc_info)
4180 {
4181 if (pdi->lowpc < *lowpc)
4182 *lowpc = pdi->lowpc;
4183 if (pdi->highpc > *highpc)
4184 *highpc = pdi->highpc;
4185 if (need_pc)
4186 {
4187 CORE_ADDR baseaddr;
4188 struct objfile *objfile = cu->objfile;
4189
4190 baseaddr = ANOFFSET (objfile->section_offsets,
4191 SECT_OFF_TEXT (objfile));
4192 addrmap_set_empty (objfile->psymtabs_addrmap,
4193 pdi->lowpc + baseaddr,
4194 pdi->highpc - 1 + baseaddr,
4195 cu->per_cu->v.psymtab);
4196 }
4197 if (!pdi->is_declaration)
4198 /* Ignore subprogram DIEs that do not have a name, they are
4199 illegal. Do not emit a complaint at this point, we will
4200 do so when we convert this psymtab into a symtab. */
4201 if (pdi->name)
4202 add_partial_symbol (pdi, cu);
4203 }
4204 }
4205
4206 if (! pdi->has_children)
4207 return;
4208
4209 if (cu->language == language_ada)
4210 {
4211 pdi = pdi->die_child;
4212 while (pdi != NULL)
4213 {
4214 fixup_partial_die (pdi, cu);
4215 if (pdi->tag == DW_TAG_subprogram
4216 || pdi->tag == DW_TAG_lexical_block)
4217 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4218 pdi = pdi->die_sibling;
4219 }
4220 }
4221 }
4222
4223 /* Read a partial die corresponding to an enumeration type. */
4224
4225 static void
4226 add_partial_enumeration (struct partial_die_info *enum_pdi,
4227 struct dwarf2_cu *cu)
4228 {
4229 struct partial_die_info *pdi;
4230
4231 if (enum_pdi->name != NULL)
4232 add_partial_symbol (enum_pdi, cu);
4233
4234 pdi = enum_pdi->die_child;
4235 while (pdi)
4236 {
4237 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4238 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4239 else
4240 add_partial_symbol (pdi, cu);
4241 pdi = pdi->die_sibling;
4242 }
4243 }
4244
4245 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4246 Return the corresponding abbrev, or NULL if the number is zero (indicating
4247 an empty DIE). In either case *BYTES_READ will be set to the length of
4248 the initial number. */
4249
4250 static struct abbrev_info *
4251 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4252 struct dwarf2_cu *cu)
4253 {
4254 bfd *abfd = cu->objfile->obfd;
4255 unsigned int abbrev_number;
4256 struct abbrev_info *abbrev;
4257
4258 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4259
4260 if (abbrev_number == 0)
4261 return NULL;
4262
4263 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4264 if (!abbrev)
4265 {
4266 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4267 abbrev_number, bfd_get_filename (abfd));
4268 }
4269
4270 return abbrev;
4271 }
4272
4273 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4274 Returns a pointer to the end of a series of DIEs, terminated by an empty
4275 DIE. Any children of the skipped DIEs will also be skipped. */
4276
4277 static gdb_byte *
4278 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4279 {
4280 struct abbrev_info *abbrev;
4281 unsigned int bytes_read;
4282
4283 while (1)
4284 {
4285 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4286 if (abbrev == NULL)
4287 return info_ptr + bytes_read;
4288 else
4289 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4290 }
4291 }
4292
4293 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4294 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4295 abbrev corresponding to that skipped uleb128 should be passed in
4296 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4297 children. */
4298
4299 static gdb_byte *
4300 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4301 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4302 {
4303 unsigned int bytes_read;
4304 struct attribute attr;
4305 bfd *abfd = cu->objfile->obfd;
4306 unsigned int form, i;
4307
4308 for (i = 0; i < abbrev->num_attrs; i++)
4309 {
4310 /* The only abbrev we care about is DW_AT_sibling. */
4311 if (abbrev->attrs[i].name == DW_AT_sibling)
4312 {
4313 read_attribute (&attr, &abbrev->attrs[i],
4314 abfd, info_ptr, cu);
4315 if (attr.form == DW_FORM_ref_addr)
4316 complaint (&symfile_complaints,
4317 _("ignoring absolute DW_AT_sibling"));
4318 else
4319 return buffer + dwarf2_get_ref_die_offset (&attr);
4320 }
4321
4322 /* If it isn't DW_AT_sibling, skip this attribute. */
4323 form = abbrev->attrs[i].form;
4324 skip_attribute:
4325 switch (form)
4326 {
4327 case DW_FORM_ref_addr:
4328 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4329 and later it is offset sized. */
4330 if (cu->header.version == 2)
4331 info_ptr += cu->header.addr_size;
4332 else
4333 info_ptr += cu->header.offset_size;
4334 break;
4335 case DW_FORM_addr:
4336 info_ptr += cu->header.addr_size;
4337 break;
4338 case DW_FORM_data1:
4339 case DW_FORM_ref1:
4340 case DW_FORM_flag:
4341 info_ptr += 1;
4342 break;
4343 case DW_FORM_flag_present:
4344 break;
4345 case DW_FORM_data2:
4346 case DW_FORM_ref2:
4347 info_ptr += 2;
4348 break;
4349 case DW_FORM_data4:
4350 case DW_FORM_ref4:
4351 info_ptr += 4;
4352 break;
4353 case DW_FORM_data8:
4354 case DW_FORM_ref8:
4355 case DW_FORM_ref_sig8:
4356 info_ptr += 8;
4357 break;
4358 case DW_FORM_string:
4359 read_direct_string (abfd, info_ptr, &bytes_read);
4360 info_ptr += bytes_read;
4361 break;
4362 case DW_FORM_sec_offset:
4363 case DW_FORM_strp:
4364 info_ptr += cu->header.offset_size;
4365 break;
4366 case DW_FORM_exprloc:
4367 case DW_FORM_block:
4368 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4369 info_ptr += bytes_read;
4370 break;
4371 case DW_FORM_block1:
4372 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4373 break;
4374 case DW_FORM_block2:
4375 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4376 break;
4377 case DW_FORM_block4:
4378 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4379 break;
4380 case DW_FORM_sdata:
4381 case DW_FORM_udata:
4382 case DW_FORM_ref_udata:
4383 info_ptr = skip_leb128 (abfd, info_ptr);
4384 break;
4385 case DW_FORM_indirect:
4386 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4387 info_ptr += bytes_read;
4388 /* We need to continue parsing from here, so just go back to
4389 the top. */
4390 goto skip_attribute;
4391
4392 default:
4393 error (_("Dwarf Error: Cannot handle %s "
4394 "in DWARF reader [in module %s]"),
4395 dwarf_form_name (form),
4396 bfd_get_filename (abfd));
4397 }
4398 }
4399
4400 if (abbrev->has_children)
4401 return skip_children (buffer, info_ptr, cu);
4402 else
4403 return info_ptr;
4404 }
4405
4406 /* Locate ORIG_PDI's sibling.
4407 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4408 in BUFFER. */
4409
4410 static gdb_byte *
4411 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4412 gdb_byte *buffer, gdb_byte *info_ptr,
4413 bfd *abfd, struct dwarf2_cu *cu)
4414 {
4415 /* Do we know the sibling already? */
4416
4417 if (orig_pdi->sibling)
4418 return orig_pdi->sibling;
4419
4420 /* Are there any children to deal with? */
4421
4422 if (!orig_pdi->has_children)
4423 return info_ptr;
4424
4425 /* Skip the children the long way. */
4426
4427 return skip_children (buffer, info_ptr, cu);
4428 }
4429
4430 /* Expand this partial symbol table into a full symbol table. */
4431
4432 static void
4433 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4434 {
4435 if (pst != NULL)
4436 {
4437 if (pst->readin)
4438 {
4439 warning (_("bug: psymtab for %s is already read in."),
4440 pst->filename);
4441 }
4442 else
4443 {
4444 if (info_verbose)
4445 {
4446 printf_filtered (_("Reading in symbols for %s..."),
4447 pst->filename);
4448 gdb_flush (gdb_stdout);
4449 }
4450
4451 /* Restore our global data. */
4452 dwarf2_per_objfile = objfile_data (pst->objfile,
4453 dwarf2_objfile_data_key);
4454
4455 /* If this psymtab is constructed from a debug-only objfile, the
4456 has_section_at_zero flag will not necessarily be correct. We
4457 can get the correct value for this flag by looking at the data
4458 associated with the (presumably stripped) associated objfile. */
4459 if (pst->objfile->separate_debug_objfile_backlink)
4460 {
4461 struct dwarf2_per_objfile *dpo_backlink
4462 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4463 dwarf2_objfile_data_key);
4464
4465 dwarf2_per_objfile->has_section_at_zero
4466 = dpo_backlink->has_section_at_zero;
4467 }
4468
4469 dwarf2_per_objfile->reading_partial_symbols = 0;
4470
4471 psymtab_to_symtab_1 (pst);
4472
4473 /* Finish up the debug error message. */
4474 if (info_verbose)
4475 printf_filtered (_("done.\n"));
4476 }
4477 }
4478 }
4479
4480 /* Add PER_CU to the queue. */
4481
4482 static void
4483 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4484 {
4485 struct dwarf2_queue_item *item;
4486
4487 per_cu->queued = 1;
4488 item = xmalloc (sizeof (*item));
4489 item->per_cu = per_cu;
4490 item->next = NULL;
4491
4492 if (dwarf2_queue == NULL)
4493 dwarf2_queue = item;
4494 else
4495 dwarf2_queue_tail->next = item;
4496
4497 dwarf2_queue_tail = item;
4498 }
4499
4500 /* Process the queue. */
4501
4502 static void
4503 process_queue (struct objfile *objfile)
4504 {
4505 struct dwarf2_queue_item *item, *next_item;
4506
4507 /* The queue starts out with one item, but following a DIE reference
4508 may load a new CU, adding it to the end of the queue. */
4509 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4510 {
4511 if (dwarf2_per_objfile->using_index
4512 ? !item->per_cu->v.quick->symtab
4513 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4514 process_full_comp_unit (item->per_cu);
4515
4516 item->per_cu->queued = 0;
4517 next_item = item->next;
4518 xfree (item);
4519 }
4520
4521 dwarf2_queue_tail = NULL;
4522 }
4523
4524 /* Free all allocated queue entries. This function only releases anything if
4525 an error was thrown; if the queue was processed then it would have been
4526 freed as we went along. */
4527
4528 static void
4529 dwarf2_release_queue (void *dummy)
4530 {
4531 struct dwarf2_queue_item *item, *last;
4532
4533 item = dwarf2_queue;
4534 while (item)
4535 {
4536 /* Anything still marked queued is likely to be in an
4537 inconsistent state, so discard it. */
4538 if (item->per_cu->queued)
4539 {
4540 if (item->per_cu->cu != NULL)
4541 free_one_cached_comp_unit (item->per_cu->cu);
4542 item->per_cu->queued = 0;
4543 }
4544
4545 last = item;
4546 item = item->next;
4547 xfree (last);
4548 }
4549
4550 dwarf2_queue = dwarf2_queue_tail = NULL;
4551 }
4552
4553 /* Read in full symbols for PST, and anything it depends on. */
4554
4555 static void
4556 psymtab_to_symtab_1 (struct partial_symtab *pst)
4557 {
4558 struct dwarf2_per_cu_data *per_cu;
4559 struct cleanup *back_to;
4560 int i;
4561
4562 for (i = 0; i < pst->number_of_dependencies; i++)
4563 if (!pst->dependencies[i]->readin)
4564 {
4565 /* Inform about additional files that need to be read in. */
4566 if (info_verbose)
4567 {
4568 /* FIXME: i18n: Need to make this a single string. */
4569 fputs_filtered (" ", gdb_stdout);
4570 wrap_here ("");
4571 fputs_filtered ("and ", gdb_stdout);
4572 wrap_here ("");
4573 printf_filtered ("%s...", pst->dependencies[i]->filename);
4574 wrap_here (""); /* Flush output. */
4575 gdb_flush (gdb_stdout);
4576 }
4577 psymtab_to_symtab_1 (pst->dependencies[i]);
4578 }
4579
4580 per_cu = pst->read_symtab_private;
4581
4582 if (per_cu == NULL)
4583 {
4584 /* It's an include file, no symbols to read for it.
4585 Everything is in the parent symtab. */
4586 pst->readin = 1;
4587 return;
4588 }
4589
4590 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4591 }
4592
4593 /* Load the DIEs associated with PER_CU into memory. */
4594
4595 static void
4596 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4597 struct objfile *objfile)
4598 {
4599 bfd *abfd = objfile->obfd;
4600 struct dwarf2_cu *cu;
4601 unsigned int offset;
4602 gdb_byte *info_ptr, *beg_of_comp_unit;
4603 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4604 struct attribute *attr;
4605 int read_cu = 0;
4606
4607 gdb_assert (! per_cu->debug_type_section);
4608
4609 /* Set local variables from the partial symbol table info. */
4610 offset = per_cu->offset;
4611
4612 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4613 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4614 beg_of_comp_unit = info_ptr;
4615
4616 if (per_cu->cu == NULL)
4617 {
4618 cu = xmalloc (sizeof (*cu));
4619 init_one_comp_unit (cu, objfile);
4620
4621 read_cu = 1;
4622
4623 /* If an error occurs while loading, release our storage. */
4624 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4625
4626 /* Read in the comp_unit header. */
4627 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4628
4629 /* Complete the cu_header. */
4630 cu->header.offset = offset;
4631 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4632
4633 /* Read the abbrevs for this compilation unit. */
4634 dwarf2_read_abbrevs (abfd, cu);
4635 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4636
4637 /* Link this compilation unit into the compilation unit tree. */
4638 per_cu->cu = cu;
4639 cu->per_cu = per_cu;
4640
4641 /* Link this CU into read_in_chain. */
4642 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4643 dwarf2_per_objfile->read_in_chain = per_cu;
4644 }
4645 else
4646 {
4647 cu = per_cu->cu;
4648 info_ptr += cu->header.first_die_offset;
4649 }
4650
4651 cu->dies = read_comp_unit (info_ptr, cu);
4652
4653 /* We try not to read any attributes in this function, because not
4654 all objfiles needed for references have been loaded yet, and symbol
4655 table processing isn't initialized. But we have to set the CU language,
4656 or we won't be able to build types correctly. */
4657 prepare_one_comp_unit (cu, cu->dies);
4658
4659 /* Similarly, if we do not read the producer, we can not apply
4660 producer-specific interpretation. */
4661 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4662 if (attr)
4663 cu->producer = DW_STRING (attr);
4664
4665 if (read_cu)
4666 {
4667 do_cleanups (free_abbrevs_cleanup);
4668
4669 /* We've successfully allocated this compilation unit. Let our
4670 caller clean it up when finished with it. */
4671 discard_cleanups (free_cu_cleanup);
4672 }
4673 }
4674
4675 /* Add a DIE to the delayed physname list. */
4676
4677 static void
4678 add_to_method_list (struct type *type, int fnfield_index, int index,
4679 const char *name, struct die_info *die,
4680 struct dwarf2_cu *cu)
4681 {
4682 struct delayed_method_info mi;
4683 mi.type = type;
4684 mi.fnfield_index = fnfield_index;
4685 mi.index = index;
4686 mi.name = name;
4687 mi.die = die;
4688 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4689 }
4690
4691 /* A cleanup for freeing the delayed method list. */
4692
4693 static void
4694 free_delayed_list (void *ptr)
4695 {
4696 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4697 if (cu->method_list != NULL)
4698 {
4699 VEC_free (delayed_method_info, cu->method_list);
4700 cu->method_list = NULL;
4701 }
4702 }
4703
4704 /* Compute the physnames of any methods on the CU's method list.
4705
4706 The computation of method physnames is delayed in order to avoid the
4707 (bad) condition that one of the method's formal parameters is of an as yet
4708 incomplete type. */
4709
4710 static void
4711 compute_delayed_physnames (struct dwarf2_cu *cu)
4712 {
4713 int i;
4714 struct delayed_method_info *mi;
4715 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4716 {
4717 const char *physname;
4718 struct fn_fieldlist *fn_flp
4719 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4720 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4721 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4722 }
4723 }
4724
4725 /* Check for GCC >= 4.x. Return minor version (x) of 4.x in such case. If it
4726 is not GCC or it is GCC older than 4.x return -1. If it is GCC 5.x or
4727 higher return INT_MAX. */
4728
4729 static int
4730 producer_is_gcc_ge_4 (struct dwarf2_cu *cu)
4731 {
4732 const char *cs;
4733 int major, minor;
4734
4735 if (cu->producer == NULL)
4736 {
4737 /* For unknown compilers expect their behavior is not compliant. For GCC
4738 this case can also happen for -gdwarf-4 type units supported since
4739 gcc-4.5. */
4740
4741 return -1;
4742 }
4743
4744 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4745
4746 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4747 {
4748 /* For non-GCC compilers expect their behavior is not compliant. */
4749
4750 return -1;
4751 }
4752 cs = &cu->producer[strlen ("GNU ")];
4753 while (*cs && !isdigit (*cs))
4754 cs++;
4755 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4756 {
4757 /* Not recognized as GCC. */
4758
4759 return -1;
4760 }
4761
4762 if (major < 4)
4763 return -1;
4764 if (major > 4)
4765 return INT_MAX;
4766 return minor;
4767 }
4768
4769 /* Generate full symbol information for PST and CU, whose DIEs have
4770 already been loaded into memory. */
4771
4772 static void
4773 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4774 {
4775 struct dwarf2_cu *cu = per_cu->cu;
4776 struct objfile *objfile = per_cu->objfile;
4777 CORE_ADDR lowpc, highpc;
4778 struct symtab *symtab;
4779 struct cleanup *back_to, *delayed_list_cleanup;
4780 CORE_ADDR baseaddr;
4781
4782 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4783
4784 buildsym_init ();
4785 back_to = make_cleanup (really_free_pendings, NULL);
4786 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4787
4788 cu->list_in_scope = &file_symbols;
4789
4790 /* Do line number decoding in read_file_scope () */
4791 process_die (cu->dies, cu);
4792
4793 /* Now that we have processed all the DIEs in the CU, all the types
4794 should be complete, and it should now be safe to compute all of the
4795 physnames. */
4796 compute_delayed_physnames (cu);
4797 do_cleanups (delayed_list_cleanup);
4798
4799 /* Some compilers don't define a DW_AT_high_pc attribute for the
4800 compilation unit. If the DW_AT_high_pc is missing, synthesize
4801 it, by scanning the DIE's below the compilation unit. */
4802 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4803
4804 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4805
4806 if (symtab != NULL)
4807 {
4808 int gcc_4_minor = producer_is_gcc_ge_4 (cu);
4809
4810 /* Set symtab language to language from DW_AT_language. If the
4811 compilation is from a C file generated by language preprocessors, do
4812 not set the language if it was already deduced by start_subfile. */
4813 if (!(cu->language == language_c && symtab->language != language_c))
4814 symtab->language = cu->language;
4815
4816 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4817 produce DW_AT_location with location lists but it can be possibly
4818 invalid without -fvar-tracking.
4819
4820 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4821 needed, it would be wrong due to missing DW_AT_producer there.
4822
4823 Still one can confuse GDB by using non-standard GCC compilation
4824 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4825 */
4826 if (cu->has_loclist && gcc_4_minor >= 0)
4827 symtab->locations_valid = 1;
4828
4829 if (gcc_4_minor >= 5)
4830 symtab->epilogue_unwind_valid = 1;
4831 }
4832
4833 if (dwarf2_per_objfile->using_index)
4834 per_cu->v.quick->symtab = symtab;
4835 else
4836 {
4837 struct partial_symtab *pst = per_cu->v.psymtab;
4838 pst->symtab = symtab;
4839 pst->readin = 1;
4840 }
4841
4842 do_cleanups (back_to);
4843 }
4844
4845 /* Process a die and its children. */
4846
4847 static void
4848 process_die (struct die_info *die, struct dwarf2_cu *cu)
4849 {
4850 switch (die->tag)
4851 {
4852 case DW_TAG_padding:
4853 break;
4854 case DW_TAG_compile_unit:
4855 read_file_scope (die, cu);
4856 break;
4857 case DW_TAG_type_unit:
4858 read_type_unit_scope (die, cu);
4859 break;
4860 case DW_TAG_subprogram:
4861 case DW_TAG_inlined_subroutine:
4862 read_func_scope (die, cu);
4863 break;
4864 case DW_TAG_lexical_block:
4865 case DW_TAG_try_block:
4866 case DW_TAG_catch_block:
4867 read_lexical_block_scope (die, cu);
4868 break;
4869 case DW_TAG_class_type:
4870 case DW_TAG_interface_type:
4871 case DW_TAG_structure_type:
4872 case DW_TAG_union_type:
4873 process_structure_scope (die, cu);
4874 break;
4875 case DW_TAG_enumeration_type:
4876 process_enumeration_scope (die, cu);
4877 break;
4878
4879 /* These dies have a type, but processing them does not create
4880 a symbol or recurse to process the children. Therefore we can
4881 read them on-demand through read_type_die. */
4882 case DW_TAG_subroutine_type:
4883 case DW_TAG_set_type:
4884 case DW_TAG_array_type:
4885 case DW_TAG_pointer_type:
4886 case DW_TAG_ptr_to_member_type:
4887 case DW_TAG_reference_type:
4888 case DW_TAG_string_type:
4889 break;
4890
4891 case DW_TAG_base_type:
4892 case DW_TAG_subrange_type:
4893 case DW_TAG_typedef:
4894 /* Add a typedef symbol for the type definition, if it has a
4895 DW_AT_name. */
4896 new_symbol (die, read_type_die (die, cu), cu);
4897 break;
4898 case DW_TAG_common_block:
4899 read_common_block (die, cu);
4900 break;
4901 case DW_TAG_common_inclusion:
4902 break;
4903 case DW_TAG_namespace:
4904 processing_has_namespace_info = 1;
4905 read_namespace (die, cu);
4906 break;
4907 case DW_TAG_module:
4908 processing_has_namespace_info = 1;
4909 read_module (die, cu);
4910 break;
4911 case DW_TAG_imported_declaration:
4912 case DW_TAG_imported_module:
4913 processing_has_namespace_info = 1;
4914 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4915 || cu->language != language_fortran))
4916 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4917 dwarf_tag_name (die->tag));
4918 read_import_statement (die, cu);
4919 break;
4920 default:
4921 new_symbol (die, NULL, cu);
4922 break;
4923 }
4924 }
4925
4926 /* A helper function for dwarf2_compute_name which determines whether DIE
4927 needs to have the name of the scope prepended to the name listed in the
4928 die. */
4929
4930 static int
4931 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4932 {
4933 struct attribute *attr;
4934
4935 switch (die->tag)
4936 {
4937 case DW_TAG_namespace:
4938 case DW_TAG_typedef:
4939 case DW_TAG_class_type:
4940 case DW_TAG_interface_type:
4941 case DW_TAG_structure_type:
4942 case DW_TAG_union_type:
4943 case DW_TAG_enumeration_type:
4944 case DW_TAG_enumerator:
4945 case DW_TAG_subprogram:
4946 case DW_TAG_member:
4947 return 1;
4948
4949 case DW_TAG_variable:
4950 case DW_TAG_constant:
4951 /* We only need to prefix "globally" visible variables. These include
4952 any variable marked with DW_AT_external or any variable that
4953 lives in a namespace. [Variables in anonymous namespaces
4954 require prefixing, but they are not DW_AT_external.] */
4955
4956 if (dwarf2_attr (die, DW_AT_specification, cu))
4957 {
4958 struct dwarf2_cu *spec_cu = cu;
4959
4960 return die_needs_namespace (die_specification (die, &spec_cu),
4961 spec_cu);
4962 }
4963
4964 attr = dwarf2_attr (die, DW_AT_external, cu);
4965 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4966 && die->parent->tag != DW_TAG_module)
4967 return 0;
4968 /* A variable in a lexical block of some kind does not need a
4969 namespace, even though in C++ such variables may be external
4970 and have a mangled name. */
4971 if (die->parent->tag == DW_TAG_lexical_block
4972 || die->parent->tag == DW_TAG_try_block
4973 || die->parent->tag == DW_TAG_catch_block
4974 || die->parent->tag == DW_TAG_subprogram)
4975 return 0;
4976 return 1;
4977
4978 default:
4979 return 0;
4980 }
4981 }
4982
4983 /* Retrieve the last character from a mem_file. */
4984
4985 static void
4986 do_ui_file_peek_last (void *object, const char *buffer, long length)
4987 {
4988 char *last_char_p = (char *) object;
4989
4990 if (length > 0)
4991 *last_char_p = buffer[length - 1];
4992 }
4993
4994 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4995 compute the physname for the object, which include a method's
4996 formal parameters (C++/Java) and return type (Java).
4997
4998 For Ada, return the DIE's linkage name rather than the fully qualified
4999 name. PHYSNAME is ignored..
5000
5001 The result is allocated on the objfile_obstack and canonicalized. */
5002
5003 static const char *
5004 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5005 int physname)
5006 {
5007 if (name == NULL)
5008 name = dwarf2_name (die, cu);
5009
5010 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5011 compute it by typename_concat inside GDB. */
5012 if (cu->language == language_ada
5013 || (cu->language == language_fortran && physname))
5014 {
5015 /* For Ada unit, we prefer the linkage name over the name, as
5016 the former contains the exported name, which the user expects
5017 to be able to reference. Ideally, we want the user to be able
5018 to reference this entity using either natural or linkage name,
5019 but we haven't started looking at this enhancement yet. */
5020 struct attribute *attr;
5021
5022 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5023 if (attr == NULL)
5024 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5025 if (attr && DW_STRING (attr))
5026 return DW_STRING (attr);
5027 }
5028
5029 /* These are the only languages we know how to qualify names in. */
5030 if (name != NULL
5031 && (cu->language == language_cplus || cu->language == language_java
5032 || cu->language == language_fortran))
5033 {
5034 if (die_needs_namespace (die, cu))
5035 {
5036 long length;
5037 char *prefix;
5038 struct ui_file *buf;
5039
5040 prefix = determine_prefix (die, cu);
5041 buf = mem_fileopen ();
5042 if (*prefix != '\0')
5043 {
5044 char *prefixed_name = typename_concat (NULL, prefix, name,
5045 physname, cu);
5046
5047 fputs_unfiltered (prefixed_name, buf);
5048 xfree (prefixed_name);
5049 }
5050 else
5051 fputs_unfiltered (name, buf);
5052
5053 /* Template parameters may be specified in the DIE's DW_AT_name, or
5054 as children with DW_TAG_template_type_param or
5055 DW_TAG_value_type_param. If the latter, add them to the name
5056 here. If the name already has template parameters, then
5057 skip this step; some versions of GCC emit both, and
5058 it is more efficient to use the pre-computed name.
5059
5060 Something to keep in mind about this process: it is very
5061 unlikely, or in some cases downright impossible, to produce
5062 something that will match the mangled name of a function.
5063 If the definition of the function has the same debug info,
5064 we should be able to match up with it anyway. But fallbacks
5065 using the minimal symbol, for instance to find a method
5066 implemented in a stripped copy of libstdc++, will not work.
5067 If we do not have debug info for the definition, we will have to
5068 match them up some other way.
5069
5070 When we do name matching there is a related problem with function
5071 templates; two instantiated function templates are allowed to
5072 differ only by their return types, which we do not add here. */
5073
5074 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5075 {
5076 struct attribute *attr;
5077 struct die_info *child;
5078 int first = 1;
5079
5080 die->building_fullname = 1;
5081
5082 for (child = die->child; child != NULL; child = child->sibling)
5083 {
5084 struct type *type;
5085 long value;
5086 gdb_byte *bytes;
5087 struct dwarf2_locexpr_baton *baton;
5088 struct value *v;
5089
5090 if (child->tag != DW_TAG_template_type_param
5091 && child->tag != DW_TAG_template_value_param)
5092 continue;
5093
5094 if (first)
5095 {
5096 fputs_unfiltered ("<", buf);
5097 first = 0;
5098 }
5099 else
5100 fputs_unfiltered (", ", buf);
5101
5102 attr = dwarf2_attr (child, DW_AT_type, cu);
5103 if (attr == NULL)
5104 {
5105 complaint (&symfile_complaints,
5106 _("template parameter missing DW_AT_type"));
5107 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5108 continue;
5109 }
5110 type = die_type (child, cu);
5111
5112 if (child->tag == DW_TAG_template_type_param)
5113 {
5114 c_print_type (type, "", buf, -1, 0);
5115 continue;
5116 }
5117
5118 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5119 if (attr == NULL)
5120 {
5121 complaint (&symfile_complaints,
5122 _("template parameter missing "
5123 "DW_AT_const_value"));
5124 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5125 continue;
5126 }
5127
5128 dwarf2_const_value_attr (attr, type, name,
5129 &cu->comp_unit_obstack, cu,
5130 &value, &bytes, &baton);
5131
5132 if (TYPE_NOSIGN (type))
5133 /* GDB prints characters as NUMBER 'CHAR'. If that's
5134 changed, this can use value_print instead. */
5135 c_printchar (value, type, buf);
5136 else
5137 {
5138 struct value_print_options opts;
5139
5140 if (baton != NULL)
5141 v = dwarf2_evaluate_loc_desc (type, NULL,
5142 baton->data,
5143 baton->size,
5144 baton->per_cu);
5145 else if (bytes != NULL)
5146 {
5147 v = allocate_value (type);
5148 memcpy (value_contents_writeable (v), bytes,
5149 TYPE_LENGTH (type));
5150 }
5151 else
5152 v = value_from_longest (type, value);
5153
5154 /* Specify decimal so that we do not depend on
5155 the radix. */
5156 get_formatted_print_options (&opts, 'd');
5157 opts.raw = 1;
5158 value_print (v, buf, &opts);
5159 release_value (v);
5160 value_free (v);
5161 }
5162 }
5163
5164 die->building_fullname = 0;
5165
5166 if (!first)
5167 {
5168 /* Close the argument list, with a space if necessary
5169 (nested templates). */
5170 char last_char = '\0';
5171 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5172 if (last_char == '>')
5173 fputs_unfiltered (" >", buf);
5174 else
5175 fputs_unfiltered (">", buf);
5176 }
5177 }
5178
5179 /* For Java and C++ methods, append formal parameter type
5180 information, if PHYSNAME. */
5181
5182 if (physname && die->tag == DW_TAG_subprogram
5183 && (cu->language == language_cplus
5184 || cu->language == language_java))
5185 {
5186 struct type *type = read_type_die (die, cu);
5187
5188 c_type_print_args (type, buf, 1, cu->language);
5189
5190 if (cu->language == language_java)
5191 {
5192 /* For java, we must append the return type to method
5193 names. */
5194 if (die->tag == DW_TAG_subprogram)
5195 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5196 0, 0);
5197 }
5198 else if (cu->language == language_cplus)
5199 {
5200 /* Assume that an artificial first parameter is
5201 "this", but do not crash if it is not. RealView
5202 marks unnamed (and thus unused) parameters as
5203 artificial; there is no way to differentiate
5204 the two cases. */
5205 if (TYPE_NFIELDS (type) > 0
5206 && TYPE_FIELD_ARTIFICIAL (type, 0)
5207 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5208 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5209 0))))
5210 fputs_unfiltered (" const", buf);
5211 }
5212 }
5213
5214 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5215 &length);
5216 ui_file_delete (buf);
5217
5218 if (cu->language == language_cplus)
5219 {
5220 char *cname
5221 = dwarf2_canonicalize_name (name, cu,
5222 &cu->objfile->objfile_obstack);
5223
5224 if (cname != NULL)
5225 name = cname;
5226 }
5227 }
5228 }
5229
5230 return name;
5231 }
5232
5233 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5234 If scope qualifiers are appropriate they will be added. The result
5235 will be allocated on the objfile_obstack, or NULL if the DIE does
5236 not have a name. NAME may either be from a previous call to
5237 dwarf2_name or NULL.
5238
5239 The output string will be canonicalized (if C++/Java). */
5240
5241 static const char *
5242 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5243 {
5244 return dwarf2_compute_name (name, die, cu, 0);
5245 }
5246
5247 /* Construct a physname for the given DIE in CU. NAME may either be
5248 from a previous call to dwarf2_name or NULL. The result will be
5249 allocated on the objfile_objstack or NULL if the DIE does not have a
5250 name.
5251
5252 The output string will be canonicalized (if C++/Java). */
5253
5254 static const char *
5255 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5256 {
5257 struct attribute *attr;
5258 const char *retval, *mangled = NULL, *canon = NULL;
5259 struct cleanup *back_to;
5260 int need_copy = 1;
5261
5262 /* In this case dwarf2_compute_name is just a shortcut not building anything
5263 on its own. */
5264 if (!die_needs_namespace (die, cu))
5265 return dwarf2_compute_name (name, die, cu, 1);
5266
5267 back_to = make_cleanup (null_cleanup, NULL);
5268
5269 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5270 if (!attr)
5271 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5272
5273 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5274 has computed. */
5275 if (attr && DW_STRING (attr))
5276 {
5277 char *demangled;
5278
5279 mangled = DW_STRING (attr);
5280
5281 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5282 type. It is easier for GDB users to search for such functions as
5283 `name(params)' than `long name(params)'. In such case the minimal
5284 symbol names do not match the full symbol names but for template
5285 functions there is never a need to look up their definition from their
5286 declaration so the only disadvantage remains the minimal symbol
5287 variant `long name(params)' does not have the proper inferior type.
5288 */
5289
5290 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5291 | (cu->language == language_java
5292 ? DMGL_JAVA | DMGL_RET_POSTFIX
5293 : DMGL_RET_DROP)));
5294 if (demangled)
5295 {
5296 make_cleanup (xfree, demangled);
5297 canon = demangled;
5298 }
5299 else
5300 {
5301 canon = mangled;
5302 need_copy = 0;
5303 }
5304 }
5305
5306 if (canon == NULL || check_physname)
5307 {
5308 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5309
5310 if (canon != NULL && strcmp (physname, canon) != 0)
5311 {
5312 /* It may not mean a bug in GDB. The compiler could also
5313 compute DW_AT_linkage_name incorrectly. But in such case
5314 GDB would need to be bug-to-bug compatible. */
5315
5316 complaint (&symfile_complaints,
5317 _("Computed physname <%s> does not match demangled <%s> "
5318 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5319 physname, canon, mangled, die->offset, cu->objfile->name);
5320
5321 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5322 is available here - over computed PHYSNAME. It is safer
5323 against both buggy GDB and buggy compilers. */
5324
5325 retval = canon;
5326 }
5327 else
5328 {
5329 retval = physname;
5330 need_copy = 0;
5331 }
5332 }
5333 else
5334 retval = canon;
5335
5336 if (need_copy)
5337 retval = obsavestring (retval, strlen (retval),
5338 &cu->objfile->objfile_obstack);
5339
5340 do_cleanups (back_to);
5341 return retval;
5342 }
5343
5344 /* Read the import statement specified by the given die and record it. */
5345
5346 static void
5347 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5348 {
5349 struct attribute *import_attr;
5350 struct die_info *imported_die, *child_die;
5351 struct dwarf2_cu *imported_cu;
5352 const char *imported_name;
5353 const char *imported_name_prefix;
5354 const char *canonical_name;
5355 const char *import_alias;
5356 const char *imported_declaration = NULL;
5357 const char *import_prefix;
5358 VEC (const_char_ptr) *excludes = NULL;
5359 struct cleanup *cleanups;
5360
5361 char *temp;
5362
5363 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5364 if (import_attr == NULL)
5365 {
5366 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5367 dwarf_tag_name (die->tag));
5368 return;
5369 }
5370
5371 imported_cu = cu;
5372 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5373 imported_name = dwarf2_name (imported_die, imported_cu);
5374 if (imported_name == NULL)
5375 {
5376 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5377
5378 The import in the following code:
5379 namespace A
5380 {
5381 typedef int B;
5382 }
5383
5384 int main ()
5385 {
5386 using A::B;
5387 B b;
5388 return b;
5389 }
5390
5391 ...
5392 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5393 <52> DW_AT_decl_file : 1
5394 <53> DW_AT_decl_line : 6
5395 <54> DW_AT_import : <0x75>
5396 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5397 <59> DW_AT_name : B
5398 <5b> DW_AT_decl_file : 1
5399 <5c> DW_AT_decl_line : 2
5400 <5d> DW_AT_type : <0x6e>
5401 ...
5402 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5403 <76> DW_AT_byte_size : 4
5404 <77> DW_AT_encoding : 5 (signed)
5405
5406 imports the wrong die ( 0x75 instead of 0x58 ).
5407 This case will be ignored until the gcc bug is fixed. */
5408 return;
5409 }
5410
5411 /* Figure out the local name after import. */
5412 import_alias = dwarf2_name (die, cu);
5413
5414 /* Figure out where the statement is being imported to. */
5415 import_prefix = determine_prefix (die, cu);
5416
5417 /* Figure out what the scope of the imported die is and prepend it
5418 to the name of the imported die. */
5419 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5420
5421 if (imported_die->tag != DW_TAG_namespace
5422 && imported_die->tag != DW_TAG_module)
5423 {
5424 imported_declaration = imported_name;
5425 canonical_name = imported_name_prefix;
5426 }
5427 else if (strlen (imported_name_prefix) > 0)
5428 {
5429 temp = alloca (strlen (imported_name_prefix)
5430 + 2 + strlen (imported_name) + 1);
5431 strcpy (temp, imported_name_prefix);
5432 strcat (temp, "::");
5433 strcat (temp, imported_name);
5434 canonical_name = temp;
5435 }
5436 else
5437 canonical_name = imported_name;
5438
5439 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5440
5441 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5442 for (child_die = die->child; child_die && child_die->tag;
5443 child_die = sibling_die (child_die))
5444 {
5445 /* DWARF-4: A Fortran use statement with a “rename list” may be
5446 represented by an imported module entry with an import attribute
5447 referring to the module and owned entries corresponding to those
5448 entities that are renamed as part of being imported. */
5449
5450 if (child_die->tag != DW_TAG_imported_declaration)
5451 {
5452 complaint (&symfile_complaints,
5453 _("child DW_TAG_imported_declaration expected "
5454 "- DIE at 0x%x [in module %s]"),
5455 child_die->offset, cu->objfile->name);
5456 continue;
5457 }
5458
5459 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5460 if (import_attr == NULL)
5461 {
5462 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5463 dwarf_tag_name (child_die->tag));
5464 continue;
5465 }
5466
5467 imported_cu = cu;
5468 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5469 &imported_cu);
5470 imported_name = dwarf2_name (imported_die, imported_cu);
5471 if (imported_name == NULL)
5472 {
5473 complaint (&symfile_complaints,
5474 _("child DW_TAG_imported_declaration has unknown "
5475 "imported name - DIE at 0x%x [in module %s]"),
5476 child_die->offset, cu->objfile->name);
5477 continue;
5478 }
5479
5480 VEC_safe_push (const_char_ptr, excludes, imported_name);
5481
5482 process_die (child_die, cu);
5483 }
5484
5485 cp_add_using_directive (import_prefix,
5486 canonical_name,
5487 import_alias,
5488 imported_declaration,
5489 excludes,
5490 &cu->objfile->objfile_obstack);
5491
5492 do_cleanups (cleanups);
5493 }
5494
5495 static void
5496 initialize_cu_func_list (struct dwarf2_cu *cu)
5497 {
5498 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5499 }
5500
5501 /* Cleanup function for read_file_scope. */
5502
5503 static void
5504 free_cu_line_header (void *arg)
5505 {
5506 struct dwarf2_cu *cu = arg;
5507
5508 free_line_header (cu->line_header);
5509 cu->line_header = NULL;
5510 }
5511
5512 static void
5513 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5514 char **name, char **comp_dir)
5515 {
5516 struct attribute *attr;
5517
5518 *name = NULL;
5519 *comp_dir = NULL;
5520
5521 /* Find the filename. Do not use dwarf2_name here, since the filename
5522 is not a source language identifier. */
5523 attr = dwarf2_attr (die, DW_AT_name, cu);
5524 if (attr)
5525 {
5526 *name = DW_STRING (attr);
5527 }
5528
5529 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5530 if (attr)
5531 *comp_dir = DW_STRING (attr);
5532 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5533 {
5534 *comp_dir = ldirname (*name);
5535 if (*comp_dir != NULL)
5536 make_cleanup (xfree, *comp_dir);
5537 }
5538 if (*comp_dir != NULL)
5539 {
5540 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5541 directory, get rid of it. */
5542 char *cp = strchr (*comp_dir, ':');
5543
5544 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5545 *comp_dir = cp + 1;
5546 }
5547
5548 if (*name == NULL)
5549 *name = "<unknown>";
5550 }
5551
5552 /* Handle DW_AT_stmt_list for a compilation unit. */
5553
5554 static void
5555 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5556 const char *comp_dir)
5557 {
5558 struct attribute *attr;
5559 struct objfile *objfile = cu->objfile;
5560 bfd *abfd = objfile->obfd;
5561
5562 /* Decode line number information if present. We do this before
5563 processing child DIEs, so that the line header table is available
5564 for DW_AT_decl_file. */
5565 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5566 if (attr)
5567 {
5568 unsigned int line_offset = DW_UNSND (attr);
5569 struct line_header *line_header
5570 = dwarf_decode_line_header (line_offset, abfd, cu);
5571
5572 if (line_header)
5573 {
5574 cu->line_header = line_header;
5575 make_cleanup (free_cu_line_header, cu);
5576 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5577 }
5578 }
5579 }
5580
5581 /* Process DW_TAG_compile_unit. */
5582
5583 static void
5584 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5585 {
5586 struct objfile *objfile = cu->objfile;
5587 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5588 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5589 CORE_ADDR highpc = ((CORE_ADDR) 0);
5590 struct attribute *attr;
5591 char *name = NULL;
5592 char *comp_dir = NULL;
5593 struct die_info *child_die;
5594 bfd *abfd = objfile->obfd;
5595 CORE_ADDR baseaddr;
5596
5597 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5598
5599 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5600
5601 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5602 from finish_block. */
5603 if (lowpc == ((CORE_ADDR) -1))
5604 lowpc = highpc;
5605 lowpc += baseaddr;
5606 highpc += baseaddr;
5607
5608 find_file_and_directory (die, cu, &name, &comp_dir);
5609
5610 attr = dwarf2_attr (die, DW_AT_language, cu);
5611 if (attr)
5612 {
5613 set_cu_language (DW_UNSND (attr), cu);
5614 }
5615
5616 attr = dwarf2_attr (die, DW_AT_producer, cu);
5617 if (attr)
5618 cu->producer = DW_STRING (attr);
5619
5620 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5621 standardised yet. As a workaround for the language detection we fall
5622 back to the DW_AT_producer string. */
5623 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5624 cu->language = language_opencl;
5625
5626 /* We assume that we're processing GCC output. */
5627 processing_gcc_compilation = 2;
5628
5629 processing_has_namespace_info = 0;
5630
5631 start_symtab (name, comp_dir, lowpc);
5632 record_debugformat ("DWARF 2");
5633 record_producer (cu->producer);
5634
5635 initialize_cu_func_list (cu);
5636
5637 handle_DW_AT_stmt_list (die, cu, comp_dir);
5638
5639 /* Process all dies in compilation unit. */
5640 if (die->child != NULL)
5641 {
5642 child_die = die->child;
5643 while (child_die && child_die->tag)
5644 {
5645 process_die (child_die, cu);
5646 child_die = sibling_die (child_die);
5647 }
5648 }
5649
5650 /* Decode macro information, if present. Dwarf 2 macro information
5651 refers to information in the line number info statement program
5652 header, so we can only read it if we've read the header
5653 successfully. */
5654 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5655 if (attr && cu->line_header)
5656 {
5657 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5658 complaint (&symfile_complaints,
5659 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5660
5661 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5662 comp_dir, abfd, cu,
5663 &dwarf2_per_objfile->macro, 1);
5664 }
5665 else
5666 {
5667 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5668 if (attr && cu->line_header)
5669 {
5670 unsigned int macro_offset = DW_UNSND (attr);
5671
5672 dwarf_decode_macros (cu->line_header, macro_offset,
5673 comp_dir, abfd, cu,
5674 &dwarf2_per_objfile->macinfo, 0);
5675 }
5676 }
5677 do_cleanups (back_to);
5678 }
5679
5680 /* Process DW_TAG_type_unit.
5681 For TUs we want to skip the first top level sibling if it's not the
5682 actual type being defined by this TU. In this case the first top
5683 level sibling is there to provide context only. */
5684
5685 static void
5686 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5687 {
5688 struct objfile *objfile = cu->objfile;
5689 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5690 CORE_ADDR lowpc;
5691 struct attribute *attr;
5692 char *name = NULL;
5693 char *comp_dir = NULL;
5694 struct die_info *child_die;
5695 bfd *abfd = objfile->obfd;
5696
5697 /* start_symtab needs a low pc, but we don't really have one.
5698 Do what read_file_scope would do in the absence of such info. */
5699 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5700
5701 /* Find the filename. Do not use dwarf2_name here, since the filename
5702 is not a source language identifier. */
5703 attr = dwarf2_attr (die, DW_AT_name, cu);
5704 if (attr)
5705 name = DW_STRING (attr);
5706
5707 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5708 if (attr)
5709 comp_dir = DW_STRING (attr);
5710 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5711 {
5712 comp_dir = ldirname (name);
5713 if (comp_dir != NULL)
5714 make_cleanup (xfree, comp_dir);
5715 }
5716
5717 if (name == NULL)
5718 name = "<unknown>";
5719
5720 attr = dwarf2_attr (die, DW_AT_language, cu);
5721 if (attr)
5722 set_cu_language (DW_UNSND (attr), cu);
5723
5724 /* This isn't technically needed today. It is done for symmetry
5725 with read_file_scope. */
5726 attr = dwarf2_attr (die, DW_AT_producer, cu);
5727 if (attr)
5728 cu->producer = DW_STRING (attr);
5729
5730 /* We assume that we're processing GCC output. */
5731 processing_gcc_compilation = 2;
5732
5733 processing_has_namespace_info = 0;
5734
5735 start_symtab (name, comp_dir, lowpc);
5736 record_debugformat ("DWARF 2");
5737 record_producer (cu->producer);
5738
5739 handle_DW_AT_stmt_list (die, cu, comp_dir);
5740
5741 /* Process the dies in the type unit. */
5742 if (die->child == NULL)
5743 {
5744 dump_die_for_error (die);
5745 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5746 bfd_get_filename (abfd));
5747 }
5748
5749 child_die = die->child;
5750
5751 while (child_die && child_die->tag)
5752 {
5753 process_die (child_die, cu);
5754
5755 child_die = sibling_die (child_die);
5756 }
5757
5758 do_cleanups (back_to);
5759 }
5760
5761 static void
5762 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5763 struct dwarf2_cu *cu)
5764 {
5765 struct function_range *thisfn;
5766
5767 thisfn = (struct function_range *)
5768 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5769 thisfn->name = name;
5770 thisfn->lowpc = lowpc;
5771 thisfn->highpc = highpc;
5772 thisfn->seen_line = 0;
5773 thisfn->next = NULL;
5774
5775 if (cu->last_fn == NULL)
5776 cu->first_fn = thisfn;
5777 else
5778 cu->last_fn->next = thisfn;
5779
5780 cu->last_fn = thisfn;
5781 }
5782
5783 /* qsort helper for inherit_abstract_dies. */
5784
5785 static int
5786 unsigned_int_compar (const void *ap, const void *bp)
5787 {
5788 unsigned int a = *(unsigned int *) ap;
5789 unsigned int b = *(unsigned int *) bp;
5790
5791 return (a > b) - (b > a);
5792 }
5793
5794 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5795 Inherit only the children of the DW_AT_abstract_origin DIE not being
5796 already referenced by DW_AT_abstract_origin from the children of the
5797 current DIE. */
5798
5799 static void
5800 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5801 {
5802 struct die_info *child_die;
5803 unsigned die_children_count;
5804 /* CU offsets which were referenced by children of the current DIE. */
5805 unsigned *offsets;
5806 unsigned *offsets_end, *offsetp;
5807 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5808 struct die_info *origin_die;
5809 /* Iterator of the ORIGIN_DIE children. */
5810 struct die_info *origin_child_die;
5811 struct cleanup *cleanups;
5812 struct attribute *attr;
5813 struct dwarf2_cu *origin_cu;
5814 struct pending **origin_previous_list_in_scope;
5815
5816 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5817 if (!attr)
5818 return;
5819
5820 /* Note that following die references may follow to a die in a
5821 different cu. */
5822
5823 origin_cu = cu;
5824 origin_die = follow_die_ref (die, attr, &origin_cu);
5825
5826 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5827 symbols in. */
5828 origin_previous_list_in_scope = origin_cu->list_in_scope;
5829 origin_cu->list_in_scope = cu->list_in_scope;
5830
5831 if (die->tag != origin_die->tag
5832 && !(die->tag == DW_TAG_inlined_subroutine
5833 && origin_die->tag == DW_TAG_subprogram))
5834 complaint (&symfile_complaints,
5835 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5836 die->offset, origin_die->offset);
5837
5838 child_die = die->child;
5839 die_children_count = 0;
5840 while (child_die && child_die->tag)
5841 {
5842 child_die = sibling_die (child_die);
5843 die_children_count++;
5844 }
5845 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5846 cleanups = make_cleanup (xfree, offsets);
5847
5848 offsets_end = offsets;
5849 child_die = die->child;
5850 while (child_die && child_die->tag)
5851 {
5852 /* For each CHILD_DIE, find the corresponding child of
5853 ORIGIN_DIE. If there is more than one layer of
5854 DW_AT_abstract_origin, follow them all; there shouldn't be,
5855 but GCC versions at least through 4.4 generate this (GCC PR
5856 40573). */
5857 struct die_info *child_origin_die = child_die;
5858 struct dwarf2_cu *child_origin_cu = cu;
5859
5860 while (1)
5861 {
5862 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5863 child_origin_cu);
5864 if (attr == NULL)
5865 break;
5866 child_origin_die = follow_die_ref (child_origin_die, attr,
5867 &child_origin_cu);
5868 }
5869
5870 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5871 counterpart may exist. */
5872 if (child_origin_die != child_die)
5873 {
5874 if (child_die->tag != child_origin_die->tag
5875 && !(child_die->tag == DW_TAG_inlined_subroutine
5876 && child_origin_die->tag == DW_TAG_subprogram))
5877 complaint (&symfile_complaints,
5878 _("Child DIE 0x%x and its abstract origin 0x%x have "
5879 "different tags"), child_die->offset,
5880 child_origin_die->offset);
5881 if (child_origin_die->parent != origin_die)
5882 complaint (&symfile_complaints,
5883 _("Child DIE 0x%x and its abstract origin 0x%x have "
5884 "different parents"), child_die->offset,
5885 child_origin_die->offset);
5886 else
5887 *offsets_end++ = child_origin_die->offset;
5888 }
5889 child_die = sibling_die (child_die);
5890 }
5891 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5892 unsigned_int_compar);
5893 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5894 if (offsetp[-1] == *offsetp)
5895 complaint (&symfile_complaints,
5896 _("Multiple children of DIE 0x%x refer "
5897 "to DIE 0x%x as their abstract origin"),
5898 die->offset, *offsetp);
5899
5900 offsetp = offsets;
5901 origin_child_die = origin_die->child;
5902 while (origin_child_die && origin_child_die->tag)
5903 {
5904 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5905 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5906 offsetp++;
5907 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5908 {
5909 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5910 process_die (origin_child_die, origin_cu);
5911 }
5912 origin_child_die = sibling_die (origin_child_die);
5913 }
5914 origin_cu->list_in_scope = origin_previous_list_in_scope;
5915
5916 do_cleanups (cleanups);
5917 }
5918
5919 static void
5920 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5921 {
5922 struct objfile *objfile = cu->objfile;
5923 struct context_stack *new;
5924 CORE_ADDR lowpc;
5925 CORE_ADDR highpc;
5926 struct die_info *child_die;
5927 struct attribute *attr, *call_line, *call_file;
5928 char *name;
5929 CORE_ADDR baseaddr;
5930 struct block *block;
5931 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5932 VEC (symbolp) *template_args = NULL;
5933 struct template_symbol *templ_func = NULL;
5934
5935 if (inlined_func)
5936 {
5937 /* If we do not have call site information, we can't show the
5938 caller of this inlined function. That's too confusing, so
5939 only use the scope for local variables. */
5940 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5941 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5942 if (call_line == NULL || call_file == NULL)
5943 {
5944 read_lexical_block_scope (die, cu);
5945 return;
5946 }
5947 }
5948
5949 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5950
5951 name = dwarf2_name (die, cu);
5952
5953 /* Ignore functions with missing or empty names. These are actually
5954 illegal according to the DWARF standard. */
5955 if (name == NULL)
5956 {
5957 complaint (&symfile_complaints,
5958 _("missing name for subprogram DIE at %d"), die->offset);
5959 return;
5960 }
5961
5962 /* Ignore functions with missing or invalid low and high pc attributes. */
5963 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5964 {
5965 attr = dwarf2_attr (die, DW_AT_external, cu);
5966 if (!attr || !DW_UNSND (attr))
5967 complaint (&symfile_complaints,
5968 _("cannot get low and high bounds "
5969 "for subprogram DIE at %d"),
5970 die->offset);
5971 return;
5972 }
5973
5974 lowpc += baseaddr;
5975 highpc += baseaddr;
5976
5977 /* Record the function range for dwarf_decode_lines. */
5978 add_to_cu_func_list (name, lowpc, highpc, cu);
5979
5980 /* If we have any template arguments, then we must allocate a
5981 different sort of symbol. */
5982 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5983 {
5984 if (child_die->tag == DW_TAG_template_type_param
5985 || child_die->tag == DW_TAG_template_value_param)
5986 {
5987 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5988 struct template_symbol);
5989 templ_func->base.is_cplus_template_function = 1;
5990 break;
5991 }
5992 }
5993
5994 new = push_context (0, lowpc);
5995 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5996 (struct symbol *) templ_func);
5997
5998 /* If there is a location expression for DW_AT_frame_base, record
5999 it. */
6000 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6001 if (attr)
6002 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6003 expression is being recorded directly in the function's symbol
6004 and not in a separate frame-base object. I guess this hack is
6005 to avoid adding some sort of frame-base adjunct/annex to the
6006 function's symbol :-(. The problem with doing this is that it
6007 results in a function symbol with a location expression that
6008 has nothing to do with the location of the function, ouch! The
6009 relationship should be: a function's symbol has-a frame base; a
6010 frame-base has-a location expression. */
6011 dwarf2_symbol_mark_computed (attr, new->name, cu);
6012
6013 cu->list_in_scope = &local_symbols;
6014
6015 if (die->child != NULL)
6016 {
6017 child_die = die->child;
6018 while (child_die && child_die->tag)
6019 {
6020 if (child_die->tag == DW_TAG_template_type_param
6021 || child_die->tag == DW_TAG_template_value_param)
6022 {
6023 struct symbol *arg = new_symbol (child_die, NULL, cu);
6024
6025 if (arg != NULL)
6026 VEC_safe_push (symbolp, template_args, arg);
6027 }
6028 else
6029 process_die (child_die, cu);
6030 child_die = sibling_die (child_die);
6031 }
6032 }
6033
6034 inherit_abstract_dies (die, cu);
6035
6036 /* If we have a DW_AT_specification, we might need to import using
6037 directives from the context of the specification DIE. See the
6038 comment in determine_prefix. */
6039 if (cu->language == language_cplus
6040 && dwarf2_attr (die, DW_AT_specification, cu))
6041 {
6042 struct dwarf2_cu *spec_cu = cu;
6043 struct die_info *spec_die = die_specification (die, &spec_cu);
6044
6045 while (spec_die)
6046 {
6047 child_die = spec_die->child;
6048 while (child_die && child_die->tag)
6049 {
6050 if (child_die->tag == DW_TAG_imported_module)
6051 process_die (child_die, spec_cu);
6052 child_die = sibling_die (child_die);
6053 }
6054
6055 /* In some cases, GCC generates specification DIEs that
6056 themselves contain DW_AT_specification attributes. */
6057 spec_die = die_specification (spec_die, &spec_cu);
6058 }
6059 }
6060
6061 new = pop_context ();
6062 /* Make a block for the local symbols within. */
6063 block = finish_block (new->name, &local_symbols, new->old_blocks,
6064 lowpc, highpc, objfile);
6065
6066 /* For C++, set the block's scope. */
6067 if (cu->language == language_cplus || cu->language == language_fortran)
6068 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6069 determine_prefix (die, cu),
6070 processing_has_namespace_info);
6071
6072 /* If we have address ranges, record them. */
6073 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6074
6075 /* Attach template arguments to function. */
6076 if (! VEC_empty (symbolp, template_args))
6077 {
6078 gdb_assert (templ_func != NULL);
6079
6080 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6081 templ_func->template_arguments
6082 = obstack_alloc (&objfile->objfile_obstack,
6083 (templ_func->n_template_arguments
6084 * sizeof (struct symbol *)));
6085 memcpy (templ_func->template_arguments,
6086 VEC_address (symbolp, template_args),
6087 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6088 VEC_free (symbolp, template_args);
6089 }
6090
6091 /* In C++, we can have functions nested inside functions (e.g., when
6092 a function declares a class that has methods). This means that
6093 when we finish processing a function scope, we may need to go
6094 back to building a containing block's symbol lists. */
6095 local_symbols = new->locals;
6096 param_symbols = new->params;
6097 using_directives = new->using_directives;
6098
6099 /* If we've finished processing a top-level function, subsequent
6100 symbols go in the file symbol list. */
6101 if (outermost_context_p ())
6102 cu->list_in_scope = &file_symbols;
6103 }
6104
6105 /* Process all the DIES contained within a lexical block scope. Start
6106 a new scope, process the dies, and then close the scope. */
6107
6108 static void
6109 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6110 {
6111 struct objfile *objfile = cu->objfile;
6112 struct context_stack *new;
6113 CORE_ADDR lowpc, highpc;
6114 struct die_info *child_die;
6115 CORE_ADDR baseaddr;
6116
6117 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6118
6119 /* Ignore blocks with missing or invalid low and high pc attributes. */
6120 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6121 as multiple lexical blocks? Handling children in a sane way would
6122 be nasty. Might be easier to properly extend generic blocks to
6123 describe ranges. */
6124 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6125 return;
6126 lowpc += baseaddr;
6127 highpc += baseaddr;
6128
6129 push_context (0, lowpc);
6130 if (die->child != NULL)
6131 {
6132 child_die = die->child;
6133 while (child_die && child_die->tag)
6134 {
6135 process_die (child_die, cu);
6136 child_die = sibling_die (child_die);
6137 }
6138 }
6139 new = pop_context ();
6140
6141 if (local_symbols != NULL || using_directives != NULL)
6142 {
6143 struct block *block
6144 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6145 highpc, objfile);
6146
6147 /* Note that recording ranges after traversing children, as we
6148 do here, means that recording a parent's ranges entails
6149 walking across all its children's ranges as they appear in
6150 the address map, which is quadratic behavior.
6151
6152 It would be nicer to record the parent's ranges before
6153 traversing its children, simply overriding whatever you find
6154 there. But since we don't even decide whether to create a
6155 block until after we've traversed its children, that's hard
6156 to do. */
6157 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6158 }
6159 local_symbols = new->locals;
6160 using_directives = new->using_directives;
6161 }
6162
6163 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6164 Return 1 if the attributes are present and valid, otherwise, return 0.
6165 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6166
6167 static int
6168 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6169 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6170 struct partial_symtab *ranges_pst)
6171 {
6172 struct objfile *objfile = cu->objfile;
6173 struct comp_unit_head *cu_header = &cu->header;
6174 bfd *obfd = objfile->obfd;
6175 unsigned int addr_size = cu_header->addr_size;
6176 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6177 /* Base address selection entry. */
6178 CORE_ADDR base;
6179 int found_base;
6180 unsigned int dummy;
6181 gdb_byte *buffer;
6182 CORE_ADDR marker;
6183 int low_set;
6184 CORE_ADDR low = 0;
6185 CORE_ADDR high = 0;
6186 CORE_ADDR baseaddr;
6187
6188 found_base = cu->base_known;
6189 base = cu->base_address;
6190
6191 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6192 if (offset >= dwarf2_per_objfile->ranges.size)
6193 {
6194 complaint (&symfile_complaints,
6195 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6196 offset);
6197 return 0;
6198 }
6199 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6200
6201 /* Read in the largest possible address. */
6202 marker = read_address (obfd, buffer, cu, &dummy);
6203 if ((marker & mask) == mask)
6204 {
6205 /* If we found the largest possible address, then
6206 read the base address. */
6207 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6208 buffer += 2 * addr_size;
6209 offset += 2 * addr_size;
6210 found_base = 1;
6211 }
6212
6213 low_set = 0;
6214
6215 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6216
6217 while (1)
6218 {
6219 CORE_ADDR range_beginning, range_end;
6220
6221 range_beginning = read_address (obfd, buffer, cu, &dummy);
6222 buffer += addr_size;
6223 range_end = read_address (obfd, buffer, cu, &dummy);
6224 buffer += addr_size;
6225 offset += 2 * addr_size;
6226
6227 /* An end of list marker is a pair of zero addresses. */
6228 if (range_beginning == 0 && range_end == 0)
6229 /* Found the end of list entry. */
6230 break;
6231
6232 /* Each base address selection entry is a pair of 2 values.
6233 The first is the largest possible address, the second is
6234 the base address. Check for a base address here. */
6235 if ((range_beginning & mask) == mask)
6236 {
6237 /* If we found the largest possible address, then
6238 read the base address. */
6239 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6240 found_base = 1;
6241 continue;
6242 }
6243
6244 if (!found_base)
6245 {
6246 /* We have no valid base address for the ranges
6247 data. */
6248 complaint (&symfile_complaints,
6249 _("Invalid .debug_ranges data (no base address)"));
6250 return 0;
6251 }
6252
6253 if (range_beginning > range_end)
6254 {
6255 /* Inverted range entries are invalid. */
6256 complaint (&symfile_complaints,
6257 _("Invalid .debug_ranges data (inverted range)"));
6258 return 0;
6259 }
6260
6261 /* Empty range entries have no effect. */
6262 if (range_beginning == range_end)
6263 continue;
6264
6265 range_beginning += base;
6266 range_end += base;
6267
6268 if (ranges_pst != NULL)
6269 addrmap_set_empty (objfile->psymtabs_addrmap,
6270 range_beginning + baseaddr,
6271 range_end - 1 + baseaddr,
6272 ranges_pst);
6273
6274 /* FIXME: This is recording everything as a low-high
6275 segment of consecutive addresses. We should have a
6276 data structure for discontiguous block ranges
6277 instead. */
6278 if (! low_set)
6279 {
6280 low = range_beginning;
6281 high = range_end;
6282 low_set = 1;
6283 }
6284 else
6285 {
6286 if (range_beginning < low)
6287 low = range_beginning;
6288 if (range_end > high)
6289 high = range_end;
6290 }
6291 }
6292
6293 if (! low_set)
6294 /* If the first entry is an end-of-list marker, the range
6295 describes an empty scope, i.e. no instructions. */
6296 return 0;
6297
6298 if (low_return)
6299 *low_return = low;
6300 if (high_return)
6301 *high_return = high;
6302 return 1;
6303 }
6304
6305 /* Get low and high pc attributes from a die. Return 1 if the attributes
6306 are present and valid, otherwise, return 0. Return -1 if the range is
6307 discontinuous, i.e. derived from DW_AT_ranges information. */
6308 static int
6309 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6310 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6311 struct partial_symtab *pst)
6312 {
6313 struct attribute *attr;
6314 CORE_ADDR low = 0;
6315 CORE_ADDR high = 0;
6316 int ret = 0;
6317
6318 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6319 if (attr)
6320 {
6321 high = DW_ADDR (attr);
6322 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6323 if (attr)
6324 low = DW_ADDR (attr);
6325 else
6326 /* Found high w/o low attribute. */
6327 return 0;
6328
6329 /* Found consecutive range of addresses. */
6330 ret = 1;
6331 }
6332 else
6333 {
6334 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6335 if (attr != NULL)
6336 {
6337 /* Value of the DW_AT_ranges attribute is the offset in the
6338 .debug_ranges section. */
6339 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6340 return 0;
6341 /* Found discontinuous range of addresses. */
6342 ret = -1;
6343 }
6344 }
6345
6346 /* read_partial_die has also the strict LOW < HIGH requirement. */
6347 if (high <= low)
6348 return 0;
6349
6350 /* When using the GNU linker, .gnu.linkonce. sections are used to
6351 eliminate duplicate copies of functions and vtables and such.
6352 The linker will arbitrarily choose one and discard the others.
6353 The AT_*_pc values for such functions refer to local labels in
6354 these sections. If the section from that file was discarded, the
6355 labels are not in the output, so the relocs get a value of 0.
6356 If this is a discarded function, mark the pc bounds as invalid,
6357 so that GDB will ignore it. */
6358 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6359 return 0;
6360
6361 *lowpc = low;
6362 *highpc = high;
6363 return ret;
6364 }
6365
6366 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6367 its low and high PC addresses. Do nothing if these addresses could not
6368 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6369 and HIGHPC to the high address if greater than HIGHPC. */
6370
6371 static void
6372 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6373 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6374 struct dwarf2_cu *cu)
6375 {
6376 CORE_ADDR low, high;
6377 struct die_info *child = die->child;
6378
6379 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6380 {
6381 *lowpc = min (*lowpc, low);
6382 *highpc = max (*highpc, high);
6383 }
6384
6385 /* If the language does not allow nested subprograms (either inside
6386 subprograms or lexical blocks), we're done. */
6387 if (cu->language != language_ada)
6388 return;
6389
6390 /* Check all the children of the given DIE. If it contains nested
6391 subprograms, then check their pc bounds. Likewise, we need to
6392 check lexical blocks as well, as they may also contain subprogram
6393 definitions. */
6394 while (child && child->tag)
6395 {
6396 if (child->tag == DW_TAG_subprogram
6397 || child->tag == DW_TAG_lexical_block)
6398 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6399 child = sibling_die (child);
6400 }
6401 }
6402
6403 /* Get the low and high pc's represented by the scope DIE, and store
6404 them in *LOWPC and *HIGHPC. If the correct values can't be
6405 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6406
6407 static void
6408 get_scope_pc_bounds (struct die_info *die,
6409 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6410 struct dwarf2_cu *cu)
6411 {
6412 CORE_ADDR best_low = (CORE_ADDR) -1;
6413 CORE_ADDR best_high = (CORE_ADDR) 0;
6414 CORE_ADDR current_low, current_high;
6415
6416 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6417 {
6418 best_low = current_low;
6419 best_high = current_high;
6420 }
6421 else
6422 {
6423 struct die_info *child = die->child;
6424
6425 while (child && child->tag)
6426 {
6427 switch (child->tag) {
6428 case DW_TAG_subprogram:
6429 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6430 break;
6431 case DW_TAG_namespace:
6432 case DW_TAG_module:
6433 /* FIXME: carlton/2004-01-16: Should we do this for
6434 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6435 that current GCC's always emit the DIEs corresponding
6436 to definitions of methods of classes as children of a
6437 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6438 the DIEs giving the declarations, which could be
6439 anywhere). But I don't see any reason why the
6440 standards says that they have to be there. */
6441 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6442
6443 if (current_low != ((CORE_ADDR) -1))
6444 {
6445 best_low = min (best_low, current_low);
6446 best_high = max (best_high, current_high);
6447 }
6448 break;
6449 default:
6450 /* Ignore. */
6451 break;
6452 }
6453
6454 child = sibling_die (child);
6455 }
6456 }
6457
6458 *lowpc = best_low;
6459 *highpc = best_high;
6460 }
6461
6462 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6463 in DIE. */
6464 static void
6465 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6466 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6467 {
6468 struct attribute *attr;
6469
6470 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6471 if (attr)
6472 {
6473 CORE_ADDR high = DW_ADDR (attr);
6474
6475 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6476 if (attr)
6477 {
6478 CORE_ADDR low = DW_ADDR (attr);
6479
6480 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6481 }
6482 }
6483
6484 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6485 if (attr)
6486 {
6487 bfd *obfd = cu->objfile->obfd;
6488
6489 /* The value of the DW_AT_ranges attribute is the offset of the
6490 address range list in the .debug_ranges section. */
6491 unsigned long offset = DW_UNSND (attr);
6492 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6493
6494 /* For some target architectures, but not others, the
6495 read_address function sign-extends the addresses it returns.
6496 To recognize base address selection entries, we need a
6497 mask. */
6498 unsigned int addr_size = cu->header.addr_size;
6499 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6500
6501 /* The base address, to which the next pair is relative. Note
6502 that this 'base' is a DWARF concept: most entries in a range
6503 list are relative, to reduce the number of relocs against the
6504 debugging information. This is separate from this function's
6505 'baseaddr' argument, which GDB uses to relocate debugging
6506 information from a shared library based on the address at
6507 which the library was loaded. */
6508 CORE_ADDR base = cu->base_address;
6509 int base_known = cu->base_known;
6510
6511 gdb_assert (dwarf2_per_objfile->ranges.readin);
6512 if (offset >= dwarf2_per_objfile->ranges.size)
6513 {
6514 complaint (&symfile_complaints,
6515 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6516 offset);
6517 return;
6518 }
6519
6520 for (;;)
6521 {
6522 unsigned int bytes_read;
6523 CORE_ADDR start, end;
6524
6525 start = read_address (obfd, buffer, cu, &bytes_read);
6526 buffer += bytes_read;
6527 end = read_address (obfd, buffer, cu, &bytes_read);
6528 buffer += bytes_read;
6529
6530 /* Did we find the end of the range list? */
6531 if (start == 0 && end == 0)
6532 break;
6533
6534 /* Did we find a base address selection entry? */
6535 else if ((start & base_select_mask) == base_select_mask)
6536 {
6537 base = end;
6538 base_known = 1;
6539 }
6540
6541 /* We found an ordinary address range. */
6542 else
6543 {
6544 if (!base_known)
6545 {
6546 complaint (&symfile_complaints,
6547 _("Invalid .debug_ranges data "
6548 "(no base address)"));
6549 return;
6550 }
6551
6552 if (start > end)
6553 {
6554 /* Inverted range entries are invalid. */
6555 complaint (&symfile_complaints,
6556 _("Invalid .debug_ranges data "
6557 "(inverted range)"));
6558 return;
6559 }
6560
6561 /* Empty range entries have no effect. */
6562 if (start == end)
6563 continue;
6564
6565 record_block_range (block,
6566 baseaddr + base + start,
6567 baseaddr + base + end - 1);
6568 }
6569 }
6570 }
6571 }
6572
6573 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6574 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6575 during 4.6.0 experimental. */
6576
6577 static int
6578 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6579 {
6580 const char *cs;
6581 int major, minor, release;
6582
6583 if (cu->producer == NULL)
6584 {
6585 /* For unknown compilers expect their behavior is DWARF version
6586 compliant.
6587
6588 GCC started to support .debug_types sections by -gdwarf-4 since
6589 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6590 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6591 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6592 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6593
6594 return 0;
6595 }
6596
6597 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6598
6599 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6600 {
6601 /* For non-GCC compilers expect their behavior is DWARF version
6602 compliant. */
6603
6604 return 0;
6605 }
6606 cs = &cu->producer[strlen ("GNU ")];
6607 while (*cs && !isdigit (*cs))
6608 cs++;
6609 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6610 {
6611 /* Not recognized as GCC. */
6612
6613 return 0;
6614 }
6615
6616 return major < 4 || (major == 4 && minor < 6);
6617 }
6618
6619 /* Return the default accessibility type if it is not overriden by
6620 DW_AT_accessibility. */
6621
6622 static enum dwarf_access_attribute
6623 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6624 {
6625 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6626 {
6627 /* The default DWARF 2 accessibility for members is public, the default
6628 accessibility for inheritance is private. */
6629
6630 if (die->tag != DW_TAG_inheritance)
6631 return DW_ACCESS_public;
6632 else
6633 return DW_ACCESS_private;
6634 }
6635 else
6636 {
6637 /* DWARF 3+ defines the default accessibility a different way. The same
6638 rules apply now for DW_TAG_inheritance as for the members and it only
6639 depends on the container kind. */
6640
6641 if (die->parent->tag == DW_TAG_class_type)
6642 return DW_ACCESS_private;
6643 else
6644 return DW_ACCESS_public;
6645 }
6646 }
6647
6648 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6649 offset. If the attribute was not found return 0, otherwise return
6650 1. If it was found but could not properly be handled, set *OFFSET
6651 to 0. */
6652
6653 static int
6654 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6655 LONGEST *offset)
6656 {
6657 struct attribute *attr;
6658
6659 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6660 if (attr != NULL)
6661 {
6662 *offset = 0;
6663
6664 /* Note that we do not check for a section offset first here.
6665 This is because DW_AT_data_member_location is new in DWARF 4,
6666 so if we see it, we can assume that a constant form is really
6667 a constant and not a section offset. */
6668 if (attr_form_is_constant (attr))
6669 *offset = dwarf2_get_attr_constant_value (attr, 0);
6670 else if (attr_form_is_section_offset (attr))
6671 dwarf2_complex_location_expr_complaint ();
6672 else if (attr_form_is_block (attr))
6673 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6674 else
6675 dwarf2_complex_location_expr_complaint ();
6676
6677 return 1;
6678 }
6679
6680 return 0;
6681 }
6682
6683 /* Add an aggregate field to the field list. */
6684
6685 static void
6686 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6687 struct dwarf2_cu *cu)
6688 {
6689 struct objfile *objfile = cu->objfile;
6690 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6691 struct nextfield *new_field;
6692 struct attribute *attr;
6693 struct field *fp;
6694 char *fieldname = "";
6695
6696 /* Allocate a new field list entry and link it in. */
6697 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6698 make_cleanup (xfree, new_field);
6699 memset (new_field, 0, sizeof (struct nextfield));
6700
6701 if (die->tag == DW_TAG_inheritance)
6702 {
6703 new_field->next = fip->baseclasses;
6704 fip->baseclasses = new_field;
6705 }
6706 else
6707 {
6708 new_field->next = fip->fields;
6709 fip->fields = new_field;
6710 }
6711 fip->nfields++;
6712
6713 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6714 if (attr)
6715 new_field->accessibility = DW_UNSND (attr);
6716 else
6717 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6718 if (new_field->accessibility != DW_ACCESS_public)
6719 fip->non_public_fields = 1;
6720
6721 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6722 if (attr)
6723 new_field->virtuality = DW_UNSND (attr);
6724 else
6725 new_field->virtuality = DW_VIRTUALITY_none;
6726
6727 fp = &new_field->field;
6728
6729 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6730 {
6731 LONGEST offset;
6732
6733 /* Data member other than a C++ static data member. */
6734
6735 /* Get type of field. */
6736 fp->type = die_type (die, cu);
6737
6738 SET_FIELD_BITPOS (*fp, 0);
6739
6740 /* Get bit size of field (zero if none). */
6741 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6742 if (attr)
6743 {
6744 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6745 }
6746 else
6747 {
6748 FIELD_BITSIZE (*fp) = 0;
6749 }
6750
6751 /* Get bit offset of field. */
6752 if (handle_data_member_location (die, cu, &offset))
6753 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6754 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6755 if (attr)
6756 {
6757 if (gdbarch_bits_big_endian (gdbarch))
6758 {
6759 /* For big endian bits, the DW_AT_bit_offset gives the
6760 additional bit offset from the MSB of the containing
6761 anonymous object to the MSB of the field. We don't
6762 have to do anything special since we don't need to
6763 know the size of the anonymous object. */
6764 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6765 }
6766 else
6767 {
6768 /* For little endian bits, compute the bit offset to the
6769 MSB of the anonymous object, subtract off the number of
6770 bits from the MSB of the field to the MSB of the
6771 object, and then subtract off the number of bits of
6772 the field itself. The result is the bit offset of
6773 the LSB of the field. */
6774 int anonymous_size;
6775 int bit_offset = DW_UNSND (attr);
6776
6777 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6778 if (attr)
6779 {
6780 /* The size of the anonymous object containing
6781 the bit field is explicit, so use the
6782 indicated size (in bytes). */
6783 anonymous_size = DW_UNSND (attr);
6784 }
6785 else
6786 {
6787 /* The size of the anonymous object containing
6788 the bit field must be inferred from the type
6789 attribute of the data member containing the
6790 bit field. */
6791 anonymous_size = TYPE_LENGTH (fp->type);
6792 }
6793 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6794 - bit_offset - FIELD_BITSIZE (*fp);
6795 }
6796 }
6797
6798 /* Get name of field. */
6799 fieldname = dwarf2_name (die, cu);
6800 if (fieldname == NULL)
6801 fieldname = "";
6802
6803 /* The name is already allocated along with this objfile, so we don't
6804 need to duplicate it for the type. */
6805 fp->name = fieldname;
6806
6807 /* Change accessibility for artificial fields (e.g. virtual table
6808 pointer or virtual base class pointer) to private. */
6809 if (dwarf2_attr (die, DW_AT_artificial, cu))
6810 {
6811 FIELD_ARTIFICIAL (*fp) = 1;
6812 new_field->accessibility = DW_ACCESS_private;
6813 fip->non_public_fields = 1;
6814 }
6815 }
6816 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6817 {
6818 /* C++ static member. */
6819
6820 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6821 is a declaration, but all versions of G++ as of this writing
6822 (so through at least 3.2.1) incorrectly generate
6823 DW_TAG_variable tags. */
6824
6825 const char *physname;
6826
6827 /* Get name of field. */
6828 fieldname = dwarf2_name (die, cu);
6829 if (fieldname == NULL)
6830 return;
6831
6832 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6833 if (attr
6834 /* Only create a symbol if this is an external value.
6835 new_symbol checks this and puts the value in the global symbol
6836 table, which we want. If it is not external, new_symbol
6837 will try to put the value in cu->list_in_scope which is wrong. */
6838 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6839 {
6840 /* A static const member, not much different than an enum as far as
6841 we're concerned, except that we can support more types. */
6842 new_symbol (die, NULL, cu);
6843 }
6844
6845 /* Get physical name. */
6846 physname = dwarf2_physname (fieldname, die, cu);
6847
6848 /* The name is already allocated along with this objfile, so we don't
6849 need to duplicate it for the type. */
6850 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6851 FIELD_TYPE (*fp) = die_type (die, cu);
6852 FIELD_NAME (*fp) = fieldname;
6853 }
6854 else if (die->tag == DW_TAG_inheritance)
6855 {
6856 LONGEST offset;
6857
6858 /* C++ base class field. */
6859 if (handle_data_member_location (die, cu, &offset))
6860 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6861 FIELD_BITSIZE (*fp) = 0;
6862 FIELD_TYPE (*fp) = die_type (die, cu);
6863 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6864 fip->nbaseclasses++;
6865 }
6866 }
6867
6868 /* Add a typedef defined in the scope of the FIP's class. */
6869
6870 static void
6871 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6872 struct dwarf2_cu *cu)
6873 {
6874 struct objfile *objfile = cu->objfile;
6875 struct typedef_field_list *new_field;
6876 struct attribute *attr;
6877 struct typedef_field *fp;
6878 char *fieldname = "";
6879
6880 /* Allocate a new field list entry and link it in. */
6881 new_field = xzalloc (sizeof (*new_field));
6882 make_cleanup (xfree, new_field);
6883
6884 gdb_assert (die->tag == DW_TAG_typedef);
6885
6886 fp = &new_field->field;
6887
6888 /* Get name of field. */
6889 fp->name = dwarf2_name (die, cu);
6890 if (fp->name == NULL)
6891 return;
6892
6893 fp->type = read_type_die (die, cu);
6894
6895 new_field->next = fip->typedef_field_list;
6896 fip->typedef_field_list = new_field;
6897 fip->typedef_field_list_count++;
6898 }
6899
6900 /* Create the vector of fields, and attach it to the type. */
6901
6902 static void
6903 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6904 struct dwarf2_cu *cu)
6905 {
6906 int nfields = fip->nfields;
6907
6908 /* Record the field count, allocate space for the array of fields,
6909 and create blank accessibility bitfields if necessary. */
6910 TYPE_NFIELDS (type) = nfields;
6911 TYPE_FIELDS (type) = (struct field *)
6912 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6913 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6914
6915 if (fip->non_public_fields && cu->language != language_ada)
6916 {
6917 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6918
6919 TYPE_FIELD_PRIVATE_BITS (type) =
6920 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6921 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6922
6923 TYPE_FIELD_PROTECTED_BITS (type) =
6924 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6925 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6926
6927 TYPE_FIELD_IGNORE_BITS (type) =
6928 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6929 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6930 }
6931
6932 /* If the type has baseclasses, allocate and clear a bit vector for
6933 TYPE_FIELD_VIRTUAL_BITS. */
6934 if (fip->nbaseclasses && cu->language != language_ada)
6935 {
6936 int num_bytes = B_BYTES (fip->nbaseclasses);
6937 unsigned char *pointer;
6938
6939 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6940 pointer = TYPE_ALLOC (type, num_bytes);
6941 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6942 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6943 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6944 }
6945
6946 /* Copy the saved-up fields into the field vector. Start from the head of
6947 the list, adding to the tail of the field array, so that they end up in
6948 the same order in the array in which they were added to the list. */
6949 while (nfields-- > 0)
6950 {
6951 struct nextfield *fieldp;
6952
6953 if (fip->fields)
6954 {
6955 fieldp = fip->fields;
6956 fip->fields = fieldp->next;
6957 }
6958 else
6959 {
6960 fieldp = fip->baseclasses;
6961 fip->baseclasses = fieldp->next;
6962 }
6963
6964 TYPE_FIELD (type, nfields) = fieldp->field;
6965 switch (fieldp->accessibility)
6966 {
6967 case DW_ACCESS_private:
6968 if (cu->language != language_ada)
6969 SET_TYPE_FIELD_PRIVATE (type, nfields);
6970 break;
6971
6972 case DW_ACCESS_protected:
6973 if (cu->language != language_ada)
6974 SET_TYPE_FIELD_PROTECTED (type, nfields);
6975 break;
6976
6977 case DW_ACCESS_public:
6978 break;
6979
6980 default:
6981 /* Unknown accessibility. Complain and treat it as public. */
6982 {
6983 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6984 fieldp->accessibility);
6985 }
6986 break;
6987 }
6988 if (nfields < fip->nbaseclasses)
6989 {
6990 switch (fieldp->virtuality)
6991 {
6992 case DW_VIRTUALITY_virtual:
6993 case DW_VIRTUALITY_pure_virtual:
6994 if (cu->language == language_ada)
6995 error (_("unexpected virtuality in component of Ada type"));
6996 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6997 break;
6998 }
6999 }
7000 }
7001 }
7002
7003 /* Add a member function to the proper fieldlist. */
7004
7005 static void
7006 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7007 struct type *type, struct dwarf2_cu *cu)
7008 {
7009 struct objfile *objfile = cu->objfile;
7010 struct attribute *attr;
7011 struct fnfieldlist *flp;
7012 int i;
7013 struct fn_field *fnp;
7014 char *fieldname;
7015 struct nextfnfield *new_fnfield;
7016 struct type *this_type;
7017 enum dwarf_access_attribute accessibility;
7018
7019 if (cu->language == language_ada)
7020 error (_("unexpected member function in Ada type"));
7021
7022 /* Get name of member function. */
7023 fieldname = dwarf2_name (die, cu);
7024 if (fieldname == NULL)
7025 return;
7026
7027 /* Look up member function name in fieldlist. */
7028 for (i = 0; i < fip->nfnfields; i++)
7029 {
7030 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7031 break;
7032 }
7033
7034 /* Create new list element if necessary. */
7035 if (i < fip->nfnfields)
7036 flp = &fip->fnfieldlists[i];
7037 else
7038 {
7039 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7040 {
7041 fip->fnfieldlists = (struct fnfieldlist *)
7042 xrealloc (fip->fnfieldlists,
7043 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7044 * sizeof (struct fnfieldlist));
7045 if (fip->nfnfields == 0)
7046 make_cleanup (free_current_contents, &fip->fnfieldlists);
7047 }
7048 flp = &fip->fnfieldlists[fip->nfnfields];
7049 flp->name = fieldname;
7050 flp->length = 0;
7051 flp->head = NULL;
7052 i = fip->nfnfields++;
7053 }
7054
7055 /* Create a new member function field and chain it to the field list
7056 entry. */
7057 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7058 make_cleanup (xfree, new_fnfield);
7059 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7060 new_fnfield->next = flp->head;
7061 flp->head = new_fnfield;
7062 flp->length++;
7063
7064 /* Fill in the member function field info. */
7065 fnp = &new_fnfield->fnfield;
7066
7067 /* Delay processing of the physname until later. */
7068 if (cu->language == language_cplus || cu->language == language_java)
7069 {
7070 add_to_method_list (type, i, flp->length - 1, fieldname,
7071 die, cu);
7072 }
7073 else
7074 {
7075 const char *physname = dwarf2_physname (fieldname, die, cu);
7076 fnp->physname = physname ? physname : "";
7077 }
7078
7079 fnp->type = alloc_type (objfile);
7080 this_type = read_type_die (die, cu);
7081 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7082 {
7083 int nparams = TYPE_NFIELDS (this_type);
7084
7085 /* TYPE is the domain of this method, and THIS_TYPE is the type
7086 of the method itself (TYPE_CODE_METHOD). */
7087 smash_to_method_type (fnp->type, type,
7088 TYPE_TARGET_TYPE (this_type),
7089 TYPE_FIELDS (this_type),
7090 TYPE_NFIELDS (this_type),
7091 TYPE_VARARGS (this_type));
7092
7093 /* Handle static member functions.
7094 Dwarf2 has no clean way to discern C++ static and non-static
7095 member functions. G++ helps GDB by marking the first
7096 parameter for non-static member functions (which is the this
7097 pointer) as artificial. We obtain this information from
7098 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7099 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7100 fnp->voffset = VOFFSET_STATIC;
7101 }
7102 else
7103 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7104 dwarf2_full_name (fieldname, die, cu));
7105
7106 /* Get fcontext from DW_AT_containing_type if present. */
7107 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7108 fnp->fcontext = die_containing_type (die, cu);
7109
7110 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7111 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7112
7113 /* Get accessibility. */
7114 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7115 if (attr)
7116 accessibility = DW_UNSND (attr);
7117 else
7118 accessibility = dwarf2_default_access_attribute (die, cu);
7119 switch (accessibility)
7120 {
7121 case DW_ACCESS_private:
7122 fnp->is_private = 1;
7123 break;
7124 case DW_ACCESS_protected:
7125 fnp->is_protected = 1;
7126 break;
7127 }
7128
7129 /* Check for artificial methods. */
7130 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7131 if (attr && DW_UNSND (attr) != 0)
7132 fnp->is_artificial = 1;
7133
7134 /* Get index in virtual function table if it is a virtual member
7135 function. For older versions of GCC, this is an offset in the
7136 appropriate virtual table, as specified by DW_AT_containing_type.
7137 For everyone else, it is an expression to be evaluated relative
7138 to the object address. */
7139
7140 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7141 if (attr)
7142 {
7143 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7144 {
7145 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7146 {
7147 /* Old-style GCC. */
7148 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7149 }
7150 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7151 || (DW_BLOCK (attr)->size > 1
7152 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7153 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7154 {
7155 struct dwarf_block blk;
7156 int offset;
7157
7158 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7159 ? 1 : 2);
7160 blk.size = DW_BLOCK (attr)->size - offset;
7161 blk.data = DW_BLOCK (attr)->data + offset;
7162 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7163 if ((fnp->voffset % cu->header.addr_size) != 0)
7164 dwarf2_complex_location_expr_complaint ();
7165 else
7166 fnp->voffset /= cu->header.addr_size;
7167 fnp->voffset += 2;
7168 }
7169 else
7170 dwarf2_complex_location_expr_complaint ();
7171
7172 if (!fnp->fcontext)
7173 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7174 }
7175 else if (attr_form_is_section_offset (attr))
7176 {
7177 dwarf2_complex_location_expr_complaint ();
7178 }
7179 else
7180 {
7181 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7182 fieldname);
7183 }
7184 }
7185 else
7186 {
7187 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7188 if (attr && DW_UNSND (attr))
7189 {
7190 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7191 complaint (&symfile_complaints,
7192 _("Member function \"%s\" (offset %d) is virtual "
7193 "but the vtable offset is not specified"),
7194 fieldname, die->offset);
7195 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7196 TYPE_CPLUS_DYNAMIC (type) = 1;
7197 }
7198 }
7199 }
7200
7201 /* Create the vector of member function fields, and attach it to the type. */
7202
7203 static void
7204 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7205 struct dwarf2_cu *cu)
7206 {
7207 struct fnfieldlist *flp;
7208 int total_length = 0;
7209 int i;
7210
7211 if (cu->language == language_ada)
7212 error (_("unexpected member functions in Ada type"));
7213
7214 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7215 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7216 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7217
7218 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7219 {
7220 struct nextfnfield *nfp = flp->head;
7221 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7222 int k;
7223
7224 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7225 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7226 fn_flp->fn_fields = (struct fn_field *)
7227 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7228 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7229 fn_flp->fn_fields[k] = nfp->fnfield;
7230
7231 total_length += flp->length;
7232 }
7233
7234 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7235 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7236 }
7237
7238 /* Returns non-zero if NAME is the name of a vtable member in CU's
7239 language, zero otherwise. */
7240 static int
7241 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7242 {
7243 static const char vptr[] = "_vptr";
7244 static const char vtable[] = "vtable";
7245
7246 /* Look for the C++ and Java forms of the vtable. */
7247 if ((cu->language == language_java
7248 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7249 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7250 && is_cplus_marker (name[sizeof (vptr) - 1])))
7251 return 1;
7252
7253 return 0;
7254 }
7255
7256 /* GCC outputs unnamed structures that are really pointers to member
7257 functions, with the ABI-specified layout. If TYPE describes
7258 such a structure, smash it into a member function type.
7259
7260 GCC shouldn't do this; it should just output pointer to member DIEs.
7261 This is GCC PR debug/28767. */
7262
7263 static void
7264 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7265 {
7266 struct type *pfn_type, *domain_type, *new_type;
7267
7268 /* Check for a structure with no name and two children. */
7269 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7270 return;
7271
7272 /* Check for __pfn and __delta members. */
7273 if (TYPE_FIELD_NAME (type, 0) == NULL
7274 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7275 || TYPE_FIELD_NAME (type, 1) == NULL
7276 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7277 return;
7278
7279 /* Find the type of the method. */
7280 pfn_type = TYPE_FIELD_TYPE (type, 0);
7281 if (pfn_type == NULL
7282 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7283 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7284 return;
7285
7286 /* Look for the "this" argument. */
7287 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7288 if (TYPE_NFIELDS (pfn_type) == 0
7289 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7290 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7291 return;
7292
7293 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7294 new_type = alloc_type (objfile);
7295 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7296 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7297 TYPE_VARARGS (pfn_type));
7298 smash_to_methodptr_type (type, new_type);
7299 }
7300
7301 /* Called when we find the DIE that starts a structure or union scope
7302 (definition) to create a type for the structure or union. Fill in
7303 the type's name and general properties; the members will not be
7304 processed until process_structure_type.
7305
7306 NOTE: we need to call these functions regardless of whether or not the
7307 DIE has a DW_AT_name attribute, since it might be an anonymous
7308 structure or union. This gets the type entered into our set of
7309 user defined types.
7310
7311 However, if the structure is incomplete (an opaque struct/union)
7312 then suppress creating a symbol table entry for it since gdb only
7313 wants to find the one with the complete definition. Note that if
7314 it is complete, we just call new_symbol, which does it's own
7315 checking about whether the struct/union is anonymous or not (and
7316 suppresses creating a symbol table entry itself). */
7317
7318 static struct type *
7319 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7320 {
7321 struct objfile *objfile = cu->objfile;
7322 struct type *type;
7323 struct attribute *attr;
7324 char *name;
7325
7326 /* If the definition of this type lives in .debug_types, read that type.
7327 Don't follow DW_AT_specification though, that will take us back up
7328 the chain and we want to go down. */
7329 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7330 if (attr)
7331 {
7332 struct dwarf2_cu *type_cu = cu;
7333 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7334
7335 /* We could just recurse on read_structure_type, but we need to call
7336 get_die_type to ensure only one type for this DIE is created.
7337 This is important, for example, because for c++ classes we need
7338 TYPE_NAME set which is only done by new_symbol. Blech. */
7339 type = read_type_die (type_die, type_cu);
7340
7341 /* TYPE_CU may not be the same as CU.
7342 Ensure TYPE is recorded in CU's type_hash table. */
7343 return set_die_type (die, type, cu);
7344 }
7345
7346 type = alloc_type (objfile);
7347 INIT_CPLUS_SPECIFIC (type);
7348
7349 name = dwarf2_name (die, cu);
7350 if (name != NULL)
7351 {
7352 if (cu->language == language_cplus
7353 || cu->language == language_java)
7354 {
7355 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7356
7357 /* dwarf2_full_name might have already finished building the DIE's
7358 type. If so, there is no need to continue. */
7359 if (get_die_type (die, cu) != NULL)
7360 return get_die_type (die, cu);
7361
7362 TYPE_TAG_NAME (type) = full_name;
7363 if (die->tag == DW_TAG_structure_type
7364 || die->tag == DW_TAG_class_type)
7365 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7366 }
7367 else
7368 {
7369 /* The name is already allocated along with this objfile, so
7370 we don't need to duplicate it for the type. */
7371 TYPE_TAG_NAME (type) = (char *) name;
7372 if (die->tag == DW_TAG_class_type)
7373 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7374 }
7375 }
7376
7377 if (die->tag == DW_TAG_structure_type)
7378 {
7379 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7380 }
7381 else if (die->tag == DW_TAG_union_type)
7382 {
7383 TYPE_CODE (type) = TYPE_CODE_UNION;
7384 }
7385 else
7386 {
7387 TYPE_CODE (type) = TYPE_CODE_CLASS;
7388 }
7389
7390 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7391 TYPE_DECLARED_CLASS (type) = 1;
7392
7393 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7394 if (attr)
7395 {
7396 TYPE_LENGTH (type) = DW_UNSND (attr);
7397 }
7398 else
7399 {
7400 TYPE_LENGTH (type) = 0;
7401 }
7402
7403 TYPE_STUB_SUPPORTED (type) = 1;
7404 if (die_is_declaration (die, cu))
7405 TYPE_STUB (type) = 1;
7406 else if (attr == NULL && die->child == NULL
7407 && producer_is_realview (cu->producer))
7408 /* RealView does not output the required DW_AT_declaration
7409 on incomplete types. */
7410 TYPE_STUB (type) = 1;
7411
7412 /* We need to add the type field to the die immediately so we don't
7413 infinitely recurse when dealing with pointers to the structure
7414 type within the structure itself. */
7415 set_die_type (die, type, cu);
7416
7417 /* set_die_type should be already done. */
7418 set_descriptive_type (type, die, cu);
7419
7420 return type;
7421 }
7422
7423 /* Finish creating a structure or union type, including filling in
7424 its members and creating a symbol for it. */
7425
7426 static void
7427 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7428 {
7429 struct objfile *objfile = cu->objfile;
7430 struct die_info *child_die = die->child;
7431 struct type *type;
7432
7433 type = get_die_type (die, cu);
7434 if (type == NULL)
7435 type = read_structure_type (die, cu);
7436
7437 if (die->child != NULL && ! die_is_declaration (die, cu))
7438 {
7439 struct field_info fi;
7440 struct die_info *child_die;
7441 VEC (symbolp) *template_args = NULL;
7442 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7443
7444 memset (&fi, 0, sizeof (struct field_info));
7445
7446 child_die = die->child;
7447
7448 while (child_die && child_die->tag)
7449 {
7450 if (child_die->tag == DW_TAG_member
7451 || child_die->tag == DW_TAG_variable)
7452 {
7453 /* NOTE: carlton/2002-11-05: A C++ static data member
7454 should be a DW_TAG_member that is a declaration, but
7455 all versions of G++ as of this writing (so through at
7456 least 3.2.1) incorrectly generate DW_TAG_variable
7457 tags for them instead. */
7458 dwarf2_add_field (&fi, child_die, cu);
7459 }
7460 else if (child_die->tag == DW_TAG_subprogram)
7461 {
7462 /* C++ member function. */
7463 dwarf2_add_member_fn (&fi, child_die, type, cu);
7464 }
7465 else if (child_die->tag == DW_TAG_inheritance)
7466 {
7467 /* C++ base class field. */
7468 dwarf2_add_field (&fi, child_die, cu);
7469 }
7470 else if (child_die->tag == DW_TAG_typedef)
7471 dwarf2_add_typedef (&fi, child_die, cu);
7472 else if (child_die->tag == DW_TAG_template_type_param
7473 || child_die->tag == DW_TAG_template_value_param)
7474 {
7475 struct symbol *arg = new_symbol (child_die, NULL, cu);
7476
7477 if (arg != NULL)
7478 VEC_safe_push (symbolp, template_args, arg);
7479 }
7480
7481 child_die = sibling_die (child_die);
7482 }
7483
7484 /* Attach template arguments to type. */
7485 if (! VEC_empty (symbolp, template_args))
7486 {
7487 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7488 TYPE_N_TEMPLATE_ARGUMENTS (type)
7489 = VEC_length (symbolp, template_args);
7490 TYPE_TEMPLATE_ARGUMENTS (type)
7491 = obstack_alloc (&objfile->objfile_obstack,
7492 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7493 * sizeof (struct symbol *)));
7494 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7495 VEC_address (symbolp, template_args),
7496 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7497 * sizeof (struct symbol *)));
7498 VEC_free (symbolp, template_args);
7499 }
7500
7501 /* Attach fields and member functions to the type. */
7502 if (fi.nfields)
7503 dwarf2_attach_fields_to_type (&fi, type, cu);
7504 if (fi.nfnfields)
7505 {
7506 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7507
7508 /* Get the type which refers to the base class (possibly this
7509 class itself) which contains the vtable pointer for the current
7510 class from the DW_AT_containing_type attribute. This use of
7511 DW_AT_containing_type is a GNU extension. */
7512
7513 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7514 {
7515 struct type *t = die_containing_type (die, cu);
7516
7517 TYPE_VPTR_BASETYPE (type) = t;
7518 if (type == t)
7519 {
7520 int i;
7521
7522 /* Our own class provides vtbl ptr. */
7523 for (i = TYPE_NFIELDS (t) - 1;
7524 i >= TYPE_N_BASECLASSES (t);
7525 --i)
7526 {
7527 char *fieldname = TYPE_FIELD_NAME (t, i);
7528
7529 if (is_vtable_name (fieldname, cu))
7530 {
7531 TYPE_VPTR_FIELDNO (type) = i;
7532 break;
7533 }
7534 }
7535
7536 /* Complain if virtual function table field not found. */
7537 if (i < TYPE_N_BASECLASSES (t))
7538 complaint (&symfile_complaints,
7539 _("virtual function table pointer "
7540 "not found when defining class '%s'"),
7541 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7542 "");
7543 }
7544 else
7545 {
7546 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7547 }
7548 }
7549 else if (cu->producer
7550 && strncmp (cu->producer,
7551 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7552 {
7553 /* The IBM XLC compiler does not provide direct indication
7554 of the containing type, but the vtable pointer is
7555 always named __vfp. */
7556
7557 int i;
7558
7559 for (i = TYPE_NFIELDS (type) - 1;
7560 i >= TYPE_N_BASECLASSES (type);
7561 --i)
7562 {
7563 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7564 {
7565 TYPE_VPTR_FIELDNO (type) = i;
7566 TYPE_VPTR_BASETYPE (type) = type;
7567 break;
7568 }
7569 }
7570 }
7571 }
7572
7573 /* Copy fi.typedef_field_list linked list elements content into the
7574 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7575 if (fi.typedef_field_list)
7576 {
7577 int i = fi.typedef_field_list_count;
7578
7579 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7580 TYPE_TYPEDEF_FIELD_ARRAY (type)
7581 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7582 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7583
7584 /* Reverse the list order to keep the debug info elements order. */
7585 while (--i >= 0)
7586 {
7587 struct typedef_field *dest, *src;
7588
7589 dest = &TYPE_TYPEDEF_FIELD (type, i);
7590 src = &fi.typedef_field_list->field;
7591 fi.typedef_field_list = fi.typedef_field_list->next;
7592 *dest = *src;
7593 }
7594 }
7595
7596 do_cleanups (back_to);
7597
7598 if (HAVE_CPLUS_STRUCT (type))
7599 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7600 }
7601
7602 quirk_gcc_member_function_pointer (type, cu->objfile);
7603
7604 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7605 snapshots) has been known to create a die giving a declaration
7606 for a class that has, as a child, a die giving a definition for a
7607 nested class. So we have to process our children even if the
7608 current die is a declaration. Normally, of course, a declaration
7609 won't have any children at all. */
7610
7611 while (child_die != NULL && child_die->tag)
7612 {
7613 if (child_die->tag == DW_TAG_member
7614 || child_die->tag == DW_TAG_variable
7615 || child_die->tag == DW_TAG_inheritance
7616 || child_die->tag == DW_TAG_template_value_param
7617 || child_die->tag == DW_TAG_template_type_param)
7618 {
7619 /* Do nothing. */
7620 }
7621 else
7622 process_die (child_die, cu);
7623
7624 child_die = sibling_die (child_die);
7625 }
7626
7627 /* Do not consider external references. According to the DWARF standard,
7628 these DIEs are identified by the fact that they have no byte_size
7629 attribute, and a declaration attribute. */
7630 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7631 || !die_is_declaration (die, cu))
7632 new_symbol (die, type, cu);
7633 }
7634
7635 /* Given a DW_AT_enumeration_type die, set its type. We do not
7636 complete the type's fields yet, or create any symbols. */
7637
7638 static struct type *
7639 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7640 {
7641 struct objfile *objfile = cu->objfile;
7642 struct type *type;
7643 struct attribute *attr;
7644 const char *name;
7645
7646 /* If the definition of this type lives in .debug_types, read that type.
7647 Don't follow DW_AT_specification though, that will take us back up
7648 the chain and we want to go down. */
7649 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7650 if (attr)
7651 {
7652 struct dwarf2_cu *type_cu = cu;
7653 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7654
7655 type = read_type_die (type_die, type_cu);
7656
7657 /* TYPE_CU may not be the same as CU.
7658 Ensure TYPE is recorded in CU's type_hash table. */
7659 return set_die_type (die, type, cu);
7660 }
7661
7662 type = alloc_type (objfile);
7663
7664 TYPE_CODE (type) = TYPE_CODE_ENUM;
7665 name = dwarf2_full_name (NULL, die, cu);
7666 if (name != NULL)
7667 TYPE_TAG_NAME (type) = (char *) name;
7668
7669 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7670 if (attr)
7671 {
7672 TYPE_LENGTH (type) = DW_UNSND (attr);
7673 }
7674 else
7675 {
7676 TYPE_LENGTH (type) = 0;
7677 }
7678
7679 /* The enumeration DIE can be incomplete. In Ada, any type can be
7680 declared as private in the package spec, and then defined only
7681 inside the package body. Such types are known as Taft Amendment
7682 Types. When another package uses such a type, an incomplete DIE
7683 may be generated by the compiler. */
7684 if (die_is_declaration (die, cu))
7685 TYPE_STUB (type) = 1;
7686
7687 return set_die_type (die, type, cu);
7688 }
7689
7690 /* Given a pointer to a die which begins an enumeration, process all
7691 the dies that define the members of the enumeration, and create the
7692 symbol for the enumeration type.
7693
7694 NOTE: We reverse the order of the element list. */
7695
7696 static void
7697 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7698 {
7699 struct type *this_type;
7700
7701 this_type = get_die_type (die, cu);
7702 if (this_type == NULL)
7703 this_type = read_enumeration_type (die, cu);
7704
7705 if (die->child != NULL)
7706 {
7707 struct die_info *child_die;
7708 struct symbol *sym;
7709 struct field *fields = NULL;
7710 int num_fields = 0;
7711 int unsigned_enum = 1;
7712 char *name;
7713
7714 child_die = die->child;
7715 while (child_die && child_die->tag)
7716 {
7717 if (child_die->tag != DW_TAG_enumerator)
7718 {
7719 process_die (child_die, cu);
7720 }
7721 else
7722 {
7723 name = dwarf2_name (child_die, cu);
7724 if (name)
7725 {
7726 sym = new_symbol (child_die, this_type, cu);
7727 if (SYMBOL_VALUE (sym) < 0)
7728 unsigned_enum = 0;
7729
7730 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7731 {
7732 fields = (struct field *)
7733 xrealloc (fields,
7734 (num_fields + DW_FIELD_ALLOC_CHUNK)
7735 * sizeof (struct field));
7736 }
7737
7738 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7739 FIELD_TYPE (fields[num_fields]) = NULL;
7740 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7741 FIELD_BITSIZE (fields[num_fields]) = 0;
7742
7743 num_fields++;
7744 }
7745 }
7746
7747 child_die = sibling_die (child_die);
7748 }
7749
7750 if (num_fields)
7751 {
7752 TYPE_NFIELDS (this_type) = num_fields;
7753 TYPE_FIELDS (this_type) = (struct field *)
7754 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7755 memcpy (TYPE_FIELDS (this_type), fields,
7756 sizeof (struct field) * num_fields);
7757 xfree (fields);
7758 }
7759 if (unsigned_enum)
7760 TYPE_UNSIGNED (this_type) = 1;
7761 }
7762
7763 /* If we are reading an enum from a .debug_types unit, and the enum
7764 is a declaration, and the enum is not the signatured type in the
7765 unit, then we do not want to add a symbol for it. Adding a
7766 symbol would in some cases obscure the true definition of the
7767 enum, giving users an incomplete type when the definition is
7768 actually available. Note that we do not want to do this for all
7769 enums which are just declarations, because C++0x allows forward
7770 enum declarations. */
7771 if (cu->per_cu->debug_type_section
7772 && die_is_declaration (die, cu))
7773 {
7774 struct signatured_type *type_sig;
7775
7776 type_sig
7777 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7778 cu->per_cu->debug_type_section,
7779 cu->per_cu->offset);
7780 if (type_sig->type_offset != die->offset)
7781 return;
7782 }
7783
7784 new_symbol (die, this_type, cu);
7785 }
7786
7787 /* Extract all information from a DW_TAG_array_type DIE and put it in
7788 the DIE's type field. For now, this only handles one dimensional
7789 arrays. */
7790
7791 static struct type *
7792 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7793 {
7794 struct objfile *objfile = cu->objfile;
7795 struct die_info *child_die;
7796 struct type *type;
7797 struct type *element_type, *range_type, *index_type;
7798 struct type **range_types = NULL;
7799 struct attribute *attr;
7800 int ndim = 0;
7801 struct cleanup *back_to;
7802 char *name;
7803
7804 element_type = die_type (die, cu);
7805
7806 /* The die_type call above may have already set the type for this DIE. */
7807 type = get_die_type (die, cu);
7808 if (type)
7809 return type;
7810
7811 /* Irix 6.2 native cc creates array types without children for
7812 arrays with unspecified length. */
7813 if (die->child == NULL)
7814 {
7815 index_type = objfile_type (objfile)->builtin_int;
7816 range_type = create_range_type (NULL, index_type, 0, -1);
7817 type = create_array_type (NULL, element_type, range_type);
7818 return set_die_type (die, type, cu);
7819 }
7820
7821 back_to = make_cleanup (null_cleanup, NULL);
7822 child_die = die->child;
7823 while (child_die && child_die->tag)
7824 {
7825 if (child_die->tag == DW_TAG_subrange_type)
7826 {
7827 struct type *child_type = read_type_die (child_die, cu);
7828
7829 if (child_type != NULL)
7830 {
7831 /* The range type was succesfully read. Save it for the
7832 array type creation. */
7833 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7834 {
7835 range_types = (struct type **)
7836 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7837 * sizeof (struct type *));
7838 if (ndim == 0)
7839 make_cleanup (free_current_contents, &range_types);
7840 }
7841 range_types[ndim++] = child_type;
7842 }
7843 }
7844 child_die = sibling_die (child_die);
7845 }
7846
7847 /* Dwarf2 dimensions are output from left to right, create the
7848 necessary array types in backwards order. */
7849
7850 type = element_type;
7851
7852 if (read_array_order (die, cu) == DW_ORD_col_major)
7853 {
7854 int i = 0;
7855
7856 while (i < ndim)
7857 type = create_array_type (NULL, type, range_types[i++]);
7858 }
7859 else
7860 {
7861 while (ndim-- > 0)
7862 type = create_array_type (NULL, type, range_types[ndim]);
7863 }
7864
7865 /* Understand Dwarf2 support for vector types (like they occur on
7866 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7867 array type. This is not part of the Dwarf2/3 standard yet, but a
7868 custom vendor extension. The main difference between a regular
7869 array and the vector variant is that vectors are passed by value
7870 to functions. */
7871 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7872 if (attr)
7873 make_vector_type (type);
7874
7875 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7876 implementation may choose to implement triple vectors using this
7877 attribute. */
7878 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7879 if (attr)
7880 {
7881 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7882 TYPE_LENGTH (type) = DW_UNSND (attr);
7883 else
7884 complaint (&symfile_complaints,
7885 _("DW_AT_byte_size for array type smaller "
7886 "than the total size of elements"));
7887 }
7888
7889 name = dwarf2_name (die, cu);
7890 if (name)
7891 TYPE_NAME (type) = name;
7892
7893 /* Install the type in the die. */
7894 set_die_type (die, type, cu);
7895
7896 /* set_die_type should be already done. */
7897 set_descriptive_type (type, die, cu);
7898
7899 do_cleanups (back_to);
7900
7901 return type;
7902 }
7903
7904 static enum dwarf_array_dim_ordering
7905 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7906 {
7907 struct attribute *attr;
7908
7909 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7910
7911 if (attr) return DW_SND (attr);
7912
7913 /* GNU F77 is a special case, as at 08/2004 array type info is the
7914 opposite order to the dwarf2 specification, but data is still
7915 laid out as per normal fortran.
7916
7917 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7918 version checking. */
7919
7920 if (cu->language == language_fortran
7921 && cu->producer && strstr (cu->producer, "GNU F77"))
7922 {
7923 return DW_ORD_row_major;
7924 }
7925
7926 switch (cu->language_defn->la_array_ordering)
7927 {
7928 case array_column_major:
7929 return DW_ORD_col_major;
7930 case array_row_major:
7931 default:
7932 return DW_ORD_row_major;
7933 };
7934 }
7935
7936 /* Extract all information from a DW_TAG_set_type DIE and put it in
7937 the DIE's type field. */
7938
7939 static struct type *
7940 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7941 {
7942 struct type *domain_type, *set_type;
7943 struct attribute *attr;
7944
7945 domain_type = die_type (die, cu);
7946
7947 /* The die_type call above may have already set the type for this DIE. */
7948 set_type = get_die_type (die, cu);
7949 if (set_type)
7950 return set_type;
7951
7952 set_type = create_set_type (NULL, domain_type);
7953
7954 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7955 if (attr)
7956 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7957
7958 return set_die_type (die, set_type, cu);
7959 }
7960
7961 /* First cut: install each common block member as a global variable. */
7962
7963 static void
7964 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7965 {
7966 struct die_info *child_die;
7967 struct attribute *attr;
7968 struct symbol *sym;
7969 CORE_ADDR base = (CORE_ADDR) 0;
7970
7971 attr = dwarf2_attr (die, DW_AT_location, cu);
7972 if (attr)
7973 {
7974 /* Support the .debug_loc offsets. */
7975 if (attr_form_is_block (attr))
7976 {
7977 base = decode_locdesc (DW_BLOCK (attr), cu);
7978 }
7979 else if (attr_form_is_section_offset (attr))
7980 {
7981 dwarf2_complex_location_expr_complaint ();
7982 }
7983 else
7984 {
7985 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7986 "common block member");
7987 }
7988 }
7989 if (die->child != NULL)
7990 {
7991 child_die = die->child;
7992 while (child_die && child_die->tag)
7993 {
7994 LONGEST offset;
7995
7996 sym = new_symbol (child_die, NULL, cu);
7997 if (sym != NULL
7998 && handle_data_member_location (child_die, cu, &offset))
7999 {
8000 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8001 add_symbol_to_list (sym, &global_symbols);
8002 }
8003 child_die = sibling_die (child_die);
8004 }
8005 }
8006 }
8007
8008 /* Create a type for a C++ namespace. */
8009
8010 static struct type *
8011 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8012 {
8013 struct objfile *objfile = cu->objfile;
8014 const char *previous_prefix, *name;
8015 int is_anonymous;
8016 struct type *type;
8017
8018 /* For extensions, reuse the type of the original namespace. */
8019 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8020 {
8021 struct die_info *ext_die;
8022 struct dwarf2_cu *ext_cu = cu;
8023
8024 ext_die = dwarf2_extension (die, &ext_cu);
8025 type = read_type_die (ext_die, ext_cu);
8026
8027 /* EXT_CU may not be the same as CU.
8028 Ensure TYPE is recorded in CU's type_hash table. */
8029 return set_die_type (die, type, cu);
8030 }
8031
8032 name = namespace_name (die, &is_anonymous, cu);
8033
8034 /* Now build the name of the current namespace. */
8035
8036 previous_prefix = determine_prefix (die, cu);
8037 if (previous_prefix[0] != '\0')
8038 name = typename_concat (&objfile->objfile_obstack,
8039 previous_prefix, name, 0, cu);
8040
8041 /* Create the type. */
8042 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8043 objfile);
8044 TYPE_NAME (type) = (char *) name;
8045 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8046
8047 return set_die_type (die, type, cu);
8048 }
8049
8050 /* Read a C++ namespace. */
8051
8052 static void
8053 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8054 {
8055 struct objfile *objfile = cu->objfile;
8056 int is_anonymous;
8057
8058 /* Add a symbol associated to this if we haven't seen the namespace
8059 before. Also, add a using directive if it's an anonymous
8060 namespace. */
8061
8062 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8063 {
8064 struct type *type;
8065
8066 type = read_type_die (die, cu);
8067 new_symbol (die, type, cu);
8068
8069 namespace_name (die, &is_anonymous, cu);
8070 if (is_anonymous)
8071 {
8072 const char *previous_prefix = determine_prefix (die, cu);
8073
8074 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8075 NULL, NULL, &objfile->objfile_obstack);
8076 }
8077 }
8078
8079 if (die->child != NULL)
8080 {
8081 struct die_info *child_die = die->child;
8082
8083 while (child_die && child_die->tag)
8084 {
8085 process_die (child_die, cu);
8086 child_die = sibling_die (child_die);
8087 }
8088 }
8089 }
8090
8091 /* Read a Fortran module as type. This DIE can be only a declaration used for
8092 imported module. Still we need that type as local Fortran "use ... only"
8093 declaration imports depend on the created type in determine_prefix. */
8094
8095 static struct type *
8096 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8097 {
8098 struct objfile *objfile = cu->objfile;
8099 char *module_name;
8100 struct type *type;
8101
8102 module_name = dwarf2_name (die, cu);
8103 if (!module_name)
8104 complaint (&symfile_complaints,
8105 _("DW_TAG_module has no name, offset 0x%x"),
8106 die->offset);
8107 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8108
8109 /* determine_prefix uses TYPE_TAG_NAME. */
8110 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8111
8112 return set_die_type (die, type, cu);
8113 }
8114
8115 /* Read a Fortran module. */
8116
8117 static void
8118 read_module (struct die_info *die, struct dwarf2_cu *cu)
8119 {
8120 struct die_info *child_die = die->child;
8121
8122 while (child_die && child_die->tag)
8123 {
8124 process_die (child_die, cu);
8125 child_die = sibling_die (child_die);
8126 }
8127 }
8128
8129 /* Return the name of the namespace represented by DIE. Set
8130 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8131 namespace. */
8132
8133 static const char *
8134 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8135 {
8136 struct die_info *current_die;
8137 const char *name = NULL;
8138
8139 /* Loop through the extensions until we find a name. */
8140
8141 for (current_die = die;
8142 current_die != NULL;
8143 current_die = dwarf2_extension (die, &cu))
8144 {
8145 name = dwarf2_name (current_die, cu);
8146 if (name != NULL)
8147 break;
8148 }
8149
8150 /* Is it an anonymous namespace? */
8151
8152 *is_anonymous = (name == NULL);
8153 if (*is_anonymous)
8154 name = CP_ANONYMOUS_NAMESPACE_STR;
8155
8156 return name;
8157 }
8158
8159 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8160 the user defined type vector. */
8161
8162 static struct type *
8163 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8164 {
8165 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8166 struct comp_unit_head *cu_header = &cu->header;
8167 struct type *type;
8168 struct attribute *attr_byte_size;
8169 struct attribute *attr_address_class;
8170 int byte_size, addr_class;
8171 struct type *target_type;
8172
8173 target_type = die_type (die, cu);
8174
8175 /* The die_type call above may have already set the type for this DIE. */
8176 type = get_die_type (die, cu);
8177 if (type)
8178 return type;
8179
8180 type = lookup_pointer_type (target_type);
8181
8182 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8183 if (attr_byte_size)
8184 byte_size = DW_UNSND (attr_byte_size);
8185 else
8186 byte_size = cu_header->addr_size;
8187
8188 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8189 if (attr_address_class)
8190 addr_class = DW_UNSND (attr_address_class);
8191 else
8192 addr_class = DW_ADDR_none;
8193
8194 /* If the pointer size or address class is different than the
8195 default, create a type variant marked as such and set the
8196 length accordingly. */
8197 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8198 {
8199 if (gdbarch_address_class_type_flags_p (gdbarch))
8200 {
8201 int type_flags;
8202
8203 type_flags = gdbarch_address_class_type_flags
8204 (gdbarch, byte_size, addr_class);
8205 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8206 == 0);
8207 type = make_type_with_address_space (type, type_flags);
8208 }
8209 else if (TYPE_LENGTH (type) != byte_size)
8210 {
8211 complaint (&symfile_complaints,
8212 _("invalid pointer size %d"), byte_size);
8213 }
8214 else
8215 {
8216 /* Should we also complain about unhandled address classes? */
8217 }
8218 }
8219
8220 TYPE_LENGTH (type) = byte_size;
8221 return set_die_type (die, type, cu);
8222 }
8223
8224 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8225 the user defined type vector. */
8226
8227 static struct type *
8228 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8229 {
8230 struct type *type;
8231 struct type *to_type;
8232 struct type *domain;
8233
8234 to_type = die_type (die, cu);
8235 domain = die_containing_type (die, cu);
8236
8237 /* The calls above may have already set the type for this DIE. */
8238 type = get_die_type (die, cu);
8239 if (type)
8240 return type;
8241
8242 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8243 type = lookup_methodptr_type (to_type);
8244 else
8245 type = lookup_memberptr_type (to_type, domain);
8246
8247 return set_die_type (die, type, cu);
8248 }
8249
8250 /* Extract all information from a DW_TAG_reference_type DIE and add to
8251 the user defined type vector. */
8252
8253 static struct type *
8254 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8255 {
8256 struct comp_unit_head *cu_header = &cu->header;
8257 struct type *type, *target_type;
8258 struct attribute *attr;
8259
8260 target_type = die_type (die, cu);
8261
8262 /* The die_type call above may have already set the type for this DIE. */
8263 type = get_die_type (die, cu);
8264 if (type)
8265 return type;
8266
8267 type = lookup_reference_type (target_type);
8268 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8269 if (attr)
8270 {
8271 TYPE_LENGTH (type) = DW_UNSND (attr);
8272 }
8273 else
8274 {
8275 TYPE_LENGTH (type) = cu_header->addr_size;
8276 }
8277 return set_die_type (die, type, cu);
8278 }
8279
8280 static struct type *
8281 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8282 {
8283 struct type *base_type, *cv_type;
8284
8285 base_type = die_type (die, cu);
8286
8287 /* The die_type call above may have already set the type for this DIE. */
8288 cv_type = get_die_type (die, cu);
8289 if (cv_type)
8290 return cv_type;
8291
8292 /* In case the const qualifier is applied to an array type, the element type
8293 is so qualified, not the array type (section 6.7.3 of C99). */
8294 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8295 {
8296 struct type *el_type, *inner_array;
8297
8298 base_type = copy_type (base_type);
8299 inner_array = base_type;
8300
8301 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8302 {
8303 TYPE_TARGET_TYPE (inner_array) =
8304 copy_type (TYPE_TARGET_TYPE (inner_array));
8305 inner_array = TYPE_TARGET_TYPE (inner_array);
8306 }
8307
8308 el_type = TYPE_TARGET_TYPE (inner_array);
8309 TYPE_TARGET_TYPE (inner_array) =
8310 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8311
8312 return set_die_type (die, base_type, cu);
8313 }
8314
8315 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8316 return set_die_type (die, cv_type, cu);
8317 }
8318
8319 static struct type *
8320 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8321 {
8322 struct type *base_type, *cv_type;
8323
8324 base_type = die_type (die, cu);
8325
8326 /* The die_type call above may have already set the type for this DIE. */
8327 cv_type = get_die_type (die, cu);
8328 if (cv_type)
8329 return cv_type;
8330
8331 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8332 return set_die_type (die, cv_type, cu);
8333 }
8334
8335 /* Extract all information from a DW_TAG_string_type DIE and add to
8336 the user defined type vector. It isn't really a user defined type,
8337 but it behaves like one, with other DIE's using an AT_user_def_type
8338 attribute to reference it. */
8339
8340 static struct type *
8341 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8342 {
8343 struct objfile *objfile = cu->objfile;
8344 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8345 struct type *type, *range_type, *index_type, *char_type;
8346 struct attribute *attr;
8347 unsigned int length;
8348
8349 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8350 if (attr)
8351 {
8352 length = DW_UNSND (attr);
8353 }
8354 else
8355 {
8356 /* Check for the DW_AT_byte_size attribute. */
8357 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8358 if (attr)
8359 {
8360 length = DW_UNSND (attr);
8361 }
8362 else
8363 {
8364 length = 1;
8365 }
8366 }
8367
8368 index_type = objfile_type (objfile)->builtin_int;
8369 range_type = create_range_type (NULL, index_type, 1, length);
8370 char_type = language_string_char_type (cu->language_defn, gdbarch);
8371 type = create_string_type (NULL, char_type, range_type);
8372
8373 return set_die_type (die, type, cu);
8374 }
8375
8376 /* Handle DIES due to C code like:
8377
8378 struct foo
8379 {
8380 int (*funcp)(int a, long l);
8381 int b;
8382 };
8383
8384 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8385
8386 static struct type *
8387 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8388 {
8389 struct type *type; /* Type that this function returns. */
8390 struct type *ftype; /* Function that returns above type. */
8391 struct attribute *attr;
8392
8393 type = die_type (die, cu);
8394
8395 /* The die_type call above may have already set the type for this DIE. */
8396 ftype = get_die_type (die, cu);
8397 if (ftype)
8398 return ftype;
8399
8400 ftype = lookup_function_type (type);
8401
8402 /* All functions in C++, Pascal and Java have prototypes. */
8403 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8404 if ((attr && (DW_UNSND (attr) != 0))
8405 || cu->language == language_cplus
8406 || cu->language == language_java
8407 || cu->language == language_pascal)
8408 TYPE_PROTOTYPED (ftype) = 1;
8409 else if (producer_is_realview (cu->producer))
8410 /* RealView does not emit DW_AT_prototyped. We can not
8411 distinguish prototyped and unprototyped functions; default to
8412 prototyped, since that is more common in modern code (and
8413 RealView warns about unprototyped functions). */
8414 TYPE_PROTOTYPED (ftype) = 1;
8415
8416 /* Store the calling convention in the type if it's available in
8417 the subroutine die. Otherwise set the calling convention to
8418 the default value DW_CC_normal. */
8419 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8420 if (attr)
8421 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8422 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8423 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8424 else
8425 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8426
8427 /* We need to add the subroutine type to the die immediately so
8428 we don't infinitely recurse when dealing with parameters
8429 declared as the same subroutine type. */
8430 set_die_type (die, ftype, cu);
8431
8432 if (die->child != NULL)
8433 {
8434 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8435 struct die_info *child_die;
8436 int nparams, iparams;
8437
8438 /* Count the number of parameters.
8439 FIXME: GDB currently ignores vararg functions, but knows about
8440 vararg member functions. */
8441 nparams = 0;
8442 child_die = die->child;
8443 while (child_die && child_die->tag)
8444 {
8445 if (child_die->tag == DW_TAG_formal_parameter)
8446 nparams++;
8447 else if (child_die->tag == DW_TAG_unspecified_parameters)
8448 TYPE_VARARGS (ftype) = 1;
8449 child_die = sibling_die (child_die);
8450 }
8451
8452 /* Allocate storage for parameters and fill them in. */
8453 TYPE_NFIELDS (ftype) = nparams;
8454 TYPE_FIELDS (ftype) = (struct field *)
8455 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8456
8457 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8458 even if we error out during the parameters reading below. */
8459 for (iparams = 0; iparams < nparams; iparams++)
8460 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8461
8462 iparams = 0;
8463 child_die = die->child;
8464 while (child_die && child_die->tag)
8465 {
8466 if (child_die->tag == DW_TAG_formal_parameter)
8467 {
8468 struct type *arg_type;
8469
8470 /* DWARF version 2 has no clean way to discern C++
8471 static and non-static member functions. G++ helps
8472 GDB by marking the first parameter for non-static
8473 member functions (which is the this pointer) as
8474 artificial. We pass this information to
8475 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8476
8477 DWARF version 3 added DW_AT_object_pointer, which GCC
8478 4.5 does not yet generate. */
8479 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8480 if (attr)
8481 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8482 else
8483 {
8484 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8485
8486 /* GCC/43521: In java, the formal parameter
8487 "this" is sometimes not marked with DW_AT_artificial. */
8488 if (cu->language == language_java)
8489 {
8490 const char *name = dwarf2_name (child_die, cu);
8491
8492 if (name && !strcmp (name, "this"))
8493 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8494 }
8495 }
8496 arg_type = die_type (child_die, cu);
8497
8498 /* RealView does not mark THIS as const, which the testsuite
8499 expects. GCC marks THIS as const in method definitions,
8500 but not in the class specifications (GCC PR 43053). */
8501 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8502 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8503 {
8504 int is_this = 0;
8505 struct dwarf2_cu *arg_cu = cu;
8506 const char *name = dwarf2_name (child_die, cu);
8507
8508 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8509 if (attr)
8510 {
8511 /* If the compiler emits this, use it. */
8512 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8513 is_this = 1;
8514 }
8515 else if (name && strcmp (name, "this") == 0)
8516 /* Function definitions will have the argument names. */
8517 is_this = 1;
8518 else if (name == NULL && iparams == 0)
8519 /* Declarations may not have the names, so like
8520 elsewhere in GDB, assume an artificial first
8521 argument is "this". */
8522 is_this = 1;
8523
8524 if (is_this)
8525 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8526 arg_type, 0);
8527 }
8528
8529 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8530 iparams++;
8531 }
8532 child_die = sibling_die (child_die);
8533 }
8534 }
8535
8536 return ftype;
8537 }
8538
8539 static struct type *
8540 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8541 {
8542 struct objfile *objfile = cu->objfile;
8543 const char *name = NULL;
8544 struct type *this_type;
8545
8546 name = dwarf2_full_name (NULL, die, cu);
8547 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8548 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8549 TYPE_NAME (this_type) = (char *) name;
8550 set_die_type (die, this_type, cu);
8551 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8552 return this_type;
8553 }
8554
8555 /* Find a representation of a given base type and install
8556 it in the TYPE field of the die. */
8557
8558 static struct type *
8559 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8560 {
8561 struct objfile *objfile = cu->objfile;
8562 struct type *type;
8563 struct attribute *attr;
8564 int encoding = 0, size = 0;
8565 char *name;
8566 enum type_code code = TYPE_CODE_INT;
8567 int type_flags = 0;
8568 struct type *target_type = NULL;
8569
8570 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8571 if (attr)
8572 {
8573 encoding = DW_UNSND (attr);
8574 }
8575 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8576 if (attr)
8577 {
8578 size = DW_UNSND (attr);
8579 }
8580 name = dwarf2_name (die, cu);
8581 if (!name)
8582 {
8583 complaint (&symfile_complaints,
8584 _("DW_AT_name missing from DW_TAG_base_type"));
8585 }
8586
8587 switch (encoding)
8588 {
8589 case DW_ATE_address:
8590 /* Turn DW_ATE_address into a void * pointer. */
8591 code = TYPE_CODE_PTR;
8592 type_flags |= TYPE_FLAG_UNSIGNED;
8593 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8594 break;
8595 case DW_ATE_boolean:
8596 code = TYPE_CODE_BOOL;
8597 type_flags |= TYPE_FLAG_UNSIGNED;
8598 break;
8599 case DW_ATE_complex_float:
8600 code = TYPE_CODE_COMPLEX;
8601 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8602 break;
8603 case DW_ATE_decimal_float:
8604 code = TYPE_CODE_DECFLOAT;
8605 break;
8606 case DW_ATE_float:
8607 code = TYPE_CODE_FLT;
8608 break;
8609 case DW_ATE_signed:
8610 break;
8611 case DW_ATE_unsigned:
8612 type_flags |= TYPE_FLAG_UNSIGNED;
8613 if (cu->language == language_fortran
8614 && name
8615 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8616 code = TYPE_CODE_CHAR;
8617 break;
8618 case DW_ATE_signed_char:
8619 if (cu->language == language_ada || cu->language == language_m2
8620 || cu->language == language_pascal
8621 || cu->language == language_fortran)
8622 code = TYPE_CODE_CHAR;
8623 break;
8624 case DW_ATE_unsigned_char:
8625 if (cu->language == language_ada || cu->language == language_m2
8626 || cu->language == language_pascal
8627 || cu->language == language_fortran)
8628 code = TYPE_CODE_CHAR;
8629 type_flags |= TYPE_FLAG_UNSIGNED;
8630 break;
8631 case DW_ATE_UTF:
8632 /* We just treat this as an integer and then recognize the
8633 type by name elsewhere. */
8634 break;
8635
8636 default:
8637 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8638 dwarf_type_encoding_name (encoding));
8639 break;
8640 }
8641
8642 type = init_type (code, size, type_flags, NULL, objfile);
8643 TYPE_NAME (type) = name;
8644 TYPE_TARGET_TYPE (type) = target_type;
8645
8646 if (name && strcmp (name, "char") == 0)
8647 TYPE_NOSIGN (type) = 1;
8648
8649 return set_die_type (die, type, cu);
8650 }
8651
8652 /* Read the given DW_AT_subrange DIE. */
8653
8654 static struct type *
8655 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8656 {
8657 struct type *base_type;
8658 struct type *range_type;
8659 struct attribute *attr;
8660 LONGEST low = 0;
8661 LONGEST high = -1;
8662 char *name;
8663 LONGEST negative_mask;
8664
8665 base_type = die_type (die, cu);
8666 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8667 check_typedef (base_type);
8668
8669 /* The die_type call above may have already set the type for this DIE. */
8670 range_type = get_die_type (die, cu);
8671 if (range_type)
8672 return range_type;
8673
8674 if (cu->language == language_fortran)
8675 {
8676 /* FORTRAN implies a lower bound of 1, if not given. */
8677 low = 1;
8678 }
8679
8680 /* FIXME: For variable sized arrays either of these could be
8681 a variable rather than a constant value. We'll allow it,
8682 but we don't know how to handle it. */
8683 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8684 if (attr)
8685 low = dwarf2_get_attr_constant_value (attr, 0);
8686
8687 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8688 if (attr)
8689 {
8690 if (attr_form_is_block (attr) || is_ref_attr (attr))
8691 {
8692 /* GCC encodes arrays with unspecified or dynamic length
8693 with a DW_FORM_block1 attribute or a reference attribute.
8694 FIXME: GDB does not yet know how to handle dynamic
8695 arrays properly, treat them as arrays with unspecified
8696 length for now.
8697
8698 FIXME: jimb/2003-09-22: GDB does not really know
8699 how to handle arrays of unspecified length
8700 either; we just represent them as zero-length
8701 arrays. Choose an appropriate upper bound given
8702 the lower bound we've computed above. */
8703 high = low - 1;
8704 }
8705 else
8706 high = dwarf2_get_attr_constant_value (attr, 1);
8707 }
8708 else
8709 {
8710 attr = dwarf2_attr (die, DW_AT_count, cu);
8711 if (attr)
8712 {
8713 int count = dwarf2_get_attr_constant_value (attr, 1);
8714 high = low + count - 1;
8715 }
8716 else
8717 {
8718 /* Unspecified array length. */
8719 high = low - 1;
8720 }
8721 }
8722
8723 /* Dwarf-2 specifications explicitly allows to create subrange types
8724 without specifying a base type.
8725 In that case, the base type must be set to the type of
8726 the lower bound, upper bound or count, in that order, if any of these
8727 three attributes references an object that has a type.
8728 If no base type is found, the Dwarf-2 specifications say that
8729 a signed integer type of size equal to the size of an address should
8730 be used.
8731 For the following C code: `extern char gdb_int [];'
8732 GCC produces an empty range DIE.
8733 FIXME: muller/2010-05-28: Possible references to object for low bound,
8734 high bound or count are not yet handled by this code. */
8735 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8736 {
8737 struct objfile *objfile = cu->objfile;
8738 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8739 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8740 struct type *int_type = objfile_type (objfile)->builtin_int;
8741
8742 /* Test "int", "long int", and "long long int" objfile types,
8743 and select the first one having a size above or equal to the
8744 architecture address size. */
8745 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8746 base_type = int_type;
8747 else
8748 {
8749 int_type = objfile_type (objfile)->builtin_long;
8750 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8751 base_type = int_type;
8752 else
8753 {
8754 int_type = objfile_type (objfile)->builtin_long_long;
8755 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8756 base_type = int_type;
8757 }
8758 }
8759 }
8760
8761 negative_mask =
8762 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8763 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8764 low |= negative_mask;
8765 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8766 high |= negative_mask;
8767
8768 range_type = create_range_type (NULL, base_type, low, high);
8769
8770 /* Mark arrays with dynamic length at least as an array of unspecified
8771 length. GDB could check the boundary but before it gets implemented at
8772 least allow accessing the array elements. */
8773 if (attr && attr_form_is_block (attr))
8774 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8775
8776 /* Ada expects an empty array on no boundary attributes. */
8777 if (attr == NULL && cu->language != language_ada)
8778 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8779
8780 name = dwarf2_name (die, cu);
8781 if (name)
8782 TYPE_NAME (range_type) = name;
8783
8784 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8785 if (attr)
8786 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8787
8788 set_die_type (die, range_type, cu);
8789
8790 /* set_die_type should be already done. */
8791 set_descriptive_type (range_type, die, cu);
8792
8793 return range_type;
8794 }
8795
8796 static struct type *
8797 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8798 {
8799 struct type *type;
8800
8801 /* For now, we only support the C meaning of an unspecified type: void. */
8802
8803 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8804 TYPE_NAME (type) = dwarf2_name (die, cu);
8805
8806 return set_die_type (die, type, cu);
8807 }
8808
8809 /* Trivial hash function for die_info: the hash value of a DIE
8810 is its offset in .debug_info for this objfile. */
8811
8812 static hashval_t
8813 die_hash (const void *item)
8814 {
8815 const struct die_info *die = item;
8816
8817 return die->offset;
8818 }
8819
8820 /* Trivial comparison function for die_info structures: two DIEs
8821 are equal if they have the same offset. */
8822
8823 static int
8824 die_eq (const void *item_lhs, const void *item_rhs)
8825 {
8826 const struct die_info *die_lhs = item_lhs;
8827 const struct die_info *die_rhs = item_rhs;
8828
8829 return die_lhs->offset == die_rhs->offset;
8830 }
8831
8832 /* Read a whole compilation unit into a linked list of dies. */
8833
8834 static struct die_info *
8835 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8836 {
8837 struct die_reader_specs reader_specs;
8838 int read_abbrevs = 0;
8839 struct cleanup *back_to = NULL;
8840 struct die_info *die;
8841
8842 if (cu->dwarf2_abbrevs == NULL)
8843 {
8844 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8845 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8846 read_abbrevs = 1;
8847 }
8848
8849 gdb_assert (cu->die_hash == NULL);
8850 cu->die_hash
8851 = htab_create_alloc_ex (cu->header.length / 12,
8852 die_hash,
8853 die_eq,
8854 NULL,
8855 &cu->comp_unit_obstack,
8856 hashtab_obstack_allocate,
8857 dummy_obstack_deallocate);
8858
8859 init_cu_die_reader (&reader_specs, cu);
8860
8861 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8862
8863 if (read_abbrevs)
8864 do_cleanups (back_to);
8865
8866 return die;
8867 }
8868
8869 /* Main entry point for reading a DIE and all children.
8870 Read the DIE and dump it if requested. */
8871
8872 static struct die_info *
8873 read_die_and_children (const struct die_reader_specs *reader,
8874 gdb_byte *info_ptr,
8875 gdb_byte **new_info_ptr,
8876 struct die_info *parent)
8877 {
8878 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8879 new_info_ptr, parent);
8880
8881 if (dwarf2_die_debug)
8882 {
8883 fprintf_unfiltered (gdb_stdlog,
8884 "\nRead die from %s of %s:\n",
8885 (reader->cu->per_cu->debug_type_section
8886 ? ".debug_types"
8887 : ".debug_info"),
8888 reader->abfd->filename);
8889 dump_die (result, dwarf2_die_debug);
8890 }
8891
8892 return result;
8893 }
8894
8895 /* Read a single die and all its descendents. Set the die's sibling
8896 field to NULL; set other fields in the die correctly, and set all
8897 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8898 location of the info_ptr after reading all of those dies. PARENT
8899 is the parent of the die in question. */
8900
8901 static struct die_info *
8902 read_die_and_children_1 (const struct die_reader_specs *reader,
8903 gdb_byte *info_ptr,
8904 gdb_byte **new_info_ptr,
8905 struct die_info *parent)
8906 {
8907 struct die_info *die;
8908 gdb_byte *cur_ptr;
8909 int has_children;
8910
8911 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8912 if (die == NULL)
8913 {
8914 *new_info_ptr = cur_ptr;
8915 return NULL;
8916 }
8917 store_in_ref_table (die, reader->cu);
8918
8919 if (has_children)
8920 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8921 else
8922 {
8923 die->child = NULL;
8924 *new_info_ptr = cur_ptr;
8925 }
8926
8927 die->sibling = NULL;
8928 die->parent = parent;
8929 return die;
8930 }
8931
8932 /* Read a die, all of its descendents, and all of its siblings; set
8933 all of the fields of all of the dies correctly. Arguments are as
8934 in read_die_and_children. */
8935
8936 static struct die_info *
8937 read_die_and_siblings (const struct die_reader_specs *reader,
8938 gdb_byte *info_ptr,
8939 gdb_byte **new_info_ptr,
8940 struct die_info *parent)
8941 {
8942 struct die_info *first_die, *last_sibling;
8943 gdb_byte *cur_ptr;
8944
8945 cur_ptr = info_ptr;
8946 first_die = last_sibling = NULL;
8947
8948 while (1)
8949 {
8950 struct die_info *die
8951 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8952
8953 if (die == NULL)
8954 {
8955 *new_info_ptr = cur_ptr;
8956 return first_die;
8957 }
8958
8959 if (!first_die)
8960 first_die = die;
8961 else
8962 last_sibling->sibling = die;
8963
8964 last_sibling = die;
8965 }
8966 }
8967
8968 /* Read the die from the .debug_info section buffer. Set DIEP to
8969 point to a newly allocated die with its information, except for its
8970 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8971 whether the die has children or not. */
8972
8973 static gdb_byte *
8974 read_full_die (const struct die_reader_specs *reader,
8975 struct die_info **diep, gdb_byte *info_ptr,
8976 int *has_children)
8977 {
8978 unsigned int abbrev_number, bytes_read, i, offset;
8979 struct abbrev_info *abbrev;
8980 struct die_info *die;
8981 struct dwarf2_cu *cu = reader->cu;
8982 bfd *abfd = reader->abfd;
8983
8984 offset = info_ptr - reader->buffer;
8985 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8986 info_ptr += bytes_read;
8987 if (!abbrev_number)
8988 {
8989 *diep = NULL;
8990 *has_children = 0;
8991 return info_ptr;
8992 }
8993
8994 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8995 if (!abbrev)
8996 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8997 abbrev_number,
8998 bfd_get_filename (abfd));
8999
9000 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9001 die->offset = offset;
9002 die->tag = abbrev->tag;
9003 die->abbrev = abbrev_number;
9004
9005 die->num_attrs = abbrev->num_attrs;
9006
9007 for (i = 0; i < abbrev->num_attrs; ++i)
9008 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9009 abfd, info_ptr, cu);
9010
9011 *diep = die;
9012 *has_children = abbrev->has_children;
9013 return info_ptr;
9014 }
9015
9016 /* In DWARF version 2, the description of the debugging information is
9017 stored in a separate .debug_abbrev section. Before we read any
9018 dies from a section we read in all abbreviations and install them
9019 in a hash table. This function also sets flags in CU describing
9020 the data found in the abbrev table. */
9021
9022 static void
9023 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
9024 {
9025 struct comp_unit_head *cu_header = &cu->header;
9026 gdb_byte *abbrev_ptr;
9027 struct abbrev_info *cur_abbrev;
9028 unsigned int abbrev_number, bytes_read, abbrev_name;
9029 unsigned int abbrev_form, hash_number;
9030 struct attr_abbrev *cur_attrs;
9031 unsigned int allocated_attrs;
9032
9033 /* Initialize dwarf2 abbrevs. */
9034 obstack_init (&cu->abbrev_obstack);
9035 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9036 (ABBREV_HASH_SIZE
9037 * sizeof (struct abbrev_info *)));
9038 memset (cu->dwarf2_abbrevs, 0,
9039 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9040
9041 dwarf2_read_section (dwarf2_per_objfile->objfile,
9042 &dwarf2_per_objfile->abbrev);
9043 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9044 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9045 abbrev_ptr += bytes_read;
9046
9047 allocated_attrs = ATTR_ALLOC_CHUNK;
9048 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9049
9050 /* Loop until we reach an abbrev number of 0. */
9051 while (abbrev_number)
9052 {
9053 cur_abbrev = dwarf_alloc_abbrev (cu);
9054
9055 /* read in abbrev header */
9056 cur_abbrev->number = abbrev_number;
9057 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9058 abbrev_ptr += bytes_read;
9059 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9060 abbrev_ptr += 1;
9061
9062 if (cur_abbrev->tag == DW_TAG_namespace)
9063 cu->has_namespace_info = 1;
9064
9065 /* now read in declarations */
9066 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9067 abbrev_ptr += bytes_read;
9068 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9069 abbrev_ptr += bytes_read;
9070 while (abbrev_name)
9071 {
9072 if (cur_abbrev->num_attrs == allocated_attrs)
9073 {
9074 allocated_attrs += ATTR_ALLOC_CHUNK;
9075 cur_attrs
9076 = xrealloc (cur_attrs, (allocated_attrs
9077 * sizeof (struct attr_abbrev)));
9078 }
9079
9080 /* Record whether this compilation unit might have
9081 inter-compilation-unit references. If we don't know what form
9082 this attribute will have, then it might potentially be a
9083 DW_FORM_ref_addr, so we conservatively expect inter-CU
9084 references. */
9085
9086 if (abbrev_form == DW_FORM_ref_addr
9087 || abbrev_form == DW_FORM_indirect)
9088 cu->has_form_ref_addr = 1;
9089
9090 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9091 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9092 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9093 abbrev_ptr += bytes_read;
9094 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9095 abbrev_ptr += bytes_read;
9096 }
9097
9098 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9099 (cur_abbrev->num_attrs
9100 * sizeof (struct attr_abbrev)));
9101 memcpy (cur_abbrev->attrs, cur_attrs,
9102 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9103
9104 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9105 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9106 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9107
9108 /* Get next abbreviation.
9109 Under Irix6 the abbreviations for a compilation unit are not
9110 always properly terminated with an abbrev number of 0.
9111 Exit loop if we encounter an abbreviation which we have
9112 already read (which means we are about to read the abbreviations
9113 for the next compile unit) or if the end of the abbreviation
9114 table is reached. */
9115 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9116 >= dwarf2_per_objfile->abbrev.size)
9117 break;
9118 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9119 abbrev_ptr += bytes_read;
9120 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9121 break;
9122 }
9123
9124 xfree (cur_attrs);
9125 }
9126
9127 /* Release the memory used by the abbrev table for a compilation unit. */
9128
9129 static void
9130 dwarf2_free_abbrev_table (void *ptr_to_cu)
9131 {
9132 struct dwarf2_cu *cu = ptr_to_cu;
9133
9134 obstack_free (&cu->abbrev_obstack, NULL);
9135 cu->dwarf2_abbrevs = NULL;
9136 }
9137
9138 /* Lookup an abbrev_info structure in the abbrev hash table. */
9139
9140 static struct abbrev_info *
9141 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9142 {
9143 unsigned int hash_number;
9144 struct abbrev_info *abbrev;
9145
9146 hash_number = number % ABBREV_HASH_SIZE;
9147 abbrev = cu->dwarf2_abbrevs[hash_number];
9148
9149 while (abbrev)
9150 {
9151 if (abbrev->number == number)
9152 return abbrev;
9153 else
9154 abbrev = abbrev->next;
9155 }
9156 return NULL;
9157 }
9158
9159 /* Returns nonzero if TAG represents a type that we might generate a partial
9160 symbol for. */
9161
9162 static int
9163 is_type_tag_for_partial (int tag)
9164 {
9165 switch (tag)
9166 {
9167 #if 0
9168 /* Some types that would be reasonable to generate partial symbols for,
9169 that we don't at present. */
9170 case DW_TAG_array_type:
9171 case DW_TAG_file_type:
9172 case DW_TAG_ptr_to_member_type:
9173 case DW_TAG_set_type:
9174 case DW_TAG_string_type:
9175 case DW_TAG_subroutine_type:
9176 #endif
9177 case DW_TAG_base_type:
9178 case DW_TAG_class_type:
9179 case DW_TAG_interface_type:
9180 case DW_TAG_enumeration_type:
9181 case DW_TAG_structure_type:
9182 case DW_TAG_subrange_type:
9183 case DW_TAG_typedef:
9184 case DW_TAG_union_type:
9185 return 1;
9186 default:
9187 return 0;
9188 }
9189 }
9190
9191 /* Load all DIEs that are interesting for partial symbols into memory. */
9192
9193 static struct partial_die_info *
9194 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9195 int building_psymtab, struct dwarf2_cu *cu)
9196 {
9197 struct partial_die_info *part_die;
9198 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9199 struct abbrev_info *abbrev;
9200 unsigned int bytes_read;
9201 unsigned int load_all = 0;
9202
9203 int nesting_level = 1;
9204
9205 parent_die = NULL;
9206 last_die = NULL;
9207
9208 if (cu->per_cu && cu->per_cu->load_all_dies)
9209 load_all = 1;
9210
9211 cu->partial_dies
9212 = htab_create_alloc_ex (cu->header.length / 12,
9213 partial_die_hash,
9214 partial_die_eq,
9215 NULL,
9216 &cu->comp_unit_obstack,
9217 hashtab_obstack_allocate,
9218 dummy_obstack_deallocate);
9219
9220 part_die = obstack_alloc (&cu->comp_unit_obstack,
9221 sizeof (struct partial_die_info));
9222
9223 while (1)
9224 {
9225 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9226
9227 /* A NULL abbrev means the end of a series of children. */
9228 if (abbrev == NULL)
9229 {
9230 if (--nesting_level == 0)
9231 {
9232 /* PART_DIE was probably the last thing allocated on the
9233 comp_unit_obstack, so we could call obstack_free
9234 here. We don't do that because the waste is small,
9235 and will be cleaned up when we're done with this
9236 compilation unit. This way, we're also more robust
9237 against other users of the comp_unit_obstack. */
9238 return first_die;
9239 }
9240 info_ptr += bytes_read;
9241 last_die = parent_die;
9242 parent_die = parent_die->die_parent;
9243 continue;
9244 }
9245
9246 /* Check for template arguments. We never save these; if
9247 they're seen, we just mark the parent, and go on our way. */
9248 if (parent_die != NULL
9249 && cu->language == language_cplus
9250 && (abbrev->tag == DW_TAG_template_type_param
9251 || abbrev->tag == DW_TAG_template_value_param))
9252 {
9253 parent_die->has_template_arguments = 1;
9254
9255 if (!load_all)
9256 {
9257 /* We don't need a partial DIE for the template argument. */
9258 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9259 cu);
9260 continue;
9261 }
9262 }
9263
9264 /* We only recurse into subprograms looking for template arguments.
9265 Skip their other children. */
9266 if (!load_all
9267 && cu->language == language_cplus
9268 && parent_die != NULL
9269 && parent_die->tag == DW_TAG_subprogram)
9270 {
9271 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9272 continue;
9273 }
9274
9275 /* Check whether this DIE is interesting enough to save. Normally
9276 we would not be interested in members here, but there may be
9277 later variables referencing them via DW_AT_specification (for
9278 static members). */
9279 if (!load_all
9280 && !is_type_tag_for_partial (abbrev->tag)
9281 && abbrev->tag != DW_TAG_constant
9282 && abbrev->tag != DW_TAG_enumerator
9283 && abbrev->tag != DW_TAG_subprogram
9284 && abbrev->tag != DW_TAG_lexical_block
9285 && abbrev->tag != DW_TAG_variable
9286 && abbrev->tag != DW_TAG_namespace
9287 && abbrev->tag != DW_TAG_module
9288 && abbrev->tag != DW_TAG_member)
9289 {
9290 /* Otherwise we skip to the next sibling, if any. */
9291 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9292 continue;
9293 }
9294
9295 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9296 buffer, info_ptr, cu);
9297
9298 /* This two-pass algorithm for processing partial symbols has a
9299 high cost in cache pressure. Thus, handle some simple cases
9300 here which cover the majority of C partial symbols. DIEs
9301 which neither have specification tags in them, nor could have
9302 specification tags elsewhere pointing at them, can simply be
9303 processed and discarded.
9304
9305 This segment is also optional; scan_partial_symbols and
9306 add_partial_symbol will handle these DIEs if we chain
9307 them in normally. When compilers which do not emit large
9308 quantities of duplicate debug information are more common,
9309 this code can probably be removed. */
9310
9311 /* Any complete simple types at the top level (pretty much all
9312 of them, for a language without namespaces), can be processed
9313 directly. */
9314 if (parent_die == NULL
9315 && part_die->has_specification == 0
9316 && part_die->is_declaration == 0
9317 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9318 || part_die->tag == DW_TAG_base_type
9319 || part_die->tag == DW_TAG_subrange_type))
9320 {
9321 if (building_psymtab && part_die->name != NULL)
9322 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9323 VAR_DOMAIN, LOC_TYPEDEF,
9324 &cu->objfile->static_psymbols,
9325 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9326 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9327 continue;
9328 }
9329
9330 /* The exception for DW_TAG_typedef with has_children above is
9331 a workaround of GCC PR debug/47510. In the case of this complaint
9332 type_name_no_tag_or_error will error on such types later.
9333
9334 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9335 it could not find the child DIEs referenced later, this is checked
9336 above. In correct DWARF DW_TAG_typedef should have no children. */
9337
9338 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9339 complaint (&symfile_complaints,
9340 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9341 "- DIE at 0x%x [in module %s]"),
9342 part_die->offset, cu->objfile->name);
9343
9344 /* If we're at the second level, and we're an enumerator, and
9345 our parent has no specification (meaning possibly lives in a
9346 namespace elsewhere), then we can add the partial symbol now
9347 instead of queueing it. */
9348 if (part_die->tag == DW_TAG_enumerator
9349 && parent_die != NULL
9350 && parent_die->die_parent == NULL
9351 && parent_die->tag == DW_TAG_enumeration_type
9352 && parent_die->has_specification == 0)
9353 {
9354 if (part_die->name == NULL)
9355 complaint (&symfile_complaints,
9356 _("malformed enumerator DIE ignored"));
9357 else if (building_psymtab)
9358 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9359 VAR_DOMAIN, LOC_CONST,
9360 (cu->language == language_cplus
9361 || cu->language == language_java)
9362 ? &cu->objfile->global_psymbols
9363 : &cu->objfile->static_psymbols,
9364 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9365
9366 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9367 continue;
9368 }
9369
9370 /* We'll save this DIE so link it in. */
9371 part_die->die_parent = parent_die;
9372 part_die->die_sibling = NULL;
9373 part_die->die_child = NULL;
9374
9375 if (last_die && last_die == parent_die)
9376 last_die->die_child = part_die;
9377 else if (last_die)
9378 last_die->die_sibling = part_die;
9379
9380 last_die = part_die;
9381
9382 if (first_die == NULL)
9383 first_die = part_die;
9384
9385 /* Maybe add the DIE to the hash table. Not all DIEs that we
9386 find interesting need to be in the hash table, because we
9387 also have the parent/sibling/child chains; only those that we
9388 might refer to by offset later during partial symbol reading.
9389
9390 For now this means things that might have be the target of a
9391 DW_AT_specification, DW_AT_abstract_origin, or
9392 DW_AT_extension. DW_AT_extension will refer only to
9393 namespaces; DW_AT_abstract_origin refers to functions (and
9394 many things under the function DIE, but we do not recurse
9395 into function DIEs during partial symbol reading) and
9396 possibly variables as well; DW_AT_specification refers to
9397 declarations. Declarations ought to have the DW_AT_declaration
9398 flag. It happens that GCC forgets to put it in sometimes, but
9399 only for functions, not for types.
9400
9401 Adding more things than necessary to the hash table is harmless
9402 except for the performance cost. Adding too few will result in
9403 wasted time in find_partial_die, when we reread the compilation
9404 unit with load_all_dies set. */
9405
9406 if (load_all
9407 || abbrev->tag == DW_TAG_constant
9408 || abbrev->tag == DW_TAG_subprogram
9409 || abbrev->tag == DW_TAG_variable
9410 || abbrev->tag == DW_TAG_namespace
9411 || part_die->is_declaration)
9412 {
9413 void **slot;
9414
9415 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9416 part_die->offset, INSERT);
9417 *slot = part_die;
9418 }
9419
9420 part_die = obstack_alloc (&cu->comp_unit_obstack,
9421 sizeof (struct partial_die_info));
9422
9423 /* For some DIEs we want to follow their children (if any). For C
9424 we have no reason to follow the children of structures; for other
9425 languages we have to, so that we can get at method physnames
9426 to infer fully qualified class names, for DW_AT_specification,
9427 and for C++ template arguments. For C++, we also look one level
9428 inside functions to find template arguments (if the name of the
9429 function does not already contain the template arguments).
9430
9431 For Ada, we need to scan the children of subprograms and lexical
9432 blocks as well because Ada allows the definition of nested
9433 entities that could be interesting for the debugger, such as
9434 nested subprograms for instance. */
9435 if (last_die->has_children
9436 && (load_all
9437 || last_die->tag == DW_TAG_namespace
9438 || last_die->tag == DW_TAG_module
9439 || last_die->tag == DW_TAG_enumeration_type
9440 || (cu->language == language_cplus
9441 && last_die->tag == DW_TAG_subprogram
9442 && (last_die->name == NULL
9443 || strchr (last_die->name, '<') == NULL))
9444 || (cu->language != language_c
9445 && (last_die->tag == DW_TAG_class_type
9446 || last_die->tag == DW_TAG_interface_type
9447 || last_die->tag == DW_TAG_structure_type
9448 || last_die->tag == DW_TAG_union_type))
9449 || (cu->language == language_ada
9450 && (last_die->tag == DW_TAG_subprogram
9451 || last_die->tag == DW_TAG_lexical_block))))
9452 {
9453 nesting_level++;
9454 parent_die = last_die;
9455 continue;
9456 }
9457
9458 /* Otherwise we skip to the next sibling, if any. */
9459 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9460
9461 /* Back to the top, do it again. */
9462 }
9463 }
9464
9465 /* Read a minimal amount of information into the minimal die structure. */
9466
9467 static gdb_byte *
9468 read_partial_die (struct partial_die_info *part_die,
9469 struct abbrev_info *abbrev,
9470 unsigned int abbrev_len, bfd *abfd,
9471 gdb_byte *buffer, gdb_byte *info_ptr,
9472 struct dwarf2_cu *cu)
9473 {
9474 unsigned int i;
9475 struct attribute attr;
9476 int has_low_pc_attr = 0;
9477 int has_high_pc_attr = 0;
9478
9479 memset (part_die, 0, sizeof (struct partial_die_info));
9480
9481 part_die->offset = info_ptr - buffer;
9482
9483 info_ptr += abbrev_len;
9484
9485 if (abbrev == NULL)
9486 return info_ptr;
9487
9488 part_die->tag = abbrev->tag;
9489 part_die->has_children = abbrev->has_children;
9490
9491 for (i = 0; i < abbrev->num_attrs; ++i)
9492 {
9493 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9494
9495 /* Store the data if it is of an attribute we want to keep in a
9496 partial symbol table. */
9497 switch (attr.name)
9498 {
9499 case DW_AT_name:
9500 switch (part_die->tag)
9501 {
9502 case DW_TAG_compile_unit:
9503 case DW_TAG_type_unit:
9504 /* Compilation units have a DW_AT_name that is a filename, not
9505 a source language identifier. */
9506 case DW_TAG_enumeration_type:
9507 case DW_TAG_enumerator:
9508 /* These tags always have simple identifiers already; no need
9509 to canonicalize them. */
9510 part_die->name = DW_STRING (&attr);
9511 break;
9512 default:
9513 part_die->name
9514 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9515 &cu->objfile->objfile_obstack);
9516 break;
9517 }
9518 break;
9519 case DW_AT_linkage_name:
9520 case DW_AT_MIPS_linkage_name:
9521 /* Note that both forms of linkage name might appear. We
9522 assume they will be the same, and we only store the last
9523 one we see. */
9524 if (cu->language == language_ada)
9525 part_die->name = DW_STRING (&attr);
9526 part_die->linkage_name = DW_STRING (&attr);
9527 break;
9528 case DW_AT_low_pc:
9529 has_low_pc_attr = 1;
9530 part_die->lowpc = DW_ADDR (&attr);
9531 break;
9532 case DW_AT_high_pc:
9533 has_high_pc_attr = 1;
9534 part_die->highpc = DW_ADDR (&attr);
9535 break;
9536 case DW_AT_location:
9537 /* Support the .debug_loc offsets. */
9538 if (attr_form_is_block (&attr))
9539 {
9540 part_die->locdesc = DW_BLOCK (&attr);
9541 }
9542 else if (attr_form_is_section_offset (&attr))
9543 {
9544 dwarf2_complex_location_expr_complaint ();
9545 }
9546 else
9547 {
9548 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9549 "partial symbol information");
9550 }
9551 break;
9552 case DW_AT_external:
9553 part_die->is_external = DW_UNSND (&attr);
9554 break;
9555 case DW_AT_declaration:
9556 part_die->is_declaration = DW_UNSND (&attr);
9557 break;
9558 case DW_AT_type:
9559 part_die->has_type = 1;
9560 break;
9561 case DW_AT_abstract_origin:
9562 case DW_AT_specification:
9563 case DW_AT_extension:
9564 part_die->has_specification = 1;
9565 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9566 break;
9567 case DW_AT_sibling:
9568 /* Ignore absolute siblings, they might point outside of
9569 the current compile unit. */
9570 if (attr.form == DW_FORM_ref_addr)
9571 complaint (&symfile_complaints,
9572 _("ignoring absolute DW_AT_sibling"));
9573 else
9574 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9575 break;
9576 case DW_AT_byte_size:
9577 part_die->has_byte_size = 1;
9578 break;
9579 case DW_AT_calling_convention:
9580 /* DWARF doesn't provide a way to identify a program's source-level
9581 entry point. DW_AT_calling_convention attributes are only meant
9582 to describe functions' calling conventions.
9583
9584 However, because it's a necessary piece of information in
9585 Fortran, and because DW_CC_program is the only piece of debugging
9586 information whose definition refers to a 'main program' at all,
9587 several compilers have begun marking Fortran main programs with
9588 DW_CC_program --- even when those functions use the standard
9589 calling conventions.
9590
9591 So until DWARF specifies a way to provide this information and
9592 compilers pick up the new representation, we'll support this
9593 practice. */
9594 if (DW_UNSND (&attr) == DW_CC_program
9595 && cu->language == language_fortran)
9596 {
9597 set_main_name (part_die->name);
9598
9599 /* As this DIE has a static linkage the name would be difficult
9600 to look up later. */
9601 language_of_main = language_fortran;
9602 }
9603 break;
9604 default:
9605 break;
9606 }
9607 }
9608
9609 if (has_low_pc_attr && has_high_pc_attr)
9610 {
9611 /* When using the GNU linker, .gnu.linkonce. sections are used to
9612 eliminate duplicate copies of functions and vtables and such.
9613 The linker will arbitrarily choose one and discard the others.
9614 The AT_*_pc values for such functions refer to local labels in
9615 these sections. If the section from that file was discarded, the
9616 labels are not in the output, so the relocs get a value of 0.
9617 If this is a discarded function, mark the pc bounds as invalid,
9618 so that GDB will ignore it. */
9619 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9620 {
9621 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9622
9623 complaint (&symfile_complaints,
9624 _("DW_AT_low_pc %s is zero "
9625 "for DIE at 0x%x [in module %s]"),
9626 paddress (gdbarch, part_die->lowpc),
9627 part_die->offset, cu->objfile->name);
9628 }
9629 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9630 else if (part_die->lowpc >= part_die->highpc)
9631 {
9632 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9633
9634 complaint (&symfile_complaints,
9635 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9636 "for DIE at 0x%x [in module %s]"),
9637 paddress (gdbarch, part_die->lowpc),
9638 paddress (gdbarch, part_die->highpc),
9639 part_die->offset, cu->objfile->name);
9640 }
9641 else
9642 part_die->has_pc_info = 1;
9643 }
9644
9645 return info_ptr;
9646 }
9647
9648 /* Find a cached partial DIE at OFFSET in CU. */
9649
9650 static struct partial_die_info *
9651 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9652 {
9653 struct partial_die_info *lookup_die = NULL;
9654 struct partial_die_info part_die;
9655
9656 part_die.offset = offset;
9657 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9658
9659 return lookup_die;
9660 }
9661
9662 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9663 except in the case of .debug_types DIEs which do not reference
9664 outside their CU (they do however referencing other types via
9665 DW_FORM_ref_sig8). */
9666
9667 static struct partial_die_info *
9668 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9669 {
9670 struct dwarf2_per_cu_data *per_cu = NULL;
9671 struct partial_die_info *pd = NULL;
9672
9673 if (cu->per_cu->debug_type_section)
9674 {
9675 pd = find_partial_die_in_comp_unit (offset, cu);
9676 if (pd != NULL)
9677 return pd;
9678 goto not_found;
9679 }
9680
9681 if (offset_in_cu_p (&cu->header, offset))
9682 {
9683 pd = find_partial_die_in_comp_unit (offset, cu);
9684 if (pd != NULL)
9685 return pd;
9686 }
9687
9688 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9689
9690 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9691 load_partial_comp_unit (per_cu, cu->objfile);
9692
9693 per_cu->cu->last_used = 0;
9694 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9695
9696 if (pd == NULL && per_cu->load_all_dies == 0)
9697 {
9698 struct cleanup *back_to;
9699 struct partial_die_info comp_unit_die;
9700 struct abbrev_info *abbrev;
9701 unsigned int bytes_read;
9702 char *info_ptr;
9703
9704 per_cu->load_all_dies = 1;
9705
9706 /* Re-read the DIEs. */
9707 back_to = make_cleanup (null_cleanup, 0);
9708 if (per_cu->cu->dwarf2_abbrevs == NULL)
9709 {
9710 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9711 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9712 }
9713 info_ptr = (dwarf2_per_objfile->info.buffer
9714 + per_cu->cu->header.offset
9715 + per_cu->cu->header.first_die_offset);
9716 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9717 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9718 per_cu->cu->objfile->obfd,
9719 dwarf2_per_objfile->info.buffer, info_ptr,
9720 per_cu->cu);
9721 if (comp_unit_die.has_children)
9722 load_partial_dies (per_cu->cu->objfile->obfd,
9723 dwarf2_per_objfile->info.buffer, info_ptr,
9724 0, per_cu->cu);
9725 do_cleanups (back_to);
9726
9727 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9728 }
9729
9730 not_found:
9731
9732 if (pd == NULL)
9733 internal_error (__FILE__, __LINE__,
9734 _("could not find partial DIE 0x%x "
9735 "in cache [from module %s]\n"),
9736 offset, bfd_get_filename (cu->objfile->obfd));
9737 return pd;
9738 }
9739
9740 /* See if we can figure out if the class lives in a namespace. We do
9741 this by looking for a member function; its demangled name will
9742 contain namespace info, if there is any. */
9743
9744 static void
9745 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9746 struct dwarf2_cu *cu)
9747 {
9748 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9749 what template types look like, because the demangler
9750 frequently doesn't give the same name as the debug info. We
9751 could fix this by only using the demangled name to get the
9752 prefix (but see comment in read_structure_type). */
9753
9754 struct partial_die_info *real_pdi;
9755 struct partial_die_info *child_pdi;
9756
9757 /* If this DIE (this DIE's specification, if any) has a parent, then
9758 we should not do this. We'll prepend the parent's fully qualified
9759 name when we create the partial symbol. */
9760
9761 real_pdi = struct_pdi;
9762 while (real_pdi->has_specification)
9763 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9764
9765 if (real_pdi->die_parent != NULL)
9766 return;
9767
9768 for (child_pdi = struct_pdi->die_child;
9769 child_pdi != NULL;
9770 child_pdi = child_pdi->die_sibling)
9771 {
9772 if (child_pdi->tag == DW_TAG_subprogram
9773 && child_pdi->linkage_name != NULL)
9774 {
9775 char *actual_class_name
9776 = language_class_name_from_physname (cu->language_defn,
9777 child_pdi->linkage_name);
9778 if (actual_class_name != NULL)
9779 {
9780 struct_pdi->name
9781 = obsavestring (actual_class_name,
9782 strlen (actual_class_name),
9783 &cu->objfile->objfile_obstack);
9784 xfree (actual_class_name);
9785 }
9786 break;
9787 }
9788 }
9789 }
9790
9791 /* Adjust PART_DIE before generating a symbol for it. This function
9792 may set the is_external flag or change the DIE's name. */
9793
9794 static void
9795 fixup_partial_die (struct partial_die_info *part_die,
9796 struct dwarf2_cu *cu)
9797 {
9798 /* Once we've fixed up a die, there's no point in doing so again.
9799 This also avoids a memory leak if we were to call
9800 guess_partial_die_structure_name multiple times. */
9801 if (part_die->fixup_called)
9802 return;
9803
9804 /* If we found a reference attribute and the DIE has no name, try
9805 to find a name in the referred to DIE. */
9806
9807 if (part_die->name == NULL && part_die->has_specification)
9808 {
9809 struct partial_die_info *spec_die;
9810
9811 spec_die = find_partial_die (part_die->spec_offset, cu);
9812
9813 fixup_partial_die (spec_die, cu);
9814
9815 if (spec_die->name)
9816 {
9817 part_die->name = spec_die->name;
9818
9819 /* Copy DW_AT_external attribute if it is set. */
9820 if (spec_die->is_external)
9821 part_die->is_external = spec_die->is_external;
9822 }
9823 }
9824
9825 /* Set default names for some unnamed DIEs. */
9826
9827 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9828 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
9829
9830 /* If there is no parent die to provide a namespace, and there are
9831 children, see if we can determine the namespace from their linkage
9832 name.
9833 NOTE: We need to do this even if cu->has_namespace_info != 0.
9834 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9835 if (cu->language == language_cplus
9836 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
9837 && part_die->die_parent == NULL
9838 && part_die->has_children
9839 && (part_die->tag == DW_TAG_class_type
9840 || part_die->tag == DW_TAG_structure_type
9841 || part_die->tag == DW_TAG_union_type))
9842 guess_partial_die_structure_name (part_die, cu);
9843
9844 /* GCC might emit a nameless struct or union that has a linkage
9845 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9846 if (part_die->name == NULL
9847 && (part_die->tag == DW_TAG_structure_type
9848 || part_die->tag == DW_TAG_union_type
9849 || part_die->tag == DW_TAG_class_type)
9850 && part_die->linkage_name != NULL)
9851 {
9852 char *demangled;
9853
9854 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9855 if (demangled)
9856 {
9857 part_die->name = obsavestring (demangled, strlen (demangled),
9858 &cu->objfile->objfile_obstack);
9859 xfree (demangled);
9860 }
9861 }
9862
9863 part_die->fixup_called = 1;
9864 }
9865
9866 /* Read an attribute value described by an attribute form. */
9867
9868 static gdb_byte *
9869 read_attribute_value (struct attribute *attr, unsigned form,
9870 bfd *abfd, gdb_byte *info_ptr,
9871 struct dwarf2_cu *cu)
9872 {
9873 struct comp_unit_head *cu_header = &cu->header;
9874 unsigned int bytes_read;
9875 struct dwarf_block *blk;
9876
9877 attr->form = form;
9878 switch (form)
9879 {
9880 case DW_FORM_ref_addr:
9881 if (cu->header.version == 2)
9882 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9883 else
9884 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9885 &cu->header, &bytes_read);
9886 info_ptr += bytes_read;
9887 break;
9888 case DW_FORM_addr:
9889 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9890 info_ptr += bytes_read;
9891 break;
9892 case DW_FORM_block2:
9893 blk = dwarf_alloc_block (cu);
9894 blk->size = read_2_bytes (abfd, info_ptr);
9895 info_ptr += 2;
9896 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9897 info_ptr += blk->size;
9898 DW_BLOCK (attr) = blk;
9899 break;
9900 case DW_FORM_block4:
9901 blk = dwarf_alloc_block (cu);
9902 blk->size = read_4_bytes (abfd, info_ptr);
9903 info_ptr += 4;
9904 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9905 info_ptr += blk->size;
9906 DW_BLOCK (attr) = blk;
9907 break;
9908 case DW_FORM_data2:
9909 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9910 info_ptr += 2;
9911 break;
9912 case DW_FORM_data4:
9913 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9914 info_ptr += 4;
9915 break;
9916 case DW_FORM_data8:
9917 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9918 info_ptr += 8;
9919 break;
9920 case DW_FORM_sec_offset:
9921 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9922 info_ptr += bytes_read;
9923 break;
9924 case DW_FORM_string:
9925 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9926 DW_STRING_IS_CANONICAL (attr) = 0;
9927 info_ptr += bytes_read;
9928 break;
9929 case DW_FORM_strp:
9930 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9931 &bytes_read);
9932 DW_STRING_IS_CANONICAL (attr) = 0;
9933 info_ptr += bytes_read;
9934 break;
9935 case DW_FORM_exprloc:
9936 case DW_FORM_block:
9937 blk = dwarf_alloc_block (cu);
9938 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9939 info_ptr += bytes_read;
9940 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9941 info_ptr += blk->size;
9942 DW_BLOCK (attr) = blk;
9943 break;
9944 case DW_FORM_block1:
9945 blk = dwarf_alloc_block (cu);
9946 blk->size = read_1_byte (abfd, info_ptr);
9947 info_ptr += 1;
9948 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9949 info_ptr += blk->size;
9950 DW_BLOCK (attr) = blk;
9951 break;
9952 case DW_FORM_data1:
9953 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9954 info_ptr += 1;
9955 break;
9956 case DW_FORM_flag:
9957 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9958 info_ptr += 1;
9959 break;
9960 case DW_FORM_flag_present:
9961 DW_UNSND (attr) = 1;
9962 break;
9963 case DW_FORM_sdata:
9964 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9965 info_ptr += bytes_read;
9966 break;
9967 case DW_FORM_udata:
9968 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9969 info_ptr += bytes_read;
9970 break;
9971 case DW_FORM_ref1:
9972 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9973 info_ptr += 1;
9974 break;
9975 case DW_FORM_ref2:
9976 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9977 info_ptr += 2;
9978 break;
9979 case DW_FORM_ref4:
9980 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9981 info_ptr += 4;
9982 break;
9983 case DW_FORM_ref8:
9984 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9985 info_ptr += 8;
9986 break;
9987 case DW_FORM_ref_sig8:
9988 /* Convert the signature to something we can record in DW_UNSND
9989 for later lookup.
9990 NOTE: This is NULL if the type wasn't found. */
9991 DW_SIGNATURED_TYPE (attr) =
9992 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9993 info_ptr += 8;
9994 break;
9995 case DW_FORM_ref_udata:
9996 DW_ADDR (attr) = (cu->header.offset
9997 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9998 info_ptr += bytes_read;
9999 break;
10000 case DW_FORM_indirect:
10001 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10002 info_ptr += bytes_read;
10003 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10004 break;
10005 default:
10006 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10007 dwarf_form_name (form),
10008 bfd_get_filename (abfd));
10009 }
10010
10011 /* We have seen instances where the compiler tried to emit a byte
10012 size attribute of -1 which ended up being encoded as an unsigned
10013 0xffffffff. Although 0xffffffff is technically a valid size value,
10014 an object of this size seems pretty unlikely so we can relatively
10015 safely treat these cases as if the size attribute was invalid and
10016 treat them as zero by default. */
10017 if (attr->name == DW_AT_byte_size
10018 && form == DW_FORM_data4
10019 && DW_UNSND (attr) >= 0xffffffff)
10020 {
10021 complaint
10022 (&symfile_complaints,
10023 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10024 hex_string (DW_UNSND (attr)));
10025 DW_UNSND (attr) = 0;
10026 }
10027
10028 return info_ptr;
10029 }
10030
10031 /* Read an attribute described by an abbreviated attribute. */
10032
10033 static gdb_byte *
10034 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10035 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10036 {
10037 attr->name = abbrev->name;
10038 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10039 }
10040
10041 /* Read dwarf information from a buffer. */
10042
10043 static unsigned int
10044 read_1_byte (bfd *abfd, gdb_byte *buf)
10045 {
10046 return bfd_get_8 (abfd, buf);
10047 }
10048
10049 static int
10050 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10051 {
10052 return bfd_get_signed_8 (abfd, buf);
10053 }
10054
10055 static unsigned int
10056 read_2_bytes (bfd *abfd, gdb_byte *buf)
10057 {
10058 return bfd_get_16 (abfd, buf);
10059 }
10060
10061 static int
10062 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10063 {
10064 return bfd_get_signed_16 (abfd, buf);
10065 }
10066
10067 static unsigned int
10068 read_4_bytes (bfd *abfd, gdb_byte *buf)
10069 {
10070 return bfd_get_32 (abfd, buf);
10071 }
10072
10073 static int
10074 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10075 {
10076 return bfd_get_signed_32 (abfd, buf);
10077 }
10078
10079 static ULONGEST
10080 read_8_bytes (bfd *abfd, gdb_byte *buf)
10081 {
10082 return bfd_get_64 (abfd, buf);
10083 }
10084
10085 static CORE_ADDR
10086 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10087 unsigned int *bytes_read)
10088 {
10089 struct comp_unit_head *cu_header = &cu->header;
10090 CORE_ADDR retval = 0;
10091
10092 if (cu_header->signed_addr_p)
10093 {
10094 switch (cu_header->addr_size)
10095 {
10096 case 2:
10097 retval = bfd_get_signed_16 (abfd, buf);
10098 break;
10099 case 4:
10100 retval = bfd_get_signed_32 (abfd, buf);
10101 break;
10102 case 8:
10103 retval = bfd_get_signed_64 (abfd, buf);
10104 break;
10105 default:
10106 internal_error (__FILE__, __LINE__,
10107 _("read_address: bad switch, signed [in module %s]"),
10108 bfd_get_filename (abfd));
10109 }
10110 }
10111 else
10112 {
10113 switch (cu_header->addr_size)
10114 {
10115 case 2:
10116 retval = bfd_get_16 (abfd, buf);
10117 break;
10118 case 4:
10119 retval = bfd_get_32 (abfd, buf);
10120 break;
10121 case 8:
10122 retval = bfd_get_64 (abfd, buf);
10123 break;
10124 default:
10125 internal_error (__FILE__, __LINE__,
10126 _("read_address: bad switch, "
10127 "unsigned [in module %s]"),
10128 bfd_get_filename (abfd));
10129 }
10130 }
10131
10132 *bytes_read = cu_header->addr_size;
10133 return retval;
10134 }
10135
10136 /* Read the initial length from a section. The (draft) DWARF 3
10137 specification allows the initial length to take up either 4 bytes
10138 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10139 bytes describe the length and all offsets will be 8 bytes in length
10140 instead of 4.
10141
10142 An older, non-standard 64-bit format is also handled by this
10143 function. The older format in question stores the initial length
10144 as an 8-byte quantity without an escape value. Lengths greater
10145 than 2^32 aren't very common which means that the initial 4 bytes
10146 is almost always zero. Since a length value of zero doesn't make
10147 sense for the 32-bit format, this initial zero can be considered to
10148 be an escape value which indicates the presence of the older 64-bit
10149 format. As written, the code can't detect (old format) lengths
10150 greater than 4GB. If it becomes necessary to handle lengths
10151 somewhat larger than 4GB, we could allow other small values (such
10152 as the non-sensical values of 1, 2, and 3) to also be used as
10153 escape values indicating the presence of the old format.
10154
10155 The value returned via bytes_read should be used to increment the
10156 relevant pointer after calling read_initial_length().
10157
10158 [ Note: read_initial_length() and read_offset() are based on the
10159 document entitled "DWARF Debugging Information Format", revision
10160 3, draft 8, dated November 19, 2001. This document was obtained
10161 from:
10162
10163 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10164
10165 This document is only a draft and is subject to change. (So beware.)
10166
10167 Details regarding the older, non-standard 64-bit format were
10168 determined empirically by examining 64-bit ELF files produced by
10169 the SGI toolchain on an IRIX 6.5 machine.
10170
10171 - Kevin, July 16, 2002
10172 ] */
10173
10174 static LONGEST
10175 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10176 {
10177 LONGEST length = bfd_get_32 (abfd, buf);
10178
10179 if (length == 0xffffffff)
10180 {
10181 length = bfd_get_64 (abfd, buf + 4);
10182 *bytes_read = 12;
10183 }
10184 else if (length == 0)
10185 {
10186 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10187 length = bfd_get_64 (abfd, buf);
10188 *bytes_read = 8;
10189 }
10190 else
10191 {
10192 *bytes_read = 4;
10193 }
10194
10195 return length;
10196 }
10197
10198 /* Cover function for read_initial_length.
10199 Returns the length of the object at BUF, and stores the size of the
10200 initial length in *BYTES_READ and stores the size that offsets will be in
10201 *OFFSET_SIZE.
10202 If the initial length size is not equivalent to that specified in
10203 CU_HEADER then issue a complaint.
10204 This is useful when reading non-comp-unit headers. */
10205
10206 static LONGEST
10207 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10208 const struct comp_unit_head *cu_header,
10209 unsigned int *bytes_read,
10210 unsigned int *offset_size)
10211 {
10212 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10213
10214 gdb_assert (cu_header->initial_length_size == 4
10215 || cu_header->initial_length_size == 8
10216 || cu_header->initial_length_size == 12);
10217
10218 if (cu_header->initial_length_size != *bytes_read)
10219 complaint (&symfile_complaints,
10220 _("intermixed 32-bit and 64-bit DWARF sections"));
10221
10222 *offset_size = (*bytes_read == 4) ? 4 : 8;
10223 return length;
10224 }
10225
10226 /* Read an offset from the data stream. The size of the offset is
10227 given by cu_header->offset_size. */
10228
10229 static LONGEST
10230 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10231 unsigned int *bytes_read)
10232 {
10233 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10234
10235 *bytes_read = cu_header->offset_size;
10236 return offset;
10237 }
10238
10239 /* Read an offset from the data stream. */
10240
10241 static LONGEST
10242 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10243 {
10244 LONGEST retval = 0;
10245
10246 switch (offset_size)
10247 {
10248 case 4:
10249 retval = bfd_get_32 (abfd, buf);
10250 break;
10251 case 8:
10252 retval = bfd_get_64 (abfd, buf);
10253 break;
10254 default:
10255 internal_error (__FILE__, __LINE__,
10256 _("read_offset_1: bad switch [in module %s]"),
10257 bfd_get_filename (abfd));
10258 }
10259
10260 return retval;
10261 }
10262
10263 static gdb_byte *
10264 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10265 {
10266 /* If the size of a host char is 8 bits, we can return a pointer
10267 to the buffer, otherwise we have to copy the data to a buffer
10268 allocated on the temporary obstack. */
10269 gdb_assert (HOST_CHAR_BIT == 8);
10270 return buf;
10271 }
10272
10273 static char *
10274 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10275 {
10276 /* If the size of a host char is 8 bits, we can return a pointer
10277 to the string, otherwise we have to copy the string to a buffer
10278 allocated on the temporary obstack. */
10279 gdb_assert (HOST_CHAR_BIT == 8);
10280 if (*buf == '\0')
10281 {
10282 *bytes_read_ptr = 1;
10283 return NULL;
10284 }
10285 *bytes_read_ptr = strlen ((char *) buf) + 1;
10286 return (char *) buf;
10287 }
10288
10289 static char *
10290 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10291 {
10292 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10293 if (dwarf2_per_objfile->str.buffer == NULL)
10294 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10295 bfd_get_filename (abfd));
10296 if (str_offset >= dwarf2_per_objfile->str.size)
10297 error (_("DW_FORM_strp pointing outside of "
10298 ".debug_str section [in module %s]"),
10299 bfd_get_filename (abfd));
10300 gdb_assert (HOST_CHAR_BIT == 8);
10301 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10302 return NULL;
10303 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10304 }
10305
10306 static char *
10307 read_indirect_string (bfd *abfd, gdb_byte *buf,
10308 const struct comp_unit_head *cu_header,
10309 unsigned int *bytes_read_ptr)
10310 {
10311 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10312
10313 return read_indirect_string_at_offset (abfd, str_offset);
10314 }
10315
10316 static unsigned long
10317 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10318 {
10319 unsigned long result;
10320 unsigned int num_read;
10321 int i, shift;
10322 unsigned char byte;
10323
10324 result = 0;
10325 shift = 0;
10326 num_read = 0;
10327 i = 0;
10328 while (1)
10329 {
10330 byte = bfd_get_8 (abfd, buf);
10331 buf++;
10332 num_read++;
10333 result |= ((unsigned long)(byte & 127) << shift);
10334 if ((byte & 128) == 0)
10335 {
10336 break;
10337 }
10338 shift += 7;
10339 }
10340 *bytes_read_ptr = num_read;
10341 return result;
10342 }
10343
10344 static long
10345 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10346 {
10347 long result;
10348 int i, shift, num_read;
10349 unsigned char byte;
10350
10351 result = 0;
10352 shift = 0;
10353 num_read = 0;
10354 i = 0;
10355 while (1)
10356 {
10357 byte = bfd_get_8 (abfd, buf);
10358 buf++;
10359 num_read++;
10360 result |= ((long)(byte & 127) << shift);
10361 shift += 7;
10362 if ((byte & 128) == 0)
10363 {
10364 break;
10365 }
10366 }
10367 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10368 result |= -(((long)1) << shift);
10369 *bytes_read_ptr = num_read;
10370 return result;
10371 }
10372
10373 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10374
10375 static gdb_byte *
10376 skip_leb128 (bfd *abfd, gdb_byte *buf)
10377 {
10378 int byte;
10379
10380 while (1)
10381 {
10382 byte = bfd_get_8 (abfd, buf);
10383 buf++;
10384 if ((byte & 128) == 0)
10385 return buf;
10386 }
10387 }
10388
10389 static void
10390 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10391 {
10392 switch (lang)
10393 {
10394 case DW_LANG_C89:
10395 case DW_LANG_C99:
10396 case DW_LANG_C:
10397 cu->language = language_c;
10398 break;
10399 case DW_LANG_C_plus_plus:
10400 cu->language = language_cplus;
10401 break;
10402 case DW_LANG_D:
10403 cu->language = language_d;
10404 break;
10405 case DW_LANG_Fortran77:
10406 case DW_LANG_Fortran90:
10407 case DW_LANG_Fortran95:
10408 cu->language = language_fortran;
10409 break;
10410 case DW_LANG_Mips_Assembler:
10411 cu->language = language_asm;
10412 break;
10413 case DW_LANG_Java:
10414 cu->language = language_java;
10415 break;
10416 case DW_LANG_Ada83:
10417 case DW_LANG_Ada95:
10418 cu->language = language_ada;
10419 break;
10420 case DW_LANG_Modula2:
10421 cu->language = language_m2;
10422 break;
10423 case DW_LANG_Pascal83:
10424 cu->language = language_pascal;
10425 break;
10426 case DW_LANG_ObjC:
10427 cu->language = language_objc;
10428 break;
10429 case DW_LANG_Cobol74:
10430 case DW_LANG_Cobol85:
10431 default:
10432 cu->language = language_minimal;
10433 break;
10434 }
10435 cu->language_defn = language_def (cu->language);
10436 }
10437
10438 /* Return the named attribute or NULL if not there. */
10439
10440 static struct attribute *
10441 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10442 {
10443 unsigned int i;
10444 struct attribute *spec = NULL;
10445
10446 for (i = 0; i < die->num_attrs; ++i)
10447 {
10448 if (die->attrs[i].name == name)
10449 return &die->attrs[i];
10450 if (die->attrs[i].name == DW_AT_specification
10451 || die->attrs[i].name == DW_AT_abstract_origin)
10452 spec = &die->attrs[i];
10453 }
10454
10455 if (spec)
10456 {
10457 die = follow_die_ref (die, spec, &cu);
10458 return dwarf2_attr (die, name, cu);
10459 }
10460
10461 return NULL;
10462 }
10463
10464 /* Return the named attribute or NULL if not there,
10465 but do not follow DW_AT_specification, etc.
10466 This is for use in contexts where we're reading .debug_types dies.
10467 Following DW_AT_specification, DW_AT_abstract_origin will take us
10468 back up the chain, and we want to go down. */
10469
10470 static struct attribute *
10471 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10472 struct dwarf2_cu *cu)
10473 {
10474 unsigned int i;
10475
10476 for (i = 0; i < die->num_attrs; ++i)
10477 if (die->attrs[i].name == name)
10478 return &die->attrs[i];
10479
10480 return NULL;
10481 }
10482
10483 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10484 and holds a non-zero value. This function should only be used for
10485 DW_FORM_flag or DW_FORM_flag_present attributes. */
10486
10487 static int
10488 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10489 {
10490 struct attribute *attr = dwarf2_attr (die, name, cu);
10491
10492 return (attr && DW_UNSND (attr));
10493 }
10494
10495 static int
10496 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10497 {
10498 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10499 which value is non-zero. However, we have to be careful with
10500 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10501 (via dwarf2_flag_true_p) follows this attribute. So we may
10502 end up accidently finding a declaration attribute that belongs
10503 to a different DIE referenced by the specification attribute,
10504 even though the given DIE does not have a declaration attribute. */
10505 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10506 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10507 }
10508
10509 /* Return the die giving the specification for DIE, if there is
10510 one. *SPEC_CU is the CU containing DIE on input, and the CU
10511 containing the return value on output. If there is no
10512 specification, but there is an abstract origin, that is
10513 returned. */
10514
10515 static struct die_info *
10516 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10517 {
10518 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10519 *spec_cu);
10520
10521 if (spec_attr == NULL)
10522 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10523
10524 if (spec_attr == NULL)
10525 return NULL;
10526 else
10527 return follow_die_ref (die, spec_attr, spec_cu);
10528 }
10529
10530 /* Free the line_header structure *LH, and any arrays and strings it
10531 refers to.
10532 NOTE: This is also used as a "cleanup" function. */
10533
10534 static void
10535 free_line_header (struct line_header *lh)
10536 {
10537 if (lh->standard_opcode_lengths)
10538 xfree (lh->standard_opcode_lengths);
10539
10540 /* Remember that all the lh->file_names[i].name pointers are
10541 pointers into debug_line_buffer, and don't need to be freed. */
10542 if (lh->file_names)
10543 xfree (lh->file_names);
10544
10545 /* Similarly for the include directory names. */
10546 if (lh->include_dirs)
10547 xfree (lh->include_dirs);
10548
10549 xfree (lh);
10550 }
10551
10552 /* Add an entry to LH's include directory table. */
10553
10554 static void
10555 add_include_dir (struct line_header *lh, char *include_dir)
10556 {
10557 /* Grow the array if necessary. */
10558 if (lh->include_dirs_size == 0)
10559 {
10560 lh->include_dirs_size = 1; /* for testing */
10561 lh->include_dirs = xmalloc (lh->include_dirs_size
10562 * sizeof (*lh->include_dirs));
10563 }
10564 else if (lh->num_include_dirs >= lh->include_dirs_size)
10565 {
10566 lh->include_dirs_size *= 2;
10567 lh->include_dirs = xrealloc (lh->include_dirs,
10568 (lh->include_dirs_size
10569 * sizeof (*lh->include_dirs)));
10570 }
10571
10572 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10573 }
10574
10575 /* Add an entry to LH's file name table. */
10576
10577 static void
10578 add_file_name (struct line_header *lh,
10579 char *name,
10580 unsigned int dir_index,
10581 unsigned int mod_time,
10582 unsigned int length)
10583 {
10584 struct file_entry *fe;
10585
10586 /* Grow the array if necessary. */
10587 if (lh->file_names_size == 0)
10588 {
10589 lh->file_names_size = 1; /* for testing */
10590 lh->file_names = xmalloc (lh->file_names_size
10591 * sizeof (*lh->file_names));
10592 }
10593 else if (lh->num_file_names >= lh->file_names_size)
10594 {
10595 lh->file_names_size *= 2;
10596 lh->file_names = xrealloc (lh->file_names,
10597 (lh->file_names_size
10598 * sizeof (*lh->file_names)));
10599 }
10600
10601 fe = &lh->file_names[lh->num_file_names++];
10602 fe->name = name;
10603 fe->dir_index = dir_index;
10604 fe->mod_time = mod_time;
10605 fe->length = length;
10606 fe->included_p = 0;
10607 fe->symtab = NULL;
10608 }
10609
10610 /* Read the statement program header starting at OFFSET in
10611 .debug_line, according to the endianness of ABFD. Return a pointer
10612 to a struct line_header, allocated using xmalloc.
10613
10614 NOTE: the strings in the include directory and file name tables of
10615 the returned object point into debug_line_buffer, and must not be
10616 freed. */
10617
10618 static struct line_header *
10619 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10620 struct dwarf2_cu *cu)
10621 {
10622 struct cleanup *back_to;
10623 struct line_header *lh;
10624 gdb_byte *line_ptr;
10625 unsigned int bytes_read, offset_size;
10626 int i;
10627 char *cur_dir, *cur_file;
10628
10629 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10630 if (dwarf2_per_objfile->line.buffer == NULL)
10631 {
10632 complaint (&symfile_complaints, _("missing .debug_line section"));
10633 return 0;
10634 }
10635
10636 /* Make sure that at least there's room for the total_length field.
10637 That could be 12 bytes long, but we're just going to fudge that. */
10638 if (offset + 4 >= dwarf2_per_objfile->line.size)
10639 {
10640 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10641 return 0;
10642 }
10643
10644 lh = xmalloc (sizeof (*lh));
10645 memset (lh, 0, sizeof (*lh));
10646 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10647 (void *) lh);
10648
10649 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10650
10651 /* Read in the header. */
10652 lh->total_length =
10653 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10654 &bytes_read, &offset_size);
10655 line_ptr += bytes_read;
10656 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10657 + dwarf2_per_objfile->line.size))
10658 {
10659 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10660 return 0;
10661 }
10662 lh->statement_program_end = line_ptr + lh->total_length;
10663 lh->version = read_2_bytes (abfd, line_ptr);
10664 line_ptr += 2;
10665 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10666 line_ptr += offset_size;
10667 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10668 line_ptr += 1;
10669 if (lh->version >= 4)
10670 {
10671 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10672 line_ptr += 1;
10673 }
10674 else
10675 lh->maximum_ops_per_instruction = 1;
10676
10677 if (lh->maximum_ops_per_instruction == 0)
10678 {
10679 lh->maximum_ops_per_instruction = 1;
10680 complaint (&symfile_complaints,
10681 _("invalid maximum_ops_per_instruction "
10682 "in `.debug_line' section"));
10683 }
10684
10685 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10686 line_ptr += 1;
10687 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10688 line_ptr += 1;
10689 lh->line_range = read_1_byte (abfd, line_ptr);
10690 line_ptr += 1;
10691 lh->opcode_base = read_1_byte (abfd, line_ptr);
10692 line_ptr += 1;
10693 lh->standard_opcode_lengths
10694 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10695
10696 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10697 for (i = 1; i < lh->opcode_base; ++i)
10698 {
10699 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10700 line_ptr += 1;
10701 }
10702
10703 /* Read directory table. */
10704 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10705 {
10706 line_ptr += bytes_read;
10707 add_include_dir (lh, cur_dir);
10708 }
10709 line_ptr += bytes_read;
10710
10711 /* Read file name table. */
10712 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10713 {
10714 unsigned int dir_index, mod_time, length;
10715
10716 line_ptr += bytes_read;
10717 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10718 line_ptr += bytes_read;
10719 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10720 line_ptr += bytes_read;
10721 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10722 line_ptr += bytes_read;
10723
10724 add_file_name (lh, cur_file, dir_index, mod_time, length);
10725 }
10726 line_ptr += bytes_read;
10727 lh->statement_program_start = line_ptr;
10728
10729 if (line_ptr > (dwarf2_per_objfile->line.buffer
10730 + dwarf2_per_objfile->line.size))
10731 complaint (&symfile_complaints,
10732 _("line number info header doesn't "
10733 "fit in `.debug_line' section"));
10734
10735 discard_cleanups (back_to);
10736 return lh;
10737 }
10738
10739 /* This function exists to work around a bug in certain compilers
10740 (particularly GCC 2.95), in which the first line number marker of a
10741 function does not show up until after the prologue, right before
10742 the second line number marker. This function shifts ADDRESS down
10743 to the beginning of the function if necessary, and is called on
10744 addresses passed to record_line. */
10745
10746 static CORE_ADDR
10747 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10748 {
10749 struct function_range *fn;
10750
10751 /* Find the function_range containing address. */
10752 if (!cu->first_fn)
10753 return address;
10754
10755 if (!cu->cached_fn)
10756 cu->cached_fn = cu->first_fn;
10757
10758 fn = cu->cached_fn;
10759 while (fn)
10760 if (fn->lowpc <= address && fn->highpc > address)
10761 goto found;
10762 else
10763 fn = fn->next;
10764
10765 fn = cu->first_fn;
10766 while (fn && fn != cu->cached_fn)
10767 if (fn->lowpc <= address && fn->highpc > address)
10768 goto found;
10769 else
10770 fn = fn->next;
10771
10772 return address;
10773
10774 found:
10775 if (fn->seen_line)
10776 return address;
10777 if (address != fn->lowpc)
10778 complaint (&symfile_complaints,
10779 _("misplaced first line number at 0x%lx for '%s'"),
10780 (unsigned long) address, fn->name);
10781 fn->seen_line = 1;
10782 return fn->lowpc;
10783 }
10784
10785 /* Subroutine of dwarf_decode_lines to simplify it.
10786 Return the file name of the psymtab for included file FILE_INDEX
10787 in line header LH of PST.
10788 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10789 If space for the result is malloc'd, it will be freed by a cleanup.
10790 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10791
10792 static char *
10793 psymtab_include_file_name (const struct line_header *lh, int file_index,
10794 const struct partial_symtab *pst,
10795 const char *comp_dir)
10796 {
10797 const struct file_entry fe = lh->file_names [file_index];
10798 char *include_name = fe.name;
10799 char *include_name_to_compare = include_name;
10800 char *dir_name = NULL;
10801 const char *pst_filename;
10802 char *copied_name = NULL;
10803 int file_is_pst;
10804
10805 if (fe.dir_index)
10806 dir_name = lh->include_dirs[fe.dir_index - 1];
10807
10808 if (!IS_ABSOLUTE_PATH (include_name)
10809 && (dir_name != NULL || comp_dir != NULL))
10810 {
10811 /* Avoid creating a duplicate psymtab for PST.
10812 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10813 Before we do the comparison, however, we need to account
10814 for DIR_NAME and COMP_DIR.
10815 First prepend dir_name (if non-NULL). If we still don't
10816 have an absolute path prepend comp_dir (if non-NULL).
10817 However, the directory we record in the include-file's
10818 psymtab does not contain COMP_DIR (to match the
10819 corresponding symtab(s)).
10820
10821 Example:
10822
10823 bash$ cd /tmp
10824 bash$ gcc -g ./hello.c
10825 include_name = "hello.c"
10826 dir_name = "."
10827 DW_AT_comp_dir = comp_dir = "/tmp"
10828 DW_AT_name = "./hello.c" */
10829
10830 if (dir_name != NULL)
10831 {
10832 include_name = concat (dir_name, SLASH_STRING,
10833 include_name, (char *)NULL);
10834 include_name_to_compare = include_name;
10835 make_cleanup (xfree, include_name);
10836 }
10837 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10838 {
10839 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10840 include_name, (char *)NULL);
10841 }
10842 }
10843
10844 pst_filename = pst->filename;
10845 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10846 {
10847 copied_name = concat (pst->dirname, SLASH_STRING,
10848 pst_filename, (char *)NULL);
10849 pst_filename = copied_name;
10850 }
10851
10852 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10853
10854 if (include_name_to_compare != include_name)
10855 xfree (include_name_to_compare);
10856 if (copied_name != NULL)
10857 xfree (copied_name);
10858
10859 if (file_is_pst)
10860 return NULL;
10861 return include_name;
10862 }
10863
10864 /* Ignore this record_line request. */
10865
10866 static void
10867 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10868 {
10869 return;
10870 }
10871
10872 /* Decode the Line Number Program (LNP) for the given line_header
10873 structure and CU. The actual information extracted and the type
10874 of structures created from the LNP depends on the value of PST.
10875
10876 1. If PST is NULL, then this procedure uses the data from the program
10877 to create all necessary symbol tables, and their linetables.
10878
10879 2. If PST is not NULL, this procedure reads the program to determine
10880 the list of files included by the unit represented by PST, and
10881 builds all the associated partial symbol tables.
10882
10883 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10884 It is used for relative paths in the line table.
10885 NOTE: When processing partial symtabs (pst != NULL),
10886 comp_dir == pst->dirname.
10887
10888 NOTE: It is important that psymtabs have the same file name (via strcmp)
10889 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10890 symtab we don't use it in the name of the psymtabs we create.
10891 E.g. expand_line_sal requires this when finding psymtabs to expand.
10892 A good testcase for this is mb-inline.exp. */
10893
10894 static void
10895 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10896 struct dwarf2_cu *cu, struct partial_symtab *pst)
10897 {
10898 gdb_byte *line_ptr, *extended_end;
10899 gdb_byte *line_end;
10900 unsigned int bytes_read, extended_len;
10901 unsigned char op_code, extended_op, adj_opcode;
10902 CORE_ADDR baseaddr;
10903 struct objfile *objfile = cu->objfile;
10904 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10905 const int decode_for_pst_p = (pst != NULL);
10906 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10907 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10908 = record_line;
10909
10910 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10911
10912 line_ptr = lh->statement_program_start;
10913 line_end = lh->statement_program_end;
10914
10915 /* Read the statement sequences until there's nothing left. */
10916 while (line_ptr < line_end)
10917 {
10918 /* state machine registers */
10919 CORE_ADDR address = 0;
10920 unsigned int file = 1;
10921 unsigned int line = 1;
10922 unsigned int column = 0;
10923 int is_stmt = lh->default_is_stmt;
10924 int basic_block = 0;
10925 int end_sequence = 0;
10926 CORE_ADDR addr;
10927 unsigned char op_index = 0;
10928
10929 if (!decode_for_pst_p && lh->num_file_names >= file)
10930 {
10931 /* Start a subfile for the current file of the state machine. */
10932 /* lh->include_dirs and lh->file_names are 0-based, but the
10933 directory and file name numbers in the statement program
10934 are 1-based. */
10935 struct file_entry *fe = &lh->file_names[file - 1];
10936 char *dir = NULL;
10937
10938 if (fe->dir_index)
10939 dir = lh->include_dirs[fe->dir_index - 1];
10940
10941 dwarf2_start_subfile (fe->name, dir, comp_dir);
10942 }
10943
10944 /* Decode the table. */
10945 while (!end_sequence)
10946 {
10947 op_code = read_1_byte (abfd, line_ptr);
10948 line_ptr += 1;
10949 if (line_ptr > line_end)
10950 {
10951 dwarf2_debug_line_missing_end_sequence_complaint ();
10952 break;
10953 }
10954
10955 if (op_code >= lh->opcode_base)
10956 {
10957 /* Special operand. */
10958 adj_opcode = op_code - lh->opcode_base;
10959 address += (((op_index + (adj_opcode / lh->line_range))
10960 / lh->maximum_ops_per_instruction)
10961 * lh->minimum_instruction_length);
10962 op_index = ((op_index + (adj_opcode / lh->line_range))
10963 % lh->maximum_ops_per_instruction);
10964 line += lh->line_base + (adj_opcode % lh->line_range);
10965 if (lh->num_file_names < file || file == 0)
10966 dwarf2_debug_line_missing_file_complaint ();
10967 /* For now we ignore lines not starting on an
10968 instruction boundary. */
10969 else if (op_index == 0)
10970 {
10971 lh->file_names[file - 1].included_p = 1;
10972 if (!decode_for_pst_p && is_stmt)
10973 {
10974 if (last_subfile != current_subfile)
10975 {
10976 addr = gdbarch_addr_bits_remove (gdbarch, address);
10977 if (last_subfile)
10978 (*p_record_line) (last_subfile, 0, addr);
10979 last_subfile = current_subfile;
10980 }
10981 /* Append row to matrix using current values. */
10982 addr = check_cu_functions (address, cu);
10983 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10984 (*p_record_line) (current_subfile, line, addr);
10985 }
10986 }
10987 basic_block = 0;
10988 }
10989 else switch (op_code)
10990 {
10991 case DW_LNS_extended_op:
10992 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10993 &bytes_read);
10994 line_ptr += bytes_read;
10995 extended_end = line_ptr + extended_len;
10996 extended_op = read_1_byte (abfd, line_ptr);
10997 line_ptr += 1;
10998 switch (extended_op)
10999 {
11000 case DW_LNE_end_sequence:
11001 p_record_line = record_line;
11002 end_sequence = 1;
11003 break;
11004 case DW_LNE_set_address:
11005 address = read_address (abfd, line_ptr, cu, &bytes_read);
11006
11007 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11008 {
11009 /* This line table is for a function which has been
11010 GCd by the linker. Ignore it. PR gdb/12528 */
11011
11012 long line_offset
11013 = line_ptr - dwarf2_per_objfile->line.buffer;
11014
11015 complaint (&symfile_complaints,
11016 _(".debug_line address at offset 0x%lx is 0 "
11017 "[in module %s]"),
11018 line_offset, cu->objfile->name);
11019 p_record_line = noop_record_line;
11020 }
11021
11022 op_index = 0;
11023 line_ptr += bytes_read;
11024 address += baseaddr;
11025 break;
11026 case DW_LNE_define_file:
11027 {
11028 char *cur_file;
11029 unsigned int dir_index, mod_time, length;
11030
11031 cur_file = read_direct_string (abfd, line_ptr,
11032 &bytes_read);
11033 line_ptr += bytes_read;
11034 dir_index =
11035 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11036 line_ptr += bytes_read;
11037 mod_time =
11038 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11039 line_ptr += bytes_read;
11040 length =
11041 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11042 line_ptr += bytes_read;
11043 add_file_name (lh, cur_file, dir_index, mod_time, length);
11044 }
11045 break;
11046 case DW_LNE_set_discriminator:
11047 /* The discriminator is not interesting to the debugger;
11048 just ignore it. */
11049 line_ptr = extended_end;
11050 break;
11051 default:
11052 complaint (&symfile_complaints,
11053 _("mangled .debug_line section"));
11054 return;
11055 }
11056 /* Make sure that we parsed the extended op correctly. If e.g.
11057 we expected a different address size than the producer used,
11058 we may have read the wrong number of bytes. */
11059 if (line_ptr != extended_end)
11060 {
11061 complaint (&symfile_complaints,
11062 _("mangled .debug_line section"));
11063 return;
11064 }
11065 break;
11066 case DW_LNS_copy:
11067 if (lh->num_file_names < file || file == 0)
11068 dwarf2_debug_line_missing_file_complaint ();
11069 else
11070 {
11071 lh->file_names[file - 1].included_p = 1;
11072 if (!decode_for_pst_p && is_stmt)
11073 {
11074 if (last_subfile != current_subfile)
11075 {
11076 addr = gdbarch_addr_bits_remove (gdbarch, address);
11077 if (last_subfile)
11078 (*p_record_line) (last_subfile, 0, addr);
11079 last_subfile = current_subfile;
11080 }
11081 addr = check_cu_functions (address, cu);
11082 addr = gdbarch_addr_bits_remove (gdbarch, addr);
11083 (*p_record_line) (current_subfile, line, addr);
11084 }
11085 }
11086 basic_block = 0;
11087 break;
11088 case DW_LNS_advance_pc:
11089 {
11090 CORE_ADDR adjust
11091 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11092
11093 address += (((op_index + adjust)
11094 / lh->maximum_ops_per_instruction)
11095 * lh->minimum_instruction_length);
11096 op_index = ((op_index + adjust)
11097 % lh->maximum_ops_per_instruction);
11098 line_ptr += bytes_read;
11099 }
11100 break;
11101 case DW_LNS_advance_line:
11102 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11103 line_ptr += bytes_read;
11104 break;
11105 case DW_LNS_set_file:
11106 {
11107 /* The arrays lh->include_dirs and lh->file_names are
11108 0-based, but the directory and file name numbers in
11109 the statement program are 1-based. */
11110 struct file_entry *fe;
11111 char *dir = NULL;
11112
11113 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11114 line_ptr += bytes_read;
11115 if (lh->num_file_names < file || file == 0)
11116 dwarf2_debug_line_missing_file_complaint ();
11117 else
11118 {
11119 fe = &lh->file_names[file - 1];
11120 if (fe->dir_index)
11121 dir = lh->include_dirs[fe->dir_index - 1];
11122 if (!decode_for_pst_p)
11123 {
11124 last_subfile = current_subfile;
11125 dwarf2_start_subfile (fe->name, dir, comp_dir);
11126 }
11127 }
11128 }
11129 break;
11130 case DW_LNS_set_column:
11131 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11132 line_ptr += bytes_read;
11133 break;
11134 case DW_LNS_negate_stmt:
11135 is_stmt = (!is_stmt);
11136 break;
11137 case DW_LNS_set_basic_block:
11138 basic_block = 1;
11139 break;
11140 /* Add to the address register of the state machine the
11141 address increment value corresponding to special opcode
11142 255. I.e., this value is scaled by the minimum
11143 instruction length since special opcode 255 would have
11144 scaled the increment. */
11145 case DW_LNS_const_add_pc:
11146 {
11147 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11148
11149 address += (((op_index + adjust)
11150 / lh->maximum_ops_per_instruction)
11151 * lh->minimum_instruction_length);
11152 op_index = ((op_index + adjust)
11153 % lh->maximum_ops_per_instruction);
11154 }
11155 break;
11156 case DW_LNS_fixed_advance_pc:
11157 address += read_2_bytes (abfd, line_ptr);
11158 op_index = 0;
11159 line_ptr += 2;
11160 break;
11161 default:
11162 {
11163 /* Unknown standard opcode, ignore it. */
11164 int i;
11165
11166 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11167 {
11168 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11169 line_ptr += bytes_read;
11170 }
11171 }
11172 }
11173 }
11174 if (lh->num_file_names < file || file == 0)
11175 dwarf2_debug_line_missing_file_complaint ();
11176 else
11177 {
11178 lh->file_names[file - 1].included_p = 1;
11179 if (!decode_for_pst_p)
11180 {
11181 addr = gdbarch_addr_bits_remove (gdbarch, address);
11182 (*p_record_line) (current_subfile, 0, addr);
11183 }
11184 }
11185 }
11186
11187 if (decode_for_pst_p)
11188 {
11189 int file_index;
11190
11191 /* Now that we're done scanning the Line Header Program, we can
11192 create the psymtab of each included file. */
11193 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11194 if (lh->file_names[file_index].included_p == 1)
11195 {
11196 char *include_name =
11197 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11198 if (include_name != NULL)
11199 dwarf2_create_include_psymtab (include_name, pst, objfile);
11200 }
11201 }
11202 else
11203 {
11204 /* Make sure a symtab is created for every file, even files
11205 which contain only variables (i.e. no code with associated
11206 line numbers). */
11207
11208 int i;
11209 struct file_entry *fe;
11210
11211 for (i = 0; i < lh->num_file_names; i++)
11212 {
11213 char *dir = NULL;
11214
11215 fe = &lh->file_names[i];
11216 if (fe->dir_index)
11217 dir = lh->include_dirs[fe->dir_index - 1];
11218 dwarf2_start_subfile (fe->name, dir, comp_dir);
11219
11220 /* Skip the main file; we don't need it, and it must be
11221 allocated last, so that it will show up before the
11222 non-primary symtabs in the objfile's symtab list. */
11223 if (current_subfile == first_subfile)
11224 continue;
11225
11226 if (current_subfile->symtab == NULL)
11227 current_subfile->symtab = allocate_symtab (current_subfile->name,
11228 cu->objfile);
11229 fe->symtab = current_subfile->symtab;
11230 }
11231 }
11232 }
11233
11234 /* Start a subfile for DWARF. FILENAME is the name of the file and
11235 DIRNAME the name of the source directory which contains FILENAME
11236 or NULL if not known. COMP_DIR is the compilation directory for the
11237 linetable's compilation unit or NULL if not known.
11238 This routine tries to keep line numbers from identical absolute and
11239 relative file names in a common subfile.
11240
11241 Using the `list' example from the GDB testsuite, which resides in
11242 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11243 of /srcdir/list0.c yields the following debugging information for list0.c:
11244
11245 DW_AT_name: /srcdir/list0.c
11246 DW_AT_comp_dir: /compdir
11247 files.files[0].name: list0.h
11248 files.files[0].dir: /srcdir
11249 files.files[1].name: list0.c
11250 files.files[1].dir: /srcdir
11251
11252 The line number information for list0.c has to end up in a single
11253 subfile, so that `break /srcdir/list0.c:1' works as expected.
11254 start_subfile will ensure that this happens provided that we pass the
11255 concatenation of files.files[1].dir and files.files[1].name as the
11256 subfile's name. */
11257
11258 static void
11259 dwarf2_start_subfile (char *filename, const char *dirname,
11260 const char *comp_dir)
11261 {
11262 char *fullname;
11263
11264 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11265 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11266 second argument to start_subfile. To be consistent, we do the
11267 same here. In order not to lose the line information directory,
11268 we concatenate it to the filename when it makes sense.
11269 Note that the Dwarf3 standard says (speaking of filenames in line
11270 information): ``The directory index is ignored for file names
11271 that represent full path names''. Thus ignoring dirname in the
11272 `else' branch below isn't an issue. */
11273
11274 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11275 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11276 else
11277 fullname = filename;
11278
11279 start_subfile (fullname, comp_dir);
11280
11281 if (fullname != filename)
11282 xfree (fullname);
11283 }
11284
11285 static void
11286 var_decode_location (struct attribute *attr, struct symbol *sym,
11287 struct dwarf2_cu *cu)
11288 {
11289 struct objfile *objfile = cu->objfile;
11290 struct comp_unit_head *cu_header = &cu->header;
11291
11292 /* NOTE drow/2003-01-30: There used to be a comment and some special
11293 code here to turn a symbol with DW_AT_external and a
11294 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11295 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11296 with some versions of binutils) where shared libraries could have
11297 relocations against symbols in their debug information - the
11298 minimal symbol would have the right address, but the debug info
11299 would not. It's no longer necessary, because we will explicitly
11300 apply relocations when we read in the debug information now. */
11301
11302 /* A DW_AT_location attribute with no contents indicates that a
11303 variable has been optimized away. */
11304 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11305 {
11306 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11307 return;
11308 }
11309
11310 /* Handle one degenerate form of location expression specially, to
11311 preserve GDB's previous behavior when section offsets are
11312 specified. If this is just a DW_OP_addr then mark this symbol
11313 as LOC_STATIC. */
11314
11315 if (attr_form_is_block (attr)
11316 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11317 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11318 {
11319 unsigned int dummy;
11320
11321 SYMBOL_VALUE_ADDRESS (sym) =
11322 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11323 SYMBOL_CLASS (sym) = LOC_STATIC;
11324 fixup_symbol_section (sym, objfile);
11325 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11326 SYMBOL_SECTION (sym));
11327 return;
11328 }
11329
11330 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11331 expression evaluator, and use LOC_COMPUTED only when necessary
11332 (i.e. when the value of a register or memory location is
11333 referenced, or a thread-local block, etc.). Then again, it might
11334 not be worthwhile. I'm assuming that it isn't unless performance
11335 or memory numbers show me otherwise. */
11336
11337 dwarf2_symbol_mark_computed (attr, sym, cu);
11338 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11339
11340 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11341 cu->has_loclist = 1;
11342 }
11343
11344 /* Given a pointer to a DWARF information entry, figure out if we need
11345 to make a symbol table entry for it, and if so, create a new entry
11346 and return a pointer to it.
11347 If TYPE is NULL, determine symbol type from the die, otherwise
11348 used the passed type.
11349 If SPACE is not NULL, use it to hold the new symbol. If it is
11350 NULL, allocate a new symbol on the objfile's obstack. */
11351
11352 static struct symbol *
11353 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11354 struct symbol *space)
11355 {
11356 struct objfile *objfile = cu->objfile;
11357 struct symbol *sym = NULL;
11358 char *name;
11359 struct attribute *attr = NULL;
11360 struct attribute *attr2 = NULL;
11361 CORE_ADDR baseaddr;
11362 struct pending **list_to_add = NULL;
11363
11364 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11365
11366 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11367
11368 name = dwarf2_name (die, cu);
11369 if (name)
11370 {
11371 const char *linkagename;
11372 int suppress_add = 0;
11373
11374 if (space)
11375 sym = space;
11376 else
11377 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11378 OBJSTAT (objfile, n_syms++);
11379
11380 /* Cache this symbol's name and the name's demangled form (if any). */
11381 SYMBOL_SET_LANGUAGE (sym, cu->language);
11382 linkagename = dwarf2_physname (name, die, cu);
11383 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11384
11385 /* Fortran does not have mangling standard and the mangling does differ
11386 between gfortran, iFort etc. */
11387 if (cu->language == language_fortran
11388 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11389 symbol_set_demangled_name (&(sym->ginfo),
11390 (char *) dwarf2_full_name (name, die, cu),
11391 NULL);
11392
11393 /* Default assumptions.
11394 Use the passed type or decode it from the die. */
11395 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11396 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11397 if (type != NULL)
11398 SYMBOL_TYPE (sym) = type;
11399 else
11400 SYMBOL_TYPE (sym) = die_type (die, cu);
11401 attr = dwarf2_attr (die,
11402 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11403 cu);
11404 if (attr)
11405 {
11406 SYMBOL_LINE (sym) = DW_UNSND (attr);
11407 }
11408
11409 attr = dwarf2_attr (die,
11410 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11411 cu);
11412 if (attr)
11413 {
11414 int file_index = DW_UNSND (attr);
11415
11416 if (cu->line_header == NULL
11417 || file_index > cu->line_header->num_file_names)
11418 complaint (&symfile_complaints,
11419 _("file index out of range"));
11420 else if (file_index > 0)
11421 {
11422 struct file_entry *fe;
11423
11424 fe = &cu->line_header->file_names[file_index - 1];
11425 SYMBOL_SYMTAB (sym) = fe->symtab;
11426 }
11427 }
11428
11429 switch (die->tag)
11430 {
11431 case DW_TAG_label:
11432 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11433 if (attr)
11434 {
11435 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11436 }
11437 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11438 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11439 SYMBOL_CLASS (sym) = LOC_LABEL;
11440 add_symbol_to_list (sym, cu->list_in_scope);
11441 break;
11442 case DW_TAG_subprogram:
11443 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11444 finish_block. */
11445 SYMBOL_CLASS (sym) = LOC_BLOCK;
11446 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11447 if ((attr2 && (DW_UNSND (attr2) != 0))
11448 || cu->language == language_ada)
11449 {
11450 /* Subprograms marked external are stored as a global symbol.
11451 Ada subprograms, whether marked external or not, are always
11452 stored as a global symbol, because we want to be able to
11453 access them globally. For instance, we want to be able
11454 to break on a nested subprogram without having to
11455 specify the context. */
11456 list_to_add = &global_symbols;
11457 }
11458 else
11459 {
11460 list_to_add = cu->list_in_scope;
11461 }
11462 break;
11463 case DW_TAG_inlined_subroutine:
11464 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11465 finish_block. */
11466 SYMBOL_CLASS (sym) = LOC_BLOCK;
11467 SYMBOL_INLINED (sym) = 1;
11468 /* Do not add the symbol to any lists. It will be found via
11469 BLOCK_FUNCTION from the blockvector. */
11470 break;
11471 case DW_TAG_template_value_param:
11472 suppress_add = 1;
11473 /* Fall through. */
11474 case DW_TAG_constant:
11475 case DW_TAG_variable:
11476 case DW_TAG_member:
11477 /* Compilation with minimal debug info may result in
11478 variables with missing type entries. Change the
11479 misleading `void' type to something sensible. */
11480 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11481 SYMBOL_TYPE (sym)
11482 = objfile_type (objfile)->nodebug_data_symbol;
11483
11484 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11485 /* In the case of DW_TAG_member, we should only be called for
11486 static const members. */
11487 if (die->tag == DW_TAG_member)
11488 {
11489 /* dwarf2_add_field uses die_is_declaration,
11490 so we do the same. */
11491 gdb_assert (die_is_declaration (die, cu));
11492 gdb_assert (attr);
11493 }
11494 if (attr)
11495 {
11496 dwarf2_const_value (attr, sym, cu);
11497 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11498 if (!suppress_add)
11499 {
11500 if (attr2 && (DW_UNSND (attr2) != 0))
11501 list_to_add = &global_symbols;
11502 else
11503 list_to_add = cu->list_in_scope;
11504 }
11505 break;
11506 }
11507 attr = dwarf2_attr (die, DW_AT_location, cu);
11508 if (attr)
11509 {
11510 var_decode_location (attr, sym, cu);
11511 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11512 if (SYMBOL_CLASS (sym) == LOC_STATIC
11513 && SYMBOL_VALUE_ADDRESS (sym) == 0
11514 && !dwarf2_per_objfile->has_section_at_zero)
11515 {
11516 /* When a static variable is eliminated by the linker,
11517 the corresponding debug information is not stripped
11518 out, but the variable address is set to null;
11519 do not add such variables into symbol table. */
11520 }
11521 else if (attr2 && (DW_UNSND (attr2) != 0))
11522 {
11523 /* Workaround gfortran PR debug/40040 - it uses
11524 DW_AT_location for variables in -fPIC libraries which may
11525 get overriden by other libraries/executable and get
11526 a different address. Resolve it by the minimal symbol
11527 which may come from inferior's executable using copy
11528 relocation. Make this workaround only for gfortran as for
11529 other compilers GDB cannot guess the minimal symbol
11530 Fortran mangling kind. */
11531 if (cu->language == language_fortran && die->parent
11532 && die->parent->tag == DW_TAG_module
11533 && cu->producer
11534 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11535 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11536
11537 /* A variable with DW_AT_external is never static,
11538 but it may be block-scoped. */
11539 list_to_add = (cu->list_in_scope == &file_symbols
11540 ? &global_symbols : cu->list_in_scope);
11541 }
11542 else
11543 list_to_add = cu->list_in_scope;
11544 }
11545 else
11546 {
11547 /* We do not know the address of this symbol.
11548 If it is an external symbol and we have type information
11549 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11550 The address of the variable will then be determined from
11551 the minimal symbol table whenever the variable is
11552 referenced. */
11553 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11554 if (attr2 && (DW_UNSND (attr2) != 0)
11555 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11556 {
11557 /* A variable with DW_AT_external is never static, but it
11558 may be block-scoped. */
11559 list_to_add = (cu->list_in_scope == &file_symbols
11560 ? &global_symbols : cu->list_in_scope);
11561
11562 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11563 }
11564 else if (!die_is_declaration (die, cu))
11565 {
11566 /* Use the default LOC_OPTIMIZED_OUT class. */
11567 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11568 if (!suppress_add)
11569 list_to_add = cu->list_in_scope;
11570 }
11571 }
11572 break;
11573 case DW_TAG_formal_parameter:
11574 /* If we are inside a function, mark this as an argument. If
11575 not, we might be looking at an argument to an inlined function
11576 when we do not have enough information to show inlined frames;
11577 pretend it's a local variable in that case so that the user can
11578 still see it. */
11579 if (context_stack_depth > 0
11580 && context_stack[context_stack_depth - 1].name != NULL)
11581 SYMBOL_IS_ARGUMENT (sym) = 1;
11582 attr = dwarf2_attr (die, DW_AT_location, cu);
11583 if (attr)
11584 {
11585 var_decode_location (attr, sym, cu);
11586 }
11587 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11588 if (attr)
11589 {
11590 dwarf2_const_value (attr, sym, cu);
11591 }
11592 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11593 if (attr && DW_UNSND (attr))
11594 {
11595 struct type *ref_type;
11596
11597 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11598 SYMBOL_TYPE (sym) = ref_type;
11599 }
11600
11601 list_to_add = cu->list_in_scope;
11602 break;
11603 case DW_TAG_unspecified_parameters:
11604 /* From varargs functions; gdb doesn't seem to have any
11605 interest in this information, so just ignore it for now.
11606 (FIXME?) */
11607 break;
11608 case DW_TAG_template_type_param:
11609 suppress_add = 1;
11610 /* Fall through. */
11611 case DW_TAG_class_type:
11612 case DW_TAG_interface_type:
11613 case DW_TAG_structure_type:
11614 case DW_TAG_union_type:
11615 case DW_TAG_set_type:
11616 case DW_TAG_enumeration_type:
11617 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11618 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11619
11620 {
11621 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11622 really ever be static objects: otherwise, if you try
11623 to, say, break of a class's method and you're in a file
11624 which doesn't mention that class, it won't work unless
11625 the check for all static symbols in lookup_symbol_aux
11626 saves you. See the OtherFileClass tests in
11627 gdb.c++/namespace.exp. */
11628
11629 if (!suppress_add)
11630 {
11631 list_to_add = (cu->list_in_scope == &file_symbols
11632 && (cu->language == language_cplus
11633 || cu->language == language_java)
11634 ? &global_symbols : cu->list_in_scope);
11635
11636 /* The semantics of C++ state that "struct foo {
11637 ... }" also defines a typedef for "foo". A Java
11638 class declaration also defines a typedef for the
11639 class. */
11640 if (cu->language == language_cplus
11641 || cu->language == language_java
11642 || cu->language == language_ada)
11643 {
11644 /* The symbol's name is already allocated along
11645 with this objfile, so we don't need to
11646 duplicate it for the type. */
11647 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11648 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11649 }
11650 }
11651 }
11652 break;
11653 case DW_TAG_typedef:
11654 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11655 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11656 list_to_add = cu->list_in_scope;
11657 break;
11658 case DW_TAG_base_type:
11659 case DW_TAG_subrange_type:
11660 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11661 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11662 list_to_add = cu->list_in_scope;
11663 break;
11664 case DW_TAG_enumerator:
11665 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11666 if (attr)
11667 {
11668 dwarf2_const_value (attr, sym, cu);
11669 }
11670 {
11671 /* NOTE: carlton/2003-11-10: See comment above in the
11672 DW_TAG_class_type, etc. block. */
11673
11674 list_to_add = (cu->list_in_scope == &file_symbols
11675 && (cu->language == language_cplus
11676 || cu->language == language_java)
11677 ? &global_symbols : cu->list_in_scope);
11678 }
11679 break;
11680 case DW_TAG_namespace:
11681 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11682 list_to_add = &global_symbols;
11683 break;
11684 default:
11685 /* Not a tag we recognize. Hopefully we aren't processing
11686 trash data, but since we must specifically ignore things
11687 we don't recognize, there is nothing else we should do at
11688 this point. */
11689 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11690 dwarf_tag_name (die->tag));
11691 break;
11692 }
11693
11694 if (suppress_add)
11695 {
11696 sym->hash_next = objfile->template_symbols;
11697 objfile->template_symbols = sym;
11698 list_to_add = NULL;
11699 }
11700
11701 if (list_to_add != NULL)
11702 add_symbol_to_list (sym, list_to_add);
11703
11704 /* For the benefit of old versions of GCC, check for anonymous
11705 namespaces based on the demangled name. */
11706 if (!processing_has_namespace_info
11707 && cu->language == language_cplus)
11708 cp_scan_for_anonymous_namespaces (sym);
11709 }
11710 return (sym);
11711 }
11712
11713 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11714
11715 static struct symbol *
11716 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11717 {
11718 return new_symbol_full (die, type, cu, NULL);
11719 }
11720
11721 /* Given an attr with a DW_FORM_dataN value in host byte order,
11722 zero-extend it as appropriate for the symbol's type. The DWARF
11723 standard (v4) is not entirely clear about the meaning of using
11724 DW_FORM_dataN for a constant with a signed type, where the type is
11725 wider than the data. The conclusion of a discussion on the DWARF
11726 list was that this is unspecified. We choose to always zero-extend
11727 because that is the interpretation long in use by GCC. */
11728
11729 static gdb_byte *
11730 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11731 const char *name, struct obstack *obstack,
11732 struct dwarf2_cu *cu, long *value, int bits)
11733 {
11734 struct objfile *objfile = cu->objfile;
11735 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11736 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11737 LONGEST l = DW_UNSND (attr);
11738
11739 if (bits < sizeof (*value) * 8)
11740 {
11741 l &= ((LONGEST) 1 << bits) - 1;
11742 *value = l;
11743 }
11744 else if (bits == sizeof (*value) * 8)
11745 *value = l;
11746 else
11747 {
11748 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11749 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11750 return bytes;
11751 }
11752
11753 return NULL;
11754 }
11755
11756 /* Read a constant value from an attribute. Either set *VALUE, or if
11757 the value does not fit in *VALUE, set *BYTES - either already
11758 allocated on the objfile obstack, or newly allocated on OBSTACK,
11759 or, set *BATON, if we translated the constant to a location
11760 expression. */
11761
11762 static void
11763 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11764 const char *name, struct obstack *obstack,
11765 struct dwarf2_cu *cu,
11766 long *value, gdb_byte **bytes,
11767 struct dwarf2_locexpr_baton **baton)
11768 {
11769 struct objfile *objfile = cu->objfile;
11770 struct comp_unit_head *cu_header = &cu->header;
11771 struct dwarf_block *blk;
11772 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11773 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11774
11775 *value = 0;
11776 *bytes = NULL;
11777 *baton = NULL;
11778
11779 switch (attr->form)
11780 {
11781 case DW_FORM_addr:
11782 {
11783 gdb_byte *data;
11784
11785 if (TYPE_LENGTH (type) != cu_header->addr_size)
11786 dwarf2_const_value_length_mismatch_complaint (name,
11787 cu_header->addr_size,
11788 TYPE_LENGTH (type));
11789 /* Symbols of this form are reasonably rare, so we just
11790 piggyback on the existing location code rather than writing
11791 a new implementation of symbol_computed_ops. */
11792 *baton = obstack_alloc (&objfile->objfile_obstack,
11793 sizeof (struct dwarf2_locexpr_baton));
11794 (*baton)->per_cu = cu->per_cu;
11795 gdb_assert ((*baton)->per_cu);
11796
11797 (*baton)->size = 2 + cu_header->addr_size;
11798 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11799 (*baton)->data = data;
11800
11801 data[0] = DW_OP_addr;
11802 store_unsigned_integer (&data[1], cu_header->addr_size,
11803 byte_order, DW_ADDR (attr));
11804 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11805 }
11806 break;
11807 case DW_FORM_string:
11808 case DW_FORM_strp:
11809 /* DW_STRING is already allocated on the objfile obstack, point
11810 directly to it. */
11811 *bytes = (gdb_byte *) DW_STRING (attr);
11812 break;
11813 case DW_FORM_block1:
11814 case DW_FORM_block2:
11815 case DW_FORM_block4:
11816 case DW_FORM_block:
11817 case DW_FORM_exprloc:
11818 blk = DW_BLOCK (attr);
11819 if (TYPE_LENGTH (type) != blk->size)
11820 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11821 TYPE_LENGTH (type));
11822 *bytes = blk->data;
11823 break;
11824
11825 /* The DW_AT_const_value attributes are supposed to carry the
11826 symbol's value "represented as it would be on the target
11827 architecture." By the time we get here, it's already been
11828 converted to host endianness, so we just need to sign- or
11829 zero-extend it as appropriate. */
11830 case DW_FORM_data1:
11831 *bytes = dwarf2_const_value_data (attr, type, name,
11832 obstack, cu, value, 8);
11833 break;
11834 case DW_FORM_data2:
11835 *bytes = dwarf2_const_value_data (attr, type, name,
11836 obstack, cu, value, 16);
11837 break;
11838 case DW_FORM_data4:
11839 *bytes = dwarf2_const_value_data (attr, type, name,
11840 obstack, cu, value, 32);
11841 break;
11842 case DW_FORM_data8:
11843 *bytes = dwarf2_const_value_data (attr, type, name,
11844 obstack, cu, value, 64);
11845 break;
11846
11847 case DW_FORM_sdata:
11848 *value = DW_SND (attr);
11849 break;
11850
11851 case DW_FORM_udata:
11852 *value = DW_UNSND (attr);
11853 break;
11854
11855 default:
11856 complaint (&symfile_complaints,
11857 _("unsupported const value attribute form: '%s'"),
11858 dwarf_form_name (attr->form));
11859 *value = 0;
11860 break;
11861 }
11862 }
11863
11864
11865 /* Copy constant value from an attribute to a symbol. */
11866
11867 static void
11868 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11869 struct dwarf2_cu *cu)
11870 {
11871 struct objfile *objfile = cu->objfile;
11872 struct comp_unit_head *cu_header = &cu->header;
11873 long value;
11874 gdb_byte *bytes;
11875 struct dwarf2_locexpr_baton *baton;
11876
11877 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11878 SYMBOL_PRINT_NAME (sym),
11879 &objfile->objfile_obstack, cu,
11880 &value, &bytes, &baton);
11881
11882 if (baton != NULL)
11883 {
11884 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11885 SYMBOL_LOCATION_BATON (sym) = baton;
11886 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11887 }
11888 else if (bytes != NULL)
11889 {
11890 SYMBOL_VALUE_BYTES (sym) = bytes;
11891 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11892 }
11893 else
11894 {
11895 SYMBOL_VALUE (sym) = value;
11896 SYMBOL_CLASS (sym) = LOC_CONST;
11897 }
11898 }
11899
11900 /* Return the type of the die in question using its DW_AT_type attribute. */
11901
11902 static struct type *
11903 die_type (struct die_info *die, struct dwarf2_cu *cu)
11904 {
11905 struct attribute *type_attr;
11906
11907 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11908 if (!type_attr)
11909 {
11910 /* A missing DW_AT_type represents a void type. */
11911 return objfile_type (cu->objfile)->builtin_void;
11912 }
11913
11914 return lookup_die_type (die, type_attr, cu);
11915 }
11916
11917 /* True iff CU's producer generates GNAT Ada auxiliary information
11918 that allows to find parallel types through that information instead
11919 of having to do expensive parallel lookups by type name. */
11920
11921 static int
11922 need_gnat_info (struct dwarf2_cu *cu)
11923 {
11924 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11925 of GNAT produces this auxiliary information, without any indication
11926 that it is produced. Part of enhancing the FSF version of GNAT
11927 to produce that information will be to put in place an indicator
11928 that we can use in order to determine whether the descriptive type
11929 info is available or not. One suggestion that has been made is
11930 to use a new attribute, attached to the CU die. For now, assume
11931 that the descriptive type info is not available. */
11932 return 0;
11933 }
11934
11935 /* Return the auxiliary type of the die in question using its
11936 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11937 attribute is not present. */
11938
11939 static struct type *
11940 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11941 {
11942 struct attribute *type_attr;
11943
11944 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11945 if (!type_attr)
11946 return NULL;
11947
11948 return lookup_die_type (die, type_attr, cu);
11949 }
11950
11951 /* If DIE has a descriptive_type attribute, then set the TYPE's
11952 descriptive type accordingly. */
11953
11954 static void
11955 set_descriptive_type (struct type *type, struct die_info *die,
11956 struct dwarf2_cu *cu)
11957 {
11958 struct type *descriptive_type = die_descriptive_type (die, cu);
11959
11960 if (descriptive_type)
11961 {
11962 ALLOCATE_GNAT_AUX_TYPE (type);
11963 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11964 }
11965 }
11966
11967 /* Return the containing type of the die in question using its
11968 DW_AT_containing_type attribute. */
11969
11970 static struct type *
11971 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11972 {
11973 struct attribute *type_attr;
11974
11975 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11976 if (!type_attr)
11977 error (_("Dwarf Error: Problem turning containing type into gdb type "
11978 "[in module %s]"), cu->objfile->name);
11979
11980 return lookup_die_type (die, type_attr, cu);
11981 }
11982
11983 /* Look up the type of DIE in CU using its type attribute ATTR.
11984 If there is no type substitute an error marker. */
11985
11986 static struct type *
11987 lookup_die_type (struct die_info *die, struct attribute *attr,
11988 struct dwarf2_cu *cu)
11989 {
11990 struct type *this_type;
11991
11992 /* First see if we have it cached. */
11993
11994 if (is_ref_attr (attr))
11995 {
11996 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11997
11998 this_type = get_die_type_at_offset (offset, cu->per_cu);
11999 }
12000 else if (attr->form == DW_FORM_ref_sig8)
12001 {
12002 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12003 struct dwarf2_cu *sig_cu;
12004 unsigned int offset;
12005
12006 /* sig_type will be NULL if the signatured type is missing from
12007 the debug info. */
12008 if (sig_type == NULL)
12009 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12010 "at 0x%x [in module %s]"),
12011 die->offset, cu->objfile->name);
12012
12013 gdb_assert (sig_type->per_cu.debug_type_section);
12014 offset = sig_type->per_cu.offset + sig_type->type_offset;
12015 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12016 }
12017 else
12018 {
12019 dump_die_for_error (die);
12020 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12021 dwarf_attr_name (attr->name), cu->objfile->name);
12022 }
12023
12024 /* If not cached we need to read it in. */
12025
12026 if (this_type == NULL)
12027 {
12028 struct die_info *type_die;
12029 struct dwarf2_cu *type_cu = cu;
12030
12031 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12032 /* If the type is cached, we should have found it above. */
12033 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12034 this_type = read_type_die_1 (type_die, type_cu);
12035 }
12036
12037 /* If we still don't have a type use an error marker. */
12038
12039 if (this_type == NULL)
12040 {
12041 char *message, *saved;
12042
12043 /* read_type_die already issued a complaint. */
12044 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12045 cu->objfile->name,
12046 cu->header.offset,
12047 die->offset);
12048 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12049 message, strlen (message));
12050 xfree (message);
12051
12052 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
12053 }
12054
12055 return this_type;
12056 }
12057
12058 /* Return the type in DIE, CU.
12059 Returns NULL for invalid types.
12060
12061 This first does a lookup in the appropriate type_hash table,
12062 and only reads the die in if necessary.
12063
12064 NOTE: This can be called when reading in partial or full symbols. */
12065
12066 static struct type *
12067 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12068 {
12069 struct type *this_type;
12070
12071 this_type = get_die_type (die, cu);
12072 if (this_type)
12073 return this_type;
12074
12075 return read_type_die_1 (die, cu);
12076 }
12077
12078 /* Read the type in DIE, CU.
12079 Returns NULL for invalid types. */
12080
12081 static struct type *
12082 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12083 {
12084 struct type *this_type = NULL;
12085
12086 switch (die->tag)
12087 {
12088 case DW_TAG_class_type:
12089 case DW_TAG_interface_type:
12090 case DW_TAG_structure_type:
12091 case DW_TAG_union_type:
12092 this_type = read_structure_type (die, cu);
12093 break;
12094 case DW_TAG_enumeration_type:
12095 this_type = read_enumeration_type (die, cu);
12096 break;
12097 case DW_TAG_subprogram:
12098 case DW_TAG_subroutine_type:
12099 case DW_TAG_inlined_subroutine:
12100 this_type = read_subroutine_type (die, cu);
12101 break;
12102 case DW_TAG_array_type:
12103 this_type = read_array_type (die, cu);
12104 break;
12105 case DW_TAG_set_type:
12106 this_type = read_set_type (die, cu);
12107 break;
12108 case DW_TAG_pointer_type:
12109 this_type = read_tag_pointer_type (die, cu);
12110 break;
12111 case DW_TAG_ptr_to_member_type:
12112 this_type = read_tag_ptr_to_member_type (die, cu);
12113 break;
12114 case DW_TAG_reference_type:
12115 this_type = read_tag_reference_type (die, cu);
12116 break;
12117 case DW_TAG_const_type:
12118 this_type = read_tag_const_type (die, cu);
12119 break;
12120 case DW_TAG_volatile_type:
12121 this_type = read_tag_volatile_type (die, cu);
12122 break;
12123 case DW_TAG_string_type:
12124 this_type = read_tag_string_type (die, cu);
12125 break;
12126 case DW_TAG_typedef:
12127 this_type = read_typedef (die, cu);
12128 break;
12129 case DW_TAG_subrange_type:
12130 this_type = read_subrange_type (die, cu);
12131 break;
12132 case DW_TAG_base_type:
12133 this_type = read_base_type (die, cu);
12134 break;
12135 case DW_TAG_unspecified_type:
12136 this_type = read_unspecified_type (die, cu);
12137 break;
12138 case DW_TAG_namespace:
12139 this_type = read_namespace_type (die, cu);
12140 break;
12141 case DW_TAG_module:
12142 this_type = read_module_type (die, cu);
12143 break;
12144 default:
12145 complaint (&symfile_complaints,
12146 _("unexpected tag in read_type_die: '%s'"),
12147 dwarf_tag_name (die->tag));
12148 break;
12149 }
12150
12151 return this_type;
12152 }
12153
12154 /* See if we can figure out if the class lives in a namespace. We do
12155 this by looking for a member function; its demangled name will
12156 contain namespace info, if there is any.
12157 Return the computed name or NULL.
12158 Space for the result is allocated on the objfile's obstack.
12159 This is the full-die version of guess_partial_die_structure_name.
12160 In this case we know DIE has no useful parent. */
12161
12162 static char *
12163 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12164 {
12165 struct die_info *spec_die;
12166 struct dwarf2_cu *spec_cu;
12167 struct die_info *child;
12168
12169 spec_cu = cu;
12170 spec_die = die_specification (die, &spec_cu);
12171 if (spec_die != NULL)
12172 {
12173 die = spec_die;
12174 cu = spec_cu;
12175 }
12176
12177 for (child = die->child;
12178 child != NULL;
12179 child = child->sibling)
12180 {
12181 if (child->tag == DW_TAG_subprogram)
12182 {
12183 struct attribute *attr;
12184
12185 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12186 if (attr == NULL)
12187 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12188 if (attr != NULL)
12189 {
12190 char *actual_name
12191 = language_class_name_from_physname (cu->language_defn,
12192 DW_STRING (attr));
12193 char *name = NULL;
12194
12195 if (actual_name != NULL)
12196 {
12197 char *die_name = dwarf2_name (die, cu);
12198
12199 if (die_name != NULL
12200 && strcmp (die_name, actual_name) != 0)
12201 {
12202 /* Strip off the class name from the full name.
12203 We want the prefix. */
12204 int die_name_len = strlen (die_name);
12205 int actual_name_len = strlen (actual_name);
12206
12207 /* Test for '::' as a sanity check. */
12208 if (actual_name_len > die_name_len + 2
12209 && actual_name[actual_name_len
12210 - die_name_len - 1] == ':')
12211 name =
12212 obsavestring (actual_name,
12213 actual_name_len - die_name_len - 2,
12214 &cu->objfile->objfile_obstack);
12215 }
12216 }
12217 xfree (actual_name);
12218 return name;
12219 }
12220 }
12221 }
12222
12223 return NULL;
12224 }
12225
12226 /* Return the name of the namespace/class that DIE is defined within,
12227 or "" if we can't tell. The caller should not xfree the result.
12228
12229 For example, if we're within the method foo() in the following
12230 code:
12231
12232 namespace N {
12233 class C {
12234 void foo () {
12235 }
12236 };
12237 }
12238
12239 then determine_prefix on foo's die will return "N::C". */
12240
12241 static char *
12242 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12243 {
12244 struct die_info *parent, *spec_die;
12245 struct dwarf2_cu *spec_cu;
12246 struct type *parent_type;
12247
12248 if (cu->language != language_cplus && cu->language != language_java
12249 && cu->language != language_fortran)
12250 return "";
12251
12252 /* We have to be careful in the presence of DW_AT_specification.
12253 For example, with GCC 3.4, given the code
12254
12255 namespace N {
12256 void foo() {
12257 // Definition of N::foo.
12258 }
12259 }
12260
12261 then we'll have a tree of DIEs like this:
12262
12263 1: DW_TAG_compile_unit
12264 2: DW_TAG_namespace // N
12265 3: DW_TAG_subprogram // declaration of N::foo
12266 4: DW_TAG_subprogram // definition of N::foo
12267 DW_AT_specification // refers to die #3
12268
12269 Thus, when processing die #4, we have to pretend that we're in
12270 the context of its DW_AT_specification, namely the contex of die
12271 #3. */
12272 spec_cu = cu;
12273 spec_die = die_specification (die, &spec_cu);
12274 if (spec_die == NULL)
12275 parent = die->parent;
12276 else
12277 {
12278 parent = spec_die->parent;
12279 cu = spec_cu;
12280 }
12281
12282 if (parent == NULL)
12283 return "";
12284 else if (parent->building_fullname)
12285 {
12286 const char *name;
12287 const char *parent_name;
12288
12289 /* It has been seen on RealView 2.2 built binaries,
12290 DW_TAG_template_type_param types actually _defined_ as
12291 children of the parent class:
12292
12293 enum E {};
12294 template class <class Enum> Class{};
12295 Class<enum E> class_e;
12296
12297 1: DW_TAG_class_type (Class)
12298 2: DW_TAG_enumeration_type (E)
12299 3: DW_TAG_enumerator (enum1:0)
12300 3: DW_TAG_enumerator (enum2:1)
12301 ...
12302 2: DW_TAG_template_type_param
12303 DW_AT_type DW_FORM_ref_udata (E)
12304
12305 Besides being broken debug info, it can put GDB into an
12306 infinite loop. Consider:
12307
12308 When we're building the full name for Class<E>, we'll start
12309 at Class, and go look over its template type parameters,
12310 finding E. We'll then try to build the full name of E, and
12311 reach here. We're now trying to build the full name of E,
12312 and look over the parent DIE for containing scope. In the
12313 broken case, if we followed the parent DIE of E, we'd again
12314 find Class, and once again go look at its template type
12315 arguments, etc., etc. Simply don't consider such parent die
12316 as source-level parent of this die (it can't be, the language
12317 doesn't allow it), and break the loop here. */
12318 name = dwarf2_name (die, cu);
12319 parent_name = dwarf2_name (parent, cu);
12320 complaint (&symfile_complaints,
12321 _("template param type '%s' defined within parent '%s'"),
12322 name ? name : "<unknown>",
12323 parent_name ? parent_name : "<unknown>");
12324 return "";
12325 }
12326 else
12327 switch (parent->tag)
12328 {
12329 case DW_TAG_namespace:
12330 parent_type = read_type_die (parent, cu);
12331 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12332 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12333 Work around this problem here. */
12334 if (cu->language == language_cplus
12335 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12336 return "";
12337 /* We give a name to even anonymous namespaces. */
12338 return TYPE_TAG_NAME (parent_type);
12339 case DW_TAG_class_type:
12340 case DW_TAG_interface_type:
12341 case DW_TAG_structure_type:
12342 case DW_TAG_union_type:
12343 case DW_TAG_module:
12344 parent_type = read_type_die (parent, cu);
12345 if (TYPE_TAG_NAME (parent_type) != NULL)
12346 return TYPE_TAG_NAME (parent_type);
12347 else
12348 /* An anonymous structure is only allowed non-static data
12349 members; no typedefs, no member functions, et cetera.
12350 So it does not need a prefix. */
12351 return "";
12352 case DW_TAG_compile_unit:
12353 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12354 if (cu->language == language_cplus
12355 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12356 && die->child != NULL
12357 && (die->tag == DW_TAG_class_type
12358 || die->tag == DW_TAG_structure_type
12359 || die->tag == DW_TAG_union_type))
12360 {
12361 char *name = guess_full_die_structure_name (die, cu);
12362 if (name != NULL)
12363 return name;
12364 }
12365 return "";
12366 default:
12367 return determine_prefix (parent, cu);
12368 }
12369 }
12370
12371 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12372 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12373 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12374 an obconcat, otherwise allocate storage for the result. The CU argument is
12375 used to determine the language and hence, the appropriate separator. */
12376
12377 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12378
12379 static char *
12380 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12381 int physname, struct dwarf2_cu *cu)
12382 {
12383 const char *lead = "";
12384 const char *sep;
12385
12386 if (suffix == NULL || suffix[0] == '\0'
12387 || prefix == NULL || prefix[0] == '\0')
12388 sep = "";
12389 else if (cu->language == language_java)
12390 sep = ".";
12391 else if (cu->language == language_fortran && physname)
12392 {
12393 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12394 DW_AT_MIPS_linkage_name is preferred and used instead. */
12395
12396 lead = "__";
12397 sep = "_MOD_";
12398 }
12399 else
12400 sep = "::";
12401
12402 if (prefix == NULL)
12403 prefix = "";
12404 if (suffix == NULL)
12405 suffix = "";
12406
12407 if (obs == NULL)
12408 {
12409 char *retval
12410 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12411
12412 strcpy (retval, lead);
12413 strcat (retval, prefix);
12414 strcat (retval, sep);
12415 strcat (retval, suffix);
12416 return retval;
12417 }
12418 else
12419 {
12420 /* We have an obstack. */
12421 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12422 }
12423 }
12424
12425 /* Return sibling of die, NULL if no sibling. */
12426
12427 static struct die_info *
12428 sibling_die (struct die_info *die)
12429 {
12430 return die->sibling;
12431 }
12432
12433 /* Get name of a die, return NULL if not found. */
12434
12435 static char *
12436 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12437 struct obstack *obstack)
12438 {
12439 if (name && cu->language == language_cplus)
12440 {
12441 char *canon_name = cp_canonicalize_string (name);
12442
12443 if (canon_name != NULL)
12444 {
12445 if (strcmp (canon_name, name) != 0)
12446 name = obsavestring (canon_name, strlen (canon_name),
12447 obstack);
12448 xfree (canon_name);
12449 }
12450 }
12451
12452 return name;
12453 }
12454
12455 /* Get name of a die, return NULL if not found. */
12456
12457 static char *
12458 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12459 {
12460 struct attribute *attr;
12461
12462 attr = dwarf2_attr (die, DW_AT_name, cu);
12463 if ((!attr || !DW_STRING (attr))
12464 && die->tag != DW_TAG_class_type
12465 && die->tag != DW_TAG_interface_type
12466 && die->tag != DW_TAG_structure_type
12467 && die->tag != DW_TAG_union_type)
12468 return NULL;
12469
12470 switch (die->tag)
12471 {
12472 case DW_TAG_compile_unit:
12473 /* Compilation units have a DW_AT_name that is a filename, not
12474 a source language identifier. */
12475 case DW_TAG_enumeration_type:
12476 case DW_TAG_enumerator:
12477 /* These tags always have simple identifiers already; no need
12478 to canonicalize them. */
12479 return DW_STRING (attr);
12480
12481 case DW_TAG_subprogram:
12482 /* Java constructors will all be named "<init>", so return
12483 the class name when we see this special case. */
12484 if (cu->language == language_java
12485 && DW_STRING (attr) != NULL
12486 && strcmp (DW_STRING (attr), "<init>") == 0)
12487 {
12488 struct dwarf2_cu *spec_cu = cu;
12489 struct die_info *spec_die;
12490
12491 /* GCJ will output '<init>' for Java constructor names.
12492 For this special case, return the name of the parent class. */
12493
12494 /* GCJ may output suprogram DIEs with AT_specification set.
12495 If so, use the name of the specified DIE. */
12496 spec_die = die_specification (die, &spec_cu);
12497 if (spec_die != NULL)
12498 return dwarf2_name (spec_die, spec_cu);
12499
12500 do
12501 {
12502 die = die->parent;
12503 if (die->tag == DW_TAG_class_type)
12504 return dwarf2_name (die, cu);
12505 }
12506 while (die->tag != DW_TAG_compile_unit);
12507 }
12508 break;
12509
12510 case DW_TAG_class_type:
12511 case DW_TAG_interface_type:
12512 case DW_TAG_structure_type:
12513 case DW_TAG_union_type:
12514 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12515 structures or unions. These were of the form "._%d" in GCC 4.1,
12516 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12517 and GCC 4.4. We work around this problem by ignoring these. */
12518 if (attr && DW_STRING (attr)
12519 && (strncmp (DW_STRING (attr), "._", 2) == 0
12520 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12521 return NULL;
12522
12523 /* GCC might emit a nameless typedef that has a linkage name. See
12524 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12525 if (!attr || DW_STRING (attr) == NULL)
12526 {
12527 char *demangled = NULL;
12528
12529 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12530 if (attr == NULL)
12531 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12532
12533 if (attr == NULL || DW_STRING (attr) == NULL)
12534 return NULL;
12535
12536 /* Avoid demangling DW_STRING (attr) the second time on a second
12537 call for the same DIE. */
12538 if (!DW_STRING_IS_CANONICAL (attr))
12539 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12540
12541 if (demangled)
12542 {
12543 /* FIXME: we already did this for the partial symbol... */
12544 DW_STRING (attr)
12545 = obsavestring (demangled, strlen (demangled),
12546 &cu->objfile->objfile_obstack);
12547 DW_STRING_IS_CANONICAL (attr) = 1;
12548 xfree (demangled);
12549 }
12550 }
12551 break;
12552
12553 default:
12554 break;
12555 }
12556
12557 if (!DW_STRING_IS_CANONICAL (attr))
12558 {
12559 DW_STRING (attr)
12560 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12561 &cu->objfile->objfile_obstack);
12562 DW_STRING_IS_CANONICAL (attr) = 1;
12563 }
12564 return DW_STRING (attr);
12565 }
12566
12567 /* Return the die that this die in an extension of, or NULL if there
12568 is none. *EXT_CU is the CU containing DIE on input, and the CU
12569 containing the return value on output. */
12570
12571 static struct die_info *
12572 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12573 {
12574 struct attribute *attr;
12575
12576 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12577 if (attr == NULL)
12578 return NULL;
12579
12580 return follow_die_ref (die, attr, ext_cu);
12581 }
12582
12583 /* Convert a DIE tag into its string name. */
12584
12585 static char *
12586 dwarf_tag_name (unsigned tag)
12587 {
12588 switch (tag)
12589 {
12590 case DW_TAG_padding:
12591 return "DW_TAG_padding";
12592 case DW_TAG_array_type:
12593 return "DW_TAG_array_type";
12594 case DW_TAG_class_type:
12595 return "DW_TAG_class_type";
12596 case DW_TAG_entry_point:
12597 return "DW_TAG_entry_point";
12598 case DW_TAG_enumeration_type:
12599 return "DW_TAG_enumeration_type";
12600 case DW_TAG_formal_parameter:
12601 return "DW_TAG_formal_parameter";
12602 case DW_TAG_imported_declaration:
12603 return "DW_TAG_imported_declaration";
12604 case DW_TAG_label:
12605 return "DW_TAG_label";
12606 case DW_TAG_lexical_block:
12607 return "DW_TAG_lexical_block";
12608 case DW_TAG_member:
12609 return "DW_TAG_member";
12610 case DW_TAG_pointer_type:
12611 return "DW_TAG_pointer_type";
12612 case DW_TAG_reference_type:
12613 return "DW_TAG_reference_type";
12614 case DW_TAG_compile_unit:
12615 return "DW_TAG_compile_unit";
12616 case DW_TAG_string_type:
12617 return "DW_TAG_string_type";
12618 case DW_TAG_structure_type:
12619 return "DW_TAG_structure_type";
12620 case DW_TAG_subroutine_type:
12621 return "DW_TAG_subroutine_type";
12622 case DW_TAG_typedef:
12623 return "DW_TAG_typedef";
12624 case DW_TAG_union_type:
12625 return "DW_TAG_union_type";
12626 case DW_TAG_unspecified_parameters:
12627 return "DW_TAG_unspecified_parameters";
12628 case DW_TAG_variant:
12629 return "DW_TAG_variant";
12630 case DW_TAG_common_block:
12631 return "DW_TAG_common_block";
12632 case DW_TAG_common_inclusion:
12633 return "DW_TAG_common_inclusion";
12634 case DW_TAG_inheritance:
12635 return "DW_TAG_inheritance";
12636 case DW_TAG_inlined_subroutine:
12637 return "DW_TAG_inlined_subroutine";
12638 case DW_TAG_module:
12639 return "DW_TAG_module";
12640 case DW_TAG_ptr_to_member_type:
12641 return "DW_TAG_ptr_to_member_type";
12642 case DW_TAG_set_type:
12643 return "DW_TAG_set_type";
12644 case DW_TAG_subrange_type:
12645 return "DW_TAG_subrange_type";
12646 case DW_TAG_with_stmt:
12647 return "DW_TAG_with_stmt";
12648 case DW_TAG_access_declaration:
12649 return "DW_TAG_access_declaration";
12650 case DW_TAG_base_type:
12651 return "DW_TAG_base_type";
12652 case DW_TAG_catch_block:
12653 return "DW_TAG_catch_block";
12654 case DW_TAG_const_type:
12655 return "DW_TAG_const_type";
12656 case DW_TAG_constant:
12657 return "DW_TAG_constant";
12658 case DW_TAG_enumerator:
12659 return "DW_TAG_enumerator";
12660 case DW_TAG_file_type:
12661 return "DW_TAG_file_type";
12662 case DW_TAG_friend:
12663 return "DW_TAG_friend";
12664 case DW_TAG_namelist:
12665 return "DW_TAG_namelist";
12666 case DW_TAG_namelist_item:
12667 return "DW_TAG_namelist_item";
12668 case DW_TAG_packed_type:
12669 return "DW_TAG_packed_type";
12670 case DW_TAG_subprogram:
12671 return "DW_TAG_subprogram";
12672 case DW_TAG_template_type_param:
12673 return "DW_TAG_template_type_param";
12674 case DW_TAG_template_value_param:
12675 return "DW_TAG_template_value_param";
12676 case DW_TAG_thrown_type:
12677 return "DW_TAG_thrown_type";
12678 case DW_TAG_try_block:
12679 return "DW_TAG_try_block";
12680 case DW_TAG_variant_part:
12681 return "DW_TAG_variant_part";
12682 case DW_TAG_variable:
12683 return "DW_TAG_variable";
12684 case DW_TAG_volatile_type:
12685 return "DW_TAG_volatile_type";
12686 case DW_TAG_dwarf_procedure:
12687 return "DW_TAG_dwarf_procedure";
12688 case DW_TAG_restrict_type:
12689 return "DW_TAG_restrict_type";
12690 case DW_TAG_interface_type:
12691 return "DW_TAG_interface_type";
12692 case DW_TAG_namespace:
12693 return "DW_TAG_namespace";
12694 case DW_TAG_imported_module:
12695 return "DW_TAG_imported_module";
12696 case DW_TAG_unspecified_type:
12697 return "DW_TAG_unspecified_type";
12698 case DW_TAG_partial_unit:
12699 return "DW_TAG_partial_unit";
12700 case DW_TAG_imported_unit:
12701 return "DW_TAG_imported_unit";
12702 case DW_TAG_condition:
12703 return "DW_TAG_condition";
12704 case DW_TAG_shared_type:
12705 return "DW_TAG_shared_type";
12706 case DW_TAG_type_unit:
12707 return "DW_TAG_type_unit";
12708 case DW_TAG_MIPS_loop:
12709 return "DW_TAG_MIPS_loop";
12710 case DW_TAG_HP_array_descriptor:
12711 return "DW_TAG_HP_array_descriptor";
12712 case DW_TAG_format_label:
12713 return "DW_TAG_format_label";
12714 case DW_TAG_function_template:
12715 return "DW_TAG_function_template";
12716 case DW_TAG_class_template:
12717 return "DW_TAG_class_template";
12718 case DW_TAG_GNU_BINCL:
12719 return "DW_TAG_GNU_BINCL";
12720 case DW_TAG_GNU_EINCL:
12721 return "DW_TAG_GNU_EINCL";
12722 case DW_TAG_upc_shared_type:
12723 return "DW_TAG_upc_shared_type";
12724 case DW_TAG_upc_strict_type:
12725 return "DW_TAG_upc_strict_type";
12726 case DW_TAG_upc_relaxed_type:
12727 return "DW_TAG_upc_relaxed_type";
12728 case DW_TAG_PGI_kanji_type:
12729 return "DW_TAG_PGI_kanji_type";
12730 case DW_TAG_PGI_interface_block:
12731 return "DW_TAG_PGI_interface_block";
12732 default:
12733 return "DW_TAG_<unknown>";
12734 }
12735 }
12736
12737 /* Convert a DWARF attribute code into its string name. */
12738
12739 static char *
12740 dwarf_attr_name (unsigned attr)
12741 {
12742 switch (attr)
12743 {
12744 case DW_AT_sibling:
12745 return "DW_AT_sibling";
12746 case DW_AT_location:
12747 return "DW_AT_location";
12748 case DW_AT_name:
12749 return "DW_AT_name";
12750 case DW_AT_ordering:
12751 return "DW_AT_ordering";
12752 case DW_AT_subscr_data:
12753 return "DW_AT_subscr_data";
12754 case DW_AT_byte_size:
12755 return "DW_AT_byte_size";
12756 case DW_AT_bit_offset:
12757 return "DW_AT_bit_offset";
12758 case DW_AT_bit_size:
12759 return "DW_AT_bit_size";
12760 case DW_AT_element_list:
12761 return "DW_AT_element_list";
12762 case DW_AT_stmt_list:
12763 return "DW_AT_stmt_list";
12764 case DW_AT_low_pc:
12765 return "DW_AT_low_pc";
12766 case DW_AT_high_pc:
12767 return "DW_AT_high_pc";
12768 case DW_AT_language:
12769 return "DW_AT_language";
12770 case DW_AT_member:
12771 return "DW_AT_member";
12772 case DW_AT_discr:
12773 return "DW_AT_discr";
12774 case DW_AT_discr_value:
12775 return "DW_AT_discr_value";
12776 case DW_AT_visibility:
12777 return "DW_AT_visibility";
12778 case DW_AT_import:
12779 return "DW_AT_import";
12780 case DW_AT_string_length:
12781 return "DW_AT_string_length";
12782 case DW_AT_common_reference:
12783 return "DW_AT_common_reference";
12784 case DW_AT_comp_dir:
12785 return "DW_AT_comp_dir";
12786 case DW_AT_const_value:
12787 return "DW_AT_const_value";
12788 case DW_AT_containing_type:
12789 return "DW_AT_containing_type";
12790 case DW_AT_default_value:
12791 return "DW_AT_default_value";
12792 case DW_AT_inline:
12793 return "DW_AT_inline";
12794 case DW_AT_is_optional:
12795 return "DW_AT_is_optional";
12796 case DW_AT_lower_bound:
12797 return "DW_AT_lower_bound";
12798 case DW_AT_producer:
12799 return "DW_AT_producer";
12800 case DW_AT_prototyped:
12801 return "DW_AT_prototyped";
12802 case DW_AT_return_addr:
12803 return "DW_AT_return_addr";
12804 case DW_AT_start_scope:
12805 return "DW_AT_start_scope";
12806 case DW_AT_bit_stride:
12807 return "DW_AT_bit_stride";
12808 case DW_AT_upper_bound:
12809 return "DW_AT_upper_bound";
12810 case DW_AT_abstract_origin:
12811 return "DW_AT_abstract_origin";
12812 case DW_AT_accessibility:
12813 return "DW_AT_accessibility";
12814 case DW_AT_address_class:
12815 return "DW_AT_address_class";
12816 case DW_AT_artificial:
12817 return "DW_AT_artificial";
12818 case DW_AT_base_types:
12819 return "DW_AT_base_types";
12820 case DW_AT_calling_convention:
12821 return "DW_AT_calling_convention";
12822 case DW_AT_count:
12823 return "DW_AT_count";
12824 case DW_AT_data_member_location:
12825 return "DW_AT_data_member_location";
12826 case DW_AT_decl_column:
12827 return "DW_AT_decl_column";
12828 case DW_AT_decl_file:
12829 return "DW_AT_decl_file";
12830 case DW_AT_decl_line:
12831 return "DW_AT_decl_line";
12832 case DW_AT_declaration:
12833 return "DW_AT_declaration";
12834 case DW_AT_discr_list:
12835 return "DW_AT_discr_list";
12836 case DW_AT_encoding:
12837 return "DW_AT_encoding";
12838 case DW_AT_external:
12839 return "DW_AT_external";
12840 case DW_AT_frame_base:
12841 return "DW_AT_frame_base";
12842 case DW_AT_friend:
12843 return "DW_AT_friend";
12844 case DW_AT_identifier_case:
12845 return "DW_AT_identifier_case";
12846 case DW_AT_macro_info:
12847 return "DW_AT_macro_info";
12848 case DW_AT_namelist_items:
12849 return "DW_AT_namelist_items";
12850 case DW_AT_priority:
12851 return "DW_AT_priority";
12852 case DW_AT_segment:
12853 return "DW_AT_segment";
12854 case DW_AT_specification:
12855 return "DW_AT_specification";
12856 case DW_AT_static_link:
12857 return "DW_AT_static_link";
12858 case DW_AT_type:
12859 return "DW_AT_type";
12860 case DW_AT_use_location:
12861 return "DW_AT_use_location";
12862 case DW_AT_variable_parameter:
12863 return "DW_AT_variable_parameter";
12864 case DW_AT_virtuality:
12865 return "DW_AT_virtuality";
12866 case DW_AT_vtable_elem_location:
12867 return "DW_AT_vtable_elem_location";
12868 /* DWARF 3 values. */
12869 case DW_AT_allocated:
12870 return "DW_AT_allocated";
12871 case DW_AT_associated:
12872 return "DW_AT_associated";
12873 case DW_AT_data_location:
12874 return "DW_AT_data_location";
12875 case DW_AT_byte_stride:
12876 return "DW_AT_byte_stride";
12877 case DW_AT_entry_pc:
12878 return "DW_AT_entry_pc";
12879 case DW_AT_use_UTF8:
12880 return "DW_AT_use_UTF8";
12881 case DW_AT_extension:
12882 return "DW_AT_extension";
12883 case DW_AT_ranges:
12884 return "DW_AT_ranges";
12885 case DW_AT_trampoline:
12886 return "DW_AT_trampoline";
12887 case DW_AT_call_column:
12888 return "DW_AT_call_column";
12889 case DW_AT_call_file:
12890 return "DW_AT_call_file";
12891 case DW_AT_call_line:
12892 return "DW_AT_call_line";
12893 case DW_AT_description:
12894 return "DW_AT_description";
12895 case DW_AT_binary_scale:
12896 return "DW_AT_binary_scale";
12897 case DW_AT_decimal_scale:
12898 return "DW_AT_decimal_scale";
12899 case DW_AT_small:
12900 return "DW_AT_small";
12901 case DW_AT_decimal_sign:
12902 return "DW_AT_decimal_sign";
12903 case DW_AT_digit_count:
12904 return "DW_AT_digit_count";
12905 case DW_AT_picture_string:
12906 return "DW_AT_picture_string";
12907 case DW_AT_mutable:
12908 return "DW_AT_mutable";
12909 case DW_AT_threads_scaled:
12910 return "DW_AT_threads_scaled";
12911 case DW_AT_explicit:
12912 return "DW_AT_explicit";
12913 case DW_AT_object_pointer:
12914 return "DW_AT_object_pointer";
12915 case DW_AT_endianity:
12916 return "DW_AT_endianity";
12917 case DW_AT_elemental:
12918 return "DW_AT_elemental";
12919 case DW_AT_pure:
12920 return "DW_AT_pure";
12921 case DW_AT_recursive:
12922 return "DW_AT_recursive";
12923 /* DWARF 4 values. */
12924 case DW_AT_signature:
12925 return "DW_AT_signature";
12926 case DW_AT_linkage_name:
12927 return "DW_AT_linkage_name";
12928 /* SGI/MIPS extensions. */
12929 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12930 case DW_AT_MIPS_fde:
12931 return "DW_AT_MIPS_fde";
12932 #endif
12933 case DW_AT_MIPS_loop_begin:
12934 return "DW_AT_MIPS_loop_begin";
12935 case DW_AT_MIPS_tail_loop_begin:
12936 return "DW_AT_MIPS_tail_loop_begin";
12937 case DW_AT_MIPS_epilog_begin:
12938 return "DW_AT_MIPS_epilog_begin";
12939 case DW_AT_MIPS_loop_unroll_factor:
12940 return "DW_AT_MIPS_loop_unroll_factor";
12941 case DW_AT_MIPS_software_pipeline_depth:
12942 return "DW_AT_MIPS_software_pipeline_depth";
12943 case DW_AT_MIPS_linkage_name:
12944 return "DW_AT_MIPS_linkage_name";
12945 case DW_AT_MIPS_stride:
12946 return "DW_AT_MIPS_stride";
12947 case DW_AT_MIPS_abstract_name:
12948 return "DW_AT_MIPS_abstract_name";
12949 case DW_AT_MIPS_clone_origin:
12950 return "DW_AT_MIPS_clone_origin";
12951 case DW_AT_MIPS_has_inlines:
12952 return "DW_AT_MIPS_has_inlines";
12953 /* HP extensions. */
12954 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12955 case DW_AT_HP_block_index:
12956 return "DW_AT_HP_block_index";
12957 #endif
12958 case DW_AT_HP_unmodifiable:
12959 return "DW_AT_HP_unmodifiable";
12960 case DW_AT_HP_actuals_stmt_list:
12961 return "DW_AT_HP_actuals_stmt_list";
12962 case DW_AT_HP_proc_per_section:
12963 return "DW_AT_HP_proc_per_section";
12964 case DW_AT_HP_raw_data_ptr:
12965 return "DW_AT_HP_raw_data_ptr";
12966 case DW_AT_HP_pass_by_reference:
12967 return "DW_AT_HP_pass_by_reference";
12968 case DW_AT_HP_opt_level:
12969 return "DW_AT_HP_opt_level";
12970 case DW_AT_HP_prof_version_id:
12971 return "DW_AT_HP_prof_version_id";
12972 case DW_AT_HP_opt_flags:
12973 return "DW_AT_HP_opt_flags";
12974 case DW_AT_HP_cold_region_low_pc:
12975 return "DW_AT_HP_cold_region_low_pc";
12976 case DW_AT_HP_cold_region_high_pc:
12977 return "DW_AT_HP_cold_region_high_pc";
12978 case DW_AT_HP_all_variables_modifiable:
12979 return "DW_AT_HP_all_variables_modifiable";
12980 case DW_AT_HP_linkage_name:
12981 return "DW_AT_HP_linkage_name";
12982 case DW_AT_HP_prof_flags:
12983 return "DW_AT_HP_prof_flags";
12984 /* GNU extensions. */
12985 case DW_AT_sf_names:
12986 return "DW_AT_sf_names";
12987 case DW_AT_src_info:
12988 return "DW_AT_src_info";
12989 case DW_AT_mac_info:
12990 return "DW_AT_mac_info";
12991 case DW_AT_src_coords:
12992 return "DW_AT_src_coords";
12993 case DW_AT_body_begin:
12994 return "DW_AT_body_begin";
12995 case DW_AT_body_end:
12996 return "DW_AT_body_end";
12997 case DW_AT_GNU_vector:
12998 return "DW_AT_GNU_vector";
12999 case DW_AT_GNU_odr_signature:
13000 return "DW_AT_GNU_odr_signature";
13001 /* VMS extensions. */
13002 case DW_AT_VMS_rtnbeg_pd_address:
13003 return "DW_AT_VMS_rtnbeg_pd_address";
13004 /* UPC extension. */
13005 case DW_AT_upc_threads_scaled:
13006 return "DW_AT_upc_threads_scaled";
13007 /* PGI (STMicroelectronics) extensions. */
13008 case DW_AT_PGI_lbase:
13009 return "DW_AT_PGI_lbase";
13010 case DW_AT_PGI_soffset:
13011 return "DW_AT_PGI_soffset";
13012 case DW_AT_PGI_lstride:
13013 return "DW_AT_PGI_lstride";
13014 default:
13015 return "DW_AT_<unknown>";
13016 }
13017 }
13018
13019 /* Convert a DWARF value form code into its string name. */
13020
13021 static char *
13022 dwarf_form_name (unsigned form)
13023 {
13024 switch (form)
13025 {
13026 case DW_FORM_addr:
13027 return "DW_FORM_addr";
13028 case DW_FORM_block2:
13029 return "DW_FORM_block2";
13030 case DW_FORM_block4:
13031 return "DW_FORM_block4";
13032 case DW_FORM_data2:
13033 return "DW_FORM_data2";
13034 case DW_FORM_data4:
13035 return "DW_FORM_data4";
13036 case DW_FORM_data8:
13037 return "DW_FORM_data8";
13038 case DW_FORM_string:
13039 return "DW_FORM_string";
13040 case DW_FORM_block:
13041 return "DW_FORM_block";
13042 case DW_FORM_block1:
13043 return "DW_FORM_block1";
13044 case DW_FORM_data1:
13045 return "DW_FORM_data1";
13046 case DW_FORM_flag:
13047 return "DW_FORM_flag";
13048 case DW_FORM_sdata:
13049 return "DW_FORM_sdata";
13050 case DW_FORM_strp:
13051 return "DW_FORM_strp";
13052 case DW_FORM_udata:
13053 return "DW_FORM_udata";
13054 case DW_FORM_ref_addr:
13055 return "DW_FORM_ref_addr";
13056 case DW_FORM_ref1:
13057 return "DW_FORM_ref1";
13058 case DW_FORM_ref2:
13059 return "DW_FORM_ref2";
13060 case DW_FORM_ref4:
13061 return "DW_FORM_ref4";
13062 case DW_FORM_ref8:
13063 return "DW_FORM_ref8";
13064 case DW_FORM_ref_udata:
13065 return "DW_FORM_ref_udata";
13066 case DW_FORM_indirect:
13067 return "DW_FORM_indirect";
13068 case DW_FORM_sec_offset:
13069 return "DW_FORM_sec_offset";
13070 case DW_FORM_exprloc:
13071 return "DW_FORM_exprloc";
13072 case DW_FORM_flag_present:
13073 return "DW_FORM_flag_present";
13074 case DW_FORM_ref_sig8:
13075 return "DW_FORM_ref_sig8";
13076 default:
13077 return "DW_FORM_<unknown>";
13078 }
13079 }
13080
13081 /* Convert a DWARF stack opcode into its string name. */
13082
13083 const char *
13084 dwarf_stack_op_name (unsigned op)
13085 {
13086 switch (op)
13087 {
13088 case DW_OP_addr:
13089 return "DW_OP_addr";
13090 case DW_OP_deref:
13091 return "DW_OP_deref";
13092 case DW_OP_const1u:
13093 return "DW_OP_const1u";
13094 case DW_OP_const1s:
13095 return "DW_OP_const1s";
13096 case DW_OP_const2u:
13097 return "DW_OP_const2u";
13098 case DW_OP_const2s:
13099 return "DW_OP_const2s";
13100 case DW_OP_const4u:
13101 return "DW_OP_const4u";
13102 case DW_OP_const4s:
13103 return "DW_OP_const4s";
13104 case DW_OP_const8u:
13105 return "DW_OP_const8u";
13106 case DW_OP_const8s:
13107 return "DW_OP_const8s";
13108 case DW_OP_constu:
13109 return "DW_OP_constu";
13110 case DW_OP_consts:
13111 return "DW_OP_consts";
13112 case DW_OP_dup:
13113 return "DW_OP_dup";
13114 case DW_OP_drop:
13115 return "DW_OP_drop";
13116 case DW_OP_over:
13117 return "DW_OP_over";
13118 case DW_OP_pick:
13119 return "DW_OP_pick";
13120 case DW_OP_swap:
13121 return "DW_OP_swap";
13122 case DW_OP_rot:
13123 return "DW_OP_rot";
13124 case DW_OP_xderef:
13125 return "DW_OP_xderef";
13126 case DW_OP_abs:
13127 return "DW_OP_abs";
13128 case DW_OP_and:
13129 return "DW_OP_and";
13130 case DW_OP_div:
13131 return "DW_OP_div";
13132 case DW_OP_minus:
13133 return "DW_OP_minus";
13134 case DW_OP_mod:
13135 return "DW_OP_mod";
13136 case DW_OP_mul:
13137 return "DW_OP_mul";
13138 case DW_OP_neg:
13139 return "DW_OP_neg";
13140 case DW_OP_not:
13141 return "DW_OP_not";
13142 case DW_OP_or:
13143 return "DW_OP_or";
13144 case DW_OP_plus:
13145 return "DW_OP_plus";
13146 case DW_OP_plus_uconst:
13147 return "DW_OP_plus_uconst";
13148 case DW_OP_shl:
13149 return "DW_OP_shl";
13150 case DW_OP_shr:
13151 return "DW_OP_shr";
13152 case DW_OP_shra:
13153 return "DW_OP_shra";
13154 case DW_OP_xor:
13155 return "DW_OP_xor";
13156 case DW_OP_bra:
13157 return "DW_OP_bra";
13158 case DW_OP_eq:
13159 return "DW_OP_eq";
13160 case DW_OP_ge:
13161 return "DW_OP_ge";
13162 case DW_OP_gt:
13163 return "DW_OP_gt";
13164 case DW_OP_le:
13165 return "DW_OP_le";
13166 case DW_OP_lt:
13167 return "DW_OP_lt";
13168 case DW_OP_ne:
13169 return "DW_OP_ne";
13170 case DW_OP_skip:
13171 return "DW_OP_skip";
13172 case DW_OP_lit0:
13173 return "DW_OP_lit0";
13174 case DW_OP_lit1:
13175 return "DW_OP_lit1";
13176 case DW_OP_lit2:
13177 return "DW_OP_lit2";
13178 case DW_OP_lit3:
13179 return "DW_OP_lit3";
13180 case DW_OP_lit4:
13181 return "DW_OP_lit4";
13182 case DW_OP_lit5:
13183 return "DW_OP_lit5";
13184 case DW_OP_lit6:
13185 return "DW_OP_lit6";
13186 case DW_OP_lit7:
13187 return "DW_OP_lit7";
13188 case DW_OP_lit8:
13189 return "DW_OP_lit8";
13190 case DW_OP_lit9:
13191 return "DW_OP_lit9";
13192 case DW_OP_lit10:
13193 return "DW_OP_lit10";
13194 case DW_OP_lit11:
13195 return "DW_OP_lit11";
13196 case DW_OP_lit12:
13197 return "DW_OP_lit12";
13198 case DW_OP_lit13:
13199 return "DW_OP_lit13";
13200 case DW_OP_lit14:
13201 return "DW_OP_lit14";
13202 case DW_OP_lit15:
13203 return "DW_OP_lit15";
13204 case DW_OP_lit16:
13205 return "DW_OP_lit16";
13206 case DW_OP_lit17:
13207 return "DW_OP_lit17";
13208 case DW_OP_lit18:
13209 return "DW_OP_lit18";
13210 case DW_OP_lit19:
13211 return "DW_OP_lit19";
13212 case DW_OP_lit20:
13213 return "DW_OP_lit20";
13214 case DW_OP_lit21:
13215 return "DW_OP_lit21";
13216 case DW_OP_lit22:
13217 return "DW_OP_lit22";
13218 case DW_OP_lit23:
13219 return "DW_OP_lit23";
13220 case DW_OP_lit24:
13221 return "DW_OP_lit24";
13222 case DW_OP_lit25:
13223 return "DW_OP_lit25";
13224 case DW_OP_lit26:
13225 return "DW_OP_lit26";
13226 case DW_OP_lit27:
13227 return "DW_OP_lit27";
13228 case DW_OP_lit28:
13229 return "DW_OP_lit28";
13230 case DW_OP_lit29:
13231 return "DW_OP_lit29";
13232 case DW_OP_lit30:
13233 return "DW_OP_lit30";
13234 case DW_OP_lit31:
13235 return "DW_OP_lit31";
13236 case DW_OP_reg0:
13237 return "DW_OP_reg0";
13238 case DW_OP_reg1:
13239 return "DW_OP_reg1";
13240 case DW_OP_reg2:
13241 return "DW_OP_reg2";
13242 case DW_OP_reg3:
13243 return "DW_OP_reg3";
13244 case DW_OP_reg4:
13245 return "DW_OP_reg4";
13246 case DW_OP_reg5:
13247 return "DW_OP_reg5";
13248 case DW_OP_reg6:
13249 return "DW_OP_reg6";
13250 case DW_OP_reg7:
13251 return "DW_OP_reg7";
13252 case DW_OP_reg8:
13253 return "DW_OP_reg8";
13254 case DW_OP_reg9:
13255 return "DW_OP_reg9";
13256 case DW_OP_reg10:
13257 return "DW_OP_reg10";
13258 case DW_OP_reg11:
13259 return "DW_OP_reg11";
13260 case DW_OP_reg12:
13261 return "DW_OP_reg12";
13262 case DW_OP_reg13:
13263 return "DW_OP_reg13";
13264 case DW_OP_reg14:
13265 return "DW_OP_reg14";
13266 case DW_OP_reg15:
13267 return "DW_OP_reg15";
13268 case DW_OP_reg16:
13269 return "DW_OP_reg16";
13270 case DW_OP_reg17:
13271 return "DW_OP_reg17";
13272 case DW_OP_reg18:
13273 return "DW_OP_reg18";
13274 case DW_OP_reg19:
13275 return "DW_OP_reg19";
13276 case DW_OP_reg20:
13277 return "DW_OP_reg20";
13278 case DW_OP_reg21:
13279 return "DW_OP_reg21";
13280 case DW_OP_reg22:
13281 return "DW_OP_reg22";
13282 case DW_OP_reg23:
13283 return "DW_OP_reg23";
13284 case DW_OP_reg24:
13285 return "DW_OP_reg24";
13286 case DW_OP_reg25:
13287 return "DW_OP_reg25";
13288 case DW_OP_reg26:
13289 return "DW_OP_reg26";
13290 case DW_OP_reg27:
13291 return "DW_OP_reg27";
13292 case DW_OP_reg28:
13293 return "DW_OP_reg28";
13294 case DW_OP_reg29:
13295 return "DW_OP_reg29";
13296 case DW_OP_reg30:
13297 return "DW_OP_reg30";
13298 case DW_OP_reg31:
13299 return "DW_OP_reg31";
13300 case DW_OP_breg0:
13301 return "DW_OP_breg0";
13302 case DW_OP_breg1:
13303 return "DW_OP_breg1";
13304 case DW_OP_breg2:
13305 return "DW_OP_breg2";
13306 case DW_OP_breg3:
13307 return "DW_OP_breg3";
13308 case DW_OP_breg4:
13309 return "DW_OP_breg4";
13310 case DW_OP_breg5:
13311 return "DW_OP_breg5";
13312 case DW_OP_breg6:
13313 return "DW_OP_breg6";
13314 case DW_OP_breg7:
13315 return "DW_OP_breg7";
13316 case DW_OP_breg8:
13317 return "DW_OP_breg8";
13318 case DW_OP_breg9:
13319 return "DW_OP_breg9";
13320 case DW_OP_breg10:
13321 return "DW_OP_breg10";
13322 case DW_OP_breg11:
13323 return "DW_OP_breg11";
13324 case DW_OP_breg12:
13325 return "DW_OP_breg12";
13326 case DW_OP_breg13:
13327 return "DW_OP_breg13";
13328 case DW_OP_breg14:
13329 return "DW_OP_breg14";
13330 case DW_OP_breg15:
13331 return "DW_OP_breg15";
13332 case DW_OP_breg16:
13333 return "DW_OP_breg16";
13334 case DW_OP_breg17:
13335 return "DW_OP_breg17";
13336 case DW_OP_breg18:
13337 return "DW_OP_breg18";
13338 case DW_OP_breg19:
13339 return "DW_OP_breg19";
13340 case DW_OP_breg20:
13341 return "DW_OP_breg20";
13342 case DW_OP_breg21:
13343 return "DW_OP_breg21";
13344 case DW_OP_breg22:
13345 return "DW_OP_breg22";
13346 case DW_OP_breg23:
13347 return "DW_OP_breg23";
13348 case DW_OP_breg24:
13349 return "DW_OP_breg24";
13350 case DW_OP_breg25:
13351 return "DW_OP_breg25";
13352 case DW_OP_breg26:
13353 return "DW_OP_breg26";
13354 case DW_OP_breg27:
13355 return "DW_OP_breg27";
13356 case DW_OP_breg28:
13357 return "DW_OP_breg28";
13358 case DW_OP_breg29:
13359 return "DW_OP_breg29";
13360 case DW_OP_breg30:
13361 return "DW_OP_breg30";
13362 case DW_OP_breg31:
13363 return "DW_OP_breg31";
13364 case DW_OP_regx:
13365 return "DW_OP_regx";
13366 case DW_OP_fbreg:
13367 return "DW_OP_fbreg";
13368 case DW_OP_bregx:
13369 return "DW_OP_bregx";
13370 case DW_OP_piece:
13371 return "DW_OP_piece";
13372 case DW_OP_deref_size:
13373 return "DW_OP_deref_size";
13374 case DW_OP_xderef_size:
13375 return "DW_OP_xderef_size";
13376 case DW_OP_nop:
13377 return "DW_OP_nop";
13378 /* DWARF 3 extensions. */
13379 case DW_OP_push_object_address:
13380 return "DW_OP_push_object_address";
13381 case DW_OP_call2:
13382 return "DW_OP_call2";
13383 case DW_OP_call4:
13384 return "DW_OP_call4";
13385 case DW_OP_call_ref:
13386 return "DW_OP_call_ref";
13387 case DW_OP_form_tls_address:
13388 return "DW_OP_form_tls_address";
13389 case DW_OP_call_frame_cfa:
13390 return "DW_OP_call_frame_cfa";
13391 case DW_OP_bit_piece:
13392 return "DW_OP_bit_piece";
13393 /* DWARF 4 extensions. */
13394 case DW_OP_implicit_value:
13395 return "DW_OP_implicit_value";
13396 case DW_OP_stack_value:
13397 return "DW_OP_stack_value";
13398 /* GNU extensions. */
13399 case DW_OP_GNU_push_tls_address:
13400 return "DW_OP_GNU_push_tls_address";
13401 case DW_OP_GNU_uninit:
13402 return "DW_OP_GNU_uninit";
13403 case DW_OP_GNU_implicit_pointer:
13404 return "DW_OP_GNU_implicit_pointer";
13405 case DW_OP_GNU_entry_value:
13406 return "DW_OP_GNU_entry_value";
13407 case DW_OP_GNU_const_type:
13408 return "DW_OP_GNU_const_type";
13409 case DW_OP_GNU_regval_type:
13410 return "DW_OP_GNU_regval_type";
13411 case DW_OP_GNU_deref_type:
13412 return "DW_OP_GNU_deref_type";
13413 case DW_OP_GNU_convert:
13414 return "DW_OP_GNU_convert";
13415 case DW_OP_GNU_reinterpret:
13416 return "DW_OP_GNU_reinterpret";
13417 default:
13418 return NULL;
13419 }
13420 }
13421
13422 static char *
13423 dwarf_bool_name (unsigned mybool)
13424 {
13425 if (mybool)
13426 return "TRUE";
13427 else
13428 return "FALSE";
13429 }
13430
13431 /* Convert a DWARF type code into its string name. */
13432
13433 static char *
13434 dwarf_type_encoding_name (unsigned enc)
13435 {
13436 switch (enc)
13437 {
13438 case DW_ATE_void:
13439 return "DW_ATE_void";
13440 case DW_ATE_address:
13441 return "DW_ATE_address";
13442 case DW_ATE_boolean:
13443 return "DW_ATE_boolean";
13444 case DW_ATE_complex_float:
13445 return "DW_ATE_complex_float";
13446 case DW_ATE_float:
13447 return "DW_ATE_float";
13448 case DW_ATE_signed:
13449 return "DW_ATE_signed";
13450 case DW_ATE_signed_char:
13451 return "DW_ATE_signed_char";
13452 case DW_ATE_unsigned:
13453 return "DW_ATE_unsigned";
13454 case DW_ATE_unsigned_char:
13455 return "DW_ATE_unsigned_char";
13456 /* DWARF 3. */
13457 case DW_ATE_imaginary_float:
13458 return "DW_ATE_imaginary_float";
13459 case DW_ATE_packed_decimal:
13460 return "DW_ATE_packed_decimal";
13461 case DW_ATE_numeric_string:
13462 return "DW_ATE_numeric_string";
13463 case DW_ATE_edited:
13464 return "DW_ATE_edited";
13465 case DW_ATE_signed_fixed:
13466 return "DW_ATE_signed_fixed";
13467 case DW_ATE_unsigned_fixed:
13468 return "DW_ATE_unsigned_fixed";
13469 case DW_ATE_decimal_float:
13470 return "DW_ATE_decimal_float";
13471 /* DWARF 4. */
13472 case DW_ATE_UTF:
13473 return "DW_ATE_UTF";
13474 /* HP extensions. */
13475 case DW_ATE_HP_float80:
13476 return "DW_ATE_HP_float80";
13477 case DW_ATE_HP_complex_float80:
13478 return "DW_ATE_HP_complex_float80";
13479 case DW_ATE_HP_float128:
13480 return "DW_ATE_HP_float128";
13481 case DW_ATE_HP_complex_float128:
13482 return "DW_ATE_HP_complex_float128";
13483 case DW_ATE_HP_floathpintel:
13484 return "DW_ATE_HP_floathpintel";
13485 case DW_ATE_HP_imaginary_float80:
13486 return "DW_ATE_HP_imaginary_float80";
13487 case DW_ATE_HP_imaginary_float128:
13488 return "DW_ATE_HP_imaginary_float128";
13489 default:
13490 return "DW_ATE_<unknown>";
13491 }
13492 }
13493
13494 /* Convert a DWARF call frame info operation to its string name. */
13495
13496 #if 0
13497 static char *
13498 dwarf_cfi_name (unsigned cfi_opc)
13499 {
13500 switch (cfi_opc)
13501 {
13502 case DW_CFA_advance_loc:
13503 return "DW_CFA_advance_loc";
13504 case DW_CFA_offset:
13505 return "DW_CFA_offset";
13506 case DW_CFA_restore:
13507 return "DW_CFA_restore";
13508 case DW_CFA_nop:
13509 return "DW_CFA_nop";
13510 case DW_CFA_set_loc:
13511 return "DW_CFA_set_loc";
13512 case DW_CFA_advance_loc1:
13513 return "DW_CFA_advance_loc1";
13514 case DW_CFA_advance_loc2:
13515 return "DW_CFA_advance_loc2";
13516 case DW_CFA_advance_loc4:
13517 return "DW_CFA_advance_loc4";
13518 case DW_CFA_offset_extended:
13519 return "DW_CFA_offset_extended";
13520 case DW_CFA_restore_extended:
13521 return "DW_CFA_restore_extended";
13522 case DW_CFA_undefined:
13523 return "DW_CFA_undefined";
13524 case DW_CFA_same_value:
13525 return "DW_CFA_same_value";
13526 case DW_CFA_register:
13527 return "DW_CFA_register";
13528 case DW_CFA_remember_state:
13529 return "DW_CFA_remember_state";
13530 case DW_CFA_restore_state:
13531 return "DW_CFA_restore_state";
13532 case DW_CFA_def_cfa:
13533 return "DW_CFA_def_cfa";
13534 case DW_CFA_def_cfa_register:
13535 return "DW_CFA_def_cfa_register";
13536 case DW_CFA_def_cfa_offset:
13537 return "DW_CFA_def_cfa_offset";
13538 /* DWARF 3. */
13539 case DW_CFA_def_cfa_expression:
13540 return "DW_CFA_def_cfa_expression";
13541 case DW_CFA_expression:
13542 return "DW_CFA_expression";
13543 case DW_CFA_offset_extended_sf:
13544 return "DW_CFA_offset_extended_sf";
13545 case DW_CFA_def_cfa_sf:
13546 return "DW_CFA_def_cfa_sf";
13547 case DW_CFA_def_cfa_offset_sf:
13548 return "DW_CFA_def_cfa_offset_sf";
13549 case DW_CFA_val_offset:
13550 return "DW_CFA_val_offset";
13551 case DW_CFA_val_offset_sf:
13552 return "DW_CFA_val_offset_sf";
13553 case DW_CFA_val_expression:
13554 return "DW_CFA_val_expression";
13555 /* SGI/MIPS specific. */
13556 case DW_CFA_MIPS_advance_loc8:
13557 return "DW_CFA_MIPS_advance_loc8";
13558 /* GNU extensions. */
13559 case DW_CFA_GNU_window_save:
13560 return "DW_CFA_GNU_window_save";
13561 case DW_CFA_GNU_args_size:
13562 return "DW_CFA_GNU_args_size";
13563 case DW_CFA_GNU_negative_offset_extended:
13564 return "DW_CFA_GNU_negative_offset_extended";
13565 default:
13566 return "DW_CFA_<unknown>";
13567 }
13568 }
13569 #endif
13570
13571 static void
13572 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13573 {
13574 unsigned int i;
13575
13576 print_spaces (indent, f);
13577 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13578 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13579
13580 if (die->parent != NULL)
13581 {
13582 print_spaces (indent, f);
13583 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13584 die->parent->offset);
13585 }
13586
13587 print_spaces (indent, f);
13588 fprintf_unfiltered (f, " has children: %s\n",
13589 dwarf_bool_name (die->child != NULL));
13590
13591 print_spaces (indent, f);
13592 fprintf_unfiltered (f, " attributes:\n");
13593
13594 for (i = 0; i < die->num_attrs; ++i)
13595 {
13596 print_spaces (indent, f);
13597 fprintf_unfiltered (f, " %s (%s) ",
13598 dwarf_attr_name (die->attrs[i].name),
13599 dwarf_form_name (die->attrs[i].form));
13600
13601 switch (die->attrs[i].form)
13602 {
13603 case DW_FORM_ref_addr:
13604 case DW_FORM_addr:
13605 fprintf_unfiltered (f, "address: ");
13606 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13607 break;
13608 case DW_FORM_block2:
13609 case DW_FORM_block4:
13610 case DW_FORM_block:
13611 case DW_FORM_block1:
13612 fprintf_unfiltered (f, "block: size %d",
13613 DW_BLOCK (&die->attrs[i])->size);
13614 break;
13615 case DW_FORM_exprloc:
13616 fprintf_unfiltered (f, "expression: size %u",
13617 DW_BLOCK (&die->attrs[i])->size);
13618 break;
13619 case DW_FORM_ref1:
13620 case DW_FORM_ref2:
13621 case DW_FORM_ref4:
13622 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13623 (long) (DW_ADDR (&die->attrs[i])));
13624 break;
13625 case DW_FORM_data1:
13626 case DW_FORM_data2:
13627 case DW_FORM_data4:
13628 case DW_FORM_data8:
13629 case DW_FORM_udata:
13630 case DW_FORM_sdata:
13631 fprintf_unfiltered (f, "constant: %s",
13632 pulongest (DW_UNSND (&die->attrs[i])));
13633 break;
13634 case DW_FORM_sec_offset:
13635 fprintf_unfiltered (f, "section offset: %s",
13636 pulongest (DW_UNSND (&die->attrs[i])));
13637 break;
13638 case DW_FORM_ref_sig8:
13639 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13640 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13641 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13642 else
13643 fprintf_unfiltered (f, "signatured type, offset: unknown");
13644 break;
13645 case DW_FORM_string:
13646 case DW_FORM_strp:
13647 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13648 DW_STRING (&die->attrs[i])
13649 ? DW_STRING (&die->attrs[i]) : "",
13650 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13651 break;
13652 case DW_FORM_flag:
13653 if (DW_UNSND (&die->attrs[i]))
13654 fprintf_unfiltered (f, "flag: TRUE");
13655 else
13656 fprintf_unfiltered (f, "flag: FALSE");
13657 break;
13658 case DW_FORM_flag_present:
13659 fprintf_unfiltered (f, "flag: TRUE");
13660 break;
13661 case DW_FORM_indirect:
13662 /* The reader will have reduced the indirect form to
13663 the "base form" so this form should not occur. */
13664 fprintf_unfiltered (f,
13665 "unexpected attribute form: DW_FORM_indirect");
13666 break;
13667 default:
13668 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13669 die->attrs[i].form);
13670 break;
13671 }
13672 fprintf_unfiltered (f, "\n");
13673 }
13674 }
13675
13676 static void
13677 dump_die_for_error (struct die_info *die)
13678 {
13679 dump_die_shallow (gdb_stderr, 0, die);
13680 }
13681
13682 static void
13683 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13684 {
13685 int indent = level * 4;
13686
13687 gdb_assert (die != NULL);
13688
13689 if (level >= max_level)
13690 return;
13691
13692 dump_die_shallow (f, indent, die);
13693
13694 if (die->child != NULL)
13695 {
13696 print_spaces (indent, f);
13697 fprintf_unfiltered (f, " Children:");
13698 if (level + 1 < max_level)
13699 {
13700 fprintf_unfiltered (f, "\n");
13701 dump_die_1 (f, level + 1, max_level, die->child);
13702 }
13703 else
13704 {
13705 fprintf_unfiltered (f,
13706 " [not printed, max nesting level reached]\n");
13707 }
13708 }
13709
13710 if (die->sibling != NULL && level > 0)
13711 {
13712 dump_die_1 (f, level, max_level, die->sibling);
13713 }
13714 }
13715
13716 /* This is called from the pdie macro in gdbinit.in.
13717 It's not static so gcc will keep a copy callable from gdb. */
13718
13719 void
13720 dump_die (struct die_info *die, int max_level)
13721 {
13722 dump_die_1 (gdb_stdlog, 0, max_level, die);
13723 }
13724
13725 static void
13726 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13727 {
13728 void **slot;
13729
13730 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13731
13732 *slot = die;
13733 }
13734
13735 static int
13736 is_ref_attr (struct attribute *attr)
13737 {
13738 switch (attr->form)
13739 {
13740 case DW_FORM_ref_addr:
13741 case DW_FORM_ref1:
13742 case DW_FORM_ref2:
13743 case DW_FORM_ref4:
13744 case DW_FORM_ref8:
13745 case DW_FORM_ref_udata:
13746 return 1;
13747 default:
13748 return 0;
13749 }
13750 }
13751
13752 static unsigned int
13753 dwarf2_get_ref_die_offset (struct attribute *attr)
13754 {
13755 if (is_ref_attr (attr))
13756 return DW_ADDR (attr);
13757
13758 complaint (&symfile_complaints,
13759 _("unsupported die ref attribute form: '%s'"),
13760 dwarf_form_name (attr->form));
13761 return 0;
13762 }
13763
13764 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13765 * the value held by the attribute is not constant. */
13766
13767 static LONGEST
13768 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13769 {
13770 if (attr->form == DW_FORM_sdata)
13771 return DW_SND (attr);
13772 else if (attr->form == DW_FORM_udata
13773 || attr->form == DW_FORM_data1
13774 || attr->form == DW_FORM_data2
13775 || attr->form == DW_FORM_data4
13776 || attr->form == DW_FORM_data8)
13777 return DW_UNSND (attr);
13778 else
13779 {
13780 complaint (&symfile_complaints,
13781 _("Attribute value is not a constant (%s)"),
13782 dwarf_form_name (attr->form));
13783 return default_value;
13784 }
13785 }
13786
13787 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
13788 unit and add it to our queue.
13789 The result is non-zero if PER_CU was queued, otherwise the result is zero
13790 meaning either PER_CU is already queued or it is already loaded. */
13791
13792 static int
13793 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13794 struct dwarf2_per_cu_data *per_cu)
13795 {
13796 /* We may arrive here during partial symbol reading, if we need full
13797 DIEs to process an unusual case (e.g. template arguments). Do
13798 not queue PER_CU, just tell our caller to load its DIEs. */
13799 if (dwarf2_per_objfile->reading_partial_symbols)
13800 {
13801 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13802 return 1;
13803 return 0;
13804 }
13805
13806 /* Mark the dependence relation so that we don't flush PER_CU
13807 too early. */
13808 dwarf2_add_dependence (this_cu, per_cu);
13809
13810 /* If it's already on the queue, we have nothing to do. */
13811 if (per_cu->queued)
13812 return 0;
13813
13814 /* If the compilation unit is already loaded, just mark it as
13815 used. */
13816 if (per_cu->cu != NULL)
13817 {
13818 per_cu->cu->last_used = 0;
13819 return 0;
13820 }
13821
13822 /* Add it to the queue. */
13823 queue_comp_unit (per_cu, this_cu->objfile);
13824
13825 return 1;
13826 }
13827
13828 /* Follow reference or signature attribute ATTR of SRC_DIE.
13829 On entry *REF_CU is the CU of SRC_DIE.
13830 On exit *REF_CU is the CU of the result. */
13831
13832 static struct die_info *
13833 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13834 struct dwarf2_cu **ref_cu)
13835 {
13836 struct die_info *die;
13837
13838 if (is_ref_attr (attr))
13839 die = follow_die_ref (src_die, attr, ref_cu);
13840 else if (attr->form == DW_FORM_ref_sig8)
13841 die = follow_die_sig (src_die, attr, ref_cu);
13842 else
13843 {
13844 dump_die_for_error (src_die);
13845 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13846 (*ref_cu)->objfile->name);
13847 }
13848
13849 return die;
13850 }
13851
13852 /* Follow reference OFFSET.
13853 On entry *REF_CU is the CU of the source die referencing OFFSET.
13854 On exit *REF_CU is the CU of the result.
13855 Returns NULL if OFFSET is invalid. */
13856
13857 static struct die_info *
13858 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13859 {
13860 struct die_info temp_die;
13861 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13862
13863 gdb_assert (cu->per_cu != NULL);
13864
13865 target_cu = cu;
13866
13867 if (cu->per_cu->debug_type_section)
13868 {
13869 /* .debug_types CUs cannot reference anything outside their CU.
13870 If they need to, they have to reference a signatured type via
13871 DW_FORM_ref_sig8. */
13872 if (! offset_in_cu_p (&cu->header, offset))
13873 return NULL;
13874 }
13875 else if (! offset_in_cu_p (&cu->header, offset))
13876 {
13877 struct dwarf2_per_cu_data *per_cu;
13878
13879 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13880
13881 /* If necessary, add it to the queue and load its DIEs. */
13882 if (maybe_queue_comp_unit (cu, per_cu))
13883 load_full_comp_unit (per_cu, cu->objfile);
13884
13885 target_cu = per_cu->cu;
13886 }
13887 else if (cu->dies == NULL)
13888 {
13889 /* We're loading full DIEs during partial symbol reading. */
13890 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13891 load_full_comp_unit (cu->per_cu, cu->objfile);
13892 }
13893
13894 *ref_cu = target_cu;
13895 temp_die.offset = offset;
13896 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13897 }
13898
13899 /* Follow reference attribute ATTR of SRC_DIE.
13900 On entry *REF_CU is the CU of SRC_DIE.
13901 On exit *REF_CU is the CU of the result. */
13902
13903 static struct die_info *
13904 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13905 struct dwarf2_cu **ref_cu)
13906 {
13907 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13908 struct dwarf2_cu *cu = *ref_cu;
13909 struct die_info *die;
13910
13911 die = follow_die_offset (offset, ref_cu);
13912 if (!die)
13913 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13914 "at 0x%x [in module %s]"),
13915 offset, src_die->offset, cu->objfile->name);
13916
13917 return die;
13918 }
13919
13920 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13921 value is intended for DW_OP_call*. You must call xfree on returned
13922 dwarf2_locexpr_baton->data. */
13923
13924 struct dwarf2_locexpr_baton
13925 dwarf2_fetch_die_location_block (unsigned int offset,
13926 struct dwarf2_per_cu_data *per_cu,
13927 CORE_ADDR (*get_frame_pc) (void *baton),
13928 void *baton)
13929 {
13930 struct dwarf2_cu *cu;
13931 struct die_info *die;
13932 struct attribute *attr;
13933 struct dwarf2_locexpr_baton retval;
13934
13935 dw2_setup (per_cu->objfile);
13936
13937 if (per_cu->cu == NULL)
13938 load_cu (per_cu);
13939 cu = per_cu->cu;
13940
13941 die = follow_die_offset (offset, &cu);
13942 if (!die)
13943 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13944 offset, per_cu->cu->objfile->name);
13945
13946 attr = dwarf2_attr (die, DW_AT_location, cu);
13947 if (!attr)
13948 {
13949 /* DWARF: "If there is no such attribute, then there is no effect.". */
13950
13951 retval.data = NULL;
13952 retval.size = 0;
13953 }
13954 else if (attr_form_is_section_offset (attr))
13955 {
13956 struct dwarf2_loclist_baton loclist_baton;
13957 CORE_ADDR pc = (*get_frame_pc) (baton);
13958 size_t size;
13959
13960 fill_in_loclist_baton (cu, &loclist_baton, attr);
13961
13962 retval.data = dwarf2_find_location_expression (&loclist_baton,
13963 &size, pc);
13964 retval.size = size;
13965 }
13966 else
13967 {
13968 if (!attr_form_is_block (attr))
13969 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13970 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13971 offset, per_cu->cu->objfile->name);
13972
13973 retval.data = DW_BLOCK (attr)->data;
13974 retval.size = DW_BLOCK (attr)->size;
13975 }
13976 retval.per_cu = cu->per_cu;
13977
13978 if (retval.data)
13979 retval.data = xmemdup (retval.data, retval.size, retval.size);
13980
13981 age_cached_comp_units ();
13982
13983 return retval;
13984 }
13985
13986 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13987 PER_CU. */
13988
13989 struct type *
13990 dwarf2_get_die_type (unsigned int die_offset,
13991 struct dwarf2_per_cu_data *per_cu)
13992 {
13993 dw2_setup (per_cu->objfile);
13994 return get_die_type_at_offset (die_offset, per_cu);
13995 }
13996
13997 /* Follow the signature attribute ATTR in SRC_DIE.
13998 On entry *REF_CU is the CU of SRC_DIE.
13999 On exit *REF_CU is the CU of the result. */
14000
14001 static struct die_info *
14002 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14003 struct dwarf2_cu **ref_cu)
14004 {
14005 struct objfile *objfile = (*ref_cu)->objfile;
14006 struct die_info temp_die;
14007 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14008 struct dwarf2_cu *sig_cu;
14009 struct die_info *die;
14010
14011 /* sig_type will be NULL if the signatured type is missing from
14012 the debug info. */
14013 if (sig_type == NULL)
14014 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14015 "at 0x%x [in module %s]"),
14016 src_die->offset, objfile->name);
14017
14018 /* If necessary, add it to the queue and load its DIEs. */
14019
14020 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14021 read_signatured_type (objfile, sig_type);
14022
14023 gdb_assert (sig_type->per_cu.cu != NULL);
14024
14025 sig_cu = sig_type->per_cu.cu;
14026 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14027 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14028 if (die)
14029 {
14030 *ref_cu = sig_cu;
14031 return die;
14032 }
14033
14034 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14035 "from DIE at 0x%x [in module %s]"),
14036 sig_type->type_offset, src_die->offset, objfile->name);
14037 }
14038
14039 /* Given an offset of a signatured type, return its signatured_type. */
14040
14041 static struct signatured_type *
14042 lookup_signatured_type_at_offset (struct objfile *objfile,
14043 struct dwarf2_section_info *section,
14044 unsigned int offset)
14045 {
14046 gdb_byte *info_ptr = section->buffer + offset;
14047 unsigned int length, initial_length_size;
14048 unsigned int sig_offset;
14049 struct signatured_type find_entry, *type_sig;
14050
14051 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14052 sig_offset = (initial_length_size
14053 + 2 /*version*/
14054 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14055 + 1 /*address_size*/);
14056 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14057 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14058
14059 /* This is only used to lookup previously recorded types.
14060 If we didn't find it, it's our bug. */
14061 gdb_assert (type_sig != NULL);
14062 gdb_assert (offset == type_sig->per_cu.offset);
14063
14064 return type_sig;
14065 }
14066
14067 /* Read in signatured type at OFFSET and build its CU and die(s). */
14068
14069 static void
14070 read_signatured_type_at_offset (struct objfile *objfile,
14071 struct dwarf2_section_info *sect,
14072 unsigned int offset)
14073 {
14074 struct signatured_type *type_sig;
14075
14076 dwarf2_read_section (objfile, sect);
14077
14078 /* We have the section offset, but we need the signature to do the
14079 hash table lookup. */
14080 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14081
14082 gdb_assert (type_sig->per_cu.cu == NULL);
14083
14084 read_signatured_type (objfile, type_sig);
14085
14086 gdb_assert (type_sig->per_cu.cu != NULL);
14087 }
14088
14089 /* Read in a signatured type and build its CU and DIEs. */
14090
14091 static void
14092 read_signatured_type (struct objfile *objfile,
14093 struct signatured_type *type_sig)
14094 {
14095 gdb_byte *types_ptr;
14096 struct die_reader_specs reader_specs;
14097 struct dwarf2_cu *cu;
14098 ULONGEST signature;
14099 struct cleanup *back_to, *free_cu_cleanup;
14100 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
14101
14102 dwarf2_read_section (objfile, section);
14103 types_ptr = section->buffer + type_sig->per_cu.offset;
14104
14105 gdb_assert (type_sig->per_cu.cu == NULL);
14106
14107 cu = xmalloc (sizeof (*cu));
14108 init_one_comp_unit (cu, objfile);
14109
14110 type_sig->per_cu.cu = cu;
14111 cu->per_cu = &type_sig->per_cu;
14112
14113 /* If an error occurs while loading, release our storage. */
14114 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14115
14116 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14117 types_ptr, objfile->obfd);
14118 gdb_assert (signature == type_sig->signature);
14119
14120 cu->die_hash
14121 = htab_create_alloc_ex (cu->header.length / 12,
14122 die_hash,
14123 die_eq,
14124 NULL,
14125 &cu->comp_unit_obstack,
14126 hashtab_obstack_allocate,
14127 dummy_obstack_deallocate);
14128
14129 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14130 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14131
14132 init_cu_die_reader (&reader_specs, cu);
14133
14134 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14135 NULL /*parent*/);
14136
14137 /* We try not to read any attributes in this function, because not
14138 all objfiles needed for references have been loaded yet, and symbol
14139 table processing isn't initialized. But we have to set the CU language,
14140 or we won't be able to build types correctly. */
14141 prepare_one_comp_unit (cu, cu->dies);
14142
14143 do_cleanups (back_to);
14144
14145 /* We've successfully allocated this compilation unit. Let our caller
14146 clean it up when finished with it. */
14147 discard_cleanups (free_cu_cleanup);
14148
14149 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14150 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14151 }
14152
14153 /* Decode simple location descriptions.
14154 Given a pointer to a dwarf block that defines a location, compute
14155 the location and return the value.
14156
14157 NOTE drow/2003-11-18: This function is called in two situations
14158 now: for the address of static or global variables (partial symbols
14159 only) and for offsets into structures which are expected to be
14160 (more or less) constant. The partial symbol case should go away,
14161 and only the constant case should remain. That will let this
14162 function complain more accurately. A few special modes are allowed
14163 without complaint for global variables (for instance, global
14164 register values and thread-local values).
14165
14166 A location description containing no operations indicates that the
14167 object is optimized out. The return value is 0 for that case.
14168 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14169 callers will only want a very basic result and this can become a
14170 complaint.
14171
14172 Note that stack[0] is unused except as a default error return. */
14173
14174 static CORE_ADDR
14175 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14176 {
14177 struct objfile *objfile = cu->objfile;
14178 int i;
14179 int size = blk->size;
14180 gdb_byte *data = blk->data;
14181 CORE_ADDR stack[64];
14182 int stacki;
14183 unsigned int bytes_read, unsnd;
14184 gdb_byte op;
14185
14186 i = 0;
14187 stacki = 0;
14188 stack[stacki] = 0;
14189 stack[++stacki] = 0;
14190
14191 while (i < size)
14192 {
14193 op = data[i++];
14194 switch (op)
14195 {
14196 case DW_OP_lit0:
14197 case DW_OP_lit1:
14198 case DW_OP_lit2:
14199 case DW_OP_lit3:
14200 case DW_OP_lit4:
14201 case DW_OP_lit5:
14202 case DW_OP_lit6:
14203 case DW_OP_lit7:
14204 case DW_OP_lit8:
14205 case DW_OP_lit9:
14206 case DW_OP_lit10:
14207 case DW_OP_lit11:
14208 case DW_OP_lit12:
14209 case DW_OP_lit13:
14210 case DW_OP_lit14:
14211 case DW_OP_lit15:
14212 case DW_OP_lit16:
14213 case DW_OP_lit17:
14214 case DW_OP_lit18:
14215 case DW_OP_lit19:
14216 case DW_OP_lit20:
14217 case DW_OP_lit21:
14218 case DW_OP_lit22:
14219 case DW_OP_lit23:
14220 case DW_OP_lit24:
14221 case DW_OP_lit25:
14222 case DW_OP_lit26:
14223 case DW_OP_lit27:
14224 case DW_OP_lit28:
14225 case DW_OP_lit29:
14226 case DW_OP_lit30:
14227 case DW_OP_lit31:
14228 stack[++stacki] = op - DW_OP_lit0;
14229 break;
14230
14231 case DW_OP_reg0:
14232 case DW_OP_reg1:
14233 case DW_OP_reg2:
14234 case DW_OP_reg3:
14235 case DW_OP_reg4:
14236 case DW_OP_reg5:
14237 case DW_OP_reg6:
14238 case DW_OP_reg7:
14239 case DW_OP_reg8:
14240 case DW_OP_reg9:
14241 case DW_OP_reg10:
14242 case DW_OP_reg11:
14243 case DW_OP_reg12:
14244 case DW_OP_reg13:
14245 case DW_OP_reg14:
14246 case DW_OP_reg15:
14247 case DW_OP_reg16:
14248 case DW_OP_reg17:
14249 case DW_OP_reg18:
14250 case DW_OP_reg19:
14251 case DW_OP_reg20:
14252 case DW_OP_reg21:
14253 case DW_OP_reg22:
14254 case DW_OP_reg23:
14255 case DW_OP_reg24:
14256 case DW_OP_reg25:
14257 case DW_OP_reg26:
14258 case DW_OP_reg27:
14259 case DW_OP_reg28:
14260 case DW_OP_reg29:
14261 case DW_OP_reg30:
14262 case DW_OP_reg31:
14263 stack[++stacki] = op - DW_OP_reg0;
14264 if (i < size)
14265 dwarf2_complex_location_expr_complaint ();
14266 break;
14267
14268 case DW_OP_regx:
14269 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14270 i += bytes_read;
14271 stack[++stacki] = unsnd;
14272 if (i < size)
14273 dwarf2_complex_location_expr_complaint ();
14274 break;
14275
14276 case DW_OP_addr:
14277 stack[++stacki] = read_address (objfile->obfd, &data[i],
14278 cu, &bytes_read);
14279 i += bytes_read;
14280 break;
14281
14282 case DW_OP_const1u:
14283 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14284 i += 1;
14285 break;
14286
14287 case DW_OP_const1s:
14288 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14289 i += 1;
14290 break;
14291
14292 case DW_OP_const2u:
14293 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14294 i += 2;
14295 break;
14296
14297 case DW_OP_const2s:
14298 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14299 i += 2;
14300 break;
14301
14302 case DW_OP_const4u:
14303 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14304 i += 4;
14305 break;
14306
14307 case DW_OP_const4s:
14308 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14309 i += 4;
14310 break;
14311
14312 case DW_OP_constu:
14313 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14314 &bytes_read);
14315 i += bytes_read;
14316 break;
14317
14318 case DW_OP_consts:
14319 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14320 i += bytes_read;
14321 break;
14322
14323 case DW_OP_dup:
14324 stack[stacki + 1] = stack[stacki];
14325 stacki++;
14326 break;
14327
14328 case DW_OP_plus:
14329 stack[stacki - 1] += stack[stacki];
14330 stacki--;
14331 break;
14332
14333 case DW_OP_plus_uconst:
14334 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14335 &bytes_read);
14336 i += bytes_read;
14337 break;
14338
14339 case DW_OP_minus:
14340 stack[stacki - 1] -= stack[stacki];
14341 stacki--;
14342 break;
14343
14344 case DW_OP_deref:
14345 /* If we're not the last op, then we definitely can't encode
14346 this using GDB's address_class enum. This is valid for partial
14347 global symbols, although the variable's address will be bogus
14348 in the psymtab. */
14349 if (i < size)
14350 dwarf2_complex_location_expr_complaint ();
14351 break;
14352
14353 case DW_OP_GNU_push_tls_address:
14354 /* The top of the stack has the offset from the beginning
14355 of the thread control block at which the variable is located. */
14356 /* Nothing should follow this operator, so the top of stack would
14357 be returned. */
14358 /* This is valid for partial global symbols, but the variable's
14359 address will be bogus in the psymtab. */
14360 if (i < size)
14361 dwarf2_complex_location_expr_complaint ();
14362 break;
14363
14364 case DW_OP_GNU_uninit:
14365 break;
14366
14367 default:
14368 {
14369 const char *name = dwarf_stack_op_name (op);
14370
14371 if (name)
14372 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14373 name);
14374 else
14375 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14376 op);
14377 }
14378
14379 return (stack[stacki]);
14380 }
14381
14382 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14383 outside of the allocated space. Also enforce minimum>0. */
14384 if (stacki >= ARRAY_SIZE (stack) - 1)
14385 {
14386 complaint (&symfile_complaints,
14387 _("location description stack overflow"));
14388 return 0;
14389 }
14390
14391 if (stacki <= 0)
14392 {
14393 complaint (&symfile_complaints,
14394 _("location description stack underflow"));
14395 return 0;
14396 }
14397 }
14398 return (stack[stacki]);
14399 }
14400
14401 /* memory allocation interface */
14402
14403 static struct dwarf_block *
14404 dwarf_alloc_block (struct dwarf2_cu *cu)
14405 {
14406 struct dwarf_block *blk;
14407
14408 blk = (struct dwarf_block *)
14409 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14410 return (blk);
14411 }
14412
14413 static struct abbrev_info *
14414 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14415 {
14416 struct abbrev_info *abbrev;
14417
14418 abbrev = (struct abbrev_info *)
14419 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14420 memset (abbrev, 0, sizeof (struct abbrev_info));
14421 return (abbrev);
14422 }
14423
14424 static struct die_info *
14425 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14426 {
14427 struct die_info *die;
14428 size_t size = sizeof (struct die_info);
14429
14430 if (num_attrs > 1)
14431 size += (num_attrs - 1) * sizeof (struct attribute);
14432
14433 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14434 memset (die, 0, sizeof (struct die_info));
14435 return (die);
14436 }
14437
14438 \f
14439 /* Macro support. */
14440
14441 /* Return the full name of file number I in *LH's file name table.
14442 Use COMP_DIR as the name of the current directory of the
14443 compilation. The result is allocated using xmalloc; the caller is
14444 responsible for freeing it. */
14445 static char *
14446 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14447 {
14448 /* Is the file number a valid index into the line header's file name
14449 table? Remember that file numbers start with one, not zero. */
14450 if (1 <= file && file <= lh->num_file_names)
14451 {
14452 struct file_entry *fe = &lh->file_names[file - 1];
14453
14454 if (IS_ABSOLUTE_PATH (fe->name))
14455 return xstrdup (fe->name);
14456 else
14457 {
14458 const char *dir;
14459 int dir_len;
14460 char *full_name;
14461
14462 if (fe->dir_index)
14463 dir = lh->include_dirs[fe->dir_index - 1];
14464 else
14465 dir = comp_dir;
14466
14467 if (dir)
14468 {
14469 dir_len = strlen (dir);
14470 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14471 strcpy (full_name, dir);
14472 full_name[dir_len] = '/';
14473 strcpy (full_name + dir_len + 1, fe->name);
14474 return full_name;
14475 }
14476 else
14477 return xstrdup (fe->name);
14478 }
14479 }
14480 else
14481 {
14482 /* The compiler produced a bogus file number. We can at least
14483 record the macro definitions made in the file, even if we
14484 won't be able to find the file by name. */
14485 char fake_name[80];
14486
14487 sprintf (fake_name, "<bad macro file number %d>", file);
14488
14489 complaint (&symfile_complaints,
14490 _("bad file number in macro information (%d)"),
14491 file);
14492
14493 return xstrdup (fake_name);
14494 }
14495 }
14496
14497
14498 static struct macro_source_file *
14499 macro_start_file (int file, int line,
14500 struct macro_source_file *current_file,
14501 const char *comp_dir,
14502 struct line_header *lh, struct objfile *objfile)
14503 {
14504 /* The full name of this source file. */
14505 char *full_name = file_full_name (file, lh, comp_dir);
14506
14507 /* We don't create a macro table for this compilation unit
14508 at all until we actually get a filename. */
14509 if (! pending_macros)
14510 pending_macros = new_macro_table (&objfile->objfile_obstack,
14511 objfile->macro_cache);
14512
14513 if (! current_file)
14514 /* If we have no current file, then this must be the start_file
14515 directive for the compilation unit's main source file. */
14516 current_file = macro_set_main (pending_macros, full_name);
14517 else
14518 current_file = macro_include (current_file, line, full_name);
14519
14520 xfree (full_name);
14521
14522 return current_file;
14523 }
14524
14525
14526 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14527 followed by a null byte. */
14528 static char *
14529 copy_string (const char *buf, int len)
14530 {
14531 char *s = xmalloc (len + 1);
14532
14533 memcpy (s, buf, len);
14534 s[len] = '\0';
14535 return s;
14536 }
14537
14538
14539 static const char *
14540 consume_improper_spaces (const char *p, const char *body)
14541 {
14542 if (*p == ' ')
14543 {
14544 complaint (&symfile_complaints,
14545 _("macro definition contains spaces "
14546 "in formal argument list:\n`%s'"),
14547 body);
14548
14549 while (*p == ' ')
14550 p++;
14551 }
14552
14553 return p;
14554 }
14555
14556
14557 static void
14558 parse_macro_definition (struct macro_source_file *file, int line,
14559 const char *body)
14560 {
14561 const char *p;
14562
14563 /* The body string takes one of two forms. For object-like macro
14564 definitions, it should be:
14565
14566 <macro name> " " <definition>
14567
14568 For function-like macro definitions, it should be:
14569
14570 <macro name> "() " <definition>
14571 or
14572 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14573
14574 Spaces may appear only where explicitly indicated, and in the
14575 <definition>.
14576
14577 The Dwarf 2 spec says that an object-like macro's name is always
14578 followed by a space, but versions of GCC around March 2002 omit
14579 the space when the macro's definition is the empty string.
14580
14581 The Dwarf 2 spec says that there should be no spaces between the
14582 formal arguments in a function-like macro's formal argument list,
14583 but versions of GCC around March 2002 include spaces after the
14584 commas. */
14585
14586
14587 /* Find the extent of the macro name. The macro name is terminated
14588 by either a space or null character (for an object-like macro) or
14589 an opening paren (for a function-like macro). */
14590 for (p = body; *p; p++)
14591 if (*p == ' ' || *p == '(')
14592 break;
14593
14594 if (*p == ' ' || *p == '\0')
14595 {
14596 /* It's an object-like macro. */
14597 int name_len = p - body;
14598 char *name = copy_string (body, name_len);
14599 const char *replacement;
14600
14601 if (*p == ' ')
14602 replacement = body + name_len + 1;
14603 else
14604 {
14605 dwarf2_macro_malformed_definition_complaint (body);
14606 replacement = body + name_len;
14607 }
14608
14609 macro_define_object (file, line, name, replacement);
14610
14611 xfree (name);
14612 }
14613 else if (*p == '(')
14614 {
14615 /* It's a function-like macro. */
14616 char *name = copy_string (body, p - body);
14617 int argc = 0;
14618 int argv_size = 1;
14619 char **argv = xmalloc (argv_size * sizeof (*argv));
14620
14621 p++;
14622
14623 p = consume_improper_spaces (p, body);
14624
14625 /* Parse the formal argument list. */
14626 while (*p && *p != ')')
14627 {
14628 /* Find the extent of the current argument name. */
14629 const char *arg_start = p;
14630
14631 while (*p && *p != ',' && *p != ')' && *p != ' ')
14632 p++;
14633
14634 if (! *p || p == arg_start)
14635 dwarf2_macro_malformed_definition_complaint (body);
14636 else
14637 {
14638 /* Make sure argv has room for the new argument. */
14639 if (argc >= argv_size)
14640 {
14641 argv_size *= 2;
14642 argv = xrealloc (argv, argv_size * sizeof (*argv));
14643 }
14644
14645 argv[argc++] = copy_string (arg_start, p - arg_start);
14646 }
14647
14648 p = consume_improper_spaces (p, body);
14649
14650 /* Consume the comma, if present. */
14651 if (*p == ',')
14652 {
14653 p++;
14654
14655 p = consume_improper_spaces (p, body);
14656 }
14657 }
14658
14659 if (*p == ')')
14660 {
14661 p++;
14662
14663 if (*p == ' ')
14664 /* Perfectly formed definition, no complaints. */
14665 macro_define_function (file, line, name,
14666 argc, (const char **) argv,
14667 p + 1);
14668 else if (*p == '\0')
14669 {
14670 /* Complain, but do define it. */
14671 dwarf2_macro_malformed_definition_complaint (body);
14672 macro_define_function (file, line, name,
14673 argc, (const char **) argv,
14674 p);
14675 }
14676 else
14677 /* Just complain. */
14678 dwarf2_macro_malformed_definition_complaint (body);
14679 }
14680 else
14681 /* Just complain. */
14682 dwarf2_macro_malformed_definition_complaint (body);
14683
14684 xfree (name);
14685 {
14686 int i;
14687
14688 for (i = 0; i < argc; i++)
14689 xfree (argv[i]);
14690 }
14691 xfree (argv);
14692 }
14693 else
14694 dwarf2_macro_malformed_definition_complaint (body);
14695 }
14696
14697 /* Skip some bytes from BYTES according to the form given in FORM.
14698 Returns the new pointer. */
14699
14700 static gdb_byte *
14701 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14702 enum dwarf_form form,
14703 unsigned int offset_size,
14704 struct dwarf2_section_info *section)
14705 {
14706 unsigned int bytes_read;
14707
14708 switch (form)
14709 {
14710 case DW_FORM_data1:
14711 case DW_FORM_flag:
14712 ++bytes;
14713 break;
14714
14715 case DW_FORM_data2:
14716 bytes += 2;
14717 break;
14718
14719 case DW_FORM_data4:
14720 bytes += 4;
14721 break;
14722
14723 case DW_FORM_data8:
14724 bytes += 8;
14725 break;
14726
14727 case DW_FORM_string:
14728 read_direct_string (abfd, bytes, &bytes_read);
14729 bytes += bytes_read;
14730 break;
14731
14732 case DW_FORM_sec_offset:
14733 case DW_FORM_strp:
14734 bytes += offset_size;
14735 break;
14736
14737 case DW_FORM_block:
14738 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14739 bytes += bytes_read;
14740 break;
14741
14742 case DW_FORM_block1:
14743 bytes += 1 + read_1_byte (abfd, bytes);
14744 break;
14745 case DW_FORM_block2:
14746 bytes += 2 + read_2_bytes (abfd, bytes);
14747 break;
14748 case DW_FORM_block4:
14749 bytes += 4 + read_4_bytes (abfd, bytes);
14750 break;
14751
14752 case DW_FORM_sdata:
14753 case DW_FORM_udata:
14754 bytes = skip_leb128 (abfd, bytes);
14755 break;
14756
14757 default:
14758 {
14759 complain:
14760 complaint (&symfile_complaints,
14761 _("invalid form 0x%x in `%s'"),
14762 form,
14763 section->asection->name);
14764 return NULL;
14765 }
14766 }
14767
14768 return bytes;
14769 }
14770
14771 /* A helper for dwarf_decode_macros that handles skipping an unknown
14772 opcode. Returns an updated pointer to the macro data buffer; or,
14773 on error, issues a complaint and returns NULL. */
14774
14775 static gdb_byte *
14776 skip_unknown_opcode (unsigned int opcode,
14777 gdb_byte **opcode_definitions,
14778 gdb_byte *mac_ptr,
14779 bfd *abfd,
14780 unsigned int offset_size,
14781 struct dwarf2_section_info *section)
14782 {
14783 unsigned int bytes_read, i;
14784 unsigned long arg;
14785 gdb_byte *defn;
14786
14787 if (opcode_definitions[opcode] == NULL)
14788 {
14789 complaint (&symfile_complaints,
14790 _("unrecognized DW_MACFINO opcode 0x%x"),
14791 opcode);
14792 return NULL;
14793 }
14794
14795 defn = opcode_definitions[opcode];
14796 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
14797 defn += bytes_read;
14798
14799 for (i = 0; i < arg; ++i)
14800 {
14801 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
14802 if (mac_ptr == NULL)
14803 {
14804 /* skip_form_bytes already issued the complaint. */
14805 return NULL;
14806 }
14807 }
14808
14809 return mac_ptr;
14810 }
14811
14812 /* A helper function which parses the header of a macro section.
14813 If the macro section is the extended (for now called "GNU") type,
14814 then this updates *OFFSET_SIZE. Returns a pointer to just after
14815 the header, or issues a complaint and returns NULL on error. */
14816
14817 static gdb_byte *
14818 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
14819 bfd *abfd,
14820 gdb_byte *mac_ptr,
14821 unsigned int *offset_size,
14822 int section_is_gnu)
14823 {
14824 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
14825
14826 if (section_is_gnu)
14827 {
14828 unsigned int version, flags;
14829
14830 version = read_2_bytes (abfd, mac_ptr);
14831 if (version != 4)
14832 {
14833 complaint (&symfile_complaints,
14834 _("unrecognized version `%d' in .debug_macro section"),
14835 version);
14836 return NULL;
14837 }
14838 mac_ptr += 2;
14839
14840 flags = read_1_byte (abfd, mac_ptr);
14841 ++mac_ptr;
14842 *offset_size = (flags & 1) ? 8 : 4;
14843
14844 if ((flags & 2) != 0)
14845 /* We don't need the line table offset. */
14846 mac_ptr += *offset_size;
14847
14848 /* Vendor opcode descriptions. */
14849 if ((flags & 4) != 0)
14850 {
14851 unsigned int i, count;
14852
14853 count = read_1_byte (abfd, mac_ptr);
14854 ++mac_ptr;
14855 for (i = 0; i < count; ++i)
14856 {
14857 unsigned int opcode, bytes_read;
14858 unsigned long arg;
14859
14860 opcode = read_1_byte (abfd, mac_ptr);
14861 ++mac_ptr;
14862 opcode_definitions[opcode] = mac_ptr;
14863 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14864 mac_ptr += bytes_read;
14865 mac_ptr += arg;
14866 }
14867 }
14868 }
14869
14870 return mac_ptr;
14871 }
14872
14873 /* A helper for dwarf_decode_macros that handles the GNU extensions,
14874 including DW_GNU_MACINFO_transparent_include. */
14875
14876 static void
14877 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
14878 struct macro_source_file *current_file,
14879 struct line_header *lh, char *comp_dir,
14880 struct dwarf2_section_info *section,
14881 int section_is_gnu,
14882 unsigned int offset_size,
14883 struct objfile *objfile)
14884 {
14885 enum dwarf_macro_record_type macinfo_type;
14886 int at_commandline;
14887 gdb_byte *opcode_definitions[256];
14888
14889 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
14890 &offset_size, section_is_gnu);
14891 if (mac_ptr == NULL)
14892 {
14893 /* We already issued a complaint. */
14894 return;
14895 }
14896
14897 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14898 GDB is still reading the definitions from command line. First
14899 DW_MACINFO_start_file will need to be ignored as it was already executed
14900 to create CURRENT_FILE for the main source holding also the command line
14901 definitions. On first met DW_MACINFO_start_file this flag is reset to
14902 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14903
14904 at_commandline = 1;
14905
14906 do
14907 {
14908 /* Do we at least have room for a macinfo type byte? */
14909 if (mac_ptr >= mac_end)
14910 {
14911 dwarf2_macros_too_long_complaint (section);
14912 break;
14913 }
14914
14915 macinfo_type = read_1_byte (abfd, mac_ptr);
14916 mac_ptr++;
14917
14918 /* Note that we rely on the fact that the corresponding GNU and
14919 DWARF constants are the same. */
14920 switch (macinfo_type)
14921 {
14922 /* A zero macinfo type indicates the end of the macro
14923 information. */
14924 case 0:
14925 break;
14926
14927 case DW_MACRO_GNU_define:
14928 case DW_MACRO_GNU_undef:
14929 case DW_MACRO_GNU_define_indirect:
14930 case DW_MACRO_GNU_undef_indirect:
14931 {
14932 unsigned int bytes_read;
14933 int line;
14934 char *body;
14935 int is_define;
14936
14937 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14938 mac_ptr += bytes_read;
14939
14940 if (macinfo_type == DW_MACRO_GNU_define
14941 || macinfo_type == DW_MACRO_GNU_undef)
14942 {
14943 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14944 mac_ptr += bytes_read;
14945 }
14946 else
14947 {
14948 LONGEST str_offset;
14949
14950 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
14951 mac_ptr += offset_size;
14952
14953 body = read_indirect_string_at_offset (abfd, str_offset);
14954 }
14955
14956 is_define = (macinfo_type == DW_MACRO_GNU_define
14957 || macinfo_type == DW_MACRO_GNU_define_indirect);
14958 if (! current_file)
14959 {
14960 /* DWARF violation as no main source is present. */
14961 complaint (&symfile_complaints,
14962 _("debug info with no main source gives macro %s "
14963 "on line %d: %s"),
14964 is_define ? _("definition") : _("undefinition"),
14965 line, body);
14966 break;
14967 }
14968 if ((line == 0 && !at_commandline)
14969 || (line != 0 && at_commandline))
14970 complaint (&symfile_complaints,
14971 _("debug info gives %s macro %s with %s line %d: %s"),
14972 at_commandline ? _("command-line") : _("in-file"),
14973 is_define ? _("definition") : _("undefinition"),
14974 line == 0 ? _("zero") : _("non-zero"), line, body);
14975
14976 if (is_define)
14977 parse_macro_definition (current_file, line, body);
14978 else
14979 {
14980 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
14981 || macinfo_type == DW_MACRO_GNU_undef_indirect);
14982 macro_undef (current_file, line, body);
14983 }
14984 }
14985 break;
14986
14987 case DW_MACRO_GNU_start_file:
14988 {
14989 unsigned int bytes_read;
14990 int line, file;
14991
14992 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14993 mac_ptr += bytes_read;
14994 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14995 mac_ptr += bytes_read;
14996
14997 if ((line == 0 && !at_commandline)
14998 || (line != 0 && at_commandline))
14999 complaint (&symfile_complaints,
15000 _("debug info gives source %d included "
15001 "from %s at %s line %d"),
15002 file, at_commandline ? _("command-line") : _("file"),
15003 line == 0 ? _("zero") : _("non-zero"), line);
15004
15005 if (at_commandline)
15006 {
15007 /* This DW_MACRO_GNU_start_file was executed in the
15008 pass one. */
15009 at_commandline = 0;
15010 }
15011 else
15012 current_file = macro_start_file (file, line,
15013 current_file, comp_dir,
15014 lh, objfile);
15015 }
15016 break;
15017
15018 case DW_MACRO_GNU_end_file:
15019 if (! current_file)
15020 complaint (&symfile_complaints,
15021 _("macro debug info has an unmatched "
15022 "`close_file' directive"));
15023 else
15024 {
15025 current_file = current_file->included_by;
15026 if (! current_file)
15027 {
15028 enum dwarf_macro_record_type next_type;
15029
15030 /* GCC circa March 2002 doesn't produce the zero
15031 type byte marking the end of the compilation
15032 unit. Complain if it's not there, but exit no
15033 matter what. */
15034
15035 /* Do we at least have room for a macinfo type byte? */
15036 if (mac_ptr >= mac_end)
15037 {
15038 dwarf2_macros_too_long_complaint (section);
15039 return;
15040 }
15041
15042 /* We don't increment mac_ptr here, so this is just
15043 a look-ahead. */
15044 next_type = read_1_byte (abfd, mac_ptr);
15045 if (next_type != 0)
15046 complaint (&symfile_complaints,
15047 _("no terminating 0-type entry for "
15048 "macros in `.debug_macinfo' section"));
15049
15050 return;
15051 }
15052 }
15053 break;
15054
15055 case DW_MACRO_GNU_transparent_include:
15056 {
15057 LONGEST offset;
15058
15059 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15060 mac_ptr += offset_size;
15061
15062 dwarf_decode_macro_bytes (abfd,
15063 section->buffer + offset,
15064 mac_end, current_file,
15065 lh, comp_dir,
15066 section, section_is_gnu,
15067 offset_size, objfile);
15068 }
15069 break;
15070
15071 case DW_MACINFO_vendor_ext:
15072 if (!section_is_gnu)
15073 {
15074 unsigned int bytes_read;
15075 int constant;
15076
15077 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15078 mac_ptr += bytes_read;
15079 read_direct_string (abfd, mac_ptr, &bytes_read);
15080 mac_ptr += bytes_read;
15081
15082 /* We don't recognize any vendor extensions. */
15083 break;
15084 }
15085 /* FALLTHROUGH */
15086
15087 default:
15088 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15089 mac_ptr, abfd, offset_size,
15090 section);
15091 if (mac_ptr == NULL)
15092 return;
15093 break;
15094 }
15095 } while (macinfo_type != 0);
15096 }
15097
15098 static void
15099 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15100 char *comp_dir, bfd *abfd,
15101 struct dwarf2_cu *cu,
15102 struct dwarf2_section_info *section,
15103 int section_is_gnu)
15104 {
15105 gdb_byte *mac_ptr, *mac_end;
15106 struct macro_source_file *current_file = 0;
15107 enum dwarf_macro_record_type macinfo_type;
15108 unsigned int offset_size = cu->header.offset_size;
15109 gdb_byte *opcode_definitions[256];
15110
15111 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15112 if (section->buffer == NULL)
15113 {
15114 complaint (&symfile_complaints, _("missing %s section"),
15115 section->asection->name);
15116 return;
15117 }
15118
15119 /* First pass: Find the name of the base filename.
15120 This filename is needed in order to process all macros whose definition
15121 (or undefinition) comes from the command line. These macros are defined
15122 before the first DW_MACINFO_start_file entry, and yet still need to be
15123 associated to the base file.
15124
15125 To determine the base file name, we scan the macro definitions until we
15126 reach the first DW_MACINFO_start_file entry. We then initialize
15127 CURRENT_FILE accordingly so that any macro definition found before the
15128 first DW_MACINFO_start_file can still be associated to the base file. */
15129
15130 mac_ptr = section->buffer + offset;
15131 mac_end = section->buffer + section->size;
15132
15133 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15134 &offset_size, section_is_gnu);
15135 if (mac_ptr == NULL)
15136 {
15137 /* We already issued a complaint. */
15138 return;
15139 }
15140
15141 do
15142 {
15143 /* Do we at least have room for a macinfo type byte? */
15144 if (mac_ptr >= mac_end)
15145 {
15146 /* Complaint is printed during the second pass as GDB will probably
15147 stop the first pass earlier upon finding
15148 DW_MACINFO_start_file. */
15149 break;
15150 }
15151
15152 macinfo_type = read_1_byte (abfd, mac_ptr);
15153 mac_ptr++;
15154
15155 /* Note that we rely on the fact that the corresponding GNU and
15156 DWARF constants are the same. */
15157 switch (macinfo_type)
15158 {
15159 /* A zero macinfo type indicates the end of the macro
15160 information. */
15161 case 0:
15162 break;
15163
15164 case DW_MACRO_GNU_define:
15165 case DW_MACRO_GNU_undef:
15166 /* Only skip the data by MAC_PTR. */
15167 {
15168 unsigned int bytes_read;
15169
15170 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15171 mac_ptr += bytes_read;
15172 read_direct_string (abfd, mac_ptr, &bytes_read);
15173 mac_ptr += bytes_read;
15174 }
15175 break;
15176
15177 case DW_MACRO_GNU_start_file:
15178 {
15179 unsigned int bytes_read;
15180 int line, file;
15181
15182 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15183 mac_ptr += bytes_read;
15184 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15185 mac_ptr += bytes_read;
15186
15187 current_file = macro_start_file (file, line, current_file,
15188 comp_dir, lh, cu->objfile);
15189 }
15190 break;
15191
15192 case DW_MACRO_GNU_end_file:
15193 /* No data to skip by MAC_PTR. */
15194 break;
15195
15196 case DW_MACRO_GNU_define_indirect:
15197 case DW_MACRO_GNU_undef_indirect:
15198 {
15199 unsigned int bytes_read;
15200
15201 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15202 mac_ptr += bytes_read;
15203 mac_ptr += offset_size;
15204 }
15205 break;
15206
15207 case DW_MACRO_GNU_transparent_include:
15208 /* Note that, according to the spec, a transparent include
15209 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15210 skip this opcode. */
15211 mac_ptr += offset_size;
15212 break;
15213
15214 case DW_MACINFO_vendor_ext:
15215 /* Only skip the data by MAC_PTR. */
15216 if (!section_is_gnu)
15217 {
15218 unsigned int bytes_read;
15219
15220 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15221 mac_ptr += bytes_read;
15222 read_direct_string (abfd, mac_ptr, &bytes_read);
15223 mac_ptr += bytes_read;
15224 }
15225 /* FALLTHROUGH */
15226
15227 default:
15228 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15229 mac_ptr, abfd, offset_size,
15230 section);
15231 if (mac_ptr == NULL)
15232 return;
15233 break;
15234 }
15235 } while (macinfo_type != 0 && current_file == NULL);
15236
15237 /* Second pass: Process all entries.
15238
15239 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15240 command-line macro definitions/undefinitions. This flag is unset when we
15241 reach the first DW_MACINFO_start_file entry. */
15242
15243 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15244 current_file, lh, comp_dir, section, section_is_gnu,
15245 offset_size, cu->objfile);
15246 }
15247
15248 /* Check if the attribute's form is a DW_FORM_block*
15249 if so return true else false. */
15250 static int
15251 attr_form_is_block (struct attribute *attr)
15252 {
15253 return (attr == NULL ? 0 :
15254 attr->form == DW_FORM_block1
15255 || attr->form == DW_FORM_block2
15256 || attr->form == DW_FORM_block4
15257 || attr->form == DW_FORM_block
15258 || attr->form == DW_FORM_exprloc);
15259 }
15260
15261 /* Return non-zero if ATTR's value is a section offset --- classes
15262 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15263 You may use DW_UNSND (attr) to retrieve such offsets.
15264
15265 Section 7.5.4, "Attribute Encodings", explains that no attribute
15266 may have a value that belongs to more than one of these classes; it
15267 would be ambiguous if we did, because we use the same forms for all
15268 of them. */
15269 static int
15270 attr_form_is_section_offset (struct attribute *attr)
15271 {
15272 return (attr->form == DW_FORM_data4
15273 || attr->form == DW_FORM_data8
15274 || attr->form == DW_FORM_sec_offset);
15275 }
15276
15277
15278 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15279 zero otherwise. When this function returns true, you can apply
15280 dwarf2_get_attr_constant_value to it.
15281
15282 However, note that for some attributes you must check
15283 attr_form_is_section_offset before using this test. DW_FORM_data4
15284 and DW_FORM_data8 are members of both the constant class, and of
15285 the classes that contain offsets into other debug sections
15286 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15287 that, if an attribute's can be either a constant or one of the
15288 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15289 taken as section offsets, not constants. */
15290 static int
15291 attr_form_is_constant (struct attribute *attr)
15292 {
15293 switch (attr->form)
15294 {
15295 case DW_FORM_sdata:
15296 case DW_FORM_udata:
15297 case DW_FORM_data1:
15298 case DW_FORM_data2:
15299 case DW_FORM_data4:
15300 case DW_FORM_data8:
15301 return 1;
15302 default:
15303 return 0;
15304 }
15305 }
15306
15307 /* A helper function that fills in a dwarf2_loclist_baton. */
15308
15309 static void
15310 fill_in_loclist_baton (struct dwarf2_cu *cu,
15311 struct dwarf2_loclist_baton *baton,
15312 struct attribute *attr)
15313 {
15314 dwarf2_read_section (dwarf2_per_objfile->objfile,
15315 &dwarf2_per_objfile->loc);
15316
15317 baton->per_cu = cu->per_cu;
15318 gdb_assert (baton->per_cu);
15319 /* We don't know how long the location list is, but make sure we
15320 don't run off the edge of the section. */
15321 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15322 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15323 baton->base_address = cu->base_address;
15324 }
15325
15326 static void
15327 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15328 struct dwarf2_cu *cu)
15329 {
15330 if (attr_form_is_section_offset (attr)
15331 /* ".debug_loc" may not exist at all, or the offset may be outside
15332 the section. If so, fall through to the complaint in the
15333 other branch. */
15334 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15335 &dwarf2_per_objfile->loc))
15336 {
15337 struct dwarf2_loclist_baton *baton;
15338
15339 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15340 sizeof (struct dwarf2_loclist_baton));
15341
15342 fill_in_loclist_baton (cu, baton, attr);
15343
15344 if (cu->base_known == 0)
15345 complaint (&symfile_complaints,
15346 _("Location list used without "
15347 "specifying the CU base address."));
15348
15349 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15350 SYMBOL_LOCATION_BATON (sym) = baton;
15351 }
15352 else
15353 {
15354 struct dwarf2_locexpr_baton *baton;
15355
15356 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15357 sizeof (struct dwarf2_locexpr_baton));
15358 baton->per_cu = cu->per_cu;
15359 gdb_assert (baton->per_cu);
15360
15361 if (attr_form_is_block (attr))
15362 {
15363 /* Note that we're just copying the block's data pointer
15364 here, not the actual data. We're still pointing into the
15365 info_buffer for SYM's objfile; right now we never release
15366 that buffer, but when we do clean up properly this may
15367 need to change. */
15368 baton->size = DW_BLOCK (attr)->size;
15369 baton->data = DW_BLOCK (attr)->data;
15370 }
15371 else
15372 {
15373 dwarf2_invalid_attrib_class_complaint ("location description",
15374 SYMBOL_NATURAL_NAME (sym));
15375 baton->size = 0;
15376 baton->data = NULL;
15377 }
15378
15379 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15380 SYMBOL_LOCATION_BATON (sym) = baton;
15381 }
15382 }
15383
15384 /* Return the OBJFILE associated with the compilation unit CU. If CU
15385 came from a separate debuginfo file, then the master objfile is
15386 returned. */
15387
15388 struct objfile *
15389 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15390 {
15391 struct objfile *objfile = per_cu->objfile;
15392
15393 /* Return the master objfile, so that we can report and look up the
15394 correct file containing this variable. */
15395 if (objfile->separate_debug_objfile_backlink)
15396 objfile = objfile->separate_debug_objfile_backlink;
15397
15398 return objfile;
15399 }
15400
15401 /* Return the address size given in the compilation unit header for CU. */
15402
15403 CORE_ADDR
15404 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15405 {
15406 if (per_cu->cu)
15407 return per_cu->cu->header.addr_size;
15408 else
15409 {
15410 /* If the CU is not currently read in, we re-read its header. */
15411 struct objfile *objfile = per_cu->objfile;
15412 struct dwarf2_per_objfile *per_objfile
15413 = objfile_data (objfile, dwarf2_objfile_data_key);
15414 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15415 struct comp_unit_head cu_header;
15416
15417 memset (&cu_header, 0, sizeof cu_header);
15418 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15419 return cu_header.addr_size;
15420 }
15421 }
15422
15423 /* Return the offset size given in the compilation unit header for CU. */
15424
15425 int
15426 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15427 {
15428 if (per_cu->cu)
15429 return per_cu->cu->header.offset_size;
15430 else
15431 {
15432 /* If the CU is not currently read in, we re-read its header. */
15433 struct objfile *objfile = per_cu->objfile;
15434 struct dwarf2_per_objfile *per_objfile
15435 = objfile_data (objfile, dwarf2_objfile_data_key);
15436 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15437 struct comp_unit_head cu_header;
15438
15439 memset (&cu_header, 0, sizeof cu_header);
15440 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15441 return cu_header.offset_size;
15442 }
15443 }
15444
15445 /* Return the text offset of the CU. The returned offset comes from
15446 this CU's objfile. If this objfile came from a separate debuginfo
15447 file, then the offset may be different from the corresponding
15448 offset in the parent objfile. */
15449
15450 CORE_ADDR
15451 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15452 {
15453 struct objfile *objfile = per_cu->objfile;
15454
15455 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15456 }
15457
15458 /* Locate the .debug_info compilation unit from CU's objfile which contains
15459 the DIE at OFFSET. Raises an error on failure. */
15460
15461 static struct dwarf2_per_cu_data *
15462 dwarf2_find_containing_comp_unit (unsigned int offset,
15463 struct objfile *objfile)
15464 {
15465 struct dwarf2_per_cu_data *this_cu;
15466 int low, high;
15467
15468 low = 0;
15469 high = dwarf2_per_objfile->n_comp_units - 1;
15470 while (high > low)
15471 {
15472 int mid = low + (high - low) / 2;
15473
15474 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15475 high = mid;
15476 else
15477 low = mid + 1;
15478 }
15479 gdb_assert (low == high);
15480 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15481 {
15482 if (low == 0)
15483 error (_("Dwarf Error: could not find partial DIE containing "
15484 "offset 0x%lx [in module %s]"),
15485 (long) offset, bfd_get_filename (objfile->obfd));
15486
15487 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15488 return dwarf2_per_objfile->all_comp_units[low-1];
15489 }
15490 else
15491 {
15492 this_cu = dwarf2_per_objfile->all_comp_units[low];
15493 if (low == dwarf2_per_objfile->n_comp_units - 1
15494 && offset >= this_cu->offset + this_cu->length)
15495 error (_("invalid dwarf2 offset %u"), offset);
15496 gdb_assert (offset < this_cu->offset + this_cu->length);
15497 return this_cu;
15498 }
15499 }
15500
15501 /* Locate the compilation unit from OBJFILE which is located at exactly
15502 OFFSET. Raises an error on failure. */
15503
15504 static struct dwarf2_per_cu_data *
15505 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15506 {
15507 struct dwarf2_per_cu_data *this_cu;
15508
15509 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15510 if (this_cu->offset != offset)
15511 error (_("no compilation unit with offset %u."), offset);
15512 return this_cu;
15513 }
15514
15515 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
15516
15517 static void
15518 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15519 {
15520 memset (cu, 0, sizeof (*cu));
15521 cu->objfile = objfile;
15522 obstack_init (&cu->comp_unit_obstack);
15523 }
15524
15525 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15526
15527 static void
15528 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15529 {
15530 struct attribute *attr;
15531
15532 /* Set the language we're debugging. */
15533 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15534 if (attr)
15535 set_cu_language (DW_UNSND (attr), cu);
15536 else
15537 {
15538 cu->language = language_minimal;
15539 cu->language_defn = language_def (cu->language);
15540 }
15541 }
15542
15543 /* Release one cached compilation unit, CU. We unlink it from the tree
15544 of compilation units, but we don't remove it from the read_in_chain;
15545 the caller is responsible for that.
15546 NOTE: DATA is a void * because this function is also used as a
15547 cleanup routine. */
15548
15549 static void
15550 free_one_comp_unit (void *data)
15551 {
15552 struct dwarf2_cu *cu = data;
15553
15554 if (cu->per_cu != NULL)
15555 cu->per_cu->cu = NULL;
15556 cu->per_cu = NULL;
15557
15558 obstack_free (&cu->comp_unit_obstack, NULL);
15559
15560 xfree (cu);
15561 }
15562
15563 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15564 when we're finished with it. We can't free the pointer itself, but be
15565 sure to unlink it from the cache. Also release any associated storage
15566 and perform cache maintenance.
15567
15568 Only used during partial symbol parsing. */
15569
15570 static void
15571 free_stack_comp_unit (void *data)
15572 {
15573 struct dwarf2_cu *cu = data;
15574
15575 obstack_free (&cu->comp_unit_obstack, NULL);
15576 cu->partial_dies = NULL;
15577
15578 if (cu->per_cu != NULL)
15579 {
15580 /* This compilation unit is on the stack in our caller, so we
15581 should not xfree it. Just unlink it. */
15582 cu->per_cu->cu = NULL;
15583 cu->per_cu = NULL;
15584
15585 /* If we had a per-cu pointer, then we may have other compilation
15586 units loaded, so age them now. */
15587 age_cached_comp_units ();
15588 }
15589 }
15590
15591 /* Free all cached compilation units. */
15592
15593 static void
15594 free_cached_comp_units (void *data)
15595 {
15596 struct dwarf2_per_cu_data *per_cu, **last_chain;
15597
15598 per_cu = dwarf2_per_objfile->read_in_chain;
15599 last_chain = &dwarf2_per_objfile->read_in_chain;
15600 while (per_cu != NULL)
15601 {
15602 struct dwarf2_per_cu_data *next_cu;
15603
15604 next_cu = per_cu->cu->read_in_chain;
15605
15606 free_one_comp_unit (per_cu->cu);
15607 *last_chain = next_cu;
15608
15609 per_cu = next_cu;
15610 }
15611 }
15612
15613 /* Increase the age counter on each cached compilation unit, and free
15614 any that are too old. */
15615
15616 static void
15617 age_cached_comp_units (void)
15618 {
15619 struct dwarf2_per_cu_data *per_cu, **last_chain;
15620
15621 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15622 per_cu = dwarf2_per_objfile->read_in_chain;
15623 while (per_cu != NULL)
15624 {
15625 per_cu->cu->last_used ++;
15626 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15627 dwarf2_mark (per_cu->cu);
15628 per_cu = per_cu->cu->read_in_chain;
15629 }
15630
15631 per_cu = dwarf2_per_objfile->read_in_chain;
15632 last_chain = &dwarf2_per_objfile->read_in_chain;
15633 while (per_cu != NULL)
15634 {
15635 struct dwarf2_per_cu_data *next_cu;
15636
15637 next_cu = per_cu->cu->read_in_chain;
15638
15639 if (!per_cu->cu->mark)
15640 {
15641 free_one_comp_unit (per_cu->cu);
15642 *last_chain = next_cu;
15643 }
15644 else
15645 last_chain = &per_cu->cu->read_in_chain;
15646
15647 per_cu = next_cu;
15648 }
15649 }
15650
15651 /* Remove a single compilation unit from the cache. */
15652
15653 static void
15654 free_one_cached_comp_unit (void *target_cu)
15655 {
15656 struct dwarf2_per_cu_data *per_cu, **last_chain;
15657
15658 per_cu = dwarf2_per_objfile->read_in_chain;
15659 last_chain = &dwarf2_per_objfile->read_in_chain;
15660 while (per_cu != NULL)
15661 {
15662 struct dwarf2_per_cu_data *next_cu;
15663
15664 next_cu = per_cu->cu->read_in_chain;
15665
15666 if (per_cu->cu == target_cu)
15667 {
15668 free_one_comp_unit (per_cu->cu);
15669 *last_chain = next_cu;
15670 break;
15671 }
15672 else
15673 last_chain = &per_cu->cu->read_in_chain;
15674
15675 per_cu = next_cu;
15676 }
15677 }
15678
15679 /* Release all extra memory associated with OBJFILE. */
15680
15681 void
15682 dwarf2_free_objfile (struct objfile *objfile)
15683 {
15684 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15685
15686 if (dwarf2_per_objfile == NULL)
15687 return;
15688
15689 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15690 free_cached_comp_units (NULL);
15691
15692 if (dwarf2_per_objfile->quick_file_names_table)
15693 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15694
15695 /* Everything else should be on the objfile obstack. */
15696 }
15697
15698 /* A pair of DIE offset and GDB type pointer. We store these
15699 in a hash table separate from the DIEs, and preserve them
15700 when the DIEs are flushed out of cache. */
15701
15702 struct dwarf2_offset_and_type
15703 {
15704 unsigned int offset;
15705 struct type *type;
15706 };
15707
15708 /* Hash function for a dwarf2_offset_and_type. */
15709
15710 static hashval_t
15711 offset_and_type_hash (const void *item)
15712 {
15713 const struct dwarf2_offset_and_type *ofs = item;
15714
15715 return ofs->offset;
15716 }
15717
15718 /* Equality function for a dwarf2_offset_and_type. */
15719
15720 static int
15721 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15722 {
15723 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15724 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15725
15726 return ofs_lhs->offset == ofs_rhs->offset;
15727 }
15728
15729 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15730 table if necessary. For convenience, return TYPE.
15731
15732 The DIEs reading must have careful ordering to:
15733 * Not cause infite loops trying to read in DIEs as a prerequisite for
15734 reading current DIE.
15735 * Not trying to dereference contents of still incompletely read in types
15736 while reading in other DIEs.
15737 * Enable referencing still incompletely read in types just by a pointer to
15738 the type without accessing its fields.
15739
15740 Therefore caller should follow these rules:
15741 * Try to fetch any prerequisite types we may need to build this DIE type
15742 before building the type and calling set_die_type.
15743 * After building type call set_die_type for current DIE as soon as
15744 possible before fetching more types to complete the current type.
15745 * Make the type as complete as possible before fetching more types. */
15746
15747 static struct type *
15748 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15749 {
15750 struct dwarf2_offset_and_type **slot, ofs;
15751 struct objfile *objfile = cu->objfile;
15752 htab_t *type_hash_ptr;
15753
15754 /* For Ada types, make sure that the gnat-specific data is always
15755 initialized (if not already set). There are a few types where
15756 we should not be doing so, because the type-specific area is
15757 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15758 where the type-specific area is used to store the floatformat).
15759 But this is not a problem, because the gnat-specific information
15760 is actually not needed for these types. */
15761 if (need_gnat_info (cu)
15762 && TYPE_CODE (type) != TYPE_CODE_FUNC
15763 && TYPE_CODE (type) != TYPE_CODE_FLT
15764 && !HAVE_GNAT_AUX_INFO (type))
15765 INIT_GNAT_SPECIFIC (type);
15766
15767 if (cu->per_cu->debug_type_section)
15768 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15769 else
15770 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15771
15772 if (*type_hash_ptr == NULL)
15773 {
15774 *type_hash_ptr
15775 = htab_create_alloc_ex (127,
15776 offset_and_type_hash,
15777 offset_and_type_eq,
15778 NULL,
15779 &objfile->objfile_obstack,
15780 hashtab_obstack_allocate,
15781 dummy_obstack_deallocate);
15782 }
15783
15784 ofs.offset = die->offset;
15785 ofs.type = type;
15786 slot = (struct dwarf2_offset_and_type **)
15787 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15788 if (*slot)
15789 complaint (&symfile_complaints,
15790 _("A problem internal to GDB: DIE 0x%x has type already set"),
15791 die->offset);
15792 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15793 **slot = ofs;
15794 return type;
15795 }
15796
15797 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15798 table, or return NULL if the die does not have a saved type. */
15799
15800 static struct type *
15801 get_die_type_at_offset (unsigned int offset,
15802 struct dwarf2_per_cu_data *per_cu)
15803 {
15804 struct dwarf2_offset_and_type *slot, ofs;
15805 htab_t type_hash;
15806
15807 if (per_cu->debug_type_section)
15808 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15809 else
15810 type_hash = dwarf2_per_objfile->debug_info_type_hash;
15811 if (type_hash == NULL)
15812 return NULL;
15813
15814 ofs.offset = offset;
15815 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15816 if (slot)
15817 return slot->type;
15818 else
15819 return NULL;
15820 }
15821
15822 /* Look up the type for DIE in the appropriate type_hash table,
15823 or return NULL if DIE does not have a saved type. */
15824
15825 static struct type *
15826 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15827 {
15828 return get_die_type_at_offset (die->offset, cu->per_cu);
15829 }
15830
15831 /* Add a dependence relationship from CU to REF_PER_CU. */
15832
15833 static void
15834 dwarf2_add_dependence (struct dwarf2_cu *cu,
15835 struct dwarf2_per_cu_data *ref_per_cu)
15836 {
15837 void **slot;
15838
15839 if (cu->dependencies == NULL)
15840 cu->dependencies
15841 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15842 NULL, &cu->comp_unit_obstack,
15843 hashtab_obstack_allocate,
15844 dummy_obstack_deallocate);
15845
15846 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15847 if (*slot == NULL)
15848 *slot = ref_per_cu;
15849 }
15850
15851 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15852 Set the mark field in every compilation unit in the
15853 cache that we must keep because we are keeping CU. */
15854
15855 static int
15856 dwarf2_mark_helper (void **slot, void *data)
15857 {
15858 struct dwarf2_per_cu_data *per_cu;
15859
15860 per_cu = (struct dwarf2_per_cu_data *) *slot;
15861
15862 /* cu->dependencies references may not yet have been ever read if QUIT aborts
15863 reading of the chain. As such dependencies remain valid it is not much
15864 useful to track and undo them during QUIT cleanups. */
15865 if (per_cu->cu == NULL)
15866 return 1;
15867
15868 if (per_cu->cu->mark)
15869 return 1;
15870 per_cu->cu->mark = 1;
15871
15872 if (per_cu->cu->dependencies != NULL)
15873 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15874
15875 return 1;
15876 }
15877
15878 /* Set the mark field in CU and in every other compilation unit in the
15879 cache that we must keep because we are keeping CU. */
15880
15881 static void
15882 dwarf2_mark (struct dwarf2_cu *cu)
15883 {
15884 if (cu->mark)
15885 return;
15886 cu->mark = 1;
15887 if (cu->dependencies != NULL)
15888 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15889 }
15890
15891 static void
15892 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15893 {
15894 while (per_cu)
15895 {
15896 per_cu->cu->mark = 0;
15897 per_cu = per_cu->cu->read_in_chain;
15898 }
15899 }
15900
15901 /* Trivial hash function for partial_die_info: the hash value of a DIE
15902 is its offset in .debug_info for this objfile. */
15903
15904 static hashval_t
15905 partial_die_hash (const void *item)
15906 {
15907 const struct partial_die_info *part_die = item;
15908
15909 return part_die->offset;
15910 }
15911
15912 /* Trivial comparison function for partial_die_info structures: two DIEs
15913 are equal if they have the same offset. */
15914
15915 static int
15916 partial_die_eq (const void *item_lhs, const void *item_rhs)
15917 {
15918 const struct partial_die_info *part_die_lhs = item_lhs;
15919 const struct partial_die_info *part_die_rhs = item_rhs;
15920
15921 return part_die_lhs->offset == part_die_rhs->offset;
15922 }
15923
15924 static struct cmd_list_element *set_dwarf2_cmdlist;
15925 static struct cmd_list_element *show_dwarf2_cmdlist;
15926
15927 static void
15928 set_dwarf2_cmd (char *args, int from_tty)
15929 {
15930 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15931 }
15932
15933 static void
15934 show_dwarf2_cmd (char *args, int from_tty)
15935 {
15936 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15937 }
15938
15939 /* If section described by INFO was mmapped, munmap it now. */
15940
15941 static void
15942 munmap_section_buffer (struct dwarf2_section_info *info)
15943 {
15944 if (info->map_addr != NULL)
15945 {
15946 #ifdef HAVE_MMAP
15947 int res;
15948
15949 res = munmap (info->map_addr, info->map_len);
15950 gdb_assert (res == 0);
15951 #else
15952 /* Without HAVE_MMAP, we should never be here to begin with. */
15953 gdb_assert_not_reached ("no mmap support");
15954 #endif
15955 }
15956 }
15957
15958 /* munmap debug sections for OBJFILE, if necessary. */
15959
15960 static void
15961 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15962 {
15963 struct dwarf2_per_objfile *data = d;
15964 int ix;
15965 struct dwarf2_section_info *section;
15966
15967 /* This is sorted according to the order they're defined in to make it easier
15968 to keep in sync. */
15969 munmap_section_buffer (&data->info);
15970 munmap_section_buffer (&data->abbrev);
15971 munmap_section_buffer (&data->line);
15972 munmap_section_buffer (&data->loc);
15973 munmap_section_buffer (&data->macinfo);
15974 munmap_section_buffer (&data->macro);
15975 munmap_section_buffer (&data->str);
15976 munmap_section_buffer (&data->ranges);
15977 munmap_section_buffer (&data->frame);
15978 munmap_section_buffer (&data->eh_frame);
15979 munmap_section_buffer (&data->gdb_index);
15980
15981 for (ix = 0;
15982 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
15983 ++ix)
15984 munmap_section_buffer (section);
15985
15986 VEC_free (dwarf2_section_info_def, data->types);
15987 }
15988
15989 \f
15990 /* The "save gdb-index" command. */
15991
15992 /* The contents of the hash table we create when building the string
15993 table. */
15994 struct strtab_entry
15995 {
15996 offset_type offset;
15997 const char *str;
15998 };
15999
16000 /* Hash function for a strtab_entry.
16001
16002 Function is used only during write_hash_table so no index format backward
16003 compatibility is needed. */
16004
16005 static hashval_t
16006 hash_strtab_entry (const void *e)
16007 {
16008 const struct strtab_entry *entry = e;
16009 return mapped_index_string_hash (INT_MAX, entry->str);
16010 }
16011
16012 /* Equality function for a strtab_entry. */
16013
16014 static int
16015 eq_strtab_entry (const void *a, const void *b)
16016 {
16017 const struct strtab_entry *ea = a;
16018 const struct strtab_entry *eb = b;
16019 return !strcmp (ea->str, eb->str);
16020 }
16021
16022 /* Create a strtab_entry hash table. */
16023
16024 static htab_t
16025 create_strtab (void)
16026 {
16027 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16028 xfree, xcalloc, xfree);
16029 }
16030
16031 /* Add a string to the constant pool. Return the string's offset in
16032 host order. */
16033
16034 static offset_type
16035 add_string (htab_t table, struct obstack *cpool, const char *str)
16036 {
16037 void **slot;
16038 struct strtab_entry entry;
16039 struct strtab_entry *result;
16040
16041 entry.str = str;
16042 slot = htab_find_slot (table, &entry, INSERT);
16043 if (*slot)
16044 result = *slot;
16045 else
16046 {
16047 result = XNEW (struct strtab_entry);
16048 result->offset = obstack_object_size (cpool);
16049 result->str = str;
16050 obstack_grow_str0 (cpool, str);
16051 *slot = result;
16052 }
16053 return result->offset;
16054 }
16055
16056 /* An entry in the symbol table. */
16057 struct symtab_index_entry
16058 {
16059 /* The name of the symbol. */
16060 const char *name;
16061 /* The offset of the name in the constant pool. */
16062 offset_type index_offset;
16063 /* A sorted vector of the indices of all the CUs that hold an object
16064 of this name. */
16065 VEC (offset_type) *cu_indices;
16066 };
16067
16068 /* The symbol table. This is a power-of-2-sized hash table. */
16069 struct mapped_symtab
16070 {
16071 offset_type n_elements;
16072 offset_type size;
16073 struct symtab_index_entry **data;
16074 };
16075
16076 /* Hash function for a symtab_index_entry. */
16077
16078 static hashval_t
16079 hash_symtab_entry (const void *e)
16080 {
16081 const struct symtab_index_entry *entry = e;
16082 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16083 sizeof (offset_type) * VEC_length (offset_type,
16084 entry->cu_indices),
16085 0);
16086 }
16087
16088 /* Equality function for a symtab_index_entry. */
16089
16090 static int
16091 eq_symtab_entry (const void *a, const void *b)
16092 {
16093 const struct symtab_index_entry *ea = a;
16094 const struct symtab_index_entry *eb = b;
16095 int len = VEC_length (offset_type, ea->cu_indices);
16096 if (len != VEC_length (offset_type, eb->cu_indices))
16097 return 0;
16098 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16099 VEC_address (offset_type, eb->cu_indices),
16100 sizeof (offset_type) * len);
16101 }
16102
16103 /* Destroy a symtab_index_entry. */
16104
16105 static void
16106 delete_symtab_entry (void *p)
16107 {
16108 struct symtab_index_entry *entry = p;
16109 VEC_free (offset_type, entry->cu_indices);
16110 xfree (entry);
16111 }
16112
16113 /* Create a hash table holding symtab_index_entry objects. */
16114
16115 static htab_t
16116 create_symbol_hash_table (void)
16117 {
16118 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16119 delete_symtab_entry, xcalloc, xfree);
16120 }
16121
16122 /* Create a new mapped symtab object. */
16123
16124 static struct mapped_symtab *
16125 create_mapped_symtab (void)
16126 {
16127 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16128 symtab->n_elements = 0;
16129 symtab->size = 1024;
16130 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16131 return symtab;
16132 }
16133
16134 /* Destroy a mapped_symtab. */
16135
16136 static void
16137 cleanup_mapped_symtab (void *p)
16138 {
16139 struct mapped_symtab *symtab = p;
16140 /* The contents of the array are freed when the other hash table is
16141 destroyed. */
16142 xfree (symtab->data);
16143 xfree (symtab);
16144 }
16145
16146 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16147 the slot.
16148
16149 Function is used only during write_hash_table so no index format backward
16150 compatibility is needed. */
16151
16152 static struct symtab_index_entry **
16153 find_slot (struct mapped_symtab *symtab, const char *name)
16154 {
16155 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16156
16157 index = hash & (symtab->size - 1);
16158 step = ((hash * 17) & (symtab->size - 1)) | 1;
16159
16160 for (;;)
16161 {
16162 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16163 return &symtab->data[index];
16164 index = (index + step) & (symtab->size - 1);
16165 }
16166 }
16167
16168 /* Expand SYMTAB's hash table. */
16169
16170 static void
16171 hash_expand (struct mapped_symtab *symtab)
16172 {
16173 offset_type old_size = symtab->size;
16174 offset_type i;
16175 struct symtab_index_entry **old_entries = symtab->data;
16176
16177 symtab->size *= 2;
16178 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16179
16180 for (i = 0; i < old_size; ++i)
16181 {
16182 if (old_entries[i])
16183 {
16184 struct symtab_index_entry **slot = find_slot (symtab,
16185 old_entries[i]->name);
16186 *slot = old_entries[i];
16187 }
16188 }
16189
16190 xfree (old_entries);
16191 }
16192
16193 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16194 is the index of the CU in which the symbol appears. */
16195
16196 static void
16197 add_index_entry (struct mapped_symtab *symtab, const char *name,
16198 offset_type cu_index)
16199 {
16200 struct symtab_index_entry **slot;
16201
16202 ++symtab->n_elements;
16203 if (4 * symtab->n_elements / 3 >= symtab->size)
16204 hash_expand (symtab);
16205
16206 slot = find_slot (symtab, name);
16207 if (!*slot)
16208 {
16209 *slot = XNEW (struct symtab_index_entry);
16210 (*slot)->name = name;
16211 (*slot)->cu_indices = NULL;
16212 }
16213 /* Don't push an index twice. Due to how we add entries we only
16214 have to check the last one. */
16215 if (VEC_empty (offset_type, (*slot)->cu_indices)
16216 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16217 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16218 }
16219
16220 /* Add a vector of indices to the constant pool. */
16221
16222 static offset_type
16223 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16224 struct symtab_index_entry *entry)
16225 {
16226 void **slot;
16227
16228 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16229 if (!*slot)
16230 {
16231 offset_type len = VEC_length (offset_type, entry->cu_indices);
16232 offset_type val = MAYBE_SWAP (len);
16233 offset_type iter;
16234 int i;
16235
16236 *slot = entry;
16237 entry->index_offset = obstack_object_size (cpool);
16238
16239 obstack_grow (cpool, &val, sizeof (val));
16240 for (i = 0;
16241 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16242 ++i)
16243 {
16244 val = MAYBE_SWAP (iter);
16245 obstack_grow (cpool, &val, sizeof (val));
16246 }
16247 }
16248 else
16249 {
16250 struct symtab_index_entry *old_entry = *slot;
16251 entry->index_offset = old_entry->index_offset;
16252 entry = old_entry;
16253 }
16254 return entry->index_offset;
16255 }
16256
16257 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16258 constant pool entries going into the obstack CPOOL. */
16259
16260 static void
16261 write_hash_table (struct mapped_symtab *symtab,
16262 struct obstack *output, struct obstack *cpool)
16263 {
16264 offset_type i;
16265 htab_t symbol_hash_table;
16266 htab_t str_table;
16267
16268 symbol_hash_table = create_symbol_hash_table ();
16269 str_table = create_strtab ();
16270
16271 /* We add all the index vectors to the constant pool first, to
16272 ensure alignment is ok. */
16273 for (i = 0; i < symtab->size; ++i)
16274 {
16275 if (symtab->data[i])
16276 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16277 }
16278
16279 /* Now write out the hash table. */
16280 for (i = 0; i < symtab->size; ++i)
16281 {
16282 offset_type str_off, vec_off;
16283
16284 if (symtab->data[i])
16285 {
16286 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16287 vec_off = symtab->data[i]->index_offset;
16288 }
16289 else
16290 {
16291 /* While 0 is a valid constant pool index, it is not valid
16292 to have 0 for both offsets. */
16293 str_off = 0;
16294 vec_off = 0;
16295 }
16296
16297 str_off = MAYBE_SWAP (str_off);
16298 vec_off = MAYBE_SWAP (vec_off);
16299
16300 obstack_grow (output, &str_off, sizeof (str_off));
16301 obstack_grow (output, &vec_off, sizeof (vec_off));
16302 }
16303
16304 htab_delete (str_table);
16305 htab_delete (symbol_hash_table);
16306 }
16307
16308 /* Struct to map psymtab to CU index in the index file. */
16309 struct psymtab_cu_index_map
16310 {
16311 struct partial_symtab *psymtab;
16312 unsigned int cu_index;
16313 };
16314
16315 static hashval_t
16316 hash_psymtab_cu_index (const void *item)
16317 {
16318 const struct psymtab_cu_index_map *map = item;
16319
16320 return htab_hash_pointer (map->psymtab);
16321 }
16322
16323 static int
16324 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16325 {
16326 const struct psymtab_cu_index_map *lhs = item_lhs;
16327 const struct psymtab_cu_index_map *rhs = item_rhs;
16328
16329 return lhs->psymtab == rhs->psymtab;
16330 }
16331
16332 /* Helper struct for building the address table. */
16333 struct addrmap_index_data
16334 {
16335 struct objfile *objfile;
16336 struct obstack *addr_obstack;
16337 htab_t cu_index_htab;
16338
16339 /* Non-zero if the previous_* fields are valid.
16340 We can't write an entry until we see the next entry (since it is only then
16341 that we know the end of the entry). */
16342 int previous_valid;
16343 /* Index of the CU in the table of all CUs in the index file. */
16344 unsigned int previous_cu_index;
16345 /* Start address of the CU. */
16346 CORE_ADDR previous_cu_start;
16347 };
16348
16349 /* Write an address entry to OBSTACK. */
16350
16351 static void
16352 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16353 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16354 {
16355 offset_type cu_index_to_write;
16356 char addr[8];
16357 CORE_ADDR baseaddr;
16358
16359 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16360
16361 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16362 obstack_grow (obstack, addr, 8);
16363 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16364 obstack_grow (obstack, addr, 8);
16365 cu_index_to_write = MAYBE_SWAP (cu_index);
16366 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16367 }
16368
16369 /* Worker function for traversing an addrmap to build the address table. */
16370
16371 static int
16372 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16373 {
16374 struct addrmap_index_data *data = datap;
16375 struct partial_symtab *pst = obj;
16376 offset_type cu_index;
16377 void **slot;
16378
16379 if (data->previous_valid)
16380 add_address_entry (data->objfile, data->addr_obstack,
16381 data->previous_cu_start, start_addr,
16382 data->previous_cu_index);
16383
16384 data->previous_cu_start = start_addr;
16385 if (pst != NULL)
16386 {
16387 struct psymtab_cu_index_map find_map, *map;
16388 find_map.psymtab = pst;
16389 map = htab_find (data->cu_index_htab, &find_map);
16390 gdb_assert (map != NULL);
16391 data->previous_cu_index = map->cu_index;
16392 data->previous_valid = 1;
16393 }
16394 else
16395 data->previous_valid = 0;
16396
16397 return 0;
16398 }
16399
16400 /* Write OBJFILE's address map to OBSTACK.
16401 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16402 in the index file. */
16403
16404 static void
16405 write_address_map (struct objfile *objfile, struct obstack *obstack,
16406 htab_t cu_index_htab)
16407 {
16408 struct addrmap_index_data addrmap_index_data;
16409
16410 /* When writing the address table, we have to cope with the fact that
16411 the addrmap iterator only provides the start of a region; we have to
16412 wait until the next invocation to get the start of the next region. */
16413
16414 addrmap_index_data.objfile = objfile;
16415 addrmap_index_data.addr_obstack = obstack;
16416 addrmap_index_data.cu_index_htab = cu_index_htab;
16417 addrmap_index_data.previous_valid = 0;
16418
16419 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16420 &addrmap_index_data);
16421
16422 /* It's highly unlikely the last entry (end address = 0xff...ff)
16423 is valid, but we should still handle it.
16424 The end address is recorded as the start of the next region, but that
16425 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16426 anyway. */
16427 if (addrmap_index_data.previous_valid)
16428 add_address_entry (objfile, obstack,
16429 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16430 addrmap_index_data.previous_cu_index);
16431 }
16432
16433 /* Add a list of partial symbols to SYMTAB. */
16434
16435 static void
16436 write_psymbols (struct mapped_symtab *symtab,
16437 htab_t psyms_seen,
16438 struct partial_symbol **psymp,
16439 int count,
16440 offset_type cu_index,
16441 int is_static)
16442 {
16443 for (; count-- > 0; ++psymp)
16444 {
16445 void **slot, *lookup;
16446
16447 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16448 error (_("Ada is not currently supported by the index"));
16449
16450 /* We only want to add a given psymbol once. However, we also
16451 want to account for whether it is global or static. So, we
16452 may add it twice, using slightly different values. */
16453 if (is_static)
16454 {
16455 uintptr_t val = 1 | (uintptr_t) *psymp;
16456
16457 lookup = (void *) val;
16458 }
16459 else
16460 lookup = *psymp;
16461
16462 /* Only add a given psymbol once. */
16463 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16464 if (!*slot)
16465 {
16466 *slot = lookup;
16467 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16468 }
16469 }
16470 }
16471
16472 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16473 exception if there is an error. */
16474
16475 static void
16476 write_obstack (FILE *file, struct obstack *obstack)
16477 {
16478 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16479 file)
16480 != obstack_object_size (obstack))
16481 error (_("couldn't data write to file"));
16482 }
16483
16484 /* Unlink a file if the argument is not NULL. */
16485
16486 static void
16487 unlink_if_set (void *p)
16488 {
16489 char **filename = p;
16490 if (*filename)
16491 unlink (*filename);
16492 }
16493
16494 /* A helper struct used when iterating over debug_types. */
16495 struct signatured_type_index_data
16496 {
16497 struct objfile *objfile;
16498 struct mapped_symtab *symtab;
16499 struct obstack *types_list;
16500 htab_t psyms_seen;
16501 int cu_index;
16502 };
16503
16504 /* A helper function that writes a single signatured_type to an
16505 obstack. */
16506
16507 static int
16508 write_one_signatured_type (void **slot, void *d)
16509 {
16510 struct signatured_type_index_data *info = d;
16511 struct signatured_type *entry = (struct signatured_type *) *slot;
16512 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16513 struct partial_symtab *psymtab = per_cu->v.psymtab;
16514 gdb_byte val[8];
16515
16516 write_psymbols (info->symtab,
16517 info->psyms_seen,
16518 info->objfile->global_psymbols.list
16519 + psymtab->globals_offset,
16520 psymtab->n_global_syms, info->cu_index,
16521 0);
16522 write_psymbols (info->symtab,
16523 info->psyms_seen,
16524 info->objfile->static_psymbols.list
16525 + psymtab->statics_offset,
16526 psymtab->n_static_syms, info->cu_index,
16527 1);
16528
16529 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16530 obstack_grow (info->types_list, val, 8);
16531 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16532 obstack_grow (info->types_list, val, 8);
16533 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16534 obstack_grow (info->types_list, val, 8);
16535
16536 ++info->cu_index;
16537
16538 return 1;
16539 }
16540
16541 /* A cleanup function for an htab_t. */
16542
16543 static void
16544 cleanup_htab (void *arg)
16545 {
16546 htab_delete (arg);
16547 }
16548
16549 /* Create an index file for OBJFILE in the directory DIR. */
16550
16551 static void
16552 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16553 {
16554 struct cleanup *cleanup;
16555 char *filename, *cleanup_filename;
16556 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16557 struct obstack cu_list, types_cu_list;
16558 int i;
16559 FILE *out_file;
16560 struct mapped_symtab *symtab;
16561 offset_type val, size_of_contents, total_len;
16562 struct stat st;
16563 char buf[8];
16564 htab_t psyms_seen;
16565 htab_t cu_index_htab;
16566 struct psymtab_cu_index_map *psymtab_cu_index_map;
16567
16568 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16569 return;
16570
16571 if (dwarf2_per_objfile->using_index)
16572 error (_("Cannot use an index to create the index"));
16573
16574 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16575 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16576
16577 if (stat (objfile->name, &st) < 0)
16578 perror_with_name (objfile->name);
16579
16580 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16581 INDEX_SUFFIX, (char *) NULL);
16582 cleanup = make_cleanup (xfree, filename);
16583
16584 out_file = fopen (filename, "wb");
16585 if (!out_file)
16586 error (_("Can't open `%s' for writing"), filename);
16587
16588 cleanup_filename = filename;
16589 make_cleanup (unlink_if_set, &cleanup_filename);
16590
16591 symtab = create_mapped_symtab ();
16592 make_cleanup (cleanup_mapped_symtab, symtab);
16593
16594 obstack_init (&addr_obstack);
16595 make_cleanup_obstack_free (&addr_obstack);
16596
16597 obstack_init (&cu_list);
16598 make_cleanup_obstack_free (&cu_list);
16599
16600 obstack_init (&types_cu_list);
16601 make_cleanup_obstack_free (&types_cu_list);
16602
16603 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16604 NULL, xcalloc, xfree);
16605 make_cleanup (cleanup_htab, psyms_seen);
16606
16607 /* While we're scanning CU's create a table that maps a psymtab pointer
16608 (which is what addrmap records) to its index (which is what is recorded
16609 in the index file). This will later be needed to write the address
16610 table. */
16611 cu_index_htab = htab_create_alloc (100,
16612 hash_psymtab_cu_index,
16613 eq_psymtab_cu_index,
16614 NULL, xcalloc, xfree);
16615 make_cleanup (cleanup_htab, cu_index_htab);
16616 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16617 xmalloc (sizeof (struct psymtab_cu_index_map)
16618 * dwarf2_per_objfile->n_comp_units);
16619 make_cleanup (xfree, psymtab_cu_index_map);
16620
16621 /* The CU list is already sorted, so we don't need to do additional
16622 work here. Also, the debug_types entries do not appear in
16623 all_comp_units, but only in their own hash table. */
16624 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16625 {
16626 struct dwarf2_per_cu_data *per_cu
16627 = dwarf2_per_objfile->all_comp_units[i];
16628 struct partial_symtab *psymtab = per_cu->v.psymtab;
16629 gdb_byte val[8];
16630 struct psymtab_cu_index_map *map;
16631 void **slot;
16632
16633 write_psymbols (symtab,
16634 psyms_seen,
16635 objfile->global_psymbols.list + psymtab->globals_offset,
16636 psymtab->n_global_syms, i,
16637 0);
16638 write_psymbols (symtab,
16639 psyms_seen,
16640 objfile->static_psymbols.list + psymtab->statics_offset,
16641 psymtab->n_static_syms, i,
16642 1);
16643
16644 map = &psymtab_cu_index_map[i];
16645 map->psymtab = psymtab;
16646 map->cu_index = i;
16647 slot = htab_find_slot (cu_index_htab, map, INSERT);
16648 gdb_assert (slot != NULL);
16649 gdb_assert (*slot == NULL);
16650 *slot = map;
16651
16652 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16653 obstack_grow (&cu_list, val, 8);
16654 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16655 obstack_grow (&cu_list, val, 8);
16656 }
16657
16658 /* Dump the address map. */
16659 write_address_map (objfile, &addr_obstack, cu_index_htab);
16660
16661 /* Write out the .debug_type entries, if any. */
16662 if (dwarf2_per_objfile->signatured_types)
16663 {
16664 struct signatured_type_index_data sig_data;
16665
16666 sig_data.objfile = objfile;
16667 sig_data.symtab = symtab;
16668 sig_data.types_list = &types_cu_list;
16669 sig_data.psyms_seen = psyms_seen;
16670 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16671 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16672 write_one_signatured_type, &sig_data);
16673 }
16674
16675 obstack_init (&constant_pool);
16676 make_cleanup_obstack_free (&constant_pool);
16677 obstack_init (&symtab_obstack);
16678 make_cleanup_obstack_free (&symtab_obstack);
16679 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16680
16681 obstack_init (&contents);
16682 make_cleanup_obstack_free (&contents);
16683 size_of_contents = 6 * sizeof (offset_type);
16684 total_len = size_of_contents;
16685
16686 /* The version number. */
16687 val = MAYBE_SWAP (5);
16688 obstack_grow (&contents, &val, sizeof (val));
16689
16690 /* The offset of the CU list from the start of the file. */
16691 val = MAYBE_SWAP (total_len);
16692 obstack_grow (&contents, &val, sizeof (val));
16693 total_len += obstack_object_size (&cu_list);
16694
16695 /* The offset of the types CU list from the start of the file. */
16696 val = MAYBE_SWAP (total_len);
16697 obstack_grow (&contents, &val, sizeof (val));
16698 total_len += obstack_object_size (&types_cu_list);
16699
16700 /* The offset of the address table from the start of the file. */
16701 val = MAYBE_SWAP (total_len);
16702 obstack_grow (&contents, &val, sizeof (val));
16703 total_len += obstack_object_size (&addr_obstack);
16704
16705 /* The offset of the symbol table from the start of the file. */
16706 val = MAYBE_SWAP (total_len);
16707 obstack_grow (&contents, &val, sizeof (val));
16708 total_len += obstack_object_size (&symtab_obstack);
16709
16710 /* The offset of the constant pool from the start of the file. */
16711 val = MAYBE_SWAP (total_len);
16712 obstack_grow (&contents, &val, sizeof (val));
16713 total_len += obstack_object_size (&constant_pool);
16714
16715 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16716
16717 write_obstack (out_file, &contents);
16718 write_obstack (out_file, &cu_list);
16719 write_obstack (out_file, &types_cu_list);
16720 write_obstack (out_file, &addr_obstack);
16721 write_obstack (out_file, &symtab_obstack);
16722 write_obstack (out_file, &constant_pool);
16723
16724 fclose (out_file);
16725
16726 /* We want to keep the file, so we set cleanup_filename to NULL
16727 here. See unlink_if_set. */
16728 cleanup_filename = NULL;
16729
16730 do_cleanups (cleanup);
16731 }
16732
16733 /* Implementation of the `save gdb-index' command.
16734
16735 Note that the file format used by this command is documented in the
16736 GDB manual. Any changes here must be documented there. */
16737
16738 static void
16739 save_gdb_index_command (char *arg, int from_tty)
16740 {
16741 struct objfile *objfile;
16742
16743 if (!arg || !*arg)
16744 error (_("usage: save gdb-index DIRECTORY"));
16745
16746 ALL_OBJFILES (objfile)
16747 {
16748 struct stat st;
16749
16750 /* If the objfile does not correspond to an actual file, skip it. */
16751 if (stat (objfile->name, &st) < 0)
16752 continue;
16753
16754 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16755 if (dwarf2_per_objfile)
16756 {
16757 volatile struct gdb_exception except;
16758
16759 TRY_CATCH (except, RETURN_MASK_ERROR)
16760 {
16761 write_psymtabs_to_index (objfile, arg);
16762 }
16763 if (except.reason < 0)
16764 exception_fprintf (gdb_stderr, except,
16765 _("Error while writing index for `%s': "),
16766 objfile->name);
16767 }
16768 }
16769 }
16770
16771 \f
16772
16773 int dwarf2_always_disassemble;
16774
16775 static void
16776 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16777 struct cmd_list_element *c, const char *value)
16778 {
16779 fprintf_filtered (file,
16780 _("Whether to always disassemble "
16781 "DWARF expressions is %s.\n"),
16782 value);
16783 }
16784
16785 static void
16786 show_check_physname (struct ui_file *file, int from_tty,
16787 struct cmd_list_element *c, const char *value)
16788 {
16789 fprintf_filtered (file,
16790 _("Whether to check \"physname\" is %s.\n"),
16791 value);
16792 }
16793
16794 void _initialize_dwarf2_read (void);
16795
16796 void
16797 _initialize_dwarf2_read (void)
16798 {
16799 struct cmd_list_element *c;
16800
16801 dwarf2_objfile_data_key
16802 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16803
16804 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16805 Set DWARF 2 specific variables.\n\
16806 Configure DWARF 2 variables such as the cache size"),
16807 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16808 0/*allow-unknown*/, &maintenance_set_cmdlist);
16809
16810 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16811 Show DWARF 2 specific variables\n\
16812 Show DWARF 2 variables such as the cache size"),
16813 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16814 0/*allow-unknown*/, &maintenance_show_cmdlist);
16815
16816 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16817 &dwarf2_max_cache_age, _("\
16818 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16819 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16820 A higher limit means that cached compilation units will be stored\n\
16821 in memory longer, and more total memory will be used. Zero disables\n\
16822 caching, which can slow down startup."),
16823 NULL,
16824 show_dwarf2_max_cache_age,
16825 &set_dwarf2_cmdlist,
16826 &show_dwarf2_cmdlist);
16827
16828 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16829 &dwarf2_always_disassemble, _("\
16830 Set whether `info address' always disassembles DWARF expressions."), _("\
16831 Show whether `info address' always disassembles DWARF expressions."), _("\
16832 When enabled, DWARF expressions are always printed in an assembly-like\n\
16833 syntax. When disabled, expressions will be printed in a more\n\
16834 conversational style, when possible."),
16835 NULL,
16836 show_dwarf2_always_disassemble,
16837 &set_dwarf2_cmdlist,
16838 &show_dwarf2_cmdlist);
16839
16840 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16841 Set debugging of the dwarf2 DIE reader."), _("\
16842 Show debugging of the dwarf2 DIE reader."), _("\
16843 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16844 The value is the maximum depth to print."),
16845 NULL,
16846 NULL,
16847 &setdebuglist, &showdebuglist);
16848
16849 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16850 Set cross-checking of \"physname\" code against demangler."), _("\
16851 Show cross-checking of \"physname\" code against demangler."), _("\
16852 When enabled, GDB's internal \"physname\" code is checked against\n\
16853 the demangler."),
16854 NULL, show_check_physname,
16855 &setdebuglist, &showdebuglist);
16856
16857 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16858 _("\
16859 Save a gdb-index file.\n\
16860 Usage: save gdb-index DIRECTORY"),
16861 &save_cmdlist);
16862 set_cmd_completer (c, filename_completer);
16863 }
This page took 0.374629 seconds and 4 git commands to generate.