gdb/
[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 23
276 };
277
278 /* local data types */
279
280 /* We hold several abbreviation tables in memory at the same time. */
281 #ifndef ABBREV_HASH_SIZE
282 #define ABBREV_HASH_SIZE 121
283 #endif
284
285 /* The data in a compilation unit header, after target2host
286 translation, looks like this. */
287 struct comp_unit_head
288 {
289 unsigned int length;
290 short version;
291 unsigned char addr_size;
292 unsigned char signed_addr_p;
293 unsigned int abbrev_offset;
294
295 /* Size of file offsets; either 4 or 8. */
296 unsigned int offset_size;
297
298 /* Size of the length field; either 4 or 12. */
299 unsigned int initial_length_size;
300
301 /* Offset to the first byte of this compilation unit header in the
302 .debug_info section, for resolving relative reference dies. */
303 unsigned int offset;
304
305 /* Offset to first die in this cu from the start of the cu.
306 This will be the first byte following the compilation unit header. */
307 unsigned int first_die_offset;
308 };
309
310 /* Type used for delaying computation of method physnames.
311 See comments for compute_delayed_physnames. */
312 struct delayed_method_info
313 {
314 /* The type to which the method is attached, i.e., its parent class. */
315 struct type *type;
316
317 /* The index of the method in the type's function fieldlists. */
318 int fnfield_index;
319
320 /* The index of the method in the fieldlist. */
321 int index;
322
323 /* The name of the DIE. */
324 const char *name;
325
326 /* The DIE associated with this method. */
327 struct die_info *die;
328 };
329
330 typedef struct delayed_method_info delayed_method_info;
331 DEF_VEC_O (delayed_method_info);
332
333 /* Internal state when decoding a particular compilation unit. */
334 struct dwarf2_cu
335 {
336 /* The objfile containing this compilation unit. */
337 struct objfile *objfile;
338
339 /* The header of the compilation unit. */
340 struct comp_unit_head header;
341
342 /* Base address of this compilation unit. */
343 CORE_ADDR base_address;
344
345 /* Non-zero if base_address has been set. */
346 int base_known;
347
348 struct function_range *first_fn, *last_fn, *cached_fn;
349
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
354 const char *producer;
355
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
372
373 /* Hash table holding all the loaded partial DIEs. */
374 htab_t partial_dies;
375
376 /* Storage for things with the same lifetime as this read-in compilation
377 unit, including partial DIEs. */
378 struct obstack comp_unit_obstack;
379
380 /* When multiple dwarf2_cu structures are living in memory, this field
381 chains them all together, so that they can be released efficiently.
382 We will probably also want a generation counter so that most-recently-used
383 compilation units are cached... */
384 struct dwarf2_per_cu_data *read_in_chain;
385
386 /* Backchain to our per_cu entry if the tree has been built. */
387 struct dwarf2_per_cu_data *per_cu;
388
389 /* How many compilation units ago was this CU last referenced? */
390 int last_used;
391
392 /* A hash table of die offsets for following references. */
393 htab_t die_hash;
394
395 /* Full DIEs if read in. */
396 struct die_info *dies;
397
398 /* A set of pointers to dwarf2_per_cu_data objects for compilation
399 units referenced by this one. Only set during full symbol processing;
400 partial symbol tables do not have dependencies. */
401 htab_t dependencies;
402
403 /* Header data from the line table, during full symbol processing. */
404 struct line_header *line_header;
405
406 /* A list of methods which need to have physnames computed
407 after all type information has been read. */
408 VEC (delayed_method_info) *method_list;
409
410 /* To be copied to symtab->call_site_htab. */
411 htab_t call_site_htab;
412
413 /* Mark used when releasing cached dies. */
414 unsigned int mark : 1;
415
416 /* This flag will be set if this compilation unit might include
417 inter-compilation-unit references. */
418 unsigned int has_form_ref_addr : 1;
419
420 /* This flag will be set if this compilation unit includes any
421 DW_TAG_namespace DIEs. If we know that there are explicit
422 DIEs for namespaces, we don't need to try to infer them
423 from mangled names. */
424 unsigned int has_namespace_info : 1;
425
426 /* This CU references .debug_loc. See the symtab->locations_valid field.
427 This test is imperfect as there may exist optimized debug code not using
428 any location list and still facing inlining issues if handled as
429 unoptimized code. For a future better test see GCC PR other/32998. */
430
431 unsigned int has_loclist : 1;
432 };
433
434 /* Persistent data held for a compilation unit, even when not
435 processing it. We put a pointer to this structure in the
436 read_symtab_private field of the psymtab. If we encounter
437 inter-compilation-unit references, we also maintain a sorted
438 list of all compilation units. */
439
440 struct dwarf2_per_cu_data
441 {
442 /* The start offset and length of this compilation unit. 2**29-1
443 bytes should suffice to store the length of any compilation unit
444 - if it doesn't, GDB will fall over anyway.
445 NOTE: Unlike comp_unit_head.length, this length includes
446 initial_length_size. */
447 unsigned int offset;
448 unsigned int length : 29;
449
450 /* Flag indicating this compilation unit will be read in before
451 any of the current compilation units are processed. */
452 unsigned int queued : 1;
453
454 /* This flag will be set if we need to load absolutely all DIEs
455 for this compilation unit, instead of just the ones we think
456 are interesting. It gets set if we look for a DIE in the
457 hash table and don't find it. */
458 unsigned int load_all_dies : 1;
459
460 /* Non-null if this CU is from .debug_types; in which case it points
461 to the section. Otherwise it's from .debug_info. */
462 struct dwarf2_section_info *debug_type_section;
463
464 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
465 of the CU cache it gets reset to NULL again. */
466 struct dwarf2_cu *cu;
467
468 /* The corresponding objfile. */
469 struct objfile *objfile;
470
471 /* When using partial symbol tables, the 'psymtab' field is active.
472 Otherwise the 'quick' field is active. */
473 union
474 {
475 /* The partial symbol table associated with this compilation unit,
476 or NULL for partial units (which do not have an associated
477 symtab). */
478 struct partial_symtab *psymtab;
479
480 /* Data needed by the "quick" functions. */
481 struct dwarf2_per_cu_quick_data *quick;
482 } v;
483 };
484
485 /* Entry in the signatured_types hash table. */
486
487 struct signatured_type
488 {
489 ULONGEST signature;
490
491 /* Offset in .debug_types of the type defined by this TU. */
492 unsigned int type_offset;
493
494 /* The CU(/TU) of this type. */
495 struct dwarf2_per_cu_data per_cu;
496 };
497
498 /* Struct used to pass misc. parameters to read_die_and_children, et
499 al. which are used for both .debug_info and .debug_types dies.
500 All parameters here are unchanging for the life of the call. This
501 struct exists to abstract away the constant parameters of die
502 reading. */
503
504 struct die_reader_specs
505 {
506 /* The bfd of this objfile. */
507 bfd* abfd;
508
509 /* The CU of the DIE we are parsing. */
510 struct dwarf2_cu *cu;
511
512 /* Pointer to start of section buffer.
513 This is either the start of .debug_info or .debug_types. */
514 const gdb_byte *buffer;
515 };
516
517 /* The line number information for a compilation unit (found in the
518 .debug_line section) begins with a "statement program header",
519 which contains the following information. */
520 struct line_header
521 {
522 unsigned int total_length;
523 unsigned short version;
524 unsigned int header_length;
525 unsigned char minimum_instruction_length;
526 unsigned char maximum_ops_per_instruction;
527 unsigned char default_is_stmt;
528 int line_base;
529 unsigned char line_range;
530 unsigned char opcode_base;
531
532 /* standard_opcode_lengths[i] is the number of operands for the
533 standard opcode whose value is i. This means that
534 standard_opcode_lengths[0] is unused, and the last meaningful
535 element is standard_opcode_lengths[opcode_base - 1]. */
536 unsigned char *standard_opcode_lengths;
537
538 /* The include_directories table. NOTE! These strings are not
539 allocated with xmalloc; instead, they are pointers into
540 debug_line_buffer. If you try to free them, `free' will get
541 indigestion. */
542 unsigned int num_include_dirs, include_dirs_size;
543 char **include_dirs;
544
545 /* The file_names table. NOTE! These strings are not allocated
546 with xmalloc; instead, they are pointers into debug_line_buffer.
547 Don't try to free them directly. */
548 unsigned int num_file_names, file_names_size;
549 struct file_entry
550 {
551 char *name;
552 unsigned int dir_index;
553 unsigned int mod_time;
554 unsigned int length;
555 int included_p; /* Non-zero if referenced by the Line Number Program. */
556 struct symtab *symtab; /* The associated symbol table, if any. */
557 } *file_names;
558
559 /* The start and end of the statement program following this
560 header. These point into dwarf2_per_objfile->line_buffer. */
561 gdb_byte *statement_program_start, *statement_program_end;
562 };
563
564 /* When we construct a partial symbol table entry we only
565 need this much information. */
566 struct partial_die_info
567 {
568 /* Offset of this DIE. */
569 unsigned int offset;
570
571 /* DWARF-2 tag for this DIE. */
572 ENUM_BITFIELD(dwarf_tag) tag : 16;
573
574 /* Assorted flags describing the data found in this DIE. */
575 unsigned int has_children : 1;
576 unsigned int is_external : 1;
577 unsigned int is_declaration : 1;
578 unsigned int has_type : 1;
579 unsigned int has_specification : 1;
580 unsigned int has_pc_info : 1;
581
582 /* Flag set if the SCOPE field of this structure has been
583 computed. */
584 unsigned int scope_set : 1;
585
586 /* Flag set if the DIE has a byte_size attribute. */
587 unsigned int has_byte_size : 1;
588
589 /* Flag set if any of the DIE's children are template arguments. */
590 unsigned int has_template_arguments : 1;
591
592 /* Flag set if fixup_partial_die has been called on this die. */
593 unsigned int fixup_called : 1;
594
595 /* The name of this DIE. Normally the value of DW_AT_name, but
596 sometimes a default name for unnamed DIEs. */
597 char *name;
598
599 /* The linkage name, if present. */
600 const char *linkage_name;
601
602 /* The scope to prepend to our children. This is generally
603 allocated on the comp_unit_obstack, so will disappear
604 when this compilation unit leaves the cache. */
605 char *scope;
606
607 /* The location description associated with this DIE, if any. */
608 struct dwarf_block *locdesc;
609
610 /* If HAS_PC_INFO, the PC range associated with this DIE. */
611 CORE_ADDR lowpc;
612 CORE_ADDR highpc;
613
614 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
615 DW_AT_sibling, if any. */
616 /* NOTE: This member isn't strictly necessary, read_partial_die could
617 return DW_AT_sibling values to its caller load_partial_dies. */
618 gdb_byte *sibling;
619
620 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
621 DW_AT_specification (or DW_AT_abstract_origin or
622 DW_AT_extension). */
623 unsigned int spec_offset;
624
625 /* Pointers to this DIE's parent, first child, and next sibling,
626 if any. */
627 struct partial_die_info *die_parent, *die_child, *die_sibling;
628 };
629
630 /* This data structure holds the information of an abbrev. */
631 struct abbrev_info
632 {
633 unsigned int number; /* number identifying abbrev */
634 enum dwarf_tag tag; /* dwarf tag */
635 unsigned short has_children; /* boolean */
636 unsigned short num_attrs; /* number of attributes */
637 struct attr_abbrev *attrs; /* an array of attribute descriptions */
638 struct abbrev_info *next; /* next in chain */
639 };
640
641 struct attr_abbrev
642 {
643 ENUM_BITFIELD(dwarf_attribute) name : 16;
644 ENUM_BITFIELD(dwarf_form) form : 16;
645 };
646
647 /* Attributes have a name and a value. */
648 struct attribute
649 {
650 ENUM_BITFIELD(dwarf_attribute) name : 16;
651 ENUM_BITFIELD(dwarf_form) form : 15;
652
653 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
654 field should be in u.str (existing only for DW_STRING) but it is kept
655 here for better struct attribute alignment. */
656 unsigned int string_is_canonical : 1;
657
658 union
659 {
660 char *str;
661 struct dwarf_block *blk;
662 ULONGEST unsnd;
663 LONGEST snd;
664 CORE_ADDR addr;
665 struct signatured_type *signatured_type;
666 }
667 u;
668 };
669
670 /* This data structure holds a complete die structure. */
671 struct die_info
672 {
673 /* DWARF-2 tag for this DIE. */
674 ENUM_BITFIELD(dwarf_tag) tag : 16;
675
676 /* Number of attributes */
677 unsigned char num_attrs;
678
679 /* True if we're presently building the full type name for the
680 type derived from this DIE. */
681 unsigned char building_fullname : 1;
682
683 /* Abbrev number */
684 unsigned int abbrev;
685
686 /* Offset in .debug_info or .debug_types section. */
687 unsigned int offset;
688
689 /* The dies in a compilation unit form an n-ary tree. PARENT
690 points to this die's parent; CHILD points to the first child of
691 this node; and all the children of a given node are chained
692 together via their SIBLING fields. */
693 struct die_info *child; /* Its first child, if any. */
694 struct die_info *sibling; /* Its next sibling, if any. */
695 struct die_info *parent; /* Its parent, if any. */
696
697 /* An array of attributes, with NUM_ATTRS elements. There may be
698 zero, but it's not common and zero-sized arrays are not
699 sufficiently portable C. */
700 struct attribute attrs[1];
701 };
702
703 struct function_range
704 {
705 const char *name;
706 CORE_ADDR lowpc, highpc;
707 int seen_line;
708 struct function_range *next;
709 };
710
711 /* Get at parts of an attribute structure. */
712
713 #define DW_STRING(attr) ((attr)->u.str)
714 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
715 #define DW_UNSND(attr) ((attr)->u.unsnd)
716 #define DW_BLOCK(attr) ((attr)->u.blk)
717 #define DW_SND(attr) ((attr)->u.snd)
718 #define DW_ADDR(attr) ((attr)->u.addr)
719 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
720
721 /* Blocks are a bunch of untyped bytes. */
722 struct dwarf_block
723 {
724 unsigned int size;
725 gdb_byte *data;
726 };
727
728 #ifndef ATTR_ALLOC_CHUNK
729 #define ATTR_ALLOC_CHUNK 4
730 #endif
731
732 /* Allocate fields for structs, unions and enums in this size. */
733 #ifndef DW_FIELD_ALLOC_CHUNK
734 #define DW_FIELD_ALLOC_CHUNK 4
735 #endif
736
737 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
738 but this would require a corresponding change in unpack_field_as_long
739 and friends. */
740 static int bits_per_byte = 8;
741
742 /* The routines that read and process dies for a C struct or C++ class
743 pass lists of data member fields and lists of member function fields
744 in an instance of a field_info structure, as defined below. */
745 struct field_info
746 {
747 /* List of data member and baseclasses fields. */
748 struct nextfield
749 {
750 struct nextfield *next;
751 int accessibility;
752 int virtuality;
753 struct field field;
754 }
755 *fields, *baseclasses;
756
757 /* Number of fields (including baseclasses). */
758 int nfields;
759
760 /* Number of baseclasses. */
761 int nbaseclasses;
762
763 /* Set if the accesibility of one of the fields is not public. */
764 int non_public_fields;
765
766 /* Member function fields array, entries are allocated in the order they
767 are encountered in the object file. */
768 struct nextfnfield
769 {
770 struct nextfnfield *next;
771 struct fn_field fnfield;
772 }
773 *fnfields;
774
775 /* Member function fieldlist array, contains name of possibly overloaded
776 member function, number of overloaded member functions and a pointer
777 to the head of the member function field chain. */
778 struct fnfieldlist
779 {
780 char *name;
781 int length;
782 struct nextfnfield *head;
783 }
784 *fnfieldlists;
785
786 /* Number of entries in the fnfieldlists array. */
787 int nfnfields;
788
789 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
790 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
791 struct typedef_field_list
792 {
793 struct typedef_field field;
794 struct typedef_field_list *next;
795 }
796 *typedef_field_list;
797 unsigned typedef_field_list_count;
798 };
799
800 /* One item on the queue of compilation units to read in full symbols
801 for. */
802 struct dwarf2_queue_item
803 {
804 struct dwarf2_per_cu_data *per_cu;
805 struct dwarf2_queue_item *next;
806 };
807
808 /* The current queue. */
809 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
810
811 /* Loaded secondary compilation units are kept in memory until they
812 have not been referenced for the processing of this many
813 compilation units. Set this to zero to disable caching. Cache
814 sizes of up to at least twenty will improve startup time for
815 typical inter-CU-reference binaries, at an obvious memory cost. */
816 static int dwarf2_max_cache_age = 5;
817 static void
818 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
819 struct cmd_list_element *c, const char *value)
820 {
821 fprintf_filtered (file, _("The upper bound on the age of cached "
822 "dwarf2 compilation units is %s.\n"),
823 value);
824 }
825
826
827 /* Various complaints about symbol reading that don't abort the process. */
828
829 static void
830 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
831 {
832 complaint (&symfile_complaints,
833 _("statement list doesn't fit in .debug_line section"));
834 }
835
836 static void
837 dwarf2_debug_line_missing_file_complaint (void)
838 {
839 complaint (&symfile_complaints,
840 _(".debug_line section has line data without a file"));
841 }
842
843 static void
844 dwarf2_debug_line_missing_end_sequence_complaint (void)
845 {
846 complaint (&symfile_complaints,
847 _(".debug_line section has line "
848 "program sequence without an end"));
849 }
850
851 static void
852 dwarf2_complex_location_expr_complaint (void)
853 {
854 complaint (&symfile_complaints, _("location expression too complex"));
855 }
856
857 static void
858 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
859 int arg3)
860 {
861 complaint (&symfile_complaints,
862 _("const value length mismatch for '%s', got %d, expected %d"),
863 arg1, arg2, arg3);
864 }
865
866 static void
867 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
868 {
869 complaint (&symfile_complaints,
870 _("macro info runs off end of `%s' section"),
871 section->asection->name);
872 }
873
874 static void
875 dwarf2_macro_malformed_definition_complaint (const char *arg1)
876 {
877 complaint (&symfile_complaints,
878 _("macro debug info contains a "
879 "malformed macro definition:\n`%s'"),
880 arg1);
881 }
882
883 static void
884 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
885 {
886 complaint (&symfile_complaints,
887 _("invalid attribute class or form for '%s' in '%s'"),
888 arg1, arg2);
889 }
890
891 /* local function prototypes */
892
893 static void dwarf2_locate_sections (bfd *, asection *, void *);
894
895 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
896 struct objfile *);
897
898 static void dwarf2_find_base_address (struct die_info *die,
899 struct dwarf2_cu *cu);
900
901 static void dwarf2_build_psymtabs_hard (struct objfile *);
902
903 static void scan_partial_symbols (struct partial_die_info *,
904 CORE_ADDR *, CORE_ADDR *,
905 int, struct dwarf2_cu *);
906
907 static void add_partial_symbol (struct partial_die_info *,
908 struct dwarf2_cu *);
909
910 static void add_partial_namespace (struct partial_die_info *pdi,
911 CORE_ADDR *lowpc, CORE_ADDR *highpc,
912 int need_pc, struct dwarf2_cu *cu);
913
914 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
915 CORE_ADDR *highpc, int need_pc,
916 struct dwarf2_cu *cu);
917
918 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
919 struct dwarf2_cu *cu);
920
921 static void add_partial_subprogram (struct partial_die_info *pdi,
922 CORE_ADDR *lowpc, CORE_ADDR *highpc,
923 int need_pc, struct dwarf2_cu *cu);
924
925 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
926 gdb_byte *buffer, gdb_byte *info_ptr,
927 bfd *abfd, struct dwarf2_cu *cu);
928
929 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
930
931 static void psymtab_to_symtab_1 (struct partial_symtab *);
932
933 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
934
935 static void dwarf2_free_abbrev_table (void *);
936
937 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
938 struct dwarf2_cu *);
939
940 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
941 struct dwarf2_cu *);
942
943 static struct partial_die_info *load_partial_dies (bfd *,
944 gdb_byte *, gdb_byte *,
945 int, struct dwarf2_cu *);
946
947 static gdb_byte *read_partial_die (struct partial_die_info *,
948 struct abbrev_info *abbrev,
949 unsigned int, bfd *,
950 gdb_byte *, gdb_byte *,
951 struct dwarf2_cu *);
952
953 static struct partial_die_info *find_partial_die (unsigned int,
954 struct dwarf2_cu *);
955
956 static void fixup_partial_die (struct partial_die_info *,
957 struct dwarf2_cu *);
958
959 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
960 bfd *, gdb_byte *, struct dwarf2_cu *);
961
962 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
963 bfd *, gdb_byte *, struct dwarf2_cu *);
964
965 static unsigned int read_1_byte (bfd *, gdb_byte *);
966
967 static int read_1_signed_byte (bfd *, gdb_byte *);
968
969 static unsigned int read_2_bytes (bfd *, gdb_byte *);
970
971 static unsigned int read_4_bytes (bfd *, gdb_byte *);
972
973 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
974
975 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
976 unsigned int *);
977
978 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
979
980 static LONGEST read_checked_initial_length_and_offset
981 (bfd *, gdb_byte *, const struct comp_unit_head *,
982 unsigned int *, unsigned int *);
983
984 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
985 unsigned int *);
986
987 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
988
989 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
990
991 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
992
993 static char *read_indirect_string (bfd *, gdb_byte *,
994 const struct comp_unit_head *,
995 unsigned int *);
996
997 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
998
999 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1000
1001 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1002
1003 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1004
1005 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1006 struct dwarf2_cu *);
1007
1008 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1009 unsigned int,
1010 struct dwarf2_cu *);
1011
1012 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1013 struct dwarf2_cu *cu);
1014
1015 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1016
1017 static struct die_info *die_specification (struct die_info *die,
1018 struct dwarf2_cu **);
1019
1020 static void free_line_header (struct line_header *lh);
1021
1022 static void add_file_name (struct line_header *, char *, unsigned int,
1023 unsigned int, unsigned int);
1024
1025 static struct line_header *(dwarf_decode_line_header
1026 (unsigned int offset,
1027 bfd *abfd, struct dwarf2_cu *cu));
1028
1029 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1030 struct dwarf2_cu *, struct partial_symtab *);
1031
1032 static void dwarf2_start_subfile (char *, const char *, const char *);
1033
1034 static struct symbol *new_symbol (struct die_info *, struct type *,
1035 struct dwarf2_cu *);
1036
1037 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1038 struct dwarf2_cu *, struct symbol *);
1039
1040 static void dwarf2_const_value (struct attribute *, struct symbol *,
1041 struct dwarf2_cu *);
1042
1043 static void dwarf2_const_value_attr (struct attribute *attr,
1044 struct type *type,
1045 const char *name,
1046 struct obstack *obstack,
1047 struct dwarf2_cu *cu, long *value,
1048 gdb_byte **bytes,
1049 struct dwarf2_locexpr_baton **baton);
1050
1051 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1052
1053 static int need_gnat_info (struct dwarf2_cu *);
1054
1055 static struct type *die_descriptive_type (struct die_info *,
1056 struct dwarf2_cu *);
1057
1058 static void set_descriptive_type (struct type *, struct die_info *,
1059 struct dwarf2_cu *);
1060
1061 static struct type *die_containing_type (struct die_info *,
1062 struct dwarf2_cu *);
1063
1064 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1065 struct dwarf2_cu *);
1066
1067 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1068
1069 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1070
1071 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1072
1073 static char *typename_concat (struct obstack *obs, const char *prefix,
1074 const char *suffix, int physname,
1075 struct dwarf2_cu *cu);
1076
1077 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1078
1079 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1080
1081 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1082
1083 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1084
1085 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1086
1087 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1088 struct dwarf2_cu *, struct partial_symtab *);
1089
1090 static int dwarf2_get_pc_bounds (struct die_info *,
1091 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1092 struct partial_symtab *);
1093
1094 static void get_scope_pc_bounds (struct die_info *,
1095 CORE_ADDR *, CORE_ADDR *,
1096 struct dwarf2_cu *);
1097
1098 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1099 CORE_ADDR, struct dwarf2_cu *);
1100
1101 static void dwarf2_add_field (struct field_info *, struct die_info *,
1102 struct dwarf2_cu *);
1103
1104 static void dwarf2_attach_fields_to_type (struct field_info *,
1105 struct type *, struct dwarf2_cu *);
1106
1107 static void dwarf2_add_member_fn (struct field_info *,
1108 struct die_info *, struct type *,
1109 struct dwarf2_cu *);
1110
1111 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1112 struct type *,
1113 struct dwarf2_cu *);
1114
1115 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1116
1117 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1118
1119 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1120
1121 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1122
1123 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1124
1125 static struct type *read_module_type (struct die_info *die,
1126 struct dwarf2_cu *cu);
1127
1128 static const char *namespace_name (struct die_info *die,
1129 int *is_anonymous, struct dwarf2_cu *);
1130
1131 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1132
1133 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1134
1135 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1136 struct dwarf2_cu *);
1137
1138 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1139
1140 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1141 gdb_byte *info_ptr,
1142 gdb_byte **new_info_ptr,
1143 struct die_info *parent);
1144
1145 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1146 gdb_byte *info_ptr,
1147 gdb_byte **new_info_ptr,
1148 struct die_info *parent);
1149
1150 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1151 gdb_byte *info_ptr,
1152 gdb_byte **new_info_ptr,
1153 struct die_info *parent);
1154
1155 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1156 struct die_info **, gdb_byte *,
1157 int *);
1158
1159 static void process_die (struct die_info *, struct dwarf2_cu *);
1160
1161 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1162 struct obstack *);
1163
1164 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1165
1166 static const char *dwarf2_full_name (char *name,
1167 struct die_info *die,
1168 struct dwarf2_cu *cu);
1169
1170 static struct die_info *dwarf2_extension (struct die_info *die,
1171 struct dwarf2_cu **);
1172
1173 static char *dwarf_tag_name (unsigned int);
1174
1175 static char *dwarf_attr_name (unsigned int);
1176
1177 static char *dwarf_form_name (unsigned int);
1178
1179 static char *dwarf_bool_name (unsigned int);
1180
1181 static char *dwarf_type_encoding_name (unsigned int);
1182
1183 #if 0
1184 static char *dwarf_cfi_name (unsigned int);
1185 #endif
1186
1187 static struct die_info *sibling_die (struct die_info *);
1188
1189 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1190
1191 static void dump_die_for_error (struct die_info *);
1192
1193 static void dump_die_1 (struct ui_file *, int level, int max_level,
1194 struct die_info *);
1195
1196 /*static*/ void dump_die (struct die_info *, int max_level);
1197
1198 static void store_in_ref_table (struct die_info *,
1199 struct dwarf2_cu *);
1200
1201 static int is_ref_attr (struct attribute *);
1202
1203 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1204
1205 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1206
1207 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1208 struct attribute *,
1209 struct dwarf2_cu **);
1210
1211 static struct die_info *follow_die_ref (struct die_info *,
1212 struct attribute *,
1213 struct dwarf2_cu **);
1214
1215 static struct die_info *follow_die_sig (struct die_info *,
1216 struct attribute *,
1217 struct dwarf2_cu **);
1218
1219 static struct signatured_type *lookup_signatured_type_at_offset
1220 (struct objfile *objfile,
1221 struct dwarf2_section_info *section,
1222 unsigned int offset);
1223
1224 static void read_signatured_type_at_offset (struct objfile *objfile,
1225 struct dwarf2_section_info *sect,
1226 unsigned int offset);
1227
1228 static void read_signatured_type (struct objfile *,
1229 struct signatured_type *type_sig);
1230
1231 /* memory allocation interface */
1232
1233 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1234
1235 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1236
1237 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1238
1239 static void initialize_cu_func_list (struct dwarf2_cu *);
1240
1241 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1242 struct dwarf2_cu *);
1243
1244 static void dwarf_decode_macros (struct line_header *, unsigned int,
1245 char *, bfd *, struct dwarf2_cu *,
1246 struct dwarf2_section_info *,
1247 int);
1248
1249 static int attr_form_is_block (struct attribute *);
1250
1251 static int attr_form_is_section_offset (struct attribute *);
1252
1253 static int attr_form_is_constant (struct attribute *);
1254
1255 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1256 struct dwarf2_loclist_baton *baton,
1257 struct attribute *attr);
1258
1259 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1260 struct symbol *sym,
1261 struct dwarf2_cu *cu);
1262
1263 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1264 struct abbrev_info *abbrev,
1265 struct dwarf2_cu *cu);
1266
1267 static void free_stack_comp_unit (void *);
1268
1269 static hashval_t partial_die_hash (const void *item);
1270
1271 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1272
1273 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1274 (unsigned int offset, struct objfile *objfile);
1275
1276 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1277 (unsigned int offset, struct objfile *objfile);
1278
1279 static void init_one_comp_unit (struct dwarf2_cu *cu,
1280 struct objfile *objfile);
1281
1282 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1283 struct die_info *comp_unit_die);
1284
1285 static void free_one_comp_unit (void *);
1286
1287 static void free_cached_comp_units (void *);
1288
1289 static void age_cached_comp_units (void);
1290
1291 static void free_one_cached_comp_unit (void *);
1292
1293 static struct type *set_die_type (struct die_info *, struct type *,
1294 struct dwarf2_cu *);
1295
1296 static void create_all_comp_units (struct objfile *);
1297
1298 static int create_debug_types_hash_table (struct objfile *objfile);
1299
1300 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1301 struct objfile *);
1302
1303 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1304
1305 static void dwarf2_add_dependence (struct dwarf2_cu *,
1306 struct dwarf2_per_cu_data *);
1307
1308 static void dwarf2_mark (struct dwarf2_cu *);
1309
1310 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1311
1312 static struct type *get_die_type_at_offset (unsigned int,
1313 struct dwarf2_per_cu_data *per_cu);
1314
1315 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1316
1317 static void dwarf2_release_queue (void *dummy);
1318
1319 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1320 struct objfile *objfile);
1321
1322 static void process_queue (struct objfile *objfile);
1323
1324 static void find_file_and_directory (struct die_info *die,
1325 struct dwarf2_cu *cu,
1326 char **name, char **comp_dir);
1327
1328 static char *file_full_name (int file, struct line_header *lh,
1329 const char *comp_dir);
1330
1331 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1332 gdb_byte *info_ptr,
1333 gdb_byte *buffer,
1334 unsigned int buffer_size,
1335 bfd *abfd);
1336
1337 static void init_cu_die_reader (struct die_reader_specs *reader,
1338 struct dwarf2_cu *cu);
1339
1340 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1341
1342 #if WORDS_BIGENDIAN
1343
1344 /* Convert VALUE between big- and little-endian. */
1345 static offset_type
1346 byte_swap (offset_type value)
1347 {
1348 offset_type result;
1349
1350 result = (value & 0xff) << 24;
1351 result |= (value & 0xff00) << 8;
1352 result |= (value & 0xff0000) >> 8;
1353 result |= (value & 0xff000000) >> 24;
1354 return result;
1355 }
1356
1357 #define MAYBE_SWAP(V) byte_swap (V)
1358
1359 #else
1360 #define MAYBE_SWAP(V) (V)
1361 #endif /* WORDS_BIGENDIAN */
1362
1363 /* The suffix for an index file. */
1364 #define INDEX_SUFFIX ".gdb-index"
1365
1366 static const char *dwarf2_physname (char *name, struct die_info *die,
1367 struct dwarf2_cu *cu);
1368
1369 /* Try to locate the sections we need for DWARF 2 debugging
1370 information and return true if we have enough to do something.
1371 NAMES points to the dwarf2 section names, or is NULL if the standard
1372 ELF names are used. */
1373
1374 int
1375 dwarf2_has_info (struct objfile *objfile,
1376 const struct dwarf2_debug_sections *names)
1377 {
1378 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1379 if (!dwarf2_per_objfile)
1380 {
1381 /* Initialize per-objfile state. */
1382 struct dwarf2_per_objfile *data
1383 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1384
1385 memset (data, 0, sizeof (*data));
1386 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1387 dwarf2_per_objfile = data;
1388
1389 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1390 (void *) names);
1391 dwarf2_per_objfile->objfile = objfile;
1392 }
1393 return (dwarf2_per_objfile->info.asection != NULL
1394 && dwarf2_per_objfile->abbrev.asection != NULL);
1395 }
1396
1397 /* When loading sections, we look either for uncompressed section or for
1398 compressed section names. */
1399
1400 static int
1401 section_is_p (const char *section_name,
1402 const struct dwarf2_section_names *names)
1403 {
1404 if (names->normal != NULL
1405 && strcmp (section_name, names->normal) == 0)
1406 return 1;
1407 if (names->compressed != NULL
1408 && strcmp (section_name, names->compressed) == 0)
1409 return 1;
1410 return 0;
1411 }
1412
1413 /* This function is mapped across the sections and remembers the
1414 offset and size of each of the debugging sections we are interested
1415 in. */
1416
1417 static void
1418 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1419 {
1420 const struct dwarf2_debug_sections *names;
1421
1422 if (vnames == NULL)
1423 names = &dwarf2_elf_names;
1424 else
1425 names = (const struct dwarf2_debug_sections *) vnames;
1426
1427 if (section_is_p (sectp->name, &names->info))
1428 {
1429 dwarf2_per_objfile->info.asection = sectp;
1430 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1431 }
1432 else if (section_is_p (sectp->name, &names->abbrev))
1433 {
1434 dwarf2_per_objfile->abbrev.asection = sectp;
1435 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1436 }
1437 else if (section_is_p (sectp->name, &names->line))
1438 {
1439 dwarf2_per_objfile->line.asection = sectp;
1440 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1441 }
1442 else if (section_is_p (sectp->name, &names->loc))
1443 {
1444 dwarf2_per_objfile->loc.asection = sectp;
1445 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1446 }
1447 else if (section_is_p (sectp->name, &names->macinfo))
1448 {
1449 dwarf2_per_objfile->macinfo.asection = sectp;
1450 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1451 }
1452 else if (section_is_p (sectp->name, &names->macro))
1453 {
1454 dwarf2_per_objfile->macro.asection = sectp;
1455 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1456 }
1457 else if (section_is_p (sectp->name, &names->str))
1458 {
1459 dwarf2_per_objfile->str.asection = sectp;
1460 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1461 }
1462 else if (section_is_p (sectp->name, &names->frame))
1463 {
1464 dwarf2_per_objfile->frame.asection = sectp;
1465 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1466 }
1467 else if (section_is_p (sectp->name, &names->eh_frame))
1468 {
1469 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1470
1471 if (aflag & SEC_HAS_CONTENTS)
1472 {
1473 dwarf2_per_objfile->eh_frame.asection = sectp;
1474 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1475 }
1476 }
1477 else if (section_is_p (sectp->name, &names->ranges))
1478 {
1479 dwarf2_per_objfile->ranges.asection = sectp;
1480 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1481 }
1482 else if (section_is_p (sectp->name, &names->types))
1483 {
1484 struct dwarf2_section_info type_section;
1485
1486 memset (&type_section, 0, sizeof (type_section));
1487 type_section.asection = sectp;
1488 type_section.size = bfd_get_section_size (sectp);
1489
1490 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1491 &type_section);
1492 }
1493 else if (section_is_p (sectp->name, &names->gdb_index))
1494 {
1495 dwarf2_per_objfile->gdb_index.asection = sectp;
1496 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1497 }
1498
1499 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1500 && bfd_section_vma (abfd, sectp) == 0)
1501 dwarf2_per_objfile->has_section_at_zero = 1;
1502 }
1503
1504 /* Decompress a section that was compressed using zlib. Store the
1505 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1506
1507 static void
1508 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1509 gdb_byte **outbuf, bfd_size_type *outsize)
1510 {
1511 bfd *abfd = objfile->obfd;
1512 #ifndef HAVE_ZLIB_H
1513 error (_("Support for zlib-compressed DWARF data (from '%s') "
1514 "is disabled in this copy of GDB"),
1515 bfd_get_filename (abfd));
1516 #else
1517 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1518 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1519 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1520 bfd_size_type uncompressed_size;
1521 gdb_byte *uncompressed_buffer;
1522 z_stream strm;
1523 int rc;
1524 int header_size = 12;
1525
1526 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1527 || bfd_bread (compressed_buffer,
1528 compressed_size, abfd) != compressed_size)
1529 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1530 bfd_get_filename (abfd));
1531
1532 /* Read the zlib header. In this case, it should be "ZLIB" followed
1533 by the uncompressed section size, 8 bytes in big-endian order. */
1534 if (compressed_size < header_size
1535 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1536 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1537 bfd_get_filename (abfd));
1538 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1539 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1540 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1541 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1542 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1543 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1544 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1545 uncompressed_size += compressed_buffer[11];
1546
1547 /* It is possible the section consists of several compressed
1548 buffers concatenated together, so we uncompress in a loop. */
1549 strm.zalloc = NULL;
1550 strm.zfree = NULL;
1551 strm.opaque = NULL;
1552 strm.avail_in = compressed_size - header_size;
1553 strm.next_in = (Bytef*) compressed_buffer + header_size;
1554 strm.avail_out = uncompressed_size;
1555 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1556 uncompressed_size);
1557 rc = inflateInit (&strm);
1558 while (strm.avail_in > 0)
1559 {
1560 if (rc != Z_OK)
1561 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1562 bfd_get_filename (abfd), rc);
1563 strm.next_out = ((Bytef*) uncompressed_buffer
1564 + (uncompressed_size - strm.avail_out));
1565 rc = inflate (&strm, Z_FINISH);
1566 if (rc != Z_STREAM_END)
1567 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1568 bfd_get_filename (abfd), rc);
1569 rc = inflateReset (&strm);
1570 }
1571 rc = inflateEnd (&strm);
1572 if (rc != Z_OK
1573 || strm.avail_out != 0)
1574 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1575 bfd_get_filename (abfd), rc);
1576
1577 do_cleanups (cleanup);
1578 *outbuf = uncompressed_buffer;
1579 *outsize = uncompressed_size;
1580 #endif
1581 }
1582
1583 /* A helper function that decides whether a section is empty. */
1584
1585 static int
1586 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1587 {
1588 return info->asection == NULL || info->size == 0;
1589 }
1590
1591 /* Read the contents of the section SECTP from object file specified by
1592 OBJFILE, store info about the section into INFO.
1593 If the section is compressed, uncompress it before returning. */
1594
1595 static void
1596 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1597 {
1598 bfd *abfd = objfile->obfd;
1599 asection *sectp = info->asection;
1600 gdb_byte *buf, *retbuf;
1601 unsigned char header[4];
1602
1603 if (info->readin)
1604 return;
1605 info->buffer = NULL;
1606 info->map_addr = NULL;
1607 info->readin = 1;
1608
1609 if (dwarf2_section_empty_p (info))
1610 return;
1611
1612 /* Check if the file has a 4-byte header indicating compression. */
1613 if (info->size > sizeof (header)
1614 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1615 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1616 {
1617 /* Upon decompression, update the buffer and its size. */
1618 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1619 {
1620 zlib_decompress_section (objfile, sectp, &info->buffer,
1621 &info->size);
1622 return;
1623 }
1624 }
1625
1626 #ifdef HAVE_MMAP
1627 if (pagesize == 0)
1628 pagesize = getpagesize ();
1629
1630 /* Only try to mmap sections which are large enough: we don't want to
1631 waste space due to fragmentation. Also, only try mmap for sections
1632 without relocations. */
1633
1634 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1635 {
1636 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1637 MAP_PRIVATE, sectp->filepos,
1638 &info->map_addr, &info->map_len);
1639
1640 if ((caddr_t)info->buffer != MAP_FAILED)
1641 {
1642 #if HAVE_POSIX_MADVISE
1643 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1644 #endif
1645 return;
1646 }
1647 }
1648 #endif
1649
1650 /* If we get here, we are a normal, not-compressed section. */
1651 info->buffer = buf
1652 = obstack_alloc (&objfile->objfile_obstack, info->size);
1653
1654 /* When debugging .o files, we may need to apply relocations; see
1655 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1656 We never compress sections in .o files, so we only need to
1657 try this when the section is not compressed. */
1658 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1659 if (retbuf != NULL)
1660 {
1661 info->buffer = retbuf;
1662 return;
1663 }
1664
1665 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1666 || bfd_bread (buf, info->size, abfd) != info->size)
1667 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1668 bfd_get_filename (abfd));
1669 }
1670
1671 /* A helper function that returns the size of a section in a safe way.
1672 If you are positive that the section has been read before using the
1673 size, then it is safe to refer to the dwarf2_section_info object's
1674 "size" field directly. In other cases, you must call this
1675 function, because for compressed sections the size field is not set
1676 correctly until the section has been read. */
1677
1678 static bfd_size_type
1679 dwarf2_section_size (struct objfile *objfile,
1680 struct dwarf2_section_info *info)
1681 {
1682 if (!info->readin)
1683 dwarf2_read_section (objfile, info);
1684 return info->size;
1685 }
1686
1687 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1688 SECTION_NAME. */
1689
1690 void
1691 dwarf2_get_section_info (struct objfile *objfile,
1692 enum dwarf2_section_enum sect,
1693 asection **sectp, gdb_byte **bufp,
1694 bfd_size_type *sizep)
1695 {
1696 struct dwarf2_per_objfile *data
1697 = objfile_data (objfile, dwarf2_objfile_data_key);
1698 struct dwarf2_section_info *info;
1699
1700 /* We may see an objfile without any DWARF, in which case we just
1701 return nothing. */
1702 if (data == NULL)
1703 {
1704 *sectp = NULL;
1705 *bufp = NULL;
1706 *sizep = 0;
1707 return;
1708 }
1709 switch (sect)
1710 {
1711 case DWARF2_DEBUG_FRAME:
1712 info = &data->frame;
1713 break;
1714 case DWARF2_EH_FRAME:
1715 info = &data->eh_frame;
1716 break;
1717 default:
1718 gdb_assert_not_reached ("unexpected section");
1719 }
1720
1721 dwarf2_read_section (objfile, info);
1722
1723 *sectp = info->asection;
1724 *bufp = info->buffer;
1725 *sizep = info->size;
1726 }
1727
1728 \f
1729 /* DWARF quick_symbols_functions support. */
1730
1731 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1732 unique line tables, so we maintain a separate table of all .debug_line
1733 derived entries to support the sharing.
1734 All the quick functions need is the list of file names. We discard the
1735 line_header when we're done and don't need to record it here. */
1736 struct quick_file_names
1737 {
1738 /* The offset in .debug_line of the line table. We hash on this. */
1739 unsigned int offset;
1740
1741 /* The number of entries in file_names, real_names. */
1742 unsigned int num_file_names;
1743
1744 /* The file names from the line table, after being run through
1745 file_full_name. */
1746 const char **file_names;
1747
1748 /* The file names from the line table after being run through
1749 gdb_realpath. These are computed lazily. */
1750 const char **real_names;
1751 };
1752
1753 /* When using the index (and thus not using psymtabs), each CU has an
1754 object of this type. This is used to hold information needed by
1755 the various "quick" methods. */
1756 struct dwarf2_per_cu_quick_data
1757 {
1758 /* The file table. This can be NULL if there was no file table
1759 or it's currently not read in.
1760 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1761 struct quick_file_names *file_names;
1762
1763 /* The corresponding symbol table. This is NULL if symbols for this
1764 CU have not yet been read. */
1765 struct symtab *symtab;
1766
1767 /* A temporary mark bit used when iterating over all CUs in
1768 expand_symtabs_matching. */
1769 unsigned int mark : 1;
1770
1771 /* True if we've tried to read the file table and found there isn't one.
1772 There will be no point in trying to read it again next time. */
1773 unsigned int no_file_data : 1;
1774 };
1775
1776 /* Hash function for a quick_file_names. */
1777
1778 static hashval_t
1779 hash_file_name_entry (const void *e)
1780 {
1781 const struct quick_file_names *file_data = e;
1782
1783 return file_data->offset;
1784 }
1785
1786 /* Equality function for a quick_file_names. */
1787
1788 static int
1789 eq_file_name_entry (const void *a, const void *b)
1790 {
1791 const struct quick_file_names *ea = a;
1792 const struct quick_file_names *eb = b;
1793
1794 return ea->offset == eb->offset;
1795 }
1796
1797 /* Delete function for a quick_file_names. */
1798
1799 static void
1800 delete_file_name_entry (void *e)
1801 {
1802 struct quick_file_names *file_data = e;
1803 int i;
1804
1805 for (i = 0; i < file_data->num_file_names; ++i)
1806 {
1807 xfree ((void*) file_data->file_names[i]);
1808 if (file_data->real_names)
1809 xfree ((void*) file_data->real_names[i]);
1810 }
1811
1812 /* The space for the struct itself lives on objfile_obstack,
1813 so we don't free it here. */
1814 }
1815
1816 /* Create a quick_file_names hash table. */
1817
1818 static htab_t
1819 create_quick_file_names_table (unsigned int nr_initial_entries)
1820 {
1821 return htab_create_alloc (nr_initial_entries,
1822 hash_file_name_entry, eq_file_name_entry,
1823 delete_file_name_entry, xcalloc, xfree);
1824 }
1825
1826 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1827 have to be created afterwards. You should call age_cached_comp_units after
1828 processing PER_CU->CU. dw2_setup must have been already called. */
1829
1830 static void
1831 load_cu (struct dwarf2_per_cu_data *per_cu)
1832 {
1833 if (per_cu->debug_type_section)
1834 read_signatured_type_at_offset (per_cu->objfile,
1835 per_cu->debug_type_section,
1836 per_cu->offset);
1837 else
1838 load_full_comp_unit (per_cu, per_cu->objfile);
1839
1840 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1841
1842 gdb_assert (per_cu->cu != NULL);
1843 }
1844
1845 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1846 this CU came. */
1847
1848 static void
1849 dw2_do_instantiate_symtab (struct objfile *objfile,
1850 struct dwarf2_per_cu_data *per_cu)
1851 {
1852 struct cleanup *back_to;
1853
1854 back_to = make_cleanup (dwarf2_release_queue, NULL);
1855
1856 queue_comp_unit (per_cu, objfile);
1857
1858 load_cu (per_cu);
1859
1860 process_queue (objfile);
1861
1862 /* Age the cache, releasing compilation units that have not
1863 been used recently. */
1864 age_cached_comp_units ();
1865
1866 do_cleanups (back_to);
1867 }
1868
1869 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1870 the objfile from which this CU came. Returns the resulting symbol
1871 table. */
1872
1873 static struct symtab *
1874 dw2_instantiate_symtab (struct objfile *objfile,
1875 struct dwarf2_per_cu_data *per_cu)
1876 {
1877 if (!per_cu->v.quick->symtab)
1878 {
1879 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1880 increment_reading_symtab ();
1881 dw2_do_instantiate_symtab (objfile, per_cu);
1882 do_cleanups (back_to);
1883 }
1884 return per_cu->v.quick->symtab;
1885 }
1886
1887 /* Return the CU given its index. */
1888
1889 static struct dwarf2_per_cu_data *
1890 dw2_get_cu (int index)
1891 {
1892 if (index >= dwarf2_per_objfile->n_comp_units)
1893 {
1894 index -= dwarf2_per_objfile->n_comp_units;
1895 return dwarf2_per_objfile->type_comp_units[index];
1896 }
1897 return dwarf2_per_objfile->all_comp_units[index];
1898 }
1899
1900 /* A helper function that knows how to read a 64-bit value in a way
1901 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1902 otherwise. */
1903
1904 static int
1905 extract_cu_value (const char *bytes, ULONGEST *result)
1906 {
1907 if (sizeof (ULONGEST) < 8)
1908 {
1909 int i;
1910
1911 /* Ignore the upper 4 bytes if they are all zero. */
1912 for (i = 0; i < 4; ++i)
1913 if (bytes[i + 4] != 0)
1914 return 0;
1915
1916 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1917 }
1918 else
1919 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1920 return 1;
1921 }
1922
1923 /* Read the CU list from the mapped index, and use it to create all
1924 the CU objects for this objfile. Return 0 if something went wrong,
1925 1 if everything went ok. */
1926
1927 static int
1928 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1929 offset_type cu_list_elements)
1930 {
1931 offset_type i;
1932
1933 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1934 dwarf2_per_objfile->all_comp_units
1935 = obstack_alloc (&objfile->objfile_obstack,
1936 dwarf2_per_objfile->n_comp_units
1937 * sizeof (struct dwarf2_per_cu_data *));
1938
1939 for (i = 0; i < cu_list_elements; i += 2)
1940 {
1941 struct dwarf2_per_cu_data *the_cu;
1942 ULONGEST offset, length;
1943
1944 if (!extract_cu_value (cu_list, &offset)
1945 || !extract_cu_value (cu_list + 8, &length))
1946 return 0;
1947 cu_list += 2 * 8;
1948
1949 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1950 struct dwarf2_per_cu_data);
1951 the_cu->offset = offset;
1952 the_cu->length = length;
1953 the_cu->objfile = objfile;
1954 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1955 struct dwarf2_per_cu_quick_data);
1956 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1957 }
1958
1959 return 1;
1960 }
1961
1962 /* Create the signatured type hash table from the index. */
1963
1964 static int
1965 create_signatured_type_table_from_index (struct objfile *objfile,
1966 struct dwarf2_section_info *section,
1967 const gdb_byte *bytes,
1968 offset_type elements)
1969 {
1970 offset_type i;
1971 htab_t sig_types_hash;
1972
1973 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1974 dwarf2_per_objfile->type_comp_units
1975 = obstack_alloc (&objfile->objfile_obstack,
1976 dwarf2_per_objfile->n_type_comp_units
1977 * sizeof (struct dwarf2_per_cu_data *));
1978
1979 sig_types_hash = allocate_signatured_type_table (objfile);
1980
1981 for (i = 0; i < elements; i += 3)
1982 {
1983 struct signatured_type *type_sig;
1984 ULONGEST offset, type_offset, signature;
1985 void **slot;
1986
1987 if (!extract_cu_value (bytes, &offset)
1988 || !extract_cu_value (bytes + 8, &type_offset))
1989 return 0;
1990 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1991 bytes += 3 * 8;
1992
1993 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1994 struct signatured_type);
1995 type_sig->signature = signature;
1996 type_sig->type_offset = type_offset;
1997 type_sig->per_cu.debug_type_section = section;
1998 type_sig->per_cu.offset = offset;
1999 type_sig->per_cu.objfile = objfile;
2000 type_sig->per_cu.v.quick
2001 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2002 struct dwarf2_per_cu_quick_data);
2003
2004 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
2005 *slot = type_sig;
2006
2007 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2008 }
2009
2010 dwarf2_per_objfile->signatured_types = sig_types_hash;
2011
2012 return 1;
2013 }
2014
2015 /* Read the address map data from the mapped index, and use it to
2016 populate the objfile's psymtabs_addrmap. */
2017
2018 static void
2019 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2020 {
2021 const gdb_byte *iter, *end;
2022 struct obstack temp_obstack;
2023 struct addrmap *mutable_map;
2024 struct cleanup *cleanup;
2025 CORE_ADDR baseaddr;
2026
2027 obstack_init (&temp_obstack);
2028 cleanup = make_cleanup_obstack_free (&temp_obstack);
2029 mutable_map = addrmap_create_mutable (&temp_obstack);
2030
2031 iter = index->address_table;
2032 end = iter + index->address_table_size;
2033
2034 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2035
2036 while (iter < end)
2037 {
2038 ULONGEST hi, lo, cu_index;
2039 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2040 iter += 8;
2041 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2042 iter += 8;
2043 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2044 iter += 4;
2045
2046 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2047 dw2_get_cu (cu_index));
2048 }
2049
2050 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2051 &objfile->objfile_obstack);
2052 do_cleanups (cleanup);
2053 }
2054
2055 /* The hash function for strings in the mapped index. This is the same as
2056 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2057 implementation. This is necessary because the hash function is tied to the
2058 format of the mapped index file. The hash values do not have to match with
2059 SYMBOL_HASH_NEXT.
2060
2061 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2062
2063 static hashval_t
2064 mapped_index_string_hash (int index_version, const void *p)
2065 {
2066 const unsigned char *str = (const unsigned char *) p;
2067 hashval_t r = 0;
2068 unsigned char c;
2069
2070 while ((c = *str++) != 0)
2071 {
2072 if (index_version >= 5)
2073 c = tolower (c);
2074 r = r * 67 + c - 113;
2075 }
2076
2077 return r;
2078 }
2079
2080 /* Find a slot in the mapped index INDEX for the object named NAME.
2081 If NAME is found, set *VEC_OUT to point to the CU vector in the
2082 constant pool and return 1. If NAME cannot be found, return 0. */
2083
2084 static int
2085 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2086 offset_type **vec_out)
2087 {
2088 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2089 offset_type hash;
2090 offset_type slot, step;
2091 int (*cmp) (const char *, const char *);
2092
2093 if (current_language->la_language == language_cplus
2094 || current_language->la_language == language_java
2095 || current_language->la_language == language_fortran)
2096 {
2097 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2098 not contain any. */
2099 const char *paren = strchr (name, '(');
2100
2101 if (paren)
2102 {
2103 char *dup;
2104
2105 dup = xmalloc (paren - name + 1);
2106 memcpy (dup, name, paren - name);
2107 dup[paren - name] = 0;
2108
2109 make_cleanup (xfree, dup);
2110 name = dup;
2111 }
2112 }
2113
2114 /* Index version 4 did not support case insensitive searches. But the
2115 indexes for case insensitive languages are built in lowercase, therefore
2116 simulate our NAME being searched is also lowercased. */
2117 hash = mapped_index_string_hash ((index->version == 4
2118 && case_sensitivity == case_sensitive_off
2119 ? 5 : index->version),
2120 name);
2121
2122 slot = hash & (index->symbol_table_slots - 1);
2123 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2124 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2125
2126 for (;;)
2127 {
2128 /* Convert a slot number to an offset into the table. */
2129 offset_type i = 2 * slot;
2130 const char *str;
2131 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2132 {
2133 do_cleanups (back_to);
2134 return 0;
2135 }
2136
2137 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2138 if (!cmp (name, str))
2139 {
2140 *vec_out = (offset_type *) (index->constant_pool
2141 + MAYBE_SWAP (index->symbol_table[i + 1]));
2142 do_cleanups (back_to);
2143 return 1;
2144 }
2145
2146 slot = (slot + step) & (index->symbol_table_slots - 1);
2147 }
2148 }
2149
2150 /* Read the index file. If everything went ok, initialize the "quick"
2151 elements of all the CUs and return 1. Otherwise, return 0. */
2152
2153 static int
2154 dwarf2_read_index (struct objfile *objfile)
2155 {
2156 char *addr;
2157 struct mapped_index *map;
2158 offset_type *metadata;
2159 const gdb_byte *cu_list;
2160 const gdb_byte *types_list = NULL;
2161 offset_type version, cu_list_elements;
2162 offset_type types_list_elements = 0;
2163 int i;
2164
2165 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2166 return 0;
2167
2168 /* Older elfutils strip versions could keep the section in the main
2169 executable while splitting it for the separate debug info file. */
2170 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2171 & SEC_HAS_CONTENTS) == 0)
2172 return 0;
2173
2174 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2175
2176 addr = dwarf2_per_objfile->gdb_index.buffer;
2177 /* Version check. */
2178 version = MAYBE_SWAP (*(offset_type *) addr);
2179 /* Versions earlier than 3 emitted every copy of a psymbol. This
2180 causes the index to behave very poorly for certain requests. Version 3
2181 contained incomplete addrmap. So, it seems better to just ignore such
2182 indices. Index version 4 uses a different hash function than index
2183 version 5 and later. */
2184 if (version < 4)
2185 return 0;
2186 /* Indexes with higher version than the one supported by GDB may be no
2187 longer backward compatible. */
2188 if (version > 5)
2189 return 0;
2190
2191 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2192 map->version = version;
2193 map->total_size = dwarf2_per_objfile->gdb_index.size;
2194
2195 metadata = (offset_type *) (addr + sizeof (offset_type));
2196
2197 i = 0;
2198 cu_list = addr + MAYBE_SWAP (metadata[i]);
2199 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2200 / 8);
2201 ++i;
2202
2203 types_list = addr + MAYBE_SWAP (metadata[i]);
2204 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2205 - MAYBE_SWAP (metadata[i]))
2206 / 8);
2207 ++i;
2208
2209 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2210 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2211 - MAYBE_SWAP (metadata[i]));
2212 ++i;
2213
2214 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2215 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2216 - MAYBE_SWAP (metadata[i]))
2217 / (2 * sizeof (offset_type)));
2218 ++i;
2219
2220 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2221
2222 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2223 return 0;
2224
2225 if (types_list_elements)
2226 {
2227 struct dwarf2_section_info *section;
2228
2229 /* We can only handle a single .debug_types when we have an
2230 index. */
2231 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2232 return 0;
2233
2234 section = VEC_index (dwarf2_section_info_def,
2235 dwarf2_per_objfile->types, 0);
2236
2237 if (!create_signatured_type_table_from_index (objfile, section,
2238 types_list,
2239 types_list_elements))
2240 return 0;
2241 }
2242
2243 create_addrmap_from_index (objfile, map);
2244
2245 dwarf2_per_objfile->index_table = map;
2246 dwarf2_per_objfile->using_index = 1;
2247 dwarf2_per_objfile->quick_file_names_table =
2248 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2249
2250 return 1;
2251 }
2252
2253 /* A helper for the "quick" functions which sets the global
2254 dwarf2_per_objfile according to OBJFILE. */
2255
2256 static void
2257 dw2_setup (struct objfile *objfile)
2258 {
2259 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2260 gdb_assert (dwarf2_per_objfile);
2261 }
2262
2263 /* A helper for the "quick" functions which attempts to read the line
2264 table for THIS_CU. */
2265
2266 static struct quick_file_names *
2267 dw2_get_file_names (struct objfile *objfile,
2268 struct dwarf2_per_cu_data *this_cu)
2269 {
2270 bfd *abfd = objfile->obfd;
2271 struct line_header *lh;
2272 struct attribute *attr;
2273 struct cleanup *cleanups;
2274 struct die_info *comp_unit_die;
2275 struct dwarf2_section_info* sec;
2276 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2277 int has_children, i;
2278 struct dwarf2_cu cu;
2279 unsigned int bytes_read, buffer_size;
2280 struct die_reader_specs reader_specs;
2281 char *name, *comp_dir;
2282 void **slot;
2283 struct quick_file_names *qfn;
2284 unsigned int line_offset;
2285
2286 if (this_cu->v.quick->file_names != NULL)
2287 return this_cu->v.quick->file_names;
2288 /* If we know there is no line data, no point in looking again. */
2289 if (this_cu->v.quick->no_file_data)
2290 return NULL;
2291
2292 init_one_comp_unit (&cu, objfile);
2293 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2294
2295 if (this_cu->debug_type_section)
2296 sec = this_cu->debug_type_section;
2297 else
2298 sec = &dwarf2_per_objfile->info;
2299 dwarf2_read_section (objfile, sec);
2300 buffer_size = sec->size;
2301 buffer = sec->buffer;
2302 info_ptr = buffer + this_cu->offset;
2303 beg_of_comp_unit = info_ptr;
2304
2305 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2306 buffer, buffer_size,
2307 abfd);
2308
2309 /* Complete the cu_header. */
2310 cu.header.offset = beg_of_comp_unit - buffer;
2311 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2312
2313 this_cu->cu = &cu;
2314 cu.per_cu = this_cu;
2315
2316 dwarf2_read_abbrevs (abfd, &cu);
2317 make_cleanup (dwarf2_free_abbrev_table, &cu);
2318
2319 if (this_cu->debug_type_section)
2320 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2321 init_cu_die_reader (&reader_specs, &cu);
2322 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2323 &has_children);
2324
2325 lh = NULL;
2326 slot = NULL;
2327 line_offset = 0;
2328 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2329 if (attr)
2330 {
2331 struct quick_file_names find_entry;
2332
2333 line_offset = DW_UNSND (attr);
2334
2335 /* We may have already read in this line header (TU line header sharing).
2336 If we have we're done. */
2337 find_entry.offset = line_offset;
2338 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2339 &find_entry, INSERT);
2340 if (*slot != NULL)
2341 {
2342 do_cleanups (cleanups);
2343 this_cu->v.quick->file_names = *slot;
2344 return *slot;
2345 }
2346
2347 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2348 }
2349 if (lh == NULL)
2350 {
2351 do_cleanups (cleanups);
2352 this_cu->v.quick->no_file_data = 1;
2353 return NULL;
2354 }
2355
2356 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2357 qfn->offset = line_offset;
2358 gdb_assert (slot != NULL);
2359 *slot = qfn;
2360
2361 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2362
2363 qfn->num_file_names = lh->num_file_names;
2364 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2365 lh->num_file_names * sizeof (char *));
2366 for (i = 0; i < lh->num_file_names; ++i)
2367 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2368 qfn->real_names = NULL;
2369
2370 free_line_header (lh);
2371 do_cleanups (cleanups);
2372
2373 this_cu->v.quick->file_names = qfn;
2374 return qfn;
2375 }
2376
2377 /* A helper for the "quick" functions which computes and caches the
2378 real path for a given file name from the line table. */
2379
2380 static const char *
2381 dw2_get_real_path (struct objfile *objfile,
2382 struct quick_file_names *qfn, int index)
2383 {
2384 if (qfn->real_names == NULL)
2385 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2386 qfn->num_file_names, sizeof (char *));
2387
2388 if (qfn->real_names[index] == NULL)
2389 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2390
2391 return qfn->real_names[index];
2392 }
2393
2394 static struct symtab *
2395 dw2_find_last_source_symtab (struct objfile *objfile)
2396 {
2397 int index;
2398
2399 dw2_setup (objfile);
2400 index = dwarf2_per_objfile->n_comp_units - 1;
2401 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2402 }
2403
2404 /* Traversal function for dw2_forget_cached_source_info. */
2405
2406 static int
2407 dw2_free_cached_file_names (void **slot, void *info)
2408 {
2409 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2410
2411 if (file_data->real_names)
2412 {
2413 int i;
2414
2415 for (i = 0; i < file_data->num_file_names; ++i)
2416 {
2417 xfree ((void*) file_data->real_names[i]);
2418 file_data->real_names[i] = NULL;
2419 }
2420 }
2421
2422 return 1;
2423 }
2424
2425 static void
2426 dw2_forget_cached_source_info (struct objfile *objfile)
2427 {
2428 dw2_setup (objfile);
2429
2430 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2431 dw2_free_cached_file_names, NULL);
2432 }
2433
2434 static int
2435 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2436 const char *full_path, const char *real_path,
2437 struct symtab **result)
2438 {
2439 int i;
2440 int check_basename = lbasename (name) == name;
2441 struct dwarf2_per_cu_data *base_cu = NULL;
2442
2443 dw2_setup (objfile);
2444
2445 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2446 + dwarf2_per_objfile->n_type_comp_units); ++i)
2447 {
2448 int j;
2449 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2450 struct quick_file_names *file_data;
2451
2452 if (per_cu->v.quick->symtab)
2453 continue;
2454
2455 file_data = dw2_get_file_names (objfile, per_cu);
2456 if (file_data == NULL)
2457 continue;
2458
2459 for (j = 0; j < file_data->num_file_names; ++j)
2460 {
2461 const char *this_name = file_data->file_names[j];
2462
2463 if (FILENAME_CMP (name, this_name) == 0)
2464 {
2465 *result = dw2_instantiate_symtab (objfile, per_cu);
2466 return 1;
2467 }
2468
2469 if (check_basename && ! base_cu
2470 && FILENAME_CMP (lbasename (this_name), name) == 0)
2471 base_cu = per_cu;
2472
2473 if (full_path != NULL)
2474 {
2475 const char *this_real_name = dw2_get_real_path (objfile,
2476 file_data, j);
2477
2478 if (this_real_name != NULL
2479 && FILENAME_CMP (full_path, this_real_name) == 0)
2480 {
2481 *result = dw2_instantiate_symtab (objfile, per_cu);
2482 return 1;
2483 }
2484 }
2485
2486 if (real_path != NULL)
2487 {
2488 const char *this_real_name = dw2_get_real_path (objfile,
2489 file_data, j);
2490
2491 if (this_real_name != NULL
2492 && FILENAME_CMP (real_path, this_real_name) == 0)
2493 {
2494 *result = dw2_instantiate_symtab (objfile, per_cu);
2495 return 1;
2496 }
2497 }
2498 }
2499 }
2500
2501 if (base_cu)
2502 {
2503 *result = dw2_instantiate_symtab (objfile, base_cu);
2504 return 1;
2505 }
2506
2507 return 0;
2508 }
2509
2510 static struct symtab *
2511 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2512 const char *name, domain_enum domain)
2513 {
2514 /* We do all the work in the pre_expand_symtabs_matching hook
2515 instead. */
2516 return NULL;
2517 }
2518
2519 /* A helper function that expands all symtabs that hold an object
2520 named NAME. */
2521
2522 static void
2523 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2524 {
2525 dw2_setup (objfile);
2526
2527 /* index_table is NULL if OBJF_READNOW. */
2528 if (dwarf2_per_objfile->index_table)
2529 {
2530 offset_type *vec;
2531
2532 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2533 name, &vec))
2534 {
2535 offset_type i, len = MAYBE_SWAP (*vec);
2536 for (i = 0; i < len; ++i)
2537 {
2538 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2539 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2540
2541 dw2_instantiate_symtab (objfile, per_cu);
2542 }
2543 }
2544 }
2545 }
2546
2547 static void
2548 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2549 enum block_enum block_kind, const char *name,
2550 domain_enum domain)
2551 {
2552 dw2_do_expand_symtabs_matching (objfile, name);
2553 }
2554
2555 static void
2556 dw2_print_stats (struct objfile *objfile)
2557 {
2558 int i, count;
2559
2560 dw2_setup (objfile);
2561 count = 0;
2562 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2563 + dwarf2_per_objfile->n_type_comp_units); ++i)
2564 {
2565 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2566
2567 if (!per_cu->v.quick->symtab)
2568 ++count;
2569 }
2570 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2571 }
2572
2573 static void
2574 dw2_dump (struct objfile *objfile)
2575 {
2576 /* Nothing worth printing. */
2577 }
2578
2579 static void
2580 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2581 struct section_offsets *delta)
2582 {
2583 /* There's nothing to relocate here. */
2584 }
2585
2586 static void
2587 dw2_expand_symtabs_for_function (struct objfile *objfile,
2588 const char *func_name)
2589 {
2590 dw2_do_expand_symtabs_matching (objfile, func_name);
2591 }
2592
2593 static void
2594 dw2_expand_all_symtabs (struct objfile *objfile)
2595 {
2596 int i;
2597
2598 dw2_setup (objfile);
2599
2600 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2601 + dwarf2_per_objfile->n_type_comp_units); ++i)
2602 {
2603 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2604
2605 dw2_instantiate_symtab (objfile, per_cu);
2606 }
2607 }
2608
2609 static void
2610 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2611 const char *filename)
2612 {
2613 int i;
2614
2615 dw2_setup (objfile);
2616
2617 /* We don't need to consider type units here.
2618 This is only called for examining code, e.g. expand_line_sal.
2619 There can be an order of magnitude (or more) more type units
2620 than comp units, and we avoid them if we can. */
2621
2622 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2623 {
2624 int j;
2625 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2626 struct quick_file_names *file_data;
2627
2628 if (per_cu->v.quick->symtab)
2629 continue;
2630
2631 file_data = dw2_get_file_names (objfile, per_cu);
2632 if (file_data == NULL)
2633 continue;
2634
2635 for (j = 0; j < file_data->num_file_names; ++j)
2636 {
2637 const char *this_name = file_data->file_names[j];
2638 if (FILENAME_CMP (this_name, filename) == 0)
2639 {
2640 dw2_instantiate_symtab (objfile, per_cu);
2641 break;
2642 }
2643 }
2644 }
2645 }
2646
2647 static const char *
2648 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2649 {
2650 struct dwarf2_per_cu_data *per_cu;
2651 offset_type *vec;
2652 struct quick_file_names *file_data;
2653
2654 dw2_setup (objfile);
2655
2656 /* index_table is NULL if OBJF_READNOW. */
2657 if (!dwarf2_per_objfile->index_table)
2658 {
2659 struct symtab *s;
2660
2661 ALL_OBJFILE_SYMTABS (objfile, s)
2662 if (s->primary)
2663 {
2664 struct blockvector *bv = BLOCKVECTOR (s);
2665 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2666 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2667
2668 if (sym)
2669 return sym->symtab->filename;
2670 }
2671 return NULL;
2672 }
2673
2674 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2675 name, &vec))
2676 return NULL;
2677
2678 /* Note that this just looks at the very first one named NAME -- but
2679 actually we are looking for a function. find_main_filename
2680 should be rewritten so that it doesn't require a custom hook. It
2681 could just use the ordinary symbol tables. */
2682 /* vec[0] is the length, which must always be >0. */
2683 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2684
2685 file_data = dw2_get_file_names (objfile, per_cu);
2686 if (file_data == NULL)
2687 return NULL;
2688
2689 return file_data->file_names[file_data->num_file_names - 1];
2690 }
2691
2692 static void
2693 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2694 struct objfile *objfile, int global,
2695 int (*callback) (struct block *,
2696 struct symbol *, void *),
2697 void *data, symbol_compare_ftype *match,
2698 symbol_compare_ftype *ordered_compare)
2699 {
2700 /* Currently unimplemented; used for Ada. The function can be called if the
2701 current language is Ada for a non-Ada objfile using GNU index. As Ada
2702 does not look for non-Ada symbols this function should just return. */
2703 }
2704
2705 static void
2706 dw2_expand_symtabs_matching (struct objfile *objfile,
2707 int (*file_matcher) (const char *, void *),
2708 int (*name_matcher) (const char *, void *),
2709 enum search_domain kind,
2710 void *data)
2711 {
2712 int i;
2713 offset_type iter;
2714 struct mapped_index *index;
2715
2716 dw2_setup (objfile);
2717
2718 /* index_table is NULL if OBJF_READNOW. */
2719 if (!dwarf2_per_objfile->index_table)
2720 return;
2721 index = dwarf2_per_objfile->index_table;
2722
2723 if (file_matcher != NULL)
2724 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2725 + dwarf2_per_objfile->n_type_comp_units); ++i)
2726 {
2727 int j;
2728 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2729 struct quick_file_names *file_data;
2730
2731 per_cu->v.quick->mark = 0;
2732 if (per_cu->v.quick->symtab)
2733 continue;
2734
2735 file_data = dw2_get_file_names (objfile, per_cu);
2736 if (file_data == NULL)
2737 continue;
2738
2739 for (j = 0; j < file_data->num_file_names; ++j)
2740 {
2741 if (file_matcher (file_data->file_names[j], data))
2742 {
2743 per_cu->v.quick->mark = 1;
2744 break;
2745 }
2746 }
2747 }
2748
2749 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2750 {
2751 offset_type idx = 2 * iter;
2752 const char *name;
2753 offset_type *vec, vec_len, vec_idx;
2754
2755 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2756 continue;
2757
2758 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2759
2760 if (! (*name_matcher) (name, data))
2761 continue;
2762
2763 /* The name was matched, now expand corresponding CUs that were
2764 marked. */
2765 vec = (offset_type *) (index->constant_pool
2766 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2767 vec_len = MAYBE_SWAP (vec[0]);
2768 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2769 {
2770 struct dwarf2_per_cu_data *per_cu;
2771
2772 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2773 if (file_matcher == NULL || per_cu->v.quick->mark)
2774 dw2_instantiate_symtab (objfile, per_cu);
2775 }
2776 }
2777 }
2778
2779 static struct symtab *
2780 dw2_find_pc_sect_symtab (struct objfile *objfile,
2781 struct minimal_symbol *msymbol,
2782 CORE_ADDR pc,
2783 struct obj_section *section,
2784 int warn_if_readin)
2785 {
2786 struct dwarf2_per_cu_data *data;
2787
2788 dw2_setup (objfile);
2789
2790 if (!objfile->psymtabs_addrmap)
2791 return NULL;
2792
2793 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2794 if (!data)
2795 return NULL;
2796
2797 if (warn_if_readin && data->v.quick->symtab)
2798 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2799 paddress (get_objfile_arch (objfile), pc));
2800
2801 return dw2_instantiate_symtab (objfile, data);
2802 }
2803
2804 static void
2805 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2806 void *data)
2807 {
2808 int i;
2809
2810 dw2_setup (objfile);
2811
2812 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2813 + dwarf2_per_objfile->n_type_comp_units); ++i)
2814 {
2815 int j;
2816 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2817 struct quick_file_names *file_data;
2818
2819 if (per_cu->v.quick->symtab)
2820 continue;
2821
2822 file_data = dw2_get_file_names (objfile, per_cu);
2823 if (file_data == NULL)
2824 continue;
2825
2826 for (j = 0; j < file_data->num_file_names; ++j)
2827 {
2828 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2829 j);
2830 (*fun) (file_data->file_names[j], this_real_name, data);
2831 }
2832 }
2833 }
2834
2835 static int
2836 dw2_has_symbols (struct objfile *objfile)
2837 {
2838 return 1;
2839 }
2840
2841 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2842 {
2843 dw2_has_symbols,
2844 dw2_find_last_source_symtab,
2845 dw2_forget_cached_source_info,
2846 dw2_lookup_symtab,
2847 dw2_lookup_symbol,
2848 dw2_pre_expand_symtabs_matching,
2849 dw2_print_stats,
2850 dw2_dump,
2851 dw2_relocate,
2852 dw2_expand_symtabs_for_function,
2853 dw2_expand_all_symtabs,
2854 dw2_expand_symtabs_with_filename,
2855 dw2_find_symbol_file,
2856 dw2_map_matching_symbols,
2857 dw2_expand_symtabs_matching,
2858 dw2_find_pc_sect_symtab,
2859 dw2_map_symbol_filenames
2860 };
2861
2862 /* Initialize for reading DWARF for this objfile. Return 0 if this
2863 file will use psymtabs, or 1 if using the GNU index. */
2864
2865 int
2866 dwarf2_initialize_objfile (struct objfile *objfile)
2867 {
2868 /* If we're about to read full symbols, don't bother with the
2869 indices. In this case we also don't care if some other debug
2870 format is making psymtabs, because they are all about to be
2871 expanded anyway. */
2872 if ((objfile->flags & OBJF_READNOW))
2873 {
2874 int i;
2875
2876 dwarf2_per_objfile->using_index = 1;
2877 create_all_comp_units (objfile);
2878 create_debug_types_hash_table (objfile);
2879 dwarf2_per_objfile->quick_file_names_table =
2880 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2881
2882 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2883 + dwarf2_per_objfile->n_type_comp_units); ++i)
2884 {
2885 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2886
2887 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2888 struct dwarf2_per_cu_quick_data);
2889 }
2890
2891 /* Return 1 so that gdb sees the "quick" functions. However,
2892 these functions will be no-ops because we will have expanded
2893 all symtabs. */
2894 return 1;
2895 }
2896
2897 if (dwarf2_read_index (objfile))
2898 return 1;
2899
2900 return 0;
2901 }
2902
2903 \f
2904
2905 /* Build a partial symbol table. */
2906
2907 void
2908 dwarf2_build_psymtabs (struct objfile *objfile)
2909 {
2910 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2911 {
2912 init_psymbol_list (objfile, 1024);
2913 }
2914
2915 dwarf2_build_psymtabs_hard (objfile);
2916 }
2917
2918 /* Return TRUE if OFFSET is within CU_HEADER. */
2919
2920 static inline int
2921 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2922 {
2923 unsigned int bottom = cu_header->offset;
2924 unsigned int top = (cu_header->offset
2925 + cu_header->length
2926 + cu_header->initial_length_size);
2927
2928 return (offset >= bottom && offset < top);
2929 }
2930
2931 /* Read in the comp unit header information from the debug_info at info_ptr.
2932 NOTE: This leaves members offset, first_die_offset to be filled in
2933 by the caller. */
2934
2935 static gdb_byte *
2936 read_comp_unit_head (struct comp_unit_head *cu_header,
2937 gdb_byte *info_ptr, bfd *abfd)
2938 {
2939 int signed_addr;
2940 unsigned int bytes_read;
2941
2942 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2943 cu_header->initial_length_size = bytes_read;
2944 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2945 info_ptr += bytes_read;
2946 cu_header->version = read_2_bytes (abfd, info_ptr);
2947 info_ptr += 2;
2948 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2949 &bytes_read);
2950 info_ptr += bytes_read;
2951 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2952 info_ptr += 1;
2953 signed_addr = bfd_get_sign_extend_vma (abfd);
2954 if (signed_addr < 0)
2955 internal_error (__FILE__, __LINE__,
2956 _("read_comp_unit_head: dwarf from non elf file"));
2957 cu_header->signed_addr_p = signed_addr;
2958
2959 return info_ptr;
2960 }
2961
2962 static gdb_byte *
2963 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2964 gdb_byte *buffer, unsigned int buffer_size,
2965 bfd *abfd)
2966 {
2967 gdb_byte *beg_of_comp_unit = info_ptr;
2968
2969 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2970
2971 if (header->version != 2 && header->version != 3 && header->version != 4)
2972 error (_("Dwarf Error: wrong version in compilation unit header "
2973 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2974 bfd_get_filename (abfd));
2975
2976 if (header->abbrev_offset
2977 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2978 &dwarf2_per_objfile->abbrev))
2979 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2980 "(offset 0x%lx + 6) [in module %s]"),
2981 (long) header->abbrev_offset,
2982 (long) (beg_of_comp_unit - buffer),
2983 bfd_get_filename (abfd));
2984
2985 if (beg_of_comp_unit + header->length + header->initial_length_size
2986 > buffer + buffer_size)
2987 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2988 "(offset 0x%lx + 0) [in module %s]"),
2989 (long) header->length,
2990 (long) (beg_of_comp_unit - buffer),
2991 bfd_get_filename (abfd));
2992
2993 return info_ptr;
2994 }
2995
2996 /* Read in the types comp unit header information from .debug_types entry at
2997 types_ptr. The result is a pointer to one past the end of the header. */
2998
2999 static gdb_byte *
3000 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3001 struct dwarf2_section_info *section,
3002 ULONGEST *signature,
3003 gdb_byte *types_ptr, bfd *abfd)
3004 {
3005 gdb_byte *initial_types_ptr = types_ptr;
3006
3007 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3008 cu_header->offset = types_ptr - section->buffer;
3009
3010 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3011
3012 *signature = read_8_bytes (abfd, types_ptr);
3013 types_ptr += 8;
3014 types_ptr += cu_header->offset_size;
3015 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3016
3017 return types_ptr;
3018 }
3019
3020 /* Allocate a new partial symtab for file named NAME and mark this new
3021 partial symtab as being an include of PST. */
3022
3023 static void
3024 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3025 struct objfile *objfile)
3026 {
3027 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3028
3029 subpst->section_offsets = pst->section_offsets;
3030 subpst->textlow = 0;
3031 subpst->texthigh = 0;
3032
3033 subpst->dependencies = (struct partial_symtab **)
3034 obstack_alloc (&objfile->objfile_obstack,
3035 sizeof (struct partial_symtab *));
3036 subpst->dependencies[0] = pst;
3037 subpst->number_of_dependencies = 1;
3038
3039 subpst->globals_offset = 0;
3040 subpst->n_global_syms = 0;
3041 subpst->statics_offset = 0;
3042 subpst->n_static_syms = 0;
3043 subpst->symtab = NULL;
3044 subpst->read_symtab = pst->read_symtab;
3045 subpst->readin = 0;
3046
3047 /* No private part is necessary for include psymtabs. This property
3048 can be used to differentiate between such include psymtabs and
3049 the regular ones. */
3050 subpst->read_symtab_private = NULL;
3051 }
3052
3053 /* Read the Line Number Program data and extract the list of files
3054 included by the source file represented by PST. Build an include
3055 partial symtab for each of these included files. */
3056
3057 static void
3058 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3059 struct die_info *die,
3060 struct partial_symtab *pst)
3061 {
3062 struct objfile *objfile = cu->objfile;
3063 bfd *abfd = objfile->obfd;
3064 struct line_header *lh = NULL;
3065 struct attribute *attr;
3066
3067 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3068 if (attr)
3069 {
3070 unsigned int line_offset = DW_UNSND (attr);
3071
3072 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3073 }
3074 if (lh == NULL)
3075 return; /* No linetable, so no includes. */
3076
3077 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3078 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3079
3080 free_line_header (lh);
3081 }
3082
3083 static hashval_t
3084 hash_type_signature (const void *item)
3085 {
3086 const struct signatured_type *type_sig = item;
3087
3088 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3089 return type_sig->signature;
3090 }
3091
3092 static int
3093 eq_type_signature (const void *item_lhs, const void *item_rhs)
3094 {
3095 const struct signatured_type *lhs = item_lhs;
3096 const struct signatured_type *rhs = item_rhs;
3097
3098 return lhs->signature == rhs->signature;
3099 }
3100
3101 /* Allocate a hash table for signatured types. */
3102
3103 static htab_t
3104 allocate_signatured_type_table (struct objfile *objfile)
3105 {
3106 return htab_create_alloc_ex (41,
3107 hash_type_signature,
3108 eq_type_signature,
3109 NULL,
3110 &objfile->objfile_obstack,
3111 hashtab_obstack_allocate,
3112 dummy_obstack_deallocate);
3113 }
3114
3115 /* A helper function to add a signatured type CU to a list. */
3116
3117 static int
3118 add_signatured_type_cu_to_list (void **slot, void *datum)
3119 {
3120 struct signatured_type *sigt = *slot;
3121 struct dwarf2_per_cu_data ***datap = datum;
3122
3123 **datap = &sigt->per_cu;
3124 ++*datap;
3125
3126 return 1;
3127 }
3128
3129 /* Create the hash table of all entries in the .debug_types section.
3130 The result is zero if there is an error (e.g. missing .debug_types section),
3131 otherwise non-zero. */
3132
3133 static int
3134 create_debug_types_hash_table (struct objfile *objfile)
3135 {
3136 htab_t types_htab = NULL;
3137 struct dwarf2_per_cu_data **iter;
3138 int ix;
3139 struct dwarf2_section_info *section;
3140
3141 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3142 {
3143 dwarf2_per_objfile->signatured_types = NULL;
3144 return 0;
3145 }
3146
3147 for (ix = 0;
3148 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3149 ix, section);
3150 ++ix)
3151 {
3152 gdb_byte *info_ptr, *end_ptr;
3153
3154 dwarf2_read_section (objfile, section);
3155 info_ptr = section->buffer;
3156
3157 if (info_ptr == NULL)
3158 continue;
3159
3160 if (types_htab == NULL)
3161 types_htab = allocate_signatured_type_table (objfile);
3162
3163 if (dwarf2_die_debug)
3164 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3165
3166 end_ptr = info_ptr + section->size;
3167 while (info_ptr < end_ptr)
3168 {
3169 unsigned int offset;
3170 unsigned int offset_size;
3171 unsigned int type_offset;
3172 unsigned int length, initial_length_size;
3173 unsigned short version;
3174 ULONGEST signature;
3175 struct signatured_type *type_sig;
3176 void **slot;
3177 gdb_byte *ptr = info_ptr;
3178
3179 offset = ptr - section->buffer;
3180
3181 /* We need to read the type's signature in order to build the hash
3182 table, but we don't need to read anything else just yet. */
3183
3184 /* Sanity check to ensure entire cu is present. */
3185 length = read_initial_length (objfile->obfd, ptr,
3186 &initial_length_size);
3187 if (ptr + length + initial_length_size > end_ptr)
3188 {
3189 complaint (&symfile_complaints,
3190 _("debug type entry runs off end "
3191 "of `.debug_types' section, ignored"));
3192 break;
3193 }
3194
3195 offset_size = initial_length_size == 4 ? 4 : 8;
3196 ptr += initial_length_size;
3197 version = bfd_get_16 (objfile->obfd, ptr);
3198 ptr += 2;
3199 ptr += offset_size; /* abbrev offset */
3200 ptr += 1; /* address size */
3201 signature = bfd_get_64 (objfile->obfd, ptr);
3202 ptr += 8;
3203 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3204
3205 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3206 memset (type_sig, 0, sizeof (*type_sig));
3207 type_sig->signature = signature;
3208 type_sig->type_offset = type_offset;
3209 type_sig->per_cu.objfile = objfile;
3210 type_sig->per_cu.debug_type_section = section;
3211 type_sig->per_cu.offset = offset;
3212
3213 slot = htab_find_slot (types_htab, type_sig, INSERT);
3214 gdb_assert (slot != NULL);
3215 if (*slot != NULL)
3216 {
3217 const struct signatured_type *dup_sig = *slot;
3218
3219 complaint (&symfile_complaints,
3220 _("debug type entry at offset 0x%x is duplicate to the "
3221 "entry at offset 0x%x, signature 0x%s"),
3222 offset, dup_sig->per_cu.offset,
3223 phex (signature, sizeof (signature)));
3224 gdb_assert (signature == dup_sig->signature);
3225 }
3226 *slot = type_sig;
3227
3228 if (dwarf2_die_debug)
3229 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3230 offset, phex (signature, sizeof (signature)));
3231
3232 info_ptr = info_ptr + initial_length_size + length;
3233 }
3234 }
3235
3236 dwarf2_per_objfile->signatured_types = types_htab;
3237
3238 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3239 dwarf2_per_objfile->type_comp_units
3240 = obstack_alloc (&objfile->objfile_obstack,
3241 dwarf2_per_objfile->n_type_comp_units
3242 * sizeof (struct dwarf2_per_cu_data *));
3243 iter = &dwarf2_per_objfile->type_comp_units[0];
3244 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3245 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3246 == dwarf2_per_objfile->n_type_comp_units);
3247
3248 return 1;
3249 }
3250
3251 /* Lookup a signature based type.
3252 Returns NULL if SIG is not present in the table. */
3253
3254 static struct signatured_type *
3255 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3256 {
3257 struct signatured_type find_entry, *entry;
3258
3259 if (dwarf2_per_objfile->signatured_types == NULL)
3260 {
3261 complaint (&symfile_complaints,
3262 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3263 return 0;
3264 }
3265
3266 find_entry.signature = sig;
3267 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3268 return entry;
3269 }
3270
3271 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3272
3273 static void
3274 init_cu_die_reader (struct die_reader_specs *reader,
3275 struct dwarf2_cu *cu)
3276 {
3277 reader->abfd = cu->objfile->obfd;
3278 reader->cu = cu;
3279 if (cu->per_cu->debug_type_section)
3280 {
3281 gdb_assert (cu->per_cu->debug_type_section->readin);
3282 reader->buffer = cu->per_cu->debug_type_section->buffer;
3283 }
3284 else
3285 {
3286 gdb_assert (dwarf2_per_objfile->info.readin);
3287 reader->buffer = dwarf2_per_objfile->info.buffer;
3288 }
3289 }
3290
3291 /* Find the base address of the compilation unit for range lists and
3292 location lists. It will normally be specified by DW_AT_low_pc.
3293 In DWARF-3 draft 4, the base address could be overridden by
3294 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3295 compilation units with discontinuous ranges. */
3296
3297 static void
3298 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3299 {
3300 struct attribute *attr;
3301
3302 cu->base_known = 0;
3303 cu->base_address = 0;
3304
3305 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3306 if (attr)
3307 {
3308 cu->base_address = DW_ADDR (attr);
3309 cu->base_known = 1;
3310 }
3311 else
3312 {
3313 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3314 if (attr)
3315 {
3316 cu->base_address = DW_ADDR (attr);
3317 cu->base_known = 1;
3318 }
3319 }
3320 }
3321
3322 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3323 to combine the common parts.
3324 Process a compilation unit for a psymtab.
3325 BUFFER is a pointer to the beginning of the dwarf section buffer,
3326 either .debug_info or debug_types.
3327 INFO_PTR is a pointer to the start of the CU.
3328 Returns a pointer to the next CU. */
3329
3330 static gdb_byte *
3331 process_psymtab_comp_unit (struct objfile *objfile,
3332 struct dwarf2_per_cu_data *this_cu,
3333 gdb_byte *buffer, gdb_byte *info_ptr,
3334 unsigned int buffer_size)
3335 {
3336 bfd *abfd = objfile->obfd;
3337 gdb_byte *beg_of_comp_unit = info_ptr;
3338 struct die_info *comp_unit_die;
3339 struct partial_symtab *pst;
3340 CORE_ADDR baseaddr;
3341 struct cleanup *back_to_inner;
3342 struct dwarf2_cu cu;
3343 int has_children, has_pc_info;
3344 struct attribute *attr;
3345 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3346 struct die_reader_specs reader_specs;
3347 const char *filename;
3348
3349 init_one_comp_unit (&cu, objfile);
3350 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3351
3352 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3353 buffer, buffer_size,
3354 abfd);
3355
3356 /* Complete the cu_header. */
3357 cu.header.offset = beg_of_comp_unit - buffer;
3358 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3359
3360 cu.list_in_scope = &file_symbols;
3361
3362 /* If this compilation unit was already read in, free the
3363 cached copy in order to read it in again. This is
3364 necessary because we skipped some symbols when we first
3365 read in the compilation unit (see load_partial_dies).
3366 This problem could be avoided, but the benefit is
3367 unclear. */
3368 if (this_cu->cu != NULL)
3369 free_one_cached_comp_unit (this_cu->cu);
3370
3371 /* Note that this is a pointer to our stack frame, being
3372 added to a global data structure. It will be cleaned up
3373 in free_stack_comp_unit when we finish with this
3374 compilation unit. */
3375 this_cu->cu = &cu;
3376 cu.per_cu = this_cu;
3377
3378 /* Read the abbrevs for this compilation unit into a table. */
3379 dwarf2_read_abbrevs (abfd, &cu);
3380 make_cleanup (dwarf2_free_abbrev_table, &cu);
3381
3382 /* Read the compilation unit die. */
3383 if (this_cu->debug_type_section)
3384 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3385 init_cu_die_reader (&reader_specs, &cu);
3386 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3387 &has_children);
3388
3389 if (this_cu->debug_type_section)
3390 {
3391 /* LENGTH has not been set yet for type units. */
3392 gdb_assert (this_cu->offset == cu.header.offset);
3393 this_cu->length = cu.header.length + cu.header.initial_length_size;
3394 }
3395 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3396 {
3397 info_ptr = (beg_of_comp_unit + cu.header.length
3398 + cu.header.initial_length_size);
3399 do_cleanups (back_to_inner);
3400 return info_ptr;
3401 }
3402
3403 prepare_one_comp_unit (&cu, comp_unit_die);
3404
3405 /* Allocate a new partial symbol table structure. */
3406 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3407 if (attr == NULL || !DW_STRING (attr))
3408 filename = "";
3409 else
3410 filename = DW_STRING (attr);
3411 pst = start_psymtab_common (objfile, objfile->section_offsets,
3412 filename,
3413 /* TEXTLOW and TEXTHIGH are set below. */
3414 0,
3415 objfile->global_psymbols.next,
3416 objfile->static_psymbols.next);
3417
3418 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3419 if (attr != NULL)
3420 pst->dirname = DW_STRING (attr);
3421
3422 pst->read_symtab_private = this_cu;
3423
3424 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3425
3426 /* Store the function that reads in the rest of the symbol table. */
3427 pst->read_symtab = dwarf2_psymtab_to_symtab;
3428
3429 this_cu->v.psymtab = pst;
3430
3431 dwarf2_find_base_address (comp_unit_die, &cu);
3432
3433 /* Possibly set the default values of LOWPC and HIGHPC from
3434 `DW_AT_ranges'. */
3435 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3436 &best_highpc, &cu, pst);
3437 if (has_pc_info == 1 && best_lowpc < best_highpc)
3438 /* Store the contiguous range if it is not empty; it can be empty for
3439 CUs with no code. */
3440 addrmap_set_empty (objfile->psymtabs_addrmap,
3441 best_lowpc + baseaddr,
3442 best_highpc + baseaddr - 1, pst);
3443
3444 /* Check if comp unit has_children.
3445 If so, read the rest of the partial symbols from this comp unit.
3446 If not, there's no more debug_info for this comp unit. */
3447 if (has_children)
3448 {
3449 struct partial_die_info *first_die;
3450 CORE_ADDR lowpc, highpc;
3451
3452 lowpc = ((CORE_ADDR) -1);
3453 highpc = ((CORE_ADDR) 0);
3454
3455 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3456
3457 scan_partial_symbols (first_die, &lowpc, &highpc,
3458 ! has_pc_info, &cu);
3459
3460 /* If we didn't find a lowpc, set it to highpc to avoid
3461 complaints from `maint check'. */
3462 if (lowpc == ((CORE_ADDR) -1))
3463 lowpc = highpc;
3464
3465 /* If the compilation unit didn't have an explicit address range,
3466 then use the information extracted from its child dies. */
3467 if (! has_pc_info)
3468 {
3469 best_lowpc = lowpc;
3470 best_highpc = highpc;
3471 }
3472 }
3473 pst->textlow = best_lowpc + baseaddr;
3474 pst->texthigh = best_highpc + baseaddr;
3475
3476 pst->n_global_syms = objfile->global_psymbols.next -
3477 (objfile->global_psymbols.list + pst->globals_offset);
3478 pst->n_static_syms = objfile->static_psymbols.next -
3479 (objfile->static_psymbols.list + pst->statics_offset);
3480 sort_pst_symbols (pst);
3481
3482 info_ptr = (beg_of_comp_unit + cu.header.length
3483 + cu.header.initial_length_size);
3484
3485 if (this_cu->debug_type_section)
3486 {
3487 /* It's not clear we want to do anything with stmt lists here.
3488 Waiting to see what gcc ultimately does. */
3489 }
3490 else
3491 {
3492 /* Get the list of files included in the current compilation unit,
3493 and build a psymtab for each of them. */
3494 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3495 }
3496
3497 do_cleanups (back_to_inner);
3498
3499 return info_ptr;
3500 }
3501
3502 /* Traversal function for htab_traverse_noresize.
3503 Process one .debug_types comp-unit. */
3504
3505 static int
3506 process_type_comp_unit (void **slot, void *info)
3507 {
3508 struct signatured_type *entry = (struct signatured_type *) *slot;
3509 struct objfile *objfile = (struct objfile *) info;
3510 struct dwarf2_per_cu_data *this_cu;
3511
3512 this_cu = &entry->per_cu;
3513
3514 gdb_assert (this_cu->debug_type_section->readin);
3515 process_psymtab_comp_unit (objfile, this_cu,
3516 this_cu->debug_type_section->buffer,
3517 (this_cu->debug_type_section->buffer
3518 + this_cu->offset),
3519 this_cu->debug_type_section->size);
3520
3521 return 1;
3522 }
3523
3524 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3525 Build partial symbol tables for the .debug_types comp-units. */
3526
3527 static void
3528 build_type_psymtabs (struct objfile *objfile)
3529 {
3530 if (! create_debug_types_hash_table (objfile))
3531 return;
3532
3533 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3534 process_type_comp_unit, objfile);
3535 }
3536
3537 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3538
3539 static void
3540 psymtabs_addrmap_cleanup (void *o)
3541 {
3542 struct objfile *objfile = o;
3543
3544 objfile->psymtabs_addrmap = NULL;
3545 }
3546
3547 /* Build the partial symbol table by doing a quick pass through the
3548 .debug_info and .debug_abbrev sections. */
3549
3550 static void
3551 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3552 {
3553 gdb_byte *info_ptr;
3554 struct cleanup *back_to, *addrmap_cleanup;
3555 struct obstack temp_obstack;
3556
3557 dwarf2_per_objfile->reading_partial_symbols = 1;
3558
3559 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3560 info_ptr = dwarf2_per_objfile->info.buffer;
3561
3562 /* Any cached compilation units will be linked by the per-objfile
3563 read_in_chain. Make sure to free them when we're done. */
3564 back_to = make_cleanup (free_cached_comp_units, NULL);
3565
3566 build_type_psymtabs (objfile);
3567
3568 create_all_comp_units (objfile);
3569
3570 /* Create a temporary address map on a temporary obstack. We later
3571 copy this to the final obstack. */
3572 obstack_init (&temp_obstack);
3573 make_cleanup_obstack_free (&temp_obstack);
3574 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3575 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3576
3577 /* Since the objects we're extracting from .debug_info vary in
3578 length, only the individual functions to extract them (like
3579 read_comp_unit_head and load_partial_die) can really know whether
3580 the buffer is large enough to hold another complete object.
3581
3582 At the moment, they don't actually check that. If .debug_info
3583 holds just one extra byte after the last compilation unit's dies,
3584 then read_comp_unit_head will happily read off the end of the
3585 buffer. read_partial_die is similarly casual. Those functions
3586 should be fixed.
3587
3588 For this loop condition, simply checking whether there's any data
3589 left at all should be sufficient. */
3590
3591 while (info_ptr < (dwarf2_per_objfile->info.buffer
3592 + dwarf2_per_objfile->info.size))
3593 {
3594 struct dwarf2_per_cu_data *this_cu;
3595
3596 this_cu = dwarf2_find_comp_unit (info_ptr
3597 - dwarf2_per_objfile->info.buffer,
3598 objfile);
3599
3600 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3601 dwarf2_per_objfile->info.buffer,
3602 info_ptr,
3603 dwarf2_per_objfile->info.size);
3604 }
3605
3606 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3607 &objfile->objfile_obstack);
3608 discard_cleanups (addrmap_cleanup);
3609
3610 do_cleanups (back_to);
3611 }
3612
3613 /* Load the partial DIEs for a secondary CU into memory. */
3614
3615 static void
3616 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3617 struct objfile *objfile)
3618 {
3619 bfd *abfd = objfile->obfd;
3620 gdb_byte *info_ptr, *beg_of_comp_unit;
3621 struct die_info *comp_unit_die;
3622 struct dwarf2_cu *cu;
3623 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3624 int has_children;
3625 struct die_reader_specs reader_specs;
3626 int read_cu = 0;
3627
3628 gdb_assert (! this_cu->debug_type_section);
3629
3630 gdb_assert (dwarf2_per_objfile->info.readin);
3631 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3632 beg_of_comp_unit = info_ptr;
3633
3634 if (this_cu->cu == NULL)
3635 {
3636 cu = xmalloc (sizeof (*cu));
3637 init_one_comp_unit (cu, objfile);
3638
3639 read_cu = 1;
3640
3641 /* If an error occurs while loading, release our storage. */
3642 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3643
3644 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3645 dwarf2_per_objfile->info.buffer,
3646 dwarf2_per_objfile->info.size,
3647 abfd);
3648
3649 /* Complete the cu_header. */
3650 cu->header.offset = this_cu->offset;
3651 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3652
3653 /* Link this compilation unit into the compilation unit tree. */
3654 this_cu->cu = cu;
3655 cu->per_cu = this_cu;
3656
3657 /* Link this CU into read_in_chain. */
3658 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3659 dwarf2_per_objfile->read_in_chain = this_cu;
3660 }
3661 else
3662 {
3663 cu = this_cu->cu;
3664 info_ptr += cu->header.first_die_offset;
3665 }
3666
3667 /* Read the abbrevs for this compilation unit into a table. */
3668 gdb_assert (cu->dwarf2_abbrevs == NULL);
3669 dwarf2_read_abbrevs (abfd, cu);
3670 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3671
3672 /* Read the compilation unit die. */
3673 init_cu_die_reader (&reader_specs, cu);
3674 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3675 &has_children);
3676
3677 prepare_one_comp_unit (cu, comp_unit_die);
3678
3679 /* Check if comp unit has_children.
3680 If so, read the rest of the partial symbols from this comp unit.
3681 If not, there's no more debug_info for this comp unit. */
3682 if (has_children)
3683 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3684
3685 do_cleanups (free_abbrevs_cleanup);
3686
3687 if (read_cu)
3688 {
3689 /* We've successfully allocated this compilation unit. Let our
3690 caller clean it up when finished with it. */
3691 discard_cleanups (free_cu_cleanup);
3692 }
3693 }
3694
3695 /* Create a list of all compilation units in OBJFILE. We do this only
3696 if an inter-comp-unit reference is found; presumably if there is one,
3697 there will be many, and one will occur early in the .debug_info section.
3698 So there's no point in building this list incrementally. */
3699
3700 static void
3701 create_all_comp_units (struct objfile *objfile)
3702 {
3703 int n_allocated;
3704 int n_comp_units;
3705 struct dwarf2_per_cu_data **all_comp_units;
3706 gdb_byte *info_ptr;
3707
3708 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3709 info_ptr = dwarf2_per_objfile->info.buffer;
3710
3711 n_comp_units = 0;
3712 n_allocated = 10;
3713 all_comp_units = xmalloc (n_allocated
3714 * sizeof (struct dwarf2_per_cu_data *));
3715
3716 while (info_ptr < dwarf2_per_objfile->info.buffer
3717 + dwarf2_per_objfile->info.size)
3718 {
3719 unsigned int length, initial_length_size;
3720 struct dwarf2_per_cu_data *this_cu;
3721 unsigned int offset;
3722
3723 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3724
3725 /* Read just enough information to find out where the next
3726 compilation unit is. */
3727 length = read_initial_length (objfile->obfd, info_ptr,
3728 &initial_length_size);
3729
3730 /* Save the compilation unit for later lookup. */
3731 this_cu = obstack_alloc (&objfile->objfile_obstack,
3732 sizeof (struct dwarf2_per_cu_data));
3733 memset (this_cu, 0, sizeof (*this_cu));
3734 this_cu->offset = offset;
3735 this_cu->length = length + initial_length_size;
3736 this_cu->objfile = objfile;
3737
3738 if (n_comp_units == n_allocated)
3739 {
3740 n_allocated *= 2;
3741 all_comp_units = xrealloc (all_comp_units,
3742 n_allocated
3743 * sizeof (struct dwarf2_per_cu_data *));
3744 }
3745 all_comp_units[n_comp_units++] = this_cu;
3746
3747 info_ptr = info_ptr + this_cu->length;
3748 }
3749
3750 dwarf2_per_objfile->all_comp_units
3751 = obstack_alloc (&objfile->objfile_obstack,
3752 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3753 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3754 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3755 xfree (all_comp_units);
3756 dwarf2_per_objfile->n_comp_units = n_comp_units;
3757 }
3758
3759 /* Process all loaded DIEs for compilation unit CU, starting at
3760 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3761 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3762 DW_AT_ranges). If NEED_PC is set, then this function will set
3763 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3764 and record the covered ranges in the addrmap. */
3765
3766 static void
3767 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3768 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3769 {
3770 struct partial_die_info *pdi;
3771
3772 /* Now, march along the PDI's, descending into ones which have
3773 interesting children but skipping the children of the other ones,
3774 until we reach the end of the compilation unit. */
3775
3776 pdi = first_die;
3777
3778 while (pdi != NULL)
3779 {
3780 fixup_partial_die (pdi, cu);
3781
3782 /* Anonymous namespaces or modules have no name but have interesting
3783 children, so we need to look at them. Ditto for anonymous
3784 enums. */
3785
3786 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3787 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3788 {
3789 switch (pdi->tag)
3790 {
3791 case DW_TAG_subprogram:
3792 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3793 break;
3794 case DW_TAG_constant:
3795 case DW_TAG_variable:
3796 case DW_TAG_typedef:
3797 case DW_TAG_union_type:
3798 if (!pdi->is_declaration)
3799 {
3800 add_partial_symbol (pdi, cu);
3801 }
3802 break;
3803 case DW_TAG_class_type:
3804 case DW_TAG_interface_type:
3805 case DW_TAG_structure_type:
3806 if (!pdi->is_declaration)
3807 {
3808 add_partial_symbol (pdi, cu);
3809 }
3810 break;
3811 case DW_TAG_enumeration_type:
3812 if (!pdi->is_declaration)
3813 add_partial_enumeration (pdi, cu);
3814 break;
3815 case DW_TAG_base_type:
3816 case DW_TAG_subrange_type:
3817 /* File scope base type definitions are added to the partial
3818 symbol table. */
3819 add_partial_symbol (pdi, cu);
3820 break;
3821 case DW_TAG_namespace:
3822 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3823 break;
3824 case DW_TAG_module:
3825 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3826 break;
3827 default:
3828 break;
3829 }
3830 }
3831
3832 /* If the die has a sibling, skip to the sibling. */
3833
3834 pdi = pdi->die_sibling;
3835 }
3836 }
3837
3838 /* Functions used to compute the fully scoped name of a partial DIE.
3839
3840 Normally, this is simple. For C++, the parent DIE's fully scoped
3841 name is concatenated with "::" and the partial DIE's name. For
3842 Java, the same thing occurs except that "." is used instead of "::".
3843 Enumerators are an exception; they use the scope of their parent
3844 enumeration type, i.e. the name of the enumeration type is not
3845 prepended to the enumerator.
3846
3847 There are two complexities. One is DW_AT_specification; in this
3848 case "parent" means the parent of the target of the specification,
3849 instead of the direct parent of the DIE. The other is compilers
3850 which do not emit DW_TAG_namespace; in this case we try to guess
3851 the fully qualified name of structure types from their members'
3852 linkage names. This must be done using the DIE's children rather
3853 than the children of any DW_AT_specification target. We only need
3854 to do this for structures at the top level, i.e. if the target of
3855 any DW_AT_specification (if any; otherwise the DIE itself) does not
3856 have a parent. */
3857
3858 /* Compute the scope prefix associated with PDI's parent, in
3859 compilation unit CU. The result will be allocated on CU's
3860 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3861 field. NULL is returned if no prefix is necessary. */
3862 static char *
3863 partial_die_parent_scope (struct partial_die_info *pdi,
3864 struct dwarf2_cu *cu)
3865 {
3866 char *grandparent_scope;
3867 struct partial_die_info *parent, *real_pdi;
3868
3869 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3870 then this means the parent of the specification DIE. */
3871
3872 real_pdi = pdi;
3873 while (real_pdi->has_specification)
3874 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3875
3876 parent = real_pdi->die_parent;
3877 if (parent == NULL)
3878 return NULL;
3879
3880 if (parent->scope_set)
3881 return parent->scope;
3882
3883 fixup_partial_die (parent, cu);
3884
3885 grandparent_scope = partial_die_parent_scope (parent, cu);
3886
3887 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3888 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3889 Work around this problem here. */
3890 if (cu->language == language_cplus
3891 && parent->tag == DW_TAG_namespace
3892 && strcmp (parent->name, "::") == 0
3893 && grandparent_scope == NULL)
3894 {
3895 parent->scope = NULL;
3896 parent->scope_set = 1;
3897 return NULL;
3898 }
3899
3900 if (parent->tag == DW_TAG_namespace
3901 || parent->tag == DW_TAG_module
3902 || parent->tag == DW_TAG_structure_type
3903 || parent->tag == DW_TAG_class_type
3904 || parent->tag == DW_TAG_interface_type
3905 || parent->tag == DW_TAG_union_type
3906 || parent->tag == DW_TAG_enumeration_type)
3907 {
3908 if (grandparent_scope == NULL)
3909 parent->scope = parent->name;
3910 else
3911 parent->scope = typename_concat (&cu->comp_unit_obstack,
3912 grandparent_scope,
3913 parent->name, 0, cu);
3914 }
3915 else if (parent->tag == DW_TAG_enumerator)
3916 /* Enumerators should not get the name of the enumeration as a prefix. */
3917 parent->scope = grandparent_scope;
3918 else
3919 {
3920 /* FIXME drow/2004-04-01: What should we be doing with
3921 function-local names? For partial symbols, we should probably be
3922 ignoring them. */
3923 complaint (&symfile_complaints,
3924 _("unhandled containing DIE tag %d for DIE at %d"),
3925 parent->tag, pdi->offset);
3926 parent->scope = grandparent_scope;
3927 }
3928
3929 parent->scope_set = 1;
3930 return parent->scope;
3931 }
3932
3933 /* Return the fully scoped name associated with PDI, from compilation unit
3934 CU. The result will be allocated with malloc. */
3935 static char *
3936 partial_die_full_name (struct partial_die_info *pdi,
3937 struct dwarf2_cu *cu)
3938 {
3939 char *parent_scope;
3940
3941 /* If this is a template instantiation, we can not work out the
3942 template arguments from partial DIEs. So, unfortunately, we have
3943 to go through the full DIEs. At least any work we do building
3944 types here will be reused if full symbols are loaded later. */
3945 if (pdi->has_template_arguments)
3946 {
3947 fixup_partial_die (pdi, cu);
3948
3949 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3950 {
3951 struct die_info *die;
3952 struct attribute attr;
3953 struct dwarf2_cu *ref_cu = cu;
3954
3955 attr.name = 0;
3956 attr.form = DW_FORM_ref_addr;
3957 attr.u.addr = pdi->offset;
3958 die = follow_die_ref (NULL, &attr, &ref_cu);
3959
3960 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3961 }
3962 }
3963
3964 parent_scope = partial_die_parent_scope (pdi, cu);
3965 if (parent_scope == NULL)
3966 return NULL;
3967 else
3968 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3969 }
3970
3971 static void
3972 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3973 {
3974 struct objfile *objfile = cu->objfile;
3975 CORE_ADDR addr = 0;
3976 char *actual_name = NULL;
3977 const struct partial_symbol *psym = NULL;
3978 CORE_ADDR baseaddr;
3979 int built_actual_name = 0;
3980
3981 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3982
3983 actual_name = partial_die_full_name (pdi, cu);
3984 if (actual_name)
3985 built_actual_name = 1;
3986
3987 if (actual_name == NULL)
3988 actual_name = pdi->name;
3989
3990 switch (pdi->tag)
3991 {
3992 case DW_TAG_subprogram:
3993 if (pdi->is_external || cu->language == language_ada)
3994 {
3995 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3996 of the global scope. But in Ada, we want to be able to access
3997 nested procedures globally. So all Ada subprograms are stored
3998 in the global scope. */
3999 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4000 mst_text, objfile); */
4001 add_psymbol_to_list (actual_name, strlen (actual_name),
4002 built_actual_name,
4003 VAR_DOMAIN, LOC_BLOCK,
4004 &objfile->global_psymbols,
4005 0, pdi->lowpc + baseaddr,
4006 cu->language, objfile);
4007 }
4008 else
4009 {
4010 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4011 mst_file_text, objfile); */
4012 add_psymbol_to_list (actual_name, strlen (actual_name),
4013 built_actual_name,
4014 VAR_DOMAIN, LOC_BLOCK,
4015 &objfile->static_psymbols,
4016 0, pdi->lowpc + baseaddr,
4017 cu->language, objfile);
4018 }
4019 break;
4020 case DW_TAG_constant:
4021 {
4022 struct psymbol_allocation_list *list;
4023
4024 if (pdi->is_external)
4025 list = &objfile->global_psymbols;
4026 else
4027 list = &objfile->static_psymbols;
4028 add_psymbol_to_list (actual_name, strlen (actual_name),
4029 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4030 list, 0, 0, cu->language, objfile);
4031 }
4032 break;
4033 case DW_TAG_variable:
4034 if (pdi->locdesc)
4035 addr = decode_locdesc (pdi->locdesc, cu);
4036
4037 if (pdi->locdesc
4038 && addr == 0
4039 && !dwarf2_per_objfile->has_section_at_zero)
4040 {
4041 /* A global or static variable may also have been stripped
4042 out by the linker if unused, in which case its address
4043 will be nullified; do not add such variables into partial
4044 symbol table then. */
4045 }
4046 else if (pdi->is_external)
4047 {
4048 /* Global Variable.
4049 Don't enter into the minimal symbol tables as there is
4050 a minimal symbol table entry from the ELF symbols already.
4051 Enter into partial symbol table if it has a location
4052 descriptor or a type.
4053 If the location descriptor is missing, new_symbol will create
4054 a LOC_UNRESOLVED symbol, the address of the variable will then
4055 be determined from the minimal symbol table whenever the variable
4056 is referenced.
4057 The address for the partial symbol table entry is not
4058 used by GDB, but it comes in handy for debugging partial symbol
4059 table building. */
4060
4061 if (pdi->locdesc || pdi->has_type)
4062 add_psymbol_to_list (actual_name, strlen (actual_name),
4063 built_actual_name,
4064 VAR_DOMAIN, LOC_STATIC,
4065 &objfile->global_psymbols,
4066 0, addr + baseaddr,
4067 cu->language, objfile);
4068 }
4069 else
4070 {
4071 /* Static Variable. Skip symbols without location descriptors. */
4072 if (pdi->locdesc == NULL)
4073 {
4074 if (built_actual_name)
4075 xfree (actual_name);
4076 return;
4077 }
4078 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4079 mst_file_data, objfile); */
4080 add_psymbol_to_list (actual_name, strlen (actual_name),
4081 built_actual_name,
4082 VAR_DOMAIN, LOC_STATIC,
4083 &objfile->static_psymbols,
4084 0, addr + baseaddr,
4085 cu->language, objfile);
4086 }
4087 break;
4088 case DW_TAG_typedef:
4089 case DW_TAG_base_type:
4090 case DW_TAG_subrange_type:
4091 add_psymbol_to_list (actual_name, strlen (actual_name),
4092 built_actual_name,
4093 VAR_DOMAIN, LOC_TYPEDEF,
4094 &objfile->static_psymbols,
4095 0, (CORE_ADDR) 0, cu->language, objfile);
4096 break;
4097 case DW_TAG_namespace:
4098 add_psymbol_to_list (actual_name, strlen (actual_name),
4099 built_actual_name,
4100 VAR_DOMAIN, LOC_TYPEDEF,
4101 &objfile->global_psymbols,
4102 0, (CORE_ADDR) 0, cu->language, objfile);
4103 break;
4104 case DW_TAG_class_type:
4105 case DW_TAG_interface_type:
4106 case DW_TAG_structure_type:
4107 case DW_TAG_union_type:
4108 case DW_TAG_enumeration_type:
4109 /* Skip external references. The DWARF standard says in the section
4110 about "Structure, Union, and Class Type Entries": "An incomplete
4111 structure, union or class type is represented by a structure,
4112 union or class entry that does not have a byte size attribute
4113 and that has a DW_AT_declaration attribute." */
4114 if (!pdi->has_byte_size && pdi->is_declaration)
4115 {
4116 if (built_actual_name)
4117 xfree (actual_name);
4118 return;
4119 }
4120
4121 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4122 static vs. global. */
4123 add_psymbol_to_list (actual_name, strlen (actual_name),
4124 built_actual_name,
4125 STRUCT_DOMAIN, LOC_TYPEDEF,
4126 (cu->language == language_cplus
4127 || cu->language == language_java)
4128 ? &objfile->global_psymbols
4129 : &objfile->static_psymbols,
4130 0, (CORE_ADDR) 0, cu->language, objfile);
4131
4132 break;
4133 case DW_TAG_enumerator:
4134 add_psymbol_to_list (actual_name, strlen (actual_name),
4135 built_actual_name,
4136 VAR_DOMAIN, LOC_CONST,
4137 (cu->language == language_cplus
4138 || cu->language == language_java)
4139 ? &objfile->global_psymbols
4140 : &objfile->static_psymbols,
4141 0, (CORE_ADDR) 0, cu->language, objfile);
4142 break;
4143 default:
4144 break;
4145 }
4146
4147 if (built_actual_name)
4148 xfree (actual_name);
4149 }
4150
4151 /* Read a partial die corresponding to a namespace; also, add a symbol
4152 corresponding to that namespace to the symbol table. NAMESPACE is
4153 the name of the enclosing namespace. */
4154
4155 static void
4156 add_partial_namespace (struct partial_die_info *pdi,
4157 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4158 int need_pc, struct dwarf2_cu *cu)
4159 {
4160 /* Add a symbol for the namespace. */
4161
4162 add_partial_symbol (pdi, cu);
4163
4164 /* Now scan partial symbols in that namespace. */
4165
4166 if (pdi->has_children)
4167 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4168 }
4169
4170 /* Read a partial die corresponding to a Fortran module. */
4171
4172 static void
4173 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4174 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4175 {
4176 /* Now scan partial symbols in that module. */
4177
4178 if (pdi->has_children)
4179 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4180 }
4181
4182 /* Read a partial die corresponding to a subprogram and create a partial
4183 symbol for that subprogram. When the CU language allows it, this
4184 routine also defines a partial symbol for each nested subprogram
4185 that this subprogram contains.
4186
4187 DIE my also be a lexical block, in which case we simply search
4188 recursively for suprograms defined inside that lexical block.
4189 Again, this is only performed when the CU language allows this
4190 type of definitions. */
4191
4192 static void
4193 add_partial_subprogram (struct partial_die_info *pdi,
4194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4195 int need_pc, struct dwarf2_cu *cu)
4196 {
4197 if (pdi->tag == DW_TAG_subprogram)
4198 {
4199 if (pdi->has_pc_info)
4200 {
4201 if (pdi->lowpc < *lowpc)
4202 *lowpc = pdi->lowpc;
4203 if (pdi->highpc > *highpc)
4204 *highpc = pdi->highpc;
4205 if (need_pc)
4206 {
4207 CORE_ADDR baseaddr;
4208 struct objfile *objfile = cu->objfile;
4209
4210 baseaddr = ANOFFSET (objfile->section_offsets,
4211 SECT_OFF_TEXT (objfile));
4212 addrmap_set_empty (objfile->psymtabs_addrmap,
4213 pdi->lowpc + baseaddr,
4214 pdi->highpc - 1 + baseaddr,
4215 cu->per_cu->v.psymtab);
4216 }
4217 if (!pdi->is_declaration)
4218 /* Ignore subprogram DIEs that do not have a name, they are
4219 illegal. Do not emit a complaint at this point, we will
4220 do so when we convert this psymtab into a symtab. */
4221 if (pdi->name)
4222 add_partial_symbol (pdi, cu);
4223 }
4224 }
4225
4226 if (! pdi->has_children)
4227 return;
4228
4229 if (cu->language == language_ada)
4230 {
4231 pdi = pdi->die_child;
4232 while (pdi != NULL)
4233 {
4234 fixup_partial_die (pdi, cu);
4235 if (pdi->tag == DW_TAG_subprogram
4236 || pdi->tag == DW_TAG_lexical_block)
4237 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4238 pdi = pdi->die_sibling;
4239 }
4240 }
4241 }
4242
4243 /* Read a partial die corresponding to an enumeration type. */
4244
4245 static void
4246 add_partial_enumeration (struct partial_die_info *enum_pdi,
4247 struct dwarf2_cu *cu)
4248 {
4249 struct partial_die_info *pdi;
4250
4251 if (enum_pdi->name != NULL)
4252 add_partial_symbol (enum_pdi, cu);
4253
4254 pdi = enum_pdi->die_child;
4255 while (pdi)
4256 {
4257 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4258 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4259 else
4260 add_partial_symbol (pdi, cu);
4261 pdi = pdi->die_sibling;
4262 }
4263 }
4264
4265 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4266 Return the corresponding abbrev, or NULL if the number is zero (indicating
4267 an empty DIE). In either case *BYTES_READ will be set to the length of
4268 the initial number. */
4269
4270 static struct abbrev_info *
4271 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4272 struct dwarf2_cu *cu)
4273 {
4274 bfd *abfd = cu->objfile->obfd;
4275 unsigned int abbrev_number;
4276 struct abbrev_info *abbrev;
4277
4278 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4279
4280 if (abbrev_number == 0)
4281 return NULL;
4282
4283 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4284 if (!abbrev)
4285 {
4286 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4287 abbrev_number, bfd_get_filename (abfd));
4288 }
4289
4290 return abbrev;
4291 }
4292
4293 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4294 Returns a pointer to the end of a series of DIEs, terminated by an empty
4295 DIE. Any children of the skipped DIEs will also be skipped. */
4296
4297 static gdb_byte *
4298 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4299 {
4300 struct abbrev_info *abbrev;
4301 unsigned int bytes_read;
4302
4303 while (1)
4304 {
4305 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4306 if (abbrev == NULL)
4307 return info_ptr + bytes_read;
4308 else
4309 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4310 }
4311 }
4312
4313 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4314 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4315 abbrev corresponding to that skipped uleb128 should be passed in
4316 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4317 children. */
4318
4319 static gdb_byte *
4320 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4321 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4322 {
4323 unsigned int bytes_read;
4324 struct attribute attr;
4325 bfd *abfd = cu->objfile->obfd;
4326 unsigned int form, i;
4327
4328 for (i = 0; i < abbrev->num_attrs; i++)
4329 {
4330 /* The only abbrev we care about is DW_AT_sibling. */
4331 if (abbrev->attrs[i].name == DW_AT_sibling)
4332 {
4333 read_attribute (&attr, &abbrev->attrs[i],
4334 abfd, info_ptr, cu);
4335 if (attr.form == DW_FORM_ref_addr)
4336 complaint (&symfile_complaints,
4337 _("ignoring absolute DW_AT_sibling"));
4338 else
4339 return buffer + dwarf2_get_ref_die_offset (&attr);
4340 }
4341
4342 /* If it isn't DW_AT_sibling, skip this attribute. */
4343 form = abbrev->attrs[i].form;
4344 skip_attribute:
4345 switch (form)
4346 {
4347 case DW_FORM_ref_addr:
4348 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4349 and later it is offset sized. */
4350 if (cu->header.version == 2)
4351 info_ptr += cu->header.addr_size;
4352 else
4353 info_ptr += cu->header.offset_size;
4354 break;
4355 case DW_FORM_addr:
4356 info_ptr += cu->header.addr_size;
4357 break;
4358 case DW_FORM_data1:
4359 case DW_FORM_ref1:
4360 case DW_FORM_flag:
4361 info_ptr += 1;
4362 break;
4363 case DW_FORM_flag_present:
4364 break;
4365 case DW_FORM_data2:
4366 case DW_FORM_ref2:
4367 info_ptr += 2;
4368 break;
4369 case DW_FORM_data4:
4370 case DW_FORM_ref4:
4371 info_ptr += 4;
4372 break;
4373 case DW_FORM_data8:
4374 case DW_FORM_ref8:
4375 case DW_FORM_ref_sig8:
4376 info_ptr += 8;
4377 break;
4378 case DW_FORM_string:
4379 read_direct_string (abfd, info_ptr, &bytes_read);
4380 info_ptr += bytes_read;
4381 break;
4382 case DW_FORM_sec_offset:
4383 case DW_FORM_strp:
4384 info_ptr += cu->header.offset_size;
4385 break;
4386 case DW_FORM_exprloc:
4387 case DW_FORM_block:
4388 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4389 info_ptr += bytes_read;
4390 break;
4391 case DW_FORM_block1:
4392 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4393 break;
4394 case DW_FORM_block2:
4395 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4396 break;
4397 case DW_FORM_block4:
4398 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4399 break;
4400 case DW_FORM_sdata:
4401 case DW_FORM_udata:
4402 case DW_FORM_ref_udata:
4403 info_ptr = skip_leb128 (abfd, info_ptr);
4404 break;
4405 case DW_FORM_indirect:
4406 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4407 info_ptr += bytes_read;
4408 /* We need to continue parsing from here, so just go back to
4409 the top. */
4410 goto skip_attribute;
4411
4412 default:
4413 error (_("Dwarf Error: Cannot handle %s "
4414 "in DWARF reader [in module %s]"),
4415 dwarf_form_name (form),
4416 bfd_get_filename (abfd));
4417 }
4418 }
4419
4420 if (abbrev->has_children)
4421 return skip_children (buffer, info_ptr, cu);
4422 else
4423 return info_ptr;
4424 }
4425
4426 /* Locate ORIG_PDI's sibling.
4427 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4428 in BUFFER. */
4429
4430 static gdb_byte *
4431 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4432 gdb_byte *buffer, gdb_byte *info_ptr,
4433 bfd *abfd, struct dwarf2_cu *cu)
4434 {
4435 /* Do we know the sibling already? */
4436
4437 if (orig_pdi->sibling)
4438 return orig_pdi->sibling;
4439
4440 /* Are there any children to deal with? */
4441
4442 if (!orig_pdi->has_children)
4443 return info_ptr;
4444
4445 /* Skip the children the long way. */
4446
4447 return skip_children (buffer, info_ptr, cu);
4448 }
4449
4450 /* Expand this partial symbol table into a full symbol table. */
4451
4452 static void
4453 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4454 {
4455 if (pst != NULL)
4456 {
4457 if (pst->readin)
4458 {
4459 warning (_("bug: psymtab for %s is already read in."),
4460 pst->filename);
4461 }
4462 else
4463 {
4464 if (info_verbose)
4465 {
4466 printf_filtered (_("Reading in symbols for %s..."),
4467 pst->filename);
4468 gdb_flush (gdb_stdout);
4469 }
4470
4471 /* Restore our global data. */
4472 dwarf2_per_objfile = objfile_data (pst->objfile,
4473 dwarf2_objfile_data_key);
4474
4475 /* If this psymtab is constructed from a debug-only objfile, the
4476 has_section_at_zero flag will not necessarily be correct. We
4477 can get the correct value for this flag by looking at the data
4478 associated with the (presumably stripped) associated objfile. */
4479 if (pst->objfile->separate_debug_objfile_backlink)
4480 {
4481 struct dwarf2_per_objfile *dpo_backlink
4482 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4483 dwarf2_objfile_data_key);
4484
4485 dwarf2_per_objfile->has_section_at_zero
4486 = dpo_backlink->has_section_at_zero;
4487 }
4488
4489 dwarf2_per_objfile->reading_partial_symbols = 0;
4490
4491 psymtab_to_symtab_1 (pst);
4492
4493 /* Finish up the debug error message. */
4494 if (info_verbose)
4495 printf_filtered (_("done.\n"));
4496 }
4497 }
4498 }
4499
4500 /* Add PER_CU to the queue. */
4501
4502 static void
4503 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4504 {
4505 struct dwarf2_queue_item *item;
4506
4507 per_cu->queued = 1;
4508 item = xmalloc (sizeof (*item));
4509 item->per_cu = per_cu;
4510 item->next = NULL;
4511
4512 if (dwarf2_queue == NULL)
4513 dwarf2_queue = item;
4514 else
4515 dwarf2_queue_tail->next = item;
4516
4517 dwarf2_queue_tail = item;
4518 }
4519
4520 /* Process the queue. */
4521
4522 static void
4523 process_queue (struct objfile *objfile)
4524 {
4525 struct dwarf2_queue_item *item, *next_item;
4526
4527 /* The queue starts out with one item, but following a DIE reference
4528 may load a new CU, adding it to the end of the queue. */
4529 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4530 {
4531 if (dwarf2_per_objfile->using_index
4532 ? !item->per_cu->v.quick->symtab
4533 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4534 process_full_comp_unit (item->per_cu);
4535
4536 item->per_cu->queued = 0;
4537 next_item = item->next;
4538 xfree (item);
4539 }
4540
4541 dwarf2_queue_tail = NULL;
4542 }
4543
4544 /* Free all allocated queue entries. This function only releases anything if
4545 an error was thrown; if the queue was processed then it would have been
4546 freed as we went along. */
4547
4548 static void
4549 dwarf2_release_queue (void *dummy)
4550 {
4551 struct dwarf2_queue_item *item, *last;
4552
4553 item = dwarf2_queue;
4554 while (item)
4555 {
4556 /* Anything still marked queued is likely to be in an
4557 inconsistent state, so discard it. */
4558 if (item->per_cu->queued)
4559 {
4560 if (item->per_cu->cu != NULL)
4561 free_one_cached_comp_unit (item->per_cu->cu);
4562 item->per_cu->queued = 0;
4563 }
4564
4565 last = item;
4566 item = item->next;
4567 xfree (last);
4568 }
4569
4570 dwarf2_queue = dwarf2_queue_tail = NULL;
4571 }
4572
4573 /* Read in full symbols for PST, and anything it depends on. */
4574
4575 static void
4576 psymtab_to_symtab_1 (struct partial_symtab *pst)
4577 {
4578 struct dwarf2_per_cu_data *per_cu;
4579 struct cleanup *back_to;
4580 int i;
4581
4582 for (i = 0; i < pst->number_of_dependencies; i++)
4583 if (!pst->dependencies[i]->readin)
4584 {
4585 /* Inform about additional files that need to be read in. */
4586 if (info_verbose)
4587 {
4588 /* FIXME: i18n: Need to make this a single string. */
4589 fputs_filtered (" ", gdb_stdout);
4590 wrap_here ("");
4591 fputs_filtered ("and ", gdb_stdout);
4592 wrap_here ("");
4593 printf_filtered ("%s...", pst->dependencies[i]->filename);
4594 wrap_here (""); /* Flush output. */
4595 gdb_flush (gdb_stdout);
4596 }
4597 psymtab_to_symtab_1 (pst->dependencies[i]);
4598 }
4599
4600 per_cu = pst->read_symtab_private;
4601
4602 if (per_cu == NULL)
4603 {
4604 /* It's an include file, no symbols to read for it.
4605 Everything is in the parent symtab. */
4606 pst->readin = 1;
4607 return;
4608 }
4609
4610 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4611 }
4612
4613 /* Load the DIEs associated with PER_CU into memory. */
4614
4615 static void
4616 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4617 struct objfile *objfile)
4618 {
4619 bfd *abfd = objfile->obfd;
4620 struct dwarf2_cu *cu;
4621 unsigned int offset;
4622 gdb_byte *info_ptr, *beg_of_comp_unit;
4623 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4624 struct attribute *attr;
4625 int read_cu = 0;
4626
4627 gdb_assert (! per_cu->debug_type_section);
4628
4629 /* Set local variables from the partial symbol table info. */
4630 offset = per_cu->offset;
4631
4632 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4633 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4634 beg_of_comp_unit = info_ptr;
4635
4636 if (per_cu->cu == NULL)
4637 {
4638 cu = xmalloc (sizeof (*cu));
4639 init_one_comp_unit (cu, objfile);
4640
4641 read_cu = 1;
4642
4643 /* If an error occurs while loading, release our storage. */
4644 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4645
4646 /* Read in the comp_unit header. */
4647 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4648
4649 /* Complete the cu_header. */
4650 cu->header.offset = offset;
4651 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4652
4653 /* Read the abbrevs for this compilation unit. */
4654 dwarf2_read_abbrevs (abfd, cu);
4655 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4656
4657 /* Link this compilation unit into the compilation unit tree. */
4658 per_cu->cu = cu;
4659 cu->per_cu = per_cu;
4660
4661 /* Link this CU into read_in_chain. */
4662 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4663 dwarf2_per_objfile->read_in_chain = per_cu;
4664 }
4665 else
4666 {
4667 cu = per_cu->cu;
4668 info_ptr += cu->header.first_die_offset;
4669 }
4670
4671 cu->dies = read_comp_unit (info_ptr, cu);
4672
4673 /* We try not to read any attributes in this function, because not
4674 all objfiles needed for references have been loaded yet, and symbol
4675 table processing isn't initialized. But we have to set the CU language,
4676 or we won't be able to build types correctly. */
4677 prepare_one_comp_unit (cu, cu->dies);
4678
4679 /* Similarly, if we do not read the producer, we can not apply
4680 producer-specific interpretation. */
4681 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4682 if (attr)
4683 cu->producer = DW_STRING (attr);
4684
4685 if (read_cu)
4686 {
4687 do_cleanups (free_abbrevs_cleanup);
4688
4689 /* We've successfully allocated this compilation unit. Let our
4690 caller clean it up when finished with it. */
4691 discard_cleanups (free_cu_cleanup);
4692 }
4693 }
4694
4695 /* Add a DIE to the delayed physname list. */
4696
4697 static void
4698 add_to_method_list (struct type *type, int fnfield_index, int index,
4699 const char *name, struct die_info *die,
4700 struct dwarf2_cu *cu)
4701 {
4702 struct delayed_method_info mi;
4703 mi.type = type;
4704 mi.fnfield_index = fnfield_index;
4705 mi.index = index;
4706 mi.name = name;
4707 mi.die = die;
4708 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4709 }
4710
4711 /* A cleanup for freeing the delayed method list. */
4712
4713 static void
4714 free_delayed_list (void *ptr)
4715 {
4716 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4717 if (cu->method_list != NULL)
4718 {
4719 VEC_free (delayed_method_info, cu->method_list);
4720 cu->method_list = NULL;
4721 }
4722 }
4723
4724 /* Compute the physnames of any methods on the CU's method list.
4725
4726 The computation of method physnames is delayed in order to avoid the
4727 (bad) condition that one of the method's formal parameters is of an as yet
4728 incomplete type. */
4729
4730 static void
4731 compute_delayed_physnames (struct dwarf2_cu *cu)
4732 {
4733 int i;
4734 struct delayed_method_info *mi;
4735 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4736 {
4737 const char *physname;
4738 struct fn_fieldlist *fn_flp
4739 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4740 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4741 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4742 }
4743 }
4744
4745 /* Generate full symbol information for PST and CU, whose DIEs have
4746 already been loaded into memory. */
4747
4748 static void
4749 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4750 {
4751 struct dwarf2_cu *cu = per_cu->cu;
4752 struct objfile *objfile = per_cu->objfile;
4753 CORE_ADDR lowpc, highpc;
4754 struct symtab *symtab;
4755 struct cleanup *back_to, *delayed_list_cleanup;
4756 CORE_ADDR baseaddr;
4757
4758 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4759
4760 buildsym_init ();
4761 back_to = make_cleanup (really_free_pendings, NULL);
4762 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4763
4764 cu->list_in_scope = &file_symbols;
4765
4766 /* Do line number decoding in read_file_scope () */
4767 process_die (cu->dies, cu);
4768
4769 /* Now that we have processed all the DIEs in the CU, all the types
4770 should be complete, and it should now be safe to compute all of the
4771 physnames. */
4772 compute_delayed_physnames (cu);
4773 do_cleanups (delayed_list_cleanup);
4774
4775 /* Some compilers don't define a DW_AT_high_pc attribute for the
4776 compilation unit. If the DW_AT_high_pc is missing, synthesize
4777 it, by scanning the DIE's below the compilation unit. */
4778 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4779
4780 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4781
4782 if (symtab != NULL)
4783 {
4784 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4785
4786 /* Set symtab language to language from DW_AT_language. If the
4787 compilation is from a C file generated by language preprocessors, do
4788 not set the language if it was already deduced by start_subfile. */
4789 if (!(cu->language == language_c && symtab->language != language_c))
4790 symtab->language = cu->language;
4791
4792 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4793 produce DW_AT_location with location lists but it can be possibly
4794 invalid without -fvar-tracking.
4795
4796 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4797 needed, it would be wrong due to missing DW_AT_producer there.
4798
4799 Still one can confuse GDB by using non-standard GCC compilation
4800 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4801 */
4802 if (cu->has_loclist && gcc_4_minor >= 0)
4803 symtab->locations_valid = 1;
4804
4805 if (gcc_4_minor >= 5)
4806 symtab->epilogue_unwind_valid = 1;
4807
4808 symtab->call_site_htab = cu->call_site_htab;
4809 }
4810
4811 if (dwarf2_per_objfile->using_index)
4812 per_cu->v.quick->symtab = symtab;
4813 else
4814 {
4815 struct partial_symtab *pst = per_cu->v.psymtab;
4816 pst->symtab = symtab;
4817 pst->readin = 1;
4818 }
4819
4820 do_cleanups (back_to);
4821 }
4822
4823 /* Process a die and its children. */
4824
4825 static void
4826 process_die (struct die_info *die, struct dwarf2_cu *cu)
4827 {
4828 switch (die->tag)
4829 {
4830 case DW_TAG_padding:
4831 break;
4832 case DW_TAG_compile_unit:
4833 read_file_scope (die, cu);
4834 break;
4835 case DW_TAG_type_unit:
4836 read_type_unit_scope (die, cu);
4837 break;
4838 case DW_TAG_subprogram:
4839 case DW_TAG_inlined_subroutine:
4840 read_func_scope (die, cu);
4841 break;
4842 case DW_TAG_lexical_block:
4843 case DW_TAG_try_block:
4844 case DW_TAG_catch_block:
4845 read_lexical_block_scope (die, cu);
4846 break;
4847 case DW_TAG_GNU_call_site:
4848 read_call_site_scope (die, cu);
4849 break;
4850 case DW_TAG_class_type:
4851 case DW_TAG_interface_type:
4852 case DW_TAG_structure_type:
4853 case DW_TAG_union_type:
4854 process_structure_scope (die, cu);
4855 break;
4856 case DW_TAG_enumeration_type:
4857 process_enumeration_scope (die, cu);
4858 break;
4859
4860 /* These dies have a type, but processing them does not create
4861 a symbol or recurse to process the children. Therefore we can
4862 read them on-demand through read_type_die. */
4863 case DW_TAG_subroutine_type:
4864 case DW_TAG_set_type:
4865 case DW_TAG_array_type:
4866 case DW_TAG_pointer_type:
4867 case DW_TAG_ptr_to_member_type:
4868 case DW_TAG_reference_type:
4869 case DW_TAG_string_type:
4870 break;
4871
4872 case DW_TAG_base_type:
4873 case DW_TAG_subrange_type:
4874 case DW_TAG_typedef:
4875 /* Add a typedef symbol for the type definition, if it has a
4876 DW_AT_name. */
4877 new_symbol (die, read_type_die (die, cu), cu);
4878 break;
4879 case DW_TAG_common_block:
4880 read_common_block (die, cu);
4881 break;
4882 case DW_TAG_common_inclusion:
4883 break;
4884 case DW_TAG_namespace:
4885 processing_has_namespace_info = 1;
4886 read_namespace (die, cu);
4887 break;
4888 case DW_TAG_module:
4889 processing_has_namespace_info = 1;
4890 read_module (die, cu);
4891 break;
4892 case DW_TAG_imported_declaration:
4893 case DW_TAG_imported_module:
4894 processing_has_namespace_info = 1;
4895 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4896 || cu->language != language_fortran))
4897 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4898 dwarf_tag_name (die->tag));
4899 read_import_statement (die, cu);
4900 break;
4901 default:
4902 new_symbol (die, NULL, cu);
4903 break;
4904 }
4905 }
4906
4907 /* A helper function for dwarf2_compute_name which determines whether DIE
4908 needs to have the name of the scope prepended to the name listed in the
4909 die. */
4910
4911 static int
4912 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4913 {
4914 struct attribute *attr;
4915
4916 switch (die->tag)
4917 {
4918 case DW_TAG_namespace:
4919 case DW_TAG_typedef:
4920 case DW_TAG_class_type:
4921 case DW_TAG_interface_type:
4922 case DW_TAG_structure_type:
4923 case DW_TAG_union_type:
4924 case DW_TAG_enumeration_type:
4925 case DW_TAG_enumerator:
4926 case DW_TAG_subprogram:
4927 case DW_TAG_member:
4928 return 1;
4929
4930 case DW_TAG_variable:
4931 case DW_TAG_constant:
4932 /* We only need to prefix "globally" visible variables. These include
4933 any variable marked with DW_AT_external or any variable that
4934 lives in a namespace. [Variables in anonymous namespaces
4935 require prefixing, but they are not DW_AT_external.] */
4936
4937 if (dwarf2_attr (die, DW_AT_specification, cu))
4938 {
4939 struct dwarf2_cu *spec_cu = cu;
4940
4941 return die_needs_namespace (die_specification (die, &spec_cu),
4942 spec_cu);
4943 }
4944
4945 attr = dwarf2_attr (die, DW_AT_external, cu);
4946 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4947 && die->parent->tag != DW_TAG_module)
4948 return 0;
4949 /* A variable in a lexical block of some kind does not need a
4950 namespace, even though in C++ such variables may be external
4951 and have a mangled name. */
4952 if (die->parent->tag == DW_TAG_lexical_block
4953 || die->parent->tag == DW_TAG_try_block
4954 || die->parent->tag == DW_TAG_catch_block
4955 || die->parent->tag == DW_TAG_subprogram)
4956 return 0;
4957 return 1;
4958
4959 default:
4960 return 0;
4961 }
4962 }
4963
4964 /* Retrieve the last character from a mem_file. */
4965
4966 static void
4967 do_ui_file_peek_last (void *object, const char *buffer, long length)
4968 {
4969 char *last_char_p = (char *) object;
4970
4971 if (length > 0)
4972 *last_char_p = buffer[length - 1];
4973 }
4974
4975 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4976 compute the physname for the object, which include a method's
4977 formal parameters (C++/Java) and return type (Java).
4978
4979 For Ada, return the DIE's linkage name rather than the fully qualified
4980 name. PHYSNAME is ignored..
4981
4982 The result is allocated on the objfile_obstack and canonicalized. */
4983
4984 static const char *
4985 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4986 int physname)
4987 {
4988 if (name == NULL)
4989 name = dwarf2_name (die, cu);
4990
4991 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4992 compute it by typename_concat inside GDB. */
4993 if (cu->language == language_ada
4994 || (cu->language == language_fortran && physname))
4995 {
4996 /* For Ada unit, we prefer the linkage name over the name, as
4997 the former contains the exported name, which the user expects
4998 to be able to reference. Ideally, we want the user to be able
4999 to reference this entity using either natural or linkage name,
5000 but we haven't started looking at this enhancement yet. */
5001 struct attribute *attr;
5002
5003 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5004 if (attr == NULL)
5005 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5006 if (attr && DW_STRING (attr))
5007 return DW_STRING (attr);
5008 }
5009
5010 /* These are the only languages we know how to qualify names in. */
5011 if (name != NULL
5012 && (cu->language == language_cplus || cu->language == language_java
5013 || cu->language == language_fortran))
5014 {
5015 if (die_needs_namespace (die, cu))
5016 {
5017 long length;
5018 char *prefix;
5019 struct ui_file *buf;
5020
5021 prefix = determine_prefix (die, cu);
5022 buf = mem_fileopen ();
5023 if (*prefix != '\0')
5024 {
5025 char *prefixed_name = typename_concat (NULL, prefix, name,
5026 physname, cu);
5027
5028 fputs_unfiltered (prefixed_name, buf);
5029 xfree (prefixed_name);
5030 }
5031 else
5032 fputs_unfiltered (name, buf);
5033
5034 /* Template parameters may be specified in the DIE's DW_AT_name, or
5035 as children with DW_TAG_template_type_param or
5036 DW_TAG_value_type_param. If the latter, add them to the name
5037 here. If the name already has template parameters, then
5038 skip this step; some versions of GCC emit both, and
5039 it is more efficient to use the pre-computed name.
5040
5041 Something to keep in mind about this process: it is very
5042 unlikely, or in some cases downright impossible, to produce
5043 something that will match the mangled name of a function.
5044 If the definition of the function has the same debug info,
5045 we should be able to match up with it anyway. But fallbacks
5046 using the minimal symbol, for instance to find a method
5047 implemented in a stripped copy of libstdc++, will not work.
5048 If we do not have debug info for the definition, we will have to
5049 match them up some other way.
5050
5051 When we do name matching there is a related problem with function
5052 templates; two instantiated function templates are allowed to
5053 differ only by their return types, which we do not add here. */
5054
5055 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5056 {
5057 struct attribute *attr;
5058 struct die_info *child;
5059 int first = 1;
5060
5061 die->building_fullname = 1;
5062
5063 for (child = die->child; child != NULL; child = child->sibling)
5064 {
5065 struct type *type;
5066 long value;
5067 gdb_byte *bytes;
5068 struct dwarf2_locexpr_baton *baton;
5069 struct value *v;
5070
5071 if (child->tag != DW_TAG_template_type_param
5072 && child->tag != DW_TAG_template_value_param)
5073 continue;
5074
5075 if (first)
5076 {
5077 fputs_unfiltered ("<", buf);
5078 first = 0;
5079 }
5080 else
5081 fputs_unfiltered (", ", buf);
5082
5083 attr = dwarf2_attr (child, DW_AT_type, cu);
5084 if (attr == NULL)
5085 {
5086 complaint (&symfile_complaints,
5087 _("template parameter missing DW_AT_type"));
5088 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5089 continue;
5090 }
5091 type = die_type (child, cu);
5092
5093 if (child->tag == DW_TAG_template_type_param)
5094 {
5095 c_print_type (type, "", buf, -1, 0);
5096 continue;
5097 }
5098
5099 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5100 if (attr == NULL)
5101 {
5102 complaint (&symfile_complaints,
5103 _("template parameter missing "
5104 "DW_AT_const_value"));
5105 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5106 continue;
5107 }
5108
5109 dwarf2_const_value_attr (attr, type, name,
5110 &cu->comp_unit_obstack, cu,
5111 &value, &bytes, &baton);
5112
5113 if (TYPE_NOSIGN (type))
5114 /* GDB prints characters as NUMBER 'CHAR'. If that's
5115 changed, this can use value_print instead. */
5116 c_printchar (value, type, buf);
5117 else
5118 {
5119 struct value_print_options opts;
5120
5121 if (baton != NULL)
5122 v = dwarf2_evaluate_loc_desc (type, NULL,
5123 baton->data,
5124 baton->size,
5125 baton->per_cu);
5126 else if (bytes != NULL)
5127 {
5128 v = allocate_value (type);
5129 memcpy (value_contents_writeable (v), bytes,
5130 TYPE_LENGTH (type));
5131 }
5132 else
5133 v = value_from_longest (type, value);
5134
5135 /* Specify decimal so that we do not depend on
5136 the radix. */
5137 get_formatted_print_options (&opts, 'd');
5138 opts.raw = 1;
5139 value_print (v, buf, &opts);
5140 release_value (v);
5141 value_free (v);
5142 }
5143 }
5144
5145 die->building_fullname = 0;
5146
5147 if (!first)
5148 {
5149 /* Close the argument list, with a space if necessary
5150 (nested templates). */
5151 char last_char = '\0';
5152 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5153 if (last_char == '>')
5154 fputs_unfiltered (" >", buf);
5155 else
5156 fputs_unfiltered (">", buf);
5157 }
5158 }
5159
5160 /* For Java and C++ methods, append formal parameter type
5161 information, if PHYSNAME. */
5162
5163 if (physname && die->tag == DW_TAG_subprogram
5164 && (cu->language == language_cplus
5165 || cu->language == language_java))
5166 {
5167 struct type *type = read_type_die (die, cu);
5168
5169 c_type_print_args (type, buf, 1, cu->language);
5170
5171 if (cu->language == language_java)
5172 {
5173 /* For java, we must append the return type to method
5174 names. */
5175 if (die->tag == DW_TAG_subprogram)
5176 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5177 0, 0);
5178 }
5179 else if (cu->language == language_cplus)
5180 {
5181 /* Assume that an artificial first parameter is
5182 "this", but do not crash if it is not. RealView
5183 marks unnamed (and thus unused) parameters as
5184 artificial; there is no way to differentiate
5185 the two cases. */
5186 if (TYPE_NFIELDS (type) > 0
5187 && TYPE_FIELD_ARTIFICIAL (type, 0)
5188 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5189 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5190 0))))
5191 fputs_unfiltered (" const", buf);
5192 }
5193 }
5194
5195 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5196 &length);
5197 ui_file_delete (buf);
5198
5199 if (cu->language == language_cplus)
5200 {
5201 char *cname
5202 = dwarf2_canonicalize_name (name, cu,
5203 &cu->objfile->objfile_obstack);
5204
5205 if (cname != NULL)
5206 name = cname;
5207 }
5208 }
5209 }
5210
5211 return name;
5212 }
5213
5214 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5215 If scope qualifiers are appropriate they will be added. The result
5216 will be allocated on the objfile_obstack, or NULL if the DIE does
5217 not have a name. NAME may either be from a previous call to
5218 dwarf2_name or NULL.
5219
5220 The output string will be canonicalized (if C++/Java). */
5221
5222 static const char *
5223 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5224 {
5225 return dwarf2_compute_name (name, die, cu, 0);
5226 }
5227
5228 /* Construct a physname for the given DIE in CU. NAME may either be
5229 from a previous call to dwarf2_name or NULL. The result will be
5230 allocated on the objfile_objstack or NULL if the DIE does not have a
5231 name.
5232
5233 The output string will be canonicalized (if C++/Java). */
5234
5235 static const char *
5236 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5237 {
5238 struct attribute *attr;
5239 const char *retval, *mangled = NULL, *canon = NULL;
5240 struct cleanup *back_to;
5241 int need_copy = 1;
5242
5243 /* In this case dwarf2_compute_name is just a shortcut not building anything
5244 on its own. */
5245 if (!die_needs_namespace (die, cu))
5246 return dwarf2_compute_name (name, die, cu, 1);
5247
5248 back_to = make_cleanup (null_cleanup, NULL);
5249
5250 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5251 if (!attr)
5252 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5253
5254 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5255 has computed. */
5256 if (attr && DW_STRING (attr))
5257 {
5258 char *demangled;
5259
5260 mangled = DW_STRING (attr);
5261
5262 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5263 type. It is easier for GDB users to search for such functions as
5264 `name(params)' than `long name(params)'. In such case the minimal
5265 symbol names do not match the full symbol names but for template
5266 functions there is never a need to look up their definition from their
5267 declaration so the only disadvantage remains the minimal symbol
5268 variant `long name(params)' does not have the proper inferior type.
5269 */
5270
5271 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5272 | (cu->language == language_java
5273 ? DMGL_JAVA | DMGL_RET_POSTFIX
5274 : DMGL_RET_DROP)));
5275 if (demangled)
5276 {
5277 make_cleanup (xfree, demangled);
5278 canon = demangled;
5279 }
5280 else
5281 {
5282 canon = mangled;
5283 need_copy = 0;
5284 }
5285 }
5286
5287 if (canon == NULL || check_physname)
5288 {
5289 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5290
5291 if (canon != NULL && strcmp (physname, canon) != 0)
5292 {
5293 /* It may not mean a bug in GDB. The compiler could also
5294 compute DW_AT_linkage_name incorrectly. But in such case
5295 GDB would need to be bug-to-bug compatible. */
5296
5297 complaint (&symfile_complaints,
5298 _("Computed physname <%s> does not match demangled <%s> "
5299 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5300 physname, canon, mangled, die->offset, cu->objfile->name);
5301
5302 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5303 is available here - over computed PHYSNAME. It is safer
5304 against both buggy GDB and buggy compilers. */
5305
5306 retval = canon;
5307 }
5308 else
5309 {
5310 retval = physname;
5311 need_copy = 0;
5312 }
5313 }
5314 else
5315 retval = canon;
5316
5317 if (need_copy)
5318 retval = obsavestring (retval, strlen (retval),
5319 &cu->objfile->objfile_obstack);
5320
5321 do_cleanups (back_to);
5322 return retval;
5323 }
5324
5325 /* Read the import statement specified by the given die and record it. */
5326
5327 static void
5328 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5329 {
5330 struct attribute *import_attr;
5331 struct die_info *imported_die, *child_die;
5332 struct dwarf2_cu *imported_cu;
5333 const char *imported_name;
5334 const char *imported_name_prefix;
5335 const char *canonical_name;
5336 const char *import_alias;
5337 const char *imported_declaration = NULL;
5338 const char *import_prefix;
5339 VEC (const_char_ptr) *excludes = NULL;
5340 struct cleanup *cleanups;
5341
5342 char *temp;
5343
5344 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5345 if (import_attr == NULL)
5346 {
5347 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5348 dwarf_tag_name (die->tag));
5349 return;
5350 }
5351
5352 imported_cu = cu;
5353 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5354 imported_name = dwarf2_name (imported_die, imported_cu);
5355 if (imported_name == NULL)
5356 {
5357 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5358
5359 The import in the following code:
5360 namespace A
5361 {
5362 typedef int B;
5363 }
5364
5365 int main ()
5366 {
5367 using A::B;
5368 B b;
5369 return b;
5370 }
5371
5372 ...
5373 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5374 <52> DW_AT_decl_file : 1
5375 <53> DW_AT_decl_line : 6
5376 <54> DW_AT_import : <0x75>
5377 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5378 <59> DW_AT_name : B
5379 <5b> DW_AT_decl_file : 1
5380 <5c> DW_AT_decl_line : 2
5381 <5d> DW_AT_type : <0x6e>
5382 ...
5383 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5384 <76> DW_AT_byte_size : 4
5385 <77> DW_AT_encoding : 5 (signed)
5386
5387 imports the wrong die ( 0x75 instead of 0x58 ).
5388 This case will be ignored until the gcc bug is fixed. */
5389 return;
5390 }
5391
5392 /* Figure out the local name after import. */
5393 import_alias = dwarf2_name (die, cu);
5394
5395 /* Figure out where the statement is being imported to. */
5396 import_prefix = determine_prefix (die, cu);
5397
5398 /* Figure out what the scope of the imported die is and prepend it
5399 to the name of the imported die. */
5400 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5401
5402 if (imported_die->tag != DW_TAG_namespace
5403 && imported_die->tag != DW_TAG_module)
5404 {
5405 imported_declaration = imported_name;
5406 canonical_name = imported_name_prefix;
5407 }
5408 else if (strlen (imported_name_prefix) > 0)
5409 {
5410 temp = alloca (strlen (imported_name_prefix)
5411 + 2 + strlen (imported_name) + 1);
5412 strcpy (temp, imported_name_prefix);
5413 strcat (temp, "::");
5414 strcat (temp, imported_name);
5415 canonical_name = temp;
5416 }
5417 else
5418 canonical_name = imported_name;
5419
5420 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5421
5422 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5423 for (child_die = die->child; child_die && child_die->tag;
5424 child_die = sibling_die (child_die))
5425 {
5426 /* DWARF-4: A Fortran use statement with a “rename list” may be
5427 represented by an imported module entry with an import attribute
5428 referring to the module and owned entries corresponding to those
5429 entities that are renamed as part of being imported. */
5430
5431 if (child_die->tag != DW_TAG_imported_declaration)
5432 {
5433 complaint (&symfile_complaints,
5434 _("child DW_TAG_imported_declaration expected "
5435 "- DIE at 0x%x [in module %s]"),
5436 child_die->offset, cu->objfile->name);
5437 continue;
5438 }
5439
5440 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5441 if (import_attr == NULL)
5442 {
5443 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5444 dwarf_tag_name (child_die->tag));
5445 continue;
5446 }
5447
5448 imported_cu = cu;
5449 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5450 &imported_cu);
5451 imported_name = dwarf2_name (imported_die, imported_cu);
5452 if (imported_name == NULL)
5453 {
5454 complaint (&symfile_complaints,
5455 _("child DW_TAG_imported_declaration has unknown "
5456 "imported name - DIE at 0x%x [in module %s]"),
5457 child_die->offset, cu->objfile->name);
5458 continue;
5459 }
5460
5461 VEC_safe_push (const_char_ptr, excludes, imported_name);
5462
5463 process_die (child_die, cu);
5464 }
5465
5466 cp_add_using_directive (import_prefix,
5467 canonical_name,
5468 import_alias,
5469 imported_declaration,
5470 excludes,
5471 &cu->objfile->objfile_obstack);
5472
5473 do_cleanups (cleanups);
5474 }
5475
5476 static void
5477 initialize_cu_func_list (struct dwarf2_cu *cu)
5478 {
5479 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5480 }
5481
5482 /* Cleanup function for read_file_scope. */
5483
5484 static void
5485 free_cu_line_header (void *arg)
5486 {
5487 struct dwarf2_cu *cu = arg;
5488
5489 free_line_header (cu->line_header);
5490 cu->line_header = NULL;
5491 }
5492
5493 static void
5494 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5495 char **name, char **comp_dir)
5496 {
5497 struct attribute *attr;
5498
5499 *name = NULL;
5500 *comp_dir = NULL;
5501
5502 /* Find the filename. Do not use dwarf2_name here, since the filename
5503 is not a source language identifier. */
5504 attr = dwarf2_attr (die, DW_AT_name, cu);
5505 if (attr)
5506 {
5507 *name = DW_STRING (attr);
5508 }
5509
5510 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5511 if (attr)
5512 *comp_dir = DW_STRING (attr);
5513 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5514 {
5515 *comp_dir = ldirname (*name);
5516 if (*comp_dir != NULL)
5517 make_cleanup (xfree, *comp_dir);
5518 }
5519 if (*comp_dir != NULL)
5520 {
5521 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5522 directory, get rid of it. */
5523 char *cp = strchr (*comp_dir, ':');
5524
5525 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5526 *comp_dir = cp + 1;
5527 }
5528
5529 if (*name == NULL)
5530 *name = "<unknown>";
5531 }
5532
5533 /* Handle DW_AT_stmt_list for a compilation unit. */
5534
5535 static void
5536 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5537 const char *comp_dir)
5538 {
5539 struct attribute *attr;
5540 struct objfile *objfile = cu->objfile;
5541 bfd *abfd = objfile->obfd;
5542
5543 /* Decode line number information if present. We do this before
5544 processing child DIEs, so that the line header table is available
5545 for DW_AT_decl_file. */
5546 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5547 if (attr)
5548 {
5549 unsigned int line_offset = DW_UNSND (attr);
5550 struct line_header *line_header
5551 = dwarf_decode_line_header (line_offset, abfd, cu);
5552
5553 if (line_header)
5554 {
5555 cu->line_header = line_header;
5556 make_cleanup (free_cu_line_header, cu);
5557 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5558 }
5559 }
5560 }
5561
5562 /* Process DW_TAG_compile_unit. */
5563
5564 static void
5565 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5566 {
5567 struct objfile *objfile = cu->objfile;
5568 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5569 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5570 CORE_ADDR highpc = ((CORE_ADDR) 0);
5571 struct attribute *attr;
5572 char *name = NULL;
5573 char *comp_dir = NULL;
5574 struct die_info *child_die;
5575 bfd *abfd = objfile->obfd;
5576 CORE_ADDR baseaddr;
5577
5578 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5579
5580 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5581
5582 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5583 from finish_block. */
5584 if (lowpc == ((CORE_ADDR) -1))
5585 lowpc = highpc;
5586 lowpc += baseaddr;
5587 highpc += baseaddr;
5588
5589 find_file_and_directory (die, cu, &name, &comp_dir);
5590
5591 attr = dwarf2_attr (die, DW_AT_language, cu);
5592 if (attr)
5593 {
5594 set_cu_language (DW_UNSND (attr), cu);
5595 }
5596
5597 attr = dwarf2_attr (die, DW_AT_producer, cu);
5598 if (attr)
5599 cu->producer = DW_STRING (attr);
5600
5601 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5602 standardised yet. As a workaround for the language detection we fall
5603 back to the DW_AT_producer string. */
5604 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5605 cu->language = language_opencl;
5606
5607 /* We assume that we're processing GCC output. */
5608 processing_gcc_compilation = 2;
5609
5610 processing_has_namespace_info = 0;
5611
5612 start_symtab (name, comp_dir, lowpc);
5613 record_debugformat ("DWARF 2");
5614 record_producer (cu->producer);
5615
5616 initialize_cu_func_list (cu);
5617
5618 handle_DW_AT_stmt_list (die, cu, comp_dir);
5619
5620 /* Process all dies in compilation unit. */
5621 if (die->child != NULL)
5622 {
5623 child_die = die->child;
5624 while (child_die && child_die->tag)
5625 {
5626 process_die (child_die, cu);
5627 child_die = sibling_die (child_die);
5628 }
5629 }
5630
5631 /* Decode macro information, if present. Dwarf 2 macro information
5632 refers to information in the line number info statement program
5633 header, so we can only read it if we've read the header
5634 successfully. */
5635 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5636 if (attr && cu->line_header)
5637 {
5638 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5639 complaint (&symfile_complaints,
5640 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5641
5642 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5643 comp_dir, abfd, cu,
5644 &dwarf2_per_objfile->macro, 1);
5645 }
5646 else
5647 {
5648 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5649 if (attr && cu->line_header)
5650 {
5651 unsigned int macro_offset = DW_UNSND (attr);
5652
5653 dwarf_decode_macros (cu->line_header, macro_offset,
5654 comp_dir, abfd, cu,
5655 &dwarf2_per_objfile->macinfo, 0);
5656 }
5657 }
5658 do_cleanups (back_to);
5659 }
5660
5661 /* Process DW_TAG_type_unit.
5662 For TUs we want to skip the first top level sibling if it's not the
5663 actual type being defined by this TU. In this case the first top
5664 level sibling is there to provide context only. */
5665
5666 static void
5667 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5668 {
5669 struct objfile *objfile = cu->objfile;
5670 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5671 CORE_ADDR lowpc;
5672 struct attribute *attr;
5673 char *name = NULL;
5674 char *comp_dir = NULL;
5675 struct die_info *child_die;
5676 bfd *abfd = objfile->obfd;
5677
5678 /* start_symtab needs a low pc, but we don't really have one.
5679 Do what read_file_scope would do in the absence of such info. */
5680 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5681
5682 /* Find the filename. Do not use dwarf2_name here, since the filename
5683 is not a source language identifier. */
5684 attr = dwarf2_attr (die, DW_AT_name, cu);
5685 if (attr)
5686 name = DW_STRING (attr);
5687
5688 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5689 if (attr)
5690 comp_dir = DW_STRING (attr);
5691 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5692 {
5693 comp_dir = ldirname (name);
5694 if (comp_dir != NULL)
5695 make_cleanup (xfree, comp_dir);
5696 }
5697
5698 if (name == NULL)
5699 name = "<unknown>";
5700
5701 attr = dwarf2_attr (die, DW_AT_language, cu);
5702 if (attr)
5703 set_cu_language (DW_UNSND (attr), cu);
5704
5705 /* This isn't technically needed today. It is done for symmetry
5706 with read_file_scope. */
5707 attr = dwarf2_attr (die, DW_AT_producer, cu);
5708 if (attr)
5709 cu->producer = DW_STRING (attr);
5710
5711 /* We assume that we're processing GCC output. */
5712 processing_gcc_compilation = 2;
5713
5714 processing_has_namespace_info = 0;
5715
5716 start_symtab (name, comp_dir, lowpc);
5717 record_debugformat ("DWARF 2");
5718 record_producer (cu->producer);
5719
5720 handle_DW_AT_stmt_list (die, cu, comp_dir);
5721
5722 /* Process the dies in the type unit. */
5723 if (die->child == NULL)
5724 {
5725 dump_die_for_error (die);
5726 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5727 bfd_get_filename (abfd));
5728 }
5729
5730 child_die = die->child;
5731
5732 while (child_die && child_die->tag)
5733 {
5734 process_die (child_die, cu);
5735
5736 child_die = sibling_die (child_die);
5737 }
5738
5739 do_cleanups (back_to);
5740 }
5741
5742 static void
5743 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5744 struct dwarf2_cu *cu)
5745 {
5746 struct function_range *thisfn;
5747
5748 thisfn = (struct function_range *)
5749 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5750 thisfn->name = name;
5751 thisfn->lowpc = lowpc;
5752 thisfn->highpc = highpc;
5753 thisfn->seen_line = 0;
5754 thisfn->next = NULL;
5755
5756 if (cu->last_fn == NULL)
5757 cu->first_fn = thisfn;
5758 else
5759 cu->last_fn->next = thisfn;
5760
5761 cu->last_fn = thisfn;
5762 }
5763
5764 /* qsort helper for inherit_abstract_dies. */
5765
5766 static int
5767 unsigned_int_compar (const void *ap, const void *bp)
5768 {
5769 unsigned int a = *(unsigned int *) ap;
5770 unsigned int b = *(unsigned int *) bp;
5771
5772 return (a > b) - (b > a);
5773 }
5774
5775 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5776 Inherit only the children of the DW_AT_abstract_origin DIE not being
5777 already referenced by DW_AT_abstract_origin from the children of the
5778 current DIE. */
5779
5780 static void
5781 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5782 {
5783 struct die_info *child_die;
5784 unsigned die_children_count;
5785 /* CU offsets which were referenced by children of the current DIE. */
5786 unsigned *offsets;
5787 unsigned *offsets_end, *offsetp;
5788 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5789 struct die_info *origin_die;
5790 /* Iterator of the ORIGIN_DIE children. */
5791 struct die_info *origin_child_die;
5792 struct cleanup *cleanups;
5793 struct attribute *attr;
5794 struct dwarf2_cu *origin_cu;
5795 struct pending **origin_previous_list_in_scope;
5796
5797 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5798 if (!attr)
5799 return;
5800
5801 /* Note that following die references may follow to a die in a
5802 different cu. */
5803
5804 origin_cu = cu;
5805 origin_die = follow_die_ref (die, attr, &origin_cu);
5806
5807 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5808 symbols in. */
5809 origin_previous_list_in_scope = origin_cu->list_in_scope;
5810 origin_cu->list_in_scope = cu->list_in_scope;
5811
5812 if (die->tag != origin_die->tag
5813 && !(die->tag == DW_TAG_inlined_subroutine
5814 && origin_die->tag == DW_TAG_subprogram))
5815 complaint (&symfile_complaints,
5816 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5817 die->offset, origin_die->offset);
5818
5819 child_die = die->child;
5820 die_children_count = 0;
5821 while (child_die && child_die->tag)
5822 {
5823 child_die = sibling_die (child_die);
5824 die_children_count++;
5825 }
5826 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5827 cleanups = make_cleanup (xfree, offsets);
5828
5829 offsets_end = offsets;
5830 child_die = die->child;
5831 while (child_die && child_die->tag)
5832 {
5833 /* For each CHILD_DIE, find the corresponding child of
5834 ORIGIN_DIE. If there is more than one layer of
5835 DW_AT_abstract_origin, follow them all; there shouldn't be,
5836 but GCC versions at least through 4.4 generate this (GCC PR
5837 40573). */
5838 struct die_info *child_origin_die = child_die;
5839 struct dwarf2_cu *child_origin_cu = cu;
5840
5841 while (1)
5842 {
5843 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5844 child_origin_cu);
5845 if (attr == NULL)
5846 break;
5847 child_origin_die = follow_die_ref (child_origin_die, attr,
5848 &child_origin_cu);
5849 }
5850
5851 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5852 counterpart may exist. */
5853 if (child_origin_die != child_die)
5854 {
5855 if (child_die->tag != child_origin_die->tag
5856 && !(child_die->tag == DW_TAG_inlined_subroutine
5857 && child_origin_die->tag == DW_TAG_subprogram))
5858 complaint (&symfile_complaints,
5859 _("Child DIE 0x%x and its abstract origin 0x%x have "
5860 "different tags"), child_die->offset,
5861 child_origin_die->offset);
5862 if (child_origin_die->parent != origin_die)
5863 complaint (&symfile_complaints,
5864 _("Child DIE 0x%x and its abstract origin 0x%x have "
5865 "different parents"), child_die->offset,
5866 child_origin_die->offset);
5867 else
5868 *offsets_end++ = child_origin_die->offset;
5869 }
5870 child_die = sibling_die (child_die);
5871 }
5872 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5873 unsigned_int_compar);
5874 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5875 if (offsetp[-1] == *offsetp)
5876 complaint (&symfile_complaints,
5877 _("Multiple children of DIE 0x%x refer "
5878 "to DIE 0x%x as their abstract origin"),
5879 die->offset, *offsetp);
5880
5881 offsetp = offsets;
5882 origin_child_die = origin_die->child;
5883 while (origin_child_die && origin_child_die->tag)
5884 {
5885 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5886 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5887 offsetp++;
5888 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5889 {
5890 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5891 process_die (origin_child_die, origin_cu);
5892 }
5893 origin_child_die = sibling_die (origin_child_die);
5894 }
5895 origin_cu->list_in_scope = origin_previous_list_in_scope;
5896
5897 do_cleanups (cleanups);
5898 }
5899
5900 static void
5901 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5902 {
5903 struct objfile *objfile = cu->objfile;
5904 struct context_stack *new;
5905 CORE_ADDR lowpc;
5906 CORE_ADDR highpc;
5907 struct die_info *child_die;
5908 struct attribute *attr, *call_line, *call_file;
5909 char *name;
5910 CORE_ADDR baseaddr;
5911 struct block *block;
5912 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5913 VEC (symbolp) *template_args = NULL;
5914 struct template_symbol *templ_func = NULL;
5915
5916 if (inlined_func)
5917 {
5918 /* If we do not have call site information, we can't show the
5919 caller of this inlined function. That's too confusing, so
5920 only use the scope for local variables. */
5921 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5922 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5923 if (call_line == NULL || call_file == NULL)
5924 {
5925 read_lexical_block_scope (die, cu);
5926 return;
5927 }
5928 }
5929
5930 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5931
5932 name = dwarf2_name (die, cu);
5933
5934 /* Ignore functions with missing or empty names. These are actually
5935 illegal according to the DWARF standard. */
5936 if (name == NULL)
5937 {
5938 complaint (&symfile_complaints,
5939 _("missing name for subprogram DIE at %d"), die->offset);
5940 return;
5941 }
5942
5943 /* Ignore functions with missing or invalid low and high pc attributes. */
5944 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5945 {
5946 attr = dwarf2_attr (die, DW_AT_external, cu);
5947 if (!attr || !DW_UNSND (attr))
5948 complaint (&symfile_complaints,
5949 _("cannot get low and high bounds "
5950 "for subprogram DIE at %d"),
5951 die->offset);
5952 return;
5953 }
5954
5955 lowpc += baseaddr;
5956 highpc += baseaddr;
5957
5958 /* Record the function range for dwarf_decode_lines. */
5959 add_to_cu_func_list (name, lowpc, highpc, cu);
5960
5961 /* If we have any template arguments, then we must allocate a
5962 different sort of symbol. */
5963 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5964 {
5965 if (child_die->tag == DW_TAG_template_type_param
5966 || child_die->tag == DW_TAG_template_value_param)
5967 {
5968 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5969 struct template_symbol);
5970 templ_func->base.is_cplus_template_function = 1;
5971 break;
5972 }
5973 }
5974
5975 new = push_context (0, lowpc);
5976 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5977 (struct symbol *) templ_func);
5978
5979 /* If there is a location expression for DW_AT_frame_base, record
5980 it. */
5981 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5982 if (attr)
5983 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5984 expression is being recorded directly in the function's symbol
5985 and not in a separate frame-base object. I guess this hack is
5986 to avoid adding some sort of frame-base adjunct/annex to the
5987 function's symbol :-(. The problem with doing this is that it
5988 results in a function symbol with a location expression that
5989 has nothing to do with the location of the function, ouch! The
5990 relationship should be: a function's symbol has-a frame base; a
5991 frame-base has-a location expression. */
5992 dwarf2_symbol_mark_computed (attr, new->name, cu);
5993
5994 cu->list_in_scope = &local_symbols;
5995
5996 if (die->child != NULL)
5997 {
5998 child_die = die->child;
5999 while (child_die && child_die->tag)
6000 {
6001 if (child_die->tag == DW_TAG_template_type_param
6002 || child_die->tag == DW_TAG_template_value_param)
6003 {
6004 struct symbol *arg = new_symbol (child_die, NULL, cu);
6005
6006 if (arg != NULL)
6007 VEC_safe_push (symbolp, template_args, arg);
6008 }
6009 else
6010 process_die (child_die, cu);
6011 child_die = sibling_die (child_die);
6012 }
6013 }
6014
6015 inherit_abstract_dies (die, cu);
6016
6017 /* If we have a DW_AT_specification, we might need to import using
6018 directives from the context of the specification DIE. See the
6019 comment in determine_prefix. */
6020 if (cu->language == language_cplus
6021 && dwarf2_attr (die, DW_AT_specification, cu))
6022 {
6023 struct dwarf2_cu *spec_cu = cu;
6024 struct die_info *spec_die = die_specification (die, &spec_cu);
6025
6026 while (spec_die)
6027 {
6028 child_die = spec_die->child;
6029 while (child_die && child_die->tag)
6030 {
6031 if (child_die->tag == DW_TAG_imported_module)
6032 process_die (child_die, spec_cu);
6033 child_die = sibling_die (child_die);
6034 }
6035
6036 /* In some cases, GCC generates specification DIEs that
6037 themselves contain DW_AT_specification attributes. */
6038 spec_die = die_specification (spec_die, &spec_cu);
6039 }
6040 }
6041
6042 new = pop_context ();
6043 /* Make a block for the local symbols within. */
6044 block = finish_block (new->name, &local_symbols, new->old_blocks,
6045 lowpc, highpc, objfile);
6046
6047 /* For C++, set the block's scope. */
6048 if (cu->language == language_cplus || cu->language == language_fortran)
6049 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6050 determine_prefix (die, cu),
6051 processing_has_namespace_info);
6052
6053 /* If we have address ranges, record them. */
6054 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6055
6056 /* Attach template arguments to function. */
6057 if (! VEC_empty (symbolp, template_args))
6058 {
6059 gdb_assert (templ_func != NULL);
6060
6061 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6062 templ_func->template_arguments
6063 = obstack_alloc (&objfile->objfile_obstack,
6064 (templ_func->n_template_arguments
6065 * sizeof (struct symbol *)));
6066 memcpy (templ_func->template_arguments,
6067 VEC_address (symbolp, template_args),
6068 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6069 VEC_free (symbolp, template_args);
6070 }
6071
6072 /* In C++, we can have functions nested inside functions (e.g., when
6073 a function declares a class that has methods). This means that
6074 when we finish processing a function scope, we may need to go
6075 back to building a containing block's symbol lists. */
6076 local_symbols = new->locals;
6077 param_symbols = new->params;
6078 using_directives = new->using_directives;
6079
6080 /* If we've finished processing a top-level function, subsequent
6081 symbols go in the file symbol list. */
6082 if (outermost_context_p ())
6083 cu->list_in_scope = &file_symbols;
6084 }
6085
6086 /* Process all the DIES contained within a lexical block scope. Start
6087 a new scope, process the dies, and then close the scope. */
6088
6089 static void
6090 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6091 {
6092 struct objfile *objfile = cu->objfile;
6093 struct context_stack *new;
6094 CORE_ADDR lowpc, highpc;
6095 struct die_info *child_die;
6096 CORE_ADDR baseaddr;
6097
6098 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6099
6100 /* Ignore blocks with missing or invalid low and high pc attributes. */
6101 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6102 as multiple lexical blocks? Handling children in a sane way would
6103 be nasty. Might be easier to properly extend generic blocks to
6104 describe ranges. */
6105 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6106 return;
6107 lowpc += baseaddr;
6108 highpc += baseaddr;
6109
6110 push_context (0, lowpc);
6111 if (die->child != NULL)
6112 {
6113 child_die = die->child;
6114 while (child_die && child_die->tag)
6115 {
6116 process_die (child_die, cu);
6117 child_die = sibling_die (child_die);
6118 }
6119 }
6120 new = pop_context ();
6121
6122 if (local_symbols != NULL || using_directives != NULL)
6123 {
6124 struct block *block
6125 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6126 highpc, objfile);
6127
6128 /* Note that recording ranges after traversing children, as we
6129 do here, means that recording a parent's ranges entails
6130 walking across all its children's ranges as they appear in
6131 the address map, which is quadratic behavior.
6132
6133 It would be nicer to record the parent's ranges before
6134 traversing its children, simply overriding whatever you find
6135 there. But since we don't even decide whether to create a
6136 block until after we've traversed its children, that's hard
6137 to do. */
6138 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6139 }
6140 local_symbols = new->locals;
6141 using_directives = new->using_directives;
6142 }
6143
6144 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6145
6146 static void
6147 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6148 {
6149 struct objfile *objfile = cu->objfile;
6150 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6151 CORE_ADDR pc, baseaddr;
6152 struct attribute *attr;
6153 struct call_site *call_site, call_site_local;
6154 void **slot;
6155 int nparams;
6156 struct die_info *child_die;
6157
6158 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6159
6160 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6161 if (!attr)
6162 {
6163 complaint (&symfile_complaints,
6164 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6165 "DIE 0x%x [in module %s]"),
6166 die->offset, cu->objfile->name);
6167 return;
6168 }
6169 pc = DW_ADDR (attr) + baseaddr;
6170
6171 if (cu->call_site_htab == NULL)
6172 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6173 NULL, &objfile->objfile_obstack,
6174 hashtab_obstack_allocate, NULL);
6175 call_site_local.pc = pc;
6176 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6177 if (*slot != NULL)
6178 {
6179 complaint (&symfile_complaints,
6180 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6181 "DIE 0x%x [in module %s]"),
6182 paddress (gdbarch, pc), die->offset, cu->objfile->name);
6183 return;
6184 }
6185
6186 /* Count parameters at the caller. */
6187
6188 nparams = 0;
6189 for (child_die = die->child; child_die && child_die->tag;
6190 child_die = sibling_die (child_die))
6191 {
6192 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6193 {
6194 complaint (&symfile_complaints,
6195 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6196 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6197 child_die->tag, child_die->offset, cu->objfile->name);
6198 continue;
6199 }
6200
6201 nparams++;
6202 }
6203
6204 call_site = obstack_alloc (&objfile->objfile_obstack,
6205 (sizeof (*call_site)
6206 + (sizeof (*call_site->parameter)
6207 * (nparams - 1))));
6208 *slot = call_site;
6209 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6210 call_site->pc = pc;
6211
6212 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6213 if (attr == NULL)
6214 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6215 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6216 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6217 /* Keep NULL DWARF_BLOCK. */;
6218 else if (attr_form_is_block (attr))
6219 {
6220 struct dwarf2_locexpr_baton *dlbaton;
6221
6222 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6223 dlbaton->data = DW_BLOCK (attr)->data;
6224 dlbaton->size = DW_BLOCK (attr)->size;
6225 dlbaton->per_cu = cu->per_cu;
6226
6227 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6228 }
6229 else if (is_ref_attr (attr))
6230 {
6231 struct objfile *objfile = cu->objfile;
6232 struct dwarf2_cu *target_cu = cu;
6233 struct die_info *target_die;
6234
6235 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6236 gdb_assert (target_cu->objfile == objfile);
6237 if (die_is_declaration (target_die, target_cu))
6238 {
6239 const char *target_physname;
6240
6241 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6242 if (target_physname == NULL)
6243 complaint (&symfile_complaints,
6244 _("DW_AT_GNU_call_site_target target DIE has invalid "
6245 "physname, for referencing DIE 0x%x [in module %s]"),
6246 die->offset, cu->objfile->name);
6247 else
6248 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6249 }
6250 else
6251 {
6252 CORE_ADDR lowpc;
6253
6254 /* DW_AT_entry_pc should be preferred. */
6255 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6256 complaint (&symfile_complaints,
6257 _("DW_AT_GNU_call_site_target target DIE has invalid "
6258 "low pc, for referencing DIE 0x%x [in module %s]"),
6259 die->offset, cu->objfile->name);
6260 else
6261 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6262 }
6263 }
6264 else
6265 complaint (&symfile_complaints,
6266 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6267 "block nor reference, for DIE 0x%x [in module %s]"),
6268 die->offset, cu->objfile->name);
6269
6270 call_site->per_cu = cu->per_cu;
6271
6272 for (child_die = die->child;
6273 child_die && child_die->tag;
6274 child_die = sibling_die (child_die))
6275 {
6276 struct dwarf2_locexpr_baton *dlbaton;
6277 struct call_site_parameter *parameter;
6278
6279 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6280 {
6281 /* Already printed the complaint above. */
6282 continue;
6283 }
6284
6285 gdb_assert (call_site->parameter_count < nparams);
6286 parameter = &call_site->parameter[call_site->parameter_count];
6287
6288 /* DW_AT_location specifies the register number. Value of the data
6289 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6290
6291 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6292 if (!attr || !attr_form_is_block (attr))
6293 {
6294 complaint (&symfile_complaints,
6295 _("No DW_FORM_block* DW_AT_location for "
6296 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6297 child_die->offset, cu->objfile->name);
6298 continue;
6299 }
6300 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6301 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6302 if (parameter->dwarf_reg == -1)
6303 {
6304 complaint (&symfile_complaints,
6305 _("Only single DW_OP_reg is supported "
6306 "for DW_FORM_block* DW_AT_location for "
6307 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6308 child_die->offset, cu->objfile->name);
6309 continue;
6310 }
6311
6312 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6313 if (!attr_form_is_block (attr))
6314 {
6315 complaint (&symfile_complaints,
6316 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6317 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6318 child_die->offset, cu->objfile->name);
6319 continue;
6320 }
6321 parameter->value = DW_BLOCK (attr)->data;
6322 parameter->value_size = DW_BLOCK (attr)->size;
6323
6324 /* Parameters are not pre-cleared by memset above. */
6325 parameter->data_value = NULL;
6326 parameter->data_value_size = 0;
6327 call_site->parameter_count++;
6328
6329 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6330 if (attr)
6331 {
6332 if (!attr_form_is_block (attr))
6333 complaint (&symfile_complaints,
6334 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6335 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6336 child_die->offset, cu->objfile->name);
6337 else
6338 {
6339 parameter->data_value = DW_BLOCK (attr)->data;
6340 parameter->data_value_size = DW_BLOCK (attr)->size;
6341 }
6342 }
6343 }
6344 }
6345
6346 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6347 Return 1 if the attributes are present and valid, otherwise, return 0.
6348 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6349
6350 static int
6351 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6352 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6353 struct partial_symtab *ranges_pst)
6354 {
6355 struct objfile *objfile = cu->objfile;
6356 struct comp_unit_head *cu_header = &cu->header;
6357 bfd *obfd = objfile->obfd;
6358 unsigned int addr_size = cu_header->addr_size;
6359 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6360 /* Base address selection entry. */
6361 CORE_ADDR base;
6362 int found_base;
6363 unsigned int dummy;
6364 gdb_byte *buffer;
6365 CORE_ADDR marker;
6366 int low_set;
6367 CORE_ADDR low = 0;
6368 CORE_ADDR high = 0;
6369 CORE_ADDR baseaddr;
6370
6371 found_base = cu->base_known;
6372 base = cu->base_address;
6373
6374 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6375 if (offset >= dwarf2_per_objfile->ranges.size)
6376 {
6377 complaint (&symfile_complaints,
6378 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6379 offset);
6380 return 0;
6381 }
6382 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6383
6384 /* Read in the largest possible address. */
6385 marker = read_address (obfd, buffer, cu, &dummy);
6386 if ((marker & mask) == mask)
6387 {
6388 /* If we found the largest possible address, then
6389 read the base address. */
6390 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6391 buffer += 2 * addr_size;
6392 offset += 2 * addr_size;
6393 found_base = 1;
6394 }
6395
6396 low_set = 0;
6397
6398 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6399
6400 while (1)
6401 {
6402 CORE_ADDR range_beginning, range_end;
6403
6404 range_beginning = read_address (obfd, buffer, cu, &dummy);
6405 buffer += addr_size;
6406 range_end = read_address (obfd, buffer, cu, &dummy);
6407 buffer += addr_size;
6408 offset += 2 * addr_size;
6409
6410 /* An end of list marker is a pair of zero addresses. */
6411 if (range_beginning == 0 && range_end == 0)
6412 /* Found the end of list entry. */
6413 break;
6414
6415 /* Each base address selection entry is a pair of 2 values.
6416 The first is the largest possible address, the second is
6417 the base address. Check for a base address here. */
6418 if ((range_beginning & mask) == mask)
6419 {
6420 /* If we found the largest possible address, then
6421 read the base address. */
6422 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6423 found_base = 1;
6424 continue;
6425 }
6426
6427 if (!found_base)
6428 {
6429 /* We have no valid base address for the ranges
6430 data. */
6431 complaint (&symfile_complaints,
6432 _("Invalid .debug_ranges data (no base address)"));
6433 return 0;
6434 }
6435
6436 if (range_beginning > range_end)
6437 {
6438 /* Inverted range entries are invalid. */
6439 complaint (&symfile_complaints,
6440 _("Invalid .debug_ranges data (inverted range)"));
6441 return 0;
6442 }
6443
6444 /* Empty range entries have no effect. */
6445 if (range_beginning == range_end)
6446 continue;
6447
6448 range_beginning += base;
6449 range_end += base;
6450
6451 if (ranges_pst != NULL)
6452 addrmap_set_empty (objfile->psymtabs_addrmap,
6453 range_beginning + baseaddr,
6454 range_end - 1 + baseaddr,
6455 ranges_pst);
6456
6457 /* FIXME: This is recording everything as a low-high
6458 segment of consecutive addresses. We should have a
6459 data structure for discontiguous block ranges
6460 instead. */
6461 if (! low_set)
6462 {
6463 low = range_beginning;
6464 high = range_end;
6465 low_set = 1;
6466 }
6467 else
6468 {
6469 if (range_beginning < low)
6470 low = range_beginning;
6471 if (range_end > high)
6472 high = range_end;
6473 }
6474 }
6475
6476 if (! low_set)
6477 /* If the first entry is an end-of-list marker, the range
6478 describes an empty scope, i.e. no instructions. */
6479 return 0;
6480
6481 if (low_return)
6482 *low_return = low;
6483 if (high_return)
6484 *high_return = high;
6485 return 1;
6486 }
6487
6488 /* Get low and high pc attributes from a die. Return 1 if the attributes
6489 are present and valid, otherwise, return 0. Return -1 if the range is
6490 discontinuous, i.e. derived from DW_AT_ranges information. */
6491 static int
6492 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6493 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6494 struct partial_symtab *pst)
6495 {
6496 struct attribute *attr;
6497 CORE_ADDR low = 0;
6498 CORE_ADDR high = 0;
6499 int ret = 0;
6500
6501 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6502 if (attr)
6503 {
6504 high = DW_ADDR (attr);
6505 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6506 if (attr)
6507 low = DW_ADDR (attr);
6508 else
6509 /* Found high w/o low attribute. */
6510 return 0;
6511
6512 /* Found consecutive range of addresses. */
6513 ret = 1;
6514 }
6515 else
6516 {
6517 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6518 if (attr != NULL)
6519 {
6520 /* Value of the DW_AT_ranges attribute is the offset in the
6521 .debug_ranges section. */
6522 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6523 return 0;
6524 /* Found discontinuous range of addresses. */
6525 ret = -1;
6526 }
6527 }
6528
6529 /* read_partial_die has also the strict LOW < HIGH requirement. */
6530 if (high <= low)
6531 return 0;
6532
6533 /* When using the GNU linker, .gnu.linkonce. sections are used to
6534 eliminate duplicate copies of functions and vtables and such.
6535 The linker will arbitrarily choose one and discard the others.
6536 The AT_*_pc values for such functions refer to local labels in
6537 these sections. If the section from that file was discarded, the
6538 labels are not in the output, so the relocs get a value of 0.
6539 If this is a discarded function, mark the pc bounds as invalid,
6540 so that GDB will ignore it. */
6541 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6542 return 0;
6543
6544 *lowpc = low;
6545 if (highpc)
6546 *highpc = high;
6547 return ret;
6548 }
6549
6550 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6551 its low and high PC addresses. Do nothing if these addresses could not
6552 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6553 and HIGHPC to the high address if greater than HIGHPC. */
6554
6555 static void
6556 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6557 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6558 struct dwarf2_cu *cu)
6559 {
6560 CORE_ADDR low, high;
6561 struct die_info *child = die->child;
6562
6563 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6564 {
6565 *lowpc = min (*lowpc, low);
6566 *highpc = max (*highpc, high);
6567 }
6568
6569 /* If the language does not allow nested subprograms (either inside
6570 subprograms or lexical blocks), we're done. */
6571 if (cu->language != language_ada)
6572 return;
6573
6574 /* Check all the children of the given DIE. If it contains nested
6575 subprograms, then check their pc bounds. Likewise, we need to
6576 check lexical blocks as well, as they may also contain subprogram
6577 definitions. */
6578 while (child && child->tag)
6579 {
6580 if (child->tag == DW_TAG_subprogram
6581 || child->tag == DW_TAG_lexical_block)
6582 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6583 child = sibling_die (child);
6584 }
6585 }
6586
6587 /* Get the low and high pc's represented by the scope DIE, and store
6588 them in *LOWPC and *HIGHPC. If the correct values can't be
6589 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6590
6591 static void
6592 get_scope_pc_bounds (struct die_info *die,
6593 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6594 struct dwarf2_cu *cu)
6595 {
6596 CORE_ADDR best_low = (CORE_ADDR) -1;
6597 CORE_ADDR best_high = (CORE_ADDR) 0;
6598 CORE_ADDR current_low, current_high;
6599
6600 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6601 {
6602 best_low = current_low;
6603 best_high = current_high;
6604 }
6605 else
6606 {
6607 struct die_info *child = die->child;
6608
6609 while (child && child->tag)
6610 {
6611 switch (child->tag) {
6612 case DW_TAG_subprogram:
6613 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6614 break;
6615 case DW_TAG_namespace:
6616 case DW_TAG_module:
6617 /* FIXME: carlton/2004-01-16: Should we do this for
6618 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6619 that current GCC's always emit the DIEs corresponding
6620 to definitions of methods of classes as children of a
6621 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6622 the DIEs giving the declarations, which could be
6623 anywhere). But I don't see any reason why the
6624 standards says that they have to be there. */
6625 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6626
6627 if (current_low != ((CORE_ADDR) -1))
6628 {
6629 best_low = min (best_low, current_low);
6630 best_high = max (best_high, current_high);
6631 }
6632 break;
6633 default:
6634 /* Ignore. */
6635 break;
6636 }
6637
6638 child = sibling_die (child);
6639 }
6640 }
6641
6642 *lowpc = best_low;
6643 *highpc = best_high;
6644 }
6645
6646 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6647 in DIE. */
6648 static void
6649 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6650 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6651 {
6652 struct attribute *attr;
6653
6654 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6655 if (attr)
6656 {
6657 CORE_ADDR high = DW_ADDR (attr);
6658
6659 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6660 if (attr)
6661 {
6662 CORE_ADDR low = DW_ADDR (attr);
6663
6664 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6665 }
6666 }
6667
6668 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6669 if (attr)
6670 {
6671 bfd *obfd = cu->objfile->obfd;
6672
6673 /* The value of the DW_AT_ranges attribute is the offset of the
6674 address range list in the .debug_ranges section. */
6675 unsigned long offset = DW_UNSND (attr);
6676 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6677
6678 /* For some target architectures, but not others, the
6679 read_address function sign-extends the addresses it returns.
6680 To recognize base address selection entries, we need a
6681 mask. */
6682 unsigned int addr_size = cu->header.addr_size;
6683 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6684
6685 /* The base address, to which the next pair is relative. Note
6686 that this 'base' is a DWARF concept: most entries in a range
6687 list are relative, to reduce the number of relocs against the
6688 debugging information. This is separate from this function's
6689 'baseaddr' argument, which GDB uses to relocate debugging
6690 information from a shared library based on the address at
6691 which the library was loaded. */
6692 CORE_ADDR base = cu->base_address;
6693 int base_known = cu->base_known;
6694
6695 gdb_assert (dwarf2_per_objfile->ranges.readin);
6696 if (offset >= dwarf2_per_objfile->ranges.size)
6697 {
6698 complaint (&symfile_complaints,
6699 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6700 offset);
6701 return;
6702 }
6703
6704 for (;;)
6705 {
6706 unsigned int bytes_read;
6707 CORE_ADDR start, end;
6708
6709 start = read_address (obfd, buffer, cu, &bytes_read);
6710 buffer += bytes_read;
6711 end = read_address (obfd, buffer, cu, &bytes_read);
6712 buffer += bytes_read;
6713
6714 /* Did we find the end of the range list? */
6715 if (start == 0 && end == 0)
6716 break;
6717
6718 /* Did we find a base address selection entry? */
6719 else if ((start & base_select_mask) == base_select_mask)
6720 {
6721 base = end;
6722 base_known = 1;
6723 }
6724
6725 /* We found an ordinary address range. */
6726 else
6727 {
6728 if (!base_known)
6729 {
6730 complaint (&symfile_complaints,
6731 _("Invalid .debug_ranges data "
6732 "(no base address)"));
6733 return;
6734 }
6735
6736 if (start > end)
6737 {
6738 /* Inverted range entries are invalid. */
6739 complaint (&symfile_complaints,
6740 _("Invalid .debug_ranges data "
6741 "(inverted range)"));
6742 return;
6743 }
6744
6745 /* Empty range entries have no effect. */
6746 if (start == end)
6747 continue;
6748
6749 record_block_range (block,
6750 baseaddr + base + start,
6751 baseaddr + base + end - 1);
6752 }
6753 }
6754 }
6755 }
6756
6757 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6758 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6759 during 4.6.0 experimental. */
6760
6761 static int
6762 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6763 {
6764 const char *cs;
6765 int major, minor, release;
6766
6767 if (cu->producer == NULL)
6768 {
6769 /* For unknown compilers expect their behavior is DWARF version
6770 compliant.
6771
6772 GCC started to support .debug_types sections by -gdwarf-4 since
6773 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6774 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6775 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6776 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6777
6778 return 0;
6779 }
6780
6781 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6782
6783 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6784 {
6785 /* For non-GCC compilers expect their behavior is DWARF version
6786 compliant. */
6787
6788 return 0;
6789 }
6790 cs = &cu->producer[strlen ("GNU ")];
6791 while (*cs && !isdigit (*cs))
6792 cs++;
6793 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6794 {
6795 /* Not recognized as GCC. */
6796
6797 return 0;
6798 }
6799
6800 return major < 4 || (major == 4 && minor < 6);
6801 }
6802
6803 /* Return the default accessibility type if it is not overriden by
6804 DW_AT_accessibility. */
6805
6806 static enum dwarf_access_attribute
6807 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6808 {
6809 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6810 {
6811 /* The default DWARF 2 accessibility for members is public, the default
6812 accessibility for inheritance is private. */
6813
6814 if (die->tag != DW_TAG_inheritance)
6815 return DW_ACCESS_public;
6816 else
6817 return DW_ACCESS_private;
6818 }
6819 else
6820 {
6821 /* DWARF 3+ defines the default accessibility a different way. The same
6822 rules apply now for DW_TAG_inheritance as for the members and it only
6823 depends on the container kind. */
6824
6825 if (die->parent->tag == DW_TAG_class_type)
6826 return DW_ACCESS_private;
6827 else
6828 return DW_ACCESS_public;
6829 }
6830 }
6831
6832 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6833 offset. If the attribute was not found return 0, otherwise return
6834 1. If it was found but could not properly be handled, set *OFFSET
6835 to 0. */
6836
6837 static int
6838 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6839 LONGEST *offset)
6840 {
6841 struct attribute *attr;
6842
6843 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6844 if (attr != NULL)
6845 {
6846 *offset = 0;
6847
6848 /* Note that we do not check for a section offset first here.
6849 This is because DW_AT_data_member_location is new in DWARF 4,
6850 so if we see it, we can assume that a constant form is really
6851 a constant and not a section offset. */
6852 if (attr_form_is_constant (attr))
6853 *offset = dwarf2_get_attr_constant_value (attr, 0);
6854 else if (attr_form_is_section_offset (attr))
6855 dwarf2_complex_location_expr_complaint ();
6856 else if (attr_form_is_block (attr))
6857 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6858 else
6859 dwarf2_complex_location_expr_complaint ();
6860
6861 return 1;
6862 }
6863
6864 return 0;
6865 }
6866
6867 /* Add an aggregate field to the field list. */
6868
6869 static void
6870 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6871 struct dwarf2_cu *cu)
6872 {
6873 struct objfile *objfile = cu->objfile;
6874 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6875 struct nextfield *new_field;
6876 struct attribute *attr;
6877 struct field *fp;
6878 char *fieldname = "";
6879
6880 /* Allocate a new field list entry and link it in. */
6881 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6882 make_cleanup (xfree, new_field);
6883 memset (new_field, 0, sizeof (struct nextfield));
6884
6885 if (die->tag == DW_TAG_inheritance)
6886 {
6887 new_field->next = fip->baseclasses;
6888 fip->baseclasses = new_field;
6889 }
6890 else
6891 {
6892 new_field->next = fip->fields;
6893 fip->fields = new_field;
6894 }
6895 fip->nfields++;
6896
6897 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6898 if (attr)
6899 new_field->accessibility = DW_UNSND (attr);
6900 else
6901 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6902 if (new_field->accessibility != DW_ACCESS_public)
6903 fip->non_public_fields = 1;
6904
6905 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6906 if (attr)
6907 new_field->virtuality = DW_UNSND (attr);
6908 else
6909 new_field->virtuality = DW_VIRTUALITY_none;
6910
6911 fp = &new_field->field;
6912
6913 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6914 {
6915 LONGEST offset;
6916
6917 /* Data member other than a C++ static data member. */
6918
6919 /* Get type of field. */
6920 fp->type = die_type (die, cu);
6921
6922 SET_FIELD_BITPOS (*fp, 0);
6923
6924 /* Get bit size of field (zero if none). */
6925 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6926 if (attr)
6927 {
6928 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6929 }
6930 else
6931 {
6932 FIELD_BITSIZE (*fp) = 0;
6933 }
6934
6935 /* Get bit offset of field. */
6936 if (handle_data_member_location (die, cu, &offset))
6937 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6938 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6939 if (attr)
6940 {
6941 if (gdbarch_bits_big_endian (gdbarch))
6942 {
6943 /* For big endian bits, the DW_AT_bit_offset gives the
6944 additional bit offset from the MSB of the containing
6945 anonymous object to the MSB of the field. We don't
6946 have to do anything special since we don't need to
6947 know the size of the anonymous object. */
6948 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6949 }
6950 else
6951 {
6952 /* For little endian bits, compute the bit offset to the
6953 MSB of the anonymous object, subtract off the number of
6954 bits from the MSB of the field to the MSB of the
6955 object, and then subtract off the number of bits of
6956 the field itself. The result is the bit offset of
6957 the LSB of the field. */
6958 int anonymous_size;
6959 int bit_offset = DW_UNSND (attr);
6960
6961 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6962 if (attr)
6963 {
6964 /* The size of the anonymous object containing
6965 the bit field is explicit, so use the
6966 indicated size (in bytes). */
6967 anonymous_size = DW_UNSND (attr);
6968 }
6969 else
6970 {
6971 /* The size of the anonymous object containing
6972 the bit field must be inferred from the type
6973 attribute of the data member containing the
6974 bit field. */
6975 anonymous_size = TYPE_LENGTH (fp->type);
6976 }
6977 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6978 - bit_offset - FIELD_BITSIZE (*fp);
6979 }
6980 }
6981
6982 /* Get name of field. */
6983 fieldname = dwarf2_name (die, cu);
6984 if (fieldname == NULL)
6985 fieldname = "";
6986
6987 /* The name is already allocated along with this objfile, so we don't
6988 need to duplicate it for the type. */
6989 fp->name = fieldname;
6990
6991 /* Change accessibility for artificial fields (e.g. virtual table
6992 pointer or virtual base class pointer) to private. */
6993 if (dwarf2_attr (die, DW_AT_artificial, cu))
6994 {
6995 FIELD_ARTIFICIAL (*fp) = 1;
6996 new_field->accessibility = DW_ACCESS_private;
6997 fip->non_public_fields = 1;
6998 }
6999 }
7000 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7001 {
7002 /* C++ static member. */
7003
7004 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7005 is a declaration, but all versions of G++ as of this writing
7006 (so through at least 3.2.1) incorrectly generate
7007 DW_TAG_variable tags. */
7008
7009 const char *physname;
7010
7011 /* Get name of field. */
7012 fieldname = dwarf2_name (die, cu);
7013 if (fieldname == NULL)
7014 return;
7015
7016 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7017 if (attr
7018 /* Only create a symbol if this is an external value.
7019 new_symbol checks this and puts the value in the global symbol
7020 table, which we want. If it is not external, new_symbol
7021 will try to put the value in cu->list_in_scope which is wrong. */
7022 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7023 {
7024 /* A static const member, not much different than an enum as far as
7025 we're concerned, except that we can support more types. */
7026 new_symbol (die, NULL, cu);
7027 }
7028
7029 /* Get physical name. */
7030 physname = dwarf2_physname (fieldname, die, cu);
7031
7032 /* The name is already allocated along with this objfile, so we don't
7033 need to duplicate it for the type. */
7034 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7035 FIELD_TYPE (*fp) = die_type (die, cu);
7036 FIELD_NAME (*fp) = fieldname;
7037 }
7038 else if (die->tag == DW_TAG_inheritance)
7039 {
7040 LONGEST offset;
7041
7042 /* C++ base class field. */
7043 if (handle_data_member_location (die, cu, &offset))
7044 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7045 FIELD_BITSIZE (*fp) = 0;
7046 FIELD_TYPE (*fp) = die_type (die, cu);
7047 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7048 fip->nbaseclasses++;
7049 }
7050 }
7051
7052 /* Add a typedef defined in the scope of the FIP's class. */
7053
7054 static void
7055 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7056 struct dwarf2_cu *cu)
7057 {
7058 struct objfile *objfile = cu->objfile;
7059 struct typedef_field_list *new_field;
7060 struct attribute *attr;
7061 struct typedef_field *fp;
7062 char *fieldname = "";
7063
7064 /* Allocate a new field list entry and link it in. */
7065 new_field = xzalloc (sizeof (*new_field));
7066 make_cleanup (xfree, new_field);
7067
7068 gdb_assert (die->tag == DW_TAG_typedef);
7069
7070 fp = &new_field->field;
7071
7072 /* Get name of field. */
7073 fp->name = dwarf2_name (die, cu);
7074 if (fp->name == NULL)
7075 return;
7076
7077 fp->type = read_type_die (die, cu);
7078
7079 new_field->next = fip->typedef_field_list;
7080 fip->typedef_field_list = new_field;
7081 fip->typedef_field_list_count++;
7082 }
7083
7084 /* Create the vector of fields, and attach it to the type. */
7085
7086 static void
7087 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7088 struct dwarf2_cu *cu)
7089 {
7090 int nfields = fip->nfields;
7091
7092 /* Record the field count, allocate space for the array of fields,
7093 and create blank accessibility bitfields if necessary. */
7094 TYPE_NFIELDS (type) = nfields;
7095 TYPE_FIELDS (type) = (struct field *)
7096 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7097 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7098
7099 if (fip->non_public_fields && cu->language != language_ada)
7100 {
7101 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7102
7103 TYPE_FIELD_PRIVATE_BITS (type) =
7104 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7105 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7106
7107 TYPE_FIELD_PROTECTED_BITS (type) =
7108 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7109 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7110
7111 TYPE_FIELD_IGNORE_BITS (type) =
7112 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7113 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7114 }
7115
7116 /* If the type has baseclasses, allocate and clear a bit vector for
7117 TYPE_FIELD_VIRTUAL_BITS. */
7118 if (fip->nbaseclasses && cu->language != language_ada)
7119 {
7120 int num_bytes = B_BYTES (fip->nbaseclasses);
7121 unsigned char *pointer;
7122
7123 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7124 pointer = TYPE_ALLOC (type, num_bytes);
7125 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7126 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7127 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7128 }
7129
7130 /* Copy the saved-up fields into the field vector. Start from the head of
7131 the list, adding to the tail of the field array, so that they end up in
7132 the same order in the array in which they were added to the list. */
7133 while (nfields-- > 0)
7134 {
7135 struct nextfield *fieldp;
7136
7137 if (fip->fields)
7138 {
7139 fieldp = fip->fields;
7140 fip->fields = fieldp->next;
7141 }
7142 else
7143 {
7144 fieldp = fip->baseclasses;
7145 fip->baseclasses = fieldp->next;
7146 }
7147
7148 TYPE_FIELD (type, nfields) = fieldp->field;
7149 switch (fieldp->accessibility)
7150 {
7151 case DW_ACCESS_private:
7152 if (cu->language != language_ada)
7153 SET_TYPE_FIELD_PRIVATE (type, nfields);
7154 break;
7155
7156 case DW_ACCESS_protected:
7157 if (cu->language != language_ada)
7158 SET_TYPE_FIELD_PROTECTED (type, nfields);
7159 break;
7160
7161 case DW_ACCESS_public:
7162 break;
7163
7164 default:
7165 /* Unknown accessibility. Complain and treat it as public. */
7166 {
7167 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7168 fieldp->accessibility);
7169 }
7170 break;
7171 }
7172 if (nfields < fip->nbaseclasses)
7173 {
7174 switch (fieldp->virtuality)
7175 {
7176 case DW_VIRTUALITY_virtual:
7177 case DW_VIRTUALITY_pure_virtual:
7178 if (cu->language == language_ada)
7179 error (_("unexpected virtuality in component of Ada type"));
7180 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7181 break;
7182 }
7183 }
7184 }
7185 }
7186
7187 /* Add a member function to the proper fieldlist. */
7188
7189 static void
7190 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7191 struct type *type, struct dwarf2_cu *cu)
7192 {
7193 struct objfile *objfile = cu->objfile;
7194 struct attribute *attr;
7195 struct fnfieldlist *flp;
7196 int i;
7197 struct fn_field *fnp;
7198 char *fieldname;
7199 struct nextfnfield *new_fnfield;
7200 struct type *this_type;
7201 enum dwarf_access_attribute accessibility;
7202
7203 if (cu->language == language_ada)
7204 error (_("unexpected member function in Ada type"));
7205
7206 /* Get name of member function. */
7207 fieldname = dwarf2_name (die, cu);
7208 if (fieldname == NULL)
7209 return;
7210
7211 /* Look up member function name in fieldlist. */
7212 for (i = 0; i < fip->nfnfields; i++)
7213 {
7214 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7215 break;
7216 }
7217
7218 /* Create new list element if necessary. */
7219 if (i < fip->nfnfields)
7220 flp = &fip->fnfieldlists[i];
7221 else
7222 {
7223 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7224 {
7225 fip->fnfieldlists = (struct fnfieldlist *)
7226 xrealloc (fip->fnfieldlists,
7227 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7228 * sizeof (struct fnfieldlist));
7229 if (fip->nfnfields == 0)
7230 make_cleanup (free_current_contents, &fip->fnfieldlists);
7231 }
7232 flp = &fip->fnfieldlists[fip->nfnfields];
7233 flp->name = fieldname;
7234 flp->length = 0;
7235 flp->head = NULL;
7236 i = fip->nfnfields++;
7237 }
7238
7239 /* Create a new member function field and chain it to the field list
7240 entry. */
7241 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7242 make_cleanup (xfree, new_fnfield);
7243 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7244 new_fnfield->next = flp->head;
7245 flp->head = new_fnfield;
7246 flp->length++;
7247
7248 /* Fill in the member function field info. */
7249 fnp = &new_fnfield->fnfield;
7250
7251 /* Delay processing of the physname until later. */
7252 if (cu->language == language_cplus || cu->language == language_java)
7253 {
7254 add_to_method_list (type, i, flp->length - 1, fieldname,
7255 die, cu);
7256 }
7257 else
7258 {
7259 const char *physname = dwarf2_physname (fieldname, die, cu);
7260 fnp->physname = physname ? physname : "";
7261 }
7262
7263 fnp->type = alloc_type (objfile);
7264 this_type = read_type_die (die, cu);
7265 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7266 {
7267 int nparams = TYPE_NFIELDS (this_type);
7268
7269 /* TYPE is the domain of this method, and THIS_TYPE is the type
7270 of the method itself (TYPE_CODE_METHOD). */
7271 smash_to_method_type (fnp->type, type,
7272 TYPE_TARGET_TYPE (this_type),
7273 TYPE_FIELDS (this_type),
7274 TYPE_NFIELDS (this_type),
7275 TYPE_VARARGS (this_type));
7276
7277 /* Handle static member functions.
7278 Dwarf2 has no clean way to discern C++ static and non-static
7279 member functions. G++ helps GDB by marking the first
7280 parameter for non-static member functions (which is the this
7281 pointer) as artificial. We obtain this information from
7282 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7283 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7284 fnp->voffset = VOFFSET_STATIC;
7285 }
7286 else
7287 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7288 dwarf2_full_name (fieldname, die, cu));
7289
7290 /* Get fcontext from DW_AT_containing_type if present. */
7291 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7292 fnp->fcontext = die_containing_type (die, cu);
7293
7294 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7295 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7296
7297 /* Get accessibility. */
7298 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7299 if (attr)
7300 accessibility = DW_UNSND (attr);
7301 else
7302 accessibility = dwarf2_default_access_attribute (die, cu);
7303 switch (accessibility)
7304 {
7305 case DW_ACCESS_private:
7306 fnp->is_private = 1;
7307 break;
7308 case DW_ACCESS_protected:
7309 fnp->is_protected = 1;
7310 break;
7311 }
7312
7313 /* Check for artificial methods. */
7314 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7315 if (attr && DW_UNSND (attr) != 0)
7316 fnp->is_artificial = 1;
7317
7318 /* Get index in virtual function table if it is a virtual member
7319 function. For older versions of GCC, this is an offset in the
7320 appropriate virtual table, as specified by DW_AT_containing_type.
7321 For everyone else, it is an expression to be evaluated relative
7322 to the object address. */
7323
7324 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7325 if (attr)
7326 {
7327 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7328 {
7329 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7330 {
7331 /* Old-style GCC. */
7332 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7333 }
7334 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7335 || (DW_BLOCK (attr)->size > 1
7336 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7337 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7338 {
7339 struct dwarf_block blk;
7340 int offset;
7341
7342 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7343 ? 1 : 2);
7344 blk.size = DW_BLOCK (attr)->size - offset;
7345 blk.data = DW_BLOCK (attr)->data + offset;
7346 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7347 if ((fnp->voffset % cu->header.addr_size) != 0)
7348 dwarf2_complex_location_expr_complaint ();
7349 else
7350 fnp->voffset /= cu->header.addr_size;
7351 fnp->voffset += 2;
7352 }
7353 else
7354 dwarf2_complex_location_expr_complaint ();
7355
7356 if (!fnp->fcontext)
7357 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7358 }
7359 else if (attr_form_is_section_offset (attr))
7360 {
7361 dwarf2_complex_location_expr_complaint ();
7362 }
7363 else
7364 {
7365 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7366 fieldname);
7367 }
7368 }
7369 else
7370 {
7371 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7372 if (attr && DW_UNSND (attr))
7373 {
7374 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7375 complaint (&symfile_complaints,
7376 _("Member function \"%s\" (offset %d) is virtual "
7377 "but the vtable offset is not specified"),
7378 fieldname, die->offset);
7379 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7380 TYPE_CPLUS_DYNAMIC (type) = 1;
7381 }
7382 }
7383 }
7384
7385 /* Create the vector of member function fields, and attach it to the type. */
7386
7387 static void
7388 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7389 struct dwarf2_cu *cu)
7390 {
7391 struct fnfieldlist *flp;
7392 int total_length = 0;
7393 int i;
7394
7395 if (cu->language == language_ada)
7396 error (_("unexpected member functions in Ada type"));
7397
7398 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7399 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7400 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7401
7402 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7403 {
7404 struct nextfnfield *nfp = flp->head;
7405 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7406 int k;
7407
7408 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7409 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7410 fn_flp->fn_fields = (struct fn_field *)
7411 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7412 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7413 fn_flp->fn_fields[k] = nfp->fnfield;
7414
7415 total_length += flp->length;
7416 }
7417
7418 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7419 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7420 }
7421
7422 /* Returns non-zero if NAME is the name of a vtable member in CU's
7423 language, zero otherwise. */
7424 static int
7425 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7426 {
7427 static const char vptr[] = "_vptr";
7428 static const char vtable[] = "vtable";
7429
7430 /* Look for the C++ and Java forms of the vtable. */
7431 if ((cu->language == language_java
7432 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7433 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7434 && is_cplus_marker (name[sizeof (vptr) - 1])))
7435 return 1;
7436
7437 return 0;
7438 }
7439
7440 /* GCC outputs unnamed structures that are really pointers to member
7441 functions, with the ABI-specified layout. If TYPE describes
7442 such a structure, smash it into a member function type.
7443
7444 GCC shouldn't do this; it should just output pointer to member DIEs.
7445 This is GCC PR debug/28767. */
7446
7447 static void
7448 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7449 {
7450 struct type *pfn_type, *domain_type, *new_type;
7451
7452 /* Check for a structure with no name and two children. */
7453 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7454 return;
7455
7456 /* Check for __pfn and __delta members. */
7457 if (TYPE_FIELD_NAME (type, 0) == NULL
7458 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7459 || TYPE_FIELD_NAME (type, 1) == NULL
7460 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7461 return;
7462
7463 /* Find the type of the method. */
7464 pfn_type = TYPE_FIELD_TYPE (type, 0);
7465 if (pfn_type == NULL
7466 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7467 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7468 return;
7469
7470 /* Look for the "this" argument. */
7471 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7472 if (TYPE_NFIELDS (pfn_type) == 0
7473 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7474 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7475 return;
7476
7477 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7478 new_type = alloc_type (objfile);
7479 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7480 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7481 TYPE_VARARGS (pfn_type));
7482 smash_to_methodptr_type (type, new_type);
7483 }
7484
7485 /* Called when we find the DIE that starts a structure or union scope
7486 (definition) to create a type for the structure or union. Fill in
7487 the type's name and general properties; the members will not be
7488 processed until process_structure_type.
7489
7490 NOTE: we need to call these functions regardless of whether or not the
7491 DIE has a DW_AT_name attribute, since it might be an anonymous
7492 structure or union. This gets the type entered into our set of
7493 user defined types.
7494
7495 However, if the structure is incomplete (an opaque struct/union)
7496 then suppress creating a symbol table entry for it since gdb only
7497 wants to find the one with the complete definition. Note that if
7498 it is complete, we just call new_symbol, which does it's own
7499 checking about whether the struct/union is anonymous or not (and
7500 suppresses creating a symbol table entry itself). */
7501
7502 static struct type *
7503 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7504 {
7505 struct objfile *objfile = cu->objfile;
7506 struct type *type;
7507 struct attribute *attr;
7508 char *name;
7509
7510 /* If the definition of this type lives in .debug_types, read that type.
7511 Don't follow DW_AT_specification though, that will take us back up
7512 the chain and we want to go down. */
7513 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7514 if (attr)
7515 {
7516 struct dwarf2_cu *type_cu = cu;
7517 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7518
7519 /* We could just recurse on read_structure_type, but we need to call
7520 get_die_type to ensure only one type for this DIE is created.
7521 This is important, for example, because for c++ classes we need
7522 TYPE_NAME set which is only done by new_symbol. Blech. */
7523 type = read_type_die (type_die, type_cu);
7524
7525 /* TYPE_CU may not be the same as CU.
7526 Ensure TYPE is recorded in CU's type_hash table. */
7527 return set_die_type (die, type, cu);
7528 }
7529
7530 type = alloc_type (objfile);
7531 INIT_CPLUS_SPECIFIC (type);
7532
7533 name = dwarf2_name (die, cu);
7534 if (name != NULL)
7535 {
7536 if (cu->language == language_cplus
7537 || cu->language == language_java)
7538 {
7539 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7540
7541 /* dwarf2_full_name might have already finished building the DIE's
7542 type. If so, there is no need to continue. */
7543 if (get_die_type (die, cu) != NULL)
7544 return get_die_type (die, cu);
7545
7546 TYPE_TAG_NAME (type) = full_name;
7547 if (die->tag == DW_TAG_structure_type
7548 || die->tag == DW_TAG_class_type)
7549 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7550 }
7551 else
7552 {
7553 /* The name is already allocated along with this objfile, so
7554 we don't need to duplicate it for the type. */
7555 TYPE_TAG_NAME (type) = (char *) name;
7556 if (die->tag == DW_TAG_class_type)
7557 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7558 }
7559 }
7560
7561 if (die->tag == DW_TAG_structure_type)
7562 {
7563 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7564 }
7565 else if (die->tag == DW_TAG_union_type)
7566 {
7567 TYPE_CODE (type) = TYPE_CODE_UNION;
7568 }
7569 else
7570 {
7571 TYPE_CODE (type) = TYPE_CODE_CLASS;
7572 }
7573
7574 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7575 TYPE_DECLARED_CLASS (type) = 1;
7576
7577 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7578 if (attr)
7579 {
7580 TYPE_LENGTH (type) = DW_UNSND (attr);
7581 }
7582 else
7583 {
7584 TYPE_LENGTH (type) = 0;
7585 }
7586
7587 TYPE_STUB_SUPPORTED (type) = 1;
7588 if (die_is_declaration (die, cu))
7589 TYPE_STUB (type) = 1;
7590 else if (attr == NULL && die->child == NULL
7591 && producer_is_realview (cu->producer))
7592 /* RealView does not output the required DW_AT_declaration
7593 on incomplete types. */
7594 TYPE_STUB (type) = 1;
7595
7596 /* We need to add the type field to the die immediately so we don't
7597 infinitely recurse when dealing with pointers to the structure
7598 type within the structure itself. */
7599 set_die_type (die, type, cu);
7600
7601 /* set_die_type should be already done. */
7602 set_descriptive_type (type, die, cu);
7603
7604 return type;
7605 }
7606
7607 /* Finish creating a structure or union type, including filling in
7608 its members and creating a symbol for it. */
7609
7610 static void
7611 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7612 {
7613 struct objfile *objfile = cu->objfile;
7614 struct die_info *child_die = die->child;
7615 struct type *type;
7616
7617 type = get_die_type (die, cu);
7618 if (type == NULL)
7619 type = read_structure_type (die, cu);
7620
7621 if (die->child != NULL && ! die_is_declaration (die, cu))
7622 {
7623 struct field_info fi;
7624 struct die_info *child_die;
7625 VEC (symbolp) *template_args = NULL;
7626 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7627
7628 memset (&fi, 0, sizeof (struct field_info));
7629
7630 child_die = die->child;
7631
7632 while (child_die && child_die->tag)
7633 {
7634 if (child_die->tag == DW_TAG_member
7635 || child_die->tag == DW_TAG_variable)
7636 {
7637 /* NOTE: carlton/2002-11-05: A C++ static data member
7638 should be a DW_TAG_member that is a declaration, but
7639 all versions of G++ as of this writing (so through at
7640 least 3.2.1) incorrectly generate DW_TAG_variable
7641 tags for them instead. */
7642 dwarf2_add_field (&fi, child_die, cu);
7643 }
7644 else if (child_die->tag == DW_TAG_subprogram)
7645 {
7646 /* C++ member function. */
7647 dwarf2_add_member_fn (&fi, child_die, type, cu);
7648 }
7649 else if (child_die->tag == DW_TAG_inheritance)
7650 {
7651 /* C++ base class field. */
7652 dwarf2_add_field (&fi, child_die, cu);
7653 }
7654 else if (child_die->tag == DW_TAG_typedef)
7655 dwarf2_add_typedef (&fi, child_die, cu);
7656 else if (child_die->tag == DW_TAG_template_type_param
7657 || child_die->tag == DW_TAG_template_value_param)
7658 {
7659 struct symbol *arg = new_symbol (child_die, NULL, cu);
7660
7661 if (arg != NULL)
7662 VEC_safe_push (symbolp, template_args, arg);
7663 }
7664
7665 child_die = sibling_die (child_die);
7666 }
7667
7668 /* Attach template arguments to type. */
7669 if (! VEC_empty (symbolp, template_args))
7670 {
7671 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7672 TYPE_N_TEMPLATE_ARGUMENTS (type)
7673 = VEC_length (symbolp, template_args);
7674 TYPE_TEMPLATE_ARGUMENTS (type)
7675 = obstack_alloc (&objfile->objfile_obstack,
7676 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7677 * sizeof (struct symbol *)));
7678 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7679 VEC_address (symbolp, template_args),
7680 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7681 * sizeof (struct symbol *)));
7682 VEC_free (symbolp, template_args);
7683 }
7684
7685 /* Attach fields and member functions to the type. */
7686 if (fi.nfields)
7687 dwarf2_attach_fields_to_type (&fi, type, cu);
7688 if (fi.nfnfields)
7689 {
7690 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7691
7692 /* Get the type which refers to the base class (possibly this
7693 class itself) which contains the vtable pointer for the current
7694 class from the DW_AT_containing_type attribute. This use of
7695 DW_AT_containing_type is a GNU extension. */
7696
7697 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7698 {
7699 struct type *t = die_containing_type (die, cu);
7700
7701 TYPE_VPTR_BASETYPE (type) = t;
7702 if (type == t)
7703 {
7704 int i;
7705
7706 /* Our own class provides vtbl ptr. */
7707 for (i = TYPE_NFIELDS (t) - 1;
7708 i >= TYPE_N_BASECLASSES (t);
7709 --i)
7710 {
7711 char *fieldname = TYPE_FIELD_NAME (t, i);
7712
7713 if (is_vtable_name (fieldname, cu))
7714 {
7715 TYPE_VPTR_FIELDNO (type) = i;
7716 break;
7717 }
7718 }
7719
7720 /* Complain if virtual function table field not found. */
7721 if (i < TYPE_N_BASECLASSES (t))
7722 complaint (&symfile_complaints,
7723 _("virtual function table pointer "
7724 "not found when defining class '%s'"),
7725 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7726 "");
7727 }
7728 else
7729 {
7730 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7731 }
7732 }
7733 else if (cu->producer
7734 && strncmp (cu->producer,
7735 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7736 {
7737 /* The IBM XLC compiler does not provide direct indication
7738 of the containing type, but the vtable pointer is
7739 always named __vfp. */
7740
7741 int i;
7742
7743 for (i = TYPE_NFIELDS (type) - 1;
7744 i >= TYPE_N_BASECLASSES (type);
7745 --i)
7746 {
7747 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7748 {
7749 TYPE_VPTR_FIELDNO (type) = i;
7750 TYPE_VPTR_BASETYPE (type) = type;
7751 break;
7752 }
7753 }
7754 }
7755 }
7756
7757 /* Copy fi.typedef_field_list linked list elements content into the
7758 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7759 if (fi.typedef_field_list)
7760 {
7761 int i = fi.typedef_field_list_count;
7762
7763 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7764 TYPE_TYPEDEF_FIELD_ARRAY (type)
7765 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7766 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7767
7768 /* Reverse the list order to keep the debug info elements order. */
7769 while (--i >= 0)
7770 {
7771 struct typedef_field *dest, *src;
7772
7773 dest = &TYPE_TYPEDEF_FIELD (type, i);
7774 src = &fi.typedef_field_list->field;
7775 fi.typedef_field_list = fi.typedef_field_list->next;
7776 *dest = *src;
7777 }
7778 }
7779
7780 do_cleanups (back_to);
7781
7782 if (HAVE_CPLUS_STRUCT (type))
7783 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7784 }
7785
7786 quirk_gcc_member_function_pointer (type, cu->objfile);
7787
7788 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7789 snapshots) has been known to create a die giving a declaration
7790 for a class that has, as a child, a die giving a definition for a
7791 nested class. So we have to process our children even if the
7792 current die is a declaration. Normally, of course, a declaration
7793 won't have any children at all. */
7794
7795 while (child_die != NULL && child_die->tag)
7796 {
7797 if (child_die->tag == DW_TAG_member
7798 || child_die->tag == DW_TAG_variable
7799 || child_die->tag == DW_TAG_inheritance
7800 || child_die->tag == DW_TAG_template_value_param
7801 || child_die->tag == DW_TAG_template_type_param)
7802 {
7803 /* Do nothing. */
7804 }
7805 else
7806 process_die (child_die, cu);
7807
7808 child_die = sibling_die (child_die);
7809 }
7810
7811 /* Do not consider external references. According to the DWARF standard,
7812 these DIEs are identified by the fact that they have no byte_size
7813 attribute, and a declaration attribute. */
7814 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7815 || !die_is_declaration (die, cu))
7816 new_symbol (die, type, cu);
7817 }
7818
7819 /* Given a DW_AT_enumeration_type die, set its type. We do not
7820 complete the type's fields yet, or create any symbols. */
7821
7822 static struct type *
7823 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7824 {
7825 struct objfile *objfile = cu->objfile;
7826 struct type *type;
7827 struct attribute *attr;
7828 const char *name;
7829
7830 /* If the definition of this type lives in .debug_types, read that type.
7831 Don't follow DW_AT_specification though, that will take us back up
7832 the chain and we want to go down. */
7833 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7834 if (attr)
7835 {
7836 struct dwarf2_cu *type_cu = cu;
7837 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7838
7839 type = read_type_die (type_die, type_cu);
7840
7841 /* TYPE_CU may not be the same as CU.
7842 Ensure TYPE is recorded in CU's type_hash table. */
7843 return set_die_type (die, type, cu);
7844 }
7845
7846 type = alloc_type (objfile);
7847
7848 TYPE_CODE (type) = TYPE_CODE_ENUM;
7849 name = dwarf2_full_name (NULL, die, cu);
7850 if (name != NULL)
7851 TYPE_TAG_NAME (type) = (char *) name;
7852
7853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7854 if (attr)
7855 {
7856 TYPE_LENGTH (type) = DW_UNSND (attr);
7857 }
7858 else
7859 {
7860 TYPE_LENGTH (type) = 0;
7861 }
7862
7863 /* The enumeration DIE can be incomplete. In Ada, any type can be
7864 declared as private in the package spec, and then defined only
7865 inside the package body. Such types are known as Taft Amendment
7866 Types. When another package uses such a type, an incomplete DIE
7867 may be generated by the compiler. */
7868 if (die_is_declaration (die, cu))
7869 TYPE_STUB (type) = 1;
7870
7871 return set_die_type (die, type, cu);
7872 }
7873
7874 /* Given a pointer to a die which begins an enumeration, process all
7875 the dies that define the members of the enumeration, and create the
7876 symbol for the enumeration type.
7877
7878 NOTE: We reverse the order of the element list. */
7879
7880 static void
7881 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7882 {
7883 struct type *this_type;
7884
7885 this_type = get_die_type (die, cu);
7886 if (this_type == NULL)
7887 this_type = read_enumeration_type (die, cu);
7888
7889 if (die->child != NULL)
7890 {
7891 struct die_info *child_die;
7892 struct symbol *sym;
7893 struct field *fields = NULL;
7894 int num_fields = 0;
7895 int unsigned_enum = 1;
7896 char *name;
7897
7898 child_die = die->child;
7899 while (child_die && child_die->tag)
7900 {
7901 if (child_die->tag != DW_TAG_enumerator)
7902 {
7903 process_die (child_die, cu);
7904 }
7905 else
7906 {
7907 name = dwarf2_name (child_die, cu);
7908 if (name)
7909 {
7910 sym = new_symbol (child_die, this_type, cu);
7911 if (SYMBOL_VALUE (sym) < 0)
7912 unsigned_enum = 0;
7913
7914 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7915 {
7916 fields = (struct field *)
7917 xrealloc (fields,
7918 (num_fields + DW_FIELD_ALLOC_CHUNK)
7919 * sizeof (struct field));
7920 }
7921
7922 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7923 FIELD_TYPE (fields[num_fields]) = NULL;
7924 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7925 FIELD_BITSIZE (fields[num_fields]) = 0;
7926
7927 num_fields++;
7928 }
7929 }
7930
7931 child_die = sibling_die (child_die);
7932 }
7933
7934 if (num_fields)
7935 {
7936 TYPE_NFIELDS (this_type) = num_fields;
7937 TYPE_FIELDS (this_type) = (struct field *)
7938 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7939 memcpy (TYPE_FIELDS (this_type), fields,
7940 sizeof (struct field) * num_fields);
7941 xfree (fields);
7942 }
7943 if (unsigned_enum)
7944 TYPE_UNSIGNED (this_type) = 1;
7945 }
7946
7947 /* If we are reading an enum from a .debug_types unit, and the enum
7948 is a declaration, and the enum is not the signatured type in the
7949 unit, then we do not want to add a symbol for it. Adding a
7950 symbol would in some cases obscure the true definition of the
7951 enum, giving users an incomplete type when the definition is
7952 actually available. Note that we do not want to do this for all
7953 enums which are just declarations, because C++0x allows forward
7954 enum declarations. */
7955 if (cu->per_cu->debug_type_section
7956 && die_is_declaration (die, cu))
7957 {
7958 struct signatured_type *type_sig;
7959
7960 type_sig
7961 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7962 cu->per_cu->debug_type_section,
7963 cu->per_cu->offset);
7964 if (type_sig->type_offset != die->offset)
7965 return;
7966 }
7967
7968 new_symbol (die, this_type, cu);
7969 }
7970
7971 /* Extract all information from a DW_TAG_array_type DIE and put it in
7972 the DIE's type field. For now, this only handles one dimensional
7973 arrays. */
7974
7975 static struct type *
7976 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7977 {
7978 struct objfile *objfile = cu->objfile;
7979 struct die_info *child_die;
7980 struct type *type;
7981 struct type *element_type, *range_type, *index_type;
7982 struct type **range_types = NULL;
7983 struct attribute *attr;
7984 int ndim = 0;
7985 struct cleanup *back_to;
7986 char *name;
7987
7988 element_type = die_type (die, cu);
7989
7990 /* The die_type call above may have already set the type for this DIE. */
7991 type = get_die_type (die, cu);
7992 if (type)
7993 return type;
7994
7995 /* Irix 6.2 native cc creates array types without children for
7996 arrays with unspecified length. */
7997 if (die->child == NULL)
7998 {
7999 index_type = objfile_type (objfile)->builtin_int;
8000 range_type = create_range_type (NULL, index_type, 0, -1);
8001 type = create_array_type (NULL, element_type, range_type);
8002 return set_die_type (die, type, cu);
8003 }
8004
8005 back_to = make_cleanup (null_cleanup, NULL);
8006 child_die = die->child;
8007 while (child_die && child_die->tag)
8008 {
8009 if (child_die->tag == DW_TAG_subrange_type)
8010 {
8011 struct type *child_type = read_type_die (child_die, cu);
8012
8013 if (child_type != NULL)
8014 {
8015 /* The range type was succesfully read. Save it for the
8016 array type creation. */
8017 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8018 {
8019 range_types = (struct type **)
8020 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8021 * sizeof (struct type *));
8022 if (ndim == 0)
8023 make_cleanup (free_current_contents, &range_types);
8024 }
8025 range_types[ndim++] = child_type;
8026 }
8027 }
8028 child_die = sibling_die (child_die);
8029 }
8030
8031 /* Dwarf2 dimensions are output from left to right, create the
8032 necessary array types in backwards order. */
8033
8034 type = element_type;
8035
8036 if (read_array_order (die, cu) == DW_ORD_col_major)
8037 {
8038 int i = 0;
8039
8040 while (i < ndim)
8041 type = create_array_type (NULL, type, range_types[i++]);
8042 }
8043 else
8044 {
8045 while (ndim-- > 0)
8046 type = create_array_type (NULL, type, range_types[ndim]);
8047 }
8048
8049 /* Understand Dwarf2 support for vector types (like they occur on
8050 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8051 array type. This is not part of the Dwarf2/3 standard yet, but a
8052 custom vendor extension. The main difference between a regular
8053 array and the vector variant is that vectors are passed by value
8054 to functions. */
8055 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8056 if (attr)
8057 make_vector_type (type);
8058
8059 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8060 implementation may choose to implement triple vectors using this
8061 attribute. */
8062 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8063 if (attr)
8064 {
8065 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8066 TYPE_LENGTH (type) = DW_UNSND (attr);
8067 else
8068 complaint (&symfile_complaints,
8069 _("DW_AT_byte_size for array type smaller "
8070 "than the total size of elements"));
8071 }
8072
8073 name = dwarf2_name (die, cu);
8074 if (name)
8075 TYPE_NAME (type) = name;
8076
8077 /* Install the type in the die. */
8078 set_die_type (die, type, cu);
8079
8080 /* set_die_type should be already done. */
8081 set_descriptive_type (type, die, cu);
8082
8083 do_cleanups (back_to);
8084
8085 return type;
8086 }
8087
8088 static enum dwarf_array_dim_ordering
8089 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8090 {
8091 struct attribute *attr;
8092
8093 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8094
8095 if (attr) return DW_SND (attr);
8096
8097 /* GNU F77 is a special case, as at 08/2004 array type info is the
8098 opposite order to the dwarf2 specification, but data is still
8099 laid out as per normal fortran.
8100
8101 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8102 version checking. */
8103
8104 if (cu->language == language_fortran
8105 && cu->producer && strstr (cu->producer, "GNU F77"))
8106 {
8107 return DW_ORD_row_major;
8108 }
8109
8110 switch (cu->language_defn->la_array_ordering)
8111 {
8112 case array_column_major:
8113 return DW_ORD_col_major;
8114 case array_row_major:
8115 default:
8116 return DW_ORD_row_major;
8117 };
8118 }
8119
8120 /* Extract all information from a DW_TAG_set_type DIE and put it in
8121 the DIE's type field. */
8122
8123 static struct type *
8124 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8125 {
8126 struct type *domain_type, *set_type;
8127 struct attribute *attr;
8128
8129 domain_type = die_type (die, cu);
8130
8131 /* The die_type call above may have already set the type for this DIE. */
8132 set_type = get_die_type (die, cu);
8133 if (set_type)
8134 return set_type;
8135
8136 set_type = create_set_type (NULL, domain_type);
8137
8138 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8139 if (attr)
8140 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8141
8142 return set_die_type (die, set_type, cu);
8143 }
8144
8145 /* First cut: install each common block member as a global variable. */
8146
8147 static void
8148 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8149 {
8150 struct die_info *child_die;
8151 struct attribute *attr;
8152 struct symbol *sym;
8153 CORE_ADDR base = (CORE_ADDR) 0;
8154
8155 attr = dwarf2_attr (die, DW_AT_location, cu);
8156 if (attr)
8157 {
8158 /* Support the .debug_loc offsets. */
8159 if (attr_form_is_block (attr))
8160 {
8161 base = decode_locdesc (DW_BLOCK (attr), cu);
8162 }
8163 else if (attr_form_is_section_offset (attr))
8164 {
8165 dwarf2_complex_location_expr_complaint ();
8166 }
8167 else
8168 {
8169 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8170 "common block member");
8171 }
8172 }
8173 if (die->child != NULL)
8174 {
8175 child_die = die->child;
8176 while (child_die && child_die->tag)
8177 {
8178 LONGEST offset;
8179
8180 sym = new_symbol (child_die, NULL, cu);
8181 if (sym != NULL
8182 && handle_data_member_location (child_die, cu, &offset))
8183 {
8184 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8185 add_symbol_to_list (sym, &global_symbols);
8186 }
8187 child_die = sibling_die (child_die);
8188 }
8189 }
8190 }
8191
8192 /* Create a type for a C++ namespace. */
8193
8194 static struct type *
8195 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8196 {
8197 struct objfile *objfile = cu->objfile;
8198 const char *previous_prefix, *name;
8199 int is_anonymous;
8200 struct type *type;
8201
8202 /* For extensions, reuse the type of the original namespace. */
8203 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8204 {
8205 struct die_info *ext_die;
8206 struct dwarf2_cu *ext_cu = cu;
8207
8208 ext_die = dwarf2_extension (die, &ext_cu);
8209 type = read_type_die (ext_die, ext_cu);
8210
8211 /* EXT_CU may not be the same as CU.
8212 Ensure TYPE is recorded in CU's type_hash table. */
8213 return set_die_type (die, type, cu);
8214 }
8215
8216 name = namespace_name (die, &is_anonymous, cu);
8217
8218 /* Now build the name of the current namespace. */
8219
8220 previous_prefix = determine_prefix (die, cu);
8221 if (previous_prefix[0] != '\0')
8222 name = typename_concat (&objfile->objfile_obstack,
8223 previous_prefix, name, 0, cu);
8224
8225 /* Create the type. */
8226 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8227 objfile);
8228 TYPE_NAME (type) = (char *) name;
8229 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8230
8231 return set_die_type (die, type, cu);
8232 }
8233
8234 /* Read a C++ namespace. */
8235
8236 static void
8237 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8238 {
8239 struct objfile *objfile = cu->objfile;
8240 int is_anonymous;
8241
8242 /* Add a symbol associated to this if we haven't seen the namespace
8243 before. Also, add a using directive if it's an anonymous
8244 namespace. */
8245
8246 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8247 {
8248 struct type *type;
8249
8250 type = read_type_die (die, cu);
8251 new_symbol (die, type, cu);
8252
8253 namespace_name (die, &is_anonymous, cu);
8254 if (is_anonymous)
8255 {
8256 const char *previous_prefix = determine_prefix (die, cu);
8257
8258 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8259 NULL, NULL, &objfile->objfile_obstack);
8260 }
8261 }
8262
8263 if (die->child != NULL)
8264 {
8265 struct die_info *child_die = die->child;
8266
8267 while (child_die && child_die->tag)
8268 {
8269 process_die (child_die, cu);
8270 child_die = sibling_die (child_die);
8271 }
8272 }
8273 }
8274
8275 /* Read a Fortran module as type. This DIE can be only a declaration used for
8276 imported module. Still we need that type as local Fortran "use ... only"
8277 declaration imports depend on the created type in determine_prefix. */
8278
8279 static struct type *
8280 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8281 {
8282 struct objfile *objfile = cu->objfile;
8283 char *module_name;
8284 struct type *type;
8285
8286 module_name = dwarf2_name (die, cu);
8287 if (!module_name)
8288 complaint (&symfile_complaints,
8289 _("DW_TAG_module has no name, offset 0x%x"),
8290 die->offset);
8291 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8292
8293 /* determine_prefix uses TYPE_TAG_NAME. */
8294 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8295
8296 return set_die_type (die, type, cu);
8297 }
8298
8299 /* Read a Fortran module. */
8300
8301 static void
8302 read_module (struct die_info *die, struct dwarf2_cu *cu)
8303 {
8304 struct die_info *child_die = die->child;
8305
8306 while (child_die && child_die->tag)
8307 {
8308 process_die (child_die, cu);
8309 child_die = sibling_die (child_die);
8310 }
8311 }
8312
8313 /* Return the name of the namespace represented by DIE. Set
8314 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8315 namespace. */
8316
8317 static const char *
8318 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8319 {
8320 struct die_info *current_die;
8321 const char *name = NULL;
8322
8323 /* Loop through the extensions until we find a name. */
8324
8325 for (current_die = die;
8326 current_die != NULL;
8327 current_die = dwarf2_extension (die, &cu))
8328 {
8329 name = dwarf2_name (current_die, cu);
8330 if (name != NULL)
8331 break;
8332 }
8333
8334 /* Is it an anonymous namespace? */
8335
8336 *is_anonymous = (name == NULL);
8337 if (*is_anonymous)
8338 name = CP_ANONYMOUS_NAMESPACE_STR;
8339
8340 return name;
8341 }
8342
8343 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8344 the user defined type vector. */
8345
8346 static struct type *
8347 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8348 {
8349 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8350 struct comp_unit_head *cu_header = &cu->header;
8351 struct type *type;
8352 struct attribute *attr_byte_size;
8353 struct attribute *attr_address_class;
8354 int byte_size, addr_class;
8355 struct type *target_type;
8356
8357 target_type = die_type (die, cu);
8358
8359 /* The die_type call above may have already set the type for this DIE. */
8360 type = get_die_type (die, cu);
8361 if (type)
8362 return type;
8363
8364 type = lookup_pointer_type (target_type);
8365
8366 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8367 if (attr_byte_size)
8368 byte_size = DW_UNSND (attr_byte_size);
8369 else
8370 byte_size = cu_header->addr_size;
8371
8372 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8373 if (attr_address_class)
8374 addr_class = DW_UNSND (attr_address_class);
8375 else
8376 addr_class = DW_ADDR_none;
8377
8378 /* If the pointer size or address class is different than the
8379 default, create a type variant marked as such and set the
8380 length accordingly. */
8381 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8382 {
8383 if (gdbarch_address_class_type_flags_p (gdbarch))
8384 {
8385 int type_flags;
8386
8387 type_flags = gdbarch_address_class_type_flags
8388 (gdbarch, byte_size, addr_class);
8389 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8390 == 0);
8391 type = make_type_with_address_space (type, type_flags);
8392 }
8393 else if (TYPE_LENGTH (type) != byte_size)
8394 {
8395 complaint (&symfile_complaints,
8396 _("invalid pointer size %d"), byte_size);
8397 }
8398 else
8399 {
8400 /* Should we also complain about unhandled address classes? */
8401 }
8402 }
8403
8404 TYPE_LENGTH (type) = byte_size;
8405 return set_die_type (die, type, cu);
8406 }
8407
8408 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8409 the user defined type vector. */
8410
8411 static struct type *
8412 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8413 {
8414 struct type *type;
8415 struct type *to_type;
8416 struct type *domain;
8417
8418 to_type = die_type (die, cu);
8419 domain = die_containing_type (die, cu);
8420
8421 /* The calls above may have already set the type for this DIE. */
8422 type = get_die_type (die, cu);
8423 if (type)
8424 return type;
8425
8426 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8427 type = lookup_methodptr_type (to_type);
8428 else
8429 type = lookup_memberptr_type (to_type, domain);
8430
8431 return set_die_type (die, type, cu);
8432 }
8433
8434 /* Extract all information from a DW_TAG_reference_type DIE and add to
8435 the user defined type vector. */
8436
8437 static struct type *
8438 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8439 {
8440 struct comp_unit_head *cu_header = &cu->header;
8441 struct type *type, *target_type;
8442 struct attribute *attr;
8443
8444 target_type = die_type (die, cu);
8445
8446 /* The die_type call above may have already set the type for this DIE. */
8447 type = get_die_type (die, cu);
8448 if (type)
8449 return type;
8450
8451 type = lookup_reference_type (target_type);
8452 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8453 if (attr)
8454 {
8455 TYPE_LENGTH (type) = DW_UNSND (attr);
8456 }
8457 else
8458 {
8459 TYPE_LENGTH (type) = cu_header->addr_size;
8460 }
8461 return set_die_type (die, type, cu);
8462 }
8463
8464 static struct type *
8465 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8466 {
8467 struct type *base_type, *cv_type;
8468
8469 base_type = die_type (die, cu);
8470
8471 /* The die_type call above may have already set the type for this DIE. */
8472 cv_type = get_die_type (die, cu);
8473 if (cv_type)
8474 return cv_type;
8475
8476 /* In case the const qualifier is applied to an array type, the element type
8477 is so qualified, not the array type (section 6.7.3 of C99). */
8478 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8479 {
8480 struct type *el_type, *inner_array;
8481
8482 base_type = copy_type (base_type);
8483 inner_array = base_type;
8484
8485 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8486 {
8487 TYPE_TARGET_TYPE (inner_array) =
8488 copy_type (TYPE_TARGET_TYPE (inner_array));
8489 inner_array = TYPE_TARGET_TYPE (inner_array);
8490 }
8491
8492 el_type = TYPE_TARGET_TYPE (inner_array);
8493 TYPE_TARGET_TYPE (inner_array) =
8494 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8495
8496 return set_die_type (die, base_type, cu);
8497 }
8498
8499 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8500 return set_die_type (die, cv_type, cu);
8501 }
8502
8503 static struct type *
8504 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8505 {
8506 struct type *base_type, *cv_type;
8507
8508 base_type = die_type (die, cu);
8509
8510 /* The die_type call above may have already set the type for this DIE. */
8511 cv_type = get_die_type (die, cu);
8512 if (cv_type)
8513 return cv_type;
8514
8515 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8516 return set_die_type (die, cv_type, cu);
8517 }
8518
8519 /* Extract all information from a DW_TAG_string_type DIE and add to
8520 the user defined type vector. It isn't really a user defined type,
8521 but it behaves like one, with other DIE's using an AT_user_def_type
8522 attribute to reference it. */
8523
8524 static struct type *
8525 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8526 {
8527 struct objfile *objfile = cu->objfile;
8528 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8529 struct type *type, *range_type, *index_type, *char_type;
8530 struct attribute *attr;
8531 unsigned int length;
8532
8533 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8534 if (attr)
8535 {
8536 length = DW_UNSND (attr);
8537 }
8538 else
8539 {
8540 /* Check for the DW_AT_byte_size attribute. */
8541 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8542 if (attr)
8543 {
8544 length = DW_UNSND (attr);
8545 }
8546 else
8547 {
8548 length = 1;
8549 }
8550 }
8551
8552 index_type = objfile_type (objfile)->builtin_int;
8553 range_type = create_range_type (NULL, index_type, 1, length);
8554 char_type = language_string_char_type (cu->language_defn, gdbarch);
8555 type = create_string_type (NULL, char_type, range_type);
8556
8557 return set_die_type (die, type, cu);
8558 }
8559
8560 /* Handle DIES due to C code like:
8561
8562 struct foo
8563 {
8564 int (*funcp)(int a, long l);
8565 int b;
8566 };
8567
8568 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8569
8570 static struct type *
8571 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8572 {
8573 struct type *type; /* Type that this function returns. */
8574 struct type *ftype; /* Function that returns above type. */
8575 struct attribute *attr;
8576
8577 type = die_type (die, cu);
8578
8579 /* The die_type call above may have already set the type for this DIE. */
8580 ftype = get_die_type (die, cu);
8581 if (ftype)
8582 return ftype;
8583
8584 ftype = lookup_function_type (type);
8585
8586 /* All functions in C++, Pascal and Java have prototypes. */
8587 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8588 if ((attr && (DW_UNSND (attr) != 0))
8589 || cu->language == language_cplus
8590 || cu->language == language_java
8591 || cu->language == language_pascal)
8592 TYPE_PROTOTYPED (ftype) = 1;
8593 else if (producer_is_realview (cu->producer))
8594 /* RealView does not emit DW_AT_prototyped. We can not
8595 distinguish prototyped and unprototyped functions; default to
8596 prototyped, since that is more common in modern code (and
8597 RealView warns about unprototyped functions). */
8598 TYPE_PROTOTYPED (ftype) = 1;
8599
8600 /* Store the calling convention in the type if it's available in
8601 the subroutine die. Otherwise set the calling convention to
8602 the default value DW_CC_normal. */
8603 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8604 if (attr)
8605 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8606 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8607 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8608 else
8609 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8610
8611 /* We need to add the subroutine type to the die immediately so
8612 we don't infinitely recurse when dealing with parameters
8613 declared as the same subroutine type. */
8614 set_die_type (die, ftype, cu);
8615
8616 if (die->child != NULL)
8617 {
8618 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8619 struct die_info *child_die;
8620 int nparams, iparams;
8621
8622 /* Count the number of parameters.
8623 FIXME: GDB currently ignores vararg functions, but knows about
8624 vararg member functions. */
8625 nparams = 0;
8626 child_die = die->child;
8627 while (child_die && child_die->tag)
8628 {
8629 if (child_die->tag == DW_TAG_formal_parameter)
8630 nparams++;
8631 else if (child_die->tag == DW_TAG_unspecified_parameters)
8632 TYPE_VARARGS (ftype) = 1;
8633 child_die = sibling_die (child_die);
8634 }
8635
8636 /* Allocate storage for parameters and fill them in. */
8637 TYPE_NFIELDS (ftype) = nparams;
8638 TYPE_FIELDS (ftype) = (struct field *)
8639 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8640
8641 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8642 even if we error out during the parameters reading below. */
8643 for (iparams = 0; iparams < nparams; iparams++)
8644 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8645
8646 iparams = 0;
8647 child_die = die->child;
8648 while (child_die && child_die->tag)
8649 {
8650 if (child_die->tag == DW_TAG_formal_parameter)
8651 {
8652 struct type *arg_type;
8653
8654 /* DWARF version 2 has no clean way to discern C++
8655 static and non-static member functions. G++ helps
8656 GDB by marking the first parameter for non-static
8657 member functions (which is the this pointer) as
8658 artificial. We pass this information to
8659 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8660
8661 DWARF version 3 added DW_AT_object_pointer, which GCC
8662 4.5 does not yet generate. */
8663 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8664 if (attr)
8665 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8666 else
8667 {
8668 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8669
8670 /* GCC/43521: In java, the formal parameter
8671 "this" is sometimes not marked with DW_AT_artificial. */
8672 if (cu->language == language_java)
8673 {
8674 const char *name = dwarf2_name (child_die, cu);
8675
8676 if (name && !strcmp (name, "this"))
8677 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8678 }
8679 }
8680 arg_type = die_type (child_die, cu);
8681
8682 /* RealView does not mark THIS as const, which the testsuite
8683 expects. GCC marks THIS as const in method definitions,
8684 but not in the class specifications (GCC PR 43053). */
8685 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8686 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8687 {
8688 int is_this = 0;
8689 struct dwarf2_cu *arg_cu = cu;
8690 const char *name = dwarf2_name (child_die, cu);
8691
8692 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8693 if (attr)
8694 {
8695 /* If the compiler emits this, use it. */
8696 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8697 is_this = 1;
8698 }
8699 else if (name && strcmp (name, "this") == 0)
8700 /* Function definitions will have the argument names. */
8701 is_this = 1;
8702 else if (name == NULL && iparams == 0)
8703 /* Declarations may not have the names, so like
8704 elsewhere in GDB, assume an artificial first
8705 argument is "this". */
8706 is_this = 1;
8707
8708 if (is_this)
8709 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8710 arg_type, 0);
8711 }
8712
8713 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8714 iparams++;
8715 }
8716 child_die = sibling_die (child_die);
8717 }
8718 }
8719
8720 return ftype;
8721 }
8722
8723 static struct type *
8724 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8725 {
8726 struct objfile *objfile = cu->objfile;
8727 const char *name = NULL;
8728 struct type *this_type;
8729
8730 name = dwarf2_full_name (NULL, die, cu);
8731 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8732 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8733 TYPE_NAME (this_type) = (char *) name;
8734 set_die_type (die, this_type, cu);
8735 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8736 return this_type;
8737 }
8738
8739 /* Find a representation of a given base type and install
8740 it in the TYPE field of the die. */
8741
8742 static struct type *
8743 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8744 {
8745 struct objfile *objfile = cu->objfile;
8746 struct type *type;
8747 struct attribute *attr;
8748 int encoding = 0, size = 0;
8749 char *name;
8750 enum type_code code = TYPE_CODE_INT;
8751 int type_flags = 0;
8752 struct type *target_type = NULL;
8753
8754 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8755 if (attr)
8756 {
8757 encoding = DW_UNSND (attr);
8758 }
8759 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8760 if (attr)
8761 {
8762 size = DW_UNSND (attr);
8763 }
8764 name = dwarf2_name (die, cu);
8765 if (!name)
8766 {
8767 complaint (&symfile_complaints,
8768 _("DW_AT_name missing from DW_TAG_base_type"));
8769 }
8770
8771 switch (encoding)
8772 {
8773 case DW_ATE_address:
8774 /* Turn DW_ATE_address into a void * pointer. */
8775 code = TYPE_CODE_PTR;
8776 type_flags |= TYPE_FLAG_UNSIGNED;
8777 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8778 break;
8779 case DW_ATE_boolean:
8780 code = TYPE_CODE_BOOL;
8781 type_flags |= TYPE_FLAG_UNSIGNED;
8782 break;
8783 case DW_ATE_complex_float:
8784 code = TYPE_CODE_COMPLEX;
8785 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8786 break;
8787 case DW_ATE_decimal_float:
8788 code = TYPE_CODE_DECFLOAT;
8789 break;
8790 case DW_ATE_float:
8791 code = TYPE_CODE_FLT;
8792 break;
8793 case DW_ATE_signed:
8794 break;
8795 case DW_ATE_unsigned:
8796 type_flags |= TYPE_FLAG_UNSIGNED;
8797 if (cu->language == language_fortran
8798 && name
8799 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8800 code = TYPE_CODE_CHAR;
8801 break;
8802 case DW_ATE_signed_char:
8803 if (cu->language == language_ada || cu->language == language_m2
8804 || cu->language == language_pascal
8805 || cu->language == language_fortran)
8806 code = TYPE_CODE_CHAR;
8807 break;
8808 case DW_ATE_unsigned_char:
8809 if (cu->language == language_ada || cu->language == language_m2
8810 || cu->language == language_pascal
8811 || cu->language == language_fortran)
8812 code = TYPE_CODE_CHAR;
8813 type_flags |= TYPE_FLAG_UNSIGNED;
8814 break;
8815 case DW_ATE_UTF:
8816 /* We just treat this as an integer and then recognize the
8817 type by name elsewhere. */
8818 break;
8819
8820 default:
8821 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8822 dwarf_type_encoding_name (encoding));
8823 break;
8824 }
8825
8826 type = init_type (code, size, type_flags, NULL, objfile);
8827 TYPE_NAME (type) = name;
8828 TYPE_TARGET_TYPE (type) = target_type;
8829
8830 if (name && strcmp (name, "char") == 0)
8831 TYPE_NOSIGN (type) = 1;
8832
8833 return set_die_type (die, type, cu);
8834 }
8835
8836 /* Read the given DW_AT_subrange DIE. */
8837
8838 static struct type *
8839 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8840 {
8841 struct type *base_type;
8842 struct type *range_type;
8843 struct attribute *attr;
8844 LONGEST low = 0;
8845 LONGEST high = -1;
8846 char *name;
8847 LONGEST negative_mask;
8848
8849 base_type = die_type (die, cu);
8850 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8851 check_typedef (base_type);
8852
8853 /* The die_type call above may have already set the type for this DIE. */
8854 range_type = get_die_type (die, cu);
8855 if (range_type)
8856 return range_type;
8857
8858 if (cu->language == language_fortran)
8859 {
8860 /* FORTRAN implies a lower bound of 1, if not given. */
8861 low = 1;
8862 }
8863
8864 /* FIXME: For variable sized arrays either of these could be
8865 a variable rather than a constant value. We'll allow it,
8866 but we don't know how to handle it. */
8867 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8868 if (attr)
8869 low = dwarf2_get_attr_constant_value (attr, 0);
8870
8871 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8872 if (attr)
8873 {
8874 if (attr_form_is_block (attr) || is_ref_attr (attr))
8875 {
8876 /* GCC encodes arrays with unspecified or dynamic length
8877 with a DW_FORM_block1 attribute or a reference attribute.
8878 FIXME: GDB does not yet know how to handle dynamic
8879 arrays properly, treat them as arrays with unspecified
8880 length for now.
8881
8882 FIXME: jimb/2003-09-22: GDB does not really know
8883 how to handle arrays of unspecified length
8884 either; we just represent them as zero-length
8885 arrays. Choose an appropriate upper bound given
8886 the lower bound we've computed above. */
8887 high = low - 1;
8888 }
8889 else
8890 high = dwarf2_get_attr_constant_value (attr, 1);
8891 }
8892 else
8893 {
8894 attr = dwarf2_attr (die, DW_AT_count, cu);
8895 if (attr)
8896 {
8897 int count = dwarf2_get_attr_constant_value (attr, 1);
8898 high = low + count - 1;
8899 }
8900 else
8901 {
8902 /* Unspecified array length. */
8903 high = low - 1;
8904 }
8905 }
8906
8907 /* Dwarf-2 specifications explicitly allows to create subrange types
8908 without specifying a base type.
8909 In that case, the base type must be set to the type of
8910 the lower bound, upper bound or count, in that order, if any of these
8911 three attributes references an object that has a type.
8912 If no base type is found, the Dwarf-2 specifications say that
8913 a signed integer type of size equal to the size of an address should
8914 be used.
8915 For the following C code: `extern char gdb_int [];'
8916 GCC produces an empty range DIE.
8917 FIXME: muller/2010-05-28: Possible references to object for low bound,
8918 high bound or count are not yet handled by this code. */
8919 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8920 {
8921 struct objfile *objfile = cu->objfile;
8922 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8923 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8924 struct type *int_type = objfile_type (objfile)->builtin_int;
8925
8926 /* Test "int", "long int", and "long long int" objfile types,
8927 and select the first one having a size above or equal to the
8928 architecture address size. */
8929 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8930 base_type = int_type;
8931 else
8932 {
8933 int_type = objfile_type (objfile)->builtin_long;
8934 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8935 base_type = int_type;
8936 else
8937 {
8938 int_type = objfile_type (objfile)->builtin_long_long;
8939 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8940 base_type = int_type;
8941 }
8942 }
8943 }
8944
8945 negative_mask =
8946 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8947 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8948 low |= negative_mask;
8949 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8950 high |= negative_mask;
8951
8952 range_type = create_range_type (NULL, base_type, low, high);
8953
8954 /* Mark arrays with dynamic length at least as an array of unspecified
8955 length. GDB could check the boundary but before it gets implemented at
8956 least allow accessing the array elements. */
8957 if (attr && attr_form_is_block (attr))
8958 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8959
8960 /* Ada expects an empty array on no boundary attributes. */
8961 if (attr == NULL && cu->language != language_ada)
8962 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8963
8964 name = dwarf2_name (die, cu);
8965 if (name)
8966 TYPE_NAME (range_type) = name;
8967
8968 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8969 if (attr)
8970 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8971
8972 set_die_type (die, range_type, cu);
8973
8974 /* set_die_type should be already done. */
8975 set_descriptive_type (range_type, die, cu);
8976
8977 return range_type;
8978 }
8979
8980 static struct type *
8981 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8982 {
8983 struct type *type;
8984
8985 /* For now, we only support the C meaning of an unspecified type: void. */
8986
8987 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8988 TYPE_NAME (type) = dwarf2_name (die, cu);
8989
8990 return set_die_type (die, type, cu);
8991 }
8992
8993 /* Trivial hash function for die_info: the hash value of a DIE
8994 is its offset in .debug_info for this objfile. */
8995
8996 static hashval_t
8997 die_hash (const void *item)
8998 {
8999 const struct die_info *die = item;
9000
9001 return die->offset;
9002 }
9003
9004 /* Trivial comparison function for die_info structures: two DIEs
9005 are equal if they have the same offset. */
9006
9007 static int
9008 die_eq (const void *item_lhs, const void *item_rhs)
9009 {
9010 const struct die_info *die_lhs = item_lhs;
9011 const struct die_info *die_rhs = item_rhs;
9012
9013 return die_lhs->offset == die_rhs->offset;
9014 }
9015
9016 /* Read a whole compilation unit into a linked list of dies. */
9017
9018 static struct die_info *
9019 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9020 {
9021 struct die_reader_specs reader_specs;
9022 int read_abbrevs = 0;
9023 struct cleanup *back_to = NULL;
9024 struct die_info *die;
9025
9026 if (cu->dwarf2_abbrevs == NULL)
9027 {
9028 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9029 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9030 read_abbrevs = 1;
9031 }
9032
9033 gdb_assert (cu->die_hash == NULL);
9034 cu->die_hash
9035 = htab_create_alloc_ex (cu->header.length / 12,
9036 die_hash,
9037 die_eq,
9038 NULL,
9039 &cu->comp_unit_obstack,
9040 hashtab_obstack_allocate,
9041 dummy_obstack_deallocate);
9042
9043 init_cu_die_reader (&reader_specs, cu);
9044
9045 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9046
9047 if (read_abbrevs)
9048 do_cleanups (back_to);
9049
9050 return die;
9051 }
9052
9053 /* Main entry point for reading a DIE and all children.
9054 Read the DIE and dump it if requested. */
9055
9056 static struct die_info *
9057 read_die_and_children (const struct die_reader_specs *reader,
9058 gdb_byte *info_ptr,
9059 gdb_byte **new_info_ptr,
9060 struct die_info *parent)
9061 {
9062 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9063 new_info_ptr, parent);
9064
9065 if (dwarf2_die_debug)
9066 {
9067 fprintf_unfiltered (gdb_stdlog,
9068 "\nRead die from %s of %s:\n",
9069 (reader->cu->per_cu->debug_type_section
9070 ? ".debug_types"
9071 : ".debug_info"),
9072 reader->abfd->filename);
9073 dump_die (result, dwarf2_die_debug);
9074 }
9075
9076 return result;
9077 }
9078
9079 /* Read a single die and all its descendents. Set the die's sibling
9080 field to NULL; set other fields in the die correctly, and set all
9081 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9082 location of the info_ptr after reading all of those dies. PARENT
9083 is the parent of the die in question. */
9084
9085 static struct die_info *
9086 read_die_and_children_1 (const struct die_reader_specs *reader,
9087 gdb_byte *info_ptr,
9088 gdb_byte **new_info_ptr,
9089 struct die_info *parent)
9090 {
9091 struct die_info *die;
9092 gdb_byte *cur_ptr;
9093 int has_children;
9094
9095 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9096 if (die == NULL)
9097 {
9098 *new_info_ptr = cur_ptr;
9099 return NULL;
9100 }
9101 store_in_ref_table (die, reader->cu);
9102
9103 if (has_children)
9104 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9105 else
9106 {
9107 die->child = NULL;
9108 *new_info_ptr = cur_ptr;
9109 }
9110
9111 die->sibling = NULL;
9112 die->parent = parent;
9113 return die;
9114 }
9115
9116 /* Read a die, all of its descendents, and all of its siblings; set
9117 all of the fields of all of the dies correctly. Arguments are as
9118 in read_die_and_children. */
9119
9120 static struct die_info *
9121 read_die_and_siblings (const struct die_reader_specs *reader,
9122 gdb_byte *info_ptr,
9123 gdb_byte **new_info_ptr,
9124 struct die_info *parent)
9125 {
9126 struct die_info *first_die, *last_sibling;
9127 gdb_byte *cur_ptr;
9128
9129 cur_ptr = info_ptr;
9130 first_die = last_sibling = NULL;
9131
9132 while (1)
9133 {
9134 struct die_info *die
9135 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9136
9137 if (die == NULL)
9138 {
9139 *new_info_ptr = cur_ptr;
9140 return first_die;
9141 }
9142
9143 if (!first_die)
9144 first_die = die;
9145 else
9146 last_sibling->sibling = die;
9147
9148 last_sibling = die;
9149 }
9150 }
9151
9152 /* Read the die from the .debug_info section buffer. Set DIEP to
9153 point to a newly allocated die with its information, except for its
9154 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9155 whether the die has children or not. */
9156
9157 static gdb_byte *
9158 read_full_die (const struct die_reader_specs *reader,
9159 struct die_info **diep, gdb_byte *info_ptr,
9160 int *has_children)
9161 {
9162 unsigned int abbrev_number, bytes_read, i, offset;
9163 struct abbrev_info *abbrev;
9164 struct die_info *die;
9165 struct dwarf2_cu *cu = reader->cu;
9166 bfd *abfd = reader->abfd;
9167
9168 offset = info_ptr - reader->buffer;
9169 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9170 info_ptr += bytes_read;
9171 if (!abbrev_number)
9172 {
9173 *diep = NULL;
9174 *has_children = 0;
9175 return info_ptr;
9176 }
9177
9178 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9179 if (!abbrev)
9180 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9181 abbrev_number,
9182 bfd_get_filename (abfd));
9183
9184 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9185 die->offset = offset;
9186 die->tag = abbrev->tag;
9187 die->abbrev = abbrev_number;
9188
9189 die->num_attrs = abbrev->num_attrs;
9190
9191 for (i = 0; i < abbrev->num_attrs; ++i)
9192 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9193 abfd, info_ptr, cu);
9194
9195 *diep = die;
9196 *has_children = abbrev->has_children;
9197 return info_ptr;
9198 }
9199
9200 /* In DWARF version 2, the description of the debugging information is
9201 stored in a separate .debug_abbrev section. Before we read any
9202 dies from a section we read in all abbreviations and install them
9203 in a hash table. This function also sets flags in CU describing
9204 the data found in the abbrev table. */
9205
9206 static void
9207 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
9208 {
9209 struct comp_unit_head *cu_header = &cu->header;
9210 gdb_byte *abbrev_ptr;
9211 struct abbrev_info *cur_abbrev;
9212 unsigned int abbrev_number, bytes_read, abbrev_name;
9213 unsigned int abbrev_form, hash_number;
9214 struct attr_abbrev *cur_attrs;
9215 unsigned int allocated_attrs;
9216
9217 /* Initialize dwarf2 abbrevs. */
9218 obstack_init (&cu->abbrev_obstack);
9219 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9220 (ABBREV_HASH_SIZE
9221 * sizeof (struct abbrev_info *)));
9222 memset (cu->dwarf2_abbrevs, 0,
9223 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9224
9225 dwarf2_read_section (dwarf2_per_objfile->objfile,
9226 &dwarf2_per_objfile->abbrev);
9227 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9228 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9229 abbrev_ptr += bytes_read;
9230
9231 allocated_attrs = ATTR_ALLOC_CHUNK;
9232 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9233
9234 /* Loop until we reach an abbrev number of 0. */
9235 while (abbrev_number)
9236 {
9237 cur_abbrev = dwarf_alloc_abbrev (cu);
9238
9239 /* read in abbrev header */
9240 cur_abbrev->number = abbrev_number;
9241 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9242 abbrev_ptr += bytes_read;
9243 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9244 abbrev_ptr += 1;
9245
9246 if (cur_abbrev->tag == DW_TAG_namespace)
9247 cu->has_namespace_info = 1;
9248
9249 /* now read in declarations */
9250 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9251 abbrev_ptr += bytes_read;
9252 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9253 abbrev_ptr += bytes_read;
9254 while (abbrev_name)
9255 {
9256 if (cur_abbrev->num_attrs == allocated_attrs)
9257 {
9258 allocated_attrs += ATTR_ALLOC_CHUNK;
9259 cur_attrs
9260 = xrealloc (cur_attrs, (allocated_attrs
9261 * sizeof (struct attr_abbrev)));
9262 }
9263
9264 /* Record whether this compilation unit might have
9265 inter-compilation-unit references. If we don't know what form
9266 this attribute will have, then it might potentially be a
9267 DW_FORM_ref_addr, so we conservatively expect inter-CU
9268 references. */
9269
9270 if (abbrev_form == DW_FORM_ref_addr
9271 || abbrev_form == DW_FORM_indirect)
9272 cu->has_form_ref_addr = 1;
9273
9274 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9275 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9276 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9277 abbrev_ptr += bytes_read;
9278 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9279 abbrev_ptr += bytes_read;
9280 }
9281
9282 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9283 (cur_abbrev->num_attrs
9284 * sizeof (struct attr_abbrev)));
9285 memcpy (cur_abbrev->attrs, cur_attrs,
9286 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9287
9288 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9289 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9290 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9291
9292 /* Get next abbreviation.
9293 Under Irix6 the abbreviations for a compilation unit are not
9294 always properly terminated with an abbrev number of 0.
9295 Exit loop if we encounter an abbreviation which we have
9296 already read (which means we are about to read the abbreviations
9297 for the next compile unit) or if the end of the abbreviation
9298 table is reached. */
9299 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9300 >= dwarf2_per_objfile->abbrev.size)
9301 break;
9302 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9303 abbrev_ptr += bytes_read;
9304 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9305 break;
9306 }
9307
9308 xfree (cur_attrs);
9309 }
9310
9311 /* Release the memory used by the abbrev table for a compilation unit. */
9312
9313 static void
9314 dwarf2_free_abbrev_table (void *ptr_to_cu)
9315 {
9316 struct dwarf2_cu *cu = ptr_to_cu;
9317
9318 obstack_free (&cu->abbrev_obstack, NULL);
9319 cu->dwarf2_abbrevs = NULL;
9320 }
9321
9322 /* Lookup an abbrev_info structure in the abbrev hash table. */
9323
9324 static struct abbrev_info *
9325 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9326 {
9327 unsigned int hash_number;
9328 struct abbrev_info *abbrev;
9329
9330 hash_number = number % ABBREV_HASH_SIZE;
9331 abbrev = cu->dwarf2_abbrevs[hash_number];
9332
9333 while (abbrev)
9334 {
9335 if (abbrev->number == number)
9336 return abbrev;
9337 else
9338 abbrev = abbrev->next;
9339 }
9340 return NULL;
9341 }
9342
9343 /* Returns nonzero if TAG represents a type that we might generate a partial
9344 symbol for. */
9345
9346 static int
9347 is_type_tag_for_partial (int tag)
9348 {
9349 switch (tag)
9350 {
9351 #if 0
9352 /* Some types that would be reasonable to generate partial symbols for,
9353 that we don't at present. */
9354 case DW_TAG_array_type:
9355 case DW_TAG_file_type:
9356 case DW_TAG_ptr_to_member_type:
9357 case DW_TAG_set_type:
9358 case DW_TAG_string_type:
9359 case DW_TAG_subroutine_type:
9360 #endif
9361 case DW_TAG_base_type:
9362 case DW_TAG_class_type:
9363 case DW_TAG_interface_type:
9364 case DW_TAG_enumeration_type:
9365 case DW_TAG_structure_type:
9366 case DW_TAG_subrange_type:
9367 case DW_TAG_typedef:
9368 case DW_TAG_union_type:
9369 return 1;
9370 default:
9371 return 0;
9372 }
9373 }
9374
9375 /* Load all DIEs that are interesting for partial symbols into memory. */
9376
9377 static struct partial_die_info *
9378 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9379 int building_psymtab, struct dwarf2_cu *cu)
9380 {
9381 struct partial_die_info *part_die;
9382 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9383 struct abbrev_info *abbrev;
9384 unsigned int bytes_read;
9385 unsigned int load_all = 0;
9386
9387 int nesting_level = 1;
9388
9389 parent_die = NULL;
9390 last_die = NULL;
9391
9392 if (cu->per_cu && cu->per_cu->load_all_dies)
9393 load_all = 1;
9394
9395 cu->partial_dies
9396 = htab_create_alloc_ex (cu->header.length / 12,
9397 partial_die_hash,
9398 partial_die_eq,
9399 NULL,
9400 &cu->comp_unit_obstack,
9401 hashtab_obstack_allocate,
9402 dummy_obstack_deallocate);
9403
9404 part_die = obstack_alloc (&cu->comp_unit_obstack,
9405 sizeof (struct partial_die_info));
9406
9407 while (1)
9408 {
9409 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9410
9411 /* A NULL abbrev means the end of a series of children. */
9412 if (abbrev == NULL)
9413 {
9414 if (--nesting_level == 0)
9415 {
9416 /* PART_DIE was probably the last thing allocated on the
9417 comp_unit_obstack, so we could call obstack_free
9418 here. We don't do that because the waste is small,
9419 and will be cleaned up when we're done with this
9420 compilation unit. This way, we're also more robust
9421 against other users of the comp_unit_obstack. */
9422 return first_die;
9423 }
9424 info_ptr += bytes_read;
9425 last_die = parent_die;
9426 parent_die = parent_die->die_parent;
9427 continue;
9428 }
9429
9430 /* Check for template arguments. We never save these; if
9431 they're seen, we just mark the parent, and go on our way. */
9432 if (parent_die != NULL
9433 && cu->language == language_cplus
9434 && (abbrev->tag == DW_TAG_template_type_param
9435 || abbrev->tag == DW_TAG_template_value_param))
9436 {
9437 parent_die->has_template_arguments = 1;
9438
9439 if (!load_all)
9440 {
9441 /* We don't need a partial DIE for the template argument. */
9442 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9443 cu);
9444 continue;
9445 }
9446 }
9447
9448 /* We only recurse into subprograms looking for template arguments.
9449 Skip their other children. */
9450 if (!load_all
9451 && cu->language == language_cplus
9452 && parent_die != NULL
9453 && parent_die->tag == DW_TAG_subprogram)
9454 {
9455 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9456 continue;
9457 }
9458
9459 /* Check whether this DIE is interesting enough to save. Normally
9460 we would not be interested in members here, but there may be
9461 later variables referencing them via DW_AT_specification (for
9462 static members). */
9463 if (!load_all
9464 && !is_type_tag_for_partial (abbrev->tag)
9465 && abbrev->tag != DW_TAG_constant
9466 && abbrev->tag != DW_TAG_enumerator
9467 && abbrev->tag != DW_TAG_subprogram
9468 && abbrev->tag != DW_TAG_lexical_block
9469 && abbrev->tag != DW_TAG_variable
9470 && abbrev->tag != DW_TAG_namespace
9471 && abbrev->tag != DW_TAG_module
9472 && abbrev->tag != DW_TAG_member)
9473 {
9474 /* Otherwise we skip to the next sibling, if any. */
9475 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9476 continue;
9477 }
9478
9479 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9480 buffer, info_ptr, cu);
9481
9482 /* This two-pass algorithm for processing partial symbols has a
9483 high cost in cache pressure. Thus, handle some simple cases
9484 here which cover the majority of C partial symbols. DIEs
9485 which neither have specification tags in them, nor could have
9486 specification tags elsewhere pointing at them, can simply be
9487 processed and discarded.
9488
9489 This segment is also optional; scan_partial_symbols and
9490 add_partial_symbol will handle these DIEs if we chain
9491 them in normally. When compilers which do not emit large
9492 quantities of duplicate debug information are more common,
9493 this code can probably be removed. */
9494
9495 /* Any complete simple types at the top level (pretty much all
9496 of them, for a language without namespaces), can be processed
9497 directly. */
9498 if (parent_die == NULL
9499 && part_die->has_specification == 0
9500 && part_die->is_declaration == 0
9501 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9502 || part_die->tag == DW_TAG_base_type
9503 || part_die->tag == DW_TAG_subrange_type))
9504 {
9505 if (building_psymtab && part_die->name != NULL)
9506 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9507 VAR_DOMAIN, LOC_TYPEDEF,
9508 &cu->objfile->static_psymbols,
9509 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9510 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9511 continue;
9512 }
9513
9514 /* The exception for DW_TAG_typedef with has_children above is
9515 a workaround of GCC PR debug/47510. In the case of this complaint
9516 type_name_no_tag_or_error will error on such types later.
9517
9518 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9519 it could not find the child DIEs referenced later, this is checked
9520 above. In correct DWARF DW_TAG_typedef should have no children. */
9521
9522 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9523 complaint (&symfile_complaints,
9524 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9525 "- DIE at 0x%x [in module %s]"),
9526 part_die->offset, cu->objfile->name);
9527
9528 /* If we're at the second level, and we're an enumerator, and
9529 our parent has no specification (meaning possibly lives in a
9530 namespace elsewhere), then we can add the partial symbol now
9531 instead of queueing it. */
9532 if (part_die->tag == DW_TAG_enumerator
9533 && parent_die != NULL
9534 && parent_die->die_parent == NULL
9535 && parent_die->tag == DW_TAG_enumeration_type
9536 && parent_die->has_specification == 0)
9537 {
9538 if (part_die->name == NULL)
9539 complaint (&symfile_complaints,
9540 _("malformed enumerator DIE ignored"));
9541 else if (building_psymtab)
9542 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9543 VAR_DOMAIN, LOC_CONST,
9544 (cu->language == language_cplus
9545 || cu->language == language_java)
9546 ? &cu->objfile->global_psymbols
9547 : &cu->objfile->static_psymbols,
9548 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9549
9550 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9551 continue;
9552 }
9553
9554 /* We'll save this DIE so link it in. */
9555 part_die->die_parent = parent_die;
9556 part_die->die_sibling = NULL;
9557 part_die->die_child = NULL;
9558
9559 if (last_die && last_die == parent_die)
9560 last_die->die_child = part_die;
9561 else if (last_die)
9562 last_die->die_sibling = part_die;
9563
9564 last_die = part_die;
9565
9566 if (first_die == NULL)
9567 first_die = part_die;
9568
9569 /* Maybe add the DIE to the hash table. Not all DIEs that we
9570 find interesting need to be in the hash table, because we
9571 also have the parent/sibling/child chains; only those that we
9572 might refer to by offset later during partial symbol reading.
9573
9574 For now this means things that might have be the target of a
9575 DW_AT_specification, DW_AT_abstract_origin, or
9576 DW_AT_extension. DW_AT_extension will refer only to
9577 namespaces; DW_AT_abstract_origin refers to functions (and
9578 many things under the function DIE, but we do not recurse
9579 into function DIEs during partial symbol reading) and
9580 possibly variables as well; DW_AT_specification refers to
9581 declarations. Declarations ought to have the DW_AT_declaration
9582 flag. It happens that GCC forgets to put it in sometimes, but
9583 only for functions, not for types.
9584
9585 Adding more things than necessary to the hash table is harmless
9586 except for the performance cost. Adding too few will result in
9587 wasted time in find_partial_die, when we reread the compilation
9588 unit with load_all_dies set. */
9589
9590 if (load_all
9591 || abbrev->tag == DW_TAG_constant
9592 || abbrev->tag == DW_TAG_subprogram
9593 || abbrev->tag == DW_TAG_variable
9594 || abbrev->tag == DW_TAG_namespace
9595 || part_die->is_declaration)
9596 {
9597 void **slot;
9598
9599 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9600 part_die->offset, INSERT);
9601 *slot = part_die;
9602 }
9603
9604 part_die = obstack_alloc (&cu->comp_unit_obstack,
9605 sizeof (struct partial_die_info));
9606
9607 /* For some DIEs we want to follow their children (if any). For C
9608 we have no reason to follow the children of structures; for other
9609 languages we have to, so that we can get at method physnames
9610 to infer fully qualified class names, for DW_AT_specification,
9611 and for C++ template arguments. For C++, we also look one level
9612 inside functions to find template arguments (if the name of the
9613 function does not already contain the template arguments).
9614
9615 For Ada, we need to scan the children of subprograms and lexical
9616 blocks as well because Ada allows the definition of nested
9617 entities that could be interesting for the debugger, such as
9618 nested subprograms for instance. */
9619 if (last_die->has_children
9620 && (load_all
9621 || last_die->tag == DW_TAG_namespace
9622 || last_die->tag == DW_TAG_module
9623 || last_die->tag == DW_TAG_enumeration_type
9624 || (cu->language == language_cplus
9625 && last_die->tag == DW_TAG_subprogram
9626 && (last_die->name == NULL
9627 || strchr (last_die->name, '<') == NULL))
9628 || (cu->language != language_c
9629 && (last_die->tag == DW_TAG_class_type
9630 || last_die->tag == DW_TAG_interface_type
9631 || last_die->tag == DW_TAG_structure_type
9632 || last_die->tag == DW_TAG_union_type))
9633 || (cu->language == language_ada
9634 && (last_die->tag == DW_TAG_subprogram
9635 || last_die->tag == DW_TAG_lexical_block))))
9636 {
9637 nesting_level++;
9638 parent_die = last_die;
9639 continue;
9640 }
9641
9642 /* Otherwise we skip to the next sibling, if any. */
9643 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9644
9645 /* Back to the top, do it again. */
9646 }
9647 }
9648
9649 /* Read a minimal amount of information into the minimal die structure. */
9650
9651 static gdb_byte *
9652 read_partial_die (struct partial_die_info *part_die,
9653 struct abbrev_info *abbrev,
9654 unsigned int abbrev_len, bfd *abfd,
9655 gdb_byte *buffer, gdb_byte *info_ptr,
9656 struct dwarf2_cu *cu)
9657 {
9658 unsigned int i;
9659 struct attribute attr;
9660 int has_low_pc_attr = 0;
9661 int has_high_pc_attr = 0;
9662
9663 memset (part_die, 0, sizeof (struct partial_die_info));
9664
9665 part_die->offset = info_ptr - buffer;
9666
9667 info_ptr += abbrev_len;
9668
9669 if (abbrev == NULL)
9670 return info_ptr;
9671
9672 part_die->tag = abbrev->tag;
9673 part_die->has_children = abbrev->has_children;
9674
9675 for (i = 0; i < abbrev->num_attrs; ++i)
9676 {
9677 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9678
9679 /* Store the data if it is of an attribute we want to keep in a
9680 partial symbol table. */
9681 switch (attr.name)
9682 {
9683 case DW_AT_name:
9684 switch (part_die->tag)
9685 {
9686 case DW_TAG_compile_unit:
9687 case DW_TAG_type_unit:
9688 /* Compilation units have a DW_AT_name that is a filename, not
9689 a source language identifier. */
9690 case DW_TAG_enumeration_type:
9691 case DW_TAG_enumerator:
9692 /* These tags always have simple identifiers already; no need
9693 to canonicalize them. */
9694 part_die->name = DW_STRING (&attr);
9695 break;
9696 default:
9697 part_die->name
9698 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9699 &cu->objfile->objfile_obstack);
9700 break;
9701 }
9702 break;
9703 case DW_AT_linkage_name:
9704 case DW_AT_MIPS_linkage_name:
9705 /* Note that both forms of linkage name might appear. We
9706 assume they will be the same, and we only store the last
9707 one we see. */
9708 if (cu->language == language_ada)
9709 part_die->name = DW_STRING (&attr);
9710 part_die->linkage_name = DW_STRING (&attr);
9711 break;
9712 case DW_AT_low_pc:
9713 has_low_pc_attr = 1;
9714 part_die->lowpc = DW_ADDR (&attr);
9715 break;
9716 case DW_AT_high_pc:
9717 has_high_pc_attr = 1;
9718 part_die->highpc = DW_ADDR (&attr);
9719 break;
9720 case DW_AT_location:
9721 /* Support the .debug_loc offsets. */
9722 if (attr_form_is_block (&attr))
9723 {
9724 part_die->locdesc = DW_BLOCK (&attr);
9725 }
9726 else if (attr_form_is_section_offset (&attr))
9727 {
9728 dwarf2_complex_location_expr_complaint ();
9729 }
9730 else
9731 {
9732 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9733 "partial symbol information");
9734 }
9735 break;
9736 case DW_AT_external:
9737 part_die->is_external = DW_UNSND (&attr);
9738 break;
9739 case DW_AT_declaration:
9740 part_die->is_declaration = DW_UNSND (&attr);
9741 break;
9742 case DW_AT_type:
9743 part_die->has_type = 1;
9744 break;
9745 case DW_AT_abstract_origin:
9746 case DW_AT_specification:
9747 case DW_AT_extension:
9748 part_die->has_specification = 1;
9749 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9750 break;
9751 case DW_AT_sibling:
9752 /* Ignore absolute siblings, they might point outside of
9753 the current compile unit. */
9754 if (attr.form == DW_FORM_ref_addr)
9755 complaint (&symfile_complaints,
9756 _("ignoring absolute DW_AT_sibling"));
9757 else
9758 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9759 break;
9760 case DW_AT_byte_size:
9761 part_die->has_byte_size = 1;
9762 break;
9763 case DW_AT_calling_convention:
9764 /* DWARF doesn't provide a way to identify a program's source-level
9765 entry point. DW_AT_calling_convention attributes are only meant
9766 to describe functions' calling conventions.
9767
9768 However, because it's a necessary piece of information in
9769 Fortran, and because DW_CC_program is the only piece of debugging
9770 information whose definition refers to a 'main program' at all,
9771 several compilers have begun marking Fortran main programs with
9772 DW_CC_program --- even when those functions use the standard
9773 calling conventions.
9774
9775 So until DWARF specifies a way to provide this information and
9776 compilers pick up the new representation, we'll support this
9777 practice. */
9778 if (DW_UNSND (&attr) == DW_CC_program
9779 && cu->language == language_fortran)
9780 {
9781 set_main_name (part_die->name);
9782
9783 /* As this DIE has a static linkage the name would be difficult
9784 to look up later. */
9785 language_of_main = language_fortran;
9786 }
9787 break;
9788 default:
9789 break;
9790 }
9791 }
9792
9793 if (has_low_pc_attr && has_high_pc_attr)
9794 {
9795 /* When using the GNU linker, .gnu.linkonce. sections are used to
9796 eliminate duplicate copies of functions and vtables and such.
9797 The linker will arbitrarily choose one and discard the others.
9798 The AT_*_pc values for such functions refer to local labels in
9799 these sections. If the section from that file was discarded, the
9800 labels are not in the output, so the relocs get a value of 0.
9801 If this is a discarded function, mark the pc bounds as invalid,
9802 so that GDB will ignore it. */
9803 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9804 {
9805 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9806
9807 complaint (&symfile_complaints,
9808 _("DW_AT_low_pc %s is zero "
9809 "for DIE at 0x%x [in module %s]"),
9810 paddress (gdbarch, part_die->lowpc),
9811 part_die->offset, cu->objfile->name);
9812 }
9813 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9814 else if (part_die->lowpc >= part_die->highpc)
9815 {
9816 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9817
9818 complaint (&symfile_complaints,
9819 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9820 "for DIE at 0x%x [in module %s]"),
9821 paddress (gdbarch, part_die->lowpc),
9822 paddress (gdbarch, part_die->highpc),
9823 part_die->offset, cu->objfile->name);
9824 }
9825 else
9826 part_die->has_pc_info = 1;
9827 }
9828
9829 return info_ptr;
9830 }
9831
9832 /* Find a cached partial DIE at OFFSET in CU. */
9833
9834 static struct partial_die_info *
9835 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9836 {
9837 struct partial_die_info *lookup_die = NULL;
9838 struct partial_die_info part_die;
9839
9840 part_die.offset = offset;
9841 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9842
9843 return lookup_die;
9844 }
9845
9846 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9847 except in the case of .debug_types DIEs which do not reference
9848 outside their CU (they do however referencing other types via
9849 DW_FORM_ref_sig8). */
9850
9851 static struct partial_die_info *
9852 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9853 {
9854 struct dwarf2_per_cu_data *per_cu = NULL;
9855 struct partial_die_info *pd = NULL;
9856
9857 if (cu->per_cu->debug_type_section)
9858 {
9859 pd = find_partial_die_in_comp_unit (offset, cu);
9860 if (pd != NULL)
9861 return pd;
9862 goto not_found;
9863 }
9864
9865 if (offset_in_cu_p (&cu->header, offset))
9866 {
9867 pd = find_partial_die_in_comp_unit (offset, cu);
9868 if (pd != NULL)
9869 return pd;
9870 }
9871
9872 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9873
9874 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9875 load_partial_comp_unit (per_cu, cu->objfile);
9876
9877 per_cu->cu->last_used = 0;
9878 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9879
9880 if (pd == NULL && per_cu->load_all_dies == 0)
9881 {
9882 struct cleanup *back_to;
9883 struct partial_die_info comp_unit_die;
9884 struct abbrev_info *abbrev;
9885 unsigned int bytes_read;
9886 char *info_ptr;
9887
9888 per_cu->load_all_dies = 1;
9889
9890 /* Re-read the DIEs. */
9891 back_to = make_cleanup (null_cleanup, 0);
9892 if (per_cu->cu->dwarf2_abbrevs == NULL)
9893 {
9894 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9895 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9896 }
9897 info_ptr = (dwarf2_per_objfile->info.buffer
9898 + per_cu->cu->header.offset
9899 + per_cu->cu->header.first_die_offset);
9900 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9901 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9902 per_cu->cu->objfile->obfd,
9903 dwarf2_per_objfile->info.buffer, info_ptr,
9904 per_cu->cu);
9905 if (comp_unit_die.has_children)
9906 load_partial_dies (per_cu->cu->objfile->obfd,
9907 dwarf2_per_objfile->info.buffer, info_ptr,
9908 0, per_cu->cu);
9909 do_cleanups (back_to);
9910
9911 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9912 }
9913
9914 not_found:
9915
9916 if (pd == NULL)
9917 internal_error (__FILE__, __LINE__,
9918 _("could not find partial DIE 0x%x "
9919 "in cache [from module %s]\n"),
9920 offset, bfd_get_filename (cu->objfile->obfd));
9921 return pd;
9922 }
9923
9924 /* See if we can figure out if the class lives in a namespace. We do
9925 this by looking for a member function; its demangled name will
9926 contain namespace info, if there is any. */
9927
9928 static void
9929 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9930 struct dwarf2_cu *cu)
9931 {
9932 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9933 what template types look like, because the demangler
9934 frequently doesn't give the same name as the debug info. We
9935 could fix this by only using the demangled name to get the
9936 prefix (but see comment in read_structure_type). */
9937
9938 struct partial_die_info *real_pdi;
9939 struct partial_die_info *child_pdi;
9940
9941 /* If this DIE (this DIE's specification, if any) has a parent, then
9942 we should not do this. We'll prepend the parent's fully qualified
9943 name when we create the partial symbol. */
9944
9945 real_pdi = struct_pdi;
9946 while (real_pdi->has_specification)
9947 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9948
9949 if (real_pdi->die_parent != NULL)
9950 return;
9951
9952 for (child_pdi = struct_pdi->die_child;
9953 child_pdi != NULL;
9954 child_pdi = child_pdi->die_sibling)
9955 {
9956 if (child_pdi->tag == DW_TAG_subprogram
9957 && child_pdi->linkage_name != NULL)
9958 {
9959 char *actual_class_name
9960 = language_class_name_from_physname (cu->language_defn,
9961 child_pdi->linkage_name);
9962 if (actual_class_name != NULL)
9963 {
9964 struct_pdi->name
9965 = obsavestring (actual_class_name,
9966 strlen (actual_class_name),
9967 &cu->objfile->objfile_obstack);
9968 xfree (actual_class_name);
9969 }
9970 break;
9971 }
9972 }
9973 }
9974
9975 /* Adjust PART_DIE before generating a symbol for it. This function
9976 may set the is_external flag or change the DIE's name. */
9977
9978 static void
9979 fixup_partial_die (struct partial_die_info *part_die,
9980 struct dwarf2_cu *cu)
9981 {
9982 /* Once we've fixed up a die, there's no point in doing so again.
9983 This also avoids a memory leak if we were to call
9984 guess_partial_die_structure_name multiple times. */
9985 if (part_die->fixup_called)
9986 return;
9987
9988 /* If we found a reference attribute and the DIE has no name, try
9989 to find a name in the referred to DIE. */
9990
9991 if (part_die->name == NULL && part_die->has_specification)
9992 {
9993 struct partial_die_info *spec_die;
9994
9995 spec_die = find_partial_die (part_die->spec_offset, cu);
9996
9997 fixup_partial_die (spec_die, cu);
9998
9999 if (spec_die->name)
10000 {
10001 part_die->name = spec_die->name;
10002
10003 /* Copy DW_AT_external attribute if it is set. */
10004 if (spec_die->is_external)
10005 part_die->is_external = spec_die->is_external;
10006 }
10007 }
10008
10009 /* Set default names for some unnamed DIEs. */
10010
10011 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10012 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10013
10014 /* If there is no parent die to provide a namespace, and there are
10015 children, see if we can determine the namespace from their linkage
10016 name.
10017 NOTE: We need to do this even if cu->has_namespace_info != 0.
10018 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10019 if (cu->language == language_cplus
10020 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10021 && part_die->die_parent == NULL
10022 && part_die->has_children
10023 && (part_die->tag == DW_TAG_class_type
10024 || part_die->tag == DW_TAG_structure_type
10025 || part_die->tag == DW_TAG_union_type))
10026 guess_partial_die_structure_name (part_die, cu);
10027
10028 /* GCC might emit a nameless struct or union that has a linkage
10029 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10030 if (part_die->name == NULL
10031 && (part_die->tag == DW_TAG_class_type
10032 || part_die->tag == DW_TAG_interface_type
10033 || part_die->tag == DW_TAG_structure_type
10034 || part_die->tag == DW_TAG_union_type)
10035 && part_die->linkage_name != NULL)
10036 {
10037 char *demangled;
10038
10039 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10040 if (demangled)
10041 {
10042 const char *base;
10043
10044 /* Strip any leading namespaces/classes, keep only the base name.
10045 DW_AT_name for named DIEs does not contain the prefixes. */
10046 base = strrchr (demangled, ':');
10047 if (base && base > demangled && base[-1] == ':')
10048 base++;
10049 else
10050 base = demangled;
10051
10052 part_die->name = obsavestring (base, strlen (base),
10053 &cu->objfile->objfile_obstack);
10054 xfree (demangled);
10055 }
10056 }
10057
10058 part_die->fixup_called = 1;
10059 }
10060
10061 /* Read an attribute value described by an attribute form. */
10062
10063 static gdb_byte *
10064 read_attribute_value (struct attribute *attr, unsigned form,
10065 bfd *abfd, gdb_byte *info_ptr,
10066 struct dwarf2_cu *cu)
10067 {
10068 struct comp_unit_head *cu_header = &cu->header;
10069 unsigned int bytes_read;
10070 struct dwarf_block *blk;
10071
10072 attr->form = form;
10073 switch (form)
10074 {
10075 case DW_FORM_ref_addr:
10076 if (cu->header.version == 2)
10077 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10078 else
10079 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10080 &cu->header, &bytes_read);
10081 info_ptr += bytes_read;
10082 break;
10083 case DW_FORM_addr:
10084 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10085 info_ptr += bytes_read;
10086 break;
10087 case DW_FORM_block2:
10088 blk = dwarf_alloc_block (cu);
10089 blk->size = read_2_bytes (abfd, info_ptr);
10090 info_ptr += 2;
10091 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10092 info_ptr += blk->size;
10093 DW_BLOCK (attr) = blk;
10094 break;
10095 case DW_FORM_block4:
10096 blk = dwarf_alloc_block (cu);
10097 blk->size = read_4_bytes (abfd, info_ptr);
10098 info_ptr += 4;
10099 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10100 info_ptr += blk->size;
10101 DW_BLOCK (attr) = blk;
10102 break;
10103 case DW_FORM_data2:
10104 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10105 info_ptr += 2;
10106 break;
10107 case DW_FORM_data4:
10108 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10109 info_ptr += 4;
10110 break;
10111 case DW_FORM_data8:
10112 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10113 info_ptr += 8;
10114 break;
10115 case DW_FORM_sec_offset:
10116 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10117 info_ptr += bytes_read;
10118 break;
10119 case DW_FORM_string:
10120 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10121 DW_STRING_IS_CANONICAL (attr) = 0;
10122 info_ptr += bytes_read;
10123 break;
10124 case DW_FORM_strp:
10125 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10126 &bytes_read);
10127 DW_STRING_IS_CANONICAL (attr) = 0;
10128 info_ptr += bytes_read;
10129 break;
10130 case DW_FORM_exprloc:
10131 case DW_FORM_block:
10132 blk = dwarf_alloc_block (cu);
10133 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10134 info_ptr += bytes_read;
10135 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10136 info_ptr += blk->size;
10137 DW_BLOCK (attr) = blk;
10138 break;
10139 case DW_FORM_block1:
10140 blk = dwarf_alloc_block (cu);
10141 blk->size = read_1_byte (abfd, info_ptr);
10142 info_ptr += 1;
10143 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10144 info_ptr += blk->size;
10145 DW_BLOCK (attr) = blk;
10146 break;
10147 case DW_FORM_data1:
10148 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10149 info_ptr += 1;
10150 break;
10151 case DW_FORM_flag:
10152 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10153 info_ptr += 1;
10154 break;
10155 case DW_FORM_flag_present:
10156 DW_UNSND (attr) = 1;
10157 break;
10158 case DW_FORM_sdata:
10159 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10160 info_ptr += bytes_read;
10161 break;
10162 case DW_FORM_udata:
10163 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10164 info_ptr += bytes_read;
10165 break;
10166 case DW_FORM_ref1:
10167 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10168 info_ptr += 1;
10169 break;
10170 case DW_FORM_ref2:
10171 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10172 info_ptr += 2;
10173 break;
10174 case DW_FORM_ref4:
10175 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10176 info_ptr += 4;
10177 break;
10178 case DW_FORM_ref8:
10179 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10180 info_ptr += 8;
10181 break;
10182 case DW_FORM_ref_sig8:
10183 /* Convert the signature to something we can record in DW_UNSND
10184 for later lookup.
10185 NOTE: This is NULL if the type wasn't found. */
10186 DW_SIGNATURED_TYPE (attr) =
10187 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10188 info_ptr += 8;
10189 break;
10190 case DW_FORM_ref_udata:
10191 DW_ADDR (attr) = (cu->header.offset
10192 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10193 info_ptr += bytes_read;
10194 break;
10195 case DW_FORM_indirect:
10196 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10197 info_ptr += bytes_read;
10198 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10199 break;
10200 default:
10201 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10202 dwarf_form_name (form),
10203 bfd_get_filename (abfd));
10204 }
10205
10206 /* We have seen instances where the compiler tried to emit a byte
10207 size attribute of -1 which ended up being encoded as an unsigned
10208 0xffffffff. Although 0xffffffff is technically a valid size value,
10209 an object of this size seems pretty unlikely so we can relatively
10210 safely treat these cases as if the size attribute was invalid and
10211 treat them as zero by default. */
10212 if (attr->name == DW_AT_byte_size
10213 && form == DW_FORM_data4
10214 && DW_UNSND (attr) >= 0xffffffff)
10215 {
10216 complaint
10217 (&symfile_complaints,
10218 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10219 hex_string (DW_UNSND (attr)));
10220 DW_UNSND (attr) = 0;
10221 }
10222
10223 return info_ptr;
10224 }
10225
10226 /* Read an attribute described by an abbreviated attribute. */
10227
10228 static gdb_byte *
10229 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10230 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10231 {
10232 attr->name = abbrev->name;
10233 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10234 }
10235
10236 /* Read dwarf information from a buffer. */
10237
10238 static unsigned int
10239 read_1_byte (bfd *abfd, gdb_byte *buf)
10240 {
10241 return bfd_get_8 (abfd, buf);
10242 }
10243
10244 static int
10245 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10246 {
10247 return bfd_get_signed_8 (abfd, buf);
10248 }
10249
10250 static unsigned int
10251 read_2_bytes (bfd *abfd, gdb_byte *buf)
10252 {
10253 return bfd_get_16 (abfd, buf);
10254 }
10255
10256 static unsigned int
10257 read_4_bytes (bfd *abfd, gdb_byte *buf)
10258 {
10259 return bfd_get_32 (abfd, buf);
10260 }
10261
10262 static ULONGEST
10263 read_8_bytes (bfd *abfd, gdb_byte *buf)
10264 {
10265 return bfd_get_64 (abfd, buf);
10266 }
10267
10268 static CORE_ADDR
10269 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10270 unsigned int *bytes_read)
10271 {
10272 struct comp_unit_head *cu_header = &cu->header;
10273 CORE_ADDR retval = 0;
10274
10275 if (cu_header->signed_addr_p)
10276 {
10277 switch (cu_header->addr_size)
10278 {
10279 case 2:
10280 retval = bfd_get_signed_16 (abfd, buf);
10281 break;
10282 case 4:
10283 retval = bfd_get_signed_32 (abfd, buf);
10284 break;
10285 case 8:
10286 retval = bfd_get_signed_64 (abfd, buf);
10287 break;
10288 default:
10289 internal_error (__FILE__, __LINE__,
10290 _("read_address: bad switch, signed [in module %s]"),
10291 bfd_get_filename (abfd));
10292 }
10293 }
10294 else
10295 {
10296 switch (cu_header->addr_size)
10297 {
10298 case 2:
10299 retval = bfd_get_16 (abfd, buf);
10300 break;
10301 case 4:
10302 retval = bfd_get_32 (abfd, buf);
10303 break;
10304 case 8:
10305 retval = bfd_get_64 (abfd, buf);
10306 break;
10307 default:
10308 internal_error (__FILE__, __LINE__,
10309 _("read_address: bad switch, "
10310 "unsigned [in module %s]"),
10311 bfd_get_filename (abfd));
10312 }
10313 }
10314
10315 *bytes_read = cu_header->addr_size;
10316 return retval;
10317 }
10318
10319 /* Read the initial length from a section. The (draft) DWARF 3
10320 specification allows the initial length to take up either 4 bytes
10321 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10322 bytes describe the length and all offsets will be 8 bytes in length
10323 instead of 4.
10324
10325 An older, non-standard 64-bit format is also handled by this
10326 function. The older format in question stores the initial length
10327 as an 8-byte quantity without an escape value. Lengths greater
10328 than 2^32 aren't very common which means that the initial 4 bytes
10329 is almost always zero. Since a length value of zero doesn't make
10330 sense for the 32-bit format, this initial zero can be considered to
10331 be an escape value which indicates the presence of the older 64-bit
10332 format. As written, the code can't detect (old format) lengths
10333 greater than 4GB. If it becomes necessary to handle lengths
10334 somewhat larger than 4GB, we could allow other small values (such
10335 as the non-sensical values of 1, 2, and 3) to also be used as
10336 escape values indicating the presence of the old format.
10337
10338 The value returned via bytes_read should be used to increment the
10339 relevant pointer after calling read_initial_length().
10340
10341 [ Note: read_initial_length() and read_offset() are based on the
10342 document entitled "DWARF Debugging Information Format", revision
10343 3, draft 8, dated November 19, 2001. This document was obtained
10344 from:
10345
10346 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10347
10348 This document is only a draft and is subject to change. (So beware.)
10349
10350 Details regarding the older, non-standard 64-bit format were
10351 determined empirically by examining 64-bit ELF files produced by
10352 the SGI toolchain on an IRIX 6.5 machine.
10353
10354 - Kevin, July 16, 2002
10355 ] */
10356
10357 static LONGEST
10358 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10359 {
10360 LONGEST length = bfd_get_32 (abfd, buf);
10361
10362 if (length == 0xffffffff)
10363 {
10364 length = bfd_get_64 (abfd, buf + 4);
10365 *bytes_read = 12;
10366 }
10367 else if (length == 0)
10368 {
10369 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10370 length = bfd_get_64 (abfd, buf);
10371 *bytes_read = 8;
10372 }
10373 else
10374 {
10375 *bytes_read = 4;
10376 }
10377
10378 return length;
10379 }
10380
10381 /* Cover function for read_initial_length.
10382 Returns the length of the object at BUF, and stores the size of the
10383 initial length in *BYTES_READ and stores the size that offsets will be in
10384 *OFFSET_SIZE.
10385 If the initial length size is not equivalent to that specified in
10386 CU_HEADER then issue a complaint.
10387 This is useful when reading non-comp-unit headers. */
10388
10389 static LONGEST
10390 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10391 const struct comp_unit_head *cu_header,
10392 unsigned int *bytes_read,
10393 unsigned int *offset_size)
10394 {
10395 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10396
10397 gdb_assert (cu_header->initial_length_size == 4
10398 || cu_header->initial_length_size == 8
10399 || cu_header->initial_length_size == 12);
10400
10401 if (cu_header->initial_length_size != *bytes_read)
10402 complaint (&symfile_complaints,
10403 _("intermixed 32-bit and 64-bit DWARF sections"));
10404
10405 *offset_size = (*bytes_read == 4) ? 4 : 8;
10406 return length;
10407 }
10408
10409 /* Read an offset from the data stream. The size of the offset is
10410 given by cu_header->offset_size. */
10411
10412 static LONGEST
10413 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10414 unsigned int *bytes_read)
10415 {
10416 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10417
10418 *bytes_read = cu_header->offset_size;
10419 return offset;
10420 }
10421
10422 /* Read an offset from the data stream. */
10423
10424 static LONGEST
10425 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10426 {
10427 LONGEST retval = 0;
10428
10429 switch (offset_size)
10430 {
10431 case 4:
10432 retval = bfd_get_32 (abfd, buf);
10433 break;
10434 case 8:
10435 retval = bfd_get_64 (abfd, buf);
10436 break;
10437 default:
10438 internal_error (__FILE__, __LINE__,
10439 _("read_offset_1: bad switch [in module %s]"),
10440 bfd_get_filename (abfd));
10441 }
10442
10443 return retval;
10444 }
10445
10446 static gdb_byte *
10447 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10448 {
10449 /* If the size of a host char is 8 bits, we can return a pointer
10450 to the buffer, otherwise we have to copy the data to a buffer
10451 allocated on the temporary obstack. */
10452 gdb_assert (HOST_CHAR_BIT == 8);
10453 return buf;
10454 }
10455
10456 static char *
10457 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10458 {
10459 /* If the size of a host char is 8 bits, we can return a pointer
10460 to the string, otherwise we have to copy the string to a buffer
10461 allocated on the temporary obstack. */
10462 gdb_assert (HOST_CHAR_BIT == 8);
10463 if (*buf == '\0')
10464 {
10465 *bytes_read_ptr = 1;
10466 return NULL;
10467 }
10468 *bytes_read_ptr = strlen ((char *) buf) + 1;
10469 return (char *) buf;
10470 }
10471
10472 static char *
10473 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10474 {
10475 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10476 if (dwarf2_per_objfile->str.buffer == NULL)
10477 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10478 bfd_get_filename (abfd));
10479 if (str_offset >= dwarf2_per_objfile->str.size)
10480 error (_("DW_FORM_strp pointing outside of "
10481 ".debug_str section [in module %s]"),
10482 bfd_get_filename (abfd));
10483 gdb_assert (HOST_CHAR_BIT == 8);
10484 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10485 return NULL;
10486 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10487 }
10488
10489 static char *
10490 read_indirect_string (bfd *abfd, gdb_byte *buf,
10491 const struct comp_unit_head *cu_header,
10492 unsigned int *bytes_read_ptr)
10493 {
10494 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10495
10496 return read_indirect_string_at_offset (abfd, str_offset);
10497 }
10498
10499 static unsigned long
10500 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10501 {
10502 unsigned long result;
10503 unsigned int num_read;
10504 int i, shift;
10505 unsigned char byte;
10506
10507 result = 0;
10508 shift = 0;
10509 num_read = 0;
10510 i = 0;
10511 while (1)
10512 {
10513 byte = bfd_get_8 (abfd, buf);
10514 buf++;
10515 num_read++;
10516 result |= ((unsigned long)(byte & 127) << shift);
10517 if ((byte & 128) == 0)
10518 {
10519 break;
10520 }
10521 shift += 7;
10522 }
10523 *bytes_read_ptr = num_read;
10524 return result;
10525 }
10526
10527 static long
10528 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10529 {
10530 long result;
10531 int i, shift, num_read;
10532 unsigned char byte;
10533
10534 result = 0;
10535 shift = 0;
10536 num_read = 0;
10537 i = 0;
10538 while (1)
10539 {
10540 byte = bfd_get_8 (abfd, buf);
10541 buf++;
10542 num_read++;
10543 result |= ((long)(byte & 127) << shift);
10544 shift += 7;
10545 if ((byte & 128) == 0)
10546 {
10547 break;
10548 }
10549 }
10550 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10551 result |= -(((long)1) << shift);
10552 *bytes_read_ptr = num_read;
10553 return result;
10554 }
10555
10556 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10557
10558 static gdb_byte *
10559 skip_leb128 (bfd *abfd, gdb_byte *buf)
10560 {
10561 int byte;
10562
10563 while (1)
10564 {
10565 byte = bfd_get_8 (abfd, buf);
10566 buf++;
10567 if ((byte & 128) == 0)
10568 return buf;
10569 }
10570 }
10571
10572 static void
10573 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10574 {
10575 switch (lang)
10576 {
10577 case DW_LANG_C89:
10578 case DW_LANG_C99:
10579 case DW_LANG_C:
10580 cu->language = language_c;
10581 break;
10582 case DW_LANG_C_plus_plus:
10583 cu->language = language_cplus;
10584 break;
10585 case DW_LANG_D:
10586 cu->language = language_d;
10587 break;
10588 case DW_LANG_Fortran77:
10589 case DW_LANG_Fortran90:
10590 case DW_LANG_Fortran95:
10591 cu->language = language_fortran;
10592 break;
10593 case DW_LANG_Mips_Assembler:
10594 cu->language = language_asm;
10595 break;
10596 case DW_LANG_Java:
10597 cu->language = language_java;
10598 break;
10599 case DW_LANG_Ada83:
10600 case DW_LANG_Ada95:
10601 cu->language = language_ada;
10602 break;
10603 case DW_LANG_Modula2:
10604 cu->language = language_m2;
10605 break;
10606 case DW_LANG_Pascal83:
10607 cu->language = language_pascal;
10608 break;
10609 case DW_LANG_ObjC:
10610 cu->language = language_objc;
10611 break;
10612 case DW_LANG_Cobol74:
10613 case DW_LANG_Cobol85:
10614 default:
10615 cu->language = language_minimal;
10616 break;
10617 }
10618 cu->language_defn = language_def (cu->language);
10619 }
10620
10621 /* Return the named attribute or NULL if not there. */
10622
10623 static struct attribute *
10624 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10625 {
10626 unsigned int i;
10627 struct attribute *spec = NULL;
10628
10629 for (i = 0; i < die->num_attrs; ++i)
10630 {
10631 if (die->attrs[i].name == name)
10632 return &die->attrs[i];
10633 if (die->attrs[i].name == DW_AT_specification
10634 || die->attrs[i].name == DW_AT_abstract_origin)
10635 spec = &die->attrs[i];
10636 }
10637
10638 if (spec)
10639 {
10640 die = follow_die_ref (die, spec, &cu);
10641 return dwarf2_attr (die, name, cu);
10642 }
10643
10644 return NULL;
10645 }
10646
10647 /* Return the named attribute or NULL if not there,
10648 but do not follow DW_AT_specification, etc.
10649 This is for use in contexts where we're reading .debug_types dies.
10650 Following DW_AT_specification, DW_AT_abstract_origin will take us
10651 back up the chain, and we want to go down. */
10652
10653 static struct attribute *
10654 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10655 struct dwarf2_cu *cu)
10656 {
10657 unsigned int i;
10658
10659 for (i = 0; i < die->num_attrs; ++i)
10660 if (die->attrs[i].name == name)
10661 return &die->attrs[i];
10662
10663 return NULL;
10664 }
10665
10666 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10667 and holds a non-zero value. This function should only be used for
10668 DW_FORM_flag or DW_FORM_flag_present attributes. */
10669
10670 static int
10671 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10672 {
10673 struct attribute *attr = dwarf2_attr (die, name, cu);
10674
10675 return (attr && DW_UNSND (attr));
10676 }
10677
10678 static int
10679 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10680 {
10681 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10682 which value is non-zero. However, we have to be careful with
10683 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10684 (via dwarf2_flag_true_p) follows this attribute. So we may
10685 end up accidently finding a declaration attribute that belongs
10686 to a different DIE referenced by the specification attribute,
10687 even though the given DIE does not have a declaration attribute. */
10688 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10689 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10690 }
10691
10692 /* Return the die giving the specification for DIE, if there is
10693 one. *SPEC_CU is the CU containing DIE on input, and the CU
10694 containing the return value on output. If there is no
10695 specification, but there is an abstract origin, that is
10696 returned. */
10697
10698 static struct die_info *
10699 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10700 {
10701 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10702 *spec_cu);
10703
10704 if (spec_attr == NULL)
10705 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10706
10707 if (spec_attr == NULL)
10708 return NULL;
10709 else
10710 return follow_die_ref (die, spec_attr, spec_cu);
10711 }
10712
10713 /* Free the line_header structure *LH, and any arrays and strings it
10714 refers to.
10715 NOTE: This is also used as a "cleanup" function. */
10716
10717 static void
10718 free_line_header (struct line_header *lh)
10719 {
10720 if (lh->standard_opcode_lengths)
10721 xfree (lh->standard_opcode_lengths);
10722
10723 /* Remember that all the lh->file_names[i].name pointers are
10724 pointers into debug_line_buffer, and don't need to be freed. */
10725 if (lh->file_names)
10726 xfree (lh->file_names);
10727
10728 /* Similarly for the include directory names. */
10729 if (lh->include_dirs)
10730 xfree (lh->include_dirs);
10731
10732 xfree (lh);
10733 }
10734
10735 /* Add an entry to LH's include directory table. */
10736
10737 static void
10738 add_include_dir (struct line_header *lh, char *include_dir)
10739 {
10740 /* Grow the array if necessary. */
10741 if (lh->include_dirs_size == 0)
10742 {
10743 lh->include_dirs_size = 1; /* for testing */
10744 lh->include_dirs = xmalloc (lh->include_dirs_size
10745 * sizeof (*lh->include_dirs));
10746 }
10747 else if (lh->num_include_dirs >= lh->include_dirs_size)
10748 {
10749 lh->include_dirs_size *= 2;
10750 lh->include_dirs = xrealloc (lh->include_dirs,
10751 (lh->include_dirs_size
10752 * sizeof (*lh->include_dirs)));
10753 }
10754
10755 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10756 }
10757
10758 /* Add an entry to LH's file name table. */
10759
10760 static void
10761 add_file_name (struct line_header *lh,
10762 char *name,
10763 unsigned int dir_index,
10764 unsigned int mod_time,
10765 unsigned int length)
10766 {
10767 struct file_entry *fe;
10768
10769 /* Grow the array if necessary. */
10770 if (lh->file_names_size == 0)
10771 {
10772 lh->file_names_size = 1; /* for testing */
10773 lh->file_names = xmalloc (lh->file_names_size
10774 * sizeof (*lh->file_names));
10775 }
10776 else if (lh->num_file_names >= lh->file_names_size)
10777 {
10778 lh->file_names_size *= 2;
10779 lh->file_names = xrealloc (lh->file_names,
10780 (lh->file_names_size
10781 * sizeof (*lh->file_names)));
10782 }
10783
10784 fe = &lh->file_names[lh->num_file_names++];
10785 fe->name = name;
10786 fe->dir_index = dir_index;
10787 fe->mod_time = mod_time;
10788 fe->length = length;
10789 fe->included_p = 0;
10790 fe->symtab = NULL;
10791 }
10792
10793 /* Read the statement program header starting at OFFSET in
10794 .debug_line, according to the endianness of ABFD. Return a pointer
10795 to a struct line_header, allocated using xmalloc.
10796
10797 NOTE: the strings in the include directory and file name tables of
10798 the returned object point into debug_line_buffer, and must not be
10799 freed. */
10800
10801 static struct line_header *
10802 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10803 struct dwarf2_cu *cu)
10804 {
10805 struct cleanup *back_to;
10806 struct line_header *lh;
10807 gdb_byte *line_ptr;
10808 unsigned int bytes_read, offset_size;
10809 int i;
10810 char *cur_dir, *cur_file;
10811
10812 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10813 if (dwarf2_per_objfile->line.buffer == NULL)
10814 {
10815 complaint (&symfile_complaints, _("missing .debug_line section"));
10816 return 0;
10817 }
10818
10819 /* Make sure that at least there's room for the total_length field.
10820 That could be 12 bytes long, but we're just going to fudge that. */
10821 if (offset + 4 >= dwarf2_per_objfile->line.size)
10822 {
10823 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10824 return 0;
10825 }
10826
10827 lh = xmalloc (sizeof (*lh));
10828 memset (lh, 0, sizeof (*lh));
10829 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10830 (void *) lh);
10831
10832 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10833
10834 /* Read in the header. */
10835 lh->total_length =
10836 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10837 &bytes_read, &offset_size);
10838 line_ptr += bytes_read;
10839 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10840 + dwarf2_per_objfile->line.size))
10841 {
10842 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10843 return 0;
10844 }
10845 lh->statement_program_end = line_ptr + lh->total_length;
10846 lh->version = read_2_bytes (abfd, line_ptr);
10847 line_ptr += 2;
10848 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10849 line_ptr += offset_size;
10850 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10851 line_ptr += 1;
10852 if (lh->version >= 4)
10853 {
10854 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10855 line_ptr += 1;
10856 }
10857 else
10858 lh->maximum_ops_per_instruction = 1;
10859
10860 if (lh->maximum_ops_per_instruction == 0)
10861 {
10862 lh->maximum_ops_per_instruction = 1;
10863 complaint (&symfile_complaints,
10864 _("invalid maximum_ops_per_instruction "
10865 "in `.debug_line' section"));
10866 }
10867
10868 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10869 line_ptr += 1;
10870 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10871 line_ptr += 1;
10872 lh->line_range = read_1_byte (abfd, line_ptr);
10873 line_ptr += 1;
10874 lh->opcode_base = read_1_byte (abfd, line_ptr);
10875 line_ptr += 1;
10876 lh->standard_opcode_lengths
10877 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10878
10879 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10880 for (i = 1; i < lh->opcode_base; ++i)
10881 {
10882 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10883 line_ptr += 1;
10884 }
10885
10886 /* Read directory table. */
10887 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10888 {
10889 line_ptr += bytes_read;
10890 add_include_dir (lh, cur_dir);
10891 }
10892 line_ptr += bytes_read;
10893
10894 /* Read file name table. */
10895 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10896 {
10897 unsigned int dir_index, mod_time, length;
10898
10899 line_ptr += bytes_read;
10900 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10901 line_ptr += bytes_read;
10902 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10903 line_ptr += bytes_read;
10904 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10905 line_ptr += bytes_read;
10906
10907 add_file_name (lh, cur_file, dir_index, mod_time, length);
10908 }
10909 line_ptr += bytes_read;
10910 lh->statement_program_start = line_ptr;
10911
10912 if (line_ptr > (dwarf2_per_objfile->line.buffer
10913 + dwarf2_per_objfile->line.size))
10914 complaint (&symfile_complaints,
10915 _("line number info header doesn't "
10916 "fit in `.debug_line' section"));
10917
10918 discard_cleanups (back_to);
10919 return lh;
10920 }
10921
10922 /* This function exists to work around a bug in certain compilers
10923 (particularly GCC 2.95), in which the first line number marker of a
10924 function does not show up until after the prologue, right before
10925 the second line number marker. This function shifts ADDRESS down
10926 to the beginning of the function if necessary, and is called on
10927 addresses passed to record_line. */
10928
10929 static CORE_ADDR
10930 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10931 {
10932 struct function_range *fn;
10933
10934 /* Find the function_range containing address. */
10935 if (!cu->first_fn)
10936 return address;
10937
10938 if (!cu->cached_fn)
10939 cu->cached_fn = cu->first_fn;
10940
10941 fn = cu->cached_fn;
10942 while (fn)
10943 if (fn->lowpc <= address && fn->highpc > address)
10944 goto found;
10945 else
10946 fn = fn->next;
10947
10948 fn = cu->first_fn;
10949 while (fn && fn != cu->cached_fn)
10950 if (fn->lowpc <= address && fn->highpc > address)
10951 goto found;
10952 else
10953 fn = fn->next;
10954
10955 return address;
10956
10957 found:
10958 if (fn->seen_line)
10959 return address;
10960 if (address != fn->lowpc)
10961 complaint (&symfile_complaints,
10962 _("misplaced first line number at 0x%lx for '%s'"),
10963 (unsigned long) address, fn->name);
10964 fn->seen_line = 1;
10965 return fn->lowpc;
10966 }
10967
10968 /* Subroutine of dwarf_decode_lines to simplify it.
10969 Return the file name of the psymtab for included file FILE_INDEX
10970 in line header LH of PST.
10971 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10972 If space for the result is malloc'd, it will be freed by a cleanup.
10973 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10974
10975 static char *
10976 psymtab_include_file_name (const struct line_header *lh, int file_index,
10977 const struct partial_symtab *pst,
10978 const char *comp_dir)
10979 {
10980 const struct file_entry fe = lh->file_names [file_index];
10981 char *include_name = fe.name;
10982 char *include_name_to_compare = include_name;
10983 char *dir_name = NULL;
10984 const char *pst_filename;
10985 char *copied_name = NULL;
10986 int file_is_pst;
10987
10988 if (fe.dir_index)
10989 dir_name = lh->include_dirs[fe.dir_index - 1];
10990
10991 if (!IS_ABSOLUTE_PATH (include_name)
10992 && (dir_name != NULL || comp_dir != NULL))
10993 {
10994 /* Avoid creating a duplicate psymtab for PST.
10995 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10996 Before we do the comparison, however, we need to account
10997 for DIR_NAME and COMP_DIR.
10998 First prepend dir_name (if non-NULL). If we still don't
10999 have an absolute path prepend comp_dir (if non-NULL).
11000 However, the directory we record in the include-file's
11001 psymtab does not contain COMP_DIR (to match the
11002 corresponding symtab(s)).
11003
11004 Example:
11005
11006 bash$ cd /tmp
11007 bash$ gcc -g ./hello.c
11008 include_name = "hello.c"
11009 dir_name = "."
11010 DW_AT_comp_dir = comp_dir = "/tmp"
11011 DW_AT_name = "./hello.c" */
11012
11013 if (dir_name != NULL)
11014 {
11015 include_name = concat (dir_name, SLASH_STRING,
11016 include_name, (char *)NULL);
11017 include_name_to_compare = include_name;
11018 make_cleanup (xfree, include_name);
11019 }
11020 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11021 {
11022 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11023 include_name, (char *)NULL);
11024 }
11025 }
11026
11027 pst_filename = pst->filename;
11028 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11029 {
11030 copied_name = concat (pst->dirname, SLASH_STRING,
11031 pst_filename, (char *)NULL);
11032 pst_filename = copied_name;
11033 }
11034
11035 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11036
11037 if (include_name_to_compare != include_name)
11038 xfree (include_name_to_compare);
11039 if (copied_name != NULL)
11040 xfree (copied_name);
11041
11042 if (file_is_pst)
11043 return NULL;
11044 return include_name;
11045 }
11046
11047 /* Ignore this record_line request. */
11048
11049 static void
11050 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11051 {
11052 return;
11053 }
11054
11055 /* Decode the Line Number Program (LNP) for the given line_header
11056 structure and CU. The actual information extracted and the type
11057 of structures created from the LNP depends on the value of PST.
11058
11059 1. If PST is NULL, then this procedure uses the data from the program
11060 to create all necessary symbol tables, and their linetables.
11061
11062 2. If PST is not NULL, this procedure reads the program to determine
11063 the list of files included by the unit represented by PST, and
11064 builds all the associated partial symbol tables.
11065
11066 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11067 It is used for relative paths in the line table.
11068 NOTE: When processing partial symtabs (pst != NULL),
11069 comp_dir == pst->dirname.
11070
11071 NOTE: It is important that psymtabs have the same file name (via strcmp)
11072 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11073 symtab we don't use it in the name of the psymtabs we create.
11074 E.g. expand_line_sal requires this when finding psymtabs to expand.
11075 A good testcase for this is mb-inline.exp. */
11076
11077 static void
11078 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11079 struct dwarf2_cu *cu, struct partial_symtab *pst)
11080 {
11081 gdb_byte *line_ptr, *extended_end;
11082 gdb_byte *line_end;
11083 unsigned int bytes_read, extended_len;
11084 unsigned char op_code, extended_op, adj_opcode;
11085 CORE_ADDR baseaddr;
11086 struct objfile *objfile = cu->objfile;
11087 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11088 const int decode_for_pst_p = (pst != NULL);
11089 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11090 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11091 = record_line;
11092
11093 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11094
11095 line_ptr = lh->statement_program_start;
11096 line_end = lh->statement_program_end;
11097
11098 /* Read the statement sequences until there's nothing left. */
11099 while (line_ptr < line_end)
11100 {
11101 /* state machine registers */
11102 CORE_ADDR address = 0;
11103 unsigned int file = 1;
11104 unsigned int line = 1;
11105 unsigned int column = 0;
11106 int is_stmt = lh->default_is_stmt;
11107 int basic_block = 0;
11108 int end_sequence = 0;
11109 CORE_ADDR addr;
11110 unsigned char op_index = 0;
11111
11112 if (!decode_for_pst_p && lh->num_file_names >= file)
11113 {
11114 /* Start a subfile for the current file of the state machine. */
11115 /* lh->include_dirs and lh->file_names are 0-based, but the
11116 directory and file name numbers in the statement program
11117 are 1-based. */
11118 struct file_entry *fe = &lh->file_names[file - 1];
11119 char *dir = NULL;
11120
11121 if (fe->dir_index)
11122 dir = lh->include_dirs[fe->dir_index - 1];
11123
11124 dwarf2_start_subfile (fe->name, dir, comp_dir);
11125 }
11126
11127 /* Decode the table. */
11128 while (!end_sequence)
11129 {
11130 op_code = read_1_byte (abfd, line_ptr);
11131 line_ptr += 1;
11132 if (line_ptr > line_end)
11133 {
11134 dwarf2_debug_line_missing_end_sequence_complaint ();
11135 break;
11136 }
11137
11138 if (op_code >= lh->opcode_base)
11139 {
11140 /* Special operand. */
11141 adj_opcode = op_code - lh->opcode_base;
11142 address += (((op_index + (adj_opcode / lh->line_range))
11143 / lh->maximum_ops_per_instruction)
11144 * lh->minimum_instruction_length);
11145 op_index = ((op_index + (adj_opcode / lh->line_range))
11146 % lh->maximum_ops_per_instruction);
11147 line += lh->line_base + (adj_opcode % lh->line_range);
11148 if (lh->num_file_names < file || file == 0)
11149 dwarf2_debug_line_missing_file_complaint ();
11150 /* For now we ignore lines not starting on an
11151 instruction boundary. */
11152 else if (op_index == 0)
11153 {
11154 lh->file_names[file - 1].included_p = 1;
11155 if (!decode_for_pst_p && is_stmt)
11156 {
11157 if (last_subfile != current_subfile)
11158 {
11159 addr = gdbarch_addr_bits_remove (gdbarch, address);
11160 if (last_subfile)
11161 (*p_record_line) (last_subfile, 0, addr);
11162 last_subfile = current_subfile;
11163 }
11164 /* Append row to matrix using current values. */
11165 addr = check_cu_functions (address, cu);
11166 addr = gdbarch_addr_bits_remove (gdbarch, addr);
11167 (*p_record_line) (current_subfile, line, addr);
11168 }
11169 }
11170 basic_block = 0;
11171 }
11172 else switch (op_code)
11173 {
11174 case DW_LNS_extended_op:
11175 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11176 &bytes_read);
11177 line_ptr += bytes_read;
11178 extended_end = line_ptr + extended_len;
11179 extended_op = read_1_byte (abfd, line_ptr);
11180 line_ptr += 1;
11181 switch (extended_op)
11182 {
11183 case DW_LNE_end_sequence:
11184 p_record_line = record_line;
11185 end_sequence = 1;
11186 break;
11187 case DW_LNE_set_address:
11188 address = read_address (abfd, line_ptr, cu, &bytes_read);
11189
11190 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11191 {
11192 /* This line table is for a function which has been
11193 GCd by the linker. Ignore it. PR gdb/12528 */
11194
11195 long line_offset
11196 = line_ptr - dwarf2_per_objfile->line.buffer;
11197
11198 complaint (&symfile_complaints,
11199 _(".debug_line address at offset 0x%lx is 0 "
11200 "[in module %s]"),
11201 line_offset, cu->objfile->name);
11202 p_record_line = noop_record_line;
11203 }
11204
11205 op_index = 0;
11206 line_ptr += bytes_read;
11207 address += baseaddr;
11208 break;
11209 case DW_LNE_define_file:
11210 {
11211 char *cur_file;
11212 unsigned int dir_index, mod_time, length;
11213
11214 cur_file = read_direct_string (abfd, line_ptr,
11215 &bytes_read);
11216 line_ptr += bytes_read;
11217 dir_index =
11218 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11219 line_ptr += bytes_read;
11220 mod_time =
11221 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11222 line_ptr += bytes_read;
11223 length =
11224 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11225 line_ptr += bytes_read;
11226 add_file_name (lh, cur_file, dir_index, mod_time, length);
11227 }
11228 break;
11229 case DW_LNE_set_discriminator:
11230 /* The discriminator is not interesting to the debugger;
11231 just ignore it. */
11232 line_ptr = extended_end;
11233 break;
11234 default:
11235 complaint (&symfile_complaints,
11236 _("mangled .debug_line section"));
11237 return;
11238 }
11239 /* Make sure that we parsed the extended op correctly. If e.g.
11240 we expected a different address size than the producer used,
11241 we may have read the wrong number of bytes. */
11242 if (line_ptr != extended_end)
11243 {
11244 complaint (&symfile_complaints,
11245 _("mangled .debug_line section"));
11246 return;
11247 }
11248 break;
11249 case DW_LNS_copy:
11250 if (lh->num_file_names < file || file == 0)
11251 dwarf2_debug_line_missing_file_complaint ();
11252 else
11253 {
11254 lh->file_names[file - 1].included_p = 1;
11255 if (!decode_for_pst_p && is_stmt)
11256 {
11257 if (last_subfile != current_subfile)
11258 {
11259 addr = gdbarch_addr_bits_remove (gdbarch, address);
11260 if (last_subfile)
11261 (*p_record_line) (last_subfile, 0, addr);
11262 last_subfile = current_subfile;
11263 }
11264 addr = check_cu_functions (address, cu);
11265 addr = gdbarch_addr_bits_remove (gdbarch, addr);
11266 (*p_record_line) (current_subfile, line, addr);
11267 }
11268 }
11269 basic_block = 0;
11270 break;
11271 case DW_LNS_advance_pc:
11272 {
11273 CORE_ADDR adjust
11274 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11275
11276 address += (((op_index + adjust)
11277 / lh->maximum_ops_per_instruction)
11278 * lh->minimum_instruction_length);
11279 op_index = ((op_index + adjust)
11280 % lh->maximum_ops_per_instruction);
11281 line_ptr += bytes_read;
11282 }
11283 break;
11284 case DW_LNS_advance_line:
11285 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11286 line_ptr += bytes_read;
11287 break;
11288 case DW_LNS_set_file:
11289 {
11290 /* The arrays lh->include_dirs and lh->file_names are
11291 0-based, but the directory and file name numbers in
11292 the statement program are 1-based. */
11293 struct file_entry *fe;
11294 char *dir = NULL;
11295
11296 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11297 line_ptr += bytes_read;
11298 if (lh->num_file_names < file || file == 0)
11299 dwarf2_debug_line_missing_file_complaint ();
11300 else
11301 {
11302 fe = &lh->file_names[file - 1];
11303 if (fe->dir_index)
11304 dir = lh->include_dirs[fe->dir_index - 1];
11305 if (!decode_for_pst_p)
11306 {
11307 last_subfile = current_subfile;
11308 dwarf2_start_subfile (fe->name, dir, comp_dir);
11309 }
11310 }
11311 }
11312 break;
11313 case DW_LNS_set_column:
11314 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11315 line_ptr += bytes_read;
11316 break;
11317 case DW_LNS_negate_stmt:
11318 is_stmt = (!is_stmt);
11319 break;
11320 case DW_LNS_set_basic_block:
11321 basic_block = 1;
11322 break;
11323 /* Add to the address register of the state machine the
11324 address increment value corresponding to special opcode
11325 255. I.e., this value is scaled by the minimum
11326 instruction length since special opcode 255 would have
11327 scaled the increment. */
11328 case DW_LNS_const_add_pc:
11329 {
11330 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11331
11332 address += (((op_index + adjust)
11333 / lh->maximum_ops_per_instruction)
11334 * lh->minimum_instruction_length);
11335 op_index = ((op_index + adjust)
11336 % lh->maximum_ops_per_instruction);
11337 }
11338 break;
11339 case DW_LNS_fixed_advance_pc:
11340 address += read_2_bytes (abfd, line_ptr);
11341 op_index = 0;
11342 line_ptr += 2;
11343 break;
11344 default:
11345 {
11346 /* Unknown standard opcode, ignore it. */
11347 int i;
11348
11349 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11350 {
11351 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11352 line_ptr += bytes_read;
11353 }
11354 }
11355 }
11356 }
11357 if (lh->num_file_names < file || file == 0)
11358 dwarf2_debug_line_missing_file_complaint ();
11359 else
11360 {
11361 lh->file_names[file - 1].included_p = 1;
11362 if (!decode_for_pst_p)
11363 {
11364 addr = gdbarch_addr_bits_remove (gdbarch, address);
11365 (*p_record_line) (current_subfile, 0, addr);
11366 }
11367 }
11368 }
11369
11370 if (decode_for_pst_p)
11371 {
11372 int file_index;
11373
11374 /* Now that we're done scanning the Line Header Program, we can
11375 create the psymtab of each included file. */
11376 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11377 if (lh->file_names[file_index].included_p == 1)
11378 {
11379 char *include_name =
11380 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11381 if (include_name != NULL)
11382 dwarf2_create_include_psymtab (include_name, pst, objfile);
11383 }
11384 }
11385 else
11386 {
11387 /* Make sure a symtab is created for every file, even files
11388 which contain only variables (i.e. no code with associated
11389 line numbers). */
11390
11391 int i;
11392 struct file_entry *fe;
11393
11394 for (i = 0; i < lh->num_file_names; i++)
11395 {
11396 char *dir = NULL;
11397
11398 fe = &lh->file_names[i];
11399 if (fe->dir_index)
11400 dir = lh->include_dirs[fe->dir_index - 1];
11401 dwarf2_start_subfile (fe->name, dir, comp_dir);
11402
11403 /* Skip the main file; we don't need it, and it must be
11404 allocated last, so that it will show up before the
11405 non-primary symtabs in the objfile's symtab list. */
11406 if (current_subfile == first_subfile)
11407 continue;
11408
11409 if (current_subfile->symtab == NULL)
11410 current_subfile->symtab = allocate_symtab (current_subfile->name,
11411 cu->objfile);
11412 fe->symtab = current_subfile->symtab;
11413 }
11414 }
11415 }
11416
11417 /* Start a subfile for DWARF. FILENAME is the name of the file and
11418 DIRNAME the name of the source directory which contains FILENAME
11419 or NULL if not known. COMP_DIR is the compilation directory for the
11420 linetable's compilation unit or NULL if not known.
11421 This routine tries to keep line numbers from identical absolute and
11422 relative file names in a common subfile.
11423
11424 Using the `list' example from the GDB testsuite, which resides in
11425 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11426 of /srcdir/list0.c yields the following debugging information for list0.c:
11427
11428 DW_AT_name: /srcdir/list0.c
11429 DW_AT_comp_dir: /compdir
11430 files.files[0].name: list0.h
11431 files.files[0].dir: /srcdir
11432 files.files[1].name: list0.c
11433 files.files[1].dir: /srcdir
11434
11435 The line number information for list0.c has to end up in a single
11436 subfile, so that `break /srcdir/list0.c:1' works as expected.
11437 start_subfile will ensure that this happens provided that we pass the
11438 concatenation of files.files[1].dir and files.files[1].name as the
11439 subfile's name. */
11440
11441 static void
11442 dwarf2_start_subfile (char *filename, const char *dirname,
11443 const char *comp_dir)
11444 {
11445 char *fullname;
11446
11447 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11448 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11449 second argument to start_subfile. To be consistent, we do the
11450 same here. In order not to lose the line information directory,
11451 we concatenate it to the filename when it makes sense.
11452 Note that the Dwarf3 standard says (speaking of filenames in line
11453 information): ``The directory index is ignored for file names
11454 that represent full path names''. Thus ignoring dirname in the
11455 `else' branch below isn't an issue. */
11456
11457 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11458 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11459 else
11460 fullname = filename;
11461
11462 start_subfile (fullname, comp_dir);
11463
11464 if (fullname != filename)
11465 xfree (fullname);
11466 }
11467
11468 static void
11469 var_decode_location (struct attribute *attr, struct symbol *sym,
11470 struct dwarf2_cu *cu)
11471 {
11472 struct objfile *objfile = cu->objfile;
11473 struct comp_unit_head *cu_header = &cu->header;
11474
11475 /* NOTE drow/2003-01-30: There used to be a comment and some special
11476 code here to turn a symbol with DW_AT_external and a
11477 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11478 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11479 with some versions of binutils) where shared libraries could have
11480 relocations against symbols in their debug information - the
11481 minimal symbol would have the right address, but the debug info
11482 would not. It's no longer necessary, because we will explicitly
11483 apply relocations when we read in the debug information now. */
11484
11485 /* A DW_AT_location attribute with no contents indicates that a
11486 variable has been optimized away. */
11487 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11488 {
11489 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11490 return;
11491 }
11492
11493 /* Handle one degenerate form of location expression specially, to
11494 preserve GDB's previous behavior when section offsets are
11495 specified. If this is just a DW_OP_addr then mark this symbol
11496 as LOC_STATIC. */
11497
11498 if (attr_form_is_block (attr)
11499 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11500 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11501 {
11502 unsigned int dummy;
11503
11504 SYMBOL_VALUE_ADDRESS (sym) =
11505 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11506 SYMBOL_CLASS (sym) = LOC_STATIC;
11507 fixup_symbol_section (sym, objfile);
11508 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11509 SYMBOL_SECTION (sym));
11510 return;
11511 }
11512
11513 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11514 expression evaluator, and use LOC_COMPUTED only when necessary
11515 (i.e. when the value of a register or memory location is
11516 referenced, or a thread-local block, etc.). Then again, it might
11517 not be worthwhile. I'm assuming that it isn't unless performance
11518 or memory numbers show me otherwise. */
11519
11520 dwarf2_symbol_mark_computed (attr, sym, cu);
11521 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11522
11523 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11524 cu->has_loclist = 1;
11525 }
11526
11527 /* Given a pointer to a DWARF information entry, figure out if we need
11528 to make a symbol table entry for it, and if so, create a new entry
11529 and return a pointer to it.
11530 If TYPE is NULL, determine symbol type from the die, otherwise
11531 used the passed type.
11532 If SPACE is not NULL, use it to hold the new symbol. If it is
11533 NULL, allocate a new symbol on the objfile's obstack. */
11534
11535 static struct symbol *
11536 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11537 struct symbol *space)
11538 {
11539 struct objfile *objfile = cu->objfile;
11540 struct symbol *sym = NULL;
11541 char *name;
11542 struct attribute *attr = NULL;
11543 struct attribute *attr2 = NULL;
11544 CORE_ADDR baseaddr;
11545 struct pending **list_to_add = NULL;
11546
11547 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11548
11549 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11550
11551 name = dwarf2_name (die, cu);
11552 if (name)
11553 {
11554 const char *linkagename;
11555 int suppress_add = 0;
11556
11557 if (space)
11558 sym = space;
11559 else
11560 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11561 OBJSTAT (objfile, n_syms++);
11562
11563 /* Cache this symbol's name and the name's demangled form (if any). */
11564 SYMBOL_SET_LANGUAGE (sym, cu->language);
11565 linkagename = dwarf2_physname (name, die, cu);
11566 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11567
11568 /* Fortran does not have mangling standard and the mangling does differ
11569 between gfortran, iFort etc. */
11570 if (cu->language == language_fortran
11571 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11572 symbol_set_demangled_name (&(sym->ginfo),
11573 (char *) dwarf2_full_name (name, die, cu),
11574 NULL);
11575
11576 /* Default assumptions.
11577 Use the passed type or decode it from the die. */
11578 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11579 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11580 if (type != NULL)
11581 SYMBOL_TYPE (sym) = type;
11582 else
11583 SYMBOL_TYPE (sym) = die_type (die, cu);
11584 attr = dwarf2_attr (die,
11585 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11586 cu);
11587 if (attr)
11588 {
11589 SYMBOL_LINE (sym) = DW_UNSND (attr);
11590 }
11591
11592 attr = dwarf2_attr (die,
11593 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11594 cu);
11595 if (attr)
11596 {
11597 int file_index = DW_UNSND (attr);
11598
11599 if (cu->line_header == NULL
11600 || file_index > cu->line_header->num_file_names)
11601 complaint (&symfile_complaints,
11602 _("file index out of range"));
11603 else if (file_index > 0)
11604 {
11605 struct file_entry *fe;
11606
11607 fe = &cu->line_header->file_names[file_index - 1];
11608 SYMBOL_SYMTAB (sym) = fe->symtab;
11609 }
11610 }
11611
11612 switch (die->tag)
11613 {
11614 case DW_TAG_label:
11615 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11616 if (attr)
11617 {
11618 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11619 }
11620 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11621 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11622 SYMBOL_CLASS (sym) = LOC_LABEL;
11623 add_symbol_to_list (sym, cu->list_in_scope);
11624 break;
11625 case DW_TAG_subprogram:
11626 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11627 finish_block. */
11628 SYMBOL_CLASS (sym) = LOC_BLOCK;
11629 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11630 if ((attr2 && (DW_UNSND (attr2) != 0))
11631 || cu->language == language_ada)
11632 {
11633 /* Subprograms marked external are stored as a global symbol.
11634 Ada subprograms, whether marked external or not, are always
11635 stored as a global symbol, because we want to be able to
11636 access them globally. For instance, we want to be able
11637 to break on a nested subprogram without having to
11638 specify the context. */
11639 list_to_add = &global_symbols;
11640 }
11641 else
11642 {
11643 list_to_add = cu->list_in_scope;
11644 }
11645 break;
11646 case DW_TAG_inlined_subroutine:
11647 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11648 finish_block. */
11649 SYMBOL_CLASS (sym) = LOC_BLOCK;
11650 SYMBOL_INLINED (sym) = 1;
11651 /* Do not add the symbol to any lists. It will be found via
11652 BLOCK_FUNCTION from the blockvector. */
11653 break;
11654 case DW_TAG_template_value_param:
11655 suppress_add = 1;
11656 /* Fall through. */
11657 case DW_TAG_constant:
11658 case DW_TAG_variable:
11659 case DW_TAG_member:
11660 /* Compilation with minimal debug info may result in
11661 variables with missing type entries. Change the
11662 misleading `void' type to something sensible. */
11663 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11664 SYMBOL_TYPE (sym)
11665 = objfile_type (objfile)->nodebug_data_symbol;
11666
11667 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11668 /* In the case of DW_TAG_member, we should only be called for
11669 static const members. */
11670 if (die->tag == DW_TAG_member)
11671 {
11672 /* dwarf2_add_field uses die_is_declaration,
11673 so we do the same. */
11674 gdb_assert (die_is_declaration (die, cu));
11675 gdb_assert (attr);
11676 }
11677 if (attr)
11678 {
11679 dwarf2_const_value (attr, sym, cu);
11680 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11681 if (!suppress_add)
11682 {
11683 if (attr2 && (DW_UNSND (attr2) != 0))
11684 list_to_add = &global_symbols;
11685 else
11686 list_to_add = cu->list_in_scope;
11687 }
11688 break;
11689 }
11690 attr = dwarf2_attr (die, DW_AT_location, cu);
11691 if (attr)
11692 {
11693 var_decode_location (attr, sym, cu);
11694 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11695 if (SYMBOL_CLASS (sym) == LOC_STATIC
11696 && SYMBOL_VALUE_ADDRESS (sym) == 0
11697 && !dwarf2_per_objfile->has_section_at_zero)
11698 {
11699 /* When a static variable is eliminated by the linker,
11700 the corresponding debug information is not stripped
11701 out, but the variable address is set to null;
11702 do not add such variables into symbol table. */
11703 }
11704 else if (attr2 && (DW_UNSND (attr2) != 0))
11705 {
11706 /* Workaround gfortran PR debug/40040 - it uses
11707 DW_AT_location for variables in -fPIC libraries which may
11708 get overriden by other libraries/executable and get
11709 a different address. Resolve it by the minimal symbol
11710 which may come from inferior's executable using copy
11711 relocation. Make this workaround only for gfortran as for
11712 other compilers GDB cannot guess the minimal symbol
11713 Fortran mangling kind. */
11714 if (cu->language == language_fortran && die->parent
11715 && die->parent->tag == DW_TAG_module
11716 && cu->producer
11717 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11718 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11719
11720 /* A variable with DW_AT_external is never static,
11721 but it may be block-scoped. */
11722 list_to_add = (cu->list_in_scope == &file_symbols
11723 ? &global_symbols : cu->list_in_scope);
11724 }
11725 else
11726 list_to_add = cu->list_in_scope;
11727 }
11728 else
11729 {
11730 /* We do not know the address of this symbol.
11731 If it is an external symbol and we have type information
11732 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11733 The address of the variable will then be determined from
11734 the minimal symbol table whenever the variable is
11735 referenced. */
11736 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11737 if (attr2 && (DW_UNSND (attr2) != 0)
11738 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11739 {
11740 /* A variable with DW_AT_external is never static, but it
11741 may be block-scoped. */
11742 list_to_add = (cu->list_in_scope == &file_symbols
11743 ? &global_symbols : cu->list_in_scope);
11744
11745 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11746 }
11747 else if (!die_is_declaration (die, cu))
11748 {
11749 /* Use the default LOC_OPTIMIZED_OUT class. */
11750 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11751 if (!suppress_add)
11752 list_to_add = cu->list_in_scope;
11753 }
11754 }
11755 break;
11756 case DW_TAG_formal_parameter:
11757 /* If we are inside a function, mark this as an argument. If
11758 not, we might be looking at an argument to an inlined function
11759 when we do not have enough information to show inlined frames;
11760 pretend it's a local variable in that case so that the user can
11761 still see it. */
11762 if (context_stack_depth > 0
11763 && context_stack[context_stack_depth - 1].name != NULL)
11764 SYMBOL_IS_ARGUMENT (sym) = 1;
11765 attr = dwarf2_attr (die, DW_AT_location, cu);
11766 if (attr)
11767 {
11768 var_decode_location (attr, sym, cu);
11769 }
11770 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11771 if (attr)
11772 {
11773 dwarf2_const_value (attr, sym, cu);
11774 }
11775
11776 list_to_add = cu->list_in_scope;
11777 break;
11778 case DW_TAG_unspecified_parameters:
11779 /* From varargs functions; gdb doesn't seem to have any
11780 interest in this information, so just ignore it for now.
11781 (FIXME?) */
11782 break;
11783 case DW_TAG_template_type_param:
11784 suppress_add = 1;
11785 /* Fall through. */
11786 case DW_TAG_class_type:
11787 case DW_TAG_interface_type:
11788 case DW_TAG_structure_type:
11789 case DW_TAG_union_type:
11790 case DW_TAG_set_type:
11791 case DW_TAG_enumeration_type:
11792 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11793 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11794
11795 {
11796 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11797 really ever be static objects: otherwise, if you try
11798 to, say, break of a class's method and you're in a file
11799 which doesn't mention that class, it won't work unless
11800 the check for all static symbols in lookup_symbol_aux
11801 saves you. See the OtherFileClass tests in
11802 gdb.c++/namespace.exp. */
11803
11804 if (!suppress_add)
11805 {
11806 list_to_add = (cu->list_in_scope == &file_symbols
11807 && (cu->language == language_cplus
11808 || cu->language == language_java)
11809 ? &global_symbols : cu->list_in_scope);
11810
11811 /* The semantics of C++ state that "struct foo {
11812 ... }" also defines a typedef for "foo". A Java
11813 class declaration also defines a typedef for the
11814 class. */
11815 if (cu->language == language_cplus
11816 || cu->language == language_java
11817 || cu->language == language_ada)
11818 {
11819 /* The symbol's name is already allocated along
11820 with this objfile, so we don't need to
11821 duplicate it for the type. */
11822 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11823 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11824 }
11825 }
11826 }
11827 break;
11828 case DW_TAG_typedef:
11829 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11830 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11831 list_to_add = cu->list_in_scope;
11832 break;
11833 case DW_TAG_base_type:
11834 case DW_TAG_subrange_type:
11835 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11836 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11837 list_to_add = cu->list_in_scope;
11838 break;
11839 case DW_TAG_enumerator:
11840 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11841 if (attr)
11842 {
11843 dwarf2_const_value (attr, sym, cu);
11844 }
11845 {
11846 /* NOTE: carlton/2003-11-10: See comment above in the
11847 DW_TAG_class_type, etc. block. */
11848
11849 list_to_add = (cu->list_in_scope == &file_symbols
11850 && (cu->language == language_cplus
11851 || cu->language == language_java)
11852 ? &global_symbols : cu->list_in_scope);
11853 }
11854 break;
11855 case DW_TAG_namespace:
11856 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11857 list_to_add = &global_symbols;
11858 break;
11859 default:
11860 /* Not a tag we recognize. Hopefully we aren't processing
11861 trash data, but since we must specifically ignore things
11862 we don't recognize, there is nothing else we should do at
11863 this point. */
11864 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11865 dwarf_tag_name (die->tag));
11866 break;
11867 }
11868
11869 if (suppress_add)
11870 {
11871 sym->hash_next = objfile->template_symbols;
11872 objfile->template_symbols = sym;
11873 list_to_add = NULL;
11874 }
11875
11876 if (list_to_add != NULL)
11877 add_symbol_to_list (sym, list_to_add);
11878
11879 /* For the benefit of old versions of GCC, check for anonymous
11880 namespaces based on the demangled name. */
11881 if (!processing_has_namespace_info
11882 && cu->language == language_cplus)
11883 cp_scan_for_anonymous_namespaces (sym);
11884 }
11885 return (sym);
11886 }
11887
11888 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11889
11890 static struct symbol *
11891 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11892 {
11893 return new_symbol_full (die, type, cu, NULL);
11894 }
11895
11896 /* Given an attr with a DW_FORM_dataN value in host byte order,
11897 zero-extend it as appropriate for the symbol's type. The DWARF
11898 standard (v4) is not entirely clear about the meaning of using
11899 DW_FORM_dataN for a constant with a signed type, where the type is
11900 wider than the data. The conclusion of a discussion on the DWARF
11901 list was that this is unspecified. We choose to always zero-extend
11902 because that is the interpretation long in use by GCC. */
11903
11904 static gdb_byte *
11905 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11906 const char *name, struct obstack *obstack,
11907 struct dwarf2_cu *cu, long *value, int bits)
11908 {
11909 struct objfile *objfile = cu->objfile;
11910 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11911 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11912 LONGEST l = DW_UNSND (attr);
11913
11914 if (bits < sizeof (*value) * 8)
11915 {
11916 l &= ((LONGEST) 1 << bits) - 1;
11917 *value = l;
11918 }
11919 else if (bits == sizeof (*value) * 8)
11920 *value = l;
11921 else
11922 {
11923 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11924 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11925 return bytes;
11926 }
11927
11928 return NULL;
11929 }
11930
11931 /* Read a constant value from an attribute. Either set *VALUE, or if
11932 the value does not fit in *VALUE, set *BYTES - either already
11933 allocated on the objfile obstack, or newly allocated on OBSTACK,
11934 or, set *BATON, if we translated the constant to a location
11935 expression. */
11936
11937 static void
11938 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11939 const char *name, struct obstack *obstack,
11940 struct dwarf2_cu *cu,
11941 long *value, gdb_byte **bytes,
11942 struct dwarf2_locexpr_baton **baton)
11943 {
11944 struct objfile *objfile = cu->objfile;
11945 struct comp_unit_head *cu_header = &cu->header;
11946 struct dwarf_block *blk;
11947 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11948 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11949
11950 *value = 0;
11951 *bytes = NULL;
11952 *baton = NULL;
11953
11954 switch (attr->form)
11955 {
11956 case DW_FORM_addr:
11957 {
11958 gdb_byte *data;
11959
11960 if (TYPE_LENGTH (type) != cu_header->addr_size)
11961 dwarf2_const_value_length_mismatch_complaint (name,
11962 cu_header->addr_size,
11963 TYPE_LENGTH (type));
11964 /* Symbols of this form are reasonably rare, so we just
11965 piggyback on the existing location code rather than writing
11966 a new implementation of symbol_computed_ops. */
11967 *baton = obstack_alloc (&objfile->objfile_obstack,
11968 sizeof (struct dwarf2_locexpr_baton));
11969 (*baton)->per_cu = cu->per_cu;
11970 gdb_assert ((*baton)->per_cu);
11971
11972 (*baton)->size = 2 + cu_header->addr_size;
11973 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11974 (*baton)->data = data;
11975
11976 data[0] = DW_OP_addr;
11977 store_unsigned_integer (&data[1], cu_header->addr_size,
11978 byte_order, DW_ADDR (attr));
11979 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11980 }
11981 break;
11982 case DW_FORM_string:
11983 case DW_FORM_strp:
11984 /* DW_STRING is already allocated on the objfile obstack, point
11985 directly to it. */
11986 *bytes = (gdb_byte *) DW_STRING (attr);
11987 break;
11988 case DW_FORM_block1:
11989 case DW_FORM_block2:
11990 case DW_FORM_block4:
11991 case DW_FORM_block:
11992 case DW_FORM_exprloc:
11993 blk = DW_BLOCK (attr);
11994 if (TYPE_LENGTH (type) != blk->size)
11995 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11996 TYPE_LENGTH (type));
11997 *bytes = blk->data;
11998 break;
11999
12000 /* The DW_AT_const_value attributes are supposed to carry the
12001 symbol's value "represented as it would be on the target
12002 architecture." By the time we get here, it's already been
12003 converted to host endianness, so we just need to sign- or
12004 zero-extend it as appropriate. */
12005 case DW_FORM_data1:
12006 *bytes = dwarf2_const_value_data (attr, type, name,
12007 obstack, cu, value, 8);
12008 break;
12009 case DW_FORM_data2:
12010 *bytes = dwarf2_const_value_data (attr, type, name,
12011 obstack, cu, value, 16);
12012 break;
12013 case DW_FORM_data4:
12014 *bytes = dwarf2_const_value_data (attr, type, name,
12015 obstack, cu, value, 32);
12016 break;
12017 case DW_FORM_data8:
12018 *bytes = dwarf2_const_value_data (attr, type, name,
12019 obstack, cu, value, 64);
12020 break;
12021
12022 case DW_FORM_sdata:
12023 *value = DW_SND (attr);
12024 break;
12025
12026 case DW_FORM_udata:
12027 *value = DW_UNSND (attr);
12028 break;
12029
12030 default:
12031 complaint (&symfile_complaints,
12032 _("unsupported const value attribute form: '%s'"),
12033 dwarf_form_name (attr->form));
12034 *value = 0;
12035 break;
12036 }
12037 }
12038
12039
12040 /* Copy constant value from an attribute to a symbol. */
12041
12042 static void
12043 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12044 struct dwarf2_cu *cu)
12045 {
12046 struct objfile *objfile = cu->objfile;
12047 struct comp_unit_head *cu_header = &cu->header;
12048 long value;
12049 gdb_byte *bytes;
12050 struct dwarf2_locexpr_baton *baton;
12051
12052 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12053 SYMBOL_PRINT_NAME (sym),
12054 &objfile->objfile_obstack, cu,
12055 &value, &bytes, &baton);
12056
12057 if (baton != NULL)
12058 {
12059 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12060 SYMBOL_LOCATION_BATON (sym) = baton;
12061 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12062 }
12063 else if (bytes != NULL)
12064 {
12065 SYMBOL_VALUE_BYTES (sym) = bytes;
12066 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12067 }
12068 else
12069 {
12070 SYMBOL_VALUE (sym) = value;
12071 SYMBOL_CLASS (sym) = LOC_CONST;
12072 }
12073 }
12074
12075 /* Return the type of the die in question using its DW_AT_type attribute. */
12076
12077 static struct type *
12078 die_type (struct die_info *die, struct dwarf2_cu *cu)
12079 {
12080 struct attribute *type_attr;
12081
12082 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12083 if (!type_attr)
12084 {
12085 /* A missing DW_AT_type represents a void type. */
12086 return objfile_type (cu->objfile)->builtin_void;
12087 }
12088
12089 return lookup_die_type (die, type_attr, cu);
12090 }
12091
12092 /* True iff CU's producer generates GNAT Ada auxiliary information
12093 that allows to find parallel types through that information instead
12094 of having to do expensive parallel lookups by type name. */
12095
12096 static int
12097 need_gnat_info (struct dwarf2_cu *cu)
12098 {
12099 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12100 of GNAT produces this auxiliary information, without any indication
12101 that it is produced. Part of enhancing the FSF version of GNAT
12102 to produce that information will be to put in place an indicator
12103 that we can use in order to determine whether the descriptive type
12104 info is available or not. One suggestion that has been made is
12105 to use a new attribute, attached to the CU die. For now, assume
12106 that the descriptive type info is not available. */
12107 return 0;
12108 }
12109
12110 /* Return the auxiliary type of the die in question using its
12111 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12112 attribute is not present. */
12113
12114 static struct type *
12115 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12116 {
12117 struct attribute *type_attr;
12118
12119 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12120 if (!type_attr)
12121 return NULL;
12122
12123 return lookup_die_type (die, type_attr, cu);
12124 }
12125
12126 /* If DIE has a descriptive_type attribute, then set the TYPE's
12127 descriptive type accordingly. */
12128
12129 static void
12130 set_descriptive_type (struct type *type, struct die_info *die,
12131 struct dwarf2_cu *cu)
12132 {
12133 struct type *descriptive_type = die_descriptive_type (die, cu);
12134
12135 if (descriptive_type)
12136 {
12137 ALLOCATE_GNAT_AUX_TYPE (type);
12138 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12139 }
12140 }
12141
12142 /* Return the containing type of the die in question using its
12143 DW_AT_containing_type attribute. */
12144
12145 static struct type *
12146 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12147 {
12148 struct attribute *type_attr;
12149
12150 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12151 if (!type_attr)
12152 error (_("Dwarf Error: Problem turning containing type into gdb type "
12153 "[in module %s]"), cu->objfile->name);
12154
12155 return lookup_die_type (die, type_attr, cu);
12156 }
12157
12158 /* Look up the type of DIE in CU using its type attribute ATTR.
12159 If there is no type substitute an error marker. */
12160
12161 static struct type *
12162 lookup_die_type (struct die_info *die, struct attribute *attr,
12163 struct dwarf2_cu *cu)
12164 {
12165 struct type *this_type;
12166
12167 /* First see if we have it cached. */
12168
12169 if (is_ref_attr (attr))
12170 {
12171 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12172
12173 this_type = get_die_type_at_offset (offset, cu->per_cu);
12174 }
12175 else if (attr->form == DW_FORM_ref_sig8)
12176 {
12177 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12178 struct dwarf2_cu *sig_cu;
12179 unsigned int offset;
12180
12181 /* sig_type will be NULL if the signatured type is missing from
12182 the debug info. */
12183 if (sig_type == NULL)
12184 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12185 "at 0x%x [in module %s]"),
12186 die->offset, cu->objfile->name);
12187
12188 gdb_assert (sig_type->per_cu.debug_type_section);
12189 offset = sig_type->per_cu.offset + sig_type->type_offset;
12190 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12191 }
12192 else
12193 {
12194 dump_die_for_error (die);
12195 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12196 dwarf_attr_name (attr->name), cu->objfile->name);
12197 }
12198
12199 /* If not cached we need to read it in. */
12200
12201 if (this_type == NULL)
12202 {
12203 struct die_info *type_die;
12204 struct dwarf2_cu *type_cu = cu;
12205
12206 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12207 /* If the type is cached, we should have found it above. */
12208 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12209 this_type = read_type_die_1 (type_die, type_cu);
12210 }
12211
12212 /* If we still don't have a type use an error marker. */
12213
12214 if (this_type == NULL)
12215 {
12216 char *message, *saved;
12217
12218 /* read_type_die already issued a complaint. */
12219 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12220 cu->objfile->name,
12221 cu->header.offset,
12222 die->offset);
12223 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12224 message, strlen (message));
12225 xfree (message);
12226
12227 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
12228 }
12229
12230 return this_type;
12231 }
12232
12233 /* Return the type in DIE, CU.
12234 Returns NULL for invalid types.
12235
12236 This first does a lookup in the appropriate type_hash table,
12237 and only reads the die in if necessary.
12238
12239 NOTE: This can be called when reading in partial or full symbols. */
12240
12241 static struct type *
12242 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12243 {
12244 struct type *this_type;
12245
12246 this_type = get_die_type (die, cu);
12247 if (this_type)
12248 return this_type;
12249
12250 return read_type_die_1 (die, cu);
12251 }
12252
12253 /* Read the type in DIE, CU.
12254 Returns NULL for invalid types. */
12255
12256 static struct type *
12257 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12258 {
12259 struct type *this_type = NULL;
12260
12261 switch (die->tag)
12262 {
12263 case DW_TAG_class_type:
12264 case DW_TAG_interface_type:
12265 case DW_TAG_structure_type:
12266 case DW_TAG_union_type:
12267 this_type = read_structure_type (die, cu);
12268 break;
12269 case DW_TAG_enumeration_type:
12270 this_type = read_enumeration_type (die, cu);
12271 break;
12272 case DW_TAG_subprogram:
12273 case DW_TAG_subroutine_type:
12274 case DW_TAG_inlined_subroutine:
12275 this_type = read_subroutine_type (die, cu);
12276 break;
12277 case DW_TAG_array_type:
12278 this_type = read_array_type (die, cu);
12279 break;
12280 case DW_TAG_set_type:
12281 this_type = read_set_type (die, cu);
12282 break;
12283 case DW_TAG_pointer_type:
12284 this_type = read_tag_pointer_type (die, cu);
12285 break;
12286 case DW_TAG_ptr_to_member_type:
12287 this_type = read_tag_ptr_to_member_type (die, cu);
12288 break;
12289 case DW_TAG_reference_type:
12290 this_type = read_tag_reference_type (die, cu);
12291 break;
12292 case DW_TAG_const_type:
12293 this_type = read_tag_const_type (die, cu);
12294 break;
12295 case DW_TAG_volatile_type:
12296 this_type = read_tag_volatile_type (die, cu);
12297 break;
12298 case DW_TAG_string_type:
12299 this_type = read_tag_string_type (die, cu);
12300 break;
12301 case DW_TAG_typedef:
12302 this_type = read_typedef (die, cu);
12303 break;
12304 case DW_TAG_subrange_type:
12305 this_type = read_subrange_type (die, cu);
12306 break;
12307 case DW_TAG_base_type:
12308 this_type = read_base_type (die, cu);
12309 break;
12310 case DW_TAG_unspecified_type:
12311 this_type = read_unspecified_type (die, cu);
12312 break;
12313 case DW_TAG_namespace:
12314 this_type = read_namespace_type (die, cu);
12315 break;
12316 case DW_TAG_module:
12317 this_type = read_module_type (die, cu);
12318 break;
12319 default:
12320 complaint (&symfile_complaints,
12321 _("unexpected tag in read_type_die: '%s'"),
12322 dwarf_tag_name (die->tag));
12323 break;
12324 }
12325
12326 return this_type;
12327 }
12328
12329 /* See if we can figure out if the class lives in a namespace. We do
12330 this by looking for a member function; its demangled name will
12331 contain namespace info, if there is any.
12332 Return the computed name or NULL.
12333 Space for the result is allocated on the objfile's obstack.
12334 This is the full-die version of guess_partial_die_structure_name.
12335 In this case we know DIE has no useful parent. */
12336
12337 static char *
12338 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12339 {
12340 struct die_info *spec_die;
12341 struct dwarf2_cu *spec_cu;
12342 struct die_info *child;
12343
12344 spec_cu = cu;
12345 spec_die = die_specification (die, &spec_cu);
12346 if (spec_die != NULL)
12347 {
12348 die = spec_die;
12349 cu = spec_cu;
12350 }
12351
12352 for (child = die->child;
12353 child != NULL;
12354 child = child->sibling)
12355 {
12356 if (child->tag == DW_TAG_subprogram)
12357 {
12358 struct attribute *attr;
12359
12360 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12361 if (attr == NULL)
12362 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12363 if (attr != NULL)
12364 {
12365 char *actual_name
12366 = language_class_name_from_physname (cu->language_defn,
12367 DW_STRING (attr));
12368 char *name = NULL;
12369
12370 if (actual_name != NULL)
12371 {
12372 char *die_name = dwarf2_name (die, cu);
12373
12374 if (die_name != NULL
12375 && strcmp (die_name, actual_name) != 0)
12376 {
12377 /* Strip off the class name from the full name.
12378 We want the prefix. */
12379 int die_name_len = strlen (die_name);
12380 int actual_name_len = strlen (actual_name);
12381
12382 /* Test for '::' as a sanity check. */
12383 if (actual_name_len > die_name_len + 2
12384 && actual_name[actual_name_len
12385 - die_name_len - 1] == ':')
12386 name =
12387 obsavestring (actual_name,
12388 actual_name_len - die_name_len - 2,
12389 &cu->objfile->objfile_obstack);
12390 }
12391 }
12392 xfree (actual_name);
12393 return name;
12394 }
12395 }
12396 }
12397
12398 return NULL;
12399 }
12400
12401 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12402 prefix part in such case. See
12403 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12404
12405 static char *
12406 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12407 {
12408 struct attribute *attr;
12409 char *base;
12410
12411 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12412 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12413 return NULL;
12414
12415 attr = dwarf2_attr (die, DW_AT_name, cu);
12416 if (attr != NULL && DW_STRING (attr) != NULL)
12417 return NULL;
12418
12419 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12420 if (attr == NULL)
12421 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12422 if (attr == NULL || DW_STRING (attr) == NULL)
12423 return NULL;
12424
12425 /* dwarf2_name had to be already called. */
12426 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12427
12428 /* Strip the base name, keep any leading namespaces/classes. */
12429 base = strrchr (DW_STRING (attr), ':');
12430 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12431 return "";
12432
12433 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12434 &cu->objfile->objfile_obstack);
12435 }
12436
12437 /* Return the name of the namespace/class that DIE is defined within,
12438 or "" if we can't tell. The caller should not xfree the result.
12439
12440 For example, if we're within the method foo() in the following
12441 code:
12442
12443 namespace N {
12444 class C {
12445 void foo () {
12446 }
12447 };
12448 }
12449
12450 then determine_prefix on foo's die will return "N::C". */
12451
12452 static char *
12453 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12454 {
12455 struct die_info *parent, *spec_die;
12456 struct dwarf2_cu *spec_cu;
12457 struct type *parent_type;
12458 char *retval;
12459
12460 if (cu->language != language_cplus && cu->language != language_java
12461 && cu->language != language_fortran)
12462 return "";
12463
12464 retval = anonymous_struct_prefix (die, cu);
12465 if (retval)
12466 return retval;
12467
12468 /* We have to be careful in the presence of DW_AT_specification.
12469 For example, with GCC 3.4, given the code
12470
12471 namespace N {
12472 void foo() {
12473 // Definition of N::foo.
12474 }
12475 }
12476
12477 then we'll have a tree of DIEs like this:
12478
12479 1: DW_TAG_compile_unit
12480 2: DW_TAG_namespace // N
12481 3: DW_TAG_subprogram // declaration of N::foo
12482 4: DW_TAG_subprogram // definition of N::foo
12483 DW_AT_specification // refers to die #3
12484
12485 Thus, when processing die #4, we have to pretend that we're in
12486 the context of its DW_AT_specification, namely the contex of die
12487 #3. */
12488 spec_cu = cu;
12489 spec_die = die_specification (die, &spec_cu);
12490 if (spec_die == NULL)
12491 parent = die->parent;
12492 else
12493 {
12494 parent = spec_die->parent;
12495 cu = spec_cu;
12496 }
12497
12498 if (parent == NULL)
12499 return "";
12500 else if (parent->building_fullname)
12501 {
12502 const char *name;
12503 const char *parent_name;
12504
12505 /* It has been seen on RealView 2.2 built binaries,
12506 DW_TAG_template_type_param types actually _defined_ as
12507 children of the parent class:
12508
12509 enum E {};
12510 template class <class Enum> Class{};
12511 Class<enum E> class_e;
12512
12513 1: DW_TAG_class_type (Class)
12514 2: DW_TAG_enumeration_type (E)
12515 3: DW_TAG_enumerator (enum1:0)
12516 3: DW_TAG_enumerator (enum2:1)
12517 ...
12518 2: DW_TAG_template_type_param
12519 DW_AT_type DW_FORM_ref_udata (E)
12520
12521 Besides being broken debug info, it can put GDB into an
12522 infinite loop. Consider:
12523
12524 When we're building the full name for Class<E>, we'll start
12525 at Class, and go look over its template type parameters,
12526 finding E. We'll then try to build the full name of E, and
12527 reach here. We're now trying to build the full name of E,
12528 and look over the parent DIE for containing scope. In the
12529 broken case, if we followed the parent DIE of E, we'd again
12530 find Class, and once again go look at its template type
12531 arguments, etc., etc. Simply don't consider such parent die
12532 as source-level parent of this die (it can't be, the language
12533 doesn't allow it), and break the loop here. */
12534 name = dwarf2_name (die, cu);
12535 parent_name = dwarf2_name (parent, cu);
12536 complaint (&symfile_complaints,
12537 _("template param type '%s' defined within parent '%s'"),
12538 name ? name : "<unknown>",
12539 parent_name ? parent_name : "<unknown>");
12540 return "";
12541 }
12542 else
12543 switch (parent->tag)
12544 {
12545 case DW_TAG_namespace:
12546 parent_type = read_type_die (parent, cu);
12547 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12548 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12549 Work around this problem here. */
12550 if (cu->language == language_cplus
12551 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12552 return "";
12553 /* We give a name to even anonymous namespaces. */
12554 return TYPE_TAG_NAME (parent_type);
12555 case DW_TAG_class_type:
12556 case DW_TAG_interface_type:
12557 case DW_TAG_structure_type:
12558 case DW_TAG_union_type:
12559 case DW_TAG_module:
12560 parent_type = read_type_die (parent, cu);
12561 if (TYPE_TAG_NAME (parent_type) != NULL)
12562 return TYPE_TAG_NAME (parent_type);
12563 else
12564 /* An anonymous structure is only allowed non-static data
12565 members; no typedefs, no member functions, et cetera.
12566 So it does not need a prefix. */
12567 return "";
12568 case DW_TAG_compile_unit:
12569 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12570 if (cu->language == language_cplus
12571 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12572 && die->child != NULL
12573 && (die->tag == DW_TAG_class_type
12574 || die->tag == DW_TAG_structure_type
12575 || die->tag == DW_TAG_union_type))
12576 {
12577 char *name = guess_full_die_structure_name (die, cu);
12578 if (name != NULL)
12579 return name;
12580 }
12581 return "";
12582 default:
12583 return determine_prefix (parent, cu);
12584 }
12585 }
12586
12587 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12588 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12589 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12590 an obconcat, otherwise allocate storage for the result. The CU argument is
12591 used to determine the language and hence, the appropriate separator. */
12592
12593 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12594
12595 static char *
12596 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12597 int physname, struct dwarf2_cu *cu)
12598 {
12599 const char *lead = "";
12600 const char *sep;
12601
12602 if (suffix == NULL || suffix[0] == '\0'
12603 || prefix == NULL || prefix[0] == '\0')
12604 sep = "";
12605 else if (cu->language == language_java)
12606 sep = ".";
12607 else if (cu->language == language_fortran && physname)
12608 {
12609 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12610 DW_AT_MIPS_linkage_name is preferred and used instead. */
12611
12612 lead = "__";
12613 sep = "_MOD_";
12614 }
12615 else
12616 sep = "::";
12617
12618 if (prefix == NULL)
12619 prefix = "";
12620 if (suffix == NULL)
12621 suffix = "";
12622
12623 if (obs == NULL)
12624 {
12625 char *retval
12626 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12627
12628 strcpy (retval, lead);
12629 strcat (retval, prefix);
12630 strcat (retval, sep);
12631 strcat (retval, suffix);
12632 return retval;
12633 }
12634 else
12635 {
12636 /* We have an obstack. */
12637 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12638 }
12639 }
12640
12641 /* Return sibling of die, NULL if no sibling. */
12642
12643 static struct die_info *
12644 sibling_die (struct die_info *die)
12645 {
12646 return die->sibling;
12647 }
12648
12649 /* Get name of a die, return NULL if not found. */
12650
12651 static char *
12652 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12653 struct obstack *obstack)
12654 {
12655 if (name && cu->language == language_cplus)
12656 {
12657 char *canon_name = cp_canonicalize_string (name);
12658
12659 if (canon_name != NULL)
12660 {
12661 if (strcmp (canon_name, name) != 0)
12662 name = obsavestring (canon_name, strlen (canon_name),
12663 obstack);
12664 xfree (canon_name);
12665 }
12666 }
12667
12668 return name;
12669 }
12670
12671 /* Get name of a die, return NULL if not found. */
12672
12673 static char *
12674 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12675 {
12676 struct attribute *attr;
12677
12678 attr = dwarf2_attr (die, DW_AT_name, cu);
12679 if ((!attr || !DW_STRING (attr))
12680 && die->tag != DW_TAG_class_type
12681 && die->tag != DW_TAG_interface_type
12682 && die->tag != DW_TAG_structure_type
12683 && die->tag != DW_TAG_union_type)
12684 return NULL;
12685
12686 switch (die->tag)
12687 {
12688 case DW_TAG_compile_unit:
12689 /* Compilation units have a DW_AT_name that is a filename, not
12690 a source language identifier. */
12691 case DW_TAG_enumeration_type:
12692 case DW_TAG_enumerator:
12693 /* These tags always have simple identifiers already; no need
12694 to canonicalize them. */
12695 return DW_STRING (attr);
12696
12697 case DW_TAG_subprogram:
12698 /* Java constructors will all be named "<init>", so return
12699 the class name when we see this special case. */
12700 if (cu->language == language_java
12701 && DW_STRING (attr) != NULL
12702 && strcmp (DW_STRING (attr), "<init>") == 0)
12703 {
12704 struct dwarf2_cu *spec_cu = cu;
12705 struct die_info *spec_die;
12706
12707 /* GCJ will output '<init>' for Java constructor names.
12708 For this special case, return the name of the parent class. */
12709
12710 /* GCJ may output suprogram DIEs with AT_specification set.
12711 If so, use the name of the specified DIE. */
12712 spec_die = die_specification (die, &spec_cu);
12713 if (spec_die != NULL)
12714 return dwarf2_name (spec_die, spec_cu);
12715
12716 do
12717 {
12718 die = die->parent;
12719 if (die->tag == DW_TAG_class_type)
12720 return dwarf2_name (die, cu);
12721 }
12722 while (die->tag != DW_TAG_compile_unit);
12723 }
12724 break;
12725
12726 case DW_TAG_class_type:
12727 case DW_TAG_interface_type:
12728 case DW_TAG_structure_type:
12729 case DW_TAG_union_type:
12730 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12731 structures or unions. These were of the form "._%d" in GCC 4.1,
12732 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12733 and GCC 4.4. We work around this problem by ignoring these. */
12734 if (attr && DW_STRING (attr)
12735 && (strncmp (DW_STRING (attr), "._", 2) == 0
12736 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12737 return NULL;
12738
12739 /* GCC might emit a nameless typedef that has a linkage name. See
12740 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12741 if (!attr || DW_STRING (attr) == NULL)
12742 {
12743 char *demangled = NULL;
12744
12745 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12746 if (attr == NULL)
12747 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12748
12749 if (attr == NULL || DW_STRING (attr) == NULL)
12750 return NULL;
12751
12752 /* Avoid demangling DW_STRING (attr) the second time on a second
12753 call for the same DIE. */
12754 if (!DW_STRING_IS_CANONICAL (attr))
12755 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12756
12757 if (demangled)
12758 {
12759 char *base;
12760
12761 /* FIXME: we already did this for the partial symbol... */
12762 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12763 &cu->objfile->objfile_obstack);
12764 DW_STRING_IS_CANONICAL (attr) = 1;
12765 xfree (demangled);
12766
12767 /* Strip any leading namespaces/classes, keep only the base name.
12768 DW_AT_name for named DIEs does not contain the prefixes. */
12769 base = strrchr (DW_STRING (attr), ':');
12770 if (base && base > DW_STRING (attr) && base[-1] == ':')
12771 return &base[1];
12772 else
12773 return DW_STRING (attr);
12774 }
12775 }
12776 break;
12777
12778 default:
12779 break;
12780 }
12781
12782 if (!DW_STRING_IS_CANONICAL (attr))
12783 {
12784 DW_STRING (attr)
12785 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12786 &cu->objfile->objfile_obstack);
12787 DW_STRING_IS_CANONICAL (attr) = 1;
12788 }
12789 return DW_STRING (attr);
12790 }
12791
12792 /* Return the die that this die in an extension of, or NULL if there
12793 is none. *EXT_CU is the CU containing DIE on input, and the CU
12794 containing the return value on output. */
12795
12796 static struct die_info *
12797 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12798 {
12799 struct attribute *attr;
12800
12801 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12802 if (attr == NULL)
12803 return NULL;
12804
12805 return follow_die_ref (die, attr, ext_cu);
12806 }
12807
12808 /* Convert a DIE tag into its string name. */
12809
12810 static char *
12811 dwarf_tag_name (unsigned tag)
12812 {
12813 switch (tag)
12814 {
12815 case DW_TAG_padding:
12816 return "DW_TAG_padding";
12817 case DW_TAG_array_type:
12818 return "DW_TAG_array_type";
12819 case DW_TAG_class_type:
12820 return "DW_TAG_class_type";
12821 case DW_TAG_entry_point:
12822 return "DW_TAG_entry_point";
12823 case DW_TAG_enumeration_type:
12824 return "DW_TAG_enumeration_type";
12825 case DW_TAG_formal_parameter:
12826 return "DW_TAG_formal_parameter";
12827 case DW_TAG_imported_declaration:
12828 return "DW_TAG_imported_declaration";
12829 case DW_TAG_label:
12830 return "DW_TAG_label";
12831 case DW_TAG_lexical_block:
12832 return "DW_TAG_lexical_block";
12833 case DW_TAG_member:
12834 return "DW_TAG_member";
12835 case DW_TAG_pointer_type:
12836 return "DW_TAG_pointer_type";
12837 case DW_TAG_reference_type:
12838 return "DW_TAG_reference_type";
12839 case DW_TAG_compile_unit:
12840 return "DW_TAG_compile_unit";
12841 case DW_TAG_string_type:
12842 return "DW_TAG_string_type";
12843 case DW_TAG_structure_type:
12844 return "DW_TAG_structure_type";
12845 case DW_TAG_subroutine_type:
12846 return "DW_TAG_subroutine_type";
12847 case DW_TAG_typedef:
12848 return "DW_TAG_typedef";
12849 case DW_TAG_union_type:
12850 return "DW_TAG_union_type";
12851 case DW_TAG_unspecified_parameters:
12852 return "DW_TAG_unspecified_parameters";
12853 case DW_TAG_variant:
12854 return "DW_TAG_variant";
12855 case DW_TAG_common_block:
12856 return "DW_TAG_common_block";
12857 case DW_TAG_common_inclusion:
12858 return "DW_TAG_common_inclusion";
12859 case DW_TAG_inheritance:
12860 return "DW_TAG_inheritance";
12861 case DW_TAG_inlined_subroutine:
12862 return "DW_TAG_inlined_subroutine";
12863 case DW_TAG_module:
12864 return "DW_TAG_module";
12865 case DW_TAG_ptr_to_member_type:
12866 return "DW_TAG_ptr_to_member_type";
12867 case DW_TAG_set_type:
12868 return "DW_TAG_set_type";
12869 case DW_TAG_subrange_type:
12870 return "DW_TAG_subrange_type";
12871 case DW_TAG_with_stmt:
12872 return "DW_TAG_with_stmt";
12873 case DW_TAG_access_declaration:
12874 return "DW_TAG_access_declaration";
12875 case DW_TAG_base_type:
12876 return "DW_TAG_base_type";
12877 case DW_TAG_catch_block:
12878 return "DW_TAG_catch_block";
12879 case DW_TAG_const_type:
12880 return "DW_TAG_const_type";
12881 case DW_TAG_constant:
12882 return "DW_TAG_constant";
12883 case DW_TAG_enumerator:
12884 return "DW_TAG_enumerator";
12885 case DW_TAG_file_type:
12886 return "DW_TAG_file_type";
12887 case DW_TAG_friend:
12888 return "DW_TAG_friend";
12889 case DW_TAG_namelist:
12890 return "DW_TAG_namelist";
12891 case DW_TAG_namelist_item:
12892 return "DW_TAG_namelist_item";
12893 case DW_TAG_packed_type:
12894 return "DW_TAG_packed_type";
12895 case DW_TAG_subprogram:
12896 return "DW_TAG_subprogram";
12897 case DW_TAG_template_type_param:
12898 return "DW_TAG_template_type_param";
12899 case DW_TAG_template_value_param:
12900 return "DW_TAG_template_value_param";
12901 case DW_TAG_thrown_type:
12902 return "DW_TAG_thrown_type";
12903 case DW_TAG_try_block:
12904 return "DW_TAG_try_block";
12905 case DW_TAG_variant_part:
12906 return "DW_TAG_variant_part";
12907 case DW_TAG_variable:
12908 return "DW_TAG_variable";
12909 case DW_TAG_volatile_type:
12910 return "DW_TAG_volatile_type";
12911 case DW_TAG_dwarf_procedure:
12912 return "DW_TAG_dwarf_procedure";
12913 case DW_TAG_restrict_type:
12914 return "DW_TAG_restrict_type";
12915 case DW_TAG_interface_type:
12916 return "DW_TAG_interface_type";
12917 case DW_TAG_namespace:
12918 return "DW_TAG_namespace";
12919 case DW_TAG_imported_module:
12920 return "DW_TAG_imported_module";
12921 case DW_TAG_unspecified_type:
12922 return "DW_TAG_unspecified_type";
12923 case DW_TAG_partial_unit:
12924 return "DW_TAG_partial_unit";
12925 case DW_TAG_imported_unit:
12926 return "DW_TAG_imported_unit";
12927 case DW_TAG_condition:
12928 return "DW_TAG_condition";
12929 case DW_TAG_shared_type:
12930 return "DW_TAG_shared_type";
12931 case DW_TAG_type_unit:
12932 return "DW_TAG_type_unit";
12933 case DW_TAG_MIPS_loop:
12934 return "DW_TAG_MIPS_loop";
12935 case DW_TAG_HP_array_descriptor:
12936 return "DW_TAG_HP_array_descriptor";
12937 case DW_TAG_format_label:
12938 return "DW_TAG_format_label";
12939 case DW_TAG_function_template:
12940 return "DW_TAG_function_template";
12941 case DW_TAG_class_template:
12942 return "DW_TAG_class_template";
12943 case DW_TAG_GNU_BINCL:
12944 return "DW_TAG_GNU_BINCL";
12945 case DW_TAG_GNU_EINCL:
12946 return "DW_TAG_GNU_EINCL";
12947 case DW_TAG_upc_shared_type:
12948 return "DW_TAG_upc_shared_type";
12949 case DW_TAG_upc_strict_type:
12950 return "DW_TAG_upc_strict_type";
12951 case DW_TAG_upc_relaxed_type:
12952 return "DW_TAG_upc_relaxed_type";
12953 case DW_TAG_PGI_kanji_type:
12954 return "DW_TAG_PGI_kanji_type";
12955 case DW_TAG_PGI_interface_block:
12956 return "DW_TAG_PGI_interface_block";
12957 case DW_TAG_GNU_call_site:
12958 return "DW_TAG_GNU_call_site";
12959 default:
12960 return "DW_TAG_<unknown>";
12961 }
12962 }
12963
12964 /* Convert a DWARF attribute code into its string name. */
12965
12966 static char *
12967 dwarf_attr_name (unsigned attr)
12968 {
12969 switch (attr)
12970 {
12971 case DW_AT_sibling:
12972 return "DW_AT_sibling";
12973 case DW_AT_location:
12974 return "DW_AT_location";
12975 case DW_AT_name:
12976 return "DW_AT_name";
12977 case DW_AT_ordering:
12978 return "DW_AT_ordering";
12979 case DW_AT_subscr_data:
12980 return "DW_AT_subscr_data";
12981 case DW_AT_byte_size:
12982 return "DW_AT_byte_size";
12983 case DW_AT_bit_offset:
12984 return "DW_AT_bit_offset";
12985 case DW_AT_bit_size:
12986 return "DW_AT_bit_size";
12987 case DW_AT_element_list:
12988 return "DW_AT_element_list";
12989 case DW_AT_stmt_list:
12990 return "DW_AT_stmt_list";
12991 case DW_AT_low_pc:
12992 return "DW_AT_low_pc";
12993 case DW_AT_high_pc:
12994 return "DW_AT_high_pc";
12995 case DW_AT_language:
12996 return "DW_AT_language";
12997 case DW_AT_member:
12998 return "DW_AT_member";
12999 case DW_AT_discr:
13000 return "DW_AT_discr";
13001 case DW_AT_discr_value:
13002 return "DW_AT_discr_value";
13003 case DW_AT_visibility:
13004 return "DW_AT_visibility";
13005 case DW_AT_import:
13006 return "DW_AT_import";
13007 case DW_AT_string_length:
13008 return "DW_AT_string_length";
13009 case DW_AT_common_reference:
13010 return "DW_AT_common_reference";
13011 case DW_AT_comp_dir:
13012 return "DW_AT_comp_dir";
13013 case DW_AT_const_value:
13014 return "DW_AT_const_value";
13015 case DW_AT_containing_type:
13016 return "DW_AT_containing_type";
13017 case DW_AT_default_value:
13018 return "DW_AT_default_value";
13019 case DW_AT_inline:
13020 return "DW_AT_inline";
13021 case DW_AT_is_optional:
13022 return "DW_AT_is_optional";
13023 case DW_AT_lower_bound:
13024 return "DW_AT_lower_bound";
13025 case DW_AT_producer:
13026 return "DW_AT_producer";
13027 case DW_AT_prototyped:
13028 return "DW_AT_prototyped";
13029 case DW_AT_return_addr:
13030 return "DW_AT_return_addr";
13031 case DW_AT_start_scope:
13032 return "DW_AT_start_scope";
13033 case DW_AT_bit_stride:
13034 return "DW_AT_bit_stride";
13035 case DW_AT_upper_bound:
13036 return "DW_AT_upper_bound";
13037 case DW_AT_abstract_origin:
13038 return "DW_AT_abstract_origin";
13039 case DW_AT_accessibility:
13040 return "DW_AT_accessibility";
13041 case DW_AT_address_class:
13042 return "DW_AT_address_class";
13043 case DW_AT_artificial:
13044 return "DW_AT_artificial";
13045 case DW_AT_base_types:
13046 return "DW_AT_base_types";
13047 case DW_AT_calling_convention:
13048 return "DW_AT_calling_convention";
13049 case DW_AT_count:
13050 return "DW_AT_count";
13051 case DW_AT_data_member_location:
13052 return "DW_AT_data_member_location";
13053 case DW_AT_decl_column:
13054 return "DW_AT_decl_column";
13055 case DW_AT_decl_file:
13056 return "DW_AT_decl_file";
13057 case DW_AT_decl_line:
13058 return "DW_AT_decl_line";
13059 case DW_AT_declaration:
13060 return "DW_AT_declaration";
13061 case DW_AT_discr_list:
13062 return "DW_AT_discr_list";
13063 case DW_AT_encoding:
13064 return "DW_AT_encoding";
13065 case DW_AT_external:
13066 return "DW_AT_external";
13067 case DW_AT_frame_base:
13068 return "DW_AT_frame_base";
13069 case DW_AT_friend:
13070 return "DW_AT_friend";
13071 case DW_AT_identifier_case:
13072 return "DW_AT_identifier_case";
13073 case DW_AT_macro_info:
13074 return "DW_AT_macro_info";
13075 case DW_AT_namelist_items:
13076 return "DW_AT_namelist_items";
13077 case DW_AT_priority:
13078 return "DW_AT_priority";
13079 case DW_AT_segment:
13080 return "DW_AT_segment";
13081 case DW_AT_specification:
13082 return "DW_AT_specification";
13083 case DW_AT_static_link:
13084 return "DW_AT_static_link";
13085 case DW_AT_type:
13086 return "DW_AT_type";
13087 case DW_AT_use_location:
13088 return "DW_AT_use_location";
13089 case DW_AT_variable_parameter:
13090 return "DW_AT_variable_parameter";
13091 case DW_AT_virtuality:
13092 return "DW_AT_virtuality";
13093 case DW_AT_vtable_elem_location:
13094 return "DW_AT_vtable_elem_location";
13095 /* DWARF 3 values. */
13096 case DW_AT_allocated:
13097 return "DW_AT_allocated";
13098 case DW_AT_associated:
13099 return "DW_AT_associated";
13100 case DW_AT_data_location:
13101 return "DW_AT_data_location";
13102 case DW_AT_byte_stride:
13103 return "DW_AT_byte_stride";
13104 case DW_AT_entry_pc:
13105 return "DW_AT_entry_pc";
13106 case DW_AT_use_UTF8:
13107 return "DW_AT_use_UTF8";
13108 case DW_AT_extension:
13109 return "DW_AT_extension";
13110 case DW_AT_ranges:
13111 return "DW_AT_ranges";
13112 case DW_AT_trampoline:
13113 return "DW_AT_trampoline";
13114 case DW_AT_call_column:
13115 return "DW_AT_call_column";
13116 case DW_AT_call_file:
13117 return "DW_AT_call_file";
13118 case DW_AT_call_line:
13119 return "DW_AT_call_line";
13120 case DW_AT_description:
13121 return "DW_AT_description";
13122 case DW_AT_binary_scale:
13123 return "DW_AT_binary_scale";
13124 case DW_AT_decimal_scale:
13125 return "DW_AT_decimal_scale";
13126 case DW_AT_small:
13127 return "DW_AT_small";
13128 case DW_AT_decimal_sign:
13129 return "DW_AT_decimal_sign";
13130 case DW_AT_digit_count:
13131 return "DW_AT_digit_count";
13132 case DW_AT_picture_string:
13133 return "DW_AT_picture_string";
13134 case DW_AT_mutable:
13135 return "DW_AT_mutable";
13136 case DW_AT_threads_scaled:
13137 return "DW_AT_threads_scaled";
13138 case DW_AT_explicit:
13139 return "DW_AT_explicit";
13140 case DW_AT_object_pointer:
13141 return "DW_AT_object_pointer";
13142 case DW_AT_endianity:
13143 return "DW_AT_endianity";
13144 case DW_AT_elemental:
13145 return "DW_AT_elemental";
13146 case DW_AT_pure:
13147 return "DW_AT_pure";
13148 case DW_AT_recursive:
13149 return "DW_AT_recursive";
13150 /* DWARF 4 values. */
13151 case DW_AT_signature:
13152 return "DW_AT_signature";
13153 case DW_AT_linkage_name:
13154 return "DW_AT_linkage_name";
13155 /* SGI/MIPS extensions. */
13156 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13157 case DW_AT_MIPS_fde:
13158 return "DW_AT_MIPS_fde";
13159 #endif
13160 case DW_AT_MIPS_loop_begin:
13161 return "DW_AT_MIPS_loop_begin";
13162 case DW_AT_MIPS_tail_loop_begin:
13163 return "DW_AT_MIPS_tail_loop_begin";
13164 case DW_AT_MIPS_epilog_begin:
13165 return "DW_AT_MIPS_epilog_begin";
13166 case DW_AT_MIPS_loop_unroll_factor:
13167 return "DW_AT_MIPS_loop_unroll_factor";
13168 case DW_AT_MIPS_software_pipeline_depth:
13169 return "DW_AT_MIPS_software_pipeline_depth";
13170 case DW_AT_MIPS_linkage_name:
13171 return "DW_AT_MIPS_linkage_name";
13172 case DW_AT_MIPS_stride:
13173 return "DW_AT_MIPS_stride";
13174 case DW_AT_MIPS_abstract_name:
13175 return "DW_AT_MIPS_abstract_name";
13176 case DW_AT_MIPS_clone_origin:
13177 return "DW_AT_MIPS_clone_origin";
13178 case DW_AT_MIPS_has_inlines:
13179 return "DW_AT_MIPS_has_inlines";
13180 /* HP extensions. */
13181 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13182 case DW_AT_HP_block_index:
13183 return "DW_AT_HP_block_index";
13184 #endif
13185 case DW_AT_HP_unmodifiable:
13186 return "DW_AT_HP_unmodifiable";
13187 case DW_AT_HP_actuals_stmt_list:
13188 return "DW_AT_HP_actuals_stmt_list";
13189 case DW_AT_HP_proc_per_section:
13190 return "DW_AT_HP_proc_per_section";
13191 case DW_AT_HP_raw_data_ptr:
13192 return "DW_AT_HP_raw_data_ptr";
13193 case DW_AT_HP_pass_by_reference:
13194 return "DW_AT_HP_pass_by_reference";
13195 case DW_AT_HP_opt_level:
13196 return "DW_AT_HP_opt_level";
13197 case DW_AT_HP_prof_version_id:
13198 return "DW_AT_HP_prof_version_id";
13199 case DW_AT_HP_opt_flags:
13200 return "DW_AT_HP_opt_flags";
13201 case DW_AT_HP_cold_region_low_pc:
13202 return "DW_AT_HP_cold_region_low_pc";
13203 case DW_AT_HP_cold_region_high_pc:
13204 return "DW_AT_HP_cold_region_high_pc";
13205 case DW_AT_HP_all_variables_modifiable:
13206 return "DW_AT_HP_all_variables_modifiable";
13207 case DW_AT_HP_linkage_name:
13208 return "DW_AT_HP_linkage_name";
13209 case DW_AT_HP_prof_flags:
13210 return "DW_AT_HP_prof_flags";
13211 /* GNU extensions. */
13212 case DW_AT_sf_names:
13213 return "DW_AT_sf_names";
13214 case DW_AT_src_info:
13215 return "DW_AT_src_info";
13216 case DW_AT_mac_info:
13217 return "DW_AT_mac_info";
13218 case DW_AT_src_coords:
13219 return "DW_AT_src_coords";
13220 case DW_AT_body_begin:
13221 return "DW_AT_body_begin";
13222 case DW_AT_body_end:
13223 return "DW_AT_body_end";
13224 case DW_AT_GNU_vector:
13225 return "DW_AT_GNU_vector";
13226 case DW_AT_GNU_odr_signature:
13227 return "DW_AT_GNU_odr_signature";
13228 /* VMS extensions. */
13229 case DW_AT_VMS_rtnbeg_pd_address:
13230 return "DW_AT_VMS_rtnbeg_pd_address";
13231 /* UPC extension. */
13232 case DW_AT_upc_threads_scaled:
13233 return "DW_AT_upc_threads_scaled";
13234 /* PGI (STMicroelectronics) extensions. */
13235 case DW_AT_PGI_lbase:
13236 return "DW_AT_PGI_lbase";
13237 case DW_AT_PGI_soffset:
13238 return "DW_AT_PGI_soffset";
13239 case DW_AT_PGI_lstride:
13240 return "DW_AT_PGI_lstride";
13241 default:
13242 return "DW_AT_<unknown>";
13243 }
13244 }
13245
13246 /* Convert a DWARF value form code into its string name. */
13247
13248 static char *
13249 dwarf_form_name (unsigned form)
13250 {
13251 switch (form)
13252 {
13253 case DW_FORM_addr:
13254 return "DW_FORM_addr";
13255 case DW_FORM_block2:
13256 return "DW_FORM_block2";
13257 case DW_FORM_block4:
13258 return "DW_FORM_block4";
13259 case DW_FORM_data2:
13260 return "DW_FORM_data2";
13261 case DW_FORM_data4:
13262 return "DW_FORM_data4";
13263 case DW_FORM_data8:
13264 return "DW_FORM_data8";
13265 case DW_FORM_string:
13266 return "DW_FORM_string";
13267 case DW_FORM_block:
13268 return "DW_FORM_block";
13269 case DW_FORM_block1:
13270 return "DW_FORM_block1";
13271 case DW_FORM_data1:
13272 return "DW_FORM_data1";
13273 case DW_FORM_flag:
13274 return "DW_FORM_flag";
13275 case DW_FORM_sdata:
13276 return "DW_FORM_sdata";
13277 case DW_FORM_strp:
13278 return "DW_FORM_strp";
13279 case DW_FORM_udata:
13280 return "DW_FORM_udata";
13281 case DW_FORM_ref_addr:
13282 return "DW_FORM_ref_addr";
13283 case DW_FORM_ref1:
13284 return "DW_FORM_ref1";
13285 case DW_FORM_ref2:
13286 return "DW_FORM_ref2";
13287 case DW_FORM_ref4:
13288 return "DW_FORM_ref4";
13289 case DW_FORM_ref8:
13290 return "DW_FORM_ref8";
13291 case DW_FORM_ref_udata:
13292 return "DW_FORM_ref_udata";
13293 case DW_FORM_indirect:
13294 return "DW_FORM_indirect";
13295 case DW_FORM_sec_offset:
13296 return "DW_FORM_sec_offset";
13297 case DW_FORM_exprloc:
13298 return "DW_FORM_exprloc";
13299 case DW_FORM_flag_present:
13300 return "DW_FORM_flag_present";
13301 case DW_FORM_ref_sig8:
13302 return "DW_FORM_ref_sig8";
13303 default:
13304 return "DW_FORM_<unknown>";
13305 }
13306 }
13307
13308 /* Convert a DWARF stack opcode into its string name. */
13309
13310 const char *
13311 dwarf_stack_op_name (unsigned op)
13312 {
13313 switch (op)
13314 {
13315 case DW_OP_addr:
13316 return "DW_OP_addr";
13317 case DW_OP_deref:
13318 return "DW_OP_deref";
13319 case DW_OP_const1u:
13320 return "DW_OP_const1u";
13321 case DW_OP_const1s:
13322 return "DW_OP_const1s";
13323 case DW_OP_const2u:
13324 return "DW_OP_const2u";
13325 case DW_OP_const2s:
13326 return "DW_OP_const2s";
13327 case DW_OP_const4u:
13328 return "DW_OP_const4u";
13329 case DW_OP_const4s:
13330 return "DW_OP_const4s";
13331 case DW_OP_const8u:
13332 return "DW_OP_const8u";
13333 case DW_OP_const8s:
13334 return "DW_OP_const8s";
13335 case DW_OP_constu:
13336 return "DW_OP_constu";
13337 case DW_OP_consts:
13338 return "DW_OP_consts";
13339 case DW_OP_dup:
13340 return "DW_OP_dup";
13341 case DW_OP_drop:
13342 return "DW_OP_drop";
13343 case DW_OP_over:
13344 return "DW_OP_over";
13345 case DW_OP_pick:
13346 return "DW_OP_pick";
13347 case DW_OP_swap:
13348 return "DW_OP_swap";
13349 case DW_OP_rot:
13350 return "DW_OP_rot";
13351 case DW_OP_xderef:
13352 return "DW_OP_xderef";
13353 case DW_OP_abs:
13354 return "DW_OP_abs";
13355 case DW_OP_and:
13356 return "DW_OP_and";
13357 case DW_OP_div:
13358 return "DW_OP_div";
13359 case DW_OP_minus:
13360 return "DW_OP_minus";
13361 case DW_OP_mod:
13362 return "DW_OP_mod";
13363 case DW_OP_mul:
13364 return "DW_OP_mul";
13365 case DW_OP_neg:
13366 return "DW_OP_neg";
13367 case DW_OP_not:
13368 return "DW_OP_not";
13369 case DW_OP_or:
13370 return "DW_OP_or";
13371 case DW_OP_plus:
13372 return "DW_OP_plus";
13373 case DW_OP_plus_uconst:
13374 return "DW_OP_plus_uconst";
13375 case DW_OP_shl:
13376 return "DW_OP_shl";
13377 case DW_OP_shr:
13378 return "DW_OP_shr";
13379 case DW_OP_shra:
13380 return "DW_OP_shra";
13381 case DW_OP_xor:
13382 return "DW_OP_xor";
13383 case DW_OP_bra:
13384 return "DW_OP_bra";
13385 case DW_OP_eq:
13386 return "DW_OP_eq";
13387 case DW_OP_ge:
13388 return "DW_OP_ge";
13389 case DW_OP_gt:
13390 return "DW_OP_gt";
13391 case DW_OP_le:
13392 return "DW_OP_le";
13393 case DW_OP_lt:
13394 return "DW_OP_lt";
13395 case DW_OP_ne:
13396 return "DW_OP_ne";
13397 case DW_OP_skip:
13398 return "DW_OP_skip";
13399 case DW_OP_lit0:
13400 return "DW_OP_lit0";
13401 case DW_OP_lit1:
13402 return "DW_OP_lit1";
13403 case DW_OP_lit2:
13404 return "DW_OP_lit2";
13405 case DW_OP_lit3:
13406 return "DW_OP_lit3";
13407 case DW_OP_lit4:
13408 return "DW_OP_lit4";
13409 case DW_OP_lit5:
13410 return "DW_OP_lit5";
13411 case DW_OP_lit6:
13412 return "DW_OP_lit6";
13413 case DW_OP_lit7:
13414 return "DW_OP_lit7";
13415 case DW_OP_lit8:
13416 return "DW_OP_lit8";
13417 case DW_OP_lit9:
13418 return "DW_OP_lit9";
13419 case DW_OP_lit10:
13420 return "DW_OP_lit10";
13421 case DW_OP_lit11:
13422 return "DW_OP_lit11";
13423 case DW_OP_lit12:
13424 return "DW_OP_lit12";
13425 case DW_OP_lit13:
13426 return "DW_OP_lit13";
13427 case DW_OP_lit14:
13428 return "DW_OP_lit14";
13429 case DW_OP_lit15:
13430 return "DW_OP_lit15";
13431 case DW_OP_lit16:
13432 return "DW_OP_lit16";
13433 case DW_OP_lit17:
13434 return "DW_OP_lit17";
13435 case DW_OP_lit18:
13436 return "DW_OP_lit18";
13437 case DW_OP_lit19:
13438 return "DW_OP_lit19";
13439 case DW_OP_lit20:
13440 return "DW_OP_lit20";
13441 case DW_OP_lit21:
13442 return "DW_OP_lit21";
13443 case DW_OP_lit22:
13444 return "DW_OP_lit22";
13445 case DW_OP_lit23:
13446 return "DW_OP_lit23";
13447 case DW_OP_lit24:
13448 return "DW_OP_lit24";
13449 case DW_OP_lit25:
13450 return "DW_OP_lit25";
13451 case DW_OP_lit26:
13452 return "DW_OP_lit26";
13453 case DW_OP_lit27:
13454 return "DW_OP_lit27";
13455 case DW_OP_lit28:
13456 return "DW_OP_lit28";
13457 case DW_OP_lit29:
13458 return "DW_OP_lit29";
13459 case DW_OP_lit30:
13460 return "DW_OP_lit30";
13461 case DW_OP_lit31:
13462 return "DW_OP_lit31";
13463 case DW_OP_reg0:
13464 return "DW_OP_reg0";
13465 case DW_OP_reg1:
13466 return "DW_OP_reg1";
13467 case DW_OP_reg2:
13468 return "DW_OP_reg2";
13469 case DW_OP_reg3:
13470 return "DW_OP_reg3";
13471 case DW_OP_reg4:
13472 return "DW_OP_reg4";
13473 case DW_OP_reg5:
13474 return "DW_OP_reg5";
13475 case DW_OP_reg6:
13476 return "DW_OP_reg6";
13477 case DW_OP_reg7:
13478 return "DW_OP_reg7";
13479 case DW_OP_reg8:
13480 return "DW_OP_reg8";
13481 case DW_OP_reg9:
13482 return "DW_OP_reg9";
13483 case DW_OP_reg10:
13484 return "DW_OP_reg10";
13485 case DW_OP_reg11:
13486 return "DW_OP_reg11";
13487 case DW_OP_reg12:
13488 return "DW_OP_reg12";
13489 case DW_OP_reg13:
13490 return "DW_OP_reg13";
13491 case DW_OP_reg14:
13492 return "DW_OP_reg14";
13493 case DW_OP_reg15:
13494 return "DW_OP_reg15";
13495 case DW_OP_reg16:
13496 return "DW_OP_reg16";
13497 case DW_OP_reg17:
13498 return "DW_OP_reg17";
13499 case DW_OP_reg18:
13500 return "DW_OP_reg18";
13501 case DW_OP_reg19:
13502 return "DW_OP_reg19";
13503 case DW_OP_reg20:
13504 return "DW_OP_reg20";
13505 case DW_OP_reg21:
13506 return "DW_OP_reg21";
13507 case DW_OP_reg22:
13508 return "DW_OP_reg22";
13509 case DW_OP_reg23:
13510 return "DW_OP_reg23";
13511 case DW_OP_reg24:
13512 return "DW_OP_reg24";
13513 case DW_OP_reg25:
13514 return "DW_OP_reg25";
13515 case DW_OP_reg26:
13516 return "DW_OP_reg26";
13517 case DW_OP_reg27:
13518 return "DW_OP_reg27";
13519 case DW_OP_reg28:
13520 return "DW_OP_reg28";
13521 case DW_OP_reg29:
13522 return "DW_OP_reg29";
13523 case DW_OP_reg30:
13524 return "DW_OP_reg30";
13525 case DW_OP_reg31:
13526 return "DW_OP_reg31";
13527 case DW_OP_breg0:
13528 return "DW_OP_breg0";
13529 case DW_OP_breg1:
13530 return "DW_OP_breg1";
13531 case DW_OP_breg2:
13532 return "DW_OP_breg2";
13533 case DW_OP_breg3:
13534 return "DW_OP_breg3";
13535 case DW_OP_breg4:
13536 return "DW_OP_breg4";
13537 case DW_OP_breg5:
13538 return "DW_OP_breg5";
13539 case DW_OP_breg6:
13540 return "DW_OP_breg6";
13541 case DW_OP_breg7:
13542 return "DW_OP_breg7";
13543 case DW_OP_breg8:
13544 return "DW_OP_breg8";
13545 case DW_OP_breg9:
13546 return "DW_OP_breg9";
13547 case DW_OP_breg10:
13548 return "DW_OP_breg10";
13549 case DW_OP_breg11:
13550 return "DW_OP_breg11";
13551 case DW_OP_breg12:
13552 return "DW_OP_breg12";
13553 case DW_OP_breg13:
13554 return "DW_OP_breg13";
13555 case DW_OP_breg14:
13556 return "DW_OP_breg14";
13557 case DW_OP_breg15:
13558 return "DW_OP_breg15";
13559 case DW_OP_breg16:
13560 return "DW_OP_breg16";
13561 case DW_OP_breg17:
13562 return "DW_OP_breg17";
13563 case DW_OP_breg18:
13564 return "DW_OP_breg18";
13565 case DW_OP_breg19:
13566 return "DW_OP_breg19";
13567 case DW_OP_breg20:
13568 return "DW_OP_breg20";
13569 case DW_OP_breg21:
13570 return "DW_OP_breg21";
13571 case DW_OP_breg22:
13572 return "DW_OP_breg22";
13573 case DW_OP_breg23:
13574 return "DW_OP_breg23";
13575 case DW_OP_breg24:
13576 return "DW_OP_breg24";
13577 case DW_OP_breg25:
13578 return "DW_OP_breg25";
13579 case DW_OP_breg26:
13580 return "DW_OP_breg26";
13581 case DW_OP_breg27:
13582 return "DW_OP_breg27";
13583 case DW_OP_breg28:
13584 return "DW_OP_breg28";
13585 case DW_OP_breg29:
13586 return "DW_OP_breg29";
13587 case DW_OP_breg30:
13588 return "DW_OP_breg30";
13589 case DW_OP_breg31:
13590 return "DW_OP_breg31";
13591 case DW_OP_regx:
13592 return "DW_OP_regx";
13593 case DW_OP_fbreg:
13594 return "DW_OP_fbreg";
13595 case DW_OP_bregx:
13596 return "DW_OP_bregx";
13597 case DW_OP_piece:
13598 return "DW_OP_piece";
13599 case DW_OP_deref_size:
13600 return "DW_OP_deref_size";
13601 case DW_OP_xderef_size:
13602 return "DW_OP_xderef_size";
13603 case DW_OP_nop:
13604 return "DW_OP_nop";
13605 /* DWARF 3 extensions. */
13606 case DW_OP_push_object_address:
13607 return "DW_OP_push_object_address";
13608 case DW_OP_call2:
13609 return "DW_OP_call2";
13610 case DW_OP_call4:
13611 return "DW_OP_call4";
13612 case DW_OP_call_ref:
13613 return "DW_OP_call_ref";
13614 case DW_OP_form_tls_address:
13615 return "DW_OP_form_tls_address";
13616 case DW_OP_call_frame_cfa:
13617 return "DW_OP_call_frame_cfa";
13618 case DW_OP_bit_piece:
13619 return "DW_OP_bit_piece";
13620 /* DWARF 4 extensions. */
13621 case DW_OP_implicit_value:
13622 return "DW_OP_implicit_value";
13623 case DW_OP_stack_value:
13624 return "DW_OP_stack_value";
13625 /* GNU extensions. */
13626 case DW_OP_GNU_push_tls_address:
13627 return "DW_OP_GNU_push_tls_address";
13628 case DW_OP_GNU_uninit:
13629 return "DW_OP_GNU_uninit";
13630 case DW_OP_GNU_implicit_pointer:
13631 return "DW_OP_GNU_implicit_pointer";
13632 case DW_OP_GNU_entry_value:
13633 return "DW_OP_GNU_entry_value";
13634 case DW_OP_GNU_const_type:
13635 return "DW_OP_GNU_const_type";
13636 case DW_OP_GNU_regval_type:
13637 return "DW_OP_GNU_regval_type";
13638 case DW_OP_GNU_deref_type:
13639 return "DW_OP_GNU_deref_type";
13640 case DW_OP_GNU_convert:
13641 return "DW_OP_GNU_convert";
13642 case DW_OP_GNU_reinterpret:
13643 return "DW_OP_GNU_reinterpret";
13644 default:
13645 return NULL;
13646 }
13647 }
13648
13649 static char *
13650 dwarf_bool_name (unsigned mybool)
13651 {
13652 if (mybool)
13653 return "TRUE";
13654 else
13655 return "FALSE";
13656 }
13657
13658 /* Convert a DWARF type code into its string name. */
13659
13660 static char *
13661 dwarf_type_encoding_name (unsigned enc)
13662 {
13663 switch (enc)
13664 {
13665 case DW_ATE_void:
13666 return "DW_ATE_void";
13667 case DW_ATE_address:
13668 return "DW_ATE_address";
13669 case DW_ATE_boolean:
13670 return "DW_ATE_boolean";
13671 case DW_ATE_complex_float:
13672 return "DW_ATE_complex_float";
13673 case DW_ATE_float:
13674 return "DW_ATE_float";
13675 case DW_ATE_signed:
13676 return "DW_ATE_signed";
13677 case DW_ATE_signed_char:
13678 return "DW_ATE_signed_char";
13679 case DW_ATE_unsigned:
13680 return "DW_ATE_unsigned";
13681 case DW_ATE_unsigned_char:
13682 return "DW_ATE_unsigned_char";
13683 /* DWARF 3. */
13684 case DW_ATE_imaginary_float:
13685 return "DW_ATE_imaginary_float";
13686 case DW_ATE_packed_decimal:
13687 return "DW_ATE_packed_decimal";
13688 case DW_ATE_numeric_string:
13689 return "DW_ATE_numeric_string";
13690 case DW_ATE_edited:
13691 return "DW_ATE_edited";
13692 case DW_ATE_signed_fixed:
13693 return "DW_ATE_signed_fixed";
13694 case DW_ATE_unsigned_fixed:
13695 return "DW_ATE_unsigned_fixed";
13696 case DW_ATE_decimal_float:
13697 return "DW_ATE_decimal_float";
13698 /* DWARF 4. */
13699 case DW_ATE_UTF:
13700 return "DW_ATE_UTF";
13701 /* HP extensions. */
13702 case DW_ATE_HP_float80:
13703 return "DW_ATE_HP_float80";
13704 case DW_ATE_HP_complex_float80:
13705 return "DW_ATE_HP_complex_float80";
13706 case DW_ATE_HP_float128:
13707 return "DW_ATE_HP_float128";
13708 case DW_ATE_HP_complex_float128:
13709 return "DW_ATE_HP_complex_float128";
13710 case DW_ATE_HP_floathpintel:
13711 return "DW_ATE_HP_floathpintel";
13712 case DW_ATE_HP_imaginary_float80:
13713 return "DW_ATE_HP_imaginary_float80";
13714 case DW_ATE_HP_imaginary_float128:
13715 return "DW_ATE_HP_imaginary_float128";
13716 default:
13717 return "DW_ATE_<unknown>";
13718 }
13719 }
13720
13721 /* Convert a DWARF call frame info operation to its string name. */
13722
13723 #if 0
13724 static char *
13725 dwarf_cfi_name (unsigned cfi_opc)
13726 {
13727 switch (cfi_opc)
13728 {
13729 case DW_CFA_advance_loc:
13730 return "DW_CFA_advance_loc";
13731 case DW_CFA_offset:
13732 return "DW_CFA_offset";
13733 case DW_CFA_restore:
13734 return "DW_CFA_restore";
13735 case DW_CFA_nop:
13736 return "DW_CFA_nop";
13737 case DW_CFA_set_loc:
13738 return "DW_CFA_set_loc";
13739 case DW_CFA_advance_loc1:
13740 return "DW_CFA_advance_loc1";
13741 case DW_CFA_advance_loc2:
13742 return "DW_CFA_advance_loc2";
13743 case DW_CFA_advance_loc4:
13744 return "DW_CFA_advance_loc4";
13745 case DW_CFA_offset_extended:
13746 return "DW_CFA_offset_extended";
13747 case DW_CFA_restore_extended:
13748 return "DW_CFA_restore_extended";
13749 case DW_CFA_undefined:
13750 return "DW_CFA_undefined";
13751 case DW_CFA_same_value:
13752 return "DW_CFA_same_value";
13753 case DW_CFA_register:
13754 return "DW_CFA_register";
13755 case DW_CFA_remember_state:
13756 return "DW_CFA_remember_state";
13757 case DW_CFA_restore_state:
13758 return "DW_CFA_restore_state";
13759 case DW_CFA_def_cfa:
13760 return "DW_CFA_def_cfa";
13761 case DW_CFA_def_cfa_register:
13762 return "DW_CFA_def_cfa_register";
13763 case DW_CFA_def_cfa_offset:
13764 return "DW_CFA_def_cfa_offset";
13765 /* DWARF 3. */
13766 case DW_CFA_def_cfa_expression:
13767 return "DW_CFA_def_cfa_expression";
13768 case DW_CFA_expression:
13769 return "DW_CFA_expression";
13770 case DW_CFA_offset_extended_sf:
13771 return "DW_CFA_offset_extended_sf";
13772 case DW_CFA_def_cfa_sf:
13773 return "DW_CFA_def_cfa_sf";
13774 case DW_CFA_def_cfa_offset_sf:
13775 return "DW_CFA_def_cfa_offset_sf";
13776 case DW_CFA_val_offset:
13777 return "DW_CFA_val_offset";
13778 case DW_CFA_val_offset_sf:
13779 return "DW_CFA_val_offset_sf";
13780 case DW_CFA_val_expression:
13781 return "DW_CFA_val_expression";
13782 /* SGI/MIPS specific. */
13783 case DW_CFA_MIPS_advance_loc8:
13784 return "DW_CFA_MIPS_advance_loc8";
13785 /* GNU extensions. */
13786 case DW_CFA_GNU_window_save:
13787 return "DW_CFA_GNU_window_save";
13788 case DW_CFA_GNU_args_size:
13789 return "DW_CFA_GNU_args_size";
13790 case DW_CFA_GNU_negative_offset_extended:
13791 return "DW_CFA_GNU_negative_offset_extended";
13792 default:
13793 return "DW_CFA_<unknown>";
13794 }
13795 }
13796 #endif
13797
13798 static void
13799 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13800 {
13801 unsigned int i;
13802
13803 print_spaces (indent, f);
13804 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13805 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13806
13807 if (die->parent != NULL)
13808 {
13809 print_spaces (indent, f);
13810 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13811 die->parent->offset);
13812 }
13813
13814 print_spaces (indent, f);
13815 fprintf_unfiltered (f, " has children: %s\n",
13816 dwarf_bool_name (die->child != NULL));
13817
13818 print_spaces (indent, f);
13819 fprintf_unfiltered (f, " attributes:\n");
13820
13821 for (i = 0; i < die->num_attrs; ++i)
13822 {
13823 print_spaces (indent, f);
13824 fprintf_unfiltered (f, " %s (%s) ",
13825 dwarf_attr_name (die->attrs[i].name),
13826 dwarf_form_name (die->attrs[i].form));
13827
13828 switch (die->attrs[i].form)
13829 {
13830 case DW_FORM_ref_addr:
13831 case DW_FORM_addr:
13832 fprintf_unfiltered (f, "address: ");
13833 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13834 break;
13835 case DW_FORM_block2:
13836 case DW_FORM_block4:
13837 case DW_FORM_block:
13838 case DW_FORM_block1:
13839 fprintf_unfiltered (f, "block: size %d",
13840 DW_BLOCK (&die->attrs[i])->size);
13841 break;
13842 case DW_FORM_exprloc:
13843 fprintf_unfiltered (f, "expression: size %u",
13844 DW_BLOCK (&die->attrs[i])->size);
13845 break;
13846 case DW_FORM_ref1:
13847 case DW_FORM_ref2:
13848 case DW_FORM_ref4:
13849 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13850 (long) (DW_ADDR (&die->attrs[i])));
13851 break;
13852 case DW_FORM_data1:
13853 case DW_FORM_data2:
13854 case DW_FORM_data4:
13855 case DW_FORM_data8:
13856 case DW_FORM_udata:
13857 case DW_FORM_sdata:
13858 fprintf_unfiltered (f, "constant: %s",
13859 pulongest (DW_UNSND (&die->attrs[i])));
13860 break;
13861 case DW_FORM_sec_offset:
13862 fprintf_unfiltered (f, "section offset: %s",
13863 pulongest (DW_UNSND (&die->attrs[i])));
13864 break;
13865 case DW_FORM_ref_sig8:
13866 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13867 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13868 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13869 else
13870 fprintf_unfiltered (f, "signatured type, offset: unknown");
13871 break;
13872 case DW_FORM_string:
13873 case DW_FORM_strp:
13874 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13875 DW_STRING (&die->attrs[i])
13876 ? DW_STRING (&die->attrs[i]) : "",
13877 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13878 break;
13879 case DW_FORM_flag:
13880 if (DW_UNSND (&die->attrs[i]))
13881 fprintf_unfiltered (f, "flag: TRUE");
13882 else
13883 fprintf_unfiltered (f, "flag: FALSE");
13884 break;
13885 case DW_FORM_flag_present:
13886 fprintf_unfiltered (f, "flag: TRUE");
13887 break;
13888 case DW_FORM_indirect:
13889 /* The reader will have reduced the indirect form to
13890 the "base form" so this form should not occur. */
13891 fprintf_unfiltered (f,
13892 "unexpected attribute form: DW_FORM_indirect");
13893 break;
13894 default:
13895 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13896 die->attrs[i].form);
13897 break;
13898 }
13899 fprintf_unfiltered (f, "\n");
13900 }
13901 }
13902
13903 static void
13904 dump_die_for_error (struct die_info *die)
13905 {
13906 dump_die_shallow (gdb_stderr, 0, die);
13907 }
13908
13909 static void
13910 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13911 {
13912 int indent = level * 4;
13913
13914 gdb_assert (die != NULL);
13915
13916 if (level >= max_level)
13917 return;
13918
13919 dump_die_shallow (f, indent, die);
13920
13921 if (die->child != NULL)
13922 {
13923 print_spaces (indent, f);
13924 fprintf_unfiltered (f, " Children:");
13925 if (level + 1 < max_level)
13926 {
13927 fprintf_unfiltered (f, "\n");
13928 dump_die_1 (f, level + 1, max_level, die->child);
13929 }
13930 else
13931 {
13932 fprintf_unfiltered (f,
13933 " [not printed, max nesting level reached]\n");
13934 }
13935 }
13936
13937 if (die->sibling != NULL && level > 0)
13938 {
13939 dump_die_1 (f, level, max_level, die->sibling);
13940 }
13941 }
13942
13943 /* This is called from the pdie macro in gdbinit.in.
13944 It's not static so gcc will keep a copy callable from gdb. */
13945
13946 void
13947 dump_die (struct die_info *die, int max_level)
13948 {
13949 dump_die_1 (gdb_stdlog, 0, max_level, die);
13950 }
13951
13952 static void
13953 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13954 {
13955 void **slot;
13956
13957 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13958
13959 *slot = die;
13960 }
13961
13962 static int
13963 is_ref_attr (struct attribute *attr)
13964 {
13965 switch (attr->form)
13966 {
13967 case DW_FORM_ref_addr:
13968 case DW_FORM_ref1:
13969 case DW_FORM_ref2:
13970 case DW_FORM_ref4:
13971 case DW_FORM_ref8:
13972 case DW_FORM_ref_udata:
13973 return 1;
13974 default:
13975 return 0;
13976 }
13977 }
13978
13979 static unsigned int
13980 dwarf2_get_ref_die_offset (struct attribute *attr)
13981 {
13982 if (is_ref_attr (attr))
13983 return DW_ADDR (attr);
13984
13985 complaint (&symfile_complaints,
13986 _("unsupported die ref attribute form: '%s'"),
13987 dwarf_form_name (attr->form));
13988 return 0;
13989 }
13990
13991 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13992 * the value held by the attribute is not constant. */
13993
13994 static LONGEST
13995 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13996 {
13997 if (attr->form == DW_FORM_sdata)
13998 return DW_SND (attr);
13999 else if (attr->form == DW_FORM_udata
14000 || attr->form == DW_FORM_data1
14001 || attr->form == DW_FORM_data2
14002 || attr->form == DW_FORM_data4
14003 || attr->form == DW_FORM_data8)
14004 return DW_UNSND (attr);
14005 else
14006 {
14007 complaint (&symfile_complaints,
14008 _("Attribute value is not a constant (%s)"),
14009 dwarf_form_name (attr->form));
14010 return default_value;
14011 }
14012 }
14013
14014 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14015 unit and add it to our queue.
14016 The result is non-zero if PER_CU was queued, otherwise the result is zero
14017 meaning either PER_CU is already queued or it is already loaded. */
14018
14019 static int
14020 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14021 struct dwarf2_per_cu_data *per_cu)
14022 {
14023 /* We may arrive here during partial symbol reading, if we need full
14024 DIEs to process an unusual case (e.g. template arguments). Do
14025 not queue PER_CU, just tell our caller to load its DIEs. */
14026 if (dwarf2_per_objfile->reading_partial_symbols)
14027 {
14028 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14029 return 1;
14030 return 0;
14031 }
14032
14033 /* Mark the dependence relation so that we don't flush PER_CU
14034 too early. */
14035 dwarf2_add_dependence (this_cu, per_cu);
14036
14037 /* If it's already on the queue, we have nothing to do. */
14038 if (per_cu->queued)
14039 return 0;
14040
14041 /* If the compilation unit is already loaded, just mark it as
14042 used. */
14043 if (per_cu->cu != NULL)
14044 {
14045 per_cu->cu->last_used = 0;
14046 return 0;
14047 }
14048
14049 /* Add it to the queue. */
14050 queue_comp_unit (per_cu, this_cu->objfile);
14051
14052 return 1;
14053 }
14054
14055 /* Follow reference or signature attribute ATTR of SRC_DIE.
14056 On entry *REF_CU is the CU of SRC_DIE.
14057 On exit *REF_CU is the CU of the result. */
14058
14059 static struct die_info *
14060 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14061 struct dwarf2_cu **ref_cu)
14062 {
14063 struct die_info *die;
14064
14065 if (is_ref_attr (attr))
14066 die = follow_die_ref (src_die, attr, ref_cu);
14067 else if (attr->form == DW_FORM_ref_sig8)
14068 die = follow_die_sig (src_die, attr, ref_cu);
14069 else
14070 {
14071 dump_die_for_error (src_die);
14072 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14073 (*ref_cu)->objfile->name);
14074 }
14075
14076 return die;
14077 }
14078
14079 /* Follow reference OFFSET.
14080 On entry *REF_CU is the CU of the source die referencing OFFSET.
14081 On exit *REF_CU is the CU of the result.
14082 Returns NULL if OFFSET is invalid. */
14083
14084 static struct die_info *
14085 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14086 {
14087 struct die_info temp_die;
14088 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14089
14090 gdb_assert (cu->per_cu != NULL);
14091
14092 target_cu = cu;
14093
14094 if (cu->per_cu->debug_type_section)
14095 {
14096 /* .debug_types CUs cannot reference anything outside their CU.
14097 If they need to, they have to reference a signatured type via
14098 DW_FORM_ref_sig8. */
14099 if (! offset_in_cu_p (&cu->header, offset))
14100 return NULL;
14101 }
14102 else if (! offset_in_cu_p (&cu->header, offset))
14103 {
14104 struct dwarf2_per_cu_data *per_cu;
14105
14106 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14107
14108 /* If necessary, add it to the queue and load its DIEs. */
14109 if (maybe_queue_comp_unit (cu, per_cu))
14110 load_full_comp_unit (per_cu, cu->objfile);
14111
14112 target_cu = per_cu->cu;
14113 }
14114 else if (cu->dies == NULL)
14115 {
14116 /* We're loading full DIEs during partial symbol reading. */
14117 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14118 load_full_comp_unit (cu->per_cu, cu->objfile);
14119 }
14120
14121 *ref_cu = target_cu;
14122 temp_die.offset = offset;
14123 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14124 }
14125
14126 /* Follow reference attribute ATTR of SRC_DIE.
14127 On entry *REF_CU is the CU of SRC_DIE.
14128 On exit *REF_CU is the CU of the result. */
14129
14130 static struct die_info *
14131 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14132 struct dwarf2_cu **ref_cu)
14133 {
14134 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14135 struct dwarf2_cu *cu = *ref_cu;
14136 struct die_info *die;
14137
14138 die = follow_die_offset (offset, ref_cu);
14139 if (!die)
14140 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14141 "at 0x%x [in module %s]"),
14142 offset, src_die->offset, cu->objfile->name);
14143
14144 return die;
14145 }
14146
14147 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14148 Returned value is intended for DW_OP_call*. Returned
14149 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14150
14151 struct dwarf2_locexpr_baton
14152 dwarf2_fetch_die_location_block (unsigned int offset,
14153 struct dwarf2_per_cu_data *per_cu,
14154 CORE_ADDR (*get_frame_pc) (void *baton),
14155 void *baton)
14156 {
14157 struct dwarf2_cu *cu;
14158 struct die_info *die;
14159 struct attribute *attr;
14160 struct dwarf2_locexpr_baton retval;
14161
14162 dw2_setup (per_cu->objfile);
14163
14164 if (per_cu->cu == NULL)
14165 load_cu (per_cu);
14166 cu = per_cu->cu;
14167
14168 die = follow_die_offset (offset, &cu);
14169 if (!die)
14170 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14171 offset, per_cu->cu->objfile->name);
14172
14173 attr = dwarf2_attr (die, DW_AT_location, cu);
14174 if (!attr)
14175 {
14176 /* DWARF: "If there is no such attribute, then there is no effect.". */
14177
14178 retval.data = NULL;
14179 retval.size = 0;
14180 }
14181 else if (attr_form_is_section_offset (attr))
14182 {
14183 struct dwarf2_loclist_baton loclist_baton;
14184 CORE_ADDR pc = (*get_frame_pc) (baton);
14185 size_t size;
14186
14187 fill_in_loclist_baton (cu, &loclist_baton, attr);
14188
14189 retval.data = dwarf2_find_location_expression (&loclist_baton,
14190 &size, pc);
14191 retval.size = size;
14192 }
14193 else
14194 {
14195 if (!attr_form_is_block (attr))
14196 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14197 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14198 offset, per_cu->cu->objfile->name);
14199
14200 retval.data = DW_BLOCK (attr)->data;
14201 retval.size = DW_BLOCK (attr)->size;
14202 }
14203 retval.per_cu = cu->per_cu;
14204
14205 age_cached_comp_units ();
14206
14207 return retval;
14208 }
14209
14210 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14211 PER_CU. */
14212
14213 struct type *
14214 dwarf2_get_die_type (unsigned int die_offset,
14215 struct dwarf2_per_cu_data *per_cu)
14216 {
14217 dw2_setup (per_cu->objfile);
14218 return get_die_type_at_offset (die_offset, per_cu);
14219 }
14220
14221 /* Follow the signature attribute ATTR in SRC_DIE.
14222 On entry *REF_CU is the CU of SRC_DIE.
14223 On exit *REF_CU is the CU of the result. */
14224
14225 static struct die_info *
14226 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14227 struct dwarf2_cu **ref_cu)
14228 {
14229 struct objfile *objfile = (*ref_cu)->objfile;
14230 struct die_info temp_die;
14231 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14232 struct dwarf2_cu *sig_cu;
14233 struct die_info *die;
14234
14235 /* sig_type will be NULL if the signatured type is missing from
14236 the debug info. */
14237 if (sig_type == NULL)
14238 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14239 "at 0x%x [in module %s]"),
14240 src_die->offset, objfile->name);
14241
14242 /* If necessary, add it to the queue and load its DIEs. */
14243
14244 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14245 read_signatured_type (objfile, sig_type);
14246
14247 gdb_assert (sig_type->per_cu.cu != NULL);
14248
14249 sig_cu = sig_type->per_cu.cu;
14250 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14251 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14252 if (die)
14253 {
14254 *ref_cu = sig_cu;
14255 return die;
14256 }
14257
14258 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14259 "from DIE at 0x%x [in module %s]"),
14260 sig_type->type_offset, src_die->offset, objfile->name);
14261 }
14262
14263 /* Given an offset of a signatured type, return its signatured_type. */
14264
14265 static struct signatured_type *
14266 lookup_signatured_type_at_offset (struct objfile *objfile,
14267 struct dwarf2_section_info *section,
14268 unsigned int offset)
14269 {
14270 gdb_byte *info_ptr = section->buffer + offset;
14271 unsigned int length, initial_length_size;
14272 unsigned int sig_offset;
14273 struct signatured_type find_entry, *type_sig;
14274
14275 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14276 sig_offset = (initial_length_size
14277 + 2 /*version*/
14278 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14279 + 1 /*address_size*/);
14280 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14281 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14282
14283 /* This is only used to lookup previously recorded types.
14284 If we didn't find it, it's our bug. */
14285 gdb_assert (type_sig != NULL);
14286 gdb_assert (offset == type_sig->per_cu.offset);
14287
14288 return type_sig;
14289 }
14290
14291 /* Read in signatured type at OFFSET and build its CU and die(s). */
14292
14293 static void
14294 read_signatured_type_at_offset (struct objfile *objfile,
14295 struct dwarf2_section_info *sect,
14296 unsigned int offset)
14297 {
14298 struct signatured_type *type_sig;
14299
14300 dwarf2_read_section (objfile, sect);
14301
14302 /* We have the section offset, but we need the signature to do the
14303 hash table lookup. */
14304 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14305
14306 gdb_assert (type_sig->per_cu.cu == NULL);
14307
14308 read_signatured_type (objfile, type_sig);
14309
14310 gdb_assert (type_sig->per_cu.cu != NULL);
14311 }
14312
14313 /* Read in a signatured type and build its CU and DIEs. */
14314
14315 static void
14316 read_signatured_type (struct objfile *objfile,
14317 struct signatured_type *type_sig)
14318 {
14319 gdb_byte *types_ptr;
14320 struct die_reader_specs reader_specs;
14321 struct dwarf2_cu *cu;
14322 ULONGEST signature;
14323 struct cleanup *back_to, *free_cu_cleanup;
14324 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
14325
14326 dwarf2_read_section (objfile, section);
14327 types_ptr = section->buffer + type_sig->per_cu.offset;
14328
14329 gdb_assert (type_sig->per_cu.cu == NULL);
14330
14331 cu = xmalloc (sizeof (*cu));
14332 init_one_comp_unit (cu, objfile);
14333
14334 type_sig->per_cu.cu = cu;
14335 cu->per_cu = &type_sig->per_cu;
14336
14337 /* If an error occurs while loading, release our storage. */
14338 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14339
14340 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14341 types_ptr, objfile->obfd);
14342 gdb_assert (signature == type_sig->signature);
14343
14344 cu->die_hash
14345 = htab_create_alloc_ex (cu->header.length / 12,
14346 die_hash,
14347 die_eq,
14348 NULL,
14349 &cu->comp_unit_obstack,
14350 hashtab_obstack_allocate,
14351 dummy_obstack_deallocate);
14352
14353 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14354 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14355
14356 init_cu_die_reader (&reader_specs, cu);
14357
14358 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14359 NULL /*parent*/);
14360
14361 /* We try not to read any attributes in this function, because not
14362 all objfiles needed for references have been loaded yet, and symbol
14363 table processing isn't initialized. But we have to set the CU language,
14364 or we won't be able to build types correctly. */
14365 prepare_one_comp_unit (cu, cu->dies);
14366
14367 do_cleanups (back_to);
14368
14369 /* We've successfully allocated this compilation unit. Let our caller
14370 clean it up when finished with it. */
14371 discard_cleanups (free_cu_cleanup);
14372
14373 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14374 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14375 }
14376
14377 /* Workaround as dwarf_expr_context_funcs.read_mem implementation before
14378 a proper runtime DWARF expressions evaluator gets implemented.
14379 Otherwise gnuv3_baseclass_offset would error by:
14380 Expected a negative vbase offset (old compiler?) */
14381
14382 static void
14383 decode_locdesc_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr,
14384 size_t length)
14385 {
14386 struct dwarf_expr_context *ctx = baton;
14387 struct gdbarch *gdbarch = ctx->gdbarch;
14388 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
14389
14390 memset (buf, 0, length);
14391
14392 if (TYPE_LENGTH (ptr_type) == length)
14393 store_typed_address (buf, ptr_type, addr);
14394 }
14395
14396 static const struct dwarf_expr_context_funcs decode_locdesc_ctx_funcs =
14397 {
14398 ctx_no_read_reg,
14399 decode_locdesc_read_mem,
14400 ctx_no_get_frame_base,
14401 ctx_no_get_frame_cfa,
14402 ctx_no_get_frame_pc,
14403 ctx_no_get_tls_address,
14404 ctx_no_dwarf_call,
14405 ctx_no_get_base_type
14406 };
14407
14408 /* Decode simple location descriptions.
14409 Given a pointer to a dwarf block that defines a location, compute
14410 the location and return the value.
14411
14412 NOTE drow/2003-11-18: This function is called in two situations
14413 now: for the address of static or global variables (partial symbols
14414 only) and for offsets into structures which are expected to be
14415 (more or less) constant. The partial symbol case should go away,
14416 and only the constant case should remain. That will let this
14417 function complain more accurately. A few special modes are allowed
14418 without complaint for global variables (for instance, global
14419 register values and thread-local values).
14420
14421 A location description containing no operations indicates that the
14422 object is optimized out. The return value is 0 for that case.
14423 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14424 callers will only want a very basic result and this can become a
14425 complaint. */
14426
14427 static CORE_ADDR
14428 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14429 {
14430 struct objfile *objfile = cu->objfile;
14431 struct dwarf_expr_context *ctx;
14432 struct cleanup *old_chain;
14433 volatile struct gdb_exception ex;
14434
14435 ctx = new_dwarf_expr_context ();
14436 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
14437 make_cleanup_value_free_to_mark (value_mark ());
14438
14439 ctx->gdbarch = get_objfile_arch (objfile);
14440 ctx->addr_size = cu->header.addr_size;
14441 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (cu->per_cu);
14442 ctx->offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14443 ctx->baton = ctx;
14444 ctx->funcs = &decode_locdesc_ctx_funcs;
14445
14446 /* DW_AT_data_member_location expects the structure address to be pushed on
14447 the stack. Simulate the offset by address 0. */
14448 dwarf_expr_push_address (ctx, 0, 0);
14449
14450 TRY_CATCH (ex, RETURN_MASK_ERROR)
14451 {
14452 dwarf_expr_eval (ctx, blk->data, blk->size);
14453 }
14454 if (ex.reason < 0)
14455 {
14456 if (ex.message)
14457 complaint (&symfile_complaints, "%s", ex.message);
14458 }
14459 else if (ctx->num_pieces == 0)
14460 switch (ctx->location)
14461 {
14462 /* The returned number will be bogus, just do not complain for locations
14463 in global registers - it is here only a partial symbol address. */
14464 case DWARF_VALUE_REGISTER:
14465
14466 case DWARF_VALUE_MEMORY:
14467 case DWARF_VALUE_STACK:
14468 {
14469 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
14470
14471 do_cleanups (old_chain);
14472 return address;
14473 }
14474 }
14475
14476 do_cleanups (old_chain);
14477 dwarf2_complex_location_expr_complaint ();
14478 return 0;
14479 }
14480
14481 /* memory allocation interface */
14482
14483 static struct dwarf_block *
14484 dwarf_alloc_block (struct dwarf2_cu *cu)
14485 {
14486 struct dwarf_block *blk;
14487
14488 blk = (struct dwarf_block *)
14489 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14490 return (blk);
14491 }
14492
14493 static struct abbrev_info *
14494 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14495 {
14496 struct abbrev_info *abbrev;
14497
14498 abbrev = (struct abbrev_info *)
14499 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14500 memset (abbrev, 0, sizeof (struct abbrev_info));
14501 return (abbrev);
14502 }
14503
14504 static struct die_info *
14505 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14506 {
14507 struct die_info *die;
14508 size_t size = sizeof (struct die_info);
14509
14510 if (num_attrs > 1)
14511 size += (num_attrs - 1) * sizeof (struct attribute);
14512
14513 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14514 memset (die, 0, sizeof (struct die_info));
14515 return (die);
14516 }
14517
14518 \f
14519 /* Macro support. */
14520
14521 /* Return the full name of file number I in *LH's file name table.
14522 Use COMP_DIR as the name of the current directory of the
14523 compilation. The result is allocated using xmalloc; the caller is
14524 responsible for freeing it. */
14525 static char *
14526 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14527 {
14528 /* Is the file number a valid index into the line header's file name
14529 table? Remember that file numbers start with one, not zero. */
14530 if (1 <= file && file <= lh->num_file_names)
14531 {
14532 struct file_entry *fe = &lh->file_names[file - 1];
14533
14534 if (IS_ABSOLUTE_PATH (fe->name))
14535 return xstrdup (fe->name);
14536 else
14537 {
14538 const char *dir;
14539 int dir_len;
14540 char *full_name;
14541
14542 if (fe->dir_index)
14543 dir = lh->include_dirs[fe->dir_index - 1];
14544 else
14545 dir = comp_dir;
14546
14547 if (dir)
14548 {
14549 dir_len = strlen (dir);
14550 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14551 strcpy (full_name, dir);
14552 full_name[dir_len] = '/';
14553 strcpy (full_name + dir_len + 1, fe->name);
14554 return full_name;
14555 }
14556 else
14557 return xstrdup (fe->name);
14558 }
14559 }
14560 else
14561 {
14562 /* The compiler produced a bogus file number. We can at least
14563 record the macro definitions made in the file, even if we
14564 won't be able to find the file by name. */
14565 char fake_name[80];
14566
14567 sprintf (fake_name, "<bad macro file number %d>", file);
14568
14569 complaint (&symfile_complaints,
14570 _("bad file number in macro information (%d)"),
14571 file);
14572
14573 return xstrdup (fake_name);
14574 }
14575 }
14576
14577
14578 static struct macro_source_file *
14579 macro_start_file (int file, int line,
14580 struct macro_source_file *current_file,
14581 const char *comp_dir,
14582 struct line_header *lh, struct objfile *objfile)
14583 {
14584 /* The full name of this source file. */
14585 char *full_name = file_full_name (file, lh, comp_dir);
14586
14587 /* We don't create a macro table for this compilation unit
14588 at all until we actually get a filename. */
14589 if (! pending_macros)
14590 pending_macros = new_macro_table (&objfile->objfile_obstack,
14591 objfile->macro_cache);
14592
14593 if (! current_file)
14594 /* If we have no current file, then this must be the start_file
14595 directive for the compilation unit's main source file. */
14596 current_file = macro_set_main (pending_macros, full_name);
14597 else
14598 current_file = macro_include (current_file, line, full_name);
14599
14600 xfree (full_name);
14601
14602 return current_file;
14603 }
14604
14605
14606 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14607 followed by a null byte. */
14608 static char *
14609 copy_string (const char *buf, int len)
14610 {
14611 char *s = xmalloc (len + 1);
14612
14613 memcpy (s, buf, len);
14614 s[len] = '\0';
14615 return s;
14616 }
14617
14618
14619 static const char *
14620 consume_improper_spaces (const char *p, const char *body)
14621 {
14622 if (*p == ' ')
14623 {
14624 complaint (&symfile_complaints,
14625 _("macro definition contains spaces "
14626 "in formal argument list:\n`%s'"),
14627 body);
14628
14629 while (*p == ' ')
14630 p++;
14631 }
14632
14633 return p;
14634 }
14635
14636
14637 static void
14638 parse_macro_definition (struct macro_source_file *file, int line,
14639 const char *body)
14640 {
14641 const char *p;
14642
14643 /* The body string takes one of two forms. For object-like macro
14644 definitions, it should be:
14645
14646 <macro name> " " <definition>
14647
14648 For function-like macro definitions, it should be:
14649
14650 <macro name> "() " <definition>
14651 or
14652 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14653
14654 Spaces may appear only where explicitly indicated, and in the
14655 <definition>.
14656
14657 The Dwarf 2 spec says that an object-like macro's name is always
14658 followed by a space, but versions of GCC around March 2002 omit
14659 the space when the macro's definition is the empty string.
14660
14661 The Dwarf 2 spec says that there should be no spaces between the
14662 formal arguments in a function-like macro's formal argument list,
14663 but versions of GCC around March 2002 include spaces after the
14664 commas. */
14665
14666
14667 /* Find the extent of the macro name. The macro name is terminated
14668 by either a space or null character (for an object-like macro) or
14669 an opening paren (for a function-like macro). */
14670 for (p = body; *p; p++)
14671 if (*p == ' ' || *p == '(')
14672 break;
14673
14674 if (*p == ' ' || *p == '\0')
14675 {
14676 /* It's an object-like macro. */
14677 int name_len = p - body;
14678 char *name = copy_string (body, name_len);
14679 const char *replacement;
14680
14681 if (*p == ' ')
14682 replacement = body + name_len + 1;
14683 else
14684 {
14685 dwarf2_macro_malformed_definition_complaint (body);
14686 replacement = body + name_len;
14687 }
14688
14689 macro_define_object (file, line, name, replacement);
14690
14691 xfree (name);
14692 }
14693 else if (*p == '(')
14694 {
14695 /* It's a function-like macro. */
14696 char *name = copy_string (body, p - body);
14697 int argc = 0;
14698 int argv_size = 1;
14699 char **argv = xmalloc (argv_size * sizeof (*argv));
14700
14701 p++;
14702
14703 p = consume_improper_spaces (p, body);
14704
14705 /* Parse the formal argument list. */
14706 while (*p && *p != ')')
14707 {
14708 /* Find the extent of the current argument name. */
14709 const char *arg_start = p;
14710
14711 while (*p && *p != ',' && *p != ')' && *p != ' ')
14712 p++;
14713
14714 if (! *p || p == arg_start)
14715 dwarf2_macro_malformed_definition_complaint (body);
14716 else
14717 {
14718 /* Make sure argv has room for the new argument. */
14719 if (argc >= argv_size)
14720 {
14721 argv_size *= 2;
14722 argv = xrealloc (argv, argv_size * sizeof (*argv));
14723 }
14724
14725 argv[argc++] = copy_string (arg_start, p - arg_start);
14726 }
14727
14728 p = consume_improper_spaces (p, body);
14729
14730 /* Consume the comma, if present. */
14731 if (*p == ',')
14732 {
14733 p++;
14734
14735 p = consume_improper_spaces (p, body);
14736 }
14737 }
14738
14739 if (*p == ')')
14740 {
14741 p++;
14742
14743 if (*p == ' ')
14744 /* Perfectly formed definition, no complaints. */
14745 macro_define_function (file, line, name,
14746 argc, (const char **) argv,
14747 p + 1);
14748 else if (*p == '\0')
14749 {
14750 /* Complain, but do define it. */
14751 dwarf2_macro_malformed_definition_complaint (body);
14752 macro_define_function (file, line, name,
14753 argc, (const char **) argv,
14754 p);
14755 }
14756 else
14757 /* Just complain. */
14758 dwarf2_macro_malformed_definition_complaint (body);
14759 }
14760 else
14761 /* Just complain. */
14762 dwarf2_macro_malformed_definition_complaint (body);
14763
14764 xfree (name);
14765 {
14766 int i;
14767
14768 for (i = 0; i < argc; i++)
14769 xfree (argv[i]);
14770 }
14771 xfree (argv);
14772 }
14773 else
14774 dwarf2_macro_malformed_definition_complaint (body);
14775 }
14776
14777 /* Skip some bytes from BYTES according to the form given in FORM.
14778 Returns the new pointer. */
14779
14780 static gdb_byte *
14781 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14782 enum dwarf_form form,
14783 unsigned int offset_size,
14784 struct dwarf2_section_info *section)
14785 {
14786 unsigned int bytes_read;
14787
14788 switch (form)
14789 {
14790 case DW_FORM_data1:
14791 case DW_FORM_flag:
14792 ++bytes;
14793 break;
14794
14795 case DW_FORM_data2:
14796 bytes += 2;
14797 break;
14798
14799 case DW_FORM_data4:
14800 bytes += 4;
14801 break;
14802
14803 case DW_FORM_data8:
14804 bytes += 8;
14805 break;
14806
14807 case DW_FORM_string:
14808 read_direct_string (abfd, bytes, &bytes_read);
14809 bytes += bytes_read;
14810 break;
14811
14812 case DW_FORM_sec_offset:
14813 case DW_FORM_strp:
14814 bytes += offset_size;
14815 break;
14816
14817 case DW_FORM_block:
14818 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14819 bytes += bytes_read;
14820 break;
14821
14822 case DW_FORM_block1:
14823 bytes += 1 + read_1_byte (abfd, bytes);
14824 break;
14825 case DW_FORM_block2:
14826 bytes += 2 + read_2_bytes (abfd, bytes);
14827 break;
14828 case DW_FORM_block4:
14829 bytes += 4 + read_4_bytes (abfd, bytes);
14830 break;
14831
14832 case DW_FORM_sdata:
14833 case DW_FORM_udata:
14834 bytes = skip_leb128 (abfd, bytes);
14835 break;
14836
14837 default:
14838 {
14839 complain:
14840 complaint (&symfile_complaints,
14841 _("invalid form 0x%x in `%s'"),
14842 form,
14843 section->asection->name);
14844 return NULL;
14845 }
14846 }
14847
14848 return bytes;
14849 }
14850
14851 /* A helper for dwarf_decode_macros that handles skipping an unknown
14852 opcode. Returns an updated pointer to the macro data buffer; or,
14853 on error, issues a complaint and returns NULL. */
14854
14855 static gdb_byte *
14856 skip_unknown_opcode (unsigned int opcode,
14857 gdb_byte **opcode_definitions,
14858 gdb_byte *mac_ptr,
14859 bfd *abfd,
14860 unsigned int offset_size,
14861 struct dwarf2_section_info *section)
14862 {
14863 unsigned int bytes_read, i;
14864 unsigned long arg;
14865 gdb_byte *defn;
14866
14867 if (opcode_definitions[opcode] == NULL)
14868 {
14869 complaint (&symfile_complaints,
14870 _("unrecognized DW_MACFINO opcode 0x%x"),
14871 opcode);
14872 return NULL;
14873 }
14874
14875 defn = opcode_definitions[opcode];
14876 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
14877 defn += bytes_read;
14878
14879 for (i = 0; i < arg; ++i)
14880 {
14881 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
14882 if (mac_ptr == NULL)
14883 {
14884 /* skip_form_bytes already issued the complaint. */
14885 return NULL;
14886 }
14887 }
14888
14889 return mac_ptr;
14890 }
14891
14892 /* A helper function which parses the header of a macro section.
14893 If the macro section is the extended (for now called "GNU") type,
14894 then this updates *OFFSET_SIZE. Returns a pointer to just after
14895 the header, or issues a complaint and returns NULL on error. */
14896
14897 static gdb_byte *
14898 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
14899 bfd *abfd,
14900 gdb_byte *mac_ptr,
14901 unsigned int *offset_size,
14902 int section_is_gnu)
14903 {
14904 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
14905
14906 if (section_is_gnu)
14907 {
14908 unsigned int version, flags;
14909
14910 version = read_2_bytes (abfd, mac_ptr);
14911 if (version != 4)
14912 {
14913 complaint (&symfile_complaints,
14914 _("unrecognized version `%d' in .debug_macro section"),
14915 version);
14916 return NULL;
14917 }
14918 mac_ptr += 2;
14919
14920 flags = read_1_byte (abfd, mac_ptr);
14921 ++mac_ptr;
14922 *offset_size = (flags & 1) ? 8 : 4;
14923
14924 if ((flags & 2) != 0)
14925 /* We don't need the line table offset. */
14926 mac_ptr += *offset_size;
14927
14928 /* Vendor opcode descriptions. */
14929 if ((flags & 4) != 0)
14930 {
14931 unsigned int i, count;
14932
14933 count = read_1_byte (abfd, mac_ptr);
14934 ++mac_ptr;
14935 for (i = 0; i < count; ++i)
14936 {
14937 unsigned int opcode, bytes_read;
14938 unsigned long arg;
14939
14940 opcode = read_1_byte (abfd, mac_ptr);
14941 ++mac_ptr;
14942 opcode_definitions[opcode] = mac_ptr;
14943 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14944 mac_ptr += bytes_read;
14945 mac_ptr += arg;
14946 }
14947 }
14948 }
14949
14950 return mac_ptr;
14951 }
14952
14953 /* A helper for dwarf_decode_macros that handles the GNU extensions,
14954 including DW_GNU_MACINFO_transparent_include. */
14955
14956 static void
14957 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
14958 struct macro_source_file *current_file,
14959 struct line_header *lh, char *comp_dir,
14960 struct dwarf2_section_info *section,
14961 int section_is_gnu,
14962 unsigned int offset_size,
14963 struct objfile *objfile)
14964 {
14965 enum dwarf_macro_record_type macinfo_type;
14966 int at_commandline;
14967 gdb_byte *opcode_definitions[256];
14968
14969 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
14970 &offset_size, section_is_gnu);
14971 if (mac_ptr == NULL)
14972 {
14973 /* We already issued a complaint. */
14974 return;
14975 }
14976
14977 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14978 GDB is still reading the definitions from command line. First
14979 DW_MACINFO_start_file will need to be ignored as it was already executed
14980 to create CURRENT_FILE for the main source holding also the command line
14981 definitions. On first met DW_MACINFO_start_file this flag is reset to
14982 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14983
14984 at_commandline = 1;
14985
14986 do
14987 {
14988 /* Do we at least have room for a macinfo type byte? */
14989 if (mac_ptr >= mac_end)
14990 {
14991 dwarf2_macros_too_long_complaint (section);
14992 break;
14993 }
14994
14995 macinfo_type = read_1_byte (abfd, mac_ptr);
14996 mac_ptr++;
14997
14998 /* Note that we rely on the fact that the corresponding GNU and
14999 DWARF constants are the same. */
15000 switch (macinfo_type)
15001 {
15002 /* A zero macinfo type indicates the end of the macro
15003 information. */
15004 case 0:
15005 break;
15006
15007 case DW_MACRO_GNU_define:
15008 case DW_MACRO_GNU_undef:
15009 case DW_MACRO_GNU_define_indirect:
15010 case DW_MACRO_GNU_undef_indirect:
15011 {
15012 unsigned int bytes_read;
15013 int line;
15014 char *body;
15015 int is_define;
15016
15017 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15018 mac_ptr += bytes_read;
15019
15020 if (macinfo_type == DW_MACRO_GNU_define
15021 || macinfo_type == DW_MACRO_GNU_undef)
15022 {
15023 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15024 mac_ptr += bytes_read;
15025 }
15026 else
15027 {
15028 LONGEST str_offset;
15029
15030 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15031 mac_ptr += offset_size;
15032
15033 body = read_indirect_string_at_offset (abfd, str_offset);
15034 }
15035
15036 is_define = (macinfo_type == DW_MACRO_GNU_define
15037 || macinfo_type == DW_MACRO_GNU_define_indirect);
15038 if (! current_file)
15039 {
15040 /* DWARF violation as no main source is present. */
15041 complaint (&symfile_complaints,
15042 _("debug info with no main source gives macro %s "
15043 "on line %d: %s"),
15044 is_define ? _("definition") : _("undefinition"),
15045 line, body);
15046 break;
15047 }
15048 if ((line == 0 && !at_commandline)
15049 || (line != 0 && at_commandline))
15050 complaint (&symfile_complaints,
15051 _("debug info gives %s macro %s with %s line %d: %s"),
15052 at_commandline ? _("command-line") : _("in-file"),
15053 is_define ? _("definition") : _("undefinition"),
15054 line == 0 ? _("zero") : _("non-zero"), line, body);
15055
15056 if (is_define)
15057 parse_macro_definition (current_file, line, body);
15058 else
15059 {
15060 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15061 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15062 macro_undef (current_file, line, body);
15063 }
15064 }
15065 break;
15066
15067 case DW_MACRO_GNU_start_file:
15068 {
15069 unsigned int bytes_read;
15070 int line, file;
15071
15072 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15073 mac_ptr += bytes_read;
15074 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15075 mac_ptr += bytes_read;
15076
15077 if ((line == 0 && !at_commandline)
15078 || (line != 0 && at_commandline))
15079 complaint (&symfile_complaints,
15080 _("debug info gives source %d included "
15081 "from %s at %s line %d"),
15082 file, at_commandline ? _("command-line") : _("file"),
15083 line == 0 ? _("zero") : _("non-zero"), line);
15084
15085 if (at_commandline)
15086 {
15087 /* This DW_MACRO_GNU_start_file was executed in the
15088 pass one. */
15089 at_commandline = 0;
15090 }
15091 else
15092 current_file = macro_start_file (file, line,
15093 current_file, comp_dir,
15094 lh, objfile);
15095 }
15096 break;
15097
15098 case DW_MACRO_GNU_end_file:
15099 if (! current_file)
15100 complaint (&symfile_complaints,
15101 _("macro debug info has an unmatched "
15102 "`close_file' directive"));
15103 else
15104 {
15105 current_file = current_file->included_by;
15106 if (! current_file)
15107 {
15108 enum dwarf_macro_record_type next_type;
15109
15110 /* GCC circa March 2002 doesn't produce the zero
15111 type byte marking the end of the compilation
15112 unit. Complain if it's not there, but exit no
15113 matter what. */
15114
15115 /* Do we at least have room for a macinfo type byte? */
15116 if (mac_ptr >= mac_end)
15117 {
15118 dwarf2_macros_too_long_complaint (section);
15119 return;
15120 }
15121
15122 /* We don't increment mac_ptr here, so this is just
15123 a look-ahead. */
15124 next_type = read_1_byte (abfd, mac_ptr);
15125 if (next_type != 0)
15126 complaint (&symfile_complaints,
15127 _("no terminating 0-type entry for "
15128 "macros in `.debug_macinfo' section"));
15129
15130 return;
15131 }
15132 }
15133 break;
15134
15135 case DW_MACRO_GNU_transparent_include:
15136 {
15137 LONGEST offset;
15138
15139 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15140 mac_ptr += offset_size;
15141
15142 dwarf_decode_macro_bytes (abfd,
15143 section->buffer + offset,
15144 mac_end, current_file,
15145 lh, comp_dir,
15146 section, section_is_gnu,
15147 offset_size, objfile);
15148 }
15149 break;
15150
15151 case DW_MACINFO_vendor_ext:
15152 if (!section_is_gnu)
15153 {
15154 unsigned int bytes_read;
15155 int constant;
15156
15157 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15158 mac_ptr += bytes_read;
15159 read_direct_string (abfd, mac_ptr, &bytes_read);
15160 mac_ptr += bytes_read;
15161
15162 /* We don't recognize any vendor extensions. */
15163 break;
15164 }
15165 /* FALLTHROUGH */
15166
15167 default:
15168 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15169 mac_ptr, abfd, offset_size,
15170 section);
15171 if (mac_ptr == NULL)
15172 return;
15173 break;
15174 }
15175 } while (macinfo_type != 0);
15176 }
15177
15178 static void
15179 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15180 char *comp_dir, bfd *abfd,
15181 struct dwarf2_cu *cu,
15182 struct dwarf2_section_info *section,
15183 int section_is_gnu)
15184 {
15185 gdb_byte *mac_ptr, *mac_end;
15186 struct macro_source_file *current_file = 0;
15187 enum dwarf_macro_record_type macinfo_type;
15188 unsigned int offset_size = cu->header.offset_size;
15189 gdb_byte *opcode_definitions[256];
15190
15191 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15192 if (section->buffer == NULL)
15193 {
15194 complaint (&symfile_complaints, _("missing %s section"),
15195 section->asection->name);
15196 return;
15197 }
15198
15199 /* First pass: Find the name of the base filename.
15200 This filename is needed in order to process all macros whose definition
15201 (or undefinition) comes from the command line. These macros are defined
15202 before the first DW_MACINFO_start_file entry, and yet still need to be
15203 associated to the base file.
15204
15205 To determine the base file name, we scan the macro definitions until we
15206 reach the first DW_MACINFO_start_file entry. We then initialize
15207 CURRENT_FILE accordingly so that any macro definition found before the
15208 first DW_MACINFO_start_file can still be associated to the base file. */
15209
15210 mac_ptr = section->buffer + offset;
15211 mac_end = section->buffer + section->size;
15212
15213 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15214 &offset_size, section_is_gnu);
15215 if (mac_ptr == NULL)
15216 {
15217 /* We already issued a complaint. */
15218 return;
15219 }
15220
15221 do
15222 {
15223 /* Do we at least have room for a macinfo type byte? */
15224 if (mac_ptr >= mac_end)
15225 {
15226 /* Complaint is printed during the second pass as GDB will probably
15227 stop the first pass earlier upon finding
15228 DW_MACINFO_start_file. */
15229 break;
15230 }
15231
15232 macinfo_type = read_1_byte (abfd, mac_ptr);
15233 mac_ptr++;
15234
15235 /* Note that we rely on the fact that the corresponding GNU and
15236 DWARF constants are the same. */
15237 switch (macinfo_type)
15238 {
15239 /* A zero macinfo type indicates the end of the macro
15240 information. */
15241 case 0:
15242 break;
15243
15244 case DW_MACRO_GNU_define:
15245 case DW_MACRO_GNU_undef:
15246 /* Only skip the data by MAC_PTR. */
15247 {
15248 unsigned int bytes_read;
15249
15250 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15251 mac_ptr += bytes_read;
15252 read_direct_string (abfd, mac_ptr, &bytes_read);
15253 mac_ptr += bytes_read;
15254 }
15255 break;
15256
15257 case DW_MACRO_GNU_start_file:
15258 {
15259 unsigned int bytes_read;
15260 int line, file;
15261
15262 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15263 mac_ptr += bytes_read;
15264 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15265 mac_ptr += bytes_read;
15266
15267 current_file = macro_start_file (file, line, current_file,
15268 comp_dir, lh, cu->objfile);
15269 }
15270 break;
15271
15272 case DW_MACRO_GNU_end_file:
15273 /* No data to skip by MAC_PTR. */
15274 break;
15275
15276 case DW_MACRO_GNU_define_indirect:
15277 case DW_MACRO_GNU_undef_indirect:
15278 {
15279 unsigned int bytes_read;
15280
15281 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15282 mac_ptr += bytes_read;
15283 mac_ptr += offset_size;
15284 }
15285 break;
15286
15287 case DW_MACRO_GNU_transparent_include:
15288 /* Note that, according to the spec, a transparent include
15289 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15290 skip this opcode. */
15291 mac_ptr += offset_size;
15292 break;
15293
15294 case DW_MACINFO_vendor_ext:
15295 /* Only skip the data by MAC_PTR. */
15296 if (!section_is_gnu)
15297 {
15298 unsigned int bytes_read;
15299
15300 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15301 mac_ptr += bytes_read;
15302 read_direct_string (abfd, mac_ptr, &bytes_read);
15303 mac_ptr += bytes_read;
15304 }
15305 /* FALLTHROUGH */
15306
15307 default:
15308 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15309 mac_ptr, abfd, offset_size,
15310 section);
15311 if (mac_ptr == NULL)
15312 return;
15313 break;
15314 }
15315 } while (macinfo_type != 0 && current_file == NULL);
15316
15317 /* Second pass: Process all entries.
15318
15319 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15320 command-line macro definitions/undefinitions. This flag is unset when we
15321 reach the first DW_MACINFO_start_file entry. */
15322
15323 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15324 current_file, lh, comp_dir, section, section_is_gnu,
15325 offset_size, cu->objfile);
15326 }
15327
15328 /* Check if the attribute's form is a DW_FORM_block*
15329 if so return true else false. */
15330 static int
15331 attr_form_is_block (struct attribute *attr)
15332 {
15333 return (attr == NULL ? 0 :
15334 attr->form == DW_FORM_block1
15335 || attr->form == DW_FORM_block2
15336 || attr->form == DW_FORM_block4
15337 || attr->form == DW_FORM_block
15338 || attr->form == DW_FORM_exprloc);
15339 }
15340
15341 /* Return non-zero if ATTR's value is a section offset --- classes
15342 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15343 You may use DW_UNSND (attr) to retrieve such offsets.
15344
15345 Section 7.5.4, "Attribute Encodings", explains that no attribute
15346 may have a value that belongs to more than one of these classes; it
15347 would be ambiguous if we did, because we use the same forms for all
15348 of them. */
15349 static int
15350 attr_form_is_section_offset (struct attribute *attr)
15351 {
15352 return (attr->form == DW_FORM_data4
15353 || attr->form == DW_FORM_data8
15354 || attr->form == DW_FORM_sec_offset);
15355 }
15356
15357
15358 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15359 zero otherwise. When this function returns true, you can apply
15360 dwarf2_get_attr_constant_value to it.
15361
15362 However, note that for some attributes you must check
15363 attr_form_is_section_offset before using this test. DW_FORM_data4
15364 and DW_FORM_data8 are members of both the constant class, and of
15365 the classes that contain offsets into other debug sections
15366 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15367 that, if an attribute's can be either a constant or one of the
15368 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15369 taken as section offsets, not constants. */
15370 static int
15371 attr_form_is_constant (struct attribute *attr)
15372 {
15373 switch (attr->form)
15374 {
15375 case DW_FORM_sdata:
15376 case DW_FORM_udata:
15377 case DW_FORM_data1:
15378 case DW_FORM_data2:
15379 case DW_FORM_data4:
15380 case DW_FORM_data8:
15381 return 1;
15382 default:
15383 return 0;
15384 }
15385 }
15386
15387 /* A helper function that fills in a dwarf2_loclist_baton. */
15388
15389 static void
15390 fill_in_loclist_baton (struct dwarf2_cu *cu,
15391 struct dwarf2_loclist_baton *baton,
15392 struct attribute *attr)
15393 {
15394 dwarf2_read_section (dwarf2_per_objfile->objfile,
15395 &dwarf2_per_objfile->loc);
15396
15397 baton->per_cu = cu->per_cu;
15398 gdb_assert (baton->per_cu);
15399 /* We don't know how long the location list is, but make sure we
15400 don't run off the edge of the section. */
15401 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15402 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15403 baton->base_address = cu->base_address;
15404 }
15405
15406 static void
15407 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15408 struct dwarf2_cu *cu)
15409 {
15410 if (attr_form_is_section_offset (attr)
15411 /* ".debug_loc" may not exist at all, or the offset may be outside
15412 the section. If so, fall through to the complaint in the
15413 other branch. */
15414 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15415 &dwarf2_per_objfile->loc))
15416 {
15417 struct dwarf2_loclist_baton *baton;
15418
15419 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15420 sizeof (struct dwarf2_loclist_baton));
15421
15422 fill_in_loclist_baton (cu, baton, attr);
15423
15424 if (cu->base_known == 0)
15425 complaint (&symfile_complaints,
15426 _("Location list used without "
15427 "specifying the CU base address."));
15428
15429 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15430 SYMBOL_LOCATION_BATON (sym) = baton;
15431 }
15432 else
15433 {
15434 struct dwarf2_locexpr_baton *baton;
15435
15436 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15437 sizeof (struct dwarf2_locexpr_baton));
15438 baton->per_cu = cu->per_cu;
15439 gdb_assert (baton->per_cu);
15440
15441 if (attr_form_is_block (attr))
15442 {
15443 /* Note that we're just copying the block's data pointer
15444 here, not the actual data. We're still pointing into the
15445 info_buffer for SYM's objfile; right now we never release
15446 that buffer, but when we do clean up properly this may
15447 need to change. */
15448 baton->size = DW_BLOCK (attr)->size;
15449 baton->data = DW_BLOCK (attr)->data;
15450 }
15451 else
15452 {
15453 dwarf2_invalid_attrib_class_complaint ("location description",
15454 SYMBOL_NATURAL_NAME (sym));
15455 baton->size = 0;
15456 baton->data = NULL;
15457 }
15458
15459 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15460 SYMBOL_LOCATION_BATON (sym) = baton;
15461 }
15462 }
15463
15464 /* Return the OBJFILE associated with the compilation unit CU. If CU
15465 came from a separate debuginfo file, then the master objfile is
15466 returned. */
15467
15468 struct objfile *
15469 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15470 {
15471 struct objfile *objfile = per_cu->objfile;
15472
15473 /* Return the master objfile, so that we can report and look up the
15474 correct file containing this variable. */
15475 if (objfile->separate_debug_objfile_backlink)
15476 objfile = objfile->separate_debug_objfile_backlink;
15477
15478 return objfile;
15479 }
15480
15481 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15482 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15483 CU_HEADERP first. */
15484
15485 static const struct comp_unit_head *
15486 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15487 struct dwarf2_per_cu_data *per_cu)
15488 {
15489 struct objfile *objfile;
15490 struct dwarf2_per_objfile *per_objfile;
15491 gdb_byte *info_ptr;
15492
15493 if (per_cu->cu)
15494 return &per_cu->cu->header;
15495
15496 objfile = per_cu->objfile;
15497 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15498 info_ptr = per_objfile->info.buffer + per_cu->offset;
15499
15500 memset (cu_headerp, 0, sizeof (*cu_headerp));
15501 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15502
15503 return cu_headerp;
15504 }
15505
15506 /* Return the address size given in the compilation unit header for CU. */
15507
15508 CORE_ADDR
15509 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15510 {
15511 struct comp_unit_head cu_header_local;
15512 const struct comp_unit_head *cu_headerp;
15513
15514 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15515
15516 return cu_headerp->addr_size;
15517 }
15518
15519 /* Return the offset size given in the compilation unit header for CU. */
15520
15521 int
15522 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15523 {
15524 struct comp_unit_head cu_header_local;
15525 const struct comp_unit_head *cu_headerp;
15526
15527 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15528
15529 return cu_headerp->offset_size;
15530 }
15531
15532 /* See its dwarf2loc.h declaration. */
15533
15534 int
15535 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15536 {
15537 struct comp_unit_head cu_header_local;
15538 const struct comp_unit_head *cu_headerp;
15539
15540 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15541
15542 if (cu_headerp->version == 2)
15543 return cu_headerp->addr_size;
15544 else
15545 return cu_headerp->offset_size;
15546 }
15547
15548 /* Return the text offset of the CU. The returned offset comes from
15549 this CU's objfile. If this objfile came from a separate debuginfo
15550 file, then the offset may be different from the corresponding
15551 offset in the parent objfile. */
15552
15553 CORE_ADDR
15554 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15555 {
15556 struct objfile *objfile = per_cu->objfile;
15557
15558 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15559 }
15560
15561 /* Locate the .debug_info compilation unit from CU's objfile which contains
15562 the DIE at OFFSET. Raises an error on failure. */
15563
15564 static struct dwarf2_per_cu_data *
15565 dwarf2_find_containing_comp_unit (unsigned int offset,
15566 struct objfile *objfile)
15567 {
15568 struct dwarf2_per_cu_data *this_cu;
15569 int low, high;
15570
15571 low = 0;
15572 high = dwarf2_per_objfile->n_comp_units - 1;
15573 while (high > low)
15574 {
15575 int mid = low + (high - low) / 2;
15576
15577 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15578 high = mid;
15579 else
15580 low = mid + 1;
15581 }
15582 gdb_assert (low == high);
15583 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15584 {
15585 if (low == 0)
15586 error (_("Dwarf Error: could not find partial DIE containing "
15587 "offset 0x%lx [in module %s]"),
15588 (long) offset, bfd_get_filename (objfile->obfd));
15589
15590 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15591 return dwarf2_per_objfile->all_comp_units[low-1];
15592 }
15593 else
15594 {
15595 this_cu = dwarf2_per_objfile->all_comp_units[low];
15596 if (low == dwarf2_per_objfile->n_comp_units - 1
15597 && offset >= this_cu->offset + this_cu->length)
15598 error (_("invalid dwarf2 offset %u"), offset);
15599 gdb_assert (offset < this_cu->offset + this_cu->length);
15600 return this_cu;
15601 }
15602 }
15603
15604 /* Locate the compilation unit from OBJFILE which is located at exactly
15605 OFFSET. Raises an error on failure. */
15606
15607 static struct dwarf2_per_cu_data *
15608 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15609 {
15610 struct dwarf2_per_cu_data *this_cu;
15611
15612 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15613 if (this_cu->offset != offset)
15614 error (_("no compilation unit with offset %u."), offset);
15615 return this_cu;
15616 }
15617
15618 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
15619
15620 static void
15621 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15622 {
15623 memset (cu, 0, sizeof (*cu));
15624 cu->objfile = objfile;
15625 obstack_init (&cu->comp_unit_obstack);
15626 }
15627
15628 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15629
15630 static void
15631 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15632 {
15633 struct attribute *attr;
15634
15635 /* Set the language we're debugging. */
15636 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15637 if (attr)
15638 set_cu_language (DW_UNSND (attr), cu);
15639 else
15640 {
15641 cu->language = language_minimal;
15642 cu->language_defn = language_def (cu->language);
15643 }
15644 }
15645
15646 /* Release one cached compilation unit, CU. We unlink it from the tree
15647 of compilation units, but we don't remove it from the read_in_chain;
15648 the caller is responsible for that.
15649 NOTE: DATA is a void * because this function is also used as a
15650 cleanup routine. */
15651
15652 static void
15653 free_one_comp_unit (void *data)
15654 {
15655 struct dwarf2_cu *cu = data;
15656
15657 if (cu->per_cu != NULL)
15658 cu->per_cu->cu = NULL;
15659 cu->per_cu = NULL;
15660
15661 obstack_free (&cu->comp_unit_obstack, NULL);
15662
15663 xfree (cu);
15664 }
15665
15666 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15667 when we're finished with it. We can't free the pointer itself, but be
15668 sure to unlink it from the cache. Also release any associated storage
15669 and perform cache maintenance.
15670
15671 Only used during partial symbol parsing. */
15672
15673 static void
15674 free_stack_comp_unit (void *data)
15675 {
15676 struct dwarf2_cu *cu = data;
15677
15678 obstack_free (&cu->comp_unit_obstack, NULL);
15679 cu->partial_dies = NULL;
15680
15681 if (cu->per_cu != NULL)
15682 {
15683 /* This compilation unit is on the stack in our caller, so we
15684 should not xfree it. Just unlink it. */
15685 cu->per_cu->cu = NULL;
15686 cu->per_cu = NULL;
15687
15688 /* If we had a per-cu pointer, then we may have other compilation
15689 units loaded, so age them now. */
15690 age_cached_comp_units ();
15691 }
15692 }
15693
15694 /* Free all cached compilation units. */
15695
15696 static void
15697 free_cached_comp_units (void *data)
15698 {
15699 struct dwarf2_per_cu_data *per_cu, **last_chain;
15700
15701 per_cu = dwarf2_per_objfile->read_in_chain;
15702 last_chain = &dwarf2_per_objfile->read_in_chain;
15703 while (per_cu != NULL)
15704 {
15705 struct dwarf2_per_cu_data *next_cu;
15706
15707 next_cu = per_cu->cu->read_in_chain;
15708
15709 free_one_comp_unit (per_cu->cu);
15710 *last_chain = next_cu;
15711
15712 per_cu = next_cu;
15713 }
15714 }
15715
15716 /* Increase the age counter on each cached compilation unit, and free
15717 any that are too old. */
15718
15719 static void
15720 age_cached_comp_units (void)
15721 {
15722 struct dwarf2_per_cu_data *per_cu, **last_chain;
15723
15724 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15725 per_cu = dwarf2_per_objfile->read_in_chain;
15726 while (per_cu != NULL)
15727 {
15728 per_cu->cu->last_used ++;
15729 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15730 dwarf2_mark (per_cu->cu);
15731 per_cu = per_cu->cu->read_in_chain;
15732 }
15733
15734 per_cu = dwarf2_per_objfile->read_in_chain;
15735 last_chain = &dwarf2_per_objfile->read_in_chain;
15736 while (per_cu != NULL)
15737 {
15738 struct dwarf2_per_cu_data *next_cu;
15739
15740 next_cu = per_cu->cu->read_in_chain;
15741
15742 if (!per_cu->cu->mark)
15743 {
15744 free_one_comp_unit (per_cu->cu);
15745 *last_chain = next_cu;
15746 }
15747 else
15748 last_chain = &per_cu->cu->read_in_chain;
15749
15750 per_cu = next_cu;
15751 }
15752 }
15753
15754 /* Remove a single compilation unit from the cache. */
15755
15756 static void
15757 free_one_cached_comp_unit (void *target_cu)
15758 {
15759 struct dwarf2_per_cu_data *per_cu, **last_chain;
15760
15761 per_cu = dwarf2_per_objfile->read_in_chain;
15762 last_chain = &dwarf2_per_objfile->read_in_chain;
15763 while (per_cu != NULL)
15764 {
15765 struct dwarf2_per_cu_data *next_cu;
15766
15767 next_cu = per_cu->cu->read_in_chain;
15768
15769 if (per_cu->cu == target_cu)
15770 {
15771 free_one_comp_unit (per_cu->cu);
15772 *last_chain = next_cu;
15773 break;
15774 }
15775 else
15776 last_chain = &per_cu->cu->read_in_chain;
15777
15778 per_cu = next_cu;
15779 }
15780 }
15781
15782 /* Release all extra memory associated with OBJFILE. */
15783
15784 void
15785 dwarf2_free_objfile (struct objfile *objfile)
15786 {
15787 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15788
15789 if (dwarf2_per_objfile == NULL)
15790 return;
15791
15792 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15793 free_cached_comp_units (NULL);
15794
15795 if (dwarf2_per_objfile->quick_file_names_table)
15796 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15797
15798 /* Everything else should be on the objfile obstack. */
15799 }
15800
15801 /* A pair of DIE offset and GDB type pointer. We store these
15802 in a hash table separate from the DIEs, and preserve them
15803 when the DIEs are flushed out of cache. */
15804
15805 struct dwarf2_offset_and_type
15806 {
15807 unsigned int offset;
15808 struct type *type;
15809 };
15810
15811 /* Hash function for a dwarf2_offset_and_type. */
15812
15813 static hashval_t
15814 offset_and_type_hash (const void *item)
15815 {
15816 const struct dwarf2_offset_and_type *ofs = item;
15817
15818 return ofs->offset;
15819 }
15820
15821 /* Equality function for a dwarf2_offset_and_type. */
15822
15823 static int
15824 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15825 {
15826 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15827 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15828
15829 return ofs_lhs->offset == ofs_rhs->offset;
15830 }
15831
15832 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15833 table if necessary. For convenience, return TYPE.
15834
15835 The DIEs reading must have careful ordering to:
15836 * Not cause infite loops trying to read in DIEs as a prerequisite for
15837 reading current DIE.
15838 * Not trying to dereference contents of still incompletely read in types
15839 while reading in other DIEs.
15840 * Enable referencing still incompletely read in types just by a pointer to
15841 the type without accessing its fields.
15842
15843 Therefore caller should follow these rules:
15844 * Try to fetch any prerequisite types we may need to build this DIE type
15845 before building the type and calling set_die_type.
15846 * After building type call set_die_type for current DIE as soon as
15847 possible before fetching more types to complete the current type.
15848 * Make the type as complete as possible before fetching more types. */
15849
15850 static struct type *
15851 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15852 {
15853 struct dwarf2_offset_and_type **slot, ofs;
15854 struct objfile *objfile = cu->objfile;
15855 htab_t *type_hash_ptr;
15856
15857 /* For Ada types, make sure that the gnat-specific data is always
15858 initialized (if not already set). There are a few types where
15859 we should not be doing so, because the type-specific area is
15860 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15861 where the type-specific area is used to store the floatformat).
15862 But this is not a problem, because the gnat-specific information
15863 is actually not needed for these types. */
15864 if (need_gnat_info (cu)
15865 && TYPE_CODE (type) != TYPE_CODE_FUNC
15866 && TYPE_CODE (type) != TYPE_CODE_FLT
15867 && !HAVE_GNAT_AUX_INFO (type))
15868 INIT_GNAT_SPECIFIC (type);
15869
15870 if (cu->per_cu->debug_type_section)
15871 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15872 else
15873 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15874
15875 if (*type_hash_ptr == NULL)
15876 {
15877 *type_hash_ptr
15878 = htab_create_alloc_ex (127,
15879 offset_and_type_hash,
15880 offset_and_type_eq,
15881 NULL,
15882 &objfile->objfile_obstack,
15883 hashtab_obstack_allocate,
15884 dummy_obstack_deallocate);
15885 }
15886
15887 ofs.offset = die->offset;
15888 ofs.type = type;
15889 slot = (struct dwarf2_offset_and_type **)
15890 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15891 if (*slot)
15892 complaint (&symfile_complaints,
15893 _("A problem internal to GDB: DIE 0x%x has type already set"),
15894 die->offset);
15895 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15896 **slot = ofs;
15897 return type;
15898 }
15899
15900 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15901 table, or return NULL if the die does not have a saved type. */
15902
15903 static struct type *
15904 get_die_type_at_offset (unsigned int offset,
15905 struct dwarf2_per_cu_data *per_cu)
15906 {
15907 struct dwarf2_offset_and_type *slot, ofs;
15908 htab_t type_hash;
15909
15910 if (per_cu->debug_type_section)
15911 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15912 else
15913 type_hash = dwarf2_per_objfile->debug_info_type_hash;
15914 if (type_hash == NULL)
15915 return NULL;
15916
15917 ofs.offset = offset;
15918 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15919 if (slot)
15920 return slot->type;
15921 else
15922 return NULL;
15923 }
15924
15925 /* Look up the type for DIE in the appropriate type_hash table,
15926 or return NULL if DIE does not have a saved type. */
15927
15928 static struct type *
15929 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15930 {
15931 return get_die_type_at_offset (die->offset, cu->per_cu);
15932 }
15933
15934 /* Add a dependence relationship from CU to REF_PER_CU. */
15935
15936 static void
15937 dwarf2_add_dependence (struct dwarf2_cu *cu,
15938 struct dwarf2_per_cu_data *ref_per_cu)
15939 {
15940 void **slot;
15941
15942 if (cu->dependencies == NULL)
15943 cu->dependencies
15944 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15945 NULL, &cu->comp_unit_obstack,
15946 hashtab_obstack_allocate,
15947 dummy_obstack_deallocate);
15948
15949 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15950 if (*slot == NULL)
15951 *slot = ref_per_cu;
15952 }
15953
15954 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15955 Set the mark field in every compilation unit in the
15956 cache that we must keep because we are keeping CU. */
15957
15958 static int
15959 dwarf2_mark_helper (void **slot, void *data)
15960 {
15961 struct dwarf2_per_cu_data *per_cu;
15962
15963 per_cu = (struct dwarf2_per_cu_data *) *slot;
15964
15965 /* cu->dependencies references may not yet have been ever read if QUIT aborts
15966 reading of the chain. As such dependencies remain valid it is not much
15967 useful to track and undo them during QUIT cleanups. */
15968 if (per_cu->cu == NULL)
15969 return 1;
15970
15971 if (per_cu->cu->mark)
15972 return 1;
15973 per_cu->cu->mark = 1;
15974
15975 if (per_cu->cu->dependencies != NULL)
15976 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15977
15978 return 1;
15979 }
15980
15981 /* Set the mark field in CU and in every other compilation unit in the
15982 cache that we must keep because we are keeping CU. */
15983
15984 static void
15985 dwarf2_mark (struct dwarf2_cu *cu)
15986 {
15987 if (cu->mark)
15988 return;
15989 cu->mark = 1;
15990 if (cu->dependencies != NULL)
15991 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15992 }
15993
15994 static void
15995 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15996 {
15997 while (per_cu)
15998 {
15999 per_cu->cu->mark = 0;
16000 per_cu = per_cu->cu->read_in_chain;
16001 }
16002 }
16003
16004 /* Trivial hash function for partial_die_info: the hash value of a DIE
16005 is its offset in .debug_info for this objfile. */
16006
16007 static hashval_t
16008 partial_die_hash (const void *item)
16009 {
16010 const struct partial_die_info *part_die = item;
16011
16012 return part_die->offset;
16013 }
16014
16015 /* Trivial comparison function for partial_die_info structures: two DIEs
16016 are equal if they have the same offset. */
16017
16018 static int
16019 partial_die_eq (const void *item_lhs, const void *item_rhs)
16020 {
16021 const struct partial_die_info *part_die_lhs = item_lhs;
16022 const struct partial_die_info *part_die_rhs = item_rhs;
16023
16024 return part_die_lhs->offset == part_die_rhs->offset;
16025 }
16026
16027 static struct cmd_list_element *set_dwarf2_cmdlist;
16028 static struct cmd_list_element *show_dwarf2_cmdlist;
16029
16030 static void
16031 set_dwarf2_cmd (char *args, int from_tty)
16032 {
16033 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16034 }
16035
16036 static void
16037 show_dwarf2_cmd (char *args, int from_tty)
16038 {
16039 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16040 }
16041
16042 /* If section described by INFO was mmapped, munmap it now. */
16043
16044 static void
16045 munmap_section_buffer (struct dwarf2_section_info *info)
16046 {
16047 if (info->map_addr != NULL)
16048 {
16049 #ifdef HAVE_MMAP
16050 int res;
16051
16052 res = munmap (info->map_addr, info->map_len);
16053 gdb_assert (res == 0);
16054 #else
16055 /* Without HAVE_MMAP, we should never be here to begin with. */
16056 gdb_assert_not_reached ("no mmap support");
16057 #endif
16058 }
16059 }
16060
16061 /* munmap debug sections for OBJFILE, if necessary. */
16062
16063 static void
16064 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16065 {
16066 struct dwarf2_per_objfile *data = d;
16067 int ix;
16068 struct dwarf2_section_info *section;
16069
16070 /* This is sorted according to the order they're defined in to make it easier
16071 to keep in sync. */
16072 munmap_section_buffer (&data->info);
16073 munmap_section_buffer (&data->abbrev);
16074 munmap_section_buffer (&data->line);
16075 munmap_section_buffer (&data->loc);
16076 munmap_section_buffer (&data->macinfo);
16077 munmap_section_buffer (&data->macro);
16078 munmap_section_buffer (&data->str);
16079 munmap_section_buffer (&data->ranges);
16080 munmap_section_buffer (&data->frame);
16081 munmap_section_buffer (&data->eh_frame);
16082 munmap_section_buffer (&data->gdb_index);
16083
16084 for (ix = 0;
16085 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16086 ++ix)
16087 munmap_section_buffer (section);
16088
16089 VEC_free (dwarf2_section_info_def, data->types);
16090 }
16091
16092 \f
16093 /* The "save gdb-index" command. */
16094
16095 /* The contents of the hash table we create when building the string
16096 table. */
16097 struct strtab_entry
16098 {
16099 offset_type offset;
16100 const char *str;
16101 };
16102
16103 /* Hash function for a strtab_entry.
16104
16105 Function is used only during write_hash_table so no index format backward
16106 compatibility is needed. */
16107
16108 static hashval_t
16109 hash_strtab_entry (const void *e)
16110 {
16111 const struct strtab_entry *entry = e;
16112 return mapped_index_string_hash (INT_MAX, entry->str);
16113 }
16114
16115 /* Equality function for a strtab_entry. */
16116
16117 static int
16118 eq_strtab_entry (const void *a, const void *b)
16119 {
16120 const struct strtab_entry *ea = a;
16121 const struct strtab_entry *eb = b;
16122 return !strcmp (ea->str, eb->str);
16123 }
16124
16125 /* Create a strtab_entry hash table. */
16126
16127 static htab_t
16128 create_strtab (void)
16129 {
16130 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16131 xfree, xcalloc, xfree);
16132 }
16133
16134 /* Add a string to the constant pool. Return the string's offset in
16135 host order. */
16136
16137 static offset_type
16138 add_string (htab_t table, struct obstack *cpool, const char *str)
16139 {
16140 void **slot;
16141 struct strtab_entry entry;
16142 struct strtab_entry *result;
16143
16144 entry.str = str;
16145 slot = htab_find_slot (table, &entry, INSERT);
16146 if (*slot)
16147 result = *slot;
16148 else
16149 {
16150 result = XNEW (struct strtab_entry);
16151 result->offset = obstack_object_size (cpool);
16152 result->str = str;
16153 obstack_grow_str0 (cpool, str);
16154 *slot = result;
16155 }
16156 return result->offset;
16157 }
16158
16159 /* An entry in the symbol table. */
16160 struct symtab_index_entry
16161 {
16162 /* The name of the symbol. */
16163 const char *name;
16164 /* The offset of the name in the constant pool. */
16165 offset_type index_offset;
16166 /* A sorted vector of the indices of all the CUs that hold an object
16167 of this name. */
16168 VEC (offset_type) *cu_indices;
16169 };
16170
16171 /* The symbol table. This is a power-of-2-sized hash table. */
16172 struct mapped_symtab
16173 {
16174 offset_type n_elements;
16175 offset_type size;
16176 struct symtab_index_entry **data;
16177 };
16178
16179 /* Hash function for a symtab_index_entry. */
16180
16181 static hashval_t
16182 hash_symtab_entry (const void *e)
16183 {
16184 const struct symtab_index_entry *entry = e;
16185 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16186 sizeof (offset_type) * VEC_length (offset_type,
16187 entry->cu_indices),
16188 0);
16189 }
16190
16191 /* Equality function for a symtab_index_entry. */
16192
16193 static int
16194 eq_symtab_entry (const void *a, const void *b)
16195 {
16196 const struct symtab_index_entry *ea = a;
16197 const struct symtab_index_entry *eb = b;
16198 int len = VEC_length (offset_type, ea->cu_indices);
16199 if (len != VEC_length (offset_type, eb->cu_indices))
16200 return 0;
16201 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16202 VEC_address (offset_type, eb->cu_indices),
16203 sizeof (offset_type) * len);
16204 }
16205
16206 /* Destroy a symtab_index_entry. */
16207
16208 static void
16209 delete_symtab_entry (void *p)
16210 {
16211 struct symtab_index_entry *entry = p;
16212 VEC_free (offset_type, entry->cu_indices);
16213 xfree (entry);
16214 }
16215
16216 /* Create a hash table holding symtab_index_entry objects. */
16217
16218 static htab_t
16219 create_symbol_hash_table (void)
16220 {
16221 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16222 delete_symtab_entry, xcalloc, xfree);
16223 }
16224
16225 /* Create a new mapped symtab object. */
16226
16227 static struct mapped_symtab *
16228 create_mapped_symtab (void)
16229 {
16230 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16231 symtab->n_elements = 0;
16232 symtab->size = 1024;
16233 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16234 return symtab;
16235 }
16236
16237 /* Destroy a mapped_symtab. */
16238
16239 static void
16240 cleanup_mapped_symtab (void *p)
16241 {
16242 struct mapped_symtab *symtab = p;
16243 /* The contents of the array are freed when the other hash table is
16244 destroyed. */
16245 xfree (symtab->data);
16246 xfree (symtab);
16247 }
16248
16249 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16250 the slot.
16251
16252 Function is used only during write_hash_table so no index format backward
16253 compatibility is needed. */
16254
16255 static struct symtab_index_entry **
16256 find_slot (struct mapped_symtab *symtab, const char *name)
16257 {
16258 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16259
16260 index = hash & (symtab->size - 1);
16261 step = ((hash * 17) & (symtab->size - 1)) | 1;
16262
16263 for (;;)
16264 {
16265 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16266 return &symtab->data[index];
16267 index = (index + step) & (symtab->size - 1);
16268 }
16269 }
16270
16271 /* Expand SYMTAB's hash table. */
16272
16273 static void
16274 hash_expand (struct mapped_symtab *symtab)
16275 {
16276 offset_type old_size = symtab->size;
16277 offset_type i;
16278 struct symtab_index_entry **old_entries = symtab->data;
16279
16280 symtab->size *= 2;
16281 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16282
16283 for (i = 0; i < old_size; ++i)
16284 {
16285 if (old_entries[i])
16286 {
16287 struct symtab_index_entry **slot = find_slot (symtab,
16288 old_entries[i]->name);
16289 *slot = old_entries[i];
16290 }
16291 }
16292
16293 xfree (old_entries);
16294 }
16295
16296 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16297 is the index of the CU in which the symbol appears. */
16298
16299 static void
16300 add_index_entry (struct mapped_symtab *symtab, const char *name,
16301 offset_type cu_index)
16302 {
16303 struct symtab_index_entry **slot;
16304
16305 ++symtab->n_elements;
16306 if (4 * symtab->n_elements / 3 >= symtab->size)
16307 hash_expand (symtab);
16308
16309 slot = find_slot (symtab, name);
16310 if (!*slot)
16311 {
16312 *slot = XNEW (struct symtab_index_entry);
16313 (*slot)->name = name;
16314 (*slot)->cu_indices = NULL;
16315 }
16316 /* Don't push an index twice. Due to how we add entries we only
16317 have to check the last one. */
16318 if (VEC_empty (offset_type, (*slot)->cu_indices)
16319 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16320 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16321 }
16322
16323 /* Add a vector of indices to the constant pool. */
16324
16325 static offset_type
16326 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16327 struct symtab_index_entry *entry)
16328 {
16329 void **slot;
16330
16331 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16332 if (!*slot)
16333 {
16334 offset_type len = VEC_length (offset_type, entry->cu_indices);
16335 offset_type val = MAYBE_SWAP (len);
16336 offset_type iter;
16337 int i;
16338
16339 *slot = entry;
16340 entry->index_offset = obstack_object_size (cpool);
16341
16342 obstack_grow (cpool, &val, sizeof (val));
16343 for (i = 0;
16344 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16345 ++i)
16346 {
16347 val = MAYBE_SWAP (iter);
16348 obstack_grow (cpool, &val, sizeof (val));
16349 }
16350 }
16351 else
16352 {
16353 struct symtab_index_entry *old_entry = *slot;
16354 entry->index_offset = old_entry->index_offset;
16355 entry = old_entry;
16356 }
16357 return entry->index_offset;
16358 }
16359
16360 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16361 constant pool entries going into the obstack CPOOL. */
16362
16363 static void
16364 write_hash_table (struct mapped_symtab *symtab,
16365 struct obstack *output, struct obstack *cpool)
16366 {
16367 offset_type i;
16368 htab_t symbol_hash_table;
16369 htab_t str_table;
16370
16371 symbol_hash_table = create_symbol_hash_table ();
16372 str_table = create_strtab ();
16373
16374 /* We add all the index vectors to the constant pool first, to
16375 ensure alignment is ok. */
16376 for (i = 0; i < symtab->size; ++i)
16377 {
16378 if (symtab->data[i])
16379 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16380 }
16381
16382 /* Now write out the hash table. */
16383 for (i = 0; i < symtab->size; ++i)
16384 {
16385 offset_type str_off, vec_off;
16386
16387 if (symtab->data[i])
16388 {
16389 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16390 vec_off = symtab->data[i]->index_offset;
16391 }
16392 else
16393 {
16394 /* While 0 is a valid constant pool index, it is not valid
16395 to have 0 for both offsets. */
16396 str_off = 0;
16397 vec_off = 0;
16398 }
16399
16400 str_off = MAYBE_SWAP (str_off);
16401 vec_off = MAYBE_SWAP (vec_off);
16402
16403 obstack_grow (output, &str_off, sizeof (str_off));
16404 obstack_grow (output, &vec_off, sizeof (vec_off));
16405 }
16406
16407 htab_delete (str_table);
16408 htab_delete (symbol_hash_table);
16409 }
16410
16411 /* Struct to map psymtab to CU index in the index file. */
16412 struct psymtab_cu_index_map
16413 {
16414 struct partial_symtab *psymtab;
16415 unsigned int cu_index;
16416 };
16417
16418 static hashval_t
16419 hash_psymtab_cu_index (const void *item)
16420 {
16421 const struct psymtab_cu_index_map *map = item;
16422
16423 return htab_hash_pointer (map->psymtab);
16424 }
16425
16426 static int
16427 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16428 {
16429 const struct psymtab_cu_index_map *lhs = item_lhs;
16430 const struct psymtab_cu_index_map *rhs = item_rhs;
16431
16432 return lhs->psymtab == rhs->psymtab;
16433 }
16434
16435 /* Helper struct for building the address table. */
16436 struct addrmap_index_data
16437 {
16438 struct objfile *objfile;
16439 struct obstack *addr_obstack;
16440 htab_t cu_index_htab;
16441
16442 /* Non-zero if the previous_* fields are valid.
16443 We can't write an entry until we see the next entry (since it is only then
16444 that we know the end of the entry). */
16445 int previous_valid;
16446 /* Index of the CU in the table of all CUs in the index file. */
16447 unsigned int previous_cu_index;
16448 /* Start address of the CU. */
16449 CORE_ADDR previous_cu_start;
16450 };
16451
16452 /* Write an address entry to OBSTACK. */
16453
16454 static void
16455 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16456 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16457 {
16458 offset_type cu_index_to_write;
16459 char addr[8];
16460 CORE_ADDR baseaddr;
16461
16462 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16463
16464 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16465 obstack_grow (obstack, addr, 8);
16466 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16467 obstack_grow (obstack, addr, 8);
16468 cu_index_to_write = MAYBE_SWAP (cu_index);
16469 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16470 }
16471
16472 /* Worker function for traversing an addrmap to build the address table. */
16473
16474 static int
16475 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16476 {
16477 struct addrmap_index_data *data = datap;
16478 struct partial_symtab *pst = obj;
16479 offset_type cu_index;
16480 void **slot;
16481
16482 if (data->previous_valid)
16483 add_address_entry (data->objfile, data->addr_obstack,
16484 data->previous_cu_start, start_addr,
16485 data->previous_cu_index);
16486
16487 data->previous_cu_start = start_addr;
16488 if (pst != NULL)
16489 {
16490 struct psymtab_cu_index_map find_map, *map;
16491 find_map.psymtab = pst;
16492 map = htab_find (data->cu_index_htab, &find_map);
16493 gdb_assert (map != NULL);
16494 data->previous_cu_index = map->cu_index;
16495 data->previous_valid = 1;
16496 }
16497 else
16498 data->previous_valid = 0;
16499
16500 return 0;
16501 }
16502
16503 /* Write OBJFILE's address map to OBSTACK.
16504 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16505 in the index file. */
16506
16507 static void
16508 write_address_map (struct objfile *objfile, struct obstack *obstack,
16509 htab_t cu_index_htab)
16510 {
16511 struct addrmap_index_data addrmap_index_data;
16512
16513 /* When writing the address table, we have to cope with the fact that
16514 the addrmap iterator only provides the start of a region; we have to
16515 wait until the next invocation to get the start of the next region. */
16516
16517 addrmap_index_data.objfile = objfile;
16518 addrmap_index_data.addr_obstack = obstack;
16519 addrmap_index_data.cu_index_htab = cu_index_htab;
16520 addrmap_index_data.previous_valid = 0;
16521
16522 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16523 &addrmap_index_data);
16524
16525 /* It's highly unlikely the last entry (end address = 0xff...ff)
16526 is valid, but we should still handle it.
16527 The end address is recorded as the start of the next region, but that
16528 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16529 anyway. */
16530 if (addrmap_index_data.previous_valid)
16531 add_address_entry (objfile, obstack,
16532 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16533 addrmap_index_data.previous_cu_index);
16534 }
16535
16536 /* Add a list of partial symbols to SYMTAB. */
16537
16538 static void
16539 write_psymbols (struct mapped_symtab *symtab,
16540 htab_t psyms_seen,
16541 struct partial_symbol **psymp,
16542 int count,
16543 offset_type cu_index,
16544 int is_static)
16545 {
16546 for (; count-- > 0; ++psymp)
16547 {
16548 void **slot, *lookup;
16549
16550 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16551 error (_("Ada is not currently supported by the index"));
16552
16553 /* We only want to add a given psymbol once. However, we also
16554 want to account for whether it is global or static. So, we
16555 may add it twice, using slightly different values. */
16556 if (is_static)
16557 {
16558 uintptr_t val = 1 | (uintptr_t) *psymp;
16559
16560 lookup = (void *) val;
16561 }
16562 else
16563 lookup = *psymp;
16564
16565 /* Only add a given psymbol once. */
16566 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16567 if (!*slot)
16568 {
16569 *slot = lookup;
16570 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16571 }
16572 }
16573 }
16574
16575 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16576 exception if there is an error. */
16577
16578 static void
16579 write_obstack (FILE *file, struct obstack *obstack)
16580 {
16581 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16582 file)
16583 != obstack_object_size (obstack))
16584 error (_("couldn't data write to file"));
16585 }
16586
16587 /* Unlink a file if the argument is not NULL. */
16588
16589 static void
16590 unlink_if_set (void *p)
16591 {
16592 char **filename = p;
16593 if (*filename)
16594 unlink (*filename);
16595 }
16596
16597 /* A helper struct used when iterating over debug_types. */
16598 struct signatured_type_index_data
16599 {
16600 struct objfile *objfile;
16601 struct mapped_symtab *symtab;
16602 struct obstack *types_list;
16603 htab_t psyms_seen;
16604 int cu_index;
16605 };
16606
16607 /* A helper function that writes a single signatured_type to an
16608 obstack. */
16609
16610 static int
16611 write_one_signatured_type (void **slot, void *d)
16612 {
16613 struct signatured_type_index_data *info = d;
16614 struct signatured_type *entry = (struct signatured_type *) *slot;
16615 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16616 struct partial_symtab *psymtab = per_cu->v.psymtab;
16617 gdb_byte val[8];
16618
16619 write_psymbols (info->symtab,
16620 info->psyms_seen,
16621 info->objfile->global_psymbols.list
16622 + psymtab->globals_offset,
16623 psymtab->n_global_syms, info->cu_index,
16624 0);
16625 write_psymbols (info->symtab,
16626 info->psyms_seen,
16627 info->objfile->static_psymbols.list
16628 + psymtab->statics_offset,
16629 psymtab->n_static_syms, info->cu_index,
16630 1);
16631
16632 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16633 obstack_grow (info->types_list, val, 8);
16634 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16635 obstack_grow (info->types_list, val, 8);
16636 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16637 obstack_grow (info->types_list, val, 8);
16638
16639 ++info->cu_index;
16640
16641 return 1;
16642 }
16643
16644 /* Create an index file for OBJFILE in the directory DIR. */
16645
16646 static void
16647 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16648 {
16649 struct cleanup *cleanup;
16650 char *filename, *cleanup_filename;
16651 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16652 struct obstack cu_list, types_cu_list;
16653 int i;
16654 FILE *out_file;
16655 struct mapped_symtab *symtab;
16656 offset_type val, size_of_contents, total_len;
16657 struct stat st;
16658 char buf[8];
16659 htab_t psyms_seen;
16660 htab_t cu_index_htab;
16661 struct psymtab_cu_index_map *psymtab_cu_index_map;
16662
16663 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16664 return;
16665
16666 if (dwarf2_per_objfile->using_index)
16667 error (_("Cannot use an index to create the index"));
16668
16669 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16670 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16671
16672 if (stat (objfile->name, &st) < 0)
16673 perror_with_name (objfile->name);
16674
16675 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16676 INDEX_SUFFIX, (char *) NULL);
16677 cleanup = make_cleanup (xfree, filename);
16678
16679 out_file = fopen (filename, "wb");
16680 if (!out_file)
16681 error (_("Can't open `%s' for writing"), filename);
16682
16683 cleanup_filename = filename;
16684 make_cleanup (unlink_if_set, &cleanup_filename);
16685
16686 symtab = create_mapped_symtab ();
16687 make_cleanup (cleanup_mapped_symtab, symtab);
16688
16689 obstack_init (&addr_obstack);
16690 make_cleanup_obstack_free (&addr_obstack);
16691
16692 obstack_init (&cu_list);
16693 make_cleanup_obstack_free (&cu_list);
16694
16695 obstack_init (&types_cu_list);
16696 make_cleanup_obstack_free (&types_cu_list);
16697
16698 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16699 NULL, xcalloc, xfree);
16700 make_cleanup_htab_delete (psyms_seen);
16701
16702 /* While we're scanning CU's create a table that maps a psymtab pointer
16703 (which is what addrmap records) to its index (which is what is recorded
16704 in the index file). This will later be needed to write the address
16705 table. */
16706 cu_index_htab = htab_create_alloc (100,
16707 hash_psymtab_cu_index,
16708 eq_psymtab_cu_index,
16709 NULL, xcalloc, xfree);
16710 make_cleanup_htab_delete (cu_index_htab);
16711 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16712 xmalloc (sizeof (struct psymtab_cu_index_map)
16713 * dwarf2_per_objfile->n_comp_units);
16714 make_cleanup (xfree, psymtab_cu_index_map);
16715
16716 /* The CU list is already sorted, so we don't need to do additional
16717 work here. Also, the debug_types entries do not appear in
16718 all_comp_units, but only in their own hash table. */
16719 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16720 {
16721 struct dwarf2_per_cu_data *per_cu
16722 = dwarf2_per_objfile->all_comp_units[i];
16723 struct partial_symtab *psymtab = per_cu->v.psymtab;
16724 gdb_byte val[8];
16725 struct psymtab_cu_index_map *map;
16726 void **slot;
16727
16728 write_psymbols (symtab,
16729 psyms_seen,
16730 objfile->global_psymbols.list + psymtab->globals_offset,
16731 psymtab->n_global_syms, i,
16732 0);
16733 write_psymbols (symtab,
16734 psyms_seen,
16735 objfile->static_psymbols.list + psymtab->statics_offset,
16736 psymtab->n_static_syms, i,
16737 1);
16738
16739 map = &psymtab_cu_index_map[i];
16740 map->psymtab = psymtab;
16741 map->cu_index = i;
16742 slot = htab_find_slot (cu_index_htab, map, INSERT);
16743 gdb_assert (slot != NULL);
16744 gdb_assert (*slot == NULL);
16745 *slot = map;
16746
16747 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16748 obstack_grow (&cu_list, val, 8);
16749 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16750 obstack_grow (&cu_list, val, 8);
16751 }
16752
16753 /* Dump the address map. */
16754 write_address_map (objfile, &addr_obstack, cu_index_htab);
16755
16756 /* Write out the .debug_type entries, if any. */
16757 if (dwarf2_per_objfile->signatured_types)
16758 {
16759 struct signatured_type_index_data sig_data;
16760
16761 sig_data.objfile = objfile;
16762 sig_data.symtab = symtab;
16763 sig_data.types_list = &types_cu_list;
16764 sig_data.psyms_seen = psyms_seen;
16765 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16766 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16767 write_one_signatured_type, &sig_data);
16768 }
16769
16770 obstack_init (&constant_pool);
16771 make_cleanup_obstack_free (&constant_pool);
16772 obstack_init (&symtab_obstack);
16773 make_cleanup_obstack_free (&symtab_obstack);
16774 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16775
16776 obstack_init (&contents);
16777 make_cleanup_obstack_free (&contents);
16778 size_of_contents = 6 * sizeof (offset_type);
16779 total_len = size_of_contents;
16780
16781 /* The version number. */
16782 val = MAYBE_SWAP (5);
16783 obstack_grow (&contents, &val, sizeof (val));
16784
16785 /* The offset of the CU list from the start of the file. */
16786 val = MAYBE_SWAP (total_len);
16787 obstack_grow (&contents, &val, sizeof (val));
16788 total_len += obstack_object_size (&cu_list);
16789
16790 /* The offset of the types CU list from the start of the file. */
16791 val = MAYBE_SWAP (total_len);
16792 obstack_grow (&contents, &val, sizeof (val));
16793 total_len += obstack_object_size (&types_cu_list);
16794
16795 /* The offset of the address table from the start of the file. */
16796 val = MAYBE_SWAP (total_len);
16797 obstack_grow (&contents, &val, sizeof (val));
16798 total_len += obstack_object_size (&addr_obstack);
16799
16800 /* The offset of the symbol table from the start of the file. */
16801 val = MAYBE_SWAP (total_len);
16802 obstack_grow (&contents, &val, sizeof (val));
16803 total_len += obstack_object_size (&symtab_obstack);
16804
16805 /* The offset of the constant pool from the start of the file. */
16806 val = MAYBE_SWAP (total_len);
16807 obstack_grow (&contents, &val, sizeof (val));
16808 total_len += obstack_object_size (&constant_pool);
16809
16810 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16811
16812 write_obstack (out_file, &contents);
16813 write_obstack (out_file, &cu_list);
16814 write_obstack (out_file, &types_cu_list);
16815 write_obstack (out_file, &addr_obstack);
16816 write_obstack (out_file, &symtab_obstack);
16817 write_obstack (out_file, &constant_pool);
16818
16819 fclose (out_file);
16820
16821 /* We want to keep the file, so we set cleanup_filename to NULL
16822 here. See unlink_if_set. */
16823 cleanup_filename = NULL;
16824
16825 do_cleanups (cleanup);
16826 }
16827
16828 /* Implementation of the `save gdb-index' command.
16829
16830 Note that the file format used by this command is documented in the
16831 GDB manual. Any changes here must be documented there. */
16832
16833 static void
16834 save_gdb_index_command (char *arg, int from_tty)
16835 {
16836 struct objfile *objfile;
16837
16838 if (!arg || !*arg)
16839 error (_("usage: save gdb-index DIRECTORY"));
16840
16841 ALL_OBJFILES (objfile)
16842 {
16843 struct stat st;
16844
16845 /* If the objfile does not correspond to an actual file, skip it. */
16846 if (stat (objfile->name, &st) < 0)
16847 continue;
16848
16849 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16850 if (dwarf2_per_objfile)
16851 {
16852 volatile struct gdb_exception except;
16853
16854 TRY_CATCH (except, RETURN_MASK_ERROR)
16855 {
16856 write_psymtabs_to_index (objfile, arg);
16857 }
16858 if (except.reason < 0)
16859 exception_fprintf (gdb_stderr, except,
16860 _("Error while writing index for `%s': "),
16861 objfile->name);
16862 }
16863 }
16864 }
16865
16866 \f
16867
16868 int dwarf2_always_disassemble;
16869
16870 static void
16871 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16872 struct cmd_list_element *c, const char *value)
16873 {
16874 fprintf_filtered (file,
16875 _("Whether to always disassemble "
16876 "DWARF expressions is %s.\n"),
16877 value);
16878 }
16879
16880 static void
16881 show_check_physname (struct ui_file *file, int from_tty,
16882 struct cmd_list_element *c, const char *value)
16883 {
16884 fprintf_filtered (file,
16885 _("Whether to check \"physname\" is %s.\n"),
16886 value);
16887 }
16888
16889 void _initialize_dwarf2_read (void);
16890
16891 void
16892 _initialize_dwarf2_read (void)
16893 {
16894 struct cmd_list_element *c;
16895
16896 dwarf2_objfile_data_key
16897 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16898
16899 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16900 Set DWARF 2 specific variables.\n\
16901 Configure DWARF 2 variables such as the cache size"),
16902 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16903 0/*allow-unknown*/, &maintenance_set_cmdlist);
16904
16905 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16906 Show DWARF 2 specific variables\n\
16907 Show DWARF 2 variables such as the cache size"),
16908 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16909 0/*allow-unknown*/, &maintenance_show_cmdlist);
16910
16911 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16912 &dwarf2_max_cache_age, _("\
16913 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16914 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16915 A higher limit means that cached compilation units will be stored\n\
16916 in memory longer, and more total memory will be used. Zero disables\n\
16917 caching, which can slow down startup."),
16918 NULL,
16919 show_dwarf2_max_cache_age,
16920 &set_dwarf2_cmdlist,
16921 &show_dwarf2_cmdlist);
16922
16923 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16924 &dwarf2_always_disassemble, _("\
16925 Set whether `info address' always disassembles DWARF expressions."), _("\
16926 Show whether `info address' always disassembles DWARF expressions."), _("\
16927 When enabled, DWARF expressions are always printed in an assembly-like\n\
16928 syntax. When disabled, expressions will be printed in a more\n\
16929 conversational style, when possible."),
16930 NULL,
16931 show_dwarf2_always_disassemble,
16932 &set_dwarf2_cmdlist,
16933 &show_dwarf2_cmdlist);
16934
16935 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16936 Set debugging of the dwarf2 DIE reader."), _("\
16937 Show debugging of the dwarf2 DIE reader."), _("\
16938 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16939 The value is the maximum depth to print."),
16940 NULL,
16941 NULL,
16942 &setdebuglist, &showdebuglist);
16943
16944 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16945 Set cross-checking of \"physname\" code against demangler."), _("\
16946 Show cross-checking of \"physname\" code against demangler."), _("\
16947 When enabled, GDB's internal \"physname\" code is checked against\n\
16948 the demangler."),
16949 NULL, show_check_physname,
16950 &setdebuglist, &showdebuglist);
16951
16952 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16953 _("\
16954 Save a gdb-index file.\n\
16955 Usage: save gdb-index DIRECTORY"),
16956 &save_cmdlist);
16957 set_cmd_completer (c, filename_completer);
16958 }
This page took 0.360332 seconds and 5 git commands to generate.