* python/py-type.c (type_object_methods): Fix "array" doc string.
[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 "gdb-demangle.h"
38 #include "expression.h"
39 #include "filenames.h" /* for DOSish file names */
40 #include "macrotab.h"
41 #include "language.h"
42 #include "complaints.h"
43 #include "bcache.h"
44 #include "dwarf2expr.h"
45 #include "dwarf2loc.h"
46 #include "cp-support.h"
47 #include "hashtab.h"
48 #include "command.h"
49 #include "gdbcmd.h"
50 #include "block.h"
51 #include "addrmap.h"
52 #include "typeprint.h"
53 #include "jv-lang.h"
54 #include "psympriv.h"
55 #include "exceptions.h"
56 #include "gdb_stat.h"
57 #include "completer.h"
58 #include "vec.h"
59 #include "c-lang.h"
60 #include "valprint.h"
61 #include <ctype.h>
62
63 #include <fcntl.h>
64 #include "gdb_string.h"
65 #include "gdb_assert.h"
66 #include <sys/types.h>
67 #ifdef HAVE_ZLIB_H
68 #include <zlib.h>
69 #endif
70 #ifdef HAVE_MMAP
71 #include <sys/mman.h>
72 #ifndef MAP_FAILED
73 #define MAP_FAILED ((void *) -1)
74 #endif
75 #endif
76
77 typedef struct symbol *symbolp;
78 DEF_VEC_P (symbolp);
79
80 #if 0
81 /* .debug_info header for a compilation unit
82 Because of alignment constraints, this structure has padding and cannot
83 be mapped directly onto the beginning of the .debug_info section. */
84 typedef struct comp_unit_header
85 {
86 unsigned int length; /* length of the .debug_info
87 contribution */
88 unsigned short version; /* version number -- 2 for DWARF
89 version 2 */
90 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
91 unsigned char addr_size; /* byte size of an address -- 4 */
92 }
93 _COMP_UNIT_HEADER;
94 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
95 #endif
96
97 /* .debug_line statement program prologue
98 Because of alignment constraints, this structure has padding and cannot
99 be mapped directly onto the beginning of the .debug_info section. */
100 typedef struct statement_prologue
101 {
102 unsigned int total_length; /* byte length of the statement
103 information */
104 unsigned short version; /* version number -- 2 for DWARF
105 version 2 */
106 unsigned int prologue_length; /* # bytes between prologue &
107 stmt program */
108 unsigned char minimum_instruction_length; /* byte size of
109 smallest instr */
110 unsigned char default_is_stmt; /* initial value of is_stmt
111 register */
112 char line_base;
113 unsigned char line_range;
114 unsigned char opcode_base; /* number assigned to first special
115 opcode */
116 unsigned char *standard_opcode_lengths;
117 }
118 _STATEMENT_PROLOGUE;
119
120 /* When non-zero, dump DIEs after they are read in. */
121 static int dwarf2_die_debug = 0;
122
123 /* When non-zero, cross-check physname against demangler. */
124 static int check_physname = 0;
125
126 static int pagesize;
127
128 /* When set, the file that we're processing is known to have debugging
129 info for C++ namespaces. GCC 3.3.x did not produce this information,
130 but later versions do. */
131
132 static int processing_has_namespace_info;
133
134 static const struct objfile_data *dwarf2_objfile_data_key;
135
136 struct dwarf2_section_info
137 {
138 asection *asection;
139 gdb_byte *buffer;
140 bfd_size_type size;
141 /* Not NULL if the section was actually mmapped. */
142 void *map_addr;
143 /* Page aligned size of mmapped area. */
144 bfd_size_type map_len;
145 /* True if we have tried to read this section. */
146 int readin;
147 };
148
149 typedef struct dwarf2_section_info dwarf2_section_info_def;
150 DEF_VEC_O (dwarf2_section_info_def);
151
152 /* All offsets in the index are of this type. It must be
153 architecture-independent. */
154 typedef uint32_t offset_type;
155
156 DEF_VEC_I (offset_type);
157
158 /* A description of the mapped index. The file format is described in
159 a comment by the code that writes the index. */
160 struct mapped_index
161 {
162 /* Index data format version. */
163 int version;
164
165 /* The total length of the buffer. */
166 off_t total_size;
167
168 /* A pointer to the address table data. */
169 const gdb_byte *address_table;
170
171 /* Size of the address table data in bytes. */
172 offset_type address_table_size;
173
174 /* The symbol table, implemented as a hash table. */
175 const offset_type *symbol_table;
176
177 /* Size in slots, each slot is 2 offset_types. */
178 offset_type symbol_table_slots;
179
180 /* A pointer to the constant pool. */
181 const char *constant_pool;
182 };
183
184 struct dwarf2_per_objfile
185 {
186 struct dwarf2_section_info info;
187 struct dwarf2_section_info abbrev;
188 struct dwarf2_section_info line;
189 struct dwarf2_section_info loc;
190 struct dwarf2_section_info macinfo;
191 struct dwarf2_section_info macro;
192 struct dwarf2_section_info str;
193 struct dwarf2_section_info ranges;
194 struct dwarf2_section_info frame;
195 struct dwarf2_section_info eh_frame;
196 struct dwarf2_section_info gdb_index;
197
198 VEC (dwarf2_section_info_def) *types;
199
200 /* Back link. */
201 struct objfile *objfile;
202
203 /* A list of all the compilation units. This is used to locate
204 the target compilation unit of a particular reference. */
205 struct dwarf2_per_cu_data **all_comp_units;
206
207 /* The number of compilation units in ALL_COMP_UNITS. */
208 int n_comp_units;
209
210 /* The number of .debug_types-related CUs. */
211 int n_type_comp_units;
212
213 /* The .debug_types-related CUs. */
214 struct dwarf2_per_cu_data **type_comp_units;
215
216 /* A chain of compilation units that are currently read in, so that
217 they can be freed later. */
218 struct dwarf2_per_cu_data *read_in_chain;
219
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
224 /* A flag indicating wether this objfile has a section loaded at a
225 VMA of 0. */
226 int has_section_at_zero;
227
228 /* True if we are using the mapped index,
229 or we are faking it for OBJF_READNOW's sake. */
230 unsigned char using_index;
231
232 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
233 struct mapped_index *index_table;
234
235 /* When using index_table, this keeps track of all quick_file_names entries.
236 TUs can share line table entries with CUs or other TUs, and there can be
237 a lot more TUs than unique line tables, so we maintain a separate table
238 of all line table entries to support the sharing. */
239 htab_t quick_file_names_table;
240
241 /* Set during partial symbol reading, to prevent queueing of full
242 symbols. */
243 int reading_partial_symbols;
244
245 /* Table mapping type .debug_info DIE offsets to types.
246 This is NULL if not allocated yet.
247 It (currently) makes sense to allocate debug_types_type_hash lazily.
248 To keep things simple we allocate both lazily. */
249 htab_t debug_info_type_hash;
250
251 /* Table mapping type .debug_types DIE offsets to types.
252 This is NULL if not allocated yet. */
253 htab_t debug_types_type_hash;
254 };
255
256 static struct dwarf2_per_objfile *dwarf2_per_objfile;
257
258 /* Default names of the debugging sections. */
259
260 /* Note that if the debugging section has been compressed, it might
261 have a name like .zdebug_info. */
262
263 static const struct dwarf2_debug_sections dwarf2_elf_names = {
264 { ".debug_info", ".zdebug_info" },
265 { ".debug_abbrev", ".zdebug_abbrev" },
266 { ".debug_line", ".zdebug_line" },
267 { ".debug_loc", ".zdebug_loc" },
268 { ".debug_macinfo", ".zdebug_macinfo" },
269 { ".debug_macro", ".zdebug_macro" },
270 { ".debug_str", ".zdebug_str" },
271 { ".debug_ranges", ".zdebug_ranges" },
272 { ".debug_types", ".zdebug_types" },
273 { ".debug_frame", ".zdebug_frame" },
274 { ".eh_frame", NULL },
275 { ".gdb_index", ".zgdb_index" },
276 23
277 };
278
279 /* local data types */
280
281 /* We hold several abbreviation tables in memory at the same time. */
282 #ifndef ABBREV_HASH_SIZE
283 #define ABBREV_HASH_SIZE 121
284 #endif
285
286 /* The data in a compilation unit header, after target2host
287 translation, looks like this. */
288 struct comp_unit_head
289 {
290 unsigned int length;
291 short version;
292 unsigned char addr_size;
293 unsigned char signed_addr_p;
294 unsigned int abbrev_offset;
295
296 /* Size of file offsets; either 4 or 8. */
297 unsigned int offset_size;
298
299 /* Size of the length field; either 4 or 12. */
300 unsigned int initial_length_size;
301
302 /* Offset to the first byte of this compilation unit header in the
303 .debug_info section, for resolving relative reference dies. */
304 unsigned int offset;
305
306 /* Offset to first die in this cu from the start of the cu.
307 This will be the first byte following the compilation unit header. */
308 unsigned int first_die_offset;
309 };
310
311 /* Type used for delaying computation of method physnames.
312 See comments for compute_delayed_physnames. */
313 struct delayed_method_info
314 {
315 /* The type to which the method is attached, i.e., its parent class. */
316 struct type *type;
317
318 /* The index of the method in the type's function fieldlists. */
319 int fnfield_index;
320
321 /* The index of the method in the fieldlist. */
322 int index;
323
324 /* The name of the DIE. */
325 const char *name;
326
327 /* The DIE associated with this method. */
328 struct die_info *die;
329 };
330
331 typedef struct delayed_method_info delayed_method_info;
332 DEF_VEC_O (delayed_method_info);
333
334 /* Internal state when decoding a particular compilation unit. */
335 struct dwarf2_cu
336 {
337 /* The objfile containing this compilation unit. */
338 struct objfile *objfile;
339
340 /* The header of the compilation unit. */
341 struct comp_unit_head header;
342
343 /* Base address of this compilation unit. */
344 CORE_ADDR base_address;
345
346 /* Non-zero if base_address has been set. */
347 int base_known;
348
349 struct function_range *first_fn, *last_fn, *cached_fn;
350
351 /* The language we are debugging. */
352 enum language language;
353 const struct language_defn *language_defn;
354
355 const char *producer;
356
357 /* The generic symbol table building routines have separate lists for
358 file scope symbols and all all other scopes (local scopes). So
359 we need to select the right one to pass to add_symbol_to_list().
360 We do it by keeping a pointer to the correct list in list_in_scope.
361
362 FIXME: The original dwarf code just treated the file scope as the
363 first local scope, and all other local scopes as nested local
364 scopes, and worked fine. Check to see if we really need to
365 distinguish these in buildsym.c. */
366 struct pending **list_in_scope;
367
368 /* DWARF abbreviation table associated with this compilation unit. */
369 struct abbrev_info **dwarf2_abbrevs;
370
371 /* Storage for the abbrev table. */
372 struct obstack abbrev_obstack;
373
374 /* Hash table holding all the loaded partial DIEs. */
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
393 /* A hash table of die offsets for following references. */
394 htab_t die_hash;
395
396 /* Full DIEs if read in. */
397 struct die_info *dies;
398
399 /* A set of pointers to dwarf2_per_cu_data objects for compilation
400 units referenced by this one. Only set during full symbol processing;
401 partial symbol tables do not have dependencies. */
402 htab_t dependencies;
403
404 /* Header data from the line table, during full symbol processing. */
405 struct line_header *line_header;
406
407 /* A list of methods which need to have physnames computed
408 after all type information has been read. */
409 VEC (delayed_method_info) *method_list;
410
411 /* To be copied to symtab->call_site_htab. */
412 htab_t call_site_htab;
413
414 /* Mark used when releasing cached dies. */
415 unsigned int mark : 1;
416
417 /* This flag will be set if this compilation unit might include
418 inter-compilation-unit references. */
419 unsigned int has_form_ref_addr : 1;
420
421 /* This flag will be set if this compilation unit includes any
422 DW_TAG_namespace DIEs. If we know that there are explicit
423 DIEs for namespaces, we don't need to try to infer them
424 from mangled names. */
425 unsigned int has_namespace_info : 1;
426
427 /* This CU references .debug_loc. See the symtab->locations_valid field.
428 This test is imperfect as there may exist optimized debug code not using
429 any location list and still facing inlining issues if handled as
430 unoptimized code. For a future better test see GCC PR other/32998. */
431
432 unsigned int has_loclist : 1;
433 };
434
435 /* Persistent data held for a compilation unit, even when not
436 processing it. We put a pointer to this structure in the
437 read_symtab_private field of the psymtab. If we encounter
438 inter-compilation-unit references, we also maintain a sorted
439 list of all compilation units. */
440
441 struct dwarf2_per_cu_data
442 {
443 /* The start offset and length of this compilation unit. 2**29-1
444 bytes should suffice to store the length of any compilation unit
445 - if it doesn't, GDB will fall over anyway.
446 NOTE: Unlike comp_unit_head.length, this length includes
447 initial_length_size. */
448 unsigned int offset;
449 unsigned int length : 29;
450
451 /* Flag indicating this compilation unit will be read in before
452 any of the current compilation units are processed. */
453 unsigned int queued : 1;
454
455 /* This flag will be set if we need to load absolutely all DIEs
456 for this compilation unit, instead of just the ones we think
457 are interesting. It gets set if we look for a DIE in the
458 hash table and don't find it. */
459 unsigned int load_all_dies : 1;
460
461 /* Non-null if this CU is from .debug_types; in which case it points
462 to the section. Otherwise it's from .debug_info. */
463 struct dwarf2_section_info *debug_type_section;
464
465 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
466 of the CU cache it gets reset to NULL again. */
467 struct dwarf2_cu *cu;
468
469 /* The corresponding objfile. */
470 struct objfile *objfile;
471
472 /* When using partial symbol tables, the 'psymtab' field is active.
473 Otherwise the 'quick' field is active. */
474 union
475 {
476 /* The partial symbol table associated with this compilation unit,
477 or NULL for partial units (which do not have an associated
478 symtab). */
479 struct partial_symtab *psymtab;
480
481 /* Data needed by the "quick" functions. */
482 struct dwarf2_per_cu_quick_data *quick;
483 } v;
484 };
485
486 /* Entry in the signatured_types hash table. */
487
488 struct signatured_type
489 {
490 ULONGEST signature;
491
492 /* Offset in .debug_types of the type defined by this TU. */
493 unsigned int type_offset;
494
495 /* The CU(/TU) of this type. */
496 struct dwarf2_per_cu_data per_cu;
497 };
498
499 /* Struct used to pass misc. parameters to read_die_and_children, et
500 al. which are used for both .debug_info and .debug_types dies.
501 All parameters here are unchanging for the life of the call. This
502 struct exists to abstract away the constant parameters of die
503 reading. */
504
505 struct die_reader_specs
506 {
507 /* The bfd of this objfile. */
508 bfd* abfd;
509
510 /* The CU of the DIE we are parsing. */
511 struct dwarf2_cu *cu;
512
513 /* Pointer to start of section buffer.
514 This is either the start of .debug_info or .debug_types. */
515 const gdb_byte *buffer;
516 };
517
518 /* The line number information for a compilation unit (found in the
519 .debug_line section) begins with a "statement program header",
520 which contains the following information. */
521 struct line_header
522 {
523 unsigned int total_length;
524 unsigned short version;
525 unsigned int header_length;
526 unsigned char minimum_instruction_length;
527 unsigned char maximum_ops_per_instruction;
528 unsigned char default_is_stmt;
529 int line_base;
530 unsigned char line_range;
531 unsigned char opcode_base;
532
533 /* standard_opcode_lengths[i] is the number of operands for the
534 standard opcode whose value is i. This means that
535 standard_opcode_lengths[0] is unused, and the last meaningful
536 element is standard_opcode_lengths[opcode_base - 1]. */
537 unsigned char *standard_opcode_lengths;
538
539 /* The include_directories table. NOTE! These strings are not
540 allocated with xmalloc; instead, they are pointers into
541 debug_line_buffer. If you try to free them, `free' will get
542 indigestion. */
543 unsigned int num_include_dirs, include_dirs_size;
544 char **include_dirs;
545
546 /* The file_names table. NOTE! These strings are not allocated
547 with xmalloc; instead, they are pointers into debug_line_buffer.
548 Don't try to free them directly. */
549 unsigned int num_file_names, file_names_size;
550 struct file_entry
551 {
552 char *name;
553 unsigned int dir_index;
554 unsigned int mod_time;
555 unsigned int length;
556 int included_p; /* Non-zero if referenced by the Line Number Program. */
557 struct symtab *symtab; /* The associated symbol table, if any. */
558 } *file_names;
559
560 /* The start and end of the statement program following this
561 header. These point into dwarf2_per_objfile->line_buffer. */
562 gdb_byte *statement_program_start, *statement_program_end;
563 };
564
565 /* When we construct a partial symbol table entry we only
566 need this much information. */
567 struct partial_die_info
568 {
569 /* Offset of this DIE. */
570 unsigned int offset;
571
572 /* DWARF-2 tag for this DIE. */
573 ENUM_BITFIELD(dwarf_tag) tag : 16;
574
575 /* Assorted flags describing the data found in this DIE. */
576 unsigned int has_children : 1;
577 unsigned int is_external : 1;
578 unsigned int is_declaration : 1;
579 unsigned int has_type : 1;
580 unsigned int has_specification : 1;
581 unsigned int has_pc_info : 1;
582
583 /* Flag set if the SCOPE field of this structure has been
584 computed. */
585 unsigned int scope_set : 1;
586
587 /* Flag set if the DIE has a byte_size attribute. */
588 unsigned int has_byte_size : 1;
589
590 /* Flag set if any of the DIE's children are template arguments. */
591 unsigned int has_template_arguments : 1;
592
593 /* Flag set if fixup_partial_die has been called on this die. */
594 unsigned int fixup_called : 1;
595
596 /* The name of this DIE. Normally the value of DW_AT_name, but
597 sometimes a default name for unnamed DIEs. */
598 char *name;
599
600 /* The linkage name, if present. */
601 const char *linkage_name;
602
603 /* The scope to prepend to our children. This is generally
604 allocated on the comp_unit_obstack, so will disappear
605 when this compilation unit leaves the cache. */
606 char *scope;
607
608 /* The location description associated with this DIE, if any. */
609 struct dwarf_block *locdesc;
610
611 /* If HAS_PC_INFO, the PC range associated with this DIE. */
612 CORE_ADDR lowpc;
613 CORE_ADDR highpc;
614
615 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
616 DW_AT_sibling, if any. */
617 /* NOTE: This member isn't strictly necessary, read_partial_die could
618 return DW_AT_sibling values to its caller load_partial_dies. */
619 gdb_byte *sibling;
620
621 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
622 DW_AT_specification (or DW_AT_abstract_origin or
623 DW_AT_extension). */
624 unsigned int spec_offset;
625
626 /* Pointers to this DIE's parent, first child, and next sibling,
627 if any. */
628 struct partial_die_info *die_parent, *die_child, *die_sibling;
629 };
630
631 /* This data structure holds the information of an abbrev. */
632 struct abbrev_info
633 {
634 unsigned int number; /* number identifying abbrev */
635 enum dwarf_tag tag; /* dwarf tag */
636 unsigned short has_children; /* boolean */
637 unsigned short num_attrs; /* number of attributes */
638 struct attr_abbrev *attrs; /* an array of attribute descriptions */
639 struct abbrev_info *next; /* next in chain */
640 };
641
642 struct attr_abbrev
643 {
644 ENUM_BITFIELD(dwarf_attribute) name : 16;
645 ENUM_BITFIELD(dwarf_form) form : 16;
646 };
647
648 /* Attributes have a name and a value. */
649 struct attribute
650 {
651 ENUM_BITFIELD(dwarf_attribute) name : 16;
652 ENUM_BITFIELD(dwarf_form) form : 15;
653
654 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
655 field should be in u.str (existing only for DW_STRING) but it is kept
656 here for better struct attribute alignment. */
657 unsigned int string_is_canonical : 1;
658
659 union
660 {
661 char *str;
662 struct dwarf_block *blk;
663 ULONGEST unsnd;
664 LONGEST snd;
665 CORE_ADDR addr;
666 struct signatured_type *signatured_type;
667 }
668 u;
669 };
670
671 /* This data structure holds a complete die structure. */
672 struct die_info
673 {
674 /* DWARF-2 tag for this DIE. */
675 ENUM_BITFIELD(dwarf_tag) tag : 16;
676
677 /* Number of attributes */
678 unsigned char num_attrs;
679
680 /* True if we're presently building the full type name for the
681 type derived from this DIE. */
682 unsigned char building_fullname : 1;
683
684 /* Abbrev number */
685 unsigned int abbrev;
686
687 /* Offset in .debug_info or .debug_types section. */
688 unsigned int offset;
689
690 /* The dies in a compilation unit form an n-ary tree. PARENT
691 points to this die's parent; CHILD points to the first child of
692 this node; and all the children of a given node are chained
693 together via their SIBLING fields. */
694 struct die_info *child; /* Its first child, if any. */
695 struct die_info *sibling; /* Its next sibling, if any. */
696 struct die_info *parent; /* Its parent, if any. */
697
698 /* An array of attributes, with NUM_ATTRS elements. There may be
699 zero, but it's not common and zero-sized arrays are not
700 sufficiently portable C. */
701 struct attribute attrs[1];
702 };
703
704 struct function_range
705 {
706 const char *name;
707 CORE_ADDR lowpc, highpc;
708 int seen_line;
709 struct function_range *next;
710 };
711
712 /* Get at parts of an attribute structure. */
713
714 #define DW_STRING(attr) ((attr)->u.str)
715 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
716 #define DW_UNSND(attr) ((attr)->u.unsnd)
717 #define DW_BLOCK(attr) ((attr)->u.blk)
718 #define DW_SND(attr) ((attr)->u.snd)
719 #define DW_ADDR(attr) ((attr)->u.addr)
720 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
721
722 /* Blocks are a bunch of untyped bytes. */
723 struct dwarf_block
724 {
725 unsigned int size;
726
727 /* Valid only if SIZE is not zero. */
728 gdb_byte *data;
729 };
730
731 #ifndef ATTR_ALLOC_CHUNK
732 #define ATTR_ALLOC_CHUNK 4
733 #endif
734
735 /* Allocate fields for structs, unions and enums in this size. */
736 #ifndef DW_FIELD_ALLOC_CHUNK
737 #define DW_FIELD_ALLOC_CHUNK 4
738 #endif
739
740 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
741 but this would require a corresponding change in unpack_field_as_long
742 and friends. */
743 static int bits_per_byte = 8;
744
745 /* The routines that read and process dies for a C struct or C++ class
746 pass lists of data member fields and lists of member function fields
747 in an instance of a field_info structure, as defined below. */
748 struct field_info
749 {
750 /* List of data member and baseclasses fields. */
751 struct nextfield
752 {
753 struct nextfield *next;
754 int accessibility;
755 int virtuality;
756 struct field field;
757 }
758 *fields, *baseclasses;
759
760 /* Number of fields (including baseclasses). */
761 int nfields;
762
763 /* Number of baseclasses. */
764 int nbaseclasses;
765
766 /* Set if the accesibility of one of the fields is not public. */
767 int non_public_fields;
768
769 /* Member function fields array, entries are allocated in the order they
770 are encountered in the object file. */
771 struct nextfnfield
772 {
773 struct nextfnfield *next;
774 struct fn_field fnfield;
775 }
776 *fnfields;
777
778 /* Member function fieldlist array, contains name of possibly overloaded
779 member function, number of overloaded member functions and a pointer
780 to the head of the member function field chain. */
781 struct fnfieldlist
782 {
783 char *name;
784 int length;
785 struct nextfnfield *head;
786 }
787 *fnfieldlists;
788
789 /* Number of entries in the fnfieldlists array. */
790 int nfnfields;
791
792 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
793 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
794 struct typedef_field_list
795 {
796 struct typedef_field field;
797 struct typedef_field_list *next;
798 }
799 *typedef_field_list;
800 unsigned typedef_field_list_count;
801 };
802
803 /* One item on the queue of compilation units to read in full symbols
804 for. */
805 struct dwarf2_queue_item
806 {
807 struct dwarf2_per_cu_data *per_cu;
808 struct dwarf2_queue_item *next;
809 };
810
811 /* The current queue. */
812 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
813
814 /* Loaded secondary compilation units are kept in memory until they
815 have not been referenced for the processing of this many
816 compilation units. Set this to zero to disable caching. Cache
817 sizes of up to at least twenty will improve startup time for
818 typical inter-CU-reference binaries, at an obvious memory cost. */
819 static int dwarf2_max_cache_age = 5;
820 static void
821 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
822 struct cmd_list_element *c, const char *value)
823 {
824 fprintf_filtered (file, _("The upper bound on the age of cached "
825 "dwarf2 compilation units is %s.\n"),
826 value);
827 }
828
829
830 /* Various complaints about symbol reading that don't abort the process. */
831
832 static void
833 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
834 {
835 complaint (&symfile_complaints,
836 _("statement list doesn't fit in .debug_line section"));
837 }
838
839 static void
840 dwarf2_debug_line_missing_file_complaint (void)
841 {
842 complaint (&symfile_complaints,
843 _(".debug_line section has line data without a file"));
844 }
845
846 static void
847 dwarf2_debug_line_missing_end_sequence_complaint (void)
848 {
849 complaint (&symfile_complaints,
850 _(".debug_line section has line "
851 "program sequence without an end"));
852 }
853
854 static void
855 dwarf2_complex_location_expr_complaint (void)
856 {
857 complaint (&symfile_complaints, _("location expression too complex"));
858 }
859
860 static void
861 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
862 int arg3)
863 {
864 complaint (&symfile_complaints,
865 _("const value length mismatch for '%s', got %d, expected %d"),
866 arg1, arg2, arg3);
867 }
868
869 static void
870 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
871 {
872 complaint (&symfile_complaints,
873 _("macro info runs off end of `%s' section"),
874 section->asection->name);
875 }
876
877 static void
878 dwarf2_macro_malformed_definition_complaint (const char *arg1)
879 {
880 complaint (&symfile_complaints,
881 _("macro debug info contains a "
882 "malformed macro definition:\n`%s'"),
883 arg1);
884 }
885
886 static void
887 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
888 {
889 complaint (&symfile_complaints,
890 _("invalid attribute class or form for '%s' in '%s'"),
891 arg1, arg2);
892 }
893
894 /* local function prototypes */
895
896 static void dwarf2_locate_sections (bfd *, asection *, void *);
897
898 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
899 struct objfile *);
900
901 static void dwarf2_find_base_address (struct die_info *die,
902 struct dwarf2_cu *cu);
903
904 static void dwarf2_build_psymtabs_hard (struct objfile *);
905
906 static void scan_partial_symbols (struct partial_die_info *,
907 CORE_ADDR *, CORE_ADDR *,
908 int, struct dwarf2_cu *);
909
910 static void add_partial_symbol (struct partial_die_info *,
911 struct dwarf2_cu *);
912
913 static void add_partial_namespace (struct partial_die_info *pdi,
914 CORE_ADDR *lowpc, CORE_ADDR *highpc,
915 int need_pc, struct dwarf2_cu *cu);
916
917 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
918 CORE_ADDR *highpc, int need_pc,
919 struct dwarf2_cu *cu);
920
921 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
922 struct dwarf2_cu *cu);
923
924 static void add_partial_subprogram (struct partial_die_info *pdi,
925 CORE_ADDR *lowpc, CORE_ADDR *highpc,
926 int need_pc, struct dwarf2_cu *cu);
927
928 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
929 gdb_byte *buffer, gdb_byte *info_ptr,
930 bfd *abfd, struct dwarf2_cu *cu);
931
932 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
933
934 static void psymtab_to_symtab_1 (struct partial_symtab *);
935
936 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
937
938 static void dwarf2_free_abbrev_table (void *);
939
940 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
941
942 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
943 struct dwarf2_cu *);
944
945 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
946 struct dwarf2_cu *);
947
948 static struct partial_die_info *load_partial_dies (bfd *,
949 gdb_byte *, gdb_byte *,
950 int, struct dwarf2_cu *);
951
952 static gdb_byte *read_partial_die (struct partial_die_info *,
953 struct abbrev_info *abbrev,
954 unsigned int, bfd *,
955 gdb_byte *, gdb_byte *,
956 struct dwarf2_cu *);
957
958 static struct partial_die_info *find_partial_die (unsigned int,
959 struct dwarf2_cu *);
960
961 static void fixup_partial_die (struct partial_die_info *,
962 struct dwarf2_cu *);
963
964 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
965 bfd *, gdb_byte *, struct dwarf2_cu *);
966
967 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
968 bfd *, gdb_byte *, struct dwarf2_cu *);
969
970 static unsigned int read_1_byte (bfd *, gdb_byte *);
971
972 static int read_1_signed_byte (bfd *, gdb_byte *);
973
974 static unsigned int read_2_bytes (bfd *, gdb_byte *);
975
976 static unsigned int read_4_bytes (bfd *, gdb_byte *);
977
978 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
979
980 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
981 unsigned int *);
982
983 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
984
985 static LONGEST read_checked_initial_length_and_offset
986 (bfd *, gdb_byte *, const struct comp_unit_head *,
987 unsigned int *, unsigned int *);
988
989 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
990 unsigned int *);
991
992 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
993
994 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
995
996 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
997
998 static char *read_indirect_string (bfd *, gdb_byte *,
999 const struct comp_unit_head *,
1000 unsigned int *);
1001
1002 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1003
1004 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1005
1006 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1007
1008 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1009
1010 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1011 struct dwarf2_cu *);
1012
1013 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1014 unsigned int,
1015 struct dwarf2_cu *);
1016
1017 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1018 struct dwarf2_cu *cu);
1019
1020 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1021
1022 static struct die_info *die_specification (struct die_info *die,
1023 struct dwarf2_cu **);
1024
1025 static void free_line_header (struct line_header *lh);
1026
1027 static void add_file_name (struct line_header *, char *, unsigned int,
1028 unsigned int, unsigned int);
1029
1030 static struct line_header *(dwarf_decode_line_header
1031 (unsigned int offset,
1032 bfd *abfd, struct dwarf2_cu *cu));
1033
1034 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1035 struct dwarf2_cu *, struct partial_symtab *);
1036
1037 static void dwarf2_start_subfile (char *, const char *, const char *);
1038
1039 static struct symbol *new_symbol (struct die_info *, struct type *,
1040 struct dwarf2_cu *);
1041
1042 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1043 struct dwarf2_cu *, struct symbol *);
1044
1045 static void dwarf2_const_value (struct attribute *, struct symbol *,
1046 struct dwarf2_cu *);
1047
1048 static void dwarf2_const_value_attr (struct attribute *attr,
1049 struct type *type,
1050 const char *name,
1051 struct obstack *obstack,
1052 struct dwarf2_cu *cu, long *value,
1053 gdb_byte **bytes,
1054 struct dwarf2_locexpr_baton **baton);
1055
1056 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1057
1058 static int need_gnat_info (struct dwarf2_cu *);
1059
1060 static struct type *die_descriptive_type (struct die_info *,
1061 struct dwarf2_cu *);
1062
1063 static void set_descriptive_type (struct type *, struct die_info *,
1064 struct dwarf2_cu *);
1065
1066 static struct type *die_containing_type (struct die_info *,
1067 struct dwarf2_cu *);
1068
1069 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1070 struct dwarf2_cu *);
1071
1072 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1073
1074 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1075
1076 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1077
1078 static char *typename_concat (struct obstack *obs, const char *prefix,
1079 const char *suffix, int physname,
1080 struct dwarf2_cu *cu);
1081
1082 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1083
1084 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1085
1086 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1087
1088 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1089
1090 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1091
1092 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1093 struct dwarf2_cu *, struct partial_symtab *);
1094
1095 static int dwarf2_get_pc_bounds (struct die_info *,
1096 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1097 struct partial_symtab *);
1098
1099 static void get_scope_pc_bounds (struct die_info *,
1100 CORE_ADDR *, CORE_ADDR *,
1101 struct dwarf2_cu *);
1102
1103 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1104 CORE_ADDR, struct dwarf2_cu *);
1105
1106 static void dwarf2_add_field (struct field_info *, struct die_info *,
1107 struct dwarf2_cu *);
1108
1109 static void dwarf2_attach_fields_to_type (struct field_info *,
1110 struct type *, struct dwarf2_cu *);
1111
1112 static void dwarf2_add_member_fn (struct field_info *,
1113 struct die_info *, struct type *,
1114 struct dwarf2_cu *);
1115
1116 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1117 struct type *,
1118 struct dwarf2_cu *);
1119
1120 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1121
1122 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1123
1124 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1125
1126 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1127
1128 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1129
1130 static struct type *read_module_type (struct die_info *die,
1131 struct dwarf2_cu *cu);
1132
1133 static const char *namespace_name (struct die_info *die,
1134 int *is_anonymous, struct dwarf2_cu *);
1135
1136 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1137
1138 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1139
1140 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1141 struct dwarf2_cu *);
1142
1143 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1144
1145 static struct die_info *read_die_and_children_1 (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_children (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 struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1156 gdb_byte *info_ptr,
1157 gdb_byte **new_info_ptr,
1158 struct die_info *parent);
1159
1160 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1161 struct die_info **, gdb_byte *,
1162 int *);
1163
1164 static void process_die (struct die_info *, struct dwarf2_cu *);
1165
1166 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1167 struct obstack *);
1168
1169 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1170
1171 static const char *dwarf2_full_name (char *name,
1172 struct die_info *die,
1173 struct dwarf2_cu *cu);
1174
1175 static struct die_info *dwarf2_extension (struct die_info *die,
1176 struct dwarf2_cu **);
1177
1178 static char *dwarf_tag_name (unsigned int);
1179
1180 static char *dwarf_attr_name (unsigned int);
1181
1182 static char *dwarf_form_name (unsigned int);
1183
1184 static char *dwarf_bool_name (unsigned int);
1185
1186 static char *dwarf_type_encoding_name (unsigned int);
1187
1188 #if 0
1189 static char *dwarf_cfi_name (unsigned int);
1190 #endif
1191
1192 static struct die_info *sibling_die (struct die_info *);
1193
1194 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1195
1196 static void dump_die_for_error (struct die_info *);
1197
1198 static void dump_die_1 (struct ui_file *, int level, int max_level,
1199 struct die_info *);
1200
1201 /*static*/ void dump_die (struct die_info *, int max_level);
1202
1203 static void store_in_ref_table (struct die_info *,
1204 struct dwarf2_cu *);
1205
1206 static int is_ref_attr (struct attribute *);
1207
1208 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1209
1210 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1211
1212 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1213 struct attribute *,
1214 struct dwarf2_cu **);
1215
1216 static struct die_info *follow_die_ref (struct die_info *,
1217 struct attribute *,
1218 struct dwarf2_cu **);
1219
1220 static struct die_info *follow_die_sig (struct die_info *,
1221 struct attribute *,
1222 struct dwarf2_cu **);
1223
1224 static struct signatured_type *lookup_signatured_type_at_offset
1225 (struct objfile *objfile,
1226 struct dwarf2_section_info *section,
1227 unsigned int offset);
1228
1229 static void read_signatured_type_at_offset (struct objfile *objfile,
1230 struct dwarf2_section_info *sect,
1231 unsigned int offset);
1232
1233 static void read_signatured_type (struct objfile *,
1234 struct signatured_type *type_sig);
1235
1236 /* memory allocation interface */
1237
1238 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1239
1240 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1241
1242 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1243
1244 static void initialize_cu_func_list (struct dwarf2_cu *);
1245
1246 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1247 struct dwarf2_cu *);
1248
1249 static void dwarf_decode_macros (struct line_header *, unsigned int,
1250 char *, bfd *, struct dwarf2_cu *,
1251 struct dwarf2_section_info *,
1252 int);
1253
1254 static int attr_form_is_block (struct attribute *);
1255
1256 static int attr_form_is_section_offset (struct attribute *);
1257
1258 static int attr_form_is_constant (struct attribute *);
1259
1260 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1261 struct dwarf2_loclist_baton *baton,
1262 struct attribute *attr);
1263
1264 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1265 struct symbol *sym,
1266 struct dwarf2_cu *cu);
1267
1268 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1269 struct abbrev_info *abbrev,
1270 struct dwarf2_cu *cu);
1271
1272 static void free_stack_comp_unit (void *);
1273
1274 static hashval_t partial_die_hash (const void *item);
1275
1276 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1277
1278 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1279 (unsigned int offset, struct objfile *objfile);
1280
1281 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1282 (unsigned int offset, struct objfile *objfile);
1283
1284 static void init_one_comp_unit (struct dwarf2_cu *cu,
1285 struct objfile *objfile);
1286
1287 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1288 struct die_info *comp_unit_die);
1289
1290 static void free_one_comp_unit (void *);
1291
1292 static void free_cached_comp_units (void *);
1293
1294 static void age_cached_comp_units (void);
1295
1296 static void free_one_cached_comp_unit (void *);
1297
1298 static struct type *set_die_type (struct die_info *, struct type *,
1299 struct dwarf2_cu *);
1300
1301 static void create_all_comp_units (struct objfile *);
1302
1303 static int create_debug_types_hash_table (struct objfile *objfile);
1304
1305 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1306 struct objfile *);
1307
1308 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1309
1310 static void dwarf2_add_dependence (struct dwarf2_cu *,
1311 struct dwarf2_per_cu_data *);
1312
1313 static void dwarf2_mark (struct dwarf2_cu *);
1314
1315 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1316
1317 static struct type *get_die_type_at_offset (unsigned int,
1318 struct dwarf2_per_cu_data *per_cu);
1319
1320 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1321
1322 static void dwarf2_release_queue (void *dummy);
1323
1324 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1325 struct objfile *objfile);
1326
1327 static void process_queue (struct objfile *objfile);
1328
1329 static void find_file_and_directory (struct die_info *die,
1330 struct dwarf2_cu *cu,
1331 char **name, char **comp_dir);
1332
1333 static char *file_full_name (int file, struct line_header *lh,
1334 const char *comp_dir);
1335
1336 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1337 gdb_byte *info_ptr,
1338 gdb_byte *buffer,
1339 unsigned int buffer_size,
1340 bfd *abfd,
1341 int is_debug_type_section);
1342
1343 static void init_cu_die_reader (struct die_reader_specs *reader,
1344 struct dwarf2_cu *cu);
1345
1346 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1347
1348 #if WORDS_BIGENDIAN
1349
1350 /* Convert VALUE between big- and little-endian. */
1351 static offset_type
1352 byte_swap (offset_type value)
1353 {
1354 offset_type result;
1355
1356 result = (value & 0xff) << 24;
1357 result |= (value & 0xff00) << 8;
1358 result |= (value & 0xff0000) >> 8;
1359 result |= (value & 0xff000000) >> 24;
1360 return result;
1361 }
1362
1363 #define MAYBE_SWAP(V) byte_swap (V)
1364
1365 #else
1366 #define MAYBE_SWAP(V) (V)
1367 #endif /* WORDS_BIGENDIAN */
1368
1369 /* The suffix for an index file. */
1370 #define INDEX_SUFFIX ".gdb-index"
1371
1372 static const char *dwarf2_physname (char *name, struct die_info *die,
1373 struct dwarf2_cu *cu);
1374
1375 /* Try to locate the sections we need for DWARF 2 debugging
1376 information and return true if we have enough to do something.
1377 NAMES points to the dwarf2 section names, or is NULL if the standard
1378 ELF names are used. */
1379
1380 int
1381 dwarf2_has_info (struct objfile *objfile,
1382 const struct dwarf2_debug_sections *names)
1383 {
1384 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1385 if (!dwarf2_per_objfile)
1386 {
1387 /* Initialize per-objfile state. */
1388 struct dwarf2_per_objfile *data
1389 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1390
1391 memset (data, 0, sizeof (*data));
1392 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1393 dwarf2_per_objfile = data;
1394
1395 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1396 (void *) names);
1397 dwarf2_per_objfile->objfile = objfile;
1398 }
1399 return (dwarf2_per_objfile->info.asection != NULL
1400 && dwarf2_per_objfile->abbrev.asection != NULL);
1401 }
1402
1403 /* When loading sections, we look either for uncompressed section or for
1404 compressed section names. */
1405
1406 static int
1407 section_is_p (const char *section_name,
1408 const struct dwarf2_section_names *names)
1409 {
1410 if (names->normal != NULL
1411 && strcmp (section_name, names->normal) == 0)
1412 return 1;
1413 if (names->compressed != NULL
1414 && strcmp (section_name, names->compressed) == 0)
1415 return 1;
1416 return 0;
1417 }
1418
1419 /* This function is mapped across the sections and remembers the
1420 offset and size of each of the debugging sections we are interested
1421 in. */
1422
1423 static void
1424 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1425 {
1426 const struct dwarf2_debug_sections *names;
1427
1428 if (vnames == NULL)
1429 names = &dwarf2_elf_names;
1430 else
1431 names = (const struct dwarf2_debug_sections *) vnames;
1432
1433 if (section_is_p (sectp->name, &names->info))
1434 {
1435 dwarf2_per_objfile->info.asection = sectp;
1436 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1437 }
1438 else if (section_is_p (sectp->name, &names->abbrev))
1439 {
1440 dwarf2_per_objfile->abbrev.asection = sectp;
1441 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1442 }
1443 else if (section_is_p (sectp->name, &names->line))
1444 {
1445 dwarf2_per_objfile->line.asection = sectp;
1446 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1447 }
1448 else if (section_is_p (sectp->name, &names->loc))
1449 {
1450 dwarf2_per_objfile->loc.asection = sectp;
1451 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1452 }
1453 else if (section_is_p (sectp->name, &names->macinfo))
1454 {
1455 dwarf2_per_objfile->macinfo.asection = sectp;
1456 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1457 }
1458 else if (section_is_p (sectp->name, &names->macro))
1459 {
1460 dwarf2_per_objfile->macro.asection = sectp;
1461 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1462 }
1463 else if (section_is_p (sectp->name, &names->str))
1464 {
1465 dwarf2_per_objfile->str.asection = sectp;
1466 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1467 }
1468 else if (section_is_p (sectp->name, &names->frame))
1469 {
1470 dwarf2_per_objfile->frame.asection = sectp;
1471 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1472 }
1473 else if (section_is_p (sectp->name, &names->eh_frame))
1474 {
1475 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1476
1477 if (aflag & SEC_HAS_CONTENTS)
1478 {
1479 dwarf2_per_objfile->eh_frame.asection = sectp;
1480 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1481 }
1482 }
1483 else if (section_is_p (sectp->name, &names->ranges))
1484 {
1485 dwarf2_per_objfile->ranges.asection = sectp;
1486 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1487 }
1488 else if (section_is_p (sectp->name, &names->types))
1489 {
1490 struct dwarf2_section_info type_section;
1491
1492 memset (&type_section, 0, sizeof (type_section));
1493 type_section.asection = sectp;
1494 type_section.size = bfd_get_section_size (sectp);
1495
1496 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1497 &type_section);
1498 }
1499 else if (section_is_p (sectp->name, &names->gdb_index))
1500 {
1501 dwarf2_per_objfile->gdb_index.asection = sectp;
1502 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1503 }
1504
1505 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1506 && bfd_section_vma (abfd, sectp) == 0)
1507 dwarf2_per_objfile->has_section_at_zero = 1;
1508 }
1509
1510 /* Decompress a section that was compressed using zlib. Store the
1511 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1512
1513 static void
1514 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1515 gdb_byte **outbuf, bfd_size_type *outsize)
1516 {
1517 bfd *abfd = objfile->obfd;
1518 #ifndef HAVE_ZLIB_H
1519 error (_("Support for zlib-compressed DWARF data (from '%s') "
1520 "is disabled in this copy of GDB"),
1521 bfd_get_filename (abfd));
1522 #else
1523 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1524 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1525 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1526 bfd_size_type uncompressed_size;
1527 gdb_byte *uncompressed_buffer;
1528 z_stream strm;
1529 int rc;
1530 int header_size = 12;
1531
1532 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1533 || bfd_bread (compressed_buffer,
1534 compressed_size, abfd) != compressed_size)
1535 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1536 bfd_get_filename (abfd));
1537
1538 /* Read the zlib header. In this case, it should be "ZLIB" followed
1539 by the uncompressed section size, 8 bytes in big-endian order. */
1540 if (compressed_size < header_size
1541 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1542 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1543 bfd_get_filename (abfd));
1544 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1545 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1546 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1547 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1548 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1549 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1550 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1551 uncompressed_size += compressed_buffer[11];
1552
1553 /* It is possible the section consists of several compressed
1554 buffers concatenated together, so we uncompress in a loop. */
1555 strm.zalloc = NULL;
1556 strm.zfree = NULL;
1557 strm.opaque = NULL;
1558 strm.avail_in = compressed_size - header_size;
1559 strm.next_in = (Bytef*) compressed_buffer + header_size;
1560 strm.avail_out = uncompressed_size;
1561 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1562 uncompressed_size);
1563 rc = inflateInit (&strm);
1564 while (strm.avail_in > 0)
1565 {
1566 if (rc != Z_OK)
1567 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1568 bfd_get_filename (abfd), rc);
1569 strm.next_out = ((Bytef*) uncompressed_buffer
1570 + (uncompressed_size - strm.avail_out));
1571 rc = inflate (&strm, Z_FINISH);
1572 if (rc != Z_STREAM_END)
1573 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1574 bfd_get_filename (abfd), rc);
1575 rc = inflateReset (&strm);
1576 }
1577 rc = inflateEnd (&strm);
1578 if (rc != Z_OK
1579 || strm.avail_out != 0)
1580 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1581 bfd_get_filename (abfd), rc);
1582
1583 do_cleanups (cleanup);
1584 *outbuf = uncompressed_buffer;
1585 *outsize = uncompressed_size;
1586 #endif
1587 }
1588
1589 /* A helper function that decides whether a section is empty. */
1590
1591 static int
1592 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1593 {
1594 return info->asection == NULL || info->size == 0;
1595 }
1596
1597 /* Read the contents of the section SECTP from object file specified by
1598 OBJFILE, store info about the section into INFO.
1599 If the section is compressed, uncompress it before returning. */
1600
1601 static void
1602 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1603 {
1604 bfd *abfd = objfile->obfd;
1605 asection *sectp = info->asection;
1606 gdb_byte *buf, *retbuf;
1607 unsigned char header[4];
1608
1609 if (info->readin)
1610 return;
1611 info->buffer = NULL;
1612 info->map_addr = NULL;
1613 info->readin = 1;
1614
1615 if (dwarf2_section_empty_p (info))
1616 return;
1617
1618 /* Check if the file has a 4-byte header indicating compression. */
1619 if (info->size > sizeof (header)
1620 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1621 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1622 {
1623 /* Upon decompression, update the buffer and its size. */
1624 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1625 {
1626 zlib_decompress_section (objfile, sectp, &info->buffer,
1627 &info->size);
1628 return;
1629 }
1630 }
1631
1632 #ifdef HAVE_MMAP
1633 if (pagesize == 0)
1634 pagesize = getpagesize ();
1635
1636 /* Only try to mmap sections which are large enough: we don't want to
1637 waste space due to fragmentation. Also, only try mmap for sections
1638 without relocations. */
1639
1640 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1641 {
1642 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1643 MAP_PRIVATE, sectp->filepos,
1644 &info->map_addr, &info->map_len);
1645
1646 if ((caddr_t)info->buffer != MAP_FAILED)
1647 {
1648 #if HAVE_POSIX_MADVISE
1649 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1650 #endif
1651 return;
1652 }
1653 }
1654 #endif
1655
1656 /* If we get here, we are a normal, not-compressed section. */
1657 info->buffer = buf
1658 = obstack_alloc (&objfile->objfile_obstack, info->size);
1659
1660 /* When debugging .o files, we may need to apply relocations; see
1661 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1662 We never compress sections in .o files, so we only need to
1663 try this when the section is not compressed. */
1664 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1665 if (retbuf != NULL)
1666 {
1667 info->buffer = retbuf;
1668 return;
1669 }
1670
1671 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1672 || bfd_bread (buf, info->size, abfd) != info->size)
1673 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1674 bfd_get_filename (abfd));
1675 }
1676
1677 /* A helper function that returns the size of a section in a safe way.
1678 If you are positive that the section has been read before using the
1679 size, then it is safe to refer to the dwarf2_section_info object's
1680 "size" field directly. In other cases, you must call this
1681 function, because for compressed sections the size field is not set
1682 correctly until the section has been read. */
1683
1684 static bfd_size_type
1685 dwarf2_section_size (struct objfile *objfile,
1686 struct dwarf2_section_info *info)
1687 {
1688 if (!info->readin)
1689 dwarf2_read_section (objfile, info);
1690 return info->size;
1691 }
1692
1693 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1694 SECTION_NAME. */
1695
1696 void
1697 dwarf2_get_section_info (struct objfile *objfile,
1698 enum dwarf2_section_enum sect,
1699 asection **sectp, gdb_byte **bufp,
1700 bfd_size_type *sizep)
1701 {
1702 struct dwarf2_per_objfile *data
1703 = objfile_data (objfile, dwarf2_objfile_data_key);
1704 struct dwarf2_section_info *info;
1705
1706 /* We may see an objfile without any DWARF, in which case we just
1707 return nothing. */
1708 if (data == NULL)
1709 {
1710 *sectp = NULL;
1711 *bufp = NULL;
1712 *sizep = 0;
1713 return;
1714 }
1715 switch (sect)
1716 {
1717 case DWARF2_DEBUG_FRAME:
1718 info = &data->frame;
1719 break;
1720 case DWARF2_EH_FRAME:
1721 info = &data->eh_frame;
1722 break;
1723 default:
1724 gdb_assert_not_reached ("unexpected section");
1725 }
1726
1727 dwarf2_read_section (objfile, info);
1728
1729 *sectp = info->asection;
1730 *bufp = info->buffer;
1731 *sizep = info->size;
1732 }
1733
1734 \f
1735 /* DWARF quick_symbols_functions support. */
1736
1737 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1738 unique line tables, so we maintain a separate table of all .debug_line
1739 derived entries to support the sharing.
1740 All the quick functions need is the list of file names. We discard the
1741 line_header when we're done and don't need to record it here. */
1742 struct quick_file_names
1743 {
1744 /* The offset in .debug_line of the line table. We hash on this. */
1745 unsigned int offset;
1746
1747 /* The number of entries in file_names, real_names. */
1748 unsigned int num_file_names;
1749
1750 /* The file names from the line table, after being run through
1751 file_full_name. */
1752 const char **file_names;
1753
1754 /* The file names from the line table after being run through
1755 gdb_realpath. These are computed lazily. */
1756 const char **real_names;
1757 };
1758
1759 /* When using the index (and thus not using psymtabs), each CU has an
1760 object of this type. This is used to hold information needed by
1761 the various "quick" methods. */
1762 struct dwarf2_per_cu_quick_data
1763 {
1764 /* The file table. This can be NULL if there was no file table
1765 or it's currently not read in.
1766 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1767 struct quick_file_names *file_names;
1768
1769 /* The corresponding symbol table. This is NULL if symbols for this
1770 CU have not yet been read. */
1771 struct symtab *symtab;
1772
1773 /* A temporary mark bit used when iterating over all CUs in
1774 expand_symtabs_matching. */
1775 unsigned int mark : 1;
1776
1777 /* True if we've tried to read the file table and found there isn't one.
1778 There will be no point in trying to read it again next time. */
1779 unsigned int no_file_data : 1;
1780 };
1781
1782 /* Hash function for a quick_file_names. */
1783
1784 static hashval_t
1785 hash_file_name_entry (const void *e)
1786 {
1787 const struct quick_file_names *file_data = e;
1788
1789 return file_data->offset;
1790 }
1791
1792 /* Equality function for a quick_file_names. */
1793
1794 static int
1795 eq_file_name_entry (const void *a, const void *b)
1796 {
1797 const struct quick_file_names *ea = a;
1798 const struct quick_file_names *eb = b;
1799
1800 return ea->offset == eb->offset;
1801 }
1802
1803 /* Delete function for a quick_file_names. */
1804
1805 static void
1806 delete_file_name_entry (void *e)
1807 {
1808 struct quick_file_names *file_data = e;
1809 int i;
1810
1811 for (i = 0; i < file_data->num_file_names; ++i)
1812 {
1813 xfree ((void*) file_data->file_names[i]);
1814 if (file_data->real_names)
1815 xfree ((void*) file_data->real_names[i]);
1816 }
1817
1818 /* The space for the struct itself lives on objfile_obstack,
1819 so we don't free it here. */
1820 }
1821
1822 /* Create a quick_file_names hash table. */
1823
1824 static htab_t
1825 create_quick_file_names_table (unsigned int nr_initial_entries)
1826 {
1827 return htab_create_alloc (nr_initial_entries,
1828 hash_file_name_entry, eq_file_name_entry,
1829 delete_file_name_entry, xcalloc, xfree);
1830 }
1831
1832 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1833 have to be created afterwards. You should call age_cached_comp_units after
1834 processing PER_CU->CU. dw2_setup must have been already called. */
1835
1836 static void
1837 load_cu (struct dwarf2_per_cu_data *per_cu)
1838 {
1839 if (per_cu->debug_type_section)
1840 read_signatured_type_at_offset (per_cu->objfile,
1841 per_cu->debug_type_section,
1842 per_cu->offset);
1843 else
1844 load_full_comp_unit (per_cu, per_cu->objfile);
1845
1846 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1847
1848 gdb_assert (per_cu->cu != NULL);
1849 }
1850
1851 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1852 this CU came. */
1853
1854 static void
1855 dw2_do_instantiate_symtab (struct objfile *objfile,
1856 struct dwarf2_per_cu_data *per_cu)
1857 {
1858 struct cleanup *back_to;
1859
1860 back_to = make_cleanup (dwarf2_release_queue, NULL);
1861
1862 queue_comp_unit (per_cu, objfile);
1863
1864 load_cu (per_cu);
1865
1866 process_queue (objfile);
1867
1868 /* Age the cache, releasing compilation units that have not
1869 been used recently. */
1870 age_cached_comp_units ();
1871
1872 do_cleanups (back_to);
1873 }
1874
1875 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1876 the objfile from which this CU came. Returns the resulting symbol
1877 table. */
1878
1879 static struct symtab *
1880 dw2_instantiate_symtab (struct objfile *objfile,
1881 struct dwarf2_per_cu_data *per_cu)
1882 {
1883 if (!per_cu->v.quick->symtab)
1884 {
1885 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1886 increment_reading_symtab ();
1887 dw2_do_instantiate_symtab (objfile, per_cu);
1888 do_cleanups (back_to);
1889 }
1890 return per_cu->v.quick->symtab;
1891 }
1892
1893 /* Return the CU given its index. */
1894
1895 static struct dwarf2_per_cu_data *
1896 dw2_get_cu (int index)
1897 {
1898 if (index >= dwarf2_per_objfile->n_comp_units)
1899 {
1900 index -= dwarf2_per_objfile->n_comp_units;
1901 return dwarf2_per_objfile->type_comp_units[index];
1902 }
1903 return dwarf2_per_objfile->all_comp_units[index];
1904 }
1905
1906 /* A helper function that knows how to read a 64-bit value in a way
1907 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1908 otherwise. */
1909
1910 static int
1911 extract_cu_value (const char *bytes, ULONGEST *result)
1912 {
1913 if (sizeof (ULONGEST) < 8)
1914 {
1915 int i;
1916
1917 /* Ignore the upper 4 bytes if they are all zero. */
1918 for (i = 0; i < 4; ++i)
1919 if (bytes[i + 4] != 0)
1920 return 0;
1921
1922 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1923 }
1924 else
1925 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1926 return 1;
1927 }
1928
1929 /* Read the CU list from the mapped index, and use it to create all
1930 the CU objects for this objfile. Return 0 if something went wrong,
1931 1 if everything went ok. */
1932
1933 static int
1934 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1935 offset_type cu_list_elements)
1936 {
1937 offset_type i;
1938
1939 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1940 dwarf2_per_objfile->all_comp_units
1941 = obstack_alloc (&objfile->objfile_obstack,
1942 dwarf2_per_objfile->n_comp_units
1943 * sizeof (struct dwarf2_per_cu_data *));
1944
1945 for (i = 0; i < cu_list_elements; i += 2)
1946 {
1947 struct dwarf2_per_cu_data *the_cu;
1948 ULONGEST offset, length;
1949
1950 if (!extract_cu_value (cu_list, &offset)
1951 || !extract_cu_value (cu_list + 8, &length))
1952 return 0;
1953 cu_list += 2 * 8;
1954
1955 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1956 struct dwarf2_per_cu_data);
1957 the_cu->offset = offset;
1958 the_cu->length = length;
1959 the_cu->objfile = objfile;
1960 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1961 struct dwarf2_per_cu_quick_data);
1962 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1963 }
1964
1965 return 1;
1966 }
1967
1968 /* Create the signatured type hash table from the index. */
1969
1970 static int
1971 create_signatured_type_table_from_index (struct objfile *objfile,
1972 struct dwarf2_section_info *section,
1973 const gdb_byte *bytes,
1974 offset_type elements)
1975 {
1976 offset_type i;
1977 htab_t sig_types_hash;
1978
1979 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1980 dwarf2_per_objfile->type_comp_units
1981 = obstack_alloc (&objfile->objfile_obstack,
1982 dwarf2_per_objfile->n_type_comp_units
1983 * sizeof (struct dwarf2_per_cu_data *));
1984
1985 sig_types_hash = allocate_signatured_type_table (objfile);
1986
1987 for (i = 0; i < elements; i += 3)
1988 {
1989 struct signatured_type *type_sig;
1990 ULONGEST offset, type_offset, signature;
1991 void **slot;
1992
1993 if (!extract_cu_value (bytes, &offset)
1994 || !extract_cu_value (bytes + 8, &type_offset))
1995 return 0;
1996 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1997 bytes += 3 * 8;
1998
1999 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2000 struct signatured_type);
2001 type_sig->signature = signature;
2002 type_sig->type_offset = type_offset;
2003 type_sig->per_cu.debug_type_section = section;
2004 type_sig->per_cu.offset = offset;
2005 type_sig->per_cu.objfile = objfile;
2006 type_sig->per_cu.v.quick
2007 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2008 struct dwarf2_per_cu_quick_data);
2009
2010 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
2011 *slot = type_sig;
2012
2013 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2014 }
2015
2016 dwarf2_per_objfile->signatured_types = sig_types_hash;
2017
2018 return 1;
2019 }
2020
2021 /* Read the address map data from the mapped index, and use it to
2022 populate the objfile's psymtabs_addrmap. */
2023
2024 static void
2025 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2026 {
2027 const gdb_byte *iter, *end;
2028 struct obstack temp_obstack;
2029 struct addrmap *mutable_map;
2030 struct cleanup *cleanup;
2031 CORE_ADDR baseaddr;
2032
2033 obstack_init (&temp_obstack);
2034 cleanup = make_cleanup_obstack_free (&temp_obstack);
2035 mutable_map = addrmap_create_mutable (&temp_obstack);
2036
2037 iter = index->address_table;
2038 end = iter + index->address_table_size;
2039
2040 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2041
2042 while (iter < end)
2043 {
2044 ULONGEST hi, lo, cu_index;
2045 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2046 iter += 8;
2047 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2048 iter += 8;
2049 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2050 iter += 4;
2051
2052 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2053 dw2_get_cu (cu_index));
2054 }
2055
2056 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2057 &objfile->objfile_obstack);
2058 do_cleanups (cleanup);
2059 }
2060
2061 /* The hash function for strings in the mapped index. This is the same as
2062 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2063 implementation. This is necessary because the hash function is tied to the
2064 format of the mapped index file. The hash values do not have to match with
2065 SYMBOL_HASH_NEXT.
2066
2067 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2068
2069 static hashval_t
2070 mapped_index_string_hash (int index_version, const void *p)
2071 {
2072 const unsigned char *str = (const unsigned char *) p;
2073 hashval_t r = 0;
2074 unsigned char c;
2075
2076 while ((c = *str++) != 0)
2077 {
2078 if (index_version >= 5)
2079 c = tolower (c);
2080 r = r * 67 + c - 113;
2081 }
2082
2083 return r;
2084 }
2085
2086 /* Find a slot in the mapped index INDEX for the object named NAME.
2087 If NAME is found, set *VEC_OUT to point to the CU vector in the
2088 constant pool and return 1. If NAME cannot be found, return 0. */
2089
2090 static int
2091 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2092 offset_type **vec_out)
2093 {
2094 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2095 offset_type hash;
2096 offset_type slot, step;
2097 int (*cmp) (const char *, const char *);
2098
2099 if (current_language->la_language == language_cplus
2100 || current_language->la_language == language_java
2101 || current_language->la_language == language_fortran)
2102 {
2103 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2104 not contain any. */
2105 const char *paren = strchr (name, '(');
2106
2107 if (paren)
2108 {
2109 char *dup;
2110
2111 dup = xmalloc (paren - name + 1);
2112 memcpy (dup, name, paren - name);
2113 dup[paren - name] = 0;
2114
2115 make_cleanup (xfree, dup);
2116 name = dup;
2117 }
2118 }
2119
2120 /* Index version 4 did not support case insensitive searches. But the
2121 indexes for case insensitive languages are built in lowercase, therefore
2122 simulate our NAME being searched is also lowercased. */
2123 hash = mapped_index_string_hash ((index->version == 4
2124 && case_sensitivity == case_sensitive_off
2125 ? 5 : index->version),
2126 name);
2127
2128 slot = hash & (index->symbol_table_slots - 1);
2129 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2130 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2131
2132 for (;;)
2133 {
2134 /* Convert a slot number to an offset into the table. */
2135 offset_type i = 2 * slot;
2136 const char *str;
2137 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2138 {
2139 do_cleanups (back_to);
2140 return 0;
2141 }
2142
2143 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2144 if (!cmp (name, str))
2145 {
2146 *vec_out = (offset_type *) (index->constant_pool
2147 + MAYBE_SWAP (index->symbol_table[i + 1]));
2148 do_cleanups (back_to);
2149 return 1;
2150 }
2151
2152 slot = (slot + step) & (index->symbol_table_slots - 1);
2153 }
2154 }
2155
2156 /* Read the index file. If everything went ok, initialize the "quick"
2157 elements of all the CUs and return 1. Otherwise, return 0. */
2158
2159 static int
2160 dwarf2_read_index (struct objfile *objfile)
2161 {
2162 char *addr;
2163 struct mapped_index *map;
2164 offset_type *metadata;
2165 const gdb_byte *cu_list;
2166 const gdb_byte *types_list = NULL;
2167 offset_type version, cu_list_elements;
2168 offset_type types_list_elements = 0;
2169 int i;
2170
2171 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2172 return 0;
2173
2174 /* Older elfutils strip versions could keep the section in the main
2175 executable while splitting it for the separate debug info file. */
2176 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2177 & SEC_HAS_CONTENTS) == 0)
2178 return 0;
2179
2180 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2181
2182 addr = dwarf2_per_objfile->gdb_index.buffer;
2183 /* Version check. */
2184 version = MAYBE_SWAP (*(offset_type *) addr);
2185 /* Versions earlier than 3 emitted every copy of a psymbol. This
2186 causes the index to behave very poorly for certain requests. Version 3
2187 contained incomplete addrmap. So, it seems better to just ignore such
2188 indices. Index version 4 uses a different hash function than index
2189 version 5 and later. */
2190 if (version < 4)
2191 return 0;
2192 /* Indexes with higher version than the one supported by GDB may be no
2193 longer backward compatible. */
2194 if (version > 5)
2195 return 0;
2196
2197 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2198 map->version = version;
2199 map->total_size = dwarf2_per_objfile->gdb_index.size;
2200
2201 metadata = (offset_type *) (addr + sizeof (offset_type));
2202
2203 i = 0;
2204 cu_list = addr + MAYBE_SWAP (metadata[i]);
2205 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2206 / 8);
2207 ++i;
2208
2209 types_list = addr + MAYBE_SWAP (metadata[i]);
2210 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2211 - MAYBE_SWAP (metadata[i]))
2212 / 8);
2213 ++i;
2214
2215 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2216 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2217 - MAYBE_SWAP (metadata[i]));
2218 ++i;
2219
2220 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2221 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2222 - MAYBE_SWAP (metadata[i]))
2223 / (2 * sizeof (offset_type)));
2224 ++i;
2225
2226 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2227
2228 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2229 return 0;
2230
2231 if (types_list_elements)
2232 {
2233 struct dwarf2_section_info *section;
2234
2235 /* We can only handle a single .debug_types when we have an
2236 index. */
2237 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2238 return 0;
2239
2240 section = VEC_index (dwarf2_section_info_def,
2241 dwarf2_per_objfile->types, 0);
2242
2243 if (!create_signatured_type_table_from_index (objfile, section,
2244 types_list,
2245 types_list_elements))
2246 return 0;
2247 }
2248
2249 create_addrmap_from_index (objfile, map);
2250
2251 dwarf2_per_objfile->index_table = map;
2252 dwarf2_per_objfile->using_index = 1;
2253 dwarf2_per_objfile->quick_file_names_table =
2254 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2255
2256 return 1;
2257 }
2258
2259 /* A helper for the "quick" functions which sets the global
2260 dwarf2_per_objfile according to OBJFILE. */
2261
2262 static void
2263 dw2_setup (struct objfile *objfile)
2264 {
2265 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2266 gdb_assert (dwarf2_per_objfile);
2267 }
2268
2269 /* A helper for the "quick" functions which attempts to read the line
2270 table for THIS_CU. */
2271
2272 static struct quick_file_names *
2273 dw2_get_file_names (struct objfile *objfile,
2274 struct dwarf2_per_cu_data *this_cu)
2275 {
2276 bfd *abfd = objfile->obfd;
2277 struct line_header *lh;
2278 struct attribute *attr;
2279 struct cleanup *cleanups;
2280 struct die_info *comp_unit_die;
2281 struct dwarf2_section_info* sec;
2282 gdb_byte *info_ptr, *buffer;
2283 int has_children, i;
2284 struct dwarf2_cu cu;
2285 unsigned int bytes_read, buffer_size;
2286 struct die_reader_specs reader_specs;
2287 char *name, *comp_dir;
2288 void **slot;
2289 struct quick_file_names *qfn;
2290 unsigned int line_offset;
2291
2292 if (this_cu->v.quick->file_names != NULL)
2293 return this_cu->v.quick->file_names;
2294 /* If we know there is no line data, no point in looking again. */
2295 if (this_cu->v.quick->no_file_data)
2296 return NULL;
2297
2298 init_one_comp_unit (&cu, objfile);
2299 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2300
2301 if (this_cu->debug_type_section)
2302 sec = this_cu->debug_type_section;
2303 else
2304 sec = &dwarf2_per_objfile->info;
2305 dwarf2_read_section (objfile, sec);
2306 buffer_size = sec->size;
2307 buffer = sec->buffer;
2308 info_ptr = buffer + this_cu->offset;
2309
2310 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2311 buffer, buffer_size,
2312 abfd,
2313 this_cu->debug_type_section != NULL);
2314
2315 /* Skip dummy compilation units. */
2316 if (info_ptr >= buffer + buffer_size
2317 || peek_abbrev_code (abfd, info_ptr) == 0)
2318 {
2319 do_cleanups (cleanups);
2320 return NULL;
2321 }
2322
2323 this_cu->cu = &cu;
2324 cu.per_cu = this_cu;
2325
2326 dwarf2_read_abbrevs (abfd, &cu);
2327 make_cleanup (dwarf2_free_abbrev_table, &cu);
2328
2329 init_cu_die_reader (&reader_specs, &cu);
2330 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2331 &has_children);
2332
2333 lh = NULL;
2334 slot = NULL;
2335 line_offset = 0;
2336 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2337 if (attr)
2338 {
2339 struct quick_file_names find_entry;
2340
2341 line_offset = DW_UNSND (attr);
2342
2343 /* We may have already read in this line header (TU line header sharing).
2344 If we have we're done. */
2345 find_entry.offset = line_offset;
2346 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2347 &find_entry, INSERT);
2348 if (*slot != NULL)
2349 {
2350 do_cleanups (cleanups);
2351 this_cu->v.quick->file_names = *slot;
2352 return *slot;
2353 }
2354
2355 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2356 }
2357 if (lh == NULL)
2358 {
2359 do_cleanups (cleanups);
2360 this_cu->v.quick->no_file_data = 1;
2361 return NULL;
2362 }
2363
2364 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2365 qfn->offset = line_offset;
2366 gdb_assert (slot != NULL);
2367 *slot = qfn;
2368
2369 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2370
2371 qfn->num_file_names = lh->num_file_names;
2372 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2373 lh->num_file_names * sizeof (char *));
2374 for (i = 0; i < lh->num_file_names; ++i)
2375 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2376 qfn->real_names = NULL;
2377
2378 free_line_header (lh);
2379 do_cleanups (cleanups);
2380
2381 this_cu->v.quick->file_names = qfn;
2382 return qfn;
2383 }
2384
2385 /* A helper for the "quick" functions which computes and caches the
2386 real path for a given file name from the line table. */
2387
2388 static const char *
2389 dw2_get_real_path (struct objfile *objfile,
2390 struct quick_file_names *qfn, int index)
2391 {
2392 if (qfn->real_names == NULL)
2393 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2394 qfn->num_file_names, sizeof (char *));
2395
2396 if (qfn->real_names[index] == NULL)
2397 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2398
2399 return qfn->real_names[index];
2400 }
2401
2402 static struct symtab *
2403 dw2_find_last_source_symtab (struct objfile *objfile)
2404 {
2405 int index;
2406
2407 dw2_setup (objfile);
2408 index = dwarf2_per_objfile->n_comp_units - 1;
2409 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2410 }
2411
2412 /* Traversal function for dw2_forget_cached_source_info. */
2413
2414 static int
2415 dw2_free_cached_file_names (void **slot, void *info)
2416 {
2417 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2418
2419 if (file_data->real_names)
2420 {
2421 int i;
2422
2423 for (i = 0; i < file_data->num_file_names; ++i)
2424 {
2425 xfree ((void*) file_data->real_names[i]);
2426 file_data->real_names[i] = NULL;
2427 }
2428 }
2429
2430 return 1;
2431 }
2432
2433 static void
2434 dw2_forget_cached_source_info (struct objfile *objfile)
2435 {
2436 dw2_setup (objfile);
2437
2438 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2439 dw2_free_cached_file_names, NULL);
2440 }
2441
2442 static int
2443 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2444 const char *full_path, const char *real_path,
2445 struct symtab **result)
2446 {
2447 int i;
2448 const char *name_basename = lbasename (name);
2449 int check_basename = name_basename == name;
2450 struct dwarf2_per_cu_data *base_cu = NULL;
2451
2452 dw2_setup (objfile);
2453
2454 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2455 + dwarf2_per_objfile->n_type_comp_units); ++i)
2456 {
2457 int j;
2458 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2459 struct quick_file_names *file_data;
2460
2461 /* We only need to look at symtabs not already expanded. */
2462 if (per_cu->v.quick->symtab)
2463 continue;
2464
2465 file_data = dw2_get_file_names (objfile, per_cu);
2466 if (file_data == NULL)
2467 continue;
2468
2469 for (j = 0; j < file_data->num_file_names; ++j)
2470 {
2471 const char *this_name = file_data->file_names[j];
2472
2473 if (FILENAME_CMP (name, this_name) == 0)
2474 {
2475 *result = dw2_instantiate_symtab (objfile, per_cu);
2476 return 1;
2477 }
2478
2479 if (check_basename && ! base_cu
2480 && FILENAME_CMP (lbasename (this_name), name) == 0)
2481 base_cu = per_cu;
2482
2483 /* Before we invoke realpath, which can get expensive when many
2484 files are involved, do a quick comparison of the basenames. */
2485 if (! basenames_may_differ
2486 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2487 continue;
2488
2489 if (full_path != NULL)
2490 {
2491 const char *this_real_name = dw2_get_real_path (objfile,
2492 file_data, j);
2493
2494 if (this_real_name != NULL
2495 && FILENAME_CMP (full_path, this_real_name) == 0)
2496 {
2497 *result = dw2_instantiate_symtab (objfile, per_cu);
2498 return 1;
2499 }
2500 }
2501
2502 if (real_path != NULL)
2503 {
2504 const char *this_real_name = dw2_get_real_path (objfile,
2505 file_data, j);
2506
2507 if (this_real_name != NULL
2508 && FILENAME_CMP (real_path, this_real_name) == 0)
2509 {
2510 *result = dw2_instantiate_symtab (objfile, per_cu);
2511 return 1;
2512 }
2513 }
2514 }
2515 }
2516
2517 if (base_cu)
2518 {
2519 *result = dw2_instantiate_symtab (objfile, base_cu);
2520 return 1;
2521 }
2522
2523 return 0;
2524 }
2525
2526 static struct symtab *
2527 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2528 const char *name, domain_enum domain)
2529 {
2530 /* We do all the work in the pre_expand_symtabs_matching hook
2531 instead. */
2532 return NULL;
2533 }
2534
2535 /* A helper function that expands all symtabs that hold an object
2536 named NAME. */
2537
2538 static void
2539 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2540 {
2541 dw2_setup (objfile);
2542
2543 /* index_table is NULL if OBJF_READNOW. */
2544 if (dwarf2_per_objfile->index_table)
2545 {
2546 offset_type *vec;
2547
2548 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2549 name, &vec))
2550 {
2551 offset_type i, len = MAYBE_SWAP (*vec);
2552 for (i = 0; i < len; ++i)
2553 {
2554 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2555 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2556
2557 dw2_instantiate_symtab (objfile, per_cu);
2558 }
2559 }
2560 }
2561 }
2562
2563 static void
2564 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2565 enum block_enum block_kind, const char *name,
2566 domain_enum domain)
2567 {
2568 dw2_do_expand_symtabs_matching (objfile, name);
2569 }
2570
2571 static void
2572 dw2_print_stats (struct objfile *objfile)
2573 {
2574 int i, count;
2575
2576 dw2_setup (objfile);
2577 count = 0;
2578 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2579 + dwarf2_per_objfile->n_type_comp_units); ++i)
2580 {
2581 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2582
2583 if (!per_cu->v.quick->symtab)
2584 ++count;
2585 }
2586 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2587 }
2588
2589 static void
2590 dw2_dump (struct objfile *objfile)
2591 {
2592 /* Nothing worth printing. */
2593 }
2594
2595 static void
2596 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2597 struct section_offsets *delta)
2598 {
2599 /* There's nothing to relocate here. */
2600 }
2601
2602 static void
2603 dw2_expand_symtabs_for_function (struct objfile *objfile,
2604 const char *func_name)
2605 {
2606 dw2_do_expand_symtabs_matching (objfile, func_name);
2607 }
2608
2609 static void
2610 dw2_expand_all_symtabs (struct objfile *objfile)
2611 {
2612 int i;
2613
2614 dw2_setup (objfile);
2615
2616 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2617 + dwarf2_per_objfile->n_type_comp_units); ++i)
2618 {
2619 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2620
2621 dw2_instantiate_symtab (objfile, per_cu);
2622 }
2623 }
2624
2625 static void
2626 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2627 const char *filename)
2628 {
2629 int i;
2630
2631 dw2_setup (objfile);
2632
2633 /* We don't need to consider type units here.
2634 This is only called for examining code, e.g. expand_line_sal.
2635 There can be an order of magnitude (or more) more type units
2636 than comp units, and we avoid them if we can. */
2637
2638 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2639 {
2640 int j;
2641 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2642 struct quick_file_names *file_data;
2643
2644 /* We only need to look at symtabs not already expanded. */
2645 if (per_cu->v.quick->symtab)
2646 continue;
2647
2648 file_data = dw2_get_file_names (objfile, per_cu);
2649 if (file_data == NULL)
2650 continue;
2651
2652 for (j = 0; j < file_data->num_file_names; ++j)
2653 {
2654 const char *this_name = file_data->file_names[j];
2655 if (FILENAME_CMP (this_name, filename) == 0)
2656 {
2657 dw2_instantiate_symtab (objfile, per_cu);
2658 break;
2659 }
2660 }
2661 }
2662 }
2663
2664 static const char *
2665 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2666 {
2667 struct dwarf2_per_cu_data *per_cu;
2668 offset_type *vec;
2669 struct quick_file_names *file_data;
2670
2671 dw2_setup (objfile);
2672
2673 /* index_table is NULL if OBJF_READNOW. */
2674 if (!dwarf2_per_objfile->index_table)
2675 {
2676 struct symtab *s;
2677
2678 ALL_OBJFILE_SYMTABS (objfile, s)
2679 if (s->primary)
2680 {
2681 struct blockvector *bv = BLOCKVECTOR (s);
2682 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2683 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2684
2685 if (sym)
2686 return sym->symtab->filename;
2687 }
2688 return NULL;
2689 }
2690
2691 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2692 name, &vec))
2693 return NULL;
2694
2695 /* Note that this just looks at the very first one named NAME -- but
2696 actually we are looking for a function. find_main_filename
2697 should be rewritten so that it doesn't require a custom hook. It
2698 could just use the ordinary symbol tables. */
2699 /* vec[0] is the length, which must always be >0. */
2700 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2701
2702 file_data = dw2_get_file_names (objfile, per_cu);
2703 if (file_data == NULL)
2704 return NULL;
2705
2706 return file_data->file_names[file_data->num_file_names - 1];
2707 }
2708
2709 static void
2710 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2711 struct objfile *objfile, int global,
2712 int (*callback) (struct block *,
2713 struct symbol *, void *),
2714 void *data, symbol_compare_ftype *match,
2715 symbol_compare_ftype *ordered_compare)
2716 {
2717 /* Currently unimplemented; used for Ada. The function can be called if the
2718 current language is Ada for a non-Ada objfile using GNU index. As Ada
2719 does not look for non-Ada symbols this function should just return. */
2720 }
2721
2722 static void
2723 dw2_expand_symtabs_matching (struct objfile *objfile,
2724 int (*file_matcher) (const char *, void *),
2725 int (*name_matcher) (const char *, void *),
2726 enum search_domain kind,
2727 void *data)
2728 {
2729 int i;
2730 offset_type iter;
2731 struct mapped_index *index;
2732
2733 dw2_setup (objfile);
2734
2735 /* index_table is NULL if OBJF_READNOW. */
2736 if (!dwarf2_per_objfile->index_table)
2737 return;
2738 index = dwarf2_per_objfile->index_table;
2739
2740 if (file_matcher != NULL)
2741 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2742 + dwarf2_per_objfile->n_type_comp_units); ++i)
2743 {
2744 int j;
2745 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2746 struct quick_file_names *file_data;
2747
2748 per_cu->v.quick->mark = 0;
2749
2750 /* We only need to look at symtabs not already expanded. */
2751 if (per_cu->v.quick->symtab)
2752 continue;
2753
2754 file_data = dw2_get_file_names (objfile, per_cu);
2755 if (file_data == NULL)
2756 continue;
2757
2758 for (j = 0; j < file_data->num_file_names; ++j)
2759 {
2760 if (file_matcher (file_data->file_names[j], data))
2761 {
2762 per_cu->v.quick->mark = 1;
2763 break;
2764 }
2765 }
2766 }
2767
2768 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2769 {
2770 offset_type idx = 2 * iter;
2771 const char *name;
2772 offset_type *vec, vec_len, vec_idx;
2773
2774 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2775 continue;
2776
2777 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2778
2779 if (! (*name_matcher) (name, data))
2780 continue;
2781
2782 /* The name was matched, now expand corresponding CUs that were
2783 marked. */
2784 vec = (offset_type *) (index->constant_pool
2785 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2786 vec_len = MAYBE_SWAP (vec[0]);
2787 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2788 {
2789 struct dwarf2_per_cu_data *per_cu;
2790
2791 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2792 if (file_matcher == NULL || per_cu->v.quick->mark)
2793 dw2_instantiate_symtab (objfile, per_cu);
2794 }
2795 }
2796 }
2797
2798 static struct symtab *
2799 dw2_find_pc_sect_symtab (struct objfile *objfile,
2800 struct minimal_symbol *msymbol,
2801 CORE_ADDR pc,
2802 struct obj_section *section,
2803 int warn_if_readin)
2804 {
2805 struct dwarf2_per_cu_data *data;
2806
2807 dw2_setup (objfile);
2808
2809 if (!objfile->psymtabs_addrmap)
2810 return NULL;
2811
2812 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2813 if (!data)
2814 return NULL;
2815
2816 if (warn_if_readin && data->v.quick->symtab)
2817 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2818 paddress (get_objfile_arch (objfile), pc));
2819
2820 return dw2_instantiate_symtab (objfile, data);
2821 }
2822
2823 static void
2824 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2825 void *data, int need_fullname)
2826 {
2827 int i;
2828
2829 dw2_setup (objfile);
2830
2831 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2832 + dwarf2_per_objfile->n_type_comp_units); ++i)
2833 {
2834 int j;
2835 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2836 struct quick_file_names *file_data;
2837
2838 /* We only need to look at symtabs not already expanded. */
2839 if (per_cu->v.quick->symtab)
2840 continue;
2841
2842 file_data = dw2_get_file_names (objfile, per_cu);
2843 if (file_data == NULL)
2844 continue;
2845
2846 for (j = 0; j < file_data->num_file_names; ++j)
2847 {
2848 const char *this_real_name;
2849
2850 if (need_fullname)
2851 this_real_name = dw2_get_real_path (objfile, file_data, j);
2852 else
2853 this_real_name = NULL;
2854 (*fun) (file_data->file_names[j], this_real_name, data);
2855 }
2856 }
2857 }
2858
2859 static int
2860 dw2_has_symbols (struct objfile *objfile)
2861 {
2862 return 1;
2863 }
2864
2865 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2866 {
2867 dw2_has_symbols,
2868 dw2_find_last_source_symtab,
2869 dw2_forget_cached_source_info,
2870 dw2_lookup_symtab,
2871 dw2_lookup_symbol,
2872 dw2_pre_expand_symtabs_matching,
2873 dw2_print_stats,
2874 dw2_dump,
2875 dw2_relocate,
2876 dw2_expand_symtabs_for_function,
2877 dw2_expand_all_symtabs,
2878 dw2_expand_symtabs_with_filename,
2879 dw2_find_symbol_file,
2880 dw2_map_matching_symbols,
2881 dw2_expand_symtabs_matching,
2882 dw2_find_pc_sect_symtab,
2883 dw2_map_symbol_filenames
2884 };
2885
2886 /* Initialize for reading DWARF for this objfile. Return 0 if this
2887 file will use psymtabs, or 1 if using the GNU index. */
2888
2889 int
2890 dwarf2_initialize_objfile (struct objfile *objfile)
2891 {
2892 /* If we're about to read full symbols, don't bother with the
2893 indices. In this case we also don't care if some other debug
2894 format is making psymtabs, because they are all about to be
2895 expanded anyway. */
2896 if ((objfile->flags & OBJF_READNOW))
2897 {
2898 int i;
2899
2900 dwarf2_per_objfile->using_index = 1;
2901 create_all_comp_units (objfile);
2902 create_debug_types_hash_table (objfile);
2903 dwarf2_per_objfile->quick_file_names_table =
2904 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2905
2906 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2907 + dwarf2_per_objfile->n_type_comp_units); ++i)
2908 {
2909 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2910
2911 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2912 struct dwarf2_per_cu_quick_data);
2913 }
2914
2915 /* Return 1 so that gdb sees the "quick" functions. However,
2916 these functions will be no-ops because we will have expanded
2917 all symtabs. */
2918 return 1;
2919 }
2920
2921 if (dwarf2_read_index (objfile))
2922 return 1;
2923
2924 return 0;
2925 }
2926
2927 \f
2928
2929 /* Build a partial symbol table. */
2930
2931 void
2932 dwarf2_build_psymtabs (struct objfile *objfile)
2933 {
2934 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2935 {
2936 init_psymbol_list (objfile, 1024);
2937 }
2938
2939 dwarf2_build_psymtabs_hard (objfile);
2940 }
2941
2942 /* Return TRUE if OFFSET is within CU_HEADER. */
2943
2944 static inline int
2945 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2946 {
2947 unsigned int bottom = cu_header->offset;
2948 unsigned int top = (cu_header->offset
2949 + cu_header->length
2950 + cu_header->initial_length_size);
2951
2952 return (offset >= bottom && offset < top);
2953 }
2954
2955 /* Read in the comp unit header information from the debug_info at info_ptr.
2956 NOTE: This leaves members offset, first_die_offset to be filled in
2957 by the caller. */
2958
2959 static gdb_byte *
2960 read_comp_unit_head (struct comp_unit_head *cu_header,
2961 gdb_byte *info_ptr, bfd *abfd)
2962 {
2963 int signed_addr;
2964 unsigned int bytes_read;
2965
2966 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2967 cu_header->initial_length_size = bytes_read;
2968 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2969 info_ptr += bytes_read;
2970 cu_header->version = read_2_bytes (abfd, info_ptr);
2971 info_ptr += 2;
2972 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2973 &bytes_read);
2974 info_ptr += bytes_read;
2975 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2976 info_ptr += 1;
2977 signed_addr = bfd_get_sign_extend_vma (abfd);
2978 if (signed_addr < 0)
2979 internal_error (__FILE__, __LINE__,
2980 _("read_comp_unit_head: dwarf from non elf file"));
2981 cu_header->signed_addr_p = signed_addr;
2982
2983 return info_ptr;
2984 }
2985
2986 /* Read in a CU header and perform some basic error checking. */
2987
2988 static gdb_byte *
2989 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2990 gdb_byte *buffer, unsigned int buffer_size,
2991 bfd *abfd, int is_debug_type_section)
2992 {
2993 gdb_byte *beg_of_comp_unit = info_ptr;
2994
2995 header->offset = beg_of_comp_unit - buffer;
2996
2997 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2998
2999 /* If we're reading a type unit, skip over the signature and
3000 type_offset fields. */
3001 if (is_debug_type_section)
3002 info_ptr += 8 /*signature*/ + header->offset_size;
3003
3004 header->first_die_offset = info_ptr - beg_of_comp_unit;
3005
3006 if (header->version != 2 && header->version != 3 && header->version != 4)
3007 error (_("Dwarf Error: wrong version in compilation unit header "
3008 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3009 bfd_get_filename (abfd));
3010
3011 if (header->abbrev_offset
3012 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3013 &dwarf2_per_objfile->abbrev))
3014 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3015 "(offset 0x%lx + 6) [in module %s]"),
3016 (long) header->abbrev_offset,
3017 (long) (beg_of_comp_unit - buffer),
3018 bfd_get_filename (abfd));
3019
3020 if (beg_of_comp_unit + header->length + header->initial_length_size
3021 > buffer + buffer_size)
3022 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3023 "(offset 0x%lx + 0) [in module %s]"),
3024 (long) header->length,
3025 (long) (beg_of_comp_unit - buffer),
3026 bfd_get_filename (abfd));
3027
3028 return info_ptr;
3029 }
3030
3031 /* Read in the types comp unit header information from .debug_types entry at
3032 types_ptr. The result is a pointer to one past the end of the header. */
3033
3034 static gdb_byte *
3035 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3036 struct dwarf2_section_info *section,
3037 ULONGEST *signature,
3038 gdb_byte *types_ptr, bfd *abfd)
3039 {
3040 gdb_byte *initial_types_ptr = types_ptr;
3041
3042 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3043 cu_header->offset = types_ptr - section->buffer;
3044
3045 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3046
3047 *signature = read_8_bytes (abfd, types_ptr);
3048 types_ptr += 8;
3049 types_ptr += cu_header->offset_size;
3050 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3051
3052 return types_ptr;
3053 }
3054
3055 /* Allocate a new partial symtab for file named NAME and mark this new
3056 partial symtab as being an include of PST. */
3057
3058 static void
3059 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3060 struct objfile *objfile)
3061 {
3062 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3063
3064 subpst->section_offsets = pst->section_offsets;
3065 subpst->textlow = 0;
3066 subpst->texthigh = 0;
3067
3068 subpst->dependencies = (struct partial_symtab **)
3069 obstack_alloc (&objfile->objfile_obstack,
3070 sizeof (struct partial_symtab *));
3071 subpst->dependencies[0] = pst;
3072 subpst->number_of_dependencies = 1;
3073
3074 subpst->globals_offset = 0;
3075 subpst->n_global_syms = 0;
3076 subpst->statics_offset = 0;
3077 subpst->n_static_syms = 0;
3078 subpst->symtab = NULL;
3079 subpst->read_symtab = pst->read_symtab;
3080 subpst->readin = 0;
3081
3082 /* No private part is necessary for include psymtabs. This property
3083 can be used to differentiate between such include psymtabs and
3084 the regular ones. */
3085 subpst->read_symtab_private = NULL;
3086 }
3087
3088 /* Read the Line Number Program data and extract the list of files
3089 included by the source file represented by PST. Build an include
3090 partial symtab for each of these included files. */
3091
3092 static void
3093 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3094 struct die_info *die,
3095 struct partial_symtab *pst)
3096 {
3097 struct objfile *objfile = cu->objfile;
3098 bfd *abfd = objfile->obfd;
3099 struct line_header *lh = NULL;
3100 struct attribute *attr;
3101
3102 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3103 if (attr)
3104 {
3105 unsigned int line_offset = DW_UNSND (attr);
3106
3107 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3108 }
3109 if (lh == NULL)
3110 return; /* No linetable, so no includes. */
3111
3112 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3113 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3114
3115 free_line_header (lh);
3116 }
3117
3118 static hashval_t
3119 hash_type_signature (const void *item)
3120 {
3121 const struct signatured_type *type_sig = item;
3122
3123 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3124 return type_sig->signature;
3125 }
3126
3127 static int
3128 eq_type_signature (const void *item_lhs, const void *item_rhs)
3129 {
3130 const struct signatured_type *lhs = item_lhs;
3131 const struct signatured_type *rhs = item_rhs;
3132
3133 return lhs->signature == rhs->signature;
3134 }
3135
3136 /* Allocate a hash table for signatured types. */
3137
3138 static htab_t
3139 allocate_signatured_type_table (struct objfile *objfile)
3140 {
3141 return htab_create_alloc_ex (41,
3142 hash_type_signature,
3143 eq_type_signature,
3144 NULL,
3145 &objfile->objfile_obstack,
3146 hashtab_obstack_allocate,
3147 dummy_obstack_deallocate);
3148 }
3149
3150 /* A helper function to add a signatured type CU to a list. */
3151
3152 static int
3153 add_signatured_type_cu_to_list (void **slot, void *datum)
3154 {
3155 struct signatured_type *sigt = *slot;
3156 struct dwarf2_per_cu_data ***datap = datum;
3157
3158 **datap = &sigt->per_cu;
3159 ++*datap;
3160
3161 return 1;
3162 }
3163
3164 /* Create the hash table of all entries in the .debug_types section.
3165 The result is zero if there is an error (e.g. missing .debug_types section),
3166 otherwise non-zero. */
3167
3168 static int
3169 create_debug_types_hash_table (struct objfile *objfile)
3170 {
3171 htab_t types_htab = NULL;
3172 struct dwarf2_per_cu_data **iter;
3173 int ix;
3174 struct dwarf2_section_info *section;
3175
3176 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3177 {
3178 dwarf2_per_objfile->signatured_types = NULL;
3179 return 0;
3180 }
3181
3182 for (ix = 0;
3183 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3184 ix, section);
3185 ++ix)
3186 {
3187 gdb_byte *info_ptr, *end_ptr;
3188
3189 dwarf2_read_section (objfile, section);
3190 info_ptr = section->buffer;
3191
3192 if (info_ptr == NULL)
3193 continue;
3194
3195 if (types_htab == NULL)
3196 types_htab = allocate_signatured_type_table (objfile);
3197
3198 if (dwarf2_die_debug)
3199 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3200
3201 end_ptr = info_ptr + section->size;
3202 while (info_ptr < end_ptr)
3203 {
3204 unsigned int offset;
3205 unsigned int offset_size;
3206 unsigned int type_offset;
3207 unsigned int length, initial_length_size;
3208 unsigned short version;
3209 ULONGEST signature;
3210 struct signatured_type *type_sig;
3211 void **slot;
3212 gdb_byte *ptr = info_ptr;
3213
3214 offset = ptr - section->buffer;
3215
3216 /* We need to read the type's signature in order to build the hash
3217 table, but we don't need to read anything else just yet. */
3218
3219 /* Sanity check to ensure entire cu is present. */
3220 length = read_initial_length (objfile->obfd, ptr,
3221 &initial_length_size);
3222 if (ptr + length + initial_length_size > end_ptr)
3223 {
3224 complaint (&symfile_complaints,
3225 _("debug type entry runs off end "
3226 "of `.debug_types' section, ignored"));
3227 break;
3228 }
3229
3230 offset_size = initial_length_size == 4 ? 4 : 8;
3231 ptr += initial_length_size;
3232 version = bfd_get_16 (objfile->obfd, ptr);
3233 ptr += 2;
3234 ptr += offset_size; /* abbrev offset */
3235 ptr += 1; /* address size */
3236 signature = bfd_get_64 (objfile->obfd, ptr);
3237 ptr += 8;
3238 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3239 ptr += offset_size;
3240
3241 /* Skip dummy type units. */
3242 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3243 {
3244 info_ptr = info_ptr + initial_length_size + length;
3245 continue;
3246 }
3247
3248 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3249 memset (type_sig, 0, sizeof (*type_sig));
3250 type_sig->signature = signature;
3251 type_sig->type_offset = type_offset;
3252 type_sig->per_cu.objfile = objfile;
3253 type_sig->per_cu.debug_type_section = section;
3254 type_sig->per_cu.offset = offset;
3255
3256 slot = htab_find_slot (types_htab, type_sig, INSERT);
3257 gdb_assert (slot != NULL);
3258 if (*slot != NULL)
3259 {
3260 const struct signatured_type *dup_sig = *slot;
3261
3262 complaint (&symfile_complaints,
3263 _("debug type entry at offset 0x%x is duplicate to the "
3264 "entry at offset 0x%x, signature 0x%s"),
3265 offset, dup_sig->per_cu.offset,
3266 phex (signature, sizeof (signature)));
3267 gdb_assert (signature == dup_sig->signature);
3268 }
3269 *slot = type_sig;
3270
3271 if (dwarf2_die_debug)
3272 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3273 offset, phex (signature, sizeof (signature)));
3274
3275 info_ptr = info_ptr + initial_length_size + length;
3276 }
3277 }
3278
3279 dwarf2_per_objfile->signatured_types = types_htab;
3280
3281 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3282 dwarf2_per_objfile->type_comp_units
3283 = obstack_alloc (&objfile->objfile_obstack,
3284 dwarf2_per_objfile->n_type_comp_units
3285 * sizeof (struct dwarf2_per_cu_data *));
3286 iter = &dwarf2_per_objfile->type_comp_units[0];
3287 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3288 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3289 == dwarf2_per_objfile->n_type_comp_units);
3290
3291 return 1;
3292 }
3293
3294 /* Lookup a signature based type.
3295 Returns NULL if SIG is not present in the table. */
3296
3297 static struct signatured_type *
3298 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3299 {
3300 struct signatured_type find_entry, *entry;
3301
3302 if (dwarf2_per_objfile->signatured_types == NULL)
3303 {
3304 complaint (&symfile_complaints,
3305 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3306 return 0;
3307 }
3308
3309 find_entry.signature = sig;
3310 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3311 return entry;
3312 }
3313
3314 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3315
3316 static void
3317 init_cu_die_reader (struct die_reader_specs *reader,
3318 struct dwarf2_cu *cu)
3319 {
3320 reader->abfd = cu->objfile->obfd;
3321 reader->cu = cu;
3322 if (cu->per_cu->debug_type_section)
3323 {
3324 gdb_assert (cu->per_cu->debug_type_section->readin);
3325 reader->buffer = cu->per_cu->debug_type_section->buffer;
3326 }
3327 else
3328 {
3329 gdb_assert (dwarf2_per_objfile->info.readin);
3330 reader->buffer = dwarf2_per_objfile->info.buffer;
3331 }
3332 }
3333
3334 /* Find the base address of the compilation unit for range lists and
3335 location lists. It will normally be specified by DW_AT_low_pc.
3336 In DWARF-3 draft 4, the base address could be overridden by
3337 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3338 compilation units with discontinuous ranges. */
3339
3340 static void
3341 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3342 {
3343 struct attribute *attr;
3344
3345 cu->base_known = 0;
3346 cu->base_address = 0;
3347
3348 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3349 if (attr)
3350 {
3351 cu->base_address = DW_ADDR (attr);
3352 cu->base_known = 1;
3353 }
3354 else
3355 {
3356 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3357 if (attr)
3358 {
3359 cu->base_address = DW_ADDR (attr);
3360 cu->base_known = 1;
3361 }
3362 }
3363 }
3364
3365 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3366 to combine the common parts.
3367 Process a compilation unit for a psymtab.
3368 BUFFER is a pointer to the beginning of the dwarf section buffer,
3369 either .debug_info or debug_types.
3370 INFO_PTR is a pointer to the start of the CU.
3371 Returns a pointer to the next CU. */
3372
3373 static gdb_byte *
3374 process_psymtab_comp_unit (struct objfile *objfile,
3375 struct dwarf2_per_cu_data *this_cu,
3376 gdb_byte *buffer, gdb_byte *info_ptr,
3377 unsigned int buffer_size)
3378 {
3379 bfd *abfd = objfile->obfd;
3380 gdb_byte *beg_of_comp_unit = info_ptr;
3381 struct die_info *comp_unit_die;
3382 struct partial_symtab *pst;
3383 CORE_ADDR baseaddr;
3384 struct cleanup *back_to_inner;
3385 struct dwarf2_cu cu;
3386 int has_children, has_pc_info;
3387 struct attribute *attr;
3388 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3389 struct die_reader_specs reader_specs;
3390 const char *filename;
3391
3392 init_one_comp_unit (&cu, objfile);
3393 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3394
3395 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3396 buffer, buffer_size,
3397 abfd,
3398 this_cu->debug_type_section != NULL);
3399
3400 /* Skip dummy compilation units. */
3401 if (info_ptr >= buffer + buffer_size
3402 || peek_abbrev_code (abfd, info_ptr) == 0)
3403 {
3404 info_ptr = (beg_of_comp_unit + cu.header.length
3405 + cu.header.initial_length_size);
3406 do_cleanups (back_to_inner);
3407 return info_ptr;
3408 }
3409
3410 cu.list_in_scope = &file_symbols;
3411
3412 /* If this compilation unit was already read in, free the
3413 cached copy in order to read it in again. This is
3414 necessary because we skipped some symbols when we first
3415 read in the compilation unit (see load_partial_dies).
3416 This problem could be avoided, but the benefit is
3417 unclear. */
3418 if (this_cu->cu != NULL)
3419 free_one_cached_comp_unit (this_cu->cu);
3420
3421 /* Note that this is a pointer to our stack frame, being
3422 added to a global data structure. It will be cleaned up
3423 in free_stack_comp_unit when we finish with this
3424 compilation unit. */
3425 this_cu->cu = &cu;
3426 cu.per_cu = this_cu;
3427
3428 /* Read the abbrevs for this compilation unit into a table. */
3429 dwarf2_read_abbrevs (abfd, &cu);
3430 make_cleanup (dwarf2_free_abbrev_table, &cu);
3431
3432 /* Read the compilation unit die. */
3433 init_cu_die_reader (&reader_specs, &cu);
3434 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3435 &has_children);
3436
3437 if (this_cu->debug_type_section)
3438 {
3439 /* LENGTH has not been set yet for type units. */
3440 gdb_assert (this_cu->offset == cu.header.offset);
3441 this_cu->length = cu.header.length + cu.header.initial_length_size;
3442 }
3443 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3444 {
3445 info_ptr = (beg_of_comp_unit + cu.header.length
3446 + cu.header.initial_length_size);
3447 do_cleanups (back_to_inner);
3448 return info_ptr;
3449 }
3450
3451 prepare_one_comp_unit (&cu, comp_unit_die);
3452
3453 /* Allocate a new partial symbol table structure. */
3454 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3455 if (attr == NULL || !DW_STRING (attr))
3456 filename = "";
3457 else
3458 filename = DW_STRING (attr);
3459 pst = start_psymtab_common (objfile, objfile->section_offsets,
3460 filename,
3461 /* TEXTLOW and TEXTHIGH are set below. */
3462 0,
3463 objfile->global_psymbols.next,
3464 objfile->static_psymbols.next);
3465
3466 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3467 if (attr != NULL)
3468 pst->dirname = DW_STRING (attr);
3469
3470 pst->read_symtab_private = this_cu;
3471
3472 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3473
3474 /* Store the function that reads in the rest of the symbol table. */
3475 pst->read_symtab = dwarf2_psymtab_to_symtab;
3476
3477 this_cu->v.psymtab = pst;
3478
3479 dwarf2_find_base_address (comp_unit_die, &cu);
3480
3481 /* Possibly set the default values of LOWPC and HIGHPC from
3482 `DW_AT_ranges'. */
3483 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3484 &best_highpc, &cu, pst);
3485 if (has_pc_info == 1 && best_lowpc < best_highpc)
3486 /* Store the contiguous range if it is not empty; it can be empty for
3487 CUs with no code. */
3488 addrmap_set_empty (objfile->psymtabs_addrmap,
3489 best_lowpc + baseaddr,
3490 best_highpc + baseaddr - 1, pst);
3491
3492 /* Check if comp unit has_children.
3493 If so, read the rest of the partial symbols from this comp unit.
3494 If not, there's no more debug_info for this comp unit. */
3495 if (has_children)
3496 {
3497 struct partial_die_info *first_die;
3498 CORE_ADDR lowpc, highpc;
3499
3500 lowpc = ((CORE_ADDR) -1);
3501 highpc = ((CORE_ADDR) 0);
3502
3503 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3504
3505 scan_partial_symbols (first_die, &lowpc, &highpc,
3506 ! has_pc_info, &cu);
3507
3508 /* If we didn't find a lowpc, set it to highpc to avoid
3509 complaints from `maint check'. */
3510 if (lowpc == ((CORE_ADDR) -1))
3511 lowpc = highpc;
3512
3513 /* If the compilation unit didn't have an explicit address range,
3514 then use the information extracted from its child dies. */
3515 if (! has_pc_info)
3516 {
3517 best_lowpc = lowpc;
3518 best_highpc = highpc;
3519 }
3520 }
3521 pst->textlow = best_lowpc + baseaddr;
3522 pst->texthigh = best_highpc + baseaddr;
3523
3524 pst->n_global_syms = objfile->global_psymbols.next -
3525 (objfile->global_psymbols.list + pst->globals_offset);
3526 pst->n_static_syms = objfile->static_psymbols.next -
3527 (objfile->static_psymbols.list + pst->statics_offset);
3528 sort_pst_symbols (pst);
3529
3530 info_ptr = (beg_of_comp_unit + cu.header.length
3531 + cu.header.initial_length_size);
3532
3533 if (this_cu->debug_type_section)
3534 {
3535 /* It's not clear we want to do anything with stmt lists here.
3536 Waiting to see what gcc ultimately does. */
3537 }
3538 else
3539 {
3540 /* Get the list of files included in the current compilation unit,
3541 and build a psymtab for each of them. */
3542 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3543 }
3544
3545 do_cleanups (back_to_inner);
3546
3547 return info_ptr;
3548 }
3549
3550 /* Traversal function for htab_traverse_noresize.
3551 Process one .debug_types comp-unit. */
3552
3553 static int
3554 process_type_comp_unit (void **slot, void *info)
3555 {
3556 struct signatured_type *entry = (struct signatured_type *) *slot;
3557 struct objfile *objfile = (struct objfile *) info;
3558 struct dwarf2_per_cu_data *this_cu;
3559
3560 this_cu = &entry->per_cu;
3561
3562 gdb_assert (this_cu->debug_type_section->readin);
3563 process_psymtab_comp_unit (objfile, this_cu,
3564 this_cu->debug_type_section->buffer,
3565 (this_cu->debug_type_section->buffer
3566 + this_cu->offset),
3567 this_cu->debug_type_section->size);
3568
3569 return 1;
3570 }
3571
3572 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3573 Build partial symbol tables for the .debug_types comp-units. */
3574
3575 static void
3576 build_type_psymtabs (struct objfile *objfile)
3577 {
3578 if (! create_debug_types_hash_table (objfile))
3579 return;
3580
3581 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3582 process_type_comp_unit, objfile);
3583 }
3584
3585 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3586
3587 static void
3588 psymtabs_addrmap_cleanup (void *o)
3589 {
3590 struct objfile *objfile = o;
3591
3592 objfile->psymtabs_addrmap = NULL;
3593 }
3594
3595 /* Build the partial symbol table by doing a quick pass through the
3596 .debug_info and .debug_abbrev sections. */
3597
3598 static void
3599 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3600 {
3601 gdb_byte *info_ptr;
3602 struct cleanup *back_to, *addrmap_cleanup;
3603 struct obstack temp_obstack;
3604
3605 dwarf2_per_objfile->reading_partial_symbols = 1;
3606
3607 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3608 info_ptr = dwarf2_per_objfile->info.buffer;
3609
3610 /* Any cached compilation units will be linked by the per-objfile
3611 read_in_chain. Make sure to free them when we're done. */
3612 back_to = make_cleanup (free_cached_comp_units, NULL);
3613
3614 build_type_psymtabs (objfile);
3615
3616 create_all_comp_units (objfile);
3617
3618 /* Create a temporary address map on a temporary obstack. We later
3619 copy this to the final obstack. */
3620 obstack_init (&temp_obstack);
3621 make_cleanup_obstack_free (&temp_obstack);
3622 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3623 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3624
3625 /* Since the objects we're extracting from .debug_info vary in
3626 length, only the individual functions to extract them (like
3627 read_comp_unit_head and load_partial_die) can really know whether
3628 the buffer is large enough to hold another complete object.
3629
3630 At the moment, they don't actually check that. If .debug_info
3631 holds just one extra byte after the last compilation unit's dies,
3632 then read_comp_unit_head will happily read off the end of the
3633 buffer. read_partial_die is similarly casual. Those functions
3634 should be fixed.
3635
3636 For this loop condition, simply checking whether there's any data
3637 left at all should be sufficient. */
3638
3639 while (info_ptr < (dwarf2_per_objfile->info.buffer
3640 + dwarf2_per_objfile->info.size))
3641 {
3642 struct dwarf2_per_cu_data *this_cu;
3643
3644 this_cu = dwarf2_find_comp_unit (info_ptr
3645 - dwarf2_per_objfile->info.buffer,
3646 objfile);
3647
3648 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3649 dwarf2_per_objfile->info.buffer,
3650 info_ptr,
3651 dwarf2_per_objfile->info.size);
3652 }
3653
3654 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3655 &objfile->objfile_obstack);
3656 discard_cleanups (addrmap_cleanup);
3657
3658 do_cleanups (back_to);
3659 }
3660
3661 /* Load the partial DIEs for a secondary CU into memory. */
3662
3663 static void
3664 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3665 struct objfile *objfile)
3666 {
3667 bfd *abfd = objfile->obfd;
3668 gdb_byte *info_ptr;
3669 struct die_info *comp_unit_die;
3670 struct dwarf2_cu *cu;
3671 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3672 int has_children;
3673 struct die_reader_specs reader_specs;
3674 int read_cu = 0;
3675
3676 gdb_assert (! this_cu->debug_type_section);
3677
3678 gdb_assert (dwarf2_per_objfile->info.readin);
3679 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3680
3681 if (this_cu->cu == NULL)
3682 {
3683 cu = xmalloc (sizeof (*cu));
3684 init_one_comp_unit (cu, objfile);
3685
3686 read_cu = 1;
3687
3688 /* If an error occurs while loading, release our storage. */
3689 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3690
3691 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3692 dwarf2_per_objfile->info.buffer,
3693 dwarf2_per_objfile->info.size,
3694 abfd, 0);
3695
3696 /* Skip dummy compilation units. */
3697 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3698 + dwarf2_per_objfile->info.size)
3699 || peek_abbrev_code (abfd, info_ptr) == 0)
3700 {
3701 do_cleanups (free_cu_cleanup);
3702 return;
3703 }
3704
3705 /* Link this compilation unit into the compilation unit tree. */
3706 this_cu->cu = cu;
3707 cu->per_cu = this_cu;
3708
3709 /* Link this CU into read_in_chain. */
3710 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3711 dwarf2_per_objfile->read_in_chain = this_cu;
3712 }
3713 else
3714 {
3715 cu = this_cu->cu;
3716 info_ptr += cu->header.first_die_offset;
3717 }
3718
3719 /* Read the abbrevs for this compilation unit into a table. */
3720 gdb_assert (cu->dwarf2_abbrevs == NULL);
3721 dwarf2_read_abbrevs (abfd, cu);
3722 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3723
3724 /* Read the compilation unit die. */
3725 init_cu_die_reader (&reader_specs, cu);
3726 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3727 &has_children);
3728
3729 prepare_one_comp_unit (cu, comp_unit_die);
3730
3731 /* Check if comp unit has_children.
3732 If so, read the rest of the partial symbols from this comp unit.
3733 If not, there's no more debug_info for this comp unit. */
3734 if (has_children)
3735 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3736
3737 do_cleanups (free_abbrevs_cleanup);
3738
3739 if (read_cu)
3740 {
3741 /* We've successfully allocated this compilation unit. Let our
3742 caller clean it up when finished with it. */
3743 discard_cleanups (free_cu_cleanup);
3744 }
3745 }
3746
3747 /* Create a list of all compilation units in OBJFILE. We do this only
3748 if an inter-comp-unit reference is found; presumably if there is one,
3749 there will be many, and one will occur early in the .debug_info section.
3750 So there's no point in building this list incrementally. */
3751
3752 static void
3753 create_all_comp_units (struct objfile *objfile)
3754 {
3755 int n_allocated;
3756 int n_comp_units;
3757 struct dwarf2_per_cu_data **all_comp_units;
3758 gdb_byte *info_ptr;
3759
3760 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3761 info_ptr = dwarf2_per_objfile->info.buffer;
3762
3763 n_comp_units = 0;
3764 n_allocated = 10;
3765 all_comp_units = xmalloc (n_allocated
3766 * sizeof (struct dwarf2_per_cu_data *));
3767
3768 while (info_ptr < dwarf2_per_objfile->info.buffer
3769 + dwarf2_per_objfile->info.size)
3770 {
3771 unsigned int length, initial_length_size;
3772 struct dwarf2_per_cu_data *this_cu;
3773 unsigned int offset;
3774
3775 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3776
3777 /* Read just enough information to find out where the next
3778 compilation unit is. */
3779 length = read_initial_length (objfile->obfd, info_ptr,
3780 &initial_length_size);
3781
3782 /* Save the compilation unit for later lookup. */
3783 this_cu = obstack_alloc (&objfile->objfile_obstack,
3784 sizeof (struct dwarf2_per_cu_data));
3785 memset (this_cu, 0, sizeof (*this_cu));
3786 this_cu->offset = offset;
3787 this_cu->length = length + initial_length_size;
3788 this_cu->objfile = objfile;
3789
3790 if (n_comp_units == n_allocated)
3791 {
3792 n_allocated *= 2;
3793 all_comp_units = xrealloc (all_comp_units,
3794 n_allocated
3795 * sizeof (struct dwarf2_per_cu_data *));
3796 }
3797 all_comp_units[n_comp_units++] = this_cu;
3798
3799 info_ptr = info_ptr + this_cu->length;
3800 }
3801
3802 dwarf2_per_objfile->all_comp_units
3803 = obstack_alloc (&objfile->objfile_obstack,
3804 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3805 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3806 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3807 xfree (all_comp_units);
3808 dwarf2_per_objfile->n_comp_units = n_comp_units;
3809 }
3810
3811 /* Process all loaded DIEs for compilation unit CU, starting at
3812 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3813 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3814 DW_AT_ranges). If NEED_PC is set, then this function will set
3815 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3816 and record the covered ranges in the addrmap. */
3817
3818 static void
3819 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3820 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3821 {
3822 struct partial_die_info *pdi;
3823
3824 /* Now, march along the PDI's, descending into ones which have
3825 interesting children but skipping the children of the other ones,
3826 until we reach the end of the compilation unit. */
3827
3828 pdi = first_die;
3829
3830 while (pdi != NULL)
3831 {
3832 fixup_partial_die (pdi, cu);
3833
3834 /* Anonymous namespaces or modules have no name but have interesting
3835 children, so we need to look at them. Ditto for anonymous
3836 enums. */
3837
3838 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3839 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3840 {
3841 switch (pdi->tag)
3842 {
3843 case DW_TAG_subprogram:
3844 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3845 break;
3846 case DW_TAG_constant:
3847 case DW_TAG_variable:
3848 case DW_TAG_typedef:
3849 case DW_TAG_union_type:
3850 if (!pdi->is_declaration)
3851 {
3852 add_partial_symbol (pdi, cu);
3853 }
3854 break;
3855 case DW_TAG_class_type:
3856 case DW_TAG_interface_type:
3857 case DW_TAG_structure_type:
3858 if (!pdi->is_declaration)
3859 {
3860 add_partial_symbol (pdi, cu);
3861 }
3862 break;
3863 case DW_TAG_enumeration_type:
3864 if (!pdi->is_declaration)
3865 add_partial_enumeration (pdi, cu);
3866 break;
3867 case DW_TAG_base_type:
3868 case DW_TAG_subrange_type:
3869 /* File scope base type definitions are added to the partial
3870 symbol table. */
3871 add_partial_symbol (pdi, cu);
3872 break;
3873 case DW_TAG_namespace:
3874 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3875 break;
3876 case DW_TAG_module:
3877 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3878 break;
3879 default:
3880 break;
3881 }
3882 }
3883
3884 /* If the die has a sibling, skip to the sibling. */
3885
3886 pdi = pdi->die_sibling;
3887 }
3888 }
3889
3890 /* Functions used to compute the fully scoped name of a partial DIE.
3891
3892 Normally, this is simple. For C++, the parent DIE's fully scoped
3893 name is concatenated with "::" and the partial DIE's name. For
3894 Java, the same thing occurs except that "." is used instead of "::".
3895 Enumerators are an exception; they use the scope of their parent
3896 enumeration type, i.e. the name of the enumeration type is not
3897 prepended to the enumerator.
3898
3899 There are two complexities. One is DW_AT_specification; in this
3900 case "parent" means the parent of the target of the specification,
3901 instead of the direct parent of the DIE. The other is compilers
3902 which do not emit DW_TAG_namespace; in this case we try to guess
3903 the fully qualified name of structure types from their members'
3904 linkage names. This must be done using the DIE's children rather
3905 than the children of any DW_AT_specification target. We only need
3906 to do this for structures at the top level, i.e. if the target of
3907 any DW_AT_specification (if any; otherwise the DIE itself) does not
3908 have a parent. */
3909
3910 /* Compute the scope prefix associated with PDI's parent, in
3911 compilation unit CU. The result will be allocated on CU's
3912 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3913 field. NULL is returned if no prefix is necessary. */
3914 static char *
3915 partial_die_parent_scope (struct partial_die_info *pdi,
3916 struct dwarf2_cu *cu)
3917 {
3918 char *grandparent_scope;
3919 struct partial_die_info *parent, *real_pdi;
3920
3921 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3922 then this means the parent of the specification DIE. */
3923
3924 real_pdi = pdi;
3925 while (real_pdi->has_specification)
3926 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3927
3928 parent = real_pdi->die_parent;
3929 if (parent == NULL)
3930 return NULL;
3931
3932 if (parent->scope_set)
3933 return parent->scope;
3934
3935 fixup_partial_die (parent, cu);
3936
3937 grandparent_scope = partial_die_parent_scope (parent, cu);
3938
3939 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3940 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3941 Work around this problem here. */
3942 if (cu->language == language_cplus
3943 && parent->tag == DW_TAG_namespace
3944 && strcmp (parent->name, "::") == 0
3945 && grandparent_scope == NULL)
3946 {
3947 parent->scope = NULL;
3948 parent->scope_set = 1;
3949 return NULL;
3950 }
3951
3952 if (pdi->tag == DW_TAG_enumerator)
3953 /* Enumerators should not get the name of the enumeration as a prefix. */
3954 parent->scope = grandparent_scope;
3955 else if (parent->tag == DW_TAG_namespace
3956 || parent->tag == DW_TAG_module
3957 || parent->tag == DW_TAG_structure_type
3958 || parent->tag == DW_TAG_class_type
3959 || parent->tag == DW_TAG_interface_type
3960 || parent->tag == DW_TAG_union_type
3961 || parent->tag == DW_TAG_enumeration_type)
3962 {
3963 if (grandparent_scope == NULL)
3964 parent->scope = parent->name;
3965 else
3966 parent->scope = typename_concat (&cu->comp_unit_obstack,
3967 grandparent_scope,
3968 parent->name, 0, cu);
3969 }
3970 else
3971 {
3972 /* FIXME drow/2004-04-01: What should we be doing with
3973 function-local names? For partial symbols, we should probably be
3974 ignoring them. */
3975 complaint (&symfile_complaints,
3976 _("unhandled containing DIE tag %d for DIE at %d"),
3977 parent->tag, pdi->offset);
3978 parent->scope = grandparent_scope;
3979 }
3980
3981 parent->scope_set = 1;
3982 return parent->scope;
3983 }
3984
3985 /* Return the fully scoped name associated with PDI, from compilation unit
3986 CU. The result will be allocated with malloc. */
3987 static char *
3988 partial_die_full_name (struct partial_die_info *pdi,
3989 struct dwarf2_cu *cu)
3990 {
3991 char *parent_scope;
3992
3993 /* If this is a template instantiation, we can not work out the
3994 template arguments from partial DIEs. So, unfortunately, we have
3995 to go through the full DIEs. At least any work we do building
3996 types here will be reused if full symbols are loaded later. */
3997 if (pdi->has_template_arguments)
3998 {
3999 fixup_partial_die (pdi, cu);
4000
4001 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4002 {
4003 struct die_info *die;
4004 struct attribute attr;
4005 struct dwarf2_cu *ref_cu = cu;
4006
4007 attr.name = 0;
4008 attr.form = DW_FORM_ref_addr;
4009 attr.u.addr = pdi->offset;
4010 die = follow_die_ref (NULL, &attr, &ref_cu);
4011
4012 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4013 }
4014 }
4015
4016 parent_scope = partial_die_parent_scope (pdi, cu);
4017 if (parent_scope == NULL)
4018 return NULL;
4019 else
4020 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4021 }
4022
4023 static void
4024 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4025 {
4026 struct objfile *objfile = cu->objfile;
4027 CORE_ADDR addr = 0;
4028 char *actual_name = NULL;
4029 const struct partial_symbol *psym = NULL;
4030 CORE_ADDR baseaddr;
4031 int built_actual_name = 0;
4032
4033 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4034
4035 actual_name = partial_die_full_name (pdi, cu);
4036 if (actual_name)
4037 built_actual_name = 1;
4038
4039 if (actual_name == NULL)
4040 actual_name = pdi->name;
4041
4042 switch (pdi->tag)
4043 {
4044 case DW_TAG_subprogram:
4045 if (pdi->is_external || cu->language == language_ada)
4046 {
4047 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4048 of the global scope. But in Ada, we want to be able to access
4049 nested procedures globally. So all Ada subprograms are stored
4050 in the global scope. */
4051 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4052 mst_text, objfile); */
4053 add_psymbol_to_list (actual_name, strlen (actual_name),
4054 built_actual_name,
4055 VAR_DOMAIN, LOC_BLOCK,
4056 &objfile->global_psymbols,
4057 0, pdi->lowpc + baseaddr,
4058 cu->language, objfile);
4059 }
4060 else
4061 {
4062 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4063 mst_file_text, objfile); */
4064 add_psymbol_to_list (actual_name, strlen (actual_name),
4065 built_actual_name,
4066 VAR_DOMAIN, LOC_BLOCK,
4067 &objfile->static_psymbols,
4068 0, pdi->lowpc + baseaddr,
4069 cu->language, objfile);
4070 }
4071 break;
4072 case DW_TAG_constant:
4073 {
4074 struct psymbol_allocation_list *list;
4075
4076 if (pdi->is_external)
4077 list = &objfile->global_psymbols;
4078 else
4079 list = &objfile->static_psymbols;
4080 add_psymbol_to_list (actual_name, strlen (actual_name),
4081 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4082 list, 0, 0, cu->language, objfile);
4083 }
4084 break;
4085 case DW_TAG_variable:
4086 if (pdi->locdesc)
4087 addr = decode_locdesc (pdi->locdesc, cu);
4088
4089 if (pdi->locdesc
4090 && addr == 0
4091 && !dwarf2_per_objfile->has_section_at_zero)
4092 {
4093 /* A global or static variable may also have been stripped
4094 out by the linker if unused, in which case its address
4095 will be nullified; do not add such variables into partial
4096 symbol table then. */
4097 }
4098 else if (pdi->is_external)
4099 {
4100 /* Global Variable.
4101 Don't enter into the minimal symbol tables as there is
4102 a minimal symbol table entry from the ELF symbols already.
4103 Enter into partial symbol table if it has a location
4104 descriptor or a type.
4105 If the location descriptor is missing, new_symbol will create
4106 a LOC_UNRESOLVED symbol, the address of the variable will then
4107 be determined from the minimal symbol table whenever the variable
4108 is referenced.
4109 The address for the partial symbol table entry is not
4110 used by GDB, but it comes in handy for debugging partial symbol
4111 table building. */
4112
4113 if (pdi->locdesc || pdi->has_type)
4114 add_psymbol_to_list (actual_name, strlen (actual_name),
4115 built_actual_name,
4116 VAR_DOMAIN, LOC_STATIC,
4117 &objfile->global_psymbols,
4118 0, addr + baseaddr,
4119 cu->language, objfile);
4120 }
4121 else
4122 {
4123 /* Static Variable. Skip symbols without location descriptors. */
4124 if (pdi->locdesc == NULL)
4125 {
4126 if (built_actual_name)
4127 xfree (actual_name);
4128 return;
4129 }
4130 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4131 mst_file_data, objfile); */
4132 add_psymbol_to_list (actual_name, strlen (actual_name),
4133 built_actual_name,
4134 VAR_DOMAIN, LOC_STATIC,
4135 &objfile->static_psymbols,
4136 0, addr + baseaddr,
4137 cu->language, objfile);
4138 }
4139 break;
4140 case DW_TAG_typedef:
4141 case DW_TAG_base_type:
4142 case DW_TAG_subrange_type:
4143 add_psymbol_to_list (actual_name, strlen (actual_name),
4144 built_actual_name,
4145 VAR_DOMAIN, LOC_TYPEDEF,
4146 &objfile->static_psymbols,
4147 0, (CORE_ADDR) 0, cu->language, objfile);
4148 break;
4149 case DW_TAG_namespace:
4150 add_psymbol_to_list (actual_name, strlen (actual_name),
4151 built_actual_name,
4152 VAR_DOMAIN, LOC_TYPEDEF,
4153 &objfile->global_psymbols,
4154 0, (CORE_ADDR) 0, cu->language, objfile);
4155 break;
4156 case DW_TAG_class_type:
4157 case DW_TAG_interface_type:
4158 case DW_TAG_structure_type:
4159 case DW_TAG_union_type:
4160 case DW_TAG_enumeration_type:
4161 /* Skip external references. The DWARF standard says in the section
4162 about "Structure, Union, and Class Type Entries": "An incomplete
4163 structure, union or class type is represented by a structure,
4164 union or class entry that does not have a byte size attribute
4165 and that has a DW_AT_declaration attribute." */
4166 if (!pdi->has_byte_size && pdi->is_declaration)
4167 {
4168 if (built_actual_name)
4169 xfree (actual_name);
4170 return;
4171 }
4172
4173 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4174 static vs. global. */
4175 add_psymbol_to_list (actual_name, strlen (actual_name),
4176 built_actual_name,
4177 STRUCT_DOMAIN, LOC_TYPEDEF,
4178 (cu->language == language_cplus
4179 || cu->language == language_java)
4180 ? &objfile->global_psymbols
4181 : &objfile->static_psymbols,
4182 0, (CORE_ADDR) 0, cu->language, objfile);
4183
4184 break;
4185 case DW_TAG_enumerator:
4186 add_psymbol_to_list (actual_name, strlen (actual_name),
4187 built_actual_name,
4188 VAR_DOMAIN, LOC_CONST,
4189 (cu->language == language_cplus
4190 || cu->language == language_java)
4191 ? &objfile->global_psymbols
4192 : &objfile->static_psymbols,
4193 0, (CORE_ADDR) 0, cu->language, objfile);
4194 break;
4195 default:
4196 break;
4197 }
4198
4199 if (built_actual_name)
4200 xfree (actual_name);
4201 }
4202
4203 /* Read a partial die corresponding to a namespace; also, add a symbol
4204 corresponding to that namespace to the symbol table. NAMESPACE is
4205 the name of the enclosing namespace. */
4206
4207 static void
4208 add_partial_namespace (struct partial_die_info *pdi,
4209 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4210 int need_pc, struct dwarf2_cu *cu)
4211 {
4212 /* Add a symbol for the namespace. */
4213
4214 add_partial_symbol (pdi, cu);
4215
4216 /* Now scan partial symbols in that namespace. */
4217
4218 if (pdi->has_children)
4219 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4220 }
4221
4222 /* Read a partial die corresponding to a Fortran module. */
4223
4224 static void
4225 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4226 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4227 {
4228 /* Now scan partial symbols in that module. */
4229
4230 if (pdi->has_children)
4231 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4232 }
4233
4234 /* Read a partial die corresponding to a subprogram and create a partial
4235 symbol for that subprogram. When the CU language allows it, this
4236 routine also defines a partial symbol for each nested subprogram
4237 that this subprogram contains.
4238
4239 DIE my also be a lexical block, in which case we simply search
4240 recursively for suprograms defined inside that lexical block.
4241 Again, this is only performed when the CU language allows this
4242 type of definitions. */
4243
4244 static void
4245 add_partial_subprogram (struct partial_die_info *pdi,
4246 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4247 int need_pc, struct dwarf2_cu *cu)
4248 {
4249 if (pdi->tag == DW_TAG_subprogram)
4250 {
4251 if (pdi->has_pc_info)
4252 {
4253 if (pdi->lowpc < *lowpc)
4254 *lowpc = pdi->lowpc;
4255 if (pdi->highpc > *highpc)
4256 *highpc = pdi->highpc;
4257 if (need_pc)
4258 {
4259 CORE_ADDR baseaddr;
4260 struct objfile *objfile = cu->objfile;
4261
4262 baseaddr = ANOFFSET (objfile->section_offsets,
4263 SECT_OFF_TEXT (objfile));
4264 addrmap_set_empty (objfile->psymtabs_addrmap,
4265 pdi->lowpc + baseaddr,
4266 pdi->highpc - 1 + baseaddr,
4267 cu->per_cu->v.psymtab);
4268 }
4269 if (!pdi->is_declaration)
4270 /* Ignore subprogram DIEs that do not have a name, they are
4271 illegal. Do not emit a complaint at this point, we will
4272 do so when we convert this psymtab into a symtab. */
4273 if (pdi->name)
4274 add_partial_symbol (pdi, cu);
4275 }
4276 }
4277
4278 if (! pdi->has_children)
4279 return;
4280
4281 if (cu->language == language_ada)
4282 {
4283 pdi = pdi->die_child;
4284 while (pdi != NULL)
4285 {
4286 fixup_partial_die (pdi, cu);
4287 if (pdi->tag == DW_TAG_subprogram
4288 || pdi->tag == DW_TAG_lexical_block)
4289 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4290 pdi = pdi->die_sibling;
4291 }
4292 }
4293 }
4294
4295 /* Read a partial die corresponding to an enumeration type. */
4296
4297 static void
4298 add_partial_enumeration (struct partial_die_info *enum_pdi,
4299 struct dwarf2_cu *cu)
4300 {
4301 struct partial_die_info *pdi;
4302
4303 if (enum_pdi->name != NULL)
4304 add_partial_symbol (enum_pdi, cu);
4305
4306 pdi = enum_pdi->die_child;
4307 while (pdi)
4308 {
4309 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4310 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4311 else
4312 add_partial_symbol (pdi, cu);
4313 pdi = pdi->die_sibling;
4314 }
4315 }
4316
4317 /* Return the initial uleb128 in the die at INFO_PTR. */
4318
4319 static unsigned int
4320 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4321 {
4322 unsigned int bytes_read;
4323
4324 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4325 }
4326
4327 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4328 Return the corresponding abbrev, or NULL if the number is zero (indicating
4329 an empty DIE). In either case *BYTES_READ will be set to the length of
4330 the initial number. */
4331
4332 static struct abbrev_info *
4333 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4334 struct dwarf2_cu *cu)
4335 {
4336 bfd *abfd = cu->objfile->obfd;
4337 unsigned int abbrev_number;
4338 struct abbrev_info *abbrev;
4339
4340 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4341
4342 if (abbrev_number == 0)
4343 return NULL;
4344
4345 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4346 if (!abbrev)
4347 {
4348 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4349 abbrev_number, bfd_get_filename (abfd));
4350 }
4351
4352 return abbrev;
4353 }
4354
4355 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4356 Returns a pointer to the end of a series of DIEs, terminated by an empty
4357 DIE. Any children of the skipped DIEs will also be skipped. */
4358
4359 static gdb_byte *
4360 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4361 {
4362 struct abbrev_info *abbrev;
4363 unsigned int bytes_read;
4364
4365 while (1)
4366 {
4367 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4368 if (abbrev == NULL)
4369 return info_ptr + bytes_read;
4370 else
4371 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4372 }
4373 }
4374
4375 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4376 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4377 abbrev corresponding to that skipped uleb128 should be passed in
4378 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4379 children. */
4380
4381 static gdb_byte *
4382 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4383 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4384 {
4385 unsigned int bytes_read;
4386 struct attribute attr;
4387 bfd *abfd = cu->objfile->obfd;
4388 unsigned int form, i;
4389
4390 for (i = 0; i < abbrev->num_attrs; i++)
4391 {
4392 /* The only abbrev we care about is DW_AT_sibling. */
4393 if (abbrev->attrs[i].name == DW_AT_sibling)
4394 {
4395 read_attribute (&attr, &abbrev->attrs[i],
4396 abfd, info_ptr, cu);
4397 if (attr.form == DW_FORM_ref_addr)
4398 complaint (&symfile_complaints,
4399 _("ignoring absolute DW_AT_sibling"));
4400 else
4401 return buffer + dwarf2_get_ref_die_offset (&attr);
4402 }
4403
4404 /* If it isn't DW_AT_sibling, skip this attribute. */
4405 form = abbrev->attrs[i].form;
4406 skip_attribute:
4407 switch (form)
4408 {
4409 case DW_FORM_ref_addr:
4410 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4411 and later it is offset sized. */
4412 if (cu->header.version == 2)
4413 info_ptr += cu->header.addr_size;
4414 else
4415 info_ptr += cu->header.offset_size;
4416 break;
4417 case DW_FORM_addr:
4418 info_ptr += cu->header.addr_size;
4419 break;
4420 case DW_FORM_data1:
4421 case DW_FORM_ref1:
4422 case DW_FORM_flag:
4423 info_ptr += 1;
4424 break;
4425 case DW_FORM_flag_present:
4426 break;
4427 case DW_FORM_data2:
4428 case DW_FORM_ref2:
4429 info_ptr += 2;
4430 break;
4431 case DW_FORM_data4:
4432 case DW_FORM_ref4:
4433 info_ptr += 4;
4434 break;
4435 case DW_FORM_data8:
4436 case DW_FORM_ref8:
4437 case DW_FORM_ref_sig8:
4438 info_ptr += 8;
4439 break;
4440 case DW_FORM_string:
4441 read_direct_string (abfd, info_ptr, &bytes_read);
4442 info_ptr += bytes_read;
4443 break;
4444 case DW_FORM_sec_offset:
4445 case DW_FORM_strp:
4446 info_ptr += cu->header.offset_size;
4447 break;
4448 case DW_FORM_exprloc:
4449 case DW_FORM_block:
4450 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4451 info_ptr += bytes_read;
4452 break;
4453 case DW_FORM_block1:
4454 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4455 break;
4456 case DW_FORM_block2:
4457 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4458 break;
4459 case DW_FORM_block4:
4460 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4461 break;
4462 case DW_FORM_sdata:
4463 case DW_FORM_udata:
4464 case DW_FORM_ref_udata:
4465 info_ptr = skip_leb128 (abfd, info_ptr);
4466 break;
4467 case DW_FORM_indirect:
4468 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4469 info_ptr += bytes_read;
4470 /* We need to continue parsing from here, so just go back to
4471 the top. */
4472 goto skip_attribute;
4473
4474 default:
4475 error (_("Dwarf Error: Cannot handle %s "
4476 "in DWARF reader [in module %s]"),
4477 dwarf_form_name (form),
4478 bfd_get_filename (abfd));
4479 }
4480 }
4481
4482 if (abbrev->has_children)
4483 return skip_children (buffer, info_ptr, cu);
4484 else
4485 return info_ptr;
4486 }
4487
4488 /* Locate ORIG_PDI's sibling.
4489 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4490 in BUFFER. */
4491
4492 static gdb_byte *
4493 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4494 gdb_byte *buffer, gdb_byte *info_ptr,
4495 bfd *abfd, struct dwarf2_cu *cu)
4496 {
4497 /* Do we know the sibling already? */
4498
4499 if (orig_pdi->sibling)
4500 return orig_pdi->sibling;
4501
4502 /* Are there any children to deal with? */
4503
4504 if (!orig_pdi->has_children)
4505 return info_ptr;
4506
4507 /* Skip the children the long way. */
4508
4509 return skip_children (buffer, info_ptr, cu);
4510 }
4511
4512 /* Expand this partial symbol table into a full symbol table. */
4513
4514 static void
4515 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4516 {
4517 if (pst != NULL)
4518 {
4519 if (pst->readin)
4520 {
4521 warning (_("bug: psymtab for %s is already read in."),
4522 pst->filename);
4523 }
4524 else
4525 {
4526 if (info_verbose)
4527 {
4528 printf_filtered (_("Reading in symbols for %s..."),
4529 pst->filename);
4530 gdb_flush (gdb_stdout);
4531 }
4532
4533 /* Restore our global data. */
4534 dwarf2_per_objfile = objfile_data (pst->objfile,
4535 dwarf2_objfile_data_key);
4536
4537 /* If this psymtab is constructed from a debug-only objfile, the
4538 has_section_at_zero flag will not necessarily be correct. We
4539 can get the correct value for this flag by looking at the data
4540 associated with the (presumably stripped) associated objfile. */
4541 if (pst->objfile->separate_debug_objfile_backlink)
4542 {
4543 struct dwarf2_per_objfile *dpo_backlink
4544 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4545 dwarf2_objfile_data_key);
4546
4547 dwarf2_per_objfile->has_section_at_zero
4548 = dpo_backlink->has_section_at_zero;
4549 }
4550
4551 dwarf2_per_objfile->reading_partial_symbols = 0;
4552
4553 psymtab_to_symtab_1 (pst);
4554
4555 /* Finish up the debug error message. */
4556 if (info_verbose)
4557 printf_filtered (_("done.\n"));
4558 }
4559 }
4560 }
4561
4562 /* Add PER_CU to the queue. */
4563
4564 static void
4565 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4566 {
4567 struct dwarf2_queue_item *item;
4568
4569 per_cu->queued = 1;
4570 item = xmalloc (sizeof (*item));
4571 item->per_cu = per_cu;
4572 item->next = NULL;
4573
4574 if (dwarf2_queue == NULL)
4575 dwarf2_queue = item;
4576 else
4577 dwarf2_queue_tail->next = item;
4578
4579 dwarf2_queue_tail = item;
4580 }
4581
4582 /* Process the queue. */
4583
4584 static void
4585 process_queue (struct objfile *objfile)
4586 {
4587 struct dwarf2_queue_item *item, *next_item;
4588
4589 /* The queue starts out with one item, but following a DIE reference
4590 may load a new CU, adding it to the end of the queue. */
4591 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4592 {
4593 if (dwarf2_per_objfile->using_index
4594 ? !item->per_cu->v.quick->symtab
4595 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4596 process_full_comp_unit (item->per_cu);
4597
4598 item->per_cu->queued = 0;
4599 next_item = item->next;
4600 xfree (item);
4601 }
4602
4603 dwarf2_queue_tail = NULL;
4604 }
4605
4606 /* Free all allocated queue entries. This function only releases anything if
4607 an error was thrown; if the queue was processed then it would have been
4608 freed as we went along. */
4609
4610 static void
4611 dwarf2_release_queue (void *dummy)
4612 {
4613 struct dwarf2_queue_item *item, *last;
4614
4615 item = dwarf2_queue;
4616 while (item)
4617 {
4618 /* Anything still marked queued is likely to be in an
4619 inconsistent state, so discard it. */
4620 if (item->per_cu->queued)
4621 {
4622 if (item->per_cu->cu != NULL)
4623 free_one_cached_comp_unit (item->per_cu->cu);
4624 item->per_cu->queued = 0;
4625 }
4626
4627 last = item;
4628 item = item->next;
4629 xfree (last);
4630 }
4631
4632 dwarf2_queue = dwarf2_queue_tail = NULL;
4633 }
4634
4635 /* Read in full symbols for PST, and anything it depends on. */
4636
4637 static void
4638 psymtab_to_symtab_1 (struct partial_symtab *pst)
4639 {
4640 struct dwarf2_per_cu_data *per_cu;
4641 struct cleanup *back_to;
4642 int i;
4643
4644 for (i = 0; i < pst->number_of_dependencies; i++)
4645 if (!pst->dependencies[i]->readin)
4646 {
4647 /* Inform about additional files that need to be read in. */
4648 if (info_verbose)
4649 {
4650 /* FIXME: i18n: Need to make this a single string. */
4651 fputs_filtered (" ", gdb_stdout);
4652 wrap_here ("");
4653 fputs_filtered ("and ", gdb_stdout);
4654 wrap_here ("");
4655 printf_filtered ("%s...", pst->dependencies[i]->filename);
4656 wrap_here (""); /* Flush output. */
4657 gdb_flush (gdb_stdout);
4658 }
4659 psymtab_to_symtab_1 (pst->dependencies[i]);
4660 }
4661
4662 per_cu = pst->read_symtab_private;
4663
4664 if (per_cu == NULL)
4665 {
4666 /* It's an include file, no symbols to read for it.
4667 Everything is in the parent symtab. */
4668 pst->readin = 1;
4669 return;
4670 }
4671
4672 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4673 }
4674
4675 /* Load the DIEs associated with PER_CU into memory. */
4676
4677 static void
4678 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4679 struct objfile *objfile)
4680 {
4681 bfd *abfd = objfile->obfd;
4682 struct dwarf2_cu *cu;
4683 unsigned int offset;
4684 gdb_byte *info_ptr, *beg_of_comp_unit;
4685 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4686 struct attribute *attr;
4687 int read_cu = 0;
4688
4689 gdb_assert (! per_cu->debug_type_section);
4690
4691 /* Set local variables from the partial symbol table info. */
4692 offset = per_cu->offset;
4693
4694 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4695 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4696 beg_of_comp_unit = info_ptr;
4697
4698 if (per_cu->cu == NULL)
4699 {
4700 cu = xmalloc (sizeof (*cu));
4701 init_one_comp_unit (cu, objfile);
4702
4703 read_cu = 1;
4704
4705 /* If an error occurs while loading, release our storage. */
4706 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4707
4708 /* Read in the comp_unit header. */
4709 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4710
4711 /* Skip dummy compilation units. */
4712 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4713 + dwarf2_per_objfile->info.size)
4714 || peek_abbrev_code (abfd, info_ptr) == 0)
4715 {
4716 do_cleanups (free_cu_cleanup);
4717 return;
4718 }
4719
4720 /* Complete the cu_header. */
4721 cu->header.offset = offset;
4722 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4723
4724 /* Read the abbrevs for this compilation unit. */
4725 dwarf2_read_abbrevs (abfd, cu);
4726 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4727
4728 /* Link this compilation unit into the compilation unit tree. */
4729 per_cu->cu = cu;
4730 cu->per_cu = per_cu;
4731
4732 /* Link this CU into read_in_chain. */
4733 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4734 dwarf2_per_objfile->read_in_chain = per_cu;
4735 }
4736 else
4737 {
4738 cu = per_cu->cu;
4739 info_ptr += cu->header.first_die_offset;
4740 }
4741
4742 cu->dies = read_comp_unit (info_ptr, cu);
4743
4744 /* We try not to read any attributes in this function, because not
4745 all objfiles needed for references have been loaded yet, and symbol
4746 table processing isn't initialized. But we have to set the CU language,
4747 or we won't be able to build types correctly. */
4748 prepare_one_comp_unit (cu, cu->dies);
4749
4750 /* Similarly, if we do not read the producer, we can not apply
4751 producer-specific interpretation. */
4752 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4753 if (attr)
4754 cu->producer = DW_STRING (attr);
4755
4756 if (read_cu)
4757 {
4758 do_cleanups (free_abbrevs_cleanup);
4759
4760 /* We've successfully allocated this compilation unit. Let our
4761 caller clean it up when finished with it. */
4762 discard_cleanups (free_cu_cleanup);
4763 }
4764 }
4765
4766 /* Add a DIE to the delayed physname list. */
4767
4768 static void
4769 add_to_method_list (struct type *type, int fnfield_index, int index,
4770 const char *name, struct die_info *die,
4771 struct dwarf2_cu *cu)
4772 {
4773 struct delayed_method_info mi;
4774 mi.type = type;
4775 mi.fnfield_index = fnfield_index;
4776 mi.index = index;
4777 mi.name = name;
4778 mi.die = die;
4779 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4780 }
4781
4782 /* A cleanup for freeing the delayed method list. */
4783
4784 static void
4785 free_delayed_list (void *ptr)
4786 {
4787 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4788 if (cu->method_list != NULL)
4789 {
4790 VEC_free (delayed_method_info, cu->method_list);
4791 cu->method_list = NULL;
4792 }
4793 }
4794
4795 /* Compute the physnames of any methods on the CU's method list.
4796
4797 The computation of method physnames is delayed in order to avoid the
4798 (bad) condition that one of the method's formal parameters is of an as yet
4799 incomplete type. */
4800
4801 static void
4802 compute_delayed_physnames (struct dwarf2_cu *cu)
4803 {
4804 int i;
4805 struct delayed_method_info *mi;
4806 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4807 {
4808 const char *physname;
4809 struct fn_fieldlist *fn_flp
4810 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4811 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4812 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4813 }
4814 }
4815
4816 /* Generate full symbol information for PST and CU, whose DIEs have
4817 already been loaded into memory. */
4818
4819 static void
4820 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4821 {
4822 struct dwarf2_cu *cu = per_cu->cu;
4823 struct objfile *objfile = per_cu->objfile;
4824 CORE_ADDR lowpc, highpc;
4825 struct symtab *symtab;
4826 struct cleanup *back_to, *delayed_list_cleanup;
4827 CORE_ADDR baseaddr;
4828
4829 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4830
4831 buildsym_init ();
4832 back_to = make_cleanup (really_free_pendings, NULL);
4833 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4834
4835 cu->list_in_scope = &file_symbols;
4836
4837 /* Do line number decoding in read_file_scope () */
4838 process_die (cu->dies, cu);
4839
4840 /* Now that we have processed all the DIEs in the CU, all the types
4841 should be complete, and it should now be safe to compute all of the
4842 physnames. */
4843 compute_delayed_physnames (cu);
4844 do_cleanups (delayed_list_cleanup);
4845
4846 /* Some compilers don't define a DW_AT_high_pc attribute for the
4847 compilation unit. If the DW_AT_high_pc is missing, synthesize
4848 it, by scanning the DIE's below the compilation unit. */
4849 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4850
4851 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4852
4853 if (symtab != NULL)
4854 {
4855 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4856
4857 /* Set symtab language to language from DW_AT_language. If the
4858 compilation is from a C file generated by language preprocessors, do
4859 not set the language if it was already deduced by start_subfile. */
4860 if (!(cu->language == language_c && symtab->language != language_c))
4861 symtab->language = cu->language;
4862
4863 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4864 produce DW_AT_location with location lists but it can be possibly
4865 invalid without -fvar-tracking.
4866
4867 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4868 needed, it would be wrong due to missing DW_AT_producer there.
4869
4870 Still one can confuse GDB by using non-standard GCC compilation
4871 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4872 */
4873 if (cu->has_loclist && gcc_4_minor >= 0)
4874 symtab->locations_valid = 1;
4875
4876 if (gcc_4_minor >= 5)
4877 symtab->epilogue_unwind_valid = 1;
4878
4879 symtab->call_site_htab = cu->call_site_htab;
4880 }
4881
4882 if (dwarf2_per_objfile->using_index)
4883 per_cu->v.quick->symtab = symtab;
4884 else
4885 {
4886 struct partial_symtab *pst = per_cu->v.psymtab;
4887 pst->symtab = symtab;
4888 pst->readin = 1;
4889 }
4890
4891 do_cleanups (back_to);
4892 }
4893
4894 /* Process a die and its children. */
4895
4896 static void
4897 process_die (struct die_info *die, struct dwarf2_cu *cu)
4898 {
4899 switch (die->tag)
4900 {
4901 case DW_TAG_padding:
4902 break;
4903 case DW_TAG_compile_unit:
4904 read_file_scope (die, cu);
4905 break;
4906 case DW_TAG_type_unit:
4907 read_type_unit_scope (die, cu);
4908 break;
4909 case DW_TAG_subprogram:
4910 case DW_TAG_inlined_subroutine:
4911 read_func_scope (die, cu);
4912 break;
4913 case DW_TAG_lexical_block:
4914 case DW_TAG_try_block:
4915 case DW_TAG_catch_block:
4916 read_lexical_block_scope (die, cu);
4917 break;
4918 case DW_TAG_GNU_call_site:
4919 read_call_site_scope (die, cu);
4920 break;
4921 case DW_TAG_class_type:
4922 case DW_TAG_interface_type:
4923 case DW_TAG_structure_type:
4924 case DW_TAG_union_type:
4925 process_structure_scope (die, cu);
4926 break;
4927 case DW_TAG_enumeration_type:
4928 process_enumeration_scope (die, cu);
4929 break;
4930
4931 /* These dies have a type, but processing them does not create
4932 a symbol or recurse to process the children. Therefore we can
4933 read them on-demand through read_type_die. */
4934 case DW_TAG_subroutine_type:
4935 case DW_TAG_set_type:
4936 case DW_TAG_array_type:
4937 case DW_TAG_pointer_type:
4938 case DW_TAG_ptr_to_member_type:
4939 case DW_TAG_reference_type:
4940 case DW_TAG_string_type:
4941 break;
4942
4943 case DW_TAG_base_type:
4944 case DW_TAG_subrange_type:
4945 case DW_TAG_typedef:
4946 /* Add a typedef symbol for the type definition, if it has a
4947 DW_AT_name. */
4948 new_symbol (die, read_type_die (die, cu), cu);
4949 break;
4950 case DW_TAG_common_block:
4951 read_common_block (die, cu);
4952 break;
4953 case DW_TAG_common_inclusion:
4954 break;
4955 case DW_TAG_namespace:
4956 processing_has_namespace_info = 1;
4957 read_namespace (die, cu);
4958 break;
4959 case DW_TAG_module:
4960 processing_has_namespace_info = 1;
4961 read_module (die, cu);
4962 break;
4963 case DW_TAG_imported_declaration:
4964 case DW_TAG_imported_module:
4965 processing_has_namespace_info = 1;
4966 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4967 || cu->language != language_fortran))
4968 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4969 dwarf_tag_name (die->tag));
4970 read_import_statement (die, cu);
4971 break;
4972 default:
4973 new_symbol (die, NULL, cu);
4974 break;
4975 }
4976 }
4977
4978 /* A helper function for dwarf2_compute_name which determines whether DIE
4979 needs to have the name of the scope prepended to the name listed in the
4980 die. */
4981
4982 static int
4983 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4984 {
4985 struct attribute *attr;
4986
4987 switch (die->tag)
4988 {
4989 case DW_TAG_namespace:
4990 case DW_TAG_typedef:
4991 case DW_TAG_class_type:
4992 case DW_TAG_interface_type:
4993 case DW_TAG_structure_type:
4994 case DW_TAG_union_type:
4995 case DW_TAG_enumeration_type:
4996 case DW_TAG_enumerator:
4997 case DW_TAG_subprogram:
4998 case DW_TAG_member:
4999 return 1;
5000
5001 case DW_TAG_variable:
5002 case DW_TAG_constant:
5003 /* We only need to prefix "globally" visible variables. These include
5004 any variable marked with DW_AT_external or any variable that
5005 lives in a namespace. [Variables in anonymous namespaces
5006 require prefixing, but they are not DW_AT_external.] */
5007
5008 if (dwarf2_attr (die, DW_AT_specification, cu))
5009 {
5010 struct dwarf2_cu *spec_cu = cu;
5011
5012 return die_needs_namespace (die_specification (die, &spec_cu),
5013 spec_cu);
5014 }
5015
5016 attr = dwarf2_attr (die, DW_AT_external, cu);
5017 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5018 && die->parent->tag != DW_TAG_module)
5019 return 0;
5020 /* A variable in a lexical block of some kind does not need a
5021 namespace, even though in C++ such variables may be external
5022 and have a mangled name. */
5023 if (die->parent->tag == DW_TAG_lexical_block
5024 || die->parent->tag == DW_TAG_try_block
5025 || die->parent->tag == DW_TAG_catch_block
5026 || die->parent->tag == DW_TAG_subprogram)
5027 return 0;
5028 return 1;
5029
5030 default:
5031 return 0;
5032 }
5033 }
5034
5035 /* Retrieve the last character from a mem_file. */
5036
5037 static void
5038 do_ui_file_peek_last (void *object, const char *buffer, long length)
5039 {
5040 char *last_char_p = (char *) object;
5041
5042 if (length > 0)
5043 *last_char_p = buffer[length - 1];
5044 }
5045
5046 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5047 compute the physname for the object, which include a method's
5048 formal parameters (C++/Java) and return type (Java).
5049
5050 For Ada, return the DIE's linkage name rather than the fully qualified
5051 name. PHYSNAME is ignored..
5052
5053 The result is allocated on the objfile_obstack and canonicalized. */
5054
5055 static const char *
5056 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5057 int physname)
5058 {
5059 if (name == NULL)
5060 name = dwarf2_name (die, cu);
5061
5062 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5063 compute it by typename_concat inside GDB. */
5064 if (cu->language == language_ada
5065 || (cu->language == language_fortran && physname))
5066 {
5067 /* For Ada unit, we prefer the linkage name over the name, as
5068 the former contains the exported name, which the user expects
5069 to be able to reference. Ideally, we want the user to be able
5070 to reference this entity using either natural or linkage name,
5071 but we haven't started looking at this enhancement yet. */
5072 struct attribute *attr;
5073
5074 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5075 if (attr == NULL)
5076 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5077 if (attr && DW_STRING (attr))
5078 return DW_STRING (attr);
5079 }
5080
5081 /* These are the only languages we know how to qualify names in. */
5082 if (name != NULL
5083 && (cu->language == language_cplus || cu->language == language_java
5084 || cu->language == language_fortran))
5085 {
5086 if (die_needs_namespace (die, cu))
5087 {
5088 long length;
5089 char *prefix;
5090 struct ui_file *buf;
5091
5092 prefix = determine_prefix (die, cu);
5093 buf = mem_fileopen ();
5094 if (*prefix != '\0')
5095 {
5096 char *prefixed_name = typename_concat (NULL, prefix, name,
5097 physname, cu);
5098
5099 fputs_unfiltered (prefixed_name, buf);
5100 xfree (prefixed_name);
5101 }
5102 else
5103 fputs_unfiltered (name, buf);
5104
5105 /* Template parameters may be specified in the DIE's DW_AT_name, or
5106 as children with DW_TAG_template_type_param or
5107 DW_TAG_value_type_param. If the latter, add them to the name
5108 here. If the name already has template parameters, then
5109 skip this step; some versions of GCC emit both, and
5110 it is more efficient to use the pre-computed name.
5111
5112 Something to keep in mind about this process: it is very
5113 unlikely, or in some cases downright impossible, to produce
5114 something that will match the mangled name of a function.
5115 If the definition of the function has the same debug info,
5116 we should be able to match up with it anyway. But fallbacks
5117 using the minimal symbol, for instance to find a method
5118 implemented in a stripped copy of libstdc++, will not work.
5119 If we do not have debug info for the definition, we will have to
5120 match them up some other way.
5121
5122 When we do name matching there is a related problem with function
5123 templates; two instantiated function templates are allowed to
5124 differ only by their return types, which we do not add here. */
5125
5126 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5127 {
5128 struct attribute *attr;
5129 struct die_info *child;
5130 int first = 1;
5131
5132 die->building_fullname = 1;
5133
5134 for (child = die->child; child != NULL; child = child->sibling)
5135 {
5136 struct type *type;
5137 long value;
5138 gdb_byte *bytes;
5139 struct dwarf2_locexpr_baton *baton;
5140 struct value *v;
5141
5142 if (child->tag != DW_TAG_template_type_param
5143 && child->tag != DW_TAG_template_value_param)
5144 continue;
5145
5146 if (first)
5147 {
5148 fputs_unfiltered ("<", buf);
5149 first = 0;
5150 }
5151 else
5152 fputs_unfiltered (", ", buf);
5153
5154 attr = dwarf2_attr (child, DW_AT_type, cu);
5155 if (attr == NULL)
5156 {
5157 complaint (&symfile_complaints,
5158 _("template parameter missing DW_AT_type"));
5159 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5160 continue;
5161 }
5162 type = die_type (child, cu);
5163
5164 if (child->tag == DW_TAG_template_type_param)
5165 {
5166 c_print_type (type, "", buf, -1, 0);
5167 continue;
5168 }
5169
5170 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5171 if (attr == NULL)
5172 {
5173 complaint (&symfile_complaints,
5174 _("template parameter missing "
5175 "DW_AT_const_value"));
5176 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5177 continue;
5178 }
5179
5180 dwarf2_const_value_attr (attr, type, name,
5181 &cu->comp_unit_obstack, cu,
5182 &value, &bytes, &baton);
5183
5184 if (TYPE_NOSIGN (type))
5185 /* GDB prints characters as NUMBER 'CHAR'. If that's
5186 changed, this can use value_print instead. */
5187 c_printchar (value, type, buf);
5188 else
5189 {
5190 struct value_print_options opts;
5191
5192 if (baton != NULL)
5193 v = dwarf2_evaluate_loc_desc (type, NULL,
5194 baton->data,
5195 baton->size,
5196 baton->per_cu);
5197 else if (bytes != NULL)
5198 {
5199 v = allocate_value (type);
5200 memcpy (value_contents_writeable (v), bytes,
5201 TYPE_LENGTH (type));
5202 }
5203 else
5204 v = value_from_longest (type, value);
5205
5206 /* Specify decimal so that we do not depend on
5207 the radix. */
5208 get_formatted_print_options (&opts, 'd');
5209 opts.raw = 1;
5210 value_print (v, buf, &opts);
5211 release_value (v);
5212 value_free (v);
5213 }
5214 }
5215
5216 die->building_fullname = 0;
5217
5218 if (!first)
5219 {
5220 /* Close the argument list, with a space if necessary
5221 (nested templates). */
5222 char last_char = '\0';
5223 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5224 if (last_char == '>')
5225 fputs_unfiltered (" >", buf);
5226 else
5227 fputs_unfiltered (">", buf);
5228 }
5229 }
5230
5231 /* For Java and C++ methods, append formal parameter type
5232 information, if PHYSNAME. */
5233
5234 if (physname && die->tag == DW_TAG_subprogram
5235 && (cu->language == language_cplus
5236 || cu->language == language_java))
5237 {
5238 struct type *type = read_type_die (die, cu);
5239
5240 c_type_print_args (type, buf, 1, cu->language);
5241
5242 if (cu->language == language_java)
5243 {
5244 /* For java, we must append the return type to method
5245 names. */
5246 if (die->tag == DW_TAG_subprogram)
5247 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5248 0, 0);
5249 }
5250 else if (cu->language == language_cplus)
5251 {
5252 /* Assume that an artificial first parameter is
5253 "this", but do not crash if it is not. RealView
5254 marks unnamed (and thus unused) parameters as
5255 artificial; there is no way to differentiate
5256 the two cases. */
5257 if (TYPE_NFIELDS (type) > 0
5258 && TYPE_FIELD_ARTIFICIAL (type, 0)
5259 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5260 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5261 0))))
5262 fputs_unfiltered (" const", buf);
5263 }
5264 }
5265
5266 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5267 &length);
5268 ui_file_delete (buf);
5269
5270 if (cu->language == language_cplus)
5271 {
5272 char *cname
5273 = dwarf2_canonicalize_name (name, cu,
5274 &cu->objfile->objfile_obstack);
5275
5276 if (cname != NULL)
5277 name = cname;
5278 }
5279 }
5280 }
5281
5282 return name;
5283 }
5284
5285 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5286 If scope qualifiers are appropriate they will be added. The result
5287 will be allocated on the objfile_obstack, or NULL if the DIE does
5288 not have a name. NAME may either be from a previous call to
5289 dwarf2_name or NULL.
5290
5291 The output string will be canonicalized (if C++/Java). */
5292
5293 static const char *
5294 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5295 {
5296 return dwarf2_compute_name (name, die, cu, 0);
5297 }
5298
5299 /* Construct a physname for the given DIE in CU. NAME may either be
5300 from a previous call to dwarf2_name or NULL. The result will be
5301 allocated on the objfile_objstack or NULL if the DIE does not have a
5302 name.
5303
5304 The output string will be canonicalized (if C++/Java). */
5305
5306 static const char *
5307 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5308 {
5309 struct attribute *attr;
5310 const char *retval, *mangled = NULL, *canon = NULL;
5311 struct cleanup *back_to;
5312 int need_copy = 1;
5313
5314 /* In this case dwarf2_compute_name is just a shortcut not building anything
5315 on its own. */
5316 if (!die_needs_namespace (die, cu))
5317 return dwarf2_compute_name (name, die, cu, 1);
5318
5319 back_to = make_cleanup (null_cleanup, NULL);
5320
5321 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5322 if (!attr)
5323 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5324
5325 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5326 has computed. */
5327 if (attr && DW_STRING (attr))
5328 {
5329 char *demangled;
5330
5331 mangled = DW_STRING (attr);
5332
5333 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5334 type. It is easier for GDB users to search for such functions as
5335 `name(params)' than `long name(params)'. In such case the minimal
5336 symbol names do not match the full symbol names but for template
5337 functions there is never a need to look up their definition from their
5338 declaration so the only disadvantage remains the minimal symbol
5339 variant `long name(params)' does not have the proper inferior type.
5340 */
5341
5342 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5343 | (cu->language == language_java
5344 ? DMGL_JAVA | DMGL_RET_POSTFIX
5345 : DMGL_RET_DROP)));
5346 if (demangled)
5347 {
5348 make_cleanup (xfree, demangled);
5349 canon = demangled;
5350 }
5351 else
5352 {
5353 canon = mangled;
5354 need_copy = 0;
5355 }
5356 }
5357
5358 if (canon == NULL || check_physname)
5359 {
5360 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5361
5362 if (canon != NULL && strcmp (physname, canon) != 0)
5363 {
5364 /* It may not mean a bug in GDB. The compiler could also
5365 compute DW_AT_linkage_name incorrectly. But in such case
5366 GDB would need to be bug-to-bug compatible. */
5367
5368 complaint (&symfile_complaints,
5369 _("Computed physname <%s> does not match demangled <%s> "
5370 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5371 physname, canon, mangled, die->offset, cu->objfile->name);
5372
5373 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5374 is available here - over computed PHYSNAME. It is safer
5375 against both buggy GDB and buggy compilers. */
5376
5377 retval = canon;
5378 }
5379 else
5380 {
5381 retval = physname;
5382 need_copy = 0;
5383 }
5384 }
5385 else
5386 retval = canon;
5387
5388 if (need_copy)
5389 retval = obsavestring (retval, strlen (retval),
5390 &cu->objfile->objfile_obstack);
5391
5392 do_cleanups (back_to);
5393 return retval;
5394 }
5395
5396 /* Read the import statement specified by the given die and record it. */
5397
5398 static void
5399 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5400 {
5401 struct attribute *import_attr;
5402 struct die_info *imported_die, *child_die;
5403 struct dwarf2_cu *imported_cu;
5404 const char *imported_name;
5405 const char *imported_name_prefix;
5406 const char *canonical_name;
5407 const char *import_alias;
5408 const char *imported_declaration = NULL;
5409 const char *import_prefix;
5410 VEC (const_char_ptr) *excludes = NULL;
5411 struct cleanup *cleanups;
5412
5413 char *temp;
5414
5415 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5416 if (import_attr == NULL)
5417 {
5418 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5419 dwarf_tag_name (die->tag));
5420 return;
5421 }
5422
5423 imported_cu = cu;
5424 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5425 imported_name = dwarf2_name (imported_die, imported_cu);
5426 if (imported_name == NULL)
5427 {
5428 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5429
5430 The import in the following code:
5431 namespace A
5432 {
5433 typedef int B;
5434 }
5435
5436 int main ()
5437 {
5438 using A::B;
5439 B b;
5440 return b;
5441 }
5442
5443 ...
5444 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5445 <52> DW_AT_decl_file : 1
5446 <53> DW_AT_decl_line : 6
5447 <54> DW_AT_import : <0x75>
5448 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5449 <59> DW_AT_name : B
5450 <5b> DW_AT_decl_file : 1
5451 <5c> DW_AT_decl_line : 2
5452 <5d> DW_AT_type : <0x6e>
5453 ...
5454 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5455 <76> DW_AT_byte_size : 4
5456 <77> DW_AT_encoding : 5 (signed)
5457
5458 imports the wrong die ( 0x75 instead of 0x58 ).
5459 This case will be ignored until the gcc bug is fixed. */
5460 return;
5461 }
5462
5463 /* Figure out the local name after import. */
5464 import_alias = dwarf2_name (die, cu);
5465
5466 /* Figure out where the statement is being imported to. */
5467 import_prefix = determine_prefix (die, cu);
5468
5469 /* Figure out what the scope of the imported die is and prepend it
5470 to the name of the imported die. */
5471 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5472
5473 if (imported_die->tag != DW_TAG_namespace
5474 && imported_die->tag != DW_TAG_module)
5475 {
5476 imported_declaration = imported_name;
5477 canonical_name = imported_name_prefix;
5478 }
5479 else if (strlen (imported_name_prefix) > 0)
5480 {
5481 temp = alloca (strlen (imported_name_prefix)
5482 + 2 + strlen (imported_name) + 1);
5483 strcpy (temp, imported_name_prefix);
5484 strcat (temp, "::");
5485 strcat (temp, imported_name);
5486 canonical_name = temp;
5487 }
5488 else
5489 canonical_name = imported_name;
5490
5491 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5492
5493 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5494 for (child_die = die->child; child_die && child_die->tag;
5495 child_die = sibling_die (child_die))
5496 {
5497 /* DWARF-4: A Fortran use statement with a “rename list” may be
5498 represented by an imported module entry with an import attribute
5499 referring to the module and owned entries corresponding to those
5500 entities that are renamed as part of being imported. */
5501
5502 if (child_die->tag != DW_TAG_imported_declaration)
5503 {
5504 complaint (&symfile_complaints,
5505 _("child DW_TAG_imported_declaration expected "
5506 "- DIE at 0x%x [in module %s]"),
5507 child_die->offset, cu->objfile->name);
5508 continue;
5509 }
5510
5511 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5512 if (import_attr == NULL)
5513 {
5514 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5515 dwarf_tag_name (child_die->tag));
5516 continue;
5517 }
5518
5519 imported_cu = cu;
5520 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5521 &imported_cu);
5522 imported_name = dwarf2_name (imported_die, imported_cu);
5523 if (imported_name == NULL)
5524 {
5525 complaint (&symfile_complaints,
5526 _("child DW_TAG_imported_declaration has unknown "
5527 "imported name - DIE at 0x%x [in module %s]"),
5528 child_die->offset, cu->objfile->name);
5529 continue;
5530 }
5531
5532 VEC_safe_push (const_char_ptr, excludes, imported_name);
5533
5534 process_die (child_die, cu);
5535 }
5536
5537 cp_add_using_directive (import_prefix,
5538 canonical_name,
5539 import_alias,
5540 imported_declaration,
5541 excludes,
5542 &cu->objfile->objfile_obstack);
5543
5544 do_cleanups (cleanups);
5545 }
5546
5547 static void
5548 initialize_cu_func_list (struct dwarf2_cu *cu)
5549 {
5550 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5551 }
5552
5553 /* Cleanup function for read_file_scope. */
5554
5555 static void
5556 free_cu_line_header (void *arg)
5557 {
5558 struct dwarf2_cu *cu = arg;
5559
5560 free_line_header (cu->line_header);
5561 cu->line_header = NULL;
5562 }
5563
5564 static void
5565 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5566 char **name, char **comp_dir)
5567 {
5568 struct attribute *attr;
5569
5570 *name = NULL;
5571 *comp_dir = NULL;
5572
5573 /* Find the filename. Do not use dwarf2_name here, since the filename
5574 is not a source language identifier. */
5575 attr = dwarf2_attr (die, DW_AT_name, cu);
5576 if (attr)
5577 {
5578 *name = DW_STRING (attr);
5579 }
5580
5581 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5582 if (attr)
5583 *comp_dir = DW_STRING (attr);
5584 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5585 {
5586 *comp_dir = ldirname (*name);
5587 if (*comp_dir != NULL)
5588 make_cleanup (xfree, *comp_dir);
5589 }
5590 if (*comp_dir != NULL)
5591 {
5592 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5593 directory, get rid of it. */
5594 char *cp = strchr (*comp_dir, ':');
5595
5596 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5597 *comp_dir = cp + 1;
5598 }
5599
5600 if (*name == NULL)
5601 *name = "<unknown>";
5602 }
5603
5604 /* Handle DW_AT_stmt_list for a compilation unit. */
5605
5606 static void
5607 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5608 const char *comp_dir)
5609 {
5610 struct attribute *attr;
5611 struct objfile *objfile = cu->objfile;
5612 bfd *abfd = objfile->obfd;
5613
5614 /* Decode line number information if present. We do this before
5615 processing child DIEs, so that the line header table is available
5616 for DW_AT_decl_file. */
5617 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5618 if (attr)
5619 {
5620 unsigned int line_offset = DW_UNSND (attr);
5621 struct line_header *line_header
5622 = dwarf_decode_line_header (line_offset, abfd, cu);
5623
5624 if (line_header)
5625 {
5626 cu->line_header = line_header;
5627 make_cleanup (free_cu_line_header, cu);
5628 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5629 }
5630 }
5631 }
5632
5633 /* Process DW_TAG_compile_unit. */
5634
5635 static void
5636 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5637 {
5638 struct objfile *objfile = cu->objfile;
5639 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5640 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5641 CORE_ADDR highpc = ((CORE_ADDR) 0);
5642 struct attribute *attr;
5643 char *name = NULL;
5644 char *comp_dir = NULL;
5645 struct die_info *child_die;
5646 bfd *abfd = objfile->obfd;
5647 CORE_ADDR baseaddr;
5648
5649 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5650
5651 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5652
5653 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5654 from finish_block. */
5655 if (lowpc == ((CORE_ADDR) -1))
5656 lowpc = highpc;
5657 lowpc += baseaddr;
5658 highpc += baseaddr;
5659
5660 find_file_and_directory (die, cu, &name, &comp_dir);
5661
5662 attr = dwarf2_attr (die, DW_AT_language, cu);
5663 if (attr)
5664 {
5665 set_cu_language (DW_UNSND (attr), cu);
5666 }
5667
5668 attr = dwarf2_attr (die, DW_AT_producer, cu);
5669 if (attr)
5670 cu->producer = DW_STRING (attr);
5671
5672 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5673 standardised yet. As a workaround for the language detection we fall
5674 back to the DW_AT_producer string. */
5675 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5676 cu->language = language_opencl;
5677
5678 /* We assume that we're processing GCC output. */
5679 processing_gcc_compilation = 2;
5680
5681 processing_has_namespace_info = 0;
5682
5683 start_symtab (name, comp_dir, lowpc);
5684 record_debugformat ("DWARF 2");
5685 record_producer (cu->producer);
5686
5687 initialize_cu_func_list (cu);
5688
5689 handle_DW_AT_stmt_list (die, cu, comp_dir);
5690
5691 /* Process all dies in compilation unit. */
5692 if (die->child != NULL)
5693 {
5694 child_die = die->child;
5695 while (child_die && child_die->tag)
5696 {
5697 process_die (child_die, cu);
5698 child_die = sibling_die (child_die);
5699 }
5700 }
5701
5702 /* Decode macro information, if present. Dwarf 2 macro information
5703 refers to information in the line number info statement program
5704 header, so we can only read it if we've read the header
5705 successfully. */
5706 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5707 if (attr && cu->line_header)
5708 {
5709 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5710 complaint (&symfile_complaints,
5711 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5712
5713 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5714 comp_dir, abfd, cu,
5715 &dwarf2_per_objfile->macro, 1);
5716 }
5717 else
5718 {
5719 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5720 if (attr && cu->line_header)
5721 {
5722 unsigned int macro_offset = DW_UNSND (attr);
5723
5724 dwarf_decode_macros (cu->line_header, macro_offset,
5725 comp_dir, abfd, cu,
5726 &dwarf2_per_objfile->macinfo, 0);
5727 }
5728 }
5729 do_cleanups (back_to);
5730 }
5731
5732 /* Process DW_TAG_type_unit.
5733 For TUs we want to skip the first top level sibling if it's not the
5734 actual type being defined by this TU. In this case the first top
5735 level sibling is there to provide context only. */
5736
5737 static void
5738 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5739 {
5740 struct objfile *objfile = cu->objfile;
5741 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5742 CORE_ADDR lowpc;
5743 struct attribute *attr;
5744 char *name = NULL;
5745 char *comp_dir = NULL;
5746 struct die_info *child_die;
5747 bfd *abfd = objfile->obfd;
5748
5749 /* start_symtab needs a low pc, but we don't really have one.
5750 Do what read_file_scope would do in the absence of such info. */
5751 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5752
5753 /* Find the filename. Do not use dwarf2_name here, since the filename
5754 is not a source language identifier. */
5755 attr = dwarf2_attr (die, DW_AT_name, cu);
5756 if (attr)
5757 name = DW_STRING (attr);
5758
5759 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5760 if (attr)
5761 comp_dir = DW_STRING (attr);
5762 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5763 {
5764 comp_dir = ldirname (name);
5765 if (comp_dir != NULL)
5766 make_cleanup (xfree, comp_dir);
5767 }
5768
5769 if (name == NULL)
5770 name = "<unknown>";
5771
5772 attr = dwarf2_attr (die, DW_AT_language, cu);
5773 if (attr)
5774 set_cu_language (DW_UNSND (attr), cu);
5775
5776 /* This isn't technically needed today. It is done for symmetry
5777 with read_file_scope. */
5778 attr = dwarf2_attr (die, DW_AT_producer, cu);
5779 if (attr)
5780 cu->producer = DW_STRING (attr);
5781
5782 /* We assume that we're processing GCC output. */
5783 processing_gcc_compilation = 2;
5784
5785 processing_has_namespace_info = 0;
5786
5787 start_symtab (name, comp_dir, lowpc);
5788 record_debugformat ("DWARF 2");
5789 record_producer (cu->producer);
5790
5791 handle_DW_AT_stmt_list (die, cu, comp_dir);
5792
5793 /* Process the dies in the type unit. */
5794 if (die->child == NULL)
5795 {
5796 dump_die_for_error (die);
5797 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5798 bfd_get_filename (abfd));
5799 }
5800
5801 child_die = die->child;
5802
5803 while (child_die && child_die->tag)
5804 {
5805 process_die (child_die, cu);
5806
5807 child_die = sibling_die (child_die);
5808 }
5809
5810 do_cleanups (back_to);
5811 }
5812
5813 static void
5814 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5815 struct dwarf2_cu *cu)
5816 {
5817 struct function_range *thisfn;
5818
5819 thisfn = (struct function_range *)
5820 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5821 thisfn->name = name;
5822 thisfn->lowpc = lowpc;
5823 thisfn->highpc = highpc;
5824 thisfn->seen_line = 0;
5825 thisfn->next = NULL;
5826
5827 if (cu->last_fn == NULL)
5828 cu->first_fn = thisfn;
5829 else
5830 cu->last_fn->next = thisfn;
5831
5832 cu->last_fn = thisfn;
5833 }
5834
5835 /* qsort helper for inherit_abstract_dies. */
5836
5837 static int
5838 unsigned_int_compar (const void *ap, const void *bp)
5839 {
5840 unsigned int a = *(unsigned int *) ap;
5841 unsigned int b = *(unsigned int *) bp;
5842
5843 return (a > b) - (b > a);
5844 }
5845
5846 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5847 Inherit only the children of the DW_AT_abstract_origin DIE not being
5848 already referenced by DW_AT_abstract_origin from the children of the
5849 current DIE. */
5850
5851 static void
5852 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5853 {
5854 struct die_info *child_die;
5855 unsigned die_children_count;
5856 /* CU offsets which were referenced by children of the current DIE. */
5857 unsigned *offsets;
5858 unsigned *offsets_end, *offsetp;
5859 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5860 struct die_info *origin_die;
5861 /* Iterator of the ORIGIN_DIE children. */
5862 struct die_info *origin_child_die;
5863 struct cleanup *cleanups;
5864 struct attribute *attr;
5865 struct dwarf2_cu *origin_cu;
5866 struct pending **origin_previous_list_in_scope;
5867
5868 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5869 if (!attr)
5870 return;
5871
5872 /* Note that following die references may follow to a die in a
5873 different cu. */
5874
5875 origin_cu = cu;
5876 origin_die = follow_die_ref (die, attr, &origin_cu);
5877
5878 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5879 symbols in. */
5880 origin_previous_list_in_scope = origin_cu->list_in_scope;
5881 origin_cu->list_in_scope = cu->list_in_scope;
5882
5883 if (die->tag != origin_die->tag
5884 && !(die->tag == DW_TAG_inlined_subroutine
5885 && origin_die->tag == DW_TAG_subprogram))
5886 complaint (&symfile_complaints,
5887 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5888 die->offset, origin_die->offset);
5889
5890 child_die = die->child;
5891 die_children_count = 0;
5892 while (child_die && child_die->tag)
5893 {
5894 child_die = sibling_die (child_die);
5895 die_children_count++;
5896 }
5897 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5898 cleanups = make_cleanup (xfree, offsets);
5899
5900 offsets_end = offsets;
5901 child_die = die->child;
5902 while (child_die && child_die->tag)
5903 {
5904 /* For each CHILD_DIE, find the corresponding child of
5905 ORIGIN_DIE. If there is more than one layer of
5906 DW_AT_abstract_origin, follow them all; there shouldn't be,
5907 but GCC versions at least through 4.4 generate this (GCC PR
5908 40573). */
5909 struct die_info *child_origin_die = child_die;
5910 struct dwarf2_cu *child_origin_cu = cu;
5911
5912 while (1)
5913 {
5914 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5915 child_origin_cu);
5916 if (attr == NULL)
5917 break;
5918 child_origin_die = follow_die_ref (child_origin_die, attr,
5919 &child_origin_cu);
5920 }
5921
5922 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5923 counterpart may exist. */
5924 if (child_origin_die != child_die)
5925 {
5926 if (child_die->tag != child_origin_die->tag
5927 && !(child_die->tag == DW_TAG_inlined_subroutine
5928 && child_origin_die->tag == DW_TAG_subprogram))
5929 complaint (&symfile_complaints,
5930 _("Child DIE 0x%x and its abstract origin 0x%x have "
5931 "different tags"), child_die->offset,
5932 child_origin_die->offset);
5933 if (child_origin_die->parent != origin_die)
5934 complaint (&symfile_complaints,
5935 _("Child DIE 0x%x and its abstract origin 0x%x have "
5936 "different parents"), child_die->offset,
5937 child_origin_die->offset);
5938 else
5939 *offsets_end++ = child_origin_die->offset;
5940 }
5941 child_die = sibling_die (child_die);
5942 }
5943 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5944 unsigned_int_compar);
5945 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5946 if (offsetp[-1] == *offsetp)
5947 complaint (&symfile_complaints,
5948 _("Multiple children of DIE 0x%x refer "
5949 "to DIE 0x%x as their abstract origin"),
5950 die->offset, *offsetp);
5951
5952 offsetp = offsets;
5953 origin_child_die = origin_die->child;
5954 while (origin_child_die && origin_child_die->tag)
5955 {
5956 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5957 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5958 offsetp++;
5959 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5960 {
5961 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5962 process_die (origin_child_die, origin_cu);
5963 }
5964 origin_child_die = sibling_die (origin_child_die);
5965 }
5966 origin_cu->list_in_scope = origin_previous_list_in_scope;
5967
5968 do_cleanups (cleanups);
5969 }
5970
5971 static void
5972 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5973 {
5974 struct objfile *objfile = cu->objfile;
5975 struct context_stack *new;
5976 CORE_ADDR lowpc;
5977 CORE_ADDR highpc;
5978 struct die_info *child_die;
5979 struct attribute *attr, *call_line, *call_file;
5980 char *name;
5981 CORE_ADDR baseaddr;
5982 struct block *block;
5983 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5984 VEC (symbolp) *template_args = NULL;
5985 struct template_symbol *templ_func = NULL;
5986
5987 if (inlined_func)
5988 {
5989 /* If we do not have call site information, we can't show the
5990 caller of this inlined function. That's too confusing, so
5991 only use the scope for local variables. */
5992 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5993 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5994 if (call_line == NULL || call_file == NULL)
5995 {
5996 read_lexical_block_scope (die, cu);
5997 return;
5998 }
5999 }
6000
6001 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6002
6003 name = dwarf2_name (die, cu);
6004
6005 /* Ignore functions with missing or empty names. These are actually
6006 illegal according to the DWARF standard. */
6007 if (name == NULL)
6008 {
6009 complaint (&symfile_complaints,
6010 _("missing name for subprogram DIE at %d"), die->offset);
6011 return;
6012 }
6013
6014 /* Ignore functions with missing or invalid low and high pc attributes. */
6015 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6016 {
6017 attr = dwarf2_attr (die, DW_AT_external, cu);
6018 if (!attr || !DW_UNSND (attr))
6019 complaint (&symfile_complaints,
6020 _("cannot get low and high bounds "
6021 "for subprogram DIE at %d"),
6022 die->offset);
6023 return;
6024 }
6025
6026 lowpc += baseaddr;
6027 highpc += baseaddr;
6028
6029 /* Record the function range for dwarf_decode_lines. */
6030 add_to_cu_func_list (name, lowpc, highpc, cu);
6031
6032 /* If we have any template arguments, then we must allocate a
6033 different sort of symbol. */
6034 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6035 {
6036 if (child_die->tag == DW_TAG_template_type_param
6037 || child_die->tag == DW_TAG_template_value_param)
6038 {
6039 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6040 struct template_symbol);
6041 templ_func->base.is_cplus_template_function = 1;
6042 break;
6043 }
6044 }
6045
6046 new = push_context (0, lowpc);
6047 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6048 (struct symbol *) templ_func);
6049
6050 /* If there is a location expression for DW_AT_frame_base, record
6051 it. */
6052 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6053 if (attr)
6054 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6055 expression is being recorded directly in the function's symbol
6056 and not in a separate frame-base object. I guess this hack is
6057 to avoid adding some sort of frame-base adjunct/annex to the
6058 function's symbol :-(. The problem with doing this is that it
6059 results in a function symbol with a location expression that
6060 has nothing to do with the location of the function, ouch! The
6061 relationship should be: a function's symbol has-a frame base; a
6062 frame-base has-a location expression. */
6063 dwarf2_symbol_mark_computed (attr, new->name, cu);
6064
6065 cu->list_in_scope = &local_symbols;
6066
6067 if (die->child != NULL)
6068 {
6069 child_die = die->child;
6070 while (child_die && child_die->tag)
6071 {
6072 if (child_die->tag == DW_TAG_template_type_param
6073 || child_die->tag == DW_TAG_template_value_param)
6074 {
6075 struct symbol *arg = new_symbol (child_die, NULL, cu);
6076
6077 if (arg != NULL)
6078 VEC_safe_push (symbolp, template_args, arg);
6079 }
6080 else
6081 process_die (child_die, cu);
6082 child_die = sibling_die (child_die);
6083 }
6084 }
6085
6086 inherit_abstract_dies (die, cu);
6087
6088 /* If we have a DW_AT_specification, we might need to import using
6089 directives from the context of the specification DIE. See the
6090 comment in determine_prefix. */
6091 if (cu->language == language_cplus
6092 && dwarf2_attr (die, DW_AT_specification, cu))
6093 {
6094 struct dwarf2_cu *spec_cu = cu;
6095 struct die_info *spec_die = die_specification (die, &spec_cu);
6096
6097 while (spec_die)
6098 {
6099 child_die = spec_die->child;
6100 while (child_die && child_die->tag)
6101 {
6102 if (child_die->tag == DW_TAG_imported_module)
6103 process_die (child_die, spec_cu);
6104 child_die = sibling_die (child_die);
6105 }
6106
6107 /* In some cases, GCC generates specification DIEs that
6108 themselves contain DW_AT_specification attributes. */
6109 spec_die = die_specification (spec_die, &spec_cu);
6110 }
6111 }
6112
6113 new = pop_context ();
6114 /* Make a block for the local symbols within. */
6115 block = finish_block (new->name, &local_symbols, new->old_blocks,
6116 lowpc, highpc, objfile);
6117
6118 /* For C++, set the block's scope. */
6119 if (cu->language == language_cplus || cu->language == language_fortran)
6120 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6121 determine_prefix (die, cu),
6122 processing_has_namespace_info);
6123
6124 /* If we have address ranges, record them. */
6125 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6126
6127 /* Attach template arguments to function. */
6128 if (! VEC_empty (symbolp, template_args))
6129 {
6130 gdb_assert (templ_func != NULL);
6131
6132 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6133 templ_func->template_arguments
6134 = obstack_alloc (&objfile->objfile_obstack,
6135 (templ_func->n_template_arguments
6136 * sizeof (struct symbol *)));
6137 memcpy (templ_func->template_arguments,
6138 VEC_address (symbolp, template_args),
6139 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6140 VEC_free (symbolp, template_args);
6141 }
6142
6143 /* In C++, we can have functions nested inside functions (e.g., when
6144 a function declares a class that has methods). This means that
6145 when we finish processing a function scope, we may need to go
6146 back to building a containing block's symbol lists. */
6147 local_symbols = new->locals;
6148 param_symbols = new->params;
6149 using_directives = new->using_directives;
6150
6151 /* If we've finished processing a top-level function, subsequent
6152 symbols go in the file symbol list. */
6153 if (outermost_context_p ())
6154 cu->list_in_scope = &file_symbols;
6155 }
6156
6157 /* Process all the DIES contained within a lexical block scope. Start
6158 a new scope, process the dies, and then close the scope. */
6159
6160 static void
6161 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6162 {
6163 struct objfile *objfile = cu->objfile;
6164 struct context_stack *new;
6165 CORE_ADDR lowpc, highpc;
6166 struct die_info *child_die;
6167 CORE_ADDR baseaddr;
6168
6169 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6170
6171 /* Ignore blocks with missing or invalid low and high pc attributes. */
6172 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6173 as multiple lexical blocks? Handling children in a sane way would
6174 be nasty. Might be easier to properly extend generic blocks to
6175 describe ranges. */
6176 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6177 return;
6178 lowpc += baseaddr;
6179 highpc += baseaddr;
6180
6181 push_context (0, lowpc);
6182 if (die->child != NULL)
6183 {
6184 child_die = die->child;
6185 while (child_die && child_die->tag)
6186 {
6187 process_die (child_die, cu);
6188 child_die = sibling_die (child_die);
6189 }
6190 }
6191 new = pop_context ();
6192
6193 if (local_symbols != NULL || using_directives != NULL)
6194 {
6195 struct block *block
6196 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6197 highpc, objfile);
6198
6199 /* Note that recording ranges after traversing children, as we
6200 do here, means that recording a parent's ranges entails
6201 walking across all its children's ranges as they appear in
6202 the address map, which is quadratic behavior.
6203
6204 It would be nicer to record the parent's ranges before
6205 traversing its children, simply overriding whatever you find
6206 there. But since we don't even decide whether to create a
6207 block until after we've traversed its children, that's hard
6208 to do. */
6209 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6210 }
6211 local_symbols = new->locals;
6212 using_directives = new->using_directives;
6213 }
6214
6215 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6216
6217 static void
6218 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6219 {
6220 struct objfile *objfile = cu->objfile;
6221 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6222 CORE_ADDR pc, baseaddr;
6223 struct attribute *attr;
6224 struct call_site *call_site, call_site_local;
6225 void **slot;
6226 int nparams;
6227 struct die_info *child_die;
6228
6229 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6230
6231 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6232 if (!attr)
6233 {
6234 complaint (&symfile_complaints,
6235 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6236 "DIE 0x%x [in module %s]"),
6237 die->offset, cu->objfile->name);
6238 return;
6239 }
6240 pc = DW_ADDR (attr) + baseaddr;
6241
6242 if (cu->call_site_htab == NULL)
6243 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6244 NULL, &objfile->objfile_obstack,
6245 hashtab_obstack_allocate, NULL);
6246 call_site_local.pc = pc;
6247 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6248 if (*slot != NULL)
6249 {
6250 complaint (&symfile_complaints,
6251 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6252 "DIE 0x%x [in module %s]"),
6253 paddress (gdbarch, pc), die->offset, cu->objfile->name);
6254 return;
6255 }
6256
6257 /* Count parameters at the caller. */
6258
6259 nparams = 0;
6260 for (child_die = die->child; child_die && child_die->tag;
6261 child_die = sibling_die (child_die))
6262 {
6263 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6264 {
6265 complaint (&symfile_complaints,
6266 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6267 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6268 child_die->tag, child_die->offset, cu->objfile->name);
6269 continue;
6270 }
6271
6272 nparams++;
6273 }
6274
6275 call_site = obstack_alloc (&objfile->objfile_obstack,
6276 (sizeof (*call_site)
6277 + (sizeof (*call_site->parameter)
6278 * (nparams - 1))));
6279 *slot = call_site;
6280 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6281 call_site->pc = pc;
6282
6283 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6284 {
6285 struct die_info *func_die;
6286
6287 /* Skip also over DW_TAG_inlined_subroutine. */
6288 for (func_die = die->parent;
6289 func_die && func_die->tag != DW_TAG_subprogram
6290 && func_die->tag != DW_TAG_subroutine_type;
6291 func_die = func_die->parent);
6292
6293 /* DW_AT_GNU_all_call_sites is a superset
6294 of DW_AT_GNU_all_tail_call_sites. */
6295 if (func_die
6296 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6297 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6298 {
6299 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6300 not complete. But keep CALL_SITE for look ups via call_site_htab,
6301 both the initial caller containing the real return address PC and
6302 the final callee containing the current PC of a chain of tail
6303 calls do not need to have the tail call list complete. But any
6304 function candidate for a virtual tail call frame searched via
6305 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6306 determined unambiguously. */
6307 }
6308 else
6309 {
6310 struct type *func_type = NULL;
6311
6312 if (func_die)
6313 func_type = get_die_type (func_die, cu);
6314 if (func_type != NULL)
6315 {
6316 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6317
6318 /* Enlist this call site to the function. */
6319 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6320 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6321 }
6322 else
6323 complaint (&symfile_complaints,
6324 _("Cannot find function owning DW_TAG_GNU_call_site "
6325 "DIE 0x%x [in module %s]"),
6326 die->offset, cu->objfile->name);
6327 }
6328 }
6329
6330 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6331 if (attr == NULL)
6332 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6333 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6334 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6335 /* Keep NULL DWARF_BLOCK. */;
6336 else if (attr_form_is_block (attr))
6337 {
6338 struct dwarf2_locexpr_baton *dlbaton;
6339
6340 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6341 dlbaton->data = DW_BLOCK (attr)->data;
6342 dlbaton->size = DW_BLOCK (attr)->size;
6343 dlbaton->per_cu = cu->per_cu;
6344
6345 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6346 }
6347 else if (is_ref_attr (attr))
6348 {
6349 struct objfile *objfile = cu->objfile;
6350 struct dwarf2_cu *target_cu = cu;
6351 struct die_info *target_die;
6352
6353 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6354 gdb_assert (target_cu->objfile == objfile);
6355 if (die_is_declaration (target_die, target_cu))
6356 {
6357 const char *target_physname;
6358
6359 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6360 if (target_physname == NULL)
6361 complaint (&symfile_complaints,
6362 _("DW_AT_GNU_call_site_target target DIE has invalid "
6363 "physname, for referencing DIE 0x%x [in module %s]"),
6364 die->offset, cu->objfile->name);
6365 else
6366 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6367 }
6368 else
6369 {
6370 CORE_ADDR lowpc;
6371
6372 /* DW_AT_entry_pc should be preferred. */
6373 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6374 complaint (&symfile_complaints,
6375 _("DW_AT_GNU_call_site_target target DIE has invalid "
6376 "low pc, for referencing DIE 0x%x [in module %s]"),
6377 die->offset, cu->objfile->name);
6378 else
6379 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6380 }
6381 }
6382 else
6383 complaint (&symfile_complaints,
6384 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6385 "block nor reference, for DIE 0x%x [in module %s]"),
6386 die->offset, cu->objfile->name);
6387
6388 call_site->per_cu = cu->per_cu;
6389
6390 for (child_die = die->child;
6391 child_die && child_die->tag;
6392 child_die = sibling_die (child_die))
6393 {
6394 struct dwarf2_locexpr_baton *dlbaton;
6395 struct call_site_parameter *parameter;
6396
6397 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6398 {
6399 /* Already printed the complaint above. */
6400 continue;
6401 }
6402
6403 gdb_assert (call_site->parameter_count < nparams);
6404 parameter = &call_site->parameter[call_site->parameter_count];
6405
6406 /* DW_AT_location specifies the register number. Value of the data
6407 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6408
6409 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6410 if (!attr || !attr_form_is_block (attr))
6411 {
6412 complaint (&symfile_complaints,
6413 _("No DW_FORM_block* DW_AT_location for "
6414 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6415 child_die->offset, cu->objfile->name);
6416 continue;
6417 }
6418 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6419 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6420 if (parameter->dwarf_reg == -1
6421 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6422 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6423 &parameter->fb_offset))
6424 {
6425 complaint (&symfile_complaints,
6426 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6427 "for DW_FORM_block* DW_AT_location for "
6428 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6429 child_die->offset, cu->objfile->name);
6430 continue;
6431 }
6432
6433 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6434 if (!attr_form_is_block (attr))
6435 {
6436 complaint (&symfile_complaints,
6437 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6438 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6439 child_die->offset, cu->objfile->name);
6440 continue;
6441 }
6442 parameter->value = DW_BLOCK (attr)->data;
6443 parameter->value_size = DW_BLOCK (attr)->size;
6444
6445 /* Parameters are not pre-cleared by memset above. */
6446 parameter->data_value = NULL;
6447 parameter->data_value_size = 0;
6448 call_site->parameter_count++;
6449
6450 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6451 if (attr)
6452 {
6453 if (!attr_form_is_block (attr))
6454 complaint (&symfile_complaints,
6455 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6456 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6457 child_die->offset, cu->objfile->name);
6458 else
6459 {
6460 parameter->data_value = DW_BLOCK (attr)->data;
6461 parameter->data_value_size = DW_BLOCK (attr)->size;
6462 }
6463 }
6464 }
6465 }
6466
6467 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6468 Return 1 if the attributes are present and valid, otherwise, return 0.
6469 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6470
6471 static int
6472 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6473 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6474 struct partial_symtab *ranges_pst)
6475 {
6476 struct objfile *objfile = cu->objfile;
6477 struct comp_unit_head *cu_header = &cu->header;
6478 bfd *obfd = objfile->obfd;
6479 unsigned int addr_size = cu_header->addr_size;
6480 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6481 /* Base address selection entry. */
6482 CORE_ADDR base;
6483 int found_base;
6484 unsigned int dummy;
6485 gdb_byte *buffer;
6486 CORE_ADDR marker;
6487 int low_set;
6488 CORE_ADDR low = 0;
6489 CORE_ADDR high = 0;
6490 CORE_ADDR baseaddr;
6491
6492 found_base = cu->base_known;
6493 base = cu->base_address;
6494
6495 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6496 if (offset >= dwarf2_per_objfile->ranges.size)
6497 {
6498 complaint (&symfile_complaints,
6499 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6500 offset);
6501 return 0;
6502 }
6503 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6504
6505 /* Read in the largest possible address. */
6506 marker = read_address (obfd, buffer, cu, &dummy);
6507 if ((marker & mask) == mask)
6508 {
6509 /* If we found the largest possible address, then
6510 read the base address. */
6511 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6512 buffer += 2 * addr_size;
6513 offset += 2 * addr_size;
6514 found_base = 1;
6515 }
6516
6517 low_set = 0;
6518
6519 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6520
6521 while (1)
6522 {
6523 CORE_ADDR range_beginning, range_end;
6524
6525 range_beginning = read_address (obfd, buffer, cu, &dummy);
6526 buffer += addr_size;
6527 range_end = read_address (obfd, buffer, cu, &dummy);
6528 buffer += addr_size;
6529 offset += 2 * addr_size;
6530
6531 /* An end of list marker is a pair of zero addresses. */
6532 if (range_beginning == 0 && range_end == 0)
6533 /* Found the end of list entry. */
6534 break;
6535
6536 /* Each base address selection entry is a pair of 2 values.
6537 The first is the largest possible address, the second is
6538 the base address. Check for a base address here. */
6539 if ((range_beginning & mask) == mask)
6540 {
6541 /* If we found the largest possible address, then
6542 read the base address. */
6543 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6544 found_base = 1;
6545 continue;
6546 }
6547
6548 if (!found_base)
6549 {
6550 /* We have no valid base address for the ranges
6551 data. */
6552 complaint (&symfile_complaints,
6553 _("Invalid .debug_ranges data (no base address)"));
6554 return 0;
6555 }
6556
6557 if (range_beginning > range_end)
6558 {
6559 /* Inverted range entries are invalid. */
6560 complaint (&symfile_complaints,
6561 _("Invalid .debug_ranges data (inverted range)"));
6562 return 0;
6563 }
6564
6565 /* Empty range entries have no effect. */
6566 if (range_beginning == range_end)
6567 continue;
6568
6569 range_beginning += base;
6570 range_end += base;
6571
6572 if (ranges_pst != NULL)
6573 addrmap_set_empty (objfile->psymtabs_addrmap,
6574 range_beginning + baseaddr,
6575 range_end - 1 + baseaddr,
6576 ranges_pst);
6577
6578 /* FIXME: This is recording everything as a low-high
6579 segment of consecutive addresses. We should have a
6580 data structure for discontiguous block ranges
6581 instead. */
6582 if (! low_set)
6583 {
6584 low = range_beginning;
6585 high = range_end;
6586 low_set = 1;
6587 }
6588 else
6589 {
6590 if (range_beginning < low)
6591 low = range_beginning;
6592 if (range_end > high)
6593 high = range_end;
6594 }
6595 }
6596
6597 if (! low_set)
6598 /* If the first entry is an end-of-list marker, the range
6599 describes an empty scope, i.e. no instructions. */
6600 return 0;
6601
6602 if (low_return)
6603 *low_return = low;
6604 if (high_return)
6605 *high_return = high;
6606 return 1;
6607 }
6608
6609 /* Get low and high pc attributes from a die. Return 1 if the attributes
6610 are present and valid, otherwise, return 0. Return -1 if the range is
6611 discontinuous, i.e. derived from DW_AT_ranges information. */
6612 static int
6613 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6614 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6615 struct partial_symtab *pst)
6616 {
6617 struct attribute *attr;
6618 CORE_ADDR low = 0;
6619 CORE_ADDR high = 0;
6620 int ret = 0;
6621
6622 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6623 if (attr)
6624 {
6625 high = DW_ADDR (attr);
6626 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6627 if (attr)
6628 low = DW_ADDR (attr);
6629 else
6630 /* Found high w/o low attribute. */
6631 return 0;
6632
6633 /* Found consecutive range of addresses. */
6634 ret = 1;
6635 }
6636 else
6637 {
6638 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6639 if (attr != NULL)
6640 {
6641 /* Value of the DW_AT_ranges attribute is the offset in the
6642 .debug_ranges section. */
6643 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6644 return 0;
6645 /* Found discontinuous range of addresses. */
6646 ret = -1;
6647 }
6648 }
6649
6650 /* read_partial_die has also the strict LOW < HIGH requirement. */
6651 if (high <= low)
6652 return 0;
6653
6654 /* When using the GNU linker, .gnu.linkonce. sections are used to
6655 eliminate duplicate copies of functions and vtables and such.
6656 The linker will arbitrarily choose one and discard the others.
6657 The AT_*_pc values for such functions refer to local labels in
6658 these sections. If the section from that file was discarded, the
6659 labels are not in the output, so the relocs get a value of 0.
6660 If this is a discarded function, mark the pc bounds as invalid,
6661 so that GDB will ignore it. */
6662 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6663 return 0;
6664
6665 *lowpc = low;
6666 if (highpc)
6667 *highpc = high;
6668 return ret;
6669 }
6670
6671 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6672 its low and high PC addresses. Do nothing if these addresses could not
6673 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6674 and HIGHPC to the high address if greater than HIGHPC. */
6675
6676 static void
6677 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6678 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6679 struct dwarf2_cu *cu)
6680 {
6681 CORE_ADDR low, high;
6682 struct die_info *child = die->child;
6683
6684 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6685 {
6686 *lowpc = min (*lowpc, low);
6687 *highpc = max (*highpc, high);
6688 }
6689
6690 /* If the language does not allow nested subprograms (either inside
6691 subprograms or lexical blocks), we're done. */
6692 if (cu->language != language_ada)
6693 return;
6694
6695 /* Check all the children of the given DIE. If it contains nested
6696 subprograms, then check their pc bounds. Likewise, we need to
6697 check lexical blocks as well, as they may also contain subprogram
6698 definitions. */
6699 while (child && child->tag)
6700 {
6701 if (child->tag == DW_TAG_subprogram
6702 || child->tag == DW_TAG_lexical_block)
6703 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6704 child = sibling_die (child);
6705 }
6706 }
6707
6708 /* Get the low and high pc's represented by the scope DIE, and store
6709 them in *LOWPC and *HIGHPC. If the correct values can't be
6710 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6711
6712 static void
6713 get_scope_pc_bounds (struct die_info *die,
6714 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6715 struct dwarf2_cu *cu)
6716 {
6717 CORE_ADDR best_low = (CORE_ADDR) -1;
6718 CORE_ADDR best_high = (CORE_ADDR) 0;
6719 CORE_ADDR current_low, current_high;
6720
6721 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6722 {
6723 best_low = current_low;
6724 best_high = current_high;
6725 }
6726 else
6727 {
6728 struct die_info *child = die->child;
6729
6730 while (child && child->tag)
6731 {
6732 switch (child->tag) {
6733 case DW_TAG_subprogram:
6734 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6735 break;
6736 case DW_TAG_namespace:
6737 case DW_TAG_module:
6738 /* FIXME: carlton/2004-01-16: Should we do this for
6739 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6740 that current GCC's always emit the DIEs corresponding
6741 to definitions of methods of classes as children of a
6742 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6743 the DIEs giving the declarations, which could be
6744 anywhere). But I don't see any reason why the
6745 standards says that they have to be there. */
6746 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6747
6748 if (current_low != ((CORE_ADDR) -1))
6749 {
6750 best_low = min (best_low, current_low);
6751 best_high = max (best_high, current_high);
6752 }
6753 break;
6754 default:
6755 /* Ignore. */
6756 break;
6757 }
6758
6759 child = sibling_die (child);
6760 }
6761 }
6762
6763 *lowpc = best_low;
6764 *highpc = best_high;
6765 }
6766
6767 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6768 in DIE. */
6769 static void
6770 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6771 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6772 {
6773 struct attribute *attr;
6774
6775 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6776 if (attr)
6777 {
6778 CORE_ADDR high = DW_ADDR (attr);
6779
6780 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6781 if (attr)
6782 {
6783 CORE_ADDR low = DW_ADDR (attr);
6784
6785 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6786 }
6787 }
6788
6789 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6790 if (attr)
6791 {
6792 bfd *obfd = cu->objfile->obfd;
6793
6794 /* The value of the DW_AT_ranges attribute is the offset of the
6795 address range list in the .debug_ranges section. */
6796 unsigned long offset = DW_UNSND (attr);
6797 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6798
6799 /* For some target architectures, but not others, the
6800 read_address function sign-extends the addresses it returns.
6801 To recognize base address selection entries, we need a
6802 mask. */
6803 unsigned int addr_size = cu->header.addr_size;
6804 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6805
6806 /* The base address, to which the next pair is relative. Note
6807 that this 'base' is a DWARF concept: most entries in a range
6808 list are relative, to reduce the number of relocs against the
6809 debugging information. This is separate from this function's
6810 'baseaddr' argument, which GDB uses to relocate debugging
6811 information from a shared library based on the address at
6812 which the library was loaded. */
6813 CORE_ADDR base = cu->base_address;
6814 int base_known = cu->base_known;
6815
6816 gdb_assert (dwarf2_per_objfile->ranges.readin);
6817 if (offset >= dwarf2_per_objfile->ranges.size)
6818 {
6819 complaint (&symfile_complaints,
6820 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6821 offset);
6822 return;
6823 }
6824
6825 for (;;)
6826 {
6827 unsigned int bytes_read;
6828 CORE_ADDR start, end;
6829
6830 start = read_address (obfd, buffer, cu, &bytes_read);
6831 buffer += bytes_read;
6832 end = read_address (obfd, buffer, cu, &bytes_read);
6833 buffer += bytes_read;
6834
6835 /* Did we find the end of the range list? */
6836 if (start == 0 && end == 0)
6837 break;
6838
6839 /* Did we find a base address selection entry? */
6840 else if ((start & base_select_mask) == base_select_mask)
6841 {
6842 base = end;
6843 base_known = 1;
6844 }
6845
6846 /* We found an ordinary address range. */
6847 else
6848 {
6849 if (!base_known)
6850 {
6851 complaint (&symfile_complaints,
6852 _("Invalid .debug_ranges data "
6853 "(no base address)"));
6854 return;
6855 }
6856
6857 if (start > end)
6858 {
6859 /* Inverted range entries are invalid. */
6860 complaint (&symfile_complaints,
6861 _("Invalid .debug_ranges data "
6862 "(inverted range)"));
6863 return;
6864 }
6865
6866 /* Empty range entries have no effect. */
6867 if (start == end)
6868 continue;
6869
6870 record_block_range (block,
6871 baseaddr + base + start,
6872 baseaddr + base + end - 1);
6873 }
6874 }
6875 }
6876 }
6877
6878 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6879 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6880 during 4.6.0 experimental. */
6881
6882 static int
6883 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6884 {
6885 const char *cs;
6886 int major, minor, release;
6887
6888 if (cu->producer == NULL)
6889 {
6890 /* For unknown compilers expect their behavior is DWARF version
6891 compliant.
6892
6893 GCC started to support .debug_types sections by -gdwarf-4 since
6894 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6895 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6896 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6897 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6898
6899 return 0;
6900 }
6901
6902 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6903
6904 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6905 {
6906 /* For non-GCC compilers expect their behavior is DWARF version
6907 compliant. */
6908
6909 return 0;
6910 }
6911 cs = &cu->producer[strlen ("GNU ")];
6912 while (*cs && !isdigit (*cs))
6913 cs++;
6914 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6915 {
6916 /* Not recognized as GCC. */
6917
6918 return 0;
6919 }
6920
6921 return major < 4 || (major == 4 && minor < 6);
6922 }
6923
6924 /* Return the default accessibility type if it is not overriden by
6925 DW_AT_accessibility. */
6926
6927 static enum dwarf_access_attribute
6928 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6929 {
6930 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6931 {
6932 /* The default DWARF 2 accessibility for members is public, the default
6933 accessibility for inheritance is private. */
6934
6935 if (die->tag != DW_TAG_inheritance)
6936 return DW_ACCESS_public;
6937 else
6938 return DW_ACCESS_private;
6939 }
6940 else
6941 {
6942 /* DWARF 3+ defines the default accessibility a different way. The same
6943 rules apply now for DW_TAG_inheritance as for the members and it only
6944 depends on the container kind. */
6945
6946 if (die->parent->tag == DW_TAG_class_type)
6947 return DW_ACCESS_private;
6948 else
6949 return DW_ACCESS_public;
6950 }
6951 }
6952
6953 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6954 offset. If the attribute was not found return 0, otherwise return
6955 1. If it was found but could not properly be handled, set *OFFSET
6956 to 0. */
6957
6958 static int
6959 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6960 LONGEST *offset)
6961 {
6962 struct attribute *attr;
6963
6964 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6965 if (attr != NULL)
6966 {
6967 *offset = 0;
6968
6969 /* Note that we do not check for a section offset first here.
6970 This is because DW_AT_data_member_location is new in DWARF 4,
6971 so if we see it, we can assume that a constant form is really
6972 a constant and not a section offset. */
6973 if (attr_form_is_constant (attr))
6974 *offset = dwarf2_get_attr_constant_value (attr, 0);
6975 else if (attr_form_is_section_offset (attr))
6976 dwarf2_complex_location_expr_complaint ();
6977 else if (attr_form_is_block (attr))
6978 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6979 else
6980 dwarf2_complex_location_expr_complaint ();
6981
6982 return 1;
6983 }
6984
6985 return 0;
6986 }
6987
6988 /* Add an aggregate field to the field list. */
6989
6990 static void
6991 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6992 struct dwarf2_cu *cu)
6993 {
6994 struct objfile *objfile = cu->objfile;
6995 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6996 struct nextfield *new_field;
6997 struct attribute *attr;
6998 struct field *fp;
6999 char *fieldname = "";
7000
7001 /* Allocate a new field list entry and link it in. */
7002 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
7003 make_cleanup (xfree, new_field);
7004 memset (new_field, 0, sizeof (struct nextfield));
7005
7006 if (die->tag == DW_TAG_inheritance)
7007 {
7008 new_field->next = fip->baseclasses;
7009 fip->baseclasses = new_field;
7010 }
7011 else
7012 {
7013 new_field->next = fip->fields;
7014 fip->fields = new_field;
7015 }
7016 fip->nfields++;
7017
7018 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7019 if (attr)
7020 new_field->accessibility = DW_UNSND (attr);
7021 else
7022 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7023 if (new_field->accessibility != DW_ACCESS_public)
7024 fip->non_public_fields = 1;
7025
7026 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7027 if (attr)
7028 new_field->virtuality = DW_UNSND (attr);
7029 else
7030 new_field->virtuality = DW_VIRTUALITY_none;
7031
7032 fp = &new_field->field;
7033
7034 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7035 {
7036 LONGEST offset;
7037
7038 /* Data member other than a C++ static data member. */
7039
7040 /* Get type of field. */
7041 fp->type = die_type (die, cu);
7042
7043 SET_FIELD_BITPOS (*fp, 0);
7044
7045 /* Get bit size of field (zero if none). */
7046 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7047 if (attr)
7048 {
7049 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7050 }
7051 else
7052 {
7053 FIELD_BITSIZE (*fp) = 0;
7054 }
7055
7056 /* Get bit offset of field. */
7057 if (handle_data_member_location (die, cu, &offset))
7058 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7059 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7060 if (attr)
7061 {
7062 if (gdbarch_bits_big_endian (gdbarch))
7063 {
7064 /* For big endian bits, the DW_AT_bit_offset gives the
7065 additional bit offset from the MSB of the containing
7066 anonymous object to the MSB of the field. We don't
7067 have to do anything special since we don't need to
7068 know the size of the anonymous object. */
7069 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7070 }
7071 else
7072 {
7073 /* For little endian bits, compute the bit offset to the
7074 MSB of the anonymous object, subtract off the number of
7075 bits from the MSB of the field to the MSB of the
7076 object, and then subtract off the number of bits of
7077 the field itself. The result is the bit offset of
7078 the LSB of the field. */
7079 int anonymous_size;
7080 int bit_offset = DW_UNSND (attr);
7081
7082 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7083 if (attr)
7084 {
7085 /* The size of the anonymous object containing
7086 the bit field is explicit, so use the
7087 indicated size (in bytes). */
7088 anonymous_size = DW_UNSND (attr);
7089 }
7090 else
7091 {
7092 /* The size of the anonymous object containing
7093 the bit field must be inferred from the type
7094 attribute of the data member containing the
7095 bit field. */
7096 anonymous_size = TYPE_LENGTH (fp->type);
7097 }
7098 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7099 - bit_offset - FIELD_BITSIZE (*fp);
7100 }
7101 }
7102
7103 /* Get name of field. */
7104 fieldname = dwarf2_name (die, cu);
7105 if (fieldname == NULL)
7106 fieldname = "";
7107
7108 /* The name is already allocated along with this objfile, so we don't
7109 need to duplicate it for the type. */
7110 fp->name = fieldname;
7111
7112 /* Change accessibility for artificial fields (e.g. virtual table
7113 pointer or virtual base class pointer) to private. */
7114 if (dwarf2_attr (die, DW_AT_artificial, cu))
7115 {
7116 FIELD_ARTIFICIAL (*fp) = 1;
7117 new_field->accessibility = DW_ACCESS_private;
7118 fip->non_public_fields = 1;
7119 }
7120 }
7121 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7122 {
7123 /* C++ static member. */
7124
7125 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7126 is a declaration, but all versions of G++ as of this writing
7127 (so through at least 3.2.1) incorrectly generate
7128 DW_TAG_variable tags. */
7129
7130 const char *physname;
7131
7132 /* Get name of field. */
7133 fieldname = dwarf2_name (die, cu);
7134 if (fieldname == NULL)
7135 return;
7136
7137 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7138 if (attr
7139 /* Only create a symbol if this is an external value.
7140 new_symbol checks this and puts the value in the global symbol
7141 table, which we want. If it is not external, new_symbol
7142 will try to put the value in cu->list_in_scope which is wrong. */
7143 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7144 {
7145 /* A static const member, not much different than an enum as far as
7146 we're concerned, except that we can support more types. */
7147 new_symbol (die, NULL, cu);
7148 }
7149
7150 /* Get physical name. */
7151 physname = dwarf2_physname (fieldname, die, cu);
7152
7153 /* The name is already allocated along with this objfile, so we don't
7154 need to duplicate it for the type. */
7155 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7156 FIELD_TYPE (*fp) = die_type (die, cu);
7157 FIELD_NAME (*fp) = fieldname;
7158 }
7159 else if (die->tag == DW_TAG_inheritance)
7160 {
7161 LONGEST offset;
7162
7163 /* C++ base class field. */
7164 if (handle_data_member_location (die, cu, &offset))
7165 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7166 FIELD_BITSIZE (*fp) = 0;
7167 FIELD_TYPE (*fp) = die_type (die, cu);
7168 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7169 fip->nbaseclasses++;
7170 }
7171 }
7172
7173 /* Add a typedef defined in the scope of the FIP's class. */
7174
7175 static void
7176 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7177 struct dwarf2_cu *cu)
7178 {
7179 struct objfile *objfile = cu->objfile;
7180 struct typedef_field_list *new_field;
7181 struct attribute *attr;
7182 struct typedef_field *fp;
7183 char *fieldname = "";
7184
7185 /* Allocate a new field list entry and link it in. */
7186 new_field = xzalloc (sizeof (*new_field));
7187 make_cleanup (xfree, new_field);
7188
7189 gdb_assert (die->tag == DW_TAG_typedef);
7190
7191 fp = &new_field->field;
7192
7193 /* Get name of field. */
7194 fp->name = dwarf2_name (die, cu);
7195 if (fp->name == NULL)
7196 return;
7197
7198 fp->type = read_type_die (die, cu);
7199
7200 new_field->next = fip->typedef_field_list;
7201 fip->typedef_field_list = new_field;
7202 fip->typedef_field_list_count++;
7203 }
7204
7205 /* Create the vector of fields, and attach it to the type. */
7206
7207 static void
7208 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7209 struct dwarf2_cu *cu)
7210 {
7211 int nfields = fip->nfields;
7212
7213 /* Record the field count, allocate space for the array of fields,
7214 and create blank accessibility bitfields if necessary. */
7215 TYPE_NFIELDS (type) = nfields;
7216 TYPE_FIELDS (type) = (struct field *)
7217 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7218 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7219
7220 if (fip->non_public_fields && cu->language != language_ada)
7221 {
7222 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7223
7224 TYPE_FIELD_PRIVATE_BITS (type) =
7225 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7226 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7227
7228 TYPE_FIELD_PROTECTED_BITS (type) =
7229 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7230 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7231
7232 TYPE_FIELD_IGNORE_BITS (type) =
7233 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7234 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7235 }
7236
7237 /* If the type has baseclasses, allocate and clear a bit vector for
7238 TYPE_FIELD_VIRTUAL_BITS. */
7239 if (fip->nbaseclasses && cu->language != language_ada)
7240 {
7241 int num_bytes = B_BYTES (fip->nbaseclasses);
7242 unsigned char *pointer;
7243
7244 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7245 pointer = TYPE_ALLOC (type, num_bytes);
7246 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7247 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7248 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7249 }
7250
7251 /* Copy the saved-up fields into the field vector. Start from the head of
7252 the list, adding to the tail of the field array, so that they end up in
7253 the same order in the array in which they were added to the list. */
7254 while (nfields-- > 0)
7255 {
7256 struct nextfield *fieldp;
7257
7258 if (fip->fields)
7259 {
7260 fieldp = fip->fields;
7261 fip->fields = fieldp->next;
7262 }
7263 else
7264 {
7265 fieldp = fip->baseclasses;
7266 fip->baseclasses = fieldp->next;
7267 }
7268
7269 TYPE_FIELD (type, nfields) = fieldp->field;
7270 switch (fieldp->accessibility)
7271 {
7272 case DW_ACCESS_private:
7273 if (cu->language != language_ada)
7274 SET_TYPE_FIELD_PRIVATE (type, nfields);
7275 break;
7276
7277 case DW_ACCESS_protected:
7278 if (cu->language != language_ada)
7279 SET_TYPE_FIELD_PROTECTED (type, nfields);
7280 break;
7281
7282 case DW_ACCESS_public:
7283 break;
7284
7285 default:
7286 /* Unknown accessibility. Complain and treat it as public. */
7287 {
7288 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7289 fieldp->accessibility);
7290 }
7291 break;
7292 }
7293 if (nfields < fip->nbaseclasses)
7294 {
7295 switch (fieldp->virtuality)
7296 {
7297 case DW_VIRTUALITY_virtual:
7298 case DW_VIRTUALITY_pure_virtual:
7299 if (cu->language == language_ada)
7300 error (_("unexpected virtuality in component of Ada type"));
7301 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7302 break;
7303 }
7304 }
7305 }
7306 }
7307
7308 /* Add a member function to the proper fieldlist. */
7309
7310 static void
7311 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7312 struct type *type, struct dwarf2_cu *cu)
7313 {
7314 struct objfile *objfile = cu->objfile;
7315 struct attribute *attr;
7316 struct fnfieldlist *flp;
7317 int i;
7318 struct fn_field *fnp;
7319 char *fieldname;
7320 struct nextfnfield *new_fnfield;
7321 struct type *this_type;
7322 enum dwarf_access_attribute accessibility;
7323
7324 if (cu->language == language_ada)
7325 error (_("unexpected member function in Ada type"));
7326
7327 /* Get name of member function. */
7328 fieldname = dwarf2_name (die, cu);
7329 if (fieldname == NULL)
7330 return;
7331
7332 /* Look up member function name in fieldlist. */
7333 for (i = 0; i < fip->nfnfields; i++)
7334 {
7335 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7336 break;
7337 }
7338
7339 /* Create new list element if necessary. */
7340 if (i < fip->nfnfields)
7341 flp = &fip->fnfieldlists[i];
7342 else
7343 {
7344 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7345 {
7346 fip->fnfieldlists = (struct fnfieldlist *)
7347 xrealloc (fip->fnfieldlists,
7348 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7349 * sizeof (struct fnfieldlist));
7350 if (fip->nfnfields == 0)
7351 make_cleanup (free_current_contents, &fip->fnfieldlists);
7352 }
7353 flp = &fip->fnfieldlists[fip->nfnfields];
7354 flp->name = fieldname;
7355 flp->length = 0;
7356 flp->head = NULL;
7357 i = fip->nfnfields++;
7358 }
7359
7360 /* Create a new member function field and chain it to the field list
7361 entry. */
7362 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7363 make_cleanup (xfree, new_fnfield);
7364 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7365 new_fnfield->next = flp->head;
7366 flp->head = new_fnfield;
7367 flp->length++;
7368
7369 /* Fill in the member function field info. */
7370 fnp = &new_fnfield->fnfield;
7371
7372 /* Delay processing of the physname until later. */
7373 if (cu->language == language_cplus || cu->language == language_java)
7374 {
7375 add_to_method_list (type, i, flp->length - 1, fieldname,
7376 die, cu);
7377 }
7378 else
7379 {
7380 const char *physname = dwarf2_physname (fieldname, die, cu);
7381 fnp->physname = physname ? physname : "";
7382 }
7383
7384 fnp->type = alloc_type (objfile);
7385 this_type = read_type_die (die, cu);
7386 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7387 {
7388 int nparams = TYPE_NFIELDS (this_type);
7389
7390 /* TYPE is the domain of this method, and THIS_TYPE is the type
7391 of the method itself (TYPE_CODE_METHOD). */
7392 smash_to_method_type (fnp->type, type,
7393 TYPE_TARGET_TYPE (this_type),
7394 TYPE_FIELDS (this_type),
7395 TYPE_NFIELDS (this_type),
7396 TYPE_VARARGS (this_type));
7397
7398 /* Handle static member functions.
7399 Dwarf2 has no clean way to discern C++ static and non-static
7400 member functions. G++ helps GDB by marking the first
7401 parameter for non-static member functions (which is the this
7402 pointer) as artificial. We obtain this information from
7403 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7404 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7405 fnp->voffset = VOFFSET_STATIC;
7406 }
7407 else
7408 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7409 dwarf2_full_name (fieldname, die, cu));
7410
7411 /* Get fcontext from DW_AT_containing_type if present. */
7412 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7413 fnp->fcontext = die_containing_type (die, cu);
7414
7415 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7416 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7417
7418 /* Get accessibility. */
7419 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7420 if (attr)
7421 accessibility = DW_UNSND (attr);
7422 else
7423 accessibility = dwarf2_default_access_attribute (die, cu);
7424 switch (accessibility)
7425 {
7426 case DW_ACCESS_private:
7427 fnp->is_private = 1;
7428 break;
7429 case DW_ACCESS_protected:
7430 fnp->is_protected = 1;
7431 break;
7432 }
7433
7434 /* Check for artificial methods. */
7435 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7436 if (attr && DW_UNSND (attr) != 0)
7437 fnp->is_artificial = 1;
7438
7439 /* Get index in virtual function table if it is a virtual member
7440 function. For older versions of GCC, this is an offset in the
7441 appropriate virtual table, as specified by DW_AT_containing_type.
7442 For everyone else, it is an expression to be evaluated relative
7443 to the object address. */
7444
7445 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7446 if (attr)
7447 {
7448 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7449 {
7450 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7451 {
7452 /* Old-style GCC. */
7453 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7454 }
7455 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7456 || (DW_BLOCK (attr)->size > 1
7457 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7458 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7459 {
7460 struct dwarf_block blk;
7461 int offset;
7462
7463 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7464 ? 1 : 2);
7465 blk.size = DW_BLOCK (attr)->size - offset;
7466 blk.data = DW_BLOCK (attr)->data + offset;
7467 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7468 if ((fnp->voffset % cu->header.addr_size) != 0)
7469 dwarf2_complex_location_expr_complaint ();
7470 else
7471 fnp->voffset /= cu->header.addr_size;
7472 fnp->voffset += 2;
7473 }
7474 else
7475 dwarf2_complex_location_expr_complaint ();
7476
7477 if (!fnp->fcontext)
7478 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7479 }
7480 else if (attr_form_is_section_offset (attr))
7481 {
7482 dwarf2_complex_location_expr_complaint ();
7483 }
7484 else
7485 {
7486 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7487 fieldname);
7488 }
7489 }
7490 else
7491 {
7492 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7493 if (attr && DW_UNSND (attr))
7494 {
7495 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7496 complaint (&symfile_complaints,
7497 _("Member function \"%s\" (offset %d) is virtual "
7498 "but the vtable offset is not specified"),
7499 fieldname, die->offset);
7500 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7501 TYPE_CPLUS_DYNAMIC (type) = 1;
7502 }
7503 }
7504 }
7505
7506 /* Create the vector of member function fields, and attach it to the type. */
7507
7508 static void
7509 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7510 struct dwarf2_cu *cu)
7511 {
7512 struct fnfieldlist *flp;
7513 int total_length = 0;
7514 int i;
7515
7516 if (cu->language == language_ada)
7517 error (_("unexpected member functions in Ada type"));
7518
7519 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7520 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7521 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7522
7523 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7524 {
7525 struct nextfnfield *nfp = flp->head;
7526 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7527 int k;
7528
7529 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7530 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7531 fn_flp->fn_fields = (struct fn_field *)
7532 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7533 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7534 fn_flp->fn_fields[k] = nfp->fnfield;
7535
7536 total_length += flp->length;
7537 }
7538
7539 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7540 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7541 }
7542
7543 /* Returns non-zero if NAME is the name of a vtable member in CU's
7544 language, zero otherwise. */
7545 static int
7546 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7547 {
7548 static const char vptr[] = "_vptr";
7549 static const char vtable[] = "vtable";
7550
7551 /* Look for the C++ and Java forms of the vtable. */
7552 if ((cu->language == language_java
7553 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7554 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7555 && is_cplus_marker (name[sizeof (vptr) - 1])))
7556 return 1;
7557
7558 return 0;
7559 }
7560
7561 /* GCC outputs unnamed structures that are really pointers to member
7562 functions, with the ABI-specified layout. If TYPE describes
7563 such a structure, smash it into a member function type.
7564
7565 GCC shouldn't do this; it should just output pointer to member DIEs.
7566 This is GCC PR debug/28767. */
7567
7568 static void
7569 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7570 {
7571 struct type *pfn_type, *domain_type, *new_type;
7572
7573 /* Check for a structure with no name and two children. */
7574 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7575 return;
7576
7577 /* Check for __pfn and __delta members. */
7578 if (TYPE_FIELD_NAME (type, 0) == NULL
7579 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7580 || TYPE_FIELD_NAME (type, 1) == NULL
7581 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7582 return;
7583
7584 /* Find the type of the method. */
7585 pfn_type = TYPE_FIELD_TYPE (type, 0);
7586 if (pfn_type == NULL
7587 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7588 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7589 return;
7590
7591 /* Look for the "this" argument. */
7592 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7593 if (TYPE_NFIELDS (pfn_type) == 0
7594 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7595 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7596 return;
7597
7598 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7599 new_type = alloc_type (objfile);
7600 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7601 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7602 TYPE_VARARGS (pfn_type));
7603 smash_to_methodptr_type (type, new_type);
7604 }
7605
7606 /* Called when we find the DIE that starts a structure or union scope
7607 (definition) to create a type for the structure or union. Fill in
7608 the type's name and general properties; the members will not be
7609 processed until process_structure_type.
7610
7611 NOTE: we need to call these functions regardless of whether or not the
7612 DIE has a DW_AT_name attribute, since it might be an anonymous
7613 structure or union. This gets the type entered into our set of
7614 user defined types.
7615
7616 However, if the structure is incomplete (an opaque struct/union)
7617 then suppress creating a symbol table entry for it since gdb only
7618 wants to find the one with the complete definition. Note that if
7619 it is complete, we just call new_symbol, which does it's own
7620 checking about whether the struct/union is anonymous or not (and
7621 suppresses creating a symbol table entry itself). */
7622
7623 static struct type *
7624 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7625 {
7626 struct objfile *objfile = cu->objfile;
7627 struct type *type;
7628 struct attribute *attr;
7629 char *name;
7630
7631 /* If the definition of this type lives in .debug_types, read that type.
7632 Don't follow DW_AT_specification though, that will take us back up
7633 the chain and we want to go down. */
7634 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7635 if (attr)
7636 {
7637 struct dwarf2_cu *type_cu = cu;
7638 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7639
7640 /* We could just recurse on read_structure_type, but we need to call
7641 get_die_type to ensure only one type for this DIE is created.
7642 This is important, for example, because for c++ classes we need
7643 TYPE_NAME set which is only done by new_symbol. Blech. */
7644 type = read_type_die (type_die, type_cu);
7645
7646 /* TYPE_CU may not be the same as CU.
7647 Ensure TYPE is recorded in CU's type_hash table. */
7648 return set_die_type (die, type, cu);
7649 }
7650
7651 type = alloc_type (objfile);
7652 INIT_CPLUS_SPECIFIC (type);
7653
7654 name = dwarf2_name (die, cu);
7655 if (name != NULL)
7656 {
7657 if (cu->language == language_cplus
7658 || cu->language == language_java)
7659 {
7660 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7661
7662 /* dwarf2_full_name might have already finished building the DIE's
7663 type. If so, there is no need to continue. */
7664 if (get_die_type (die, cu) != NULL)
7665 return get_die_type (die, cu);
7666
7667 TYPE_TAG_NAME (type) = full_name;
7668 if (die->tag == DW_TAG_structure_type
7669 || die->tag == DW_TAG_class_type)
7670 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7671 }
7672 else
7673 {
7674 /* The name is already allocated along with this objfile, so
7675 we don't need to duplicate it for the type. */
7676 TYPE_TAG_NAME (type) = (char *) name;
7677 if (die->tag == DW_TAG_class_type)
7678 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7679 }
7680 }
7681
7682 if (die->tag == DW_TAG_structure_type)
7683 {
7684 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7685 }
7686 else if (die->tag == DW_TAG_union_type)
7687 {
7688 TYPE_CODE (type) = TYPE_CODE_UNION;
7689 }
7690 else
7691 {
7692 TYPE_CODE (type) = TYPE_CODE_CLASS;
7693 }
7694
7695 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7696 TYPE_DECLARED_CLASS (type) = 1;
7697
7698 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7699 if (attr)
7700 {
7701 TYPE_LENGTH (type) = DW_UNSND (attr);
7702 }
7703 else
7704 {
7705 TYPE_LENGTH (type) = 0;
7706 }
7707
7708 TYPE_STUB_SUPPORTED (type) = 1;
7709 if (die_is_declaration (die, cu))
7710 TYPE_STUB (type) = 1;
7711 else if (attr == NULL && die->child == NULL
7712 && producer_is_realview (cu->producer))
7713 /* RealView does not output the required DW_AT_declaration
7714 on incomplete types. */
7715 TYPE_STUB (type) = 1;
7716
7717 /* We need to add the type field to the die immediately so we don't
7718 infinitely recurse when dealing with pointers to the structure
7719 type within the structure itself. */
7720 set_die_type (die, type, cu);
7721
7722 /* set_die_type should be already done. */
7723 set_descriptive_type (type, die, cu);
7724
7725 return type;
7726 }
7727
7728 /* Finish creating a structure or union type, including filling in
7729 its members and creating a symbol for it. */
7730
7731 static void
7732 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7733 {
7734 struct objfile *objfile = cu->objfile;
7735 struct die_info *child_die = die->child;
7736 struct type *type;
7737
7738 type = get_die_type (die, cu);
7739 if (type == NULL)
7740 type = read_structure_type (die, cu);
7741
7742 if (die->child != NULL && ! die_is_declaration (die, cu))
7743 {
7744 struct field_info fi;
7745 struct die_info *child_die;
7746 VEC (symbolp) *template_args = NULL;
7747 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7748
7749 memset (&fi, 0, sizeof (struct field_info));
7750
7751 child_die = die->child;
7752
7753 while (child_die && child_die->tag)
7754 {
7755 if (child_die->tag == DW_TAG_member
7756 || child_die->tag == DW_TAG_variable)
7757 {
7758 /* NOTE: carlton/2002-11-05: A C++ static data member
7759 should be a DW_TAG_member that is a declaration, but
7760 all versions of G++ as of this writing (so through at
7761 least 3.2.1) incorrectly generate DW_TAG_variable
7762 tags for them instead. */
7763 dwarf2_add_field (&fi, child_die, cu);
7764 }
7765 else if (child_die->tag == DW_TAG_subprogram)
7766 {
7767 /* C++ member function. */
7768 dwarf2_add_member_fn (&fi, child_die, type, cu);
7769 }
7770 else if (child_die->tag == DW_TAG_inheritance)
7771 {
7772 /* C++ base class field. */
7773 dwarf2_add_field (&fi, child_die, cu);
7774 }
7775 else if (child_die->tag == DW_TAG_typedef)
7776 dwarf2_add_typedef (&fi, child_die, cu);
7777 else if (child_die->tag == DW_TAG_template_type_param
7778 || child_die->tag == DW_TAG_template_value_param)
7779 {
7780 struct symbol *arg = new_symbol (child_die, NULL, cu);
7781
7782 if (arg != NULL)
7783 VEC_safe_push (symbolp, template_args, arg);
7784 }
7785
7786 child_die = sibling_die (child_die);
7787 }
7788
7789 /* Attach template arguments to type. */
7790 if (! VEC_empty (symbolp, template_args))
7791 {
7792 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7793 TYPE_N_TEMPLATE_ARGUMENTS (type)
7794 = VEC_length (symbolp, template_args);
7795 TYPE_TEMPLATE_ARGUMENTS (type)
7796 = obstack_alloc (&objfile->objfile_obstack,
7797 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7798 * sizeof (struct symbol *)));
7799 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7800 VEC_address (symbolp, template_args),
7801 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7802 * sizeof (struct symbol *)));
7803 VEC_free (symbolp, template_args);
7804 }
7805
7806 /* Attach fields and member functions to the type. */
7807 if (fi.nfields)
7808 dwarf2_attach_fields_to_type (&fi, type, cu);
7809 if (fi.nfnfields)
7810 {
7811 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7812
7813 /* Get the type which refers to the base class (possibly this
7814 class itself) which contains the vtable pointer for the current
7815 class from the DW_AT_containing_type attribute. This use of
7816 DW_AT_containing_type is a GNU extension. */
7817
7818 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7819 {
7820 struct type *t = die_containing_type (die, cu);
7821
7822 TYPE_VPTR_BASETYPE (type) = t;
7823 if (type == t)
7824 {
7825 int i;
7826
7827 /* Our own class provides vtbl ptr. */
7828 for (i = TYPE_NFIELDS (t) - 1;
7829 i >= TYPE_N_BASECLASSES (t);
7830 --i)
7831 {
7832 char *fieldname = TYPE_FIELD_NAME (t, i);
7833
7834 if (is_vtable_name (fieldname, cu))
7835 {
7836 TYPE_VPTR_FIELDNO (type) = i;
7837 break;
7838 }
7839 }
7840
7841 /* Complain if virtual function table field not found. */
7842 if (i < TYPE_N_BASECLASSES (t))
7843 complaint (&symfile_complaints,
7844 _("virtual function table pointer "
7845 "not found when defining class '%s'"),
7846 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7847 "");
7848 }
7849 else
7850 {
7851 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7852 }
7853 }
7854 else if (cu->producer
7855 && strncmp (cu->producer,
7856 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7857 {
7858 /* The IBM XLC compiler does not provide direct indication
7859 of the containing type, but the vtable pointer is
7860 always named __vfp. */
7861
7862 int i;
7863
7864 for (i = TYPE_NFIELDS (type) - 1;
7865 i >= TYPE_N_BASECLASSES (type);
7866 --i)
7867 {
7868 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7869 {
7870 TYPE_VPTR_FIELDNO (type) = i;
7871 TYPE_VPTR_BASETYPE (type) = type;
7872 break;
7873 }
7874 }
7875 }
7876 }
7877
7878 /* Copy fi.typedef_field_list linked list elements content into the
7879 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7880 if (fi.typedef_field_list)
7881 {
7882 int i = fi.typedef_field_list_count;
7883
7884 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7885 TYPE_TYPEDEF_FIELD_ARRAY (type)
7886 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7887 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7888
7889 /* Reverse the list order to keep the debug info elements order. */
7890 while (--i >= 0)
7891 {
7892 struct typedef_field *dest, *src;
7893
7894 dest = &TYPE_TYPEDEF_FIELD (type, i);
7895 src = &fi.typedef_field_list->field;
7896 fi.typedef_field_list = fi.typedef_field_list->next;
7897 *dest = *src;
7898 }
7899 }
7900
7901 do_cleanups (back_to);
7902
7903 if (HAVE_CPLUS_STRUCT (type))
7904 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7905 }
7906
7907 quirk_gcc_member_function_pointer (type, cu->objfile);
7908
7909 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7910 snapshots) has been known to create a die giving a declaration
7911 for a class that has, as a child, a die giving a definition for a
7912 nested class. So we have to process our children even if the
7913 current die is a declaration. Normally, of course, a declaration
7914 won't have any children at all. */
7915
7916 while (child_die != NULL && child_die->tag)
7917 {
7918 if (child_die->tag == DW_TAG_member
7919 || child_die->tag == DW_TAG_variable
7920 || child_die->tag == DW_TAG_inheritance
7921 || child_die->tag == DW_TAG_template_value_param
7922 || child_die->tag == DW_TAG_template_type_param)
7923 {
7924 /* Do nothing. */
7925 }
7926 else
7927 process_die (child_die, cu);
7928
7929 child_die = sibling_die (child_die);
7930 }
7931
7932 /* Do not consider external references. According to the DWARF standard,
7933 these DIEs are identified by the fact that they have no byte_size
7934 attribute, and a declaration attribute. */
7935 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7936 || !die_is_declaration (die, cu))
7937 new_symbol (die, type, cu);
7938 }
7939
7940 /* Given a DW_AT_enumeration_type die, set its type. We do not
7941 complete the type's fields yet, or create any symbols. */
7942
7943 static struct type *
7944 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7945 {
7946 struct objfile *objfile = cu->objfile;
7947 struct type *type;
7948 struct attribute *attr;
7949 const char *name;
7950
7951 /* If the definition of this type lives in .debug_types, read that type.
7952 Don't follow DW_AT_specification though, that will take us back up
7953 the chain and we want to go down. */
7954 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7955 if (attr)
7956 {
7957 struct dwarf2_cu *type_cu = cu;
7958 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7959
7960 type = read_type_die (type_die, type_cu);
7961
7962 /* TYPE_CU may not be the same as CU.
7963 Ensure TYPE is recorded in CU's type_hash table. */
7964 return set_die_type (die, type, cu);
7965 }
7966
7967 type = alloc_type (objfile);
7968
7969 TYPE_CODE (type) = TYPE_CODE_ENUM;
7970 name = dwarf2_full_name (NULL, die, cu);
7971 if (name != NULL)
7972 TYPE_TAG_NAME (type) = (char *) name;
7973
7974 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7975 if (attr)
7976 {
7977 TYPE_LENGTH (type) = DW_UNSND (attr);
7978 }
7979 else
7980 {
7981 TYPE_LENGTH (type) = 0;
7982 }
7983
7984 /* The enumeration DIE can be incomplete. In Ada, any type can be
7985 declared as private in the package spec, and then defined only
7986 inside the package body. Such types are known as Taft Amendment
7987 Types. When another package uses such a type, an incomplete DIE
7988 may be generated by the compiler. */
7989 if (die_is_declaration (die, cu))
7990 TYPE_STUB (type) = 1;
7991
7992 return set_die_type (die, type, cu);
7993 }
7994
7995 /* Given a pointer to a die which begins an enumeration, process all
7996 the dies that define the members of the enumeration, and create the
7997 symbol for the enumeration type.
7998
7999 NOTE: We reverse the order of the element list. */
8000
8001 static void
8002 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
8003 {
8004 struct type *this_type;
8005
8006 this_type = get_die_type (die, cu);
8007 if (this_type == NULL)
8008 this_type = read_enumeration_type (die, cu);
8009
8010 if (die->child != NULL)
8011 {
8012 struct die_info *child_die;
8013 struct symbol *sym;
8014 struct field *fields = NULL;
8015 int num_fields = 0;
8016 int unsigned_enum = 1;
8017 char *name;
8018
8019 child_die = die->child;
8020 while (child_die && child_die->tag)
8021 {
8022 if (child_die->tag != DW_TAG_enumerator)
8023 {
8024 process_die (child_die, cu);
8025 }
8026 else
8027 {
8028 name = dwarf2_name (child_die, cu);
8029 if (name)
8030 {
8031 sym = new_symbol (child_die, this_type, cu);
8032 if (SYMBOL_VALUE (sym) < 0)
8033 unsigned_enum = 0;
8034
8035 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8036 {
8037 fields = (struct field *)
8038 xrealloc (fields,
8039 (num_fields + DW_FIELD_ALLOC_CHUNK)
8040 * sizeof (struct field));
8041 }
8042
8043 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8044 FIELD_TYPE (fields[num_fields]) = NULL;
8045 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8046 FIELD_BITSIZE (fields[num_fields]) = 0;
8047
8048 num_fields++;
8049 }
8050 }
8051
8052 child_die = sibling_die (child_die);
8053 }
8054
8055 if (num_fields)
8056 {
8057 TYPE_NFIELDS (this_type) = num_fields;
8058 TYPE_FIELDS (this_type) = (struct field *)
8059 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8060 memcpy (TYPE_FIELDS (this_type), fields,
8061 sizeof (struct field) * num_fields);
8062 xfree (fields);
8063 }
8064 if (unsigned_enum)
8065 TYPE_UNSIGNED (this_type) = 1;
8066 }
8067
8068 /* If we are reading an enum from a .debug_types unit, and the enum
8069 is a declaration, and the enum is not the signatured type in the
8070 unit, then we do not want to add a symbol for it. Adding a
8071 symbol would in some cases obscure the true definition of the
8072 enum, giving users an incomplete type when the definition is
8073 actually available. Note that we do not want to do this for all
8074 enums which are just declarations, because C++0x allows forward
8075 enum declarations. */
8076 if (cu->per_cu->debug_type_section
8077 && die_is_declaration (die, cu))
8078 {
8079 struct signatured_type *type_sig;
8080
8081 type_sig
8082 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8083 cu->per_cu->debug_type_section,
8084 cu->per_cu->offset);
8085 if (type_sig->type_offset != die->offset)
8086 return;
8087 }
8088
8089 new_symbol (die, this_type, cu);
8090 }
8091
8092 /* Extract all information from a DW_TAG_array_type DIE and put it in
8093 the DIE's type field. For now, this only handles one dimensional
8094 arrays. */
8095
8096 static struct type *
8097 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8098 {
8099 struct objfile *objfile = cu->objfile;
8100 struct die_info *child_die;
8101 struct type *type;
8102 struct type *element_type, *range_type, *index_type;
8103 struct type **range_types = NULL;
8104 struct attribute *attr;
8105 int ndim = 0;
8106 struct cleanup *back_to;
8107 char *name;
8108
8109 element_type = die_type (die, cu);
8110
8111 /* The die_type call above may have already set the type for this DIE. */
8112 type = get_die_type (die, cu);
8113 if (type)
8114 return type;
8115
8116 /* Irix 6.2 native cc creates array types without children for
8117 arrays with unspecified length. */
8118 if (die->child == NULL)
8119 {
8120 index_type = objfile_type (objfile)->builtin_int;
8121 range_type = create_range_type (NULL, index_type, 0, -1);
8122 type = create_array_type (NULL, element_type, range_type);
8123 return set_die_type (die, type, cu);
8124 }
8125
8126 back_to = make_cleanup (null_cleanup, NULL);
8127 child_die = die->child;
8128 while (child_die && child_die->tag)
8129 {
8130 if (child_die->tag == DW_TAG_subrange_type)
8131 {
8132 struct type *child_type = read_type_die (child_die, cu);
8133
8134 if (child_type != NULL)
8135 {
8136 /* The range type was succesfully read. Save it for the
8137 array type creation. */
8138 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8139 {
8140 range_types = (struct type **)
8141 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8142 * sizeof (struct type *));
8143 if (ndim == 0)
8144 make_cleanup (free_current_contents, &range_types);
8145 }
8146 range_types[ndim++] = child_type;
8147 }
8148 }
8149 child_die = sibling_die (child_die);
8150 }
8151
8152 /* Dwarf2 dimensions are output from left to right, create the
8153 necessary array types in backwards order. */
8154
8155 type = element_type;
8156
8157 if (read_array_order (die, cu) == DW_ORD_col_major)
8158 {
8159 int i = 0;
8160
8161 while (i < ndim)
8162 type = create_array_type (NULL, type, range_types[i++]);
8163 }
8164 else
8165 {
8166 while (ndim-- > 0)
8167 type = create_array_type (NULL, type, range_types[ndim]);
8168 }
8169
8170 /* Understand Dwarf2 support for vector types (like they occur on
8171 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8172 array type. This is not part of the Dwarf2/3 standard yet, but a
8173 custom vendor extension. The main difference between a regular
8174 array and the vector variant is that vectors are passed by value
8175 to functions. */
8176 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8177 if (attr)
8178 make_vector_type (type);
8179
8180 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8181 implementation may choose to implement triple vectors using this
8182 attribute. */
8183 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8184 if (attr)
8185 {
8186 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8187 TYPE_LENGTH (type) = DW_UNSND (attr);
8188 else
8189 complaint (&symfile_complaints,
8190 _("DW_AT_byte_size for array type smaller "
8191 "than the total size of elements"));
8192 }
8193
8194 name = dwarf2_name (die, cu);
8195 if (name)
8196 TYPE_NAME (type) = name;
8197
8198 /* Install the type in the die. */
8199 set_die_type (die, type, cu);
8200
8201 /* set_die_type should be already done. */
8202 set_descriptive_type (type, die, cu);
8203
8204 do_cleanups (back_to);
8205
8206 return type;
8207 }
8208
8209 static enum dwarf_array_dim_ordering
8210 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8211 {
8212 struct attribute *attr;
8213
8214 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8215
8216 if (attr) return DW_SND (attr);
8217
8218 /* GNU F77 is a special case, as at 08/2004 array type info is the
8219 opposite order to the dwarf2 specification, but data is still
8220 laid out as per normal fortran.
8221
8222 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8223 version checking. */
8224
8225 if (cu->language == language_fortran
8226 && cu->producer && strstr (cu->producer, "GNU F77"))
8227 {
8228 return DW_ORD_row_major;
8229 }
8230
8231 switch (cu->language_defn->la_array_ordering)
8232 {
8233 case array_column_major:
8234 return DW_ORD_col_major;
8235 case array_row_major:
8236 default:
8237 return DW_ORD_row_major;
8238 };
8239 }
8240
8241 /* Extract all information from a DW_TAG_set_type DIE and put it in
8242 the DIE's type field. */
8243
8244 static struct type *
8245 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8246 {
8247 struct type *domain_type, *set_type;
8248 struct attribute *attr;
8249
8250 domain_type = die_type (die, cu);
8251
8252 /* The die_type call above may have already set the type for this DIE. */
8253 set_type = get_die_type (die, cu);
8254 if (set_type)
8255 return set_type;
8256
8257 set_type = create_set_type (NULL, domain_type);
8258
8259 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8260 if (attr)
8261 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8262
8263 return set_die_type (die, set_type, cu);
8264 }
8265
8266 /* First cut: install each common block member as a global variable. */
8267
8268 static void
8269 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8270 {
8271 struct die_info *child_die;
8272 struct attribute *attr;
8273 struct symbol *sym;
8274 CORE_ADDR base = (CORE_ADDR) 0;
8275
8276 attr = dwarf2_attr (die, DW_AT_location, cu);
8277 if (attr)
8278 {
8279 /* Support the .debug_loc offsets. */
8280 if (attr_form_is_block (attr))
8281 {
8282 base = decode_locdesc (DW_BLOCK (attr), cu);
8283 }
8284 else if (attr_form_is_section_offset (attr))
8285 {
8286 dwarf2_complex_location_expr_complaint ();
8287 }
8288 else
8289 {
8290 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8291 "common block member");
8292 }
8293 }
8294 if (die->child != NULL)
8295 {
8296 child_die = die->child;
8297 while (child_die && child_die->tag)
8298 {
8299 LONGEST offset;
8300
8301 sym = new_symbol (child_die, NULL, cu);
8302 if (sym != NULL
8303 && handle_data_member_location (child_die, cu, &offset))
8304 {
8305 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8306 add_symbol_to_list (sym, &global_symbols);
8307 }
8308 child_die = sibling_die (child_die);
8309 }
8310 }
8311 }
8312
8313 /* Create a type for a C++ namespace. */
8314
8315 static struct type *
8316 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8317 {
8318 struct objfile *objfile = cu->objfile;
8319 const char *previous_prefix, *name;
8320 int is_anonymous;
8321 struct type *type;
8322
8323 /* For extensions, reuse the type of the original namespace. */
8324 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8325 {
8326 struct die_info *ext_die;
8327 struct dwarf2_cu *ext_cu = cu;
8328
8329 ext_die = dwarf2_extension (die, &ext_cu);
8330 type = read_type_die (ext_die, ext_cu);
8331
8332 /* EXT_CU may not be the same as CU.
8333 Ensure TYPE is recorded in CU's type_hash table. */
8334 return set_die_type (die, type, cu);
8335 }
8336
8337 name = namespace_name (die, &is_anonymous, cu);
8338
8339 /* Now build the name of the current namespace. */
8340
8341 previous_prefix = determine_prefix (die, cu);
8342 if (previous_prefix[0] != '\0')
8343 name = typename_concat (&objfile->objfile_obstack,
8344 previous_prefix, name, 0, cu);
8345
8346 /* Create the type. */
8347 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8348 objfile);
8349 TYPE_NAME (type) = (char *) name;
8350 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8351
8352 return set_die_type (die, type, cu);
8353 }
8354
8355 /* Read a C++ namespace. */
8356
8357 static void
8358 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8359 {
8360 struct objfile *objfile = cu->objfile;
8361 int is_anonymous;
8362
8363 /* Add a symbol associated to this if we haven't seen the namespace
8364 before. Also, add a using directive if it's an anonymous
8365 namespace. */
8366
8367 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8368 {
8369 struct type *type;
8370
8371 type = read_type_die (die, cu);
8372 new_symbol (die, type, cu);
8373
8374 namespace_name (die, &is_anonymous, cu);
8375 if (is_anonymous)
8376 {
8377 const char *previous_prefix = determine_prefix (die, cu);
8378
8379 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8380 NULL, NULL, &objfile->objfile_obstack);
8381 }
8382 }
8383
8384 if (die->child != NULL)
8385 {
8386 struct die_info *child_die = die->child;
8387
8388 while (child_die && child_die->tag)
8389 {
8390 process_die (child_die, cu);
8391 child_die = sibling_die (child_die);
8392 }
8393 }
8394 }
8395
8396 /* Read a Fortran module as type. This DIE can be only a declaration used for
8397 imported module. Still we need that type as local Fortran "use ... only"
8398 declaration imports depend on the created type in determine_prefix. */
8399
8400 static struct type *
8401 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8402 {
8403 struct objfile *objfile = cu->objfile;
8404 char *module_name;
8405 struct type *type;
8406
8407 module_name = dwarf2_name (die, cu);
8408 if (!module_name)
8409 complaint (&symfile_complaints,
8410 _("DW_TAG_module has no name, offset 0x%x"),
8411 die->offset);
8412 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8413
8414 /* determine_prefix uses TYPE_TAG_NAME. */
8415 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8416
8417 return set_die_type (die, type, cu);
8418 }
8419
8420 /* Read a Fortran module. */
8421
8422 static void
8423 read_module (struct die_info *die, struct dwarf2_cu *cu)
8424 {
8425 struct die_info *child_die = die->child;
8426
8427 while (child_die && child_die->tag)
8428 {
8429 process_die (child_die, cu);
8430 child_die = sibling_die (child_die);
8431 }
8432 }
8433
8434 /* Return the name of the namespace represented by DIE. Set
8435 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8436 namespace. */
8437
8438 static const char *
8439 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8440 {
8441 struct die_info *current_die;
8442 const char *name = NULL;
8443
8444 /* Loop through the extensions until we find a name. */
8445
8446 for (current_die = die;
8447 current_die != NULL;
8448 current_die = dwarf2_extension (die, &cu))
8449 {
8450 name = dwarf2_name (current_die, cu);
8451 if (name != NULL)
8452 break;
8453 }
8454
8455 /* Is it an anonymous namespace? */
8456
8457 *is_anonymous = (name == NULL);
8458 if (*is_anonymous)
8459 name = CP_ANONYMOUS_NAMESPACE_STR;
8460
8461 return name;
8462 }
8463
8464 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8465 the user defined type vector. */
8466
8467 static struct type *
8468 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8469 {
8470 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8471 struct comp_unit_head *cu_header = &cu->header;
8472 struct type *type;
8473 struct attribute *attr_byte_size;
8474 struct attribute *attr_address_class;
8475 int byte_size, addr_class;
8476 struct type *target_type;
8477
8478 target_type = die_type (die, cu);
8479
8480 /* The die_type call above may have already set the type for this DIE. */
8481 type = get_die_type (die, cu);
8482 if (type)
8483 return type;
8484
8485 type = lookup_pointer_type (target_type);
8486
8487 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8488 if (attr_byte_size)
8489 byte_size = DW_UNSND (attr_byte_size);
8490 else
8491 byte_size = cu_header->addr_size;
8492
8493 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8494 if (attr_address_class)
8495 addr_class = DW_UNSND (attr_address_class);
8496 else
8497 addr_class = DW_ADDR_none;
8498
8499 /* If the pointer size or address class is different than the
8500 default, create a type variant marked as such and set the
8501 length accordingly. */
8502 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8503 {
8504 if (gdbarch_address_class_type_flags_p (gdbarch))
8505 {
8506 int type_flags;
8507
8508 type_flags = gdbarch_address_class_type_flags
8509 (gdbarch, byte_size, addr_class);
8510 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8511 == 0);
8512 type = make_type_with_address_space (type, type_flags);
8513 }
8514 else if (TYPE_LENGTH (type) != byte_size)
8515 {
8516 complaint (&symfile_complaints,
8517 _("invalid pointer size %d"), byte_size);
8518 }
8519 else
8520 {
8521 /* Should we also complain about unhandled address classes? */
8522 }
8523 }
8524
8525 TYPE_LENGTH (type) = byte_size;
8526 return set_die_type (die, type, cu);
8527 }
8528
8529 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8530 the user defined type vector. */
8531
8532 static struct type *
8533 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8534 {
8535 struct type *type;
8536 struct type *to_type;
8537 struct type *domain;
8538
8539 to_type = die_type (die, cu);
8540 domain = die_containing_type (die, cu);
8541
8542 /* The calls above may have already set the type for this DIE. */
8543 type = get_die_type (die, cu);
8544 if (type)
8545 return type;
8546
8547 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8548 type = lookup_methodptr_type (to_type);
8549 else
8550 type = lookup_memberptr_type (to_type, domain);
8551
8552 return set_die_type (die, type, cu);
8553 }
8554
8555 /* Extract all information from a DW_TAG_reference_type DIE and add to
8556 the user defined type vector. */
8557
8558 static struct type *
8559 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8560 {
8561 struct comp_unit_head *cu_header = &cu->header;
8562 struct type *type, *target_type;
8563 struct attribute *attr;
8564
8565 target_type = die_type (die, cu);
8566
8567 /* The die_type call above may have already set the type for this DIE. */
8568 type = get_die_type (die, cu);
8569 if (type)
8570 return type;
8571
8572 type = lookup_reference_type (target_type);
8573 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8574 if (attr)
8575 {
8576 TYPE_LENGTH (type) = DW_UNSND (attr);
8577 }
8578 else
8579 {
8580 TYPE_LENGTH (type) = cu_header->addr_size;
8581 }
8582 return set_die_type (die, type, cu);
8583 }
8584
8585 static struct type *
8586 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8587 {
8588 struct type *base_type, *cv_type;
8589
8590 base_type = die_type (die, cu);
8591
8592 /* The die_type call above may have already set the type for this DIE. */
8593 cv_type = get_die_type (die, cu);
8594 if (cv_type)
8595 return cv_type;
8596
8597 /* In case the const qualifier is applied to an array type, the element type
8598 is so qualified, not the array type (section 6.7.3 of C99). */
8599 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8600 {
8601 struct type *el_type, *inner_array;
8602
8603 base_type = copy_type (base_type);
8604 inner_array = base_type;
8605
8606 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8607 {
8608 TYPE_TARGET_TYPE (inner_array) =
8609 copy_type (TYPE_TARGET_TYPE (inner_array));
8610 inner_array = TYPE_TARGET_TYPE (inner_array);
8611 }
8612
8613 el_type = TYPE_TARGET_TYPE (inner_array);
8614 TYPE_TARGET_TYPE (inner_array) =
8615 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8616
8617 return set_die_type (die, base_type, cu);
8618 }
8619
8620 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8621 return set_die_type (die, cv_type, cu);
8622 }
8623
8624 static struct type *
8625 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8626 {
8627 struct type *base_type, *cv_type;
8628
8629 base_type = die_type (die, cu);
8630
8631 /* The die_type call above may have already set the type for this DIE. */
8632 cv_type = get_die_type (die, cu);
8633 if (cv_type)
8634 return cv_type;
8635
8636 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8637 return set_die_type (die, cv_type, cu);
8638 }
8639
8640 /* Extract all information from a DW_TAG_string_type DIE and add to
8641 the user defined type vector. It isn't really a user defined type,
8642 but it behaves like one, with other DIE's using an AT_user_def_type
8643 attribute to reference it. */
8644
8645 static struct type *
8646 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8647 {
8648 struct objfile *objfile = cu->objfile;
8649 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8650 struct type *type, *range_type, *index_type, *char_type;
8651 struct attribute *attr;
8652 unsigned int length;
8653
8654 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8655 if (attr)
8656 {
8657 length = DW_UNSND (attr);
8658 }
8659 else
8660 {
8661 /* Check for the DW_AT_byte_size attribute. */
8662 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8663 if (attr)
8664 {
8665 length = DW_UNSND (attr);
8666 }
8667 else
8668 {
8669 length = 1;
8670 }
8671 }
8672
8673 index_type = objfile_type (objfile)->builtin_int;
8674 range_type = create_range_type (NULL, index_type, 1, length);
8675 char_type = language_string_char_type (cu->language_defn, gdbarch);
8676 type = create_string_type (NULL, char_type, range_type);
8677
8678 return set_die_type (die, type, cu);
8679 }
8680
8681 /* Handle DIES due to C code like:
8682
8683 struct foo
8684 {
8685 int (*funcp)(int a, long l);
8686 int b;
8687 };
8688
8689 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8690
8691 static struct type *
8692 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8693 {
8694 struct type *type; /* Type that this function returns. */
8695 struct type *ftype; /* Function that returns above type. */
8696 struct attribute *attr;
8697
8698 type = die_type (die, cu);
8699
8700 /* The die_type call above may have already set the type for this DIE. */
8701 ftype = get_die_type (die, cu);
8702 if (ftype)
8703 return ftype;
8704
8705 ftype = lookup_function_type (type);
8706
8707 /* All functions in C++, Pascal and Java have prototypes. */
8708 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8709 if ((attr && (DW_UNSND (attr) != 0))
8710 || cu->language == language_cplus
8711 || cu->language == language_java
8712 || cu->language == language_pascal)
8713 TYPE_PROTOTYPED (ftype) = 1;
8714 else if (producer_is_realview (cu->producer))
8715 /* RealView does not emit DW_AT_prototyped. We can not
8716 distinguish prototyped and unprototyped functions; default to
8717 prototyped, since that is more common in modern code (and
8718 RealView warns about unprototyped functions). */
8719 TYPE_PROTOTYPED (ftype) = 1;
8720
8721 /* Store the calling convention in the type if it's available in
8722 the subroutine die. Otherwise set the calling convention to
8723 the default value DW_CC_normal. */
8724 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8725 if (attr)
8726 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8727 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8728 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8729 else
8730 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8731
8732 /* We need to add the subroutine type to the die immediately so
8733 we don't infinitely recurse when dealing with parameters
8734 declared as the same subroutine type. */
8735 set_die_type (die, ftype, cu);
8736
8737 if (die->child != NULL)
8738 {
8739 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8740 struct die_info *child_die;
8741 int nparams, iparams;
8742
8743 /* Count the number of parameters.
8744 FIXME: GDB currently ignores vararg functions, but knows about
8745 vararg member functions. */
8746 nparams = 0;
8747 child_die = die->child;
8748 while (child_die && child_die->tag)
8749 {
8750 if (child_die->tag == DW_TAG_formal_parameter)
8751 nparams++;
8752 else if (child_die->tag == DW_TAG_unspecified_parameters)
8753 TYPE_VARARGS (ftype) = 1;
8754 child_die = sibling_die (child_die);
8755 }
8756
8757 /* Allocate storage for parameters and fill them in. */
8758 TYPE_NFIELDS (ftype) = nparams;
8759 TYPE_FIELDS (ftype) = (struct field *)
8760 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8761
8762 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8763 even if we error out during the parameters reading below. */
8764 for (iparams = 0; iparams < nparams; iparams++)
8765 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8766
8767 iparams = 0;
8768 child_die = die->child;
8769 while (child_die && child_die->tag)
8770 {
8771 if (child_die->tag == DW_TAG_formal_parameter)
8772 {
8773 struct type *arg_type;
8774
8775 /* DWARF version 2 has no clean way to discern C++
8776 static and non-static member functions. G++ helps
8777 GDB by marking the first parameter for non-static
8778 member functions (which is the this pointer) as
8779 artificial. We pass this information to
8780 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8781
8782 DWARF version 3 added DW_AT_object_pointer, which GCC
8783 4.5 does not yet generate. */
8784 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8785 if (attr)
8786 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8787 else
8788 {
8789 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8790
8791 /* GCC/43521: In java, the formal parameter
8792 "this" is sometimes not marked with DW_AT_artificial. */
8793 if (cu->language == language_java)
8794 {
8795 const char *name = dwarf2_name (child_die, cu);
8796
8797 if (name && !strcmp (name, "this"))
8798 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8799 }
8800 }
8801 arg_type = die_type (child_die, cu);
8802
8803 /* RealView does not mark THIS as const, which the testsuite
8804 expects. GCC marks THIS as const in method definitions,
8805 but not in the class specifications (GCC PR 43053). */
8806 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8807 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8808 {
8809 int is_this = 0;
8810 struct dwarf2_cu *arg_cu = cu;
8811 const char *name = dwarf2_name (child_die, cu);
8812
8813 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8814 if (attr)
8815 {
8816 /* If the compiler emits this, use it. */
8817 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8818 is_this = 1;
8819 }
8820 else if (name && strcmp (name, "this") == 0)
8821 /* Function definitions will have the argument names. */
8822 is_this = 1;
8823 else if (name == NULL && iparams == 0)
8824 /* Declarations may not have the names, so like
8825 elsewhere in GDB, assume an artificial first
8826 argument is "this". */
8827 is_this = 1;
8828
8829 if (is_this)
8830 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8831 arg_type, 0);
8832 }
8833
8834 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8835 iparams++;
8836 }
8837 child_die = sibling_die (child_die);
8838 }
8839 }
8840
8841 return ftype;
8842 }
8843
8844 static struct type *
8845 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8846 {
8847 struct objfile *objfile = cu->objfile;
8848 const char *name = NULL;
8849 struct type *this_type;
8850
8851 name = dwarf2_full_name (NULL, die, cu);
8852 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8853 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8854 TYPE_NAME (this_type) = (char *) name;
8855 set_die_type (die, this_type, cu);
8856 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8857 return this_type;
8858 }
8859
8860 /* Find a representation of a given base type and install
8861 it in the TYPE field of the die. */
8862
8863 static struct type *
8864 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8865 {
8866 struct objfile *objfile = cu->objfile;
8867 struct type *type;
8868 struct attribute *attr;
8869 int encoding = 0, size = 0;
8870 char *name;
8871 enum type_code code = TYPE_CODE_INT;
8872 int type_flags = 0;
8873 struct type *target_type = NULL;
8874
8875 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8876 if (attr)
8877 {
8878 encoding = DW_UNSND (attr);
8879 }
8880 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8881 if (attr)
8882 {
8883 size = DW_UNSND (attr);
8884 }
8885 name = dwarf2_name (die, cu);
8886 if (!name)
8887 {
8888 complaint (&symfile_complaints,
8889 _("DW_AT_name missing from DW_TAG_base_type"));
8890 }
8891
8892 switch (encoding)
8893 {
8894 case DW_ATE_address:
8895 /* Turn DW_ATE_address into a void * pointer. */
8896 code = TYPE_CODE_PTR;
8897 type_flags |= TYPE_FLAG_UNSIGNED;
8898 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8899 break;
8900 case DW_ATE_boolean:
8901 code = TYPE_CODE_BOOL;
8902 type_flags |= TYPE_FLAG_UNSIGNED;
8903 break;
8904 case DW_ATE_complex_float:
8905 code = TYPE_CODE_COMPLEX;
8906 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8907 break;
8908 case DW_ATE_decimal_float:
8909 code = TYPE_CODE_DECFLOAT;
8910 break;
8911 case DW_ATE_float:
8912 code = TYPE_CODE_FLT;
8913 break;
8914 case DW_ATE_signed:
8915 break;
8916 case DW_ATE_unsigned:
8917 type_flags |= TYPE_FLAG_UNSIGNED;
8918 if (cu->language == language_fortran
8919 && name
8920 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8921 code = TYPE_CODE_CHAR;
8922 break;
8923 case DW_ATE_signed_char:
8924 if (cu->language == language_ada || cu->language == language_m2
8925 || cu->language == language_pascal
8926 || cu->language == language_fortran)
8927 code = TYPE_CODE_CHAR;
8928 break;
8929 case DW_ATE_unsigned_char:
8930 if (cu->language == language_ada || cu->language == language_m2
8931 || cu->language == language_pascal
8932 || cu->language == language_fortran)
8933 code = TYPE_CODE_CHAR;
8934 type_flags |= TYPE_FLAG_UNSIGNED;
8935 break;
8936 case DW_ATE_UTF:
8937 /* We just treat this as an integer and then recognize the
8938 type by name elsewhere. */
8939 break;
8940
8941 default:
8942 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8943 dwarf_type_encoding_name (encoding));
8944 break;
8945 }
8946
8947 type = init_type (code, size, type_flags, NULL, objfile);
8948 TYPE_NAME (type) = name;
8949 TYPE_TARGET_TYPE (type) = target_type;
8950
8951 if (name && strcmp (name, "char") == 0)
8952 TYPE_NOSIGN (type) = 1;
8953
8954 return set_die_type (die, type, cu);
8955 }
8956
8957 /* Read the given DW_AT_subrange DIE. */
8958
8959 static struct type *
8960 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8961 {
8962 struct type *base_type;
8963 struct type *range_type;
8964 struct attribute *attr;
8965 LONGEST low = 0;
8966 LONGEST high = -1;
8967 char *name;
8968 LONGEST negative_mask;
8969
8970 base_type = die_type (die, cu);
8971 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8972 check_typedef (base_type);
8973
8974 /* The die_type call above may have already set the type for this DIE. */
8975 range_type = get_die_type (die, cu);
8976 if (range_type)
8977 return range_type;
8978
8979 if (cu->language == language_fortran)
8980 {
8981 /* FORTRAN implies a lower bound of 1, if not given. */
8982 low = 1;
8983 }
8984
8985 /* FIXME: For variable sized arrays either of these could be
8986 a variable rather than a constant value. We'll allow it,
8987 but we don't know how to handle it. */
8988 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8989 if (attr)
8990 low = dwarf2_get_attr_constant_value (attr, 0);
8991
8992 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8993 if (attr)
8994 {
8995 if (attr_form_is_block (attr) || is_ref_attr (attr))
8996 {
8997 /* GCC encodes arrays with unspecified or dynamic length
8998 with a DW_FORM_block1 attribute or a reference attribute.
8999 FIXME: GDB does not yet know how to handle dynamic
9000 arrays properly, treat them as arrays with unspecified
9001 length for now.
9002
9003 FIXME: jimb/2003-09-22: GDB does not really know
9004 how to handle arrays of unspecified length
9005 either; we just represent them as zero-length
9006 arrays. Choose an appropriate upper bound given
9007 the lower bound we've computed above. */
9008 high = low - 1;
9009 }
9010 else
9011 high = dwarf2_get_attr_constant_value (attr, 1);
9012 }
9013 else
9014 {
9015 attr = dwarf2_attr (die, DW_AT_count, cu);
9016 if (attr)
9017 {
9018 int count = dwarf2_get_attr_constant_value (attr, 1);
9019 high = low + count - 1;
9020 }
9021 else
9022 {
9023 /* Unspecified array length. */
9024 high = low - 1;
9025 }
9026 }
9027
9028 /* Dwarf-2 specifications explicitly allows to create subrange types
9029 without specifying a base type.
9030 In that case, the base type must be set to the type of
9031 the lower bound, upper bound or count, in that order, if any of these
9032 three attributes references an object that has a type.
9033 If no base type is found, the Dwarf-2 specifications say that
9034 a signed integer type of size equal to the size of an address should
9035 be used.
9036 For the following C code: `extern char gdb_int [];'
9037 GCC produces an empty range DIE.
9038 FIXME: muller/2010-05-28: Possible references to object for low bound,
9039 high bound or count are not yet handled by this code. */
9040 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9041 {
9042 struct objfile *objfile = cu->objfile;
9043 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9044 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9045 struct type *int_type = objfile_type (objfile)->builtin_int;
9046
9047 /* Test "int", "long int", and "long long int" objfile types,
9048 and select the first one having a size above or equal to the
9049 architecture address size. */
9050 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9051 base_type = int_type;
9052 else
9053 {
9054 int_type = objfile_type (objfile)->builtin_long;
9055 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9056 base_type = int_type;
9057 else
9058 {
9059 int_type = objfile_type (objfile)->builtin_long_long;
9060 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9061 base_type = int_type;
9062 }
9063 }
9064 }
9065
9066 negative_mask =
9067 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9068 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9069 low |= negative_mask;
9070 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9071 high |= negative_mask;
9072
9073 range_type = create_range_type (NULL, base_type, low, high);
9074
9075 /* Mark arrays with dynamic length at least as an array of unspecified
9076 length. GDB could check the boundary but before it gets implemented at
9077 least allow accessing the array elements. */
9078 if (attr && attr_form_is_block (attr))
9079 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9080
9081 /* Ada expects an empty array on no boundary attributes. */
9082 if (attr == NULL && cu->language != language_ada)
9083 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9084
9085 name = dwarf2_name (die, cu);
9086 if (name)
9087 TYPE_NAME (range_type) = name;
9088
9089 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9090 if (attr)
9091 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9092
9093 set_die_type (die, range_type, cu);
9094
9095 /* set_die_type should be already done. */
9096 set_descriptive_type (range_type, die, cu);
9097
9098 return range_type;
9099 }
9100
9101 static struct type *
9102 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9103 {
9104 struct type *type;
9105
9106 /* For now, we only support the C meaning of an unspecified type: void. */
9107
9108 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9109 TYPE_NAME (type) = dwarf2_name (die, cu);
9110
9111 return set_die_type (die, type, cu);
9112 }
9113
9114 /* Trivial hash function for die_info: the hash value of a DIE
9115 is its offset in .debug_info for this objfile. */
9116
9117 static hashval_t
9118 die_hash (const void *item)
9119 {
9120 const struct die_info *die = item;
9121
9122 return die->offset;
9123 }
9124
9125 /* Trivial comparison function for die_info structures: two DIEs
9126 are equal if they have the same offset. */
9127
9128 static int
9129 die_eq (const void *item_lhs, const void *item_rhs)
9130 {
9131 const struct die_info *die_lhs = item_lhs;
9132 const struct die_info *die_rhs = item_rhs;
9133
9134 return die_lhs->offset == die_rhs->offset;
9135 }
9136
9137 /* Read a whole compilation unit into a linked list of dies. */
9138
9139 static struct die_info *
9140 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9141 {
9142 struct die_reader_specs reader_specs;
9143 int read_abbrevs = 0;
9144 struct cleanup *back_to = NULL;
9145 struct die_info *die;
9146
9147 if (cu->dwarf2_abbrevs == NULL)
9148 {
9149 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9150 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9151 read_abbrevs = 1;
9152 }
9153
9154 gdb_assert (cu->die_hash == NULL);
9155 cu->die_hash
9156 = htab_create_alloc_ex (cu->header.length / 12,
9157 die_hash,
9158 die_eq,
9159 NULL,
9160 &cu->comp_unit_obstack,
9161 hashtab_obstack_allocate,
9162 dummy_obstack_deallocate);
9163
9164 init_cu_die_reader (&reader_specs, cu);
9165
9166 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9167
9168 if (read_abbrevs)
9169 do_cleanups (back_to);
9170
9171 return die;
9172 }
9173
9174 /* Main entry point for reading a DIE and all children.
9175 Read the DIE and dump it if requested. */
9176
9177 static struct die_info *
9178 read_die_and_children (const struct die_reader_specs *reader,
9179 gdb_byte *info_ptr,
9180 gdb_byte **new_info_ptr,
9181 struct die_info *parent)
9182 {
9183 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9184 new_info_ptr, parent);
9185
9186 if (dwarf2_die_debug)
9187 {
9188 fprintf_unfiltered (gdb_stdlog,
9189 "\nRead die from %s of %s:\n",
9190 (reader->cu->per_cu->debug_type_section
9191 ? ".debug_types"
9192 : ".debug_info"),
9193 reader->abfd->filename);
9194 dump_die (result, dwarf2_die_debug);
9195 }
9196
9197 return result;
9198 }
9199
9200 /* Read a single die and all its descendents. Set the die's sibling
9201 field to NULL; set other fields in the die correctly, and set all
9202 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9203 location of the info_ptr after reading all of those dies. PARENT
9204 is the parent of the die in question. */
9205
9206 static struct die_info *
9207 read_die_and_children_1 (const struct die_reader_specs *reader,
9208 gdb_byte *info_ptr,
9209 gdb_byte **new_info_ptr,
9210 struct die_info *parent)
9211 {
9212 struct die_info *die;
9213 gdb_byte *cur_ptr;
9214 int has_children;
9215
9216 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9217 if (die == NULL)
9218 {
9219 *new_info_ptr = cur_ptr;
9220 return NULL;
9221 }
9222 store_in_ref_table (die, reader->cu);
9223
9224 if (has_children)
9225 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9226 else
9227 {
9228 die->child = NULL;
9229 *new_info_ptr = cur_ptr;
9230 }
9231
9232 die->sibling = NULL;
9233 die->parent = parent;
9234 return die;
9235 }
9236
9237 /* Read a die, all of its descendents, and all of its siblings; set
9238 all of the fields of all of the dies correctly. Arguments are as
9239 in read_die_and_children. */
9240
9241 static struct die_info *
9242 read_die_and_siblings (const struct die_reader_specs *reader,
9243 gdb_byte *info_ptr,
9244 gdb_byte **new_info_ptr,
9245 struct die_info *parent)
9246 {
9247 struct die_info *first_die, *last_sibling;
9248 gdb_byte *cur_ptr;
9249
9250 cur_ptr = info_ptr;
9251 first_die = last_sibling = NULL;
9252
9253 while (1)
9254 {
9255 struct die_info *die
9256 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9257
9258 if (die == NULL)
9259 {
9260 *new_info_ptr = cur_ptr;
9261 return first_die;
9262 }
9263
9264 if (!first_die)
9265 first_die = die;
9266 else
9267 last_sibling->sibling = die;
9268
9269 last_sibling = die;
9270 }
9271 }
9272
9273 /* Read the die from the .debug_info section buffer. Set DIEP to
9274 point to a newly allocated die with its information, except for its
9275 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9276 whether the die has children or not. */
9277
9278 static gdb_byte *
9279 read_full_die (const struct die_reader_specs *reader,
9280 struct die_info **diep, gdb_byte *info_ptr,
9281 int *has_children)
9282 {
9283 unsigned int abbrev_number, bytes_read, i, offset;
9284 struct abbrev_info *abbrev;
9285 struct die_info *die;
9286 struct dwarf2_cu *cu = reader->cu;
9287 bfd *abfd = reader->abfd;
9288
9289 offset = info_ptr - reader->buffer;
9290 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9291 info_ptr += bytes_read;
9292 if (!abbrev_number)
9293 {
9294 *diep = NULL;
9295 *has_children = 0;
9296 return info_ptr;
9297 }
9298
9299 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9300 if (!abbrev)
9301 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9302 abbrev_number,
9303 bfd_get_filename (abfd));
9304
9305 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9306 die->offset = offset;
9307 die->tag = abbrev->tag;
9308 die->abbrev = abbrev_number;
9309
9310 die->num_attrs = abbrev->num_attrs;
9311
9312 for (i = 0; i < abbrev->num_attrs; ++i)
9313 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9314 abfd, info_ptr, cu);
9315
9316 *diep = die;
9317 *has_children = abbrev->has_children;
9318 return info_ptr;
9319 }
9320
9321 /* In DWARF version 2, the description of the debugging information is
9322 stored in a separate .debug_abbrev section. Before we read any
9323 dies from a section we read in all abbreviations and install them
9324 in a hash table. This function also sets flags in CU describing
9325 the data found in the abbrev table. */
9326
9327 static void
9328 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
9329 {
9330 struct comp_unit_head *cu_header = &cu->header;
9331 gdb_byte *abbrev_ptr;
9332 struct abbrev_info *cur_abbrev;
9333 unsigned int abbrev_number, bytes_read, abbrev_name;
9334 unsigned int abbrev_form, hash_number;
9335 struct attr_abbrev *cur_attrs;
9336 unsigned int allocated_attrs;
9337
9338 /* Initialize dwarf2 abbrevs. */
9339 obstack_init (&cu->abbrev_obstack);
9340 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9341 (ABBREV_HASH_SIZE
9342 * sizeof (struct abbrev_info *)));
9343 memset (cu->dwarf2_abbrevs, 0,
9344 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9345
9346 dwarf2_read_section (dwarf2_per_objfile->objfile,
9347 &dwarf2_per_objfile->abbrev);
9348 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9349 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9350 abbrev_ptr += bytes_read;
9351
9352 allocated_attrs = ATTR_ALLOC_CHUNK;
9353 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9354
9355 /* Loop until we reach an abbrev number of 0. */
9356 while (abbrev_number)
9357 {
9358 cur_abbrev = dwarf_alloc_abbrev (cu);
9359
9360 /* read in abbrev header */
9361 cur_abbrev->number = abbrev_number;
9362 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9363 abbrev_ptr += bytes_read;
9364 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9365 abbrev_ptr += 1;
9366
9367 if (cur_abbrev->tag == DW_TAG_namespace)
9368 cu->has_namespace_info = 1;
9369
9370 /* now read in declarations */
9371 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9372 abbrev_ptr += bytes_read;
9373 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9374 abbrev_ptr += bytes_read;
9375 while (abbrev_name)
9376 {
9377 if (cur_abbrev->num_attrs == allocated_attrs)
9378 {
9379 allocated_attrs += ATTR_ALLOC_CHUNK;
9380 cur_attrs
9381 = xrealloc (cur_attrs, (allocated_attrs
9382 * sizeof (struct attr_abbrev)));
9383 }
9384
9385 /* Record whether this compilation unit might have
9386 inter-compilation-unit references. If we don't know what form
9387 this attribute will have, then it might potentially be a
9388 DW_FORM_ref_addr, so we conservatively expect inter-CU
9389 references. */
9390
9391 if (abbrev_form == DW_FORM_ref_addr
9392 || abbrev_form == DW_FORM_indirect)
9393 cu->has_form_ref_addr = 1;
9394
9395 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9396 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9397 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9398 abbrev_ptr += bytes_read;
9399 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9400 abbrev_ptr += bytes_read;
9401 }
9402
9403 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9404 (cur_abbrev->num_attrs
9405 * sizeof (struct attr_abbrev)));
9406 memcpy (cur_abbrev->attrs, cur_attrs,
9407 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9408
9409 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9410 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9411 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9412
9413 /* Get next abbreviation.
9414 Under Irix6 the abbreviations for a compilation unit are not
9415 always properly terminated with an abbrev number of 0.
9416 Exit loop if we encounter an abbreviation which we have
9417 already read (which means we are about to read the abbreviations
9418 for the next compile unit) or if the end of the abbreviation
9419 table is reached. */
9420 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9421 >= dwarf2_per_objfile->abbrev.size)
9422 break;
9423 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9424 abbrev_ptr += bytes_read;
9425 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9426 break;
9427 }
9428
9429 xfree (cur_attrs);
9430 }
9431
9432 /* Release the memory used by the abbrev table for a compilation unit. */
9433
9434 static void
9435 dwarf2_free_abbrev_table (void *ptr_to_cu)
9436 {
9437 struct dwarf2_cu *cu = ptr_to_cu;
9438
9439 obstack_free (&cu->abbrev_obstack, NULL);
9440 cu->dwarf2_abbrevs = NULL;
9441 }
9442
9443 /* Lookup an abbrev_info structure in the abbrev hash table. */
9444
9445 static struct abbrev_info *
9446 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9447 {
9448 unsigned int hash_number;
9449 struct abbrev_info *abbrev;
9450
9451 hash_number = number % ABBREV_HASH_SIZE;
9452 abbrev = cu->dwarf2_abbrevs[hash_number];
9453
9454 while (abbrev)
9455 {
9456 if (abbrev->number == number)
9457 return abbrev;
9458 else
9459 abbrev = abbrev->next;
9460 }
9461 return NULL;
9462 }
9463
9464 /* Returns nonzero if TAG represents a type that we might generate a partial
9465 symbol for. */
9466
9467 static int
9468 is_type_tag_for_partial (int tag)
9469 {
9470 switch (tag)
9471 {
9472 #if 0
9473 /* Some types that would be reasonable to generate partial symbols for,
9474 that we don't at present. */
9475 case DW_TAG_array_type:
9476 case DW_TAG_file_type:
9477 case DW_TAG_ptr_to_member_type:
9478 case DW_TAG_set_type:
9479 case DW_TAG_string_type:
9480 case DW_TAG_subroutine_type:
9481 #endif
9482 case DW_TAG_base_type:
9483 case DW_TAG_class_type:
9484 case DW_TAG_interface_type:
9485 case DW_TAG_enumeration_type:
9486 case DW_TAG_structure_type:
9487 case DW_TAG_subrange_type:
9488 case DW_TAG_typedef:
9489 case DW_TAG_union_type:
9490 return 1;
9491 default:
9492 return 0;
9493 }
9494 }
9495
9496 /* Load all DIEs that are interesting for partial symbols into memory. */
9497
9498 static struct partial_die_info *
9499 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9500 int building_psymtab, struct dwarf2_cu *cu)
9501 {
9502 struct partial_die_info *part_die;
9503 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9504 struct abbrev_info *abbrev;
9505 unsigned int bytes_read;
9506 unsigned int load_all = 0;
9507
9508 int nesting_level = 1;
9509
9510 parent_die = NULL;
9511 last_die = NULL;
9512
9513 if (cu->per_cu && cu->per_cu->load_all_dies)
9514 load_all = 1;
9515
9516 cu->partial_dies
9517 = htab_create_alloc_ex (cu->header.length / 12,
9518 partial_die_hash,
9519 partial_die_eq,
9520 NULL,
9521 &cu->comp_unit_obstack,
9522 hashtab_obstack_allocate,
9523 dummy_obstack_deallocate);
9524
9525 part_die = obstack_alloc (&cu->comp_unit_obstack,
9526 sizeof (struct partial_die_info));
9527
9528 while (1)
9529 {
9530 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9531
9532 /* A NULL abbrev means the end of a series of children. */
9533 if (abbrev == NULL)
9534 {
9535 if (--nesting_level == 0)
9536 {
9537 /* PART_DIE was probably the last thing allocated on the
9538 comp_unit_obstack, so we could call obstack_free
9539 here. We don't do that because the waste is small,
9540 and will be cleaned up when we're done with this
9541 compilation unit. This way, we're also more robust
9542 against other users of the comp_unit_obstack. */
9543 return first_die;
9544 }
9545 info_ptr += bytes_read;
9546 last_die = parent_die;
9547 parent_die = parent_die->die_parent;
9548 continue;
9549 }
9550
9551 /* Check for template arguments. We never save these; if
9552 they're seen, we just mark the parent, and go on our way. */
9553 if (parent_die != NULL
9554 && cu->language == language_cplus
9555 && (abbrev->tag == DW_TAG_template_type_param
9556 || abbrev->tag == DW_TAG_template_value_param))
9557 {
9558 parent_die->has_template_arguments = 1;
9559
9560 if (!load_all)
9561 {
9562 /* We don't need a partial DIE for the template argument. */
9563 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9564 cu);
9565 continue;
9566 }
9567 }
9568
9569 /* We only recurse into subprograms looking for template arguments.
9570 Skip their other children. */
9571 if (!load_all
9572 && cu->language == language_cplus
9573 && parent_die != NULL
9574 && parent_die->tag == DW_TAG_subprogram)
9575 {
9576 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9577 continue;
9578 }
9579
9580 /* Check whether this DIE is interesting enough to save. Normally
9581 we would not be interested in members here, but there may be
9582 later variables referencing them via DW_AT_specification (for
9583 static members). */
9584 if (!load_all
9585 && !is_type_tag_for_partial (abbrev->tag)
9586 && abbrev->tag != DW_TAG_constant
9587 && abbrev->tag != DW_TAG_enumerator
9588 && abbrev->tag != DW_TAG_subprogram
9589 && abbrev->tag != DW_TAG_lexical_block
9590 && abbrev->tag != DW_TAG_variable
9591 && abbrev->tag != DW_TAG_namespace
9592 && abbrev->tag != DW_TAG_module
9593 && abbrev->tag != DW_TAG_member)
9594 {
9595 /* Otherwise we skip to the next sibling, if any. */
9596 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9597 continue;
9598 }
9599
9600 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9601 buffer, info_ptr, cu);
9602
9603 /* This two-pass algorithm for processing partial symbols has a
9604 high cost in cache pressure. Thus, handle some simple cases
9605 here which cover the majority of C partial symbols. DIEs
9606 which neither have specification tags in them, nor could have
9607 specification tags elsewhere pointing at them, can simply be
9608 processed and discarded.
9609
9610 This segment is also optional; scan_partial_symbols and
9611 add_partial_symbol will handle these DIEs if we chain
9612 them in normally. When compilers which do not emit large
9613 quantities of duplicate debug information are more common,
9614 this code can probably be removed. */
9615
9616 /* Any complete simple types at the top level (pretty much all
9617 of them, for a language without namespaces), can be processed
9618 directly. */
9619 if (parent_die == NULL
9620 && part_die->has_specification == 0
9621 && part_die->is_declaration == 0
9622 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9623 || part_die->tag == DW_TAG_base_type
9624 || part_die->tag == DW_TAG_subrange_type))
9625 {
9626 if (building_psymtab && part_die->name != NULL)
9627 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9628 VAR_DOMAIN, LOC_TYPEDEF,
9629 &cu->objfile->static_psymbols,
9630 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9631 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9632 continue;
9633 }
9634
9635 /* The exception for DW_TAG_typedef with has_children above is
9636 a workaround of GCC PR debug/47510. In the case of this complaint
9637 type_name_no_tag_or_error will error on such types later.
9638
9639 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9640 it could not find the child DIEs referenced later, this is checked
9641 above. In correct DWARF DW_TAG_typedef should have no children. */
9642
9643 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9644 complaint (&symfile_complaints,
9645 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9646 "- DIE at 0x%x [in module %s]"),
9647 part_die->offset, cu->objfile->name);
9648
9649 /* If we're at the second level, and we're an enumerator, and
9650 our parent has no specification (meaning possibly lives in a
9651 namespace elsewhere), then we can add the partial symbol now
9652 instead of queueing it. */
9653 if (part_die->tag == DW_TAG_enumerator
9654 && parent_die != NULL
9655 && parent_die->die_parent == NULL
9656 && parent_die->tag == DW_TAG_enumeration_type
9657 && parent_die->has_specification == 0)
9658 {
9659 if (part_die->name == NULL)
9660 complaint (&symfile_complaints,
9661 _("malformed enumerator DIE ignored"));
9662 else if (building_psymtab)
9663 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9664 VAR_DOMAIN, LOC_CONST,
9665 (cu->language == language_cplus
9666 || cu->language == language_java)
9667 ? &cu->objfile->global_psymbols
9668 : &cu->objfile->static_psymbols,
9669 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9670
9671 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9672 continue;
9673 }
9674
9675 /* We'll save this DIE so link it in. */
9676 part_die->die_parent = parent_die;
9677 part_die->die_sibling = NULL;
9678 part_die->die_child = NULL;
9679
9680 if (last_die && last_die == parent_die)
9681 last_die->die_child = part_die;
9682 else if (last_die)
9683 last_die->die_sibling = part_die;
9684
9685 last_die = part_die;
9686
9687 if (first_die == NULL)
9688 first_die = part_die;
9689
9690 /* Maybe add the DIE to the hash table. Not all DIEs that we
9691 find interesting need to be in the hash table, because we
9692 also have the parent/sibling/child chains; only those that we
9693 might refer to by offset later during partial symbol reading.
9694
9695 For now this means things that might have be the target of a
9696 DW_AT_specification, DW_AT_abstract_origin, or
9697 DW_AT_extension. DW_AT_extension will refer only to
9698 namespaces; DW_AT_abstract_origin refers to functions (and
9699 many things under the function DIE, but we do not recurse
9700 into function DIEs during partial symbol reading) and
9701 possibly variables as well; DW_AT_specification refers to
9702 declarations. Declarations ought to have the DW_AT_declaration
9703 flag. It happens that GCC forgets to put it in sometimes, but
9704 only for functions, not for types.
9705
9706 Adding more things than necessary to the hash table is harmless
9707 except for the performance cost. Adding too few will result in
9708 wasted time in find_partial_die, when we reread the compilation
9709 unit with load_all_dies set. */
9710
9711 if (load_all
9712 || abbrev->tag == DW_TAG_constant
9713 || abbrev->tag == DW_TAG_subprogram
9714 || abbrev->tag == DW_TAG_variable
9715 || abbrev->tag == DW_TAG_namespace
9716 || part_die->is_declaration)
9717 {
9718 void **slot;
9719
9720 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9721 part_die->offset, INSERT);
9722 *slot = part_die;
9723 }
9724
9725 part_die = obstack_alloc (&cu->comp_unit_obstack,
9726 sizeof (struct partial_die_info));
9727
9728 /* For some DIEs we want to follow their children (if any). For C
9729 we have no reason to follow the children of structures; for other
9730 languages we have to, so that we can get at method physnames
9731 to infer fully qualified class names, for DW_AT_specification,
9732 and for C++ template arguments. For C++, we also look one level
9733 inside functions to find template arguments (if the name of the
9734 function does not already contain the template arguments).
9735
9736 For Ada, we need to scan the children of subprograms and lexical
9737 blocks as well because Ada allows the definition of nested
9738 entities that could be interesting for the debugger, such as
9739 nested subprograms for instance. */
9740 if (last_die->has_children
9741 && (load_all
9742 || last_die->tag == DW_TAG_namespace
9743 || last_die->tag == DW_TAG_module
9744 || last_die->tag == DW_TAG_enumeration_type
9745 || (cu->language == language_cplus
9746 && last_die->tag == DW_TAG_subprogram
9747 && (last_die->name == NULL
9748 || strchr (last_die->name, '<') == NULL))
9749 || (cu->language != language_c
9750 && (last_die->tag == DW_TAG_class_type
9751 || last_die->tag == DW_TAG_interface_type
9752 || last_die->tag == DW_TAG_structure_type
9753 || last_die->tag == DW_TAG_union_type))
9754 || (cu->language == language_ada
9755 && (last_die->tag == DW_TAG_subprogram
9756 || last_die->tag == DW_TAG_lexical_block))))
9757 {
9758 nesting_level++;
9759 parent_die = last_die;
9760 continue;
9761 }
9762
9763 /* Otherwise we skip to the next sibling, if any. */
9764 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9765
9766 /* Back to the top, do it again. */
9767 }
9768 }
9769
9770 /* Read a minimal amount of information into the minimal die structure. */
9771
9772 static gdb_byte *
9773 read_partial_die (struct partial_die_info *part_die,
9774 struct abbrev_info *abbrev,
9775 unsigned int abbrev_len, bfd *abfd,
9776 gdb_byte *buffer, gdb_byte *info_ptr,
9777 struct dwarf2_cu *cu)
9778 {
9779 unsigned int i;
9780 struct attribute attr;
9781 int has_low_pc_attr = 0;
9782 int has_high_pc_attr = 0;
9783
9784 memset (part_die, 0, sizeof (struct partial_die_info));
9785
9786 part_die->offset = info_ptr - buffer;
9787
9788 info_ptr += abbrev_len;
9789
9790 if (abbrev == NULL)
9791 return info_ptr;
9792
9793 part_die->tag = abbrev->tag;
9794 part_die->has_children = abbrev->has_children;
9795
9796 for (i = 0; i < abbrev->num_attrs; ++i)
9797 {
9798 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9799
9800 /* Store the data if it is of an attribute we want to keep in a
9801 partial symbol table. */
9802 switch (attr.name)
9803 {
9804 case DW_AT_name:
9805 switch (part_die->tag)
9806 {
9807 case DW_TAG_compile_unit:
9808 case DW_TAG_type_unit:
9809 /* Compilation units have a DW_AT_name that is a filename, not
9810 a source language identifier. */
9811 case DW_TAG_enumeration_type:
9812 case DW_TAG_enumerator:
9813 /* These tags always have simple identifiers already; no need
9814 to canonicalize them. */
9815 part_die->name = DW_STRING (&attr);
9816 break;
9817 default:
9818 part_die->name
9819 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9820 &cu->objfile->objfile_obstack);
9821 break;
9822 }
9823 break;
9824 case DW_AT_linkage_name:
9825 case DW_AT_MIPS_linkage_name:
9826 /* Note that both forms of linkage name might appear. We
9827 assume they will be the same, and we only store the last
9828 one we see. */
9829 if (cu->language == language_ada)
9830 part_die->name = DW_STRING (&attr);
9831 part_die->linkage_name = DW_STRING (&attr);
9832 break;
9833 case DW_AT_low_pc:
9834 has_low_pc_attr = 1;
9835 part_die->lowpc = DW_ADDR (&attr);
9836 break;
9837 case DW_AT_high_pc:
9838 has_high_pc_attr = 1;
9839 part_die->highpc = DW_ADDR (&attr);
9840 break;
9841 case DW_AT_location:
9842 /* Support the .debug_loc offsets. */
9843 if (attr_form_is_block (&attr))
9844 {
9845 part_die->locdesc = DW_BLOCK (&attr);
9846 }
9847 else if (attr_form_is_section_offset (&attr))
9848 {
9849 dwarf2_complex_location_expr_complaint ();
9850 }
9851 else
9852 {
9853 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9854 "partial symbol information");
9855 }
9856 break;
9857 case DW_AT_external:
9858 part_die->is_external = DW_UNSND (&attr);
9859 break;
9860 case DW_AT_declaration:
9861 part_die->is_declaration = DW_UNSND (&attr);
9862 break;
9863 case DW_AT_type:
9864 part_die->has_type = 1;
9865 break;
9866 case DW_AT_abstract_origin:
9867 case DW_AT_specification:
9868 case DW_AT_extension:
9869 part_die->has_specification = 1;
9870 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9871 break;
9872 case DW_AT_sibling:
9873 /* Ignore absolute siblings, they might point outside of
9874 the current compile unit. */
9875 if (attr.form == DW_FORM_ref_addr)
9876 complaint (&symfile_complaints,
9877 _("ignoring absolute DW_AT_sibling"));
9878 else
9879 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9880 break;
9881 case DW_AT_byte_size:
9882 part_die->has_byte_size = 1;
9883 break;
9884 case DW_AT_calling_convention:
9885 /* DWARF doesn't provide a way to identify a program's source-level
9886 entry point. DW_AT_calling_convention attributes are only meant
9887 to describe functions' calling conventions.
9888
9889 However, because it's a necessary piece of information in
9890 Fortran, and because DW_CC_program is the only piece of debugging
9891 information whose definition refers to a 'main program' at all,
9892 several compilers have begun marking Fortran main programs with
9893 DW_CC_program --- even when those functions use the standard
9894 calling conventions.
9895
9896 So until DWARF specifies a way to provide this information and
9897 compilers pick up the new representation, we'll support this
9898 practice. */
9899 if (DW_UNSND (&attr) == DW_CC_program
9900 && cu->language == language_fortran)
9901 {
9902 set_main_name (part_die->name);
9903
9904 /* As this DIE has a static linkage the name would be difficult
9905 to look up later. */
9906 language_of_main = language_fortran;
9907 }
9908 break;
9909 default:
9910 break;
9911 }
9912 }
9913
9914 if (has_low_pc_attr && has_high_pc_attr)
9915 {
9916 /* When using the GNU linker, .gnu.linkonce. sections are used to
9917 eliminate duplicate copies of functions and vtables and such.
9918 The linker will arbitrarily choose one and discard the others.
9919 The AT_*_pc values for such functions refer to local labels in
9920 these sections. If the section from that file was discarded, the
9921 labels are not in the output, so the relocs get a value of 0.
9922 If this is a discarded function, mark the pc bounds as invalid,
9923 so that GDB will ignore it. */
9924 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9925 {
9926 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9927
9928 complaint (&symfile_complaints,
9929 _("DW_AT_low_pc %s is zero "
9930 "for DIE at 0x%x [in module %s]"),
9931 paddress (gdbarch, part_die->lowpc),
9932 part_die->offset, cu->objfile->name);
9933 }
9934 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9935 else if (part_die->lowpc >= part_die->highpc)
9936 {
9937 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9938
9939 complaint (&symfile_complaints,
9940 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9941 "for DIE at 0x%x [in module %s]"),
9942 paddress (gdbarch, part_die->lowpc),
9943 paddress (gdbarch, part_die->highpc),
9944 part_die->offset, cu->objfile->name);
9945 }
9946 else
9947 part_die->has_pc_info = 1;
9948 }
9949
9950 return info_ptr;
9951 }
9952
9953 /* Find a cached partial DIE at OFFSET in CU. */
9954
9955 static struct partial_die_info *
9956 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9957 {
9958 struct partial_die_info *lookup_die = NULL;
9959 struct partial_die_info part_die;
9960
9961 part_die.offset = offset;
9962 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9963
9964 return lookup_die;
9965 }
9966
9967 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9968 except in the case of .debug_types DIEs which do not reference
9969 outside their CU (they do however referencing other types via
9970 DW_FORM_ref_sig8). */
9971
9972 static struct partial_die_info *
9973 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9974 {
9975 struct dwarf2_per_cu_data *per_cu = NULL;
9976 struct partial_die_info *pd = NULL;
9977
9978 if (cu->per_cu->debug_type_section)
9979 {
9980 pd = find_partial_die_in_comp_unit (offset, cu);
9981 if (pd != NULL)
9982 return pd;
9983 goto not_found;
9984 }
9985
9986 if (offset_in_cu_p (&cu->header, offset))
9987 {
9988 pd = find_partial_die_in_comp_unit (offset, cu);
9989 if (pd != NULL)
9990 return pd;
9991 }
9992
9993 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9994
9995 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9996 load_partial_comp_unit (per_cu, cu->objfile);
9997
9998 per_cu->cu->last_used = 0;
9999 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10000
10001 if (pd == NULL && per_cu->load_all_dies == 0)
10002 {
10003 struct cleanup *back_to;
10004 struct partial_die_info comp_unit_die;
10005 struct abbrev_info *abbrev;
10006 unsigned int bytes_read;
10007 char *info_ptr;
10008
10009 per_cu->load_all_dies = 1;
10010
10011 /* Re-read the DIEs. */
10012 back_to = make_cleanup (null_cleanup, 0);
10013 if (per_cu->cu->dwarf2_abbrevs == NULL)
10014 {
10015 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
10016 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10017 }
10018 info_ptr = (dwarf2_per_objfile->info.buffer
10019 + per_cu->cu->header.offset
10020 + per_cu->cu->header.first_die_offset);
10021 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10022 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10023 per_cu->cu->objfile->obfd,
10024 dwarf2_per_objfile->info.buffer, info_ptr,
10025 per_cu->cu);
10026 if (comp_unit_die.has_children)
10027 load_partial_dies (per_cu->cu->objfile->obfd,
10028 dwarf2_per_objfile->info.buffer, info_ptr,
10029 0, per_cu->cu);
10030 do_cleanups (back_to);
10031
10032 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10033 }
10034
10035 not_found:
10036
10037 if (pd == NULL)
10038 internal_error (__FILE__, __LINE__,
10039 _("could not find partial DIE 0x%x "
10040 "in cache [from module %s]\n"),
10041 offset, bfd_get_filename (cu->objfile->obfd));
10042 return pd;
10043 }
10044
10045 /* See if we can figure out if the class lives in a namespace. We do
10046 this by looking for a member function; its demangled name will
10047 contain namespace info, if there is any. */
10048
10049 static void
10050 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10051 struct dwarf2_cu *cu)
10052 {
10053 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10054 what template types look like, because the demangler
10055 frequently doesn't give the same name as the debug info. We
10056 could fix this by only using the demangled name to get the
10057 prefix (but see comment in read_structure_type). */
10058
10059 struct partial_die_info *real_pdi;
10060 struct partial_die_info *child_pdi;
10061
10062 /* If this DIE (this DIE's specification, if any) has a parent, then
10063 we should not do this. We'll prepend the parent's fully qualified
10064 name when we create the partial symbol. */
10065
10066 real_pdi = struct_pdi;
10067 while (real_pdi->has_specification)
10068 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10069
10070 if (real_pdi->die_parent != NULL)
10071 return;
10072
10073 for (child_pdi = struct_pdi->die_child;
10074 child_pdi != NULL;
10075 child_pdi = child_pdi->die_sibling)
10076 {
10077 if (child_pdi->tag == DW_TAG_subprogram
10078 && child_pdi->linkage_name != NULL)
10079 {
10080 char *actual_class_name
10081 = language_class_name_from_physname (cu->language_defn,
10082 child_pdi->linkage_name);
10083 if (actual_class_name != NULL)
10084 {
10085 struct_pdi->name
10086 = obsavestring (actual_class_name,
10087 strlen (actual_class_name),
10088 &cu->objfile->objfile_obstack);
10089 xfree (actual_class_name);
10090 }
10091 break;
10092 }
10093 }
10094 }
10095
10096 /* Adjust PART_DIE before generating a symbol for it. This function
10097 may set the is_external flag or change the DIE's name. */
10098
10099 static void
10100 fixup_partial_die (struct partial_die_info *part_die,
10101 struct dwarf2_cu *cu)
10102 {
10103 /* Once we've fixed up a die, there's no point in doing so again.
10104 This also avoids a memory leak if we were to call
10105 guess_partial_die_structure_name multiple times. */
10106 if (part_die->fixup_called)
10107 return;
10108
10109 /* If we found a reference attribute and the DIE has no name, try
10110 to find a name in the referred to DIE. */
10111
10112 if (part_die->name == NULL && part_die->has_specification)
10113 {
10114 struct partial_die_info *spec_die;
10115
10116 spec_die = find_partial_die (part_die->spec_offset, cu);
10117
10118 fixup_partial_die (spec_die, cu);
10119
10120 if (spec_die->name)
10121 {
10122 part_die->name = spec_die->name;
10123
10124 /* Copy DW_AT_external attribute if it is set. */
10125 if (spec_die->is_external)
10126 part_die->is_external = spec_die->is_external;
10127 }
10128 }
10129
10130 /* Set default names for some unnamed DIEs. */
10131
10132 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10133 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10134
10135 /* If there is no parent die to provide a namespace, and there are
10136 children, see if we can determine the namespace from their linkage
10137 name.
10138 NOTE: We need to do this even if cu->has_namespace_info != 0.
10139 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10140 if (cu->language == language_cplus
10141 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10142 && part_die->die_parent == NULL
10143 && part_die->has_children
10144 && (part_die->tag == DW_TAG_class_type
10145 || part_die->tag == DW_TAG_structure_type
10146 || part_die->tag == DW_TAG_union_type))
10147 guess_partial_die_structure_name (part_die, cu);
10148
10149 /* GCC might emit a nameless struct or union that has a linkage
10150 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10151 if (part_die->name == NULL
10152 && (part_die->tag == DW_TAG_class_type
10153 || part_die->tag == DW_TAG_interface_type
10154 || part_die->tag == DW_TAG_structure_type
10155 || part_die->tag == DW_TAG_union_type)
10156 && part_die->linkage_name != NULL)
10157 {
10158 char *demangled;
10159
10160 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10161 if (demangled)
10162 {
10163 const char *base;
10164
10165 /* Strip any leading namespaces/classes, keep only the base name.
10166 DW_AT_name for named DIEs does not contain the prefixes. */
10167 base = strrchr (demangled, ':');
10168 if (base && base > demangled && base[-1] == ':')
10169 base++;
10170 else
10171 base = demangled;
10172
10173 part_die->name = obsavestring (base, strlen (base),
10174 &cu->objfile->objfile_obstack);
10175 xfree (demangled);
10176 }
10177 }
10178
10179 part_die->fixup_called = 1;
10180 }
10181
10182 /* Read an attribute value described by an attribute form. */
10183
10184 static gdb_byte *
10185 read_attribute_value (struct attribute *attr, unsigned form,
10186 bfd *abfd, gdb_byte *info_ptr,
10187 struct dwarf2_cu *cu)
10188 {
10189 struct comp_unit_head *cu_header = &cu->header;
10190 unsigned int bytes_read;
10191 struct dwarf_block *blk;
10192
10193 attr->form = form;
10194 switch (form)
10195 {
10196 case DW_FORM_ref_addr:
10197 if (cu->header.version == 2)
10198 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10199 else
10200 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10201 &cu->header, &bytes_read);
10202 info_ptr += bytes_read;
10203 break;
10204 case DW_FORM_addr:
10205 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10206 info_ptr += bytes_read;
10207 break;
10208 case DW_FORM_block2:
10209 blk = dwarf_alloc_block (cu);
10210 blk->size = read_2_bytes (abfd, info_ptr);
10211 info_ptr += 2;
10212 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10213 info_ptr += blk->size;
10214 DW_BLOCK (attr) = blk;
10215 break;
10216 case DW_FORM_block4:
10217 blk = dwarf_alloc_block (cu);
10218 blk->size = read_4_bytes (abfd, info_ptr);
10219 info_ptr += 4;
10220 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10221 info_ptr += blk->size;
10222 DW_BLOCK (attr) = blk;
10223 break;
10224 case DW_FORM_data2:
10225 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10226 info_ptr += 2;
10227 break;
10228 case DW_FORM_data4:
10229 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10230 info_ptr += 4;
10231 break;
10232 case DW_FORM_data8:
10233 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10234 info_ptr += 8;
10235 break;
10236 case DW_FORM_sec_offset:
10237 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10238 info_ptr += bytes_read;
10239 break;
10240 case DW_FORM_string:
10241 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10242 DW_STRING_IS_CANONICAL (attr) = 0;
10243 info_ptr += bytes_read;
10244 break;
10245 case DW_FORM_strp:
10246 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10247 &bytes_read);
10248 DW_STRING_IS_CANONICAL (attr) = 0;
10249 info_ptr += bytes_read;
10250 break;
10251 case DW_FORM_exprloc:
10252 case DW_FORM_block:
10253 blk = dwarf_alloc_block (cu);
10254 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10255 info_ptr += bytes_read;
10256 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10257 info_ptr += blk->size;
10258 DW_BLOCK (attr) = blk;
10259 break;
10260 case DW_FORM_block1:
10261 blk = dwarf_alloc_block (cu);
10262 blk->size = read_1_byte (abfd, info_ptr);
10263 info_ptr += 1;
10264 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10265 info_ptr += blk->size;
10266 DW_BLOCK (attr) = blk;
10267 break;
10268 case DW_FORM_data1:
10269 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10270 info_ptr += 1;
10271 break;
10272 case DW_FORM_flag:
10273 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10274 info_ptr += 1;
10275 break;
10276 case DW_FORM_flag_present:
10277 DW_UNSND (attr) = 1;
10278 break;
10279 case DW_FORM_sdata:
10280 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10281 info_ptr += bytes_read;
10282 break;
10283 case DW_FORM_udata:
10284 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10285 info_ptr += bytes_read;
10286 break;
10287 case DW_FORM_ref1:
10288 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10289 info_ptr += 1;
10290 break;
10291 case DW_FORM_ref2:
10292 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10293 info_ptr += 2;
10294 break;
10295 case DW_FORM_ref4:
10296 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10297 info_ptr += 4;
10298 break;
10299 case DW_FORM_ref8:
10300 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10301 info_ptr += 8;
10302 break;
10303 case DW_FORM_ref_sig8:
10304 /* Convert the signature to something we can record in DW_UNSND
10305 for later lookup.
10306 NOTE: This is NULL if the type wasn't found. */
10307 DW_SIGNATURED_TYPE (attr) =
10308 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10309 info_ptr += 8;
10310 break;
10311 case DW_FORM_ref_udata:
10312 DW_ADDR (attr) = (cu->header.offset
10313 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10314 info_ptr += bytes_read;
10315 break;
10316 case DW_FORM_indirect:
10317 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10318 info_ptr += bytes_read;
10319 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10320 break;
10321 default:
10322 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10323 dwarf_form_name (form),
10324 bfd_get_filename (abfd));
10325 }
10326
10327 /* We have seen instances where the compiler tried to emit a byte
10328 size attribute of -1 which ended up being encoded as an unsigned
10329 0xffffffff. Although 0xffffffff is technically a valid size value,
10330 an object of this size seems pretty unlikely so we can relatively
10331 safely treat these cases as if the size attribute was invalid and
10332 treat them as zero by default. */
10333 if (attr->name == DW_AT_byte_size
10334 && form == DW_FORM_data4
10335 && DW_UNSND (attr) >= 0xffffffff)
10336 {
10337 complaint
10338 (&symfile_complaints,
10339 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10340 hex_string (DW_UNSND (attr)));
10341 DW_UNSND (attr) = 0;
10342 }
10343
10344 return info_ptr;
10345 }
10346
10347 /* Read an attribute described by an abbreviated attribute. */
10348
10349 static gdb_byte *
10350 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10351 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10352 {
10353 attr->name = abbrev->name;
10354 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10355 }
10356
10357 /* Read dwarf information from a buffer. */
10358
10359 static unsigned int
10360 read_1_byte (bfd *abfd, gdb_byte *buf)
10361 {
10362 return bfd_get_8 (abfd, buf);
10363 }
10364
10365 static int
10366 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10367 {
10368 return bfd_get_signed_8 (abfd, buf);
10369 }
10370
10371 static unsigned int
10372 read_2_bytes (bfd *abfd, gdb_byte *buf)
10373 {
10374 return bfd_get_16 (abfd, buf);
10375 }
10376
10377 static int
10378 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10379 {
10380 return bfd_get_signed_16 (abfd, buf);
10381 }
10382
10383 static unsigned int
10384 read_4_bytes (bfd *abfd, gdb_byte *buf)
10385 {
10386 return bfd_get_32 (abfd, buf);
10387 }
10388
10389 static int
10390 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10391 {
10392 return bfd_get_signed_32 (abfd, buf);
10393 }
10394
10395 static ULONGEST
10396 read_8_bytes (bfd *abfd, gdb_byte *buf)
10397 {
10398 return bfd_get_64 (abfd, buf);
10399 }
10400
10401 static CORE_ADDR
10402 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10403 unsigned int *bytes_read)
10404 {
10405 struct comp_unit_head *cu_header = &cu->header;
10406 CORE_ADDR retval = 0;
10407
10408 if (cu_header->signed_addr_p)
10409 {
10410 switch (cu_header->addr_size)
10411 {
10412 case 2:
10413 retval = bfd_get_signed_16 (abfd, buf);
10414 break;
10415 case 4:
10416 retval = bfd_get_signed_32 (abfd, buf);
10417 break;
10418 case 8:
10419 retval = bfd_get_signed_64 (abfd, buf);
10420 break;
10421 default:
10422 internal_error (__FILE__, __LINE__,
10423 _("read_address: bad switch, signed [in module %s]"),
10424 bfd_get_filename (abfd));
10425 }
10426 }
10427 else
10428 {
10429 switch (cu_header->addr_size)
10430 {
10431 case 2:
10432 retval = bfd_get_16 (abfd, buf);
10433 break;
10434 case 4:
10435 retval = bfd_get_32 (abfd, buf);
10436 break;
10437 case 8:
10438 retval = bfd_get_64 (abfd, buf);
10439 break;
10440 default:
10441 internal_error (__FILE__, __LINE__,
10442 _("read_address: bad switch, "
10443 "unsigned [in module %s]"),
10444 bfd_get_filename (abfd));
10445 }
10446 }
10447
10448 *bytes_read = cu_header->addr_size;
10449 return retval;
10450 }
10451
10452 /* Read the initial length from a section. The (draft) DWARF 3
10453 specification allows the initial length to take up either 4 bytes
10454 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10455 bytes describe the length and all offsets will be 8 bytes in length
10456 instead of 4.
10457
10458 An older, non-standard 64-bit format is also handled by this
10459 function. The older format in question stores the initial length
10460 as an 8-byte quantity without an escape value. Lengths greater
10461 than 2^32 aren't very common which means that the initial 4 bytes
10462 is almost always zero. Since a length value of zero doesn't make
10463 sense for the 32-bit format, this initial zero can be considered to
10464 be an escape value which indicates the presence of the older 64-bit
10465 format. As written, the code can't detect (old format) lengths
10466 greater than 4GB. If it becomes necessary to handle lengths
10467 somewhat larger than 4GB, we could allow other small values (such
10468 as the non-sensical values of 1, 2, and 3) to also be used as
10469 escape values indicating the presence of the old format.
10470
10471 The value returned via bytes_read should be used to increment the
10472 relevant pointer after calling read_initial_length().
10473
10474 [ Note: read_initial_length() and read_offset() are based on the
10475 document entitled "DWARF Debugging Information Format", revision
10476 3, draft 8, dated November 19, 2001. This document was obtained
10477 from:
10478
10479 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10480
10481 This document is only a draft and is subject to change. (So beware.)
10482
10483 Details regarding the older, non-standard 64-bit format were
10484 determined empirically by examining 64-bit ELF files produced by
10485 the SGI toolchain on an IRIX 6.5 machine.
10486
10487 - Kevin, July 16, 2002
10488 ] */
10489
10490 static LONGEST
10491 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10492 {
10493 LONGEST length = bfd_get_32 (abfd, buf);
10494
10495 if (length == 0xffffffff)
10496 {
10497 length = bfd_get_64 (abfd, buf + 4);
10498 *bytes_read = 12;
10499 }
10500 else if (length == 0)
10501 {
10502 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10503 length = bfd_get_64 (abfd, buf);
10504 *bytes_read = 8;
10505 }
10506 else
10507 {
10508 *bytes_read = 4;
10509 }
10510
10511 return length;
10512 }
10513
10514 /* Cover function for read_initial_length.
10515 Returns the length of the object at BUF, and stores the size of the
10516 initial length in *BYTES_READ and stores the size that offsets will be in
10517 *OFFSET_SIZE.
10518 If the initial length size is not equivalent to that specified in
10519 CU_HEADER then issue a complaint.
10520 This is useful when reading non-comp-unit headers. */
10521
10522 static LONGEST
10523 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10524 const struct comp_unit_head *cu_header,
10525 unsigned int *bytes_read,
10526 unsigned int *offset_size)
10527 {
10528 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10529
10530 gdb_assert (cu_header->initial_length_size == 4
10531 || cu_header->initial_length_size == 8
10532 || cu_header->initial_length_size == 12);
10533
10534 if (cu_header->initial_length_size != *bytes_read)
10535 complaint (&symfile_complaints,
10536 _("intermixed 32-bit and 64-bit DWARF sections"));
10537
10538 *offset_size = (*bytes_read == 4) ? 4 : 8;
10539 return length;
10540 }
10541
10542 /* Read an offset from the data stream. The size of the offset is
10543 given by cu_header->offset_size. */
10544
10545 static LONGEST
10546 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10547 unsigned int *bytes_read)
10548 {
10549 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10550
10551 *bytes_read = cu_header->offset_size;
10552 return offset;
10553 }
10554
10555 /* Read an offset from the data stream. */
10556
10557 static LONGEST
10558 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10559 {
10560 LONGEST retval = 0;
10561
10562 switch (offset_size)
10563 {
10564 case 4:
10565 retval = bfd_get_32 (abfd, buf);
10566 break;
10567 case 8:
10568 retval = bfd_get_64 (abfd, buf);
10569 break;
10570 default:
10571 internal_error (__FILE__, __LINE__,
10572 _("read_offset_1: bad switch [in module %s]"),
10573 bfd_get_filename (abfd));
10574 }
10575
10576 return retval;
10577 }
10578
10579 static gdb_byte *
10580 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10581 {
10582 /* If the size of a host char is 8 bits, we can return a pointer
10583 to the buffer, otherwise we have to copy the data to a buffer
10584 allocated on the temporary obstack. */
10585 gdb_assert (HOST_CHAR_BIT == 8);
10586 return buf;
10587 }
10588
10589 static char *
10590 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10591 {
10592 /* If the size of a host char is 8 bits, we can return a pointer
10593 to the string, otherwise we have to copy the string to a buffer
10594 allocated on the temporary obstack. */
10595 gdb_assert (HOST_CHAR_BIT == 8);
10596 if (*buf == '\0')
10597 {
10598 *bytes_read_ptr = 1;
10599 return NULL;
10600 }
10601 *bytes_read_ptr = strlen ((char *) buf) + 1;
10602 return (char *) buf;
10603 }
10604
10605 static char *
10606 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10607 {
10608 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10609 if (dwarf2_per_objfile->str.buffer == NULL)
10610 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10611 bfd_get_filename (abfd));
10612 if (str_offset >= dwarf2_per_objfile->str.size)
10613 error (_("DW_FORM_strp pointing outside of "
10614 ".debug_str section [in module %s]"),
10615 bfd_get_filename (abfd));
10616 gdb_assert (HOST_CHAR_BIT == 8);
10617 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10618 return NULL;
10619 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10620 }
10621
10622 static char *
10623 read_indirect_string (bfd *abfd, gdb_byte *buf,
10624 const struct comp_unit_head *cu_header,
10625 unsigned int *bytes_read_ptr)
10626 {
10627 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10628
10629 return read_indirect_string_at_offset (abfd, str_offset);
10630 }
10631
10632 static unsigned long
10633 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10634 {
10635 unsigned long result;
10636 unsigned int num_read;
10637 int i, shift;
10638 unsigned char byte;
10639
10640 result = 0;
10641 shift = 0;
10642 num_read = 0;
10643 i = 0;
10644 while (1)
10645 {
10646 byte = bfd_get_8 (abfd, buf);
10647 buf++;
10648 num_read++;
10649 result |= ((unsigned long)(byte & 127) << shift);
10650 if ((byte & 128) == 0)
10651 {
10652 break;
10653 }
10654 shift += 7;
10655 }
10656 *bytes_read_ptr = num_read;
10657 return result;
10658 }
10659
10660 static long
10661 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10662 {
10663 long result;
10664 int i, shift, num_read;
10665 unsigned char byte;
10666
10667 result = 0;
10668 shift = 0;
10669 num_read = 0;
10670 i = 0;
10671 while (1)
10672 {
10673 byte = bfd_get_8 (abfd, buf);
10674 buf++;
10675 num_read++;
10676 result |= ((long)(byte & 127) << shift);
10677 shift += 7;
10678 if ((byte & 128) == 0)
10679 {
10680 break;
10681 }
10682 }
10683 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10684 result |= -(((long)1) << shift);
10685 *bytes_read_ptr = num_read;
10686 return result;
10687 }
10688
10689 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10690
10691 static gdb_byte *
10692 skip_leb128 (bfd *abfd, gdb_byte *buf)
10693 {
10694 int byte;
10695
10696 while (1)
10697 {
10698 byte = bfd_get_8 (abfd, buf);
10699 buf++;
10700 if ((byte & 128) == 0)
10701 return buf;
10702 }
10703 }
10704
10705 static void
10706 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10707 {
10708 switch (lang)
10709 {
10710 case DW_LANG_C89:
10711 case DW_LANG_C99:
10712 case DW_LANG_C:
10713 cu->language = language_c;
10714 break;
10715 case DW_LANG_C_plus_plus:
10716 cu->language = language_cplus;
10717 break;
10718 case DW_LANG_D:
10719 cu->language = language_d;
10720 break;
10721 case DW_LANG_Fortran77:
10722 case DW_LANG_Fortran90:
10723 case DW_LANG_Fortran95:
10724 cu->language = language_fortran;
10725 break;
10726 case DW_LANG_Mips_Assembler:
10727 cu->language = language_asm;
10728 break;
10729 case DW_LANG_Java:
10730 cu->language = language_java;
10731 break;
10732 case DW_LANG_Ada83:
10733 case DW_LANG_Ada95:
10734 cu->language = language_ada;
10735 break;
10736 case DW_LANG_Modula2:
10737 cu->language = language_m2;
10738 break;
10739 case DW_LANG_Pascal83:
10740 cu->language = language_pascal;
10741 break;
10742 case DW_LANG_ObjC:
10743 cu->language = language_objc;
10744 break;
10745 case DW_LANG_Cobol74:
10746 case DW_LANG_Cobol85:
10747 default:
10748 cu->language = language_minimal;
10749 break;
10750 }
10751 cu->language_defn = language_def (cu->language);
10752 }
10753
10754 /* Return the named attribute or NULL if not there. */
10755
10756 static struct attribute *
10757 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10758 {
10759 unsigned int i;
10760 struct attribute *spec = NULL;
10761
10762 for (i = 0; i < die->num_attrs; ++i)
10763 {
10764 if (die->attrs[i].name == name)
10765 return &die->attrs[i];
10766 if (die->attrs[i].name == DW_AT_specification
10767 || die->attrs[i].name == DW_AT_abstract_origin)
10768 spec = &die->attrs[i];
10769 }
10770
10771 if (spec)
10772 {
10773 die = follow_die_ref (die, spec, &cu);
10774 return dwarf2_attr (die, name, cu);
10775 }
10776
10777 return NULL;
10778 }
10779
10780 /* Return the named attribute or NULL if not there,
10781 but do not follow DW_AT_specification, etc.
10782 This is for use in contexts where we're reading .debug_types dies.
10783 Following DW_AT_specification, DW_AT_abstract_origin will take us
10784 back up the chain, and we want to go down. */
10785
10786 static struct attribute *
10787 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10788 struct dwarf2_cu *cu)
10789 {
10790 unsigned int i;
10791
10792 for (i = 0; i < die->num_attrs; ++i)
10793 if (die->attrs[i].name == name)
10794 return &die->attrs[i];
10795
10796 return NULL;
10797 }
10798
10799 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10800 and holds a non-zero value. This function should only be used for
10801 DW_FORM_flag or DW_FORM_flag_present attributes. */
10802
10803 static int
10804 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10805 {
10806 struct attribute *attr = dwarf2_attr (die, name, cu);
10807
10808 return (attr && DW_UNSND (attr));
10809 }
10810
10811 static int
10812 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10813 {
10814 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10815 which value is non-zero. However, we have to be careful with
10816 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10817 (via dwarf2_flag_true_p) follows this attribute. So we may
10818 end up accidently finding a declaration attribute that belongs
10819 to a different DIE referenced by the specification attribute,
10820 even though the given DIE does not have a declaration attribute. */
10821 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10822 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10823 }
10824
10825 /* Return the die giving the specification for DIE, if there is
10826 one. *SPEC_CU is the CU containing DIE on input, and the CU
10827 containing the return value on output. If there is no
10828 specification, but there is an abstract origin, that is
10829 returned. */
10830
10831 static struct die_info *
10832 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10833 {
10834 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10835 *spec_cu);
10836
10837 if (spec_attr == NULL)
10838 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10839
10840 if (spec_attr == NULL)
10841 return NULL;
10842 else
10843 return follow_die_ref (die, spec_attr, spec_cu);
10844 }
10845
10846 /* Free the line_header structure *LH, and any arrays and strings it
10847 refers to.
10848 NOTE: This is also used as a "cleanup" function. */
10849
10850 static void
10851 free_line_header (struct line_header *lh)
10852 {
10853 if (lh->standard_opcode_lengths)
10854 xfree (lh->standard_opcode_lengths);
10855
10856 /* Remember that all the lh->file_names[i].name pointers are
10857 pointers into debug_line_buffer, and don't need to be freed. */
10858 if (lh->file_names)
10859 xfree (lh->file_names);
10860
10861 /* Similarly for the include directory names. */
10862 if (lh->include_dirs)
10863 xfree (lh->include_dirs);
10864
10865 xfree (lh);
10866 }
10867
10868 /* Add an entry to LH's include directory table. */
10869
10870 static void
10871 add_include_dir (struct line_header *lh, char *include_dir)
10872 {
10873 /* Grow the array if necessary. */
10874 if (lh->include_dirs_size == 0)
10875 {
10876 lh->include_dirs_size = 1; /* for testing */
10877 lh->include_dirs = xmalloc (lh->include_dirs_size
10878 * sizeof (*lh->include_dirs));
10879 }
10880 else if (lh->num_include_dirs >= lh->include_dirs_size)
10881 {
10882 lh->include_dirs_size *= 2;
10883 lh->include_dirs = xrealloc (lh->include_dirs,
10884 (lh->include_dirs_size
10885 * sizeof (*lh->include_dirs)));
10886 }
10887
10888 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10889 }
10890
10891 /* Add an entry to LH's file name table. */
10892
10893 static void
10894 add_file_name (struct line_header *lh,
10895 char *name,
10896 unsigned int dir_index,
10897 unsigned int mod_time,
10898 unsigned int length)
10899 {
10900 struct file_entry *fe;
10901
10902 /* Grow the array if necessary. */
10903 if (lh->file_names_size == 0)
10904 {
10905 lh->file_names_size = 1; /* for testing */
10906 lh->file_names = xmalloc (lh->file_names_size
10907 * sizeof (*lh->file_names));
10908 }
10909 else if (lh->num_file_names >= lh->file_names_size)
10910 {
10911 lh->file_names_size *= 2;
10912 lh->file_names = xrealloc (lh->file_names,
10913 (lh->file_names_size
10914 * sizeof (*lh->file_names)));
10915 }
10916
10917 fe = &lh->file_names[lh->num_file_names++];
10918 fe->name = name;
10919 fe->dir_index = dir_index;
10920 fe->mod_time = mod_time;
10921 fe->length = length;
10922 fe->included_p = 0;
10923 fe->symtab = NULL;
10924 }
10925
10926 /* Read the statement program header starting at OFFSET in
10927 .debug_line, according to the endianness of ABFD. Return a pointer
10928 to a struct line_header, allocated using xmalloc.
10929
10930 NOTE: the strings in the include directory and file name tables of
10931 the returned object point into debug_line_buffer, and must not be
10932 freed. */
10933
10934 static struct line_header *
10935 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10936 struct dwarf2_cu *cu)
10937 {
10938 struct cleanup *back_to;
10939 struct line_header *lh;
10940 gdb_byte *line_ptr;
10941 unsigned int bytes_read, offset_size;
10942 int i;
10943 char *cur_dir, *cur_file;
10944
10945 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10946 if (dwarf2_per_objfile->line.buffer == NULL)
10947 {
10948 complaint (&symfile_complaints, _("missing .debug_line section"));
10949 return 0;
10950 }
10951
10952 /* Make sure that at least there's room for the total_length field.
10953 That could be 12 bytes long, but we're just going to fudge that. */
10954 if (offset + 4 >= dwarf2_per_objfile->line.size)
10955 {
10956 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10957 return 0;
10958 }
10959
10960 lh = xmalloc (sizeof (*lh));
10961 memset (lh, 0, sizeof (*lh));
10962 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10963 (void *) lh);
10964
10965 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10966
10967 /* Read in the header. */
10968 lh->total_length =
10969 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10970 &bytes_read, &offset_size);
10971 line_ptr += bytes_read;
10972 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10973 + dwarf2_per_objfile->line.size))
10974 {
10975 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10976 return 0;
10977 }
10978 lh->statement_program_end = line_ptr + lh->total_length;
10979 lh->version = read_2_bytes (abfd, line_ptr);
10980 line_ptr += 2;
10981 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10982 line_ptr += offset_size;
10983 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10984 line_ptr += 1;
10985 if (lh->version >= 4)
10986 {
10987 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10988 line_ptr += 1;
10989 }
10990 else
10991 lh->maximum_ops_per_instruction = 1;
10992
10993 if (lh->maximum_ops_per_instruction == 0)
10994 {
10995 lh->maximum_ops_per_instruction = 1;
10996 complaint (&symfile_complaints,
10997 _("invalid maximum_ops_per_instruction "
10998 "in `.debug_line' section"));
10999 }
11000
11001 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11002 line_ptr += 1;
11003 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11004 line_ptr += 1;
11005 lh->line_range = read_1_byte (abfd, line_ptr);
11006 line_ptr += 1;
11007 lh->opcode_base = read_1_byte (abfd, line_ptr);
11008 line_ptr += 1;
11009 lh->standard_opcode_lengths
11010 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11011
11012 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11013 for (i = 1; i < lh->opcode_base; ++i)
11014 {
11015 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11016 line_ptr += 1;
11017 }
11018
11019 /* Read directory table. */
11020 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11021 {
11022 line_ptr += bytes_read;
11023 add_include_dir (lh, cur_dir);
11024 }
11025 line_ptr += bytes_read;
11026
11027 /* Read file name table. */
11028 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11029 {
11030 unsigned int dir_index, mod_time, length;
11031
11032 line_ptr += bytes_read;
11033 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11034 line_ptr += bytes_read;
11035 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11036 line_ptr += bytes_read;
11037 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11038 line_ptr += bytes_read;
11039
11040 add_file_name (lh, cur_file, dir_index, mod_time, length);
11041 }
11042 line_ptr += bytes_read;
11043 lh->statement_program_start = line_ptr;
11044
11045 if (line_ptr > (dwarf2_per_objfile->line.buffer
11046 + dwarf2_per_objfile->line.size))
11047 complaint (&symfile_complaints,
11048 _("line number info header doesn't "
11049 "fit in `.debug_line' section"));
11050
11051 discard_cleanups (back_to);
11052 return lh;
11053 }
11054
11055 /* This function exists to work around a bug in certain compilers
11056 (particularly GCC 2.95), in which the first line number marker of a
11057 function does not show up until after the prologue, right before
11058 the second line number marker. This function shifts ADDRESS down
11059 to the beginning of the function if necessary, and is called on
11060 addresses passed to record_line. */
11061
11062 static CORE_ADDR
11063 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
11064 {
11065 struct function_range *fn;
11066
11067 /* Find the function_range containing address. */
11068 if (!cu->first_fn)
11069 return address;
11070
11071 if (!cu->cached_fn)
11072 cu->cached_fn = cu->first_fn;
11073
11074 fn = cu->cached_fn;
11075 while (fn)
11076 if (fn->lowpc <= address && fn->highpc > address)
11077 goto found;
11078 else
11079 fn = fn->next;
11080
11081 fn = cu->first_fn;
11082 while (fn && fn != cu->cached_fn)
11083 if (fn->lowpc <= address && fn->highpc > address)
11084 goto found;
11085 else
11086 fn = fn->next;
11087
11088 return address;
11089
11090 found:
11091 if (fn->seen_line)
11092 return address;
11093 if (address != fn->lowpc)
11094 complaint (&symfile_complaints,
11095 _("misplaced first line number at 0x%lx for '%s'"),
11096 (unsigned long) address, fn->name);
11097 fn->seen_line = 1;
11098 return fn->lowpc;
11099 }
11100
11101 /* Subroutine of dwarf_decode_lines to simplify it.
11102 Return the file name of the psymtab for included file FILE_INDEX
11103 in line header LH of PST.
11104 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11105 If space for the result is malloc'd, it will be freed by a cleanup.
11106 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11107
11108 static char *
11109 psymtab_include_file_name (const struct line_header *lh, int file_index,
11110 const struct partial_symtab *pst,
11111 const char *comp_dir)
11112 {
11113 const struct file_entry fe = lh->file_names [file_index];
11114 char *include_name = fe.name;
11115 char *include_name_to_compare = include_name;
11116 char *dir_name = NULL;
11117 const char *pst_filename;
11118 char *copied_name = NULL;
11119 int file_is_pst;
11120
11121 if (fe.dir_index)
11122 dir_name = lh->include_dirs[fe.dir_index - 1];
11123
11124 if (!IS_ABSOLUTE_PATH (include_name)
11125 && (dir_name != NULL || comp_dir != NULL))
11126 {
11127 /* Avoid creating a duplicate psymtab for PST.
11128 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11129 Before we do the comparison, however, we need to account
11130 for DIR_NAME and COMP_DIR.
11131 First prepend dir_name (if non-NULL). If we still don't
11132 have an absolute path prepend comp_dir (if non-NULL).
11133 However, the directory we record in the include-file's
11134 psymtab does not contain COMP_DIR (to match the
11135 corresponding symtab(s)).
11136
11137 Example:
11138
11139 bash$ cd /tmp
11140 bash$ gcc -g ./hello.c
11141 include_name = "hello.c"
11142 dir_name = "."
11143 DW_AT_comp_dir = comp_dir = "/tmp"
11144 DW_AT_name = "./hello.c" */
11145
11146 if (dir_name != NULL)
11147 {
11148 include_name = concat (dir_name, SLASH_STRING,
11149 include_name, (char *)NULL);
11150 include_name_to_compare = include_name;
11151 make_cleanup (xfree, include_name);
11152 }
11153 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11154 {
11155 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11156 include_name, (char *)NULL);
11157 }
11158 }
11159
11160 pst_filename = pst->filename;
11161 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11162 {
11163 copied_name = concat (pst->dirname, SLASH_STRING,
11164 pst_filename, (char *)NULL);
11165 pst_filename = copied_name;
11166 }
11167
11168 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11169
11170 if (include_name_to_compare != include_name)
11171 xfree (include_name_to_compare);
11172 if (copied_name != NULL)
11173 xfree (copied_name);
11174
11175 if (file_is_pst)
11176 return NULL;
11177 return include_name;
11178 }
11179
11180 /* Ignore this record_line request. */
11181
11182 static void
11183 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11184 {
11185 return;
11186 }
11187
11188 /* Decode the Line Number Program (LNP) for the given line_header
11189 structure and CU. The actual information extracted and the type
11190 of structures created from the LNP depends on the value of PST.
11191
11192 1. If PST is NULL, then this procedure uses the data from the program
11193 to create all necessary symbol tables, and their linetables.
11194
11195 2. If PST is not NULL, this procedure reads the program to determine
11196 the list of files included by the unit represented by PST, and
11197 builds all the associated partial symbol tables.
11198
11199 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11200 It is used for relative paths in the line table.
11201 NOTE: When processing partial symtabs (pst != NULL),
11202 comp_dir == pst->dirname.
11203
11204 NOTE: It is important that psymtabs have the same file name (via strcmp)
11205 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11206 symtab we don't use it in the name of the psymtabs we create.
11207 E.g. expand_line_sal requires this when finding psymtabs to expand.
11208 A good testcase for this is mb-inline.exp. */
11209
11210 static void
11211 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11212 struct dwarf2_cu *cu, struct partial_symtab *pst)
11213 {
11214 gdb_byte *line_ptr, *extended_end;
11215 gdb_byte *line_end;
11216 unsigned int bytes_read, extended_len;
11217 unsigned char op_code, extended_op, adj_opcode;
11218 CORE_ADDR baseaddr;
11219 struct objfile *objfile = cu->objfile;
11220 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11221 const int decode_for_pst_p = (pst != NULL);
11222 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11223 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11224 = record_line;
11225
11226 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11227
11228 line_ptr = lh->statement_program_start;
11229 line_end = lh->statement_program_end;
11230
11231 /* Read the statement sequences until there's nothing left. */
11232 while (line_ptr < line_end)
11233 {
11234 /* state machine registers */
11235 CORE_ADDR address = 0;
11236 unsigned int file = 1;
11237 unsigned int line = 1;
11238 unsigned int column = 0;
11239 int is_stmt = lh->default_is_stmt;
11240 int basic_block = 0;
11241 int end_sequence = 0;
11242 CORE_ADDR addr;
11243 unsigned char op_index = 0;
11244
11245 if (!decode_for_pst_p && lh->num_file_names >= file)
11246 {
11247 /* Start a subfile for the current file of the state machine. */
11248 /* lh->include_dirs and lh->file_names are 0-based, but the
11249 directory and file name numbers in the statement program
11250 are 1-based. */
11251 struct file_entry *fe = &lh->file_names[file - 1];
11252 char *dir = NULL;
11253
11254 if (fe->dir_index)
11255 dir = lh->include_dirs[fe->dir_index - 1];
11256
11257 dwarf2_start_subfile (fe->name, dir, comp_dir);
11258 }
11259
11260 /* Decode the table. */
11261 while (!end_sequence)
11262 {
11263 op_code = read_1_byte (abfd, line_ptr);
11264 line_ptr += 1;
11265 if (line_ptr > line_end)
11266 {
11267 dwarf2_debug_line_missing_end_sequence_complaint ();
11268 break;
11269 }
11270
11271 if (op_code >= lh->opcode_base)
11272 {
11273 /* Special operand. */
11274 adj_opcode = op_code - lh->opcode_base;
11275 address += (((op_index + (adj_opcode / lh->line_range))
11276 / lh->maximum_ops_per_instruction)
11277 * lh->minimum_instruction_length);
11278 op_index = ((op_index + (adj_opcode / lh->line_range))
11279 % lh->maximum_ops_per_instruction);
11280 line += lh->line_base + (adj_opcode % lh->line_range);
11281 if (lh->num_file_names < file || file == 0)
11282 dwarf2_debug_line_missing_file_complaint ();
11283 /* For now we ignore lines not starting on an
11284 instruction boundary. */
11285 else if (op_index == 0)
11286 {
11287 lh->file_names[file - 1].included_p = 1;
11288 if (!decode_for_pst_p && is_stmt)
11289 {
11290 if (last_subfile != current_subfile)
11291 {
11292 addr = gdbarch_addr_bits_remove (gdbarch, address);
11293 if (last_subfile)
11294 (*p_record_line) (last_subfile, 0, addr);
11295 last_subfile = current_subfile;
11296 }
11297 /* Append row to matrix using current values. */
11298 addr = check_cu_functions (address, cu);
11299 addr = gdbarch_addr_bits_remove (gdbarch, addr);
11300 (*p_record_line) (current_subfile, line, addr);
11301 }
11302 }
11303 basic_block = 0;
11304 }
11305 else switch (op_code)
11306 {
11307 case DW_LNS_extended_op:
11308 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11309 &bytes_read);
11310 line_ptr += bytes_read;
11311 extended_end = line_ptr + extended_len;
11312 extended_op = read_1_byte (abfd, line_ptr);
11313 line_ptr += 1;
11314 switch (extended_op)
11315 {
11316 case DW_LNE_end_sequence:
11317 p_record_line = record_line;
11318 end_sequence = 1;
11319 break;
11320 case DW_LNE_set_address:
11321 address = read_address (abfd, line_ptr, cu, &bytes_read);
11322
11323 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11324 {
11325 /* This line table is for a function which has been
11326 GCd by the linker. Ignore it. PR gdb/12528 */
11327
11328 long line_offset
11329 = line_ptr - dwarf2_per_objfile->line.buffer;
11330
11331 complaint (&symfile_complaints,
11332 _(".debug_line address at offset 0x%lx is 0 "
11333 "[in module %s]"),
11334 line_offset, cu->objfile->name);
11335 p_record_line = noop_record_line;
11336 }
11337
11338 op_index = 0;
11339 line_ptr += bytes_read;
11340 address += baseaddr;
11341 break;
11342 case DW_LNE_define_file:
11343 {
11344 char *cur_file;
11345 unsigned int dir_index, mod_time, length;
11346
11347 cur_file = read_direct_string (abfd, line_ptr,
11348 &bytes_read);
11349 line_ptr += bytes_read;
11350 dir_index =
11351 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11352 line_ptr += bytes_read;
11353 mod_time =
11354 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11355 line_ptr += bytes_read;
11356 length =
11357 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11358 line_ptr += bytes_read;
11359 add_file_name (lh, cur_file, dir_index, mod_time, length);
11360 }
11361 break;
11362 case DW_LNE_set_discriminator:
11363 /* The discriminator is not interesting to the debugger;
11364 just ignore it. */
11365 line_ptr = extended_end;
11366 break;
11367 default:
11368 complaint (&symfile_complaints,
11369 _("mangled .debug_line section"));
11370 return;
11371 }
11372 /* Make sure that we parsed the extended op correctly. If e.g.
11373 we expected a different address size than the producer used,
11374 we may have read the wrong number of bytes. */
11375 if (line_ptr != extended_end)
11376 {
11377 complaint (&symfile_complaints,
11378 _("mangled .debug_line section"));
11379 return;
11380 }
11381 break;
11382 case DW_LNS_copy:
11383 if (lh->num_file_names < file || file == 0)
11384 dwarf2_debug_line_missing_file_complaint ();
11385 else
11386 {
11387 lh->file_names[file - 1].included_p = 1;
11388 if (!decode_for_pst_p && is_stmt)
11389 {
11390 if (last_subfile != current_subfile)
11391 {
11392 addr = gdbarch_addr_bits_remove (gdbarch, address);
11393 if (last_subfile)
11394 (*p_record_line) (last_subfile, 0, addr);
11395 last_subfile = current_subfile;
11396 }
11397 addr = check_cu_functions (address, cu);
11398 addr = gdbarch_addr_bits_remove (gdbarch, addr);
11399 (*p_record_line) (current_subfile, line, addr);
11400 }
11401 }
11402 basic_block = 0;
11403 break;
11404 case DW_LNS_advance_pc:
11405 {
11406 CORE_ADDR adjust
11407 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11408
11409 address += (((op_index + adjust)
11410 / lh->maximum_ops_per_instruction)
11411 * lh->minimum_instruction_length);
11412 op_index = ((op_index + adjust)
11413 % lh->maximum_ops_per_instruction);
11414 line_ptr += bytes_read;
11415 }
11416 break;
11417 case DW_LNS_advance_line:
11418 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11419 line_ptr += bytes_read;
11420 break;
11421 case DW_LNS_set_file:
11422 {
11423 /* The arrays lh->include_dirs and lh->file_names are
11424 0-based, but the directory and file name numbers in
11425 the statement program are 1-based. */
11426 struct file_entry *fe;
11427 char *dir = NULL;
11428
11429 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11430 line_ptr += bytes_read;
11431 if (lh->num_file_names < file || file == 0)
11432 dwarf2_debug_line_missing_file_complaint ();
11433 else
11434 {
11435 fe = &lh->file_names[file - 1];
11436 if (fe->dir_index)
11437 dir = lh->include_dirs[fe->dir_index - 1];
11438 if (!decode_for_pst_p)
11439 {
11440 last_subfile = current_subfile;
11441 dwarf2_start_subfile (fe->name, dir, comp_dir);
11442 }
11443 }
11444 }
11445 break;
11446 case DW_LNS_set_column:
11447 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11448 line_ptr += bytes_read;
11449 break;
11450 case DW_LNS_negate_stmt:
11451 is_stmt = (!is_stmt);
11452 break;
11453 case DW_LNS_set_basic_block:
11454 basic_block = 1;
11455 break;
11456 /* Add to the address register of the state machine the
11457 address increment value corresponding to special opcode
11458 255. I.e., this value is scaled by the minimum
11459 instruction length since special opcode 255 would have
11460 scaled the increment. */
11461 case DW_LNS_const_add_pc:
11462 {
11463 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11464
11465 address += (((op_index + adjust)
11466 / lh->maximum_ops_per_instruction)
11467 * lh->minimum_instruction_length);
11468 op_index = ((op_index + adjust)
11469 % lh->maximum_ops_per_instruction);
11470 }
11471 break;
11472 case DW_LNS_fixed_advance_pc:
11473 address += read_2_bytes (abfd, line_ptr);
11474 op_index = 0;
11475 line_ptr += 2;
11476 break;
11477 default:
11478 {
11479 /* Unknown standard opcode, ignore it. */
11480 int i;
11481
11482 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11483 {
11484 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11485 line_ptr += bytes_read;
11486 }
11487 }
11488 }
11489 }
11490 if (lh->num_file_names < file || file == 0)
11491 dwarf2_debug_line_missing_file_complaint ();
11492 else
11493 {
11494 lh->file_names[file - 1].included_p = 1;
11495 if (!decode_for_pst_p)
11496 {
11497 addr = gdbarch_addr_bits_remove (gdbarch, address);
11498 (*p_record_line) (current_subfile, 0, addr);
11499 }
11500 }
11501 }
11502
11503 if (decode_for_pst_p)
11504 {
11505 int file_index;
11506
11507 /* Now that we're done scanning the Line Header Program, we can
11508 create the psymtab of each included file. */
11509 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11510 if (lh->file_names[file_index].included_p == 1)
11511 {
11512 char *include_name =
11513 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11514 if (include_name != NULL)
11515 dwarf2_create_include_psymtab (include_name, pst, objfile);
11516 }
11517 }
11518 else
11519 {
11520 /* Make sure a symtab is created for every file, even files
11521 which contain only variables (i.e. no code with associated
11522 line numbers). */
11523
11524 int i;
11525 struct file_entry *fe;
11526
11527 for (i = 0; i < lh->num_file_names; i++)
11528 {
11529 char *dir = NULL;
11530
11531 fe = &lh->file_names[i];
11532 if (fe->dir_index)
11533 dir = lh->include_dirs[fe->dir_index - 1];
11534 dwarf2_start_subfile (fe->name, dir, comp_dir);
11535
11536 /* Skip the main file; we don't need it, and it must be
11537 allocated last, so that it will show up before the
11538 non-primary symtabs in the objfile's symtab list. */
11539 if (current_subfile == first_subfile)
11540 continue;
11541
11542 if (current_subfile->symtab == NULL)
11543 current_subfile->symtab = allocate_symtab (current_subfile->name,
11544 cu->objfile);
11545 fe->symtab = current_subfile->symtab;
11546 }
11547 }
11548 }
11549
11550 /* Start a subfile for DWARF. FILENAME is the name of the file and
11551 DIRNAME the name of the source directory which contains FILENAME
11552 or NULL if not known. COMP_DIR is the compilation directory for the
11553 linetable's compilation unit or NULL if not known.
11554 This routine tries to keep line numbers from identical absolute and
11555 relative file names in a common subfile.
11556
11557 Using the `list' example from the GDB testsuite, which resides in
11558 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11559 of /srcdir/list0.c yields the following debugging information for list0.c:
11560
11561 DW_AT_name: /srcdir/list0.c
11562 DW_AT_comp_dir: /compdir
11563 files.files[0].name: list0.h
11564 files.files[0].dir: /srcdir
11565 files.files[1].name: list0.c
11566 files.files[1].dir: /srcdir
11567
11568 The line number information for list0.c has to end up in a single
11569 subfile, so that `break /srcdir/list0.c:1' works as expected.
11570 start_subfile will ensure that this happens provided that we pass the
11571 concatenation of files.files[1].dir and files.files[1].name as the
11572 subfile's name. */
11573
11574 static void
11575 dwarf2_start_subfile (char *filename, const char *dirname,
11576 const char *comp_dir)
11577 {
11578 char *fullname;
11579
11580 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11581 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11582 second argument to start_subfile. To be consistent, we do the
11583 same here. In order not to lose the line information directory,
11584 we concatenate it to the filename when it makes sense.
11585 Note that the Dwarf3 standard says (speaking of filenames in line
11586 information): ``The directory index is ignored for file names
11587 that represent full path names''. Thus ignoring dirname in the
11588 `else' branch below isn't an issue. */
11589
11590 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11591 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11592 else
11593 fullname = filename;
11594
11595 start_subfile (fullname, comp_dir);
11596
11597 if (fullname != filename)
11598 xfree (fullname);
11599 }
11600
11601 static void
11602 var_decode_location (struct attribute *attr, struct symbol *sym,
11603 struct dwarf2_cu *cu)
11604 {
11605 struct objfile *objfile = cu->objfile;
11606 struct comp_unit_head *cu_header = &cu->header;
11607
11608 /* NOTE drow/2003-01-30: There used to be a comment and some special
11609 code here to turn a symbol with DW_AT_external and a
11610 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11611 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11612 with some versions of binutils) where shared libraries could have
11613 relocations against symbols in their debug information - the
11614 minimal symbol would have the right address, but the debug info
11615 would not. It's no longer necessary, because we will explicitly
11616 apply relocations when we read in the debug information now. */
11617
11618 /* A DW_AT_location attribute with no contents indicates that a
11619 variable has been optimized away. */
11620 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11621 {
11622 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11623 return;
11624 }
11625
11626 /* Handle one degenerate form of location expression specially, to
11627 preserve GDB's previous behavior when section offsets are
11628 specified. If this is just a DW_OP_addr then mark this symbol
11629 as LOC_STATIC. */
11630
11631 if (attr_form_is_block (attr)
11632 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11633 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11634 {
11635 unsigned int dummy;
11636
11637 SYMBOL_VALUE_ADDRESS (sym) =
11638 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11639 SYMBOL_CLASS (sym) = LOC_STATIC;
11640 fixup_symbol_section (sym, objfile);
11641 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11642 SYMBOL_SECTION (sym));
11643 return;
11644 }
11645
11646 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11647 expression evaluator, and use LOC_COMPUTED only when necessary
11648 (i.e. when the value of a register or memory location is
11649 referenced, or a thread-local block, etc.). Then again, it might
11650 not be worthwhile. I'm assuming that it isn't unless performance
11651 or memory numbers show me otherwise. */
11652
11653 dwarf2_symbol_mark_computed (attr, sym, cu);
11654 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11655
11656 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11657 cu->has_loclist = 1;
11658 }
11659
11660 /* Given a pointer to a DWARF information entry, figure out if we need
11661 to make a symbol table entry for it, and if so, create a new entry
11662 and return a pointer to it.
11663 If TYPE is NULL, determine symbol type from the die, otherwise
11664 used the passed type.
11665 If SPACE is not NULL, use it to hold the new symbol. If it is
11666 NULL, allocate a new symbol on the objfile's obstack. */
11667
11668 static struct symbol *
11669 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11670 struct symbol *space)
11671 {
11672 struct objfile *objfile = cu->objfile;
11673 struct symbol *sym = NULL;
11674 char *name;
11675 struct attribute *attr = NULL;
11676 struct attribute *attr2 = NULL;
11677 CORE_ADDR baseaddr;
11678 struct pending **list_to_add = NULL;
11679
11680 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11681
11682 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11683
11684 name = dwarf2_name (die, cu);
11685 if (name)
11686 {
11687 const char *linkagename;
11688 int suppress_add = 0;
11689
11690 if (space)
11691 sym = space;
11692 else
11693 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11694 OBJSTAT (objfile, n_syms++);
11695
11696 /* Cache this symbol's name and the name's demangled form (if any). */
11697 SYMBOL_SET_LANGUAGE (sym, cu->language);
11698 linkagename = dwarf2_physname (name, die, cu);
11699 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11700
11701 /* Fortran does not have mangling standard and the mangling does differ
11702 between gfortran, iFort etc. */
11703 if (cu->language == language_fortran
11704 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11705 symbol_set_demangled_name (&(sym->ginfo),
11706 (char *) dwarf2_full_name (name, die, cu),
11707 NULL);
11708
11709 /* Default assumptions.
11710 Use the passed type or decode it from the die. */
11711 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11712 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11713 if (type != NULL)
11714 SYMBOL_TYPE (sym) = type;
11715 else
11716 SYMBOL_TYPE (sym) = die_type (die, cu);
11717 attr = dwarf2_attr (die,
11718 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11719 cu);
11720 if (attr)
11721 {
11722 SYMBOL_LINE (sym) = DW_UNSND (attr);
11723 }
11724
11725 attr = dwarf2_attr (die,
11726 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11727 cu);
11728 if (attr)
11729 {
11730 int file_index = DW_UNSND (attr);
11731
11732 if (cu->line_header == NULL
11733 || file_index > cu->line_header->num_file_names)
11734 complaint (&symfile_complaints,
11735 _("file index out of range"));
11736 else if (file_index > 0)
11737 {
11738 struct file_entry *fe;
11739
11740 fe = &cu->line_header->file_names[file_index - 1];
11741 SYMBOL_SYMTAB (sym) = fe->symtab;
11742 }
11743 }
11744
11745 switch (die->tag)
11746 {
11747 case DW_TAG_label:
11748 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11749 if (attr)
11750 {
11751 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11752 }
11753 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11754 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11755 SYMBOL_CLASS (sym) = LOC_LABEL;
11756 add_symbol_to_list (sym, cu->list_in_scope);
11757 break;
11758 case DW_TAG_subprogram:
11759 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11760 finish_block. */
11761 SYMBOL_CLASS (sym) = LOC_BLOCK;
11762 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11763 if ((attr2 && (DW_UNSND (attr2) != 0))
11764 || cu->language == language_ada)
11765 {
11766 /* Subprograms marked external are stored as a global symbol.
11767 Ada subprograms, whether marked external or not, are always
11768 stored as a global symbol, because we want to be able to
11769 access them globally. For instance, we want to be able
11770 to break on a nested subprogram without having to
11771 specify the context. */
11772 list_to_add = &global_symbols;
11773 }
11774 else
11775 {
11776 list_to_add = cu->list_in_scope;
11777 }
11778 break;
11779 case DW_TAG_inlined_subroutine:
11780 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11781 finish_block. */
11782 SYMBOL_CLASS (sym) = LOC_BLOCK;
11783 SYMBOL_INLINED (sym) = 1;
11784 /* Do not add the symbol to any lists. It will be found via
11785 BLOCK_FUNCTION from the blockvector. */
11786 break;
11787 case DW_TAG_template_value_param:
11788 suppress_add = 1;
11789 /* Fall through. */
11790 case DW_TAG_constant:
11791 case DW_TAG_variable:
11792 case DW_TAG_member:
11793 /* Compilation with minimal debug info may result in
11794 variables with missing type entries. Change the
11795 misleading `void' type to something sensible. */
11796 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11797 SYMBOL_TYPE (sym)
11798 = objfile_type (objfile)->nodebug_data_symbol;
11799
11800 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11801 /* In the case of DW_TAG_member, we should only be called for
11802 static const members. */
11803 if (die->tag == DW_TAG_member)
11804 {
11805 /* dwarf2_add_field uses die_is_declaration,
11806 so we do the same. */
11807 gdb_assert (die_is_declaration (die, cu));
11808 gdb_assert (attr);
11809 }
11810 if (attr)
11811 {
11812 dwarf2_const_value (attr, sym, cu);
11813 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11814 if (!suppress_add)
11815 {
11816 if (attr2 && (DW_UNSND (attr2) != 0))
11817 list_to_add = &global_symbols;
11818 else
11819 list_to_add = cu->list_in_scope;
11820 }
11821 break;
11822 }
11823 attr = dwarf2_attr (die, DW_AT_location, cu);
11824 if (attr)
11825 {
11826 var_decode_location (attr, sym, cu);
11827 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11828 if (SYMBOL_CLASS (sym) == LOC_STATIC
11829 && SYMBOL_VALUE_ADDRESS (sym) == 0
11830 && !dwarf2_per_objfile->has_section_at_zero)
11831 {
11832 /* When a static variable is eliminated by the linker,
11833 the corresponding debug information is not stripped
11834 out, but the variable address is set to null;
11835 do not add such variables into symbol table. */
11836 }
11837 else if (attr2 && (DW_UNSND (attr2) != 0))
11838 {
11839 /* Workaround gfortran PR debug/40040 - it uses
11840 DW_AT_location for variables in -fPIC libraries which may
11841 get overriden by other libraries/executable and get
11842 a different address. Resolve it by the minimal symbol
11843 which may come from inferior's executable using copy
11844 relocation. Make this workaround only for gfortran as for
11845 other compilers GDB cannot guess the minimal symbol
11846 Fortran mangling kind. */
11847 if (cu->language == language_fortran && die->parent
11848 && die->parent->tag == DW_TAG_module
11849 && cu->producer
11850 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11851 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11852
11853 /* A variable with DW_AT_external is never static,
11854 but it may be block-scoped. */
11855 list_to_add = (cu->list_in_scope == &file_symbols
11856 ? &global_symbols : cu->list_in_scope);
11857 }
11858 else
11859 list_to_add = cu->list_in_scope;
11860 }
11861 else
11862 {
11863 /* We do not know the address of this symbol.
11864 If it is an external symbol and we have type information
11865 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11866 The address of the variable will then be determined from
11867 the minimal symbol table whenever the variable is
11868 referenced. */
11869 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11870 if (attr2 && (DW_UNSND (attr2) != 0)
11871 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11872 {
11873 /* A variable with DW_AT_external is never static, but it
11874 may be block-scoped. */
11875 list_to_add = (cu->list_in_scope == &file_symbols
11876 ? &global_symbols : cu->list_in_scope);
11877
11878 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11879 }
11880 else if (!die_is_declaration (die, cu))
11881 {
11882 /* Use the default LOC_OPTIMIZED_OUT class. */
11883 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11884 if (!suppress_add)
11885 list_to_add = cu->list_in_scope;
11886 }
11887 }
11888 break;
11889 case DW_TAG_formal_parameter:
11890 /* If we are inside a function, mark this as an argument. If
11891 not, we might be looking at an argument to an inlined function
11892 when we do not have enough information to show inlined frames;
11893 pretend it's a local variable in that case so that the user can
11894 still see it. */
11895 if (context_stack_depth > 0
11896 && context_stack[context_stack_depth - 1].name != NULL)
11897 SYMBOL_IS_ARGUMENT (sym) = 1;
11898 attr = dwarf2_attr (die, DW_AT_location, cu);
11899 if (attr)
11900 {
11901 var_decode_location (attr, sym, cu);
11902 }
11903 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11904 if (attr)
11905 {
11906 dwarf2_const_value (attr, sym, cu);
11907 }
11908
11909 list_to_add = cu->list_in_scope;
11910 break;
11911 case DW_TAG_unspecified_parameters:
11912 /* From varargs functions; gdb doesn't seem to have any
11913 interest in this information, so just ignore it for now.
11914 (FIXME?) */
11915 break;
11916 case DW_TAG_template_type_param:
11917 suppress_add = 1;
11918 /* Fall through. */
11919 case DW_TAG_class_type:
11920 case DW_TAG_interface_type:
11921 case DW_TAG_structure_type:
11922 case DW_TAG_union_type:
11923 case DW_TAG_set_type:
11924 case DW_TAG_enumeration_type:
11925 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11926 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11927
11928 {
11929 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11930 really ever be static objects: otherwise, if you try
11931 to, say, break of a class's method and you're in a file
11932 which doesn't mention that class, it won't work unless
11933 the check for all static symbols in lookup_symbol_aux
11934 saves you. See the OtherFileClass tests in
11935 gdb.c++/namespace.exp. */
11936
11937 if (!suppress_add)
11938 {
11939 list_to_add = (cu->list_in_scope == &file_symbols
11940 && (cu->language == language_cplus
11941 || cu->language == language_java)
11942 ? &global_symbols : cu->list_in_scope);
11943
11944 /* The semantics of C++ state that "struct foo {
11945 ... }" also defines a typedef for "foo". A Java
11946 class declaration also defines a typedef for the
11947 class. */
11948 if (cu->language == language_cplus
11949 || cu->language == language_java
11950 || cu->language == language_ada)
11951 {
11952 /* The symbol's name is already allocated along
11953 with this objfile, so we don't need to
11954 duplicate it for the type. */
11955 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11956 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11957 }
11958 }
11959 }
11960 break;
11961 case DW_TAG_typedef:
11962 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11963 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11964 list_to_add = cu->list_in_scope;
11965 break;
11966 case DW_TAG_base_type:
11967 case DW_TAG_subrange_type:
11968 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11969 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11970 list_to_add = cu->list_in_scope;
11971 break;
11972 case DW_TAG_enumerator:
11973 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11974 if (attr)
11975 {
11976 dwarf2_const_value (attr, sym, cu);
11977 }
11978 {
11979 /* NOTE: carlton/2003-11-10: See comment above in the
11980 DW_TAG_class_type, etc. block. */
11981
11982 list_to_add = (cu->list_in_scope == &file_symbols
11983 && (cu->language == language_cplus
11984 || cu->language == language_java)
11985 ? &global_symbols : cu->list_in_scope);
11986 }
11987 break;
11988 case DW_TAG_namespace:
11989 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11990 list_to_add = &global_symbols;
11991 break;
11992 default:
11993 /* Not a tag we recognize. Hopefully we aren't processing
11994 trash data, but since we must specifically ignore things
11995 we don't recognize, there is nothing else we should do at
11996 this point. */
11997 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11998 dwarf_tag_name (die->tag));
11999 break;
12000 }
12001
12002 if (suppress_add)
12003 {
12004 sym->hash_next = objfile->template_symbols;
12005 objfile->template_symbols = sym;
12006 list_to_add = NULL;
12007 }
12008
12009 if (list_to_add != NULL)
12010 add_symbol_to_list (sym, list_to_add);
12011
12012 /* For the benefit of old versions of GCC, check for anonymous
12013 namespaces based on the demangled name. */
12014 if (!processing_has_namespace_info
12015 && cu->language == language_cplus)
12016 cp_scan_for_anonymous_namespaces (sym, objfile);
12017 }
12018 return (sym);
12019 }
12020
12021 /* A wrapper for new_symbol_full that always allocates a new symbol. */
12022
12023 static struct symbol *
12024 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12025 {
12026 return new_symbol_full (die, type, cu, NULL);
12027 }
12028
12029 /* Given an attr with a DW_FORM_dataN value in host byte order,
12030 zero-extend it as appropriate for the symbol's type. The DWARF
12031 standard (v4) is not entirely clear about the meaning of using
12032 DW_FORM_dataN for a constant with a signed type, where the type is
12033 wider than the data. The conclusion of a discussion on the DWARF
12034 list was that this is unspecified. We choose to always zero-extend
12035 because that is the interpretation long in use by GCC. */
12036
12037 static gdb_byte *
12038 dwarf2_const_value_data (struct attribute *attr, struct type *type,
12039 const char *name, struct obstack *obstack,
12040 struct dwarf2_cu *cu, long *value, int bits)
12041 {
12042 struct objfile *objfile = cu->objfile;
12043 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12044 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
12045 LONGEST l = DW_UNSND (attr);
12046
12047 if (bits < sizeof (*value) * 8)
12048 {
12049 l &= ((LONGEST) 1 << bits) - 1;
12050 *value = l;
12051 }
12052 else if (bits == sizeof (*value) * 8)
12053 *value = l;
12054 else
12055 {
12056 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12057 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12058 return bytes;
12059 }
12060
12061 return NULL;
12062 }
12063
12064 /* Read a constant value from an attribute. Either set *VALUE, or if
12065 the value does not fit in *VALUE, set *BYTES - either already
12066 allocated on the objfile obstack, or newly allocated on OBSTACK,
12067 or, set *BATON, if we translated the constant to a location
12068 expression. */
12069
12070 static void
12071 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12072 const char *name, struct obstack *obstack,
12073 struct dwarf2_cu *cu,
12074 long *value, gdb_byte **bytes,
12075 struct dwarf2_locexpr_baton **baton)
12076 {
12077 struct objfile *objfile = cu->objfile;
12078 struct comp_unit_head *cu_header = &cu->header;
12079 struct dwarf_block *blk;
12080 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12081 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12082
12083 *value = 0;
12084 *bytes = NULL;
12085 *baton = NULL;
12086
12087 switch (attr->form)
12088 {
12089 case DW_FORM_addr:
12090 {
12091 gdb_byte *data;
12092
12093 if (TYPE_LENGTH (type) != cu_header->addr_size)
12094 dwarf2_const_value_length_mismatch_complaint (name,
12095 cu_header->addr_size,
12096 TYPE_LENGTH (type));
12097 /* Symbols of this form are reasonably rare, so we just
12098 piggyback on the existing location code rather than writing
12099 a new implementation of symbol_computed_ops. */
12100 *baton = obstack_alloc (&objfile->objfile_obstack,
12101 sizeof (struct dwarf2_locexpr_baton));
12102 (*baton)->per_cu = cu->per_cu;
12103 gdb_assert ((*baton)->per_cu);
12104
12105 (*baton)->size = 2 + cu_header->addr_size;
12106 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12107 (*baton)->data = data;
12108
12109 data[0] = DW_OP_addr;
12110 store_unsigned_integer (&data[1], cu_header->addr_size,
12111 byte_order, DW_ADDR (attr));
12112 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12113 }
12114 break;
12115 case DW_FORM_string:
12116 case DW_FORM_strp:
12117 /* DW_STRING is already allocated on the objfile obstack, point
12118 directly to it. */
12119 *bytes = (gdb_byte *) DW_STRING (attr);
12120 break;
12121 case DW_FORM_block1:
12122 case DW_FORM_block2:
12123 case DW_FORM_block4:
12124 case DW_FORM_block:
12125 case DW_FORM_exprloc:
12126 blk = DW_BLOCK (attr);
12127 if (TYPE_LENGTH (type) != blk->size)
12128 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12129 TYPE_LENGTH (type));
12130 *bytes = blk->data;
12131 break;
12132
12133 /* The DW_AT_const_value attributes are supposed to carry the
12134 symbol's value "represented as it would be on the target
12135 architecture." By the time we get here, it's already been
12136 converted to host endianness, so we just need to sign- or
12137 zero-extend it as appropriate. */
12138 case DW_FORM_data1:
12139 *bytes = dwarf2_const_value_data (attr, type, name,
12140 obstack, cu, value, 8);
12141 break;
12142 case DW_FORM_data2:
12143 *bytes = dwarf2_const_value_data (attr, type, name,
12144 obstack, cu, value, 16);
12145 break;
12146 case DW_FORM_data4:
12147 *bytes = dwarf2_const_value_data (attr, type, name,
12148 obstack, cu, value, 32);
12149 break;
12150 case DW_FORM_data8:
12151 *bytes = dwarf2_const_value_data (attr, type, name,
12152 obstack, cu, value, 64);
12153 break;
12154
12155 case DW_FORM_sdata:
12156 *value = DW_SND (attr);
12157 break;
12158
12159 case DW_FORM_udata:
12160 *value = DW_UNSND (attr);
12161 break;
12162
12163 default:
12164 complaint (&symfile_complaints,
12165 _("unsupported const value attribute form: '%s'"),
12166 dwarf_form_name (attr->form));
12167 *value = 0;
12168 break;
12169 }
12170 }
12171
12172
12173 /* Copy constant value from an attribute to a symbol. */
12174
12175 static void
12176 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12177 struct dwarf2_cu *cu)
12178 {
12179 struct objfile *objfile = cu->objfile;
12180 struct comp_unit_head *cu_header = &cu->header;
12181 long value;
12182 gdb_byte *bytes;
12183 struct dwarf2_locexpr_baton *baton;
12184
12185 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12186 SYMBOL_PRINT_NAME (sym),
12187 &objfile->objfile_obstack, cu,
12188 &value, &bytes, &baton);
12189
12190 if (baton != NULL)
12191 {
12192 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12193 SYMBOL_LOCATION_BATON (sym) = baton;
12194 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12195 }
12196 else if (bytes != NULL)
12197 {
12198 SYMBOL_VALUE_BYTES (sym) = bytes;
12199 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12200 }
12201 else
12202 {
12203 SYMBOL_VALUE (sym) = value;
12204 SYMBOL_CLASS (sym) = LOC_CONST;
12205 }
12206 }
12207
12208 /* Return the type of the die in question using its DW_AT_type attribute. */
12209
12210 static struct type *
12211 die_type (struct die_info *die, struct dwarf2_cu *cu)
12212 {
12213 struct attribute *type_attr;
12214
12215 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12216 if (!type_attr)
12217 {
12218 /* A missing DW_AT_type represents a void type. */
12219 return objfile_type (cu->objfile)->builtin_void;
12220 }
12221
12222 return lookup_die_type (die, type_attr, cu);
12223 }
12224
12225 /* True iff CU's producer generates GNAT Ada auxiliary information
12226 that allows to find parallel types through that information instead
12227 of having to do expensive parallel lookups by type name. */
12228
12229 static int
12230 need_gnat_info (struct dwarf2_cu *cu)
12231 {
12232 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12233 of GNAT produces this auxiliary information, without any indication
12234 that it is produced. Part of enhancing the FSF version of GNAT
12235 to produce that information will be to put in place an indicator
12236 that we can use in order to determine whether the descriptive type
12237 info is available or not. One suggestion that has been made is
12238 to use a new attribute, attached to the CU die. For now, assume
12239 that the descriptive type info is not available. */
12240 return 0;
12241 }
12242
12243 /* Return the auxiliary type of the die in question using its
12244 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12245 attribute is not present. */
12246
12247 static struct type *
12248 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12249 {
12250 struct attribute *type_attr;
12251
12252 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12253 if (!type_attr)
12254 return NULL;
12255
12256 return lookup_die_type (die, type_attr, cu);
12257 }
12258
12259 /* If DIE has a descriptive_type attribute, then set the TYPE's
12260 descriptive type accordingly. */
12261
12262 static void
12263 set_descriptive_type (struct type *type, struct die_info *die,
12264 struct dwarf2_cu *cu)
12265 {
12266 struct type *descriptive_type = die_descriptive_type (die, cu);
12267
12268 if (descriptive_type)
12269 {
12270 ALLOCATE_GNAT_AUX_TYPE (type);
12271 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12272 }
12273 }
12274
12275 /* Return the containing type of the die in question using its
12276 DW_AT_containing_type attribute. */
12277
12278 static struct type *
12279 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12280 {
12281 struct attribute *type_attr;
12282
12283 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12284 if (!type_attr)
12285 error (_("Dwarf Error: Problem turning containing type into gdb type "
12286 "[in module %s]"), cu->objfile->name);
12287
12288 return lookup_die_type (die, type_attr, cu);
12289 }
12290
12291 /* Look up the type of DIE in CU using its type attribute ATTR.
12292 If there is no type substitute an error marker. */
12293
12294 static struct type *
12295 lookup_die_type (struct die_info *die, struct attribute *attr,
12296 struct dwarf2_cu *cu)
12297 {
12298 struct type *this_type;
12299
12300 /* First see if we have it cached. */
12301
12302 if (is_ref_attr (attr))
12303 {
12304 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12305
12306 this_type = get_die_type_at_offset (offset, cu->per_cu);
12307 }
12308 else if (attr->form == DW_FORM_ref_sig8)
12309 {
12310 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12311 struct dwarf2_cu *sig_cu;
12312 unsigned int offset;
12313
12314 /* sig_type will be NULL if the signatured type is missing from
12315 the debug info. */
12316 if (sig_type == NULL)
12317 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12318 "at 0x%x [in module %s]"),
12319 die->offset, cu->objfile->name);
12320
12321 gdb_assert (sig_type->per_cu.debug_type_section);
12322 offset = sig_type->per_cu.offset + sig_type->type_offset;
12323 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12324 }
12325 else
12326 {
12327 dump_die_for_error (die);
12328 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12329 dwarf_attr_name (attr->name), cu->objfile->name);
12330 }
12331
12332 /* If not cached we need to read it in. */
12333
12334 if (this_type == NULL)
12335 {
12336 struct die_info *type_die;
12337 struct dwarf2_cu *type_cu = cu;
12338
12339 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12340 /* If the type is cached, we should have found it above. */
12341 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12342 this_type = read_type_die_1 (type_die, type_cu);
12343 }
12344
12345 /* If we still don't have a type use an error marker. */
12346
12347 if (this_type == NULL)
12348 {
12349 char *message, *saved;
12350
12351 /* read_type_die already issued a complaint. */
12352 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12353 cu->objfile->name,
12354 cu->header.offset,
12355 die->offset);
12356 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12357 message, strlen (message));
12358 xfree (message);
12359
12360 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
12361 }
12362
12363 return this_type;
12364 }
12365
12366 /* Return the type in DIE, CU.
12367 Returns NULL for invalid types.
12368
12369 This first does a lookup in the appropriate type_hash table,
12370 and only reads the die in if necessary.
12371
12372 NOTE: This can be called when reading in partial or full symbols. */
12373
12374 static struct type *
12375 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12376 {
12377 struct type *this_type;
12378
12379 this_type = get_die_type (die, cu);
12380 if (this_type)
12381 return this_type;
12382
12383 return read_type_die_1 (die, cu);
12384 }
12385
12386 /* Read the type in DIE, CU.
12387 Returns NULL for invalid types. */
12388
12389 static struct type *
12390 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12391 {
12392 struct type *this_type = NULL;
12393
12394 switch (die->tag)
12395 {
12396 case DW_TAG_class_type:
12397 case DW_TAG_interface_type:
12398 case DW_TAG_structure_type:
12399 case DW_TAG_union_type:
12400 this_type = read_structure_type (die, cu);
12401 break;
12402 case DW_TAG_enumeration_type:
12403 this_type = read_enumeration_type (die, cu);
12404 break;
12405 case DW_TAG_subprogram:
12406 case DW_TAG_subroutine_type:
12407 case DW_TAG_inlined_subroutine:
12408 this_type = read_subroutine_type (die, cu);
12409 break;
12410 case DW_TAG_array_type:
12411 this_type = read_array_type (die, cu);
12412 break;
12413 case DW_TAG_set_type:
12414 this_type = read_set_type (die, cu);
12415 break;
12416 case DW_TAG_pointer_type:
12417 this_type = read_tag_pointer_type (die, cu);
12418 break;
12419 case DW_TAG_ptr_to_member_type:
12420 this_type = read_tag_ptr_to_member_type (die, cu);
12421 break;
12422 case DW_TAG_reference_type:
12423 this_type = read_tag_reference_type (die, cu);
12424 break;
12425 case DW_TAG_const_type:
12426 this_type = read_tag_const_type (die, cu);
12427 break;
12428 case DW_TAG_volatile_type:
12429 this_type = read_tag_volatile_type (die, cu);
12430 break;
12431 case DW_TAG_string_type:
12432 this_type = read_tag_string_type (die, cu);
12433 break;
12434 case DW_TAG_typedef:
12435 this_type = read_typedef (die, cu);
12436 break;
12437 case DW_TAG_subrange_type:
12438 this_type = read_subrange_type (die, cu);
12439 break;
12440 case DW_TAG_base_type:
12441 this_type = read_base_type (die, cu);
12442 break;
12443 case DW_TAG_unspecified_type:
12444 this_type = read_unspecified_type (die, cu);
12445 break;
12446 case DW_TAG_namespace:
12447 this_type = read_namespace_type (die, cu);
12448 break;
12449 case DW_TAG_module:
12450 this_type = read_module_type (die, cu);
12451 break;
12452 default:
12453 complaint (&symfile_complaints,
12454 _("unexpected tag in read_type_die: '%s'"),
12455 dwarf_tag_name (die->tag));
12456 break;
12457 }
12458
12459 return this_type;
12460 }
12461
12462 /* See if we can figure out if the class lives in a namespace. We do
12463 this by looking for a member function; its demangled name will
12464 contain namespace info, if there is any.
12465 Return the computed name or NULL.
12466 Space for the result is allocated on the objfile's obstack.
12467 This is the full-die version of guess_partial_die_structure_name.
12468 In this case we know DIE has no useful parent. */
12469
12470 static char *
12471 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12472 {
12473 struct die_info *spec_die;
12474 struct dwarf2_cu *spec_cu;
12475 struct die_info *child;
12476
12477 spec_cu = cu;
12478 spec_die = die_specification (die, &spec_cu);
12479 if (spec_die != NULL)
12480 {
12481 die = spec_die;
12482 cu = spec_cu;
12483 }
12484
12485 for (child = die->child;
12486 child != NULL;
12487 child = child->sibling)
12488 {
12489 if (child->tag == DW_TAG_subprogram)
12490 {
12491 struct attribute *attr;
12492
12493 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12494 if (attr == NULL)
12495 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12496 if (attr != NULL)
12497 {
12498 char *actual_name
12499 = language_class_name_from_physname (cu->language_defn,
12500 DW_STRING (attr));
12501 char *name = NULL;
12502
12503 if (actual_name != NULL)
12504 {
12505 char *die_name = dwarf2_name (die, cu);
12506
12507 if (die_name != NULL
12508 && strcmp (die_name, actual_name) != 0)
12509 {
12510 /* Strip off the class name from the full name.
12511 We want the prefix. */
12512 int die_name_len = strlen (die_name);
12513 int actual_name_len = strlen (actual_name);
12514
12515 /* Test for '::' as a sanity check. */
12516 if (actual_name_len > die_name_len + 2
12517 && actual_name[actual_name_len
12518 - die_name_len - 1] == ':')
12519 name =
12520 obsavestring (actual_name,
12521 actual_name_len - die_name_len - 2,
12522 &cu->objfile->objfile_obstack);
12523 }
12524 }
12525 xfree (actual_name);
12526 return name;
12527 }
12528 }
12529 }
12530
12531 return NULL;
12532 }
12533
12534 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12535 prefix part in such case. See
12536 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12537
12538 static char *
12539 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12540 {
12541 struct attribute *attr;
12542 char *base;
12543
12544 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12545 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12546 return NULL;
12547
12548 attr = dwarf2_attr (die, DW_AT_name, cu);
12549 if (attr != NULL && DW_STRING (attr) != NULL)
12550 return NULL;
12551
12552 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12553 if (attr == NULL)
12554 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12555 if (attr == NULL || DW_STRING (attr) == NULL)
12556 return NULL;
12557
12558 /* dwarf2_name had to be already called. */
12559 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12560
12561 /* Strip the base name, keep any leading namespaces/classes. */
12562 base = strrchr (DW_STRING (attr), ':');
12563 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12564 return "";
12565
12566 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12567 &cu->objfile->objfile_obstack);
12568 }
12569
12570 /* Return the name of the namespace/class that DIE is defined within,
12571 or "" if we can't tell. The caller should not xfree the result.
12572
12573 For example, if we're within the method foo() in the following
12574 code:
12575
12576 namespace N {
12577 class C {
12578 void foo () {
12579 }
12580 };
12581 }
12582
12583 then determine_prefix on foo's die will return "N::C". */
12584
12585 static char *
12586 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12587 {
12588 struct die_info *parent, *spec_die;
12589 struct dwarf2_cu *spec_cu;
12590 struct type *parent_type;
12591 char *retval;
12592
12593 if (cu->language != language_cplus && cu->language != language_java
12594 && cu->language != language_fortran)
12595 return "";
12596
12597 retval = anonymous_struct_prefix (die, cu);
12598 if (retval)
12599 return retval;
12600
12601 /* We have to be careful in the presence of DW_AT_specification.
12602 For example, with GCC 3.4, given the code
12603
12604 namespace N {
12605 void foo() {
12606 // Definition of N::foo.
12607 }
12608 }
12609
12610 then we'll have a tree of DIEs like this:
12611
12612 1: DW_TAG_compile_unit
12613 2: DW_TAG_namespace // N
12614 3: DW_TAG_subprogram // declaration of N::foo
12615 4: DW_TAG_subprogram // definition of N::foo
12616 DW_AT_specification // refers to die #3
12617
12618 Thus, when processing die #4, we have to pretend that we're in
12619 the context of its DW_AT_specification, namely the contex of die
12620 #3. */
12621 spec_cu = cu;
12622 spec_die = die_specification (die, &spec_cu);
12623 if (spec_die == NULL)
12624 parent = die->parent;
12625 else
12626 {
12627 parent = spec_die->parent;
12628 cu = spec_cu;
12629 }
12630
12631 if (parent == NULL)
12632 return "";
12633 else if (parent->building_fullname)
12634 {
12635 const char *name;
12636 const char *parent_name;
12637
12638 /* It has been seen on RealView 2.2 built binaries,
12639 DW_TAG_template_type_param types actually _defined_ as
12640 children of the parent class:
12641
12642 enum E {};
12643 template class <class Enum> Class{};
12644 Class<enum E> class_e;
12645
12646 1: DW_TAG_class_type (Class)
12647 2: DW_TAG_enumeration_type (E)
12648 3: DW_TAG_enumerator (enum1:0)
12649 3: DW_TAG_enumerator (enum2:1)
12650 ...
12651 2: DW_TAG_template_type_param
12652 DW_AT_type DW_FORM_ref_udata (E)
12653
12654 Besides being broken debug info, it can put GDB into an
12655 infinite loop. Consider:
12656
12657 When we're building the full name for Class<E>, we'll start
12658 at Class, and go look over its template type parameters,
12659 finding E. We'll then try to build the full name of E, and
12660 reach here. We're now trying to build the full name of E,
12661 and look over the parent DIE for containing scope. In the
12662 broken case, if we followed the parent DIE of E, we'd again
12663 find Class, and once again go look at its template type
12664 arguments, etc., etc. Simply don't consider such parent die
12665 as source-level parent of this die (it can't be, the language
12666 doesn't allow it), and break the loop here. */
12667 name = dwarf2_name (die, cu);
12668 parent_name = dwarf2_name (parent, cu);
12669 complaint (&symfile_complaints,
12670 _("template param type '%s' defined within parent '%s'"),
12671 name ? name : "<unknown>",
12672 parent_name ? parent_name : "<unknown>");
12673 return "";
12674 }
12675 else
12676 switch (parent->tag)
12677 {
12678 case DW_TAG_namespace:
12679 parent_type = read_type_die (parent, cu);
12680 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12681 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12682 Work around this problem here. */
12683 if (cu->language == language_cplus
12684 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12685 return "";
12686 /* We give a name to even anonymous namespaces. */
12687 return TYPE_TAG_NAME (parent_type);
12688 case DW_TAG_class_type:
12689 case DW_TAG_interface_type:
12690 case DW_TAG_structure_type:
12691 case DW_TAG_union_type:
12692 case DW_TAG_module:
12693 parent_type = read_type_die (parent, cu);
12694 if (TYPE_TAG_NAME (parent_type) != NULL)
12695 return TYPE_TAG_NAME (parent_type);
12696 else
12697 /* An anonymous structure is only allowed non-static data
12698 members; no typedefs, no member functions, et cetera.
12699 So it does not need a prefix. */
12700 return "";
12701 case DW_TAG_compile_unit:
12702 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12703 if (cu->language == language_cplus
12704 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12705 && die->child != NULL
12706 && (die->tag == DW_TAG_class_type
12707 || die->tag == DW_TAG_structure_type
12708 || die->tag == DW_TAG_union_type))
12709 {
12710 char *name = guess_full_die_structure_name (die, cu);
12711 if (name != NULL)
12712 return name;
12713 }
12714 return "";
12715 default:
12716 return determine_prefix (parent, cu);
12717 }
12718 }
12719
12720 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12721 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12722 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12723 an obconcat, otherwise allocate storage for the result. The CU argument is
12724 used to determine the language and hence, the appropriate separator. */
12725
12726 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12727
12728 static char *
12729 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12730 int physname, struct dwarf2_cu *cu)
12731 {
12732 const char *lead = "";
12733 const char *sep;
12734
12735 if (suffix == NULL || suffix[0] == '\0'
12736 || prefix == NULL || prefix[0] == '\0')
12737 sep = "";
12738 else if (cu->language == language_java)
12739 sep = ".";
12740 else if (cu->language == language_fortran && physname)
12741 {
12742 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12743 DW_AT_MIPS_linkage_name is preferred and used instead. */
12744
12745 lead = "__";
12746 sep = "_MOD_";
12747 }
12748 else
12749 sep = "::";
12750
12751 if (prefix == NULL)
12752 prefix = "";
12753 if (suffix == NULL)
12754 suffix = "";
12755
12756 if (obs == NULL)
12757 {
12758 char *retval
12759 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12760
12761 strcpy (retval, lead);
12762 strcat (retval, prefix);
12763 strcat (retval, sep);
12764 strcat (retval, suffix);
12765 return retval;
12766 }
12767 else
12768 {
12769 /* We have an obstack. */
12770 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12771 }
12772 }
12773
12774 /* Return sibling of die, NULL if no sibling. */
12775
12776 static struct die_info *
12777 sibling_die (struct die_info *die)
12778 {
12779 return die->sibling;
12780 }
12781
12782 /* Get name of a die, return NULL if not found. */
12783
12784 static char *
12785 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12786 struct obstack *obstack)
12787 {
12788 if (name && cu->language == language_cplus)
12789 {
12790 char *canon_name = cp_canonicalize_string (name);
12791
12792 if (canon_name != NULL)
12793 {
12794 if (strcmp (canon_name, name) != 0)
12795 name = obsavestring (canon_name, strlen (canon_name),
12796 obstack);
12797 xfree (canon_name);
12798 }
12799 }
12800
12801 return name;
12802 }
12803
12804 /* Get name of a die, return NULL if not found. */
12805
12806 static char *
12807 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12808 {
12809 struct attribute *attr;
12810
12811 attr = dwarf2_attr (die, DW_AT_name, cu);
12812 if ((!attr || !DW_STRING (attr))
12813 && die->tag != DW_TAG_class_type
12814 && die->tag != DW_TAG_interface_type
12815 && die->tag != DW_TAG_structure_type
12816 && die->tag != DW_TAG_union_type)
12817 return NULL;
12818
12819 switch (die->tag)
12820 {
12821 case DW_TAG_compile_unit:
12822 /* Compilation units have a DW_AT_name that is a filename, not
12823 a source language identifier. */
12824 case DW_TAG_enumeration_type:
12825 case DW_TAG_enumerator:
12826 /* These tags always have simple identifiers already; no need
12827 to canonicalize them. */
12828 return DW_STRING (attr);
12829
12830 case DW_TAG_subprogram:
12831 /* Java constructors will all be named "<init>", so return
12832 the class name when we see this special case. */
12833 if (cu->language == language_java
12834 && DW_STRING (attr) != NULL
12835 && strcmp (DW_STRING (attr), "<init>") == 0)
12836 {
12837 struct dwarf2_cu *spec_cu = cu;
12838 struct die_info *spec_die;
12839
12840 /* GCJ will output '<init>' for Java constructor names.
12841 For this special case, return the name of the parent class. */
12842
12843 /* GCJ may output suprogram DIEs with AT_specification set.
12844 If so, use the name of the specified DIE. */
12845 spec_die = die_specification (die, &spec_cu);
12846 if (spec_die != NULL)
12847 return dwarf2_name (spec_die, spec_cu);
12848
12849 do
12850 {
12851 die = die->parent;
12852 if (die->tag == DW_TAG_class_type)
12853 return dwarf2_name (die, cu);
12854 }
12855 while (die->tag != DW_TAG_compile_unit);
12856 }
12857 break;
12858
12859 case DW_TAG_class_type:
12860 case DW_TAG_interface_type:
12861 case DW_TAG_structure_type:
12862 case DW_TAG_union_type:
12863 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12864 structures or unions. These were of the form "._%d" in GCC 4.1,
12865 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12866 and GCC 4.4. We work around this problem by ignoring these. */
12867 if (attr && DW_STRING (attr)
12868 && (strncmp (DW_STRING (attr), "._", 2) == 0
12869 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12870 return NULL;
12871
12872 /* GCC might emit a nameless typedef that has a linkage name. See
12873 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12874 if (!attr || DW_STRING (attr) == NULL)
12875 {
12876 char *demangled = NULL;
12877
12878 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12879 if (attr == NULL)
12880 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12881
12882 if (attr == NULL || DW_STRING (attr) == NULL)
12883 return NULL;
12884
12885 /* Avoid demangling DW_STRING (attr) the second time on a second
12886 call for the same DIE. */
12887 if (!DW_STRING_IS_CANONICAL (attr))
12888 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12889
12890 if (demangled)
12891 {
12892 char *base;
12893
12894 /* FIXME: we already did this for the partial symbol... */
12895 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12896 &cu->objfile->objfile_obstack);
12897 DW_STRING_IS_CANONICAL (attr) = 1;
12898 xfree (demangled);
12899
12900 /* Strip any leading namespaces/classes, keep only the base name.
12901 DW_AT_name for named DIEs does not contain the prefixes. */
12902 base = strrchr (DW_STRING (attr), ':');
12903 if (base && base > DW_STRING (attr) && base[-1] == ':')
12904 return &base[1];
12905 else
12906 return DW_STRING (attr);
12907 }
12908 }
12909 break;
12910
12911 default:
12912 break;
12913 }
12914
12915 if (!DW_STRING_IS_CANONICAL (attr))
12916 {
12917 DW_STRING (attr)
12918 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12919 &cu->objfile->objfile_obstack);
12920 DW_STRING_IS_CANONICAL (attr) = 1;
12921 }
12922 return DW_STRING (attr);
12923 }
12924
12925 /* Return the die that this die in an extension of, or NULL if there
12926 is none. *EXT_CU is the CU containing DIE on input, and the CU
12927 containing the return value on output. */
12928
12929 static struct die_info *
12930 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12931 {
12932 struct attribute *attr;
12933
12934 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12935 if (attr == NULL)
12936 return NULL;
12937
12938 return follow_die_ref (die, attr, ext_cu);
12939 }
12940
12941 /* Convert a DIE tag into its string name. */
12942
12943 static char *
12944 dwarf_tag_name (unsigned tag)
12945 {
12946 switch (tag)
12947 {
12948 case DW_TAG_padding:
12949 return "DW_TAG_padding";
12950 case DW_TAG_array_type:
12951 return "DW_TAG_array_type";
12952 case DW_TAG_class_type:
12953 return "DW_TAG_class_type";
12954 case DW_TAG_entry_point:
12955 return "DW_TAG_entry_point";
12956 case DW_TAG_enumeration_type:
12957 return "DW_TAG_enumeration_type";
12958 case DW_TAG_formal_parameter:
12959 return "DW_TAG_formal_parameter";
12960 case DW_TAG_imported_declaration:
12961 return "DW_TAG_imported_declaration";
12962 case DW_TAG_label:
12963 return "DW_TAG_label";
12964 case DW_TAG_lexical_block:
12965 return "DW_TAG_lexical_block";
12966 case DW_TAG_member:
12967 return "DW_TAG_member";
12968 case DW_TAG_pointer_type:
12969 return "DW_TAG_pointer_type";
12970 case DW_TAG_reference_type:
12971 return "DW_TAG_reference_type";
12972 case DW_TAG_compile_unit:
12973 return "DW_TAG_compile_unit";
12974 case DW_TAG_string_type:
12975 return "DW_TAG_string_type";
12976 case DW_TAG_structure_type:
12977 return "DW_TAG_structure_type";
12978 case DW_TAG_subroutine_type:
12979 return "DW_TAG_subroutine_type";
12980 case DW_TAG_typedef:
12981 return "DW_TAG_typedef";
12982 case DW_TAG_union_type:
12983 return "DW_TAG_union_type";
12984 case DW_TAG_unspecified_parameters:
12985 return "DW_TAG_unspecified_parameters";
12986 case DW_TAG_variant:
12987 return "DW_TAG_variant";
12988 case DW_TAG_common_block:
12989 return "DW_TAG_common_block";
12990 case DW_TAG_common_inclusion:
12991 return "DW_TAG_common_inclusion";
12992 case DW_TAG_inheritance:
12993 return "DW_TAG_inheritance";
12994 case DW_TAG_inlined_subroutine:
12995 return "DW_TAG_inlined_subroutine";
12996 case DW_TAG_module:
12997 return "DW_TAG_module";
12998 case DW_TAG_ptr_to_member_type:
12999 return "DW_TAG_ptr_to_member_type";
13000 case DW_TAG_set_type:
13001 return "DW_TAG_set_type";
13002 case DW_TAG_subrange_type:
13003 return "DW_TAG_subrange_type";
13004 case DW_TAG_with_stmt:
13005 return "DW_TAG_with_stmt";
13006 case DW_TAG_access_declaration:
13007 return "DW_TAG_access_declaration";
13008 case DW_TAG_base_type:
13009 return "DW_TAG_base_type";
13010 case DW_TAG_catch_block:
13011 return "DW_TAG_catch_block";
13012 case DW_TAG_const_type:
13013 return "DW_TAG_const_type";
13014 case DW_TAG_constant:
13015 return "DW_TAG_constant";
13016 case DW_TAG_enumerator:
13017 return "DW_TAG_enumerator";
13018 case DW_TAG_file_type:
13019 return "DW_TAG_file_type";
13020 case DW_TAG_friend:
13021 return "DW_TAG_friend";
13022 case DW_TAG_namelist:
13023 return "DW_TAG_namelist";
13024 case DW_TAG_namelist_item:
13025 return "DW_TAG_namelist_item";
13026 case DW_TAG_packed_type:
13027 return "DW_TAG_packed_type";
13028 case DW_TAG_subprogram:
13029 return "DW_TAG_subprogram";
13030 case DW_TAG_template_type_param:
13031 return "DW_TAG_template_type_param";
13032 case DW_TAG_template_value_param:
13033 return "DW_TAG_template_value_param";
13034 case DW_TAG_thrown_type:
13035 return "DW_TAG_thrown_type";
13036 case DW_TAG_try_block:
13037 return "DW_TAG_try_block";
13038 case DW_TAG_variant_part:
13039 return "DW_TAG_variant_part";
13040 case DW_TAG_variable:
13041 return "DW_TAG_variable";
13042 case DW_TAG_volatile_type:
13043 return "DW_TAG_volatile_type";
13044 case DW_TAG_dwarf_procedure:
13045 return "DW_TAG_dwarf_procedure";
13046 case DW_TAG_restrict_type:
13047 return "DW_TAG_restrict_type";
13048 case DW_TAG_interface_type:
13049 return "DW_TAG_interface_type";
13050 case DW_TAG_namespace:
13051 return "DW_TAG_namespace";
13052 case DW_TAG_imported_module:
13053 return "DW_TAG_imported_module";
13054 case DW_TAG_unspecified_type:
13055 return "DW_TAG_unspecified_type";
13056 case DW_TAG_partial_unit:
13057 return "DW_TAG_partial_unit";
13058 case DW_TAG_imported_unit:
13059 return "DW_TAG_imported_unit";
13060 case DW_TAG_condition:
13061 return "DW_TAG_condition";
13062 case DW_TAG_shared_type:
13063 return "DW_TAG_shared_type";
13064 case DW_TAG_type_unit:
13065 return "DW_TAG_type_unit";
13066 case DW_TAG_MIPS_loop:
13067 return "DW_TAG_MIPS_loop";
13068 case DW_TAG_HP_array_descriptor:
13069 return "DW_TAG_HP_array_descriptor";
13070 case DW_TAG_format_label:
13071 return "DW_TAG_format_label";
13072 case DW_TAG_function_template:
13073 return "DW_TAG_function_template";
13074 case DW_TAG_class_template:
13075 return "DW_TAG_class_template";
13076 case DW_TAG_GNU_BINCL:
13077 return "DW_TAG_GNU_BINCL";
13078 case DW_TAG_GNU_EINCL:
13079 return "DW_TAG_GNU_EINCL";
13080 case DW_TAG_upc_shared_type:
13081 return "DW_TAG_upc_shared_type";
13082 case DW_TAG_upc_strict_type:
13083 return "DW_TAG_upc_strict_type";
13084 case DW_TAG_upc_relaxed_type:
13085 return "DW_TAG_upc_relaxed_type";
13086 case DW_TAG_PGI_kanji_type:
13087 return "DW_TAG_PGI_kanji_type";
13088 case DW_TAG_PGI_interface_block:
13089 return "DW_TAG_PGI_interface_block";
13090 case DW_TAG_GNU_call_site:
13091 return "DW_TAG_GNU_call_site";
13092 default:
13093 return "DW_TAG_<unknown>";
13094 }
13095 }
13096
13097 /* Convert a DWARF attribute code into its string name. */
13098
13099 static char *
13100 dwarf_attr_name (unsigned attr)
13101 {
13102 switch (attr)
13103 {
13104 case DW_AT_sibling:
13105 return "DW_AT_sibling";
13106 case DW_AT_location:
13107 return "DW_AT_location";
13108 case DW_AT_name:
13109 return "DW_AT_name";
13110 case DW_AT_ordering:
13111 return "DW_AT_ordering";
13112 case DW_AT_subscr_data:
13113 return "DW_AT_subscr_data";
13114 case DW_AT_byte_size:
13115 return "DW_AT_byte_size";
13116 case DW_AT_bit_offset:
13117 return "DW_AT_bit_offset";
13118 case DW_AT_bit_size:
13119 return "DW_AT_bit_size";
13120 case DW_AT_element_list:
13121 return "DW_AT_element_list";
13122 case DW_AT_stmt_list:
13123 return "DW_AT_stmt_list";
13124 case DW_AT_low_pc:
13125 return "DW_AT_low_pc";
13126 case DW_AT_high_pc:
13127 return "DW_AT_high_pc";
13128 case DW_AT_language:
13129 return "DW_AT_language";
13130 case DW_AT_member:
13131 return "DW_AT_member";
13132 case DW_AT_discr:
13133 return "DW_AT_discr";
13134 case DW_AT_discr_value:
13135 return "DW_AT_discr_value";
13136 case DW_AT_visibility:
13137 return "DW_AT_visibility";
13138 case DW_AT_import:
13139 return "DW_AT_import";
13140 case DW_AT_string_length:
13141 return "DW_AT_string_length";
13142 case DW_AT_common_reference:
13143 return "DW_AT_common_reference";
13144 case DW_AT_comp_dir:
13145 return "DW_AT_comp_dir";
13146 case DW_AT_const_value:
13147 return "DW_AT_const_value";
13148 case DW_AT_containing_type:
13149 return "DW_AT_containing_type";
13150 case DW_AT_default_value:
13151 return "DW_AT_default_value";
13152 case DW_AT_inline:
13153 return "DW_AT_inline";
13154 case DW_AT_is_optional:
13155 return "DW_AT_is_optional";
13156 case DW_AT_lower_bound:
13157 return "DW_AT_lower_bound";
13158 case DW_AT_producer:
13159 return "DW_AT_producer";
13160 case DW_AT_prototyped:
13161 return "DW_AT_prototyped";
13162 case DW_AT_return_addr:
13163 return "DW_AT_return_addr";
13164 case DW_AT_start_scope:
13165 return "DW_AT_start_scope";
13166 case DW_AT_bit_stride:
13167 return "DW_AT_bit_stride";
13168 case DW_AT_upper_bound:
13169 return "DW_AT_upper_bound";
13170 case DW_AT_abstract_origin:
13171 return "DW_AT_abstract_origin";
13172 case DW_AT_accessibility:
13173 return "DW_AT_accessibility";
13174 case DW_AT_address_class:
13175 return "DW_AT_address_class";
13176 case DW_AT_artificial:
13177 return "DW_AT_artificial";
13178 case DW_AT_base_types:
13179 return "DW_AT_base_types";
13180 case DW_AT_calling_convention:
13181 return "DW_AT_calling_convention";
13182 case DW_AT_count:
13183 return "DW_AT_count";
13184 case DW_AT_data_member_location:
13185 return "DW_AT_data_member_location";
13186 case DW_AT_decl_column:
13187 return "DW_AT_decl_column";
13188 case DW_AT_decl_file:
13189 return "DW_AT_decl_file";
13190 case DW_AT_decl_line:
13191 return "DW_AT_decl_line";
13192 case DW_AT_declaration:
13193 return "DW_AT_declaration";
13194 case DW_AT_discr_list:
13195 return "DW_AT_discr_list";
13196 case DW_AT_encoding:
13197 return "DW_AT_encoding";
13198 case DW_AT_external:
13199 return "DW_AT_external";
13200 case DW_AT_frame_base:
13201 return "DW_AT_frame_base";
13202 case DW_AT_friend:
13203 return "DW_AT_friend";
13204 case DW_AT_identifier_case:
13205 return "DW_AT_identifier_case";
13206 case DW_AT_macro_info:
13207 return "DW_AT_macro_info";
13208 case DW_AT_namelist_items:
13209 return "DW_AT_namelist_items";
13210 case DW_AT_priority:
13211 return "DW_AT_priority";
13212 case DW_AT_segment:
13213 return "DW_AT_segment";
13214 case DW_AT_specification:
13215 return "DW_AT_specification";
13216 case DW_AT_static_link:
13217 return "DW_AT_static_link";
13218 case DW_AT_type:
13219 return "DW_AT_type";
13220 case DW_AT_use_location:
13221 return "DW_AT_use_location";
13222 case DW_AT_variable_parameter:
13223 return "DW_AT_variable_parameter";
13224 case DW_AT_virtuality:
13225 return "DW_AT_virtuality";
13226 case DW_AT_vtable_elem_location:
13227 return "DW_AT_vtable_elem_location";
13228 /* DWARF 3 values. */
13229 case DW_AT_allocated:
13230 return "DW_AT_allocated";
13231 case DW_AT_associated:
13232 return "DW_AT_associated";
13233 case DW_AT_data_location:
13234 return "DW_AT_data_location";
13235 case DW_AT_byte_stride:
13236 return "DW_AT_byte_stride";
13237 case DW_AT_entry_pc:
13238 return "DW_AT_entry_pc";
13239 case DW_AT_use_UTF8:
13240 return "DW_AT_use_UTF8";
13241 case DW_AT_extension:
13242 return "DW_AT_extension";
13243 case DW_AT_ranges:
13244 return "DW_AT_ranges";
13245 case DW_AT_trampoline:
13246 return "DW_AT_trampoline";
13247 case DW_AT_call_column:
13248 return "DW_AT_call_column";
13249 case DW_AT_call_file:
13250 return "DW_AT_call_file";
13251 case DW_AT_call_line:
13252 return "DW_AT_call_line";
13253 case DW_AT_description:
13254 return "DW_AT_description";
13255 case DW_AT_binary_scale:
13256 return "DW_AT_binary_scale";
13257 case DW_AT_decimal_scale:
13258 return "DW_AT_decimal_scale";
13259 case DW_AT_small:
13260 return "DW_AT_small";
13261 case DW_AT_decimal_sign:
13262 return "DW_AT_decimal_sign";
13263 case DW_AT_digit_count:
13264 return "DW_AT_digit_count";
13265 case DW_AT_picture_string:
13266 return "DW_AT_picture_string";
13267 case DW_AT_mutable:
13268 return "DW_AT_mutable";
13269 case DW_AT_threads_scaled:
13270 return "DW_AT_threads_scaled";
13271 case DW_AT_explicit:
13272 return "DW_AT_explicit";
13273 case DW_AT_object_pointer:
13274 return "DW_AT_object_pointer";
13275 case DW_AT_endianity:
13276 return "DW_AT_endianity";
13277 case DW_AT_elemental:
13278 return "DW_AT_elemental";
13279 case DW_AT_pure:
13280 return "DW_AT_pure";
13281 case DW_AT_recursive:
13282 return "DW_AT_recursive";
13283 /* DWARF 4 values. */
13284 case DW_AT_signature:
13285 return "DW_AT_signature";
13286 case DW_AT_linkage_name:
13287 return "DW_AT_linkage_name";
13288 /* SGI/MIPS extensions. */
13289 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13290 case DW_AT_MIPS_fde:
13291 return "DW_AT_MIPS_fde";
13292 #endif
13293 case DW_AT_MIPS_loop_begin:
13294 return "DW_AT_MIPS_loop_begin";
13295 case DW_AT_MIPS_tail_loop_begin:
13296 return "DW_AT_MIPS_tail_loop_begin";
13297 case DW_AT_MIPS_epilog_begin:
13298 return "DW_AT_MIPS_epilog_begin";
13299 case DW_AT_MIPS_loop_unroll_factor:
13300 return "DW_AT_MIPS_loop_unroll_factor";
13301 case DW_AT_MIPS_software_pipeline_depth:
13302 return "DW_AT_MIPS_software_pipeline_depth";
13303 case DW_AT_MIPS_linkage_name:
13304 return "DW_AT_MIPS_linkage_name";
13305 case DW_AT_MIPS_stride:
13306 return "DW_AT_MIPS_stride";
13307 case DW_AT_MIPS_abstract_name:
13308 return "DW_AT_MIPS_abstract_name";
13309 case DW_AT_MIPS_clone_origin:
13310 return "DW_AT_MIPS_clone_origin";
13311 case DW_AT_MIPS_has_inlines:
13312 return "DW_AT_MIPS_has_inlines";
13313 /* HP extensions. */
13314 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13315 case DW_AT_HP_block_index:
13316 return "DW_AT_HP_block_index";
13317 #endif
13318 case DW_AT_HP_unmodifiable:
13319 return "DW_AT_HP_unmodifiable";
13320 case DW_AT_HP_actuals_stmt_list:
13321 return "DW_AT_HP_actuals_stmt_list";
13322 case DW_AT_HP_proc_per_section:
13323 return "DW_AT_HP_proc_per_section";
13324 case DW_AT_HP_raw_data_ptr:
13325 return "DW_AT_HP_raw_data_ptr";
13326 case DW_AT_HP_pass_by_reference:
13327 return "DW_AT_HP_pass_by_reference";
13328 case DW_AT_HP_opt_level:
13329 return "DW_AT_HP_opt_level";
13330 case DW_AT_HP_prof_version_id:
13331 return "DW_AT_HP_prof_version_id";
13332 case DW_AT_HP_opt_flags:
13333 return "DW_AT_HP_opt_flags";
13334 case DW_AT_HP_cold_region_low_pc:
13335 return "DW_AT_HP_cold_region_low_pc";
13336 case DW_AT_HP_cold_region_high_pc:
13337 return "DW_AT_HP_cold_region_high_pc";
13338 case DW_AT_HP_all_variables_modifiable:
13339 return "DW_AT_HP_all_variables_modifiable";
13340 case DW_AT_HP_linkage_name:
13341 return "DW_AT_HP_linkage_name";
13342 case DW_AT_HP_prof_flags:
13343 return "DW_AT_HP_prof_flags";
13344 /* GNU extensions. */
13345 case DW_AT_sf_names:
13346 return "DW_AT_sf_names";
13347 case DW_AT_src_info:
13348 return "DW_AT_src_info";
13349 case DW_AT_mac_info:
13350 return "DW_AT_mac_info";
13351 case DW_AT_src_coords:
13352 return "DW_AT_src_coords";
13353 case DW_AT_body_begin:
13354 return "DW_AT_body_begin";
13355 case DW_AT_body_end:
13356 return "DW_AT_body_end";
13357 case DW_AT_GNU_vector:
13358 return "DW_AT_GNU_vector";
13359 case DW_AT_GNU_odr_signature:
13360 return "DW_AT_GNU_odr_signature";
13361 /* VMS extensions. */
13362 case DW_AT_VMS_rtnbeg_pd_address:
13363 return "DW_AT_VMS_rtnbeg_pd_address";
13364 /* UPC extension. */
13365 case DW_AT_upc_threads_scaled:
13366 return "DW_AT_upc_threads_scaled";
13367 /* PGI (STMicroelectronics) extensions. */
13368 case DW_AT_PGI_lbase:
13369 return "DW_AT_PGI_lbase";
13370 case DW_AT_PGI_soffset:
13371 return "DW_AT_PGI_soffset";
13372 case DW_AT_PGI_lstride:
13373 return "DW_AT_PGI_lstride";
13374 default:
13375 return "DW_AT_<unknown>";
13376 }
13377 }
13378
13379 /* Convert a DWARF value form code into its string name. */
13380
13381 static char *
13382 dwarf_form_name (unsigned form)
13383 {
13384 switch (form)
13385 {
13386 case DW_FORM_addr:
13387 return "DW_FORM_addr";
13388 case DW_FORM_block2:
13389 return "DW_FORM_block2";
13390 case DW_FORM_block4:
13391 return "DW_FORM_block4";
13392 case DW_FORM_data2:
13393 return "DW_FORM_data2";
13394 case DW_FORM_data4:
13395 return "DW_FORM_data4";
13396 case DW_FORM_data8:
13397 return "DW_FORM_data8";
13398 case DW_FORM_string:
13399 return "DW_FORM_string";
13400 case DW_FORM_block:
13401 return "DW_FORM_block";
13402 case DW_FORM_block1:
13403 return "DW_FORM_block1";
13404 case DW_FORM_data1:
13405 return "DW_FORM_data1";
13406 case DW_FORM_flag:
13407 return "DW_FORM_flag";
13408 case DW_FORM_sdata:
13409 return "DW_FORM_sdata";
13410 case DW_FORM_strp:
13411 return "DW_FORM_strp";
13412 case DW_FORM_udata:
13413 return "DW_FORM_udata";
13414 case DW_FORM_ref_addr:
13415 return "DW_FORM_ref_addr";
13416 case DW_FORM_ref1:
13417 return "DW_FORM_ref1";
13418 case DW_FORM_ref2:
13419 return "DW_FORM_ref2";
13420 case DW_FORM_ref4:
13421 return "DW_FORM_ref4";
13422 case DW_FORM_ref8:
13423 return "DW_FORM_ref8";
13424 case DW_FORM_ref_udata:
13425 return "DW_FORM_ref_udata";
13426 case DW_FORM_indirect:
13427 return "DW_FORM_indirect";
13428 case DW_FORM_sec_offset:
13429 return "DW_FORM_sec_offset";
13430 case DW_FORM_exprloc:
13431 return "DW_FORM_exprloc";
13432 case DW_FORM_flag_present:
13433 return "DW_FORM_flag_present";
13434 case DW_FORM_ref_sig8:
13435 return "DW_FORM_ref_sig8";
13436 default:
13437 return "DW_FORM_<unknown>";
13438 }
13439 }
13440
13441 /* Convert a DWARF stack opcode into its string name. */
13442
13443 const char *
13444 dwarf_stack_op_name (unsigned op)
13445 {
13446 switch (op)
13447 {
13448 case DW_OP_addr:
13449 return "DW_OP_addr";
13450 case DW_OP_deref:
13451 return "DW_OP_deref";
13452 case DW_OP_const1u:
13453 return "DW_OP_const1u";
13454 case DW_OP_const1s:
13455 return "DW_OP_const1s";
13456 case DW_OP_const2u:
13457 return "DW_OP_const2u";
13458 case DW_OP_const2s:
13459 return "DW_OP_const2s";
13460 case DW_OP_const4u:
13461 return "DW_OP_const4u";
13462 case DW_OP_const4s:
13463 return "DW_OP_const4s";
13464 case DW_OP_const8u:
13465 return "DW_OP_const8u";
13466 case DW_OP_const8s:
13467 return "DW_OP_const8s";
13468 case DW_OP_constu:
13469 return "DW_OP_constu";
13470 case DW_OP_consts:
13471 return "DW_OP_consts";
13472 case DW_OP_dup:
13473 return "DW_OP_dup";
13474 case DW_OP_drop:
13475 return "DW_OP_drop";
13476 case DW_OP_over:
13477 return "DW_OP_over";
13478 case DW_OP_pick:
13479 return "DW_OP_pick";
13480 case DW_OP_swap:
13481 return "DW_OP_swap";
13482 case DW_OP_rot:
13483 return "DW_OP_rot";
13484 case DW_OP_xderef:
13485 return "DW_OP_xderef";
13486 case DW_OP_abs:
13487 return "DW_OP_abs";
13488 case DW_OP_and:
13489 return "DW_OP_and";
13490 case DW_OP_div:
13491 return "DW_OP_div";
13492 case DW_OP_minus:
13493 return "DW_OP_minus";
13494 case DW_OP_mod:
13495 return "DW_OP_mod";
13496 case DW_OP_mul:
13497 return "DW_OP_mul";
13498 case DW_OP_neg:
13499 return "DW_OP_neg";
13500 case DW_OP_not:
13501 return "DW_OP_not";
13502 case DW_OP_or:
13503 return "DW_OP_or";
13504 case DW_OP_plus:
13505 return "DW_OP_plus";
13506 case DW_OP_plus_uconst:
13507 return "DW_OP_plus_uconst";
13508 case DW_OP_shl:
13509 return "DW_OP_shl";
13510 case DW_OP_shr:
13511 return "DW_OP_shr";
13512 case DW_OP_shra:
13513 return "DW_OP_shra";
13514 case DW_OP_xor:
13515 return "DW_OP_xor";
13516 case DW_OP_bra:
13517 return "DW_OP_bra";
13518 case DW_OP_eq:
13519 return "DW_OP_eq";
13520 case DW_OP_ge:
13521 return "DW_OP_ge";
13522 case DW_OP_gt:
13523 return "DW_OP_gt";
13524 case DW_OP_le:
13525 return "DW_OP_le";
13526 case DW_OP_lt:
13527 return "DW_OP_lt";
13528 case DW_OP_ne:
13529 return "DW_OP_ne";
13530 case DW_OP_skip:
13531 return "DW_OP_skip";
13532 case DW_OP_lit0:
13533 return "DW_OP_lit0";
13534 case DW_OP_lit1:
13535 return "DW_OP_lit1";
13536 case DW_OP_lit2:
13537 return "DW_OP_lit2";
13538 case DW_OP_lit3:
13539 return "DW_OP_lit3";
13540 case DW_OP_lit4:
13541 return "DW_OP_lit4";
13542 case DW_OP_lit5:
13543 return "DW_OP_lit5";
13544 case DW_OP_lit6:
13545 return "DW_OP_lit6";
13546 case DW_OP_lit7:
13547 return "DW_OP_lit7";
13548 case DW_OP_lit8:
13549 return "DW_OP_lit8";
13550 case DW_OP_lit9:
13551 return "DW_OP_lit9";
13552 case DW_OP_lit10:
13553 return "DW_OP_lit10";
13554 case DW_OP_lit11:
13555 return "DW_OP_lit11";
13556 case DW_OP_lit12:
13557 return "DW_OP_lit12";
13558 case DW_OP_lit13:
13559 return "DW_OP_lit13";
13560 case DW_OP_lit14:
13561 return "DW_OP_lit14";
13562 case DW_OP_lit15:
13563 return "DW_OP_lit15";
13564 case DW_OP_lit16:
13565 return "DW_OP_lit16";
13566 case DW_OP_lit17:
13567 return "DW_OP_lit17";
13568 case DW_OP_lit18:
13569 return "DW_OP_lit18";
13570 case DW_OP_lit19:
13571 return "DW_OP_lit19";
13572 case DW_OP_lit20:
13573 return "DW_OP_lit20";
13574 case DW_OP_lit21:
13575 return "DW_OP_lit21";
13576 case DW_OP_lit22:
13577 return "DW_OP_lit22";
13578 case DW_OP_lit23:
13579 return "DW_OP_lit23";
13580 case DW_OP_lit24:
13581 return "DW_OP_lit24";
13582 case DW_OP_lit25:
13583 return "DW_OP_lit25";
13584 case DW_OP_lit26:
13585 return "DW_OP_lit26";
13586 case DW_OP_lit27:
13587 return "DW_OP_lit27";
13588 case DW_OP_lit28:
13589 return "DW_OP_lit28";
13590 case DW_OP_lit29:
13591 return "DW_OP_lit29";
13592 case DW_OP_lit30:
13593 return "DW_OP_lit30";
13594 case DW_OP_lit31:
13595 return "DW_OP_lit31";
13596 case DW_OP_reg0:
13597 return "DW_OP_reg0";
13598 case DW_OP_reg1:
13599 return "DW_OP_reg1";
13600 case DW_OP_reg2:
13601 return "DW_OP_reg2";
13602 case DW_OP_reg3:
13603 return "DW_OP_reg3";
13604 case DW_OP_reg4:
13605 return "DW_OP_reg4";
13606 case DW_OP_reg5:
13607 return "DW_OP_reg5";
13608 case DW_OP_reg6:
13609 return "DW_OP_reg6";
13610 case DW_OP_reg7:
13611 return "DW_OP_reg7";
13612 case DW_OP_reg8:
13613 return "DW_OP_reg8";
13614 case DW_OP_reg9:
13615 return "DW_OP_reg9";
13616 case DW_OP_reg10:
13617 return "DW_OP_reg10";
13618 case DW_OP_reg11:
13619 return "DW_OP_reg11";
13620 case DW_OP_reg12:
13621 return "DW_OP_reg12";
13622 case DW_OP_reg13:
13623 return "DW_OP_reg13";
13624 case DW_OP_reg14:
13625 return "DW_OP_reg14";
13626 case DW_OP_reg15:
13627 return "DW_OP_reg15";
13628 case DW_OP_reg16:
13629 return "DW_OP_reg16";
13630 case DW_OP_reg17:
13631 return "DW_OP_reg17";
13632 case DW_OP_reg18:
13633 return "DW_OP_reg18";
13634 case DW_OP_reg19:
13635 return "DW_OP_reg19";
13636 case DW_OP_reg20:
13637 return "DW_OP_reg20";
13638 case DW_OP_reg21:
13639 return "DW_OP_reg21";
13640 case DW_OP_reg22:
13641 return "DW_OP_reg22";
13642 case DW_OP_reg23:
13643 return "DW_OP_reg23";
13644 case DW_OP_reg24:
13645 return "DW_OP_reg24";
13646 case DW_OP_reg25:
13647 return "DW_OP_reg25";
13648 case DW_OP_reg26:
13649 return "DW_OP_reg26";
13650 case DW_OP_reg27:
13651 return "DW_OP_reg27";
13652 case DW_OP_reg28:
13653 return "DW_OP_reg28";
13654 case DW_OP_reg29:
13655 return "DW_OP_reg29";
13656 case DW_OP_reg30:
13657 return "DW_OP_reg30";
13658 case DW_OP_reg31:
13659 return "DW_OP_reg31";
13660 case DW_OP_breg0:
13661 return "DW_OP_breg0";
13662 case DW_OP_breg1:
13663 return "DW_OP_breg1";
13664 case DW_OP_breg2:
13665 return "DW_OP_breg2";
13666 case DW_OP_breg3:
13667 return "DW_OP_breg3";
13668 case DW_OP_breg4:
13669 return "DW_OP_breg4";
13670 case DW_OP_breg5:
13671 return "DW_OP_breg5";
13672 case DW_OP_breg6:
13673 return "DW_OP_breg6";
13674 case DW_OP_breg7:
13675 return "DW_OP_breg7";
13676 case DW_OP_breg8:
13677 return "DW_OP_breg8";
13678 case DW_OP_breg9:
13679 return "DW_OP_breg9";
13680 case DW_OP_breg10:
13681 return "DW_OP_breg10";
13682 case DW_OP_breg11:
13683 return "DW_OP_breg11";
13684 case DW_OP_breg12:
13685 return "DW_OP_breg12";
13686 case DW_OP_breg13:
13687 return "DW_OP_breg13";
13688 case DW_OP_breg14:
13689 return "DW_OP_breg14";
13690 case DW_OP_breg15:
13691 return "DW_OP_breg15";
13692 case DW_OP_breg16:
13693 return "DW_OP_breg16";
13694 case DW_OP_breg17:
13695 return "DW_OP_breg17";
13696 case DW_OP_breg18:
13697 return "DW_OP_breg18";
13698 case DW_OP_breg19:
13699 return "DW_OP_breg19";
13700 case DW_OP_breg20:
13701 return "DW_OP_breg20";
13702 case DW_OP_breg21:
13703 return "DW_OP_breg21";
13704 case DW_OP_breg22:
13705 return "DW_OP_breg22";
13706 case DW_OP_breg23:
13707 return "DW_OP_breg23";
13708 case DW_OP_breg24:
13709 return "DW_OP_breg24";
13710 case DW_OP_breg25:
13711 return "DW_OP_breg25";
13712 case DW_OP_breg26:
13713 return "DW_OP_breg26";
13714 case DW_OP_breg27:
13715 return "DW_OP_breg27";
13716 case DW_OP_breg28:
13717 return "DW_OP_breg28";
13718 case DW_OP_breg29:
13719 return "DW_OP_breg29";
13720 case DW_OP_breg30:
13721 return "DW_OP_breg30";
13722 case DW_OP_breg31:
13723 return "DW_OP_breg31";
13724 case DW_OP_regx:
13725 return "DW_OP_regx";
13726 case DW_OP_fbreg:
13727 return "DW_OP_fbreg";
13728 case DW_OP_bregx:
13729 return "DW_OP_bregx";
13730 case DW_OP_piece:
13731 return "DW_OP_piece";
13732 case DW_OP_deref_size:
13733 return "DW_OP_deref_size";
13734 case DW_OP_xderef_size:
13735 return "DW_OP_xderef_size";
13736 case DW_OP_nop:
13737 return "DW_OP_nop";
13738 /* DWARF 3 extensions. */
13739 case DW_OP_push_object_address:
13740 return "DW_OP_push_object_address";
13741 case DW_OP_call2:
13742 return "DW_OP_call2";
13743 case DW_OP_call4:
13744 return "DW_OP_call4";
13745 case DW_OP_call_ref:
13746 return "DW_OP_call_ref";
13747 case DW_OP_form_tls_address:
13748 return "DW_OP_form_tls_address";
13749 case DW_OP_call_frame_cfa:
13750 return "DW_OP_call_frame_cfa";
13751 case DW_OP_bit_piece:
13752 return "DW_OP_bit_piece";
13753 /* DWARF 4 extensions. */
13754 case DW_OP_implicit_value:
13755 return "DW_OP_implicit_value";
13756 case DW_OP_stack_value:
13757 return "DW_OP_stack_value";
13758 /* GNU extensions. */
13759 case DW_OP_GNU_push_tls_address:
13760 return "DW_OP_GNU_push_tls_address";
13761 case DW_OP_GNU_uninit:
13762 return "DW_OP_GNU_uninit";
13763 case DW_OP_GNU_implicit_pointer:
13764 return "DW_OP_GNU_implicit_pointer";
13765 case DW_OP_GNU_entry_value:
13766 return "DW_OP_GNU_entry_value";
13767 case DW_OP_GNU_const_type:
13768 return "DW_OP_GNU_const_type";
13769 case DW_OP_GNU_regval_type:
13770 return "DW_OP_GNU_regval_type";
13771 case DW_OP_GNU_deref_type:
13772 return "DW_OP_GNU_deref_type";
13773 case DW_OP_GNU_convert:
13774 return "DW_OP_GNU_convert";
13775 case DW_OP_GNU_reinterpret:
13776 return "DW_OP_GNU_reinterpret";
13777 default:
13778 return NULL;
13779 }
13780 }
13781
13782 static char *
13783 dwarf_bool_name (unsigned mybool)
13784 {
13785 if (mybool)
13786 return "TRUE";
13787 else
13788 return "FALSE";
13789 }
13790
13791 /* Convert a DWARF type code into its string name. */
13792
13793 static char *
13794 dwarf_type_encoding_name (unsigned enc)
13795 {
13796 switch (enc)
13797 {
13798 case DW_ATE_void:
13799 return "DW_ATE_void";
13800 case DW_ATE_address:
13801 return "DW_ATE_address";
13802 case DW_ATE_boolean:
13803 return "DW_ATE_boolean";
13804 case DW_ATE_complex_float:
13805 return "DW_ATE_complex_float";
13806 case DW_ATE_float:
13807 return "DW_ATE_float";
13808 case DW_ATE_signed:
13809 return "DW_ATE_signed";
13810 case DW_ATE_signed_char:
13811 return "DW_ATE_signed_char";
13812 case DW_ATE_unsigned:
13813 return "DW_ATE_unsigned";
13814 case DW_ATE_unsigned_char:
13815 return "DW_ATE_unsigned_char";
13816 /* DWARF 3. */
13817 case DW_ATE_imaginary_float:
13818 return "DW_ATE_imaginary_float";
13819 case DW_ATE_packed_decimal:
13820 return "DW_ATE_packed_decimal";
13821 case DW_ATE_numeric_string:
13822 return "DW_ATE_numeric_string";
13823 case DW_ATE_edited:
13824 return "DW_ATE_edited";
13825 case DW_ATE_signed_fixed:
13826 return "DW_ATE_signed_fixed";
13827 case DW_ATE_unsigned_fixed:
13828 return "DW_ATE_unsigned_fixed";
13829 case DW_ATE_decimal_float:
13830 return "DW_ATE_decimal_float";
13831 /* DWARF 4. */
13832 case DW_ATE_UTF:
13833 return "DW_ATE_UTF";
13834 /* HP extensions. */
13835 case DW_ATE_HP_float80:
13836 return "DW_ATE_HP_float80";
13837 case DW_ATE_HP_complex_float80:
13838 return "DW_ATE_HP_complex_float80";
13839 case DW_ATE_HP_float128:
13840 return "DW_ATE_HP_float128";
13841 case DW_ATE_HP_complex_float128:
13842 return "DW_ATE_HP_complex_float128";
13843 case DW_ATE_HP_floathpintel:
13844 return "DW_ATE_HP_floathpintel";
13845 case DW_ATE_HP_imaginary_float80:
13846 return "DW_ATE_HP_imaginary_float80";
13847 case DW_ATE_HP_imaginary_float128:
13848 return "DW_ATE_HP_imaginary_float128";
13849 default:
13850 return "DW_ATE_<unknown>";
13851 }
13852 }
13853
13854 /* Convert a DWARF call frame info operation to its string name. */
13855
13856 #if 0
13857 static char *
13858 dwarf_cfi_name (unsigned cfi_opc)
13859 {
13860 switch (cfi_opc)
13861 {
13862 case DW_CFA_advance_loc:
13863 return "DW_CFA_advance_loc";
13864 case DW_CFA_offset:
13865 return "DW_CFA_offset";
13866 case DW_CFA_restore:
13867 return "DW_CFA_restore";
13868 case DW_CFA_nop:
13869 return "DW_CFA_nop";
13870 case DW_CFA_set_loc:
13871 return "DW_CFA_set_loc";
13872 case DW_CFA_advance_loc1:
13873 return "DW_CFA_advance_loc1";
13874 case DW_CFA_advance_loc2:
13875 return "DW_CFA_advance_loc2";
13876 case DW_CFA_advance_loc4:
13877 return "DW_CFA_advance_loc4";
13878 case DW_CFA_offset_extended:
13879 return "DW_CFA_offset_extended";
13880 case DW_CFA_restore_extended:
13881 return "DW_CFA_restore_extended";
13882 case DW_CFA_undefined:
13883 return "DW_CFA_undefined";
13884 case DW_CFA_same_value:
13885 return "DW_CFA_same_value";
13886 case DW_CFA_register:
13887 return "DW_CFA_register";
13888 case DW_CFA_remember_state:
13889 return "DW_CFA_remember_state";
13890 case DW_CFA_restore_state:
13891 return "DW_CFA_restore_state";
13892 case DW_CFA_def_cfa:
13893 return "DW_CFA_def_cfa";
13894 case DW_CFA_def_cfa_register:
13895 return "DW_CFA_def_cfa_register";
13896 case DW_CFA_def_cfa_offset:
13897 return "DW_CFA_def_cfa_offset";
13898 /* DWARF 3. */
13899 case DW_CFA_def_cfa_expression:
13900 return "DW_CFA_def_cfa_expression";
13901 case DW_CFA_expression:
13902 return "DW_CFA_expression";
13903 case DW_CFA_offset_extended_sf:
13904 return "DW_CFA_offset_extended_sf";
13905 case DW_CFA_def_cfa_sf:
13906 return "DW_CFA_def_cfa_sf";
13907 case DW_CFA_def_cfa_offset_sf:
13908 return "DW_CFA_def_cfa_offset_sf";
13909 case DW_CFA_val_offset:
13910 return "DW_CFA_val_offset";
13911 case DW_CFA_val_offset_sf:
13912 return "DW_CFA_val_offset_sf";
13913 case DW_CFA_val_expression:
13914 return "DW_CFA_val_expression";
13915 /* SGI/MIPS specific. */
13916 case DW_CFA_MIPS_advance_loc8:
13917 return "DW_CFA_MIPS_advance_loc8";
13918 /* GNU extensions. */
13919 case DW_CFA_GNU_window_save:
13920 return "DW_CFA_GNU_window_save";
13921 case DW_CFA_GNU_args_size:
13922 return "DW_CFA_GNU_args_size";
13923 case DW_CFA_GNU_negative_offset_extended:
13924 return "DW_CFA_GNU_negative_offset_extended";
13925 default:
13926 return "DW_CFA_<unknown>";
13927 }
13928 }
13929 #endif
13930
13931 static void
13932 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13933 {
13934 unsigned int i;
13935
13936 print_spaces (indent, f);
13937 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13938 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13939
13940 if (die->parent != NULL)
13941 {
13942 print_spaces (indent, f);
13943 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13944 die->parent->offset);
13945 }
13946
13947 print_spaces (indent, f);
13948 fprintf_unfiltered (f, " has children: %s\n",
13949 dwarf_bool_name (die->child != NULL));
13950
13951 print_spaces (indent, f);
13952 fprintf_unfiltered (f, " attributes:\n");
13953
13954 for (i = 0; i < die->num_attrs; ++i)
13955 {
13956 print_spaces (indent, f);
13957 fprintf_unfiltered (f, " %s (%s) ",
13958 dwarf_attr_name (die->attrs[i].name),
13959 dwarf_form_name (die->attrs[i].form));
13960
13961 switch (die->attrs[i].form)
13962 {
13963 case DW_FORM_ref_addr:
13964 case DW_FORM_addr:
13965 fprintf_unfiltered (f, "address: ");
13966 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13967 break;
13968 case DW_FORM_block2:
13969 case DW_FORM_block4:
13970 case DW_FORM_block:
13971 case DW_FORM_block1:
13972 fprintf_unfiltered (f, "block: size %d",
13973 DW_BLOCK (&die->attrs[i])->size);
13974 break;
13975 case DW_FORM_exprloc:
13976 fprintf_unfiltered (f, "expression: size %u",
13977 DW_BLOCK (&die->attrs[i])->size);
13978 break;
13979 case DW_FORM_ref1:
13980 case DW_FORM_ref2:
13981 case DW_FORM_ref4:
13982 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13983 (long) (DW_ADDR (&die->attrs[i])));
13984 break;
13985 case DW_FORM_data1:
13986 case DW_FORM_data2:
13987 case DW_FORM_data4:
13988 case DW_FORM_data8:
13989 case DW_FORM_udata:
13990 case DW_FORM_sdata:
13991 fprintf_unfiltered (f, "constant: %s",
13992 pulongest (DW_UNSND (&die->attrs[i])));
13993 break;
13994 case DW_FORM_sec_offset:
13995 fprintf_unfiltered (f, "section offset: %s",
13996 pulongest (DW_UNSND (&die->attrs[i])));
13997 break;
13998 case DW_FORM_ref_sig8:
13999 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14000 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14001 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
14002 else
14003 fprintf_unfiltered (f, "signatured type, offset: unknown");
14004 break;
14005 case DW_FORM_string:
14006 case DW_FORM_strp:
14007 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14008 DW_STRING (&die->attrs[i])
14009 ? DW_STRING (&die->attrs[i]) : "",
14010 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14011 break;
14012 case DW_FORM_flag:
14013 if (DW_UNSND (&die->attrs[i]))
14014 fprintf_unfiltered (f, "flag: TRUE");
14015 else
14016 fprintf_unfiltered (f, "flag: FALSE");
14017 break;
14018 case DW_FORM_flag_present:
14019 fprintf_unfiltered (f, "flag: TRUE");
14020 break;
14021 case DW_FORM_indirect:
14022 /* The reader will have reduced the indirect form to
14023 the "base form" so this form should not occur. */
14024 fprintf_unfiltered (f,
14025 "unexpected attribute form: DW_FORM_indirect");
14026 break;
14027 default:
14028 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14029 die->attrs[i].form);
14030 break;
14031 }
14032 fprintf_unfiltered (f, "\n");
14033 }
14034 }
14035
14036 static void
14037 dump_die_for_error (struct die_info *die)
14038 {
14039 dump_die_shallow (gdb_stderr, 0, die);
14040 }
14041
14042 static void
14043 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14044 {
14045 int indent = level * 4;
14046
14047 gdb_assert (die != NULL);
14048
14049 if (level >= max_level)
14050 return;
14051
14052 dump_die_shallow (f, indent, die);
14053
14054 if (die->child != NULL)
14055 {
14056 print_spaces (indent, f);
14057 fprintf_unfiltered (f, " Children:");
14058 if (level + 1 < max_level)
14059 {
14060 fprintf_unfiltered (f, "\n");
14061 dump_die_1 (f, level + 1, max_level, die->child);
14062 }
14063 else
14064 {
14065 fprintf_unfiltered (f,
14066 " [not printed, max nesting level reached]\n");
14067 }
14068 }
14069
14070 if (die->sibling != NULL && level > 0)
14071 {
14072 dump_die_1 (f, level, max_level, die->sibling);
14073 }
14074 }
14075
14076 /* This is called from the pdie macro in gdbinit.in.
14077 It's not static so gcc will keep a copy callable from gdb. */
14078
14079 void
14080 dump_die (struct die_info *die, int max_level)
14081 {
14082 dump_die_1 (gdb_stdlog, 0, max_level, die);
14083 }
14084
14085 static void
14086 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14087 {
14088 void **slot;
14089
14090 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14091
14092 *slot = die;
14093 }
14094
14095 static int
14096 is_ref_attr (struct attribute *attr)
14097 {
14098 switch (attr->form)
14099 {
14100 case DW_FORM_ref_addr:
14101 case DW_FORM_ref1:
14102 case DW_FORM_ref2:
14103 case DW_FORM_ref4:
14104 case DW_FORM_ref8:
14105 case DW_FORM_ref_udata:
14106 return 1;
14107 default:
14108 return 0;
14109 }
14110 }
14111
14112 static unsigned int
14113 dwarf2_get_ref_die_offset (struct attribute *attr)
14114 {
14115 if (is_ref_attr (attr))
14116 return DW_ADDR (attr);
14117
14118 complaint (&symfile_complaints,
14119 _("unsupported die ref attribute form: '%s'"),
14120 dwarf_form_name (attr->form));
14121 return 0;
14122 }
14123
14124 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14125 * the value held by the attribute is not constant. */
14126
14127 static LONGEST
14128 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14129 {
14130 if (attr->form == DW_FORM_sdata)
14131 return DW_SND (attr);
14132 else if (attr->form == DW_FORM_udata
14133 || attr->form == DW_FORM_data1
14134 || attr->form == DW_FORM_data2
14135 || attr->form == DW_FORM_data4
14136 || attr->form == DW_FORM_data8)
14137 return DW_UNSND (attr);
14138 else
14139 {
14140 complaint (&symfile_complaints,
14141 _("Attribute value is not a constant (%s)"),
14142 dwarf_form_name (attr->form));
14143 return default_value;
14144 }
14145 }
14146
14147 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14148 unit and add it to our queue.
14149 The result is non-zero if PER_CU was queued, otherwise the result is zero
14150 meaning either PER_CU is already queued or it is already loaded. */
14151
14152 static int
14153 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14154 struct dwarf2_per_cu_data *per_cu)
14155 {
14156 /* We may arrive here during partial symbol reading, if we need full
14157 DIEs to process an unusual case (e.g. template arguments). Do
14158 not queue PER_CU, just tell our caller to load its DIEs. */
14159 if (dwarf2_per_objfile->reading_partial_symbols)
14160 {
14161 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14162 return 1;
14163 return 0;
14164 }
14165
14166 /* Mark the dependence relation so that we don't flush PER_CU
14167 too early. */
14168 dwarf2_add_dependence (this_cu, per_cu);
14169
14170 /* If it's already on the queue, we have nothing to do. */
14171 if (per_cu->queued)
14172 return 0;
14173
14174 /* If the compilation unit is already loaded, just mark it as
14175 used. */
14176 if (per_cu->cu != NULL)
14177 {
14178 per_cu->cu->last_used = 0;
14179 return 0;
14180 }
14181
14182 /* Add it to the queue. */
14183 queue_comp_unit (per_cu, this_cu->objfile);
14184
14185 return 1;
14186 }
14187
14188 /* Follow reference or signature attribute ATTR of SRC_DIE.
14189 On entry *REF_CU is the CU of SRC_DIE.
14190 On exit *REF_CU is the CU of the result. */
14191
14192 static struct die_info *
14193 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14194 struct dwarf2_cu **ref_cu)
14195 {
14196 struct die_info *die;
14197
14198 if (is_ref_attr (attr))
14199 die = follow_die_ref (src_die, attr, ref_cu);
14200 else if (attr->form == DW_FORM_ref_sig8)
14201 die = follow_die_sig (src_die, attr, ref_cu);
14202 else
14203 {
14204 dump_die_for_error (src_die);
14205 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14206 (*ref_cu)->objfile->name);
14207 }
14208
14209 return die;
14210 }
14211
14212 /* Follow reference OFFSET.
14213 On entry *REF_CU is the CU of the source die referencing OFFSET.
14214 On exit *REF_CU is the CU of the result.
14215 Returns NULL if OFFSET is invalid. */
14216
14217 static struct die_info *
14218 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14219 {
14220 struct die_info temp_die;
14221 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14222
14223 gdb_assert (cu->per_cu != NULL);
14224
14225 target_cu = cu;
14226
14227 if (cu->per_cu->debug_type_section)
14228 {
14229 /* .debug_types CUs cannot reference anything outside their CU.
14230 If they need to, they have to reference a signatured type via
14231 DW_FORM_ref_sig8. */
14232 if (! offset_in_cu_p (&cu->header, offset))
14233 return NULL;
14234 }
14235 else if (! offset_in_cu_p (&cu->header, offset))
14236 {
14237 struct dwarf2_per_cu_data *per_cu;
14238
14239 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14240
14241 /* If necessary, add it to the queue and load its DIEs. */
14242 if (maybe_queue_comp_unit (cu, per_cu))
14243 load_full_comp_unit (per_cu, cu->objfile);
14244
14245 target_cu = per_cu->cu;
14246 }
14247 else if (cu->dies == NULL)
14248 {
14249 /* We're loading full DIEs during partial symbol reading. */
14250 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14251 load_full_comp_unit (cu->per_cu, cu->objfile);
14252 }
14253
14254 *ref_cu = target_cu;
14255 temp_die.offset = offset;
14256 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14257 }
14258
14259 /* Follow reference attribute ATTR of SRC_DIE.
14260 On entry *REF_CU is the CU of SRC_DIE.
14261 On exit *REF_CU is the CU of the result. */
14262
14263 static struct die_info *
14264 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14265 struct dwarf2_cu **ref_cu)
14266 {
14267 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14268 struct dwarf2_cu *cu = *ref_cu;
14269 struct die_info *die;
14270
14271 die = follow_die_offset (offset, ref_cu);
14272 if (!die)
14273 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14274 "at 0x%x [in module %s]"),
14275 offset, src_die->offset, cu->objfile->name);
14276
14277 return die;
14278 }
14279
14280 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14281 Returned value is intended for DW_OP_call*. Returned
14282 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14283
14284 struct dwarf2_locexpr_baton
14285 dwarf2_fetch_die_location_block (unsigned int offset,
14286 struct dwarf2_per_cu_data *per_cu,
14287 CORE_ADDR (*get_frame_pc) (void *baton),
14288 void *baton)
14289 {
14290 struct dwarf2_cu *cu;
14291 struct die_info *die;
14292 struct attribute *attr;
14293 struct dwarf2_locexpr_baton retval;
14294
14295 dw2_setup (per_cu->objfile);
14296
14297 if (per_cu->cu == NULL)
14298 load_cu (per_cu);
14299 cu = per_cu->cu;
14300
14301 die = follow_die_offset (offset, &cu);
14302 if (!die)
14303 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14304 offset, per_cu->cu->objfile->name);
14305
14306 attr = dwarf2_attr (die, DW_AT_location, cu);
14307 if (!attr)
14308 {
14309 /* DWARF: "If there is no such attribute, then there is no effect.".
14310 DATA is ignored if SIZE is 0. */
14311
14312 retval.data = NULL;
14313 retval.size = 0;
14314 }
14315 else if (attr_form_is_section_offset (attr))
14316 {
14317 struct dwarf2_loclist_baton loclist_baton;
14318 CORE_ADDR pc = (*get_frame_pc) (baton);
14319 size_t size;
14320
14321 fill_in_loclist_baton (cu, &loclist_baton, attr);
14322
14323 retval.data = dwarf2_find_location_expression (&loclist_baton,
14324 &size, pc);
14325 retval.size = size;
14326 }
14327 else
14328 {
14329 if (!attr_form_is_block (attr))
14330 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14331 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14332 offset, per_cu->cu->objfile->name);
14333
14334 retval.data = DW_BLOCK (attr)->data;
14335 retval.size = DW_BLOCK (attr)->size;
14336 }
14337 retval.per_cu = cu->per_cu;
14338
14339 age_cached_comp_units ();
14340
14341 return retval;
14342 }
14343
14344 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14345 PER_CU. */
14346
14347 struct type *
14348 dwarf2_get_die_type (unsigned int die_offset,
14349 struct dwarf2_per_cu_data *per_cu)
14350 {
14351 dw2_setup (per_cu->objfile);
14352 return get_die_type_at_offset (die_offset, per_cu);
14353 }
14354
14355 /* Follow the signature attribute ATTR in SRC_DIE.
14356 On entry *REF_CU is the CU of SRC_DIE.
14357 On exit *REF_CU is the CU of the result. */
14358
14359 static struct die_info *
14360 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14361 struct dwarf2_cu **ref_cu)
14362 {
14363 struct objfile *objfile = (*ref_cu)->objfile;
14364 struct die_info temp_die;
14365 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14366 struct dwarf2_cu *sig_cu;
14367 struct die_info *die;
14368
14369 /* sig_type will be NULL if the signatured type is missing from
14370 the debug info. */
14371 if (sig_type == NULL)
14372 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14373 "at 0x%x [in module %s]"),
14374 src_die->offset, objfile->name);
14375
14376 /* If necessary, add it to the queue and load its DIEs. */
14377
14378 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14379 read_signatured_type (objfile, sig_type);
14380
14381 gdb_assert (sig_type->per_cu.cu != NULL);
14382
14383 sig_cu = sig_type->per_cu.cu;
14384 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14385 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14386 if (die)
14387 {
14388 *ref_cu = sig_cu;
14389 return die;
14390 }
14391
14392 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14393 "from DIE at 0x%x [in module %s]"),
14394 sig_type->type_offset, src_die->offset, objfile->name);
14395 }
14396
14397 /* Given an offset of a signatured type, return its signatured_type. */
14398
14399 static struct signatured_type *
14400 lookup_signatured_type_at_offset (struct objfile *objfile,
14401 struct dwarf2_section_info *section,
14402 unsigned int offset)
14403 {
14404 gdb_byte *info_ptr = section->buffer + offset;
14405 unsigned int length, initial_length_size;
14406 unsigned int sig_offset;
14407 struct signatured_type find_entry, *type_sig;
14408
14409 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14410 sig_offset = (initial_length_size
14411 + 2 /*version*/
14412 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14413 + 1 /*address_size*/);
14414 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14415 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14416
14417 /* This is only used to lookup previously recorded types.
14418 If we didn't find it, it's our bug. */
14419 gdb_assert (type_sig != NULL);
14420 gdb_assert (offset == type_sig->per_cu.offset);
14421
14422 return type_sig;
14423 }
14424
14425 /* Read in signatured type at OFFSET and build its CU and die(s). */
14426
14427 static void
14428 read_signatured_type_at_offset (struct objfile *objfile,
14429 struct dwarf2_section_info *sect,
14430 unsigned int offset)
14431 {
14432 struct signatured_type *type_sig;
14433
14434 dwarf2_read_section (objfile, sect);
14435
14436 /* We have the section offset, but we need the signature to do the
14437 hash table lookup. */
14438 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14439
14440 gdb_assert (type_sig->per_cu.cu == NULL);
14441
14442 read_signatured_type (objfile, type_sig);
14443
14444 gdb_assert (type_sig->per_cu.cu != NULL);
14445 }
14446
14447 /* Read in a signatured type and build its CU and DIEs. */
14448
14449 static void
14450 read_signatured_type (struct objfile *objfile,
14451 struct signatured_type *type_sig)
14452 {
14453 gdb_byte *types_ptr;
14454 struct die_reader_specs reader_specs;
14455 struct dwarf2_cu *cu;
14456 ULONGEST signature;
14457 struct cleanup *back_to, *free_cu_cleanup;
14458 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
14459
14460 dwarf2_read_section (objfile, section);
14461 types_ptr = section->buffer + type_sig->per_cu.offset;
14462
14463 gdb_assert (type_sig->per_cu.cu == NULL);
14464
14465 cu = xmalloc (sizeof (*cu));
14466 init_one_comp_unit (cu, objfile);
14467
14468 type_sig->per_cu.cu = cu;
14469 cu->per_cu = &type_sig->per_cu;
14470
14471 /* If an error occurs while loading, release our storage. */
14472 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14473
14474 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14475 types_ptr, objfile->obfd);
14476 gdb_assert (signature == type_sig->signature);
14477
14478 cu->die_hash
14479 = htab_create_alloc_ex (cu->header.length / 12,
14480 die_hash,
14481 die_eq,
14482 NULL,
14483 &cu->comp_unit_obstack,
14484 hashtab_obstack_allocate,
14485 dummy_obstack_deallocate);
14486
14487 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14488 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14489
14490 init_cu_die_reader (&reader_specs, cu);
14491
14492 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14493 NULL /*parent*/);
14494
14495 /* We try not to read any attributes in this function, because not
14496 all objfiles needed for references have been loaded yet, and symbol
14497 table processing isn't initialized. But we have to set the CU language,
14498 or we won't be able to build types correctly. */
14499 prepare_one_comp_unit (cu, cu->dies);
14500
14501 do_cleanups (back_to);
14502
14503 /* We've successfully allocated this compilation unit. Let our caller
14504 clean it up when finished with it. */
14505 discard_cleanups (free_cu_cleanup);
14506
14507 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14508 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14509 }
14510
14511 /* Decode simple location descriptions.
14512 Given a pointer to a dwarf block that defines a location, compute
14513 the location and return the value.
14514
14515 NOTE drow/2003-11-18: This function is called in two situations
14516 now: for the address of static or global variables (partial symbols
14517 only) and for offsets into structures which are expected to be
14518 (more or less) constant. The partial symbol case should go away,
14519 and only the constant case should remain. That will let this
14520 function complain more accurately. A few special modes are allowed
14521 without complaint for global variables (for instance, global
14522 register values and thread-local values).
14523
14524 A location description containing no operations indicates that the
14525 object is optimized out. The return value is 0 for that case.
14526 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14527 callers will only want a very basic result and this can become a
14528 complaint.
14529
14530 Note that stack[0] is unused except as a default error return. */
14531
14532 static CORE_ADDR
14533 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14534 {
14535 struct objfile *objfile = cu->objfile;
14536 int i;
14537 int size = blk->size;
14538 gdb_byte *data = blk->data;
14539 CORE_ADDR stack[64];
14540 int stacki;
14541 unsigned int bytes_read, unsnd;
14542 gdb_byte op;
14543
14544 i = 0;
14545 stacki = 0;
14546 stack[stacki] = 0;
14547 stack[++stacki] = 0;
14548
14549 while (i < size)
14550 {
14551 op = data[i++];
14552 switch (op)
14553 {
14554 case DW_OP_lit0:
14555 case DW_OP_lit1:
14556 case DW_OP_lit2:
14557 case DW_OP_lit3:
14558 case DW_OP_lit4:
14559 case DW_OP_lit5:
14560 case DW_OP_lit6:
14561 case DW_OP_lit7:
14562 case DW_OP_lit8:
14563 case DW_OP_lit9:
14564 case DW_OP_lit10:
14565 case DW_OP_lit11:
14566 case DW_OP_lit12:
14567 case DW_OP_lit13:
14568 case DW_OP_lit14:
14569 case DW_OP_lit15:
14570 case DW_OP_lit16:
14571 case DW_OP_lit17:
14572 case DW_OP_lit18:
14573 case DW_OP_lit19:
14574 case DW_OP_lit20:
14575 case DW_OP_lit21:
14576 case DW_OP_lit22:
14577 case DW_OP_lit23:
14578 case DW_OP_lit24:
14579 case DW_OP_lit25:
14580 case DW_OP_lit26:
14581 case DW_OP_lit27:
14582 case DW_OP_lit28:
14583 case DW_OP_lit29:
14584 case DW_OP_lit30:
14585 case DW_OP_lit31:
14586 stack[++stacki] = op - DW_OP_lit0;
14587 break;
14588
14589 case DW_OP_reg0:
14590 case DW_OP_reg1:
14591 case DW_OP_reg2:
14592 case DW_OP_reg3:
14593 case DW_OP_reg4:
14594 case DW_OP_reg5:
14595 case DW_OP_reg6:
14596 case DW_OP_reg7:
14597 case DW_OP_reg8:
14598 case DW_OP_reg9:
14599 case DW_OP_reg10:
14600 case DW_OP_reg11:
14601 case DW_OP_reg12:
14602 case DW_OP_reg13:
14603 case DW_OP_reg14:
14604 case DW_OP_reg15:
14605 case DW_OP_reg16:
14606 case DW_OP_reg17:
14607 case DW_OP_reg18:
14608 case DW_OP_reg19:
14609 case DW_OP_reg20:
14610 case DW_OP_reg21:
14611 case DW_OP_reg22:
14612 case DW_OP_reg23:
14613 case DW_OP_reg24:
14614 case DW_OP_reg25:
14615 case DW_OP_reg26:
14616 case DW_OP_reg27:
14617 case DW_OP_reg28:
14618 case DW_OP_reg29:
14619 case DW_OP_reg30:
14620 case DW_OP_reg31:
14621 stack[++stacki] = op - DW_OP_reg0;
14622 if (i < size)
14623 dwarf2_complex_location_expr_complaint ();
14624 break;
14625
14626 case DW_OP_regx:
14627 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14628 i += bytes_read;
14629 stack[++stacki] = unsnd;
14630 if (i < size)
14631 dwarf2_complex_location_expr_complaint ();
14632 break;
14633
14634 case DW_OP_addr:
14635 stack[++stacki] = read_address (objfile->obfd, &data[i],
14636 cu, &bytes_read);
14637 i += bytes_read;
14638 break;
14639
14640 case DW_OP_const1u:
14641 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14642 i += 1;
14643 break;
14644
14645 case DW_OP_const1s:
14646 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14647 i += 1;
14648 break;
14649
14650 case DW_OP_const2u:
14651 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14652 i += 2;
14653 break;
14654
14655 case DW_OP_const2s:
14656 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14657 i += 2;
14658 break;
14659
14660 case DW_OP_const4u:
14661 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14662 i += 4;
14663 break;
14664
14665 case DW_OP_const4s:
14666 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14667 i += 4;
14668 break;
14669
14670 case DW_OP_constu:
14671 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14672 &bytes_read);
14673 i += bytes_read;
14674 break;
14675
14676 case DW_OP_consts:
14677 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14678 i += bytes_read;
14679 break;
14680
14681 case DW_OP_dup:
14682 stack[stacki + 1] = stack[stacki];
14683 stacki++;
14684 break;
14685
14686 case DW_OP_plus:
14687 stack[stacki - 1] += stack[stacki];
14688 stacki--;
14689 break;
14690
14691 case DW_OP_plus_uconst:
14692 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14693 &bytes_read);
14694 i += bytes_read;
14695 break;
14696
14697 case DW_OP_minus:
14698 stack[stacki - 1] -= stack[stacki];
14699 stacki--;
14700 break;
14701
14702 case DW_OP_deref:
14703 /* If we're not the last op, then we definitely can't encode
14704 this using GDB's address_class enum. This is valid for partial
14705 global symbols, although the variable's address will be bogus
14706 in the psymtab. */
14707 if (i < size)
14708 dwarf2_complex_location_expr_complaint ();
14709 break;
14710
14711 case DW_OP_GNU_push_tls_address:
14712 /* The top of the stack has the offset from the beginning
14713 of the thread control block at which the variable is located. */
14714 /* Nothing should follow this operator, so the top of stack would
14715 be returned. */
14716 /* This is valid for partial global symbols, but the variable's
14717 address will be bogus in the psymtab. */
14718 if (i < size)
14719 dwarf2_complex_location_expr_complaint ();
14720 break;
14721
14722 case DW_OP_GNU_uninit:
14723 break;
14724
14725 default:
14726 {
14727 const char *name = dwarf_stack_op_name (op);
14728
14729 if (name)
14730 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14731 name);
14732 else
14733 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14734 op);
14735 }
14736
14737 return (stack[stacki]);
14738 }
14739
14740 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14741 outside of the allocated space. Also enforce minimum>0. */
14742 if (stacki >= ARRAY_SIZE (stack) - 1)
14743 {
14744 complaint (&symfile_complaints,
14745 _("location description stack overflow"));
14746 return 0;
14747 }
14748
14749 if (stacki <= 0)
14750 {
14751 complaint (&symfile_complaints,
14752 _("location description stack underflow"));
14753 return 0;
14754 }
14755 }
14756 return (stack[stacki]);
14757 }
14758
14759 /* memory allocation interface */
14760
14761 static struct dwarf_block *
14762 dwarf_alloc_block (struct dwarf2_cu *cu)
14763 {
14764 struct dwarf_block *blk;
14765
14766 blk = (struct dwarf_block *)
14767 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14768 return (blk);
14769 }
14770
14771 static struct abbrev_info *
14772 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14773 {
14774 struct abbrev_info *abbrev;
14775
14776 abbrev = (struct abbrev_info *)
14777 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14778 memset (abbrev, 0, sizeof (struct abbrev_info));
14779 return (abbrev);
14780 }
14781
14782 static struct die_info *
14783 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14784 {
14785 struct die_info *die;
14786 size_t size = sizeof (struct die_info);
14787
14788 if (num_attrs > 1)
14789 size += (num_attrs - 1) * sizeof (struct attribute);
14790
14791 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14792 memset (die, 0, sizeof (struct die_info));
14793 return (die);
14794 }
14795
14796 \f
14797 /* Macro support. */
14798
14799 /* Return the full name of file number I in *LH's file name table.
14800 Use COMP_DIR as the name of the current directory of the
14801 compilation. The result is allocated using xmalloc; the caller is
14802 responsible for freeing it. */
14803 static char *
14804 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14805 {
14806 /* Is the file number a valid index into the line header's file name
14807 table? Remember that file numbers start with one, not zero. */
14808 if (1 <= file && file <= lh->num_file_names)
14809 {
14810 struct file_entry *fe = &lh->file_names[file - 1];
14811
14812 if (IS_ABSOLUTE_PATH (fe->name))
14813 return xstrdup (fe->name);
14814 else
14815 {
14816 const char *dir;
14817 int dir_len;
14818 char *full_name;
14819
14820 if (fe->dir_index)
14821 dir = lh->include_dirs[fe->dir_index - 1];
14822 else
14823 dir = comp_dir;
14824
14825 if (dir)
14826 {
14827 dir_len = strlen (dir);
14828 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14829 strcpy (full_name, dir);
14830 full_name[dir_len] = '/';
14831 strcpy (full_name + dir_len + 1, fe->name);
14832 return full_name;
14833 }
14834 else
14835 return xstrdup (fe->name);
14836 }
14837 }
14838 else
14839 {
14840 /* The compiler produced a bogus file number. We can at least
14841 record the macro definitions made in the file, even if we
14842 won't be able to find the file by name. */
14843 char fake_name[80];
14844
14845 sprintf (fake_name, "<bad macro file number %d>", file);
14846
14847 complaint (&symfile_complaints,
14848 _("bad file number in macro information (%d)"),
14849 file);
14850
14851 return xstrdup (fake_name);
14852 }
14853 }
14854
14855
14856 static struct macro_source_file *
14857 macro_start_file (int file, int line,
14858 struct macro_source_file *current_file,
14859 const char *comp_dir,
14860 struct line_header *lh, struct objfile *objfile)
14861 {
14862 /* The full name of this source file. */
14863 char *full_name = file_full_name (file, lh, comp_dir);
14864
14865 /* We don't create a macro table for this compilation unit
14866 at all until we actually get a filename. */
14867 if (! pending_macros)
14868 pending_macros = new_macro_table (&objfile->objfile_obstack,
14869 objfile->macro_cache);
14870
14871 if (! current_file)
14872 /* If we have no current file, then this must be the start_file
14873 directive for the compilation unit's main source file. */
14874 current_file = macro_set_main (pending_macros, full_name);
14875 else
14876 current_file = macro_include (current_file, line, full_name);
14877
14878 xfree (full_name);
14879
14880 return current_file;
14881 }
14882
14883
14884 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14885 followed by a null byte. */
14886 static char *
14887 copy_string (const char *buf, int len)
14888 {
14889 char *s = xmalloc (len + 1);
14890
14891 memcpy (s, buf, len);
14892 s[len] = '\0';
14893 return s;
14894 }
14895
14896
14897 static const char *
14898 consume_improper_spaces (const char *p, const char *body)
14899 {
14900 if (*p == ' ')
14901 {
14902 complaint (&symfile_complaints,
14903 _("macro definition contains spaces "
14904 "in formal argument list:\n`%s'"),
14905 body);
14906
14907 while (*p == ' ')
14908 p++;
14909 }
14910
14911 return p;
14912 }
14913
14914
14915 static void
14916 parse_macro_definition (struct macro_source_file *file, int line,
14917 const char *body)
14918 {
14919 const char *p;
14920
14921 /* The body string takes one of two forms. For object-like macro
14922 definitions, it should be:
14923
14924 <macro name> " " <definition>
14925
14926 For function-like macro definitions, it should be:
14927
14928 <macro name> "() " <definition>
14929 or
14930 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14931
14932 Spaces may appear only where explicitly indicated, and in the
14933 <definition>.
14934
14935 The Dwarf 2 spec says that an object-like macro's name is always
14936 followed by a space, but versions of GCC around March 2002 omit
14937 the space when the macro's definition is the empty string.
14938
14939 The Dwarf 2 spec says that there should be no spaces between the
14940 formal arguments in a function-like macro's formal argument list,
14941 but versions of GCC around March 2002 include spaces after the
14942 commas. */
14943
14944
14945 /* Find the extent of the macro name. The macro name is terminated
14946 by either a space or null character (for an object-like macro) or
14947 an opening paren (for a function-like macro). */
14948 for (p = body; *p; p++)
14949 if (*p == ' ' || *p == '(')
14950 break;
14951
14952 if (*p == ' ' || *p == '\0')
14953 {
14954 /* It's an object-like macro. */
14955 int name_len = p - body;
14956 char *name = copy_string (body, name_len);
14957 const char *replacement;
14958
14959 if (*p == ' ')
14960 replacement = body + name_len + 1;
14961 else
14962 {
14963 dwarf2_macro_malformed_definition_complaint (body);
14964 replacement = body + name_len;
14965 }
14966
14967 macro_define_object (file, line, name, replacement);
14968
14969 xfree (name);
14970 }
14971 else if (*p == '(')
14972 {
14973 /* It's a function-like macro. */
14974 char *name = copy_string (body, p - body);
14975 int argc = 0;
14976 int argv_size = 1;
14977 char **argv = xmalloc (argv_size * sizeof (*argv));
14978
14979 p++;
14980
14981 p = consume_improper_spaces (p, body);
14982
14983 /* Parse the formal argument list. */
14984 while (*p && *p != ')')
14985 {
14986 /* Find the extent of the current argument name. */
14987 const char *arg_start = p;
14988
14989 while (*p && *p != ',' && *p != ')' && *p != ' ')
14990 p++;
14991
14992 if (! *p || p == arg_start)
14993 dwarf2_macro_malformed_definition_complaint (body);
14994 else
14995 {
14996 /* Make sure argv has room for the new argument. */
14997 if (argc >= argv_size)
14998 {
14999 argv_size *= 2;
15000 argv = xrealloc (argv, argv_size * sizeof (*argv));
15001 }
15002
15003 argv[argc++] = copy_string (arg_start, p - arg_start);
15004 }
15005
15006 p = consume_improper_spaces (p, body);
15007
15008 /* Consume the comma, if present. */
15009 if (*p == ',')
15010 {
15011 p++;
15012
15013 p = consume_improper_spaces (p, body);
15014 }
15015 }
15016
15017 if (*p == ')')
15018 {
15019 p++;
15020
15021 if (*p == ' ')
15022 /* Perfectly formed definition, no complaints. */
15023 macro_define_function (file, line, name,
15024 argc, (const char **) argv,
15025 p + 1);
15026 else if (*p == '\0')
15027 {
15028 /* Complain, but do define it. */
15029 dwarf2_macro_malformed_definition_complaint (body);
15030 macro_define_function (file, line, name,
15031 argc, (const char **) argv,
15032 p);
15033 }
15034 else
15035 /* Just complain. */
15036 dwarf2_macro_malformed_definition_complaint (body);
15037 }
15038 else
15039 /* Just complain. */
15040 dwarf2_macro_malformed_definition_complaint (body);
15041
15042 xfree (name);
15043 {
15044 int i;
15045
15046 for (i = 0; i < argc; i++)
15047 xfree (argv[i]);
15048 }
15049 xfree (argv);
15050 }
15051 else
15052 dwarf2_macro_malformed_definition_complaint (body);
15053 }
15054
15055 /* Skip some bytes from BYTES according to the form given in FORM.
15056 Returns the new pointer. */
15057
15058 static gdb_byte *
15059 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15060 enum dwarf_form form,
15061 unsigned int offset_size,
15062 struct dwarf2_section_info *section)
15063 {
15064 unsigned int bytes_read;
15065
15066 switch (form)
15067 {
15068 case DW_FORM_data1:
15069 case DW_FORM_flag:
15070 ++bytes;
15071 break;
15072
15073 case DW_FORM_data2:
15074 bytes += 2;
15075 break;
15076
15077 case DW_FORM_data4:
15078 bytes += 4;
15079 break;
15080
15081 case DW_FORM_data8:
15082 bytes += 8;
15083 break;
15084
15085 case DW_FORM_string:
15086 read_direct_string (abfd, bytes, &bytes_read);
15087 bytes += bytes_read;
15088 break;
15089
15090 case DW_FORM_sec_offset:
15091 case DW_FORM_strp:
15092 bytes += offset_size;
15093 break;
15094
15095 case DW_FORM_block:
15096 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15097 bytes += bytes_read;
15098 break;
15099
15100 case DW_FORM_block1:
15101 bytes += 1 + read_1_byte (abfd, bytes);
15102 break;
15103 case DW_FORM_block2:
15104 bytes += 2 + read_2_bytes (abfd, bytes);
15105 break;
15106 case DW_FORM_block4:
15107 bytes += 4 + read_4_bytes (abfd, bytes);
15108 break;
15109
15110 case DW_FORM_sdata:
15111 case DW_FORM_udata:
15112 bytes = skip_leb128 (abfd, bytes);
15113 break;
15114
15115 default:
15116 {
15117 complain:
15118 complaint (&symfile_complaints,
15119 _("invalid form 0x%x in `%s'"),
15120 form,
15121 section->asection->name);
15122 return NULL;
15123 }
15124 }
15125
15126 return bytes;
15127 }
15128
15129 /* A helper for dwarf_decode_macros that handles skipping an unknown
15130 opcode. Returns an updated pointer to the macro data buffer; or,
15131 on error, issues a complaint and returns NULL. */
15132
15133 static gdb_byte *
15134 skip_unknown_opcode (unsigned int opcode,
15135 gdb_byte **opcode_definitions,
15136 gdb_byte *mac_ptr,
15137 bfd *abfd,
15138 unsigned int offset_size,
15139 struct dwarf2_section_info *section)
15140 {
15141 unsigned int bytes_read, i;
15142 unsigned long arg;
15143 gdb_byte *defn;
15144
15145 if (opcode_definitions[opcode] == NULL)
15146 {
15147 complaint (&symfile_complaints,
15148 _("unrecognized DW_MACFINO opcode 0x%x"),
15149 opcode);
15150 return NULL;
15151 }
15152
15153 defn = opcode_definitions[opcode];
15154 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15155 defn += bytes_read;
15156
15157 for (i = 0; i < arg; ++i)
15158 {
15159 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15160 if (mac_ptr == NULL)
15161 {
15162 /* skip_form_bytes already issued the complaint. */
15163 return NULL;
15164 }
15165 }
15166
15167 return mac_ptr;
15168 }
15169
15170 /* A helper function which parses the header of a macro section.
15171 If the macro section is the extended (for now called "GNU") type,
15172 then this updates *OFFSET_SIZE. Returns a pointer to just after
15173 the header, or issues a complaint and returns NULL on error. */
15174
15175 static gdb_byte *
15176 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15177 bfd *abfd,
15178 gdb_byte *mac_ptr,
15179 unsigned int *offset_size,
15180 int section_is_gnu)
15181 {
15182 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15183
15184 if (section_is_gnu)
15185 {
15186 unsigned int version, flags;
15187
15188 version = read_2_bytes (abfd, mac_ptr);
15189 if (version != 4)
15190 {
15191 complaint (&symfile_complaints,
15192 _("unrecognized version `%d' in .debug_macro section"),
15193 version);
15194 return NULL;
15195 }
15196 mac_ptr += 2;
15197
15198 flags = read_1_byte (abfd, mac_ptr);
15199 ++mac_ptr;
15200 *offset_size = (flags & 1) ? 8 : 4;
15201
15202 if ((flags & 2) != 0)
15203 /* We don't need the line table offset. */
15204 mac_ptr += *offset_size;
15205
15206 /* Vendor opcode descriptions. */
15207 if ((flags & 4) != 0)
15208 {
15209 unsigned int i, count;
15210
15211 count = read_1_byte (abfd, mac_ptr);
15212 ++mac_ptr;
15213 for (i = 0; i < count; ++i)
15214 {
15215 unsigned int opcode, bytes_read;
15216 unsigned long arg;
15217
15218 opcode = read_1_byte (abfd, mac_ptr);
15219 ++mac_ptr;
15220 opcode_definitions[opcode] = mac_ptr;
15221 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15222 mac_ptr += bytes_read;
15223 mac_ptr += arg;
15224 }
15225 }
15226 }
15227
15228 return mac_ptr;
15229 }
15230
15231 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15232 including DW_GNU_MACINFO_transparent_include. */
15233
15234 static void
15235 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15236 struct macro_source_file *current_file,
15237 struct line_header *lh, char *comp_dir,
15238 struct dwarf2_section_info *section,
15239 int section_is_gnu,
15240 unsigned int offset_size,
15241 struct objfile *objfile)
15242 {
15243 enum dwarf_macro_record_type macinfo_type;
15244 int at_commandline;
15245 gdb_byte *opcode_definitions[256];
15246
15247 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15248 &offset_size, section_is_gnu);
15249 if (mac_ptr == NULL)
15250 {
15251 /* We already issued a complaint. */
15252 return;
15253 }
15254
15255 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15256 GDB is still reading the definitions from command line. First
15257 DW_MACINFO_start_file will need to be ignored as it was already executed
15258 to create CURRENT_FILE for the main source holding also the command line
15259 definitions. On first met DW_MACINFO_start_file this flag is reset to
15260 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15261
15262 at_commandline = 1;
15263
15264 do
15265 {
15266 /* Do we at least have room for a macinfo type byte? */
15267 if (mac_ptr >= mac_end)
15268 {
15269 dwarf2_macros_too_long_complaint (section);
15270 break;
15271 }
15272
15273 macinfo_type = read_1_byte (abfd, mac_ptr);
15274 mac_ptr++;
15275
15276 /* Note that we rely on the fact that the corresponding GNU and
15277 DWARF constants are the same. */
15278 switch (macinfo_type)
15279 {
15280 /* A zero macinfo type indicates the end of the macro
15281 information. */
15282 case 0:
15283 break;
15284
15285 case DW_MACRO_GNU_define:
15286 case DW_MACRO_GNU_undef:
15287 case DW_MACRO_GNU_define_indirect:
15288 case DW_MACRO_GNU_undef_indirect:
15289 {
15290 unsigned int bytes_read;
15291 int line;
15292 char *body;
15293 int is_define;
15294
15295 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15296 mac_ptr += bytes_read;
15297
15298 if (macinfo_type == DW_MACRO_GNU_define
15299 || macinfo_type == DW_MACRO_GNU_undef)
15300 {
15301 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15302 mac_ptr += bytes_read;
15303 }
15304 else
15305 {
15306 LONGEST str_offset;
15307
15308 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15309 mac_ptr += offset_size;
15310
15311 body = read_indirect_string_at_offset (abfd, str_offset);
15312 }
15313
15314 is_define = (macinfo_type == DW_MACRO_GNU_define
15315 || macinfo_type == DW_MACRO_GNU_define_indirect);
15316 if (! current_file)
15317 {
15318 /* DWARF violation as no main source is present. */
15319 complaint (&symfile_complaints,
15320 _("debug info with no main source gives macro %s "
15321 "on line %d: %s"),
15322 is_define ? _("definition") : _("undefinition"),
15323 line, body);
15324 break;
15325 }
15326 if ((line == 0 && !at_commandline)
15327 || (line != 0 && at_commandline))
15328 complaint (&symfile_complaints,
15329 _("debug info gives %s macro %s with %s line %d: %s"),
15330 at_commandline ? _("command-line") : _("in-file"),
15331 is_define ? _("definition") : _("undefinition"),
15332 line == 0 ? _("zero") : _("non-zero"), line, body);
15333
15334 if (is_define)
15335 parse_macro_definition (current_file, line, body);
15336 else
15337 {
15338 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15339 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15340 macro_undef (current_file, line, body);
15341 }
15342 }
15343 break;
15344
15345 case DW_MACRO_GNU_start_file:
15346 {
15347 unsigned int bytes_read;
15348 int line, file;
15349
15350 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15351 mac_ptr += bytes_read;
15352 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15353 mac_ptr += bytes_read;
15354
15355 if ((line == 0 && !at_commandline)
15356 || (line != 0 && at_commandline))
15357 complaint (&symfile_complaints,
15358 _("debug info gives source %d included "
15359 "from %s at %s line %d"),
15360 file, at_commandline ? _("command-line") : _("file"),
15361 line == 0 ? _("zero") : _("non-zero"), line);
15362
15363 if (at_commandline)
15364 {
15365 /* This DW_MACRO_GNU_start_file was executed in the
15366 pass one. */
15367 at_commandline = 0;
15368 }
15369 else
15370 current_file = macro_start_file (file, line,
15371 current_file, comp_dir,
15372 lh, objfile);
15373 }
15374 break;
15375
15376 case DW_MACRO_GNU_end_file:
15377 if (! current_file)
15378 complaint (&symfile_complaints,
15379 _("macro debug info has an unmatched "
15380 "`close_file' directive"));
15381 else
15382 {
15383 current_file = current_file->included_by;
15384 if (! current_file)
15385 {
15386 enum dwarf_macro_record_type next_type;
15387
15388 /* GCC circa March 2002 doesn't produce the zero
15389 type byte marking the end of the compilation
15390 unit. Complain if it's not there, but exit no
15391 matter what. */
15392
15393 /* Do we at least have room for a macinfo type byte? */
15394 if (mac_ptr >= mac_end)
15395 {
15396 dwarf2_macros_too_long_complaint (section);
15397 return;
15398 }
15399
15400 /* We don't increment mac_ptr here, so this is just
15401 a look-ahead. */
15402 next_type = read_1_byte (abfd, mac_ptr);
15403 if (next_type != 0)
15404 complaint (&symfile_complaints,
15405 _("no terminating 0-type entry for "
15406 "macros in `.debug_macinfo' section"));
15407
15408 return;
15409 }
15410 }
15411 break;
15412
15413 case DW_MACRO_GNU_transparent_include:
15414 {
15415 LONGEST offset;
15416
15417 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15418 mac_ptr += offset_size;
15419
15420 dwarf_decode_macro_bytes (abfd,
15421 section->buffer + offset,
15422 mac_end, current_file,
15423 lh, comp_dir,
15424 section, section_is_gnu,
15425 offset_size, objfile);
15426 }
15427 break;
15428
15429 case DW_MACINFO_vendor_ext:
15430 if (!section_is_gnu)
15431 {
15432 unsigned int bytes_read;
15433 int constant;
15434
15435 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15436 mac_ptr += bytes_read;
15437 read_direct_string (abfd, mac_ptr, &bytes_read);
15438 mac_ptr += bytes_read;
15439
15440 /* We don't recognize any vendor extensions. */
15441 break;
15442 }
15443 /* FALLTHROUGH */
15444
15445 default:
15446 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15447 mac_ptr, abfd, offset_size,
15448 section);
15449 if (mac_ptr == NULL)
15450 return;
15451 break;
15452 }
15453 } while (macinfo_type != 0);
15454 }
15455
15456 static void
15457 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15458 char *comp_dir, bfd *abfd,
15459 struct dwarf2_cu *cu,
15460 struct dwarf2_section_info *section,
15461 int section_is_gnu)
15462 {
15463 gdb_byte *mac_ptr, *mac_end;
15464 struct macro_source_file *current_file = 0;
15465 enum dwarf_macro_record_type macinfo_type;
15466 unsigned int offset_size = cu->header.offset_size;
15467 gdb_byte *opcode_definitions[256];
15468
15469 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15470 if (section->buffer == NULL)
15471 {
15472 complaint (&symfile_complaints, _("missing %s section"),
15473 section->asection->name);
15474 return;
15475 }
15476
15477 /* First pass: Find the name of the base filename.
15478 This filename is needed in order to process all macros whose definition
15479 (or undefinition) comes from the command line. These macros are defined
15480 before the first DW_MACINFO_start_file entry, and yet still need to be
15481 associated to the base file.
15482
15483 To determine the base file name, we scan the macro definitions until we
15484 reach the first DW_MACINFO_start_file entry. We then initialize
15485 CURRENT_FILE accordingly so that any macro definition found before the
15486 first DW_MACINFO_start_file can still be associated to the base file. */
15487
15488 mac_ptr = section->buffer + offset;
15489 mac_end = section->buffer + section->size;
15490
15491 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15492 &offset_size, section_is_gnu);
15493 if (mac_ptr == NULL)
15494 {
15495 /* We already issued a complaint. */
15496 return;
15497 }
15498
15499 do
15500 {
15501 /* Do we at least have room for a macinfo type byte? */
15502 if (mac_ptr >= mac_end)
15503 {
15504 /* Complaint is printed during the second pass as GDB will probably
15505 stop the first pass earlier upon finding
15506 DW_MACINFO_start_file. */
15507 break;
15508 }
15509
15510 macinfo_type = read_1_byte (abfd, mac_ptr);
15511 mac_ptr++;
15512
15513 /* Note that we rely on the fact that the corresponding GNU and
15514 DWARF constants are the same. */
15515 switch (macinfo_type)
15516 {
15517 /* A zero macinfo type indicates the end of the macro
15518 information. */
15519 case 0:
15520 break;
15521
15522 case DW_MACRO_GNU_define:
15523 case DW_MACRO_GNU_undef:
15524 /* Only skip the data by MAC_PTR. */
15525 {
15526 unsigned int bytes_read;
15527
15528 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15529 mac_ptr += bytes_read;
15530 read_direct_string (abfd, mac_ptr, &bytes_read);
15531 mac_ptr += bytes_read;
15532 }
15533 break;
15534
15535 case DW_MACRO_GNU_start_file:
15536 {
15537 unsigned int bytes_read;
15538 int line, file;
15539
15540 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15541 mac_ptr += bytes_read;
15542 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15543 mac_ptr += bytes_read;
15544
15545 current_file = macro_start_file (file, line, current_file,
15546 comp_dir, lh, cu->objfile);
15547 }
15548 break;
15549
15550 case DW_MACRO_GNU_end_file:
15551 /* No data to skip by MAC_PTR. */
15552 break;
15553
15554 case DW_MACRO_GNU_define_indirect:
15555 case DW_MACRO_GNU_undef_indirect:
15556 {
15557 unsigned int bytes_read;
15558
15559 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15560 mac_ptr += bytes_read;
15561 mac_ptr += offset_size;
15562 }
15563 break;
15564
15565 case DW_MACRO_GNU_transparent_include:
15566 /* Note that, according to the spec, a transparent include
15567 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15568 skip this opcode. */
15569 mac_ptr += offset_size;
15570 break;
15571
15572 case DW_MACINFO_vendor_ext:
15573 /* Only skip the data by MAC_PTR. */
15574 if (!section_is_gnu)
15575 {
15576 unsigned int bytes_read;
15577
15578 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15579 mac_ptr += bytes_read;
15580 read_direct_string (abfd, mac_ptr, &bytes_read);
15581 mac_ptr += bytes_read;
15582 }
15583 /* FALLTHROUGH */
15584
15585 default:
15586 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15587 mac_ptr, abfd, offset_size,
15588 section);
15589 if (mac_ptr == NULL)
15590 return;
15591 break;
15592 }
15593 } while (macinfo_type != 0 && current_file == NULL);
15594
15595 /* Second pass: Process all entries.
15596
15597 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15598 command-line macro definitions/undefinitions. This flag is unset when we
15599 reach the first DW_MACINFO_start_file entry. */
15600
15601 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15602 current_file, lh, comp_dir, section, section_is_gnu,
15603 offset_size, cu->objfile);
15604 }
15605
15606 /* Check if the attribute's form is a DW_FORM_block*
15607 if so return true else false. */
15608 static int
15609 attr_form_is_block (struct attribute *attr)
15610 {
15611 return (attr == NULL ? 0 :
15612 attr->form == DW_FORM_block1
15613 || attr->form == DW_FORM_block2
15614 || attr->form == DW_FORM_block4
15615 || attr->form == DW_FORM_block
15616 || attr->form == DW_FORM_exprloc);
15617 }
15618
15619 /* Return non-zero if ATTR's value is a section offset --- classes
15620 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15621 You may use DW_UNSND (attr) to retrieve such offsets.
15622
15623 Section 7.5.4, "Attribute Encodings", explains that no attribute
15624 may have a value that belongs to more than one of these classes; it
15625 would be ambiguous if we did, because we use the same forms for all
15626 of them. */
15627 static int
15628 attr_form_is_section_offset (struct attribute *attr)
15629 {
15630 return (attr->form == DW_FORM_data4
15631 || attr->form == DW_FORM_data8
15632 || attr->form == DW_FORM_sec_offset);
15633 }
15634
15635
15636 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15637 zero otherwise. When this function returns true, you can apply
15638 dwarf2_get_attr_constant_value to it.
15639
15640 However, note that for some attributes you must check
15641 attr_form_is_section_offset before using this test. DW_FORM_data4
15642 and DW_FORM_data8 are members of both the constant class, and of
15643 the classes that contain offsets into other debug sections
15644 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15645 that, if an attribute's can be either a constant or one of the
15646 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15647 taken as section offsets, not constants. */
15648 static int
15649 attr_form_is_constant (struct attribute *attr)
15650 {
15651 switch (attr->form)
15652 {
15653 case DW_FORM_sdata:
15654 case DW_FORM_udata:
15655 case DW_FORM_data1:
15656 case DW_FORM_data2:
15657 case DW_FORM_data4:
15658 case DW_FORM_data8:
15659 return 1;
15660 default:
15661 return 0;
15662 }
15663 }
15664
15665 /* A helper function that fills in a dwarf2_loclist_baton. */
15666
15667 static void
15668 fill_in_loclist_baton (struct dwarf2_cu *cu,
15669 struct dwarf2_loclist_baton *baton,
15670 struct attribute *attr)
15671 {
15672 dwarf2_read_section (dwarf2_per_objfile->objfile,
15673 &dwarf2_per_objfile->loc);
15674
15675 baton->per_cu = cu->per_cu;
15676 gdb_assert (baton->per_cu);
15677 /* We don't know how long the location list is, but make sure we
15678 don't run off the edge of the section. */
15679 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15680 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15681 baton->base_address = cu->base_address;
15682 }
15683
15684 static void
15685 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15686 struct dwarf2_cu *cu)
15687 {
15688 if (attr_form_is_section_offset (attr)
15689 /* ".debug_loc" may not exist at all, or the offset may be outside
15690 the section. If so, fall through to the complaint in the
15691 other branch. */
15692 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15693 &dwarf2_per_objfile->loc))
15694 {
15695 struct dwarf2_loclist_baton *baton;
15696
15697 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15698 sizeof (struct dwarf2_loclist_baton));
15699
15700 fill_in_loclist_baton (cu, baton, attr);
15701
15702 if (cu->base_known == 0)
15703 complaint (&symfile_complaints,
15704 _("Location list used without "
15705 "specifying the CU base address."));
15706
15707 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15708 SYMBOL_LOCATION_BATON (sym) = baton;
15709 }
15710 else
15711 {
15712 struct dwarf2_locexpr_baton *baton;
15713
15714 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15715 sizeof (struct dwarf2_locexpr_baton));
15716 baton->per_cu = cu->per_cu;
15717 gdb_assert (baton->per_cu);
15718
15719 if (attr_form_is_block (attr))
15720 {
15721 /* Note that we're just copying the block's data pointer
15722 here, not the actual data. We're still pointing into the
15723 info_buffer for SYM's objfile; right now we never release
15724 that buffer, but when we do clean up properly this may
15725 need to change. */
15726 baton->size = DW_BLOCK (attr)->size;
15727 baton->data = DW_BLOCK (attr)->data;
15728 }
15729 else
15730 {
15731 dwarf2_invalid_attrib_class_complaint ("location description",
15732 SYMBOL_NATURAL_NAME (sym));
15733 baton->size = 0;
15734 }
15735
15736 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15737 SYMBOL_LOCATION_BATON (sym) = baton;
15738 }
15739 }
15740
15741 /* Return the OBJFILE associated with the compilation unit CU. If CU
15742 came from a separate debuginfo file, then the master objfile is
15743 returned. */
15744
15745 struct objfile *
15746 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15747 {
15748 struct objfile *objfile = per_cu->objfile;
15749
15750 /* Return the master objfile, so that we can report and look up the
15751 correct file containing this variable. */
15752 if (objfile->separate_debug_objfile_backlink)
15753 objfile = objfile->separate_debug_objfile_backlink;
15754
15755 return objfile;
15756 }
15757
15758 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15759 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15760 CU_HEADERP first. */
15761
15762 static const struct comp_unit_head *
15763 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15764 struct dwarf2_per_cu_data *per_cu)
15765 {
15766 struct objfile *objfile;
15767 struct dwarf2_per_objfile *per_objfile;
15768 gdb_byte *info_ptr;
15769
15770 if (per_cu->cu)
15771 return &per_cu->cu->header;
15772
15773 objfile = per_cu->objfile;
15774 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15775 info_ptr = per_objfile->info.buffer + per_cu->offset;
15776
15777 memset (cu_headerp, 0, sizeof (*cu_headerp));
15778 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15779
15780 return cu_headerp;
15781 }
15782
15783 /* Return the address size given in the compilation unit header for CU. */
15784
15785 CORE_ADDR
15786 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15787 {
15788 struct comp_unit_head cu_header_local;
15789 const struct comp_unit_head *cu_headerp;
15790
15791 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15792
15793 return cu_headerp->addr_size;
15794 }
15795
15796 /* Return the offset size given in the compilation unit header for CU. */
15797
15798 int
15799 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15800 {
15801 struct comp_unit_head cu_header_local;
15802 const struct comp_unit_head *cu_headerp;
15803
15804 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15805
15806 return cu_headerp->offset_size;
15807 }
15808
15809 /* See its dwarf2loc.h declaration. */
15810
15811 int
15812 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15813 {
15814 struct comp_unit_head cu_header_local;
15815 const struct comp_unit_head *cu_headerp;
15816
15817 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15818
15819 if (cu_headerp->version == 2)
15820 return cu_headerp->addr_size;
15821 else
15822 return cu_headerp->offset_size;
15823 }
15824
15825 /* Return the text offset of the CU. The returned offset comes from
15826 this CU's objfile. If this objfile came from a separate debuginfo
15827 file, then the offset may be different from the corresponding
15828 offset in the parent objfile. */
15829
15830 CORE_ADDR
15831 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15832 {
15833 struct objfile *objfile = per_cu->objfile;
15834
15835 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15836 }
15837
15838 /* Locate the .debug_info compilation unit from CU's objfile which contains
15839 the DIE at OFFSET. Raises an error on failure. */
15840
15841 static struct dwarf2_per_cu_data *
15842 dwarf2_find_containing_comp_unit (unsigned int offset,
15843 struct objfile *objfile)
15844 {
15845 struct dwarf2_per_cu_data *this_cu;
15846 int low, high;
15847
15848 low = 0;
15849 high = dwarf2_per_objfile->n_comp_units - 1;
15850 while (high > low)
15851 {
15852 int mid = low + (high - low) / 2;
15853
15854 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15855 high = mid;
15856 else
15857 low = mid + 1;
15858 }
15859 gdb_assert (low == high);
15860 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15861 {
15862 if (low == 0)
15863 error (_("Dwarf Error: could not find partial DIE containing "
15864 "offset 0x%lx [in module %s]"),
15865 (long) offset, bfd_get_filename (objfile->obfd));
15866
15867 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15868 return dwarf2_per_objfile->all_comp_units[low-1];
15869 }
15870 else
15871 {
15872 this_cu = dwarf2_per_objfile->all_comp_units[low];
15873 if (low == dwarf2_per_objfile->n_comp_units - 1
15874 && offset >= this_cu->offset + this_cu->length)
15875 error (_("invalid dwarf2 offset %u"), offset);
15876 gdb_assert (offset < this_cu->offset + this_cu->length);
15877 return this_cu;
15878 }
15879 }
15880
15881 /* Locate the compilation unit from OBJFILE which is located at exactly
15882 OFFSET. Raises an error on failure. */
15883
15884 static struct dwarf2_per_cu_data *
15885 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15886 {
15887 struct dwarf2_per_cu_data *this_cu;
15888
15889 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15890 if (this_cu->offset != offset)
15891 error (_("no compilation unit with offset %u."), offset);
15892 return this_cu;
15893 }
15894
15895 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
15896
15897 static void
15898 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15899 {
15900 memset (cu, 0, sizeof (*cu));
15901 cu->objfile = objfile;
15902 obstack_init (&cu->comp_unit_obstack);
15903 }
15904
15905 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15906
15907 static void
15908 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15909 {
15910 struct attribute *attr;
15911
15912 /* Set the language we're debugging. */
15913 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15914 if (attr)
15915 set_cu_language (DW_UNSND (attr), cu);
15916 else
15917 {
15918 cu->language = language_minimal;
15919 cu->language_defn = language_def (cu->language);
15920 }
15921 }
15922
15923 /* Release one cached compilation unit, CU. We unlink it from the tree
15924 of compilation units, but we don't remove it from the read_in_chain;
15925 the caller is responsible for that.
15926 NOTE: DATA is a void * because this function is also used as a
15927 cleanup routine. */
15928
15929 static void
15930 free_one_comp_unit (void *data)
15931 {
15932 struct dwarf2_cu *cu = data;
15933
15934 if (cu->per_cu != NULL)
15935 cu->per_cu->cu = NULL;
15936 cu->per_cu = NULL;
15937
15938 obstack_free (&cu->comp_unit_obstack, NULL);
15939
15940 xfree (cu);
15941 }
15942
15943 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15944 when we're finished with it. We can't free the pointer itself, but be
15945 sure to unlink it from the cache. Also release any associated storage
15946 and perform cache maintenance.
15947
15948 Only used during partial symbol parsing. */
15949
15950 static void
15951 free_stack_comp_unit (void *data)
15952 {
15953 struct dwarf2_cu *cu = data;
15954
15955 obstack_free (&cu->comp_unit_obstack, NULL);
15956 cu->partial_dies = NULL;
15957
15958 if (cu->per_cu != NULL)
15959 {
15960 /* This compilation unit is on the stack in our caller, so we
15961 should not xfree it. Just unlink it. */
15962 cu->per_cu->cu = NULL;
15963 cu->per_cu = NULL;
15964
15965 /* If we had a per-cu pointer, then we may have other compilation
15966 units loaded, so age them now. */
15967 age_cached_comp_units ();
15968 }
15969 }
15970
15971 /* Free all cached compilation units. */
15972
15973 static void
15974 free_cached_comp_units (void *data)
15975 {
15976 struct dwarf2_per_cu_data *per_cu, **last_chain;
15977
15978 per_cu = dwarf2_per_objfile->read_in_chain;
15979 last_chain = &dwarf2_per_objfile->read_in_chain;
15980 while (per_cu != NULL)
15981 {
15982 struct dwarf2_per_cu_data *next_cu;
15983
15984 next_cu = per_cu->cu->read_in_chain;
15985
15986 free_one_comp_unit (per_cu->cu);
15987 *last_chain = next_cu;
15988
15989 per_cu = next_cu;
15990 }
15991 }
15992
15993 /* Increase the age counter on each cached compilation unit, and free
15994 any that are too old. */
15995
15996 static void
15997 age_cached_comp_units (void)
15998 {
15999 struct dwarf2_per_cu_data *per_cu, **last_chain;
16000
16001 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16002 per_cu = dwarf2_per_objfile->read_in_chain;
16003 while (per_cu != NULL)
16004 {
16005 per_cu->cu->last_used ++;
16006 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16007 dwarf2_mark (per_cu->cu);
16008 per_cu = per_cu->cu->read_in_chain;
16009 }
16010
16011 per_cu = dwarf2_per_objfile->read_in_chain;
16012 last_chain = &dwarf2_per_objfile->read_in_chain;
16013 while (per_cu != NULL)
16014 {
16015 struct dwarf2_per_cu_data *next_cu;
16016
16017 next_cu = per_cu->cu->read_in_chain;
16018
16019 if (!per_cu->cu->mark)
16020 {
16021 free_one_comp_unit (per_cu->cu);
16022 *last_chain = next_cu;
16023 }
16024 else
16025 last_chain = &per_cu->cu->read_in_chain;
16026
16027 per_cu = next_cu;
16028 }
16029 }
16030
16031 /* Remove a single compilation unit from the cache. */
16032
16033 static void
16034 free_one_cached_comp_unit (void *target_cu)
16035 {
16036 struct dwarf2_per_cu_data *per_cu, **last_chain;
16037
16038 per_cu = dwarf2_per_objfile->read_in_chain;
16039 last_chain = &dwarf2_per_objfile->read_in_chain;
16040 while (per_cu != NULL)
16041 {
16042 struct dwarf2_per_cu_data *next_cu;
16043
16044 next_cu = per_cu->cu->read_in_chain;
16045
16046 if (per_cu->cu == target_cu)
16047 {
16048 free_one_comp_unit (per_cu->cu);
16049 *last_chain = next_cu;
16050 break;
16051 }
16052 else
16053 last_chain = &per_cu->cu->read_in_chain;
16054
16055 per_cu = next_cu;
16056 }
16057 }
16058
16059 /* Release all extra memory associated with OBJFILE. */
16060
16061 void
16062 dwarf2_free_objfile (struct objfile *objfile)
16063 {
16064 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16065
16066 if (dwarf2_per_objfile == NULL)
16067 return;
16068
16069 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16070 free_cached_comp_units (NULL);
16071
16072 if (dwarf2_per_objfile->quick_file_names_table)
16073 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16074
16075 /* Everything else should be on the objfile obstack. */
16076 }
16077
16078 /* A pair of DIE offset and GDB type pointer. We store these
16079 in a hash table separate from the DIEs, and preserve them
16080 when the DIEs are flushed out of cache. */
16081
16082 struct dwarf2_offset_and_type
16083 {
16084 unsigned int offset;
16085 struct type *type;
16086 };
16087
16088 /* Hash function for a dwarf2_offset_and_type. */
16089
16090 static hashval_t
16091 offset_and_type_hash (const void *item)
16092 {
16093 const struct dwarf2_offset_and_type *ofs = item;
16094
16095 return ofs->offset;
16096 }
16097
16098 /* Equality function for a dwarf2_offset_and_type. */
16099
16100 static int
16101 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16102 {
16103 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16104 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16105
16106 return ofs_lhs->offset == ofs_rhs->offset;
16107 }
16108
16109 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16110 table if necessary. For convenience, return TYPE.
16111
16112 The DIEs reading must have careful ordering to:
16113 * Not cause infite loops trying to read in DIEs as a prerequisite for
16114 reading current DIE.
16115 * Not trying to dereference contents of still incompletely read in types
16116 while reading in other DIEs.
16117 * Enable referencing still incompletely read in types just by a pointer to
16118 the type without accessing its fields.
16119
16120 Therefore caller should follow these rules:
16121 * Try to fetch any prerequisite types we may need to build this DIE type
16122 before building the type and calling set_die_type.
16123 * After building type call set_die_type for current DIE as soon as
16124 possible before fetching more types to complete the current type.
16125 * Make the type as complete as possible before fetching more types. */
16126
16127 static struct type *
16128 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16129 {
16130 struct dwarf2_offset_and_type **slot, ofs;
16131 struct objfile *objfile = cu->objfile;
16132 htab_t *type_hash_ptr;
16133
16134 /* For Ada types, make sure that the gnat-specific data is always
16135 initialized (if not already set). There are a few types where
16136 we should not be doing so, because the type-specific area is
16137 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16138 where the type-specific area is used to store the floatformat).
16139 But this is not a problem, because the gnat-specific information
16140 is actually not needed for these types. */
16141 if (need_gnat_info (cu)
16142 && TYPE_CODE (type) != TYPE_CODE_FUNC
16143 && TYPE_CODE (type) != TYPE_CODE_FLT
16144 && !HAVE_GNAT_AUX_INFO (type))
16145 INIT_GNAT_SPECIFIC (type);
16146
16147 if (cu->per_cu->debug_type_section)
16148 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16149 else
16150 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16151
16152 if (*type_hash_ptr == NULL)
16153 {
16154 *type_hash_ptr
16155 = htab_create_alloc_ex (127,
16156 offset_and_type_hash,
16157 offset_and_type_eq,
16158 NULL,
16159 &objfile->objfile_obstack,
16160 hashtab_obstack_allocate,
16161 dummy_obstack_deallocate);
16162 }
16163
16164 ofs.offset = die->offset;
16165 ofs.type = type;
16166 slot = (struct dwarf2_offset_and_type **)
16167 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16168 if (*slot)
16169 complaint (&symfile_complaints,
16170 _("A problem internal to GDB: DIE 0x%x has type already set"),
16171 die->offset);
16172 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16173 **slot = ofs;
16174 return type;
16175 }
16176
16177 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16178 table, or return NULL if the die does not have a saved type. */
16179
16180 static struct type *
16181 get_die_type_at_offset (unsigned int offset,
16182 struct dwarf2_per_cu_data *per_cu)
16183 {
16184 struct dwarf2_offset_and_type *slot, ofs;
16185 htab_t type_hash;
16186
16187 if (per_cu->debug_type_section)
16188 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16189 else
16190 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16191 if (type_hash == NULL)
16192 return NULL;
16193
16194 ofs.offset = offset;
16195 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16196 if (slot)
16197 return slot->type;
16198 else
16199 return NULL;
16200 }
16201
16202 /* Look up the type for DIE in the appropriate type_hash table,
16203 or return NULL if DIE does not have a saved type. */
16204
16205 static struct type *
16206 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16207 {
16208 return get_die_type_at_offset (die->offset, cu->per_cu);
16209 }
16210
16211 /* Add a dependence relationship from CU to REF_PER_CU. */
16212
16213 static void
16214 dwarf2_add_dependence (struct dwarf2_cu *cu,
16215 struct dwarf2_per_cu_data *ref_per_cu)
16216 {
16217 void **slot;
16218
16219 if (cu->dependencies == NULL)
16220 cu->dependencies
16221 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16222 NULL, &cu->comp_unit_obstack,
16223 hashtab_obstack_allocate,
16224 dummy_obstack_deallocate);
16225
16226 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16227 if (*slot == NULL)
16228 *slot = ref_per_cu;
16229 }
16230
16231 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16232 Set the mark field in every compilation unit in the
16233 cache that we must keep because we are keeping CU. */
16234
16235 static int
16236 dwarf2_mark_helper (void **slot, void *data)
16237 {
16238 struct dwarf2_per_cu_data *per_cu;
16239
16240 per_cu = (struct dwarf2_per_cu_data *) *slot;
16241
16242 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16243 reading of the chain. As such dependencies remain valid it is not much
16244 useful to track and undo them during QUIT cleanups. */
16245 if (per_cu->cu == NULL)
16246 return 1;
16247
16248 if (per_cu->cu->mark)
16249 return 1;
16250 per_cu->cu->mark = 1;
16251
16252 if (per_cu->cu->dependencies != NULL)
16253 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16254
16255 return 1;
16256 }
16257
16258 /* Set the mark field in CU and in every other compilation unit in the
16259 cache that we must keep because we are keeping CU. */
16260
16261 static void
16262 dwarf2_mark (struct dwarf2_cu *cu)
16263 {
16264 if (cu->mark)
16265 return;
16266 cu->mark = 1;
16267 if (cu->dependencies != NULL)
16268 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16269 }
16270
16271 static void
16272 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16273 {
16274 while (per_cu)
16275 {
16276 per_cu->cu->mark = 0;
16277 per_cu = per_cu->cu->read_in_chain;
16278 }
16279 }
16280
16281 /* Trivial hash function for partial_die_info: the hash value of a DIE
16282 is its offset in .debug_info for this objfile. */
16283
16284 static hashval_t
16285 partial_die_hash (const void *item)
16286 {
16287 const struct partial_die_info *part_die = item;
16288
16289 return part_die->offset;
16290 }
16291
16292 /* Trivial comparison function for partial_die_info structures: two DIEs
16293 are equal if they have the same offset. */
16294
16295 static int
16296 partial_die_eq (const void *item_lhs, const void *item_rhs)
16297 {
16298 const struct partial_die_info *part_die_lhs = item_lhs;
16299 const struct partial_die_info *part_die_rhs = item_rhs;
16300
16301 return part_die_lhs->offset == part_die_rhs->offset;
16302 }
16303
16304 static struct cmd_list_element *set_dwarf2_cmdlist;
16305 static struct cmd_list_element *show_dwarf2_cmdlist;
16306
16307 static void
16308 set_dwarf2_cmd (char *args, int from_tty)
16309 {
16310 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16311 }
16312
16313 static void
16314 show_dwarf2_cmd (char *args, int from_tty)
16315 {
16316 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16317 }
16318
16319 /* If section described by INFO was mmapped, munmap it now. */
16320
16321 static void
16322 munmap_section_buffer (struct dwarf2_section_info *info)
16323 {
16324 if (info->map_addr != NULL)
16325 {
16326 #ifdef HAVE_MMAP
16327 int res;
16328
16329 res = munmap (info->map_addr, info->map_len);
16330 gdb_assert (res == 0);
16331 #else
16332 /* Without HAVE_MMAP, we should never be here to begin with. */
16333 gdb_assert_not_reached ("no mmap support");
16334 #endif
16335 }
16336 }
16337
16338 /* munmap debug sections for OBJFILE, if necessary. */
16339
16340 static void
16341 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16342 {
16343 struct dwarf2_per_objfile *data = d;
16344 int ix;
16345 struct dwarf2_section_info *section;
16346
16347 /* This is sorted according to the order they're defined in to make it easier
16348 to keep in sync. */
16349 munmap_section_buffer (&data->info);
16350 munmap_section_buffer (&data->abbrev);
16351 munmap_section_buffer (&data->line);
16352 munmap_section_buffer (&data->loc);
16353 munmap_section_buffer (&data->macinfo);
16354 munmap_section_buffer (&data->macro);
16355 munmap_section_buffer (&data->str);
16356 munmap_section_buffer (&data->ranges);
16357 munmap_section_buffer (&data->frame);
16358 munmap_section_buffer (&data->eh_frame);
16359 munmap_section_buffer (&data->gdb_index);
16360
16361 for (ix = 0;
16362 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16363 ++ix)
16364 munmap_section_buffer (section);
16365
16366 VEC_free (dwarf2_section_info_def, data->types);
16367 }
16368
16369 \f
16370 /* The "save gdb-index" command. */
16371
16372 /* The contents of the hash table we create when building the string
16373 table. */
16374 struct strtab_entry
16375 {
16376 offset_type offset;
16377 const char *str;
16378 };
16379
16380 /* Hash function for a strtab_entry.
16381
16382 Function is used only during write_hash_table so no index format backward
16383 compatibility is needed. */
16384
16385 static hashval_t
16386 hash_strtab_entry (const void *e)
16387 {
16388 const struct strtab_entry *entry = e;
16389 return mapped_index_string_hash (INT_MAX, entry->str);
16390 }
16391
16392 /* Equality function for a strtab_entry. */
16393
16394 static int
16395 eq_strtab_entry (const void *a, const void *b)
16396 {
16397 const struct strtab_entry *ea = a;
16398 const struct strtab_entry *eb = b;
16399 return !strcmp (ea->str, eb->str);
16400 }
16401
16402 /* Create a strtab_entry hash table. */
16403
16404 static htab_t
16405 create_strtab (void)
16406 {
16407 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16408 xfree, xcalloc, xfree);
16409 }
16410
16411 /* Add a string to the constant pool. Return the string's offset in
16412 host order. */
16413
16414 static offset_type
16415 add_string (htab_t table, struct obstack *cpool, const char *str)
16416 {
16417 void **slot;
16418 struct strtab_entry entry;
16419 struct strtab_entry *result;
16420
16421 entry.str = str;
16422 slot = htab_find_slot (table, &entry, INSERT);
16423 if (*slot)
16424 result = *slot;
16425 else
16426 {
16427 result = XNEW (struct strtab_entry);
16428 result->offset = obstack_object_size (cpool);
16429 result->str = str;
16430 obstack_grow_str0 (cpool, str);
16431 *slot = result;
16432 }
16433 return result->offset;
16434 }
16435
16436 /* An entry in the symbol table. */
16437 struct symtab_index_entry
16438 {
16439 /* The name of the symbol. */
16440 const char *name;
16441 /* The offset of the name in the constant pool. */
16442 offset_type index_offset;
16443 /* A sorted vector of the indices of all the CUs that hold an object
16444 of this name. */
16445 VEC (offset_type) *cu_indices;
16446 };
16447
16448 /* The symbol table. This is a power-of-2-sized hash table. */
16449 struct mapped_symtab
16450 {
16451 offset_type n_elements;
16452 offset_type size;
16453 struct symtab_index_entry **data;
16454 };
16455
16456 /* Hash function for a symtab_index_entry. */
16457
16458 static hashval_t
16459 hash_symtab_entry (const void *e)
16460 {
16461 const struct symtab_index_entry *entry = e;
16462 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16463 sizeof (offset_type) * VEC_length (offset_type,
16464 entry->cu_indices),
16465 0);
16466 }
16467
16468 /* Equality function for a symtab_index_entry. */
16469
16470 static int
16471 eq_symtab_entry (const void *a, const void *b)
16472 {
16473 const struct symtab_index_entry *ea = a;
16474 const struct symtab_index_entry *eb = b;
16475 int len = VEC_length (offset_type, ea->cu_indices);
16476 if (len != VEC_length (offset_type, eb->cu_indices))
16477 return 0;
16478 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16479 VEC_address (offset_type, eb->cu_indices),
16480 sizeof (offset_type) * len);
16481 }
16482
16483 /* Destroy a symtab_index_entry. */
16484
16485 static void
16486 delete_symtab_entry (void *p)
16487 {
16488 struct symtab_index_entry *entry = p;
16489 VEC_free (offset_type, entry->cu_indices);
16490 xfree (entry);
16491 }
16492
16493 /* Create a hash table holding symtab_index_entry objects. */
16494
16495 static htab_t
16496 create_symbol_hash_table (void)
16497 {
16498 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16499 delete_symtab_entry, xcalloc, xfree);
16500 }
16501
16502 /* Create a new mapped symtab object. */
16503
16504 static struct mapped_symtab *
16505 create_mapped_symtab (void)
16506 {
16507 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16508 symtab->n_elements = 0;
16509 symtab->size = 1024;
16510 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16511 return symtab;
16512 }
16513
16514 /* Destroy a mapped_symtab. */
16515
16516 static void
16517 cleanup_mapped_symtab (void *p)
16518 {
16519 struct mapped_symtab *symtab = p;
16520 /* The contents of the array are freed when the other hash table is
16521 destroyed. */
16522 xfree (symtab->data);
16523 xfree (symtab);
16524 }
16525
16526 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16527 the slot.
16528
16529 Function is used only during write_hash_table so no index format backward
16530 compatibility is needed. */
16531
16532 static struct symtab_index_entry **
16533 find_slot (struct mapped_symtab *symtab, const char *name)
16534 {
16535 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16536
16537 index = hash & (symtab->size - 1);
16538 step = ((hash * 17) & (symtab->size - 1)) | 1;
16539
16540 for (;;)
16541 {
16542 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16543 return &symtab->data[index];
16544 index = (index + step) & (symtab->size - 1);
16545 }
16546 }
16547
16548 /* Expand SYMTAB's hash table. */
16549
16550 static void
16551 hash_expand (struct mapped_symtab *symtab)
16552 {
16553 offset_type old_size = symtab->size;
16554 offset_type i;
16555 struct symtab_index_entry **old_entries = symtab->data;
16556
16557 symtab->size *= 2;
16558 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16559
16560 for (i = 0; i < old_size; ++i)
16561 {
16562 if (old_entries[i])
16563 {
16564 struct symtab_index_entry **slot = find_slot (symtab,
16565 old_entries[i]->name);
16566 *slot = old_entries[i];
16567 }
16568 }
16569
16570 xfree (old_entries);
16571 }
16572
16573 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16574 is the index of the CU in which the symbol appears. */
16575
16576 static void
16577 add_index_entry (struct mapped_symtab *symtab, const char *name,
16578 offset_type cu_index)
16579 {
16580 struct symtab_index_entry **slot;
16581
16582 ++symtab->n_elements;
16583 if (4 * symtab->n_elements / 3 >= symtab->size)
16584 hash_expand (symtab);
16585
16586 slot = find_slot (symtab, name);
16587 if (!*slot)
16588 {
16589 *slot = XNEW (struct symtab_index_entry);
16590 (*slot)->name = name;
16591 (*slot)->cu_indices = NULL;
16592 }
16593 /* Don't push an index twice. Due to how we add entries we only
16594 have to check the last one. */
16595 if (VEC_empty (offset_type, (*slot)->cu_indices)
16596 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16597 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16598 }
16599
16600 /* Add a vector of indices to the constant pool. */
16601
16602 static offset_type
16603 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16604 struct symtab_index_entry *entry)
16605 {
16606 void **slot;
16607
16608 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16609 if (!*slot)
16610 {
16611 offset_type len = VEC_length (offset_type, entry->cu_indices);
16612 offset_type val = MAYBE_SWAP (len);
16613 offset_type iter;
16614 int i;
16615
16616 *slot = entry;
16617 entry->index_offset = obstack_object_size (cpool);
16618
16619 obstack_grow (cpool, &val, sizeof (val));
16620 for (i = 0;
16621 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16622 ++i)
16623 {
16624 val = MAYBE_SWAP (iter);
16625 obstack_grow (cpool, &val, sizeof (val));
16626 }
16627 }
16628 else
16629 {
16630 struct symtab_index_entry *old_entry = *slot;
16631 entry->index_offset = old_entry->index_offset;
16632 entry = old_entry;
16633 }
16634 return entry->index_offset;
16635 }
16636
16637 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16638 constant pool entries going into the obstack CPOOL. */
16639
16640 static void
16641 write_hash_table (struct mapped_symtab *symtab,
16642 struct obstack *output, struct obstack *cpool)
16643 {
16644 offset_type i;
16645 htab_t symbol_hash_table;
16646 htab_t str_table;
16647
16648 symbol_hash_table = create_symbol_hash_table ();
16649 str_table = create_strtab ();
16650
16651 /* We add all the index vectors to the constant pool first, to
16652 ensure alignment is ok. */
16653 for (i = 0; i < symtab->size; ++i)
16654 {
16655 if (symtab->data[i])
16656 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16657 }
16658
16659 /* Now write out the hash table. */
16660 for (i = 0; i < symtab->size; ++i)
16661 {
16662 offset_type str_off, vec_off;
16663
16664 if (symtab->data[i])
16665 {
16666 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16667 vec_off = symtab->data[i]->index_offset;
16668 }
16669 else
16670 {
16671 /* While 0 is a valid constant pool index, it is not valid
16672 to have 0 for both offsets. */
16673 str_off = 0;
16674 vec_off = 0;
16675 }
16676
16677 str_off = MAYBE_SWAP (str_off);
16678 vec_off = MAYBE_SWAP (vec_off);
16679
16680 obstack_grow (output, &str_off, sizeof (str_off));
16681 obstack_grow (output, &vec_off, sizeof (vec_off));
16682 }
16683
16684 htab_delete (str_table);
16685 htab_delete (symbol_hash_table);
16686 }
16687
16688 /* Struct to map psymtab to CU index in the index file. */
16689 struct psymtab_cu_index_map
16690 {
16691 struct partial_symtab *psymtab;
16692 unsigned int cu_index;
16693 };
16694
16695 static hashval_t
16696 hash_psymtab_cu_index (const void *item)
16697 {
16698 const struct psymtab_cu_index_map *map = item;
16699
16700 return htab_hash_pointer (map->psymtab);
16701 }
16702
16703 static int
16704 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16705 {
16706 const struct psymtab_cu_index_map *lhs = item_lhs;
16707 const struct psymtab_cu_index_map *rhs = item_rhs;
16708
16709 return lhs->psymtab == rhs->psymtab;
16710 }
16711
16712 /* Helper struct for building the address table. */
16713 struct addrmap_index_data
16714 {
16715 struct objfile *objfile;
16716 struct obstack *addr_obstack;
16717 htab_t cu_index_htab;
16718
16719 /* Non-zero if the previous_* fields are valid.
16720 We can't write an entry until we see the next entry (since it is only then
16721 that we know the end of the entry). */
16722 int previous_valid;
16723 /* Index of the CU in the table of all CUs in the index file. */
16724 unsigned int previous_cu_index;
16725 /* Start address of the CU. */
16726 CORE_ADDR previous_cu_start;
16727 };
16728
16729 /* Write an address entry to OBSTACK. */
16730
16731 static void
16732 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16733 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16734 {
16735 offset_type cu_index_to_write;
16736 char addr[8];
16737 CORE_ADDR baseaddr;
16738
16739 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16740
16741 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16742 obstack_grow (obstack, addr, 8);
16743 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16744 obstack_grow (obstack, addr, 8);
16745 cu_index_to_write = MAYBE_SWAP (cu_index);
16746 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16747 }
16748
16749 /* Worker function for traversing an addrmap to build the address table. */
16750
16751 static int
16752 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16753 {
16754 struct addrmap_index_data *data = datap;
16755 struct partial_symtab *pst = obj;
16756 offset_type cu_index;
16757 void **slot;
16758
16759 if (data->previous_valid)
16760 add_address_entry (data->objfile, data->addr_obstack,
16761 data->previous_cu_start, start_addr,
16762 data->previous_cu_index);
16763
16764 data->previous_cu_start = start_addr;
16765 if (pst != NULL)
16766 {
16767 struct psymtab_cu_index_map find_map, *map;
16768 find_map.psymtab = pst;
16769 map = htab_find (data->cu_index_htab, &find_map);
16770 gdb_assert (map != NULL);
16771 data->previous_cu_index = map->cu_index;
16772 data->previous_valid = 1;
16773 }
16774 else
16775 data->previous_valid = 0;
16776
16777 return 0;
16778 }
16779
16780 /* Write OBJFILE's address map to OBSTACK.
16781 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16782 in the index file. */
16783
16784 static void
16785 write_address_map (struct objfile *objfile, struct obstack *obstack,
16786 htab_t cu_index_htab)
16787 {
16788 struct addrmap_index_data addrmap_index_data;
16789
16790 /* When writing the address table, we have to cope with the fact that
16791 the addrmap iterator only provides the start of a region; we have to
16792 wait until the next invocation to get the start of the next region. */
16793
16794 addrmap_index_data.objfile = objfile;
16795 addrmap_index_data.addr_obstack = obstack;
16796 addrmap_index_data.cu_index_htab = cu_index_htab;
16797 addrmap_index_data.previous_valid = 0;
16798
16799 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16800 &addrmap_index_data);
16801
16802 /* It's highly unlikely the last entry (end address = 0xff...ff)
16803 is valid, but we should still handle it.
16804 The end address is recorded as the start of the next region, but that
16805 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16806 anyway. */
16807 if (addrmap_index_data.previous_valid)
16808 add_address_entry (objfile, obstack,
16809 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16810 addrmap_index_data.previous_cu_index);
16811 }
16812
16813 /* Add a list of partial symbols to SYMTAB. */
16814
16815 static void
16816 write_psymbols (struct mapped_symtab *symtab,
16817 htab_t psyms_seen,
16818 struct partial_symbol **psymp,
16819 int count,
16820 offset_type cu_index,
16821 int is_static)
16822 {
16823 for (; count-- > 0; ++psymp)
16824 {
16825 void **slot, *lookup;
16826
16827 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16828 error (_("Ada is not currently supported by the index"));
16829
16830 /* We only want to add a given psymbol once. However, we also
16831 want to account for whether it is global or static. So, we
16832 may add it twice, using slightly different values. */
16833 if (is_static)
16834 {
16835 uintptr_t val = 1 | (uintptr_t) *psymp;
16836
16837 lookup = (void *) val;
16838 }
16839 else
16840 lookup = *psymp;
16841
16842 /* Only add a given psymbol once. */
16843 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16844 if (!*slot)
16845 {
16846 *slot = lookup;
16847 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16848 }
16849 }
16850 }
16851
16852 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16853 exception if there is an error. */
16854
16855 static void
16856 write_obstack (FILE *file, struct obstack *obstack)
16857 {
16858 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16859 file)
16860 != obstack_object_size (obstack))
16861 error (_("couldn't data write to file"));
16862 }
16863
16864 /* Unlink a file if the argument is not NULL. */
16865
16866 static void
16867 unlink_if_set (void *p)
16868 {
16869 char **filename = p;
16870 if (*filename)
16871 unlink (*filename);
16872 }
16873
16874 /* A helper struct used when iterating over debug_types. */
16875 struct signatured_type_index_data
16876 {
16877 struct objfile *objfile;
16878 struct mapped_symtab *symtab;
16879 struct obstack *types_list;
16880 htab_t psyms_seen;
16881 int cu_index;
16882 };
16883
16884 /* A helper function that writes a single signatured_type to an
16885 obstack. */
16886
16887 static int
16888 write_one_signatured_type (void **slot, void *d)
16889 {
16890 struct signatured_type_index_data *info = d;
16891 struct signatured_type *entry = (struct signatured_type *) *slot;
16892 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16893 struct partial_symtab *psymtab = per_cu->v.psymtab;
16894 gdb_byte val[8];
16895
16896 write_psymbols (info->symtab,
16897 info->psyms_seen,
16898 info->objfile->global_psymbols.list
16899 + psymtab->globals_offset,
16900 psymtab->n_global_syms, info->cu_index,
16901 0);
16902 write_psymbols (info->symtab,
16903 info->psyms_seen,
16904 info->objfile->static_psymbols.list
16905 + psymtab->statics_offset,
16906 psymtab->n_static_syms, info->cu_index,
16907 1);
16908
16909 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16910 obstack_grow (info->types_list, val, 8);
16911 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16912 obstack_grow (info->types_list, val, 8);
16913 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16914 obstack_grow (info->types_list, val, 8);
16915
16916 ++info->cu_index;
16917
16918 return 1;
16919 }
16920
16921 /* Create an index file for OBJFILE in the directory DIR. */
16922
16923 static void
16924 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16925 {
16926 struct cleanup *cleanup;
16927 char *filename, *cleanup_filename;
16928 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16929 struct obstack cu_list, types_cu_list;
16930 int i;
16931 FILE *out_file;
16932 struct mapped_symtab *symtab;
16933 offset_type val, size_of_contents, total_len;
16934 struct stat st;
16935 char buf[8];
16936 htab_t psyms_seen;
16937 htab_t cu_index_htab;
16938 struct psymtab_cu_index_map *psymtab_cu_index_map;
16939
16940 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16941 return;
16942
16943 if (dwarf2_per_objfile->using_index)
16944 error (_("Cannot use an index to create the index"));
16945
16946 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16947 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16948
16949 if (stat (objfile->name, &st) < 0)
16950 perror_with_name (objfile->name);
16951
16952 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16953 INDEX_SUFFIX, (char *) NULL);
16954 cleanup = make_cleanup (xfree, filename);
16955
16956 out_file = fopen (filename, "wb");
16957 if (!out_file)
16958 error (_("Can't open `%s' for writing"), filename);
16959
16960 cleanup_filename = filename;
16961 make_cleanup (unlink_if_set, &cleanup_filename);
16962
16963 symtab = create_mapped_symtab ();
16964 make_cleanup (cleanup_mapped_symtab, symtab);
16965
16966 obstack_init (&addr_obstack);
16967 make_cleanup_obstack_free (&addr_obstack);
16968
16969 obstack_init (&cu_list);
16970 make_cleanup_obstack_free (&cu_list);
16971
16972 obstack_init (&types_cu_list);
16973 make_cleanup_obstack_free (&types_cu_list);
16974
16975 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16976 NULL, xcalloc, xfree);
16977 make_cleanup_htab_delete (psyms_seen);
16978
16979 /* While we're scanning CU's create a table that maps a psymtab pointer
16980 (which is what addrmap records) to its index (which is what is recorded
16981 in the index file). This will later be needed to write the address
16982 table. */
16983 cu_index_htab = htab_create_alloc (100,
16984 hash_psymtab_cu_index,
16985 eq_psymtab_cu_index,
16986 NULL, xcalloc, xfree);
16987 make_cleanup_htab_delete (cu_index_htab);
16988 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16989 xmalloc (sizeof (struct psymtab_cu_index_map)
16990 * dwarf2_per_objfile->n_comp_units);
16991 make_cleanup (xfree, psymtab_cu_index_map);
16992
16993 /* The CU list is already sorted, so we don't need to do additional
16994 work here. Also, the debug_types entries do not appear in
16995 all_comp_units, but only in their own hash table. */
16996 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16997 {
16998 struct dwarf2_per_cu_data *per_cu
16999 = dwarf2_per_objfile->all_comp_units[i];
17000 struct partial_symtab *psymtab = per_cu->v.psymtab;
17001 gdb_byte val[8];
17002 struct psymtab_cu_index_map *map;
17003 void **slot;
17004
17005 write_psymbols (symtab,
17006 psyms_seen,
17007 objfile->global_psymbols.list + psymtab->globals_offset,
17008 psymtab->n_global_syms, i,
17009 0);
17010 write_psymbols (symtab,
17011 psyms_seen,
17012 objfile->static_psymbols.list + psymtab->statics_offset,
17013 psymtab->n_static_syms, i,
17014 1);
17015
17016 map = &psymtab_cu_index_map[i];
17017 map->psymtab = psymtab;
17018 map->cu_index = i;
17019 slot = htab_find_slot (cu_index_htab, map, INSERT);
17020 gdb_assert (slot != NULL);
17021 gdb_assert (*slot == NULL);
17022 *slot = map;
17023
17024 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
17025 obstack_grow (&cu_list, val, 8);
17026 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17027 obstack_grow (&cu_list, val, 8);
17028 }
17029
17030 /* Dump the address map. */
17031 write_address_map (objfile, &addr_obstack, cu_index_htab);
17032
17033 /* Write out the .debug_type entries, if any. */
17034 if (dwarf2_per_objfile->signatured_types)
17035 {
17036 struct signatured_type_index_data sig_data;
17037
17038 sig_data.objfile = objfile;
17039 sig_data.symtab = symtab;
17040 sig_data.types_list = &types_cu_list;
17041 sig_data.psyms_seen = psyms_seen;
17042 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17043 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17044 write_one_signatured_type, &sig_data);
17045 }
17046
17047 obstack_init (&constant_pool);
17048 make_cleanup_obstack_free (&constant_pool);
17049 obstack_init (&symtab_obstack);
17050 make_cleanup_obstack_free (&symtab_obstack);
17051 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17052
17053 obstack_init (&contents);
17054 make_cleanup_obstack_free (&contents);
17055 size_of_contents = 6 * sizeof (offset_type);
17056 total_len = size_of_contents;
17057
17058 /* The version number. */
17059 val = MAYBE_SWAP (5);
17060 obstack_grow (&contents, &val, sizeof (val));
17061
17062 /* The offset of the CU list from the start of the file. */
17063 val = MAYBE_SWAP (total_len);
17064 obstack_grow (&contents, &val, sizeof (val));
17065 total_len += obstack_object_size (&cu_list);
17066
17067 /* The offset of the types CU list from the start of the file. */
17068 val = MAYBE_SWAP (total_len);
17069 obstack_grow (&contents, &val, sizeof (val));
17070 total_len += obstack_object_size (&types_cu_list);
17071
17072 /* The offset of the address table from the start of the file. */
17073 val = MAYBE_SWAP (total_len);
17074 obstack_grow (&contents, &val, sizeof (val));
17075 total_len += obstack_object_size (&addr_obstack);
17076
17077 /* The offset of the symbol table from the start of the file. */
17078 val = MAYBE_SWAP (total_len);
17079 obstack_grow (&contents, &val, sizeof (val));
17080 total_len += obstack_object_size (&symtab_obstack);
17081
17082 /* The offset of the constant pool from the start of the file. */
17083 val = MAYBE_SWAP (total_len);
17084 obstack_grow (&contents, &val, sizeof (val));
17085 total_len += obstack_object_size (&constant_pool);
17086
17087 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17088
17089 write_obstack (out_file, &contents);
17090 write_obstack (out_file, &cu_list);
17091 write_obstack (out_file, &types_cu_list);
17092 write_obstack (out_file, &addr_obstack);
17093 write_obstack (out_file, &symtab_obstack);
17094 write_obstack (out_file, &constant_pool);
17095
17096 fclose (out_file);
17097
17098 /* We want to keep the file, so we set cleanup_filename to NULL
17099 here. See unlink_if_set. */
17100 cleanup_filename = NULL;
17101
17102 do_cleanups (cleanup);
17103 }
17104
17105 /* Implementation of the `save gdb-index' command.
17106
17107 Note that the file format used by this command is documented in the
17108 GDB manual. Any changes here must be documented there. */
17109
17110 static void
17111 save_gdb_index_command (char *arg, int from_tty)
17112 {
17113 struct objfile *objfile;
17114
17115 if (!arg || !*arg)
17116 error (_("usage: save gdb-index DIRECTORY"));
17117
17118 ALL_OBJFILES (objfile)
17119 {
17120 struct stat st;
17121
17122 /* If the objfile does not correspond to an actual file, skip it. */
17123 if (stat (objfile->name, &st) < 0)
17124 continue;
17125
17126 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17127 if (dwarf2_per_objfile)
17128 {
17129 volatile struct gdb_exception except;
17130
17131 TRY_CATCH (except, RETURN_MASK_ERROR)
17132 {
17133 write_psymtabs_to_index (objfile, arg);
17134 }
17135 if (except.reason < 0)
17136 exception_fprintf (gdb_stderr, except,
17137 _("Error while writing index for `%s': "),
17138 objfile->name);
17139 }
17140 }
17141 }
17142
17143 \f
17144
17145 int dwarf2_always_disassemble;
17146
17147 static void
17148 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17149 struct cmd_list_element *c, const char *value)
17150 {
17151 fprintf_filtered (file,
17152 _("Whether to always disassemble "
17153 "DWARF expressions is %s.\n"),
17154 value);
17155 }
17156
17157 static void
17158 show_check_physname (struct ui_file *file, int from_tty,
17159 struct cmd_list_element *c, const char *value)
17160 {
17161 fprintf_filtered (file,
17162 _("Whether to check \"physname\" is %s.\n"),
17163 value);
17164 }
17165
17166 void _initialize_dwarf2_read (void);
17167
17168 void
17169 _initialize_dwarf2_read (void)
17170 {
17171 struct cmd_list_element *c;
17172
17173 dwarf2_objfile_data_key
17174 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17175
17176 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17177 Set DWARF 2 specific variables.\n\
17178 Configure DWARF 2 variables such as the cache size"),
17179 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17180 0/*allow-unknown*/, &maintenance_set_cmdlist);
17181
17182 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17183 Show DWARF 2 specific variables\n\
17184 Show DWARF 2 variables such as the cache size"),
17185 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17186 0/*allow-unknown*/, &maintenance_show_cmdlist);
17187
17188 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17189 &dwarf2_max_cache_age, _("\
17190 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17191 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17192 A higher limit means that cached compilation units will be stored\n\
17193 in memory longer, and more total memory will be used. Zero disables\n\
17194 caching, which can slow down startup."),
17195 NULL,
17196 show_dwarf2_max_cache_age,
17197 &set_dwarf2_cmdlist,
17198 &show_dwarf2_cmdlist);
17199
17200 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17201 &dwarf2_always_disassemble, _("\
17202 Set whether `info address' always disassembles DWARF expressions."), _("\
17203 Show whether `info address' always disassembles DWARF expressions."), _("\
17204 When enabled, DWARF expressions are always printed in an assembly-like\n\
17205 syntax. When disabled, expressions will be printed in a more\n\
17206 conversational style, when possible."),
17207 NULL,
17208 show_dwarf2_always_disassemble,
17209 &set_dwarf2_cmdlist,
17210 &show_dwarf2_cmdlist);
17211
17212 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17213 Set debugging of the dwarf2 DIE reader."), _("\
17214 Show debugging of the dwarf2 DIE reader."), _("\
17215 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17216 The value is the maximum depth to print."),
17217 NULL,
17218 NULL,
17219 &setdebuglist, &showdebuglist);
17220
17221 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17222 Set cross-checking of \"physname\" code against demangler."), _("\
17223 Show cross-checking of \"physname\" code against demangler."), _("\
17224 When enabled, GDB's internal \"physname\" code is checked against\n\
17225 the demangler."),
17226 NULL, show_check_physname,
17227 &setdebuglist, &showdebuglist);
17228
17229 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17230 _("\
17231 Save a gdb-index file.\n\
17232 Usage: save gdb-index DIRECTORY"),
17233 &save_cmdlist);
17234 set_cmd_completer (c, filename_completer);
17235 }
This page took 0.442052 seconds and 4 git commands to generate.