31ea14e10909828519dd953356eff0a37ceff222
[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 /* The language we are debugging. */
350 enum language language;
351 const struct language_defn *language_defn;
352
353 const char *producer;
354
355 /* The generic symbol table building routines have separate lists for
356 file scope symbols and all all other scopes (local scopes). So
357 we need to select the right one to pass to add_symbol_to_list().
358 We do it by keeping a pointer to the correct list in list_in_scope.
359
360 FIXME: The original dwarf code just treated the file scope as the
361 first local scope, and all other local scopes as nested local
362 scopes, and worked fine. Check to see if we really need to
363 distinguish these in buildsym.c. */
364 struct pending **list_in_scope;
365
366 /* DWARF abbreviation table associated with this compilation unit. */
367 struct abbrev_info **dwarf2_abbrevs;
368
369 /* Storage for the abbrev table. */
370 struct obstack abbrev_obstack;
371
372 /* Hash table holding all the loaded partial DIEs. */
373 htab_t partial_dies;
374
375 /* Storage for things with the same lifetime as this read-in compilation
376 unit, including partial DIEs. */
377 struct obstack comp_unit_obstack;
378
379 /* When multiple dwarf2_cu structures are living in memory, this field
380 chains them all together, so that they can be released efficiently.
381 We will probably also want a generation counter so that most-recently-used
382 compilation units are cached... */
383 struct dwarf2_per_cu_data *read_in_chain;
384
385 /* Backchain to our per_cu entry if the tree has been built. */
386 struct dwarf2_per_cu_data *per_cu;
387
388 /* How many compilation units ago was this CU last referenced? */
389 int last_used;
390
391 /* A hash table of die offsets for following references. */
392 htab_t die_hash;
393
394 /* Full DIEs if read in. */
395 struct die_info *dies;
396
397 /* A set of pointers to dwarf2_per_cu_data objects for compilation
398 units referenced by this one. Only set during full symbol processing;
399 partial symbol tables do not have dependencies. */
400 htab_t dependencies;
401
402 /* Header data from the line table, during full symbol processing. */
403 struct line_header *line_header;
404
405 /* A list of methods which need to have physnames computed
406 after all type information has been read. */
407 VEC (delayed_method_info) *method_list;
408
409 /* To be copied to symtab->call_site_htab. */
410 htab_t call_site_htab;
411
412 /* Mark used when releasing cached dies. */
413 unsigned int mark : 1;
414
415 /* This flag will be set if this compilation unit might include
416 inter-compilation-unit references. */
417 unsigned int has_form_ref_addr : 1;
418
419 /* This flag will be set if this compilation unit includes any
420 DW_TAG_namespace DIEs. If we know that there are explicit
421 DIEs for namespaces, we don't need to try to infer them
422 from mangled names. */
423 unsigned int has_namespace_info : 1;
424
425 /* This CU references .debug_loc. See the symtab->locations_valid field.
426 This test is imperfect as there may exist optimized debug code not using
427 any location list and still facing inlining issues if handled as
428 unoptimized code. For a future better test see GCC PR other/32998. */
429
430 unsigned int has_loclist : 1;
431 };
432
433 /* Persistent data held for a compilation unit, even when not
434 processing it. We put a pointer to this structure in the
435 read_symtab_private field of the psymtab. If we encounter
436 inter-compilation-unit references, we also maintain a sorted
437 list of all compilation units. */
438
439 struct dwarf2_per_cu_data
440 {
441 /* The start offset and length of this compilation unit. 2**29-1
442 bytes should suffice to store the length of any compilation unit
443 - if it doesn't, GDB will fall over anyway.
444 NOTE: Unlike comp_unit_head.length, this length includes
445 initial_length_size. */
446 unsigned int offset;
447 unsigned int length : 29;
448
449 /* Flag indicating this compilation unit will be read in before
450 any of the current compilation units are processed. */
451 unsigned int queued : 1;
452
453 /* This flag will be set if we need to load absolutely all DIEs
454 for this compilation unit, instead of just the ones we think
455 are interesting. It gets set if we look for a DIE in the
456 hash table and don't find it. */
457 unsigned int load_all_dies : 1;
458
459 /* Non-null if this CU is from .debug_types; in which case it points
460 to the section. Otherwise it's from .debug_info. */
461 struct dwarf2_section_info *debug_types_section;
462
463 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
464 of the CU cache it gets reset to NULL again. */
465 struct dwarf2_cu *cu;
466
467 /* The corresponding objfile. */
468 struct objfile *objfile;
469
470 /* When using partial symbol tables, the 'psymtab' field is active.
471 Otherwise the 'quick' field is active. */
472 union
473 {
474 /* The partial symbol table associated with this compilation unit,
475 or NULL for partial units (which do not have an associated
476 symtab). */
477 struct partial_symtab *psymtab;
478
479 /* Data needed by the "quick" functions. */
480 struct dwarf2_per_cu_quick_data *quick;
481 } v;
482 };
483
484 /* Entry in the signatured_types hash table. */
485
486 struct signatured_type
487 {
488 ULONGEST signature;
489
490 /* Offset in .debug_types of the type defined by this TU. */
491 unsigned int type_offset;
492
493 /* The CU(/TU) of this type. */
494 struct dwarf2_per_cu_data per_cu;
495 };
496
497 /* Struct used to pass misc. parameters to read_die_and_children, et
498 al. which are used for both .debug_info and .debug_types dies.
499 All parameters here are unchanging for the life of the call. This
500 struct exists to abstract away the constant parameters of die
501 reading. */
502
503 struct die_reader_specs
504 {
505 /* The bfd of this objfile. */
506 bfd* abfd;
507
508 /* The CU of the DIE we are parsing. */
509 struct dwarf2_cu *cu;
510
511 /* Pointer to start of section buffer.
512 This is either the start of .debug_info or .debug_types. */
513 const gdb_byte *buffer;
514 };
515
516 /* The line number information for a compilation unit (found in the
517 .debug_line section) begins with a "statement program header",
518 which contains the following information. */
519 struct line_header
520 {
521 unsigned int total_length;
522 unsigned short version;
523 unsigned int header_length;
524 unsigned char minimum_instruction_length;
525 unsigned char maximum_ops_per_instruction;
526 unsigned char default_is_stmt;
527 int line_base;
528 unsigned char line_range;
529 unsigned char opcode_base;
530
531 /* standard_opcode_lengths[i] is the number of operands for the
532 standard opcode whose value is i. This means that
533 standard_opcode_lengths[0] is unused, and the last meaningful
534 element is standard_opcode_lengths[opcode_base - 1]. */
535 unsigned char *standard_opcode_lengths;
536
537 /* The include_directories table. NOTE! These strings are not
538 allocated with xmalloc; instead, they are pointers into
539 debug_line_buffer. If you try to free them, `free' will get
540 indigestion. */
541 unsigned int num_include_dirs, include_dirs_size;
542 char **include_dirs;
543
544 /* The file_names table. NOTE! These strings are not allocated
545 with xmalloc; instead, they are pointers into debug_line_buffer.
546 Don't try to free them directly. */
547 unsigned int num_file_names, file_names_size;
548 struct file_entry
549 {
550 char *name;
551 unsigned int dir_index;
552 unsigned int mod_time;
553 unsigned int length;
554 int included_p; /* Non-zero if referenced by the Line Number Program. */
555 struct symtab *symtab; /* The associated symbol table, if any. */
556 } *file_names;
557
558 /* The start and end of the statement program following this
559 header. These point into dwarf2_per_objfile->line_buffer. */
560 gdb_byte *statement_program_start, *statement_program_end;
561 };
562
563 /* When we construct a partial symbol table entry we only
564 need this much information. */
565 struct partial_die_info
566 {
567 /* Offset of this DIE. */
568 unsigned int offset;
569
570 /* DWARF-2 tag for this DIE. */
571 ENUM_BITFIELD(dwarf_tag) tag : 16;
572
573 /* Assorted flags describing the data found in this DIE. */
574 unsigned int has_children : 1;
575 unsigned int is_external : 1;
576 unsigned int is_declaration : 1;
577 unsigned int has_type : 1;
578 unsigned int has_specification : 1;
579 unsigned int has_pc_info : 1;
580
581 /* Flag set if the SCOPE field of this structure has been
582 computed. */
583 unsigned int scope_set : 1;
584
585 /* Flag set if the DIE has a byte_size attribute. */
586 unsigned int has_byte_size : 1;
587
588 /* Flag set if any of the DIE's children are template arguments. */
589 unsigned int has_template_arguments : 1;
590
591 /* Flag set if fixup_partial_die has been called on this die. */
592 unsigned int fixup_called : 1;
593
594 /* The name of this DIE. Normally the value of DW_AT_name, but
595 sometimes a default name for unnamed DIEs. */
596 char *name;
597
598 /* The linkage name, if present. */
599 const char *linkage_name;
600
601 /* The scope to prepend to our children. This is generally
602 allocated on the comp_unit_obstack, so will disappear
603 when this compilation unit leaves the cache. */
604 char *scope;
605
606 /* The location description associated with this DIE, if any. */
607 struct dwarf_block *locdesc;
608
609 /* If HAS_PC_INFO, the PC range associated with this DIE. */
610 CORE_ADDR lowpc;
611 CORE_ADDR highpc;
612
613 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
614 DW_AT_sibling, if any. */
615 /* NOTE: This member isn't strictly necessary, read_partial_die could
616 return DW_AT_sibling values to its caller load_partial_dies. */
617 gdb_byte *sibling;
618
619 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
620 DW_AT_specification (or DW_AT_abstract_origin or
621 DW_AT_extension). */
622 unsigned int spec_offset;
623
624 /* Pointers to this DIE's parent, first child, and next sibling,
625 if any. */
626 struct partial_die_info *die_parent, *die_child, *die_sibling;
627 };
628
629 /* This data structure holds the information of an abbrev. */
630 struct abbrev_info
631 {
632 unsigned int number; /* number identifying abbrev */
633 enum dwarf_tag tag; /* dwarf tag */
634 unsigned short has_children; /* boolean */
635 unsigned short num_attrs; /* number of attributes */
636 struct attr_abbrev *attrs; /* an array of attribute descriptions */
637 struct abbrev_info *next; /* next in chain */
638 };
639
640 struct attr_abbrev
641 {
642 ENUM_BITFIELD(dwarf_attribute) name : 16;
643 ENUM_BITFIELD(dwarf_form) form : 16;
644 };
645
646 /* Attributes have a name and a value. */
647 struct attribute
648 {
649 ENUM_BITFIELD(dwarf_attribute) name : 16;
650 ENUM_BITFIELD(dwarf_form) form : 15;
651
652 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
653 field should be in u.str (existing only for DW_STRING) but it is kept
654 here for better struct attribute alignment. */
655 unsigned int string_is_canonical : 1;
656
657 union
658 {
659 char *str;
660 struct dwarf_block *blk;
661 ULONGEST unsnd;
662 LONGEST snd;
663 CORE_ADDR addr;
664 struct signatured_type *signatured_type;
665 }
666 u;
667 };
668
669 /* This data structure holds a complete die structure. */
670 struct die_info
671 {
672 /* DWARF-2 tag for this DIE. */
673 ENUM_BITFIELD(dwarf_tag) tag : 16;
674
675 /* Number of attributes */
676 unsigned char num_attrs;
677
678 /* True if we're presently building the full type name for the
679 type derived from this DIE. */
680 unsigned char building_fullname : 1;
681
682 /* Abbrev number */
683 unsigned int abbrev;
684
685 /* Offset in .debug_info or .debug_types section. */
686 unsigned int offset;
687
688 /* The dies in a compilation unit form an n-ary tree. PARENT
689 points to this die's parent; CHILD points to the first child of
690 this node; and all the children of a given node are chained
691 together via their SIBLING fields. */
692 struct die_info *child; /* Its first child, if any. */
693 struct die_info *sibling; /* Its next sibling, if any. */
694 struct die_info *parent; /* Its parent, if any. */
695
696 /* An array of attributes, with NUM_ATTRS elements. There may be
697 zero, but it's not common and zero-sized arrays are not
698 sufficiently portable C. */
699 struct attribute attrs[1];
700 };
701
702 /* Get at parts of an attribute structure. */
703
704 #define DW_STRING(attr) ((attr)->u.str)
705 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
706 #define DW_UNSND(attr) ((attr)->u.unsnd)
707 #define DW_BLOCK(attr) ((attr)->u.blk)
708 #define DW_SND(attr) ((attr)->u.snd)
709 #define DW_ADDR(attr) ((attr)->u.addr)
710 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
711
712 /* Blocks are a bunch of untyped bytes. */
713 struct dwarf_block
714 {
715 unsigned int size;
716
717 /* Valid only if SIZE is not zero. */
718 gdb_byte *data;
719 };
720
721 #ifndef ATTR_ALLOC_CHUNK
722 #define ATTR_ALLOC_CHUNK 4
723 #endif
724
725 /* Allocate fields for structs, unions and enums in this size. */
726 #ifndef DW_FIELD_ALLOC_CHUNK
727 #define DW_FIELD_ALLOC_CHUNK 4
728 #endif
729
730 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
731 but this would require a corresponding change in unpack_field_as_long
732 and friends. */
733 static int bits_per_byte = 8;
734
735 /* The routines that read and process dies for a C struct or C++ class
736 pass lists of data member fields and lists of member function fields
737 in an instance of a field_info structure, as defined below. */
738 struct field_info
739 {
740 /* List of data member and baseclasses fields. */
741 struct nextfield
742 {
743 struct nextfield *next;
744 int accessibility;
745 int virtuality;
746 struct field field;
747 }
748 *fields, *baseclasses;
749
750 /* Number of fields (including baseclasses). */
751 int nfields;
752
753 /* Number of baseclasses. */
754 int nbaseclasses;
755
756 /* Set if the accesibility of one of the fields is not public. */
757 int non_public_fields;
758
759 /* Member function fields array, entries are allocated in the order they
760 are encountered in the object file. */
761 struct nextfnfield
762 {
763 struct nextfnfield *next;
764 struct fn_field fnfield;
765 }
766 *fnfields;
767
768 /* Member function fieldlist array, contains name of possibly overloaded
769 member function, number of overloaded member functions and a pointer
770 to the head of the member function field chain. */
771 struct fnfieldlist
772 {
773 char *name;
774 int length;
775 struct nextfnfield *head;
776 }
777 *fnfieldlists;
778
779 /* Number of entries in the fnfieldlists array. */
780 int nfnfields;
781
782 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
783 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
784 struct typedef_field_list
785 {
786 struct typedef_field field;
787 struct typedef_field_list *next;
788 }
789 *typedef_field_list;
790 unsigned typedef_field_list_count;
791 };
792
793 /* One item on the queue of compilation units to read in full symbols
794 for. */
795 struct dwarf2_queue_item
796 {
797 struct dwarf2_per_cu_data *per_cu;
798 struct dwarf2_queue_item *next;
799 };
800
801 /* The current queue. */
802 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
803
804 /* Loaded secondary compilation units are kept in memory until they
805 have not been referenced for the processing of this many
806 compilation units. Set this to zero to disable caching. Cache
807 sizes of up to at least twenty will improve startup time for
808 typical inter-CU-reference binaries, at an obvious memory cost. */
809 static int dwarf2_max_cache_age = 5;
810 static void
811 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
812 struct cmd_list_element *c, const char *value)
813 {
814 fprintf_filtered (file, _("The upper bound on the age of cached "
815 "dwarf2 compilation units is %s.\n"),
816 value);
817 }
818
819
820 /* Various complaints about symbol reading that don't abort the process. */
821
822 static void
823 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
824 {
825 complaint (&symfile_complaints,
826 _("statement list doesn't fit in .debug_line section"));
827 }
828
829 static void
830 dwarf2_debug_line_missing_file_complaint (void)
831 {
832 complaint (&symfile_complaints,
833 _(".debug_line section has line data without a file"));
834 }
835
836 static void
837 dwarf2_debug_line_missing_end_sequence_complaint (void)
838 {
839 complaint (&symfile_complaints,
840 _(".debug_line section has line "
841 "program sequence without an end"));
842 }
843
844 static void
845 dwarf2_complex_location_expr_complaint (void)
846 {
847 complaint (&symfile_complaints, _("location expression too complex"));
848 }
849
850 static void
851 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
852 int arg3)
853 {
854 complaint (&symfile_complaints,
855 _("const value length mismatch for '%s', got %d, expected %d"),
856 arg1, arg2, arg3);
857 }
858
859 static void
860 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
861 {
862 complaint (&symfile_complaints,
863 _("macro info runs off end of `%s' section"),
864 section->asection->name);
865 }
866
867 static void
868 dwarf2_macro_malformed_definition_complaint (const char *arg1)
869 {
870 complaint (&symfile_complaints,
871 _("macro debug info contains a "
872 "malformed macro definition:\n`%s'"),
873 arg1);
874 }
875
876 static void
877 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
878 {
879 complaint (&symfile_complaints,
880 _("invalid attribute class or form for '%s' in '%s'"),
881 arg1, arg2);
882 }
883
884 /* local function prototypes */
885
886 static void dwarf2_locate_sections (bfd *, asection *, void *);
887
888 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
889 struct objfile *);
890
891 static void dwarf2_find_base_address (struct die_info *die,
892 struct dwarf2_cu *cu);
893
894 static void dwarf2_build_psymtabs_hard (struct objfile *);
895
896 static void scan_partial_symbols (struct partial_die_info *,
897 CORE_ADDR *, CORE_ADDR *,
898 int, struct dwarf2_cu *);
899
900 static void add_partial_symbol (struct partial_die_info *,
901 struct dwarf2_cu *);
902
903 static void add_partial_namespace (struct partial_die_info *pdi,
904 CORE_ADDR *lowpc, CORE_ADDR *highpc,
905 int need_pc, struct dwarf2_cu *cu);
906
907 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
908 CORE_ADDR *highpc, int need_pc,
909 struct dwarf2_cu *cu);
910
911 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
912 struct dwarf2_cu *cu);
913
914 static void add_partial_subprogram (struct partial_die_info *pdi,
915 CORE_ADDR *lowpc, CORE_ADDR *highpc,
916 int need_pc, struct dwarf2_cu *cu);
917
918 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
919 gdb_byte *buffer, gdb_byte *info_ptr,
920 bfd *abfd, struct dwarf2_cu *cu);
921
922 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
923
924 static void psymtab_to_symtab_1 (struct partial_symtab *);
925
926 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
927
928 static void dwarf2_free_abbrev_table (void *);
929
930 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
931
932 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
933 struct dwarf2_cu *);
934
935 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
936 struct dwarf2_cu *);
937
938 static struct partial_die_info *load_partial_dies (bfd *,
939 gdb_byte *, gdb_byte *,
940 int, struct dwarf2_cu *);
941
942 static gdb_byte *read_partial_die (struct partial_die_info *,
943 struct abbrev_info *abbrev,
944 unsigned int, bfd *,
945 gdb_byte *, gdb_byte *,
946 struct dwarf2_cu *);
947
948 static struct partial_die_info *find_partial_die (unsigned int,
949 struct dwarf2_cu *);
950
951 static void fixup_partial_die (struct partial_die_info *,
952 struct dwarf2_cu *);
953
954 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
955 bfd *, gdb_byte *, struct dwarf2_cu *);
956
957 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
958 bfd *, gdb_byte *, struct dwarf2_cu *);
959
960 static unsigned int read_1_byte (bfd *, gdb_byte *);
961
962 static int read_1_signed_byte (bfd *, gdb_byte *);
963
964 static unsigned int read_2_bytes (bfd *, gdb_byte *);
965
966 static unsigned int read_4_bytes (bfd *, gdb_byte *);
967
968 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
969
970 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
971 unsigned int *);
972
973 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
974
975 static LONGEST read_checked_initial_length_and_offset
976 (bfd *, gdb_byte *, const struct comp_unit_head *,
977 unsigned int *, unsigned int *);
978
979 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
980 unsigned int *);
981
982 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
983
984 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
985
986 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
987
988 static char *read_indirect_string (bfd *, gdb_byte *,
989 const struct comp_unit_head *,
990 unsigned int *);
991
992 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
993
994 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
995
996 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
997
998 static void set_cu_language (unsigned int, struct dwarf2_cu *);
999
1000 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1001 struct dwarf2_cu *);
1002
1003 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1004 unsigned int,
1005 struct dwarf2_cu *);
1006
1007 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1008 struct dwarf2_cu *cu);
1009
1010 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1011
1012 static struct die_info *die_specification (struct die_info *die,
1013 struct dwarf2_cu **);
1014
1015 static void free_line_header (struct line_header *lh);
1016
1017 static void add_file_name (struct line_header *, char *, unsigned int,
1018 unsigned int, unsigned int);
1019
1020 static struct line_header *(dwarf_decode_line_header
1021 (unsigned int offset,
1022 bfd *abfd, struct dwarf2_cu *cu));
1023
1024 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1025 struct dwarf2_cu *, struct partial_symtab *);
1026
1027 static void dwarf2_start_subfile (char *, const char *, const char *);
1028
1029 static struct symbol *new_symbol (struct die_info *, struct type *,
1030 struct dwarf2_cu *);
1031
1032 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1033 struct dwarf2_cu *, struct symbol *);
1034
1035 static void dwarf2_const_value (struct attribute *, struct symbol *,
1036 struct dwarf2_cu *);
1037
1038 static void dwarf2_const_value_attr (struct attribute *attr,
1039 struct type *type,
1040 const char *name,
1041 struct obstack *obstack,
1042 struct dwarf2_cu *cu, long *value,
1043 gdb_byte **bytes,
1044 struct dwarf2_locexpr_baton **baton);
1045
1046 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1047
1048 static int need_gnat_info (struct dwarf2_cu *);
1049
1050 static struct type *die_descriptive_type (struct die_info *,
1051 struct dwarf2_cu *);
1052
1053 static void set_descriptive_type (struct type *, struct die_info *,
1054 struct dwarf2_cu *);
1055
1056 static struct type *die_containing_type (struct die_info *,
1057 struct dwarf2_cu *);
1058
1059 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1060 struct dwarf2_cu *);
1061
1062 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1063
1064 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1065
1066 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1067
1068 static char *typename_concat (struct obstack *obs, const char *prefix,
1069 const char *suffix, int physname,
1070 struct dwarf2_cu *cu);
1071
1072 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1073
1074 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1075
1076 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1077
1078 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1079
1080 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1081
1082 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1083 struct dwarf2_cu *, struct partial_symtab *);
1084
1085 static int dwarf2_get_pc_bounds (struct die_info *,
1086 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1087 struct partial_symtab *);
1088
1089 static void get_scope_pc_bounds (struct die_info *,
1090 CORE_ADDR *, CORE_ADDR *,
1091 struct dwarf2_cu *);
1092
1093 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1094 CORE_ADDR, struct dwarf2_cu *);
1095
1096 static void dwarf2_add_field (struct field_info *, struct die_info *,
1097 struct dwarf2_cu *);
1098
1099 static void dwarf2_attach_fields_to_type (struct field_info *,
1100 struct type *, struct dwarf2_cu *);
1101
1102 static void dwarf2_add_member_fn (struct field_info *,
1103 struct die_info *, struct type *,
1104 struct dwarf2_cu *);
1105
1106 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1107 struct type *,
1108 struct dwarf2_cu *);
1109
1110 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1111
1112 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1113
1114 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1115
1116 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1117
1118 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1119
1120 static struct type *read_module_type (struct die_info *die,
1121 struct dwarf2_cu *cu);
1122
1123 static const char *namespace_name (struct die_info *die,
1124 int *is_anonymous, struct dwarf2_cu *);
1125
1126 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1127
1128 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1129
1130 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1131 struct dwarf2_cu *);
1132
1133 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1134
1135 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1136 gdb_byte *info_ptr,
1137 gdb_byte **new_info_ptr,
1138 struct die_info *parent);
1139
1140 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1141 gdb_byte *info_ptr,
1142 gdb_byte **new_info_ptr,
1143 struct die_info *parent);
1144
1145 static struct die_info *read_die_and_siblings (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 gdb_byte *read_full_die (const struct die_reader_specs *reader,
1151 struct die_info **, gdb_byte *,
1152 int *);
1153
1154 static void process_die (struct die_info *, struct dwarf2_cu *);
1155
1156 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1157 struct obstack *);
1158
1159 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1160
1161 static const char *dwarf2_full_name (char *name,
1162 struct die_info *die,
1163 struct dwarf2_cu *cu);
1164
1165 static struct die_info *dwarf2_extension (struct die_info *die,
1166 struct dwarf2_cu **);
1167
1168 static char *dwarf_tag_name (unsigned int);
1169
1170 static char *dwarf_attr_name (unsigned int);
1171
1172 static char *dwarf_form_name (unsigned int);
1173
1174 static char *dwarf_bool_name (unsigned int);
1175
1176 static char *dwarf_type_encoding_name (unsigned int);
1177
1178 #if 0
1179 static char *dwarf_cfi_name (unsigned int);
1180 #endif
1181
1182 static struct die_info *sibling_die (struct die_info *);
1183
1184 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1185
1186 static void dump_die_for_error (struct die_info *);
1187
1188 static void dump_die_1 (struct ui_file *, int level, int max_level,
1189 struct die_info *);
1190
1191 /*static*/ void dump_die (struct die_info *, int max_level);
1192
1193 static void store_in_ref_table (struct die_info *,
1194 struct dwarf2_cu *);
1195
1196 static int is_ref_attr (struct attribute *);
1197
1198 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1199
1200 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1201
1202 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1203 struct attribute *,
1204 struct dwarf2_cu **);
1205
1206 static struct die_info *follow_die_ref (struct die_info *,
1207 struct attribute *,
1208 struct dwarf2_cu **);
1209
1210 static struct die_info *follow_die_sig (struct die_info *,
1211 struct attribute *,
1212 struct dwarf2_cu **);
1213
1214 static struct signatured_type *lookup_signatured_type_at_offset
1215 (struct objfile *objfile,
1216 struct dwarf2_section_info *section,
1217 unsigned int offset);
1218
1219 static void read_signatured_type_at_offset (struct objfile *objfile,
1220 struct dwarf2_section_info *sect,
1221 unsigned int offset);
1222
1223 static void read_signatured_type (struct objfile *,
1224 struct signatured_type *type_sig);
1225
1226 /* memory allocation interface */
1227
1228 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1229
1230 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1231
1232 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1233
1234 static void dwarf_decode_macros (struct line_header *, unsigned int,
1235 char *, bfd *, struct dwarf2_cu *,
1236 struct dwarf2_section_info *,
1237 int);
1238
1239 static int attr_form_is_block (struct attribute *);
1240
1241 static int attr_form_is_section_offset (struct attribute *);
1242
1243 static int attr_form_is_constant (struct attribute *);
1244
1245 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1246 struct dwarf2_loclist_baton *baton,
1247 struct attribute *attr);
1248
1249 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1250 struct symbol *sym,
1251 struct dwarf2_cu *cu);
1252
1253 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1254 struct abbrev_info *abbrev,
1255 struct dwarf2_cu *cu);
1256
1257 static void free_stack_comp_unit (void *);
1258
1259 static hashval_t partial_die_hash (const void *item);
1260
1261 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1262
1263 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1264 (unsigned int offset, struct objfile *objfile);
1265
1266 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1267 (unsigned int offset, struct objfile *objfile);
1268
1269 static void init_one_comp_unit (struct dwarf2_cu *cu,
1270 struct objfile *objfile);
1271
1272 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1273 struct die_info *comp_unit_die);
1274
1275 static void free_one_comp_unit (void *);
1276
1277 static void free_cached_comp_units (void *);
1278
1279 static void age_cached_comp_units (void);
1280
1281 static void free_one_cached_comp_unit (void *);
1282
1283 static struct type *set_die_type (struct die_info *, struct type *,
1284 struct dwarf2_cu *);
1285
1286 static void create_all_comp_units (struct objfile *);
1287
1288 static int create_debug_types_hash_table (struct objfile *objfile);
1289
1290 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1291 struct objfile *);
1292
1293 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1294
1295 static void dwarf2_add_dependence (struct dwarf2_cu *,
1296 struct dwarf2_per_cu_data *);
1297
1298 static void dwarf2_mark (struct dwarf2_cu *);
1299
1300 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1301
1302 static struct type *get_die_type_at_offset (unsigned int,
1303 struct dwarf2_per_cu_data *per_cu);
1304
1305 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1306
1307 static void dwarf2_release_queue (void *dummy);
1308
1309 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1310 struct objfile *objfile);
1311
1312 static void process_queue (struct objfile *objfile);
1313
1314 static void find_file_and_directory (struct die_info *die,
1315 struct dwarf2_cu *cu,
1316 char **name, char **comp_dir);
1317
1318 static char *file_full_name (int file, struct line_header *lh,
1319 const char *comp_dir);
1320
1321 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1322 gdb_byte *info_ptr,
1323 gdb_byte *buffer,
1324 unsigned int buffer_size,
1325 bfd *abfd,
1326 int is_debug_types_section);
1327
1328 static void init_cu_die_reader (struct die_reader_specs *reader,
1329 struct dwarf2_cu *cu);
1330
1331 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1332
1333 #if WORDS_BIGENDIAN
1334
1335 /* Convert VALUE between big- and little-endian. */
1336 static offset_type
1337 byte_swap (offset_type value)
1338 {
1339 offset_type result;
1340
1341 result = (value & 0xff) << 24;
1342 result |= (value & 0xff00) << 8;
1343 result |= (value & 0xff0000) >> 8;
1344 result |= (value & 0xff000000) >> 24;
1345 return result;
1346 }
1347
1348 #define MAYBE_SWAP(V) byte_swap (V)
1349
1350 #else
1351 #define MAYBE_SWAP(V) (V)
1352 #endif /* WORDS_BIGENDIAN */
1353
1354 /* The suffix for an index file. */
1355 #define INDEX_SUFFIX ".gdb-index"
1356
1357 static const char *dwarf2_physname (char *name, struct die_info *die,
1358 struct dwarf2_cu *cu);
1359
1360 /* Try to locate the sections we need for DWARF 2 debugging
1361 information and return true if we have enough to do something.
1362 NAMES points to the dwarf2 section names, or is NULL if the standard
1363 ELF names are used. */
1364
1365 int
1366 dwarf2_has_info (struct objfile *objfile,
1367 const struct dwarf2_debug_sections *names)
1368 {
1369 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1370 if (!dwarf2_per_objfile)
1371 {
1372 /* Initialize per-objfile state. */
1373 struct dwarf2_per_objfile *data
1374 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1375
1376 memset (data, 0, sizeof (*data));
1377 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1378 dwarf2_per_objfile = data;
1379
1380 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1381 (void *) names);
1382 dwarf2_per_objfile->objfile = objfile;
1383 }
1384 return (dwarf2_per_objfile->info.asection != NULL
1385 && dwarf2_per_objfile->abbrev.asection != NULL);
1386 }
1387
1388 /* When loading sections, we look either for uncompressed section or for
1389 compressed section names. */
1390
1391 static int
1392 section_is_p (const char *section_name,
1393 const struct dwarf2_section_names *names)
1394 {
1395 if (names->normal != NULL
1396 && strcmp (section_name, names->normal) == 0)
1397 return 1;
1398 if (names->compressed != NULL
1399 && strcmp (section_name, names->compressed) == 0)
1400 return 1;
1401 return 0;
1402 }
1403
1404 /* This function is mapped across the sections and remembers the
1405 offset and size of each of the debugging sections we are interested
1406 in. */
1407
1408 static void
1409 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1410 {
1411 const struct dwarf2_debug_sections *names;
1412
1413 if (vnames == NULL)
1414 names = &dwarf2_elf_names;
1415 else
1416 names = (const struct dwarf2_debug_sections *) vnames;
1417
1418 if (section_is_p (sectp->name, &names->info))
1419 {
1420 dwarf2_per_objfile->info.asection = sectp;
1421 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1422 }
1423 else if (section_is_p (sectp->name, &names->abbrev))
1424 {
1425 dwarf2_per_objfile->abbrev.asection = sectp;
1426 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1427 }
1428 else if (section_is_p (sectp->name, &names->line))
1429 {
1430 dwarf2_per_objfile->line.asection = sectp;
1431 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1432 }
1433 else if (section_is_p (sectp->name, &names->loc))
1434 {
1435 dwarf2_per_objfile->loc.asection = sectp;
1436 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1437 }
1438 else if (section_is_p (sectp->name, &names->macinfo))
1439 {
1440 dwarf2_per_objfile->macinfo.asection = sectp;
1441 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1442 }
1443 else if (section_is_p (sectp->name, &names->macro))
1444 {
1445 dwarf2_per_objfile->macro.asection = sectp;
1446 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1447 }
1448 else if (section_is_p (sectp->name, &names->str))
1449 {
1450 dwarf2_per_objfile->str.asection = sectp;
1451 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1452 }
1453 else if (section_is_p (sectp->name, &names->frame))
1454 {
1455 dwarf2_per_objfile->frame.asection = sectp;
1456 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1457 }
1458 else if (section_is_p (sectp->name, &names->eh_frame))
1459 {
1460 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1461
1462 if (aflag & SEC_HAS_CONTENTS)
1463 {
1464 dwarf2_per_objfile->eh_frame.asection = sectp;
1465 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1466 }
1467 }
1468 else if (section_is_p (sectp->name, &names->ranges))
1469 {
1470 dwarf2_per_objfile->ranges.asection = sectp;
1471 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1472 }
1473 else if (section_is_p (sectp->name, &names->types))
1474 {
1475 struct dwarf2_section_info type_section;
1476
1477 memset (&type_section, 0, sizeof (type_section));
1478 type_section.asection = sectp;
1479 type_section.size = bfd_get_section_size (sectp);
1480
1481 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1482 &type_section);
1483 }
1484 else if (section_is_p (sectp->name, &names->gdb_index))
1485 {
1486 dwarf2_per_objfile->gdb_index.asection = sectp;
1487 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1488 }
1489
1490 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1491 && bfd_section_vma (abfd, sectp) == 0)
1492 dwarf2_per_objfile->has_section_at_zero = 1;
1493 }
1494
1495 /* Decompress a section that was compressed using zlib. Store the
1496 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1497
1498 static void
1499 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1500 gdb_byte **outbuf, bfd_size_type *outsize)
1501 {
1502 bfd *abfd = objfile->obfd;
1503 #ifndef HAVE_ZLIB_H
1504 error (_("Support for zlib-compressed DWARF data (from '%s') "
1505 "is disabled in this copy of GDB"),
1506 bfd_get_filename (abfd));
1507 #else
1508 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1509 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1510 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1511 bfd_size_type uncompressed_size;
1512 gdb_byte *uncompressed_buffer;
1513 z_stream strm;
1514 int rc;
1515 int header_size = 12;
1516
1517 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1518 || bfd_bread (compressed_buffer,
1519 compressed_size, abfd) != compressed_size)
1520 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1521 bfd_get_filename (abfd));
1522
1523 /* Read the zlib header. In this case, it should be "ZLIB" followed
1524 by the uncompressed section size, 8 bytes in big-endian order. */
1525 if (compressed_size < header_size
1526 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1527 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1528 bfd_get_filename (abfd));
1529 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1530 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1531 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1532 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1533 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1534 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1535 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1536 uncompressed_size += compressed_buffer[11];
1537
1538 /* It is possible the section consists of several compressed
1539 buffers concatenated together, so we uncompress in a loop. */
1540 strm.zalloc = NULL;
1541 strm.zfree = NULL;
1542 strm.opaque = NULL;
1543 strm.avail_in = compressed_size - header_size;
1544 strm.next_in = (Bytef*) compressed_buffer + header_size;
1545 strm.avail_out = uncompressed_size;
1546 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1547 uncompressed_size);
1548 rc = inflateInit (&strm);
1549 while (strm.avail_in > 0)
1550 {
1551 if (rc != Z_OK)
1552 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1553 bfd_get_filename (abfd), rc);
1554 strm.next_out = ((Bytef*) uncompressed_buffer
1555 + (uncompressed_size - strm.avail_out));
1556 rc = inflate (&strm, Z_FINISH);
1557 if (rc != Z_STREAM_END)
1558 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1559 bfd_get_filename (abfd), rc);
1560 rc = inflateReset (&strm);
1561 }
1562 rc = inflateEnd (&strm);
1563 if (rc != Z_OK
1564 || strm.avail_out != 0)
1565 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1566 bfd_get_filename (abfd), rc);
1567
1568 do_cleanups (cleanup);
1569 *outbuf = uncompressed_buffer;
1570 *outsize = uncompressed_size;
1571 #endif
1572 }
1573
1574 /* A helper function that decides whether a section is empty. */
1575
1576 static int
1577 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1578 {
1579 return info->asection == NULL || info->size == 0;
1580 }
1581
1582 /* Read the contents of the section SECTP from object file specified by
1583 OBJFILE, store info about the section into INFO.
1584 If the section is compressed, uncompress it before returning. */
1585
1586 static void
1587 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1588 {
1589 bfd *abfd = objfile->obfd;
1590 asection *sectp = info->asection;
1591 gdb_byte *buf, *retbuf;
1592 unsigned char header[4];
1593
1594 if (info->readin)
1595 return;
1596 info->buffer = NULL;
1597 info->map_addr = NULL;
1598 info->readin = 1;
1599
1600 if (dwarf2_section_empty_p (info))
1601 return;
1602
1603 /* Check if the file has a 4-byte header indicating compression. */
1604 if (info->size > sizeof (header)
1605 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1606 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1607 {
1608 /* Upon decompression, update the buffer and its size. */
1609 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1610 {
1611 zlib_decompress_section (objfile, sectp, &info->buffer,
1612 &info->size);
1613 return;
1614 }
1615 }
1616
1617 #ifdef HAVE_MMAP
1618 if (pagesize == 0)
1619 pagesize = getpagesize ();
1620
1621 /* Only try to mmap sections which are large enough: we don't want to
1622 waste space due to fragmentation. Also, only try mmap for sections
1623 without relocations. */
1624
1625 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1626 {
1627 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1628 MAP_PRIVATE, sectp->filepos,
1629 &info->map_addr, &info->map_len);
1630
1631 if ((caddr_t)info->buffer != MAP_FAILED)
1632 {
1633 #if HAVE_POSIX_MADVISE
1634 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1635 #endif
1636 return;
1637 }
1638 }
1639 #endif
1640
1641 /* If we get here, we are a normal, not-compressed section. */
1642 info->buffer = buf
1643 = obstack_alloc (&objfile->objfile_obstack, info->size);
1644
1645 /* When debugging .o files, we may need to apply relocations; see
1646 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1647 We never compress sections in .o files, so we only need to
1648 try this when the section is not compressed. */
1649 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1650 if (retbuf != NULL)
1651 {
1652 info->buffer = retbuf;
1653 return;
1654 }
1655
1656 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1657 || bfd_bread (buf, info->size, abfd) != info->size)
1658 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1659 bfd_get_filename (abfd));
1660 }
1661
1662 /* A helper function that returns the size of a section in a safe way.
1663 If you are positive that the section has been read before using the
1664 size, then it is safe to refer to the dwarf2_section_info object's
1665 "size" field directly. In other cases, you must call this
1666 function, because for compressed sections the size field is not set
1667 correctly until the section has been read. */
1668
1669 static bfd_size_type
1670 dwarf2_section_size (struct objfile *objfile,
1671 struct dwarf2_section_info *info)
1672 {
1673 if (!info->readin)
1674 dwarf2_read_section (objfile, info);
1675 return info->size;
1676 }
1677
1678 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1679 SECTION_NAME. */
1680
1681 void
1682 dwarf2_get_section_info (struct objfile *objfile,
1683 enum dwarf2_section_enum sect,
1684 asection **sectp, gdb_byte **bufp,
1685 bfd_size_type *sizep)
1686 {
1687 struct dwarf2_per_objfile *data
1688 = objfile_data (objfile, dwarf2_objfile_data_key);
1689 struct dwarf2_section_info *info;
1690
1691 /* We may see an objfile without any DWARF, in which case we just
1692 return nothing. */
1693 if (data == NULL)
1694 {
1695 *sectp = NULL;
1696 *bufp = NULL;
1697 *sizep = 0;
1698 return;
1699 }
1700 switch (sect)
1701 {
1702 case DWARF2_DEBUG_FRAME:
1703 info = &data->frame;
1704 break;
1705 case DWARF2_EH_FRAME:
1706 info = &data->eh_frame;
1707 break;
1708 default:
1709 gdb_assert_not_reached ("unexpected section");
1710 }
1711
1712 dwarf2_read_section (objfile, info);
1713
1714 *sectp = info->asection;
1715 *bufp = info->buffer;
1716 *sizep = info->size;
1717 }
1718
1719 \f
1720 /* DWARF quick_symbols_functions support. */
1721
1722 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1723 unique line tables, so we maintain a separate table of all .debug_line
1724 derived entries to support the sharing.
1725 All the quick functions need is the list of file names. We discard the
1726 line_header when we're done and don't need to record it here. */
1727 struct quick_file_names
1728 {
1729 /* The offset in .debug_line of the line table. We hash on this. */
1730 unsigned int offset;
1731
1732 /* The number of entries in file_names, real_names. */
1733 unsigned int num_file_names;
1734
1735 /* The file names from the line table, after being run through
1736 file_full_name. */
1737 const char **file_names;
1738
1739 /* The file names from the line table after being run through
1740 gdb_realpath. These are computed lazily. */
1741 const char **real_names;
1742 };
1743
1744 /* When using the index (and thus not using psymtabs), each CU has an
1745 object of this type. This is used to hold information needed by
1746 the various "quick" methods. */
1747 struct dwarf2_per_cu_quick_data
1748 {
1749 /* The file table. This can be NULL if there was no file table
1750 or it's currently not read in.
1751 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1752 struct quick_file_names *file_names;
1753
1754 /* The corresponding symbol table. This is NULL if symbols for this
1755 CU have not yet been read. */
1756 struct symtab *symtab;
1757
1758 /* A temporary mark bit used when iterating over all CUs in
1759 expand_symtabs_matching. */
1760 unsigned int mark : 1;
1761
1762 /* True if we've tried to read the file table and found there isn't one.
1763 There will be no point in trying to read it again next time. */
1764 unsigned int no_file_data : 1;
1765 };
1766
1767 /* Hash function for a quick_file_names. */
1768
1769 static hashval_t
1770 hash_file_name_entry (const void *e)
1771 {
1772 const struct quick_file_names *file_data = e;
1773
1774 return file_data->offset;
1775 }
1776
1777 /* Equality function for a quick_file_names. */
1778
1779 static int
1780 eq_file_name_entry (const void *a, const void *b)
1781 {
1782 const struct quick_file_names *ea = a;
1783 const struct quick_file_names *eb = b;
1784
1785 return ea->offset == eb->offset;
1786 }
1787
1788 /* Delete function for a quick_file_names. */
1789
1790 static void
1791 delete_file_name_entry (void *e)
1792 {
1793 struct quick_file_names *file_data = e;
1794 int i;
1795
1796 for (i = 0; i < file_data->num_file_names; ++i)
1797 {
1798 xfree ((void*) file_data->file_names[i]);
1799 if (file_data->real_names)
1800 xfree ((void*) file_data->real_names[i]);
1801 }
1802
1803 /* The space for the struct itself lives on objfile_obstack,
1804 so we don't free it here. */
1805 }
1806
1807 /* Create a quick_file_names hash table. */
1808
1809 static htab_t
1810 create_quick_file_names_table (unsigned int nr_initial_entries)
1811 {
1812 return htab_create_alloc (nr_initial_entries,
1813 hash_file_name_entry, eq_file_name_entry,
1814 delete_file_name_entry, xcalloc, xfree);
1815 }
1816
1817 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1818 have to be created afterwards. You should call age_cached_comp_units after
1819 processing PER_CU->CU. dw2_setup must have been already called. */
1820
1821 static void
1822 load_cu (struct dwarf2_per_cu_data *per_cu)
1823 {
1824 if (per_cu->debug_types_section)
1825 read_signatured_type_at_offset (per_cu->objfile,
1826 per_cu->debug_types_section,
1827 per_cu->offset);
1828 else
1829 load_full_comp_unit (per_cu, per_cu->objfile);
1830
1831 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1832
1833 gdb_assert (per_cu->cu != NULL);
1834 }
1835
1836 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1837 this CU came. */
1838
1839 static void
1840 dw2_do_instantiate_symtab (struct objfile *objfile,
1841 struct dwarf2_per_cu_data *per_cu)
1842 {
1843 struct cleanup *back_to;
1844
1845 back_to = make_cleanup (dwarf2_release_queue, NULL);
1846
1847 queue_comp_unit (per_cu, objfile);
1848
1849 load_cu (per_cu);
1850
1851 process_queue (objfile);
1852
1853 /* Age the cache, releasing compilation units that have not
1854 been used recently. */
1855 age_cached_comp_units ();
1856
1857 do_cleanups (back_to);
1858 }
1859
1860 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1861 the objfile from which this CU came. Returns the resulting symbol
1862 table. */
1863
1864 static struct symtab *
1865 dw2_instantiate_symtab (struct objfile *objfile,
1866 struct dwarf2_per_cu_data *per_cu)
1867 {
1868 if (!per_cu->v.quick->symtab)
1869 {
1870 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1871 increment_reading_symtab ();
1872 dw2_do_instantiate_symtab (objfile, per_cu);
1873 do_cleanups (back_to);
1874 }
1875 return per_cu->v.quick->symtab;
1876 }
1877
1878 /* Return the CU given its index. */
1879
1880 static struct dwarf2_per_cu_data *
1881 dw2_get_cu (int index)
1882 {
1883 if (index >= dwarf2_per_objfile->n_comp_units)
1884 {
1885 index -= dwarf2_per_objfile->n_comp_units;
1886 return dwarf2_per_objfile->type_comp_units[index];
1887 }
1888 return dwarf2_per_objfile->all_comp_units[index];
1889 }
1890
1891 /* A helper function that knows how to read a 64-bit value in a way
1892 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1893 otherwise. */
1894
1895 static int
1896 extract_cu_value (const char *bytes, ULONGEST *result)
1897 {
1898 if (sizeof (ULONGEST) < 8)
1899 {
1900 int i;
1901
1902 /* Ignore the upper 4 bytes if they are all zero. */
1903 for (i = 0; i < 4; ++i)
1904 if (bytes[i + 4] != 0)
1905 return 0;
1906
1907 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1908 }
1909 else
1910 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1911 return 1;
1912 }
1913
1914 /* Read the CU list from the mapped index, and use it to create all
1915 the CU objects for this objfile. Return 0 if something went wrong,
1916 1 if everything went ok. */
1917
1918 static int
1919 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1920 offset_type cu_list_elements)
1921 {
1922 offset_type i;
1923
1924 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1925 dwarf2_per_objfile->all_comp_units
1926 = obstack_alloc (&objfile->objfile_obstack,
1927 dwarf2_per_objfile->n_comp_units
1928 * sizeof (struct dwarf2_per_cu_data *));
1929
1930 for (i = 0; i < cu_list_elements; i += 2)
1931 {
1932 struct dwarf2_per_cu_data *the_cu;
1933 ULONGEST offset, length;
1934
1935 if (!extract_cu_value (cu_list, &offset)
1936 || !extract_cu_value (cu_list + 8, &length))
1937 return 0;
1938 cu_list += 2 * 8;
1939
1940 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1941 struct dwarf2_per_cu_data);
1942 the_cu->offset = offset;
1943 the_cu->length = length;
1944 the_cu->objfile = objfile;
1945 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1946 struct dwarf2_per_cu_quick_data);
1947 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1948 }
1949
1950 return 1;
1951 }
1952
1953 /* Create the signatured type hash table from the index. */
1954
1955 static int
1956 create_signatured_type_table_from_index (struct objfile *objfile,
1957 struct dwarf2_section_info *section,
1958 const gdb_byte *bytes,
1959 offset_type elements)
1960 {
1961 offset_type i;
1962 htab_t sig_types_hash;
1963
1964 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1965 dwarf2_per_objfile->type_comp_units
1966 = obstack_alloc (&objfile->objfile_obstack,
1967 dwarf2_per_objfile->n_type_comp_units
1968 * sizeof (struct dwarf2_per_cu_data *));
1969
1970 sig_types_hash = allocate_signatured_type_table (objfile);
1971
1972 for (i = 0; i < elements; i += 3)
1973 {
1974 struct signatured_type *type_sig;
1975 ULONGEST offset, type_offset, signature;
1976 void **slot;
1977
1978 if (!extract_cu_value (bytes, &offset)
1979 || !extract_cu_value (bytes + 8, &type_offset))
1980 return 0;
1981 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1982 bytes += 3 * 8;
1983
1984 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1985 struct signatured_type);
1986 type_sig->signature = signature;
1987 type_sig->type_offset = type_offset;
1988 type_sig->per_cu.debug_types_section = section;
1989 type_sig->per_cu.offset = offset;
1990 type_sig->per_cu.objfile = objfile;
1991 type_sig->per_cu.v.quick
1992 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1993 struct dwarf2_per_cu_quick_data);
1994
1995 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1996 *slot = type_sig;
1997
1998 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1999 }
2000
2001 dwarf2_per_objfile->signatured_types = sig_types_hash;
2002
2003 return 1;
2004 }
2005
2006 /* Read the address map data from the mapped index, and use it to
2007 populate the objfile's psymtabs_addrmap. */
2008
2009 static void
2010 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2011 {
2012 const gdb_byte *iter, *end;
2013 struct obstack temp_obstack;
2014 struct addrmap *mutable_map;
2015 struct cleanup *cleanup;
2016 CORE_ADDR baseaddr;
2017
2018 obstack_init (&temp_obstack);
2019 cleanup = make_cleanup_obstack_free (&temp_obstack);
2020 mutable_map = addrmap_create_mutable (&temp_obstack);
2021
2022 iter = index->address_table;
2023 end = iter + index->address_table_size;
2024
2025 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2026
2027 while (iter < end)
2028 {
2029 ULONGEST hi, lo, cu_index;
2030 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2031 iter += 8;
2032 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2033 iter += 8;
2034 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2035 iter += 4;
2036
2037 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2038 dw2_get_cu (cu_index));
2039 }
2040
2041 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2042 &objfile->objfile_obstack);
2043 do_cleanups (cleanup);
2044 }
2045
2046 /* The hash function for strings in the mapped index. This is the same as
2047 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2048 implementation. This is necessary because the hash function is tied to the
2049 format of the mapped index file. The hash values do not have to match with
2050 SYMBOL_HASH_NEXT.
2051
2052 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2053
2054 static hashval_t
2055 mapped_index_string_hash (int index_version, const void *p)
2056 {
2057 const unsigned char *str = (const unsigned char *) p;
2058 hashval_t r = 0;
2059 unsigned char c;
2060
2061 while ((c = *str++) != 0)
2062 {
2063 if (index_version >= 5)
2064 c = tolower (c);
2065 r = r * 67 + c - 113;
2066 }
2067
2068 return r;
2069 }
2070
2071 /* Find a slot in the mapped index INDEX for the object named NAME.
2072 If NAME is found, set *VEC_OUT to point to the CU vector in the
2073 constant pool and return 1. If NAME cannot be found, return 0. */
2074
2075 static int
2076 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2077 offset_type **vec_out)
2078 {
2079 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2080 offset_type hash;
2081 offset_type slot, step;
2082 int (*cmp) (const char *, const char *);
2083
2084 if (current_language->la_language == language_cplus
2085 || current_language->la_language == language_java
2086 || current_language->la_language == language_fortran)
2087 {
2088 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2089 not contain any. */
2090 const char *paren = strchr (name, '(');
2091
2092 if (paren)
2093 {
2094 char *dup;
2095
2096 dup = xmalloc (paren - name + 1);
2097 memcpy (dup, name, paren - name);
2098 dup[paren - name] = 0;
2099
2100 make_cleanup (xfree, dup);
2101 name = dup;
2102 }
2103 }
2104
2105 /* Index version 4 did not support case insensitive searches. But the
2106 indexes for case insensitive languages are built in lowercase, therefore
2107 simulate our NAME being searched is also lowercased. */
2108 hash = mapped_index_string_hash ((index->version == 4
2109 && case_sensitivity == case_sensitive_off
2110 ? 5 : index->version),
2111 name);
2112
2113 slot = hash & (index->symbol_table_slots - 1);
2114 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2115 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2116
2117 for (;;)
2118 {
2119 /* Convert a slot number to an offset into the table. */
2120 offset_type i = 2 * slot;
2121 const char *str;
2122 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2123 {
2124 do_cleanups (back_to);
2125 return 0;
2126 }
2127
2128 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2129 if (!cmp (name, str))
2130 {
2131 *vec_out = (offset_type *) (index->constant_pool
2132 + MAYBE_SWAP (index->symbol_table[i + 1]));
2133 do_cleanups (back_to);
2134 return 1;
2135 }
2136
2137 slot = (slot + step) & (index->symbol_table_slots - 1);
2138 }
2139 }
2140
2141 /* Read the index file. If everything went ok, initialize the "quick"
2142 elements of all the CUs and return 1. Otherwise, return 0. */
2143
2144 static int
2145 dwarf2_read_index (struct objfile *objfile)
2146 {
2147 char *addr;
2148 struct mapped_index *map;
2149 offset_type *metadata;
2150 const gdb_byte *cu_list;
2151 const gdb_byte *types_list = NULL;
2152 offset_type version, cu_list_elements;
2153 offset_type types_list_elements = 0;
2154 int i;
2155
2156 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2157 return 0;
2158
2159 /* Older elfutils strip versions could keep the section in the main
2160 executable while splitting it for the separate debug info file. */
2161 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2162 & SEC_HAS_CONTENTS) == 0)
2163 return 0;
2164
2165 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2166
2167 addr = dwarf2_per_objfile->gdb_index.buffer;
2168 /* Version check. */
2169 version = MAYBE_SWAP (*(offset_type *) addr);
2170 /* Versions earlier than 3 emitted every copy of a psymbol. This
2171 causes the index to behave very poorly for certain requests. Version 3
2172 contained incomplete addrmap. So, it seems better to just ignore such
2173 indices. Index version 4 uses a different hash function than index
2174 version 5 and later. */
2175 if (version < 4)
2176 return 0;
2177 /* Indexes with higher version than the one supported by GDB may be no
2178 longer backward compatible. */
2179 if (version > 5)
2180 return 0;
2181
2182 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2183 map->version = version;
2184 map->total_size = dwarf2_per_objfile->gdb_index.size;
2185
2186 metadata = (offset_type *) (addr + sizeof (offset_type));
2187
2188 i = 0;
2189 cu_list = addr + MAYBE_SWAP (metadata[i]);
2190 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2191 / 8);
2192 ++i;
2193
2194 types_list = addr + MAYBE_SWAP (metadata[i]);
2195 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2196 - MAYBE_SWAP (metadata[i]))
2197 / 8);
2198 ++i;
2199
2200 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2201 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2202 - MAYBE_SWAP (metadata[i]));
2203 ++i;
2204
2205 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2206 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2207 - MAYBE_SWAP (metadata[i]))
2208 / (2 * sizeof (offset_type)));
2209 ++i;
2210
2211 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2212
2213 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2214 return 0;
2215
2216 if (types_list_elements)
2217 {
2218 struct dwarf2_section_info *section;
2219
2220 /* We can only handle a single .debug_types when we have an
2221 index. */
2222 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2223 return 0;
2224
2225 section = VEC_index (dwarf2_section_info_def,
2226 dwarf2_per_objfile->types, 0);
2227
2228 if (!create_signatured_type_table_from_index (objfile, section,
2229 types_list,
2230 types_list_elements))
2231 return 0;
2232 }
2233
2234 create_addrmap_from_index (objfile, map);
2235
2236 dwarf2_per_objfile->index_table = map;
2237 dwarf2_per_objfile->using_index = 1;
2238 dwarf2_per_objfile->quick_file_names_table =
2239 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2240
2241 return 1;
2242 }
2243
2244 /* A helper for the "quick" functions which sets the global
2245 dwarf2_per_objfile according to OBJFILE. */
2246
2247 static void
2248 dw2_setup (struct objfile *objfile)
2249 {
2250 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2251 gdb_assert (dwarf2_per_objfile);
2252 }
2253
2254 /* A helper for the "quick" functions which attempts to read the line
2255 table for THIS_CU. */
2256
2257 static struct quick_file_names *
2258 dw2_get_file_names (struct objfile *objfile,
2259 struct dwarf2_per_cu_data *this_cu)
2260 {
2261 bfd *abfd = objfile->obfd;
2262 struct line_header *lh;
2263 struct attribute *attr;
2264 struct cleanup *cleanups;
2265 struct die_info *comp_unit_die;
2266 struct dwarf2_section_info* sec;
2267 gdb_byte *info_ptr, *buffer;
2268 int has_children, i;
2269 struct dwarf2_cu cu;
2270 unsigned int bytes_read, buffer_size;
2271 struct die_reader_specs reader_specs;
2272 char *name, *comp_dir;
2273 void **slot;
2274 struct quick_file_names *qfn;
2275 unsigned int line_offset;
2276
2277 if (this_cu->v.quick->file_names != NULL)
2278 return this_cu->v.quick->file_names;
2279 /* If we know there is no line data, no point in looking again. */
2280 if (this_cu->v.quick->no_file_data)
2281 return NULL;
2282
2283 init_one_comp_unit (&cu, objfile);
2284 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2285
2286 if (this_cu->debug_types_section)
2287 sec = this_cu->debug_types_section;
2288 else
2289 sec = &dwarf2_per_objfile->info;
2290 dwarf2_read_section (objfile, sec);
2291 buffer_size = sec->size;
2292 buffer = sec->buffer;
2293 info_ptr = buffer + this_cu->offset;
2294
2295 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2296 buffer, buffer_size,
2297 abfd,
2298 this_cu->debug_types_section != NULL);
2299
2300 /* Skip dummy compilation units. */
2301 if (info_ptr >= buffer + buffer_size
2302 || peek_abbrev_code (abfd, info_ptr) == 0)
2303 {
2304 do_cleanups (cleanups);
2305 return NULL;
2306 }
2307
2308 this_cu->cu = &cu;
2309 cu.per_cu = this_cu;
2310
2311 dwarf2_read_abbrevs (abfd, &cu);
2312 make_cleanup (dwarf2_free_abbrev_table, &cu);
2313
2314 init_cu_die_reader (&reader_specs, &cu);
2315 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2316 &has_children);
2317
2318 lh = NULL;
2319 slot = NULL;
2320 line_offset = 0;
2321 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2322 if (attr)
2323 {
2324 struct quick_file_names find_entry;
2325
2326 line_offset = DW_UNSND (attr);
2327
2328 /* We may have already read in this line header (TU line header sharing).
2329 If we have we're done. */
2330 find_entry.offset = line_offset;
2331 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2332 &find_entry, INSERT);
2333 if (*slot != NULL)
2334 {
2335 do_cleanups (cleanups);
2336 this_cu->v.quick->file_names = *slot;
2337 return *slot;
2338 }
2339
2340 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2341 }
2342 if (lh == NULL)
2343 {
2344 do_cleanups (cleanups);
2345 this_cu->v.quick->no_file_data = 1;
2346 return NULL;
2347 }
2348
2349 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2350 qfn->offset = line_offset;
2351 gdb_assert (slot != NULL);
2352 *slot = qfn;
2353
2354 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2355
2356 qfn->num_file_names = lh->num_file_names;
2357 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2358 lh->num_file_names * sizeof (char *));
2359 for (i = 0; i < lh->num_file_names; ++i)
2360 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2361 qfn->real_names = NULL;
2362
2363 free_line_header (lh);
2364 do_cleanups (cleanups);
2365
2366 this_cu->v.quick->file_names = qfn;
2367 return qfn;
2368 }
2369
2370 /* A helper for the "quick" functions which computes and caches the
2371 real path for a given file name from the line table. */
2372
2373 static const char *
2374 dw2_get_real_path (struct objfile *objfile,
2375 struct quick_file_names *qfn, int index)
2376 {
2377 if (qfn->real_names == NULL)
2378 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2379 qfn->num_file_names, sizeof (char *));
2380
2381 if (qfn->real_names[index] == NULL)
2382 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2383
2384 return qfn->real_names[index];
2385 }
2386
2387 static struct symtab *
2388 dw2_find_last_source_symtab (struct objfile *objfile)
2389 {
2390 int index;
2391
2392 dw2_setup (objfile);
2393 index = dwarf2_per_objfile->n_comp_units - 1;
2394 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2395 }
2396
2397 /* Traversal function for dw2_forget_cached_source_info. */
2398
2399 static int
2400 dw2_free_cached_file_names (void **slot, void *info)
2401 {
2402 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2403
2404 if (file_data->real_names)
2405 {
2406 int i;
2407
2408 for (i = 0; i < file_data->num_file_names; ++i)
2409 {
2410 xfree ((void*) file_data->real_names[i]);
2411 file_data->real_names[i] = NULL;
2412 }
2413 }
2414
2415 return 1;
2416 }
2417
2418 static void
2419 dw2_forget_cached_source_info (struct objfile *objfile)
2420 {
2421 dw2_setup (objfile);
2422
2423 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2424 dw2_free_cached_file_names, NULL);
2425 }
2426
2427 /* Helper function for dw2_map_symtabs_matching_filename that expands
2428 the symtabs and calls the iterator. */
2429
2430 static int
2431 dw2_map_expand_apply (struct objfile *objfile,
2432 struct dwarf2_per_cu_data *per_cu,
2433 const char *name,
2434 const char *full_path, const char *real_path,
2435 int (*callback) (struct symtab *, void *),
2436 void *data)
2437 {
2438 struct symtab *last_made = objfile->symtabs;
2439
2440 /* Don't visit already-expanded CUs. */
2441 if (per_cu->v.quick->symtab)
2442 return 0;
2443
2444 /* This may expand more than one symtab, and we want to iterate over
2445 all of them. */
2446 dw2_instantiate_symtab (objfile, per_cu);
2447
2448 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2449 objfile->symtabs, last_made);
2450 }
2451
2452 /* Implementation of the map_symtabs_matching_filename method. */
2453
2454 static int
2455 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2456 const char *full_path, const char *real_path,
2457 int (*callback) (struct symtab *, void *),
2458 void *data)
2459 {
2460 int i;
2461 const char *name_basename = lbasename (name);
2462 int check_basename = name_basename == name;
2463 struct dwarf2_per_cu_data *base_cu = NULL;
2464
2465 dw2_setup (objfile);
2466
2467 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2468 + dwarf2_per_objfile->n_type_comp_units); ++i)
2469 {
2470 int j;
2471 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2472 struct quick_file_names *file_data;
2473
2474 /* We only need to look at symtabs not already expanded. */
2475 if (per_cu->v.quick->symtab)
2476 continue;
2477
2478 file_data = dw2_get_file_names (objfile, per_cu);
2479 if (file_data == NULL)
2480 continue;
2481
2482 for (j = 0; j < file_data->num_file_names; ++j)
2483 {
2484 const char *this_name = file_data->file_names[j];
2485
2486 if (FILENAME_CMP (name, this_name) == 0)
2487 {
2488 if (dw2_map_expand_apply (objfile, per_cu,
2489 name, full_path, real_path,
2490 callback, data))
2491 return 1;
2492 }
2493
2494 if (check_basename && ! base_cu
2495 && FILENAME_CMP (lbasename (this_name), name) == 0)
2496 base_cu = per_cu;
2497
2498 /* Before we invoke realpath, which can get expensive when many
2499 files are involved, do a quick comparison of the basenames. */
2500 if (! basenames_may_differ
2501 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2502 continue;
2503
2504 if (full_path != NULL)
2505 {
2506 const char *this_real_name = dw2_get_real_path (objfile,
2507 file_data, j);
2508
2509 if (this_real_name != NULL
2510 && FILENAME_CMP (full_path, this_real_name) == 0)
2511 {
2512 if (dw2_map_expand_apply (objfile, per_cu,
2513 name, full_path, real_path,
2514 callback, data))
2515 return 1;
2516 }
2517 }
2518
2519 if (real_path != NULL)
2520 {
2521 const char *this_real_name = dw2_get_real_path (objfile,
2522 file_data, j);
2523
2524 if (this_real_name != NULL
2525 && FILENAME_CMP (real_path, this_real_name) == 0)
2526 {
2527 if (dw2_map_expand_apply (objfile, per_cu,
2528 name, full_path, real_path,
2529 callback, data))
2530 return 1;
2531 }
2532 }
2533 }
2534 }
2535
2536 if (base_cu)
2537 {
2538 if (dw2_map_expand_apply (objfile, base_cu,
2539 name, full_path, real_path,
2540 callback, data))
2541 return 1;
2542 }
2543
2544 return 0;
2545 }
2546
2547 static struct symtab *
2548 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2549 const char *name, domain_enum domain)
2550 {
2551 /* We do all the work in the pre_expand_symtabs_matching hook
2552 instead. */
2553 return NULL;
2554 }
2555
2556 /* A helper function that expands all symtabs that hold an object
2557 named NAME. */
2558
2559 static void
2560 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2561 {
2562 dw2_setup (objfile);
2563
2564 /* index_table is NULL if OBJF_READNOW. */
2565 if (dwarf2_per_objfile->index_table)
2566 {
2567 offset_type *vec;
2568
2569 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2570 name, &vec))
2571 {
2572 offset_type i, len = MAYBE_SWAP (*vec);
2573 for (i = 0; i < len; ++i)
2574 {
2575 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2576 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2577
2578 dw2_instantiate_symtab (objfile, per_cu);
2579 }
2580 }
2581 }
2582 }
2583
2584 static void
2585 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2586 enum block_enum block_kind, const char *name,
2587 domain_enum domain)
2588 {
2589 dw2_do_expand_symtabs_matching (objfile, name);
2590 }
2591
2592 static void
2593 dw2_print_stats (struct objfile *objfile)
2594 {
2595 int i, count;
2596
2597 dw2_setup (objfile);
2598 count = 0;
2599 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2600 + dwarf2_per_objfile->n_type_comp_units); ++i)
2601 {
2602 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2603
2604 if (!per_cu->v.quick->symtab)
2605 ++count;
2606 }
2607 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2608 }
2609
2610 static void
2611 dw2_dump (struct objfile *objfile)
2612 {
2613 /* Nothing worth printing. */
2614 }
2615
2616 static void
2617 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2618 struct section_offsets *delta)
2619 {
2620 /* There's nothing to relocate here. */
2621 }
2622
2623 static void
2624 dw2_expand_symtabs_for_function (struct objfile *objfile,
2625 const char *func_name)
2626 {
2627 dw2_do_expand_symtabs_matching (objfile, func_name);
2628 }
2629
2630 static void
2631 dw2_expand_all_symtabs (struct objfile *objfile)
2632 {
2633 int i;
2634
2635 dw2_setup (objfile);
2636
2637 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2638 + dwarf2_per_objfile->n_type_comp_units); ++i)
2639 {
2640 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2641
2642 dw2_instantiate_symtab (objfile, per_cu);
2643 }
2644 }
2645
2646 static void
2647 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2648 const char *filename)
2649 {
2650 int i;
2651
2652 dw2_setup (objfile);
2653
2654 /* We don't need to consider type units here.
2655 This is only called for examining code, e.g. expand_line_sal.
2656 There can be an order of magnitude (or more) more type units
2657 than comp units, and we avoid them if we can. */
2658
2659 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2660 {
2661 int j;
2662 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2663 struct quick_file_names *file_data;
2664
2665 /* We only need to look at symtabs not already expanded. */
2666 if (per_cu->v.quick->symtab)
2667 continue;
2668
2669 file_data = dw2_get_file_names (objfile, per_cu);
2670 if (file_data == NULL)
2671 continue;
2672
2673 for (j = 0; j < file_data->num_file_names; ++j)
2674 {
2675 const char *this_name = file_data->file_names[j];
2676 if (FILENAME_CMP (this_name, filename) == 0)
2677 {
2678 dw2_instantiate_symtab (objfile, per_cu);
2679 break;
2680 }
2681 }
2682 }
2683 }
2684
2685 static const char *
2686 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2687 {
2688 struct dwarf2_per_cu_data *per_cu;
2689 offset_type *vec;
2690 struct quick_file_names *file_data;
2691
2692 dw2_setup (objfile);
2693
2694 /* index_table is NULL if OBJF_READNOW. */
2695 if (!dwarf2_per_objfile->index_table)
2696 {
2697 struct symtab *s;
2698
2699 ALL_OBJFILE_SYMTABS (objfile, s)
2700 if (s->primary)
2701 {
2702 struct blockvector *bv = BLOCKVECTOR (s);
2703 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2704 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2705
2706 if (sym)
2707 return sym->symtab->filename;
2708 }
2709 return NULL;
2710 }
2711
2712 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2713 name, &vec))
2714 return NULL;
2715
2716 /* Note that this just looks at the very first one named NAME -- but
2717 actually we are looking for a function. find_main_filename
2718 should be rewritten so that it doesn't require a custom hook. It
2719 could just use the ordinary symbol tables. */
2720 /* vec[0] is the length, which must always be >0. */
2721 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2722
2723 file_data = dw2_get_file_names (objfile, per_cu);
2724 if (file_data == NULL)
2725 return NULL;
2726
2727 return file_data->file_names[file_data->num_file_names - 1];
2728 }
2729
2730 static void
2731 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2732 struct objfile *objfile, int global,
2733 int (*callback) (struct block *,
2734 struct symbol *, void *),
2735 void *data, symbol_compare_ftype *match,
2736 symbol_compare_ftype *ordered_compare)
2737 {
2738 /* Currently unimplemented; used for Ada. The function can be called if the
2739 current language is Ada for a non-Ada objfile using GNU index. As Ada
2740 does not look for non-Ada symbols this function should just return. */
2741 }
2742
2743 static void
2744 dw2_expand_symtabs_matching
2745 (struct objfile *objfile,
2746 int (*file_matcher) (const char *, void *),
2747 int (*name_matcher) (const struct language_defn *, const char *, void *),
2748 enum search_domain kind,
2749 void *data)
2750 {
2751 int i;
2752 offset_type iter;
2753 struct mapped_index *index;
2754
2755 dw2_setup (objfile);
2756
2757 /* index_table is NULL if OBJF_READNOW. */
2758 if (!dwarf2_per_objfile->index_table)
2759 return;
2760 index = dwarf2_per_objfile->index_table;
2761
2762 if (file_matcher != NULL)
2763 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2764 + dwarf2_per_objfile->n_type_comp_units); ++i)
2765 {
2766 int j;
2767 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2768 struct quick_file_names *file_data;
2769
2770 per_cu->v.quick->mark = 0;
2771
2772 /* We only need to look at symtabs not already expanded. */
2773 if (per_cu->v.quick->symtab)
2774 continue;
2775
2776 file_data = dw2_get_file_names (objfile, per_cu);
2777 if (file_data == NULL)
2778 continue;
2779
2780 for (j = 0; j < file_data->num_file_names; ++j)
2781 {
2782 if (file_matcher (file_data->file_names[j], data))
2783 {
2784 per_cu->v.quick->mark = 1;
2785 break;
2786 }
2787 }
2788 }
2789
2790 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2791 {
2792 offset_type idx = 2 * iter;
2793 const char *name;
2794 offset_type *vec, vec_len, vec_idx;
2795
2796 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2797 continue;
2798
2799 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2800
2801 if (! (*name_matcher) (current_language, name, data))
2802 continue;
2803
2804 /* The name was matched, now expand corresponding CUs that were
2805 marked. */
2806 vec = (offset_type *) (index->constant_pool
2807 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2808 vec_len = MAYBE_SWAP (vec[0]);
2809 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2810 {
2811 struct dwarf2_per_cu_data *per_cu;
2812
2813 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2814 if (file_matcher == NULL || per_cu->v.quick->mark)
2815 dw2_instantiate_symtab (objfile, per_cu);
2816 }
2817 }
2818 }
2819
2820 static struct symtab *
2821 dw2_find_pc_sect_symtab (struct objfile *objfile,
2822 struct minimal_symbol *msymbol,
2823 CORE_ADDR pc,
2824 struct obj_section *section,
2825 int warn_if_readin)
2826 {
2827 struct dwarf2_per_cu_data *data;
2828
2829 dw2_setup (objfile);
2830
2831 if (!objfile->psymtabs_addrmap)
2832 return NULL;
2833
2834 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2835 if (!data)
2836 return NULL;
2837
2838 if (warn_if_readin && data->v.quick->symtab)
2839 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2840 paddress (get_objfile_arch (objfile), pc));
2841
2842 return dw2_instantiate_symtab (objfile, data);
2843 }
2844
2845 static void
2846 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2847 void *data, int need_fullname)
2848 {
2849 int i;
2850
2851 dw2_setup (objfile);
2852
2853 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2854 + dwarf2_per_objfile->n_type_comp_units); ++i)
2855 {
2856 int j;
2857 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2858 struct quick_file_names *file_data;
2859
2860 /* We only need to look at symtabs not already expanded. */
2861 if (per_cu->v.quick->symtab)
2862 continue;
2863
2864 file_data = dw2_get_file_names (objfile, per_cu);
2865 if (file_data == NULL)
2866 continue;
2867
2868 for (j = 0; j < file_data->num_file_names; ++j)
2869 {
2870 const char *this_real_name;
2871
2872 if (need_fullname)
2873 this_real_name = dw2_get_real_path (objfile, file_data, j);
2874 else
2875 this_real_name = NULL;
2876 (*fun) (file_data->file_names[j], this_real_name, data);
2877 }
2878 }
2879 }
2880
2881 static int
2882 dw2_has_symbols (struct objfile *objfile)
2883 {
2884 return 1;
2885 }
2886
2887 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2888 {
2889 dw2_has_symbols,
2890 dw2_find_last_source_symtab,
2891 dw2_forget_cached_source_info,
2892 dw2_map_symtabs_matching_filename,
2893 dw2_lookup_symbol,
2894 dw2_pre_expand_symtabs_matching,
2895 dw2_print_stats,
2896 dw2_dump,
2897 dw2_relocate,
2898 dw2_expand_symtabs_for_function,
2899 dw2_expand_all_symtabs,
2900 dw2_expand_symtabs_with_filename,
2901 dw2_find_symbol_file,
2902 dw2_map_matching_symbols,
2903 dw2_expand_symtabs_matching,
2904 dw2_find_pc_sect_symtab,
2905 dw2_map_symbol_filenames
2906 };
2907
2908 /* Initialize for reading DWARF for this objfile. Return 0 if this
2909 file will use psymtabs, or 1 if using the GNU index. */
2910
2911 int
2912 dwarf2_initialize_objfile (struct objfile *objfile)
2913 {
2914 /* If we're about to read full symbols, don't bother with the
2915 indices. In this case we also don't care if some other debug
2916 format is making psymtabs, because they are all about to be
2917 expanded anyway. */
2918 if ((objfile->flags & OBJF_READNOW))
2919 {
2920 int i;
2921
2922 dwarf2_per_objfile->using_index = 1;
2923 create_all_comp_units (objfile);
2924 create_debug_types_hash_table (objfile);
2925 dwarf2_per_objfile->quick_file_names_table =
2926 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2927
2928 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2929 + dwarf2_per_objfile->n_type_comp_units); ++i)
2930 {
2931 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2932
2933 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2934 struct dwarf2_per_cu_quick_data);
2935 }
2936
2937 /* Return 1 so that gdb sees the "quick" functions. However,
2938 these functions will be no-ops because we will have expanded
2939 all symtabs. */
2940 return 1;
2941 }
2942
2943 if (dwarf2_read_index (objfile))
2944 return 1;
2945
2946 return 0;
2947 }
2948
2949 \f
2950
2951 /* Build a partial symbol table. */
2952
2953 void
2954 dwarf2_build_psymtabs (struct objfile *objfile)
2955 {
2956 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2957 {
2958 init_psymbol_list (objfile, 1024);
2959 }
2960
2961 dwarf2_build_psymtabs_hard (objfile);
2962 }
2963
2964 /* Return TRUE if OFFSET is within CU_HEADER. */
2965
2966 static inline int
2967 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2968 {
2969 unsigned int bottom = cu_header->offset;
2970 unsigned int top = (cu_header->offset
2971 + cu_header->length
2972 + cu_header->initial_length_size);
2973
2974 return (offset >= bottom && offset < top);
2975 }
2976
2977 /* Read in the comp unit header information from the debug_info at info_ptr.
2978 NOTE: This leaves members offset, first_die_offset to be filled in
2979 by the caller. */
2980
2981 static gdb_byte *
2982 read_comp_unit_head (struct comp_unit_head *cu_header,
2983 gdb_byte *info_ptr, bfd *abfd)
2984 {
2985 int signed_addr;
2986 unsigned int bytes_read;
2987
2988 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2989 cu_header->initial_length_size = bytes_read;
2990 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2991 info_ptr += bytes_read;
2992 cu_header->version = read_2_bytes (abfd, info_ptr);
2993 info_ptr += 2;
2994 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2995 &bytes_read);
2996 info_ptr += bytes_read;
2997 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2998 info_ptr += 1;
2999 signed_addr = bfd_get_sign_extend_vma (abfd);
3000 if (signed_addr < 0)
3001 internal_error (__FILE__, __LINE__,
3002 _("read_comp_unit_head: dwarf from non elf file"));
3003 cu_header->signed_addr_p = signed_addr;
3004
3005 return info_ptr;
3006 }
3007
3008 /* Read in a CU header and perform some basic error checking. */
3009
3010 static gdb_byte *
3011 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
3012 gdb_byte *buffer, unsigned int buffer_size,
3013 bfd *abfd, int is_debug_types_section)
3014 {
3015 gdb_byte *beg_of_comp_unit = info_ptr;
3016
3017 header->offset = beg_of_comp_unit - buffer;
3018
3019 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3020
3021 /* If we're reading a type unit, skip over the signature and
3022 type_offset fields. */
3023 if (is_debug_types_section)
3024 info_ptr += 8 /*signature*/ + header->offset_size;
3025
3026 header->first_die_offset = info_ptr - beg_of_comp_unit;
3027
3028 if (header->version != 2 && header->version != 3 && header->version != 4)
3029 error (_("Dwarf Error: wrong version in compilation unit header "
3030 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3031 bfd_get_filename (abfd));
3032
3033 if (header->abbrev_offset
3034 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3035 &dwarf2_per_objfile->abbrev))
3036 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3037 "(offset 0x%lx + 6) [in module %s]"),
3038 (long) header->abbrev_offset,
3039 (long) (beg_of_comp_unit - buffer),
3040 bfd_get_filename (abfd));
3041
3042 if (beg_of_comp_unit + header->length + header->initial_length_size
3043 > buffer + buffer_size)
3044 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3045 "(offset 0x%lx + 0) [in module %s]"),
3046 (long) header->length,
3047 (long) (beg_of_comp_unit - buffer),
3048 bfd_get_filename (abfd));
3049
3050 return info_ptr;
3051 }
3052
3053 /* Read in the types comp unit header information from .debug_types entry at
3054 types_ptr. The result is a pointer to one past the end of the header. */
3055
3056 static gdb_byte *
3057 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3058 struct dwarf2_section_info *section,
3059 ULONGEST *signature,
3060 gdb_byte *types_ptr, bfd *abfd)
3061 {
3062 gdb_byte *initial_types_ptr = types_ptr;
3063
3064 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3065 cu_header->offset = types_ptr - section->buffer;
3066
3067 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3068
3069 *signature = read_8_bytes (abfd, types_ptr);
3070 types_ptr += 8;
3071 types_ptr += cu_header->offset_size;
3072 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3073
3074 return types_ptr;
3075 }
3076
3077 /* Allocate a new partial symtab for file named NAME and mark this new
3078 partial symtab as being an include of PST. */
3079
3080 static void
3081 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3082 struct objfile *objfile)
3083 {
3084 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3085
3086 subpst->section_offsets = pst->section_offsets;
3087 subpst->textlow = 0;
3088 subpst->texthigh = 0;
3089
3090 subpst->dependencies = (struct partial_symtab **)
3091 obstack_alloc (&objfile->objfile_obstack,
3092 sizeof (struct partial_symtab *));
3093 subpst->dependencies[0] = pst;
3094 subpst->number_of_dependencies = 1;
3095
3096 subpst->globals_offset = 0;
3097 subpst->n_global_syms = 0;
3098 subpst->statics_offset = 0;
3099 subpst->n_static_syms = 0;
3100 subpst->symtab = NULL;
3101 subpst->read_symtab = pst->read_symtab;
3102 subpst->readin = 0;
3103
3104 /* No private part is necessary for include psymtabs. This property
3105 can be used to differentiate between such include psymtabs and
3106 the regular ones. */
3107 subpst->read_symtab_private = NULL;
3108 }
3109
3110 /* Read the Line Number Program data and extract the list of files
3111 included by the source file represented by PST. Build an include
3112 partial symtab for each of these included files. */
3113
3114 static void
3115 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3116 struct die_info *die,
3117 struct partial_symtab *pst)
3118 {
3119 struct objfile *objfile = cu->objfile;
3120 bfd *abfd = objfile->obfd;
3121 struct line_header *lh = NULL;
3122 struct attribute *attr;
3123
3124 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3125 if (attr)
3126 {
3127 unsigned int line_offset = DW_UNSND (attr);
3128
3129 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3130 }
3131 if (lh == NULL)
3132 return; /* No linetable, so no includes. */
3133
3134 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3135 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3136
3137 free_line_header (lh);
3138 }
3139
3140 static hashval_t
3141 hash_type_signature (const void *item)
3142 {
3143 const struct signatured_type *type_sig = item;
3144
3145 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3146 return type_sig->signature;
3147 }
3148
3149 static int
3150 eq_type_signature (const void *item_lhs, const void *item_rhs)
3151 {
3152 const struct signatured_type *lhs = item_lhs;
3153 const struct signatured_type *rhs = item_rhs;
3154
3155 return lhs->signature == rhs->signature;
3156 }
3157
3158 /* Allocate a hash table for signatured types. */
3159
3160 static htab_t
3161 allocate_signatured_type_table (struct objfile *objfile)
3162 {
3163 return htab_create_alloc_ex (41,
3164 hash_type_signature,
3165 eq_type_signature,
3166 NULL,
3167 &objfile->objfile_obstack,
3168 hashtab_obstack_allocate,
3169 dummy_obstack_deallocate);
3170 }
3171
3172 /* A helper function to add a signatured type CU to a list. */
3173
3174 static int
3175 add_signatured_type_cu_to_list (void **slot, void *datum)
3176 {
3177 struct signatured_type *sigt = *slot;
3178 struct dwarf2_per_cu_data ***datap = datum;
3179
3180 **datap = &sigt->per_cu;
3181 ++*datap;
3182
3183 return 1;
3184 }
3185
3186 /* Create the hash table of all entries in the .debug_types section.
3187 The result is zero if there is an error (e.g. missing .debug_types section),
3188 otherwise non-zero. */
3189
3190 static int
3191 create_debug_types_hash_table (struct objfile *objfile)
3192 {
3193 htab_t types_htab = NULL;
3194 struct dwarf2_per_cu_data **iter;
3195 int ix;
3196 struct dwarf2_section_info *section;
3197
3198 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3199 {
3200 dwarf2_per_objfile->signatured_types = NULL;
3201 return 0;
3202 }
3203
3204 for (ix = 0;
3205 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3206 ix, section);
3207 ++ix)
3208 {
3209 gdb_byte *info_ptr, *end_ptr;
3210
3211 dwarf2_read_section (objfile, section);
3212 info_ptr = section->buffer;
3213
3214 if (info_ptr == NULL)
3215 continue;
3216
3217 if (types_htab == NULL)
3218 types_htab = allocate_signatured_type_table (objfile);
3219
3220 if (dwarf2_die_debug)
3221 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3222
3223 end_ptr = info_ptr + section->size;
3224 while (info_ptr < end_ptr)
3225 {
3226 unsigned int offset;
3227 unsigned int offset_size;
3228 unsigned int type_offset;
3229 unsigned int length, initial_length_size;
3230 unsigned short version;
3231 ULONGEST signature;
3232 struct signatured_type *type_sig;
3233 void **slot;
3234 gdb_byte *ptr = info_ptr;
3235
3236 offset = ptr - section->buffer;
3237
3238 /* We need to read the type's signature in order to build the hash
3239 table, but we don't need to read anything else just yet. */
3240
3241 /* Sanity check to ensure entire cu is present. */
3242 length = read_initial_length (objfile->obfd, ptr,
3243 &initial_length_size);
3244 if (ptr + length + initial_length_size > end_ptr)
3245 {
3246 complaint (&symfile_complaints,
3247 _("debug type entry runs off end "
3248 "of `.debug_types' section, ignored"));
3249 break;
3250 }
3251
3252 offset_size = initial_length_size == 4 ? 4 : 8;
3253 ptr += initial_length_size;
3254 version = bfd_get_16 (objfile->obfd, ptr);
3255 ptr += 2;
3256 ptr += offset_size; /* abbrev offset */
3257 ptr += 1; /* address size */
3258 signature = bfd_get_64 (objfile->obfd, ptr);
3259 ptr += 8;
3260 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3261 ptr += offset_size;
3262
3263 /* Skip dummy type units. */
3264 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3265 {
3266 info_ptr = info_ptr + initial_length_size + length;
3267 continue;
3268 }
3269
3270 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3271 memset (type_sig, 0, sizeof (*type_sig));
3272 type_sig->signature = signature;
3273 type_sig->type_offset = type_offset;
3274 type_sig->per_cu.objfile = objfile;
3275 type_sig->per_cu.debug_types_section = section;
3276 type_sig->per_cu.offset = offset;
3277
3278 slot = htab_find_slot (types_htab, type_sig, INSERT);
3279 gdb_assert (slot != NULL);
3280 if (*slot != NULL)
3281 {
3282 const struct signatured_type *dup_sig = *slot;
3283
3284 complaint (&symfile_complaints,
3285 _("debug type entry at offset 0x%x is duplicate to the "
3286 "entry at offset 0x%x, signature 0x%s"),
3287 offset, dup_sig->per_cu.offset,
3288 phex (signature, sizeof (signature)));
3289 gdb_assert (signature == dup_sig->signature);
3290 }
3291 *slot = type_sig;
3292
3293 if (dwarf2_die_debug)
3294 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3295 offset, phex (signature, sizeof (signature)));
3296
3297 info_ptr = info_ptr + initial_length_size + length;
3298 }
3299 }
3300
3301 dwarf2_per_objfile->signatured_types = types_htab;
3302
3303 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3304 dwarf2_per_objfile->type_comp_units
3305 = obstack_alloc (&objfile->objfile_obstack,
3306 dwarf2_per_objfile->n_type_comp_units
3307 * sizeof (struct dwarf2_per_cu_data *));
3308 iter = &dwarf2_per_objfile->type_comp_units[0];
3309 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3310 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3311 == dwarf2_per_objfile->n_type_comp_units);
3312
3313 return 1;
3314 }
3315
3316 /* Lookup a signature based type.
3317 Returns NULL if SIG is not present in the table. */
3318
3319 static struct signatured_type *
3320 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3321 {
3322 struct signatured_type find_entry, *entry;
3323
3324 if (dwarf2_per_objfile->signatured_types == NULL)
3325 {
3326 complaint (&symfile_complaints,
3327 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3328 return 0;
3329 }
3330
3331 find_entry.signature = sig;
3332 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3333 return entry;
3334 }
3335
3336 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3337
3338 static void
3339 init_cu_die_reader (struct die_reader_specs *reader,
3340 struct dwarf2_cu *cu)
3341 {
3342 reader->abfd = cu->objfile->obfd;
3343 reader->cu = cu;
3344 if (cu->per_cu->debug_types_section)
3345 {
3346 gdb_assert (cu->per_cu->debug_types_section->readin);
3347 reader->buffer = cu->per_cu->debug_types_section->buffer;
3348 }
3349 else
3350 {
3351 gdb_assert (dwarf2_per_objfile->info.readin);
3352 reader->buffer = dwarf2_per_objfile->info.buffer;
3353 }
3354 }
3355
3356 /* Find the base address of the compilation unit for range lists and
3357 location lists. It will normally be specified by DW_AT_low_pc.
3358 In DWARF-3 draft 4, the base address could be overridden by
3359 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3360 compilation units with discontinuous ranges. */
3361
3362 static void
3363 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3364 {
3365 struct attribute *attr;
3366
3367 cu->base_known = 0;
3368 cu->base_address = 0;
3369
3370 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3371 if (attr)
3372 {
3373 cu->base_address = DW_ADDR (attr);
3374 cu->base_known = 1;
3375 }
3376 else
3377 {
3378 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3379 if (attr)
3380 {
3381 cu->base_address = DW_ADDR (attr);
3382 cu->base_known = 1;
3383 }
3384 }
3385 }
3386
3387 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3388 to combine the common parts.
3389 Process a compilation unit for a psymtab.
3390 BUFFER is a pointer to the beginning of the dwarf section buffer,
3391 either .debug_info or debug_types.
3392 INFO_PTR is a pointer to the start of the CU.
3393 Returns a pointer to the next CU. */
3394
3395 static gdb_byte *
3396 process_psymtab_comp_unit (struct objfile *objfile,
3397 struct dwarf2_per_cu_data *this_cu,
3398 gdb_byte *buffer, gdb_byte *info_ptr,
3399 unsigned int buffer_size)
3400 {
3401 bfd *abfd = objfile->obfd;
3402 gdb_byte *beg_of_comp_unit = info_ptr;
3403 struct die_info *comp_unit_die;
3404 struct partial_symtab *pst;
3405 CORE_ADDR baseaddr;
3406 struct cleanup *back_to_inner;
3407 struct dwarf2_cu cu;
3408 int has_children, has_pc_info;
3409 struct attribute *attr;
3410 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3411 struct die_reader_specs reader_specs;
3412 const char *filename;
3413
3414 init_one_comp_unit (&cu, objfile);
3415 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3416
3417 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3418 buffer, buffer_size,
3419 abfd,
3420 this_cu->debug_types_section != NULL);
3421
3422 /* Skip dummy compilation units. */
3423 if (info_ptr >= buffer + buffer_size
3424 || peek_abbrev_code (abfd, info_ptr) == 0)
3425 {
3426 info_ptr = (beg_of_comp_unit + cu.header.length
3427 + cu.header.initial_length_size);
3428 do_cleanups (back_to_inner);
3429 return info_ptr;
3430 }
3431
3432 cu.list_in_scope = &file_symbols;
3433
3434 /* If this compilation unit was already read in, free the
3435 cached copy in order to read it in again. This is
3436 necessary because we skipped some symbols when we first
3437 read in the compilation unit (see load_partial_dies).
3438 This problem could be avoided, but the benefit is
3439 unclear. */
3440 if (this_cu->cu != NULL)
3441 free_one_cached_comp_unit (this_cu->cu);
3442
3443 /* Note that this is a pointer to our stack frame, being
3444 added to a global data structure. It will be cleaned up
3445 in free_stack_comp_unit when we finish with this
3446 compilation unit. */
3447 this_cu->cu = &cu;
3448 cu.per_cu = this_cu;
3449
3450 /* Read the abbrevs for this compilation unit into a table. */
3451 dwarf2_read_abbrevs (abfd, &cu);
3452 make_cleanup (dwarf2_free_abbrev_table, &cu);
3453
3454 /* Read the compilation unit die. */
3455 init_cu_die_reader (&reader_specs, &cu);
3456 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3457 &has_children);
3458
3459 if (this_cu->debug_types_section)
3460 {
3461 /* LENGTH has not been set yet for type units. */
3462 gdb_assert (this_cu->offset == cu.header.offset);
3463 this_cu->length = cu.header.length + cu.header.initial_length_size;
3464 }
3465 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3466 {
3467 info_ptr = (beg_of_comp_unit + cu.header.length
3468 + cu.header.initial_length_size);
3469 do_cleanups (back_to_inner);
3470 return info_ptr;
3471 }
3472
3473 prepare_one_comp_unit (&cu, comp_unit_die);
3474
3475 /* Allocate a new partial symbol table structure. */
3476 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3477 if (attr == NULL || !DW_STRING (attr))
3478 filename = "";
3479 else
3480 filename = DW_STRING (attr);
3481 pst = start_psymtab_common (objfile, objfile->section_offsets,
3482 filename,
3483 /* TEXTLOW and TEXTHIGH are set below. */
3484 0,
3485 objfile->global_psymbols.next,
3486 objfile->static_psymbols.next);
3487 pst->psymtabs_addrmap_supported = 1;
3488
3489 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3490 if (attr != NULL)
3491 pst->dirname = DW_STRING (attr);
3492
3493 pst->read_symtab_private = this_cu;
3494
3495 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3496
3497 /* Store the function that reads in the rest of the symbol table. */
3498 pst->read_symtab = dwarf2_psymtab_to_symtab;
3499
3500 this_cu->v.psymtab = pst;
3501
3502 dwarf2_find_base_address (comp_unit_die, &cu);
3503
3504 /* Possibly set the default values of LOWPC and HIGHPC from
3505 `DW_AT_ranges'. */
3506 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3507 &best_highpc, &cu, pst);
3508 if (has_pc_info == 1 && best_lowpc < best_highpc)
3509 /* Store the contiguous range if it is not empty; it can be empty for
3510 CUs with no code. */
3511 addrmap_set_empty (objfile->psymtabs_addrmap,
3512 best_lowpc + baseaddr,
3513 best_highpc + baseaddr - 1, pst);
3514
3515 /* Check if comp unit has_children.
3516 If so, read the rest of the partial symbols from this comp unit.
3517 If not, there's no more debug_info for this comp unit. */
3518 if (has_children)
3519 {
3520 struct partial_die_info *first_die;
3521 CORE_ADDR lowpc, highpc;
3522
3523 lowpc = ((CORE_ADDR) -1);
3524 highpc = ((CORE_ADDR) 0);
3525
3526 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3527
3528 scan_partial_symbols (first_die, &lowpc, &highpc,
3529 ! has_pc_info, &cu);
3530
3531 /* If we didn't find a lowpc, set it to highpc to avoid
3532 complaints from `maint check'. */
3533 if (lowpc == ((CORE_ADDR) -1))
3534 lowpc = highpc;
3535
3536 /* If the compilation unit didn't have an explicit address range,
3537 then use the information extracted from its child dies. */
3538 if (! has_pc_info)
3539 {
3540 best_lowpc = lowpc;
3541 best_highpc = highpc;
3542 }
3543 }
3544 pst->textlow = best_lowpc + baseaddr;
3545 pst->texthigh = best_highpc + baseaddr;
3546
3547 pst->n_global_syms = objfile->global_psymbols.next -
3548 (objfile->global_psymbols.list + pst->globals_offset);
3549 pst->n_static_syms = objfile->static_psymbols.next -
3550 (objfile->static_psymbols.list + pst->statics_offset);
3551 sort_pst_symbols (pst);
3552
3553 info_ptr = (beg_of_comp_unit + cu.header.length
3554 + cu.header.initial_length_size);
3555
3556 if (this_cu->debug_types_section)
3557 {
3558 /* It's not clear we want to do anything with stmt lists here.
3559 Waiting to see what gcc ultimately does. */
3560 }
3561 else
3562 {
3563 /* Get the list of files included in the current compilation unit,
3564 and build a psymtab for each of them. */
3565 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3566 }
3567
3568 do_cleanups (back_to_inner);
3569
3570 return info_ptr;
3571 }
3572
3573 /* Traversal function for htab_traverse_noresize.
3574 Process one .debug_types comp-unit. */
3575
3576 static int
3577 process_type_comp_unit (void **slot, void *info)
3578 {
3579 struct signatured_type *entry = (struct signatured_type *) *slot;
3580 struct objfile *objfile = (struct objfile *) info;
3581 struct dwarf2_per_cu_data *this_cu;
3582
3583 this_cu = &entry->per_cu;
3584
3585 gdb_assert (this_cu->debug_types_section->readin);
3586 process_psymtab_comp_unit (objfile, this_cu,
3587 this_cu->debug_types_section->buffer,
3588 (this_cu->debug_types_section->buffer
3589 + this_cu->offset),
3590 this_cu->debug_types_section->size);
3591
3592 return 1;
3593 }
3594
3595 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3596 Build partial symbol tables for the .debug_types comp-units. */
3597
3598 static void
3599 build_type_psymtabs (struct objfile *objfile)
3600 {
3601 if (! create_debug_types_hash_table (objfile))
3602 return;
3603
3604 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3605 process_type_comp_unit, objfile);
3606 }
3607
3608 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3609
3610 static void
3611 psymtabs_addrmap_cleanup (void *o)
3612 {
3613 struct objfile *objfile = o;
3614
3615 objfile->psymtabs_addrmap = NULL;
3616 }
3617
3618 /* Build the partial symbol table by doing a quick pass through the
3619 .debug_info and .debug_abbrev sections. */
3620
3621 static void
3622 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3623 {
3624 gdb_byte *info_ptr;
3625 struct cleanup *back_to, *addrmap_cleanup;
3626 struct obstack temp_obstack;
3627
3628 dwarf2_per_objfile->reading_partial_symbols = 1;
3629
3630 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3631 info_ptr = dwarf2_per_objfile->info.buffer;
3632
3633 /* Any cached compilation units will be linked by the per-objfile
3634 read_in_chain. Make sure to free them when we're done. */
3635 back_to = make_cleanup (free_cached_comp_units, NULL);
3636
3637 build_type_psymtabs (objfile);
3638
3639 create_all_comp_units (objfile);
3640
3641 /* Create a temporary address map on a temporary obstack. We later
3642 copy this to the final obstack. */
3643 obstack_init (&temp_obstack);
3644 make_cleanup_obstack_free (&temp_obstack);
3645 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3646 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3647
3648 /* Since the objects we're extracting from .debug_info vary in
3649 length, only the individual functions to extract them (like
3650 read_comp_unit_head and load_partial_die) can really know whether
3651 the buffer is large enough to hold another complete object.
3652
3653 At the moment, they don't actually check that. If .debug_info
3654 holds just one extra byte after the last compilation unit's dies,
3655 then read_comp_unit_head will happily read off the end of the
3656 buffer. read_partial_die is similarly casual. Those functions
3657 should be fixed.
3658
3659 For this loop condition, simply checking whether there's any data
3660 left at all should be sufficient. */
3661
3662 while (info_ptr < (dwarf2_per_objfile->info.buffer
3663 + dwarf2_per_objfile->info.size))
3664 {
3665 struct dwarf2_per_cu_data *this_cu;
3666
3667 this_cu = dwarf2_find_comp_unit (info_ptr
3668 - dwarf2_per_objfile->info.buffer,
3669 objfile);
3670
3671 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3672 dwarf2_per_objfile->info.buffer,
3673 info_ptr,
3674 dwarf2_per_objfile->info.size);
3675 }
3676
3677 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3678 &objfile->objfile_obstack);
3679 discard_cleanups (addrmap_cleanup);
3680
3681 do_cleanups (back_to);
3682 }
3683
3684 /* Load the partial DIEs for a secondary CU into memory. */
3685
3686 static void
3687 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3688 struct objfile *objfile)
3689 {
3690 bfd *abfd = objfile->obfd;
3691 gdb_byte *info_ptr;
3692 struct die_info *comp_unit_die;
3693 struct dwarf2_cu *cu;
3694 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3695 int has_children;
3696 struct die_reader_specs reader_specs;
3697 int read_cu = 0;
3698
3699 gdb_assert (! this_cu->debug_types_section);
3700
3701 gdb_assert (dwarf2_per_objfile->info.readin);
3702 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3703
3704 if (this_cu->cu == NULL)
3705 {
3706 cu = xmalloc (sizeof (*cu));
3707 init_one_comp_unit (cu, objfile);
3708
3709 read_cu = 1;
3710
3711 /* If an error occurs while loading, release our storage. */
3712 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3713
3714 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3715 dwarf2_per_objfile->info.buffer,
3716 dwarf2_per_objfile->info.size,
3717 abfd, 0);
3718
3719 /* Skip dummy compilation units. */
3720 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3721 + dwarf2_per_objfile->info.size)
3722 || peek_abbrev_code (abfd, info_ptr) == 0)
3723 {
3724 do_cleanups (free_cu_cleanup);
3725 return;
3726 }
3727
3728 /* Link this compilation unit into the compilation unit tree. */
3729 this_cu->cu = cu;
3730 cu->per_cu = this_cu;
3731
3732 /* Link this CU into read_in_chain. */
3733 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3734 dwarf2_per_objfile->read_in_chain = this_cu;
3735 }
3736 else
3737 {
3738 cu = this_cu->cu;
3739 info_ptr += cu->header.first_die_offset;
3740 }
3741
3742 /* Read the abbrevs for this compilation unit into a table. */
3743 gdb_assert (cu->dwarf2_abbrevs == NULL);
3744 dwarf2_read_abbrevs (abfd, cu);
3745 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3746
3747 /* Read the compilation unit die. */
3748 init_cu_die_reader (&reader_specs, cu);
3749 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3750 &has_children);
3751
3752 prepare_one_comp_unit (cu, comp_unit_die);
3753
3754 /* Check if comp unit has_children.
3755 If so, read the rest of the partial symbols from this comp unit.
3756 If not, there's no more debug_info for this comp unit. */
3757 if (has_children)
3758 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3759
3760 do_cleanups (free_abbrevs_cleanup);
3761
3762 if (read_cu)
3763 {
3764 /* We've successfully allocated this compilation unit. Let our
3765 caller clean it up when finished with it. */
3766 discard_cleanups (free_cu_cleanup);
3767 }
3768 }
3769
3770 /* Create a list of all compilation units in OBJFILE. We do this only
3771 if an inter-comp-unit reference is found; presumably if there is one,
3772 there will be many, and one will occur early in the .debug_info section.
3773 So there's no point in building this list incrementally. */
3774
3775 static void
3776 create_all_comp_units (struct objfile *objfile)
3777 {
3778 int n_allocated;
3779 int n_comp_units;
3780 struct dwarf2_per_cu_data **all_comp_units;
3781 gdb_byte *info_ptr;
3782
3783 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3784 info_ptr = dwarf2_per_objfile->info.buffer;
3785
3786 n_comp_units = 0;
3787 n_allocated = 10;
3788 all_comp_units = xmalloc (n_allocated
3789 * sizeof (struct dwarf2_per_cu_data *));
3790
3791 while (info_ptr < dwarf2_per_objfile->info.buffer
3792 + dwarf2_per_objfile->info.size)
3793 {
3794 unsigned int length, initial_length_size;
3795 struct dwarf2_per_cu_data *this_cu;
3796 unsigned int offset;
3797
3798 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3799
3800 /* Read just enough information to find out where the next
3801 compilation unit is. */
3802 length = read_initial_length (objfile->obfd, info_ptr,
3803 &initial_length_size);
3804
3805 /* Save the compilation unit for later lookup. */
3806 this_cu = obstack_alloc (&objfile->objfile_obstack,
3807 sizeof (struct dwarf2_per_cu_data));
3808 memset (this_cu, 0, sizeof (*this_cu));
3809 this_cu->offset = offset;
3810 this_cu->length = length + initial_length_size;
3811 this_cu->objfile = objfile;
3812
3813 if (n_comp_units == n_allocated)
3814 {
3815 n_allocated *= 2;
3816 all_comp_units = xrealloc (all_comp_units,
3817 n_allocated
3818 * sizeof (struct dwarf2_per_cu_data *));
3819 }
3820 all_comp_units[n_comp_units++] = this_cu;
3821
3822 info_ptr = info_ptr + this_cu->length;
3823 }
3824
3825 dwarf2_per_objfile->all_comp_units
3826 = obstack_alloc (&objfile->objfile_obstack,
3827 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3828 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3829 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3830 xfree (all_comp_units);
3831 dwarf2_per_objfile->n_comp_units = n_comp_units;
3832 }
3833
3834 /* Process all loaded DIEs for compilation unit CU, starting at
3835 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3836 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3837 DW_AT_ranges). If NEED_PC is set, then this function will set
3838 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3839 and record the covered ranges in the addrmap. */
3840
3841 static void
3842 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3843 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3844 {
3845 struct partial_die_info *pdi;
3846
3847 /* Now, march along the PDI's, descending into ones which have
3848 interesting children but skipping the children of the other ones,
3849 until we reach the end of the compilation unit. */
3850
3851 pdi = first_die;
3852
3853 while (pdi != NULL)
3854 {
3855 fixup_partial_die (pdi, cu);
3856
3857 /* Anonymous namespaces or modules have no name but have interesting
3858 children, so we need to look at them. Ditto for anonymous
3859 enums. */
3860
3861 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3862 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3863 {
3864 switch (pdi->tag)
3865 {
3866 case DW_TAG_subprogram:
3867 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3868 break;
3869 case DW_TAG_constant:
3870 case DW_TAG_variable:
3871 case DW_TAG_typedef:
3872 case DW_TAG_union_type:
3873 if (!pdi->is_declaration)
3874 {
3875 add_partial_symbol (pdi, cu);
3876 }
3877 break;
3878 case DW_TAG_class_type:
3879 case DW_TAG_interface_type:
3880 case DW_TAG_structure_type:
3881 if (!pdi->is_declaration)
3882 {
3883 add_partial_symbol (pdi, cu);
3884 }
3885 break;
3886 case DW_TAG_enumeration_type:
3887 if (!pdi->is_declaration)
3888 add_partial_enumeration (pdi, cu);
3889 break;
3890 case DW_TAG_base_type:
3891 case DW_TAG_subrange_type:
3892 /* File scope base type definitions are added to the partial
3893 symbol table. */
3894 add_partial_symbol (pdi, cu);
3895 break;
3896 case DW_TAG_namespace:
3897 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3898 break;
3899 case DW_TAG_module:
3900 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3901 break;
3902 default:
3903 break;
3904 }
3905 }
3906
3907 /* If the die has a sibling, skip to the sibling. */
3908
3909 pdi = pdi->die_sibling;
3910 }
3911 }
3912
3913 /* Functions used to compute the fully scoped name of a partial DIE.
3914
3915 Normally, this is simple. For C++, the parent DIE's fully scoped
3916 name is concatenated with "::" and the partial DIE's name. For
3917 Java, the same thing occurs except that "." is used instead of "::".
3918 Enumerators are an exception; they use the scope of their parent
3919 enumeration type, i.e. the name of the enumeration type is not
3920 prepended to the enumerator.
3921
3922 There are two complexities. One is DW_AT_specification; in this
3923 case "parent" means the parent of the target of the specification,
3924 instead of the direct parent of the DIE. The other is compilers
3925 which do not emit DW_TAG_namespace; in this case we try to guess
3926 the fully qualified name of structure types from their members'
3927 linkage names. This must be done using the DIE's children rather
3928 than the children of any DW_AT_specification target. We only need
3929 to do this for structures at the top level, i.e. if the target of
3930 any DW_AT_specification (if any; otherwise the DIE itself) does not
3931 have a parent. */
3932
3933 /* Compute the scope prefix associated with PDI's parent, in
3934 compilation unit CU. The result will be allocated on CU's
3935 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3936 field. NULL is returned if no prefix is necessary. */
3937 static char *
3938 partial_die_parent_scope (struct partial_die_info *pdi,
3939 struct dwarf2_cu *cu)
3940 {
3941 char *grandparent_scope;
3942 struct partial_die_info *parent, *real_pdi;
3943
3944 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3945 then this means the parent of the specification DIE. */
3946
3947 real_pdi = pdi;
3948 while (real_pdi->has_specification)
3949 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3950
3951 parent = real_pdi->die_parent;
3952 if (parent == NULL)
3953 return NULL;
3954
3955 if (parent->scope_set)
3956 return parent->scope;
3957
3958 fixup_partial_die (parent, cu);
3959
3960 grandparent_scope = partial_die_parent_scope (parent, cu);
3961
3962 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3963 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3964 Work around this problem here. */
3965 if (cu->language == language_cplus
3966 && parent->tag == DW_TAG_namespace
3967 && strcmp (parent->name, "::") == 0
3968 && grandparent_scope == NULL)
3969 {
3970 parent->scope = NULL;
3971 parent->scope_set = 1;
3972 return NULL;
3973 }
3974
3975 if (pdi->tag == DW_TAG_enumerator)
3976 /* Enumerators should not get the name of the enumeration as a prefix. */
3977 parent->scope = grandparent_scope;
3978 else if (parent->tag == DW_TAG_namespace
3979 || parent->tag == DW_TAG_module
3980 || parent->tag == DW_TAG_structure_type
3981 || parent->tag == DW_TAG_class_type
3982 || parent->tag == DW_TAG_interface_type
3983 || parent->tag == DW_TAG_union_type
3984 || parent->tag == DW_TAG_enumeration_type)
3985 {
3986 if (grandparent_scope == NULL)
3987 parent->scope = parent->name;
3988 else
3989 parent->scope = typename_concat (&cu->comp_unit_obstack,
3990 grandparent_scope,
3991 parent->name, 0, cu);
3992 }
3993 else
3994 {
3995 /* FIXME drow/2004-04-01: What should we be doing with
3996 function-local names? For partial symbols, we should probably be
3997 ignoring them. */
3998 complaint (&symfile_complaints,
3999 _("unhandled containing DIE tag %d for DIE at %d"),
4000 parent->tag, pdi->offset);
4001 parent->scope = grandparent_scope;
4002 }
4003
4004 parent->scope_set = 1;
4005 return parent->scope;
4006 }
4007
4008 /* Return the fully scoped name associated with PDI, from compilation unit
4009 CU. The result will be allocated with malloc. */
4010 static char *
4011 partial_die_full_name (struct partial_die_info *pdi,
4012 struct dwarf2_cu *cu)
4013 {
4014 char *parent_scope;
4015
4016 /* If this is a template instantiation, we can not work out the
4017 template arguments from partial DIEs. So, unfortunately, we have
4018 to go through the full DIEs. At least any work we do building
4019 types here will be reused if full symbols are loaded later. */
4020 if (pdi->has_template_arguments)
4021 {
4022 fixup_partial_die (pdi, cu);
4023
4024 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4025 {
4026 struct die_info *die;
4027 struct attribute attr;
4028 struct dwarf2_cu *ref_cu = cu;
4029
4030 attr.name = 0;
4031 attr.form = DW_FORM_ref_addr;
4032 attr.u.addr = pdi->offset;
4033 die = follow_die_ref (NULL, &attr, &ref_cu);
4034
4035 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4036 }
4037 }
4038
4039 parent_scope = partial_die_parent_scope (pdi, cu);
4040 if (parent_scope == NULL)
4041 return NULL;
4042 else
4043 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4044 }
4045
4046 static void
4047 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4048 {
4049 struct objfile *objfile = cu->objfile;
4050 CORE_ADDR addr = 0;
4051 char *actual_name = NULL;
4052 const struct partial_symbol *psym = NULL;
4053 CORE_ADDR baseaddr;
4054 int built_actual_name = 0;
4055
4056 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4057
4058 actual_name = partial_die_full_name (pdi, cu);
4059 if (actual_name)
4060 built_actual_name = 1;
4061
4062 if (actual_name == NULL)
4063 actual_name = pdi->name;
4064
4065 switch (pdi->tag)
4066 {
4067 case DW_TAG_subprogram:
4068 if (pdi->is_external || cu->language == language_ada)
4069 {
4070 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4071 of the global scope. But in Ada, we want to be able to access
4072 nested procedures globally. So all Ada subprograms are stored
4073 in the global scope. */
4074 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4075 mst_text, objfile); */
4076 add_psymbol_to_list (actual_name, strlen (actual_name),
4077 built_actual_name,
4078 VAR_DOMAIN, LOC_BLOCK,
4079 &objfile->global_psymbols,
4080 0, pdi->lowpc + baseaddr,
4081 cu->language, objfile);
4082 }
4083 else
4084 {
4085 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4086 mst_file_text, objfile); */
4087 add_psymbol_to_list (actual_name, strlen (actual_name),
4088 built_actual_name,
4089 VAR_DOMAIN, LOC_BLOCK,
4090 &objfile->static_psymbols,
4091 0, pdi->lowpc + baseaddr,
4092 cu->language, objfile);
4093 }
4094 break;
4095 case DW_TAG_constant:
4096 {
4097 struct psymbol_allocation_list *list;
4098
4099 if (pdi->is_external)
4100 list = &objfile->global_psymbols;
4101 else
4102 list = &objfile->static_psymbols;
4103 add_psymbol_to_list (actual_name, strlen (actual_name),
4104 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4105 list, 0, 0, cu->language, objfile);
4106 }
4107 break;
4108 case DW_TAG_variable:
4109 if (pdi->locdesc)
4110 addr = decode_locdesc (pdi->locdesc, cu);
4111
4112 if (pdi->locdesc
4113 && addr == 0
4114 && !dwarf2_per_objfile->has_section_at_zero)
4115 {
4116 /* A global or static variable may also have been stripped
4117 out by the linker if unused, in which case its address
4118 will be nullified; do not add such variables into partial
4119 symbol table then. */
4120 }
4121 else if (pdi->is_external)
4122 {
4123 /* Global Variable.
4124 Don't enter into the minimal symbol tables as there is
4125 a minimal symbol table entry from the ELF symbols already.
4126 Enter into partial symbol table if it has a location
4127 descriptor or a type.
4128 If the location descriptor is missing, new_symbol will create
4129 a LOC_UNRESOLVED symbol, the address of the variable will then
4130 be determined from the minimal symbol table whenever the variable
4131 is referenced.
4132 The address for the partial symbol table entry is not
4133 used by GDB, but it comes in handy for debugging partial symbol
4134 table building. */
4135
4136 if (pdi->locdesc || pdi->has_type)
4137 add_psymbol_to_list (actual_name, strlen (actual_name),
4138 built_actual_name,
4139 VAR_DOMAIN, LOC_STATIC,
4140 &objfile->global_psymbols,
4141 0, addr + baseaddr,
4142 cu->language, objfile);
4143 }
4144 else
4145 {
4146 /* Static Variable. Skip symbols without location descriptors. */
4147 if (pdi->locdesc == NULL)
4148 {
4149 if (built_actual_name)
4150 xfree (actual_name);
4151 return;
4152 }
4153 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4154 mst_file_data, objfile); */
4155 add_psymbol_to_list (actual_name, strlen (actual_name),
4156 built_actual_name,
4157 VAR_DOMAIN, LOC_STATIC,
4158 &objfile->static_psymbols,
4159 0, addr + baseaddr,
4160 cu->language, objfile);
4161 }
4162 break;
4163 case DW_TAG_typedef:
4164 case DW_TAG_base_type:
4165 case DW_TAG_subrange_type:
4166 add_psymbol_to_list (actual_name, strlen (actual_name),
4167 built_actual_name,
4168 VAR_DOMAIN, LOC_TYPEDEF,
4169 &objfile->static_psymbols,
4170 0, (CORE_ADDR) 0, cu->language, objfile);
4171 break;
4172 case DW_TAG_namespace:
4173 add_psymbol_to_list (actual_name, strlen (actual_name),
4174 built_actual_name,
4175 VAR_DOMAIN, LOC_TYPEDEF,
4176 &objfile->global_psymbols,
4177 0, (CORE_ADDR) 0, cu->language, objfile);
4178 break;
4179 case DW_TAG_class_type:
4180 case DW_TAG_interface_type:
4181 case DW_TAG_structure_type:
4182 case DW_TAG_union_type:
4183 case DW_TAG_enumeration_type:
4184 /* Skip external references. The DWARF standard says in the section
4185 about "Structure, Union, and Class Type Entries": "An incomplete
4186 structure, union or class type is represented by a structure,
4187 union or class entry that does not have a byte size attribute
4188 and that has a DW_AT_declaration attribute." */
4189 if (!pdi->has_byte_size && pdi->is_declaration)
4190 {
4191 if (built_actual_name)
4192 xfree (actual_name);
4193 return;
4194 }
4195
4196 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4197 static vs. global. */
4198 add_psymbol_to_list (actual_name, strlen (actual_name),
4199 built_actual_name,
4200 STRUCT_DOMAIN, LOC_TYPEDEF,
4201 (cu->language == language_cplus
4202 || cu->language == language_java)
4203 ? &objfile->global_psymbols
4204 : &objfile->static_psymbols,
4205 0, (CORE_ADDR) 0, cu->language, objfile);
4206
4207 break;
4208 case DW_TAG_enumerator:
4209 add_psymbol_to_list (actual_name, strlen (actual_name),
4210 built_actual_name,
4211 VAR_DOMAIN, LOC_CONST,
4212 (cu->language == language_cplus
4213 || cu->language == language_java)
4214 ? &objfile->global_psymbols
4215 : &objfile->static_psymbols,
4216 0, (CORE_ADDR) 0, cu->language, objfile);
4217 break;
4218 default:
4219 break;
4220 }
4221
4222 if (built_actual_name)
4223 xfree (actual_name);
4224 }
4225
4226 /* Read a partial die corresponding to a namespace; also, add a symbol
4227 corresponding to that namespace to the symbol table. NAMESPACE is
4228 the name of the enclosing namespace. */
4229
4230 static void
4231 add_partial_namespace (struct partial_die_info *pdi,
4232 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4233 int need_pc, struct dwarf2_cu *cu)
4234 {
4235 /* Add a symbol for the namespace. */
4236
4237 add_partial_symbol (pdi, cu);
4238
4239 /* Now scan partial symbols in that namespace. */
4240
4241 if (pdi->has_children)
4242 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4243 }
4244
4245 /* Read a partial die corresponding to a Fortran module. */
4246
4247 static void
4248 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4249 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4250 {
4251 /* Now scan partial symbols in that module. */
4252
4253 if (pdi->has_children)
4254 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4255 }
4256
4257 /* Read a partial die corresponding to a subprogram and create a partial
4258 symbol for that subprogram. When the CU language allows it, this
4259 routine also defines a partial symbol for each nested subprogram
4260 that this subprogram contains.
4261
4262 DIE my also be a lexical block, in which case we simply search
4263 recursively for suprograms defined inside that lexical block.
4264 Again, this is only performed when the CU language allows this
4265 type of definitions. */
4266
4267 static void
4268 add_partial_subprogram (struct partial_die_info *pdi,
4269 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4270 int need_pc, struct dwarf2_cu *cu)
4271 {
4272 if (pdi->tag == DW_TAG_subprogram)
4273 {
4274 if (pdi->has_pc_info)
4275 {
4276 if (pdi->lowpc < *lowpc)
4277 *lowpc = pdi->lowpc;
4278 if (pdi->highpc > *highpc)
4279 *highpc = pdi->highpc;
4280 if (need_pc)
4281 {
4282 CORE_ADDR baseaddr;
4283 struct objfile *objfile = cu->objfile;
4284
4285 baseaddr = ANOFFSET (objfile->section_offsets,
4286 SECT_OFF_TEXT (objfile));
4287 addrmap_set_empty (objfile->psymtabs_addrmap,
4288 pdi->lowpc + baseaddr,
4289 pdi->highpc - 1 + baseaddr,
4290 cu->per_cu->v.psymtab);
4291 }
4292 if (!pdi->is_declaration)
4293 /* Ignore subprogram DIEs that do not have a name, they are
4294 illegal. Do not emit a complaint at this point, we will
4295 do so when we convert this psymtab into a symtab. */
4296 if (pdi->name)
4297 add_partial_symbol (pdi, cu);
4298 }
4299 }
4300
4301 if (! pdi->has_children)
4302 return;
4303
4304 if (cu->language == language_ada)
4305 {
4306 pdi = pdi->die_child;
4307 while (pdi != NULL)
4308 {
4309 fixup_partial_die (pdi, cu);
4310 if (pdi->tag == DW_TAG_subprogram
4311 || pdi->tag == DW_TAG_lexical_block)
4312 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4313 pdi = pdi->die_sibling;
4314 }
4315 }
4316 }
4317
4318 /* Read a partial die corresponding to an enumeration type. */
4319
4320 static void
4321 add_partial_enumeration (struct partial_die_info *enum_pdi,
4322 struct dwarf2_cu *cu)
4323 {
4324 struct partial_die_info *pdi;
4325
4326 if (enum_pdi->name != NULL)
4327 add_partial_symbol (enum_pdi, cu);
4328
4329 pdi = enum_pdi->die_child;
4330 while (pdi)
4331 {
4332 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4333 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4334 else
4335 add_partial_symbol (pdi, cu);
4336 pdi = pdi->die_sibling;
4337 }
4338 }
4339
4340 /* Return the initial uleb128 in the die at INFO_PTR. */
4341
4342 static unsigned int
4343 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4344 {
4345 unsigned int bytes_read;
4346
4347 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4348 }
4349
4350 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4351 Return the corresponding abbrev, or NULL if the number is zero (indicating
4352 an empty DIE). In either case *BYTES_READ will be set to the length of
4353 the initial number. */
4354
4355 static struct abbrev_info *
4356 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4357 struct dwarf2_cu *cu)
4358 {
4359 bfd *abfd = cu->objfile->obfd;
4360 unsigned int abbrev_number;
4361 struct abbrev_info *abbrev;
4362
4363 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4364
4365 if (abbrev_number == 0)
4366 return NULL;
4367
4368 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4369 if (!abbrev)
4370 {
4371 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4372 abbrev_number, bfd_get_filename (abfd));
4373 }
4374
4375 return abbrev;
4376 }
4377
4378 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4379 Returns a pointer to the end of a series of DIEs, terminated by an empty
4380 DIE. Any children of the skipped DIEs will also be skipped. */
4381
4382 static gdb_byte *
4383 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4384 {
4385 struct abbrev_info *abbrev;
4386 unsigned int bytes_read;
4387
4388 while (1)
4389 {
4390 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4391 if (abbrev == NULL)
4392 return info_ptr + bytes_read;
4393 else
4394 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4395 }
4396 }
4397
4398 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4399 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4400 abbrev corresponding to that skipped uleb128 should be passed in
4401 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4402 children. */
4403
4404 static gdb_byte *
4405 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4406 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4407 {
4408 unsigned int bytes_read;
4409 struct attribute attr;
4410 bfd *abfd = cu->objfile->obfd;
4411 unsigned int form, i;
4412
4413 for (i = 0; i < abbrev->num_attrs; i++)
4414 {
4415 /* The only abbrev we care about is DW_AT_sibling. */
4416 if (abbrev->attrs[i].name == DW_AT_sibling)
4417 {
4418 read_attribute (&attr, &abbrev->attrs[i],
4419 abfd, info_ptr, cu);
4420 if (attr.form == DW_FORM_ref_addr)
4421 complaint (&symfile_complaints,
4422 _("ignoring absolute DW_AT_sibling"));
4423 else
4424 return buffer + dwarf2_get_ref_die_offset (&attr);
4425 }
4426
4427 /* If it isn't DW_AT_sibling, skip this attribute. */
4428 form = abbrev->attrs[i].form;
4429 skip_attribute:
4430 switch (form)
4431 {
4432 case DW_FORM_ref_addr:
4433 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4434 and later it is offset sized. */
4435 if (cu->header.version == 2)
4436 info_ptr += cu->header.addr_size;
4437 else
4438 info_ptr += cu->header.offset_size;
4439 break;
4440 case DW_FORM_addr:
4441 info_ptr += cu->header.addr_size;
4442 break;
4443 case DW_FORM_data1:
4444 case DW_FORM_ref1:
4445 case DW_FORM_flag:
4446 info_ptr += 1;
4447 break;
4448 case DW_FORM_flag_present:
4449 break;
4450 case DW_FORM_data2:
4451 case DW_FORM_ref2:
4452 info_ptr += 2;
4453 break;
4454 case DW_FORM_data4:
4455 case DW_FORM_ref4:
4456 info_ptr += 4;
4457 break;
4458 case DW_FORM_data8:
4459 case DW_FORM_ref8:
4460 case DW_FORM_ref_sig8:
4461 info_ptr += 8;
4462 break;
4463 case DW_FORM_string:
4464 read_direct_string (abfd, info_ptr, &bytes_read);
4465 info_ptr += bytes_read;
4466 break;
4467 case DW_FORM_sec_offset:
4468 case DW_FORM_strp:
4469 info_ptr += cu->header.offset_size;
4470 break;
4471 case DW_FORM_exprloc:
4472 case DW_FORM_block:
4473 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4474 info_ptr += bytes_read;
4475 break;
4476 case DW_FORM_block1:
4477 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4478 break;
4479 case DW_FORM_block2:
4480 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4481 break;
4482 case DW_FORM_block4:
4483 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4484 break;
4485 case DW_FORM_sdata:
4486 case DW_FORM_udata:
4487 case DW_FORM_ref_udata:
4488 info_ptr = skip_leb128 (abfd, info_ptr);
4489 break;
4490 case DW_FORM_indirect:
4491 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4492 info_ptr += bytes_read;
4493 /* We need to continue parsing from here, so just go back to
4494 the top. */
4495 goto skip_attribute;
4496
4497 default:
4498 error (_("Dwarf Error: Cannot handle %s "
4499 "in DWARF reader [in module %s]"),
4500 dwarf_form_name (form),
4501 bfd_get_filename (abfd));
4502 }
4503 }
4504
4505 if (abbrev->has_children)
4506 return skip_children (buffer, info_ptr, cu);
4507 else
4508 return info_ptr;
4509 }
4510
4511 /* Locate ORIG_PDI's sibling.
4512 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4513 in BUFFER. */
4514
4515 static gdb_byte *
4516 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4517 gdb_byte *buffer, gdb_byte *info_ptr,
4518 bfd *abfd, struct dwarf2_cu *cu)
4519 {
4520 /* Do we know the sibling already? */
4521
4522 if (orig_pdi->sibling)
4523 return orig_pdi->sibling;
4524
4525 /* Are there any children to deal with? */
4526
4527 if (!orig_pdi->has_children)
4528 return info_ptr;
4529
4530 /* Skip the children the long way. */
4531
4532 return skip_children (buffer, info_ptr, cu);
4533 }
4534
4535 /* Expand this partial symbol table into a full symbol table. */
4536
4537 static void
4538 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4539 {
4540 if (pst != NULL)
4541 {
4542 if (pst->readin)
4543 {
4544 warning (_("bug: psymtab for %s is already read in."),
4545 pst->filename);
4546 }
4547 else
4548 {
4549 if (info_verbose)
4550 {
4551 printf_filtered (_("Reading in symbols for %s..."),
4552 pst->filename);
4553 gdb_flush (gdb_stdout);
4554 }
4555
4556 /* Restore our global data. */
4557 dwarf2_per_objfile = objfile_data (pst->objfile,
4558 dwarf2_objfile_data_key);
4559
4560 /* If this psymtab is constructed from a debug-only objfile, the
4561 has_section_at_zero flag will not necessarily be correct. We
4562 can get the correct value for this flag by looking at the data
4563 associated with the (presumably stripped) associated objfile. */
4564 if (pst->objfile->separate_debug_objfile_backlink)
4565 {
4566 struct dwarf2_per_objfile *dpo_backlink
4567 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4568 dwarf2_objfile_data_key);
4569
4570 dwarf2_per_objfile->has_section_at_zero
4571 = dpo_backlink->has_section_at_zero;
4572 }
4573
4574 dwarf2_per_objfile->reading_partial_symbols = 0;
4575
4576 psymtab_to_symtab_1 (pst);
4577
4578 /* Finish up the debug error message. */
4579 if (info_verbose)
4580 printf_filtered (_("done.\n"));
4581 }
4582 }
4583 }
4584
4585 /* Add PER_CU to the queue. */
4586
4587 static void
4588 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4589 {
4590 struct dwarf2_queue_item *item;
4591
4592 per_cu->queued = 1;
4593 item = xmalloc (sizeof (*item));
4594 item->per_cu = per_cu;
4595 item->next = NULL;
4596
4597 if (dwarf2_queue == NULL)
4598 dwarf2_queue = item;
4599 else
4600 dwarf2_queue_tail->next = item;
4601
4602 dwarf2_queue_tail = item;
4603 }
4604
4605 /* Process the queue. */
4606
4607 static void
4608 process_queue (struct objfile *objfile)
4609 {
4610 struct dwarf2_queue_item *item, *next_item;
4611
4612 /* The queue starts out with one item, but following a DIE reference
4613 may load a new CU, adding it to the end of the queue. */
4614 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4615 {
4616 if (dwarf2_per_objfile->using_index
4617 ? !item->per_cu->v.quick->symtab
4618 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4619 process_full_comp_unit (item->per_cu);
4620
4621 item->per_cu->queued = 0;
4622 next_item = item->next;
4623 xfree (item);
4624 }
4625
4626 dwarf2_queue_tail = NULL;
4627 }
4628
4629 /* Free all allocated queue entries. This function only releases anything if
4630 an error was thrown; if the queue was processed then it would have been
4631 freed as we went along. */
4632
4633 static void
4634 dwarf2_release_queue (void *dummy)
4635 {
4636 struct dwarf2_queue_item *item, *last;
4637
4638 item = dwarf2_queue;
4639 while (item)
4640 {
4641 /* Anything still marked queued is likely to be in an
4642 inconsistent state, so discard it. */
4643 if (item->per_cu->queued)
4644 {
4645 if (item->per_cu->cu != NULL)
4646 free_one_cached_comp_unit (item->per_cu->cu);
4647 item->per_cu->queued = 0;
4648 }
4649
4650 last = item;
4651 item = item->next;
4652 xfree (last);
4653 }
4654
4655 dwarf2_queue = dwarf2_queue_tail = NULL;
4656 }
4657
4658 /* Read in full symbols for PST, and anything it depends on. */
4659
4660 static void
4661 psymtab_to_symtab_1 (struct partial_symtab *pst)
4662 {
4663 struct dwarf2_per_cu_data *per_cu;
4664 struct cleanup *back_to;
4665 int i;
4666
4667 for (i = 0; i < pst->number_of_dependencies; i++)
4668 if (!pst->dependencies[i]->readin)
4669 {
4670 /* Inform about additional files that need to be read in. */
4671 if (info_verbose)
4672 {
4673 /* FIXME: i18n: Need to make this a single string. */
4674 fputs_filtered (" ", gdb_stdout);
4675 wrap_here ("");
4676 fputs_filtered ("and ", gdb_stdout);
4677 wrap_here ("");
4678 printf_filtered ("%s...", pst->dependencies[i]->filename);
4679 wrap_here (""); /* Flush output. */
4680 gdb_flush (gdb_stdout);
4681 }
4682 psymtab_to_symtab_1 (pst->dependencies[i]);
4683 }
4684
4685 per_cu = pst->read_symtab_private;
4686
4687 if (per_cu == NULL)
4688 {
4689 /* It's an include file, no symbols to read for it.
4690 Everything is in the parent symtab. */
4691 pst->readin = 1;
4692 return;
4693 }
4694
4695 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4696 }
4697
4698 /* Load the DIEs associated with PER_CU into memory. */
4699
4700 static void
4701 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4702 struct objfile *objfile)
4703 {
4704 bfd *abfd = objfile->obfd;
4705 struct dwarf2_cu *cu;
4706 unsigned int offset;
4707 gdb_byte *info_ptr, *beg_of_comp_unit;
4708 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4709 struct attribute *attr;
4710 int read_cu = 0;
4711
4712 gdb_assert (! per_cu->debug_types_section);
4713
4714 /* Set local variables from the partial symbol table info. */
4715 offset = per_cu->offset;
4716
4717 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4718 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4719 beg_of_comp_unit = info_ptr;
4720
4721 if (per_cu->cu == NULL)
4722 {
4723 cu = xmalloc (sizeof (*cu));
4724 init_one_comp_unit (cu, objfile);
4725
4726 read_cu = 1;
4727
4728 /* If an error occurs while loading, release our storage. */
4729 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4730
4731 /* Read in the comp_unit header. */
4732 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4733
4734 /* Skip dummy compilation units. */
4735 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4736 + dwarf2_per_objfile->info.size)
4737 || peek_abbrev_code (abfd, info_ptr) == 0)
4738 {
4739 do_cleanups (free_cu_cleanup);
4740 return;
4741 }
4742
4743 /* Complete the cu_header. */
4744 cu->header.offset = offset;
4745 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4746
4747 /* Read the abbrevs for this compilation unit. */
4748 dwarf2_read_abbrevs (abfd, cu);
4749 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4750
4751 /* Link this compilation unit into the compilation unit tree. */
4752 per_cu->cu = cu;
4753 cu->per_cu = per_cu;
4754
4755 /* Link this CU into read_in_chain. */
4756 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4757 dwarf2_per_objfile->read_in_chain = per_cu;
4758 }
4759 else
4760 {
4761 cu = per_cu->cu;
4762 info_ptr += cu->header.first_die_offset;
4763 }
4764
4765 cu->dies = read_comp_unit (info_ptr, cu);
4766
4767 /* We try not to read any attributes in this function, because not
4768 all objfiles needed for references have been loaded yet, and symbol
4769 table processing isn't initialized. But we have to set the CU language,
4770 or we won't be able to build types correctly. */
4771 prepare_one_comp_unit (cu, cu->dies);
4772
4773 /* Similarly, if we do not read the producer, we can not apply
4774 producer-specific interpretation. */
4775 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4776 if (attr)
4777 cu->producer = DW_STRING (attr);
4778
4779 if (read_cu)
4780 {
4781 do_cleanups (free_abbrevs_cleanup);
4782
4783 /* We've successfully allocated this compilation unit. Let our
4784 caller clean it up when finished with it. */
4785 discard_cleanups (free_cu_cleanup);
4786 }
4787 }
4788
4789 /* Add a DIE to the delayed physname list. */
4790
4791 static void
4792 add_to_method_list (struct type *type, int fnfield_index, int index,
4793 const char *name, struct die_info *die,
4794 struct dwarf2_cu *cu)
4795 {
4796 struct delayed_method_info mi;
4797 mi.type = type;
4798 mi.fnfield_index = fnfield_index;
4799 mi.index = index;
4800 mi.name = name;
4801 mi.die = die;
4802 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4803 }
4804
4805 /* A cleanup for freeing the delayed method list. */
4806
4807 static void
4808 free_delayed_list (void *ptr)
4809 {
4810 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4811 if (cu->method_list != NULL)
4812 {
4813 VEC_free (delayed_method_info, cu->method_list);
4814 cu->method_list = NULL;
4815 }
4816 }
4817
4818 /* Compute the physnames of any methods on the CU's method list.
4819
4820 The computation of method physnames is delayed in order to avoid the
4821 (bad) condition that one of the method's formal parameters is of an as yet
4822 incomplete type. */
4823
4824 static void
4825 compute_delayed_physnames (struct dwarf2_cu *cu)
4826 {
4827 int i;
4828 struct delayed_method_info *mi;
4829 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4830 {
4831 const char *physname;
4832 struct fn_fieldlist *fn_flp
4833 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4834 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4835 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4836 }
4837 }
4838
4839 /* Generate full symbol information for PST and CU, whose DIEs have
4840 already been loaded into memory. */
4841
4842 static void
4843 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4844 {
4845 struct dwarf2_cu *cu = per_cu->cu;
4846 struct objfile *objfile = per_cu->objfile;
4847 CORE_ADDR lowpc, highpc;
4848 struct symtab *symtab;
4849 struct cleanup *back_to, *delayed_list_cleanup;
4850 CORE_ADDR baseaddr;
4851
4852 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4853
4854 buildsym_init ();
4855 back_to = make_cleanup (really_free_pendings, NULL);
4856 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4857
4858 cu->list_in_scope = &file_symbols;
4859
4860 /* Do line number decoding in read_file_scope () */
4861 process_die (cu->dies, cu);
4862
4863 /* Now that we have processed all the DIEs in the CU, all the types
4864 should be complete, and it should now be safe to compute all of the
4865 physnames. */
4866 compute_delayed_physnames (cu);
4867 do_cleanups (delayed_list_cleanup);
4868
4869 /* Some compilers don't define a DW_AT_high_pc attribute for the
4870 compilation unit. If the DW_AT_high_pc is missing, synthesize
4871 it, by scanning the DIE's below the compilation unit. */
4872 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4873
4874 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4875
4876 if (symtab != NULL)
4877 {
4878 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4879
4880 /* Set symtab language to language from DW_AT_language. If the
4881 compilation is from a C file generated by language preprocessors, do
4882 not set the language if it was already deduced by start_subfile. */
4883 if (!(cu->language == language_c && symtab->language != language_c))
4884 symtab->language = cu->language;
4885
4886 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4887 produce DW_AT_location with location lists but it can be possibly
4888 invalid without -fvar-tracking.
4889
4890 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4891 needed, it would be wrong due to missing DW_AT_producer there.
4892
4893 Still one can confuse GDB by using non-standard GCC compilation
4894 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4895 */
4896 if (cu->has_loclist && gcc_4_minor >= 0)
4897 symtab->locations_valid = 1;
4898
4899 if (gcc_4_minor >= 5)
4900 symtab->epilogue_unwind_valid = 1;
4901
4902 symtab->call_site_htab = cu->call_site_htab;
4903 }
4904
4905 if (dwarf2_per_objfile->using_index)
4906 per_cu->v.quick->symtab = symtab;
4907 else
4908 {
4909 struct partial_symtab *pst = per_cu->v.psymtab;
4910 pst->symtab = symtab;
4911 pst->readin = 1;
4912 }
4913
4914 do_cleanups (back_to);
4915 }
4916
4917 /* Process a die and its children. */
4918
4919 static void
4920 process_die (struct die_info *die, struct dwarf2_cu *cu)
4921 {
4922 switch (die->tag)
4923 {
4924 case DW_TAG_padding:
4925 break;
4926 case DW_TAG_compile_unit:
4927 read_file_scope (die, cu);
4928 break;
4929 case DW_TAG_type_unit:
4930 read_type_unit_scope (die, cu);
4931 break;
4932 case DW_TAG_subprogram:
4933 case DW_TAG_inlined_subroutine:
4934 read_func_scope (die, cu);
4935 break;
4936 case DW_TAG_lexical_block:
4937 case DW_TAG_try_block:
4938 case DW_TAG_catch_block:
4939 read_lexical_block_scope (die, cu);
4940 break;
4941 case DW_TAG_GNU_call_site:
4942 read_call_site_scope (die, cu);
4943 break;
4944 case DW_TAG_class_type:
4945 case DW_TAG_interface_type:
4946 case DW_TAG_structure_type:
4947 case DW_TAG_union_type:
4948 process_structure_scope (die, cu);
4949 break;
4950 case DW_TAG_enumeration_type:
4951 process_enumeration_scope (die, cu);
4952 break;
4953
4954 /* These dies have a type, but processing them does not create
4955 a symbol or recurse to process the children. Therefore we can
4956 read them on-demand through read_type_die. */
4957 case DW_TAG_subroutine_type:
4958 case DW_TAG_set_type:
4959 case DW_TAG_array_type:
4960 case DW_TAG_pointer_type:
4961 case DW_TAG_ptr_to_member_type:
4962 case DW_TAG_reference_type:
4963 case DW_TAG_string_type:
4964 break;
4965
4966 case DW_TAG_base_type:
4967 case DW_TAG_subrange_type:
4968 case DW_TAG_typedef:
4969 /* Add a typedef symbol for the type definition, if it has a
4970 DW_AT_name. */
4971 new_symbol (die, read_type_die (die, cu), cu);
4972 break;
4973 case DW_TAG_common_block:
4974 read_common_block (die, cu);
4975 break;
4976 case DW_TAG_common_inclusion:
4977 break;
4978 case DW_TAG_namespace:
4979 processing_has_namespace_info = 1;
4980 read_namespace (die, cu);
4981 break;
4982 case DW_TAG_module:
4983 processing_has_namespace_info = 1;
4984 read_module (die, cu);
4985 break;
4986 case DW_TAG_imported_declaration:
4987 case DW_TAG_imported_module:
4988 processing_has_namespace_info = 1;
4989 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4990 || cu->language != language_fortran))
4991 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4992 dwarf_tag_name (die->tag));
4993 read_import_statement (die, cu);
4994 break;
4995 default:
4996 new_symbol (die, NULL, cu);
4997 break;
4998 }
4999 }
5000
5001 /* A helper function for dwarf2_compute_name which determines whether DIE
5002 needs to have the name of the scope prepended to the name listed in the
5003 die. */
5004
5005 static int
5006 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5007 {
5008 struct attribute *attr;
5009
5010 switch (die->tag)
5011 {
5012 case DW_TAG_namespace:
5013 case DW_TAG_typedef:
5014 case DW_TAG_class_type:
5015 case DW_TAG_interface_type:
5016 case DW_TAG_structure_type:
5017 case DW_TAG_union_type:
5018 case DW_TAG_enumeration_type:
5019 case DW_TAG_enumerator:
5020 case DW_TAG_subprogram:
5021 case DW_TAG_member:
5022 return 1;
5023
5024 case DW_TAG_variable:
5025 case DW_TAG_constant:
5026 /* We only need to prefix "globally" visible variables. These include
5027 any variable marked with DW_AT_external or any variable that
5028 lives in a namespace. [Variables in anonymous namespaces
5029 require prefixing, but they are not DW_AT_external.] */
5030
5031 if (dwarf2_attr (die, DW_AT_specification, cu))
5032 {
5033 struct dwarf2_cu *spec_cu = cu;
5034
5035 return die_needs_namespace (die_specification (die, &spec_cu),
5036 spec_cu);
5037 }
5038
5039 attr = dwarf2_attr (die, DW_AT_external, cu);
5040 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5041 && die->parent->tag != DW_TAG_module)
5042 return 0;
5043 /* A variable in a lexical block of some kind does not need a
5044 namespace, even though in C++ such variables may be external
5045 and have a mangled name. */
5046 if (die->parent->tag == DW_TAG_lexical_block
5047 || die->parent->tag == DW_TAG_try_block
5048 || die->parent->tag == DW_TAG_catch_block
5049 || die->parent->tag == DW_TAG_subprogram)
5050 return 0;
5051 return 1;
5052
5053 default:
5054 return 0;
5055 }
5056 }
5057
5058 /* Retrieve the last character from a mem_file. */
5059
5060 static void
5061 do_ui_file_peek_last (void *object, const char *buffer, long length)
5062 {
5063 char *last_char_p = (char *) object;
5064
5065 if (length > 0)
5066 *last_char_p = buffer[length - 1];
5067 }
5068
5069 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5070 compute the physname for the object, which include a method's
5071 formal parameters (C++/Java) and return type (Java).
5072
5073 For Ada, return the DIE's linkage name rather than the fully qualified
5074 name. PHYSNAME is ignored..
5075
5076 The result is allocated on the objfile_obstack and canonicalized. */
5077
5078 static const char *
5079 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5080 int physname)
5081 {
5082 if (name == NULL)
5083 name = dwarf2_name (die, cu);
5084
5085 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5086 compute it by typename_concat inside GDB. */
5087 if (cu->language == language_ada
5088 || (cu->language == language_fortran && physname))
5089 {
5090 /* For Ada unit, we prefer the linkage name over the name, as
5091 the former contains the exported name, which the user expects
5092 to be able to reference. Ideally, we want the user to be able
5093 to reference this entity using either natural or linkage name,
5094 but we haven't started looking at this enhancement yet. */
5095 struct attribute *attr;
5096
5097 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5098 if (attr == NULL)
5099 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5100 if (attr && DW_STRING (attr))
5101 return DW_STRING (attr);
5102 }
5103
5104 /* These are the only languages we know how to qualify names in. */
5105 if (name != NULL
5106 && (cu->language == language_cplus || cu->language == language_java
5107 || cu->language == language_fortran))
5108 {
5109 if (die_needs_namespace (die, cu))
5110 {
5111 long length;
5112 char *prefix;
5113 struct ui_file *buf;
5114
5115 prefix = determine_prefix (die, cu);
5116 buf = mem_fileopen ();
5117 if (*prefix != '\0')
5118 {
5119 char *prefixed_name = typename_concat (NULL, prefix, name,
5120 physname, cu);
5121
5122 fputs_unfiltered (prefixed_name, buf);
5123 xfree (prefixed_name);
5124 }
5125 else
5126 fputs_unfiltered (name, buf);
5127
5128 /* Template parameters may be specified in the DIE's DW_AT_name, or
5129 as children with DW_TAG_template_type_param or
5130 DW_TAG_value_type_param. If the latter, add them to the name
5131 here. If the name already has template parameters, then
5132 skip this step; some versions of GCC emit both, and
5133 it is more efficient to use the pre-computed name.
5134
5135 Something to keep in mind about this process: it is very
5136 unlikely, or in some cases downright impossible, to produce
5137 something that will match the mangled name of a function.
5138 If the definition of the function has the same debug info,
5139 we should be able to match up with it anyway. But fallbacks
5140 using the minimal symbol, for instance to find a method
5141 implemented in a stripped copy of libstdc++, will not work.
5142 If we do not have debug info for the definition, we will have to
5143 match them up some other way.
5144
5145 When we do name matching there is a related problem with function
5146 templates; two instantiated function templates are allowed to
5147 differ only by their return types, which we do not add here. */
5148
5149 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5150 {
5151 struct attribute *attr;
5152 struct die_info *child;
5153 int first = 1;
5154
5155 die->building_fullname = 1;
5156
5157 for (child = die->child; child != NULL; child = child->sibling)
5158 {
5159 struct type *type;
5160 long value;
5161 gdb_byte *bytes;
5162 struct dwarf2_locexpr_baton *baton;
5163 struct value *v;
5164
5165 if (child->tag != DW_TAG_template_type_param
5166 && child->tag != DW_TAG_template_value_param)
5167 continue;
5168
5169 if (first)
5170 {
5171 fputs_unfiltered ("<", buf);
5172 first = 0;
5173 }
5174 else
5175 fputs_unfiltered (", ", buf);
5176
5177 attr = dwarf2_attr (child, DW_AT_type, cu);
5178 if (attr == NULL)
5179 {
5180 complaint (&symfile_complaints,
5181 _("template parameter missing DW_AT_type"));
5182 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5183 continue;
5184 }
5185 type = die_type (child, cu);
5186
5187 if (child->tag == DW_TAG_template_type_param)
5188 {
5189 c_print_type (type, "", buf, -1, 0);
5190 continue;
5191 }
5192
5193 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5194 if (attr == NULL)
5195 {
5196 complaint (&symfile_complaints,
5197 _("template parameter missing "
5198 "DW_AT_const_value"));
5199 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5200 continue;
5201 }
5202
5203 dwarf2_const_value_attr (attr, type, name,
5204 &cu->comp_unit_obstack, cu,
5205 &value, &bytes, &baton);
5206
5207 if (TYPE_NOSIGN (type))
5208 /* GDB prints characters as NUMBER 'CHAR'. If that's
5209 changed, this can use value_print instead. */
5210 c_printchar (value, type, buf);
5211 else
5212 {
5213 struct value_print_options opts;
5214
5215 if (baton != NULL)
5216 v = dwarf2_evaluate_loc_desc (type, NULL,
5217 baton->data,
5218 baton->size,
5219 baton->per_cu);
5220 else if (bytes != NULL)
5221 {
5222 v = allocate_value (type);
5223 memcpy (value_contents_writeable (v), bytes,
5224 TYPE_LENGTH (type));
5225 }
5226 else
5227 v = value_from_longest (type, value);
5228
5229 /* Specify decimal so that we do not depend on
5230 the radix. */
5231 get_formatted_print_options (&opts, 'd');
5232 opts.raw = 1;
5233 value_print (v, buf, &opts);
5234 release_value (v);
5235 value_free (v);
5236 }
5237 }
5238
5239 die->building_fullname = 0;
5240
5241 if (!first)
5242 {
5243 /* Close the argument list, with a space if necessary
5244 (nested templates). */
5245 char last_char = '\0';
5246 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5247 if (last_char == '>')
5248 fputs_unfiltered (" >", buf);
5249 else
5250 fputs_unfiltered (">", buf);
5251 }
5252 }
5253
5254 /* For Java and C++ methods, append formal parameter type
5255 information, if PHYSNAME. */
5256
5257 if (physname && die->tag == DW_TAG_subprogram
5258 && (cu->language == language_cplus
5259 || cu->language == language_java))
5260 {
5261 struct type *type = read_type_die (die, cu);
5262
5263 c_type_print_args (type, buf, 1, cu->language);
5264
5265 if (cu->language == language_java)
5266 {
5267 /* For java, we must append the return type to method
5268 names. */
5269 if (die->tag == DW_TAG_subprogram)
5270 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5271 0, 0);
5272 }
5273 else if (cu->language == language_cplus)
5274 {
5275 /* Assume that an artificial first parameter is
5276 "this", but do not crash if it is not. RealView
5277 marks unnamed (and thus unused) parameters as
5278 artificial; there is no way to differentiate
5279 the two cases. */
5280 if (TYPE_NFIELDS (type) > 0
5281 && TYPE_FIELD_ARTIFICIAL (type, 0)
5282 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5283 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5284 0))))
5285 fputs_unfiltered (" const", buf);
5286 }
5287 }
5288
5289 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5290 &length);
5291 ui_file_delete (buf);
5292
5293 if (cu->language == language_cplus)
5294 {
5295 char *cname
5296 = dwarf2_canonicalize_name (name, cu,
5297 &cu->objfile->objfile_obstack);
5298
5299 if (cname != NULL)
5300 name = cname;
5301 }
5302 }
5303 }
5304
5305 return name;
5306 }
5307
5308 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5309 If scope qualifiers are appropriate they will be added. The result
5310 will be allocated on the objfile_obstack, or NULL if the DIE does
5311 not have a name. NAME may either be from a previous call to
5312 dwarf2_name or NULL.
5313
5314 The output string will be canonicalized (if C++/Java). */
5315
5316 static const char *
5317 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5318 {
5319 return dwarf2_compute_name (name, die, cu, 0);
5320 }
5321
5322 /* Construct a physname for the given DIE in CU. NAME may either be
5323 from a previous call to dwarf2_name or NULL. The result will be
5324 allocated on the objfile_objstack or NULL if the DIE does not have a
5325 name.
5326
5327 The output string will be canonicalized (if C++/Java). */
5328
5329 static const char *
5330 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5331 {
5332 struct attribute *attr;
5333 const char *retval, *mangled = NULL, *canon = NULL;
5334 struct cleanup *back_to;
5335 int need_copy = 1;
5336
5337 /* In this case dwarf2_compute_name is just a shortcut not building anything
5338 on its own. */
5339 if (!die_needs_namespace (die, cu))
5340 return dwarf2_compute_name (name, die, cu, 1);
5341
5342 back_to = make_cleanup (null_cleanup, NULL);
5343
5344 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5345 if (!attr)
5346 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5347
5348 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5349 has computed. */
5350 if (attr && DW_STRING (attr))
5351 {
5352 char *demangled;
5353
5354 mangled = DW_STRING (attr);
5355
5356 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5357 type. It is easier for GDB users to search for such functions as
5358 `name(params)' than `long name(params)'. In such case the minimal
5359 symbol names do not match the full symbol names but for template
5360 functions there is never a need to look up their definition from their
5361 declaration so the only disadvantage remains the minimal symbol
5362 variant `long name(params)' does not have the proper inferior type.
5363 */
5364
5365 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5366 | (cu->language == language_java
5367 ? DMGL_JAVA | DMGL_RET_POSTFIX
5368 : DMGL_RET_DROP)));
5369 if (demangled)
5370 {
5371 make_cleanup (xfree, demangled);
5372 canon = demangled;
5373 }
5374 else
5375 {
5376 canon = mangled;
5377 need_copy = 0;
5378 }
5379 }
5380
5381 if (canon == NULL || check_physname)
5382 {
5383 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5384
5385 if (canon != NULL && strcmp (physname, canon) != 0)
5386 {
5387 /* It may not mean a bug in GDB. The compiler could also
5388 compute DW_AT_linkage_name incorrectly. But in such case
5389 GDB would need to be bug-to-bug compatible. */
5390
5391 complaint (&symfile_complaints,
5392 _("Computed physname <%s> does not match demangled <%s> "
5393 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5394 physname, canon, mangled, die->offset, cu->objfile->name);
5395
5396 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5397 is available here - over computed PHYSNAME. It is safer
5398 against both buggy GDB and buggy compilers. */
5399
5400 retval = canon;
5401 }
5402 else
5403 {
5404 retval = physname;
5405 need_copy = 0;
5406 }
5407 }
5408 else
5409 retval = canon;
5410
5411 if (need_copy)
5412 retval = obsavestring (retval, strlen (retval),
5413 &cu->objfile->objfile_obstack);
5414
5415 do_cleanups (back_to);
5416 return retval;
5417 }
5418
5419 /* Read the import statement specified by the given die and record it. */
5420
5421 static void
5422 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5423 {
5424 struct attribute *import_attr;
5425 struct die_info *imported_die, *child_die;
5426 struct dwarf2_cu *imported_cu;
5427 const char *imported_name;
5428 const char *imported_name_prefix;
5429 const char *canonical_name;
5430 const char *import_alias;
5431 const char *imported_declaration = NULL;
5432 const char *import_prefix;
5433 VEC (const_char_ptr) *excludes = NULL;
5434 struct cleanup *cleanups;
5435
5436 char *temp;
5437
5438 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5439 if (import_attr == NULL)
5440 {
5441 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5442 dwarf_tag_name (die->tag));
5443 return;
5444 }
5445
5446 imported_cu = cu;
5447 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5448 imported_name = dwarf2_name (imported_die, imported_cu);
5449 if (imported_name == NULL)
5450 {
5451 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5452
5453 The import in the following code:
5454 namespace A
5455 {
5456 typedef int B;
5457 }
5458
5459 int main ()
5460 {
5461 using A::B;
5462 B b;
5463 return b;
5464 }
5465
5466 ...
5467 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5468 <52> DW_AT_decl_file : 1
5469 <53> DW_AT_decl_line : 6
5470 <54> DW_AT_import : <0x75>
5471 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5472 <59> DW_AT_name : B
5473 <5b> DW_AT_decl_file : 1
5474 <5c> DW_AT_decl_line : 2
5475 <5d> DW_AT_type : <0x6e>
5476 ...
5477 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5478 <76> DW_AT_byte_size : 4
5479 <77> DW_AT_encoding : 5 (signed)
5480
5481 imports the wrong die ( 0x75 instead of 0x58 ).
5482 This case will be ignored until the gcc bug is fixed. */
5483 return;
5484 }
5485
5486 /* Figure out the local name after import. */
5487 import_alias = dwarf2_name (die, cu);
5488
5489 /* Figure out where the statement is being imported to. */
5490 import_prefix = determine_prefix (die, cu);
5491
5492 /* Figure out what the scope of the imported die is and prepend it
5493 to the name of the imported die. */
5494 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5495
5496 if (imported_die->tag != DW_TAG_namespace
5497 && imported_die->tag != DW_TAG_module)
5498 {
5499 imported_declaration = imported_name;
5500 canonical_name = imported_name_prefix;
5501 }
5502 else if (strlen (imported_name_prefix) > 0)
5503 {
5504 temp = alloca (strlen (imported_name_prefix)
5505 + 2 + strlen (imported_name) + 1);
5506 strcpy (temp, imported_name_prefix);
5507 strcat (temp, "::");
5508 strcat (temp, imported_name);
5509 canonical_name = temp;
5510 }
5511 else
5512 canonical_name = imported_name;
5513
5514 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5515
5516 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5517 for (child_die = die->child; child_die && child_die->tag;
5518 child_die = sibling_die (child_die))
5519 {
5520 /* DWARF-4: A Fortran use statement with a “rename list” may be
5521 represented by an imported module entry with an import attribute
5522 referring to the module and owned entries corresponding to those
5523 entities that are renamed as part of being imported. */
5524
5525 if (child_die->tag != DW_TAG_imported_declaration)
5526 {
5527 complaint (&symfile_complaints,
5528 _("child DW_TAG_imported_declaration expected "
5529 "- DIE at 0x%x [in module %s]"),
5530 child_die->offset, cu->objfile->name);
5531 continue;
5532 }
5533
5534 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5535 if (import_attr == NULL)
5536 {
5537 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5538 dwarf_tag_name (child_die->tag));
5539 continue;
5540 }
5541
5542 imported_cu = cu;
5543 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5544 &imported_cu);
5545 imported_name = dwarf2_name (imported_die, imported_cu);
5546 if (imported_name == NULL)
5547 {
5548 complaint (&symfile_complaints,
5549 _("child DW_TAG_imported_declaration has unknown "
5550 "imported name - DIE at 0x%x [in module %s]"),
5551 child_die->offset, cu->objfile->name);
5552 continue;
5553 }
5554
5555 VEC_safe_push (const_char_ptr, excludes, imported_name);
5556
5557 process_die (child_die, cu);
5558 }
5559
5560 cp_add_using_directive (import_prefix,
5561 canonical_name,
5562 import_alias,
5563 imported_declaration,
5564 excludes,
5565 &cu->objfile->objfile_obstack);
5566
5567 do_cleanups (cleanups);
5568 }
5569
5570 /* Cleanup function for read_file_scope. */
5571
5572 static void
5573 free_cu_line_header (void *arg)
5574 {
5575 struct dwarf2_cu *cu = arg;
5576
5577 free_line_header (cu->line_header);
5578 cu->line_header = NULL;
5579 }
5580
5581 static void
5582 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5583 char **name, char **comp_dir)
5584 {
5585 struct attribute *attr;
5586
5587 *name = NULL;
5588 *comp_dir = NULL;
5589
5590 /* Find the filename. Do not use dwarf2_name here, since the filename
5591 is not a source language identifier. */
5592 attr = dwarf2_attr (die, DW_AT_name, cu);
5593 if (attr)
5594 {
5595 *name = DW_STRING (attr);
5596 }
5597
5598 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5599 if (attr)
5600 *comp_dir = DW_STRING (attr);
5601 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5602 {
5603 *comp_dir = ldirname (*name);
5604 if (*comp_dir != NULL)
5605 make_cleanup (xfree, *comp_dir);
5606 }
5607 if (*comp_dir != NULL)
5608 {
5609 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5610 directory, get rid of it. */
5611 char *cp = strchr (*comp_dir, ':');
5612
5613 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5614 *comp_dir = cp + 1;
5615 }
5616
5617 if (*name == NULL)
5618 *name = "<unknown>";
5619 }
5620
5621 /* Handle DW_AT_stmt_list for a compilation unit. */
5622
5623 static void
5624 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5625 const char *comp_dir)
5626 {
5627 struct attribute *attr;
5628 struct objfile *objfile = cu->objfile;
5629 bfd *abfd = objfile->obfd;
5630
5631 /* Decode line number information if present. We do this before
5632 processing child DIEs, so that the line header table is available
5633 for DW_AT_decl_file. */
5634 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5635 if (attr)
5636 {
5637 unsigned int line_offset = DW_UNSND (attr);
5638 struct line_header *line_header
5639 = dwarf_decode_line_header (line_offset, abfd, cu);
5640
5641 if (line_header)
5642 {
5643 cu->line_header = line_header;
5644 make_cleanup (free_cu_line_header, cu);
5645 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5646 }
5647 }
5648 }
5649
5650 /* Process DW_TAG_compile_unit. */
5651
5652 static void
5653 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5654 {
5655 struct objfile *objfile = cu->objfile;
5656 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5657 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5658 CORE_ADDR highpc = ((CORE_ADDR) 0);
5659 struct attribute *attr;
5660 char *name = NULL;
5661 char *comp_dir = NULL;
5662 struct die_info *child_die;
5663 bfd *abfd = objfile->obfd;
5664 CORE_ADDR baseaddr;
5665
5666 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5667
5668 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5669
5670 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5671 from finish_block. */
5672 if (lowpc == ((CORE_ADDR) -1))
5673 lowpc = highpc;
5674 lowpc += baseaddr;
5675 highpc += baseaddr;
5676
5677 find_file_and_directory (die, cu, &name, &comp_dir);
5678
5679 attr = dwarf2_attr (die, DW_AT_language, cu);
5680 if (attr)
5681 {
5682 set_cu_language (DW_UNSND (attr), cu);
5683 }
5684
5685 attr = dwarf2_attr (die, DW_AT_producer, cu);
5686 if (attr)
5687 cu->producer = DW_STRING (attr);
5688
5689 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5690 standardised yet. As a workaround for the language detection we fall
5691 back to the DW_AT_producer string. */
5692 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5693 cu->language = language_opencl;
5694
5695 /* We assume that we're processing GCC output. */
5696 processing_gcc_compilation = 2;
5697
5698 processing_has_namespace_info = 0;
5699
5700 start_symtab (name, comp_dir, lowpc);
5701 record_debugformat ("DWARF 2");
5702 record_producer (cu->producer);
5703
5704 handle_DW_AT_stmt_list (die, cu, comp_dir);
5705
5706 /* Process all dies in compilation unit. */
5707 if (die->child != NULL)
5708 {
5709 child_die = die->child;
5710 while (child_die && child_die->tag)
5711 {
5712 process_die (child_die, cu);
5713 child_die = sibling_die (child_die);
5714 }
5715 }
5716
5717 /* Decode macro information, if present. Dwarf 2 macro information
5718 refers to information in the line number info statement program
5719 header, so we can only read it if we've read the header
5720 successfully. */
5721 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5722 if (attr && cu->line_header)
5723 {
5724 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5725 complaint (&symfile_complaints,
5726 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5727
5728 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5729 comp_dir, abfd, cu,
5730 &dwarf2_per_objfile->macro, 1);
5731 }
5732 else
5733 {
5734 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5735 if (attr && cu->line_header)
5736 {
5737 unsigned int macro_offset = DW_UNSND (attr);
5738
5739 dwarf_decode_macros (cu->line_header, macro_offset,
5740 comp_dir, abfd, cu,
5741 &dwarf2_per_objfile->macinfo, 0);
5742 }
5743 }
5744 do_cleanups (back_to);
5745 }
5746
5747 /* Process DW_TAG_type_unit.
5748 For TUs we want to skip the first top level sibling if it's not the
5749 actual type being defined by this TU. In this case the first top
5750 level sibling is there to provide context only. */
5751
5752 static void
5753 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5754 {
5755 struct objfile *objfile = cu->objfile;
5756 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5757 CORE_ADDR lowpc;
5758 struct attribute *attr;
5759 char *name = NULL;
5760 char *comp_dir = NULL;
5761 struct die_info *child_die;
5762 bfd *abfd = objfile->obfd;
5763
5764 /* start_symtab needs a low pc, but we don't really have one.
5765 Do what read_file_scope would do in the absence of such info. */
5766 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5767
5768 /* Find the filename. Do not use dwarf2_name here, since the filename
5769 is not a source language identifier. */
5770 attr = dwarf2_attr (die, DW_AT_name, cu);
5771 if (attr)
5772 name = DW_STRING (attr);
5773
5774 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5775 if (attr)
5776 comp_dir = DW_STRING (attr);
5777 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5778 {
5779 comp_dir = ldirname (name);
5780 if (comp_dir != NULL)
5781 make_cleanup (xfree, comp_dir);
5782 }
5783
5784 if (name == NULL)
5785 name = "<unknown>";
5786
5787 attr = dwarf2_attr (die, DW_AT_language, cu);
5788 if (attr)
5789 set_cu_language (DW_UNSND (attr), cu);
5790
5791 /* This isn't technically needed today. It is done for symmetry
5792 with read_file_scope. */
5793 attr = dwarf2_attr (die, DW_AT_producer, cu);
5794 if (attr)
5795 cu->producer = DW_STRING (attr);
5796
5797 /* We assume that we're processing GCC output. */
5798 processing_gcc_compilation = 2;
5799
5800 processing_has_namespace_info = 0;
5801
5802 start_symtab (name, comp_dir, lowpc);
5803 record_debugformat ("DWARF 2");
5804 record_producer (cu->producer);
5805
5806 handle_DW_AT_stmt_list (die, cu, comp_dir);
5807
5808 /* Process the dies in the type unit. */
5809 if (die->child == NULL)
5810 {
5811 dump_die_for_error (die);
5812 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5813 bfd_get_filename (abfd));
5814 }
5815
5816 child_die = die->child;
5817
5818 while (child_die && child_die->tag)
5819 {
5820 process_die (child_die, cu);
5821
5822 child_die = sibling_die (child_die);
5823 }
5824
5825 do_cleanups (back_to);
5826 }
5827
5828 /* qsort helper for inherit_abstract_dies. */
5829
5830 static int
5831 unsigned_int_compar (const void *ap, const void *bp)
5832 {
5833 unsigned int a = *(unsigned int *) ap;
5834 unsigned int b = *(unsigned int *) bp;
5835
5836 return (a > b) - (b > a);
5837 }
5838
5839 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5840 Inherit only the children of the DW_AT_abstract_origin DIE not being
5841 already referenced by DW_AT_abstract_origin from the children of the
5842 current DIE. */
5843
5844 static void
5845 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5846 {
5847 struct die_info *child_die;
5848 unsigned die_children_count;
5849 /* CU offsets which were referenced by children of the current DIE. */
5850 unsigned *offsets;
5851 unsigned *offsets_end, *offsetp;
5852 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5853 struct die_info *origin_die;
5854 /* Iterator of the ORIGIN_DIE children. */
5855 struct die_info *origin_child_die;
5856 struct cleanup *cleanups;
5857 struct attribute *attr;
5858 struct dwarf2_cu *origin_cu;
5859 struct pending **origin_previous_list_in_scope;
5860
5861 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5862 if (!attr)
5863 return;
5864
5865 /* Note that following die references may follow to a die in a
5866 different cu. */
5867
5868 origin_cu = cu;
5869 origin_die = follow_die_ref (die, attr, &origin_cu);
5870
5871 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5872 symbols in. */
5873 origin_previous_list_in_scope = origin_cu->list_in_scope;
5874 origin_cu->list_in_scope = cu->list_in_scope;
5875
5876 if (die->tag != origin_die->tag
5877 && !(die->tag == DW_TAG_inlined_subroutine
5878 && origin_die->tag == DW_TAG_subprogram))
5879 complaint (&symfile_complaints,
5880 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5881 die->offset, origin_die->offset);
5882
5883 child_die = die->child;
5884 die_children_count = 0;
5885 while (child_die && child_die->tag)
5886 {
5887 child_die = sibling_die (child_die);
5888 die_children_count++;
5889 }
5890 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5891 cleanups = make_cleanup (xfree, offsets);
5892
5893 offsets_end = offsets;
5894 child_die = die->child;
5895 while (child_die && child_die->tag)
5896 {
5897 /* For each CHILD_DIE, find the corresponding child of
5898 ORIGIN_DIE. If there is more than one layer of
5899 DW_AT_abstract_origin, follow them all; there shouldn't be,
5900 but GCC versions at least through 4.4 generate this (GCC PR
5901 40573). */
5902 struct die_info *child_origin_die = child_die;
5903 struct dwarf2_cu *child_origin_cu = cu;
5904
5905 while (1)
5906 {
5907 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5908 child_origin_cu);
5909 if (attr == NULL)
5910 break;
5911 child_origin_die = follow_die_ref (child_origin_die, attr,
5912 &child_origin_cu);
5913 }
5914
5915 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5916 counterpart may exist. */
5917 if (child_origin_die != child_die)
5918 {
5919 if (child_die->tag != child_origin_die->tag
5920 && !(child_die->tag == DW_TAG_inlined_subroutine
5921 && child_origin_die->tag == DW_TAG_subprogram))
5922 complaint (&symfile_complaints,
5923 _("Child DIE 0x%x and its abstract origin 0x%x have "
5924 "different tags"), child_die->offset,
5925 child_origin_die->offset);
5926 if (child_origin_die->parent != origin_die)
5927 complaint (&symfile_complaints,
5928 _("Child DIE 0x%x and its abstract origin 0x%x have "
5929 "different parents"), child_die->offset,
5930 child_origin_die->offset);
5931 else
5932 *offsets_end++ = child_origin_die->offset;
5933 }
5934 child_die = sibling_die (child_die);
5935 }
5936 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5937 unsigned_int_compar);
5938 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5939 if (offsetp[-1] == *offsetp)
5940 complaint (&symfile_complaints,
5941 _("Multiple children of DIE 0x%x refer "
5942 "to DIE 0x%x as their abstract origin"),
5943 die->offset, *offsetp);
5944
5945 offsetp = offsets;
5946 origin_child_die = origin_die->child;
5947 while (origin_child_die && origin_child_die->tag)
5948 {
5949 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5950 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5951 offsetp++;
5952 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5953 {
5954 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5955 process_die (origin_child_die, origin_cu);
5956 }
5957 origin_child_die = sibling_die (origin_child_die);
5958 }
5959 origin_cu->list_in_scope = origin_previous_list_in_scope;
5960
5961 do_cleanups (cleanups);
5962 }
5963
5964 static void
5965 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5966 {
5967 struct objfile *objfile = cu->objfile;
5968 struct context_stack *new;
5969 CORE_ADDR lowpc;
5970 CORE_ADDR highpc;
5971 struct die_info *child_die;
5972 struct attribute *attr, *call_line, *call_file;
5973 char *name;
5974 CORE_ADDR baseaddr;
5975 struct block *block;
5976 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5977 VEC (symbolp) *template_args = NULL;
5978 struct template_symbol *templ_func = NULL;
5979
5980 if (inlined_func)
5981 {
5982 /* If we do not have call site information, we can't show the
5983 caller of this inlined function. That's too confusing, so
5984 only use the scope for local variables. */
5985 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5986 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5987 if (call_line == NULL || call_file == NULL)
5988 {
5989 read_lexical_block_scope (die, cu);
5990 return;
5991 }
5992 }
5993
5994 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5995
5996 name = dwarf2_name (die, cu);
5997
5998 /* Ignore functions with missing or empty names. These are actually
5999 illegal according to the DWARF standard. */
6000 if (name == NULL)
6001 {
6002 complaint (&symfile_complaints,
6003 _("missing name for subprogram DIE at %d"), die->offset);
6004 return;
6005 }
6006
6007 /* Ignore functions with missing or invalid low and high pc attributes. */
6008 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6009 {
6010 attr = dwarf2_attr (die, DW_AT_external, cu);
6011 if (!attr || !DW_UNSND (attr))
6012 complaint (&symfile_complaints,
6013 _("cannot get low and high bounds "
6014 "for subprogram DIE at %d"),
6015 die->offset);
6016 return;
6017 }
6018
6019 lowpc += baseaddr;
6020 highpc += baseaddr;
6021
6022 /* If we have any template arguments, then we must allocate a
6023 different sort of symbol. */
6024 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6025 {
6026 if (child_die->tag == DW_TAG_template_type_param
6027 || child_die->tag == DW_TAG_template_value_param)
6028 {
6029 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6030 struct template_symbol);
6031 templ_func->base.is_cplus_template_function = 1;
6032 break;
6033 }
6034 }
6035
6036 new = push_context (0, lowpc);
6037 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6038 (struct symbol *) templ_func);
6039
6040 /* If there is a location expression for DW_AT_frame_base, record
6041 it. */
6042 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6043 if (attr)
6044 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6045 expression is being recorded directly in the function's symbol
6046 and not in a separate frame-base object. I guess this hack is
6047 to avoid adding some sort of frame-base adjunct/annex to the
6048 function's symbol :-(. The problem with doing this is that it
6049 results in a function symbol with a location expression that
6050 has nothing to do with the location of the function, ouch! The
6051 relationship should be: a function's symbol has-a frame base; a
6052 frame-base has-a location expression. */
6053 dwarf2_symbol_mark_computed (attr, new->name, cu);
6054
6055 cu->list_in_scope = &local_symbols;
6056
6057 if (die->child != NULL)
6058 {
6059 child_die = die->child;
6060 while (child_die && child_die->tag)
6061 {
6062 if (child_die->tag == DW_TAG_template_type_param
6063 || child_die->tag == DW_TAG_template_value_param)
6064 {
6065 struct symbol *arg = new_symbol (child_die, NULL, cu);
6066
6067 if (arg != NULL)
6068 VEC_safe_push (symbolp, template_args, arg);
6069 }
6070 else
6071 process_die (child_die, cu);
6072 child_die = sibling_die (child_die);
6073 }
6074 }
6075
6076 inherit_abstract_dies (die, cu);
6077
6078 /* If we have a DW_AT_specification, we might need to import using
6079 directives from the context of the specification DIE. See the
6080 comment in determine_prefix. */
6081 if (cu->language == language_cplus
6082 && dwarf2_attr (die, DW_AT_specification, cu))
6083 {
6084 struct dwarf2_cu *spec_cu = cu;
6085 struct die_info *spec_die = die_specification (die, &spec_cu);
6086
6087 while (spec_die)
6088 {
6089 child_die = spec_die->child;
6090 while (child_die && child_die->tag)
6091 {
6092 if (child_die->tag == DW_TAG_imported_module)
6093 process_die (child_die, spec_cu);
6094 child_die = sibling_die (child_die);
6095 }
6096
6097 /* In some cases, GCC generates specification DIEs that
6098 themselves contain DW_AT_specification attributes. */
6099 spec_die = die_specification (spec_die, &spec_cu);
6100 }
6101 }
6102
6103 new = pop_context ();
6104 /* Make a block for the local symbols within. */
6105 block = finish_block (new->name, &local_symbols, new->old_blocks,
6106 lowpc, highpc, objfile);
6107
6108 /* For C++, set the block's scope. */
6109 if (cu->language == language_cplus || cu->language == language_fortran)
6110 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6111 determine_prefix (die, cu),
6112 processing_has_namespace_info);
6113
6114 /* If we have address ranges, record them. */
6115 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6116
6117 /* Attach template arguments to function. */
6118 if (! VEC_empty (symbolp, template_args))
6119 {
6120 gdb_assert (templ_func != NULL);
6121
6122 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6123 templ_func->template_arguments
6124 = obstack_alloc (&objfile->objfile_obstack,
6125 (templ_func->n_template_arguments
6126 * sizeof (struct symbol *)));
6127 memcpy (templ_func->template_arguments,
6128 VEC_address (symbolp, template_args),
6129 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6130 VEC_free (symbolp, template_args);
6131 }
6132
6133 /* In C++, we can have functions nested inside functions (e.g., when
6134 a function declares a class that has methods). This means that
6135 when we finish processing a function scope, we may need to go
6136 back to building a containing block's symbol lists. */
6137 local_symbols = new->locals;
6138 param_symbols = new->params;
6139 using_directives = new->using_directives;
6140
6141 /* If we've finished processing a top-level function, subsequent
6142 symbols go in the file symbol list. */
6143 if (outermost_context_p ())
6144 cu->list_in_scope = &file_symbols;
6145 }
6146
6147 /* Process all the DIES contained within a lexical block scope. Start
6148 a new scope, process the dies, and then close the scope. */
6149
6150 static void
6151 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6152 {
6153 struct objfile *objfile = cu->objfile;
6154 struct context_stack *new;
6155 CORE_ADDR lowpc, highpc;
6156 struct die_info *child_die;
6157 CORE_ADDR baseaddr;
6158
6159 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6160
6161 /* Ignore blocks with missing or invalid low and high pc attributes. */
6162 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6163 as multiple lexical blocks? Handling children in a sane way would
6164 be nasty. Might be easier to properly extend generic blocks to
6165 describe ranges. */
6166 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6167 return;
6168 lowpc += baseaddr;
6169 highpc += baseaddr;
6170
6171 push_context (0, lowpc);
6172 if (die->child != NULL)
6173 {
6174 child_die = die->child;
6175 while (child_die && child_die->tag)
6176 {
6177 process_die (child_die, cu);
6178 child_die = sibling_die (child_die);
6179 }
6180 }
6181 new = pop_context ();
6182
6183 if (local_symbols != NULL || using_directives != NULL)
6184 {
6185 struct block *block
6186 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6187 highpc, objfile);
6188
6189 /* Note that recording ranges after traversing children, as we
6190 do here, means that recording a parent's ranges entails
6191 walking across all its children's ranges as they appear in
6192 the address map, which is quadratic behavior.
6193
6194 It would be nicer to record the parent's ranges before
6195 traversing its children, simply overriding whatever you find
6196 there. But since we don't even decide whether to create a
6197 block until after we've traversed its children, that's hard
6198 to do. */
6199 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6200 }
6201 local_symbols = new->locals;
6202 using_directives = new->using_directives;
6203 }
6204
6205 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6206
6207 static void
6208 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6209 {
6210 struct objfile *objfile = cu->objfile;
6211 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6212 CORE_ADDR pc, baseaddr;
6213 struct attribute *attr;
6214 struct call_site *call_site, call_site_local;
6215 void **slot;
6216 int nparams;
6217 struct die_info *child_die;
6218
6219 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6220
6221 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6222 if (!attr)
6223 {
6224 complaint (&symfile_complaints,
6225 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6226 "DIE 0x%x [in module %s]"),
6227 die->offset, cu->objfile->name);
6228 return;
6229 }
6230 pc = DW_ADDR (attr) + baseaddr;
6231
6232 if (cu->call_site_htab == NULL)
6233 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6234 NULL, &objfile->objfile_obstack,
6235 hashtab_obstack_allocate, NULL);
6236 call_site_local.pc = pc;
6237 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6238 if (*slot != NULL)
6239 {
6240 complaint (&symfile_complaints,
6241 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6242 "DIE 0x%x [in module %s]"),
6243 paddress (gdbarch, pc), die->offset, cu->objfile->name);
6244 return;
6245 }
6246
6247 /* Count parameters at the caller. */
6248
6249 nparams = 0;
6250 for (child_die = die->child; child_die && child_die->tag;
6251 child_die = sibling_die (child_die))
6252 {
6253 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6254 {
6255 complaint (&symfile_complaints,
6256 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6257 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6258 child_die->tag, child_die->offset, cu->objfile->name);
6259 continue;
6260 }
6261
6262 nparams++;
6263 }
6264
6265 call_site = obstack_alloc (&objfile->objfile_obstack,
6266 (sizeof (*call_site)
6267 + (sizeof (*call_site->parameter)
6268 * (nparams - 1))));
6269 *slot = call_site;
6270 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6271 call_site->pc = pc;
6272
6273 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6274 {
6275 struct die_info *func_die;
6276
6277 /* Skip also over DW_TAG_inlined_subroutine. */
6278 for (func_die = die->parent;
6279 func_die && func_die->tag != DW_TAG_subprogram
6280 && func_die->tag != DW_TAG_subroutine_type;
6281 func_die = func_die->parent);
6282
6283 /* DW_AT_GNU_all_call_sites is a superset
6284 of DW_AT_GNU_all_tail_call_sites. */
6285 if (func_die
6286 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6287 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6288 {
6289 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6290 not complete. But keep CALL_SITE for look ups via call_site_htab,
6291 both the initial caller containing the real return address PC and
6292 the final callee containing the current PC of a chain of tail
6293 calls do not need to have the tail call list complete. But any
6294 function candidate for a virtual tail call frame searched via
6295 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6296 determined unambiguously. */
6297 }
6298 else
6299 {
6300 struct type *func_type = NULL;
6301
6302 if (func_die)
6303 func_type = get_die_type (func_die, cu);
6304 if (func_type != NULL)
6305 {
6306 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6307
6308 /* Enlist this call site to the function. */
6309 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6310 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6311 }
6312 else
6313 complaint (&symfile_complaints,
6314 _("Cannot find function owning DW_TAG_GNU_call_site "
6315 "DIE 0x%x [in module %s]"),
6316 die->offset, cu->objfile->name);
6317 }
6318 }
6319
6320 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6321 if (attr == NULL)
6322 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6323 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6324 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6325 /* Keep NULL DWARF_BLOCK. */;
6326 else if (attr_form_is_block (attr))
6327 {
6328 struct dwarf2_locexpr_baton *dlbaton;
6329
6330 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6331 dlbaton->data = DW_BLOCK (attr)->data;
6332 dlbaton->size = DW_BLOCK (attr)->size;
6333 dlbaton->per_cu = cu->per_cu;
6334
6335 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6336 }
6337 else if (is_ref_attr (attr))
6338 {
6339 struct objfile *objfile = cu->objfile;
6340 struct dwarf2_cu *target_cu = cu;
6341 struct die_info *target_die;
6342
6343 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6344 gdb_assert (target_cu->objfile == objfile);
6345 if (die_is_declaration (target_die, target_cu))
6346 {
6347 const char *target_physname;
6348
6349 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6350 if (target_physname == NULL)
6351 complaint (&symfile_complaints,
6352 _("DW_AT_GNU_call_site_target target DIE has invalid "
6353 "physname, for referencing DIE 0x%x [in module %s]"),
6354 die->offset, cu->objfile->name);
6355 else
6356 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6357 }
6358 else
6359 {
6360 CORE_ADDR lowpc;
6361
6362 /* DW_AT_entry_pc should be preferred. */
6363 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6364 complaint (&symfile_complaints,
6365 _("DW_AT_GNU_call_site_target target DIE has invalid "
6366 "low pc, for referencing DIE 0x%x [in module %s]"),
6367 die->offset, cu->objfile->name);
6368 else
6369 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6370 }
6371 }
6372 else
6373 complaint (&symfile_complaints,
6374 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6375 "block nor reference, for DIE 0x%x [in module %s]"),
6376 die->offset, cu->objfile->name);
6377
6378 call_site->per_cu = cu->per_cu;
6379
6380 for (child_die = die->child;
6381 child_die && child_die->tag;
6382 child_die = sibling_die (child_die))
6383 {
6384 struct dwarf2_locexpr_baton *dlbaton;
6385 struct call_site_parameter *parameter;
6386
6387 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6388 {
6389 /* Already printed the complaint above. */
6390 continue;
6391 }
6392
6393 gdb_assert (call_site->parameter_count < nparams);
6394 parameter = &call_site->parameter[call_site->parameter_count];
6395
6396 /* DW_AT_location specifies the register number. Value of the data
6397 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6398
6399 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6400 if (!attr || !attr_form_is_block (attr))
6401 {
6402 complaint (&symfile_complaints,
6403 _("No DW_FORM_block* DW_AT_location for "
6404 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6405 child_die->offset, cu->objfile->name);
6406 continue;
6407 }
6408 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6409 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6410 if (parameter->dwarf_reg == -1
6411 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6412 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6413 &parameter->fb_offset))
6414 {
6415 complaint (&symfile_complaints,
6416 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6417 "for DW_FORM_block* DW_AT_location for "
6418 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6419 child_die->offset, cu->objfile->name);
6420 continue;
6421 }
6422
6423 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6424 if (!attr_form_is_block (attr))
6425 {
6426 complaint (&symfile_complaints,
6427 _("No DW_FORM_block* DW_AT_GNU_call_site_value 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 parameter->value = DW_BLOCK (attr)->data;
6433 parameter->value_size = DW_BLOCK (attr)->size;
6434
6435 /* Parameters are not pre-cleared by memset above. */
6436 parameter->data_value = NULL;
6437 parameter->data_value_size = 0;
6438 call_site->parameter_count++;
6439
6440 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6441 if (attr)
6442 {
6443 if (!attr_form_is_block (attr))
6444 complaint (&symfile_complaints,
6445 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6446 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6447 child_die->offset, cu->objfile->name);
6448 else
6449 {
6450 parameter->data_value = DW_BLOCK (attr)->data;
6451 parameter->data_value_size = DW_BLOCK (attr)->size;
6452 }
6453 }
6454 }
6455 }
6456
6457 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6458 Return 1 if the attributes are present and valid, otherwise, return 0.
6459 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6460
6461 static int
6462 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6463 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6464 struct partial_symtab *ranges_pst)
6465 {
6466 struct objfile *objfile = cu->objfile;
6467 struct comp_unit_head *cu_header = &cu->header;
6468 bfd *obfd = objfile->obfd;
6469 unsigned int addr_size = cu_header->addr_size;
6470 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6471 /* Base address selection entry. */
6472 CORE_ADDR base;
6473 int found_base;
6474 unsigned int dummy;
6475 gdb_byte *buffer;
6476 CORE_ADDR marker;
6477 int low_set;
6478 CORE_ADDR low = 0;
6479 CORE_ADDR high = 0;
6480 CORE_ADDR baseaddr;
6481
6482 found_base = cu->base_known;
6483 base = cu->base_address;
6484
6485 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6486 if (offset >= dwarf2_per_objfile->ranges.size)
6487 {
6488 complaint (&symfile_complaints,
6489 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6490 offset);
6491 return 0;
6492 }
6493 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6494
6495 /* Read in the largest possible address. */
6496 marker = read_address (obfd, buffer, cu, &dummy);
6497 if ((marker & mask) == mask)
6498 {
6499 /* If we found the largest possible address, then
6500 read the base address. */
6501 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6502 buffer += 2 * addr_size;
6503 offset += 2 * addr_size;
6504 found_base = 1;
6505 }
6506
6507 low_set = 0;
6508
6509 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6510
6511 while (1)
6512 {
6513 CORE_ADDR range_beginning, range_end;
6514
6515 range_beginning = read_address (obfd, buffer, cu, &dummy);
6516 buffer += addr_size;
6517 range_end = read_address (obfd, buffer, cu, &dummy);
6518 buffer += addr_size;
6519 offset += 2 * addr_size;
6520
6521 /* An end of list marker is a pair of zero addresses. */
6522 if (range_beginning == 0 && range_end == 0)
6523 /* Found the end of list entry. */
6524 break;
6525
6526 /* Each base address selection entry is a pair of 2 values.
6527 The first is the largest possible address, the second is
6528 the base address. Check for a base address here. */
6529 if ((range_beginning & mask) == mask)
6530 {
6531 /* If we found the largest possible address, then
6532 read the base address. */
6533 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6534 found_base = 1;
6535 continue;
6536 }
6537
6538 if (!found_base)
6539 {
6540 /* We have no valid base address for the ranges
6541 data. */
6542 complaint (&symfile_complaints,
6543 _("Invalid .debug_ranges data (no base address)"));
6544 return 0;
6545 }
6546
6547 if (range_beginning > range_end)
6548 {
6549 /* Inverted range entries are invalid. */
6550 complaint (&symfile_complaints,
6551 _("Invalid .debug_ranges data (inverted range)"));
6552 return 0;
6553 }
6554
6555 /* Empty range entries have no effect. */
6556 if (range_beginning == range_end)
6557 continue;
6558
6559 range_beginning += base;
6560 range_end += base;
6561
6562 if (ranges_pst != NULL)
6563 addrmap_set_empty (objfile->psymtabs_addrmap,
6564 range_beginning + baseaddr,
6565 range_end - 1 + baseaddr,
6566 ranges_pst);
6567
6568 /* FIXME: This is recording everything as a low-high
6569 segment of consecutive addresses. We should have a
6570 data structure for discontiguous block ranges
6571 instead. */
6572 if (! low_set)
6573 {
6574 low = range_beginning;
6575 high = range_end;
6576 low_set = 1;
6577 }
6578 else
6579 {
6580 if (range_beginning < low)
6581 low = range_beginning;
6582 if (range_end > high)
6583 high = range_end;
6584 }
6585 }
6586
6587 if (! low_set)
6588 /* If the first entry is an end-of-list marker, the range
6589 describes an empty scope, i.e. no instructions. */
6590 return 0;
6591
6592 if (low_return)
6593 *low_return = low;
6594 if (high_return)
6595 *high_return = high;
6596 return 1;
6597 }
6598
6599 /* Get low and high pc attributes from a die. Return 1 if the attributes
6600 are present and valid, otherwise, return 0. Return -1 if the range is
6601 discontinuous, i.e. derived from DW_AT_ranges information. */
6602 static int
6603 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6604 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6605 struct partial_symtab *pst)
6606 {
6607 struct attribute *attr;
6608 CORE_ADDR low = 0;
6609 CORE_ADDR high = 0;
6610 int ret = 0;
6611
6612 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6613 if (attr)
6614 {
6615 high = DW_ADDR (attr);
6616 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6617 if (attr)
6618 low = DW_ADDR (attr);
6619 else
6620 /* Found high w/o low attribute. */
6621 return 0;
6622
6623 /* Found consecutive range of addresses. */
6624 ret = 1;
6625 }
6626 else
6627 {
6628 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6629 if (attr != NULL)
6630 {
6631 /* Value of the DW_AT_ranges attribute is the offset in the
6632 .debug_ranges section. */
6633 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6634 return 0;
6635 /* Found discontinuous range of addresses. */
6636 ret = -1;
6637 }
6638 }
6639
6640 /* read_partial_die has also the strict LOW < HIGH requirement. */
6641 if (high <= low)
6642 return 0;
6643
6644 /* When using the GNU linker, .gnu.linkonce. sections are used to
6645 eliminate duplicate copies of functions and vtables and such.
6646 The linker will arbitrarily choose one and discard the others.
6647 The AT_*_pc values for such functions refer to local labels in
6648 these sections. If the section from that file was discarded, the
6649 labels are not in the output, so the relocs get a value of 0.
6650 If this is a discarded function, mark the pc bounds as invalid,
6651 so that GDB will ignore it. */
6652 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6653 return 0;
6654
6655 *lowpc = low;
6656 if (highpc)
6657 *highpc = high;
6658 return ret;
6659 }
6660
6661 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6662 its low and high PC addresses. Do nothing if these addresses could not
6663 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6664 and HIGHPC to the high address if greater than HIGHPC. */
6665
6666 static void
6667 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6668 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6669 struct dwarf2_cu *cu)
6670 {
6671 CORE_ADDR low, high;
6672 struct die_info *child = die->child;
6673
6674 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6675 {
6676 *lowpc = min (*lowpc, low);
6677 *highpc = max (*highpc, high);
6678 }
6679
6680 /* If the language does not allow nested subprograms (either inside
6681 subprograms or lexical blocks), we're done. */
6682 if (cu->language != language_ada)
6683 return;
6684
6685 /* Check all the children of the given DIE. If it contains nested
6686 subprograms, then check their pc bounds. Likewise, we need to
6687 check lexical blocks as well, as they may also contain subprogram
6688 definitions. */
6689 while (child && child->tag)
6690 {
6691 if (child->tag == DW_TAG_subprogram
6692 || child->tag == DW_TAG_lexical_block)
6693 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6694 child = sibling_die (child);
6695 }
6696 }
6697
6698 /* Get the low and high pc's represented by the scope DIE, and store
6699 them in *LOWPC and *HIGHPC. If the correct values can't be
6700 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6701
6702 static void
6703 get_scope_pc_bounds (struct die_info *die,
6704 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6705 struct dwarf2_cu *cu)
6706 {
6707 CORE_ADDR best_low = (CORE_ADDR) -1;
6708 CORE_ADDR best_high = (CORE_ADDR) 0;
6709 CORE_ADDR current_low, current_high;
6710
6711 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6712 {
6713 best_low = current_low;
6714 best_high = current_high;
6715 }
6716 else
6717 {
6718 struct die_info *child = die->child;
6719
6720 while (child && child->tag)
6721 {
6722 switch (child->tag) {
6723 case DW_TAG_subprogram:
6724 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6725 break;
6726 case DW_TAG_namespace:
6727 case DW_TAG_module:
6728 /* FIXME: carlton/2004-01-16: Should we do this for
6729 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6730 that current GCC's always emit the DIEs corresponding
6731 to definitions of methods of classes as children of a
6732 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6733 the DIEs giving the declarations, which could be
6734 anywhere). But I don't see any reason why the
6735 standards says that they have to be there. */
6736 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6737
6738 if (current_low != ((CORE_ADDR) -1))
6739 {
6740 best_low = min (best_low, current_low);
6741 best_high = max (best_high, current_high);
6742 }
6743 break;
6744 default:
6745 /* Ignore. */
6746 break;
6747 }
6748
6749 child = sibling_die (child);
6750 }
6751 }
6752
6753 *lowpc = best_low;
6754 *highpc = best_high;
6755 }
6756
6757 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6758 in DIE. */
6759 static void
6760 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6761 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6762 {
6763 struct attribute *attr;
6764
6765 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6766 if (attr)
6767 {
6768 CORE_ADDR high = DW_ADDR (attr);
6769
6770 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6771 if (attr)
6772 {
6773 CORE_ADDR low = DW_ADDR (attr);
6774
6775 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6776 }
6777 }
6778
6779 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6780 if (attr)
6781 {
6782 bfd *obfd = cu->objfile->obfd;
6783
6784 /* The value of the DW_AT_ranges attribute is the offset of the
6785 address range list in the .debug_ranges section. */
6786 unsigned long offset = DW_UNSND (attr);
6787 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6788
6789 /* For some target architectures, but not others, the
6790 read_address function sign-extends the addresses it returns.
6791 To recognize base address selection entries, we need a
6792 mask. */
6793 unsigned int addr_size = cu->header.addr_size;
6794 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6795
6796 /* The base address, to which the next pair is relative. Note
6797 that this 'base' is a DWARF concept: most entries in a range
6798 list are relative, to reduce the number of relocs against the
6799 debugging information. This is separate from this function's
6800 'baseaddr' argument, which GDB uses to relocate debugging
6801 information from a shared library based on the address at
6802 which the library was loaded. */
6803 CORE_ADDR base = cu->base_address;
6804 int base_known = cu->base_known;
6805
6806 gdb_assert (dwarf2_per_objfile->ranges.readin);
6807 if (offset >= dwarf2_per_objfile->ranges.size)
6808 {
6809 complaint (&symfile_complaints,
6810 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6811 offset);
6812 return;
6813 }
6814
6815 for (;;)
6816 {
6817 unsigned int bytes_read;
6818 CORE_ADDR start, end;
6819
6820 start = read_address (obfd, buffer, cu, &bytes_read);
6821 buffer += bytes_read;
6822 end = read_address (obfd, buffer, cu, &bytes_read);
6823 buffer += bytes_read;
6824
6825 /* Did we find the end of the range list? */
6826 if (start == 0 && end == 0)
6827 break;
6828
6829 /* Did we find a base address selection entry? */
6830 else if ((start & base_select_mask) == base_select_mask)
6831 {
6832 base = end;
6833 base_known = 1;
6834 }
6835
6836 /* We found an ordinary address range. */
6837 else
6838 {
6839 if (!base_known)
6840 {
6841 complaint (&symfile_complaints,
6842 _("Invalid .debug_ranges data "
6843 "(no base address)"));
6844 return;
6845 }
6846
6847 if (start > end)
6848 {
6849 /* Inverted range entries are invalid. */
6850 complaint (&symfile_complaints,
6851 _("Invalid .debug_ranges data "
6852 "(inverted range)"));
6853 return;
6854 }
6855
6856 /* Empty range entries have no effect. */
6857 if (start == end)
6858 continue;
6859
6860 record_block_range (block,
6861 baseaddr + base + start,
6862 baseaddr + base + end - 1);
6863 }
6864 }
6865 }
6866 }
6867
6868 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6869 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6870 during 4.6.0 experimental. */
6871
6872 static int
6873 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6874 {
6875 const char *cs;
6876 int major, minor, release;
6877
6878 if (cu->producer == NULL)
6879 {
6880 /* For unknown compilers expect their behavior is DWARF version
6881 compliant.
6882
6883 GCC started to support .debug_types sections by -gdwarf-4 since
6884 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6885 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6886 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6887 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6888
6889 return 0;
6890 }
6891
6892 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6893
6894 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6895 {
6896 /* For non-GCC compilers expect their behavior is DWARF version
6897 compliant. */
6898
6899 return 0;
6900 }
6901 cs = &cu->producer[strlen ("GNU ")];
6902 while (*cs && !isdigit (*cs))
6903 cs++;
6904 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6905 {
6906 /* Not recognized as GCC. */
6907
6908 return 0;
6909 }
6910
6911 return major < 4 || (major == 4 && minor < 6);
6912 }
6913
6914 /* Return the default accessibility type if it is not overriden by
6915 DW_AT_accessibility. */
6916
6917 static enum dwarf_access_attribute
6918 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6919 {
6920 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6921 {
6922 /* The default DWARF 2 accessibility for members is public, the default
6923 accessibility for inheritance is private. */
6924
6925 if (die->tag != DW_TAG_inheritance)
6926 return DW_ACCESS_public;
6927 else
6928 return DW_ACCESS_private;
6929 }
6930 else
6931 {
6932 /* DWARF 3+ defines the default accessibility a different way. The same
6933 rules apply now for DW_TAG_inheritance as for the members and it only
6934 depends on the container kind. */
6935
6936 if (die->parent->tag == DW_TAG_class_type)
6937 return DW_ACCESS_private;
6938 else
6939 return DW_ACCESS_public;
6940 }
6941 }
6942
6943 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6944 offset. If the attribute was not found return 0, otherwise return
6945 1. If it was found but could not properly be handled, set *OFFSET
6946 to 0. */
6947
6948 static int
6949 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6950 LONGEST *offset)
6951 {
6952 struct attribute *attr;
6953
6954 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6955 if (attr != NULL)
6956 {
6957 *offset = 0;
6958
6959 /* Note that we do not check for a section offset first here.
6960 This is because DW_AT_data_member_location is new in DWARF 4,
6961 so if we see it, we can assume that a constant form is really
6962 a constant and not a section offset. */
6963 if (attr_form_is_constant (attr))
6964 *offset = dwarf2_get_attr_constant_value (attr, 0);
6965 else if (attr_form_is_section_offset (attr))
6966 dwarf2_complex_location_expr_complaint ();
6967 else if (attr_form_is_block (attr))
6968 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6969 else
6970 dwarf2_complex_location_expr_complaint ();
6971
6972 return 1;
6973 }
6974
6975 return 0;
6976 }
6977
6978 /* Add an aggregate field to the field list. */
6979
6980 static void
6981 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6982 struct dwarf2_cu *cu)
6983 {
6984 struct objfile *objfile = cu->objfile;
6985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6986 struct nextfield *new_field;
6987 struct attribute *attr;
6988 struct field *fp;
6989 char *fieldname = "";
6990
6991 /* Allocate a new field list entry and link it in. */
6992 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6993 make_cleanup (xfree, new_field);
6994 memset (new_field, 0, sizeof (struct nextfield));
6995
6996 if (die->tag == DW_TAG_inheritance)
6997 {
6998 new_field->next = fip->baseclasses;
6999 fip->baseclasses = new_field;
7000 }
7001 else
7002 {
7003 new_field->next = fip->fields;
7004 fip->fields = new_field;
7005 }
7006 fip->nfields++;
7007
7008 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7009 if (attr)
7010 new_field->accessibility = DW_UNSND (attr);
7011 else
7012 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7013 if (new_field->accessibility != DW_ACCESS_public)
7014 fip->non_public_fields = 1;
7015
7016 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7017 if (attr)
7018 new_field->virtuality = DW_UNSND (attr);
7019 else
7020 new_field->virtuality = DW_VIRTUALITY_none;
7021
7022 fp = &new_field->field;
7023
7024 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7025 {
7026 LONGEST offset;
7027
7028 /* Data member other than a C++ static data member. */
7029
7030 /* Get type of field. */
7031 fp->type = die_type (die, cu);
7032
7033 SET_FIELD_BITPOS (*fp, 0);
7034
7035 /* Get bit size of field (zero if none). */
7036 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7037 if (attr)
7038 {
7039 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7040 }
7041 else
7042 {
7043 FIELD_BITSIZE (*fp) = 0;
7044 }
7045
7046 /* Get bit offset of field. */
7047 if (handle_data_member_location (die, cu, &offset))
7048 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7049 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7050 if (attr)
7051 {
7052 if (gdbarch_bits_big_endian (gdbarch))
7053 {
7054 /* For big endian bits, the DW_AT_bit_offset gives the
7055 additional bit offset from the MSB of the containing
7056 anonymous object to the MSB of the field. We don't
7057 have to do anything special since we don't need to
7058 know the size of the anonymous object. */
7059 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7060 }
7061 else
7062 {
7063 /* For little endian bits, compute the bit offset to the
7064 MSB of the anonymous object, subtract off the number of
7065 bits from the MSB of the field to the MSB of the
7066 object, and then subtract off the number of bits of
7067 the field itself. The result is the bit offset of
7068 the LSB of the field. */
7069 int anonymous_size;
7070 int bit_offset = DW_UNSND (attr);
7071
7072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7073 if (attr)
7074 {
7075 /* The size of the anonymous object containing
7076 the bit field is explicit, so use the
7077 indicated size (in bytes). */
7078 anonymous_size = DW_UNSND (attr);
7079 }
7080 else
7081 {
7082 /* The size of the anonymous object containing
7083 the bit field must be inferred from the type
7084 attribute of the data member containing the
7085 bit field. */
7086 anonymous_size = TYPE_LENGTH (fp->type);
7087 }
7088 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7089 - bit_offset - FIELD_BITSIZE (*fp);
7090 }
7091 }
7092
7093 /* Get name of field. */
7094 fieldname = dwarf2_name (die, cu);
7095 if (fieldname == NULL)
7096 fieldname = "";
7097
7098 /* The name is already allocated along with this objfile, so we don't
7099 need to duplicate it for the type. */
7100 fp->name = fieldname;
7101
7102 /* Change accessibility for artificial fields (e.g. virtual table
7103 pointer or virtual base class pointer) to private. */
7104 if (dwarf2_attr (die, DW_AT_artificial, cu))
7105 {
7106 FIELD_ARTIFICIAL (*fp) = 1;
7107 new_field->accessibility = DW_ACCESS_private;
7108 fip->non_public_fields = 1;
7109 }
7110 }
7111 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7112 {
7113 /* C++ static member. */
7114
7115 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7116 is a declaration, but all versions of G++ as of this writing
7117 (so through at least 3.2.1) incorrectly generate
7118 DW_TAG_variable tags. */
7119
7120 const char *physname;
7121
7122 /* Get name of field. */
7123 fieldname = dwarf2_name (die, cu);
7124 if (fieldname == NULL)
7125 return;
7126
7127 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7128 if (attr
7129 /* Only create a symbol if this is an external value.
7130 new_symbol checks this and puts the value in the global symbol
7131 table, which we want. If it is not external, new_symbol
7132 will try to put the value in cu->list_in_scope which is wrong. */
7133 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7134 {
7135 /* A static const member, not much different than an enum as far as
7136 we're concerned, except that we can support more types. */
7137 new_symbol (die, NULL, cu);
7138 }
7139
7140 /* Get physical name. */
7141 physname = dwarf2_physname (fieldname, die, cu);
7142
7143 /* The name is already allocated along with this objfile, so we don't
7144 need to duplicate it for the type. */
7145 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7146 FIELD_TYPE (*fp) = die_type (die, cu);
7147 FIELD_NAME (*fp) = fieldname;
7148 }
7149 else if (die->tag == DW_TAG_inheritance)
7150 {
7151 LONGEST offset;
7152
7153 /* C++ base class field. */
7154 if (handle_data_member_location (die, cu, &offset))
7155 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7156 FIELD_BITSIZE (*fp) = 0;
7157 FIELD_TYPE (*fp) = die_type (die, cu);
7158 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7159 fip->nbaseclasses++;
7160 }
7161 }
7162
7163 /* Add a typedef defined in the scope of the FIP's class. */
7164
7165 static void
7166 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7167 struct dwarf2_cu *cu)
7168 {
7169 struct objfile *objfile = cu->objfile;
7170 struct typedef_field_list *new_field;
7171 struct attribute *attr;
7172 struct typedef_field *fp;
7173 char *fieldname = "";
7174
7175 /* Allocate a new field list entry and link it in. */
7176 new_field = xzalloc (sizeof (*new_field));
7177 make_cleanup (xfree, new_field);
7178
7179 gdb_assert (die->tag == DW_TAG_typedef);
7180
7181 fp = &new_field->field;
7182
7183 /* Get name of field. */
7184 fp->name = dwarf2_name (die, cu);
7185 if (fp->name == NULL)
7186 return;
7187
7188 fp->type = read_type_die (die, cu);
7189
7190 new_field->next = fip->typedef_field_list;
7191 fip->typedef_field_list = new_field;
7192 fip->typedef_field_list_count++;
7193 }
7194
7195 /* Create the vector of fields, and attach it to the type. */
7196
7197 static void
7198 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7199 struct dwarf2_cu *cu)
7200 {
7201 int nfields = fip->nfields;
7202
7203 /* Record the field count, allocate space for the array of fields,
7204 and create blank accessibility bitfields if necessary. */
7205 TYPE_NFIELDS (type) = nfields;
7206 TYPE_FIELDS (type) = (struct field *)
7207 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7208 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7209
7210 if (fip->non_public_fields && cu->language != language_ada)
7211 {
7212 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7213
7214 TYPE_FIELD_PRIVATE_BITS (type) =
7215 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7216 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7217
7218 TYPE_FIELD_PROTECTED_BITS (type) =
7219 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7220 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7221
7222 TYPE_FIELD_IGNORE_BITS (type) =
7223 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7224 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7225 }
7226
7227 /* If the type has baseclasses, allocate and clear a bit vector for
7228 TYPE_FIELD_VIRTUAL_BITS. */
7229 if (fip->nbaseclasses && cu->language != language_ada)
7230 {
7231 int num_bytes = B_BYTES (fip->nbaseclasses);
7232 unsigned char *pointer;
7233
7234 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7235 pointer = TYPE_ALLOC (type, num_bytes);
7236 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7237 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7238 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7239 }
7240
7241 /* Copy the saved-up fields into the field vector. Start from the head of
7242 the list, adding to the tail of the field array, so that they end up in
7243 the same order in the array in which they were added to the list. */
7244 while (nfields-- > 0)
7245 {
7246 struct nextfield *fieldp;
7247
7248 if (fip->fields)
7249 {
7250 fieldp = fip->fields;
7251 fip->fields = fieldp->next;
7252 }
7253 else
7254 {
7255 fieldp = fip->baseclasses;
7256 fip->baseclasses = fieldp->next;
7257 }
7258
7259 TYPE_FIELD (type, nfields) = fieldp->field;
7260 switch (fieldp->accessibility)
7261 {
7262 case DW_ACCESS_private:
7263 if (cu->language != language_ada)
7264 SET_TYPE_FIELD_PRIVATE (type, nfields);
7265 break;
7266
7267 case DW_ACCESS_protected:
7268 if (cu->language != language_ada)
7269 SET_TYPE_FIELD_PROTECTED (type, nfields);
7270 break;
7271
7272 case DW_ACCESS_public:
7273 break;
7274
7275 default:
7276 /* Unknown accessibility. Complain and treat it as public. */
7277 {
7278 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7279 fieldp->accessibility);
7280 }
7281 break;
7282 }
7283 if (nfields < fip->nbaseclasses)
7284 {
7285 switch (fieldp->virtuality)
7286 {
7287 case DW_VIRTUALITY_virtual:
7288 case DW_VIRTUALITY_pure_virtual:
7289 if (cu->language == language_ada)
7290 error (_("unexpected virtuality in component of Ada type"));
7291 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7292 break;
7293 }
7294 }
7295 }
7296 }
7297
7298 /* Add a member function to the proper fieldlist. */
7299
7300 static void
7301 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7302 struct type *type, struct dwarf2_cu *cu)
7303 {
7304 struct objfile *objfile = cu->objfile;
7305 struct attribute *attr;
7306 struct fnfieldlist *flp;
7307 int i;
7308 struct fn_field *fnp;
7309 char *fieldname;
7310 struct nextfnfield *new_fnfield;
7311 struct type *this_type;
7312 enum dwarf_access_attribute accessibility;
7313
7314 if (cu->language == language_ada)
7315 error (_("unexpected member function in Ada type"));
7316
7317 /* Get name of member function. */
7318 fieldname = dwarf2_name (die, cu);
7319 if (fieldname == NULL)
7320 return;
7321
7322 /* Look up member function name in fieldlist. */
7323 for (i = 0; i < fip->nfnfields; i++)
7324 {
7325 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7326 break;
7327 }
7328
7329 /* Create new list element if necessary. */
7330 if (i < fip->nfnfields)
7331 flp = &fip->fnfieldlists[i];
7332 else
7333 {
7334 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7335 {
7336 fip->fnfieldlists = (struct fnfieldlist *)
7337 xrealloc (fip->fnfieldlists,
7338 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7339 * sizeof (struct fnfieldlist));
7340 if (fip->nfnfields == 0)
7341 make_cleanup (free_current_contents, &fip->fnfieldlists);
7342 }
7343 flp = &fip->fnfieldlists[fip->nfnfields];
7344 flp->name = fieldname;
7345 flp->length = 0;
7346 flp->head = NULL;
7347 i = fip->nfnfields++;
7348 }
7349
7350 /* Create a new member function field and chain it to the field list
7351 entry. */
7352 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7353 make_cleanup (xfree, new_fnfield);
7354 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7355 new_fnfield->next = flp->head;
7356 flp->head = new_fnfield;
7357 flp->length++;
7358
7359 /* Fill in the member function field info. */
7360 fnp = &new_fnfield->fnfield;
7361
7362 /* Delay processing of the physname until later. */
7363 if (cu->language == language_cplus || cu->language == language_java)
7364 {
7365 add_to_method_list (type, i, flp->length - 1, fieldname,
7366 die, cu);
7367 }
7368 else
7369 {
7370 const char *physname = dwarf2_physname (fieldname, die, cu);
7371 fnp->physname = physname ? physname : "";
7372 }
7373
7374 fnp->type = alloc_type (objfile);
7375 this_type = read_type_die (die, cu);
7376 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7377 {
7378 int nparams = TYPE_NFIELDS (this_type);
7379
7380 /* TYPE is the domain of this method, and THIS_TYPE is the type
7381 of the method itself (TYPE_CODE_METHOD). */
7382 smash_to_method_type (fnp->type, type,
7383 TYPE_TARGET_TYPE (this_type),
7384 TYPE_FIELDS (this_type),
7385 TYPE_NFIELDS (this_type),
7386 TYPE_VARARGS (this_type));
7387
7388 /* Handle static member functions.
7389 Dwarf2 has no clean way to discern C++ static and non-static
7390 member functions. G++ helps GDB by marking the first
7391 parameter for non-static member functions (which is the this
7392 pointer) as artificial. We obtain this information from
7393 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7394 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7395 fnp->voffset = VOFFSET_STATIC;
7396 }
7397 else
7398 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7399 dwarf2_full_name (fieldname, die, cu));
7400
7401 /* Get fcontext from DW_AT_containing_type if present. */
7402 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7403 fnp->fcontext = die_containing_type (die, cu);
7404
7405 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7406 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7407
7408 /* Get accessibility. */
7409 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7410 if (attr)
7411 accessibility = DW_UNSND (attr);
7412 else
7413 accessibility = dwarf2_default_access_attribute (die, cu);
7414 switch (accessibility)
7415 {
7416 case DW_ACCESS_private:
7417 fnp->is_private = 1;
7418 break;
7419 case DW_ACCESS_protected:
7420 fnp->is_protected = 1;
7421 break;
7422 }
7423
7424 /* Check for artificial methods. */
7425 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7426 if (attr && DW_UNSND (attr) != 0)
7427 fnp->is_artificial = 1;
7428
7429 /* Get index in virtual function table if it is a virtual member
7430 function. For older versions of GCC, this is an offset in the
7431 appropriate virtual table, as specified by DW_AT_containing_type.
7432 For everyone else, it is an expression to be evaluated relative
7433 to the object address. */
7434
7435 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7436 if (attr)
7437 {
7438 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7439 {
7440 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7441 {
7442 /* Old-style GCC. */
7443 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7444 }
7445 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7446 || (DW_BLOCK (attr)->size > 1
7447 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7448 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7449 {
7450 struct dwarf_block blk;
7451 int offset;
7452
7453 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7454 ? 1 : 2);
7455 blk.size = DW_BLOCK (attr)->size - offset;
7456 blk.data = DW_BLOCK (attr)->data + offset;
7457 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7458 if ((fnp->voffset % cu->header.addr_size) != 0)
7459 dwarf2_complex_location_expr_complaint ();
7460 else
7461 fnp->voffset /= cu->header.addr_size;
7462 fnp->voffset += 2;
7463 }
7464 else
7465 dwarf2_complex_location_expr_complaint ();
7466
7467 if (!fnp->fcontext)
7468 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7469 }
7470 else if (attr_form_is_section_offset (attr))
7471 {
7472 dwarf2_complex_location_expr_complaint ();
7473 }
7474 else
7475 {
7476 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7477 fieldname);
7478 }
7479 }
7480 else
7481 {
7482 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7483 if (attr && DW_UNSND (attr))
7484 {
7485 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7486 complaint (&symfile_complaints,
7487 _("Member function \"%s\" (offset %d) is virtual "
7488 "but the vtable offset is not specified"),
7489 fieldname, die->offset);
7490 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7491 TYPE_CPLUS_DYNAMIC (type) = 1;
7492 }
7493 }
7494 }
7495
7496 /* Create the vector of member function fields, and attach it to the type. */
7497
7498 static void
7499 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7500 struct dwarf2_cu *cu)
7501 {
7502 struct fnfieldlist *flp;
7503 int total_length = 0;
7504 int i;
7505
7506 if (cu->language == language_ada)
7507 error (_("unexpected member functions in Ada type"));
7508
7509 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7510 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7511 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7512
7513 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7514 {
7515 struct nextfnfield *nfp = flp->head;
7516 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7517 int k;
7518
7519 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7520 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7521 fn_flp->fn_fields = (struct fn_field *)
7522 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7523 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7524 fn_flp->fn_fields[k] = nfp->fnfield;
7525
7526 total_length += flp->length;
7527 }
7528
7529 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7530 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7531 }
7532
7533 /* Returns non-zero if NAME is the name of a vtable member in CU's
7534 language, zero otherwise. */
7535 static int
7536 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7537 {
7538 static const char vptr[] = "_vptr";
7539 static const char vtable[] = "vtable";
7540
7541 /* Look for the C++ and Java forms of the vtable. */
7542 if ((cu->language == language_java
7543 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7544 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7545 && is_cplus_marker (name[sizeof (vptr) - 1])))
7546 return 1;
7547
7548 return 0;
7549 }
7550
7551 /* GCC outputs unnamed structures that are really pointers to member
7552 functions, with the ABI-specified layout. If TYPE describes
7553 such a structure, smash it into a member function type.
7554
7555 GCC shouldn't do this; it should just output pointer to member DIEs.
7556 This is GCC PR debug/28767. */
7557
7558 static void
7559 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7560 {
7561 struct type *pfn_type, *domain_type, *new_type;
7562
7563 /* Check for a structure with no name and two children. */
7564 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7565 return;
7566
7567 /* Check for __pfn and __delta members. */
7568 if (TYPE_FIELD_NAME (type, 0) == NULL
7569 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7570 || TYPE_FIELD_NAME (type, 1) == NULL
7571 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7572 return;
7573
7574 /* Find the type of the method. */
7575 pfn_type = TYPE_FIELD_TYPE (type, 0);
7576 if (pfn_type == NULL
7577 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7578 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7579 return;
7580
7581 /* Look for the "this" argument. */
7582 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7583 if (TYPE_NFIELDS (pfn_type) == 0
7584 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7585 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7586 return;
7587
7588 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7589 new_type = alloc_type (objfile);
7590 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7591 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7592 TYPE_VARARGS (pfn_type));
7593 smash_to_methodptr_type (type, new_type);
7594 }
7595
7596 /* Called when we find the DIE that starts a structure or union scope
7597 (definition) to create a type for the structure or union. Fill in
7598 the type's name and general properties; the members will not be
7599 processed until process_structure_type.
7600
7601 NOTE: we need to call these functions regardless of whether or not the
7602 DIE has a DW_AT_name attribute, since it might be an anonymous
7603 structure or union. This gets the type entered into our set of
7604 user defined types.
7605
7606 However, if the structure is incomplete (an opaque struct/union)
7607 then suppress creating a symbol table entry for it since gdb only
7608 wants to find the one with the complete definition. Note that if
7609 it is complete, we just call new_symbol, which does it's own
7610 checking about whether the struct/union is anonymous or not (and
7611 suppresses creating a symbol table entry itself). */
7612
7613 static struct type *
7614 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7615 {
7616 struct objfile *objfile = cu->objfile;
7617 struct type *type;
7618 struct attribute *attr;
7619 char *name;
7620
7621 /* If the definition of this type lives in .debug_types, read that type.
7622 Don't follow DW_AT_specification though, that will take us back up
7623 the chain and we want to go down. */
7624 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7625 if (attr)
7626 {
7627 struct dwarf2_cu *type_cu = cu;
7628 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7629
7630 /* We could just recurse on read_structure_type, but we need to call
7631 get_die_type to ensure only one type for this DIE is created.
7632 This is important, for example, because for c++ classes we need
7633 TYPE_NAME set which is only done by new_symbol. Blech. */
7634 type = read_type_die (type_die, type_cu);
7635
7636 /* TYPE_CU may not be the same as CU.
7637 Ensure TYPE is recorded in CU's type_hash table. */
7638 return set_die_type (die, type, cu);
7639 }
7640
7641 type = alloc_type (objfile);
7642 INIT_CPLUS_SPECIFIC (type);
7643
7644 name = dwarf2_name (die, cu);
7645 if (name != NULL)
7646 {
7647 if (cu->language == language_cplus
7648 || cu->language == language_java)
7649 {
7650 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7651
7652 /* dwarf2_full_name might have already finished building the DIE's
7653 type. If so, there is no need to continue. */
7654 if (get_die_type (die, cu) != NULL)
7655 return get_die_type (die, cu);
7656
7657 TYPE_TAG_NAME (type) = full_name;
7658 if (die->tag == DW_TAG_structure_type
7659 || die->tag == DW_TAG_class_type)
7660 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7661 }
7662 else
7663 {
7664 /* The name is already allocated along with this objfile, so
7665 we don't need to duplicate it for the type. */
7666 TYPE_TAG_NAME (type) = (char *) name;
7667 if (die->tag == DW_TAG_class_type)
7668 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7669 }
7670 }
7671
7672 if (die->tag == DW_TAG_structure_type)
7673 {
7674 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7675 }
7676 else if (die->tag == DW_TAG_union_type)
7677 {
7678 TYPE_CODE (type) = TYPE_CODE_UNION;
7679 }
7680 else
7681 {
7682 TYPE_CODE (type) = TYPE_CODE_CLASS;
7683 }
7684
7685 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7686 TYPE_DECLARED_CLASS (type) = 1;
7687
7688 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7689 if (attr)
7690 {
7691 TYPE_LENGTH (type) = DW_UNSND (attr);
7692 }
7693 else
7694 {
7695 TYPE_LENGTH (type) = 0;
7696 }
7697
7698 TYPE_STUB_SUPPORTED (type) = 1;
7699 if (die_is_declaration (die, cu))
7700 TYPE_STUB (type) = 1;
7701 else if (attr == NULL && die->child == NULL
7702 && producer_is_realview (cu->producer))
7703 /* RealView does not output the required DW_AT_declaration
7704 on incomplete types. */
7705 TYPE_STUB (type) = 1;
7706
7707 /* We need to add the type field to the die immediately so we don't
7708 infinitely recurse when dealing with pointers to the structure
7709 type within the structure itself. */
7710 set_die_type (die, type, cu);
7711
7712 /* set_die_type should be already done. */
7713 set_descriptive_type (type, die, cu);
7714
7715 return type;
7716 }
7717
7718 /* Finish creating a structure or union type, including filling in
7719 its members and creating a symbol for it. */
7720
7721 static void
7722 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7723 {
7724 struct objfile *objfile = cu->objfile;
7725 struct die_info *child_die = die->child;
7726 struct type *type;
7727
7728 type = get_die_type (die, cu);
7729 if (type == NULL)
7730 type = read_structure_type (die, cu);
7731
7732 if (die->child != NULL && ! die_is_declaration (die, cu))
7733 {
7734 struct field_info fi;
7735 struct die_info *child_die;
7736 VEC (symbolp) *template_args = NULL;
7737 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7738
7739 memset (&fi, 0, sizeof (struct field_info));
7740
7741 child_die = die->child;
7742
7743 while (child_die && child_die->tag)
7744 {
7745 if (child_die->tag == DW_TAG_member
7746 || child_die->tag == DW_TAG_variable)
7747 {
7748 /* NOTE: carlton/2002-11-05: A C++ static data member
7749 should be a DW_TAG_member that is a declaration, but
7750 all versions of G++ as of this writing (so through at
7751 least 3.2.1) incorrectly generate DW_TAG_variable
7752 tags for them instead. */
7753 dwarf2_add_field (&fi, child_die, cu);
7754 }
7755 else if (child_die->tag == DW_TAG_subprogram)
7756 {
7757 /* C++ member function. */
7758 dwarf2_add_member_fn (&fi, child_die, type, cu);
7759 }
7760 else if (child_die->tag == DW_TAG_inheritance)
7761 {
7762 /* C++ base class field. */
7763 dwarf2_add_field (&fi, child_die, cu);
7764 }
7765 else if (child_die->tag == DW_TAG_typedef)
7766 dwarf2_add_typedef (&fi, child_die, cu);
7767 else if (child_die->tag == DW_TAG_template_type_param
7768 || child_die->tag == DW_TAG_template_value_param)
7769 {
7770 struct symbol *arg = new_symbol (child_die, NULL, cu);
7771
7772 if (arg != NULL)
7773 VEC_safe_push (symbolp, template_args, arg);
7774 }
7775
7776 child_die = sibling_die (child_die);
7777 }
7778
7779 /* Attach template arguments to type. */
7780 if (! VEC_empty (symbolp, template_args))
7781 {
7782 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7783 TYPE_N_TEMPLATE_ARGUMENTS (type)
7784 = VEC_length (symbolp, template_args);
7785 TYPE_TEMPLATE_ARGUMENTS (type)
7786 = obstack_alloc (&objfile->objfile_obstack,
7787 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7788 * sizeof (struct symbol *)));
7789 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7790 VEC_address (symbolp, template_args),
7791 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7792 * sizeof (struct symbol *)));
7793 VEC_free (symbolp, template_args);
7794 }
7795
7796 /* Attach fields and member functions to the type. */
7797 if (fi.nfields)
7798 dwarf2_attach_fields_to_type (&fi, type, cu);
7799 if (fi.nfnfields)
7800 {
7801 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7802
7803 /* Get the type which refers to the base class (possibly this
7804 class itself) which contains the vtable pointer for the current
7805 class from the DW_AT_containing_type attribute. This use of
7806 DW_AT_containing_type is a GNU extension. */
7807
7808 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7809 {
7810 struct type *t = die_containing_type (die, cu);
7811
7812 TYPE_VPTR_BASETYPE (type) = t;
7813 if (type == t)
7814 {
7815 int i;
7816
7817 /* Our own class provides vtbl ptr. */
7818 for (i = TYPE_NFIELDS (t) - 1;
7819 i >= TYPE_N_BASECLASSES (t);
7820 --i)
7821 {
7822 char *fieldname = TYPE_FIELD_NAME (t, i);
7823
7824 if (is_vtable_name (fieldname, cu))
7825 {
7826 TYPE_VPTR_FIELDNO (type) = i;
7827 break;
7828 }
7829 }
7830
7831 /* Complain if virtual function table field not found. */
7832 if (i < TYPE_N_BASECLASSES (t))
7833 complaint (&symfile_complaints,
7834 _("virtual function table pointer "
7835 "not found when defining class '%s'"),
7836 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7837 "");
7838 }
7839 else
7840 {
7841 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7842 }
7843 }
7844 else if (cu->producer
7845 && strncmp (cu->producer,
7846 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7847 {
7848 /* The IBM XLC compiler does not provide direct indication
7849 of the containing type, but the vtable pointer is
7850 always named __vfp. */
7851
7852 int i;
7853
7854 for (i = TYPE_NFIELDS (type) - 1;
7855 i >= TYPE_N_BASECLASSES (type);
7856 --i)
7857 {
7858 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7859 {
7860 TYPE_VPTR_FIELDNO (type) = i;
7861 TYPE_VPTR_BASETYPE (type) = type;
7862 break;
7863 }
7864 }
7865 }
7866 }
7867
7868 /* Copy fi.typedef_field_list linked list elements content into the
7869 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7870 if (fi.typedef_field_list)
7871 {
7872 int i = fi.typedef_field_list_count;
7873
7874 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7875 TYPE_TYPEDEF_FIELD_ARRAY (type)
7876 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7877 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7878
7879 /* Reverse the list order to keep the debug info elements order. */
7880 while (--i >= 0)
7881 {
7882 struct typedef_field *dest, *src;
7883
7884 dest = &TYPE_TYPEDEF_FIELD (type, i);
7885 src = &fi.typedef_field_list->field;
7886 fi.typedef_field_list = fi.typedef_field_list->next;
7887 *dest = *src;
7888 }
7889 }
7890
7891 do_cleanups (back_to);
7892
7893 if (HAVE_CPLUS_STRUCT (type))
7894 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7895 }
7896
7897 quirk_gcc_member_function_pointer (type, cu->objfile);
7898
7899 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7900 snapshots) has been known to create a die giving a declaration
7901 for a class that has, as a child, a die giving a definition for a
7902 nested class. So we have to process our children even if the
7903 current die is a declaration. Normally, of course, a declaration
7904 won't have any children at all. */
7905
7906 while (child_die != NULL && child_die->tag)
7907 {
7908 if (child_die->tag == DW_TAG_member
7909 || child_die->tag == DW_TAG_variable
7910 || child_die->tag == DW_TAG_inheritance
7911 || child_die->tag == DW_TAG_template_value_param
7912 || child_die->tag == DW_TAG_template_type_param)
7913 {
7914 /* Do nothing. */
7915 }
7916 else
7917 process_die (child_die, cu);
7918
7919 child_die = sibling_die (child_die);
7920 }
7921
7922 /* Do not consider external references. According to the DWARF standard,
7923 these DIEs are identified by the fact that they have no byte_size
7924 attribute, and a declaration attribute. */
7925 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7926 || !die_is_declaration (die, cu))
7927 new_symbol (die, type, cu);
7928 }
7929
7930 /* Given a DW_AT_enumeration_type die, set its type. We do not
7931 complete the type's fields yet, or create any symbols. */
7932
7933 static struct type *
7934 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7935 {
7936 struct objfile *objfile = cu->objfile;
7937 struct type *type;
7938 struct attribute *attr;
7939 const char *name;
7940
7941 /* If the definition of this type lives in .debug_types, read that type.
7942 Don't follow DW_AT_specification though, that will take us back up
7943 the chain and we want to go down. */
7944 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7945 if (attr)
7946 {
7947 struct dwarf2_cu *type_cu = cu;
7948 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7949
7950 type = read_type_die (type_die, type_cu);
7951
7952 /* TYPE_CU may not be the same as CU.
7953 Ensure TYPE is recorded in CU's type_hash table. */
7954 return set_die_type (die, type, cu);
7955 }
7956
7957 type = alloc_type (objfile);
7958
7959 TYPE_CODE (type) = TYPE_CODE_ENUM;
7960 name = dwarf2_full_name (NULL, die, cu);
7961 if (name != NULL)
7962 TYPE_TAG_NAME (type) = (char *) name;
7963
7964 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7965 if (attr)
7966 {
7967 TYPE_LENGTH (type) = DW_UNSND (attr);
7968 }
7969 else
7970 {
7971 TYPE_LENGTH (type) = 0;
7972 }
7973
7974 /* The enumeration DIE can be incomplete. In Ada, any type can be
7975 declared as private in the package spec, and then defined only
7976 inside the package body. Such types are known as Taft Amendment
7977 Types. When another package uses such a type, an incomplete DIE
7978 may be generated by the compiler. */
7979 if (die_is_declaration (die, cu))
7980 TYPE_STUB (type) = 1;
7981
7982 return set_die_type (die, type, cu);
7983 }
7984
7985 /* Given a pointer to a die which begins an enumeration, process all
7986 the dies that define the members of the enumeration, and create the
7987 symbol for the enumeration type.
7988
7989 NOTE: We reverse the order of the element list. */
7990
7991 static void
7992 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7993 {
7994 struct type *this_type;
7995
7996 this_type = get_die_type (die, cu);
7997 if (this_type == NULL)
7998 this_type = read_enumeration_type (die, cu);
7999
8000 if (die->child != NULL)
8001 {
8002 struct die_info *child_die;
8003 struct symbol *sym;
8004 struct field *fields = NULL;
8005 int num_fields = 0;
8006 int unsigned_enum = 1;
8007 char *name;
8008
8009 child_die = die->child;
8010 while (child_die && child_die->tag)
8011 {
8012 if (child_die->tag != DW_TAG_enumerator)
8013 {
8014 process_die (child_die, cu);
8015 }
8016 else
8017 {
8018 name = dwarf2_name (child_die, cu);
8019 if (name)
8020 {
8021 sym = new_symbol (child_die, this_type, cu);
8022 if (SYMBOL_VALUE (sym) < 0)
8023 unsigned_enum = 0;
8024
8025 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8026 {
8027 fields = (struct field *)
8028 xrealloc (fields,
8029 (num_fields + DW_FIELD_ALLOC_CHUNK)
8030 * sizeof (struct field));
8031 }
8032
8033 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8034 FIELD_TYPE (fields[num_fields]) = NULL;
8035 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8036 FIELD_BITSIZE (fields[num_fields]) = 0;
8037
8038 num_fields++;
8039 }
8040 }
8041
8042 child_die = sibling_die (child_die);
8043 }
8044
8045 if (num_fields)
8046 {
8047 TYPE_NFIELDS (this_type) = num_fields;
8048 TYPE_FIELDS (this_type) = (struct field *)
8049 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8050 memcpy (TYPE_FIELDS (this_type), fields,
8051 sizeof (struct field) * num_fields);
8052 xfree (fields);
8053 }
8054 if (unsigned_enum)
8055 TYPE_UNSIGNED (this_type) = 1;
8056 }
8057
8058 /* If we are reading an enum from a .debug_types unit, and the enum
8059 is a declaration, and the enum is not the signatured type in the
8060 unit, then we do not want to add a symbol for it. Adding a
8061 symbol would in some cases obscure the true definition of the
8062 enum, giving users an incomplete type when the definition is
8063 actually available. Note that we do not want to do this for all
8064 enums which are just declarations, because C++0x allows forward
8065 enum declarations. */
8066 if (cu->per_cu->debug_types_section
8067 && die_is_declaration (die, cu))
8068 {
8069 struct signatured_type *type_sig;
8070
8071 type_sig
8072 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8073 cu->per_cu->debug_types_section,
8074 cu->per_cu->offset);
8075 if (type_sig->type_offset != die->offset)
8076 return;
8077 }
8078
8079 new_symbol (die, this_type, cu);
8080 }
8081
8082 /* Extract all information from a DW_TAG_array_type DIE and put it in
8083 the DIE's type field. For now, this only handles one dimensional
8084 arrays. */
8085
8086 static struct type *
8087 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8088 {
8089 struct objfile *objfile = cu->objfile;
8090 struct die_info *child_die;
8091 struct type *type;
8092 struct type *element_type, *range_type, *index_type;
8093 struct type **range_types = NULL;
8094 struct attribute *attr;
8095 int ndim = 0;
8096 struct cleanup *back_to;
8097 char *name;
8098
8099 element_type = die_type (die, cu);
8100
8101 /* The die_type call above may have already set the type for this DIE. */
8102 type = get_die_type (die, cu);
8103 if (type)
8104 return type;
8105
8106 /* Irix 6.2 native cc creates array types without children for
8107 arrays with unspecified length. */
8108 if (die->child == NULL)
8109 {
8110 index_type = objfile_type (objfile)->builtin_int;
8111 range_type = create_range_type (NULL, index_type, 0, -1);
8112 type = create_array_type (NULL, element_type, range_type);
8113 return set_die_type (die, type, cu);
8114 }
8115
8116 back_to = make_cleanup (null_cleanup, NULL);
8117 child_die = die->child;
8118 while (child_die && child_die->tag)
8119 {
8120 if (child_die->tag == DW_TAG_subrange_type)
8121 {
8122 struct type *child_type = read_type_die (child_die, cu);
8123
8124 if (child_type != NULL)
8125 {
8126 /* The range type was succesfully read. Save it for the
8127 array type creation. */
8128 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8129 {
8130 range_types = (struct type **)
8131 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8132 * sizeof (struct type *));
8133 if (ndim == 0)
8134 make_cleanup (free_current_contents, &range_types);
8135 }
8136 range_types[ndim++] = child_type;
8137 }
8138 }
8139 child_die = sibling_die (child_die);
8140 }
8141
8142 /* Dwarf2 dimensions are output from left to right, create the
8143 necessary array types in backwards order. */
8144
8145 type = element_type;
8146
8147 if (read_array_order (die, cu) == DW_ORD_col_major)
8148 {
8149 int i = 0;
8150
8151 while (i < ndim)
8152 type = create_array_type (NULL, type, range_types[i++]);
8153 }
8154 else
8155 {
8156 while (ndim-- > 0)
8157 type = create_array_type (NULL, type, range_types[ndim]);
8158 }
8159
8160 /* Understand Dwarf2 support for vector types (like they occur on
8161 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8162 array type. This is not part of the Dwarf2/3 standard yet, but a
8163 custom vendor extension. The main difference between a regular
8164 array and the vector variant is that vectors are passed by value
8165 to functions. */
8166 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8167 if (attr)
8168 make_vector_type (type);
8169
8170 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8171 implementation may choose to implement triple vectors using this
8172 attribute. */
8173 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8174 if (attr)
8175 {
8176 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8177 TYPE_LENGTH (type) = DW_UNSND (attr);
8178 else
8179 complaint (&symfile_complaints,
8180 _("DW_AT_byte_size for array type smaller "
8181 "than the total size of elements"));
8182 }
8183
8184 name = dwarf2_name (die, cu);
8185 if (name)
8186 TYPE_NAME (type) = name;
8187
8188 /* Install the type in the die. */
8189 set_die_type (die, type, cu);
8190
8191 /* set_die_type should be already done. */
8192 set_descriptive_type (type, die, cu);
8193
8194 do_cleanups (back_to);
8195
8196 return type;
8197 }
8198
8199 static enum dwarf_array_dim_ordering
8200 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8201 {
8202 struct attribute *attr;
8203
8204 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8205
8206 if (attr) return DW_SND (attr);
8207
8208 /* GNU F77 is a special case, as at 08/2004 array type info is the
8209 opposite order to the dwarf2 specification, but data is still
8210 laid out as per normal fortran.
8211
8212 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8213 version checking. */
8214
8215 if (cu->language == language_fortran
8216 && cu->producer && strstr (cu->producer, "GNU F77"))
8217 {
8218 return DW_ORD_row_major;
8219 }
8220
8221 switch (cu->language_defn->la_array_ordering)
8222 {
8223 case array_column_major:
8224 return DW_ORD_col_major;
8225 case array_row_major:
8226 default:
8227 return DW_ORD_row_major;
8228 };
8229 }
8230
8231 /* Extract all information from a DW_TAG_set_type DIE and put it in
8232 the DIE's type field. */
8233
8234 static struct type *
8235 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8236 {
8237 struct type *domain_type, *set_type;
8238 struct attribute *attr;
8239
8240 domain_type = die_type (die, cu);
8241
8242 /* The die_type call above may have already set the type for this DIE. */
8243 set_type = get_die_type (die, cu);
8244 if (set_type)
8245 return set_type;
8246
8247 set_type = create_set_type (NULL, domain_type);
8248
8249 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8250 if (attr)
8251 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8252
8253 return set_die_type (die, set_type, cu);
8254 }
8255
8256 /* First cut: install each common block member as a global variable. */
8257
8258 static void
8259 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8260 {
8261 struct die_info *child_die;
8262 struct attribute *attr;
8263 struct symbol *sym;
8264 CORE_ADDR base = (CORE_ADDR) 0;
8265
8266 attr = dwarf2_attr (die, DW_AT_location, cu);
8267 if (attr)
8268 {
8269 /* Support the .debug_loc offsets. */
8270 if (attr_form_is_block (attr))
8271 {
8272 base = decode_locdesc (DW_BLOCK (attr), cu);
8273 }
8274 else if (attr_form_is_section_offset (attr))
8275 {
8276 dwarf2_complex_location_expr_complaint ();
8277 }
8278 else
8279 {
8280 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8281 "common block member");
8282 }
8283 }
8284 if (die->child != NULL)
8285 {
8286 child_die = die->child;
8287 while (child_die && child_die->tag)
8288 {
8289 LONGEST offset;
8290
8291 sym = new_symbol (child_die, NULL, cu);
8292 if (sym != NULL
8293 && handle_data_member_location (child_die, cu, &offset))
8294 {
8295 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8296 add_symbol_to_list (sym, &global_symbols);
8297 }
8298 child_die = sibling_die (child_die);
8299 }
8300 }
8301 }
8302
8303 /* Create a type for a C++ namespace. */
8304
8305 static struct type *
8306 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8307 {
8308 struct objfile *objfile = cu->objfile;
8309 const char *previous_prefix, *name;
8310 int is_anonymous;
8311 struct type *type;
8312
8313 /* For extensions, reuse the type of the original namespace. */
8314 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8315 {
8316 struct die_info *ext_die;
8317 struct dwarf2_cu *ext_cu = cu;
8318
8319 ext_die = dwarf2_extension (die, &ext_cu);
8320 type = read_type_die (ext_die, ext_cu);
8321
8322 /* EXT_CU may not be the same as CU.
8323 Ensure TYPE is recorded in CU's type_hash table. */
8324 return set_die_type (die, type, cu);
8325 }
8326
8327 name = namespace_name (die, &is_anonymous, cu);
8328
8329 /* Now build the name of the current namespace. */
8330
8331 previous_prefix = determine_prefix (die, cu);
8332 if (previous_prefix[0] != '\0')
8333 name = typename_concat (&objfile->objfile_obstack,
8334 previous_prefix, name, 0, cu);
8335
8336 /* Create the type. */
8337 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8338 objfile);
8339 TYPE_NAME (type) = (char *) name;
8340 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8341
8342 return set_die_type (die, type, cu);
8343 }
8344
8345 /* Read a C++ namespace. */
8346
8347 static void
8348 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8349 {
8350 struct objfile *objfile = cu->objfile;
8351 int is_anonymous;
8352
8353 /* Add a symbol associated to this if we haven't seen the namespace
8354 before. Also, add a using directive if it's an anonymous
8355 namespace. */
8356
8357 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8358 {
8359 struct type *type;
8360
8361 type = read_type_die (die, cu);
8362 new_symbol (die, type, cu);
8363
8364 namespace_name (die, &is_anonymous, cu);
8365 if (is_anonymous)
8366 {
8367 const char *previous_prefix = determine_prefix (die, cu);
8368
8369 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8370 NULL, NULL, &objfile->objfile_obstack);
8371 }
8372 }
8373
8374 if (die->child != NULL)
8375 {
8376 struct die_info *child_die = die->child;
8377
8378 while (child_die && child_die->tag)
8379 {
8380 process_die (child_die, cu);
8381 child_die = sibling_die (child_die);
8382 }
8383 }
8384 }
8385
8386 /* Read a Fortran module as type. This DIE can be only a declaration used for
8387 imported module. Still we need that type as local Fortran "use ... only"
8388 declaration imports depend on the created type in determine_prefix. */
8389
8390 static struct type *
8391 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8392 {
8393 struct objfile *objfile = cu->objfile;
8394 char *module_name;
8395 struct type *type;
8396
8397 module_name = dwarf2_name (die, cu);
8398 if (!module_name)
8399 complaint (&symfile_complaints,
8400 _("DW_TAG_module has no name, offset 0x%x"),
8401 die->offset);
8402 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8403
8404 /* determine_prefix uses TYPE_TAG_NAME. */
8405 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8406
8407 return set_die_type (die, type, cu);
8408 }
8409
8410 /* Read a Fortran module. */
8411
8412 static void
8413 read_module (struct die_info *die, struct dwarf2_cu *cu)
8414 {
8415 struct die_info *child_die = die->child;
8416
8417 while (child_die && child_die->tag)
8418 {
8419 process_die (child_die, cu);
8420 child_die = sibling_die (child_die);
8421 }
8422 }
8423
8424 /* Return the name of the namespace represented by DIE. Set
8425 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8426 namespace. */
8427
8428 static const char *
8429 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8430 {
8431 struct die_info *current_die;
8432 const char *name = NULL;
8433
8434 /* Loop through the extensions until we find a name. */
8435
8436 for (current_die = die;
8437 current_die != NULL;
8438 current_die = dwarf2_extension (die, &cu))
8439 {
8440 name = dwarf2_name (current_die, cu);
8441 if (name != NULL)
8442 break;
8443 }
8444
8445 /* Is it an anonymous namespace? */
8446
8447 *is_anonymous = (name == NULL);
8448 if (*is_anonymous)
8449 name = CP_ANONYMOUS_NAMESPACE_STR;
8450
8451 return name;
8452 }
8453
8454 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8455 the user defined type vector. */
8456
8457 static struct type *
8458 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8459 {
8460 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8461 struct comp_unit_head *cu_header = &cu->header;
8462 struct type *type;
8463 struct attribute *attr_byte_size;
8464 struct attribute *attr_address_class;
8465 int byte_size, addr_class;
8466 struct type *target_type;
8467
8468 target_type = die_type (die, cu);
8469
8470 /* The die_type call above may have already set the type for this DIE. */
8471 type = get_die_type (die, cu);
8472 if (type)
8473 return type;
8474
8475 type = lookup_pointer_type (target_type);
8476
8477 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8478 if (attr_byte_size)
8479 byte_size = DW_UNSND (attr_byte_size);
8480 else
8481 byte_size = cu_header->addr_size;
8482
8483 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8484 if (attr_address_class)
8485 addr_class = DW_UNSND (attr_address_class);
8486 else
8487 addr_class = DW_ADDR_none;
8488
8489 /* If the pointer size or address class is different than the
8490 default, create a type variant marked as such and set the
8491 length accordingly. */
8492 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8493 {
8494 if (gdbarch_address_class_type_flags_p (gdbarch))
8495 {
8496 int type_flags;
8497
8498 type_flags = gdbarch_address_class_type_flags
8499 (gdbarch, byte_size, addr_class);
8500 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8501 == 0);
8502 type = make_type_with_address_space (type, type_flags);
8503 }
8504 else if (TYPE_LENGTH (type) != byte_size)
8505 {
8506 complaint (&symfile_complaints,
8507 _("invalid pointer size %d"), byte_size);
8508 }
8509 else
8510 {
8511 /* Should we also complain about unhandled address classes? */
8512 }
8513 }
8514
8515 TYPE_LENGTH (type) = byte_size;
8516 return set_die_type (die, type, cu);
8517 }
8518
8519 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8520 the user defined type vector. */
8521
8522 static struct type *
8523 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8524 {
8525 struct type *type;
8526 struct type *to_type;
8527 struct type *domain;
8528
8529 to_type = die_type (die, cu);
8530 domain = die_containing_type (die, cu);
8531
8532 /* The calls above may have already set the type for this DIE. */
8533 type = get_die_type (die, cu);
8534 if (type)
8535 return type;
8536
8537 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8538 type = lookup_methodptr_type (to_type);
8539 else
8540 type = lookup_memberptr_type (to_type, domain);
8541
8542 return set_die_type (die, type, cu);
8543 }
8544
8545 /* Extract all information from a DW_TAG_reference_type DIE and add to
8546 the user defined type vector. */
8547
8548 static struct type *
8549 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8550 {
8551 struct comp_unit_head *cu_header = &cu->header;
8552 struct type *type, *target_type;
8553 struct attribute *attr;
8554
8555 target_type = die_type (die, cu);
8556
8557 /* The die_type call above may have already set the type for this DIE. */
8558 type = get_die_type (die, cu);
8559 if (type)
8560 return type;
8561
8562 type = lookup_reference_type (target_type);
8563 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8564 if (attr)
8565 {
8566 TYPE_LENGTH (type) = DW_UNSND (attr);
8567 }
8568 else
8569 {
8570 TYPE_LENGTH (type) = cu_header->addr_size;
8571 }
8572 return set_die_type (die, type, cu);
8573 }
8574
8575 static struct type *
8576 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8577 {
8578 struct type *base_type, *cv_type;
8579
8580 base_type = die_type (die, cu);
8581
8582 /* The die_type call above may have already set the type for this DIE. */
8583 cv_type = get_die_type (die, cu);
8584 if (cv_type)
8585 return cv_type;
8586
8587 /* In case the const qualifier is applied to an array type, the element type
8588 is so qualified, not the array type (section 6.7.3 of C99). */
8589 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8590 {
8591 struct type *el_type, *inner_array;
8592
8593 base_type = copy_type (base_type);
8594 inner_array = base_type;
8595
8596 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8597 {
8598 TYPE_TARGET_TYPE (inner_array) =
8599 copy_type (TYPE_TARGET_TYPE (inner_array));
8600 inner_array = TYPE_TARGET_TYPE (inner_array);
8601 }
8602
8603 el_type = TYPE_TARGET_TYPE (inner_array);
8604 TYPE_TARGET_TYPE (inner_array) =
8605 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8606
8607 return set_die_type (die, base_type, cu);
8608 }
8609
8610 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8611 return set_die_type (die, cv_type, cu);
8612 }
8613
8614 static struct type *
8615 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8616 {
8617 struct type *base_type, *cv_type;
8618
8619 base_type = die_type (die, cu);
8620
8621 /* The die_type call above may have already set the type for this DIE. */
8622 cv_type = get_die_type (die, cu);
8623 if (cv_type)
8624 return cv_type;
8625
8626 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8627 return set_die_type (die, cv_type, cu);
8628 }
8629
8630 /* Extract all information from a DW_TAG_string_type DIE and add to
8631 the user defined type vector. It isn't really a user defined type,
8632 but it behaves like one, with other DIE's using an AT_user_def_type
8633 attribute to reference it. */
8634
8635 static struct type *
8636 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8637 {
8638 struct objfile *objfile = cu->objfile;
8639 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8640 struct type *type, *range_type, *index_type, *char_type;
8641 struct attribute *attr;
8642 unsigned int length;
8643
8644 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8645 if (attr)
8646 {
8647 length = DW_UNSND (attr);
8648 }
8649 else
8650 {
8651 /* Check for the DW_AT_byte_size attribute. */
8652 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8653 if (attr)
8654 {
8655 length = DW_UNSND (attr);
8656 }
8657 else
8658 {
8659 length = 1;
8660 }
8661 }
8662
8663 index_type = objfile_type (objfile)->builtin_int;
8664 range_type = create_range_type (NULL, index_type, 1, length);
8665 char_type = language_string_char_type (cu->language_defn, gdbarch);
8666 type = create_string_type (NULL, char_type, range_type);
8667
8668 return set_die_type (die, type, cu);
8669 }
8670
8671 /* Handle DIES due to C code like:
8672
8673 struct foo
8674 {
8675 int (*funcp)(int a, long l);
8676 int b;
8677 };
8678
8679 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8680
8681 static struct type *
8682 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8683 {
8684 struct type *type; /* Type that this function returns. */
8685 struct type *ftype; /* Function that returns above type. */
8686 struct attribute *attr;
8687
8688 type = die_type (die, cu);
8689
8690 /* The die_type call above may have already set the type for this DIE. */
8691 ftype = get_die_type (die, cu);
8692 if (ftype)
8693 return ftype;
8694
8695 ftype = lookup_function_type (type);
8696
8697 /* All functions in C++, Pascal and Java have prototypes. */
8698 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8699 if ((attr && (DW_UNSND (attr) != 0))
8700 || cu->language == language_cplus
8701 || cu->language == language_java
8702 || cu->language == language_pascal)
8703 TYPE_PROTOTYPED (ftype) = 1;
8704 else if (producer_is_realview (cu->producer))
8705 /* RealView does not emit DW_AT_prototyped. We can not
8706 distinguish prototyped and unprototyped functions; default to
8707 prototyped, since that is more common in modern code (and
8708 RealView warns about unprototyped functions). */
8709 TYPE_PROTOTYPED (ftype) = 1;
8710
8711 /* Store the calling convention in the type if it's available in
8712 the subroutine die. Otherwise set the calling convention to
8713 the default value DW_CC_normal. */
8714 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8715 if (attr)
8716 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8717 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8718 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8719 else
8720 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8721
8722 /* We need to add the subroutine type to the die immediately so
8723 we don't infinitely recurse when dealing with parameters
8724 declared as the same subroutine type. */
8725 set_die_type (die, ftype, cu);
8726
8727 if (die->child != NULL)
8728 {
8729 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8730 struct die_info *child_die;
8731 int nparams, iparams;
8732
8733 /* Count the number of parameters.
8734 FIXME: GDB currently ignores vararg functions, but knows about
8735 vararg member functions. */
8736 nparams = 0;
8737 child_die = die->child;
8738 while (child_die && child_die->tag)
8739 {
8740 if (child_die->tag == DW_TAG_formal_parameter)
8741 nparams++;
8742 else if (child_die->tag == DW_TAG_unspecified_parameters)
8743 TYPE_VARARGS (ftype) = 1;
8744 child_die = sibling_die (child_die);
8745 }
8746
8747 /* Allocate storage for parameters and fill them in. */
8748 TYPE_NFIELDS (ftype) = nparams;
8749 TYPE_FIELDS (ftype) = (struct field *)
8750 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8751
8752 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8753 even if we error out during the parameters reading below. */
8754 for (iparams = 0; iparams < nparams; iparams++)
8755 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8756
8757 iparams = 0;
8758 child_die = die->child;
8759 while (child_die && child_die->tag)
8760 {
8761 if (child_die->tag == DW_TAG_formal_parameter)
8762 {
8763 struct type *arg_type;
8764
8765 /* DWARF version 2 has no clean way to discern C++
8766 static and non-static member functions. G++ helps
8767 GDB by marking the first parameter for non-static
8768 member functions (which is the this pointer) as
8769 artificial. We pass this information to
8770 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8771
8772 DWARF version 3 added DW_AT_object_pointer, which GCC
8773 4.5 does not yet generate. */
8774 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8775 if (attr)
8776 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8777 else
8778 {
8779 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8780
8781 /* GCC/43521: In java, the formal parameter
8782 "this" is sometimes not marked with DW_AT_artificial. */
8783 if (cu->language == language_java)
8784 {
8785 const char *name = dwarf2_name (child_die, cu);
8786
8787 if (name && !strcmp (name, "this"))
8788 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8789 }
8790 }
8791 arg_type = die_type (child_die, cu);
8792
8793 /* RealView does not mark THIS as const, which the testsuite
8794 expects. GCC marks THIS as const in method definitions,
8795 but not in the class specifications (GCC PR 43053). */
8796 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8797 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8798 {
8799 int is_this = 0;
8800 struct dwarf2_cu *arg_cu = cu;
8801 const char *name = dwarf2_name (child_die, cu);
8802
8803 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8804 if (attr)
8805 {
8806 /* If the compiler emits this, use it. */
8807 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8808 is_this = 1;
8809 }
8810 else if (name && strcmp (name, "this") == 0)
8811 /* Function definitions will have the argument names. */
8812 is_this = 1;
8813 else if (name == NULL && iparams == 0)
8814 /* Declarations may not have the names, so like
8815 elsewhere in GDB, assume an artificial first
8816 argument is "this". */
8817 is_this = 1;
8818
8819 if (is_this)
8820 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8821 arg_type, 0);
8822 }
8823
8824 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8825 iparams++;
8826 }
8827 child_die = sibling_die (child_die);
8828 }
8829 }
8830
8831 return ftype;
8832 }
8833
8834 static struct type *
8835 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8836 {
8837 struct objfile *objfile = cu->objfile;
8838 const char *name = NULL;
8839 struct type *this_type;
8840
8841 name = dwarf2_full_name (NULL, die, cu);
8842 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8843 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8844 TYPE_NAME (this_type) = (char *) name;
8845 set_die_type (die, this_type, cu);
8846 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8847 return this_type;
8848 }
8849
8850 /* Find a representation of a given base type and install
8851 it in the TYPE field of the die. */
8852
8853 static struct type *
8854 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8855 {
8856 struct objfile *objfile = cu->objfile;
8857 struct type *type;
8858 struct attribute *attr;
8859 int encoding = 0, size = 0;
8860 char *name;
8861 enum type_code code = TYPE_CODE_INT;
8862 int type_flags = 0;
8863 struct type *target_type = NULL;
8864
8865 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8866 if (attr)
8867 {
8868 encoding = DW_UNSND (attr);
8869 }
8870 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8871 if (attr)
8872 {
8873 size = DW_UNSND (attr);
8874 }
8875 name = dwarf2_name (die, cu);
8876 if (!name)
8877 {
8878 complaint (&symfile_complaints,
8879 _("DW_AT_name missing from DW_TAG_base_type"));
8880 }
8881
8882 switch (encoding)
8883 {
8884 case DW_ATE_address:
8885 /* Turn DW_ATE_address into a void * pointer. */
8886 code = TYPE_CODE_PTR;
8887 type_flags |= TYPE_FLAG_UNSIGNED;
8888 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8889 break;
8890 case DW_ATE_boolean:
8891 code = TYPE_CODE_BOOL;
8892 type_flags |= TYPE_FLAG_UNSIGNED;
8893 break;
8894 case DW_ATE_complex_float:
8895 code = TYPE_CODE_COMPLEX;
8896 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8897 break;
8898 case DW_ATE_decimal_float:
8899 code = TYPE_CODE_DECFLOAT;
8900 break;
8901 case DW_ATE_float:
8902 code = TYPE_CODE_FLT;
8903 break;
8904 case DW_ATE_signed:
8905 break;
8906 case DW_ATE_unsigned:
8907 type_flags |= TYPE_FLAG_UNSIGNED;
8908 if (cu->language == language_fortran
8909 && name
8910 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8911 code = TYPE_CODE_CHAR;
8912 break;
8913 case DW_ATE_signed_char:
8914 if (cu->language == language_ada || cu->language == language_m2
8915 || cu->language == language_pascal
8916 || cu->language == language_fortran)
8917 code = TYPE_CODE_CHAR;
8918 break;
8919 case DW_ATE_unsigned_char:
8920 if (cu->language == language_ada || cu->language == language_m2
8921 || cu->language == language_pascal
8922 || cu->language == language_fortran)
8923 code = TYPE_CODE_CHAR;
8924 type_flags |= TYPE_FLAG_UNSIGNED;
8925 break;
8926 case DW_ATE_UTF:
8927 /* We just treat this as an integer and then recognize the
8928 type by name elsewhere. */
8929 break;
8930
8931 default:
8932 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8933 dwarf_type_encoding_name (encoding));
8934 break;
8935 }
8936
8937 type = init_type (code, size, type_flags, NULL, objfile);
8938 TYPE_NAME (type) = name;
8939 TYPE_TARGET_TYPE (type) = target_type;
8940
8941 if (name && strcmp (name, "char") == 0)
8942 TYPE_NOSIGN (type) = 1;
8943
8944 return set_die_type (die, type, cu);
8945 }
8946
8947 /* Read the given DW_AT_subrange DIE. */
8948
8949 static struct type *
8950 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8951 {
8952 struct type *base_type;
8953 struct type *range_type;
8954 struct attribute *attr;
8955 LONGEST low = 0;
8956 LONGEST high = -1;
8957 char *name;
8958 LONGEST negative_mask;
8959
8960 base_type = die_type (die, cu);
8961 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8962 check_typedef (base_type);
8963
8964 /* The die_type call above may have already set the type for this DIE. */
8965 range_type = get_die_type (die, cu);
8966 if (range_type)
8967 return range_type;
8968
8969 if (cu->language == language_fortran)
8970 {
8971 /* FORTRAN implies a lower bound of 1, if not given. */
8972 low = 1;
8973 }
8974
8975 /* FIXME: For variable sized arrays either of these could be
8976 a variable rather than a constant value. We'll allow it,
8977 but we don't know how to handle it. */
8978 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8979 if (attr)
8980 low = dwarf2_get_attr_constant_value (attr, 0);
8981
8982 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8983 if (attr)
8984 {
8985 if (attr_form_is_block (attr) || is_ref_attr (attr))
8986 {
8987 /* GCC encodes arrays with unspecified or dynamic length
8988 with a DW_FORM_block1 attribute or a reference attribute.
8989 FIXME: GDB does not yet know how to handle dynamic
8990 arrays properly, treat them as arrays with unspecified
8991 length for now.
8992
8993 FIXME: jimb/2003-09-22: GDB does not really know
8994 how to handle arrays of unspecified length
8995 either; we just represent them as zero-length
8996 arrays. Choose an appropriate upper bound given
8997 the lower bound we've computed above. */
8998 high = low - 1;
8999 }
9000 else
9001 high = dwarf2_get_attr_constant_value (attr, 1);
9002 }
9003 else
9004 {
9005 attr = dwarf2_attr (die, DW_AT_count, cu);
9006 if (attr)
9007 {
9008 int count = dwarf2_get_attr_constant_value (attr, 1);
9009 high = low + count - 1;
9010 }
9011 else
9012 {
9013 /* Unspecified array length. */
9014 high = low - 1;
9015 }
9016 }
9017
9018 /* Dwarf-2 specifications explicitly allows to create subrange types
9019 without specifying a base type.
9020 In that case, the base type must be set to the type of
9021 the lower bound, upper bound or count, in that order, if any of these
9022 three attributes references an object that has a type.
9023 If no base type is found, the Dwarf-2 specifications say that
9024 a signed integer type of size equal to the size of an address should
9025 be used.
9026 For the following C code: `extern char gdb_int [];'
9027 GCC produces an empty range DIE.
9028 FIXME: muller/2010-05-28: Possible references to object for low bound,
9029 high bound or count are not yet handled by this code. */
9030 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9031 {
9032 struct objfile *objfile = cu->objfile;
9033 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9034 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9035 struct type *int_type = objfile_type (objfile)->builtin_int;
9036
9037 /* Test "int", "long int", and "long long int" objfile types,
9038 and select the first one having a size above or equal to the
9039 architecture address size. */
9040 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9041 base_type = int_type;
9042 else
9043 {
9044 int_type = objfile_type (objfile)->builtin_long;
9045 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9046 base_type = int_type;
9047 else
9048 {
9049 int_type = objfile_type (objfile)->builtin_long_long;
9050 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9051 base_type = int_type;
9052 }
9053 }
9054 }
9055
9056 negative_mask =
9057 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9058 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9059 low |= negative_mask;
9060 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9061 high |= negative_mask;
9062
9063 range_type = create_range_type (NULL, base_type, low, high);
9064
9065 /* Mark arrays with dynamic length at least as an array of unspecified
9066 length. GDB could check the boundary but before it gets implemented at
9067 least allow accessing the array elements. */
9068 if (attr && attr_form_is_block (attr))
9069 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9070
9071 /* Ada expects an empty array on no boundary attributes. */
9072 if (attr == NULL && cu->language != language_ada)
9073 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9074
9075 name = dwarf2_name (die, cu);
9076 if (name)
9077 TYPE_NAME (range_type) = name;
9078
9079 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9080 if (attr)
9081 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9082
9083 set_die_type (die, range_type, cu);
9084
9085 /* set_die_type should be already done. */
9086 set_descriptive_type (range_type, die, cu);
9087
9088 return range_type;
9089 }
9090
9091 static struct type *
9092 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9093 {
9094 struct type *type;
9095
9096 /* For now, we only support the C meaning of an unspecified type: void. */
9097
9098 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9099 TYPE_NAME (type) = dwarf2_name (die, cu);
9100
9101 return set_die_type (die, type, cu);
9102 }
9103
9104 /* Trivial hash function for die_info: the hash value of a DIE
9105 is its offset in .debug_info for this objfile. */
9106
9107 static hashval_t
9108 die_hash (const void *item)
9109 {
9110 const struct die_info *die = item;
9111
9112 return die->offset;
9113 }
9114
9115 /* Trivial comparison function for die_info structures: two DIEs
9116 are equal if they have the same offset. */
9117
9118 static int
9119 die_eq (const void *item_lhs, const void *item_rhs)
9120 {
9121 const struct die_info *die_lhs = item_lhs;
9122 const struct die_info *die_rhs = item_rhs;
9123
9124 return die_lhs->offset == die_rhs->offset;
9125 }
9126
9127 /* Read a whole compilation unit into a linked list of dies. */
9128
9129 static struct die_info *
9130 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9131 {
9132 struct die_reader_specs reader_specs;
9133 int read_abbrevs = 0;
9134 struct cleanup *back_to = NULL;
9135 struct die_info *die;
9136
9137 if (cu->dwarf2_abbrevs == NULL)
9138 {
9139 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9140 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9141 read_abbrevs = 1;
9142 }
9143
9144 gdb_assert (cu->die_hash == NULL);
9145 cu->die_hash
9146 = htab_create_alloc_ex (cu->header.length / 12,
9147 die_hash,
9148 die_eq,
9149 NULL,
9150 &cu->comp_unit_obstack,
9151 hashtab_obstack_allocate,
9152 dummy_obstack_deallocate);
9153
9154 init_cu_die_reader (&reader_specs, cu);
9155
9156 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9157
9158 if (read_abbrevs)
9159 do_cleanups (back_to);
9160
9161 return die;
9162 }
9163
9164 /* Main entry point for reading a DIE and all children.
9165 Read the DIE and dump it if requested. */
9166
9167 static struct die_info *
9168 read_die_and_children (const struct die_reader_specs *reader,
9169 gdb_byte *info_ptr,
9170 gdb_byte **new_info_ptr,
9171 struct die_info *parent)
9172 {
9173 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9174 new_info_ptr, parent);
9175
9176 if (dwarf2_die_debug)
9177 {
9178 fprintf_unfiltered (gdb_stdlog,
9179 "\nRead die from %s of %s:\n",
9180 (reader->cu->per_cu->debug_types_section
9181 ? ".debug_types"
9182 : ".debug_info"),
9183 reader->abfd->filename);
9184 dump_die (result, dwarf2_die_debug);
9185 }
9186
9187 return result;
9188 }
9189
9190 /* Read a single die and all its descendents. Set the die's sibling
9191 field to NULL; set other fields in the die correctly, and set all
9192 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9193 location of the info_ptr after reading all of those dies. PARENT
9194 is the parent of the die in question. */
9195
9196 static struct die_info *
9197 read_die_and_children_1 (const struct die_reader_specs *reader,
9198 gdb_byte *info_ptr,
9199 gdb_byte **new_info_ptr,
9200 struct die_info *parent)
9201 {
9202 struct die_info *die;
9203 gdb_byte *cur_ptr;
9204 int has_children;
9205
9206 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9207 if (die == NULL)
9208 {
9209 *new_info_ptr = cur_ptr;
9210 return NULL;
9211 }
9212 store_in_ref_table (die, reader->cu);
9213
9214 if (has_children)
9215 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9216 else
9217 {
9218 die->child = NULL;
9219 *new_info_ptr = cur_ptr;
9220 }
9221
9222 die->sibling = NULL;
9223 die->parent = parent;
9224 return die;
9225 }
9226
9227 /* Read a die, all of its descendents, and all of its siblings; set
9228 all of the fields of all of the dies correctly. Arguments are as
9229 in read_die_and_children. */
9230
9231 static struct die_info *
9232 read_die_and_siblings (const struct die_reader_specs *reader,
9233 gdb_byte *info_ptr,
9234 gdb_byte **new_info_ptr,
9235 struct die_info *parent)
9236 {
9237 struct die_info *first_die, *last_sibling;
9238 gdb_byte *cur_ptr;
9239
9240 cur_ptr = info_ptr;
9241 first_die = last_sibling = NULL;
9242
9243 while (1)
9244 {
9245 struct die_info *die
9246 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9247
9248 if (die == NULL)
9249 {
9250 *new_info_ptr = cur_ptr;
9251 return first_die;
9252 }
9253
9254 if (!first_die)
9255 first_die = die;
9256 else
9257 last_sibling->sibling = die;
9258
9259 last_sibling = die;
9260 }
9261 }
9262
9263 /* Read the die from the .debug_info section buffer. Set DIEP to
9264 point to a newly allocated die with its information, except for its
9265 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9266 whether the die has children or not. */
9267
9268 static gdb_byte *
9269 read_full_die (const struct die_reader_specs *reader,
9270 struct die_info **diep, gdb_byte *info_ptr,
9271 int *has_children)
9272 {
9273 unsigned int abbrev_number, bytes_read, i, offset;
9274 struct abbrev_info *abbrev;
9275 struct die_info *die;
9276 struct dwarf2_cu *cu = reader->cu;
9277 bfd *abfd = reader->abfd;
9278
9279 offset = info_ptr - reader->buffer;
9280 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9281 info_ptr += bytes_read;
9282 if (!abbrev_number)
9283 {
9284 *diep = NULL;
9285 *has_children = 0;
9286 return info_ptr;
9287 }
9288
9289 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9290 if (!abbrev)
9291 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9292 abbrev_number,
9293 bfd_get_filename (abfd));
9294
9295 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9296 die->offset = offset;
9297 die->tag = abbrev->tag;
9298 die->abbrev = abbrev_number;
9299
9300 die->num_attrs = abbrev->num_attrs;
9301
9302 for (i = 0; i < abbrev->num_attrs; ++i)
9303 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9304 abfd, info_ptr, cu);
9305
9306 *diep = die;
9307 *has_children = abbrev->has_children;
9308 return info_ptr;
9309 }
9310
9311 /* In DWARF version 2, the description of the debugging information is
9312 stored in a separate .debug_abbrev section. Before we read any
9313 dies from a section we read in all abbreviations and install them
9314 in a hash table. This function also sets flags in CU describing
9315 the data found in the abbrev table. */
9316
9317 static void
9318 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
9319 {
9320 struct comp_unit_head *cu_header = &cu->header;
9321 gdb_byte *abbrev_ptr;
9322 struct abbrev_info *cur_abbrev;
9323 unsigned int abbrev_number, bytes_read, abbrev_name;
9324 unsigned int abbrev_form, hash_number;
9325 struct attr_abbrev *cur_attrs;
9326 unsigned int allocated_attrs;
9327
9328 /* Initialize dwarf2 abbrevs. */
9329 obstack_init (&cu->abbrev_obstack);
9330 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9331 (ABBREV_HASH_SIZE
9332 * sizeof (struct abbrev_info *)));
9333 memset (cu->dwarf2_abbrevs, 0,
9334 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9335
9336 dwarf2_read_section (dwarf2_per_objfile->objfile,
9337 &dwarf2_per_objfile->abbrev);
9338 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9339 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9340 abbrev_ptr += bytes_read;
9341
9342 allocated_attrs = ATTR_ALLOC_CHUNK;
9343 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9344
9345 /* Loop until we reach an abbrev number of 0. */
9346 while (abbrev_number)
9347 {
9348 cur_abbrev = dwarf_alloc_abbrev (cu);
9349
9350 /* read in abbrev header */
9351 cur_abbrev->number = abbrev_number;
9352 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9353 abbrev_ptr += bytes_read;
9354 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9355 abbrev_ptr += 1;
9356
9357 if (cur_abbrev->tag == DW_TAG_namespace)
9358 cu->has_namespace_info = 1;
9359
9360 /* now read in declarations */
9361 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9362 abbrev_ptr += bytes_read;
9363 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9364 abbrev_ptr += bytes_read;
9365 while (abbrev_name)
9366 {
9367 if (cur_abbrev->num_attrs == allocated_attrs)
9368 {
9369 allocated_attrs += ATTR_ALLOC_CHUNK;
9370 cur_attrs
9371 = xrealloc (cur_attrs, (allocated_attrs
9372 * sizeof (struct attr_abbrev)));
9373 }
9374
9375 /* Record whether this compilation unit might have
9376 inter-compilation-unit references. If we don't know what form
9377 this attribute will have, then it might potentially be a
9378 DW_FORM_ref_addr, so we conservatively expect inter-CU
9379 references. */
9380
9381 if (abbrev_form == DW_FORM_ref_addr
9382 || abbrev_form == DW_FORM_indirect)
9383 cu->has_form_ref_addr = 1;
9384
9385 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9386 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9387 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9388 abbrev_ptr += bytes_read;
9389 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9390 abbrev_ptr += bytes_read;
9391 }
9392
9393 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9394 (cur_abbrev->num_attrs
9395 * sizeof (struct attr_abbrev)));
9396 memcpy (cur_abbrev->attrs, cur_attrs,
9397 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9398
9399 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9400 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9401 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9402
9403 /* Get next abbreviation.
9404 Under Irix6 the abbreviations for a compilation unit are not
9405 always properly terminated with an abbrev number of 0.
9406 Exit loop if we encounter an abbreviation which we have
9407 already read (which means we are about to read the abbreviations
9408 for the next compile unit) or if the end of the abbreviation
9409 table is reached. */
9410 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9411 >= dwarf2_per_objfile->abbrev.size)
9412 break;
9413 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9414 abbrev_ptr += bytes_read;
9415 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9416 break;
9417 }
9418
9419 xfree (cur_attrs);
9420 }
9421
9422 /* Release the memory used by the abbrev table for a compilation unit. */
9423
9424 static void
9425 dwarf2_free_abbrev_table (void *ptr_to_cu)
9426 {
9427 struct dwarf2_cu *cu = ptr_to_cu;
9428
9429 obstack_free (&cu->abbrev_obstack, NULL);
9430 cu->dwarf2_abbrevs = NULL;
9431 }
9432
9433 /* Lookup an abbrev_info structure in the abbrev hash table. */
9434
9435 static struct abbrev_info *
9436 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9437 {
9438 unsigned int hash_number;
9439 struct abbrev_info *abbrev;
9440
9441 hash_number = number % ABBREV_HASH_SIZE;
9442 abbrev = cu->dwarf2_abbrevs[hash_number];
9443
9444 while (abbrev)
9445 {
9446 if (abbrev->number == number)
9447 return abbrev;
9448 else
9449 abbrev = abbrev->next;
9450 }
9451 return NULL;
9452 }
9453
9454 /* Returns nonzero if TAG represents a type that we might generate a partial
9455 symbol for. */
9456
9457 static int
9458 is_type_tag_for_partial (int tag)
9459 {
9460 switch (tag)
9461 {
9462 #if 0
9463 /* Some types that would be reasonable to generate partial symbols for,
9464 that we don't at present. */
9465 case DW_TAG_array_type:
9466 case DW_TAG_file_type:
9467 case DW_TAG_ptr_to_member_type:
9468 case DW_TAG_set_type:
9469 case DW_TAG_string_type:
9470 case DW_TAG_subroutine_type:
9471 #endif
9472 case DW_TAG_base_type:
9473 case DW_TAG_class_type:
9474 case DW_TAG_interface_type:
9475 case DW_TAG_enumeration_type:
9476 case DW_TAG_structure_type:
9477 case DW_TAG_subrange_type:
9478 case DW_TAG_typedef:
9479 case DW_TAG_union_type:
9480 return 1;
9481 default:
9482 return 0;
9483 }
9484 }
9485
9486 /* Load all DIEs that are interesting for partial symbols into memory. */
9487
9488 static struct partial_die_info *
9489 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9490 int building_psymtab, struct dwarf2_cu *cu)
9491 {
9492 struct partial_die_info *part_die;
9493 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9494 struct abbrev_info *abbrev;
9495 unsigned int bytes_read;
9496 unsigned int load_all = 0;
9497
9498 int nesting_level = 1;
9499
9500 parent_die = NULL;
9501 last_die = NULL;
9502
9503 if (cu->per_cu && cu->per_cu->load_all_dies)
9504 load_all = 1;
9505
9506 cu->partial_dies
9507 = htab_create_alloc_ex (cu->header.length / 12,
9508 partial_die_hash,
9509 partial_die_eq,
9510 NULL,
9511 &cu->comp_unit_obstack,
9512 hashtab_obstack_allocate,
9513 dummy_obstack_deallocate);
9514
9515 part_die = obstack_alloc (&cu->comp_unit_obstack,
9516 sizeof (struct partial_die_info));
9517
9518 while (1)
9519 {
9520 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9521
9522 /* A NULL abbrev means the end of a series of children. */
9523 if (abbrev == NULL)
9524 {
9525 if (--nesting_level == 0)
9526 {
9527 /* PART_DIE was probably the last thing allocated on the
9528 comp_unit_obstack, so we could call obstack_free
9529 here. We don't do that because the waste is small,
9530 and will be cleaned up when we're done with this
9531 compilation unit. This way, we're also more robust
9532 against other users of the comp_unit_obstack. */
9533 return first_die;
9534 }
9535 info_ptr += bytes_read;
9536 last_die = parent_die;
9537 parent_die = parent_die->die_parent;
9538 continue;
9539 }
9540
9541 /* Check for template arguments. We never save these; if
9542 they're seen, we just mark the parent, and go on our way. */
9543 if (parent_die != NULL
9544 && cu->language == language_cplus
9545 && (abbrev->tag == DW_TAG_template_type_param
9546 || abbrev->tag == DW_TAG_template_value_param))
9547 {
9548 parent_die->has_template_arguments = 1;
9549
9550 if (!load_all)
9551 {
9552 /* We don't need a partial DIE for the template argument. */
9553 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9554 cu);
9555 continue;
9556 }
9557 }
9558
9559 /* We only recurse into subprograms looking for template arguments.
9560 Skip their other children. */
9561 if (!load_all
9562 && cu->language == language_cplus
9563 && parent_die != NULL
9564 && parent_die->tag == DW_TAG_subprogram)
9565 {
9566 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9567 continue;
9568 }
9569
9570 /* Check whether this DIE is interesting enough to save. Normally
9571 we would not be interested in members here, but there may be
9572 later variables referencing them via DW_AT_specification (for
9573 static members). */
9574 if (!load_all
9575 && !is_type_tag_for_partial (abbrev->tag)
9576 && abbrev->tag != DW_TAG_constant
9577 && abbrev->tag != DW_TAG_enumerator
9578 && abbrev->tag != DW_TAG_subprogram
9579 && abbrev->tag != DW_TAG_lexical_block
9580 && abbrev->tag != DW_TAG_variable
9581 && abbrev->tag != DW_TAG_namespace
9582 && abbrev->tag != DW_TAG_module
9583 && abbrev->tag != DW_TAG_member)
9584 {
9585 /* Otherwise we skip to the next sibling, if any. */
9586 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9587 continue;
9588 }
9589
9590 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9591 buffer, info_ptr, cu);
9592
9593 /* This two-pass algorithm for processing partial symbols has a
9594 high cost in cache pressure. Thus, handle some simple cases
9595 here which cover the majority of C partial symbols. DIEs
9596 which neither have specification tags in them, nor could have
9597 specification tags elsewhere pointing at them, can simply be
9598 processed and discarded.
9599
9600 This segment is also optional; scan_partial_symbols and
9601 add_partial_symbol will handle these DIEs if we chain
9602 them in normally. When compilers which do not emit large
9603 quantities of duplicate debug information are more common,
9604 this code can probably be removed. */
9605
9606 /* Any complete simple types at the top level (pretty much all
9607 of them, for a language without namespaces), can be processed
9608 directly. */
9609 if (parent_die == NULL
9610 && part_die->has_specification == 0
9611 && part_die->is_declaration == 0
9612 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9613 || part_die->tag == DW_TAG_base_type
9614 || part_die->tag == DW_TAG_subrange_type))
9615 {
9616 if (building_psymtab && part_die->name != NULL)
9617 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9618 VAR_DOMAIN, LOC_TYPEDEF,
9619 &cu->objfile->static_psymbols,
9620 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9621 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9622 continue;
9623 }
9624
9625 /* The exception for DW_TAG_typedef with has_children above is
9626 a workaround of GCC PR debug/47510. In the case of this complaint
9627 type_name_no_tag_or_error will error on such types later.
9628
9629 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9630 it could not find the child DIEs referenced later, this is checked
9631 above. In correct DWARF DW_TAG_typedef should have no children. */
9632
9633 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9634 complaint (&symfile_complaints,
9635 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9636 "- DIE at 0x%x [in module %s]"),
9637 part_die->offset, cu->objfile->name);
9638
9639 /* If we're at the second level, and we're an enumerator, and
9640 our parent has no specification (meaning possibly lives in a
9641 namespace elsewhere), then we can add the partial symbol now
9642 instead of queueing it. */
9643 if (part_die->tag == DW_TAG_enumerator
9644 && parent_die != NULL
9645 && parent_die->die_parent == NULL
9646 && parent_die->tag == DW_TAG_enumeration_type
9647 && parent_die->has_specification == 0)
9648 {
9649 if (part_die->name == NULL)
9650 complaint (&symfile_complaints,
9651 _("malformed enumerator DIE ignored"));
9652 else if (building_psymtab)
9653 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9654 VAR_DOMAIN, LOC_CONST,
9655 (cu->language == language_cplus
9656 || cu->language == language_java)
9657 ? &cu->objfile->global_psymbols
9658 : &cu->objfile->static_psymbols,
9659 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9660
9661 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9662 continue;
9663 }
9664
9665 /* We'll save this DIE so link it in. */
9666 part_die->die_parent = parent_die;
9667 part_die->die_sibling = NULL;
9668 part_die->die_child = NULL;
9669
9670 if (last_die && last_die == parent_die)
9671 last_die->die_child = part_die;
9672 else if (last_die)
9673 last_die->die_sibling = part_die;
9674
9675 last_die = part_die;
9676
9677 if (first_die == NULL)
9678 first_die = part_die;
9679
9680 /* Maybe add the DIE to the hash table. Not all DIEs that we
9681 find interesting need to be in the hash table, because we
9682 also have the parent/sibling/child chains; only those that we
9683 might refer to by offset later during partial symbol reading.
9684
9685 For now this means things that might have be the target of a
9686 DW_AT_specification, DW_AT_abstract_origin, or
9687 DW_AT_extension. DW_AT_extension will refer only to
9688 namespaces; DW_AT_abstract_origin refers to functions (and
9689 many things under the function DIE, but we do not recurse
9690 into function DIEs during partial symbol reading) and
9691 possibly variables as well; DW_AT_specification refers to
9692 declarations. Declarations ought to have the DW_AT_declaration
9693 flag. It happens that GCC forgets to put it in sometimes, but
9694 only for functions, not for types.
9695
9696 Adding more things than necessary to the hash table is harmless
9697 except for the performance cost. Adding too few will result in
9698 wasted time in find_partial_die, when we reread the compilation
9699 unit with load_all_dies set. */
9700
9701 if (load_all
9702 || abbrev->tag == DW_TAG_constant
9703 || abbrev->tag == DW_TAG_subprogram
9704 || abbrev->tag == DW_TAG_variable
9705 || abbrev->tag == DW_TAG_namespace
9706 || part_die->is_declaration)
9707 {
9708 void **slot;
9709
9710 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9711 part_die->offset, INSERT);
9712 *slot = part_die;
9713 }
9714
9715 part_die = obstack_alloc (&cu->comp_unit_obstack,
9716 sizeof (struct partial_die_info));
9717
9718 /* For some DIEs we want to follow their children (if any). For C
9719 we have no reason to follow the children of structures; for other
9720 languages we have to, so that we can get at method physnames
9721 to infer fully qualified class names, for DW_AT_specification,
9722 and for C++ template arguments. For C++, we also look one level
9723 inside functions to find template arguments (if the name of the
9724 function does not already contain the template arguments).
9725
9726 For Ada, we need to scan the children of subprograms and lexical
9727 blocks as well because Ada allows the definition of nested
9728 entities that could be interesting for the debugger, such as
9729 nested subprograms for instance. */
9730 if (last_die->has_children
9731 && (load_all
9732 || last_die->tag == DW_TAG_namespace
9733 || last_die->tag == DW_TAG_module
9734 || last_die->tag == DW_TAG_enumeration_type
9735 || (cu->language == language_cplus
9736 && last_die->tag == DW_TAG_subprogram
9737 && (last_die->name == NULL
9738 || strchr (last_die->name, '<') == NULL))
9739 || (cu->language != language_c
9740 && (last_die->tag == DW_TAG_class_type
9741 || last_die->tag == DW_TAG_interface_type
9742 || last_die->tag == DW_TAG_structure_type
9743 || last_die->tag == DW_TAG_union_type))
9744 || (cu->language == language_ada
9745 && (last_die->tag == DW_TAG_subprogram
9746 || last_die->tag == DW_TAG_lexical_block))))
9747 {
9748 nesting_level++;
9749 parent_die = last_die;
9750 continue;
9751 }
9752
9753 /* Otherwise we skip to the next sibling, if any. */
9754 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9755
9756 /* Back to the top, do it again. */
9757 }
9758 }
9759
9760 /* Read a minimal amount of information into the minimal die structure. */
9761
9762 static gdb_byte *
9763 read_partial_die (struct partial_die_info *part_die,
9764 struct abbrev_info *abbrev,
9765 unsigned int abbrev_len, bfd *abfd,
9766 gdb_byte *buffer, gdb_byte *info_ptr,
9767 struct dwarf2_cu *cu)
9768 {
9769 unsigned int i;
9770 struct attribute attr;
9771 int has_low_pc_attr = 0;
9772 int has_high_pc_attr = 0;
9773
9774 memset (part_die, 0, sizeof (struct partial_die_info));
9775
9776 part_die->offset = info_ptr - buffer;
9777
9778 info_ptr += abbrev_len;
9779
9780 if (abbrev == NULL)
9781 return info_ptr;
9782
9783 part_die->tag = abbrev->tag;
9784 part_die->has_children = abbrev->has_children;
9785
9786 for (i = 0; i < abbrev->num_attrs; ++i)
9787 {
9788 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9789
9790 /* Store the data if it is of an attribute we want to keep in a
9791 partial symbol table. */
9792 switch (attr.name)
9793 {
9794 case DW_AT_name:
9795 switch (part_die->tag)
9796 {
9797 case DW_TAG_compile_unit:
9798 case DW_TAG_type_unit:
9799 /* Compilation units have a DW_AT_name that is a filename, not
9800 a source language identifier. */
9801 case DW_TAG_enumeration_type:
9802 case DW_TAG_enumerator:
9803 /* These tags always have simple identifiers already; no need
9804 to canonicalize them. */
9805 part_die->name = DW_STRING (&attr);
9806 break;
9807 default:
9808 part_die->name
9809 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9810 &cu->objfile->objfile_obstack);
9811 break;
9812 }
9813 break;
9814 case DW_AT_linkage_name:
9815 case DW_AT_MIPS_linkage_name:
9816 /* Note that both forms of linkage name might appear. We
9817 assume they will be the same, and we only store the last
9818 one we see. */
9819 if (cu->language == language_ada)
9820 part_die->name = DW_STRING (&attr);
9821 part_die->linkage_name = DW_STRING (&attr);
9822 break;
9823 case DW_AT_low_pc:
9824 has_low_pc_attr = 1;
9825 part_die->lowpc = DW_ADDR (&attr);
9826 break;
9827 case DW_AT_high_pc:
9828 has_high_pc_attr = 1;
9829 part_die->highpc = DW_ADDR (&attr);
9830 break;
9831 case DW_AT_location:
9832 /* Support the .debug_loc offsets. */
9833 if (attr_form_is_block (&attr))
9834 {
9835 part_die->locdesc = DW_BLOCK (&attr);
9836 }
9837 else if (attr_form_is_section_offset (&attr))
9838 {
9839 dwarf2_complex_location_expr_complaint ();
9840 }
9841 else
9842 {
9843 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9844 "partial symbol information");
9845 }
9846 break;
9847 case DW_AT_external:
9848 part_die->is_external = DW_UNSND (&attr);
9849 break;
9850 case DW_AT_declaration:
9851 part_die->is_declaration = DW_UNSND (&attr);
9852 break;
9853 case DW_AT_type:
9854 part_die->has_type = 1;
9855 break;
9856 case DW_AT_abstract_origin:
9857 case DW_AT_specification:
9858 case DW_AT_extension:
9859 part_die->has_specification = 1;
9860 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9861 break;
9862 case DW_AT_sibling:
9863 /* Ignore absolute siblings, they might point outside of
9864 the current compile unit. */
9865 if (attr.form == DW_FORM_ref_addr)
9866 complaint (&symfile_complaints,
9867 _("ignoring absolute DW_AT_sibling"));
9868 else
9869 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9870 break;
9871 case DW_AT_byte_size:
9872 part_die->has_byte_size = 1;
9873 break;
9874 case DW_AT_calling_convention:
9875 /* DWARF doesn't provide a way to identify a program's source-level
9876 entry point. DW_AT_calling_convention attributes are only meant
9877 to describe functions' calling conventions.
9878
9879 However, because it's a necessary piece of information in
9880 Fortran, and because DW_CC_program is the only piece of debugging
9881 information whose definition refers to a 'main program' at all,
9882 several compilers have begun marking Fortran main programs with
9883 DW_CC_program --- even when those functions use the standard
9884 calling conventions.
9885
9886 So until DWARF specifies a way to provide this information and
9887 compilers pick up the new representation, we'll support this
9888 practice. */
9889 if (DW_UNSND (&attr) == DW_CC_program
9890 && cu->language == language_fortran)
9891 {
9892 set_main_name (part_die->name);
9893
9894 /* As this DIE has a static linkage the name would be difficult
9895 to look up later. */
9896 language_of_main = language_fortran;
9897 }
9898 break;
9899 default:
9900 break;
9901 }
9902 }
9903
9904 if (has_low_pc_attr && has_high_pc_attr)
9905 {
9906 /* When using the GNU linker, .gnu.linkonce. sections are used to
9907 eliminate duplicate copies of functions and vtables and such.
9908 The linker will arbitrarily choose one and discard the others.
9909 The AT_*_pc values for such functions refer to local labels in
9910 these sections. If the section from that file was discarded, the
9911 labels are not in the output, so the relocs get a value of 0.
9912 If this is a discarded function, mark the pc bounds as invalid,
9913 so that GDB will ignore it. */
9914 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9915 {
9916 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9917
9918 complaint (&symfile_complaints,
9919 _("DW_AT_low_pc %s is zero "
9920 "for DIE at 0x%x [in module %s]"),
9921 paddress (gdbarch, part_die->lowpc),
9922 part_die->offset, cu->objfile->name);
9923 }
9924 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9925 else if (part_die->lowpc >= part_die->highpc)
9926 {
9927 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9928
9929 complaint (&symfile_complaints,
9930 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9931 "for DIE at 0x%x [in module %s]"),
9932 paddress (gdbarch, part_die->lowpc),
9933 paddress (gdbarch, part_die->highpc),
9934 part_die->offset, cu->objfile->name);
9935 }
9936 else
9937 part_die->has_pc_info = 1;
9938 }
9939
9940 return info_ptr;
9941 }
9942
9943 /* Find a cached partial DIE at OFFSET in CU. */
9944
9945 static struct partial_die_info *
9946 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9947 {
9948 struct partial_die_info *lookup_die = NULL;
9949 struct partial_die_info part_die;
9950
9951 part_die.offset = offset;
9952 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9953
9954 return lookup_die;
9955 }
9956
9957 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9958 except in the case of .debug_types DIEs which do not reference
9959 outside their CU (they do however referencing other types via
9960 DW_FORM_ref_sig8). */
9961
9962 static struct partial_die_info *
9963 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9964 {
9965 struct dwarf2_per_cu_data *per_cu = NULL;
9966 struct partial_die_info *pd = NULL;
9967
9968 if (cu->per_cu->debug_types_section)
9969 {
9970 pd = find_partial_die_in_comp_unit (offset, cu);
9971 if (pd != NULL)
9972 return pd;
9973 goto not_found;
9974 }
9975
9976 if (offset_in_cu_p (&cu->header, offset))
9977 {
9978 pd = find_partial_die_in_comp_unit (offset, cu);
9979 if (pd != NULL)
9980 return pd;
9981 }
9982
9983 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9984
9985 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9986 load_partial_comp_unit (per_cu, cu->objfile);
9987
9988 per_cu->cu->last_used = 0;
9989 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9990
9991 if (pd == NULL && per_cu->load_all_dies == 0)
9992 {
9993 struct cleanup *back_to;
9994 struct partial_die_info comp_unit_die;
9995 struct abbrev_info *abbrev;
9996 unsigned int bytes_read;
9997 char *info_ptr;
9998
9999 per_cu->load_all_dies = 1;
10000
10001 /* Re-read the DIEs. */
10002 back_to = make_cleanup (null_cleanup, 0);
10003 if (per_cu->cu->dwarf2_abbrevs == NULL)
10004 {
10005 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
10006 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10007 }
10008 info_ptr = (dwarf2_per_objfile->info.buffer
10009 + per_cu->cu->header.offset
10010 + per_cu->cu->header.first_die_offset);
10011 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10012 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10013 per_cu->cu->objfile->obfd,
10014 dwarf2_per_objfile->info.buffer, info_ptr,
10015 per_cu->cu);
10016 if (comp_unit_die.has_children)
10017 load_partial_dies (per_cu->cu->objfile->obfd,
10018 dwarf2_per_objfile->info.buffer, info_ptr,
10019 0, per_cu->cu);
10020 do_cleanups (back_to);
10021
10022 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10023 }
10024
10025 not_found:
10026
10027 if (pd == NULL)
10028 internal_error (__FILE__, __LINE__,
10029 _("could not find partial DIE 0x%x "
10030 "in cache [from module %s]\n"),
10031 offset, bfd_get_filename (cu->objfile->obfd));
10032 return pd;
10033 }
10034
10035 /* See if we can figure out if the class lives in a namespace. We do
10036 this by looking for a member function; its demangled name will
10037 contain namespace info, if there is any. */
10038
10039 static void
10040 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10041 struct dwarf2_cu *cu)
10042 {
10043 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10044 what template types look like, because the demangler
10045 frequently doesn't give the same name as the debug info. We
10046 could fix this by only using the demangled name to get the
10047 prefix (but see comment in read_structure_type). */
10048
10049 struct partial_die_info *real_pdi;
10050 struct partial_die_info *child_pdi;
10051
10052 /* If this DIE (this DIE's specification, if any) has a parent, then
10053 we should not do this. We'll prepend the parent's fully qualified
10054 name when we create the partial symbol. */
10055
10056 real_pdi = struct_pdi;
10057 while (real_pdi->has_specification)
10058 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10059
10060 if (real_pdi->die_parent != NULL)
10061 return;
10062
10063 for (child_pdi = struct_pdi->die_child;
10064 child_pdi != NULL;
10065 child_pdi = child_pdi->die_sibling)
10066 {
10067 if (child_pdi->tag == DW_TAG_subprogram
10068 && child_pdi->linkage_name != NULL)
10069 {
10070 char *actual_class_name
10071 = language_class_name_from_physname (cu->language_defn,
10072 child_pdi->linkage_name);
10073 if (actual_class_name != NULL)
10074 {
10075 struct_pdi->name
10076 = obsavestring (actual_class_name,
10077 strlen (actual_class_name),
10078 &cu->objfile->objfile_obstack);
10079 xfree (actual_class_name);
10080 }
10081 break;
10082 }
10083 }
10084 }
10085
10086 /* Adjust PART_DIE before generating a symbol for it. This function
10087 may set the is_external flag or change the DIE's name. */
10088
10089 static void
10090 fixup_partial_die (struct partial_die_info *part_die,
10091 struct dwarf2_cu *cu)
10092 {
10093 /* Once we've fixed up a die, there's no point in doing so again.
10094 This also avoids a memory leak if we were to call
10095 guess_partial_die_structure_name multiple times. */
10096 if (part_die->fixup_called)
10097 return;
10098
10099 /* If we found a reference attribute and the DIE has no name, try
10100 to find a name in the referred to DIE. */
10101
10102 if (part_die->name == NULL && part_die->has_specification)
10103 {
10104 struct partial_die_info *spec_die;
10105
10106 spec_die = find_partial_die (part_die->spec_offset, cu);
10107
10108 fixup_partial_die (spec_die, cu);
10109
10110 if (spec_die->name)
10111 {
10112 part_die->name = spec_die->name;
10113
10114 /* Copy DW_AT_external attribute if it is set. */
10115 if (spec_die->is_external)
10116 part_die->is_external = spec_die->is_external;
10117 }
10118 }
10119
10120 /* Set default names for some unnamed DIEs. */
10121
10122 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10123 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10124
10125 /* If there is no parent die to provide a namespace, and there are
10126 children, see if we can determine the namespace from their linkage
10127 name.
10128 NOTE: We need to do this even if cu->has_namespace_info != 0.
10129 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10130 if (cu->language == language_cplus
10131 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10132 && part_die->die_parent == NULL
10133 && part_die->has_children
10134 && (part_die->tag == DW_TAG_class_type
10135 || part_die->tag == DW_TAG_structure_type
10136 || part_die->tag == DW_TAG_union_type))
10137 guess_partial_die_structure_name (part_die, cu);
10138
10139 /* GCC might emit a nameless struct or union that has a linkage
10140 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10141 if (part_die->name == NULL
10142 && (part_die->tag == DW_TAG_class_type
10143 || part_die->tag == DW_TAG_interface_type
10144 || part_die->tag == DW_TAG_structure_type
10145 || part_die->tag == DW_TAG_union_type)
10146 && part_die->linkage_name != NULL)
10147 {
10148 char *demangled;
10149
10150 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10151 if (demangled)
10152 {
10153 const char *base;
10154
10155 /* Strip any leading namespaces/classes, keep only the base name.
10156 DW_AT_name for named DIEs does not contain the prefixes. */
10157 base = strrchr (demangled, ':');
10158 if (base && base > demangled && base[-1] == ':')
10159 base++;
10160 else
10161 base = demangled;
10162
10163 part_die->name = obsavestring (base, strlen (base),
10164 &cu->objfile->objfile_obstack);
10165 xfree (demangled);
10166 }
10167 }
10168
10169 part_die->fixup_called = 1;
10170 }
10171
10172 /* Read an attribute value described by an attribute form. */
10173
10174 static gdb_byte *
10175 read_attribute_value (struct attribute *attr, unsigned form,
10176 bfd *abfd, gdb_byte *info_ptr,
10177 struct dwarf2_cu *cu)
10178 {
10179 struct comp_unit_head *cu_header = &cu->header;
10180 unsigned int bytes_read;
10181 struct dwarf_block *blk;
10182
10183 attr->form = form;
10184 switch (form)
10185 {
10186 case DW_FORM_ref_addr:
10187 if (cu->header.version == 2)
10188 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10189 else
10190 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10191 &cu->header, &bytes_read);
10192 info_ptr += bytes_read;
10193 break;
10194 case DW_FORM_addr:
10195 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10196 info_ptr += bytes_read;
10197 break;
10198 case DW_FORM_block2:
10199 blk = dwarf_alloc_block (cu);
10200 blk->size = read_2_bytes (abfd, info_ptr);
10201 info_ptr += 2;
10202 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10203 info_ptr += blk->size;
10204 DW_BLOCK (attr) = blk;
10205 break;
10206 case DW_FORM_block4:
10207 blk = dwarf_alloc_block (cu);
10208 blk->size = read_4_bytes (abfd, info_ptr);
10209 info_ptr += 4;
10210 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10211 info_ptr += blk->size;
10212 DW_BLOCK (attr) = blk;
10213 break;
10214 case DW_FORM_data2:
10215 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10216 info_ptr += 2;
10217 break;
10218 case DW_FORM_data4:
10219 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10220 info_ptr += 4;
10221 break;
10222 case DW_FORM_data8:
10223 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10224 info_ptr += 8;
10225 break;
10226 case DW_FORM_sec_offset:
10227 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10228 info_ptr += bytes_read;
10229 break;
10230 case DW_FORM_string:
10231 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10232 DW_STRING_IS_CANONICAL (attr) = 0;
10233 info_ptr += bytes_read;
10234 break;
10235 case DW_FORM_strp:
10236 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10237 &bytes_read);
10238 DW_STRING_IS_CANONICAL (attr) = 0;
10239 info_ptr += bytes_read;
10240 break;
10241 case DW_FORM_exprloc:
10242 case DW_FORM_block:
10243 blk = dwarf_alloc_block (cu);
10244 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10245 info_ptr += bytes_read;
10246 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10247 info_ptr += blk->size;
10248 DW_BLOCK (attr) = blk;
10249 break;
10250 case DW_FORM_block1:
10251 blk = dwarf_alloc_block (cu);
10252 blk->size = read_1_byte (abfd, info_ptr);
10253 info_ptr += 1;
10254 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10255 info_ptr += blk->size;
10256 DW_BLOCK (attr) = blk;
10257 break;
10258 case DW_FORM_data1:
10259 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10260 info_ptr += 1;
10261 break;
10262 case DW_FORM_flag:
10263 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10264 info_ptr += 1;
10265 break;
10266 case DW_FORM_flag_present:
10267 DW_UNSND (attr) = 1;
10268 break;
10269 case DW_FORM_sdata:
10270 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10271 info_ptr += bytes_read;
10272 break;
10273 case DW_FORM_udata:
10274 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10275 info_ptr += bytes_read;
10276 break;
10277 case DW_FORM_ref1:
10278 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10279 info_ptr += 1;
10280 break;
10281 case DW_FORM_ref2:
10282 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10283 info_ptr += 2;
10284 break;
10285 case DW_FORM_ref4:
10286 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10287 info_ptr += 4;
10288 break;
10289 case DW_FORM_ref8:
10290 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10291 info_ptr += 8;
10292 break;
10293 case DW_FORM_ref_sig8:
10294 /* Convert the signature to something we can record in DW_UNSND
10295 for later lookup.
10296 NOTE: This is NULL if the type wasn't found. */
10297 DW_SIGNATURED_TYPE (attr) =
10298 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10299 info_ptr += 8;
10300 break;
10301 case DW_FORM_ref_udata:
10302 DW_ADDR (attr) = (cu->header.offset
10303 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10304 info_ptr += bytes_read;
10305 break;
10306 case DW_FORM_indirect:
10307 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10308 info_ptr += bytes_read;
10309 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10310 break;
10311 default:
10312 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10313 dwarf_form_name (form),
10314 bfd_get_filename (abfd));
10315 }
10316
10317 /* We have seen instances where the compiler tried to emit a byte
10318 size attribute of -1 which ended up being encoded as an unsigned
10319 0xffffffff. Although 0xffffffff is technically a valid size value,
10320 an object of this size seems pretty unlikely so we can relatively
10321 safely treat these cases as if the size attribute was invalid and
10322 treat them as zero by default. */
10323 if (attr->name == DW_AT_byte_size
10324 && form == DW_FORM_data4
10325 && DW_UNSND (attr) >= 0xffffffff)
10326 {
10327 complaint
10328 (&symfile_complaints,
10329 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10330 hex_string (DW_UNSND (attr)));
10331 DW_UNSND (attr) = 0;
10332 }
10333
10334 return info_ptr;
10335 }
10336
10337 /* Read an attribute described by an abbreviated attribute. */
10338
10339 static gdb_byte *
10340 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10341 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10342 {
10343 attr->name = abbrev->name;
10344 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10345 }
10346
10347 /* Read dwarf information from a buffer. */
10348
10349 static unsigned int
10350 read_1_byte (bfd *abfd, gdb_byte *buf)
10351 {
10352 return bfd_get_8 (abfd, buf);
10353 }
10354
10355 static int
10356 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10357 {
10358 return bfd_get_signed_8 (abfd, buf);
10359 }
10360
10361 static unsigned int
10362 read_2_bytes (bfd *abfd, gdb_byte *buf)
10363 {
10364 return bfd_get_16 (abfd, buf);
10365 }
10366
10367 static int
10368 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10369 {
10370 return bfd_get_signed_16 (abfd, buf);
10371 }
10372
10373 static unsigned int
10374 read_4_bytes (bfd *abfd, gdb_byte *buf)
10375 {
10376 return bfd_get_32 (abfd, buf);
10377 }
10378
10379 static int
10380 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10381 {
10382 return bfd_get_signed_32 (abfd, buf);
10383 }
10384
10385 static ULONGEST
10386 read_8_bytes (bfd *abfd, gdb_byte *buf)
10387 {
10388 return bfd_get_64 (abfd, buf);
10389 }
10390
10391 static CORE_ADDR
10392 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10393 unsigned int *bytes_read)
10394 {
10395 struct comp_unit_head *cu_header = &cu->header;
10396 CORE_ADDR retval = 0;
10397
10398 if (cu_header->signed_addr_p)
10399 {
10400 switch (cu_header->addr_size)
10401 {
10402 case 2:
10403 retval = bfd_get_signed_16 (abfd, buf);
10404 break;
10405 case 4:
10406 retval = bfd_get_signed_32 (abfd, buf);
10407 break;
10408 case 8:
10409 retval = bfd_get_signed_64 (abfd, buf);
10410 break;
10411 default:
10412 internal_error (__FILE__, __LINE__,
10413 _("read_address: bad switch, signed [in module %s]"),
10414 bfd_get_filename (abfd));
10415 }
10416 }
10417 else
10418 {
10419 switch (cu_header->addr_size)
10420 {
10421 case 2:
10422 retval = bfd_get_16 (abfd, buf);
10423 break;
10424 case 4:
10425 retval = bfd_get_32 (abfd, buf);
10426 break;
10427 case 8:
10428 retval = bfd_get_64 (abfd, buf);
10429 break;
10430 default:
10431 internal_error (__FILE__, __LINE__,
10432 _("read_address: bad switch, "
10433 "unsigned [in module %s]"),
10434 bfd_get_filename (abfd));
10435 }
10436 }
10437
10438 *bytes_read = cu_header->addr_size;
10439 return retval;
10440 }
10441
10442 /* Read the initial length from a section. The (draft) DWARF 3
10443 specification allows the initial length to take up either 4 bytes
10444 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10445 bytes describe the length and all offsets will be 8 bytes in length
10446 instead of 4.
10447
10448 An older, non-standard 64-bit format is also handled by this
10449 function. The older format in question stores the initial length
10450 as an 8-byte quantity without an escape value. Lengths greater
10451 than 2^32 aren't very common which means that the initial 4 bytes
10452 is almost always zero. Since a length value of zero doesn't make
10453 sense for the 32-bit format, this initial zero can be considered to
10454 be an escape value which indicates the presence of the older 64-bit
10455 format. As written, the code can't detect (old format) lengths
10456 greater than 4GB. If it becomes necessary to handle lengths
10457 somewhat larger than 4GB, we could allow other small values (such
10458 as the non-sensical values of 1, 2, and 3) to also be used as
10459 escape values indicating the presence of the old format.
10460
10461 The value returned via bytes_read should be used to increment the
10462 relevant pointer after calling read_initial_length().
10463
10464 [ Note: read_initial_length() and read_offset() are based on the
10465 document entitled "DWARF Debugging Information Format", revision
10466 3, draft 8, dated November 19, 2001. This document was obtained
10467 from:
10468
10469 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10470
10471 This document is only a draft and is subject to change. (So beware.)
10472
10473 Details regarding the older, non-standard 64-bit format were
10474 determined empirically by examining 64-bit ELF files produced by
10475 the SGI toolchain on an IRIX 6.5 machine.
10476
10477 - Kevin, July 16, 2002
10478 ] */
10479
10480 static LONGEST
10481 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10482 {
10483 LONGEST length = bfd_get_32 (abfd, buf);
10484
10485 if (length == 0xffffffff)
10486 {
10487 length = bfd_get_64 (abfd, buf + 4);
10488 *bytes_read = 12;
10489 }
10490 else if (length == 0)
10491 {
10492 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10493 length = bfd_get_64 (abfd, buf);
10494 *bytes_read = 8;
10495 }
10496 else
10497 {
10498 *bytes_read = 4;
10499 }
10500
10501 return length;
10502 }
10503
10504 /* Cover function for read_initial_length.
10505 Returns the length of the object at BUF, and stores the size of the
10506 initial length in *BYTES_READ and stores the size that offsets will be in
10507 *OFFSET_SIZE.
10508 If the initial length size is not equivalent to that specified in
10509 CU_HEADER then issue a complaint.
10510 This is useful when reading non-comp-unit headers. */
10511
10512 static LONGEST
10513 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10514 const struct comp_unit_head *cu_header,
10515 unsigned int *bytes_read,
10516 unsigned int *offset_size)
10517 {
10518 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10519
10520 gdb_assert (cu_header->initial_length_size == 4
10521 || cu_header->initial_length_size == 8
10522 || cu_header->initial_length_size == 12);
10523
10524 if (cu_header->initial_length_size != *bytes_read)
10525 complaint (&symfile_complaints,
10526 _("intermixed 32-bit and 64-bit DWARF sections"));
10527
10528 *offset_size = (*bytes_read == 4) ? 4 : 8;
10529 return length;
10530 }
10531
10532 /* Read an offset from the data stream. The size of the offset is
10533 given by cu_header->offset_size. */
10534
10535 static LONGEST
10536 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10537 unsigned int *bytes_read)
10538 {
10539 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10540
10541 *bytes_read = cu_header->offset_size;
10542 return offset;
10543 }
10544
10545 /* Read an offset from the data stream. */
10546
10547 static LONGEST
10548 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10549 {
10550 LONGEST retval = 0;
10551
10552 switch (offset_size)
10553 {
10554 case 4:
10555 retval = bfd_get_32 (abfd, buf);
10556 break;
10557 case 8:
10558 retval = bfd_get_64 (abfd, buf);
10559 break;
10560 default:
10561 internal_error (__FILE__, __LINE__,
10562 _("read_offset_1: bad switch [in module %s]"),
10563 bfd_get_filename (abfd));
10564 }
10565
10566 return retval;
10567 }
10568
10569 static gdb_byte *
10570 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10571 {
10572 /* If the size of a host char is 8 bits, we can return a pointer
10573 to the buffer, otherwise we have to copy the data to a buffer
10574 allocated on the temporary obstack. */
10575 gdb_assert (HOST_CHAR_BIT == 8);
10576 return buf;
10577 }
10578
10579 static char *
10580 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10581 {
10582 /* If the size of a host char is 8 bits, we can return a pointer
10583 to the string, otherwise we have to copy the string to a buffer
10584 allocated on the temporary obstack. */
10585 gdb_assert (HOST_CHAR_BIT == 8);
10586 if (*buf == '\0')
10587 {
10588 *bytes_read_ptr = 1;
10589 return NULL;
10590 }
10591 *bytes_read_ptr = strlen ((char *) buf) + 1;
10592 return (char *) buf;
10593 }
10594
10595 static char *
10596 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10597 {
10598 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10599 if (dwarf2_per_objfile->str.buffer == NULL)
10600 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10601 bfd_get_filename (abfd));
10602 if (str_offset >= dwarf2_per_objfile->str.size)
10603 error (_("DW_FORM_strp pointing outside of "
10604 ".debug_str section [in module %s]"),
10605 bfd_get_filename (abfd));
10606 gdb_assert (HOST_CHAR_BIT == 8);
10607 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10608 return NULL;
10609 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10610 }
10611
10612 static char *
10613 read_indirect_string (bfd *abfd, gdb_byte *buf,
10614 const struct comp_unit_head *cu_header,
10615 unsigned int *bytes_read_ptr)
10616 {
10617 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10618
10619 return read_indirect_string_at_offset (abfd, str_offset);
10620 }
10621
10622 static unsigned long
10623 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10624 {
10625 unsigned long result;
10626 unsigned int num_read;
10627 int i, shift;
10628 unsigned char byte;
10629
10630 result = 0;
10631 shift = 0;
10632 num_read = 0;
10633 i = 0;
10634 while (1)
10635 {
10636 byte = bfd_get_8 (abfd, buf);
10637 buf++;
10638 num_read++;
10639 result |= ((unsigned long)(byte & 127) << shift);
10640 if ((byte & 128) == 0)
10641 {
10642 break;
10643 }
10644 shift += 7;
10645 }
10646 *bytes_read_ptr = num_read;
10647 return result;
10648 }
10649
10650 static long
10651 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10652 {
10653 long result;
10654 int i, shift, num_read;
10655 unsigned char byte;
10656
10657 result = 0;
10658 shift = 0;
10659 num_read = 0;
10660 i = 0;
10661 while (1)
10662 {
10663 byte = bfd_get_8 (abfd, buf);
10664 buf++;
10665 num_read++;
10666 result |= ((long)(byte & 127) << shift);
10667 shift += 7;
10668 if ((byte & 128) == 0)
10669 {
10670 break;
10671 }
10672 }
10673 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10674 result |= -(((long)1) << shift);
10675 *bytes_read_ptr = num_read;
10676 return result;
10677 }
10678
10679 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10680
10681 static gdb_byte *
10682 skip_leb128 (bfd *abfd, gdb_byte *buf)
10683 {
10684 int byte;
10685
10686 while (1)
10687 {
10688 byte = bfd_get_8 (abfd, buf);
10689 buf++;
10690 if ((byte & 128) == 0)
10691 return buf;
10692 }
10693 }
10694
10695 static void
10696 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10697 {
10698 switch (lang)
10699 {
10700 case DW_LANG_C89:
10701 case DW_LANG_C99:
10702 case DW_LANG_C:
10703 cu->language = language_c;
10704 break;
10705 case DW_LANG_C_plus_plus:
10706 cu->language = language_cplus;
10707 break;
10708 case DW_LANG_D:
10709 cu->language = language_d;
10710 break;
10711 case DW_LANG_Fortran77:
10712 case DW_LANG_Fortran90:
10713 case DW_LANG_Fortran95:
10714 cu->language = language_fortran;
10715 break;
10716 case DW_LANG_Mips_Assembler:
10717 cu->language = language_asm;
10718 break;
10719 case DW_LANG_Java:
10720 cu->language = language_java;
10721 break;
10722 case DW_LANG_Ada83:
10723 case DW_LANG_Ada95:
10724 cu->language = language_ada;
10725 break;
10726 case DW_LANG_Modula2:
10727 cu->language = language_m2;
10728 break;
10729 case DW_LANG_Pascal83:
10730 cu->language = language_pascal;
10731 break;
10732 case DW_LANG_ObjC:
10733 cu->language = language_objc;
10734 break;
10735 case DW_LANG_Cobol74:
10736 case DW_LANG_Cobol85:
10737 default:
10738 cu->language = language_minimal;
10739 break;
10740 }
10741 cu->language_defn = language_def (cu->language);
10742 }
10743
10744 /* Return the named attribute or NULL if not there. */
10745
10746 static struct attribute *
10747 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10748 {
10749 unsigned int i;
10750 struct attribute *spec = NULL;
10751
10752 for (i = 0; i < die->num_attrs; ++i)
10753 {
10754 if (die->attrs[i].name == name)
10755 return &die->attrs[i];
10756 if (die->attrs[i].name == DW_AT_specification
10757 || die->attrs[i].name == DW_AT_abstract_origin)
10758 spec = &die->attrs[i];
10759 }
10760
10761 if (spec)
10762 {
10763 die = follow_die_ref (die, spec, &cu);
10764 return dwarf2_attr (die, name, cu);
10765 }
10766
10767 return NULL;
10768 }
10769
10770 /* Return the named attribute or NULL if not there,
10771 but do not follow DW_AT_specification, etc.
10772 This is for use in contexts where we're reading .debug_types dies.
10773 Following DW_AT_specification, DW_AT_abstract_origin will take us
10774 back up the chain, and we want to go down. */
10775
10776 static struct attribute *
10777 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10778 struct dwarf2_cu *cu)
10779 {
10780 unsigned int i;
10781
10782 for (i = 0; i < die->num_attrs; ++i)
10783 if (die->attrs[i].name == name)
10784 return &die->attrs[i];
10785
10786 return NULL;
10787 }
10788
10789 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10790 and holds a non-zero value. This function should only be used for
10791 DW_FORM_flag or DW_FORM_flag_present attributes. */
10792
10793 static int
10794 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10795 {
10796 struct attribute *attr = dwarf2_attr (die, name, cu);
10797
10798 return (attr && DW_UNSND (attr));
10799 }
10800
10801 static int
10802 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10803 {
10804 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10805 which value is non-zero. However, we have to be careful with
10806 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10807 (via dwarf2_flag_true_p) follows this attribute. So we may
10808 end up accidently finding a declaration attribute that belongs
10809 to a different DIE referenced by the specification attribute,
10810 even though the given DIE does not have a declaration attribute. */
10811 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10812 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10813 }
10814
10815 /* Return the die giving the specification for DIE, if there is
10816 one. *SPEC_CU is the CU containing DIE on input, and the CU
10817 containing the return value on output. If there is no
10818 specification, but there is an abstract origin, that is
10819 returned. */
10820
10821 static struct die_info *
10822 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10823 {
10824 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10825 *spec_cu);
10826
10827 if (spec_attr == NULL)
10828 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10829
10830 if (spec_attr == NULL)
10831 return NULL;
10832 else
10833 return follow_die_ref (die, spec_attr, spec_cu);
10834 }
10835
10836 /* Free the line_header structure *LH, and any arrays and strings it
10837 refers to.
10838 NOTE: This is also used as a "cleanup" function. */
10839
10840 static void
10841 free_line_header (struct line_header *lh)
10842 {
10843 if (lh->standard_opcode_lengths)
10844 xfree (lh->standard_opcode_lengths);
10845
10846 /* Remember that all the lh->file_names[i].name pointers are
10847 pointers into debug_line_buffer, and don't need to be freed. */
10848 if (lh->file_names)
10849 xfree (lh->file_names);
10850
10851 /* Similarly for the include directory names. */
10852 if (lh->include_dirs)
10853 xfree (lh->include_dirs);
10854
10855 xfree (lh);
10856 }
10857
10858 /* Add an entry to LH's include directory table. */
10859
10860 static void
10861 add_include_dir (struct line_header *lh, char *include_dir)
10862 {
10863 /* Grow the array if necessary. */
10864 if (lh->include_dirs_size == 0)
10865 {
10866 lh->include_dirs_size = 1; /* for testing */
10867 lh->include_dirs = xmalloc (lh->include_dirs_size
10868 * sizeof (*lh->include_dirs));
10869 }
10870 else if (lh->num_include_dirs >= lh->include_dirs_size)
10871 {
10872 lh->include_dirs_size *= 2;
10873 lh->include_dirs = xrealloc (lh->include_dirs,
10874 (lh->include_dirs_size
10875 * sizeof (*lh->include_dirs)));
10876 }
10877
10878 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10879 }
10880
10881 /* Add an entry to LH's file name table. */
10882
10883 static void
10884 add_file_name (struct line_header *lh,
10885 char *name,
10886 unsigned int dir_index,
10887 unsigned int mod_time,
10888 unsigned int length)
10889 {
10890 struct file_entry *fe;
10891
10892 /* Grow the array if necessary. */
10893 if (lh->file_names_size == 0)
10894 {
10895 lh->file_names_size = 1; /* for testing */
10896 lh->file_names = xmalloc (lh->file_names_size
10897 * sizeof (*lh->file_names));
10898 }
10899 else if (lh->num_file_names >= lh->file_names_size)
10900 {
10901 lh->file_names_size *= 2;
10902 lh->file_names = xrealloc (lh->file_names,
10903 (lh->file_names_size
10904 * sizeof (*lh->file_names)));
10905 }
10906
10907 fe = &lh->file_names[lh->num_file_names++];
10908 fe->name = name;
10909 fe->dir_index = dir_index;
10910 fe->mod_time = mod_time;
10911 fe->length = length;
10912 fe->included_p = 0;
10913 fe->symtab = NULL;
10914 }
10915
10916 /* Read the statement program header starting at OFFSET in
10917 .debug_line, according to the endianness of ABFD. Return a pointer
10918 to a struct line_header, allocated using xmalloc.
10919
10920 NOTE: the strings in the include directory and file name tables of
10921 the returned object point into debug_line_buffer, and must not be
10922 freed. */
10923
10924 static struct line_header *
10925 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10926 struct dwarf2_cu *cu)
10927 {
10928 struct cleanup *back_to;
10929 struct line_header *lh;
10930 gdb_byte *line_ptr;
10931 unsigned int bytes_read, offset_size;
10932 int i;
10933 char *cur_dir, *cur_file;
10934
10935 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10936 if (dwarf2_per_objfile->line.buffer == NULL)
10937 {
10938 complaint (&symfile_complaints, _("missing .debug_line section"));
10939 return 0;
10940 }
10941
10942 /* Make sure that at least there's room for the total_length field.
10943 That could be 12 bytes long, but we're just going to fudge that. */
10944 if (offset + 4 >= dwarf2_per_objfile->line.size)
10945 {
10946 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10947 return 0;
10948 }
10949
10950 lh = xmalloc (sizeof (*lh));
10951 memset (lh, 0, sizeof (*lh));
10952 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10953 (void *) lh);
10954
10955 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10956
10957 /* Read in the header. */
10958 lh->total_length =
10959 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10960 &bytes_read, &offset_size);
10961 line_ptr += bytes_read;
10962 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10963 + dwarf2_per_objfile->line.size))
10964 {
10965 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10966 return 0;
10967 }
10968 lh->statement_program_end = line_ptr + lh->total_length;
10969 lh->version = read_2_bytes (abfd, line_ptr);
10970 line_ptr += 2;
10971 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10972 line_ptr += offset_size;
10973 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10974 line_ptr += 1;
10975 if (lh->version >= 4)
10976 {
10977 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10978 line_ptr += 1;
10979 }
10980 else
10981 lh->maximum_ops_per_instruction = 1;
10982
10983 if (lh->maximum_ops_per_instruction == 0)
10984 {
10985 lh->maximum_ops_per_instruction = 1;
10986 complaint (&symfile_complaints,
10987 _("invalid maximum_ops_per_instruction "
10988 "in `.debug_line' section"));
10989 }
10990
10991 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10992 line_ptr += 1;
10993 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10994 line_ptr += 1;
10995 lh->line_range = read_1_byte (abfd, line_ptr);
10996 line_ptr += 1;
10997 lh->opcode_base = read_1_byte (abfd, line_ptr);
10998 line_ptr += 1;
10999 lh->standard_opcode_lengths
11000 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11001
11002 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11003 for (i = 1; i < lh->opcode_base; ++i)
11004 {
11005 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11006 line_ptr += 1;
11007 }
11008
11009 /* Read directory table. */
11010 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11011 {
11012 line_ptr += bytes_read;
11013 add_include_dir (lh, cur_dir);
11014 }
11015 line_ptr += bytes_read;
11016
11017 /* Read file name table. */
11018 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11019 {
11020 unsigned int dir_index, mod_time, length;
11021
11022 line_ptr += bytes_read;
11023 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11024 line_ptr += bytes_read;
11025 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11026 line_ptr += bytes_read;
11027 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11028 line_ptr += bytes_read;
11029
11030 add_file_name (lh, cur_file, dir_index, mod_time, length);
11031 }
11032 line_ptr += bytes_read;
11033 lh->statement_program_start = line_ptr;
11034
11035 if (line_ptr > (dwarf2_per_objfile->line.buffer
11036 + dwarf2_per_objfile->line.size))
11037 complaint (&symfile_complaints,
11038 _("line number info header doesn't "
11039 "fit in `.debug_line' section"));
11040
11041 discard_cleanups (back_to);
11042 return lh;
11043 }
11044
11045 /* Subroutine of dwarf_decode_lines to simplify it.
11046 Return the file name of the psymtab for included file FILE_INDEX
11047 in line header LH of PST.
11048 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11049 If space for the result is malloc'd, it will be freed by a cleanup.
11050 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11051
11052 static char *
11053 psymtab_include_file_name (const struct line_header *lh, int file_index,
11054 const struct partial_symtab *pst,
11055 const char *comp_dir)
11056 {
11057 const struct file_entry fe = lh->file_names [file_index];
11058 char *include_name = fe.name;
11059 char *include_name_to_compare = include_name;
11060 char *dir_name = NULL;
11061 const char *pst_filename;
11062 char *copied_name = NULL;
11063 int file_is_pst;
11064
11065 if (fe.dir_index)
11066 dir_name = lh->include_dirs[fe.dir_index - 1];
11067
11068 if (!IS_ABSOLUTE_PATH (include_name)
11069 && (dir_name != NULL || comp_dir != NULL))
11070 {
11071 /* Avoid creating a duplicate psymtab for PST.
11072 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11073 Before we do the comparison, however, we need to account
11074 for DIR_NAME and COMP_DIR.
11075 First prepend dir_name (if non-NULL). If we still don't
11076 have an absolute path prepend comp_dir (if non-NULL).
11077 However, the directory we record in the include-file's
11078 psymtab does not contain COMP_DIR (to match the
11079 corresponding symtab(s)).
11080
11081 Example:
11082
11083 bash$ cd /tmp
11084 bash$ gcc -g ./hello.c
11085 include_name = "hello.c"
11086 dir_name = "."
11087 DW_AT_comp_dir = comp_dir = "/tmp"
11088 DW_AT_name = "./hello.c" */
11089
11090 if (dir_name != NULL)
11091 {
11092 include_name = concat (dir_name, SLASH_STRING,
11093 include_name, (char *)NULL);
11094 include_name_to_compare = include_name;
11095 make_cleanup (xfree, include_name);
11096 }
11097 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11098 {
11099 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11100 include_name, (char *)NULL);
11101 }
11102 }
11103
11104 pst_filename = pst->filename;
11105 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11106 {
11107 copied_name = concat (pst->dirname, SLASH_STRING,
11108 pst_filename, (char *)NULL);
11109 pst_filename = copied_name;
11110 }
11111
11112 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11113
11114 if (include_name_to_compare != include_name)
11115 xfree (include_name_to_compare);
11116 if (copied_name != NULL)
11117 xfree (copied_name);
11118
11119 if (file_is_pst)
11120 return NULL;
11121 return include_name;
11122 }
11123
11124 /* Ignore this record_line request. */
11125
11126 static void
11127 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11128 {
11129 return;
11130 }
11131
11132 /* Decode the Line Number Program (LNP) for the given line_header
11133 structure and CU. The actual information extracted and the type
11134 of structures created from the LNP depends on the value of PST.
11135
11136 1. If PST is NULL, then this procedure uses the data from the program
11137 to create all necessary symbol tables, and their linetables.
11138
11139 2. If PST is not NULL, this procedure reads the program to determine
11140 the list of files included by the unit represented by PST, and
11141 builds all the associated partial symbol tables.
11142
11143 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11144 It is used for relative paths in the line table.
11145 NOTE: When processing partial symtabs (pst != NULL),
11146 comp_dir == pst->dirname.
11147
11148 NOTE: It is important that psymtabs have the same file name (via strcmp)
11149 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11150 symtab we don't use it in the name of the psymtabs we create.
11151 E.g. expand_line_sal requires this when finding psymtabs to expand.
11152 A good testcase for this is mb-inline.exp. */
11153
11154 static void
11155 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11156 struct dwarf2_cu *cu, struct partial_symtab *pst)
11157 {
11158 gdb_byte *line_ptr, *extended_end;
11159 gdb_byte *line_end;
11160 unsigned int bytes_read, extended_len;
11161 unsigned char op_code, extended_op, adj_opcode;
11162 CORE_ADDR baseaddr;
11163 struct objfile *objfile = cu->objfile;
11164 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11165 const int decode_for_pst_p = (pst != NULL);
11166 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11167 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11168 = record_line;
11169
11170 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11171
11172 line_ptr = lh->statement_program_start;
11173 line_end = lh->statement_program_end;
11174
11175 /* Read the statement sequences until there's nothing left. */
11176 while (line_ptr < line_end)
11177 {
11178 /* state machine registers */
11179 CORE_ADDR address = 0;
11180 unsigned int file = 1;
11181 unsigned int line = 1;
11182 unsigned int column = 0;
11183 int is_stmt = lh->default_is_stmt;
11184 int basic_block = 0;
11185 int end_sequence = 0;
11186 CORE_ADDR addr;
11187 unsigned char op_index = 0;
11188
11189 if (!decode_for_pst_p && lh->num_file_names >= file)
11190 {
11191 /* Start a subfile for the current file of the state machine. */
11192 /* lh->include_dirs and lh->file_names are 0-based, but the
11193 directory and file name numbers in the statement program
11194 are 1-based. */
11195 struct file_entry *fe = &lh->file_names[file - 1];
11196 char *dir = NULL;
11197
11198 if (fe->dir_index)
11199 dir = lh->include_dirs[fe->dir_index - 1];
11200
11201 dwarf2_start_subfile (fe->name, dir, comp_dir);
11202 }
11203
11204 /* Decode the table. */
11205 while (!end_sequence)
11206 {
11207 op_code = read_1_byte (abfd, line_ptr);
11208 line_ptr += 1;
11209 if (line_ptr > line_end)
11210 {
11211 dwarf2_debug_line_missing_end_sequence_complaint ();
11212 break;
11213 }
11214
11215 if (op_code >= lh->opcode_base)
11216 {
11217 /* Special operand. */
11218 adj_opcode = op_code - lh->opcode_base;
11219 address += (((op_index + (adj_opcode / lh->line_range))
11220 / lh->maximum_ops_per_instruction)
11221 * lh->minimum_instruction_length);
11222 op_index = ((op_index + (adj_opcode / lh->line_range))
11223 % lh->maximum_ops_per_instruction);
11224 line += lh->line_base + (adj_opcode % lh->line_range);
11225 if (lh->num_file_names < file || file == 0)
11226 dwarf2_debug_line_missing_file_complaint ();
11227 /* For now we ignore lines not starting on an
11228 instruction boundary. */
11229 else if (op_index == 0)
11230 {
11231 lh->file_names[file - 1].included_p = 1;
11232 if (!decode_for_pst_p && is_stmt)
11233 {
11234 if (last_subfile != current_subfile)
11235 {
11236 addr = gdbarch_addr_bits_remove (gdbarch, address);
11237 if (last_subfile)
11238 (*p_record_line) (last_subfile, 0, addr);
11239 last_subfile = current_subfile;
11240 }
11241 /* Append row to matrix using current values. */
11242 addr = gdbarch_addr_bits_remove (gdbarch, address);
11243 (*p_record_line) (current_subfile, line, addr);
11244 }
11245 }
11246 basic_block = 0;
11247 }
11248 else switch (op_code)
11249 {
11250 case DW_LNS_extended_op:
11251 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11252 &bytes_read);
11253 line_ptr += bytes_read;
11254 extended_end = line_ptr + extended_len;
11255 extended_op = read_1_byte (abfd, line_ptr);
11256 line_ptr += 1;
11257 switch (extended_op)
11258 {
11259 case DW_LNE_end_sequence:
11260 p_record_line = record_line;
11261 end_sequence = 1;
11262 break;
11263 case DW_LNE_set_address:
11264 address = read_address (abfd, line_ptr, cu, &bytes_read);
11265
11266 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11267 {
11268 /* This line table is for a function which has been
11269 GCd by the linker. Ignore it. PR gdb/12528 */
11270
11271 long line_offset
11272 = line_ptr - dwarf2_per_objfile->line.buffer;
11273
11274 complaint (&symfile_complaints,
11275 _(".debug_line address at offset 0x%lx is 0 "
11276 "[in module %s]"),
11277 line_offset, cu->objfile->name);
11278 p_record_line = noop_record_line;
11279 }
11280
11281 op_index = 0;
11282 line_ptr += bytes_read;
11283 address += baseaddr;
11284 break;
11285 case DW_LNE_define_file:
11286 {
11287 char *cur_file;
11288 unsigned int dir_index, mod_time, length;
11289
11290 cur_file = read_direct_string (abfd, line_ptr,
11291 &bytes_read);
11292 line_ptr += bytes_read;
11293 dir_index =
11294 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11295 line_ptr += bytes_read;
11296 mod_time =
11297 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11298 line_ptr += bytes_read;
11299 length =
11300 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11301 line_ptr += bytes_read;
11302 add_file_name (lh, cur_file, dir_index, mod_time, length);
11303 }
11304 break;
11305 case DW_LNE_set_discriminator:
11306 /* The discriminator is not interesting to the debugger;
11307 just ignore it. */
11308 line_ptr = extended_end;
11309 break;
11310 default:
11311 complaint (&symfile_complaints,
11312 _("mangled .debug_line section"));
11313 return;
11314 }
11315 /* Make sure that we parsed the extended op correctly. If e.g.
11316 we expected a different address size than the producer used,
11317 we may have read the wrong number of bytes. */
11318 if (line_ptr != extended_end)
11319 {
11320 complaint (&symfile_complaints,
11321 _("mangled .debug_line section"));
11322 return;
11323 }
11324 break;
11325 case DW_LNS_copy:
11326 if (lh->num_file_names < file || file == 0)
11327 dwarf2_debug_line_missing_file_complaint ();
11328 else
11329 {
11330 lh->file_names[file - 1].included_p = 1;
11331 if (!decode_for_pst_p && is_stmt)
11332 {
11333 if (last_subfile != current_subfile)
11334 {
11335 addr = gdbarch_addr_bits_remove (gdbarch, address);
11336 if (last_subfile)
11337 (*p_record_line) (last_subfile, 0, addr);
11338 last_subfile = current_subfile;
11339 }
11340 addr = gdbarch_addr_bits_remove (gdbarch, address);
11341 (*p_record_line) (current_subfile, line, addr);
11342 }
11343 }
11344 basic_block = 0;
11345 break;
11346 case DW_LNS_advance_pc:
11347 {
11348 CORE_ADDR adjust
11349 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11350
11351 address += (((op_index + adjust)
11352 / lh->maximum_ops_per_instruction)
11353 * lh->minimum_instruction_length);
11354 op_index = ((op_index + adjust)
11355 % lh->maximum_ops_per_instruction);
11356 line_ptr += bytes_read;
11357 }
11358 break;
11359 case DW_LNS_advance_line:
11360 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11361 line_ptr += bytes_read;
11362 break;
11363 case DW_LNS_set_file:
11364 {
11365 /* The arrays lh->include_dirs and lh->file_names are
11366 0-based, but the directory and file name numbers in
11367 the statement program are 1-based. */
11368 struct file_entry *fe;
11369 char *dir = NULL;
11370
11371 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11372 line_ptr += bytes_read;
11373 if (lh->num_file_names < file || file == 0)
11374 dwarf2_debug_line_missing_file_complaint ();
11375 else
11376 {
11377 fe = &lh->file_names[file - 1];
11378 if (fe->dir_index)
11379 dir = lh->include_dirs[fe->dir_index - 1];
11380 if (!decode_for_pst_p)
11381 {
11382 last_subfile = current_subfile;
11383 dwarf2_start_subfile (fe->name, dir, comp_dir);
11384 }
11385 }
11386 }
11387 break;
11388 case DW_LNS_set_column:
11389 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11390 line_ptr += bytes_read;
11391 break;
11392 case DW_LNS_negate_stmt:
11393 is_stmt = (!is_stmt);
11394 break;
11395 case DW_LNS_set_basic_block:
11396 basic_block = 1;
11397 break;
11398 /* Add to the address register of the state machine the
11399 address increment value corresponding to special opcode
11400 255. I.e., this value is scaled by the minimum
11401 instruction length since special opcode 255 would have
11402 scaled the increment. */
11403 case DW_LNS_const_add_pc:
11404 {
11405 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11406
11407 address += (((op_index + adjust)
11408 / lh->maximum_ops_per_instruction)
11409 * lh->minimum_instruction_length);
11410 op_index = ((op_index + adjust)
11411 % lh->maximum_ops_per_instruction);
11412 }
11413 break;
11414 case DW_LNS_fixed_advance_pc:
11415 address += read_2_bytes (abfd, line_ptr);
11416 op_index = 0;
11417 line_ptr += 2;
11418 break;
11419 default:
11420 {
11421 /* Unknown standard opcode, ignore it. */
11422 int i;
11423
11424 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11425 {
11426 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11427 line_ptr += bytes_read;
11428 }
11429 }
11430 }
11431 }
11432 if (lh->num_file_names < file || file == 0)
11433 dwarf2_debug_line_missing_file_complaint ();
11434 else
11435 {
11436 lh->file_names[file - 1].included_p = 1;
11437 if (!decode_for_pst_p)
11438 {
11439 addr = gdbarch_addr_bits_remove (gdbarch, address);
11440 (*p_record_line) (current_subfile, 0, addr);
11441 }
11442 }
11443 }
11444
11445 if (decode_for_pst_p)
11446 {
11447 int file_index;
11448
11449 /* Now that we're done scanning the Line Header Program, we can
11450 create the psymtab of each included file. */
11451 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11452 if (lh->file_names[file_index].included_p == 1)
11453 {
11454 char *include_name =
11455 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11456 if (include_name != NULL)
11457 dwarf2_create_include_psymtab (include_name, pst, objfile);
11458 }
11459 }
11460 else
11461 {
11462 /* Make sure a symtab is created for every file, even files
11463 which contain only variables (i.e. no code with associated
11464 line numbers). */
11465
11466 int i;
11467 struct file_entry *fe;
11468
11469 for (i = 0; i < lh->num_file_names; i++)
11470 {
11471 char *dir = NULL;
11472
11473 fe = &lh->file_names[i];
11474 if (fe->dir_index)
11475 dir = lh->include_dirs[fe->dir_index - 1];
11476 dwarf2_start_subfile (fe->name, dir, comp_dir);
11477
11478 /* Skip the main file; we don't need it, and it must be
11479 allocated last, so that it will show up before the
11480 non-primary symtabs in the objfile's symtab list. */
11481 if (current_subfile == first_subfile)
11482 continue;
11483
11484 if (current_subfile->symtab == NULL)
11485 current_subfile->symtab = allocate_symtab (current_subfile->name,
11486 cu->objfile);
11487 fe->symtab = current_subfile->symtab;
11488 }
11489 }
11490 }
11491
11492 /* Start a subfile for DWARF. FILENAME is the name of the file and
11493 DIRNAME the name of the source directory which contains FILENAME
11494 or NULL if not known. COMP_DIR is the compilation directory for the
11495 linetable's compilation unit or NULL if not known.
11496 This routine tries to keep line numbers from identical absolute and
11497 relative file names in a common subfile.
11498
11499 Using the `list' example from the GDB testsuite, which resides in
11500 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11501 of /srcdir/list0.c yields the following debugging information for list0.c:
11502
11503 DW_AT_name: /srcdir/list0.c
11504 DW_AT_comp_dir: /compdir
11505 files.files[0].name: list0.h
11506 files.files[0].dir: /srcdir
11507 files.files[1].name: list0.c
11508 files.files[1].dir: /srcdir
11509
11510 The line number information for list0.c has to end up in a single
11511 subfile, so that `break /srcdir/list0.c:1' works as expected.
11512 start_subfile will ensure that this happens provided that we pass the
11513 concatenation of files.files[1].dir and files.files[1].name as the
11514 subfile's name. */
11515
11516 static void
11517 dwarf2_start_subfile (char *filename, const char *dirname,
11518 const char *comp_dir)
11519 {
11520 char *fullname;
11521
11522 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11523 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11524 second argument to start_subfile. To be consistent, we do the
11525 same here. In order not to lose the line information directory,
11526 we concatenate it to the filename when it makes sense.
11527 Note that the Dwarf3 standard says (speaking of filenames in line
11528 information): ``The directory index is ignored for file names
11529 that represent full path names''. Thus ignoring dirname in the
11530 `else' branch below isn't an issue. */
11531
11532 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11533 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11534 else
11535 fullname = filename;
11536
11537 start_subfile (fullname, comp_dir);
11538
11539 if (fullname != filename)
11540 xfree (fullname);
11541 }
11542
11543 static void
11544 var_decode_location (struct attribute *attr, struct symbol *sym,
11545 struct dwarf2_cu *cu)
11546 {
11547 struct objfile *objfile = cu->objfile;
11548 struct comp_unit_head *cu_header = &cu->header;
11549
11550 /* NOTE drow/2003-01-30: There used to be a comment and some special
11551 code here to turn a symbol with DW_AT_external and a
11552 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11553 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11554 with some versions of binutils) where shared libraries could have
11555 relocations against symbols in their debug information - the
11556 minimal symbol would have the right address, but the debug info
11557 would not. It's no longer necessary, because we will explicitly
11558 apply relocations when we read in the debug information now. */
11559
11560 /* A DW_AT_location attribute with no contents indicates that a
11561 variable has been optimized away. */
11562 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11563 {
11564 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11565 return;
11566 }
11567
11568 /* Handle one degenerate form of location expression specially, to
11569 preserve GDB's previous behavior when section offsets are
11570 specified. If this is just a DW_OP_addr then mark this symbol
11571 as LOC_STATIC. */
11572
11573 if (attr_form_is_block (attr)
11574 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11575 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11576 {
11577 unsigned int dummy;
11578
11579 SYMBOL_VALUE_ADDRESS (sym) =
11580 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11581 SYMBOL_CLASS (sym) = LOC_STATIC;
11582 fixup_symbol_section (sym, objfile);
11583 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11584 SYMBOL_SECTION (sym));
11585 return;
11586 }
11587
11588 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11589 expression evaluator, and use LOC_COMPUTED only when necessary
11590 (i.e. when the value of a register or memory location is
11591 referenced, or a thread-local block, etc.). Then again, it might
11592 not be worthwhile. I'm assuming that it isn't unless performance
11593 or memory numbers show me otherwise. */
11594
11595 dwarf2_symbol_mark_computed (attr, sym, cu);
11596 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11597
11598 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11599 cu->has_loclist = 1;
11600 }
11601
11602 /* Given a pointer to a DWARF information entry, figure out if we need
11603 to make a symbol table entry for it, and if so, create a new entry
11604 and return a pointer to it.
11605 If TYPE is NULL, determine symbol type from the die, otherwise
11606 used the passed type.
11607 If SPACE is not NULL, use it to hold the new symbol. If it is
11608 NULL, allocate a new symbol on the objfile's obstack. */
11609
11610 static struct symbol *
11611 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11612 struct symbol *space)
11613 {
11614 struct objfile *objfile = cu->objfile;
11615 struct symbol *sym = NULL;
11616 char *name;
11617 struct attribute *attr = NULL;
11618 struct attribute *attr2 = NULL;
11619 CORE_ADDR baseaddr;
11620 struct pending **list_to_add = NULL;
11621
11622 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11623
11624 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11625
11626 name = dwarf2_name (die, cu);
11627 if (name)
11628 {
11629 const char *linkagename;
11630 int suppress_add = 0;
11631
11632 if (space)
11633 sym = space;
11634 else
11635 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11636 OBJSTAT (objfile, n_syms++);
11637
11638 /* Cache this symbol's name and the name's demangled form (if any). */
11639 SYMBOL_SET_LANGUAGE (sym, cu->language);
11640 linkagename = dwarf2_physname (name, die, cu);
11641 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11642
11643 /* Fortran does not have mangling standard and the mangling does differ
11644 between gfortran, iFort etc. */
11645 if (cu->language == language_fortran
11646 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11647 symbol_set_demangled_name (&(sym->ginfo),
11648 (char *) dwarf2_full_name (name, die, cu),
11649 NULL);
11650
11651 /* Default assumptions.
11652 Use the passed type or decode it from the die. */
11653 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11654 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11655 if (type != NULL)
11656 SYMBOL_TYPE (sym) = type;
11657 else
11658 SYMBOL_TYPE (sym) = die_type (die, cu);
11659 attr = dwarf2_attr (die,
11660 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11661 cu);
11662 if (attr)
11663 {
11664 SYMBOL_LINE (sym) = DW_UNSND (attr);
11665 }
11666
11667 attr = dwarf2_attr (die,
11668 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11669 cu);
11670 if (attr)
11671 {
11672 int file_index = DW_UNSND (attr);
11673
11674 if (cu->line_header == NULL
11675 || file_index > cu->line_header->num_file_names)
11676 complaint (&symfile_complaints,
11677 _("file index out of range"));
11678 else if (file_index > 0)
11679 {
11680 struct file_entry *fe;
11681
11682 fe = &cu->line_header->file_names[file_index - 1];
11683 SYMBOL_SYMTAB (sym) = fe->symtab;
11684 }
11685 }
11686
11687 switch (die->tag)
11688 {
11689 case DW_TAG_label:
11690 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11691 if (attr)
11692 {
11693 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11694 }
11695 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11696 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11697 SYMBOL_CLASS (sym) = LOC_LABEL;
11698 add_symbol_to_list (sym, cu->list_in_scope);
11699 break;
11700 case DW_TAG_subprogram:
11701 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11702 finish_block. */
11703 SYMBOL_CLASS (sym) = LOC_BLOCK;
11704 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11705 if ((attr2 && (DW_UNSND (attr2) != 0))
11706 || cu->language == language_ada)
11707 {
11708 /* Subprograms marked external are stored as a global symbol.
11709 Ada subprograms, whether marked external or not, are always
11710 stored as a global symbol, because we want to be able to
11711 access them globally. For instance, we want to be able
11712 to break on a nested subprogram without having to
11713 specify the context. */
11714 list_to_add = &global_symbols;
11715 }
11716 else
11717 {
11718 list_to_add = cu->list_in_scope;
11719 }
11720 break;
11721 case DW_TAG_inlined_subroutine:
11722 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11723 finish_block. */
11724 SYMBOL_CLASS (sym) = LOC_BLOCK;
11725 SYMBOL_INLINED (sym) = 1;
11726 /* Do not add the symbol to any lists. It will be found via
11727 BLOCK_FUNCTION from the blockvector. */
11728 break;
11729 case DW_TAG_template_value_param:
11730 suppress_add = 1;
11731 /* Fall through. */
11732 case DW_TAG_constant:
11733 case DW_TAG_variable:
11734 case DW_TAG_member:
11735 /* Compilation with minimal debug info may result in
11736 variables with missing type entries. Change the
11737 misleading `void' type to something sensible. */
11738 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11739 SYMBOL_TYPE (sym)
11740 = objfile_type (objfile)->nodebug_data_symbol;
11741
11742 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11743 /* In the case of DW_TAG_member, we should only be called for
11744 static const members. */
11745 if (die->tag == DW_TAG_member)
11746 {
11747 /* dwarf2_add_field uses die_is_declaration,
11748 so we do the same. */
11749 gdb_assert (die_is_declaration (die, cu));
11750 gdb_assert (attr);
11751 }
11752 if (attr)
11753 {
11754 dwarf2_const_value (attr, sym, cu);
11755 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11756 if (!suppress_add)
11757 {
11758 if (attr2 && (DW_UNSND (attr2) != 0))
11759 list_to_add = &global_symbols;
11760 else
11761 list_to_add = cu->list_in_scope;
11762 }
11763 break;
11764 }
11765 attr = dwarf2_attr (die, DW_AT_location, cu);
11766 if (attr)
11767 {
11768 var_decode_location (attr, sym, cu);
11769 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11770 if (SYMBOL_CLASS (sym) == LOC_STATIC
11771 && SYMBOL_VALUE_ADDRESS (sym) == 0
11772 && !dwarf2_per_objfile->has_section_at_zero)
11773 {
11774 /* When a static variable is eliminated by the linker,
11775 the corresponding debug information is not stripped
11776 out, but the variable address is set to null;
11777 do not add such variables into symbol table. */
11778 }
11779 else if (attr2 && (DW_UNSND (attr2) != 0))
11780 {
11781 /* Workaround gfortran PR debug/40040 - it uses
11782 DW_AT_location for variables in -fPIC libraries which may
11783 get overriden by other libraries/executable and get
11784 a different address. Resolve it by the minimal symbol
11785 which may come from inferior's executable using copy
11786 relocation. Make this workaround only for gfortran as for
11787 other compilers GDB cannot guess the minimal symbol
11788 Fortran mangling kind. */
11789 if (cu->language == language_fortran && die->parent
11790 && die->parent->tag == DW_TAG_module
11791 && cu->producer
11792 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11793 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11794
11795 /* A variable with DW_AT_external is never static,
11796 but it may be block-scoped. */
11797 list_to_add = (cu->list_in_scope == &file_symbols
11798 ? &global_symbols : cu->list_in_scope);
11799 }
11800 else
11801 list_to_add = cu->list_in_scope;
11802 }
11803 else
11804 {
11805 /* We do not know the address of this symbol.
11806 If it is an external symbol and we have type information
11807 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11808 The address of the variable will then be determined from
11809 the minimal symbol table whenever the variable is
11810 referenced. */
11811 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11812 if (attr2 && (DW_UNSND (attr2) != 0)
11813 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11814 {
11815 /* A variable with DW_AT_external is never static, but it
11816 may be block-scoped. */
11817 list_to_add = (cu->list_in_scope == &file_symbols
11818 ? &global_symbols : cu->list_in_scope);
11819
11820 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11821 }
11822 else if (!die_is_declaration (die, cu))
11823 {
11824 /* Use the default LOC_OPTIMIZED_OUT class. */
11825 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11826 if (!suppress_add)
11827 list_to_add = cu->list_in_scope;
11828 }
11829 }
11830 break;
11831 case DW_TAG_formal_parameter:
11832 /* If we are inside a function, mark this as an argument. If
11833 not, we might be looking at an argument to an inlined function
11834 when we do not have enough information to show inlined frames;
11835 pretend it's a local variable in that case so that the user can
11836 still see it. */
11837 if (context_stack_depth > 0
11838 && context_stack[context_stack_depth - 1].name != NULL)
11839 SYMBOL_IS_ARGUMENT (sym) = 1;
11840 attr = dwarf2_attr (die, DW_AT_location, cu);
11841 if (attr)
11842 {
11843 var_decode_location (attr, sym, cu);
11844 }
11845 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11846 if (attr)
11847 {
11848 dwarf2_const_value (attr, sym, cu);
11849 }
11850
11851 list_to_add = cu->list_in_scope;
11852 break;
11853 case DW_TAG_unspecified_parameters:
11854 /* From varargs functions; gdb doesn't seem to have any
11855 interest in this information, so just ignore it for now.
11856 (FIXME?) */
11857 break;
11858 case DW_TAG_template_type_param:
11859 suppress_add = 1;
11860 /* Fall through. */
11861 case DW_TAG_class_type:
11862 case DW_TAG_interface_type:
11863 case DW_TAG_structure_type:
11864 case DW_TAG_union_type:
11865 case DW_TAG_set_type:
11866 case DW_TAG_enumeration_type:
11867 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11868 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11869
11870 {
11871 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11872 really ever be static objects: otherwise, if you try
11873 to, say, break of a class's method and you're in a file
11874 which doesn't mention that class, it won't work unless
11875 the check for all static symbols in lookup_symbol_aux
11876 saves you. See the OtherFileClass tests in
11877 gdb.c++/namespace.exp. */
11878
11879 if (!suppress_add)
11880 {
11881 list_to_add = (cu->list_in_scope == &file_symbols
11882 && (cu->language == language_cplus
11883 || cu->language == language_java)
11884 ? &global_symbols : cu->list_in_scope);
11885
11886 /* The semantics of C++ state that "struct foo {
11887 ... }" also defines a typedef for "foo". A Java
11888 class declaration also defines a typedef for the
11889 class. */
11890 if (cu->language == language_cplus
11891 || cu->language == language_java
11892 || cu->language == language_ada)
11893 {
11894 /* The symbol's name is already allocated along
11895 with this objfile, so we don't need to
11896 duplicate it for the type. */
11897 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11898 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11899 }
11900 }
11901 }
11902 break;
11903 case DW_TAG_typedef:
11904 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11905 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11906 list_to_add = cu->list_in_scope;
11907 break;
11908 case DW_TAG_base_type:
11909 case DW_TAG_subrange_type:
11910 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11911 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11912 list_to_add = cu->list_in_scope;
11913 break;
11914 case DW_TAG_enumerator:
11915 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11916 if (attr)
11917 {
11918 dwarf2_const_value (attr, sym, cu);
11919 }
11920 {
11921 /* NOTE: carlton/2003-11-10: See comment above in the
11922 DW_TAG_class_type, etc. block. */
11923
11924 list_to_add = (cu->list_in_scope == &file_symbols
11925 && (cu->language == language_cplus
11926 || cu->language == language_java)
11927 ? &global_symbols : cu->list_in_scope);
11928 }
11929 break;
11930 case DW_TAG_namespace:
11931 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11932 list_to_add = &global_symbols;
11933 break;
11934 default:
11935 /* Not a tag we recognize. Hopefully we aren't processing
11936 trash data, but since we must specifically ignore things
11937 we don't recognize, there is nothing else we should do at
11938 this point. */
11939 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11940 dwarf_tag_name (die->tag));
11941 break;
11942 }
11943
11944 if (suppress_add)
11945 {
11946 sym->hash_next = objfile->template_symbols;
11947 objfile->template_symbols = sym;
11948 list_to_add = NULL;
11949 }
11950
11951 if (list_to_add != NULL)
11952 add_symbol_to_list (sym, list_to_add);
11953
11954 /* For the benefit of old versions of GCC, check for anonymous
11955 namespaces based on the demangled name. */
11956 if (!processing_has_namespace_info
11957 && cu->language == language_cplus)
11958 cp_scan_for_anonymous_namespaces (sym, objfile);
11959 }
11960 return (sym);
11961 }
11962
11963 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11964
11965 static struct symbol *
11966 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11967 {
11968 return new_symbol_full (die, type, cu, NULL);
11969 }
11970
11971 /* Given an attr with a DW_FORM_dataN value in host byte order,
11972 zero-extend it as appropriate for the symbol's type. The DWARF
11973 standard (v4) is not entirely clear about the meaning of using
11974 DW_FORM_dataN for a constant with a signed type, where the type is
11975 wider than the data. The conclusion of a discussion on the DWARF
11976 list was that this is unspecified. We choose to always zero-extend
11977 because that is the interpretation long in use by GCC. */
11978
11979 static gdb_byte *
11980 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11981 const char *name, struct obstack *obstack,
11982 struct dwarf2_cu *cu, long *value, int bits)
11983 {
11984 struct objfile *objfile = cu->objfile;
11985 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11986 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11987 LONGEST l = DW_UNSND (attr);
11988
11989 if (bits < sizeof (*value) * 8)
11990 {
11991 l &= ((LONGEST) 1 << bits) - 1;
11992 *value = l;
11993 }
11994 else if (bits == sizeof (*value) * 8)
11995 *value = l;
11996 else
11997 {
11998 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11999 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12000 return bytes;
12001 }
12002
12003 return NULL;
12004 }
12005
12006 /* Read a constant value from an attribute. Either set *VALUE, or if
12007 the value does not fit in *VALUE, set *BYTES - either already
12008 allocated on the objfile obstack, or newly allocated on OBSTACK,
12009 or, set *BATON, if we translated the constant to a location
12010 expression. */
12011
12012 static void
12013 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12014 const char *name, struct obstack *obstack,
12015 struct dwarf2_cu *cu,
12016 long *value, gdb_byte **bytes,
12017 struct dwarf2_locexpr_baton **baton)
12018 {
12019 struct objfile *objfile = cu->objfile;
12020 struct comp_unit_head *cu_header = &cu->header;
12021 struct dwarf_block *blk;
12022 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12023 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12024
12025 *value = 0;
12026 *bytes = NULL;
12027 *baton = NULL;
12028
12029 switch (attr->form)
12030 {
12031 case DW_FORM_addr:
12032 {
12033 gdb_byte *data;
12034
12035 if (TYPE_LENGTH (type) != cu_header->addr_size)
12036 dwarf2_const_value_length_mismatch_complaint (name,
12037 cu_header->addr_size,
12038 TYPE_LENGTH (type));
12039 /* Symbols of this form are reasonably rare, so we just
12040 piggyback on the existing location code rather than writing
12041 a new implementation of symbol_computed_ops. */
12042 *baton = obstack_alloc (&objfile->objfile_obstack,
12043 sizeof (struct dwarf2_locexpr_baton));
12044 (*baton)->per_cu = cu->per_cu;
12045 gdb_assert ((*baton)->per_cu);
12046
12047 (*baton)->size = 2 + cu_header->addr_size;
12048 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12049 (*baton)->data = data;
12050
12051 data[0] = DW_OP_addr;
12052 store_unsigned_integer (&data[1], cu_header->addr_size,
12053 byte_order, DW_ADDR (attr));
12054 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12055 }
12056 break;
12057 case DW_FORM_string:
12058 case DW_FORM_strp:
12059 /* DW_STRING is already allocated on the objfile obstack, point
12060 directly to it. */
12061 *bytes = (gdb_byte *) DW_STRING (attr);
12062 break;
12063 case DW_FORM_block1:
12064 case DW_FORM_block2:
12065 case DW_FORM_block4:
12066 case DW_FORM_block:
12067 case DW_FORM_exprloc:
12068 blk = DW_BLOCK (attr);
12069 if (TYPE_LENGTH (type) != blk->size)
12070 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12071 TYPE_LENGTH (type));
12072 *bytes = blk->data;
12073 break;
12074
12075 /* The DW_AT_const_value attributes are supposed to carry the
12076 symbol's value "represented as it would be on the target
12077 architecture." By the time we get here, it's already been
12078 converted to host endianness, so we just need to sign- or
12079 zero-extend it as appropriate. */
12080 case DW_FORM_data1:
12081 *bytes = dwarf2_const_value_data (attr, type, name,
12082 obstack, cu, value, 8);
12083 break;
12084 case DW_FORM_data2:
12085 *bytes = dwarf2_const_value_data (attr, type, name,
12086 obstack, cu, value, 16);
12087 break;
12088 case DW_FORM_data4:
12089 *bytes = dwarf2_const_value_data (attr, type, name,
12090 obstack, cu, value, 32);
12091 break;
12092 case DW_FORM_data8:
12093 *bytes = dwarf2_const_value_data (attr, type, name,
12094 obstack, cu, value, 64);
12095 break;
12096
12097 case DW_FORM_sdata:
12098 *value = DW_SND (attr);
12099 break;
12100
12101 case DW_FORM_udata:
12102 *value = DW_UNSND (attr);
12103 break;
12104
12105 default:
12106 complaint (&symfile_complaints,
12107 _("unsupported const value attribute form: '%s'"),
12108 dwarf_form_name (attr->form));
12109 *value = 0;
12110 break;
12111 }
12112 }
12113
12114
12115 /* Copy constant value from an attribute to a symbol. */
12116
12117 static void
12118 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12119 struct dwarf2_cu *cu)
12120 {
12121 struct objfile *objfile = cu->objfile;
12122 struct comp_unit_head *cu_header = &cu->header;
12123 long value;
12124 gdb_byte *bytes;
12125 struct dwarf2_locexpr_baton *baton;
12126
12127 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12128 SYMBOL_PRINT_NAME (sym),
12129 &objfile->objfile_obstack, cu,
12130 &value, &bytes, &baton);
12131
12132 if (baton != NULL)
12133 {
12134 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12135 SYMBOL_LOCATION_BATON (sym) = baton;
12136 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12137 }
12138 else if (bytes != NULL)
12139 {
12140 SYMBOL_VALUE_BYTES (sym) = bytes;
12141 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12142 }
12143 else
12144 {
12145 SYMBOL_VALUE (sym) = value;
12146 SYMBOL_CLASS (sym) = LOC_CONST;
12147 }
12148 }
12149
12150 /* Return the type of the die in question using its DW_AT_type attribute. */
12151
12152 static struct type *
12153 die_type (struct die_info *die, struct dwarf2_cu *cu)
12154 {
12155 struct attribute *type_attr;
12156
12157 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12158 if (!type_attr)
12159 {
12160 /* A missing DW_AT_type represents a void type. */
12161 return objfile_type (cu->objfile)->builtin_void;
12162 }
12163
12164 return lookup_die_type (die, type_attr, cu);
12165 }
12166
12167 /* True iff CU's producer generates GNAT Ada auxiliary information
12168 that allows to find parallel types through that information instead
12169 of having to do expensive parallel lookups by type name. */
12170
12171 static int
12172 need_gnat_info (struct dwarf2_cu *cu)
12173 {
12174 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12175 of GNAT produces this auxiliary information, without any indication
12176 that it is produced. Part of enhancing the FSF version of GNAT
12177 to produce that information will be to put in place an indicator
12178 that we can use in order to determine whether the descriptive type
12179 info is available or not. One suggestion that has been made is
12180 to use a new attribute, attached to the CU die. For now, assume
12181 that the descriptive type info is not available. */
12182 return 0;
12183 }
12184
12185 /* Return the auxiliary type of the die in question using its
12186 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12187 attribute is not present. */
12188
12189 static struct type *
12190 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12191 {
12192 struct attribute *type_attr;
12193
12194 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12195 if (!type_attr)
12196 return NULL;
12197
12198 return lookup_die_type (die, type_attr, cu);
12199 }
12200
12201 /* If DIE has a descriptive_type attribute, then set the TYPE's
12202 descriptive type accordingly. */
12203
12204 static void
12205 set_descriptive_type (struct type *type, struct die_info *die,
12206 struct dwarf2_cu *cu)
12207 {
12208 struct type *descriptive_type = die_descriptive_type (die, cu);
12209
12210 if (descriptive_type)
12211 {
12212 ALLOCATE_GNAT_AUX_TYPE (type);
12213 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12214 }
12215 }
12216
12217 /* Return the containing type of the die in question using its
12218 DW_AT_containing_type attribute. */
12219
12220 static struct type *
12221 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12222 {
12223 struct attribute *type_attr;
12224
12225 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12226 if (!type_attr)
12227 error (_("Dwarf Error: Problem turning containing type into gdb type "
12228 "[in module %s]"), cu->objfile->name);
12229
12230 return lookup_die_type (die, type_attr, cu);
12231 }
12232
12233 /* Look up the type of DIE in CU using its type attribute ATTR.
12234 If there is no type substitute an error marker. */
12235
12236 static struct type *
12237 lookup_die_type (struct die_info *die, struct attribute *attr,
12238 struct dwarf2_cu *cu)
12239 {
12240 struct type *this_type;
12241
12242 /* First see if we have it cached. */
12243
12244 if (is_ref_attr (attr))
12245 {
12246 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12247
12248 this_type = get_die_type_at_offset (offset, cu->per_cu);
12249 }
12250 else if (attr->form == DW_FORM_ref_sig8)
12251 {
12252 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12253 struct dwarf2_cu *sig_cu;
12254 unsigned int offset;
12255
12256 /* sig_type will be NULL if the signatured type is missing from
12257 the debug info. */
12258 if (sig_type == NULL)
12259 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12260 "at 0x%x [in module %s]"),
12261 die->offset, cu->objfile->name);
12262
12263 gdb_assert (sig_type->per_cu.debug_types_section);
12264 offset = sig_type->per_cu.offset + sig_type->type_offset;
12265 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12266 }
12267 else
12268 {
12269 dump_die_for_error (die);
12270 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12271 dwarf_attr_name (attr->name), cu->objfile->name);
12272 }
12273
12274 /* If not cached we need to read it in. */
12275
12276 if (this_type == NULL)
12277 {
12278 struct die_info *type_die;
12279 struct dwarf2_cu *type_cu = cu;
12280
12281 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12282 /* If the type is cached, we should have found it above. */
12283 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12284 this_type = read_type_die_1 (type_die, type_cu);
12285 }
12286
12287 /* If we still don't have a type use an error marker. */
12288
12289 if (this_type == NULL)
12290 {
12291 char *message, *saved;
12292
12293 /* read_type_die already issued a complaint. */
12294 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12295 cu->objfile->name,
12296 cu->header.offset,
12297 die->offset);
12298 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12299 message, strlen (message));
12300 xfree (message);
12301
12302 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
12303 }
12304
12305 return this_type;
12306 }
12307
12308 /* Return the type in DIE, CU.
12309 Returns NULL for invalid types.
12310
12311 This first does a lookup in the appropriate type_hash table,
12312 and only reads the die in if necessary.
12313
12314 NOTE: This can be called when reading in partial or full symbols. */
12315
12316 static struct type *
12317 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12318 {
12319 struct type *this_type;
12320
12321 this_type = get_die_type (die, cu);
12322 if (this_type)
12323 return this_type;
12324
12325 return read_type_die_1 (die, cu);
12326 }
12327
12328 /* Read the type in DIE, CU.
12329 Returns NULL for invalid types. */
12330
12331 static struct type *
12332 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12333 {
12334 struct type *this_type = NULL;
12335
12336 switch (die->tag)
12337 {
12338 case DW_TAG_class_type:
12339 case DW_TAG_interface_type:
12340 case DW_TAG_structure_type:
12341 case DW_TAG_union_type:
12342 this_type = read_structure_type (die, cu);
12343 break;
12344 case DW_TAG_enumeration_type:
12345 this_type = read_enumeration_type (die, cu);
12346 break;
12347 case DW_TAG_subprogram:
12348 case DW_TAG_subroutine_type:
12349 case DW_TAG_inlined_subroutine:
12350 this_type = read_subroutine_type (die, cu);
12351 break;
12352 case DW_TAG_array_type:
12353 this_type = read_array_type (die, cu);
12354 break;
12355 case DW_TAG_set_type:
12356 this_type = read_set_type (die, cu);
12357 break;
12358 case DW_TAG_pointer_type:
12359 this_type = read_tag_pointer_type (die, cu);
12360 break;
12361 case DW_TAG_ptr_to_member_type:
12362 this_type = read_tag_ptr_to_member_type (die, cu);
12363 break;
12364 case DW_TAG_reference_type:
12365 this_type = read_tag_reference_type (die, cu);
12366 break;
12367 case DW_TAG_const_type:
12368 this_type = read_tag_const_type (die, cu);
12369 break;
12370 case DW_TAG_volatile_type:
12371 this_type = read_tag_volatile_type (die, cu);
12372 break;
12373 case DW_TAG_string_type:
12374 this_type = read_tag_string_type (die, cu);
12375 break;
12376 case DW_TAG_typedef:
12377 this_type = read_typedef (die, cu);
12378 break;
12379 case DW_TAG_subrange_type:
12380 this_type = read_subrange_type (die, cu);
12381 break;
12382 case DW_TAG_base_type:
12383 this_type = read_base_type (die, cu);
12384 break;
12385 case DW_TAG_unspecified_type:
12386 this_type = read_unspecified_type (die, cu);
12387 break;
12388 case DW_TAG_namespace:
12389 this_type = read_namespace_type (die, cu);
12390 break;
12391 case DW_TAG_module:
12392 this_type = read_module_type (die, cu);
12393 break;
12394 default:
12395 complaint (&symfile_complaints,
12396 _("unexpected tag in read_type_die: '%s'"),
12397 dwarf_tag_name (die->tag));
12398 break;
12399 }
12400
12401 return this_type;
12402 }
12403
12404 /* See if we can figure out if the class lives in a namespace. We do
12405 this by looking for a member function; its demangled name will
12406 contain namespace info, if there is any.
12407 Return the computed name or NULL.
12408 Space for the result is allocated on the objfile's obstack.
12409 This is the full-die version of guess_partial_die_structure_name.
12410 In this case we know DIE has no useful parent. */
12411
12412 static char *
12413 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12414 {
12415 struct die_info *spec_die;
12416 struct dwarf2_cu *spec_cu;
12417 struct die_info *child;
12418
12419 spec_cu = cu;
12420 spec_die = die_specification (die, &spec_cu);
12421 if (spec_die != NULL)
12422 {
12423 die = spec_die;
12424 cu = spec_cu;
12425 }
12426
12427 for (child = die->child;
12428 child != NULL;
12429 child = child->sibling)
12430 {
12431 if (child->tag == DW_TAG_subprogram)
12432 {
12433 struct attribute *attr;
12434
12435 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12436 if (attr == NULL)
12437 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12438 if (attr != NULL)
12439 {
12440 char *actual_name
12441 = language_class_name_from_physname (cu->language_defn,
12442 DW_STRING (attr));
12443 char *name = NULL;
12444
12445 if (actual_name != NULL)
12446 {
12447 char *die_name = dwarf2_name (die, cu);
12448
12449 if (die_name != NULL
12450 && strcmp (die_name, actual_name) != 0)
12451 {
12452 /* Strip off the class name from the full name.
12453 We want the prefix. */
12454 int die_name_len = strlen (die_name);
12455 int actual_name_len = strlen (actual_name);
12456
12457 /* Test for '::' as a sanity check. */
12458 if (actual_name_len > die_name_len + 2
12459 && actual_name[actual_name_len
12460 - die_name_len - 1] == ':')
12461 name =
12462 obsavestring (actual_name,
12463 actual_name_len - die_name_len - 2,
12464 &cu->objfile->objfile_obstack);
12465 }
12466 }
12467 xfree (actual_name);
12468 return name;
12469 }
12470 }
12471 }
12472
12473 return NULL;
12474 }
12475
12476 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12477 prefix part in such case. See
12478 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12479
12480 static char *
12481 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12482 {
12483 struct attribute *attr;
12484 char *base;
12485
12486 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12487 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12488 return NULL;
12489
12490 attr = dwarf2_attr (die, DW_AT_name, cu);
12491 if (attr != NULL && DW_STRING (attr) != NULL)
12492 return NULL;
12493
12494 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12495 if (attr == NULL)
12496 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12497 if (attr == NULL || DW_STRING (attr) == NULL)
12498 return NULL;
12499
12500 /* dwarf2_name had to be already called. */
12501 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12502
12503 /* Strip the base name, keep any leading namespaces/classes. */
12504 base = strrchr (DW_STRING (attr), ':');
12505 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12506 return "";
12507
12508 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12509 &cu->objfile->objfile_obstack);
12510 }
12511
12512 /* Return the name of the namespace/class that DIE is defined within,
12513 or "" if we can't tell. The caller should not xfree the result.
12514
12515 For example, if we're within the method foo() in the following
12516 code:
12517
12518 namespace N {
12519 class C {
12520 void foo () {
12521 }
12522 };
12523 }
12524
12525 then determine_prefix on foo's die will return "N::C". */
12526
12527 static char *
12528 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12529 {
12530 struct die_info *parent, *spec_die;
12531 struct dwarf2_cu *spec_cu;
12532 struct type *parent_type;
12533 char *retval;
12534
12535 if (cu->language != language_cplus && cu->language != language_java
12536 && cu->language != language_fortran)
12537 return "";
12538
12539 retval = anonymous_struct_prefix (die, cu);
12540 if (retval)
12541 return retval;
12542
12543 /* We have to be careful in the presence of DW_AT_specification.
12544 For example, with GCC 3.4, given the code
12545
12546 namespace N {
12547 void foo() {
12548 // Definition of N::foo.
12549 }
12550 }
12551
12552 then we'll have a tree of DIEs like this:
12553
12554 1: DW_TAG_compile_unit
12555 2: DW_TAG_namespace // N
12556 3: DW_TAG_subprogram // declaration of N::foo
12557 4: DW_TAG_subprogram // definition of N::foo
12558 DW_AT_specification // refers to die #3
12559
12560 Thus, when processing die #4, we have to pretend that we're in
12561 the context of its DW_AT_specification, namely the contex of die
12562 #3. */
12563 spec_cu = cu;
12564 spec_die = die_specification (die, &spec_cu);
12565 if (spec_die == NULL)
12566 parent = die->parent;
12567 else
12568 {
12569 parent = spec_die->parent;
12570 cu = spec_cu;
12571 }
12572
12573 if (parent == NULL)
12574 return "";
12575 else if (parent->building_fullname)
12576 {
12577 const char *name;
12578 const char *parent_name;
12579
12580 /* It has been seen on RealView 2.2 built binaries,
12581 DW_TAG_template_type_param types actually _defined_ as
12582 children of the parent class:
12583
12584 enum E {};
12585 template class <class Enum> Class{};
12586 Class<enum E> class_e;
12587
12588 1: DW_TAG_class_type (Class)
12589 2: DW_TAG_enumeration_type (E)
12590 3: DW_TAG_enumerator (enum1:0)
12591 3: DW_TAG_enumerator (enum2:1)
12592 ...
12593 2: DW_TAG_template_type_param
12594 DW_AT_type DW_FORM_ref_udata (E)
12595
12596 Besides being broken debug info, it can put GDB into an
12597 infinite loop. Consider:
12598
12599 When we're building the full name for Class<E>, we'll start
12600 at Class, and go look over its template type parameters,
12601 finding E. We'll then try to build the full name of E, and
12602 reach here. We're now trying to build the full name of E,
12603 and look over the parent DIE for containing scope. In the
12604 broken case, if we followed the parent DIE of E, we'd again
12605 find Class, and once again go look at its template type
12606 arguments, etc., etc. Simply don't consider such parent die
12607 as source-level parent of this die (it can't be, the language
12608 doesn't allow it), and break the loop here. */
12609 name = dwarf2_name (die, cu);
12610 parent_name = dwarf2_name (parent, cu);
12611 complaint (&symfile_complaints,
12612 _("template param type '%s' defined within parent '%s'"),
12613 name ? name : "<unknown>",
12614 parent_name ? parent_name : "<unknown>");
12615 return "";
12616 }
12617 else
12618 switch (parent->tag)
12619 {
12620 case DW_TAG_namespace:
12621 parent_type = read_type_die (parent, cu);
12622 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12623 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12624 Work around this problem here. */
12625 if (cu->language == language_cplus
12626 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12627 return "";
12628 /* We give a name to even anonymous namespaces. */
12629 return TYPE_TAG_NAME (parent_type);
12630 case DW_TAG_class_type:
12631 case DW_TAG_interface_type:
12632 case DW_TAG_structure_type:
12633 case DW_TAG_union_type:
12634 case DW_TAG_module:
12635 parent_type = read_type_die (parent, cu);
12636 if (TYPE_TAG_NAME (parent_type) != NULL)
12637 return TYPE_TAG_NAME (parent_type);
12638 else
12639 /* An anonymous structure is only allowed non-static data
12640 members; no typedefs, no member functions, et cetera.
12641 So it does not need a prefix. */
12642 return "";
12643 case DW_TAG_compile_unit:
12644 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12645 if (cu->language == language_cplus
12646 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12647 && die->child != NULL
12648 && (die->tag == DW_TAG_class_type
12649 || die->tag == DW_TAG_structure_type
12650 || die->tag == DW_TAG_union_type))
12651 {
12652 char *name = guess_full_die_structure_name (die, cu);
12653 if (name != NULL)
12654 return name;
12655 }
12656 return "";
12657 default:
12658 return determine_prefix (parent, cu);
12659 }
12660 }
12661
12662 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12663 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12664 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12665 an obconcat, otherwise allocate storage for the result. The CU argument is
12666 used to determine the language and hence, the appropriate separator. */
12667
12668 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12669
12670 static char *
12671 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12672 int physname, struct dwarf2_cu *cu)
12673 {
12674 const char *lead = "";
12675 const char *sep;
12676
12677 if (suffix == NULL || suffix[0] == '\0'
12678 || prefix == NULL || prefix[0] == '\0')
12679 sep = "";
12680 else if (cu->language == language_java)
12681 sep = ".";
12682 else if (cu->language == language_fortran && physname)
12683 {
12684 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12685 DW_AT_MIPS_linkage_name is preferred and used instead. */
12686
12687 lead = "__";
12688 sep = "_MOD_";
12689 }
12690 else
12691 sep = "::";
12692
12693 if (prefix == NULL)
12694 prefix = "";
12695 if (suffix == NULL)
12696 suffix = "";
12697
12698 if (obs == NULL)
12699 {
12700 char *retval
12701 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12702
12703 strcpy (retval, lead);
12704 strcat (retval, prefix);
12705 strcat (retval, sep);
12706 strcat (retval, suffix);
12707 return retval;
12708 }
12709 else
12710 {
12711 /* We have an obstack. */
12712 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12713 }
12714 }
12715
12716 /* Return sibling of die, NULL if no sibling. */
12717
12718 static struct die_info *
12719 sibling_die (struct die_info *die)
12720 {
12721 return die->sibling;
12722 }
12723
12724 /* Get name of a die, return NULL if not found. */
12725
12726 static char *
12727 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12728 struct obstack *obstack)
12729 {
12730 if (name && cu->language == language_cplus)
12731 {
12732 char *canon_name = cp_canonicalize_string (name);
12733
12734 if (canon_name != NULL)
12735 {
12736 if (strcmp (canon_name, name) != 0)
12737 name = obsavestring (canon_name, strlen (canon_name),
12738 obstack);
12739 xfree (canon_name);
12740 }
12741 }
12742
12743 return name;
12744 }
12745
12746 /* Get name of a die, return NULL if not found. */
12747
12748 static char *
12749 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12750 {
12751 struct attribute *attr;
12752
12753 attr = dwarf2_attr (die, DW_AT_name, cu);
12754 if ((!attr || !DW_STRING (attr))
12755 && die->tag != DW_TAG_class_type
12756 && die->tag != DW_TAG_interface_type
12757 && die->tag != DW_TAG_structure_type
12758 && die->tag != DW_TAG_union_type)
12759 return NULL;
12760
12761 switch (die->tag)
12762 {
12763 case DW_TAG_compile_unit:
12764 /* Compilation units have a DW_AT_name that is a filename, not
12765 a source language identifier. */
12766 case DW_TAG_enumeration_type:
12767 case DW_TAG_enumerator:
12768 /* These tags always have simple identifiers already; no need
12769 to canonicalize them. */
12770 return DW_STRING (attr);
12771
12772 case DW_TAG_subprogram:
12773 /* Java constructors will all be named "<init>", so return
12774 the class name when we see this special case. */
12775 if (cu->language == language_java
12776 && DW_STRING (attr) != NULL
12777 && strcmp (DW_STRING (attr), "<init>") == 0)
12778 {
12779 struct dwarf2_cu *spec_cu = cu;
12780 struct die_info *spec_die;
12781
12782 /* GCJ will output '<init>' for Java constructor names.
12783 For this special case, return the name of the parent class. */
12784
12785 /* GCJ may output suprogram DIEs with AT_specification set.
12786 If so, use the name of the specified DIE. */
12787 spec_die = die_specification (die, &spec_cu);
12788 if (spec_die != NULL)
12789 return dwarf2_name (spec_die, spec_cu);
12790
12791 do
12792 {
12793 die = die->parent;
12794 if (die->tag == DW_TAG_class_type)
12795 return dwarf2_name (die, cu);
12796 }
12797 while (die->tag != DW_TAG_compile_unit);
12798 }
12799 break;
12800
12801 case DW_TAG_class_type:
12802 case DW_TAG_interface_type:
12803 case DW_TAG_structure_type:
12804 case DW_TAG_union_type:
12805 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12806 structures or unions. These were of the form "._%d" in GCC 4.1,
12807 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12808 and GCC 4.4. We work around this problem by ignoring these. */
12809 if (attr && DW_STRING (attr)
12810 && (strncmp (DW_STRING (attr), "._", 2) == 0
12811 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12812 return NULL;
12813
12814 /* GCC might emit a nameless typedef that has a linkage name. See
12815 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12816 if (!attr || DW_STRING (attr) == NULL)
12817 {
12818 char *demangled = NULL;
12819
12820 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12821 if (attr == NULL)
12822 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12823
12824 if (attr == NULL || DW_STRING (attr) == NULL)
12825 return NULL;
12826
12827 /* Avoid demangling DW_STRING (attr) the second time on a second
12828 call for the same DIE. */
12829 if (!DW_STRING_IS_CANONICAL (attr))
12830 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12831
12832 if (demangled)
12833 {
12834 char *base;
12835
12836 /* FIXME: we already did this for the partial symbol... */
12837 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12838 &cu->objfile->objfile_obstack);
12839 DW_STRING_IS_CANONICAL (attr) = 1;
12840 xfree (demangled);
12841
12842 /* Strip any leading namespaces/classes, keep only the base name.
12843 DW_AT_name for named DIEs does not contain the prefixes. */
12844 base = strrchr (DW_STRING (attr), ':');
12845 if (base && base > DW_STRING (attr) && base[-1] == ':')
12846 return &base[1];
12847 else
12848 return DW_STRING (attr);
12849 }
12850 }
12851 break;
12852
12853 default:
12854 break;
12855 }
12856
12857 if (!DW_STRING_IS_CANONICAL (attr))
12858 {
12859 DW_STRING (attr)
12860 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12861 &cu->objfile->objfile_obstack);
12862 DW_STRING_IS_CANONICAL (attr) = 1;
12863 }
12864 return DW_STRING (attr);
12865 }
12866
12867 /* Return the die that this die in an extension of, or NULL if there
12868 is none. *EXT_CU is the CU containing DIE on input, and the CU
12869 containing the return value on output. */
12870
12871 static struct die_info *
12872 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12873 {
12874 struct attribute *attr;
12875
12876 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12877 if (attr == NULL)
12878 return NULL;
12879
12880 return follow_die_ref (die, attr, ext_cu);
12881 }
12882
12883 /* Convert a DIE tag into its string name. */
12884
12885 static char *
12886 dwarf_tag_name (unsigned tag)
12887 {
12888 switch (tag)
12889 {
12890 case DW_TAG_padding:
12891 return "DW_TAG_padding";
12892 case DW_TAG_array_type:
12893 return "DW_TAG_array_type";
12894 case DW_TAG_class_type:
12895 return "DW_TAG_class_type";
12896 case DW_TAG_entry_point:
12897 return "DW_TAG_entry_point";
12898 case DW_TAG_enumeration_type:
12899 return "DW_TAG_enumeration_type";
12900 case DW_TAG_formal_parameter:
12901 return "DW_TAG_formal_parameter";
12902 case DW_TAG_imported_declaration:
12903 return "DW_TAG_imported_declaration";
12904 case DW_TAG_label:
12905 return "DW_TAG_label";
12906 case DW_TAG_lexical_block:
12907 return "DW_TAG_lexical_block";
12908 case DW_TAG_member:
12909 return "DW_TAG_member";
12910 case DW_TAG_pointer_type:
12911 return "DW_TAG_pointer_type";
12912 case DW_TAG_reference_type:
12913 return "DW_TAG_reference_type";
12914 case DW_TAG_compile_unit:
12915 return "DW_TAG_compile_unit";
12916 case DW_TAG_string_type:
12917 return "DW_TAG_string_type";
12918 case DW_TAG_structure_type:
12919 return "DW_TAG_structure_type";
12920 case DW_TAG_subroutine_type:
12921 return "DW_TAG_subroutine_type";
12922 case DW_TAG_typedef:
12923 return "DW_TAG_typedef";
12924 case DW_TAG_union_type:
12925 return "DW_TAG_union_type";
12926 case DW_TAG_unspecified_parameters:
12927 return "DW_TAG_unspecified_parameters";
12928 case DW_TAG_variant:
12929 return "DW_TAG_variant";
12930 case DW_TAG_common_block:
12931 return "DW_TAG_common_block";
12932 case DW_TAG_common_inclusion:
12933 return "DW_TAG_common_inclusion";
12934 case DW_TAG_inheritance:
12935 return "DW_TAG_inheritance";
12936 case DW_TAG_inlined_subroutine:
12937 return "DW_TAG_inlined_subroutine";
12938 case DW_TAG_module:
12939 return "DW_TAG_module";
12940 case DW_TAG_ptr_to_member_type:
12941 return "DW_TAG_ptr_to_member_type";
12942 case DW_TAG_set_type:
12943 return "DW_TAG_set_type";
12944 case DW_TAG_subrange_type:
12945 return "DW_TAG_subrange_type";
12946 case DW_TAG_with_stmt:
12947 return "DW_TAG_with_stmt";
12948 case DW_TAG_access_declaration:
12949 return "DW_TAG_access_declaration";
12950 case DW_TAG_base_type:
12951 return "DW_TAG_base_type";
12952 case DW_TAG_catch_block:
12953 return "DW_TAG_catch_block";
12954 case DW_TAG_const_type:
12955 return "DW_TAG_const_type";
12956 case DW_TAG_constant:
12957 return "DW_TAG_constant";
12958 case DW_TAG_enumerator:
12959 return "DW_TAG_enumerator";
12960 case DW_TAG_file_type:
12961 return "DW_TAG_file_type";
12962 case DW_TAG_friend:
12963 return "DW_TAG_friend";
12964 case DW_TAG_namelist:
12965 return "DW_TAG_namelist";
12966 case DW_TAG_namelist_item:
12967 return "DW_TAG_namelist_item";
12968 case DW_TAG_packed_type:
12969 return "DW_TAG_packed_type";
12970 case DW_TAG_subprogram:
12971 return "DW_TAG_subprogram";
12972 case DW_TAG_template_type_param:
12973 return "DW_TAG_template_type_param";
12974 case DW_TAG_template_value_param:
12975 return "DW_TAG_template_value_param";
12976 case DW_TAG_thrown_type:
12977 return "DW_TAG_thrown_type";
12978 case DW_TAG_try_block:
12979 return "DW_TAG_try_block";
12980 case DW_TAG_variant_part:
12981 return "DW_TAG_variant_part";
12982 case DW_TAG_variable:
12983 return "DW_TAG_variable";
12984 case DW_TAG_volatile_type:
12985 return "DW_TAG_volatile_type";
12986 case DW_TAG_dwarf_procedure:
12987 return "DW_TAG_dwarf_procedure";
12988 case DW_TAG_restrict_type:
12989 return "DW_TAG_restrict_type";
12990 case DW_TAG_interface_type:
12991 return "DW_TAG_interface_type";
12992 case DW_TAG_namespace:
12993 return "DW_TAG_namespace";
12994 case DW_TAG_imported_module:
12995 return "DW_TAG_imported_module";
12996 case DW_TAG_unspecified_type:
12997 return "DW_TAG_unspecified_type";
12998 case DW_TAG_partial_unit:
12999 return "DW_TAG_partial_unit";
13000 case DW_TAG_imported_unit:
13001 return "DW_TAG_imported_unit";
13002 case DW_TAG_condition:
13003 return "DW_TAG_condition";
13004 case DW_TAG_shared_type:
13005 return "DW_TAG_shared_type";
13006 case DW_TAG_type_unit:
13007 return "DW_TAG_type_unit";
13008 case DW_TAG_MIPS_loop:
13009 return "DW_TAG_MIPS_loop";
13010 case DW_TAG_HP_array_descriptor:
13011 return "DW_TAG_HP_array_descriptor";
13012 case DW_TAG_format_label:
13013 return "DW_TAG_format_label";
13014 case DW_TAG_function_template:
13015 return "DW_TAG_function_template";
13016 case DW_TAG_class_template:
13017 return "DW_TAG_class_template";
13018 case DW_TAG_GNU_BINCL:
13019 return "DW_TAG_GNU_BINCL";
13020 case DW_TAG_GNU_EINCL:
13021 return "DW_TAG_GNU_EINCL";
13022 case DW_TAG_upc_shared_type:
13023 return "DW_TAG_upc_shared_type";
13024 case DW_TAG_upc_strict_type:
13025 return "DW_TAG_upc_strict_type";
13026 case DW_TAG_upc_relaxed_type:
13027 return "DW_TAG_upc_relaxed_type";
13028 case DW_TAG_PGI_kanji_type:
13029 return "DW_TAG_PGI_kanji_type";
13030 case DW_TAG_PGI_interface_block:
13031 return "DW_TAG_PGI_interface_block";
13032 case DW_TAG_GNU_call_site:
13033 return "DW_TAG_GNU_call_site";
13034 default:
13035 return "DW_TAG_<unknown>";
13036 }
13037 }
13038
13039 /* Convert a DWARF attribute code into its string name. */
13040
13041 static char *
13042 dwarf_attr_name (unsigned attr)
13043 {
13044 switch (attr)
13045 {
13046 case DW_AT_sibling:
13047 return "DW_AT_sibling";
13048 case DW_AT_location:
13049 return "DW_AT_location";
13050 case DW_AT_name:
13051 return "DW_AT_name";
13052 case DW_AT_ordering:
13053 return "DW_AT_ordering";
13054 case DW_AT_subscr_data:
13055 return "DW_AT_subscr_data";
13056 case DW_AT_byte_size:
13057 return "DW_AT_byte_size";
13058 case DW_AT_bit_offset:
13059 return "DW_AT_bit_offset";
13060 case DW_AT_bit_size:
13061 return "DW_AT_bit_size";
13062 case DW_AT_element_list:
13063 return "DW_AT_element_list";
13064 case DW_AT_stmt_list:
13065 return "DW_AT_stmt_list";
13066 case DW_AT_low_pc:
13067 return "DW_AT_low_pc";
13068 case DW_AT_high_pc:
13069 return "DW_AT_high_pc";
13070 case DW_AT_language:
13071 return "DW_AT_language";
13072 case DW_AT_member:
13073 return "DW_AT_member";
13074 case DW_AT_discr:
13075 return "DW_AT_discr";
13076 case DW_AT_discr_value:
13077 return "DW_AT_discr_value";
13078 case DW_AT_visibility:
13079 return "DW_AT_visibility";
13080 case DW_AT_import:
13081 return "DW_AT_import";
13082 case DW_AT_string_length:
13083 return "DW_AT_string_length";
13084 case DW_AT_common_reference:
13085 return "DW_AT_common_reference";
13086 case DW_AT_comp_dir:
13087 return "DW_AT_comp_dir";
13088 case DW_AT_const_value:
13089 return "DW_AT_const_value";
13090 case DW_AT_containing_type:
13091 return "DW_AT_containing_type";
13092 case DW_AT_default_value:
13093 return "DW_AT_default_value";
13094 case DW_AT_inline:
13095 return "DW_AT_inline";
13096 case DW_AT_is_optional:
13097 return "DW_AT_is_optional";
13098 case DW_AT_lower_bound:
13099 return "DW_AT_lower_bound";
13100 case DW_AT_producer:
13101 return "DW_AT_producer";
13102 case DW_AT_prototyped:
13103 return "DW_AT_prototyped";
13104 case DW_AT_return_addr:
13105 return "DW_AT_return_addr";
13106 case DW_AT_start_scope:
13107 return "DW_AT_start_scope";
13108 case DW_AT_bit_stride:
13109 return "DW_AT_bit_stride";
13110 case DW_AT_upper_bound:
13111 return "DW_AT_upper_bound";
13112 case DW_AT_abstract_origin:
13113 return "DW_AT_abstract_origin";
13114 case DW_AT_accessibility:
13115 return "DW_AT_accessibility";
13116 case DW_AT_address_class:
13117 return "DW_AT_address_class";
13118 case DW_AT_artificial:
13119 return "DW_AT_artificial";
13120 case DW_AT_base_types:
13121 return "DW_AT_base_types";
13122 case DW_AT_calling_convention:
13123 return "DW_AT_calling_convention";
13124 case DW_AT_count:
13125 return "DW_AT_count";
13126 case DW_AT_data_member_location:
13127 return "DW_AT_data_member_location";
13128 case DW_AT_decl_column:
13129 return "DW_AT_decl_column";
13130 case DW_AT_decl_file:
13131 return "DW_AT_decl_file";
13132 case DW_AT_decl_line:
13133 return "DW_AT_decl_line";
13134 case DW_AT_declaration:
13135 return "DW_AT_declaration";
13136 case DW_AT_discr_list:
13137 return "DW_AT_discr_list";
13138 case DW_AT_encoding:
13139 return "DW_AT_encoding";
13140 case DW_AT_external:
13141 return "DW_AT_external";
13142 case DW_AT_frame_base:
13143 return "DW_AT_frame_base";
13144 case DW_AT_friend:
13145 return "DW_AT_friend";
13146 case DW_AT_identifier_case:
13147 return "DW_AT_identifier_case";
13148 case DW_AT_macro_info:
13149 return "DW_AT_macro_info";
13150 case DW_AT_namelist_items:
13151 return "DW_AT_namelist_items";
13152 case DW_AT_priority:
13153 return "DW_AT_priority";
13154 case DW_AT_segment:
13155 return "DW_AT_segment";
13156 case DW_AT_specification:
13157 return "DW_AT_specification";
13158 case DW_AT_static_link:
13159 return "DW_AT_static_link";
13160 case DW_AT_type:
13161 return "DW_AT_type";
13162 case DW_AT_use_location:
13163 return "DW_AT_use_location";
13164 case DW_AT_variable_parameter:
13165 return "DW_AT_variable_parameter";
13166 case DW_AT_virtuality:
13167 return "DW_AT_virtuality";
13168 case DW_AT_vtable_elem_location:
13169 return "DW_AT_vtable_elem_location";
13170 /* DWARF 3 values. */
13171 case DW_AT_allocated:
13172 return "DW_AT_allocated";
13173 case DW_AT_associated:
13174 return "DW_AT_associated";
13175 case DW_AT_data_location:
13176 return "DW_AT_data_location";
13177 case DW_AT_byte_stride:
13178 return "DW_AT_byte_stride";
13179 case DW_AT_entry_pc:
13180 return "DW_AT_entry_pc";
13181 case DW_AT_use_UTF8:
13182 return "DW_AT_use_UTF8";
13183 case DW_AT_extension:
13184 return "DW_AT_extension";
13185 case DW_AT_ranges:
13186 return "DW_AT_ranges";
13187 case DW_AT_trampoline:
13188 return "DW_AT_trampoline";
13189 case DW_AT_call_column:
13190 return "DW_AT_call_column";
13191 case DW_AT_call_file:
13192 return "DW_AT_call_file";
13193 case DW_AT_call_line:
13194 return "DW_AT_call_line";
13195 case DW_AT_description:
13196 return "DW_AT_description";
13197 case DW_AT_binary_scale:
13198 return "DW_AT_binary_scale";
13199 case DW_AT_decimal_scale:
13200 return "DW_AT_decimal_scale";
13201 case DW_AT_small:
13202 return "DW_AT_small";
13203 case DW_AT_decimal_sign:
13204 return "DW_AT_decimal_sign";
13205 case DW_AT_digit_count:
13206 return "DW_AT_digit_count";
13207 case DW_AT_picture_string:
13208 return "DW_AT_picture_string";
13209 case DW_AT_mutable:
13210 return "DW_AT_mutable";
13211 case DW_AT_threads_scaled:
13212 return "DW_AT_threads_scaled";
13213 case DW_AT_explicit:
13214 return "DW_AT_explicit";
13215 case DW_AT_object_pointer:
13216 return "DW_AT_object_pointer";
13217 case DW_AT_endianity:
13218 return "DW_AT_endianity";
13219 case DW_AT_elemental:
13220 return "DW_AT_elemental";
13221 case DW_AT_pure:
13222 return "DW_AT_pure";
13223 case DW_AT_recursive:
13224 return "DW_AT_recursive";
13225 /* DWARF 4 values. */
13226 case DW_AT_signature:
13227 return "DW_AT_signature";
13228 case DW_AT_linkage_name:
13229 return "DW_AT_linkage_name";
13230 /* SGI/MIPS extensions. */
13231 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13232 case DW_AT_MIPS_fde:
13233 return "DW_AT_MIPS_fde";
13234 #endif
13235 case DW_AT_MIPS_loop_begin:
13236 return "DW_AT_MIPS_loop_begin";
13237 case DW_AT_MIPS_tail_loop_begin:
13238 return "DW_AT_MIPS_tail_loop_begin";
13239 case DW_AT_MIPS_epilog_begin:
13240 return "DW_AT_MIPS_epilog_begin";
13241 case DW_AT_MIPS_loop_unroll_factor:
13242 return "DW_AT_MIPS_loop_unroll_factor";
13243 case DW_AT_MIPS_software_pipeline_depth:
13244 return "DW_AT_MIPS_software_pipeline_depth";
13245 case DW_AT_MIPS_linkage_name:
13246 return "DW_AT_MIPS_linkage_name";
13247 case DW_AT_MIPS_stride:
13248 return "DW_AT_MIPS_stride";
13249 case DW_AT_MIPS_abstract_name:
13250 return "DW_AT_MIPS_abstract_name";
13251 case DW_AT_MIPS_clone_origin:
13252 return "DW_AT_MIPS_clone_origin";
13253 case DW_AT_MIPS_has_inlines:
13254 return "DW_AT_MIPS_has_inlines";
13255 /* HP extensions. */
13256 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13257 case DW_AT_HP_block_index:
13258 return "DW_AT_HP_block_index";
13259 #endif
13260 case DW_AT_HP_unmodifiable:
13261 return "DW_AT_HP_unmodifiable";
13262 case DW_AT_HP_actuals_stmt_list:
13263 return "DW_AT_HP_actuals_stmt_list";
13264 case DW_AT_HP_proc_per_section:
13265 return "DW_AT_HP_proc_per_section";
13266 case DW_AT_HP_raw_data_ptr:
13267 return "DW_AT_HP_raw_data_ptr";
13268 case DW_AT_HP_pass_by_reference:
13269 return "DW_AT_HP_pass_by_reference";
13270 case DW_AT_HP_opt_level:
13271 return "DW_AT_HP_opt_level";
13272 case DW_AT_HP_prof_version_id:
13273 return "DW_AT_HP_prof_version_id";
13274 case DW_AT_HP_opt_flags:
13275 return "DW_AT_HP_opt_flags";
13276 case DW_AT_HP_cold_region_low_pc:
13277 return "DW_AT_HP_cold_region_low_pc";
13278 case DW_AT_HP_cold_region_high_pc:
13279 return "DW_AT_HP_cold_region_high_pc";
13280 case DW_AT_HP_all_variables_modifiable:
13281 return "DW_AT_HP_all_variables_modifiable";
13282 case DW_AT_HP_linkage_name:
13283 return "DW_AT_HP_linkage_name";
13284 case DW_AT_HP_prof_flags:
13285 return "DW_AT_HP_prof_flags";
13286 /* GNU extensions. */
13287 case DW_AT_sf_names:
13288 return "DW_AT_sf_names";
13289 case DW_AT_src_info:
13290 return "DW_AT_src_info";
13291 case DW_AT_mac_info:
13292 return "DW_AT_mac_info";
13293 case DW_AT_src_coords:
13294 return "DW_AT_src_coords";
13295 case DW_AT_body_begin:
13296 return "DW_AT_body_begin";
13297 case DW_AT_body_end:
13298 return "DW_AT_body_end";
13299 case DW_AT_GNU_vector:
13300 return "DW_AT_GNU_vector";
13301 case DW_AT_GNU_odr_signature:
13302 return "DW_AT_GNU_odr_signature";
13303 /* VMS extensions. */
13304 case DW_AT_VMS_rtnbeg_pd_address:
13305 return "DW_AT_VMS_rtnbeg_pd_address";
13306 /* UPC extension. */
13307 case DW_AT_upc_threads_scaled:
13308 return "DW_AT_upc_threads_scaled";
13309 /* PGI (STMicroelectronics) extensions. */
13310 case DW_AT_PGI_lbase:
13311 return "DW_AT_PGI_lbase";
13312 case DW_AT_PGI_soffset:
13313 return "DW_AT_PGI_soffset";
13314 case DW_AT_PGI_lstride:
13315 return "DW_AT_PGI_lstride";
13316 default:
13317 return "DW_AT_<unknown>";
13318 }
13319 }
13320
13321 /* Convert a DWARF value form code into its string name. */
13322
13323 static char *
13324 dwarf_form_name (unsigned form)
13325 {
13326 switch (form)
13327 {
13328 case DW_FORM_addr:
13329 return "DW_FORM_addr";
13330 case DW_FORM_block2:
13331 return "DW_FORM_block2";
13332 case DW_FORM_block4:
13333 return "DW_FORM_block4";
13334 case DW_FORM_data2:
13335 return "DW_FORM_data2";
13336 case DW_FORM_data4:
13337 return "DW_FORM_data4";
13338 case DW_FORM_data8:
13339 return "DW_FORM_data8";
13340 case DW_FORM_string:
13341 return "DW_FORM_string";
13342 case DW_FORM_block:
13343 return "DW_FORM_block";
13344 case DW_FORM_block1:
13345 return "DW_FORM_block1";
13346 case DW_FORM_data1:
13347 return "DW_FORM_data1";
13348 case DW_FORM_flag:
13349 return "DW_FORM_flag";
13350 case DW_FORM_sdata:
13351 return "DW_FORM_sdata";
13352 case DW_FORM_strp:
13353 return "DW_FORM_strp";
13354 case DW_FORM_udata:
13355 return "DW_FORM_udata";
13356 case DW_FORM_ref_addr:
13357 return "DW_FORM_ref_addr";
13358 case DW_FORM_ref1:
13359 return "DW_FORM_ref1";
13360 case DW_FORM_ref2:
13361 return "DW_FORM_ref2";
13362 case DW_FORM_ref4:
13363 return "DW_FORM_ref4";
13364 case DW_FORM_ref8:
13365 return "DW_FORM_ref8";
13366 case DW_FORM_ref_udata:
13367 return "DW_FORM_ref_udata";
13368 case DW_FORM_indirect:
13369 return "DW_FORM_indirect";
13370 case DW_FORM_sec_offset:
13371 return "DW_FORM_sec_offset";
13372 case DW_FORM_exprloc:
13373 return "DW_FORM_exprloc";
13374 case DW_FORM_flag_present:
13375 return "DW_FORM_flag_present";
13376 case DW_FORM_ref_sig8:
13377 return "DW_FORM_ref_sig8";
13378 default:
13379 return "DW_FORM_<unknown>";
13380 }
13381 }
13382
13383 /* Convert a DWARF stack opcode into its string name. */
13384
13385 const char *
13386 dwarf_stack_op_name (unsigned op)
13387 {
13388 switch (op)
13389 {
13390 case DW_OP_addr:
13391 return "DW_OP_addr";
13392 case DW_OP_deref:
13393 return "DW_OP_deref";
13394 case DW_OP_const1u:
13395 return "DW_OP_const1u";
13396 case DW_OP_const1s:
13397 return "DW_OP_const1s";
13398 case DW_OP_const2u:
13399 return "DW_OP_const2u";
13400 case DW_OP_const2s:
13401 return "DW_OP_const2s";
13402 case DW_OP_const4u:
13403 return "DW_OP_const4u";
13404 case DW_OP_const4s:
13405 return "DW_OP_const4s";
13406 case DW_OP_const8u:
13407 return "DW_OP_const8u";
13408 case DW_OP_const8s:
13409 return "DW_OP_const8s";
13410 case DW_OP_constu:
13411 return "DW_OP_constu";
13412 case DW_OP_consts:
13413 return "DW_OP_consts";
13414 case DW_OP_dup:
13415 return "DW_OP_dup";
13416 case DW_OP_drop:
13417 return "DW_OP_drop";
13418 case DW_OP_over:
13419 return "DW_OP_over";
13420 case DW_OP_pick:
13421 return "DW_OP_pick";
13422 case DW_OP_swap:
13423 return "DW_OP_swap";
13424 case DW_OP_rot:
13425 return "DW_OP_rot";
13426 case DW_OP_xderef:
13427 return "DW_OP_xderef";
13428 case DW_OP_abs:
13429 return "DW_OP_abs";
13430 case DW_OP_and:
13431 return "DW_OP_and";
13432 case DW_OP_div:
13433 return "DW_OP_div";
13434 case DW_OP_minus:
13435 return "DW_OP_minus";
13436 case DW_OP_mod:
13437 return "DW_OP_mod";
13438 case DW_OP_mul:
13439 return "DW_OP_mul";
13440 case DW_OP_neg:
13441 return "DW_OP_neg";
13442 case DW_OP_not:
13443 return "DW_OP_not";
13444 case DW_OP_or:
13445 return "DW_OP_or";
13446 case DW_OP_plus:
13447 return "DW_OP_plus";
13448 case DW_OP_plus_uconst:
13449 return "DW_OP_plus_uconst";
13450 case DW_OP_shl:
13451 return "DW_OP_shl";
13452 case DW_OP_shr:
13453 return "DW_OP_shr";
13454 case DW_OP_shra:
13455 return "DW_OP_shra";
13456 case DW_OP_xor:
13457 return "DW_OP_xor";
13458 case DW_OP_bra:
13459 return "DW_OP_bra";
13460 case DW_OP_eq:
13461 return "DW_OP_eq";
13462 case DW_OP_ge:
13463 return "DW_OP_ge";
13464 case DW_OP_gt:
13465 return "DW_OP_gt";
13466 case DW_OP_le:
13467 return "DW_OP_le";
13468 case DW_OP_lt:
13469 return "DW_OP_lt";
13470 case DW_OP_ne:
13471 return "DW_OP_ne";
13472 case DW_OP_skip:
13473 return "DW_OP_skip";
13474 case DW_OP_lit0:
13475 return "DW_OP_lit0";
13476 case DW_OP_lit1:
13477 return "DW_OP_lit1";
13478 case DW_OP_lit2:
13479 return "DW_OP_lit2";
13480 case DW_OP_lit3:
13481 return "DW_OP_lit3";
13482 case DW_OP_lit4:
13483 return "DW_OP_lit4";
13484 case DW_OP_lit5:
13485 return "DW_OP_lit5";
13486 case DW_OP_lit6:
13487 return "DW_OP_lit6";
13488 case DW_OP_lit7:
13489 return "DW_OP_lit7";
13490 case DW_OP_lit8:
13491 return "DW_OP_lit8";
13492 case DW_OP_lit9:
13493 return "DW_OP_lit9";
13494 case DW_OP_lit10:
13495 return "DW_OP_lit10";
13496 case DW_OP_lit11:
13497 return "DW_OP_lit11";
13498 case DW_OP_lit12:
13499 return "DW_OP_lit12";
13500 case DW_OP_lit13:
13501 return "DW_OP_lit13";
13502 case DW_OP_lit14:
13503 return "DW_OP_lit14";
13504 case DW_OP_lit15:
13505 return "DW_OP_lit15";
13506 case DW_OP_lit16:
13507 return "DW_OP_lit16";
13508 case DW_OP_lit17:
13509 return "DW_OP_lit17";
13510 case DW_OP_lit18:
13511 return "DW_OP_lit18";
13512 case DW_OP_lit19:
13513 return "DW_OP_lit19";
13514 case DW_OP_lit20:
13515 return "DW_OP_lit20";
13516 case DW_OP_lit21:
13517 return "DW_OP_lit21";
13518 case DW_OP_lit22:
13519 return "DW_OP_lit22";
13520 case DW_OP_lit23:
13521 return "DW_OP_lit23";
13522 case DW_OP_lit24:
13523 return "DW_OP_lit24";
13524 case DW_OP_lit25:
13525 return "DW_OP_lit25";
13526 case DW_OP_lit26:
13527 return "DW_OP_lit26";
13528 case DW_OP_lit27:
13529 return "DW_OP_lit27";
13530 case DW_OP_lit28:
13531 return "DW_OP_lit28";
13532 case DW_OP_lit29:
13533 return "DW_OP_lit29";
13534 case DW_OP_lit30:
13535 return "DW_OP_lit30";
13536 case DW_OP_lit31:
13537 return "DW_OP_lit31";
13538 case DW_OP_reg0:
13539 return "DW_OP_reg0";
13540 case DW_OP_reg1:
13541 return "DW_OP_reg1";
13542 case DW_OP_reg2:
13543 return "DW_OP_reg2";
13544 case DW_OP_reg3:
13545 return "DW_OP_reg3";
13546 case DW_OP_reg4:
13547 return "DW_OP_reg4";
13548 case DW_OP_reg5:
13549 return "DW_OP_reg5";
13550 case DW_OP_reg6:
13551 return "DW_OP_reg6";
13552 case DW_OP_reg7:
13553 return "DW_OP_reg7";
13554 case DW_OP_reg8:
13555 return "DW_OP_reg8";
13556 case DW_OP_reg9:
13557 return "DW_OP_reg9";
13558 case DW_OP_reg10:
13559 return "DW_OP_reg10";
13560 case DW_OP_reg11:
13561 return "DW_OP_reg11";
13562 case DW_OP_reg12:
13563 return "DW_OP_reg12";
13564 case DW_OP_reg13:
13565 return "DW_OP_reg13";
13566 case DW_OP_reg14:
13567 return "DW_OP_reg14";
13568 case DW_OP_reg15:
13569 return "DW_OP_reg15";
13570 case DW_OP_reg16:
13571 return "DW_OP_reg16";
13572 case DW_OP_reg17:
13573 return "DW_OP_reg17";
13574 case DW_OP_reg18:
13575 return "DW_OP_reg18";
13576 case DW_OP_reg19:
13577 return "DW_OP_reg19";
13578 case DW_OP_reg20:
13579 return "DW_OP_reg20";
13580 case DW_OP_reg21:
13581 return "DW_OP_reg21";
13582 case DW_OP_reg22:
13583 return "DW_OP_reg22";
13584 case DW_OP_reg23:
13585 return "DW_OP_reg23";
13586 case DW_OP_reg24:
13587 return "DW_OP_reg24";
13588 case DW_OP_reg25:
13589 return "DW_OP_reg25";
13590 case DW_OP_reg26:
13591 return "DW_OP_reg26";
13592 case DW_OP_reg27:
13593 return "DW_OP_reg27";
13594 case DW_OP_reg28:
13595 return "DW_OP_reg28";
13596 case DW_OP_reg29:
13597 return "DW_OP_reg29";
13598 case DW_OP_reg30:
13599 return "DW_OP_reg30";
13600 case DW_OP_reg31:
13601 return "DW_OP_reg31";
13602 case DW_OP_breg0:
13603 return "DW_OP_breg0";
13604 case DW_OP_breg1:
13605 return "DW_OP_breg1";
13606 case DW_OP_breg2:
13607 return "DW_OP_breg2";
13608 case DW_OP_breg3:
13609 return "DW_OP_breg3";
13610 case DW_OP_breg4:
13611 return "DW_OP_breg4";
13612 case DW_OP_breg5:
13613 return "DW_OP_breg5";
13614 case DW_OP_breg6:
13615 return "DW_OP_breg6";
13616 case DW_OP_breg7:
13617 return "DW_OP_breg7";
13618 case DW_OP_breg8:
13619 return "DW_OP_breg8";
13620 case DW_OP_breg9:
13621 return "DW_OP_breg9";
13622 case DW_OP_breg10:
13623 return "DW_OP_breg10";
13624 case DW_OP_breg11:
13625 return "DW_OP_breg11";
13626 case DW_OP_breg12:
13627 return "DW_OP_breg12";
13628 case DW_OP_breg13:
13629 return "DW_OP_breg13";
13630 case DW_OP_breg14:
13631 return "DW_OP_breg14";
13632 case DW_OP_breg15:
13633 return "DW_OP_breg15";
13634 case DW_OP_breg16:
13635 return "DW_OP_breg16";
13636 case DW_OP_breg17:
13637 return "DW_OP_breg17";
13638 case DW_OP_breg18:
13639 return "DW_OP_breg18";
13640 case DW_OP_breg19:
13641 return "DW_OP_breg19";
13642 case DW_OP_breg20:
13643 return "DW_OP_breg20";
13644 case DW_OP_breg21:
13645 return "DW_OP_breg21";
13646 case DW_OP_breg22:
13647 return "DW_OP_breg22";
13648 case DW_OP_breg23:
13649 return "DW_OP_breg23";
13650 case DW_OP_breg24:
13651 return "DW_OP_breg24";
13652 case DW_OP_breg25:
13653 return "DW_OP_breg25";
13654 case DW_OP_breg26:
13655 return "DW_OP_breg26";
13656 case DW_OP_breg27:
13657 return "DW_OP_breg27";
13658 case DW_OP_breg28:
13659 return "DW_OP_breg28";
13660 case DW_OP_breg29:
13661 return "DW_OP_breg29";
13662 case DW_OP_breg30:
13663 return "DW_OP_breg30";
13664 case DW_OP_breg31:
13665 return "DW_OP_breg31";
13666 case DW_OP_regx:
13667 return "DW_OP_regx";
13668 case DW_OP_fbreg:
13669 return "DW_OP_fbreg";
13670 case DW_OP_bregx:
13671 return "DW_OP_bregx";
13672 case DW_OP_piece:
13673 return "DW_OP_piece";
13674 case DW_OP_deref_size:
13675 return "DW_OP_deref_size";
13676 case DW_OP_xderef_size:
13677 return "DW_OP_xderef_size";
13678 case DW_OP_nop:
13679 return "DW_OP_nop";
13680 /* DWARF 3 extensions. */
13681 case DW_OP_push_object_address:
13682 return "DW_OP_push_object_address";
13683 case DW_OP_call2:
13684 return "DW_OP_call2";
13685 case DW_OP_call4:
13686 return "DW_OP_call4";
13687 case DW_OP_call_ref:
13688 return "DW_OP_call_ref";
13689 case DW_OP_form_tls_address:
13690 return "DW_OP_form_tls_address";
13691 case DW_OP_call_frame_cfa:
13692 return "DW_OP_call_frame_cfa";
13693 case DW_OP_bit_piece:
13694 return "DW_OP_bit_piece";
13695 /* DWARF 4 extensions. */
13696 case DW_OP_implicit_value:
13697 return "DW_OP_implicit_value";
13698 case DW_OP_stack_value:
13699 return "DW_OP_stack_value";
13700 /* GNU extensions. */
13701 case DW_OP_GNU_push_tls_address:
13702 return "DW_OP_GNU_push_tls_address";
13703 case DW_OP_GNU_uninit:
13704 return "DW_OP_GNU_uninit";
13705 case DW_OP_GNU_implicit_pointer:
13706 return "DW_OP_GNU_implicit_pointer";
13707 case DW_OP_GNU_entry_value:
13708 return "DW_OP_GNU_entry_value";
13709 case DW_OP_GNU_const_type:
13710 return "DW_OP_GNU_const_type";
13711 case DW_OP_GNU_regval_type:
13712 return "DW_OP_GNU_regval_type";
13713 case DW_OP_GNU_deref_type:
13714 return "DW_OP_GNU_deref_type";
13715 case DW_OP_GNU_convert:
13716 return "DW_OP_GNU_convert";
13717 case DW_OP_GNU_reinterpret:
13718 return "DW_OP_GNU_reinterpret";
13719 default:
13720 return NULL;
13721 }
13722 }
13723
13724 static char *
13725 dwarf_bool_name (unsigned mybool)
13726 {
13727 if (mybool)
13728 return "TRUE";
13729 else
13730 return "FALSE";
13731 }
13732
13733 /* Convert a DWARF type code into its string name. */
13734
13735 static char *
13736 dwarf_type_encoding_name (unsigned enc)
13737 {
13738 switch (enc)
13739 {
13740 case DW_ATE_void:
13741 return "DW_ATE_void";
13742 case DW_ATE_address:
13743 return "DW_ATE_address";
13744 case DW_ATE_boolean:
13745 return "DW_ATE_boolean";
13746 case DW_ATE_complex_float:
13747 return "DW_ATE_complex_float";
13748 case DW_ATE_float:
13749 return "DW_ATE_float";
13750 case DW_ATE_signed:
13751 return "DW_ATE_signed";
13752 case DW_ATE_signed_char:
13753 return "DW_ATE_signed_char";
13754 case DW_ATE_unsigned:
13755 return "DW_ATE_unsigned";
13756 case DW_ATE_unsigned_char:
13757 return "DW_ATE_unsigned_char";
13758 /* DWARF 3. */
13759 case DW_ATE_imaginary_float:
13760 return "DW_ATE_imaginary_float";
13761 case DW_ATE_packed_decimal:
13762 return "DW_ATE_packed_decimal";
13763 case DW_ATE_numeric_string:
13764 return "DW_ATE_numeric_string";
13765 case DW_ATE_edited:
13766 return "DW_ATE_edited";
13767 case DW_ATE_signed_fixed:
13768 return "DW_ATE_signed_fixed";
13769 case DW_ATE_unsigned_fixed:
13770 return "DW_ATE_unsigned_fixed";
13771 case DW_ATE_decimal_float:
13772 return "DW_ATE_decimal_float";
13773 /* DWARF 4. */
13774 case DW_ATE_UTF:
13775 return "DW_ATE_UTF";
13776 /* HP extensions. */
13777 case DW_ATE_HP_float80:
13778 return "DW_ATE_HP_float80";
13779 case DW_ATE_HP_complex_float80:
13780 return "DW_ATE_HP_complex_float80";
13781 case DW_ATE_HP_float128:
13782 return "DW_ATE_HP_float128";
13783 case DW_ATE_HP_complex_float128:
13784 return "DW_ATE_HP_complex_float128";
13785 case DW_ATE_HP_floathpintel:
13786 return "DW_ATE_HP_floathpintel";
13787 case DW_ATE_HP_imaginary_float80:
13788 return "DW_ATE_HP_imaginary_float80";
13789 case DW_ATE_HP_imaginary_float128:
13790 return "DW_ATE_HP_imaginary_float128";
13791 default:
13792 return "DW_ATE_<unknown>";
13793 }
13794 }
13795
13796 /* Convert a DWARF call frame info operation to its string name. */
13797
13798 #if 0
13799 static char *
13800 dwarf_cfi_name (unsigned cfi_opc)
13801 {
13802 switch (cfi_opc)
13803 {
13804 case DW_CFA_advance_loc:
13805 return "DW_CFA_advance_loc";
13806 case DW_CFA_offset:
13807 return "DW_CFA_offset";
13808 case DW_CFA_restore:
13809 return "DW_CFA_restore";
13810 case DW_CFA_nop:
13811 return "DW_CFA_nop";
13812 case DW_CFA_set_loc:
13813 return "DW_CFA_set_loc";
13814 case DW_CFA_advance_loc1:
13815 return "DW_CFA_advance_loc1";
13816 case DW_CFA_advance_loc2:
13817 return "DW_CFA_advance_loc2";
13818 case DW_CFA_advance_loc4:
13819 return "DW_CFA_advance_loc4";
13820 case DW_CFA_offset_extended:
13821 return "DW_CFA_offset_extended";
13822 case DW_CFA_restore_extended:
13823 return "DW_CFA_restore_extended";
13824 case DW_CFA_undefined:
13825 return "DW_CFA_undefined";
13826 case DW_CFA_same_value:
13827 return "DW_CFA_same_value";
13828 case DW_CFA_register:
13829 return "DW_CFA_register";
13830 case DW_CFA_remember_state:
13831 return "DW_CFA_remember_state";
13832 case DW_CFA_restore_state:
13833 return "DW_CFA_restore_state";
13834 case DW_CFA_def_cfa:
13835 return "DW_CFA_def_cfa";
13836 case DW_CFA_def_cfa_register:
13837 return "DW_CFA_def_cfa_register";
13838 case DW_CFA_def_cfa_offset:
13839 return "DW_CFA_def_cfa_offset";
13840 /* DWARF 3. */
13841 case DW_CFA_def_cfa_expression:
13842 return "DW_CFA_def_cfa_expression";
13843 case DW_CFA_expression:
13844 return "DW_CFA_expression";
13845 case DW_CFA_offset_extended_sf:
13846 return "DW_CFA_offset_extended_sf";
13847 case DW_CFA_def_cfa_sf:
13848 return "DW_CFA_def_cfa_sf";
13849 case DW_CFA_def_cfa_offset_sf:
13850 return "DW_CFA_def_cfa_offset_sf";
13851 case DW_CFA_val_offset:
13852 return "DW_CFA_val_offset";
13853 case DW_CFA_val_offset_sf:
13854 return "DW_CFA_val_offset_sf";
13855 case DW_CFA_val_expression:
13856 return "DW_CFA_val_expression";
13857 /* SGI/MIPS specific. */
13858 case DW_CFA_MIPS_advance_loc8:
13859 return "DW_CFA_MIPS_advance_loc8";
13860 /* GNU extensions. */
13861 case DW_CFA_GNU_window_save:
13862 return "DW_CFA_GNU_window_save";
13863 case DW_CFA_GNU_args_size:
13864 return "DW_CFA_GNU_args_size";
13865 case DW_CFA_GNU_negative_offset_extended:
13866 return "DW_CFA_GNU_negative_offset_extended";
13867 default:
13868 return "DW_CFA_<unknown>";
13869 }
13870 }
13871 #endif
13872
13873 static void
13874 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13875 {
13876 unsigned int i;
13877
13878 print_spaces (indent, f);
13879 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13880 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13881
13882 if (die->parent != NULL)
13883 {
13884 print_spaces (indent, f);
13885 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13886 die->parent->offset);
13887 }
13888
13889 print_spaces (indent, f);
13890 fprintf_unfiltered (f, " has children: %s\n",
13891 dwarf_bool_name (die->child != NULL));
13892
13893 print_spaces (indent, f);
13894 fprintf_unfiltered (f, " attributes:\n");
13895
13896 for (i = 0; i < die->num_attrs; ++i)
13897 {
13898 print_spaces (indent, f);
13899 fprintf_unfiltered (f, " %s (%s) ",
13900 dwarf_attr_name (die->attrs[i].name),
13901 dwarf_form_name (die->attrs[i].form));
13902
13903 switch (die->attrs[i].form)
13904 {
13905 case DW_FORM_ref_addr:
13906 case DW_FORM_addr:
13907 fprintf_unfiltered (f, "address: ");
13908 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13909 break;
13910 case DW_FORM_block2:
13911 case DW_FORM_block4:
13912 case DW_FORM_block:
13913 case DW_FORM_block1:
13914 fprintf_unfiltered (f, "block: size %d",
13915 DW_BLOCK (&die->attrs[i])->size);
13916 break;
13917 case DW_FORM_exprloc:
13918 fprintf_unfiltered (f, "expression: size %u",
13919 DW_BLOCK (&die->attrs[i])->size);
13920 break;
13921 case DW_FORM_ref1:
13922 case DW_FORM_ref2:
13923 case DW_FORM_ref4:
13924 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13925 (long) (DW_ADDR (&die->attrs[i])));
13926 break;
13927 case DW_FORM_data1:
13928 case DW_FORM_data2:
13929 case DW_FORM_data4:
13930 case DW_FORM_data8:
13931 case DW_FORM_udata:
13932 case DW_FORM_sdata:
13933 fprintf_unfiltered (f, "constant: %s",
13934 pulongest (DW_UNSND (&die->attrs[i])));
13935 break;
13936 case DW_FORM_sec_offset:
13937 fprintf_unfiltered (f, "section offset: %s",
13938 pulongest (DW_UNSND (&die->attrs[i])));
13939 break;
13940 case DW_FORM_ref_sig8:
13941 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13942 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13943 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13944 else
13945 fprintf_unfiltered (f, "signatured type, offset: unknown");
13946 break;
13947 case DW_FORM_string:
13948 case DW_FORM_strp:
13949 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13950 DW_STRING (&die->attrs[i])
13951 ? DW_STRING (&die->attrs[i]) : "",
13952 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13953 break;
13954 case DW_FORM_flag:
13955 if (DW_UNSND (&die->attrs[i]))
13956 fprintf_unfiltered (f, "flag: TRUE");
13957 else
13958 fprintf_unfiltered (f, "flag: FALSE");
13959 break;
13960 case DW_FORM_flag_present:
13961 fprintf_unfiltered (f, "flag: TRUE");
13962 break;
13963 case DW_FORM_indirect:
13964 /* The reader will have reduced the indirect form to
13965 the "base form" so this form should not occur. */
13966 fprintf_unfiltered (f,
13967 "unexpected attribute form: DW_FORM_indirect");
13968 break;
13969 default:
13970 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13971 die->attrs[i].form);
13972 break;
13973 }
13974 fprintf_unfiltered (f, "\n");
13975 }
13976 }
13977
13978 static void
13979 dump_die_for_error (struct die_info *die)
13980 {
13981 dump_die_shallow (gdb_stderr, 0, die);
13982 }
13983
13984 static void
13985 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13986 {
13987 int indent = level * 4;
13988
13989 gdb_assert (die != NULL);
13990
13991 if (level >= max_level)
13992 return;
13993
13994 dump_die_shallow (f, indent, die);
13995
13996 if (die->child != NULL)
13997 {
13998 print_spaces (indent, f);
13999 fprintf_unfiltered (f, " Children:");
14000 if (level + 1 < max_level)
14001 {
14002 fprintf_unfiltered (f, "\n");
14003 dump_die_1 (f, level + 1, max_level, die->child);
14004 }
14005 else
14006 {
14007 fprintf_unfiltered (f,
14008 " [not printed, max nesting level reached]\n");
14009 }
14010 }
14011
14012 if (die->sibling != NULL && level > 0)
14013 {
14014 dump_die_1 (f, level, max_level, die->sibling);
14015 }
14016 }
14017
14018 /* This is called from the pdie macro in gdbinit.in.
14019 It's not static so gcc will keep a copy callable from gdb. */
14020
14021 void
14022 dump_die (struct die_info *die, int max_level)
14023 {
14024 dump_die_1 (gdb_stdlog, 0, max_level, die);
14025 }
14026
14027 static void
14028 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14029 {
14030 void **slot;
14031
14032 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14033
14034 *slot = die;
14035 }
14036
14037 static int
14038 is_ref_attr (struct attribute *attr)
14039 {
14040 switch (attr->form)
14041 {
14042 case DW_FORM_ref_addr:
14043 case DW_FORM_ref1:
14044 case DW_FORM_ref2:
14045 case DW_FORM_ref4:
14046 case DW_FORM_ref8:
14047 case DW_FORM_ref_udata:
14048 return 1;
14049 default:
14050 return 0;
14051 }
14052 }
14053
14054 static unsigned int
14055 dwarf2_get_ref_die_offset (struct attribute *attr)
14056 {
14057 if (is_ref_attr (attr))
14058 return DW_ADDR (attr);
14059
14060 complaint (&symfile_complaints,
14061 _("unsupported die ref attribute form: '%s'"),
14062 dwarf_form_name (attr->form));
14063 return 0;
14064 }
14065
14066 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14067 * the value held by the attribute is not constant. */
14068
14069 static LONGEST
14070 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14071 {
14072 if (attr->form == DW_FORM_sdata)
14073 return DW_SND (attr);
14074 else if (attr->form == DW_FORM_udata
14075 || attr->form == DW_FORM_data1
14076 || attr->form == DW_FORM_data2
14077 || attr->form == DW_FORM_data4
14078 || attr->form == DW_FORM_data8)
14079 return DW_UNSND (attr);
14080 else
14081 {
14082 complaint (&symfile_complaints,
14083 _("Attribute value is not a constant (%s)"),
14084 dwarf_form_name (attr->form));
14085 return default_value;
14086 }
14087 }
14088
14089 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14090 unit and add it to our queue.
14091 The result is non-zero if PER_CU was queued, otherwise the result is zero
14092 meaning either PER_CU is already queued or it is already loaded. */
14093
14094 static int
14095 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14096 struct dwarf2_per_cu_data *per_cu)
14097 {
14098 /* We may arrive here during partial symbol reading, if we need full
14099 DIEs to process an unusual case (e.g. template arguments). Do
14100 not queue PER_CU, just tell our caller to load its DIEs. */
14101 if (dwarf2_per_objfile->reading_partial_symbols)
14102 {
14103 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14104 return 1;
14105 return 0;
14106 }
14107
14108 /* Mark the dependence relation so that we don't flush PER_CU
14109 too early. */
14110 dwarf2_add_dependence (this_cu, per_cu);
14111
14112 /* If it's already on the queue, we have nothing to do. */
14113 if (per_cu->queued)
14114 return 0;
14115
14116 /* If the compilation unit is already loaded, just mark it as
14117 used. */
14118 if (per_cu->cu != NULL)
14119 {
14120 per_cu->cu->last_used = 0;
14121 return 0;
14122 }
14123
14124 /* Add it to the queue. */
14125 queue_comp_unit (per_cu, this_cu->objfile);
14126
14127 return 1;
14128 }
14129
14130 /* Follow reference or signature attribute ATTR of SRC_DIE.
14131 On entry *REF_CU is the CU of SRC_DIE.
14132 On exit *REF_CU is the CU of the result. */
14133
14134 static struct die_info *
14135 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14136 struct dwarf2_cu **ref_cu)
14137 {
14138 struct die_info *die;
14139
14140 if (is_ref_attr (attr))
14141 die = follow_die_ref (src_die, attr, ref_cu);
14142 else if (attr->form == DW_FORM_ref_sig8)
14143 die = follow_die_sig (src_die, attr, ref_cu);
14144 else
14145 {
14146 dump_die_for_error (src_die);
14147 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14148 (*ref_cu)->objfile->name);
14149 }
14150
14151 return die;
14152 }
14153
14154 /* Follow reference OFFSET.
14155 On entry *REF_CU is the CU of the source die referencing OFFSET.
14156 On exit *REF_CU is the CU of the result.
14157 Returns NULL if OFFSET is invalid. */
14158
14159 static struct die_info *
14160 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14161 {
14162 struct die_info temp_die;
14163 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14164
14165 gdb_assert (cu->per_cu != NULL);
14166
14167 target_cu = cu;
14168
14169 if (cu->per_cu->debug_types_section)
14170 {
14171 /* .debug_types CUs cannot reference anything outside their CU.
14172 If they need to, they have to reference a signatured type via
14173 DW_FORM_ref_sig8. */
14174 if (! offset_in_cu_p (&cu->header, offset))
14175 return NULL;
14176 }
14177 else if (! offset_in_cu_p (&cu->header, offset))
14178 {
14179 struct dwarf2_per_cu_data *per_cu;
14180
14181 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14182
14183 /* If necessary, add it to the queue and load its DIEs. */
14184 if (maybe_queue_comp_unit (cu, per_cu))
14185 load_full_comp_unit (per_cu, cu->objfile);
14186
14187 target_cu = per_cu->cu;
14188 }
14189 else if (cu->dies == NULL)
14190 {
14191 /* We're loading full DIEs during partial symbol reading. */
14192 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14193 load_full_comp_unit (cu->per_cu, cu->objfile);
14194 }
14195
14196 *ref_cu = target_cu;
14197 temp_die.offset = offset;
14198 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14199 }
14200
14201 /* Follow reference attribute ATTR of SRC_DIE.
14202 On entry *REF_CU is the CU of SRC_DIE.
14203 On exit *REF_CU is the CU of the result. */
14204
14205 static struct die_info *
14206 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14207 struct dwarf2_cu **ref_cu)
14208 {
14209 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14210 struct dwarf2_cu *cu = *ref_cu;
14211 struct die_info *die;
14212
14213 die = follow_die_offset (offset, ref_cu);
14214 if (!die)
14215 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14216 "at 0x%x [in module %s]"),
14217 offset, src_die->offset, cu->objfile->name);
14218
14219 return die;
14220 }
14221
14222 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14223 Returned value is intended for DW_OP_call*. Returned
14224 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14225
14226 struct dwarf2_locexpr_baton
14227 dwarf2_fetch_die_location_block (unsigned int offset,
14228 struct dwarf2_per_cu_data *per_cu,
14229 CORE_ADDR (*get_frame_pc) (void *baton),
14230 void *baton)
14231 {
14232 struct dwarf2_cu *cu;
14233 struct die_info *die;
14234 struct attribute *attr;
14235 struct dwarf2_locexpr_baton retval;
14236
14237 dw2_setup (per_cu->objfile);
14238
14239 if (per_cu->cu == NULL)
14240 load_cu (per_cu);
14241 cu = per_cu->cu;
14242
14243 die = follow_die_offset (offset, &cu);
14244 if (!die)
14245 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14246 offset, per_cu->cu->objfile->name);
14247
14248 attr = dwarf2_attr (die, DW_AT_location, cu);
14249 if (!attr)
14250 {
14251 /* DWARF: "If there is no such attribute, then there is no effect.".
14252 DATA is ignored if SIZE is 0. */
14253
14254 retval.data = NULL;
14255 retval.size = 0;
14256 }
14257 else if (attr_form_is_section_offset (attr))
14258 {
14259 struct dwarf2_loclist_baton loclist_baton;
14260 CORE_ADDR pc = (*get_frame_pc) (baton);
14261 size_t size;
14262
14263 fill_in_loclist_baton (cu, &loclist_baton, attr);
14264
14265 retval.data = dwarf2_find_location_expression (&loclist_baton,
14266 &size, pc);
14267 retval.size = size;
14268 }
14269 else
14270 {
14271 if (!attr_form_is_block (attr))
14272 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14273 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14274 offset, per_cu->cu->objfile->name);
14275
14276 retval.data = DW_BLOCK (attr)->data;
14277 retval.size = DW_BLOCK (attr)->size;
14278 }
14279 retval.per_cu = cu->per_cu;
14280
14281 age_cached_comp_units ();
14282
14283 return retval;
14284 }
14285
14286 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14287 PER_CU. */
14288
14289 struct type *
14290 dwarf2_get_die_type (unsigned int die_offset,
14291 struct dwarf2_per_cu_data *per_cu)
14292 {
14293 dw2_setup (per_cu->objfile);
14294 return get_die_type_at_offset (die_offset, per_cu);
14295 }
14296
14297 /* Follow the signature attribute ATTR in SRC_DIE.
14298 On entry *REF_CU is the CU of SRC_DIE.
14299 On exit *REF_CU is the CU of the result. */
14300
14301 static struct die_info *
14302 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14303 struct dwarf2_cu **ref_cu)
14304 {
14305 struct objfile *objfile = (*ref_cu)->objfile;
14306 struct die_info temp_die;
14307 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14308 struct dwarf2_cu *sig_cu;
14309 struct die_info *die;
14310
14311 /* sig_type will be NULL if the signatured type is missing from
14312 the debug info. */
14313 if (sig_type == NULL)
14314 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14315 "at 0x%x [in module %s]"),
14316 src_die->offset, objfile->name);
14317
14318 /* If necessary, add it to the queue and load its DIEs. */
14319
14320 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14321 read_signatured_type (objfile, sig_type);
14322
14323 gdb_assert (sig_type->per_cu.cu != NULL);
14324
14325 sig_cu = sig_type->per_cu.cu;
14326 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14327 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14328 if (die)
14329 {
14330 *ref_cu = sig_cu;
14331 return die;
14332 }
14333
14334 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14335 "from DIE at 0x%x [in module %s]"),
14336 sig_type->type_offset, src_die->offset, objfile->name);
14337 }
14338
14339 /* Given an offset of a signatured type, return its signatured_type. */
14340
14341 static struct signatured_type *
14342 lookup_signatured_type_at_offset (struct objfile *objfile,
14343 struct dwarf2_section_info *section,
14344 unsigned int offset)
14345 {
14346 gdb_byte *info_ptr = section->buffer + offset;
14347 unsigned int length, initial_length_size;
14348 unsigned int sig_offset;
14349 struct signatured_type find_entry, *type_sig;
14350
14351 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14352 sig_offset = (initial_length_size
14353 + 2 /*version*/
14354 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14355 + 1 /*address_size*/);
14356 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14357 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14358
14359 /* This is only used to lookup previously recorded types.
14360 If we didn't find it, it's our bug. */
14361 gdb_assert (type_sig != NULL);
14362 gdb_assert (offset == type_sig->per_cu.offset);
14363
14364 return type_sig;
14365 }
14366
14367 /* Read in signatured type at OFFSET and build its CU and die(s). */
14368
14369 static void
14370 read_signatured_type_at_offset (struct objfile *objfile,
14371 struct dwarf2_section_info *sect,
14372 unsigned int offset)
14373 {
14374 struct signatured_type *type_sig;
14375
14376 dwarf2_read_section (objfile, sect);
14377
14378 /* We have the section offset, but we need the signature to do the
14379 hash table lookup. */
14380 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14381
14382 gdb_assert (type_sig->per_cu.cu == NULL);
14383
14384 read_signatured_type (objfile, type_sig);
14385
14386 gdb_assert (type_sig->per_cu.cu != NULL);
14387 }
14388
14389 /* Read in a signatured type and build its CU and DIEs. */
14390
14391 static void
14392 read_signatured_type (struct objfile *objfile,
14393 struct signatured_type *type_sig)
14394 {
14395 gdb_byte *types_ptr;
14396 struct die_reader_specs reader_specs;
14397 struct dwarf2_cu *cu;
14398 ULONGEST signature;
14399 struct cleanup *back_to, *free_cu_cleanup;
14400 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14401
14402 dwarf2_read_section (objfile, section);
14403 types_ptr = section->buffer + type_sig->per_cu.offset;
14404
14405 gdb_assert (type_sig->per_cu.cu == NULL);
14406
14407 cu = xmalloc (sizeof (*cu));
14408 init_one_comp_unit (cu, objfile);
14409
14410 type_sig->per_cu.cu = cu;
14411 cu->per_cu = &type_sig->per_cu;
14412
14413 /* If an error occurs while loading, release our storage. */
14414 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14415
14416 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14417 types_ptr, objfile->obfd);
14418 gdb_assert (signature == type_sig->signature);
14419
14420 cu->die_hash
14421 = htab_create_alloc_ex (cu->header.length / 12,
14422 die_hash,
14423 die_eq,
14424 NULL,
14425 &cu->comp_unit_obstack,
14426 hashtab_obstack_allocate,
14427 dummy_obstack_deallocate);
14428
14429 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14430 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14431
14432 init_cu_die_reader (&reader_specs, cu);
14433
14434 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14435 NULL /*parent*/);
14436
14437 /* We try not to read any attributes in this function, because not
14438 all objfiles needed for references have been loaded yet, and symbol
14439 table processing isn't initialized. But we have to set the CU language,
14440 or we won't be able to build types correctly. */
14441 prepare_one_comp_unit (cu, cu->dies);
14442
14443 do_cleanups (back_to);
14444
14445 /* We've successfully allocated this compilation unit. Let our caller
14446 clean it up when finished with it. */
14447 discard_cleanups (free_cu_cleanup);
14448
14449 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14450 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14451 }
14452
14453 /* Decode simple location descriptions.
14454 Given a pointer to a dwarf block that defines a location, compute
14455 the location and return the value.
14456
14457 NOTE drow/2003-11-18: This function is called in two situations
14458 now: for the address of static or global variables (partial symbols
14459 only) and for offsets into structures which are expected to be
14460 (more or less) constant. The partial symbol case should go away,
14461 and only the constant case should remain. That will let this
14462 function complain more accurately. A few special modes are allowed
14463 without complaint for global variables (for instance, global
14464 register values and thread-local values).
14465
14466 A location description containing no operations indicates that the
14467 object is optimized out. The return value is 0 for that case.
14468 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14469 callers will only want a very basic result and this can become a
14470 complaint.
14471
14472 Note that stack[0] is unused except as a default error return. */
14473
14474 static CORE_ADDR
14475 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14476 {
14477 struct objfile *objfile = cu->objfile;
14478 int i;
14479 int size = blk->size;
14480 gdb_byte *data = blk->data;
14481 CORE_ADDR stack[64];
14482 int stacki;
14483 unsigned int bytes_read, unsnd;
14484 gdb_byte op;
14485
14486 i = 0;
14487 stacki = 0;
14488 stack[stacki] = 0;
14489 stack[++stacki] = 0;
14490
14491 while (i < size)
14492 {
14493 op = data[i++];
14494 switch (op)
14495 {
14496 case DW_OP_lit0:
14497 case DW_OP_lit1:
14498 case DW_OP_lit2:
14499 case DW_OP_lit3:
14500 case DW_OP_lit4:
14501 case DW_OP_lit5:
14502 case DW_OP_lit6:
14503 case DW_OP_lit7:
14504 case DW_OP_lit8:
14505 case DW_OP_lit9:
14506 case DW_OP_lit10:
14507 case DW_OP_lit11:
14508 case DW_OP_lit12:
14509 case DW_OP_lit13:
14510 case DW_OP_lit14:
14511 case DW_OP_lit15:
14512 case DW_OP_lit16:
14513 case DW_OP_lit17:
14514 case DW_OP_lit18:
14515 case DW_OP_lit19:
14516 case DW_OP_lit20:
14517 case DW_OP_lit21:
14518 case DW_OP_lit22:
14519 case DW_OP_lit23:
14520 case DW_OP_lit24:
14521 case DW_OP_lit25:
14522 case DW_OP_lit26:
14523 case DW_OP_lit27:
14524 case DW_OP_lit28:
14525 case DW_OP_lit29:
14526 case DW_OP_lit30:
14527 case DW_OP_lit31:
14528 stack[++stacki] = op - DW_OP_lit0;
14529 break;
14530
14531 case DW_OP_reg0:
14532 case DW_OP_reg1:
14533 case DW_OP_reg2:
14534 case DW_OP_reg3:
14535 case DW_OP_reg4:
14536 case DW_OP_reg5:
14537 case DW_OP_reg6:
14538 case DW_OP_reg7:
14539 case DW_OP_reg8:
14540 case DW_OP_reg9:
14541 case DW_OP_reg10:
14542 case DW_OP_reg11:
14543 case DW_OP_reg12:
14544 case DW_OP_reg13:
14545 case DW_OP_reg14:
14546 case DW_OP_reg15:
14547 case DW_OP_reg16:
14548 case DW_OP_reg17:
14549 case DW_OP_reg18:
14550 case DW_OP_reg19:
14551 case DW_OP_reg20:
14552 case DW_OP_reg21:
14553 case DW_OP_reg22:
14554 case DW_OP_reg23:
14555 case DW_OP_reg24:
14556 case DW_OP_reg25:
14557 case DW_OP_reg26:
14558 case DW_OP_reg27:
14559 case DW_OP_reg28:
14560 case DW_OP_reg29:
14561 case DW_OP_reg30:
14562 case DW_OP_reg31:
14563 stack[++stacki] = op - DW_OP_reg0;
14564 if (i < size)
14565 dwarf2_complex_location_expr_complaint ();
14566 break;
14567
14568 case DW_OP_regx:
14569 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14570 i += bytes_read;
14571 stack[++stacki] = unsnd;
14572 if (i < size)
14573 dwarf2_complex_location_expr_complaint ();
14574 break;
14575
14576 case DW_OP_addr:
14577 stack[++stacki] = read_address (objfile->obfd, &data[i],
14578 cu, &bytes_read);
14579 i += bytes_read;
14580 break;
14581
14582 case DW_OP_const1u:
14583 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14584 i += 1;
14585 break;
14586
14587 case DW_OP_const1s:
14588 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14589 i += 1;
14590 break;
14591
14592 case DW_OP_const2u:
14593 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14594 i += 2;
14595 break;
14596
14597 case DW_OP_const2s:
14598 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14599 i += 2;
14600 break;
14601
14602 case DW_OP_const4u:
14603 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14604 i += 4;
14605 break;
14606
14607 case DW_OP_const4s:
14608 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14609 i += 4;
14610 break;
14611
14612 case DW_OP_const8u:
14613 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14614 i += 8;
14615 break;
14616
14617 case DW_OP_constu:
14618 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14619 &bytes_read);
14620 i += bytes_read;
14621 break;
14622
14623 case DW_OP_consts:
14624 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14625 i += bytes_read;
14626 break;
14627
14628 case DW_OP_dup:
14629 stack[stacki + 1] = stack[stacki];
14630 stacki++;
14631 break;
14632
14633 case DW_OP_plus:
14634 stack[stacki - 1] += stack[stacki];
14635 stacki--;
14636 break;
14637
14638 case DW_OP_plus_uconst:
14639 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14640 &bytes_read);
14641 i += bytes_read;
14642 break;
14643
14644 case DW_OP_minus:
14645 stack[stacki - 1] -= stack[stacki];
14646 stacki--;
14647 break;
14648
14649 case DW_OP_deref:
14650 /* If we're not the last op, then we definitely can't encode
14651 this using GDB's address_class enum. This is valid for partial
14652 global symbols, although the variable's address will be bogus
14653 in the psymtab. */
14654 if (i < size)
14655 dwarf2_complex_location_expr_complaint ();
14656 break;
14657
14658 case DW_OP_GNU_push_tls_address:
14659 /* The top of the stack has the offset from the beginning
14660 of the thread control block at which the variable is located. */
14661 /* Nothing should follow this operator, so the top of stack would
14662 be returned. */
14663 /* This is valid for partial global symbols, but the variable's
14664 address will be bogus in the psymtab. Make it always at least
14665 non-zero to not look as a variable garbage collected by linker
14666 which have DW_OP_addr 0. */
14667 if (i < size)
14668 dwarf2_complex_location_expr_complaint ();
14669 stack[stacki]++;
14670 break;
14671
14672 case DW_OP_GNU_uninit:
14673 break;
14674
14675 default:
14676 {
14677 const char *name = dwarf_stack_op_name (op);
14678
14679 if (name)
14680 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14681 name);
14682 else
14683 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14684 op);
14685 }
14686
14687 return (stack[stacki]);
14688 }
14689
14690 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14691 outside of the allocated space. Also enforce minimum>0. */
14692 if (stacki >= ARRAY_SIZE (stack) - 1)
14693 {
14694 complaint (&symfile_complaints,
14695 _("location description stack overflow"));
14696 return 0;
14697 }
14698
14699 if (stacki <= 0)
14700 {
14701 complaint (&symfile_complaints,
14702 _("location description stack underflow"));
14703 return 0;
14704 }
14705 }
14706 return (stack[stacki]);
14707 }
14708
14709 /* memory allocation interface */
14710
14711 static struct dwarf_block *
14712 dwarf_alloc_block (struct dwarf2_cu *cu)
14713 {
14714 struct dwarf_block *blk;
14715
14716 blk = (struct dwarf_block *)
14717 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14718 return (blk);
14719 }
14720
14721 static struct abbrev_info *
14722 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14723 {
14724 struct abbrev_info *abbrev;
14725
14726 abbrev = (struct abbrev_info *)
14727 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14728 memset (abbrev, 0, sizeof (struct abbrev_info));
14729 return (abbrev);
14730 }
14731
14732 static struct die_info *
14733 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14734 {
14735 struct die_info *die;
14736 size_t size = sizeof (struct die_info);
14737
14738 if (num_attrs > 1)
14739 size += (num_attrs - 1) * sizeof (struct attribute);
14740
14741 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14742 memset (die, 0, sizeof (struct die_info));
14743 return (die);
14744 }
14745
14746 \f
14747 /* Macro support. */
14748
14749 /* Return the full name of file number I in *LH's file name table.
14750 Use COMP_DIR as the name of the current directory of the
14751 compilation. The result is allocated using xmalloc; the caller is
14752 responsible for freeing it. */
14753 static char *
14754 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14755 {
14756 /* Is the file number a valid index into the line header's file name
14757 table? Remember that file numbers start with one, not zero. */
14758 if (1 <= file && file <= lh->num_file_names)
14759 {
14760 struct file_entry *fe = &lh->file_names[file - 1];
14761
14762 if (IS_ABSOLUTE_PATH (fe->name))
14763 return xstrdup (fe->name);
14764 else
14765 {
14766 const char *dir;
14767 int dir_len;
14768 char *full_name;
14769
14770 if (fe->dir_index)
14771 dir = lh->include_dirs[fe->dir_index - 1];
14772 else
14773 dir = comp_dir;
14774
14775 if (dir)
14776 {
14777 dir_len = strlen (dir);
14778 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14779 strcpy (full_name, dir);
14780 full_name[dir_len] = '/';
14781 strcpy (full_name + dir_len + 1, fe->name);
14782 return full_name;
14783 }
14784 else
14785 return xstrdup (fe->name);
14786 }
14787 }
14788 else
14789 {
14790 /* The compiler produced a bogus file number. We can at least
14791 record the macro definitions made in the file, even if we
14792 won't be able to find the file by name. */
14793 char fake_name[80];
14794
14795 sprintf (fake_name, "<bad macro file number %d>", file);
14796
14797 complaint (&symfile_complaints,
14798 _("bad file number in macro information (%d)"),
14799 file);
14800
14801 return xstrdup (fake_name);
14802 }
14803 }
14804
14805
14806 static struct macro_source_file *
14807 macro_start_file (int file, int line,
14808 struct macro_source_file *current_file,
14809 const char *comp_dir,
14810 struct line_header *lh, struct objfile *objfile)
14811 {
14812 /* The full name of this source file. */
14813 char *full_name = file_full_name (file, lh, comp_dir);
14814
14815 /* We don't create a macro table for this compilation unit
14816 at all until we actually get a filename. */
14817 if (! pending_macros)
14818 pending_macros = new_macro_table (&objfile->objfile_obstack,
14819 objfile->macro_cache);
14820
14821 if (! current_file)
14822 /* If we have no current file, then this must be the start_file
14823 directive for the compilation unit's main source file. */
14824 current_file = macro_set_main (pending_macros, full_name);
14825 else
14826 current_file = macro_include (current_file, line, full_name);
14827
14828 xfree (full_name);
14829
14830 return current_file;
14831 }
14832
14833
14834 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14835 followed by a null byte. */
14836 static char *
14837 copy_string (const char *buf, int len)
14838 {
14839 char *s = xmalloc (len + 1);
14840
14841 memcpy (s, buf, len);
14842 s[len] = '\0';
14843 return s;
14844 }
14845
14846
14847 static const char *
14848 consume_improper_spaces (const char *p, const char *body)
14849 {
14850 if (*p == ' ')
14851 {
14852 complaint (&symfile_complaints,
14853 _("macro definition contains spaces "
14854 "in formal argument list:\n`%s'"),
14855 body);
14856
14857 while (*p == ' ')
14858 p++;
14859 }
14860
14861 return p;
14862 }
14863
14864
14865 static void
14866 parse_macro_definition (struct macro_source_file *file, int line,
14867 const char *body)
14868 {
14869 const char *p;
14870
14871 /* The body string takes one of two forms. For object-like macro
14872 definitions, it should be:
14873
14874 <macro name> " " <definition>
14875
14876 For function-like macro definitions, it should be:
14877
14878 <macro name> "() " <definition>
14879 or
14880 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14881
14882 Spaces may appear only where explicitly indicated, and in the
14883 <definition>.
14884
14885 The Dwarf 2 spec says that an object-like macro's name is always
14886 followed by a space, but versions of GCC around March 2002 omit
14887 the space when the macro's definition is the empty string.
14888
14889 The Dwarf 2 spec says that there should be no spaces between the
14890 formal arguments in a function-like macro's formal argument list,
14891 but versions of GCC around March 2002 include spaces after the
14892 commas. */
14893
14894
14895 /* Find the extent of the macro name. The macro name is terminated
14896 by either a space or null character (for an object-like macro) or
14897 an opening paren (for a function-like macro). */
14898 for (p = body; *p; p++)
14899 if (*p == ' ' || *p == '(')
14900 break;
14901
14902 if (*p == ' ' || *p == '\0')
14903 {
14904 /* It's an object-like macro. */
14905 int name_len = p - body;
14906 char *name = copy_string (body, name_len);
14907 const char *replacement;
14908
14909 if (*p == ' ')
14910 replacement = body + name_len + 1;
14911 else
14912 {
14913 dwarf2_macro_malformed_definition_complaint (body);
14914 replacement = body + name_len;
14915 }
14916
14917 macro_define_object (file, line, name, replacement);
14918
14919 xfree (name);
14920 }
14921 else if (*p == '(')
14922 {
14923 /* It's a function-like macro. */
14924 char *name = copy_string (body, p - body);
14925 int argc = 0;
14926 int argv_size = 1;
14927 char **argv = xmalloc (argv_size * sizeof (*argv));
14928
14929 p++;
14930
14931 p = consume_improper_spaces (p, body);
14932
14933 /* Parse the formal argument list. */
14934 while (*p && *p != ')')
14935 {
14936 /* Find the extent of the current argument name. */
14937 const char *arg_start = p;
14938
14939 while (*p && *p != ',' && *p != ')' && *p != ' ')
14940 p++;
14941
14942 if (! *p || p == arg_start)
14943 dwarf2_macro_malformed_definition_complaint (body);
14944 else
14945 {
14946 /* Make sure argv has room for the new argument. */
14947 if (argc >= argv_size)
14948 {
14949 argv_size *= 2;
14950 argv = xrealloc (argv, argv_size * sizeof (*argv));
14951 }
14952
14953 argv[argc++] = copy_string (arg_start, p - arg_start);
14954 }
14955
14956 p = consume_improper_spaces (p, body);
14957
14958 /* Consume the comma, if present. */
14959 if (*p == ',')
14960 {
14961 p++;
14962
14963 p = consume_improper_spaces (p, body);
14964 }
14965 }
14966
14967 if (*p == ')')
14968 {
14969 p++;
14970
14971 if (*p == ' ')
14972 /* Perfectly formed definition, no complaints. */
14973 macro_define_function (file, line, name,
14974 argc, (const char **) argv,
14975 p + 1);
14976 else if (*p == '\0')
14977 {
14978 /* Complain, but do define it. */
14979 dwarf2_macro_malformed_definition_complaint (body);
14980 macro_define_function (file, line, name,
14981 argc, (const char **) argv,
14982 p);
14983 }
14984 else
14985 /* Just complain. */
14986 dwarf2_macro_malformed_definition_complaint (body);
14987 }
14988 else
14989 /* Just complain. */
14990 dwarf2_macro_malformed_definition_complaint (body);
14991
14992 xfree (name);
14993 {
14994 int i;
14995
14996 for (i = 0; i < argc; i++)
14997 xfree (argv[i]);
14998 }
14999 xfree (argv);
15000 }
15001 else
15002 dwarf2_macro_malformed_definition_complaint (body);
15003 }
15004
15005 /* Skip some bytes from BYTES according to the form given in FORM.
15006 Returns the new pointer. */
15007
15008 static gdb_byte *
15009 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15010 enum dwarf_form form,
15011 unsigned int offset_size,
15012 struct dwarf2_section_info *section)
15013 {
15014 unsigned int bytes_read;
15015
15016 switch (form)
15017 {
15018 case DW_FORM_data1:
15019 case DW_FORM_flag:
15020 ++bytes;
15021 break;
15022
15023 case DW_FORM_data2:
15024 bytes += 2;
15025 break;
15026
15027 case DW_FORM_data4:
15028 bytes += 4;
15029 break;
15030
15031 case DW_FORM_data8:
15032 bytes += 8;
15033 break;
15034
15035 case DW_FORM_string:
15036 read_direct_string (abfd, bytes, &bytes_read);
15037 bytes += bytes_read;
15038 break;
15039
15040 case DW_FORM_sec_offset:
15041 case DW_FORM_strp:
15042 bytes += offset_size;
15043 break;
15044
15045 case DW_FORM_block:
15046 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15047 bytes += bytes_read;
15048 break;
15049
15050 case DW_FORM_block1:
15051 bytes += 1 + read_1_byte (abfd, bytes);
15052 break;
15053 case DW_FORM_block2:
15054 bytes += 2 + read_2_bytes (abfd, bytes);
15055 break;
15056 case DW_FORM_block4:
15057 bytes += 4 + read_4_bytes (abfd, bytes);
15058 break;
15059
15060 case DW_FORM_sdata:
15061 case DW_FORM_udata:
15062 bytes = skip_leb128 (abfd, bytes);
15063 break;
15064
15065 default:
15066 {
15067 complain:
15068 complaint (&symfile_complaints,
15069 _("invalid form 0x%x in `%s'"),
15070 form,
15071 section->asection->name);
15072 return NULL;
15073 }
15074 }
15075
15076 return bytes;
15077 }
15078
15079 /* A helper for dwarf_decode_macros that handles skipping an unknown
15080 opcode. Returns an updated pointer to the macro data buffer; or,
15081 on error, issues a complaint and returns NULL. */
15082
15083 static gdb_byte *
15084 skip_unknown_opcode (unsigned int opcode,
15085 gdb_byte **opcode_definitions,
15086 gdb_byte *mac_ptr,
15087 bfd *abfd,
15088 unsigned int offset_size,
15089 struct dwarf2_section_info *section)
15090 {
15091 unsigned int bytes_read, i;
15092 unsigned long arg;
15093 gdb_byte *defn;
15094
15095 if (opcode_definitions[opcode] == NULL)
15096 {
15097 complaint (&symfile_complaints,
15098 _("unrecognized DW_MACFINO opcode 0x%x"),
15099 opcode);
15100 return NULL;
15101 }
15102
15103 defn = opcode_definitions[opcode];
15104 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15105 defn += bytes_read;
15106
15107 for (i = 0; i < arg; ++i)
15108 {
15109 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15110 if (mac_ptr == NULL)
15111 {
15112 /* skip_form_bytes already issued the complaint. */
15113 return NULL;
15114 }
15115 }
15116
15117 return mac_ptr;
15118 }
15119
15120 /* A helper function which parses the header of a macro section.
15121 If the macro section is the extended (for now called "GNU") type,
15122 then this updates *OFFSET_SIZE. Returns a pointer to just after
15123 the header, or issues a complaint and returns NULL on error. */
15124
15125 static gdb_byte *
15126 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15127 bfd *abfd,
15128 gdb_byte *mac_ptr,
15129 unsigned int *offset_size,
15130 int section_is_gnu)
15131 {
15132 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15133
15134 if (section_is_gnu)
15135 {
15136 unsigned int version, flags;
15137
15138 version = read_2_bytes (abfd, mac_ptr);
15139 if (version != 4)
15140 {
15141 complaint (&symfile_complaints,
15142 _("unrecognized version `%d' in .debug_macro section"),
15143 version);
15144 return NULL;
15145 }
15146 mac_ptr += 2;
15147
15148 flags = read_1_byte (abfd, mac_ptr);
15149 ++mac_ptr;
15150 *offset_size = (flags & 1) ? 8 : 4;
15151
15152 if ((flags & 2) != 0)
15153 /* We don't need the line table offset. */
15154 mac_ptr += *offset_size;
15155
15156 /* Vendor opcode descriptions. */
15157 if ((flags & 4) != 0)
15158 {
15159 unsigned int i, count;
15160
15161 count = read_1_byte (abfd, mac_ptr);
15162 ++mac_ptr;
15163 for (i = 0; i < count; ++i)
15164 {
15165 unsigned int opcode, bytes_read;
15166 unsigned long arg;
15167
15168 opcode = read_1_byte (abfd, mac_ptr);
15169 ++mac_ptr;
15170 opcode_definitions[opcode] = mac_ptr;
15171 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15172 mac_ptr += bytes_read;
15173 mac_ptr += arg;
15174 }
15175 }
15176 }
15177
15178 return mac_ptr;
15179 }
15180
15181 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15182 including DW_GNU_MACINFO_transparent_include. */
15183
15184 static void
15185 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15186 struct macro_source_file *current_file,
15187 struct line_header *lh, char *comp_dir,
15188 struct dwarf2_section_info *section,
15189 int section_is_gnu,
15190 unsigned int offset_size,
15191 struct objfile *objfile)
15192 {
15193 enum dwarf_macro_record_type macinfo_type;
15194 int at_commandline;
15195 gdb_byte *opcode_definitions[256];
15196
15197 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15198 &offset_size, section_is_gnu);
15199 if (mac_ptr == NULL)
15200 {
15201 /* We already issued a complaint. */
15202 return;
15203 }
15204
15205 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15206 GDB is still reading the definitions from command line. First
15207 DW_MACINFO_start_file will need to be ignored as it was already executed
15208 to create CURRENT_FILE for the main source holding also the command line
15209 definitions. On first met DW_MACINFO_start_file this flag is reset to
15210 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15211
15212 at_commandline = 1;
15213
15214 do
15215 {
15216 /* Do we at least have room for a macinfo type byte? */
15217 if (mac_ptr >= mac_end)
15218 {
15219 dwarf2_macros_too_long_complaint (section);
15220 break;
15221 }
15222
15223 macinfo_type = read_1_byte (abfd, mac_ptr);
15224 mac_ptr++;
15225
15226 /* Note that we rely on the fact that the corresponding GNU and
15227 DWARF constants are the same. */
15228 switch (macinfo_type)
15229 {
15230 /* A zero macinfo type indicates the end of the macro
15231 information. */
15232 case 0:
15233 break;
15234
15235 case DW_MACRO_GNU_define:
15236 case DW_MACRO_GNU_undef:
15237 case DW_MACRO_GNU_define_indirect:
15238 case DW_MACRO_GNU_undef_indirect:
15239 {
15240 unsigned int bytes_read;
15241 int line;
15242 char *body;
15243 int is_define;
15244
15245 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15246 mac_ptr += bytes_read;
15247
15248 if (macinfo_type == DW_MACRO_GNU_define
15249 || macinfo_type == DW_MACRO_GNU_undef)
15250 {
15251 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15252 mac_ptr += bytes_read;
15253 }
15254 else
15255 {
15256 LONGEST str_offset;
15257
15258 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15259 mac_ptr += offset_size;
15260
15261 body = read_indirect_string_at_offset (abfd, str_offset);
15262 }
15263
15264 is_define = (macinfo_type == DW_MACRO_GNU_define
15265 || macinfo_type == DW_MACRO_GNU_define_indirect);
15266 if (! current_file)
15267 {
15268 /* DWARF violation as no main source is present. */
15269 complaint (&symfile_complaints,
15270 _("debug info with no main source gives macro %s "
15271 "on line %d: %s"),
15272 is_define ? _("definition") : _("undefinition"),
15273 line, body);
15274 break;
15275 }
15276 if ((line == 0 && !at_commandline)
15277 || (line != 0 && at_commandline))
15278 complaint (&symfile_complaints,
15279 _("debug info gives %s macro %s with %s line %d: %s"),
15280 at_commandline ? _("command-line") : _("in-file"),
15281 is_define ? _("definition") : _("undefinition"),
15282 line == 0 ? _("zero") : _("non-zero"), line, body);
15283
15284 if (is_define)
15285 parse_macro_definition (current_file, line, body);
15286 else
15287 {
15288 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15289 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15290 macro_undef (current_file, line, body);
15291 }
15292 }
15293 break;
15294
15295 case DW_MACRO_GNU_start_file:
15296 {
15297 unsigned int bytes_read;
15298 int line, file;
15299
15300 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15301 mac_ptr += bytes_read;
15302 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15303 mac_ptr += bytes_read;
15304
15305 if ((line == 0 && !at_commandline)
15306 || (line != 0 && at_commandline))
15307 complaint (&symfile_complaints,
15308 _("debug info gives source %d included "
15309 "from %s at %s line %d"),
15310 file, at_commandline ? _("command-line") : _("file"),
15311 line == 0 ? _("zero") : _("non-zero"), line);
15312
15313 if (at_commandline)
15314 {
15315 /* This DW_MACRO_GNU_start_file was executed in the
15316 pass one. */
15317 at_commandline = 0;
15318 }
15319 else
15320 current_file = macro_start_file (file, line,
15321 current_file, comp_dir,
15322 lh, objfile);
15323 }
15324 break;
15325
15326 case DW_MACRO_GNU_end_file:
15327 if (! current_file)
15328 complaint (&symfile_complaints,
15329 _("macro debug info has an unmatched "
15330 "`close_file' directive"));
15331 else
15332 {
15333 current_file = current_file->included_by;
15334 if (! current_file)
15335 {
15336 enum dwarf_macro_record_type next_type;
15337
15338 /* GCC circa March 2002 doesn't produce the zero
15339 type byte marking the end of the compilation
15340 unit. Complain if it's not there, but exit no
15341 matter what. */
15342
15343 /* Do we at least have room for a macinfo type byte? */
15344 if (mac_ptr >= mac_end)
15345 {
15346 dwarf2_macros_too_long_complaint (section);
15347 return;
15348 }
15349
15350 /* We don't increment mac_ptr here, so this is just
15351 a look-ahead. */
15352 next_type = read_1_byte (abfd, mac_ptr);
15353 if (next_type != 0)
15354 complaint (&symfile_complaints,
15355 _("no terminating 0-type entry for "
15356 "macros in `.debug_macinfo' section"));
15357
15358 return;
15359 }
15360 }
15361 break;
15362
15363 case DW_MACRO_GNU_transparent_include:
15364 {
15365 LONGEST offset;
15366
15367 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15368 mac_ptr += offset_size;
15369
15370 dwarf_decode_macro_bytes (abfd,
15371 section->buffer + offset,
15372 mac_end, current_file,
15373 lh, comp_dir,
15374 section, section_is_gnu,
15375 offset_size, objfile);
15376 }
15377 break;
15378
15379 case DW_MACINFO_vendor_ext:
15380 if (!section_is_gnu)
15381 {
15382 unsigned int bytes_read;
15383 int constant;
15384
15385 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15386 mac_ptr += bytes_read;
15387 read_direct_string (abfd, mac_ptr, &bytes_read);
15388 mac_ptr += bytes_read;
15389
15390 /* We don't recognize any vendor extensions. */
15391 break;
15392 }
15393 /* FALLTHROUGH */
15394
15395 default:
15396 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15397 mac_ptr, abfd, offset_size,
15398 section);
15399 if (mac_ptr == NULL)
15400 return;
15401 break;
15402 }
15403 } while (macinfo_type != 0);
15404 }
15405
15406 static void
15407 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15408 char *comp_dir, bfd *abfd,
15409 struct dwarf2_cu *cu,
15410 struct dwarf2_section_info *section,
15411 int section_is_gnu)
15412 {
15413 gdb_byte *mac_ptr, *mac_end;
15414 struct macro_source_file *current_file = 0;
15415 enum dwarf_macro_record_type macinfo_type;
15416 unsigned int offset_size = cu->header.offset_size;
15417 gdb_byte *opcode_definitions[256];
15418
15419 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15420 if (section->buffer == NULL)
15421 {
15422 complaint (&symfile_complaints, _("missing %s section"),
15423 section->asection->name);
15424 return;
15425 }
15426
15427 /* First pass: Find the name of the base filename.
15428 This filename is needed in order to process all macros whose definition
15429 (or undefinition) comes from the command line. These macros are defined
15430 before the first DW_MACINFO_start_file entry, and yet still need to be
15431 associated to the base file.
15432
15433 To determine the base file name, we scan the macro definitions until we
15434 reach the first DW_MACINFO_start_file entry. We then initialize
15435 CURRENT_FILE accordingly so that any macro definition found before the
15436 first DW_MACINFO_start_file can still be associated to the base file. */
15437
15438 mac_ptr = section->buffer + offset;
15439 mac_end = section->buffer + section->size;
15440
15441 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15442 &offset_size, section_is_gnu);
15443 if (mac_ptr == NULL)
15444 {
15445 /* We already issued a complaint. */
15446 return;
15447 }
15448
15449 do
15450 {
15451 /* Do we at least have room for a macinfo type byte? */
15452 if (mac_ptr >= mac_end)
15453 {
15454 /* Complaint is printed during the second pass as GDB will probably
15455 stop the first pass earlier upon finding
15456 DW_MACINFO_start_file. */
15457 break;
15458 }
15459
15460 macinfo_type = read_1_byte (abfd, mac_ptr);
15461 mac_ptr++;
15462
15463 /* Note that we rely on the fact that the corresponding GNU and
15464 DWARF constants are the same. */
15465 switch (macinfo_type)
15466 {
15467 /* A zero macinfo type indicates the end of the macro
15468 information. */
15469 case 0:
15470 break;
15471
15472 case DW_MACRO_GNU_define:
15473 case DW_MACRO_GNU_undef:
15474 /* Only skip the data by MAC_PTR. */
15475 {
15476 unsigned int bytes_read;
15477
15478 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15479 mac_ptr += bytes_read;
15480 read_direct_string (abfd, mac_ptr, &bytes_read);
15481 mac_ptr += bytes_read;
15482 }
15483 break;
15484
15485 case DW_MACRO_GNU_start_file:
15486 {
15487 unsigned int bytes_read;
15488 int line, file;
15489
15490 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15491 mac_ptr += bytes_read;
15492 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15493 mac_ptr += bytes_read;
15494
15495 current_file = macro_start_file (file, line, current_file,
15496 comp_dir, lh, cu->objfile);
15497 }
15498 break;
15499
15500 case DW_MACRO_GNU_end_file:
15501 /* No data to skip by MAC_PTR. */
15502 break;
15503
15504 case DW_MACRO_GNU_define_indirect:
15505 case DW_MACRO_GNU_undef_indirect:
15506 {
15507 unsigned int bytes_read;
15508
15509 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15510 mac_ptr += bytes_read;
15511 mac_ptr += offset_size;
15512 }
15513 break;
15514
15515 case DW_MACRO_GNU_transparent_include:
15516 /* Note that, according to the spec, a transparent include
15517 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15518 skip this opcode. */
15519 mac_ptr += offset_size;
15520 break;
15521
15522 case DW_MACINFO_vendor_ext:
15523 /* Only skip the data by MAC_PTR. */
15524 if (!section_is_gnu)
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 /* FALLTHROUGH */
15534
15535 default:
15536 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15537 mac_ptr, abfd, offset_size,
15538 section);
15539 if (mac_ptr == NULL)
15540 return;
15541 break;
15542 }
15543 } while (macinfo_type != 0 && current_file == NULL);
15544
15545 /* Second pass: Process all entries.
15546
15547 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15548 command-line macro definitions/undefinitions. This flag is unset when we
15549 reach the first DW_MACINFO_start_file entry. */
15550
15551 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15552 current_file, lh, comp_dir, section, section_is_gnu,
15553 offset_size, cu->objfile);
15554 }
15555
15556 /* Check if the attribute's form is a DW_FORM_block*
15557 if so return true else false. */
15558 static int
15559 attr_form_is_block (struct attribute *attr)
15560 {
15561 return (attr == NULL ? 0 :
15562 attr->form == DW_FORM_block1
15563 || attr->form == DW_FORM_block2
15564 || attr->form == DW_FORM_block4
15565 || attr->form == DW_FORM_block
15566 || attr->form == DW_FORM_exprloc);
15567 }
15568
15569 /* Return non-zero if ATTR's value is a section offset --- classes
15570 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15571 You may use DW_UNSND (attr) to retrieve such offsets.
15572
15573 Section 7.5.4, "Attribute Encodings", explains that no attribute
15574 may have a value that belongs to more than one of these classes; it
15575 would be ambiguous if we did, because we use the same forms for all
15576 of them. */
15577 static int
15578 attr_form_is_section_offset (struct attribute *attr)
15579 {
15580 return (attr->form == DW_FORM_data4
15581 || attr->form == DW_FORM_data8
15582 || attr->form == DW_FORM_sec_offset);
15583 }
15584
15585
15586 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15587 zero otherwise. When this function returns true, you can apply
15588 dwarf2_get_attr_constant_value to it.
15589
15590 However, note that for some attributes you must check
15591 attr_form_is_section_offset before using this test. DW_FORM_data4
15592 and DW_FORM_data8 are members of both the constant class, and of
15593 the classes that contain offsets into other debug sections
15594 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15595 that, if an attribute's can be either a constant or one of the
15596 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15597 taken as section offsets, not constants. */
15598 static int
15599 attr_form_is_constant (struct attribute *attr)
15600 {
15601 switch (attr->form)
15602 {
15603 case DW_FORM_sdata:
15604 case DW_FORM_udata:
15605 case DW_FORM_data1:
15606 case DW_FORM_data2:
15607 case DW_FORM_data4:
15608 case DW_FORM_data8:
15609 return 1;
15610 default:
15611 return 0;
15612 }
15613 }
15614
15615 /* A helper function that fills in a dwarf2_loclist_baton. */
15616
15617 static void
15618 fill_in_loclist_baton (struct dwarf2_cu *cu,
15619 struct dwarf2_loclist_baton *baton,
15620 struct attribute *attr)
15621 {
15622 dwarf2_read_section (dwarf2_per_objfile->objfile,
15623 &dwarf2_per_objfile->loc);
15624
15625 baton->per_cu = cu->per_cu;
15626 gdb_assert (baton->per_cu);
15627 /* We don't know how long the location list is, but make sure we
15628 don't run off the edge of the section. */
15629 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15630 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15631 baton->base_address = cu->base_address;
15632 }
15633
15634 static void
15635 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15636 struct dwarf2_cu *cu)
15637 {
15638 if (attr_form_is_section_offset (attr)
15639 /* ".debug_loc" may not exist at all, or the offset may be outside
15640 the section. If so, fall through to the complaint in the
15641 other branch. */
15642 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15643 &dwarf2_per_objfile->loc))
15644 {
15645 struct dwarf2_loclist_baton *baton;
15646
15647 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15648 sizeof (struct dwarf2_loclist_baton));
15649
15650 fill_in_loclist_baton (cu, baton, attr);
15651
15652 if (cu->base_known == 0)
15653 complaint (&symfile_complaints,
15654 _("Location list used without "
15655 "specifying the CU base address."));
15656
15657 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15658 SYMBOL_LOCATION_BATON (sym) = baton;
15659 }
15660 else
15661 {
15662 struct dwarf2_locexpr_baton *baton;
15663
15664 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15665 sizeof (struct dwarf2_locexpr_baton));
15666 baton->per_cu = cu->per_cu;
15667 gdb_assert (baton->per_cu);
15668
15669 if (attr_form_is_block (attr))
15670 {
15671 /* Note that we're just copying the block's data pointer
15672 here, not the actual data. We're still pointing into the
15673 info_buffer for SYM's objfile; right now we never release
15674 that buffer, but when we do clean up properly this may
15675 need to change. */
15676 baton->size = DW_BLOCK (attr)->size;
15677 baton->data = DW_BLOCK (attr)->data;
15678 }
15679 else
15680 {
15681 dwarf2_invalid_attrib_class_complaint ("location description",
15682 SYMBOL_NATURAL_NAME (sym));
15683 baton->size = 0;
15684 }
15685
15686 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15687 SYMBOL_LOCATION_BATON (sym) = baton;
15688 }
15689 }
15690
15691 /* Return the OBJFILE associated with the compilation unit CU. If CU
15692 came from a separate debuginfo file, then the master objfile is
15693 returned. */
15694
15695 struct objfile *
15696 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15697 {
15698 struct objfile *objfile = per_cu->objfile;
15699
15700 /* Return the master objfile, so that we can report and look up the
15701 correct file containing this variable. */
15702 if (objfile->separate_debug_objfile_backlink)
15703 objfile = objfile->separate_debug_objfile_backlink;
15704
15705 return objfile;
15706 }
15707
15708 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15709 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15710 CU_HEADERP first. */
15711
15712 static const struct comp_unit_head *
15713 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15714 struct dwarf2_per_cu_data *per_cu)
15715 {
15716 struct objfile *objfile;
15717 struct dwarf2_per_objfile *per_objfile;
15718 gdb_byte *info_ptr;
15719
15720 if (per_cu->cu)
15721 return &per_cu->cu->header;
15722
15723 objfile = per_cu->objfile;
15724 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15725 info_ptr = per_objfile->info.buffer + per_cu->offset;
15726
15727 memset (cu_headerp, 0, sizeof (*cu_headerp));
15728 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15729
15730 return cu_headerp;
15731 }
15732
15733 /* Return the address size given in the compilation unit header for CU. */
15734
15735 CORE_ADDR
15736 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15737 {
15738 struct comp_unit_head cu_header_local;
15739 const struct comp_unit_head *cu_headerp;
15740
15741 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15742
15743 return cu_headerp->addr_size;
15744 }
15745
15746 /* Return the offset size given in the compilation unit header for CU. */
15747
15748 int
15749 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15750 {
15751 struct comp_unit_head cu_header_local;
15752 const struct comp_unit_head *cu_headerp;
15753
15754 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15755
15756 return cu_headerp->offset_size;
15757 }
15758
15759 /* See its dwarf2loc.h declaration. */
15760
15761 int
15762 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15763 {
15764 struct comp_unit_head cu_header_local;
15765 const struct comp_unit_head *cu_headerp;
15766
15767 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15768
15769 if (cu_headerp->version == 2)
15770 return cu_headerp->addr_size;
15771 else
15772 return cu_headerp->offset_size;
15773 }
15774
15775 /* Return the text offset of the CU. The returned offset comes from
15776 this CU's objfile. If this objfile came from a separate debuginfo
15777 file, then the offset may be different from the corresponding
15778 offset in the parent objfile. */
15779
15780 CORE_ADDR
15781 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15782 {
15783 struct objfile *objfile = per_cu->objfile;
15784
15785 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15786 }
15787
15788 /* Locate the .debug_info compilation unit from CU's objfile which contains
15789 the DIE at OFFSET. Raises an error on failure. */
15790
15791 static struct dwarf2_per_cu_data *
15792 dwarf2_find_containing_comp_unit (unsigned int offset,
15793 struct objfile *objfile)
15794 {
15795 struct dwarf2_per_cu_data *this_cu;
15796 int low, high;
15797
15798 low = 0;
15799 high = dwarf2_per_objfile->n_comp_units - 1;
15800 while (high > low)
15801 {
15802 int mid = low + (high - low) / 2;
15803
15804 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15805 high = mid;
15806 else
15807 low = mid + 1;
15808 }
15809 gdb_assert (low == high);
15810 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15811 {
15812 if (low == 0)
15813 error (_("Dwarf Error: could not find partial DIE containing "
15814 "offset 0x%lx [in module %s]"),
15815 (long) offset, bfd_get_filename (objfile->obfd));
15816
15817 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15818 return dwarf2_per_objfile->all_comp_units[low-1];
15819 }
15820 else
15821 {
15822 this_cu = dwarf2_per_objfile->all_comp_units[low];
15823 if (low == dwarf2_per_objfile->n_comp_units - 1
15824 && offset >= this_cu->offset + this_cu->length)
15825 error (_("invalid dwarf2 offset %u"), offset);
15826 gdb_assert (offset < this_cu->offset + this_cu->length);
15827 return this_cu;
15828 }
15829 }
15830
15831 /* Locate the compilation unit from OBJFILE which is located at exactly
15832 OFFSET. Raises an error on failure. */
15833
15834 static struct dwarf2_per_cu_data *
15835 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15836 {
15837 struct dwarf2_per_cu_data *this_cu;
15838
15839 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15840 if (this_cu->offset != offset)
15841 error (_("no compilation unit with offset %u."), offset);
15842 return this_cu;
15843 }
15844
15845 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
15846
15847 static void
15848 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15849 {
15850 memset (cu, 0, sizeof (*cu));
15851 cu->objfile = objfile;
15852 obstack_init (&cu->comp_unit_obstack);
15853 }
15854
15855 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15856
15857 static void
15858 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15859 {
15860 struct attribute *attr;
15861
15862 /* Set the language we're debugging. */
15863 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15864 if (attr)
15865 set_cu_language (DW_UNSND (attr), cu);
15866 else
15867 {
15868 cu->language = language_minimal;
15869 cu->language_defn = language_def (cu->language);
15870 }
15871 }
15872
15873 /* Release one cached compilation unit, CU. We unlink it from the tree
15874 of compilation units, but we don't remove it from the read_in_chain;
15875 the caller is responsible for that.
15876 NOTE: DATA is a void * because this function is also used as a
15877 cleanup routine. */
15878
15879 static void
15880 free_one_comp_unit (void *data)
15881 {
15882 struct dwarf2_cu *cu = data;
15883
15884 if (cu->per_cu != NULL)
15885 cu->per_cu->cu = NULL;
15886 cu->per_cu = NULL;
15887
15888 obstack_free (&cu->comp_unit_obstack, NULL);
15889
15890 xfree (cu);
15891 }
15892
15893 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15894 when we're finished with it. We can't free the pointer itself, but be
15895 sure to unlink it from the cache. Also release any associated storage
15896 and perform cache maintenance.
15897
15898 Only used during partial symbol parsing. */
15899
15900 static void
15901 free_stack_comp_unit (void *data)
15902 {
15903 struct dwarf2_cu *cu = data;
15904
15905 obstack_free (&cu->comp_unit_obstack, NULL);
15906 cu->partial_dies = NULL;
15907
15908 if (cu->per_cu != NULL)
15909 {
15910 /* This compilation unit is on the stack in our caller, so we
15911 should not xfree it. Just unlink it. */
15912 cu->per_cu->cu = NULL;
15913 cu->per_cu = NULL;
15914
15915 /* If we had a per-cu pointer, then we may have other compilation
15916 units loaded, so age them now. */
15917 age_cached_comp_units ();
15918 }
15919 }
15920
15921 /* Free all cached compilation units. */
15922
15923 static void
15924 free_cached_comp_units (void *data)
15925 {
15926 struct dwarf2_per_cu_data *per_cu, **last_chain;
15927
15928 per_cu = dwarf2_per_objfile->read_in_chain;
15929 last_chain = &dwarf2_per_objfile->read_in_chain;
15930 while (per_cu != NULL)
15931 {
15932 struct dwarf2_per_cu_data *next_cu;
15933
15934 next_cu = per_cu->cu->read_in_chain;
15935
15936 free_one_comp_unit (per_cu->cu);
15937 *last_chain = next_cu;
15938
15939 per_cu = next_cu;
15940 }
15941 }
15942
15943 /* Increase the age counter on each cached compilation unit, and free
15944 any that are too old. */
15945
15946 static void
15947 age_cached_comp_units (void)
15948 {
15949 struct dwarf2_per_cu_data *per_cu, **last_chain;
15950
15951 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15952 per_cu = dwarf2_per_objfile->read_in_chain;
15953 while (per_cu != NULL)
15954 {
15955 per_cu->cu->last_used ++;
15956 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15957 dwarf2_mark (per_cu->cu);
15958 per_cu = per_cu->cu->read_in_chain;
15959 }
15960
15961 per_cu = dwarf2_per_objfile->read_in_chain;
15962 last_chain = &dwarf2_per_objfile->read_in_chain;
15963 while (per_cu != NULL)
15964 {
15965 struct dwarf2_per_cu_data *next_cu;
15966
15967 next_cu = per_cu->cu->read_in_chain;
15968
15969 if (!per_cu->cu->mark)
15970 {
15971 free_one_comp_unit (per_cu->cu);
15972 *last_chain = next_cu;
15973 }
15974 else
15975 last_chain = &per_cu->cu->read_in_chain;
15976
15977 per_cu = next_cu;
15978 }
15979 }
15980
15981 /* Remove a single compilation unit from the cache. */
15982
15983 static void
15984 free_one_cached_comp_unit (void *target_cu)
15985 {
15986 struct dwarf2_per_cu_data *per_cu, **last_chain;
15987
15988 per_cu = dwarf2_per_objfile->read_in_chain;
15989 last_chain = &dwarf2_per_objfile->read_in_chain;
15990 while (per_cu != NULL)
15991 {
15992 struct dwarf2_per_cu_data *next_cu;
15993
15994 next_cu = per_cu->cu->read_in_chain;
15995
15996 if (per_cu->cu == target_cu)
15997 {
15998 free_one_comp_unit (per_cu->cu);
15999 *last_chain = next_cu;
16000 break;
16001 }
16002 else
16003 last_chain = &per_cu->cu->read_in_chain;
16004
16005 per_cu = next_cu;
16006 }
16007 }
16008
16009 /* Release all extra memory associated with OBJFILE. */
16010
16011 void
16012 dwarf2_free_objfile (struct objfile *objfile)
16013 {
16014 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16015
16016 if (dwarf2_per_objfile == NULL)
16017 return;
16018
16019 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16020 free_cached_comp_units (NULL);
16021
16022 if (dwarf2_per_objfile->quick_file_names_table)
16023 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16024
16025 /* Everything else should be on the objfile obstack. */
16026 }
16027
16028 /* A pair of DIE offset and GDB type pointer. We store these
16029 in a hash table separate from the DIEs, and preserve them
16030 when the DIEs are flushed out of cache. */
16031
16032 struct dwarf2_offset_and_type
16033 {
16034 unsigned int offset;
16035 struct type *type;
16036 };
16037
16038 /* Hash function for a dwarf2_offset_and_type. */
16039
16040 static hashval_t
16041 offset_and_type_hash (const void *item)
16042 {
16043 const struct dwarf2_offset_and_type *ofs = item;
16044
16045 return ofs->offset;
16046 }
16047
16048 /* Equality function for a dwarf2_offset_and_type. */
16049
16050 static int
16051 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16052 {
16053 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16054 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16055
16056 return ofs_lhs->offset == ofs_rhs->offset;
16057 }
16058
16059 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16060 table if necessary. For convenience, return TYPE.
16061
16062 The DIEs reading must have careful ordering to:
16063 * Not cause infite loops trying to read in DIEs as a prerequisite for
16064 reading current DIE.
16065 * Not trying to dereference contents of still incompletely read in types
16066 while reading in other DIEs.
16067 * Enable referencing still incompletely read in types just by a pointer to
16068 the type without accessing its fields.
16069
16070 Therefore caller should follow these rules:
16071 * Try to fetch any prerequisite types we may need to build this DIE type
16072 before building the type and calling set_die_type.
16073 * After building type call set_die_type for current DIE as soon as
16074 possible before fetching more types to complete the current type.
16075 * Make the type as complete as possible before fetching more types. */
16076
16077 static struct type *
16078 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16079 {
16080 struct dwarf2_offset_and_type **slot, ofs;
16081 struct objfile *objfile = cu->objfile;
16082 htab_t *type_hash_ptr;
16083
16084 /* For Ada types, make sure that the gnat-specific data is always
16085 initialized (if not already set). There are a few types where
16086 we should not be doing so, because the type-specific area is
16087 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16088 where the type-specific area is used to store the floatformat).
16089 But this is not a problem, because the gnat-specific information
16090 is actually not needed for these types. */
16091 if (need_gnat_info (cu)
16092 && TYPE_CODE (type) != TYPE_CODE_FUNC
16093 && TYPE_CODE (type) != TYPE_CODE_FLT
16094 && !HAVE_GNAT_AUX_INFO (type))
16095 INIT_GNAT_SPECIFIC (type);
16096
16097 if (cu->per_cu->debug_types_section)
16098 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16099 else
16100 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16101
16102 if (*type_hash_ptr == NULL)
16103 {
16104 *type_hash_ptr
16105 = htab_create_alloc_ex (127,
16106 offset_and_type_hash,
16107 offset_and_type_eq,
16108 NULL,
16109 &objfile->objfile_obstack,
16110 hashtab_obstack_allocate,
16111 dummy_obstack_deallocate);
16112 }
16113
16114 ofs.offset = die->offset;
16115 ofs.type = type;
16116 slot = (struct dwarf2_offset_and_type **)
16117 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16118 if (*slot)
16119 complaint (&symfile_complaints,
16120 _("A problem internal to GDB: DIE 0x%x has type already set"),
16121 die->offset);
16122 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16123 **slot = ofs;
16124 return type;
16125 }
16126
16127 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16128 table, or return NULL if the die does not have a saved type. */
16129
16130 static struct type *
16131 get_die_type_at_offset (unsigned int offset,
16132 struct dwarf2_per_cu_data *per_cu)
16133 {
16134 struct dwarf2_offset_and_type *slot, ofs;
16135 htab_t type_hash;
16136
16137 if (per_cu->debug_types_section)
16138 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16139 else
16140 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16141 if (type_hash == NULL)
16142 return NULL;
16143
16144 ofs.offset = offset;
16145 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16146 if (slot)
16147 return slot->type;
16148 else
16149 return NULL;
16150 }
16151
16152 /* Look up the type for DIE in the appropriate type_hash table,
16153 or return NULL if DIE does not have a saved type. */
16154
16155 static struct type *
16156 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16157 {
16158 return get_die_type_at_offset (die->offset, cu->per_cu);
16159 }
16160
16161 /* Add a dependence relationship from CU to REF_PER_CU. */
16162
16163 static void
16164 dwarf2_add_dependence (struct dwarf2_cu *cu,
16165 struct dwarf2_per_cu_data *ref_per_cu)
16166 {
16167 void **slot;
16168
16169 if (cu->dependencies == NULL)
16170 cu->dependencies
16171 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16172 NULL, &cu->comp_unit_obstack,
16173 hashtab_obstack_allocate,
16174 dummy_obstack_deallocate);
16175
16176 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16177 if (*slot == NULL)
16178 *slot = ref_per_cu;
16179 }
16180
16181 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16182 Set the mark field in every compilation unit in the
16183 cache that we must keep because we are keeping CU. */
16184
16185 static int
16186 dwarf2_mark_helper (void **slot, void *data)
16187 {
16188 struct dwarf2_per_cu_data *per_cu;
16189
16190 per_cu = (struct dwarf2_per_cu_data *) *slot;
16191
16192 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16193 reading of the chain. As such dependencies remain valid it is not much
16194 useful to track and undo them during QUIT cleanups. */
16195 if (per_cu->cu == NULL)
16196 return 1;
16197
16198 if (per_cu->cu->mark)
16199 return 1;
16200 per_cu->cu->mark = 1;
16201
16202 if (per_cu->cu->dependencies != NULL)
16203 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16204
16205 return 1;
16206 }
16207
16208 /* Set the mark field in CU and in every other compilation unit in the
16209 cache that we must keep because we are keeping CU. */
16210
16211 static void
16212 dwarf2_mark (struct dwarf2_cu *cu)
16213 {
16214 if (cu->mark)
16215 return;
16216 cu->mark = 1;
16217 if (cu->dependencies != NULL)
16218 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16219 }
16220
16221 static void
16222 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16223 {
16224 while (per_cu)
16225 {
16226 per_cu->cu->mark = 0;
16227 per_cu = per_cu->cu->read_in_chain;
16228 }
16229 }
16230
16231 /* Trivial hash function for partial_die_info: the hash value of a DIE
16232 is its offset in .debug_info for this objfile. */
16233
16234 static hashval_t
16235 partial_die_hash (const void *item)
16236 {
16237 const struct partial_die_info *part_die = item;
16238
16239 return part_die->offset;
16240 }
16241
16242 /* Trivial comparison function for partial_die_info structures: two DIEs
16243 are equal if they have the same offset. */
16244
16245 static int
16246 partial_die_eq (const void *item_lhs, const void *item_rhs)
16247 {
16248 const struct partial_die_info *part_die_lhs = item_lhs;
16249 const struct partial_die_info *part_die_rhs = item_rhs;
16250
16251 return part_die_lhs->offset == part_die_rhs->offset;
16252 }
16253
16254 static struct cmd_list_element *set_dwarf2_cmdlist;
16255 static struct cmd_list_element *show_dwarf2_cmdlist;
16256
16257 static void
16258 set_dwarf2_cmd (char *args, int from_tty)
16259 {
16260 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16261 }
16262
16263 static void
16264 show_dwarf2_cmd (char *args, int from_tty)
16265 {
16266 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16267 }
16268
16269 /* If section described by INFO was mmapped, munmap it now. */
16270
16271 static void
16272 munmap_section_buffer (struct dwarf2_section_info *info)
16273 {
16274 if (info->map_addr != NULL)
16275 {
16276 #ifdef HAVE_MMAP
16277 int res;
16278
16279 res = munmap (info->map_addr, info->map_len);
16280 gdb_assert (res == 0);
16281 #else
16282 /* Without HAVE_MMAP, we should never be here to begin with. */
16283 gdb_assert_not_reached ("no mmap support");
16284 #endif
16285 }
16286 }
16287
16288 /* munmap debug sections for OBJFILE, if necessary. */
16289
16290 static void
16291 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16292 {
16293 struct dwarf2_per_objfile *data = d;
16294 int ix;
16295 struct dwarf2_section_info *section;
16296
16297 /* This is sorted according to the order they're defined in to make it easier
16298 to keep in sync. */
16299 munmap_section_buffer (&data->info);
16300 munmap_section_buffer (&data->abbrev);
16301 munmap_section_buffer (&data->line);
16302 munmap_section_buffer (&data->loc);
16303 munmap_section_buffer (&data->macinfo);
16304 munmap_section_buffer (&data->macro);
16305 munmap_section_buffer (&data->str);
16306 munmap_section_buffer (&data->ranges);
16307 munmap_section_buffer (&data->frame);
16308 munmap_section_buffer (&data->eh_frame);
16309 munmap_section_buffer (&data->gdb_index);
16310
16311 for (ix = 0;
16312 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16313 ++ix)
16314 munmap_section_buffer (section);
16315
16316 VEC_free (dwarf2_section_info_def, data->types);
16317 }
16318
16319 \f
16320 /* The "save gdb-index" command. */
16321
16322 /* The contents of the hash table we create when building the string
16323 table. */
16324 struct strtab_entry
16325 {
16326 offset_type offset;
16327 const char *str;
16328 };
16329
16330 /* Hash function for a strtab_entry.
16331
16332 Function is used only during write_hash_table so no index format backward
16333 compatibility is needed. */
16334
16335 static hashval_t
16336 hash_strtab_entry (const void *e)
16337 {
16338 const struct strtab_entry *entry = e;
16339 return mapped_index_string_hash (INT_MAX, entry->str);
16340 }
16341
16342 /* Equality function for a strtab_entry. */
16343
16344 static int
16345 eq_strtab_entry (const void *a, const void *b)
16346 {
16347 const struct strtab_entry *ea = a;
16348 const struct strtab_entry *eb = b;
16349 return !strcmp (ea->str, eb->str);
16350 }
16351
16352 /* Create a strtab_entry hash table. */
16353
16354 static htab_t
16355 create_strtab (void)
16356 {
16357 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16358 xfree, xcalloc, xfree);
16359 }
16360
16361 /* Add a string to the constant pool. Return the string's offset in
16362 host order. */
16363
16364 static offset_type
16365 add_string (htab_t table, struct obstack *cpool, const char *str)
16366 {
16367 void **slot;
16368 struct strtab_entry entry;
16369 struct strtab_entry *result;
16370
16371 entry.str = str;
16372 slot = htab_find_slot (table, &entry, INSERT);
16373 if (*slot)
16374 result = *slot;
16375 else
16376 {
16377 result = XNEW (struct strtab_entry);
16378 result->offset = obstack_object_size (cpool);
16379 result->str = str;
16380 obstack_grow_str0 (cpool, str);
16381 *slot = result;
16382 }
16383 return result->offset;
16384 }
16385
16386 /* An entry in the symbol table. */
16387 struct symtab_index_entry
16388 {
16389 /* The name of the symbol. */
16390 const char *name;
16391 /* The offset of the name in the constant pool. */
16392 offset_type index_offset;
16393 /* A sorted vector of the indices of all the CUs that hold an object
16394 of this name. */
16395 VEC (offset_type) *cu_indices;
16396 };
16397
16398 /* The symbol table. This is a power-of-2-sized hash table. */
16399 struct mapped_symtab
16400 {
16401 offset_type n_elements;
16402 offset_type size;
16403 struct symtab_index_entry **data;
16404 };
16405
16406 /* Hash function for a symtab_index_entry. */
16407
16408 static hashval_t
16409 hash_symtab_entry (const void *e)
16410 {
16411 const struct symtab_index_entry *entry = e;
16412 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16413 sizeof (offset_type) * VEC_length (offset_type,
16414 entry->cu_indices),
16415 0);
16416 }
16417
16418 /* Equality function for a symtab_index_entry. */
16419
16420 static int
16421 eq_symtab_entry (const void *a, const void *b)
16422 {
16423 const struct symtab_index_entry *ea = a;
16424 const struct symtab_index_entry *eb = b;
16425 int len = VEC_length (offset_type, ea->cu_indices);
16426 if (len != VEC_length (offset_type, eb->cu_indices))
16427 return 0;
16428 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16429 VEC_address (offset_type, eb->cu_indices),
16430 sizeof (offset_type) * len);
16431 }
16432
16433 /* Destroy a symtab_index_entry. */
16434
16435 static void
16436 delete_symtab_entry (void *p)
16437 {
16438 struct symtab_index_entry *entry = p;
16439 VEC_free (offset_type, entry->cu_indices);
16440 xfree (entry);
16441 }
16442
16443 /* Create a hash table holding symtab_index_entry objects. */
16444
16445 static htab_t
16446 create_symbol_hash_table (void)
16447 {
16448 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16449 delete_symtab_entry, xcalloc, xfree);
16450 }
16451
16452 /* Create a new mapped symtab object. */
16453
16454 static struct mapped_symtab *
16455 create_mapped_symtab (void)
16456 {
16457 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16458 symtab->n_elements = 0;
16459 symtab->size = 1024;
16460 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16461 return symtab;
16462 }
16463
16464 /* Destroy a mapped_symtab. */
16465
16466 static void
16467 cleanup_mapped_symtab (void *p)
16468 {
16469 struct mapped_symtab *symtab = p;
16470 /* The contents of the array are freed when the other hash table is
16471 destroyed. */
16472 xfree (symtab->data);
16473 xfree (symtab);
16474 }
16475
16476 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16477 the slot.
16478
16479 Function is used only during write_hash_table so no index format backward
16480 compatibility is needed. */
16481
16482 static struct symtab_index_entry **
16483 find_slot (struct mapped_symtab *symtab, const char *name)
16484 {
16485 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16486
16487 index = hash & (symtab->size - 1);
16488 step = ((hash * 17) & (symtab->size - 1)) | 1;
16489
16490 for (;;)
16491 {
16492 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16493 return &symtab->data[index];
16494 index = (index + step) & (symtab->size - 1);
16495 }
16496 }
16497
16498 /* Expand SYMTAB's hash table. */
16499
16500 static void
16501 hash_expand (struct mapped_symtab *symtab)
16502 {
16503 offset_type old_size = symtab->size;
16504 offset_type i;
16505 struct symtab_index_entry **old_entries = symtab->data;
16506
16507 symtab->size *= 2;
16508 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16509
16510 for (i = 0; i < old_size; ++i)
16511 {
16512 if (old_entries[i])
16513 {
16514 struct symtab_index_entry **slot = find_slot (symtab,
16515 old_entries[i]->name);
16516 *slot = old_entries[i];
16517 }
16518 }
16519
16520 xfree (old_entries);
16521 }
16522
16523 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16524 is the index of the CU in which the symbol appears. */
16525
16526 static void
16527 add_index_entry (struct mapped_symtab *symtab, const char *name,
16528 offset_type cu_index)
16529 {
16530 struct symtab_index_entry **slot;
16531
16532 ++symtab->n_elements;
16533 if (4 * symtab->n_elements / 3 >= symtab->size)
16534 hash_expand (symtab);
16535
16536 slot = find_slot (symtab, name);
16537 if (!*slot)
16538 {
16539 *slot = XNEW (struct symtab_index_entry);
16540 (*slot)->name = name;
16541 (*slot)->cu_indices = NULL;
16542 }
16543 /* Don't push an index twice. Due to how we add entries we only
16544 have to check the last one. */
16545 if (VEC_empty (offset_type, (*slot)->cu_indices)
16546 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16547 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16548 }
16549
16550 /* Add a vector of indices to the constant pool. */
16551
16552 static offset_type
16553 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16554 struct symtab_index_entry *entry)
16555 {
16556 void **slot;
16557
16558 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16559 if (!*slot)
16560 {
16561 offset_type len = VEC_length (offset_type, entry->cu_indices);
16562 offset_type val = MAYBE_SWAP (len);
16563 offset_type iter;
16564 int i;
16565
16566 *slot = entry;
16567 entry->index_offset = obstack_object_size (cpool);
16568
16569 obstack_grow (cpool, &val, sizeof (val));
16570 for (i = 0;
16571 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16572 ++i)
16573 {
16574 val = MAYBE_SWAP (iter);
16575 obstack_grow (cpool, &val, sizeof (val));
16576 }
16577 }
16578 else
16579 {
16580 struct symtab_index_entry *old_entry = *slot;
16581 entry->index_offset = old_entry->index_offset;
16582 entry = old_entry;
16583 }
16584 return entry->index_offset;
16585 }
16586
16587 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16588 constant pool entries going into the obstack CPOOL. */
16589
16590 static void
16591 write_hash_table (struct mapped_symtab *symtab,
16592 struct obstack *output, struct obstack *cpool)
16593 {
16594 offset_type i;
16595 htab_t symbol_hash_table;
16596 htab_t str_table;
16597
16598 symbol_hash_table = create_symbol_hash_table ();
16599 str_table = create_strtab ();
16600
16601 /* We add all the index vectors to the constant pool first, to
16602 ensure alignment is ok. */
16603 for (i = 0; i < symtab->size; ++i)
16604 {
16605 if (symtab->data[i])
16606 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16607 }
16608
16609 /* Now write out the hash table. */
16610 for (i = 0; i < symtab->size; ++i)
16611 {
16612 offset_type str_off, vec_off;
16613
16614 if (symtab->data[i])
16615 {
16616 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16617 vec_off = symtab->data[i]->index_offset;
16618 }
16619 else
16620 {
16621 /* While 0 is a valid constant pool index, it is not valid
16622 to have 0 for both offsets. */
16623 str_off = 0;
16624 vec_off = 0;
16625 }
16626
16627 str_off = MAYBE_SWAP (str_off);
16628 vec_off = MAYBE_SWAP (vec_off);
16629
16630 obstack_grow (output, &str_off, sizeof (str_off));
16631 obstack_grow (output, &vec_off, sizeof (vec_off));
16632 }
16633
16634 htab_delete (str_table);
16635 htab_delete (symbol_hash_table);
16636 }
16637
16638 /* Struct to map psymtab to CU index in the index file. */
16639 struct psymtab_cu_index_map
16640 {
16641 struct partial_symtab *psymtab;
16642 unsigned int cu_index;
16643 };
16644
16645 static hashval_t
16646 hash_psymtab_cu_index (const void *item)
16647 {
16648 const struct psymtab_cu_index_map *map = item;
16649
16650 return htab_hash_pointer (map->psymtab);
16651 }
16652
16653 static int
16654 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16655 {
16656 const struct psymtab_cu_index_map *lhs = item_lhs;
16657 const struct psymtab_cu_index_map *rhs = item_rhs;
16658
16659 return lhs->psymtab == rhs->psymtab;
16660 }
16661
16662 /* Helper struct for building the address table. */
16663 struct addrmap_index_data
16664 {
16665 struct objfile *objfile;
16666 struct obstack *addr_obstack;
16667 htab_t cu_index_htab;
16668
16669 /* Non-zero if the previous_* fields are valid.
16670 We can't write an entry until we see the next entry (since it is only then
16671 that we know the end of the entry). */
16672 int previous_valid;
16673 /* Index of the CU in the table of all CUs in the index file. */
16674 unsigned int previous_cu_index;
16675 /* Start address of the CU. */
16676 CORE_ADDR previous_cu_start;
16677 };
16678
16679 /* Write an address entry to OBSTACK. */
16680
16681 static void
16682 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16683 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16684 {
16685 offset_type cu_index_to_write;
16686 char addr[8];
16687 CORE_ADDR baseaddr;
16688
16689 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16690
16691 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16692 obstack_grow (obstack, addr, 8);
16693 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16694 obstack_grow (obstack, addr, 8);
16695 cu_index_to_write = MAYBE_SWAP (cu_index);
16696 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16697 }
16698
16699 /* Worker function for traversing an addrmap to build the address table. */
16700
16701 static int
16702 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16703 {
16704 struct addrmap_index_data *data = datap;
16705 struct partial_symtab *pst = obj;
16706 offset_type cu_index;
16707 void **slot;
16708
16709 if (data->previous_valid)
16710 add_address_entry (data->objfile, data->addr_obstack,
16711 data->previous_cu_start, start_addr,
16712 data->previous_cu_index);
16713
16714 data->previous_cu_start = start_addr;
16715 if (pst != NULL)
16716 {
16717 struct psymtab_cu_index_map find_map, *map;
16718 find_map.psymtab = pst;
16719 map = htab_find (data->cu_index_htab, &find_map);
16720 gdb_assert (map != NULL);
16721 data->previous_cu_index = map->cu_index;
16722 data->previous_valid = 1;
16723 }
16724 else
16725 data->previous_valid = 0;
16726
16727 return 0;
16728 }
16729
16730 /* Write OBJFILE's address map to OBSTACK.
16731 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16732 in the index file. */
16733
16734 static void
16735 write_address_map (struct objfile *objfile, struct obstack *obstack,
16736 htab_t cu_index_htab)
16737 {
16738 struct addrmap_index_data addrmap_index_data;
16739
16740 /* When writing the address table, we have to cope with the fact that
16741 the addrmap iterator only provides the start of a region; we have to
16742 wait until the next invocation to get the start of the next region. */
16743
16744 addrmap_index_data.objfile = objfile;
16745 addrmap_index_data.addr_obstack = obstack;
16746 addrmap_index_data.cu_index_htab = cu_index_htab;
16747 addrmap_index_data.previous_valid = 0;
16748
16749 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16750 &addrmap_index_data);
16751
16752 /* It's highly unlikely the last entry (end address = 0xff...ff)
16753 is valid, but we should still handle it.
16754 The end address is recorded as the start of the next region, but that
16755 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16756 anyway. */
16757 if (addrmap_index_data.previous_valid)
16758 add_address_entry (objfile, obstack,
16759 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16760 addrmap_index_data.previous_cu_index);
16761 }
16762
16763 /* Add a list of partial symbols to SYMTAB. */
16764
16765 static void
16766 write_psymbols (struct mapped_symtab *symtab,
16767 htab_t psyms_seen,
16768 struct partial_symbol **psymp,
16769 int count,
16770 offset_type cu_index,
16771 int is_static)
16772 {
16773 for (; count-- > 0; ++psymp)
16774 {
16775 void **slot, *lookup;
16776
16777 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16778 error (_("Ada is not currently supported by the index"));
16779
16780 /* We only want to add a given psymbol once. However, we also
16781 want to account for whether it is global or static. So, we
16782 may add it twice, using slightly different values. */
16783 if (is_static)
16784 {
16785 uintptr_t val = 1 | (uintptr_t) *psymp;
16786
16787 lookup = (void *) val;
16788 }
16789 else
16790 lookup = *psymp;
16791
16792 /* Only add a given psymbol once. */
16793 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16794 if (!*slot)
16795 {
16796 *slot = lookup;
16797 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16798 }
16799 }
16800 }
16801
16802 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16803 exception if there is an error. */
16804
16805 static void
16806 write_obstack (FILE *file, struct obstack *obstack)
16807 {
16808 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16809 file)
16810 != obstack_object_size (obstack))
16811 error (_("couldn't data write to file"));
16812 }
16813
16814 /* Unlink a file if the argument is not NULL. */
16815
16816 static void
16817 unlink_if_set (void *p)
16818 {
16819 char **filename = p;
16820 if (*filename)
16821 unlink (*filename);
16822 }
16823
16824 /* A helper struct used when iterating over debug_types. */
16825 struct signatured_type_index_data
16826 {
16827 struct objfile *objfile;
16828 struct mapped_symtab *symtab;
16829 struct obstack *types_list;
16830 htab_t psyms_seen;
16831 int cu_index;
16832 };
16833
16834 /* A helper function that writes a single signatured_type to an
16835 obstack. */
16836
16837 static int
16838 write_one_signatured_type (void **slot, void *d)
16839 {
16840 struct signatured_type_index_data *info = d;
16841 struct signatured_type *entry = (struct signatured_type *) *slot;
16842 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16843 struct partial_symtab *psymtab = per_cu->v.psymtab;
16844 gdb_byte val[8];
16845
16846 write_psymbols (info->symtab,
16847 info->psyms_seen,
16848 info->objfile->global_psymbols.list
16849 + psymtab->globals_offset,
16850 psymtab->n_global_syms, info->cu_index,
16851 0);
16852 write_psymbols (info->symtab,
16853 info->psyms_seen,
16854 info->objfile->static_psymbols.list
16855 + psymtab->statics_offset,
16856 psymtab->n_static_syms, info->cu_index,
16857 1);
16858
16859 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16860 obstack_grow (info->types_list, val, 8);
16861 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16862 obstack_grow (info->types_list, val, 8);
16863 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16864 obstack_grow (info->types_list, val, 8);
16865
16866 ++info->cu_index;
16867
16868 return 1;
16869 }
16870
16871 /* Create an index file for OBJFILE in the directory DIR. */
16872
16873 static void
16874 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16875 {
16876 struct cleanup *cleanup;
16877 char *filename, *cleanup_filename;
16878 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16879 struct obstack cu_list, types_cu_list;
16880 int i;
16881 FILE *out_file;
16882 struct mapped_symtab *symtab;
16883 offset_type val, size_of_contents, total_len;
16884 struct stat st;
16885 char buf[8];
16886 htab_t psyms_seen;
16887 htab_t cu_index_htab;
16888 struct psymtab_cu_index_map *psymtab_cu_index_map;
16889
16890 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16891 return;
16892
16893 if (dwarf2_per_objfile->using_index)
16894 error (_("Cannot use an index to create the index"));
16895
16896 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16897 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16898
16899 if (stat (objfile->name, &st) < 0)
16900 perror_with_name (objfile->name);
16901
16902 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16903 INDEX_SUFFIX, (char *) NULL);
16904 cleanup = make_cleanup (xfree, filename);
16905
16906 out_file = fopen (filename, "wb");
16907 if (!out_file)
16908 error (_("Can't open `%s' for writing"), filename);
16909
16910 cleanup_filename = filename;
16911 make_cleanup (unlink_if_set, &cleanup_filename);
16912
16913 symtab = create_mapped_symtab ();
16914 make_cleanup (cleanup_mapped_symtab, symtab);
16915
16916 obstack_init (&addr_obstack);
16917 make_cleanup_obstack_free (&addr_obstack);
16918
16919 obstack_init (&cu_list);
16920 make_cleanup_obstack_free (&cu_list);
16921
16922 obstack_init (&types_cu_list);
16923 make_cleanup_obstack_free (&types_cu_list);
16924
16925 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16926 NULL, xcalloc, xfree);
16927 make_cleanup_htab_delete (psyms_seen);
16928
16929 /* While we're scanning CU's create a table that maps a psymtab pointer
16930 (which is what addrmap records) to its index (which is what is recorded
16931 in the index file). This will later be needed to write the address
16932 table. */
16933 cu_index_htab = htab_create_alloc (100,
16934 hash_psymtab_cu_index,
16935 eq_psymtab_cu_index,
16936 NULL, xcalloc, xfree);
16937 make_cleanup_htab_delete (cu_index_htab);
16938 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16939 xmalloc (sizeof (struct psymtab_cu_index_map)
16940 * dwarf2_per_objfile->n_comp_units);
16941 make_cleanup (xfree, psymtab_cu_index_map);
16942
16943 /* The CU list is already sorted, so we don't need to do additional
16944 work here. Also, the debug_types entries do not appear in
16945 all_comp_units, but only in their own hash table. */
16946 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16947 {
16948 struct dwarf2_per_cu_data *per_cu
16949 = dwarf2_per_objfile->all_comp_units[i];
16950 struct partial_symtab *psymtab = per_cu->v.psymtab;
16951 gdb_byte val[8];
16952 struct psymtab_cu_index_map *map;
16953 void **slot;
16954
16955 write_psymbols (symtab,
16956 psyms_seen,
16957 objfile->global_psymbols.list + psymtab->globals_offset,
16958 psymtab->n_global_syms, i,
16959 0);
16960 write_psymbols (symtab,
16961 psyms_seen,
16962 objfile->static_psymbols.list + psymtab->statics_offset,
16963 psymtab->n_static_syms, i,
16964 1);
16965
16966 map = &psymtab_cu_index_map[i];
16967 map->psymtab = psymtab;
16968 map->cu_index = i;
16969 slot = htab_find_slot (cu_index_htab, map, INSERT);
16970 gdb_assert (slot != NULL);
16971 gdb_assert (*slot == NULL);
16972 *slot = map;
16973
16974 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16975 obstack_grow (&cu_list, val, 8);
16976 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16977 obstack_grow (&cu_list, val, 8);
16978 }
16979
16980 /* Dump the address map. */
16981 write_address_map (objfile, &addr_obstack, cu_index_htab);
16982
16983 /* Write out the .debug_type entries, if any. */
16984 if (dwarf2_per_objfile->signatured_types)
16985 {
16986 struct signatured_type_index_data sig_data;
16987
16988 sig_data.objfile = objfile;
16989 sig_data.symtab = symtab;
16990 sig_data.types_list = &types_cu_list;
16991 sig_data.psyms_seen = psyms_seen;
16992 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16993 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16994 write_one_signatured_type, &sig_data);
16995 }
16996
16997 obstack_init (&constant_pool);
16998 make_cleanup_obstack_free (&constant_pool);
16999 obstack_init (&symtab_obstack);
17000 make_cleanup_obstack_free (&symtab_obstack);
17001 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17002
17003 obstack_init (&contents);
17004 make_cleanup_obstack_free (&contents);
17005 size_of_contents = 6 * sizeof (offset_type);
17006 total_len = size_of_contents;
17007
17008 /* The version number. */
17009 val = MAYBE_SWAP (5);
17010 obstack_grow (&contents, &val, sizeof (val));
17011
17012 /* The offset of the CU list from the start of the file. */
17013 val = MAYBE_SWAP (total_len);
17014 obstack_grow (&contents, &val, sizeof (val));
17015 total_len += obstack_object_size (&cu_list);
17016
17017 /* The offset of the types CU list from the start of the file. */
17018 val = MAYBE_SWAP (total_len);
17019 obstack_grow (&contents, &val, sizeof (val));
17020 total_len += obstack_object_size (&types_cu_list);
17021
17022 /* The offset of the address table from the start of the file. */
17023 val = MAYBE_SWAP (total_len);
17024 obstack_grow (&contents, &val, sizeof (val));
17025 total_len += obstack_object_size (&addr_obstack);
17026
17027 /* The offset of the symbol table from the start of the file. */
17028 val = MAYBE_SWAP (total_len);
17029 obstack_grow (&contents, &val, sizeof (val));
17030 total_len += obstack_object_size (&symtab_obstack);
17031
17032 /* The offset of the constant pool from the start of the file. */
17033 val = MAYBE_SWAP (total_len);
17034 obstack_grow (&contents, &val, sizeof (val));
17035 total_len += obstack_object_size (&constant_pool);
17036
17037 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17038
17039 write_obstack (out_file, &contents);
17040 write_obstack (out_file, &cu_list);
17041 write_obstack (out_file, &types_cu_list);
17042 write_obstack (out_file, &addr_obstack);
17043 write_obstack (out_file, &symtab_obstack);
17044 write_obstack (out_file, &constant_pool);
17045
17046 fclose (out_file);
17047
17048 /* We want to keep the file, so we set cleanup_filename to NULL
17049 here. See unlink_if_set. */
17050 cleanup_filename = NULL;
17051
17052 do_cleanups (cleanup);
17053 }
17054
17055 /* Implementation of the `save gdb-index' command.
17056
17057 Note that the file format used by this command is documented in the
17058 GDB manual. Any changes here must be documented there. */
17059
17060 static void
17061 save_gdb_index_command (char *arg, int from_tty)
17062 {
17063 struct objfile *objfile;
17064
17065 if (!arg || !*arg)
17066 error (_("usage: save gdb-index DIRECTORY"));
17067
17068 ALL_OBJFILES (objfile)
17069 {
17070 struct stat st;
17071
17072 /* If the objfile does not correspond to an actual file, skip it. */
17073 if (stat (objfile->name, &st) < 0)
17074 continue;
17075
17076 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17077 if (dwarf2_per_objfile)
17078 {
17079 volatile struct gdb_exception except;
17080
17081 TRY_CATCH (except, RETURN_MASK_ERROR)
17082 {
17083 write_psymtabs_to_index (objfile, arg);
17084 }
17085 if (except.reason < 0)
17086 exception_fprintf (gdb_stderr, except,
17087 _("Error while writing index for `%s': "),
17088 objfile->name);
17089 }
17090 }
17091 }
17092
17093 \f
17094
17095 int dwarf2_always_disassemble;
17096
17097 static void
17098 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17099 struct cmd_list_element *c, const char *value)
17100 {
17101 fprintf_filtered (file,
17102 _("Whether to always disassemble "
17103 "DWARF expressions is %s.\n"),
17104 value);
17105 }
17106
17107 static void
17108 show_check_physname (struct ui_file *file, int from_tty,
17109 struct cmd_list_element *c, const char *value)
17110 {
17111 fprintf_filtered (file,
17112 _("Whether to check \"physname\" is %s.\n"),
17113 value);
17114 }
17115
17116 void _initialize_dwarf2_read (void);
17117
17118 void
17119 _initialize_dwarf2_read (void)
17120 {
17121 struct cmd_list_element *c;
17122
17123 dwarf2_objfile_data_key
17124 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17125
17126 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17127 Set DWARF 2 specific variables.\n\
17128 Configure DWARF 2 variables such as the cache size"),
17129 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17130 0/*allow-unknown*/, &maintenance_set_cmdlist);
17131
17132 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17133 Show DWARF 2 specific variables\n\
17134 Show DWARF 2 variables such as the cache size"),
17135 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17136 0/*allow-unknown*/, &maintenance_show_cmdlist);
17137
17138 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17139 &dwarf2_max_cache_age, _("\
17140 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17141 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17142 A higher limit means that cached compilation units will be stored\n\
17143 in memory longer, and more total memory will be used. Zero disables\n\
17144 caching, which can slow down startup."),
17145 NULL,
17146 show_dwarf2_max_cache_age,
17147 &set_dwarf2_cmdlist,
17148 &show_dwarf2_cmdlist);
17149
17150 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17151 &dwarf2_always_disassemble, _("\
17152 Set whether `info address' always disassembles DWARF expressions."), _("\
17153 Show whether `info address' always disassembles DWARF expressions."), _("\
17154 When enabled, DWARF expressions are always printed in an assembly-like\n\
17155 syntax. When disabled, expressions will be printed in a more\n\
17156 conversational style, when possible."),
17157 NULL,
17158 show_dwarf2_always_disassemble,
17159 &set_dwarf2_cmdlist,
17160 &show_dwarf2_cmdlist);
17161
17162 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17163 Set debugging of the dwarf2 DIE reader."), _("\
17164 Show debugging of the dwarf2 DIE reader."), _("\
17165 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17166 The value is the maximum depth to print."),
17167 NULL,
17168 NULL,
17169 &setdebuglist, &showdebuglist);
17170
17171 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17172 Set cross-checking of \"physname\" code against demangler."), _("\
17173 Show cross-checking of \"physname\" code against demangler."), _("\
17174 When enabled, GDB's internal \"physname\" code is checked against\n\
17175 the demangler."),
17176 NULL, show_check_physname,
17177 &setdebuglist, &showdebuglist);
17178
17179 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17180 _("\
17181 Save a gdb-index file.\n\
17182 Usage: save gdb-index DIRECTORY"),
17183 &save_cmdlist);
17184 set_cmd_completer (c, filename_completer);
17185 }
This page took 0.385571 seconds and 4 git commands to generate.