* dwarf2read.c (dwarf2_read_abbrevs): Delete arg "abfd".
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 #include "defs.h"
28 #include "bfd.h"
29 #include "symtab.h"
30 #include "gdbtypes.h"
31 #include "objfiles.h"
32 #include "dwarf2.h"
33 #include "buildsym.h"
34 #include "demangle.h"
35 #include "gdb-demangle.h"
36 #include "expression.h"
37 #include "filenames.h" /* for DOSish file names */
38 #include "macrotab.h"
39 #include "language.h"
40 #include "complaints.h"
41 #include "bcache.h"
42 #include "dwarf2expr.h"
43 #include "dwarf2loc.h"
44 #include "cp-support.h"
45 #include "hashtab.h"
46 #include "command.h"
47 #include "gdbcmd.h"
48 #include "block.h"
49 #include "addrmap.h"
50 #include "typeprint.h"
51 #include "jv-lang.h"
52 #include "psympriv.h"
53 #include "exceptions.h"
54 #include "gdb_stat.h"
55 #include "completer.h"
56 #include "vec.h"
57 #include "c-lang.h"
58 #include "valprint.h"
59 #include <ctype.h>
60
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82 typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98 typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in. */
119 static int dwarf2_die_debug = 0;
120
121 /* When non-zero, cross-check physname against demangler. */
122 static int check_physname = 0;
123
124 static int pagesize;
125
126 /* When set, the file that we're processing is known to have debugging
127 info for C++ namespaces. GCC 3.3.x did not produce this information,
128 but later versions do. */
129
130 static int processing_has_namespace_info;
131
132 static const struct objfile_data *dwarf2_objfile_data_key;
133
134 struct dwarf2_section_info
135 {
136 asection *asection;
137 gdb_byte *buffer;
138 bfd_size_type size;
139 /* Not NULL if the section was actually mmapped. */
140 void *map_addr;
141 /* Page aligned size of mmapped area. */
142 bfd_size_type map_len;
143 /* True if we have tried to read this section. */
144 int readin;
145 };
146
147 typedef struct dwarf2_section_info dwarf2_section_info_def;
148 DEF_VEC_O (dwarf2_section_info_def);
149
150 /* All offsets in the index are of this type. It must be
151 architecture-independent. */
152 typedef uint32_t offset_type;
153
154 DEF_VEC_I (offset_type);
155
156 /* A description of the mapped index. The file format is described in
157 a comment by the code that writes the index. */
158 struct mapped_index
159 {
160 /* Index data format version. */
161 int version;
162
163 /* The total length of the buffer. */
164 off_t total_size;
165
166 /* A pointer to the address table data. */
167 const gdb_byte *address_table;
168
169 /* Size of the address table data in bytes. */
170 offset_type address_table_size;
171
172 /* The symbol table, implemented as a hash table. */
173 const offset_type *symbol_table;
174
175 /* Size in slots, each slot is 2 offset_types. */
176 offset_type symbol_table_slots;
177
178 /* A pointer to the constant pool. */
179 const char *constant_pool;
180 };
181
182 /* Collection of data recorded per objfile.
183 This hangs off of dwarf2_objfile_data_key. */
184
185 struct dwarf2_per_objfile
186 {
187 struct dwarf2_section_info info;
188 struct dwarf2_section_info abbrev;
189 struct dwarf2_section_info line;
190 struct dwarf2_section_info loc;
191 struct dwarf2_section_info macinfo;
192 struct dwarf2_section_info macro;
193 struct dwarf2_section_info str;
194 struct dwarf2_section_info ranges;
195 struct dwarf2_section_info frame;
196 struct dwarf2_section_info eh_frame;
197 struct dwarf2_section_info gdb_index;
198
199 VEC (dwarf2_section_info_def) *types;
200
201 /* Back link. */
202 struct objfile *objfile;
203
204 /* A list of all the compilation units. This is used to locate
205 the target compilation unit of a particular reference. */
206 struct dwarf2_per_cu_data **all_comp_units;
207
208 /* The number of compilation units in ALL_COMP_UNITS. */
209 int n_comp_units;
210
211 /* The number of .debug_types-related CUs. */
212 int n_type_comp_units;
213
214 /* The .debug_types-related CUs. */
215 struct dwarf2_per_cu_data **type_comp_units;
216
217 /* A chain of compilation units that are currently read in, so that
218 they can be freed later. */
219 struct dwarf2_per_cu_data *read_in_chain;
220
221 /* A table mapping .debug_types signatures to its signatured_type entry.
222 This is NULL if the .debug_types section hasn't been read in yet. */
223 htab_t signatured_types;
224
225 /* A flag indicating wether this objfile has a section loaded at a
226 VMA of 0. */
227 int has_section_at_zero;
228
229 /* True if we are using the mapped index,
230 or we are faking it for OBJF_READNOW's sake. */
231 unsigned char using_index;
232
233 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
234 struct mapped_index *index_table;
235
236 /* When using index_table, this keeps track of all quick_file_names entries.
237 TUs can share line table entries with CUs or other TUs, and there can be
238 a lot more TUs than unique line tables, so we maintain a separate table
239 of all line table entries to support the sharing. */
240 htab_t quick_file_names_table;
241
242 /* Set during partial symbol reading, to prevent queueing of full
243 symbols. */
244 int reading_partial_symbols;
245
246 /* Table mapping type .debug_info DIE offsets to types.
247 This is NULL if not allocated yet.
248 It (currently) makes sense to allocate debug_types_type_hash lazily.
249 To keep things simple we allocate both lazily. */
250 htab_t debug_info_type_hash;
251
252 /* Table mapping type .debug_types DIE offsets to types.
253 This is NULL if not allocated yet. */
254 htab_t debug_types_type_hash;
255 };
256
257 static struct dwarf2_per_objfile *dwarf2_per_objfile;
258
259 /* Default names of the debugging sections. */
260
261 /* Note that if the debugging section has been compressed, it might
262 have a name like .zdebug_info. */
263
264 static const struct dwarf2_debug_sections dwarf2_elf_names =
265 {
266 { ".debug_info", ".zdebug_info" },
267 { ".debug_abbrev", ".zdebug_abbrev" },
268 { ".debug_line", ".zdebug_line" },
269 { ".debug_loc", ".zdebug_loc" },
270 { ".debug_macinfo", ".zdebug_macinfo" },
271 { ".debug_macro", ".zdebug_macro" },
272 { ".debug_str", ".zdebug_str" },
273 { ".debug_ranges", ".zdebug_ranges" },
274 { ".debug_types", ".zdebug_types" },
275 { ".debug_frame", ".zdebug_frame" },
276 { ".eh_frame", NULL },
277 { ".gdb_index", ".zgdb_index" },
278 23
279 };
280
281 /* local data types */
282
283 /* We hold several abbreviation tables in memory at the same time. */
284 #ifndef ABBREV_HASH_SIZE
285 #define ABBREV_HASH_SIZE 121
286 #endif
287
288 /* The data in a compilation unit header, after target2host
289 translation, looks like this. */
290 struct comp_unit_head
291 {
292 unsigned int length;
293 short version;
294 unsigned char addr_size;
295 unsigned char signed_addr_p;
296 unsigned int abbrev_offset;
297
298 /* Size of file offsets; either 4 or 8. */
299 unsigned int offset_size;
300
301 /* Size of the length field; either 4 or 12. */
302 unsigned int initial_length_size;
303
304 /* Offset to the first byte of this compilation unit header in the
305 .debug_info section, for resolving relative reference dies. */
306 unsigned int offset;
307
308 /* Offset to first die in this cu from the start of the cu.
309 This will be the first byte following the compilation unit header. */
310 unsigned int first_die_offset;
311 };
312
313 /* Type used for delaying computation of method physnames.
314 See comments for compute_delayed_physnames. */
315 struct delayed_method_info
316 {
317 /* The type to which the method is attached, i.e., its parent class. */
318 struct type *type;
319
320 /* The index of the method in the type's function fieldlists. */
321 int fnfield_index;
322
323 /* The index of the method in the fieldlist. */
324 int index;
325
326 /* The name of the DIE. */
327 const char *name;
328
329 /* The DIE associated with this method. */
330 struct die_info *die;
331 };
332
333 typedef struct delayed_method_info delayed_method_info;
334 DEF_VEC_O (delayed_method_info);
335
336 /* Internal state when decoding a particular compilation unit. */
337 struct dwarf2_cu
338 {
339 /* The objfile containing this compilation unit. */
340 struct objfile *objfile;
341
342 /* The header of the compilation unit. */
343 struct comp_unit_head header;
344
345 /* Base address of this compilation unit. */
346 CORE_ADDR base_address;
347
348 /* Non-zero if base_address has been set. */
349 int base_known;
350
351 /* The language we are debugging. */
352 enum language language;
353 const struct language_defn *language_defn;
354
355 const char *producer;
356
357 /* The generic symbol table building routines have separate lists for
358 file scope symbols and all all other scopes (local scopes). So
359 we need to select the right one to pass to add_symbol_to_list().
360 We do it by keeping a pointer to the correct list in list_in_scope.
361
362 FIXME: The original dwarf code just treated the file scope as the
363 first local scope, and all other local scopes as nested local
364 scopes, and worked fine. Check to see if we really need to
365 distinguish these in buildsym.c. */
366 struct pending **list_in_scope;
367
368 /* DWARF abbreviation table associated with this compilation unit. */
369 struct abbrev_info **dwarf2_abbrevs;
370
371 /* Storage for the abbrev table. */
372 struct obstack abbrev_obstack;
373
374 /* Hash table holding all the loaded partial DIEs. */
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
393 /* A hash table of die offsets for following references. */
394 htab_t die_hash;
395
396 /* Full DIEs if read in. */
397 struct die_info *dies;
398
399 /* A set of pointers to dwarf2_per_cu_data objects for compilation
400 units referenced by this one. Only set during full symbol processing;
401 partial symbol tables do not have dependencies. */
402 htab_t dependencies;
403
404 /* Header data from the line table, during full symbol processing. */
405 struct line_header *line_header;
406
407 /* A list of methods which need to have physnames computed
408 after all type information has been read. */
409 VEC (delayed_method_info) *method_list;
410
411 /* To be copied to symtab->call_site_htab. */
412 htab_t call_site_htab;
413
414 /* Mark used when releasing cached dies. */
415 unsigned int mark : 1;
416
417 /* This flag will be set if this compilation unit might include
418 inter-compilation-unit references. */
419 unsigned int has_form_ref_addr : 1;
420
421 /* This flag will be set if this compilation unit includes any
422 DW_TAG_namespace DIEs. If we know that there are explicit
423 DIEs for namespaces, we don't need to try to infer them
424 from mangled names. */
425 unsigned int has_namespace_info : 1;
426
427 /* This CU references .debug_loc. See the symtab->locations_valid field.
428 This test is imperfect as there may exist optimized debug code not using
429 any location list and still facing inlining issues if handled as
430 unoptimized code. For a future better test see GCC PR other/32998. */
431 unsigned int has_loclist : 1;
432 };
433
434 /* Persistent data held for a compilation unit, even when not
435 processing it. We put a pointer to this structure in the
436 read_symtab_private field of the psymtab. */
437
438 struct dwarf2_per_cu_data
439 {
440 /* The start offset and length of this compilation unit. 2**29-1
441 bytes should suffice to store the length of any compilation unit
442 - if it doesn't, GDB will fall over anyway.
443 NOTE: Unlike comp_unit_head.length, this length includes
444 initial_length_size. */
445 unsigned int offset;
446 unsigned int length : 29;
447
448 /* Flag indicating this compilation unit will be read in before
449 any of the current compilation units are processed. */
450 unsigned int queued : 1;
451
452 /* This flag will be set if we need to load absolutely all DIEs
453 for this compilation unit, instead of just the ones we think
454 are interesting. It gets set if we look for a DIE in the
455 hash table and don't find it. */
456 unsigned int load_all_dies : 1;
457
458 /* Non-null if this CU is from .debug_types; in which case it points
459 to the section. Otherwise it's from .debug_info. */
460 struct dwarf2_section_info *debug_types_section;
461
462 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
463 of the CU cache it gets reset to NULL again. */
464 struct dwarf2_cu *cu;
465
466 /* The corresponding objfile.
467 Normally we can get the objfile from dwarf2_per_objfile.
468 However we can enter this file with just a "per_cu" handle. */
469 struct objfile *objfile;
470
471 /* When using partial symbol tables, the 'psymtab' field is active.
472 Otherwise the 'quick' field is active. */
473 union
474 {
475 /* The partial symbol table associated with this compilation unit,
476 or NULL for partial units (which do not have an associated
477 symtab). */
478 struct partial_symtab *psymtab;
479
480 /* Data needed by the "quick" functions. */
481 struct dwarf2_per_cu_quick_data *quick;
482 } v;
483 };
484
485 /* Entry in the signatured_types hash table. */
486
487 struct signatured_type
488 {
489 ULONGEST signature;
490
491 /* Offset in .debug_types of the type defined by this TU. */
492 unsigned int type_offset;
493
494 /* The CU(/TU) of this type. */
495 struct dwarf2_per_cu_data per_cu;
496 };
497
498 /* Struct used to pass misc. parameters to read_die_and_children, et
499 al. which are used for both .debug_info and .debug_types dies.
500 All parameters here are unchanging for the life of the call. This
501 struct exists to abstract away the constant parameters of die
502 reading. */
503
504 struct die_reader_specs
505 {
506 /* The bfd of this objfile. */
507 bfd* abfd;
508
509 /* The CU of the DIE we are parsing. */
510 struct dwarf2_cu *cu;
511
512 /* Pointer to start of section buffer.
513 This is either the start of .debug_info or .debug_types. */
514 const gdb_byte *buffer;
515 };
516
517 /* The line number information for a compilation unit (found in the
518 .debug_line section) begins with a "statement program header",
519 which contains the following information. */
520 struct line_header
521 {
522 unsigned int total_length;
523 unsigned short version;
524 unsigned int header_length;
525 unsigned char minimum_instruction_length;
526 unsigned char maximum_ops_per_instruction;
527 unsigned char default_is_stmt;
528 int line_base;
529 unsigned char line_range;
530 unsigned char opcode_base;
531
532 /* standard_opcode_lengths[i] is the number of operands for the
533 standard opcode whose value is i. This means that
534 standard_opcode_lengths[0] is unused, and the last meaningful
535 element is standard_opcode_lengths[opcode_base - 1]. */
536 unsigned char *standard_opcode_lengths;
537
538 /* The include_directories table. NOTE! These strings are not
539 allocated with xmalloc; instead, they are pointers into
540 debug_line_buffer. If you try to free them, `free' will get
541 indigestion. */
542 unsigned int num_include_dirs, include_dirs_size;
543 char **include_dirs;
544
545 /* The file_names table. NOTE! These strings are not allocated
546 with xmalloc; instead, they are pointers into debug_line_buffer.
547 Don't try to free them directly. */
548 unsigned int num_file_names, file_names_size;
549 struct file_entry
550 {
551 char *name;
552 unsigned int dir_index;
553 unsigned int mod_time;
554 unsigned int length;
555 int included_p; /* Non-zero if referenced by the Line Number Program. */
556 struct symtab *symtab; /* The associated symbol table, if any. */
557 } *file_names;
558
559 /* The start and end of the statement program following this
560 header. These point into dwarf2_per_objfile->line_buffer. */
561 gdb_byte *statement_program_start, *statement_program_end;
562 };
563
564 /* When we construct a partial symbol table entry we only
565 need this much information. */
566 struct partial_die_info
567 {
568 /* Offset of this DIE. */
569 unsigned int offset;
570
571 /* DWARF-2 tag for this DIE. */
572 ENUM_BITFIELD(dwarf_tag) tag : 16;
573
574 /* Assorted flags describing the data found in this DIE. */
575 unsigned int has_children : 1;
576 unsigned int is_external : 1;
577 unsigned int is_declaration : 1;
578 unsigned int has_type : 1;
579 unsigned int has_specification : 1;
580 unsigned int has_pc_info : 1;
581
582 /* Flag set if the SCOPE field of this structure has been
583 computed. */
584 unsigned int scope_set : 1;
585
586 /* Flag set if the DIE has a byte_size attribute. */
587 unsigned int has_byte_size : 1;
588
589 /* Flag set if any of the DIE's children are template arguments. */
590 unsigned int has_template_arguments : 1;
591
592 /* Flag set if fixup_partial_die has been called on this die. */
593 unsigned int fixup_called : 1;
594
595 /* The name of this DIE. Normally the value of DW_AT_name, but
596 sometimes a default name for unnamed DIEs. */
597 char *name;
598
599 /* The linkage name, if present. */
600 const char *linkage_name;
601
602 /* The scope to prepend to our children. This is generally
603 allocated on the comp_unit_obstack, so will disappear
604 when this compilation unit leaves the cache. */
605 char *scope;
606
607 /* The location description associated with this DIE, if any. */
608 struct dwarf_block *locdesc;
609
610 /* If HAS_PC_INFO, the PC range associated with this DIE. */
611 CORE_ADDR lowpc;
612 CORE_ADDR highpc;
613
614 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
615 DW_AT_sibling, if any. */
616 /* NOTE: This member isn't strictly necessary, read_partial_die could
617 return DW_AT_sibling values to its caller load_partial_dies. */
618 gdb_byte *sibling;
619
620 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
621 DW_AT_specification (or DW_AT_abstract_origin or
622 DW_AT_extension). */
623 unsigned int spec_offset;
624
625 /* Pointers to this DIE's parent, first child, and next sibling,
626 if any. */
627 struct partial_die_info *die_parent, *die_child, *die_sibling;
628 };
629
630 /* This data structure holds the information of an abbrev. */
631 struct abbrev_info
632 {
633 unsigned int number; /* number identifying abbrev */
634 enum dwarf_tag tag; /* dwarf tag */
635 unsigned short has_children; /* boolean */
636 unsigned short num_attrs; /* number of attributes */
637 struct attr_abbrev *attrs; /* an array of attribute descriptions */
638 struct abbrev_info *next; /* next in chain */
639 };
640
641 struct attr_abbrev
642 {
643 ENUM_BITFIELD(dwarf_attribute) name : 16;
644 ENUM_BITFIELD(dwarf_form) form : 16;
645 };
646
647 /* Attributes have a name and a value. */
648 struct attribute
649 {
650 ENUM_BITFIELD(dwarf_attribute) name : 16;
651 ENUM_BITFIELD(dwarf_form) form : 15;
652
653 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
654 field should be in u.str (existing only for DW_STRING) but it is kept
655 here for better struct attribute alignment. */
656 unsigned int string_is_canonical : 1;
657
658 union
659 {
660 char *str;
661 struct dwarf_block *blk;
662 ULONGEST unsnd;
663 LONGEST snd;
664 CORE_ADDR addr;
665 struct signatured_type *signatured_type;
666 }
667 u;
668 };
669
670 /* This data structure holds a complete die structure. */
671 struct die_info
672 {
673 /* DWARF-2 tag for this DIE. */
674 ENUM_BITFIELD(dwarf_tag) tag : 16;
675
676 /* Number of attributes */
677 unsigned char num_attrs;
678
679 /* True if we're presently building the full type name for the
680 type derived from this DIE. */
681 unsigned char building_fullname : 1;
682
683 /* Abbrev number */
684 unsigned int abbrev;
685
686 /* Offset in .debug_info or .debug_types section. */
687 unsigned int offset;
688
689 /* The dies in a compilation unit form an n-ary tree. PARENT
690 points to this die's parent; CHILD points to the first child of
691 this node; and all the children of a given node are chained
692 together via their SIBLING fields. */
693 struct die_info *child; /* Its first child, if any. */
694 struct die_info *sibling; /* Its next sibling, if any. */
695 struct die_info *parent; /* Its parent, if any. */
696
697 /* An array of attributes, with NUM_ATTRS elements. There may be
698 zero, but it's not common and zero-sized arrays are not
699 sufficiently portable C. */
700 struct attribute attrs[1];
701 };
702
703 /* Get at parts of an attribute structure. */
704
705 #define DW_STRING(attr) ((attr)->u.str)
706 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
707 #define DW_UNSND(attr) ((attr)->u.unsnd)
708 #define DW_BLOCK(attr) ((attr)->u.blk)
709 #define DW_SND(attr) ((attr)->u.snd)
710 #define DW_ADDR(attr) ((attr)->u.addr)
711 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
712
713 /* Blocks are a bunch of untyped bytes. */
714 struct dwarf_block
715 {
716 unsigned int size;
717
718 /* Valid only if SIZE is not zero. */
719 gdb_byte *data;
720 };
721
722 #ifndef ATTR_ALLOC_CHUNK
723 #define ATTR_ALLOC_CHUNK 4
724 #endif
725
726 /* Allocate fields for structs, unions and enums in this size. */
727 #ifndef DW_FIELD_ALLOC_CHUNK
728 #define DW_FIELD_ALLOC_CHUNK 4
729 #endif
730
731 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
732 but this would require a corresponding change in unpack_field_as_long
733 and friends. */
734 static int bits_per_byte = 8;
735
736 /* The routines that read and process dies for a C struct or C++ class
737 pass lists of data member fields and lists of member function fields
738 in an instance of a field_info structure, as defined below. */
739 struct field_info
740 {
741 /* List of data member and baseclasses fields. */
742 struct nextfield
743 {
744 struct nextfield *next;
745 int accessibility;
746 int virtuality;
747 struct field field;
748 }
749 *fields, *baseclasses;
750
751 /* Number of fields (including baseclasses). */
752 int nfields;
753
754 /* Number of baseclasses. */
755 int nbaseclasses;
756
757 /* Set if the accesibility of one of the fields is not public. */
758 int non_public_fields;
759
760 /* Member function fields array, entries are allocated in the order they
761 are encountered in the object file. */
762 struct nextfnfield
763 {
764 struct nextfnfield *next;
765 struct fn_field fnfield;
766 }
767 *fnfields;
768
769 /* Member function fieldlist array, contains name of possibly overloaded
770 member function, number of overloaded member functions and a pointer
771 to the head of the member function field chain. */
772 struct fnfieldlist
773 {
774 char *name;
775 int length;
776 struct nextfnfield *head;
777 }
778 *fnfieldlists;
779
780 /* Number of entries in the fnfieldlists array. */
781 int nfnfields;
782
783 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
784 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
785 struct typedef_field_list
786 {
787 struct typedef_field field;
788 struct typedef_field_list *next;
789 }
790 *typedef_field_list;
791 unsigned typedef_field_list_count;
792 };
793
794 /* One item on the queue of compilation units to read in full symbols
795 for. */
796 struct dwarf2_queue_item
797 {
798 struct dwarf2_per_cu_data *per_cu;
799 struct dwarf2_queue_item *next;
800 };
801
802 /* The current queue. */
803 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
804
805 /* Loaded secondary compilation units are kept in memory until they
806 have not been referenced for the processing of this many
807 compilation units. Set this to zero to disable caching. Cache
808 sizes of up to at least twenty will improve startup time for
809 typical inter-CU-reference binaries, at an obvious memory cost. */
810 static int dwarf2_max_cache_age = 5;
811 static void
812 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
813 struct cmd_list_element *c, const char *value)
814 {
815 fprintf_filtered (file, _("The upper bound on the age of cached "
816 "dwarf2 compilation units is %s.\n"),
817 value);
818 }
819
820
821 /* Various complaints about symbol reading that don't abort the process. */
822
823 static void
824 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
825 {
826 complaint (&symfile_complaints,
827 _("statement list doesn't fit in .debug_line section"));
828 }
829
830 static void
831 dwarf2_debug_line_missing_file_complaint (void)
832 {
833 complaint (&symfile_complaints,
834 _(".debug_line section has line data without a file"));
835 }
836
837 static void
838 dwarf2_debug_line_missing_end_sequence_complaint (void)
839 {
840 complaint (&symfile_complaints,
841 _(".debug_line section has line "
842 "program sequence without an end"));
843 }
844
845 static void
846 dwarf2_complex_location_expr_complaint (void)
847 {
848 complaint (&symfile_complaints, _("location expression too complex"));
849 }
850
851 static void
852 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
853 int arg3)
854 {
855 complaint (&symfile_complaints,
856 _("const value length mismatch for '%s', got %d, expected %d"),
857 arg1, arg2, arg3);
858 }
859
860 static void
861 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
862 {
863 complaint (&symfile_complaints,
864 _("macro info runs off end of `%s' section"),
865 section->asection->name);
866 }
867
868 static void
869 dwarf2_macro_malformed_definition_complaint (const char *arg1)
870 {
871 complaint (&symfile_complaints,
872 _("macro debug info contains a "
873 "malformed macro definition:\n`%s'"),
874 arg1);
875 }
876
877 static void
878 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
879 {
880 complaint (&symfile_complaints,
881 _("invalid attribute class or form for '%s' in '%s'"),
882 arg1, arg2);
883 }
884
885 /* local function prototypes */
886
887 static void dwarf2_locate_sections (bfd *, asection *, void *);
888
889 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
890 struct objfile *);
891
892 static void dwarf2_find_base_address (struct die_info *die,
893 struct dwarf2_cu *cu);
894
895 static void dwarf2_build_psymtabs_hard (struct objfile *);
896
897 static void scan_partial_symbols (struct partial_die_info *,
898 CORE_ADDR *, CORE_ADDR *,
899 int, struct dwarf2_cu *);
900
901 static void add_partial_symbol (struct partial_die_info *,
902 struct dwarf2_cu *);
903
904 static void add_partial_namespace (struct partial_die_info *pdi,
905 CORE_ADDR *lowpc, CORE_ADDR *highpc,
906 int need_pc, struct dwarf2_cu *cu);
907
908 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
909 CORE_ADDR *highpc, int need_pc,
910 struct dwarf2_cu *cu);
911
912 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
913 struct dwarf2_cu *cu);
914
915 static void add_partial_subprogram (struct partial_die_info *pdi,
916 CORE_ADDR *lowpc, CORE_ADDR *highpc,
917 int need_pc, struct dwarf2_cu *cu);
918
919 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
920 gdb_byte *buffer, gdb_byte *info_ptr,
921 bfd *abfd, struct dwarf2_cu *cu);
922
923 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
924
925 static void psymtab_to_symtab_1 (struct partial_symtab *);
926
927 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
928
929 static void dwarf2_free_abbrev_table (void *);
930
931 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
932
933 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
934 struct dwarf2_cu *);
935
936 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
937 struct dwarf2_cu *);
938
939 static struct partial_die_info *load_partial_dies (bfd *,
940 gdb_byte *, gdb_byte *,
941 int, struct dwarf2_cu *);
942
943 static gdb_byte *read_partial_die (struct partial_die_info *,
944 struct abbrev_info *abbrev,
945 unsigned int, bfd *,
946 gdb_byte *, gdb_byte *,
947 struct dwarf2_cu *);
948
949 static struct partial_die_info *find_partial_die (unsigned int,
950 struct dwarf2_cu *);
951
952 static void fixup_partial_die (struct partial_die_info *,
953 struct dwarf2_cu *);
954
955 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
956 bfd *, gdb_byte *, struct dwarf2_cu *);
957
958 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
959 bfd *, gdb_byte *, struct dwarf2_cu *);
960
961 static unsigned int read_1_byte (bfd *, gdb_byte *);
962
963 static int read_1_signed_byte (bfd *, gdb_byte *);
964
965 static unsigned int read_2_bytes (bfd *, gdb_byte *);
966
967 static unsigned int read_4_bytes (bfd *, gdb_byte *);
968
969 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
970
971 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
972 unsigned int *);
973
974 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
975
976 static LONGEST read_checked_initial_length_and_offset
977 (bfd *, gdb_byte *, const struct comp_unit_head *,
978 unsigned int *, unsigned int *);
979
980 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
981 unsigned int *);
982
983 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
984
985 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
986
987 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
988
989 static char *read_indirect_string (bfd *, gdb_byte *,
990 const struct comp_unit_head *,
991 unsigned int *);
992
993 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
994
995 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
996
997 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
998
999 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1000
1001 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1002 struct dwarf2_cu *);
1003
1004 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1005 unsigned int,
1006 struct dwarf2_cu *);
1007
1008 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1009 struct dwarf2_cu *cu);
1010
1011 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1012
1013 static struct die_info *die_specification (struct die_info *die,
1014 struct dwarf2_cu **);
1015
1016 static void free_line_header (struct line_header *lh);
1017
1018 static void add_file_name (struct line_header *, char *, unsigned int,
1019 unsigned int, unsigned int);
1020
1021 static struct line_header *(dwarf_decode_line_header
1022 (unsigned int offset,
1023 bfd *abfd, struct dwarf2_cu *cu));
1024
1025 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1026 struct dwarf2_cu *, struct partial_symtab *);
1027
1028 static void dwarf2_start_subfile (char *, const char *, const char *);
1029
1030 static struct symbol *new_symbol (struct die_info *, struct type *,
1031 struct dwarf2_cu *);
1032
1033 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1034 struct dwarf2_cu *, struct symbol *);
1035
1036 static void dwarf2_const_value (struct attribute *, struct symbol *,
1037 struct dwarf2_cu *);
1038
1039 static void dwarf2_const_value_attr (struct attribute *attr,
1040 struct type *type,
1041 const char *name,
1042 struct obstack *obstack,
1043 struct dwarf2_cu *cu, long *value,
1044 gdb_byte **bytes,
1045 struct dwarf2_locexpr_baton **baton);
1046
1047 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1048
1049 static int need_gnat_info (struct dwarf2_cu *);
1050
1051 static struct type *die_descriptive_type (struct die_info *,
1052 struct dwarf2_cu *);
1053
1054 static void set_descriptive_type (struct type *, struct die_info *,
1055 struct dwarf2_cu *);
1056
1057 static struct type *die_containing_type (struct die_info *,
1058 struct dwarf2_cu *);
1059
1060 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1061 struct dwarf2_cu *);
1062
1063 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1064
1065 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1066
1067 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1068
1069 static char *typename_concat (struct obstack *obs, const char *prefix,
1070 const char *suffix, int physname,
1071 struct dwarf2_cu *cu);
1072
1073 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1074
1075 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1076
1077 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1078
1079 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1080
1081 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1082
1083 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1084 struct dwarf2_cu *, struct partial_symtab *);
1085
1086 static int dwarf2_get_pc_bounds (struct die_info *,
1087 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1088 struct partial_symtab *);
1089
1090 static void get_scope_pc_bounds (struct die_info *,
1091 CORE_ADDR *, CORE_ADDR *,
1092 struct dwarf2_cu *);
1093
1094 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1095 CORE_ADDR, struct dwarf2_cu *);
1096
1097 static void dwarf2_add_field (struct field_info *, struct die_info *,
1098 struct dwarf2_cu *);
1099
1100 static void dwarf2_attach_fields_to_type (struct field_info *,
1101 struct type *, struct dwarf2_cu *);
1102
1103 static void dwarf2_add_member_fn (struct field_info *,
1104 struct die_info *, struct type *,
1105 struct dwarf2_cu *);
1106
1107 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1108 struct type *,
1109 struct dwarf2_cu *);
1110
1111 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1112
1113 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1114
1115 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1116
1117 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1118
1119 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1120
1121 static struct type *read_module_type (struct die_info *die,
1122 struct dwarf2_cu *cu);
1123
1124 static const char *namespace_name (struct die_info *die,
1125 int *is_anonymous, struct dwarf2_cu *);
1126
1127 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1128
1129 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1130
1131 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1132 struct dwarf2_cu *);
1133
1134 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1135
1136 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1137 gdb_byte *info_ptr,
1138 gdb_byte **new_info_ptr,
1139 struct die_info *parent);
1140
1141 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1142 gdb_byte *info_ptr,
1143 gdb_byte **new_info_ptr,
1144 struct die_info *parent);
1145
1146 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1147 gdb_byte *info_ptr,
1148 gdb_byte **new_info_ptr,
1149 struct die_info *parent);
1150
1151 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1152 struct die_info **, gdb_byte *,
1153 int *);
1154
1155 static void process_die (struct die_info *, struct dwarf2_cu *);
1156
1157 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1158 struct obstack *);
1159
1160 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1161
1162 static const char *dwarf2_full_name (char *name,
1163 struct die_info *die,
1164 struct dwarf2_cu *cu);
1165
1166 static struct die_info *dwarf2_extension (struct die_info *die,
1167 struct dwarf2_cu **);
1168
1169 static char *dwarf_tag_name (unsigned int);
1170
1171 static char *dwarf_attr_name (unsigned int);
1172
1173 static char *dwarf_form_name (unsigned int);
1174
1175 static char *dwarf_bool_name (unsigned int);
1176
1177 static char *dwarf_type_encoding_name (unsigned int);
1178
1179 #if 0
1180 static char *dwarf_cfi_name (unsigned int);
1181 #endif
1182
1183 static struct die_info *sibling_die (struct die_info *);
1184
1185 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1186
1187 static void dump_die_for_error (struct die_info *);
1188
1189 static void dump_die_1 (struct ui_file *, int level, int max_level,
1190 struct die_info *);
1191
1192 /*static*/ void dump_die (struct die_info *, int max_level);
1193
1194 static void store_in_ref_table (struct die_info *,
1195 struct dwarf2_cu *);
1196
1197 static int is_ref_attr (struct attribute *);
1198
1199 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1200
1201 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1202
1203 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1204 struct attribute *,
1205 struct dwarf2_cu **);
1206
1207 static struct die_info *follow_die_ref (struct die_info *,
1208 struct attribute *,
1209 struct dwarf2_cu **);
1210
1211 static struct die_info *follow_die_sig (struct die_info *,
1212 struct attribute *,
1213 struct dwarf2_cu **);
1214
1215 static struct signatured_type *lookup_signatured_type_at_offset
1216 (struct objfile *objfile,
1217 struct dwarf2_section_info *section,
1218 unsigned int offset);
1219
1220 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1221
1222 static void read_signatured_type (struct signatured_type *type_sig);
1223
1224 /* memory allocation interface */
1225
1226 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1227
1228 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1229
1230 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1231
1232 static void dwarf_decode_macros (struct line_header *, unsigned int,
1233 char *, bfd *, struct dwarf2_cu *,
1234 struct dwarf2_section_info *,
1235 int);
1236
1237 static int attr_form_is_block (struct attribute *);
1238
1239 static int attr_form_is_section_offset (struct attribute *);
1240
1241 static int attr_form_is_constant (struct attribute *);
1242
1243 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1244 struct dwarf2_loclist_baton *baton,
1245 struct attribute *attr);
1246
1247 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1248 struct symbol *sym,
1249 struct dwarf2_cu *cu);
1250
1251 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1252 struct abbrev_info *abbrev,
1253 struct dwarf2_cu *cu);
1254
1255 static void free_stack_comp_unit (void *);
1256
1257 static hashval_t partial_die_hash (const void *item);
1258
1259 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1260
1261 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1262 (unsigned int offset, struct objfile *objfile);
1263
1264 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1265 (unsigned int offset, struct objfile *objfile);
1266
1267 static void init_one_comp_unit (struct dwarf2_cu *cu,
1268 struct dwarf2_per_cu_data *per_cu);
1269
1270 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1271 struct die_info *comp_unit_die);
1272
1273 static void free_heap_comp_unit (void *);
1274
1275 static void free_cached_comp_units (void *);
1276
1277 static void age_cached_comp_units (void);
1278
1279 static void free_one_cached_comp_unit (void *);
1280
1281 static struct type *set_die_type (struct die_info *, struct type *,
1282 struct dwarf2_cu *);
1283
1284 static void create_all_comp_units (struct objfile *);
1285
1286 static int create_debug_types_hash_table (struct objfile *objfile);
1287
1288 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1289
1290 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1291
1292 static void dwarf2_add_dependence (struct dwarf2_cu *,
1293 struct dwarf2_per_cu_data *);
1294
1295 static void dwarf2_mark (struct dwarf2_cu *);
1296
1297 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1298
1299 static struct type *get_die_type_at_offset (unsigned int,
1300 struct dwarf2_per_cu_data *per_cu);
1301
1302 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1303
1304 static void dwarf2_release_queue (void *dummy);
1305
1306 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1307
1308 static void process_queue (void);
1309
1310 static void find_file_and_directory (struct die_info *die,
1311 struct dwarf2_cu *cu,
1312 char **name, char **comp_dir);
1313
1314 static char *file_full_name (int file, struct line_header *lh,
1315 const char *comp_dir);
1316
1317 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1318 gdb_byte *info_ptr,
1319 gdb_byte *buffer,
1320 unsigned int buffer_size,
1321 bfd *abfd,
1322 int is_debug_types_section);
1323
1324 static void init_cu_die_reader (struct die_reader_specs *reader,
1325 struct dwarf2_cu *cu);
1326
1327 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1328
1329 #if WORDS_BIGENDIAN
1330
1331 /* Convert VALUE between big- and little-endian. */
1332 static offset_type
1333 byte_swap (offset_type value)
1334 {
1335 offset_type result;
1336
1337 result = (value & 0xff) << 24;
1338 result |= (value & 0xff00) << 8;
1339 result |= (value & 0xff0000) >> 8;
1340 result |= (value & 0xff000000) >> 24;
1341 return result;
1342 }
1343
1344 #define MAYBE_SWAP(V) byte_swap (V)
1345
1346 #else
1347 #define MAYBE_SWAP(V) (V)
1348 #endif /* WORDS_BIGENDIAN */
1349
1350 /* The suffix for an index file. */
1351 #define INDEX_SUFFIX ".gdb-index"
1352
1353 static const char *dwarf2_physname (char *name, struct die_info *die,
1354 struct dwarf2_cu *cu);
1355
1356 /* Try to locate the sections we need for DWARF 2 debugging
1357 information and return true if we have enough to do something.
1358 NAMES points to the dwarf2 section names, or is NULL if the standard
1359 ELF names are used. */
1360
1361 int
1362 dwarf2_has_info (struct objfile *objfile,
1363 const struct dwarf2_debug_sections *names)
1364 {
1365 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1366 if (!dwarf2_per_objfile)
1367 {
1368 /* Initialize per-objfile state. */
1369 struct dwarf2_per_objfile *data
1370 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1371
1372 memset (data, 0, sizeof (*data));
1373 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1374 dwarf2_per_objfile = data;
1375
1376 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1377 (void *) names);
1378 dwarf2_per_objfile->objfile = objfile;
1379 }
1380 return (dwarf2_per_objfile->info.asection != NULL
1381 && dwarf2_per_objfile->abbrev.asection != NULL);
1382 }
1383
1384 /* When loading sections, we look either for uncompressed section or for
1385 compressed section names. */
1386
1387 static int
1388 section_is_p (const char *section_name,
1389 const struct dwarf2_section_names *names)
1390 {
1391 if (names->normal != NULL
1392 && strcmp (section_name, names->normal) == 0)
1393 return 1;
1394 if (names->compressed != NULL
1395 && strcmp (section_name, names->compressed) == 0)
1396 return 1;
1397 return 0;
1398 }
1399
1400 /* This function is mapped across the sections and remembers the
1401 offset and size of each of the debugging sections we are interested
1402 in. */
1403
1404 static void
1405 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1406 {
1407 const struct dwarf2_debug_sections *names;
1408
1409 if (vnames == NULL)
1410 names = &dwarf2_elf_names;
1411 else
1412 names = (const struct dwarf2_debug_sections *) vnames;
1413
1414 if (section_is_p (sectp->name, &names->info))
1415 {
1416 dwarf2_per_objfile->info.asection = sectp;
1417 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1418 }
1419 else if (section_is_p (sectp->name, &names->abbrev))
1420 {
1421 dwarf2_per_objfile->abbrev.asection = sectp;
1422 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1423 }
1424 else if (section_is_p (sectp->name, &names->line))
1425 {
1426 dwarf2_per_objfile->line.asection = sectp;
1427 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1428 }
1429 else if (section_is_p (sectp->name, &names->loc))
1430 {
1431 dwarf2_per_objfile->loc.asection = sectp;
1432 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1433 }
1434 else if (section_is_p (sectp->name, &names->macinfo))
1435 {
1436 dwarf2_per_objfile->macinfo.asection = sectp;
1437 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1438 }
1439 else if (section_is_p (sectp->name, &names->macro))
1440 {
1441 dwarf2_per_objfile->macro.asection = sectp;
1442 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1443 }
1444 else if (section_is_p (sectp->name, &names->str))
1445 {
1446 dwarf2_per_objfile->str.asection = sectp;
1447 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1448 }
1449 else if (section_is_p (sectp->name, &names->frame))
1450 {
1451 dwarf2_per_objfile->frame.asection = sectp;
1452 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1453 }
1454 else if (section_is_p (sectp->name, &names->eh_frame))
1455 {
1456 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1457
1458 if (aflag & SEC_HAS_CONTENTS)
1459 {
1460 dwarf2_per_objfile->eh_frame.asection = sectp;
1461 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1462 }
1463 }
1464 else if (section_is_p (sectp->name, &names->ranges))
1465 {
1466 dwarf2_per_objfile->ranges.asection = sectp;
1467 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1468 }
1469 else if (section_is_p (sectp->name, &names->types))
1470 {
1471 struct dwarf2_section_info type_section;
1472
1473 memset (&type_section, 0, sizeof (type_section));
1474 type_section.asection = sectp;
1475 type_section.size = bfd_get_section_size (sectp);
1476
1477 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1478 &type_section);
1479 }
1480 else if (section_is_p (sectp->name, &names->gdb_index))
1481 {
1482 dwarf2_per_objfile->gdb_index.asection = sectp;
1483 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1484 }
1485
1486 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1487 && bfd_section_vma (abfd, sectp) == 0)
1488 dwarf2_per_objfile->has_section_at_zero = 1;
1489 }
1490
1491 /* Decompress a section that was compressed using zlib. Store the
1492 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1493
1494 static void
1495 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1496 gdb_byte **outbuf, bfd_size_type *outsize)
1497 {
1498 bfd *abfd = objfile->obfd;
1499 #ifndef HAVE_ZLIB_H
1500 error (_("Support for zlib-compressed DWARF data (from '%s') "
1501 "is disabled in this copy of GDB"),
1502 bfd_get_filename (abfd));
1503 #else
1504 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1505 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1506 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1507 bfd_size_type uncompressed_size;
1508 gdb_byte *uncompressed_buffer;
1509 z_stream strm;
1510 int rc;
1511 int header_size = 12;
1512
1513 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1514 || bfd_bread (compressed_buffer,
1515 compressed_size, abfd) != compressed_size)
1516 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1517 bfd_get_filename (abfd));
1518
1519 /* Read the zlib header. In this case, it should be "ZLIB" followed
1520 by the uncompressed section size, 8 bytes in big-endian order. */
1521 if (compressed_size < header_size
1522 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1523 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1524 bfd_get_filename (abfd));
1525 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1526 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1527 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1528 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1529 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1530 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1531 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1532 uncompressed_size += compressed_buffer[11];
1533
1534 /* It is possible the section consists of several compressed
1535 buffers concatenated together, so we uncompress in a loop. */
1536 strm.zalloc = NULL;
1537 strm.zfree = NULL;
1538 strm.opaque = NULL;
1539 strm.avail_in = compressed_size - header_size;
1540 strm.next_in = (Bytef*) compressed_buffer + header_size;
1541 strm.avail_out = uncompressed_size;
1542 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1543 uncompressed_size);
1544 rc = inflateInit (&strm);
1545 while (strm.avail_in > 0)
1546 {
1547 if (rc != Z_OK)
1548 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1549 bfd_get_filename (abfd), rc);
1550 strm.next_out = ((Bytef*) uncompressed_buffer
1551 + (uncompressed_size - strm.avail_out));
1552 rc = inflate (&strm, Z_FINISH);
1553 if (rc != Z_STREAM_END)
1554 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1555 bfd_get_filename (abfd), rc);
1556 rc = inflateReset (&strm);
1557 }
1558 rc = inflateEnd (&strm);
1559 if (rc != Z_OK
1560 || strm.avail_out != 0)
1561 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1562 bfd_get_filename (abfd), rc);
1563
1564 do_cleanups (cleanup);
1565 *outbuf = uncompressed_buffer;
1566 *outsize = uncompressed_size;
1567 #endif
1568 }
1569
1570 /* A helper function that decides whether a section is empty. */
1571
1572 static int
1573 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1574 {
1575 return info->asection == NULL || info->size == 0;
1576 }
1577
1578 /* Read the contents of the section INFO from object file specified by
1579 OBJFILE, store info about the section into INFO.
1580 If the section is compressed, uncompress it before returning. */
1581
1582 static void
1583 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1584 {
1585 bfd *abfd = objfile->obfd;
1586 asection *sectp = info->asection;
1587 gdb_byte *buf, *retbuf;
1588 unsigned char header[4];
1589
1590 if (info->readin)
1591 return;
1592 info->buffer = NULL;
1593 info->map_addr = NULL;
1594 info->readin = 1;
1595
1596 if (dwarf2_section_empty_p (info))
1597 return;
1598
1599 /* Check if the file has a 4-byte header indicating compression. */
1600 if (info->size > sizeof (header)
1601 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1602 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1603 {
1604 /* Upon decompression, update the buffer and its size. */
1605 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1606 {
1607 zlib_decompress_section (objfile, sectp, &info->buffer,
1608 &info->size);
1609 return;
1610 }
1611 }
1612
1613 #ifdef HAVE_MMAP
1614 if (pagesize == 0)
1615 pagesize = getpagesize ();
1616
1617 /* Only try to mmap sections which are large enough: we don't want to
1618 waste space due to fragmentation. Also, only try mmap for sections
1619 without relocations. */
1620
1621 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1622 {
1623 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1624 MAP_PRIVATE, sectp->filepos,
1625 &info->map_addr, &info->map_len);
1626
1627 if ((caddr_t)info->buffer != MAP_FAILED)
1628 {
1629 #if HAVE_POSIX_MADVISE
1630 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1631 #endif
1632 return;
1633 }
1634 }
1635 #endif
1636
1637 /* If we get here, we are a normal, not-compressed section. */
1638 info->buffer = buf
1639 = obstack_alloc (&objfile->objfile_obstack, info->size);
1640
1641 /* When debugging .o files, we may need to apply relocations; see
1642 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1643 We never compress sections in .o files, so we only need to
1644 try this when the section is not compressed. */
1645 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1646 if (retbuf != NULL)
1647 {
1648 info->buffer = retbuf;
1649 return;
1650 }
1651
1652 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1653 || bfd_bread (buf, info->size, abfd) != info->size)
1654 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1655 bfd_get_filename (abfd));
1656 }
1657
1658 /* A helper function that returns the size of a section in a safe way.
1659 If you are positive that the section has been read before using the
1660 size, then it is safe to refer to the dwarf2_section_info object's
1661 "size" field directly. In other cases, you must call this
1662 function, because for compressed sections the size field is not set
1663 correctly until the section has been read. */
1664
1665 static bfd_size_type
1666 dwarf2_section_size (struct objfile *objfile,
1667 struct dwarf2_section_info *info)
1668 {
1669 if (!info->readin)
1670 dwarf2_read_section (objfile, info);
1671 return info->size;
1672 }
1673
1674 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1675 SECTION_NAME. */
1676
1677 void
1678 dwarf2_get_section_info (struct objfile *objfile,
1679 enum dwarf2_section_enum sect,
1680 asection **sectp, gdb_byte **bufp,
1681 bfd_size_type *sizep)
1682 {
1683 struct dwarf2_per_objfile *data
1684 = objfile_data (objfile, dwarf2_objfile_data_key);
1685 struct dwarf2_section_info *info;
1686
1687 /* We may see an objfile without any DWARF, in which case we just
1688 return nothing. */
1689 if (data == NULL)
1690 {
1691 *sectp = NULL;
1692 *bufp = NULL;
1693 *sizep = 0;
1694 return;
1695 }
1696 switch (sect)
1697 {
1698 case DWARF2_DEBUG_FRAME:
1699 info = &data->frame;
1700 break;
1701 case DWARF2_EH_FRAME:
1702 info = &data->eh_frame;
1703 break;
1704 default:
1705 gdb_assert_not_reached ("unexpected section");
1706 }
1707
1708 dwarf2_read_section (objfile, info);
1709
1710 *sectp = info->asection;
1711 *bufp = info->buffer;
1712 *sizep = info->size;
1713 }
1714
1715 \f
1716 /* DWARF quick_symbols_functions support. */
1717
1718 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1719 unique line tables, so we maintain a separate table of all .debug_line
1720 derived entries to support the sharing.
1721 All the quick functions need is the list of file names. We discard the
1722 line_header when we're done and don't need to record it here. */
1723 struct quick_file_names
1724 {
1725 /* The offset in .debug_line of the line table. We hash on this. */
1726 unsigned int offset;
1727
1728 /* The number of entries in file_names, real_names. */
1729 unsigned int num_file_names;
1730
1731 /* The file names from the line table, after being run through
1732 file_full_name. */
1733 const char **file_names;
1734
1735 /* The file names from the line table after being run through
1736 gdb_realpath. These are computed lazily. */
1737 const char **real_names;
1738 };
1739
1740 /* When using the index (and thus not using psymtabs), each CU has an
1741 object of this type. This is used to hold information needed by
1742 the various "quick" methods. */
1743 struct dwarf2_per_cu_quick_data
1744 {
1745 /* The file table. This can be NULL if there was no file table
1746 or it's currently not read in.
1747 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1748 struct quick_file_names *file_names;
1749
1750 /* The corresponding symbol table. This is NULL if symbols for this
1751 CU have not yet been read. */
1752 struct symtab *symtab;
1753
1754 /* A temporary mark bit used when iterating over all CUs in
1755 expand_symtabs_matching. */
1756 unsigned int mark : 1;
1757
1758 /* True if we've tried to read the file table and found there isn't one.
1759 There will be no point in trying to read it again next time. */
1760 unsigned int no_file_data : 1;
1761 };
1762
1763 /* Hash function for a quick_file_names. */
1764
1765 static hashval_t
1766 hash_file_name_entry (const void *e)
1767 {
1768 const struct quick_file_names *file_data = e;
1769
1770 return file_data->offset;
1771 }
1772
1773 /* Equality function for a quick_file_names. */
1774
1775 static int
1776 eq_file_name_entry (const void *a, const void *b)
1777 {
1778 const struct quick_file_names *ea = a;
1779 const struct quick_file_names *eb = b;
1780
1781 return ea->offset == eb->offset;
1782 }
1783
1784 /* Delete function for a quick_file_names. */
1785
1786 static void
1787 delete_file_name_entry (void *e)
1788 {
1789 struct quick_file_names *file_data = e;
1790 int i;
1791
1792 for (i = 0; i < file_data->num_file_names; ++i)
1793 {
1794 xfree ((void*) file_data->file_names[i]);
1795 if (file_data->real_names)
1796 xfree ((void*) file_data->real_names[i]);
1797 }
1798
1799 /* The space for the struct itself lives on objfile_obstack,
1800 so we don't free it here. */
1801 }
1802
1803 /* Create a quick_file_names hash table. */
1804
1805 static htab_t
1806 create_quick_file_names_table (unsigned int nr_initial_entries)
1807 {
1808 return htab_create_alloc (nr_initial_entries,
1809 hash_file_name_entry, eq_file_name_entry,
1810 delete_file_name_entry, xcalloc, xfree);
1811 }
1812
1813 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1814 have to be created afterwards. You should call age_cached_comp_units after
1815 processing PER_CU->CU. dw2_setup must have been already called. */
1816
1817 static void
1818 load_cu (struct dwarf2_per_cu_data *per_cu)
1819 {
1820 if (per_cu->debug_types_section)
1821 load_full_type_unit (per_cu);
1822 else
1823 load_full_comp_unit (per_cu);
1824
1825 gdb_assert (per_cu->cu != NULL);
1826
1827 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1828 }
1829
1830 /* Read in the symbols for PER_CU. */
1831
1832 static void
1833 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1834 {
1835 struct cleanup *back_to;
1836
1837 back_to = make_cleanup (dwarf2_release_queue, NULL);
1838
1839 queue_comp_unit (per_cu);
1840
1841 load_cu (per_cu);
1842
1843 process_queue ();
1844
1845 /* Age the cache, releasing compilation units that have not
1846 been used recently. */
1847 age_cached_comp_units ();
1848
1849 do_cleanups (back_to);
1850 }
1851
1852 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1853 the objfile from which this CU came. Returns the resulting symbol
1854 table. */
1855
1856 static struct symtab *
1857 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1858 {
1859 if (!per_cu->v.quick->symtab)
1860 {
1861 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1862 increment_reading_symtab ();
1863 dw2_do_instantiate_symtab (per_cu);
1864 do_cleanups (back_to);
1865 }
1866 return per_cu->v.quick->symtab;
1867 }
1868
1869 /* Return the CU given its index. */
1870
1871 static struct dwarf2_per_cu_data *
1872 dw2_get_cu (int index)
1873 {
1874 if (index >= dwarf2_per_objfile->n_comp_units)
1875 {
1876 index -= dwarf2_per_objfile->n_comp_units;
1877 return dwarf2_per_objfile->type_comp_units[index];
1878 }
1879 return dwarf2_per_objfile->all_comp_units[index];
1880 }
1881
1882 /* A helper function that knows how to read a 64-bit value in a way
1883 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1884 otherwise. */
1885
1886 static int
1887 extract_cu_value (const char *bytes, ULONGEST *result)
1888 {
1889 if (sizeof (ULONGEST) < 8)
1890 {
1891 int i;
1892
1893 /* Ignore the upper 4 bytes if they are all zero. */
1894 for (i = 0; i < 4; ++i)
1895 if (bytes[i + 4] != 0)
1896 return 0;
1897
1898 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1899 }
1900 else
1901 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1902 return 1;
1903 }
1904
1905 /* Read the CU list from the mapped index, and use it to create all
1906 the CU objects for this objfile. Return 0 if something went wrong,
1907 1 if everything went ok. */
1908
1909 static int
1910 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1911 offset_type cu_list_elements)
1912 {
1913 offset_type i;
1914
1915 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1916 dwarf2_per_objfile->all_comp_units
1917 = obstack_alloc (&objfile->objfile_obstack,
1918 dwarf2_per_objfile->n_comp_units
1919 * sizeof (struct dwarf2_per_cu_data *));
1920
1921 for (i = 0; i < cu_list_elements; i += 2)
1922 {
1923 struct dwarf2_per_cu_data *the_cu;
1924 ULONGEST offset, length;
1925
1926 if (!extract_cu_value (cu_list, &offset)
1927 || !extract_cu_value (cu_list + 8, &length))
1928 return 0;
1929 cu_list += 2 * 8;
1930
1931 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1932 struct dwarf2_per_cu_data);
1933 the_cu->offset = offset;
1934 the_cu->length = length;
1935 the_cu->objfile = objfile;
1936 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1937 struct dwarf2_per_cu_quick_data);
1938 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1939 }
1940
1941 return 1;
1942 }
1943
1944 /* Create the signatured type hash table from the index. */
1945
1946 static int
1947 create_signatured_type_table_from_index (struct objfile *objfile,
1948 struct dwarf2_section_info *section,
1949 const gdb_byte *bytes,
1950 offset_type elements)
1951 {
1952 offset_type i;
1953 htab_t sig_types_hash;
1954
1955 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1956 dwarf2_per_objfile->type_comp_units
1957 = obstack_alloc (&objfile->objfile_obstack,
1958 dwarf2_per_objfile->n_type_comp_units
1959 * sizeof (struct dwarf2_per_cu_data *));
1960
1961 sig_types_hash = allocate_signatured_type_table (objfile);
1962
1963 for (i = 0; i < elements; i += 3)
1964 {
1965 struct signatured_type *type_sig;
1966 ULONGEST offset, type_offset, signature;
1967 void **slot;
1968
1969 if (!extract_cu_value (bytes, &offset)
1970 || !extract_cu_value (bytes + 8, &type_offset))
1971 return 0;
1972 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1973 bytes += 3 * 8;
1974
1975 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1976 struct signatured_type);
1977 type_sig->signature = signature;
1978 type_sig->type_offset = type_offset;
1979 type_sig->per_cu.debug_types_section = section;
1980 type_sig->per_cu.offset = offset;
1981 type_sig->per_cu.objfile = objfile;
1982 type_sig->per_cu.v.quick
1983 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1984 struct dwarf2_per_cu_quick_data);
1985
1986 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1987 *slot = type_sig;
1988
1989 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1990 }
1991
1992 dwarf2_per_objfile->signatured_types = sig_types_hash;
1993
1994 return 1;
1995 }
1996
1997 /* Read the address map data from the mapped index, and use it to
1998 populate the objfile's psymtabs_addrmap. */
1999
2000 static void
2001 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2002 {
2003 const gdb_byte *iter, *end;
2004 struct obstack temp_obstack;
2005 struct addrmap *mutable_map;
2006 struct cleanup *cleanup;
2007 CORE_ADDR baseaddr;
2008
2009 obstack_init (&temp_obstack);
2010 cleanup = make_cleanup_obstack_free (&temp_obstack);
2011 mutable_map = addrmap_create_mutable (&temp_obstack);
2012
2013 iter = index->address_table;
2014 end = iter + index->address_table_size;
2015
2016 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2017
2018 while (iter < end)
2019 {
2020 ULONGEST hi, lo, cu_index;
2021 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2022 iter += 8;
2023 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2024 iter += 8;
2025 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2026 iter += 4;
2027
2028 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2029 dw2_get_cu (cu_index));
2030 }
2031
2032 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2033 &objfile->objfile_obstack);
2034 do_cleanups (cleanup);
2035 }
2036
2037 /* The hash function for strings in the mapped index. This is the same as
2038 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2039 implementation. This is necessary because the hash function is tied to the
2040 format of the mapped index file. The hash values do not have to match with
2041 SYMBOL_HASH_NEXT.
2042
2043 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2044
2045 static hashval_t
2046 mapped_index_string_hash (int index_version, const void *p)
2047 {
2048 const unsigned char *str = (const unsigned char *) p;
2049 hashval_t r = 0;
2050 unsigned char c;
2051
2052 while ((c = *str++) != 0)
2053 {
2054 if (index_version >= 5)
2055 c = tolower (c);
2056 r = r * 67 + c - 113;
2057 }
2058
2059 return r;
2060 }
2061
2062 /* Find a slot in the mapped index INDEX for the object named NAME.
2063 If NAME is found, set *VEC_OUT to point to the CU vector in the
2064 constant pool and return 1. If NAME cannot be found, return 0. */
2065
2066 static int
2067 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2068 offset_type **vec_out)
2069 {
2070 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2071 offset_type hash;
2072 offset_type slot, step;
2073 int (*cmp) (const char *, const char *);
2074
2075 if (current_language->la_language == language_cplus
2076 || current_language->la_language == language_java
2077 || current_language->la_language == language_fortran)
2078 {
2079 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2080 not contain any. */
2081 const char *paren = strchr (name, '(');
2082
2083 if (paren)
2084 {
2085 char *dup;
2086
2087 dup = xmalloc (paren - name + 1);
2088 memcpy (dup, name, paren - name);
2089 dup[paren - name] = 0;
2090
2091 make_cleanup (xfree, dup);
2092 name = dup;
2093 }
2094 }
2095
2096 /* Index version 4 did not support case insensitive searches. But the
2097 indexes for case insensitive languages are built in lowercase, therefore
2098 simulate our NAME being searched is also lowercased. */
2099 hash = mapped_index_string_hash ((index->version == 4
2100 && case_sensitivity == case_sensitive_off
2101 ? 5 : index->version),
2102 name);
2103
2104 slot = hash & (index->symbol_table_slots - 1);
2105 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2106 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2107
2108 for (;;)
2109 {
2110 /* Convert a slot number to an offset into the table. */
2111 offset_type i = 2 * slot;
2112 const char *str;
2113 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2114 {
2115 do_cleanups (back_to);
2116 return 0;
2117 }
2118
2119 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2120 if (!cmp (name, str))
2121 {
2122 *vec_out = (offset_type *) (index->constant_pool
2123 + MAYBE_SWAP (index->symbol_table[i + 1]));
2124 do_cleanups (back_to);
2125 return 1;
2126 }
2127
2128 slot = (slot + step) & (index->symbol_table_slots - 1);
2129 }
2130 }
2131
2132 /* Read the index file. If everything went ok, initialize the "quick"
2133 elements of all the CUs and return 1. Otherwise, return 0. */
2134
2135 static int
2136 dwarf2_read_index (struct objfile *objfile)
2137 {
2138 char *addr;
2139 struct mapped_index *map;
2140 offset_type *metadata;
2141 const gdb_byte *cu_list;
2142 const gdb_byte *types_list = NULL;
2143 offset_type version, cu_list_elements;
2144 offset_type types_list_elements = 0;
2145 int i;
2146
2147 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2148 return 0;
2149
2150 /* Older elfutils strip versions could keep the section in the main
2151 executable while splitting it for the separate debug info file. */
2152 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2153 & SEC_HAS_CONTENTS) == 0)
2154 return 0;
2155
2156 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2157
2158 addr = dwarf2_per_objfile->gdb_index.buffer;
2159 /* Version check. */
2160 version = MAYBE_SWAP (*(offset_type *) addr);
2161 /* Versions earlier than 3 emitted every copy of a psymbol. This
2162 causes the index to behave very poorly for certain requests. Version 3
2163 contained incomplete addrmap. So, it seems better to just ignore such
2164 indices. Index version 4 uses a different hash function than index
2165 version 5 and later. */
2166 if (version < 4)
2167 return 0;
2168 /* Indexes with higher version than the one supported by GDB may be no
2169 longer backward compatible. */
2170 if (version > 5)
2171 return 0;
2172
2173 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2174 map->version = version;
2175 map->total_size = dwarf2_per_objfile->gdb_index.size;
2176
2177 metadata = (offset_type *) (addr + sizeof (offset_type));
2178
2179 i = 0;
2180 cu_list = addr + MAYBE_SWAP (metadata[i]);
2181 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2182 / 8);
2183 ++i;
2184
2185 types_list = addr + MAYBE_SWAP (metadata[i]);
2186 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2187 - MAYBE_SWAP (metadata[i]))
2188 / 8);
2189 ++i;
2190
2191 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2192 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2193 - MAYBE_SWAP (metadata[i]));
2194 ++i;
2195
2196 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2197 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2198 - MAYBE_SWAP (metadata[i]))
2199 / (2 * sizeof (offset_type)));
2200 ++i;
2201
2202 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2203
2204 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2205 return 0;
2206
2207 if (types_list_elements)
2208 {
2209 struct dwarf2_section_info *section;
2210
2211 /* We can only handle a single .debug_types when we have an
2212 index. */
2213 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2214 return 0;
2215
2216 section = VEC_index (dwarf2_section_info_def,
2217 dwarf2_per_objfile->types, 0);
2218
2219 if (!create_signatured_type_table_from_index (objfile, section,
2220 types_list,
2221 types_list_elements))
2222 return 0;
2223 }
2224
2225 create_addrmap_from_index (objfile, map);
2226
2227 dwarf2_per_objfile->index_table = map;
2228 dwarf2_per_objfile->using_index = 1;
2229 dwarf2_per_objfile->quick_file_names_table =
2230 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2231
2232 return 1;
2233 }
2234
2235 /* A helper for the "quick" functions which sets the global
2236 dwarf2_per_objfile according to OBJFILE. */
2237
2238 static void
2239 dw2_setup (struct objfile *objfile)
2240 {
2241 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2242 gdb_assert (dwarf2_per_objfile);
2243 }
2244
2245 /* A helper for the "quick" functions which attempts to read the line
2246 table for THIS_CU. */
2247
2248 static struct quick_file_names *
2249 dw2_get_file_names (struct objfile *objfile,
2250 struct dwarf2_per_cu_data *this_cu)
2251 {
2252 bfd *abfd = objfile->obfd;
2253 struct line_header *lh;
2254 struct attribute *attr;
2255 struct cleanup *cleanups;
2256 struct die_info *comp_unit_die;
2257 struct dwarf2_section_info* sec;
2258 gdb_byte *info_ptr, *buffer;
2259 int has_children, i;
2260 struct dwarf2_cu cu;
2261 unsigned int bytes_read, buffer_size;
2262 struct die_reader_specs reader_specs;
2263 char *name, *comp_dir;
2264 void **slot;
2265 struct quick_file_names *qfn;
2266 unsigned int line_offset;
2267
2268 if (this_cu->v.quick->file_names != NULL)
2269 return this_cu->v.quick->file_names;
2270 /* If we know there is no line data, no point in looking again. */
2271 if (this_cu->v.quick->no_file_data)
2272 return NULL;
2273
2274 init_one_comp_unit (&cu, this_cu);
2275 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2276
2277 if (this_cu->debug_types_section)
2278 sec = this_cu->debug_types_section;
2279 else
2280 sec = &dwarf2_per_objfile->info;
2281 dwarf2_read_section (objfile, sec);
2282 buffer_size = sec->size;
2283 buffer = sec->buffer;
2284 info_ptr = buffer + this_cu->offset;
2285
2286 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2287 buffer, buffer_size,
2288 abfd,
2289 this_cu->debug_types_section != NULL);
2290
2291 /* Skip dummy compilation units. */
2292 if (info_ptr >= buffer + buffer_size
2293 || peek_abbrev_code (abfd, info_ptr) == 0)
2294 {
2295 do_cleanups (cleanups);
2296 return NULL;
2297 }
2298
2299 dwarf2_read_abbrevs (&cu);
2300 make_cleanup (dwarf2_free_abbrev_table, &cu);
2301
2302 init_cu_die_reader (&reader_specs, &cu);
2303 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2304 &has_children);
2305
2306 lh = NULL;
2307 slot = NULL;
2308 line_offset = 0;
2309 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2310 if (attr)
2311 {
2312 struct quick_file_names find_entry;
2313
2314 line_offset = DW_UNSND (attr);
2315
2316 /* We may have already read in this line header (TU line header sharing).
2317 If we have we're done. */
2318 find_entry.offset = line_offset;
2319 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2320 &find_entry, INSERT);
2321 if (*slot != NULL)
2322 {
2323 do_cleanups (cleanups);
2324 this_cu->v.quick->file_names = *slot;
2325 return *slot;
2326 }
2327
2328 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2329 }
2330 if (lh == NULL)
2331 {
2332 do_cleanups (cleanups);
2333 this_cu->v.quick->no_file_data = 1;
2334 return NULL;
2335 }
2336
2337 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2338 qfn->offset = line_offset;
2339 gdb_assert (slot != NULL);
2340 *slot = qfn;
2341
2342 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2343
2344 qfn->num_file_names = lh->num_file_names;
2345 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2346 lh->num_file_names * sizeof (char *));
2347 for (i = 0; i < lh->num_file_names; ++i)
2348 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2349 qfn->real_names = NULL;
2350
2351 free_line_header (lh);
2352 do_cleanups (cleanups);
2353
2354 this_cu->v.quick->file_names = qfn;
2355 return qfn;
2356 }
2357
2358 /* A helper for the "quick" functions which computes and caches the
2359 real path for a given file name from the line table. */
2360
2361 static const char *
2362 dw2_get_real_path (struct objfile *objfile,
2363 struct quick_file_names *qfn, int index)
2364 {
2365 if (qfn->real_names == NULL)
2366 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2367 qfn->num_file_names, sizeof (char *));
2368
2369 if (qfn->real_names[index] == NULL)
2370 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2371
2372 return qfn->real_names[index];
2373 }
2374
2375 static struct symtab *
2376 dw2_find_last_source_symtab (struct objfile *objfile)
2377 {
2378 int index;
2379
2380 dw2_setup (objfile);
2381 index = dwarf2_per_objfile->n_comp_units - 1;
2382 return dw2_instantiate_symtab (dw2_get_cu (index));
2383 }
2384
2385 /* Traversal function for dw2_forget_cached_source_info. */
2386
2387 static int
2388 dw2_free_cached_file_names (void **slot, void *info)
2389 {
2390 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2391
2392 if (file_data->real_names)
2393 {
2394 int i;
2395
2396 for (i = 0; i < file_data->num_file_names; ++i)
2397 {
2398 xfree ((void*) file_data->real_names[i]);
2399 file_data->real_names[i] = NULL;
2400 }
2401 }
2402
2403 return 1;
2404 }
2405
2406 static void
2407 dw2_forget_cached_source_info (struct objfile *objfile)
2408 {
2409 dw2_setup (objfile);
2410
2411 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2412 dw2_free_cached_file_names, NULL);
2413 }
2414
2415 /* Helper function for dw2_map_symtabs_matching_filename that expands
2416 the symtabs and calls the iterator. */
2417
2418 static int
2419 dw2_map_expand_apply (struct objfile *objfile,
2420 struct dwarf2_per_cu_data *per_cu,
2421 const char *name,
2422 const char *full_path, const char *real_path,
2423 int (*callback) (struct symtab *, void *),
2424 void *data)
2425 {
2426 struct symtab *last_made = objfile->symtabs;
2427
2428 /* Don't visit already-expanded CUs. */
2429 if (per_cu->v.quick->symtab)
2430 return 0;
2431
2432 /* This may expand more than one symtab, and we want to iterate over
2433 all of them. */
2434 dw2_instantiate_symtab (per_cu);
2435
2436 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2437 objfile->symtabs, last_made);
2438 }
2439
2440 /* Implementation of the map_symtabs_matching_filename method. */
2441
2442 static int
2443 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2444 const char *full_path, const char *real_path,
2445 int (*callback) (struct symtab *, void *),
2446 void *data)
2447 {
2448 int i;
2449 const char *name_basename = lbasename (name);
2450 int check_basename = name_basename == name;
2451 struct dwarf2_per_cu_data *base_cu = NULL;
2452
2453 dw2_setup (objfile);
2454
2455 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2456 + dwarf2_per_objfile->n_type_comp_units); ++i)
2457 {
2458 int j;
2459 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2460 struct quick_file_names *file_data;
2461
2462 /* We only need to look at symtabs not already expanded. */
2463 if (per_cu->v.quick->symtab)
2464 continue;
2465
2466 file_data = dw2_get_file_names (objfile, per_cu);
2467 if (file_data == NULL)
2468 continue;
2469
2470 for (j = 0; j < file_data->num_file_names; ++j)
2471 {
2472 const char *this_name = file_data->file_names[j];
2473
2474 if (FILENAME_CMP (name, this_name) == 0)
2475 {
2476 if (dw2_map_expand_apply (objfile, per_cu,
2477 name, full_path, real_path,
2478 callback, data))
2479 return 1;
2480 }
2481
2482 if (check_basename && ! base_cu
2483 && FILENAME_CMP (lbasename (this_name), name) == 0)
2484 base_cu = per_cu;
2485
2486 /* Before we invoke realpath, which can get expensive when many
2487 files are involved, do a quick comparison of the basenames. */
2488 if (! basenames_may_differ
2489 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2490 continue;
2491
2492 if (full_path != NULL)
2493 {
2494 const char *this_real_name = dw2_get_real_path (objfile,
2495 file_data, j);
2496
2497 if (this_real_name != NULL
2498 && FILENAME_CMP (full_path, this_real_name) == 0)
2499 {
2500 if (dw2_map_expand_apply (objfile, per_cu,
2501 name, full_path, real_path,
2502 callback, data))
2503 return 1;
2504 }
2505 }
2506
2507 if (real_path != NULL)
2508 {
2509 const char *this_real_name = dw2_get_real_path (objfile,
2510 file_data, j);
2511
2512 if (this_real_name != NULL
2513 && FILENAME_CMP (real_path, this_real_name) == 0)
2514 {
2515 if (dw2_map_expand_apply (objfile, per_cu,
2516 name, full_path, real_path,
2517 callback, data))
2518 return 1;
2519 }
2520 }
2521 }
2522 }
2523
2524 if (base_cu)
2525 {
2526 if (dw2_map_expand_apply (objfile, base_cu,
2527 name, full_path, real_path,
2528 callback, data))
2529 return 1;
2530 }
2531
2532 return 0;
2533 }
2534
2535 static struct symtab *
2536 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2537 const char *name, domain_enum domain)
2538 {
2539 /* We do all the work in the pre_expand_symtabs_matching hook
2540 instead. */
2541 return NULL;
2542 }
2543
2544 /* A helper function that expands all symtabs that hold an object
2545 named NAME. */
2546
2547 static void
2548 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2549 {
2550 dw2_setup (objfile);
2551
2552 /* index_table is NULL if OBJF_READNOW. */
2553 if (dwarf2_per_objfile->index_table)
2554 {
2555 offset_type *vec;
2556
2557 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2558 name, &vec))
2559 {
2560 offset_type i, len = MAYBE_SWAP (*vec);
2561 for (i = 0; i < len; ++i)
2562 {
2563 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2564 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2565
2566 dw2_instantiate_symtab (per_cu);
2567 }
2568 }
2569 }
2570 }
2571
2572 static void
2573 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2574 enum block_enum block_kind, const char *name,
2575 domain_enum domain)
2576 {
2577 dw2_do_expand_symtabs_matching (objfile, name);
2578 }
2579
2580 static void
2581 dw2_print_stats (struct objfile *objfile)
2582 {
2583 int i, count;
2584
2585 dw2_setup (objfile);
2586 count = 0;
2587 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2588 + dwarf2_per_objfile->n_type_comp_units); ++i)
2589 {
2590 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2591
2592 if (!per_cu->v.quick->symtab)
2593 ++count;
2594 }
2595 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2596 }
2597
2598 static void
2599 dw2_dump (struct objfile *objfile)
2600 {
2601 /* Nothing worth printing. */
2602 }
2603
2604 static void
2605 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2606 struct section_offsets *delta)
2607 {
2608 /* There's nothing to relocate here. */
2609 }
2610
2611 static void
2612 dw2_expand_symtabs_for_function (struct objfile *objfile,
2613 const char *func_name)
2614 {
2615 dw2_do_expand_symtabs_matching (objfile, func_name);
2616 }
2617
2618 static void
2619 dw2_expand_all_symtabs (struct objfile *objfile)
2620 {
2621 int i;
2622
2623 dw2_setup (objfile);
2624
2625 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2626 + dwarf2_per_objfile->n_type_comp_units); ++i)
2627 {
2628 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2629
2630 dw2_instantiate_symtab (per_cu);
2631 }
2632 }
2633
2634 static void
2635 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2636 const char *filename)
2637 {
2638 int i;
2639
2640 dw2_setup (objfile);
2641
2642 /* We don't need to consider type units here.
2643 This is only called for examining code, e.g. expand_line_sal.
2644 There can be an order of magnitude (or more) more type units
2645 than comp units, and we avoid them if we can. */
2646
2647 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2648 {
2649 int j;
2650 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2651 struct quick_file_names *file_data;
2652
2653 /* We only need to look at symtabs not already expanded. */
2654 if (per_cu->v.quick->symtab)
2655 continue;
2656
2657 file_data = dw2_get_file_names (objfile, per_cu);
2658 if (file_data == NULL)
2659 continue;
2660
2661 for (j = 0; j < file_data->num_file_names; ++j)
2662 {
2663 const char *this_name = file_data->file_names[j];
2664 if (FILENAME_CMP (this_name, filename) == 0)
2665 {
2666 dw2_instantiate_symtab (per_cu);
2667 break;
2668 }
2669 }
2670 }
2671 }
2672
2673 static const char *
2674 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2675 {
2676 struct dwarf2_per_cu_data *per_cu;
2677 offset_type *vec;
2678 struct quick_file_names *file_data;
2679
2680 dw2_setup (objfile);
2681
2682 /* index_table is NULL if OBJF_READNOW. */
2683 if (!dwarf2_per_objfile->index_table)
2684 {
2685 struct symtab *s;
2686
2687 ALL_OBJFILE_SYMTABS (objfile, s)
2688 if (s->primary)
2689 {
2690 struct blockvector *bv = BLOCKVECTOR (s);
2691 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2692 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2693
2694 if (sym)
2695 return sym->symtab->filename;
2696 }
2697 return NULL;
2698 }
2699
2700 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2701 name, &vec))
2702 return NULL;
2703
2704 /* Note that this just looks at the very first one named NAME -- but
2705 actually we are looking for a function. find_main_filename
2706 should be rewritten so that it doesn't require a custom hook. It
2707 could just use the ordinary symbol tables. */
2708 /* vec[0] is the length, which must always be >0. */
2709 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2710
2711 file_data = dw2_get_file_names (objfile, per_cu);
2712 if (file_data == NULL)
2713 return NULL;
2714
2715 return file_data->file_names[file_data->num_file_names - 1];
2716 }
2717
2718 static void
2719 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2720 struct objfile *objfile, int global,
2721 int (*callback) (struct block *,
2722 struct symbol *, void *),
2723 void *data, symbol_compare_ftype *match,
2724 symbol_compare_ftype *ordered_compare)
2725 {
2726 /* Currently unimplemented; used for Ada. The function can be called if the
2727 current language is Ada for a non-Ada objfile using GNU index. As Ada
2728 does not look for non-Ada symbols this function should just return. */
2729 }
2730
2731 static void
2732 dw2_expand_symtabs_matching
2733 (struct objfile *objfile,
2734 int (*file_matcher) (const char *, void *),
2735 int (*name_matcher) (const struct language_defn *, const char *, void *),
2736 enum search_domain kind,
2737 void *data)
2738 {
2739 int i;
2740 offset_type iter;
2741 struct mapped_index *index;
2742
2743 dw2_setup (objfile);
2744
2745 /* index_table is NULL if OBJF_READNOW. */
2746 if (!dwarf2_per_objfile->index_table)
2747 return;
2748 index = dwarf2_per_objfile->index_table;
2749
2750 if (file_matcher != NULL)
2751 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2752 + dwarf2_per_objfile->n_type_comp_units); ++i)
2753 {
2754 int j;
2755 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2756 struct quick_file_names *file_data;
2757
2758 per_cu->v.quick->mark = 0;
2759
2760 /* We only need to look at symtabs not already expanded. */
2761 if (per_cu->v.quick->symtab)
2762 continue;
2763
2764 file_data = dw2_get_file_names (objfile, per_cu);
2765 if (file_data == NULL)
2766 continue;
2767
2768 for (j = 0; j < file_data->num_file_names; ++j)
2769 {
2770 if (file_matcher (file_data->file_names[j], data))
2771 {
2772 per_cu->v.quick->mark = 1;
2773 break;
2774 }
2775 }
2776 }
2777
2778 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2779 {
2780 offset_type idx = 2 * iter;
2781 const char *name;
2782 offset_type *vec, vec_len, vec_idx;
2783
2784 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2785 continue;
2786
2787 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2788
2789 if (! (*name_matcher) (current_language, name, data))
2790 continue;
2791
2792 /* The name was matched, now expand corresponding CUs that were
2793 marked. */
2794 vec = (offset_type *) (index->constant_pool
2795 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2796 vec_len = MAYBE_SWAP (vec[0]);
2797 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2798 {
2799 struct dwarf2_per_cu_data *per_cu;
2800
2801 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2802 if (file_matcher == NULL || per_cu->v.quick->mark)
2803 dw2_instantiate_symtab (per_cu);
2804 }
2805 }
2806 }
2807
2808 static struct symtab *
2809 dw2_find_pc_sect_symtab (struct objfile *objfile,
2810 struct minimal_symbol *msymbol,
2811 CORE_ADDR pc,
2812 struct obj_section *section,
2813 int warn_if_readin)
2814 {
2815 struct dwarf2_per_cu_data *data;
2816
2817 dw2_setup (objfile);
2818
2819 if (!objfile->psymtabs_addrmap)
2820 return NULL;
2821
2822 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2823 if (!data)
2824 return NULL;
2825
2826 if (warn_if_readin && data->v.quick->symtab)
2827 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2828 paddress (get_objfile_arch (objfile), pc));
2829
2830 return dw2_instantiate_symtab (data);
2831 }
2832
2833 static void
2834 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2835 void *data, int need_fullname)
2836 {
2837 int i;
2838
2839 dw2_setup (objfile);
2840
2841 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2842 + dwarf2_per_objfile->n_type_comp_units); ++i)
2843 {
2844 int j;
2845 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2846 struct quick_file_names *file_data;
2847
2848 /* We only need to look at symtabs not already expanded. */
2849 if (per_cu->v.quick->symtab)
2850 continue;
2851
2852 file_data = dw2_get_file_names (objfile, per_cu);
2853 if (file_data == NULL)
2854 continue;
2855
2856 for (j = 0; j < file_data->num_file_names; ++j)
2857 {
2858 const char *this_real_name;
2859
2860 if (need_fullname)
2861 this_real_name = dw2_get_real_path (objfile, file_data, j);
2862 else
2863 this_real_name = NULL;
2864 (*fun) (file_data->file_names[j], this_real_name, data);
2865 }
2866 }
2867 }
2868
2869 static int
2870 dw2_has_symbols (struct objfile *objfile)
2871 {
2872 return 1;
2873 }
2874
2875 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2876 {
2877 dw2_has_symbols,
2878 dw2_find_last_source_symtab,
2879 dw2_forget_cached_source_info,
2880 dw2_map_symtabs_matching_filename,
2881 dw2_lookup_symbol,
2882 dw2_pre_expand_symtabs_matching,
2883 dw2_print_stats,
2884 dw2_dump,
2885 dw2_relocate,
2886 dw2_expand_symtabs_for_function,
2887 dw2_expand_all_symtabs,
2888 dw2_expand_symtabs_with_filename,
2889 dw2_find_symbol_file,
2890 dw2_map_matching_symbols,
2891 dw2_expand_symtabs_matching,
2892 dw2_find_pc_sect_symtab,
2893 dw2_map_symbol_filenames
2894 };
2895
2896 /* Initialize for reading DWARF for this objfile. Return 0 if this
2897 file will use psymtabs, or 1 if using the GNU index. */
2898
2899 int
2900 dwarf2_initialize_objfile (struct objfile *objfile)
2901 {
2902 /* If we're about to read full symbols, don't bother with the
2903 indices. In this case we also don't care if some other debug
2904 format is making psymtabs, because they are all about to be
2905 expanded anyway. */
2906 if ((objfile->flags & OBJF_READNOW))
2907 {
2908 int i;
2909
2910 dwarf2_per_objfile->using_index = 1;
2911 create_all_comp_units (objfile);
2912 create_debug_types_hash_table (objfile);
2913 dwarf2_per_objfile->quick_file_names_table =
2914 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2915
2916 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2917 + dwarf2_per_objfile->n_type_comp_units); ++i)
2918 {
2919 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2920
2921 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2922 struct dwarf2_per_cu_quick_data);
2923 }
2924
2925 /* Return 1 so that gdb sees the "quick" functions. However,
2926 these functions will be no-ops because we will have expanded
2927 all symtabs. */
2928 return 1;
2929 }
2930
2931 if (dwarf2_read_index (objfile))
2932 return 1;
2933
2934 return 0;
2935 }
2936
2937 \f
2938
2939 /* Build a partial symbol table. */
2940
2941 void
2942 dwarf2_build_psymtabs (struct objfile *objfile)
2943 {
2944 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2945 {
2946 init_psymbol_list (objfile, 1024);
2947 }
2948
2949 dwarf2_build_psymtabs_hard (objfile);
2950 }
2951
2952 /* Return TRUE if OFFSET is within CU_HEADER. */
2953
2954 static inline int
2955 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2956 {
2957 unsigned int bottom = cu_header->offset;
2958 unsigned int top = (cu_header->offset
2959 + cu_header->length
2960 + cu_header->initial_length_size);
2961
2962 return (offset >= bottom && offset < top);
2963 }
2964
2965 /* Read in the comp unit header information from the debug_info at info_ptr.
2966 NOTE: This leaves members offset, first_die_offset to be filled in
2967 by the caller. */
2968
2969 static gdb_byte *
2970 read_comp_unit_head (struct comp_unit_head *cu_header,
2971 gdb_byte *info_ptr, bfd *abfd)
2972 {
2973 int signed_addr;
2974 unsigned int bytes_read;
2975
2976 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2977 cu_header->initial_length_size = bytes_read;
2978 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2979 info_ptr += bytes_read;
2980 cu_header->version = read_2_bytes (abfd, info_ptr);
2981 info_ptr += 2;
2982 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2983 &bytes_read);
2984 info_ptr += bytes_read;
2985 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2986 info_ptr += 1;
2987 signed_addr = bfd_get_sign_extend_vma (abfd);
2988 if (signed_addr < 0)
2989 internal_error (__FILE__, __LINE__,
2990 _("read_comp_unit_head: dwarf from non elf file"));
2991 cu_header->signed_addr_p = signed_addr;
2992
2993 return info_ptr;
2994 }
2995
2996 /* Read in a CU header and perform some basic error checking. */
2997
2998 static gdb_byte *
2999 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
3000 gdb_byte *buffer, unsigned int buffer_size,
3001 bfd *abfd, int is_debug_types_section)
3002 {
3003 gdb_byte *beg_of_comp_unit = info_ptr;
3004
3005 header->offset = beg_of_comp_unit - buffer;
3006
3007 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3008
3009 /* If we're reading a type unit, skip over the signature and
3010 type_offset fields. */
3011 if (is_debug_types_section)
3012 info_ptr += 8 /*signature*/ + header->offset_size;
3013
3014 header->first_die_offset = info_ptr - beg_of_comp_unit;
3015
3016 if (header->version != 2 && header->version != 3 && header->version != 4)
3017 error (_("Dwarf Error: wrong version in compilation unit header "
3018 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3019 bfd_get_filename (abfd));
3020
3021 if (header->abbrev_offset
3022 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3023 &dwarf2_per_objfile->abbrev))
3024 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3025 "(offset 0x%lx + 6) [in module %s]"),
3026 (long) header->abbrev_offset,
3027 (long) (beg_of_comp_unit - buffer),
3028 bfd_get_filename (abfd));
3029
3030 if (beg_of_comp_unit + header->length + header->initial_length_size
3031 > buffer + buffer_size)
3032 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3033 "(offset 0x%lx + 0) [in module %s]"),
3034 (long) header->length,
3035 (long) (beg_of_comp_unit - buffer),
3036 bfd_get_filename (abfd));
3037
3038 return info_ptr;
3039 }
3040
3041 /* Read in the types comp unit header information from .debug_types entry at
3042 types_ptr. The result is a pointer to one past the end of the header. */
3043
3044 static gdb_byte *
3045 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3046 struct dwarf2_section_info *section,
3047 ULONGEST *signature,
3048 gdb_byte *types_ptr, bfd *abfd)
3049 {
3050 gdb_byte *initial_types_ptr = types_ptr;
3051
3052 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3053 cu_header->offset = types_ptr - section->buffer;
3054
3055 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3056
3057 *signature = read_8_bytes (abfd, types_ptr);
3058 types_ptr += 8;
3059 types_ptr += cu_header->offset_size;
3060 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3061
3062 return types_ptr;
3063 }
3064
3065 /* Allocate a new partial symtab for file named NAME and mark this new
3066 partial symtab as being an include of PST. */
3067
3068 static void
3069 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3070 struct objfile *objfile)
3071 {
3072 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3073
3074 subpst->section_offsets = pst->section_offsets;
3075 subpst->textlow = 0;
3076 subpst->texthigh = 0;
3077
3078 subpst->dependencies = (struct partial_symtab **)
3079 obstack_alloc (&objfile->objfile_obstack,
3080 sizeof (struct partial_symtab *));
3081 subpst->dependencies[0] = pst;
3082 subpst->number_of_dependencies = 1;
3083
3084 subpst->globals_offset = 0;
3085 subpst->n_global_syms = 0;
3086 subpst->statics_offset = 0;
3087 subpst->n_static_syms = 0;
3088 subpst->symtab = NULL;
3089 subpst->read_symtab = pst->read_symtab;
3090 subpst->readin = 0;
3091
3092 /* No private part is necessary for include psymtabs. This property
3093 can be used to differentiate between such include psymtabs and
3094 the regular ones. */
3095 subpst->read_symtab_private = NULL;
3096 }
3097
3098 /* Read the Line Number Program data and extract the list of files
3099 included by the source file represented by PST. Build an include
3100 partial symtab for each of these included files. */
3101
3102 static void
3103 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3104 struct die_info *die,
3105 struct partial_symtab *pst)
3106 {
3107 struct objfile *objfile = cu->objfile;
3108 bfd *abfd = objfile->obfd;
3109 struct line_header *lh = NULL;
3110 struct attribute *attr;
3111
3112 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3113 if (attr)
3114 {
3115 unsigned int line_offset = DW_UNSND (attr);
3116
3117 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3118 }
3119 if (lh == NULL)
3120 return; /* No linetable, so no includes. */
3121
3122 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3123 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3124
3125 free_line_header (lh);
3126 }
3127
3128 static hashval_t
3129 hash_type_signature (const void *item)
3130 {
3131 const struct signatured_type *type_sig = item;
3132
3133 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3134 return type_sig->signature;
3135 }
3136
3137 static int
3138 eq_type_signature (const void *item_lhs, const void *item_rhs)
3139 {
3140 const struct signatured_type *lhs = item_lhs;
3141 const struct signatured_type *rhs = item_rhs;
3142
3143 return lhs->signature == rhs->signature;
3144 }
3145
3146 /* Allocate a hash table for signatured types. */
3147
3148 static htab_t
3149 allocate_signatured_type_table (struct objfile *objfile)
3150 {
3151 return htab_create_alloc_ex (41,
3152 hash_type_signature,
3153 eq_type_signature,
3154 NULL,
3155 &objfile->objfile_obstack,
3156 hashtab_obstack_allocate,
3157 dummy_obstack_deallocate);
3158 }
3159
3160 /* A helper function to add a signatured type CU to a list. */
3161
3162 static int
3163 add_signatured_type_cu_to_list (void **slot, void *datum)
3164 {
3165 struct signatured_type *sigt = *slot;
3166 struct dwarf2_per_cu_data ***datap = datum;
3167
3168 **datap = &sigt->per_cu;
3169 ++*datap;
3170
3171 return 1;
3172 }
3173
3174 /* Create the hash table of all entries in the .debug_types section.
3175 The result is zero if there is an error (e.g. missing .debug_types section),
3176 otherwise non-zero. */
3177
3178 static int
3179 create_debug_types_hash_table (struct objfile *objfile)
3180 {
3181 htab_t types_htab = NULL;
3182 struct dwarf2_per_cu_data **iter;
3183 int ix;
3184 struct dwarf2_section_info *section;
3185
3186 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3187 {
3188 dwarf2_per_objfile->signatured_types = NULL;
3189 return 0;
3190 }
3191
3192 for (ix = 0;
3193 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3194 ix, section);
3195 ++ix)
3196 {
3197 gdb_byte *info_ptr, *end_ptr;
3198
3199 dwarf2_read_section (objfile, section);
3200 info_ptr = section->buffer;
3201
3202 if (info_ptr == NULL)
3203 continue;
3204
3205 if (types_htab == NULL)
3206 types_htab = allocate_signatured_type_table (objfile);
3207
3208 if (dwarf2_die_debug)
3209 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3210
3211 end_ptr = info_ptr + section->size;
3212 while (info_ptr < end_ptr)
3213 {
3214 unsigned int offset;
3215 unsigned int offset_size;
3216 unsigned int type_offset;
3217 unsigned int length, initial_length_size;
3218 unsigned short version;
3219 ULONGEST signature;
3220 struct signatured_type *type_sig;
3221 void **slot;
3222 gdb_byte *ptr = info_ptr;
3223
3224 offset = ptr - section->buffer;
3225
3226 /* We need to read the type's signature in order to build the hash
3227 table, but we don't need to read anything else just yet. */
3228
3229 /* Sanity check to ensure entire cu is present. */
3230 length = read_initial_length (objfile->obfd, ptr,
3231 &initial_length_size);
3232 if (ptr + length + initial_length_size > end_ptr)
3233 {
3234 complaint (&symfile_complaints,
3235 _("debug type entry runs off end "
3236 "of `.debug_types' section, ignored"));
3237 break;
3238 }
3239
3240 offset_size = initial_length_size == 4 ? 4 : 8;
3241 ptr += initial_length_size;
3242 version = bfd_get_16 (objfile->obfd, ptr);
3243 ptr += 2;
3244 ptr += offset_size; /* abbrev offset */
3245 ptr += 1; /* address size */
3246 signature = bfd_get_64 (objfile->obfd, ptr);
3247 ptr += 8;
3248 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3249 ptr += offset_size;
3250
3251 /* Skip dummy type units. */
3252 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3253 {
3254 info_ptr = info_ptr + initial_length_size + length;
3255 continue;
3256 }
3257
3258 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3259 memset (type_sig, 0, sizeof (*type_sig));
3260 type_sig->signature = signature;
3261 type_sig->type_offset = type_offset;
3262 type_sig->per_cu.objfile = objfile;
3263 type_sig->per_cu.debug_types_section = section;
3264 type_sig->per_cu.offset = offset;
3265
3266 slot = htab_find_slot (types_htab, type_sig, INSERT);
3267 gdb_assert (slot != NULL);
3268 if (*slot != NULL)
3269 {
3270 const struct signatured_type *dup_sig = *slot;
3271
3272 complaint (&symfile_complaints,
3273 _("debug type entry at offset 0x%x is duplicate to the "
3274 "entry at offset 0x%x, signature 0x%s"),
3275 offset, dup_sig->per_cu.offset,
3276 phex (signature, sizeof (signature)));
3277 gdb_assert (signature == dup_sig->signature);
3278 }
3279 *slot = type_sig;
3280
3281 if (dwarf2_die_debug)
3282 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3283 offset, phex (signature, sizeof (signature)));
3284
3285 info_ptr = info_ptr + initial_length_size + length;
3286 }
3287 }
3288
3289 dwarf2_per_objfile->signatured_types = types_htab;
3290
3291 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3292 dwarf2_per_objfile->type_comp_units
3293 = obstack_alloc (&objfile->objfile_obstack,
3294 dwarf2_per_objfile->n_type_comp_units
3295 * sizeof (struct dwarf2_per_cu_data *));
3296 iter = &dwarf2_per_objfile->type_comp_units[0];
3297 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3298 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3299 == dwarf2_per_objfile->n_type_comp_units);
3300
3301 return 1;
3302 }
3303
3304 /* Lookup a signature based type.
3305 Returns NULL if SIG is not present in the table. */
3306
3307 static struct signatured_type *
3308 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3309 {
3310 struct signatured_type find_entry, *entry;
3311
3312 if (dwarf2_per_objfile->signatured_types == NULL)
3313 {
3314 complaint (&symfile_complaints,
3315 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3316 return 0;
3317 }
3318
3319 find_entry.signature = sig;
3320 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3321 return entry;
3322 }
3323
3324 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3325
3326 static void
3327 init_cu_die_reader (struct die_reader_specs *reader,
3328 struct dwarf2_cu *cu)
3329 {
3330 reader->abfd = cu->objfile->obfd;
3331 reader->cu = cu;
3332 if (cu->per_cu->debug_types_section)
3333 {
3334 gdb_assert (cu->per_cu->debug_types_section->readin);
3335 reader->buffer = cu->per_cu->debug_types_section->buffer;
3336 }
3337 else
3338 {
3339 gdb_assert (dwarf2_per_objfile->info.readin);
3340 reader->buffer = dwarf2_per_objfile->info.buffer;
3341 }
3342 }
3343
3344 /* Find the base address of the compilation unit for range lists and
3345 location lists. It will normally be specified by DW_AT_low_pc.
3346 In DWARF-3 draft 4, the base address could be overridden by
3347 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3348 compilation units with discontinuous ranges. */
3349
3350 static void
3351 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3352 {
3353 struct attribute *attr;
3354
3355 cu->base_known = 0;
3356 cu->base_address = 0;
3357
3358 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3359 if (attr)
3360 {
3361 cu->base_address = DW_ADDR (attr);
3362 cu->base_known = 1;
3363 }
3364 else
3365 {
3366 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3367 if (attr)
3368 {
3369 cu->base_address = DW_ADDR (attr);
3370 cu->base_known = 1;
3371 }
3372 }
3373 }
3374
3375 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3376 to combine the common parts.
3377 Process a compilation unit for a psymtab.
3378 BUFFER is a pointer to the beginning of the dwarf section buffer,
3379 either .debug_info or debug_types.
3380 INFO_PTR is a pointer to the start of the CU.
3381 Returns a pointer to the next CU. */
3382
3383 static gdb_byte *
3384 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3385 gdb_byte *buffer, gdb_byte *info_ptr,
3386 unsigned int buffer_size)
3387 {
3388 struct objfile *objfile = this_cu->objfile;
3389 bfd *abfd = objfile->obfd;
3390 gdb_byte *beg_of_comp_unit = info_ptr;
3391 struct die_info *comp_unit_die;
3392 struct partial_symtab *pst;
3393 CORE_ADDR baseaddr;
3394 struct cleanup *back_to_inner;
3395 struct dwarf2_cu cu;
3396 int has_children, has_pc_info;
3397 struct attribute *attr;
3398 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3399 struct die_reader_specs reader_specs;
3400 const char *filename;
3401
3402 /* If this compilation unit was already read in, free the
3403 cached copy in order to read it in again. This is
3404 necessary because we skipped some symbols when we first
3405 read in the compilation unit (see load_partial_dies).
3406 This problem could be avoided, but the benefit is
3407 unclear. */
3408 if (this_cu->cu != NULL)
3409 free_one_cached_comp_unit (this_cu->cu);
3410
3411 /* Note that this is a pointer to our stack frame, being
3412 added to a global data structure. It will be cleaned up
3413 in free_stack_comp_unit when we finish with this
3414 compilation unit. */
3415 init_one_comp_unit (&cu, this_cu);
3416 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3417
3418 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3419 buffer, buffer_size,
3420 abfd,
3421 this_cu->debug_types_section != NULL);
3422
3423 /* Skip dummy compilation units. */
3424 if (info_ptr >= buffer + buffer_size
3425 || peek_abbrev_code (abfd, info_ptr) == 0)
3426 {
3427 info_ptr = (beg_of_comp_unit + cu.header.length
3428 + cu.header.initial_length_size);
3429 do_cleanups (back_to_inner);
3430 return info_ptr;
3431 }
3432
3433 cu.list_in_scope = &file_symbols;
3434
3435 /* Read the abbrevs for this compilation unit into a table. */
3436 dwarf2_read_abbrevs (&cu);
3437 make_cleanup (dwarf2_free_abbrev_table, &cu);
3438
3439 /* Read the compilation unit die. */
3440 init_cu_die_reader (&reader_specs, &cu);
3441 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3442 &has_children);
3443
3444 if (this_cu->debug_types_section)
3445 {
3446 /* LENGTH has not been set yet for type units. */
3447 gdb_assert (this_cu->offset == cu.header.offset);
3448 this_cu->length = cu.header.length + cu.header.initial_length_size;
3449 }
3450 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3451 {
3452 info_ptr = (beg_of_comp_unit + cu.header.length
3453 + cu.header.initial_length_size);
3454 do_cleanups (back_to_inner);
3455 return info_ptr;
3456 }
3457
3458 prepare_one_comp_unit (&cu, comp_unit_die);
3459
3460 /* Allocate a new partial symbol table structure. */
3461 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3462 if (attr == NULL || !DW_STRING (attr))
3463 filename = "";
3464 else
3465 filename = DW_STRING (attr);
3466 pst = start_psymtab_common (objfile, objfile->section_offsets,
3467 filename,
3468 /* TEXTLOW and TEXTHIGH are set below. */
3469 0,
3470 objfile->global_psymbols.next,
3471 objfile->static_psymbols.next);
3472 pst->psymtabs_addrmap_supported = 1;
3473
3474 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3475 if (attr != NULL)
3476 pst->dirname = DW_STRING (attr);
3477
3478 pst->read_symtab_private = this_cu;
3479
3480 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3481
3482 /* Store the function that reads in the rest of the symbol table. */
3483 pst->read_symtab = dwarf2_psymtab_to_symtab;
3484
3485 this_cu->v.psymtab = pst;
3486
3487 dwarf2_find_base_address (comp_unit_die, &cu);
3488
3489 /* Possibly set the default values of LOWPC and HIGHPC from
3490 `DW_AT_ranges'. */
3491 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3492 &best_highpc, &cu, pst);
3493 if (has_pc_info == 1 && best_lowpc < best_highpc)
3494 /* Store the contiguous range if it is not empty; it can be empty for
3495 CUs with no code. */
3496 addrmap_set_empty (objfile->psymtabs_addrmap,
3497 best_lowpc + baseaddr,
3498 best_highpc + baseaddr - 1, pst);
3499
3500 /* Check if comp unit has_children.
3501 If so, read the rest of the partial symbols from this comp unit.
3502 If not, there's no more debug_info for this comp unit. */
3503 if (has_children)
3504 {
3505 struct partial_die_info *first_die;
3506 CORE_ADDR lowpc, highpc;
3507
3508 lowpc = ((CORE_ADDR) -1);
3509 highpc = ((CORE_ADDR) 0);
3510
3511 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3512
3513 scan_partial_symbols (first_die, &lowpc, &highpc,
3514 ! has_pc_info, &cu);
3515
3516 /* If we didn't find a lowpc, set it to highpc to avoid
3517 complaints from `maint check'. */
3518 if (lowpc == ((CORE_ADDR) -1))
3519 lowpc = highpc;
3520
3521 /* If the compilation unit didn't have an explicit address range,
3522 then use the information extracted from its child dies. */
3523 if (! has_pc_info)
3524 {
3525 best_lowpc = lowpc;
3526 best_highpc = highpc;
3527 }
3528 }
3529 pst->textlow = best_lowpc + baseaddr;
3530 pst->texthigh = best_highpc + baseaddr;
3531
3532 pst->n_global_syms = objfile->global_psymbols.next -
3533 (objfile->global_psymbols.list + pst->globals_offset);
3534 pst->n_static_syms = objfile->static_psymbols.next -
3535 (objfile->static_psymbols.list + pst->statics_offset);
3536 sort_pst_symbols (pst);
3537
3538 info_ptr = (beg_of_comp_unit + cu.header.length
3539 + cu.header.initial_length_size);
3540
3541 if (this_cu->debug_types_section)
3542 {
3543 /* It's not clear we want to do anything with stmt lists here.
3544 Waiting to see what gcc ultimately does. */
3545 }
3546 else
3547 {
3548 /* Get the list of files included in the current compilation unit,
3549 and build a psymtab for each of them. */
3550 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3551 }
3552
3553 do_cleanups (back_to_inner);
3554
3555 return info_ptr;
3556 }
3557
3558 /* Traversal function for htab_traverse_noresize.
3559 Process one .debug_types comp-unit. */
3560
3561 static int
3562 process_type_comp_unit (void **slot, void *info)
3563 {
3564 struct signatured_type *entry = (struct signatured_type *) *slot;
3565 struct dwarf2_per_cu_data *this_cu;
3566
3567 gdb_assert (info == NULL);
3568 this_cu = &entry->per_cu;
3569
3570 gdb_assert (this_cu->debug_types_section->readin);
3571 process_psymtab_comp_unit (this_cu,
3572 this_cu->debug_types_section->buffer,
3573 (this_cu->debug_types_section->buffer
3574 + this_cu->offset),
3575 this_cu->debug_types_section->size);
3576
3577 return 1;
3578 }
3579
3580 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3581 Build partial symbol tables for the .debug_types comp-units. */
3582
3583 static void
3584 build_type_psymtabs (struct objfile *objfile)
3585 {
3586 if (! create_debug_types_hash_table (objfile))
3587 return;
3588
3589 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3590 process_type_comp_unit, NULL);
3591 }
3592
3593 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3594
3595 static void
3596 psymtabs_addrmap_cleanup (void *o)
3597 {
3598 struct objfile *objfile = o;
3599
3600 objfile->psymtabs_addrmap = NULL;
3601 }
3602
3603 /* Build the partial symbol table by doing a quick pass through the
3604 .debug_info and .debug_abbrev sections. */
3605
3606 static void
3607 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3608 {
3609 gdb_byte *info_ptr;
3610 struct cleanup *back_to, *addrmap_cleanup;
3611 struct obstack temp_obstack;
3612
3613 dwarf2_per_objfile->reading_partial_symbols = 1;
3614
3615 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3616 info_ptr = dwarf2_per_objfile->info.buffer;
3617
3618 /* Any cached compilation units will be linked by the per-objfile
3619 read_in_chain. Make sure to free them when we're done. */
3620 back_to = make_cleanup (free_cached_comp_units, NULL);
3621
3622 build_type_psymtabs (objfile);
3623
3624 create_all_comp_units (objfile);
3625
3626 /* Create a temporary address map on a temporary obstack. We later
3627 copy this to the final obstack. */
3628 obstack_init (&temp_obstack);
3629 make_cleanup_obstack_free (&temp_obstack);
3630 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3631 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3632
3633 /* Since the objects we're extracting from .debug_info vary in
3634 length, only the individual functions to extract them (like
3635 read_comp_unit_head and load_partial_die) can really know whether
3636 the buffer is large enough to hold another complete object.
3637
3638 At the moment, they don't actually check that. If .debug_info
3639 holds just one extra byte after the last compilation unit's dies,
3640 then read_comp_unit_head will happily read off the end of the
3641 buffer. read_partial_die is similarly casual. Those functions
3642 should be fixed.
3643
3644 For this loop condition, simply checking whether there's any data
3645 left at all should be sufficient. */
3646
3647 while (info_ptr < (dwarf2_per_objfile->info.buffer
3648 + dwarf2_per_objfile->info.size))
3649 {
3650 struct dwarf2_per_cu_data *this_cu;
3651
3652 this_cu = dwarf2_find_comp_unit (info_ptr
3653 - dwarf2_per_objfile->info.buffer,
3654 objfile);
3655
3656 info_ptr = process_psymtab_comp_unit (this_cu,
3657 dwarf2_per_objfile->info.buffer,
3658 info_ptr,
3659 dwarf2_per_objfile->info.size);
3660 }
3661
3662 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3663 &objfile->objfile_obstack);
3664 discard_cleanups (addrmap_cleanup);
3665
3666 do_cleanups (back_to);
3667 }
3668
3669 /* Load the partial DIEs for a secondary CU into memory. */
3670
3671 static void
3672 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3673 {
3674 struct objfile *objfile = this_cu->objfile;
3675 bfd *abfd = objfile->obfd;
3676 gdb_byte *info_ptr;
3677 struct die_info *comp_unit_die;
3678 struct dwarf2_cu *cu;
3679 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3680 int has_children;
3681 struct die_reader_specs reader_specs;
3682 int read_cu = 0;
3683
3684 gdb_assert (! this_cu->debug_types_section);
3685
3686 gdb_assert (dwarf2_per_objfile->info.readin);
3687 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3688
3689 if (this_cu->cu == NULL)
3690 {
3691 cu = xmalloc (sizeof (*cu));
3692 init_one_comp_unit (cu, this_cu);
3693
3694 read_cu = 1;
3695
3696 /* If an error occurs while loading, release our storage. */
3697 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3698
3699 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3700 dwarf2_per_objfile->info.buffer,
3701 dwarf2_per_objfile->info.size,
3702 abfd, 0);
3703
3704 /* Skip dummy compilation units. */
3705 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3706 + dwarf2_per_objfile->info.size)
3707 || peek_abbrev_code (abfd, info_ptr) == 0)
3708 {
3709 do_cleanups (free_cu_cleanup);
3710 return;
3711 }
3712
3713 /* Link this CU into read_in_chain. */
3714 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3715 dwarf2_per_objfile->read_in_chain = this_cu;
3716 }
3717 else
3718 {
3719 cu = this_cu->cu;
3720 info_ptr += cu->header.first_die_offset;
3721 }
3722
3723 /* Read the abbrevs for this compilation unit into a table. */
3724 gdb_assert (cu->dwarf2_abbrevs == NULL);
3725 dwarf2_read_abbrevs (cu);
3726 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3727
3728 /* Read the compilation unit die. */
3729 init_cu_die_reader (&reader_specs, cu);
3730 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3731 &has_children);
3732
3733 prepare_one_comp_unit (cu, comp_unit_die);
3734
3735 /* Check if comp unit has_children.
3736 If so, read the rest of the partial symbols from this comp unit.
3737 If not, there's no more debug_info for this comp unit. */
3738 if (has_children)
3739 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3740
3741 do_cleanups (free_abbrevs_cleanup);
3742
3743 if (read_cu)
3744 {
3745 /* We've successfully allocated this compilation unit. Let our
3746 caller clean it up when finished with it. */
3747 discard_cleanups (free_cu_cleanup);
3748 }
3749 }
3750
3751 /* Create a list of all compilation units in OBJFILE.
3752 This is only done for -readnow and building partial symtabs. */
3753
3754 static void
3755 create_all_comp_units (struct objfile *objfile)
3756 {
3757 int n_allocated;
3758 int n_comp_units;
3759 struct dwarf2_per_cu_data **all_comp_units;
3760 gdb_byte *info_ptr;
3761
3762 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3763 info_ptr = dwarf2_per_objfile->info.buffer;
3764
3765 n_comp_units = 0;
3766 n_allocated = 10;
3767 all_comp_units = xmalloc (n_allocated
3768 * sizeof (struct dwarf2_per_cu_data *));
3769
3770 while (info_ptr < dwarf2_per_objfile->info.buffer
3771 + dwarf2_per_objfile->info.size)
3772 {
3773 unsigned int length, initial_length_size;
3774 struct dwarf2_per_cu_data *this_cu;
3775 unsigned int offset;
3776
3777 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3778
3779 /* Read just enough information to find out where the next
3780 compilation unit is. */
3781 length = read_initial_length (objfile->obfd, info_ptr,
3782 &initial_length_size);
3783
3784 /* Save the compilation unit for later lookup. */
3785 this_cu = obstack_alloc (&objfile->objfile_obstack,
3786 sizeof (struct dwarf2_per_cu_data));
3787 memset (this_cu, 0, sizeof (*this_cu));
3788 this_cu->offset = offset;
3789 this_cu->length = length + initial_length_size;
3790 this_cu->objfile = objfile;
3791
3792 if (n_comp_units == n_allocated)
3793 {
3794 n_allocated *= 2;
3795 all_comp_units = xrealloc (all_comp_units,
3796 n_allocated
3797 * sizeof (struct dwarf2_per_cu_data *));
3798 }
3799 all_comp_units[n_comp_units++] = this_cu;
3800
3801 info_ptr = info_ptr + this_cu->length;
3802 }
3803
3804 dwarf2_per_objfile->all_comp_units
3805 = obstack_alloc (&objfile->objfile_obstack,
3806 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3807 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3808 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3809 xfree (all_comp_units);
3810 dwarf2_per_objfile->n_comp_units = n_comp_units;
3811 }
3812
3813 /* Process all loaded DIEs for compilation unit CU, starting at
3814 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3815 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3816 DW_AT_ranges). If NEED_PC is set, then this function will set
3817 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3818 and record the covered ranges in the addrmap. */
3819
3820 static void
3821 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3822 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3823 {
3824 struct partial_die_info *pdi;
3825
3826 /* Now, march along the PDI's, descending into ones which have
3827 interesting children but skipping the children of the other ones,
3828 until we reach the end of the compilation unit. */
3829
3830 pdi = first_die;
3831
3832 while (pdi != NULL)
3833 {
3834 fixup_partial_die (pdi, cu);
3835
3836 /* Anonymous namespaces or modules have no name but have interesting
3837 children, so we need to look at them. Ditto for anonymous
3838 enums. */
3839
3840 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3841 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3842 {
3843 switch (pdi->tag)
3844 {
3845 case DW_TAG_subprogram:
3846 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3847 break;
3848 case DW_TAG_constant:
3849 case DW_TAG_variable:
3850 case DW_TAG_typedef:
3851 case DW_TAG_union_type:
3852 if (!pdi->is_declaration)
3853 {
3854 add_partial_symbol (pdi, cu);
3855 }
3856 break;
3857 case DW_TAG_class_type:
3858 case DW_TAG_interface_type:
3859 case DW_TAG_structure_type:
3860 if (!pdi->is_declaration)
3861 {
3862 add_partial_symbol (pdi, cu);
3863 }
3864 break;
3865 case DW_TAG_enumeration_type:
3866 if (!pdi->is_declaration)
3867 add_partial_enumeration (pdi, cu);
3868 break;
3869 case DW_TAG_base_type:
3870 case DW_TAG_subrange_type:
3871 /* File scope base type definitions are added to the partial
3872 symbol table. */
3873 add_partial_symbol (pdi, cu);
3874 break;
3875 case DW_TAG_namespace:
3876 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3877 break;
3878 case DW_TAG_module:
3879 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3880 break;
3881 default:
3882 break;
3883 }
3884 }
3885
3886 /* If the die has a sibling, skip to the sibling. */
3887
3888 pdi = pdi->die_sibling;
3889 }
3890 }
3891
3892 /* Functions used to compute the fully scoped name of a partial DIE.
3893
3894 Normally, this is simple. For C++, the parent DIE's fully scoped
3895 name is concatenated with "::" and the partial DIE's name. For
3896 Java, the same thing occurs except that "." is used instead of "::".
3897 Enumerators are an exception; they use the scope of their parent
3898 enumeration type, i.e. the name of the enumeration type is not
3899 prepended to the enumerator.
3900
3901 There are two complexities. One is DW_AT_specification; in this
3902 case "parent" means the parent of the target of the specification,
3903 instead of the direct parent of the DIE. The other is compilers
3904 which do not emit DW_TAG_namespace; in this case we try to guess
3905 the fully qualified name of structure types from their members'
3906 linkage names. This must be done using the DIE's children rather
3907 than the children of any DW_AT_specification target. We only need
3908 to do this for structures at the top level, i.e. if the target of
3909 any DW_AT_specification (if any; otherwise the DIE itself) does not
3910 have a parent. */
3911
3912 /* Compute the scope prefix associated with PDI's parent, in
3913 compilation unit CU. The result will be allocated on CU's
3914 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3915 field. NULL is returned if no prefix is necessary. */
3916 static char *
3917 partial_die_parent_scope (struct partial_die_info *pdi,
3918 struct dwarf2_cu *cu)
3919 {
3920 char *grandparent_scope;
3921 struct partial_die_info *parent, *real_pdi;
3922
3923 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3924 then this means the parent of the specification DIE. */
3925
3926 real_pdi = pdi;
3927 while (real_pdi->has_specification)
3928 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3929
3930 parent = real_pdi->die_parent;
3931 if (parent == NULL)
3932 return NULL;
3933
3934 if (parent->scope_set)
3935 return parent->scope;
3936
3937 fixup_partial_die (parent, cu);
3938
3939 grandparent_scope = partial_die_parent_scope (parent, cu);
3940
3941 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3942 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3943 Work around this problem here. */
3944 if (cu->language == language_cplus
3945 && parent->tag == DW_TAG_namespace
3946 && strcmp (parent->name, "::") == 0
3947 && grandparent_scope == NULL)
3948 {
3949 parent->scope = NULL;
3950 parent->scope_set = 1;
3951 return NULL;
3952 }
3953
3954 if (pdi->tag == DW_TAG_enumerator)
3955 /* Enumerators should not get the name of the enumeration as a prefix. */
3956 parent->scope = grandparent_scope;
3957 else if (parent->tag == DW_TAG_namespace
3958 || parent->tag == DW_TAG_module
3959 || parent->tag == DW_TAG_structure_type
3960 || parent->tag == DW_TAG_class_type
3961 || parent->tag == DW_TAG_interface_type
3962 || parent->tag == DW_TAG_union_type
3963 || parent->tag == DW_TAG_enumeration_type)
3964 {
3965 if (grandparent_scope == NULL)
3966 parent->scope = parent->name;
3967 else
3968 parent->scope = typename_concat (&cu->comp_unit_obstack,
3969 grandparent_scope,
3970 parent->name, 0, cu);
3971 }
3972 else
3973 {
3974 /* FIXME drow/2004-04-01: What should we be doing with
3975 function-local names? For partial symbols, we should probably be
3976 ignoring them. */
3977 complaint (&symfile_complaints,
3978 _("unhandled containing DIE tag %d for DIE at %d"),
3979 parent->tag, pdi->offset);
3980 parent->scope = grandparent_scope;
3981 }
3982
3983 parent->scope_set = 1;
3984 return parent->scope;
3985 }
3986
3987 /* Return the fully scoped name associated with PDI, from compilation unit
3988 CU. The result will be allocated with malloc. */
3989 static char *
3990 partial_die_full_name (struct partial_die_info *pdi,
3991 struct dwarf2_cu *cu)
3992 {
3993 char *parent_scope;
3994
3995 /* If this is a template instantiation, we can not work out the
3996 template arguments from partial DIEs. So, unfortunately, we have
3997 to go through the full DIEs. At least any work we do building
3998 types here will be reused if full symbols are loaded later. */
3999 if (pdi->has_template_arguments)
4000 {
4001 fixup_partial_die (pdi, cu);
4002
4003 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4004 {
4005 struct die_info *die;
4006 struct attribute attr;
4007 struct dwarf2_cu *ref_cu = cu;
4008
4009 attr.name = 0;
4010 attr.form = DW_FORM_ref_addr;
4011 attr.u.addr = pdi->offset;
4012 die = follow_die_ref (NULL, &attr, &ref_cu);
4013
4014 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4015 }
4016 }
4017
4018 parent_scope = partial_die_parent_scope (pdi, cu);
4019 if (parent_scope == NULL)
4020 return NULL;
4021 else
4022 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4023 }
4024
4025 static void
4026 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4027 {
4028 struct objfile *objfile = cu->objfile;
4029 CORE_ADDR addr = 0;
4030 char *actual_name = NULL;
4031 const struct partial_symbol *psym = NULL;
4032 CORE_ADDR baseaddr;
4033 int built_actual_name = 0;
4034
4035 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4036
4037 actual_name = partial_die_full_name (pdi, cu);
4038 if (actual_name)
4039 built_actual_name = 1;
4040
4041 if (actual_name == NULL)
4042 actual_name = pdi->name;
4043
4044 switch (pdi->tag)
4045 {
4046 case DW_TAG_subprogram:
4047 if (pdi->is_external || cu->language == language_ada)
4048 {
4049 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4050 of the global scope. But in Ada, we want to be able to access
4051 nested procedures globally. So all Ada subprograms are stored
4052 in the global scope. */
4053 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4054 mst_text, objfile); */
4055 add_psymbol_to_list (actual_name, strlen (actual_name),
4056 built_actual_name,
4057 VAR_DOMAIN, LOC_BLOCK,
4058 &objfile->global_psymbols,
4059 0, pdi->lowpc + baseaddr,
4060 cu->language, objfile);
4061 }
4062 else
4063 {
4064 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4065 mst_file_text, objfile); */
4066 add_psymbol_to_list (actual_name, strlen (actual_name),
4067 built_actual_name,
4068 VAR_DOMAIN, LOC_BLOCK,
4069 &objfile->static_psymbols,
4070 0, pdi->lowpc + baseaddr,
4071 cu->language, objfile);
4072 }
4073 break;
4074 case DW_TAG_constant:
4075 {
4076 struct psymbol_allocation_list *list;
4077
4078 if (pdi->is_external)
4079 list = &objfile->global_psymbols;
4080 else
4081 list = &objfile->static_psymbols;
4082 add_psymbol_to_list (actual_name, strlen (actual_name),
4083 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4084 list, 0, 0, cu->language, objfile);
4085 }
4086 break;
4087 case DW_TAG_variable:
4088 if (pdi->locdesc)
4089 addr = decode_locdesc (pdi->locdesc, cu);
4090
4091 if (pdi->locdesc
4092 && addr == 0
4093 && !dwarf2_per_objfile->has_section_at_zero)
4094 {
4095 /* A global or static variable may also have been stripped
4096 out by the linker if unused, in which case its address
4097 will be nullified; do not add such variables into partial
4098 symbol table then. */
4099 }
4100 else if (pdi->is_external)
4101 {
4102 /* Global Variable.
4103 Don't enter into the minimal symbol tables as there is
4104 a minimal symbol table entry from the ELF symbols already.
4105 Enter into partial symbol table if it has a location
4106 descriptor or a type.
4107 If the location descriptor is missing, new_symbol will create
4108 a LOC_UNRESOLVED symbol, the address of the variable will then
4109 be determined from the minimal symbol table whenever the variable
4110 is referenced.
4111 The address for the partial symbol table entry is not
4112 used by GDB, but it comes in handy for debugging partial symbol
4113 table building. */
4114
4115 if (pdi->locdesc || pdi->has_type)
4116 add_psymbol_to_list (actual_name, strlen (actual_name),
4117 built_actual_name,
4118 VAR_DOMAIN, LOC_STATIC,
4119 &objfile->global_psymbols,
4120 0, addr + baseaddr,
4121 cu->language, objfile);
4122 }
4123 else
4124 {
4125 /* Static Variable. Skip symbols without location descriptors. */
4126 if (pdi->locdesc == NULL)
4127 {
4128 if (built_actual_name)
4129 xfree (actual_name);
4130 return;
4131 }
4132 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4133 mst_file_data, objfile); */
4134 add_psymbol_to_list (actual_name, strlen (actual_name),
4135 built_actual_name,
4136 VAR_DOMAIN, LOC_STATIC,
4137 &objfile->static_psymbols,
4138 0, addr + baseaddr,
4139 cu->language, objfile);
4140 }
4141 break;
4142 case DW_TAG_typedef:
4143 case DW_TAG_base_type:
4144 case DW_TAG_subrange_type:
4145 add_psymbol_to_list (actual_name, strlen (actual_name),
4146 built_actual_name,
4147 VAR_DOMAIN, LOC_TYPEDEF,
4148 &objfile->static_psymbols,
4149 0, (CORE_ADDR) 0, cu->language, objfile);
4150 break;
4151 case DW_TAG_namespace:
4152 add_psymbol_to_list (actual_name, strlen (actual_name),
4153 built_actual_name,
4154 VAR_DOMAIN, LOC_TYPEDEF,
4155 &objfile->global_psymbols,
4156 0, (CORE_ADDR) 0, cu->language, objfile);
4157 break;
4158 case DW_TAG_class_type:
4159 case DW_TAG_interface_type:
4160 case DW_TAG_structure_type:
4161 case DW_TAG_union_type:
4162 case DW_TAG_enumeration_type:
4163 /* Skip external references. The DWARF standard says in the section
4164 about "Structure, Union, and Class Type Entries": "An incomplete
4165 structure, union or class type is represented by a structure,
4166 union or class entry that does not have a byte size attribute
4167 and that has a DW_AT_declaration attribute." */
4168 if (!pdi->has_byte_size && pdi->is_declaration)
4169 {
4170 if (built_actual_name)
4171 xfree (actual_name);
4172 return;
4173 }
4174
4175 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4176 static vs. global. */
4177 add_psymbol_to_list (actual_name, strlen (actual_name),
4178 built_actual_name,
4179 STRUCT_DOMAIN, LOC_TYPEDEF,
4180 (cu->language == language_cplus
4181 || cu->language == language_java)
4182 ? &objfile->global_psymbols
4183 : &objfile->static_psymbols,
4184 0, (CORE_ADDR) 0, cu->language, objfile);
4185
4186 break;
4187 case DW_TAG_enumerator:
4188 add_psymbol_to_list (actual_name, strlen (actual_name),
4189 built_actual_name,
4190 VAR_DOMAIN, LOC_CONST,
4191 (cu->language == language_cplus
4192 || cu->language == language_java)
4193 ? &objfile->global_psymbols
4194 : &objfile->static_psymbols,
4195 0, (CORE_ADDR) 0, cu->language, objfile);
4196 break;
4197 default:
4198 break;
4199 }
4200
4201 if (built_actual_name)
4202 xfree (actual_name);
4203 }
4204
4205 /* Read a partial die corresponding to a namespace; also, add a symbol
4206 corresponding to that namespace to the symbol table. NAMESPACE is
4207 the name of the enclosing namespace. */
4208
4209 static void
4210 add_partial_namespace (struct partial_die_info *pdi,
4211 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4212 int need_pc, struct dwarf2_cu *cu)
4213 {
4214 /* Add a symbol for the namespace. */
4215
4216 add_partial_symbol (pdi, cu);
4217
4218 /* Now scan partial symbols in that namespace. */
4219
4220 if (pdi->has_children)
4221 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4222 }
4223
4224 /* Read a partial die corresponding to a Fortran module. */
4225
4226 static void
4227 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4228 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4229 {
4230 /* Now scan partial symbols in that module. */
4231
4232 if (pdi->has_children)
4233 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4234 }
4235
4236 /* Read a partial die corresponding to a subprogram and create a partial
4237 symbol for that subprogram. When the CU language allows it, this
4238 routine also defines a partial symbol for each nested subprogram
4239 that this subprogram contains.
4240
4241 DIE my also be a lexical block, in which case we simply search
4242 recursively for suprograms defined inside that lexical block.
4243 Again, this is only performed when the CU language allows this
4244 type of definitions. */
4245
4246 static void
4247 add_partial_subprogram (struct partial_die_info *pdi,
4248 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4249 int need_pc, struct dwarf2_cu *cu)
4250 {
4251 if (pdi->tag == DW_TAG_subprogram)
4252 {
4253 if (pdi->has_pc_info)
4254 {
4255 if (pdi->lowpc < *lowpc)
4256 *lowpc = pdi->lowpc;
4257 if (pdi->highpc > *highpc)
4258 *highpc = pdi->highpc;
4259 if (need_pc)
4260 {
4261 CORE_ADDR baseaddr;
4262 struct objfile *objfile = cu->objfile;
4263
4264 baseaddr = ANOFFSET (objfile->section_offsets,
4265 SECT_OFF_TEXT (objfile));
4266 addrmap_set_empty (objfile->psymtabs_addrmap,
4267 pdi->lowpc + baseaddr,
4268 pdi->highpc - 1 + baseaddr,
4269 cu->per_cu->v.psymtab);
4270 }
4271 if (!pdi->is_declaration)
4272 /* Ignore subprogram DIEs that do not have a name, they are
4273 illegal. Do not emit a complaint at this point, we will
4274 do so when we convert this psymtab into a symtab. */
4275 if (pdi->name)
4276 add_partial_symbol (pdi, cu);
4277 }
4278 }
4279
4280 if (! pdi->has_children)
4281 return;
4282
4283 if (cu->language == language_ada)
4284 {
4285 pdi = pdi->die_child;
4286 while (pdi != NULL)
4287 {
4288 fixup_partial_die (pdi, cu);
4289 if (pdi->tag == DW_TAG_subprogram
4290 || pdi->tag == DW_TAG_lexical_block)
4291 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4292 pdi = pdi->die_sibling;
4293 }
4294 }
4295 }
4296
4297 /* Read a partial die corresponding to an enumeration type. */
4298
4299 static void
4300 add_partial_enumeration (struct partial_die_info *enum_pdi,
4301 struct dwarf2_cu *cu)
4302 {
4303 struct partial_die_info *pdi;
4304
4305 if (enum_pdi->name != NULL)
4306 add_partial_symbol (enum_pdi, cu);
4307
4308 pdi = enum_pdi->die_child;
4309 while (pdi)
4310 {
4311 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4312 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4313 else
4314 add_partial_symbol (pdi, cu);
4315 pdi = pdi->die_sibling;
4316 }
4317 }
4318
4319 /* Return the initial uleb128 in the die at INFO_PTR. */
4320
4321 static unsigned int
4322 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4323 {
4324 unsigned int bytes_read;
4325
4326 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4327 }
4328
4329 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4330 Return the corresponding abbrev, or NULL if the number is zero (indicating
4331 an empty DIE). In either case *BYTES_READ will be set to the length of
4332 the initial number. */
4333
4334 static struct abbrev_info *
4335 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4336 struct dwarf2_cu *cu)
4337 {
4338 bfd *abfd = cu->objfile->obfd;
4339 unsigned int abbrev_number;
4340 struct abbrev_info *abbrev;
4341
4342 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4343
4344 if (abbrev_number == 0)
4345 return NULL;
4346
4347 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4348 if (!abbrev)
4349 {
4350 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4351 abbrev_number, bfd_get_filename (abfd));
4352 }
4353
4354 return abbrev;
4355 }
4356
4357 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4358 Returns a pointer to the end of a series of DIEs, terminated by an empty
4359 DIE. Any children of the skipped DIEs will also be skipped. */
4360
4361 static gdb_byte *
4362 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4363 {
4364 struct abbrev_info *abbrev;
4365 unsigned int bytes_read;
4366
4367 while (1)
4368 {
4369 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4370 if (abbrev == NULL)
4371 return info_ptr + bytes_read;
4372 else
4373 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4374 }
4375 }
4376
4377 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4378 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4379 abbrev corresponding to that skipped uleb128 should be passed in
4380 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4381 children. */
4382
4383 static gdb_byte *
4384 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4385 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4386 {
4387 unsigned int bytes_read;
4388 struct attribute attr;
4389 bfd *abfd = cu->objfile->obfd;
4390 unsigned int form, i;
4391
4392 for (i = 0; i < abbrev->num_attrs; i++)
4393 {
4394 /* The only abbrev we care about is DW_AT_sibling. */
4395 if (abbrev->attrs[i].name == DW_AT_sibling)
4396 {
4397 read_attribute (&attr, &abbrev->attrs[i],
4398 abfd, info_ptr, cu);
4399 if (attr.form == DW_FORM_ref_addr)
4400 complaint (&symfile_complaints,
4401 _("ignoring absolute DW_AT_sibling"));
4402 else
4403 return buffer + dwarf2_get_ref_die_offset (&attr);
4404 }
4405
4406 /* If it isn't DW_AT_sibling, skip this attribute. */
4407 form = abbrev->attrs[i].form;
4408 skip_attribute:
4409 switch (form)
4410 {
4411 case DW_FORM_ref_addr:
4412 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4413 and later it is offset sized. */
4414 if (cu->header.version == 2)
4415 info_ptr += cu->header.addr_size;
4416 else
4417 info_ptr += cu->header.offset_size;
4418 break;
4419 case DW_FORM_addr:
4420 info_ptr += cu->header.addr_size;
4421 break;
4422 case DW_FORM_data1:
4423 case DW_FORM_ref1:
4424 case DW_FORM_flag:
4425 info_ptr += 1;
4426 break;
4427 case DW_FORM_flag_present:
4428 break;
4429 case DW_FORM_data2:
4430 case DW_FORM_ref2:
4431 info_ptr += 2;
4432 break;
4433 case DW_FORM_data4:
4434 case DW_FORM_ref4:
4435 info_ptr += 4;
4436 break;
4437 case DW_FORM_data8:
4438 case DW_FORM_ref8:
4439 case DW_FORM_ref_sig8:
4440 info_ptr += 8;
4441 break;
4442 case DW_FORM_string:
4443 read_direct_string (abfd, info_ptr, &bytes_read);
4444 info_ptr += bytes_read;
4445 break;
4446 case DW_FORM_sec_offset:
4447 case DW_FORM_strp:
4448 info_ptr += cu->header.offset_size;
4449 break;
4450 case DW_FORM_exprloc:
4451 case DW_FORM_block:
4452 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4453 info_ptr += bytes_read;
4454 break;
4455 case DW_FORM_block1:
4456 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4457 break;
4458 case DW_FORM_block2:
4459 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4460 break;
4461 case DW_FORM_block4:
4462 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4463 break;
4464 case DW_FORM_sdata:
4465 case DW_FORM_udata:
4466 case DW_FORM_ref_udata:
4467 info_ptr = skip_leb128 (abfd, info_ptr);
4468 break;
4469 case DW_FORM_indirect:
4470 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4471 info_ptr += bytes_read;
4472 /* We need to continue parsing from here, so just go back to
4473 the top. */
4474 goto skip_attribute;
4475
4476 default:
4477 error (_("Dwarf Error: Cannot handle %s "
4478 "in DWARF reader [in module %s]"),
4479 dwarf_form_name (form),
4480 bfd_get_filename (abfd));
4481 }
4482 }
4483
4484 if (abbrev->has_children)
4485 return skip_children (buffer, info_ptr, cu);
4486 else
4487 return info_ptr;
4488 }
4489
4490 /* Locate ORIG_PDI's sibling.
4491 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4492 in BUFFER. */
4493
4494 static gdb_byte *
4495 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4496 gdb_byte *buffer, gdb_byte *info_ptr,
4497 bfd *abfd, struct dwarf2_cu *cu)
4498 {
4499 /* Do we know the sibling already? */
4500
4501 if (orig_pdi->sibling)
4502 return orig_pdi->sibling;
4503
4504 /* Are there any children to deal with? */
4505
4506 if (!orig_pdi->has_children)
4507 return info_ptr;
4508
4509 /* Skip the children the long way. */
4510
4511 return skip_children (buffer, info_ptr, cu);
4512 }
4513
4514 /* Expand this partial symbol table into a full symbol table. */
4515
4516 static void
4517 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4518 {
4519 if (pst != NULL)
4520 {
4521 if (pst->readin)
4522 {
4523 warning (_("bug: psymtab for %s is already read in."),
4524 pst->filename);
4525 }
4526 else
4527 {
4528 if (info_verbose)
4529 {
4530 printf_filtered (_("Reading in symbols for %s..."),
4531 pst->filename);
4532 gdb_flush (gdb_stdout);
4533 }
4534
4535 /* Restore our global data. */
4536 dwarf2_per_objfile = objfile_data (pst->objfile,
4537 dwarf2_objfile_data_key);
4538
4539 /* If this psymtab is constructed from a debug-only objfile, the
4540 has_section_at_zero flag will not necessarily be correct. We
4541 can get the correct value for this flag by looking at the data
4542 associated with the (presumably stripped) associated objfile. */
4543 if (pst->objfile->separate_debug_objfile_backlink)
4544 {
4545 struct dwarf2_per_objfile *dpo_backlink
4546 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4547 dwarf2_objfile_data_key);
4548
4549 dwarf2_per_objfile->has_section_at_zero
4550 = dpo_backlink->has_section_at_zero;
4551 }
4552
4553 dwarf2_per_objfile->reading_partial_symbols = 0;
4554
4555 psymtab_to_symtab_1 (pst);
4556
4557 /* Finish up the debug error message. */
4558 if (info_verbose)
4559 printf_filtered (_("done.\n"));
4560 }
4561 }
4562 }
4563 \f
4564 /* Reading in full CUs. */
4565
4566 /* Add PER_CU to the queue. */
4567
4568 static void
4569 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4570 {
4571 struct dwarf2_queue_item *item;
4572
4573 per_cu->queued = 1;
4574 item = xmalloc (sizeof (*item));
4575 item->per_cu = per_cu;
4576 item->next = NULL;
4577
4578 if (dwarf2_queue == NULL)
4579 dwarf2_queue = item;
4580 else
4581 dwarf2_queue_tail->next = item;
4582
4583 dwarf2_queue_tail = item;
4584 }
4585
4586 /* Process the queue. */
4587
4588 static void
4589 process_queue (void)
4590 {
4591 struct dwarf2_queue_item *item, *next_item;
4592
4593 /* The queue starts out with one item, but following a DIE reference
4594 may load a new CU, adding it to the end of the queue. */
4595 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4596 {
4597 if (dwarf2_per_objfile->using_index
4598 ? !item->per_cu->v.quick->symtab
4599 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4600 process_full_comp_unit (item->per_cu);
4601
4602 item->per_cu->queued = 0;
4603 next_item = item->next;
4604 xfree (item);
4605 }
4606
4607 dwarf2_queue_tail = NULL;
4608 }
4609
4610 /* Free all allocated queue entries. This function only releases anything if
4611 an error was thrown; if the queue was processed then it would have been
4612 freed as we went along. */
4613
4614 static void
4615 dwarf2_release_queue (void *dummy)
4616 {
4617 struct dwarf2_queue_item *item, *last;
4618
4619 item = dwarf2_queue;
4620 while (item)
4621 {
4622 /* Anything still marked queued is likely to be in an
4623 inconsistent state, so discard it. */
4624 if (item->per_cu->queued)
4625 {
4626 if (item->per_cu->cu != NULL)
4627 free_one_cached_comp_unit (item->per_cu->cu);
4628 item->per_cu->queued = 0;
4629 }
4630
4631 last = item;
4632 item = item->next;
4633 xfree (last);
4634 }
4635
4636 dwarf2_queue = dwarf2_queue_tail = NULL;
4637 }
4638
4639 /* Read in full symbols for PST, and anything it depends on. */
4640
4641 static void
4642 psymtab_to_symtab_1 (struct partial_symtab *pst)
4643 {
4644 struct dwarf2_per_cu_data *per_cu;
4645 struct cleanup *back_to;
4646 int i;
4647
4648 for (i = 0; i < pst->number_of_dependencies; i++)
4649 if (!pst->dependencies[i]->readin)
4650 {
4651 /* Inform about additional files that need to be read in. */
4652 if (info_verbose)
4653 {
4654 /* FIXME: i18n: Need to make this a single string. */
4655 fputs_filtered (" ", gdb_stdout);
4656 wrap_here ("");
4657 fputs_filtered ("and ", gdb_stdout);
4658 wrap_here ("");
4659 printf_filtered ("%s...", pst->dependencies[i]->filename);
4660 wrap_here (""); /* Flush output. */
4661 gdb_flush (gdb_stdout);
4662 }
4663 psymtab_to_symtab_1 (pst->dependencies[i]);
4664 }
4665
4666 per_cu = pst->read_symtab_private;
4667
4668 if (per_cu == NULL)
4669 {
4670 /* It's an include file, no symbols to read for it.
4671 Everything is in the parent symtab. */
4672 pst->readin = 1;
4673 return;
4674 }
4675
4676 dw2_do_instantiate_symtab (per_cu);
4677 }
4678
4679 /* Load the DIEs associated with PER_CU into memory. */
4680
4681 static void
4682 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4683 {
4684 struct objfile *objfile = per_cu->objfile;
4685 bfd *abfd = objfile->obfd;
4686 struct dwarf2_cu *cu;
4687 unsigned int offset;
4688 gdb_byte *info_ptr, *beg_of_comp_unit;
4689 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4690 struct attribute *attr;
4691 int read_cu = 0;
4692
4693 gdb_assert (! per_cu->debug_types_section);
4694
4695 /* Set local variables from the partial symbol table info. */
4696 offset = per_cu->offset;
4697
4698 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4699 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4700 beg_of_comp_unit = info_ptr;
4701
4702 if (per_cu->cu == NULL)
4703 {
4704 cu = xmalloc (sizeof (*cu));
4705 init_one_comp_unit (cu, per_cu);
4706
4707 read_cu = 1;
4708
4709 /* If an error occurs while loading, release our storage. */
4710 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4711
4712 /* Read in the comp_unit header. */
4713 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4714
4715 /* Skip dummy compilation units. */
4716 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4717 + dwarf2_per_objfile->info.size)
4718 || peek_abbrev_code (abfd, info_ptr) == 0)
4719 {
4720 do_cleanups (free_cu_cleanup);
4721 return;
4722 }
4723
4724 /* Complete the cu_header. */
4725 cu->header.offset = offset;
4726 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4727
4728 /* Read the abbrevs for this compilation unit. */
4729 dwarf2_read_abbrevs (cu);
4730 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4731
4732 /* Link this CU into read_in_chain. */
4733 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4734 dwarf2_per_objfile->read_in_chain = per_cu;
4735 }
4736 else
4737 {
4738 cu = per_cu->cu;
4739 info_ptr += cu->header.first_die_offset;
4740 }
4741
4742 cu->dies = read_comp_unit (info_ptr, cu);
4743
4744 /* We try not to read any attributes in this function, because not
4745 all CUs needed for references have been loaded yet, and symbol
4746 table processing isn't initialized. But we have to set the CU language,
4747 or we won't be able to build types correctly. */
4748 prepare_one_comp_unit (cu, cu->dies);
4749
4750 /* Similarly, if we do not read the producer, we can not apply
4751 producer-specific interpretation. */
4752 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4753 if (attr)
4754 cu->producer = DW_STRING (attr);
4755
4756 if (read_cu)
4757 {
4758 do_cleanups (free_abbrevs_cleanup);
4759
4760 /* We've successfully allocated this compilation unit. Let our
4761 caller clean it up when finished with it. */
4762 discard_cleanups (free_cu_cleanup);
4763 }
4764 }
4765
4766 /* Add a DIE to the delayed physname list. */
4767
4768 static void
4769 add_to_method_list (struct type *type, int fnfield_index, int index,
4770 const char *name, struct die_info *die,
4771 struct dwarf2_cu *cu)
4772 {
4773 struct delayed_method_info mi;
4774 mi.type = type;
4775 mi.fnfield_index = fnfield_index;
4776 mi.index = index;
4777 mi.name = name;
4778 mi.die = die;
4779 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4780 }
4781
4782 /* A cleanup for freeing the delayed method list. */
4783
4784 static void
4785 free_delayed_list (void *ptr)
4786 {
4787 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4788 if (cu->method_list != NULL)
4789 {
4790 VEC_free (delayed_method_info, cu->method_list);
4791 cu->method_list = NULL;
4792 }
4793 }
4794
4795 /* Compute the physnames of any methods on the CU's method list.
4796
4797 The computation of method physnames is delayed in order to avoid the
4798 (bad) condition that one of the method's formal parameters is of an as yet
4799 incomplete type. */
4800
4801 static void
4802 compute_delayed_physnames (struct dwarf2_cu *cu)
4803 {
4804 int i;
4805 struct delayed_method_info *mi;
4806 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4807 {
4808 const char *physname;
4809 struct fn_fieldlist *fn_flp
4810 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4811 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4812 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4813 }
4814 }
4815
4816 /* Generate full symbol information for PER_CU, whose DIEs have
4817 already been loaded into memory. */
4818
4819 static void
4820 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4821 {
4822 struct dwarf2_cu *cu = per_cu->cu;
4823 struct objfile *objfile = per_cu->objfile;
4824 CORE_ADDR lowpc, highpc;
4825 struct symtab *symtab;
4826 struct cleanup *back_to, *delayed_list_cleanup;
4827 CORE_ADDR baseaddr;
4828
4829 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4830
4831 buildsym_init ();
4832 back_to = make_cleanup (really_free_pendings, NULL);
4833 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4834
4835 cu->list_in_scope = &file_symbols;
4836
4837 /* Do line number decoding in read_file_scope () */
4838 process_die (cu->dies, cu);
4839
4840 /* Now that we have processed all the DIEs in the CU, all the types
4841 should be complete, and it should now be safe to compute all of the
4842 physnames. */
4843 compute_delayed_physnames (cu);
4844 do_cleanups (delayed_list_cleanup);
4845
4846 /* Some compilers don't define a DW_AT_high_pc attribute for the
4847 compilation unit. If the DW_AT_high_pc is missing, synthesize
4848 it, by scanning the DIE's below the compilation unit. */
4849 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4850
4851 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4852
4853 if (symtab != NULL)
4854 {
4855 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4856
4857 /* Set symtab language to language from DW_AT_language. If the
4858 compilation is from a C file generated by language preprocessors, do
4859 not set the language if it was already deduced by start_subfile. */
4860 if (!(cu->language == language_c && symtab->language != language_c))
4861 symtab->language = cu->language;
4862
4863 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4864 produce DW_AT_location with location lists but it can be possibly
4865 invalid without -fvar-tracking.
4866
4867 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4868 needed, it would be wrong due to missing DW_AT_producer there.
4869
4870 Still one can confuse GDB by using non-standard GCC compilation
4871 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4872 */
4873 if (cu->has_loclist && gcc_4_minor >= 0)
4874 symtab->locations_valid = 1;
4875
4876 if (gcc_4_minor >= 5)
4877 symtab->epilogue_unwind_valid = 1;
4878
4879 symtab->call_site_htab = cu->call_site_htab;
4880 }
4881
4882 if (dwarf2_per_objfile->using_index)
4883 per_cu->v.quick->symtab = symtab;
4884 else
4885 {
4886 struct partial_symtab *pst = per_cu->v.psymtab;
4887 pst->symtab = symtab;
4888 pst->readin = 1;
4889 }
4890
4891 do_cleanups (back_to);
4892 }
4893
4894 /* Process a die and its children. */
4895
4896 static void
4897 process_die (struct die_info *die, struct dwarf2_cu *cu)
4898 {
4899 switch (die->tag)
4900 {
4901 case DW_TAG_padding:
4902 break;
4903 case DW_TAG_compile_unit:
4904 read_file_scope (die, cu);
4905 break;
4906 case DW_TAG_type_unit:
4907 read_type_unit_scope (die, cu);
4908 break;
4909 case DW_TAG_subprogram:
4910 case DW_TAG_inlined_subroutine:
4911 read_func_scope (die, cu);
4912 break;
4913 case DW_TAG_lexical_block:
4914 case DW_TAG_try_block:
4915 case DW_TAG_catch_block:
4916 read_lexical_block_scope (die, cu);
4917 break;
4918 case DW_TAG_GNU_call_site:
4919 read_call_site_scope (die, cu);
4920 break;
4921 case DW_TAG_class_type:
4922 case DW_TAG_interface_type:
4923 case DW_TAG_structure_type:
4924 case DW_TAG_union_type:
4925 process_structure_scope (die, cu);
4926 break;
4927 case DW_TAG_enumeration_type:
4928 process_enumeration_scope (die, cu);
4929 break;
4930
4931 /* These dies have a type, but processing them does not create
4932 a symbol or recurse to process the children. Therefore we can
4933 read them on-demand through read_type_die. */
4934 case DW_TAG_subroutine_type:
4935 case DW_TAG_set_type:
4936 case DW_TAG_array_type:
4937 case DW_TAG_pointer_type:
4938 case DW_TAG_ptr_to_member_type:
4939 case DW_TAG_reference_type:
4940 case DW_TAG_string_type:
4941 break;
4942
4943 case DW_TAG_base_type:
4944 case DW_TAG_subrange_type:
4945 case DW_TAG_typedef:
4946 /* Add a typedef symbol for the type definition, if it has a
4947 DW_AT_name. */
4948 new_symbol (die, read_type_die (die, cu), cu);
4949 break;
4950 case DW_TAG_common_block:
4951 read_common_block (die, cu);
4952 break;
4953 case DW_TAG_common_inclusion:
4954 break;
4955 case DW_TAG_namespace:
4956 processing_has_namespace_info = 1;
4957 read_namespace (die, cu);
4958 break;
4959 case DW_TAG_module:
4960 processing_has_namespace_info = 1;
4961 read_module (die, cu);
4962 break;
4963 case DW_TAG_imported_declaration:
4964 case DW_TAG_imported_module:
4965 processing_has_namespace_info = 1;
4966 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4967 || cu->language != language_fortran))
4968 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4969 dwarf_tag_name (die->tag));
4970 read_import_statement (die, cu);
4971 break;
4972 default:
4973 new_symbol (die, NULL, cu);
4974 break;
4975 }
4976 }
4977
4978 /* A helper function for dwarf2_compute_name which determines whether DIE
4979 needs to have the name of the scope prepended to the name listed in the
4980 die. */
4981
4982 static int
4983 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4984 {
4985 struct attribute *attr;
4986
4987 switch (die->tag)
4988 {
4989 case DW_TAG_namespace:
4990 case DW_TAG_typedef:
4991 case DW_TAG_class_type:
4992 case DW_TAG_interface_type:
4993 case DW_TAG_structure_type:
4994 case DW_TAG_union_type:
4995 case DW_TAG_enumeration_type:
4996 case DW_TAG_enumerator:
4997 case DW_TAG_subprogram:
4998 case DW_TAG_member:
4999 return 1;
5000
5001 case DW_TAG_variable:
5002 case DW_TAG_constant:
5003 /* We only need to prefix "globally" visible variables. These include
5004 any variable marked with DW_AT_external or any variable that
5005 lives in a namespace. [Variables in anonymous namespaces
5006 require prefixing, but they are not DW_AT_external.] */
5007
5008 if (dwarf2_attr (die, DW_AT_specification, cu))
5009 {
5010 struct dwarf2_cu *spec_cu = cu;
5011
5012 return die_needs_namespace (die_specification (die, &spec_cu),
5013 spec_cu);
5014 }
5015
5016 attr = dwarf2_attr (die, DW_AT_external, cu);
5017 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5018 && die->parent->tag != DW_TAG_module)
5019 return 0;
5020 /* A variable in a lexical block of some kind does not need a
5021 namespace, even though in C++ such variables may be external
5022 and have a mangled name. */
5023 if (die->parent->tag == DW_TAG_lexical_block
5024 || die->parent->tag == DW_TAG_try_block
5025 || die->parent->tag == DW_TAG_catch_block
5026 || die->parent->tag == DW_TAG_subprogram)
5027 return 0;
5028 return 1;
5029
5030 default:
5031 return 0;
5032 }
5033 }
5034
5035 /* Retrieve the last character from a mem_file. */
5036
5037 static void
5038 do_ui_file_peek_last (void *object, const char *buffer, long length)
5039 {
5040 char *last_char_p = (char *) object;
5041
5042 if (length > 0)
5043 *last_char_p = buffer[length - 1];
5044 }
5045
5046 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5047 compute the physname for the object, which include a method's
5048 formal parameters (C++/Java) and return type (Java).
5049
5050 For Ada, return the DIE's linkage name rather than the fully qualified
5051 name. PHYSNAME is ignored..
5052
5053 The result is allocated on the objfile_obstack and canonicalized. */
5054
5055 static const char *
5056 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5057 int physname)
5058 {
5059 struct objfile *objfile = cu->objfile;
5060
5061 if (name == NULL)
5062 name = dwarf2_name (die, cu);
5063
5064 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5065 compute it by typename_concat inside GDB. */
5066 if (cu->language == language_ada
5067 || (cu->language == language_fortran && physname))
5068 {
5069 /* For Ada unit, we prefer the linkage name over the name, as
5070 the former contains the exported name, which the user expects
5071 to be able to reference. Ideally, we want the user to be able
5072 to reference this entity using either natural or linkage name,
5073 but we haven't started looking at this enhancement yet. */
5074 struct attribute *attr;
5075
5076 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5077 if (attr == NULL)
5078 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5079 if (attr && DW_STRING (attr))
5080 return DW_STRING (attr);
5081 }
5082
5083 /* These are the only languages we know how to qualify names in. */
5084 if (name != NULL
5085 && (cu->language == language_cplus || cu->language == language_java
5086 || cu->language == language_fortran))
5087 {
5088 if (die_needs_namespace (die, cu))
5089 {
5090 long length;
5091 char *prefix;
5092 struct ui_file *buf;
5093
5094 prefix = determine_prefix (die, cu);
5095 buf = mem_fileopen ();
5096 if (*prefix != '\0')
5097 {
5098 char *prefixed_name = typename_concat (NULL, prefix, name,
5099 physname, cu);
5100
5101 fputs_unfiltered (prefixed_name, buf);
5102 xfree (prefixed_name);
5103 }
5104 else
5105 fputs_unfiltered (name, buf);
5106
5107 /* Template parameters may be specified in the DIE's DW_AT_name, or
5108 as children with DW_TAG_template_type_param or
5109 DW_TAG_value_type_param. If the latter, add them to the name
5110 here. If the name already has template parameters, then
5111 skip this step; some versions of GCC emit both, and
5112 it is more efficient to use the pre-computed name.
5113
5114 Something to keep in mind about this process: it is very
5115 unlikely, or in some cases downright impossible, to produce
5116 something that will match the mangled name of a function.
5117 If the definition of the function has the same debug info,
5118 we should be able to match up with it anyway. But fallbacks
5119 using the minimal symbol, for instance to find a method
5120 implemented in a stripped copy of libstdc++, will not work.
5121 If we do not have debug info for the definition, we will have to
5122 match them up some other way.
5123
5124 When we do name matching there is a related problem with function
5125 templates; two instantiated function templates are allowed to
5126 differ only by their return types, which we do not add here. */
5127
5128 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5129 {
5130 struct attribute *attr;
5131 struct die_info *child;
5132 int first = 1;
5133
5134 die->building_fullname = 1;
5135
5136 for (child = die->child; child != NULL; child = child->sibling)
5137 {
5138 struct type *type;
5139 long value;
5140 gdb_byte *bytes;
5141 struct dwarf2_locexpr_baton *baton;
5142 struct value *v;
5143
5144 if (child->tag != DW_TAG_template_type_param
5145 && child->tag != DW_TAG_template_value_param)
5146 continue;
5147
5148 if (first)
5149 {
5150 fputs_unfiltered ("<", buf);
5151 first = 0;
5152 }
5153 else
5154 fputs_unfiltered (", ", buf);
5155
5156 attr = dwarf2_attr (child, DW_AT_type, cu);
5157 if (attr == NULL)
5158 {
5159 complaint (&symfile_complaints,
5160 _("template parameter missing DW_AT_type"));
5161 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5162 continue;
5163 }
5164 type = die_type (child, cu);
5165
5166 if (child->tag == DW_TAG_template_type_param)
5167 {
5168 c_print_type (type, "", buf, -1, 0);
5169 continue;
5170 }
5171
5172 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5173 if (attr == NULL)
5174 {
5175 complaint (&symfile_complaints,
5176 _("template parameter missing "
5177 "DW_AT_const_value"));
5178 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5179 continue;
5180 }
5181
5182 dwarf2_const_value_attr (attr, type, name,
5183 &cu->comp_unit_obstack, cu,
5184 &value, &bytes, &baton);
5185
5186 if (TYPE_NOSIGN (type))
5187 /* GDB prints characters as NUMBER 'CHAR'. If that's
5188 changed, this can use value_print instead. */
5189 c_printchar (value, type, buf);
5190 else
5191 {
5192 struct value_print_options opts;
5193
5194 if (baton != NULL)
5195 v = dwarf2_evaluate_loc_desc (type, NULL,
5196 baton->data,
5197 baton->size,
5198 baton->per_cu);
5199 else if (bytes != NULL)
5200 {
5201 v = allocate_value (type);
5202 memcpy (value_contents_writeable (v), bytes,
5203 TYPE_LENGTH (type));
5204 }
5205 else
5206 v = value_from_longest (type, value);
5207
5208 /* Specify decimal so that we do not depend on
5209 the radix. */
5210 get_formatted_print_options (&opts, 'd');
5211 opts.raw = 1;
5212 value_print (v, buf, &opts);
5213 release_value (v);
5214 value_free (v);
5215 }
5216 }
5217
5218 die->building_fullname = 0;
5219
5220 if (!first)
5221 {
5222 /* Close the argument list, with a space if necessary
5223 (nested templates). */
5224 char last_char = '\0';
5225 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5226 if (last_char == '>')
5227 fputs_unfiltered (" >", buf);
5228 else
5229 fputs_unfiltered (">", buf);
5230 }
5231 }
5232
5233 /* For Java and C++ methods, append formal parameter type
5234 information, if PHYSNAME. */
5235
5236 if (physname && die->tag == DW_TAG_subprogram
5237 && (cu->language == language_cplus
5238 || cu->language == language_java))
5239 {
5240 struct type *type = read_type_die (die, cu);
5241
5242 c_type_print_args (type, buf, 1, cu->language);
5243
5244 if (cu->language == language_java)
5245 {
5246 /* For java, we must append the return type to method
5247 names. */
5248 if (die->tag == DW_TAG_subprogram)
5249 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5250 0, 0);
5251 }
5252 else if (cu->language == language_cplus)
5253 {
5254 /* Assume that an artificial first parameter is
5255 "this", but do not crash if it is not. RealView
5256 marks unnamed (and thus unused) parameters as
5257 artificial; there is no way to differentiate
5258 the two cases. */
5259 if (TYPE_NFIELDS (type) > 0
5260 && TYPE_FIELD_ARTIFICIAL (type, 0)
5261 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5262 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5263 0))))
5264 fputs_unfiltered (" const", buf);
5265 }
5266 }
5267
5268 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5269 &length);
5270 ui_file_delete (buf);
5271
5272 if (cu->language == language_cplus)
5273 {
5274 char *cname
5275 = dwarf2_canonicalize_name (name, cu,
5276 &objfile->objfile_obstack);
5277
5278 if (cname != NULL)
5279 name = cname;
5280 }
5281 }
5282 }
5283
5284 return name;
5285 }
5286
5287 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5288 If scope qualifiers are appropriate they will be added. The result
5289 will be allocated on the objfile_obstack, or NULL if the DIE does
5290 not have a name. NAME may either be from a previous call to
5291 dwarf2_name or NULL.
5292
5293 The output string will be canonicalized (if C++/Java). */
5294
5295 static const char *
5296 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5297 {
5298 return dwarf2_compute_name (name, die, cu, 0);
5299 }
5300
5301 /* Construct a physname for the given DIE in CU. NAME may either be
5302 from a previous call to dwarf2_name or NULL. The result will be
5303 allocated on the objfile_objstack or NULL if the DIE does not have a
5304 name.
5305
5306 The output string will be canonicalized (if C++/Java). */
5307
5308 static const char *
5309 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5310 {
5311 struct objfile *objfile = cu->objfile;
5312 struct attribute *attr;
5313 const char *retval, *mangled = NULL, *canon = NULL;
5314 struct cleanup *back_to;
5315 int need_copy = 1;
5316
5317 /* In this case dwarf2_compute_name is just a shortcut not building anything
5318 on its own. */
5319 if (!die_needs_namespace (die, cu))
5320 return dwarf2_compute_name (name, die, cu, 1);
5321
5322 back_to = make_cleanup (null_cleanup, NULL);
5323
5324 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5325 if (!attr)
5326 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5327
5328 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5329 has computed. */
5330 if (attr && DW_STRING (attr))
5331 {
5332 char *demangled;
5333
5334 mangled = DW_STRING (attr);
5335
5336 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5337 type. It is easier for GDB users to search for such functions as
5338 `name(params)' than `long name(params)'. In such case the minimal
5339 symbol names do not match the full symbol names but for template
5340 functions there is never a need to look up their definition from their
5341 declaration so the only disadvantage remains the minimal symbol
5342 variant `long name(params)' does not have the proper inferior type.
5343 */
5344
5345 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5346 | (cu->language == language_java
5347 ? DMGL_JAVA | DMGL_RET_POSTFIX
5348 : DMGL_RET_DROP)));
5349 if (demangled)
5350 {
5351 make_cleanup (xfree, demangled);
5352 canon = demangled;
5353 }
5354 else
5355 {
5356 canon = mangled;
5357 need_copy = 0;
5358 }
5359 }
5360
5361 if (canon == NULL || check_physname)
5362 {
5363 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5364
5365 if (canon != NULL && strcmp (physname, canon) != 0)
5366 {
5367 /* It may not mean a bug in GDB. The compiler could also
5368 compute DW_AT_linkage_name incorrectly. But in such case
5369 GDB would need to be bug-to-bug compatible. */
5370
5371 complaint (&symfile_complaints,
5372 _("Computed physname <%s> does not match demangled <%s> "
5373 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5374 physname, canon, mangled, die->offset, objfile->name);
5375
5376 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5377 is available here - over computed PHYSNAME. It is safer
5378 against both buggy GDB and buggy compilers. */
5379
5380 retval = canon;
5381 }
5382 else
5383 {
5384 retval = physname;
5385 need_copy = 0;
5386 }
5387 }
5388 else
5389 retval = canon;
5390
5391 if (need_copy)
5392 retval = obsavestring (retval, strlen (retval),
5393 &objfile->objfile_obstack);
5394
5395 do_cleanups (back_to);
5396 return retval;
5397 }
5398
5399 /* Read the import statement specified by the given die and record it. */
5400
5401 static void
5402 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5403 {
5404 struct objfile *objfile = cu->objfile;
5405 struct attribute *import_attr;
5406 struct die_info *imported_die, *child_die;
5407 struct dwarf2_cu *imported_cu;
5408 const char *imported_name;
5409 const char *imported_name_prefix;
5410 const char *canonical_name;
5411 const char *import_alias;
5412 const char *imported_declaration = NULL;
5413 const char *import_prefix;
5414 VEC (const_char_ptr) *excludes = NULL;
5415 struct cleanup *cleanups;
5416
5417 char *temp;
5418
5419 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5420 if (import_attr == NULL)
5421 {
5422 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5423 dwarf_tag_name (die->tag));
5424 return;
5425 }
5426
5427 imported_cu = cu;
5428 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5429 imported_name = dwarf2_name (imported_die, imported_cu);
5430 if (imported_name == NULL)
5431 {
5432 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5433
5434 The import in the following code:
5435 namespace A
5436 {
5437 typedef int B;
5438 }
5439
5440 int main ()
5441 {
5442 using A::B;
5443 B b;
5444 return b;
5445 }
5446
5447 ...
5448 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5449 <52> DW_AT_decl_file : 1
5450 <53> DW_AT_decl_line : 6
5451 <54> DW_AT_import : <0x75>
5452 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5453 <59> DW_AT_name : B
5454 <5b> DW_AT_decl_file : 1
5455 <5c> DW_AT_decl_line : 2
5456 <5d> DW_AT_type : <0x6e>
5457 ...
5458 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5459 <76> DW_AT_byte_size : 4
5460 <77> DW_AT_encoding : 5 (signed)
5461
5462 imports the wrong die ( 0x75 instead of 0x58 ).
5463 This case will be ignored until the gcc bug is fixed. */
5464 return;
5465 }
5466
5467 /* Figure out the local name after import. */
5468 import_alias = dwarf2_name (die, cu);
5469
5470 /* Figure out where the statement is being imported to. */
5471 import_prefix = determine_prefix (die, cu);
5472
5473 /* Figure out what the scope of the imported die is and prepend it
5474 to the name of the imported die. */
5475 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5476
5477 if (imported_die->tag != DW_TAG_namespace
5478 && imported_die->tag != DW_TAG_module)
5479 {
5480 imported_declaration = imported_name;
5481 canonical_name = imported_name_prefix;
5482 }
5483 else if (strlen (imported_name_prefix) > 0)
5484 {
5485 temp = alloca (strlen (imported_name_prefix)
5486 + 2 + strlen (imported_name) + 1);
5487 strcpy (temp, imported_name_prefix);
5488 strcat (temp, "::");
5489 strcat (temp, imported_name);
5490 canonical_name = temp;
5491 }
5492 else
5493 canonical_name = imported_name;
5494
5495 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5496
5497 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5498 for (child_die = die->child; child_die && child_die->tag;
5499 child_die = sibling_die (child_die))
5500 {
5501 /* DWARF-4: A Fortran use statement with a “rename list” may be
5502 represented by an imported module entry with an import attribute
5503 referring to the module and owned entries corresponding to those
5504 entities that are renamed as part of being imported. */
5505
5506 if (child_die->tag != DW_TAG_imported_declaration)
5507 {
5508 complaint (&symfile_complaints,
5509 _("child DW_TAG_imported_declaration expected "
5510 "- DIE at 0x%x [in module %s]"),
5511 child_die->offset, objfile->name);
5512 continue;
5513 }
5514
5515 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5516 if (import_attr == NULL)
5517 {
5518 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5519 dwarf_tag_name (child_die->tag));
5520 continue;
5521 }
5522
5523 imported_cu = cu;
5524 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5525 &imported_cu);
5526 imported_name = dwarf2_name (imported_die, imported_cu);
5527 if (imported_name == NULL)
5528 {
5529 complaint (&symfile_complaints,
5530 _("child DW_TAG_imported_declaration has unknown "
5531 "imported name - DIE at 0x%x [in module %s]"),
5532 child_die->offset, objfile->name);
5533 continue;
5534 }
5535
5536 VEC_safe_push (const_char_ptr, excludes, imported_name);
5537
5538 process_die (child_die, cu);
5539 }
5540
5541 cp_add_using_directive (import_prefix,
5542 canonical_name,
5543 import_alias,
5544 imported_declaration,
5545 excludes,
5546 &objfile->objfile_obstack);
5547
5548 do_cleanups (cleanups);
5549 }
5550
5551 /* Cleanup function for read_file_scope. */
5552
5553 static void
5554 free_cu_line_header (void *arg)
5555 {
5556 struct dwarf2_cu *cu = arg;
5557
5558 free_line_header (cu->line_header);
5559 cu->line_header = NULL;
5560 }
5561
5562 static void
5563 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5564 char **name, char **comp_dir)
5565 {
5566 struct attribute *attr;
5567
5568 *name = NULL;
5569 *comp_dir = NULL;
5570
5571 /* Find the filename. Do not use dwarf2_name here, since the filename
5572 is not a source language identifier. */
5573 attr = dwarf2_attr (die, DW_AT_name, cu);
5574 if (attr)
5575 {
5576 *name = DW_STRING (attr);
5577 }
5578
5579 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5580 if (attr)
5581 *comp_dir = DW_STRING (attr);
5582 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5583 {
5584 *comp_dir = ldirname (*name);
5585 if (*comp_dir != NULL)
5586 make_cleanup (xfree, *comp_dir);
5587 }
5588 if (*comp_dir != NULL)
5589 {
5590 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5591 directory, get rid of it. */
5592 char *cp = strchr (*comp_dir, ':');
5593
5594 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5595 *comp_dir = cp + 1;
5596 }
5597
5598 if (*name == NULL)
5599 *name = "<unknown>";
5600 }
5601
5602 /* Handle DW_AT_stmt_list for a compilation unit. */
5603
5604 static void
5605 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5606 const char *comp_dir)
5607 {
5608 struct attribute *attr;
5609 struct objfile *objfile = cu->objfile;
5610 bfd *abfd = objfile->obfd;
5611
5612 /* Decode line number information if present. We do this before
5613 processing child DIEs, so that the line header table is available
5614 for DW_AT_decl_file. */
5615 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5616 if (attr)
5617 {
5618 unsigned int line_offset = DW_UNSND (attr);
5619 struct line_header *line_header
5620 = dwarf_decode_line_header (line_offset, abfd, cu);
5621
5622 if (line_header)
5623 {
5624 cu->line_header = line_header;
5625 make_cleanup (free_cu_line_header, cu);
5626 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5627 }
5628 }
5629 }
5630
5631 /* Process DW_TAG_compile_unit. */
5632
5633 static void
5634 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5635 {
5636 struct objfile *objfile = cu->objfile;
5637 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5638 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5639 CORE_ADDR highpc = ((CORE_ADDR) 0);
5640 struct attribute *attr;
5641 char *name = NULL;
5642 char *comp_dir = NULL;
5643 struct die_info *child_die;
5644 bfd *abfd = objfile->obfd;
5645 CORE_ADDR baseaddr;
5646
5647 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5648
5649 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5650
5651 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5652 from finish_block. */
5653 if (lowpc == ((CORE_ADDR) -1))
5654 lowpc = highpc;
5655 lowpc += baseaddr;
5656 highpc += baseaddr;
5657
5658 find_file_and_directory (die, cu, &name, &comp_dir);
5659
5660 attr = dwarf2_attr (die, DW_AT_language, cu);
5661 if (attr)
5662 {
5663 set_cu_language (DW_UNSND (attr), cu);
5664 }
5665
5666 attr = dwarf2_attr (die, DW_AT_producer, cu);
5667 if (attr)
5668 cu->producer = DW_STRING (attr);
5669
5670 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5671 standardised yet. As a workaround for the language detection we fall
5672 back to the DW_AT_producer string. */
5673 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5674 cu->language = language_opencl;
5675
5676 /* We assume that we're processing GCC output. */
5677 processing_gcc_compilation = 2;
5678
5679 processing_has_namespace_info = 0;
5680
5681 start_symtab (name, comp_dir, lowpc);
5682 record_debugformat ("DWARF 2");
5683 record_producer (cu->producer);
5684
5685 handle_DW_AT_stmt_list (die, cu, comp_dir);
5686
5687 /* Process all dies in compilation unit. */
5688 if (die->child != NULL)
5689 {
5690 child_die = die->child;
5691 while (child_die && child_die->tag)
5692 {
5693 process_die (child_die, cu);
5694 child_die = sibling_die (child_die);
5695 }
5696 }
5697
5698 /* Decode macro information, if present. Dwarf 2 macro information
5699 refers to information in the line number info statement program
5700 header, so we can only read it if we've read the header
5701 successfully. */
5702 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5703 if (attr && cu->line_header)
5704 {
5705 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5706 complaint (&symfile_complaints,
5707 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5708
5709 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5710 comp_dir, abfd, cu,
5711 &dwarf2_per_objfile->macro, 1);
5712 }
5713 else
5714 {
5715 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5716 if (attr && cu->line_header)
5717 {
5718 unsigned int macro_offset = DW_UNSND (attr);
5719
5720 dwarf_decode_macros (cu->line_header, macro_offset,
5721 comp_dir, abfd, cu,
5722 &dwarf2_per_objfile->macinfo, 0);
5723 }
5724 }
5725
5726 do_cleanups (back_to);
5727 }
5728
5729 /* Process DW_TAG_type_unit.
5730 For TUs we want to skip the first top level sibling if it's not the
5731 actual type being defined by this TU. In this case the first top
5732 level sibling is there to provide context only. */
5733
5734 static void
5735 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5736 {
5737 struct objfile *objfile = cu->objfile;
5738 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5739 CORE_ADDR lowpc;
5740 struct attribute *attr;
5741 char *name = NULL;
5742 char *comp_dir = NULL;
5743 struct die_info *child_die;
5744 bfd *abfd = objfile->obfd;
5745
5746 /* start_symtab needs a low pc, but we don't really have one.
5747 Do what read_file_scope would do in the absence of such info. */
5748 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5749
5750 /* Find the filename. Do not use dwarf2_name here, since the filename
5751 is not a source language identifier. */
5752 attr = dwarf2_attr (die, DW_AT_name, cu);
5753 if (attr)
5754 name = DW_STRING (attr);
5755
5756 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5757 if (attr)
5758 comp_dir = DW_STRING (attr);
5759 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5760 {
5761 comp_dir = ldirname (name);
5762 if (comp_dir != NULL)
5763 make_cleanup (xfree, comp_dir);
5764 }
5765
5766 if (name == NULL)
5767 name = "<unknown>";
5768
5769 attr = dwarf2_attr (die, DW_AT_language, cu);
5770 if (attr)
5771 set_cu_language (DW_UNSND (attr), cu);
5772
5773 /* This isn't technically needed today. It is done for symmetry
5774 with read_file_scope. */
5775 attr = dwarf2_attr (die, DW_AT_producer, cu);
5776 if (attr)
5777 cu->producer = DW_STRING (attr);
5778
5779 /* We assume that we're processing GCC output. */
5780 processing_gcc_compilation = 2;
5781
5782 processing_has_namespace_info = 0;
5783
5784 start_symtab (name, comp_dir, lowpc);
5785 record_debugformat ("DWARF 2");
5786 record_producer (cu->producer);
5787
5788 handle_DW_AT_stmt_list (die, cu, comp_dir);
5789
5790 /* Process the dies in the type unit. */
5791 if (die->child == NULL)
5792 {
5793 dump_die_for_error (die);
5794 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5795 bfd_get_filename (abfd));
5796 }
5797
5798 child_die = die->child;
5799
5800 while (child_die && child_die->tag)
5801 {
5802 process_die (child_die, cu);
5803
5804 child_die = sibling_die (child_die);
5805 }
5806
5807 do_cleanups (back_to);
5808 }
5809
5810 /* qsort helper for inherit_abstract_dies. */
5811
5812 static int
5813 unsigned_int_compar (const void *ap, const void *bp)
5814 {
5815 unsigned int a = *(unsigned int *) ap;
5816 unsigned int b = *(unsigned int *) bp;
5817
5818 return (a > b) - (b > a);
5819 }
5820
5821 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5822 Inherit only the children of the DW_AT_abstract_origin DIE not being
5823 already referenced by DW_AT_abstract_origin from the children of the
5824 current DIE. */
5825
5826 static void
5827 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5828 {
5829 struct die_info *child_die;
5830 unsigned die_children_count;
5831 /* CU offsets which were referenced by children of the current DIE. */
5832 unsigned *offsets;
5833 unsigned *offsets_end, *offsetp;
5834 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5835 struct die_info *origin_die;
5836 /* Iterator of the ORIGIN_DIE children. */
5837 struct die_info *origin_child_die;
5838 struct cleanup *cleanups;
5839 struct attribute *attr;
5840 struct dwarf2_cu *origin_cu;
5841 struct pending **origin_previous_list_in_scope;
5842
5843 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5844 if (!attr)
5845 return;
5846
5847 /* Note that following die references may follow to a die in a
5848 different cu. */
5849
5850 origin_cu = cu;
5851 origin_die = follow_die_ref (die, attr, &origin_cu);
5852
5853 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5854 symbols in. */
5855 origin_previous_list_in_scope = origin_cu->list_in_scope;
5856 origin_cu->list_in_scope = cu->list_in_scope;
5857
5858 if (die->tag != origin_die->tag
5859 && !(die->tag == DW_TAG_inlined_subroutine
5860 && origin_die->tag == DW_TAG_subprogram))
5861 complaint (&symfile_complaints,
5862 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5863 die->offset, origin_die->offset);
5864
5865 child_die = die->child;
5866 die_children_count = 0;
5867 while (child_die && child_die->tag)
5868 {
5869 child_die = sibling_die (child_die);
5870 die_children_count++;
5871 }
5872 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5873 cleanups = make_cleanup (xfree, offsets);
5874
5875 offsets_end = offsets;
5876 child_die = die->child;
5877 while (child_die && child_die->tag)
5878 {
5879 /* For each CHILD_DIE, find the corresponding child of
5880 ORIGIN_DIE. If there is more than one layer of
5881 DW_AT_abstract_origin, follow them all; there shouldn't be,
5882 but GCC versions at least through 4.4 generate this (GCC PR
5883 40573). */
5884 struct die_info *child_origin_die = child_die;
5885 struct dwarf2_cu *child_origin_cu = cu;
5886
5887 while (1)
5888 {
5889 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5890 child_origin_cu);
5891 if (attr == NULL)
5892 break;
5893 child_origin_die = follow_die_ref (child_origin_die, attr,
5894 &child_origin_cu);
5895 }
5896
5897 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5898 counterpart may exist. */
5899 if (child_origin_die != child_die)
5900 {
5901 if (child_die->tag != child_origin_die->tag
5902 && !(child_die->tag == DW_TAG_inlined_subroutine
5903 && child_origin_die->tag == DW_TAG_subprogram))
5904 complaint (&symfile_complaints,
5905 _("Child DIE 0x%x and its abstract origin 0x%x have "
5906 "different tags"), child_die->offset,
5907 child_origin_die->offset);
5908 if (child_origin_die->parent != origin_die)
5909 complaint (&symfile_complaints,
5910 _("Child DIE 0x%x and its abstract origin 0x%x have "
5911 "different parents"), child_die->offset,
5912 child_origin_die->offset);
5913 else
5914 *offsets_end++ = child_origin_die->offset;
5915 }
5916 child_die = sibling_die (child_die);
5917 }
5918 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5919 unsigned_int_compar);
5920 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5921 if (offsetp[-1] == *offsetp)
5922 complaint (&symfile_complaints,
5923 _("Multiple children of DIE 0x%x refer "
5924 "to DIE 0x%x as their abstract origin"),
5925 die->offset, *offsetp);
5926
5927 offsetp = offsets;
5928 origin_child_die = origin_die->child;
5929 while (origin_child_die && origin_child_die->tag)
5930 {
5931 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5932 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5933 offsetp++;
5934 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5935 {
5936 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5937 process_die (origin_child_die, origin_cu);
5938 }
5939 origin_child_die = sibling_die (origin_child_die);
5940 }
5941 origin_cu->list_in_scope = origin_previous_list_in_scope;
5942
5943 do_cleanups (cleanups);
5944 }
5945
5946 static void
5947 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5948 {
5949 struct objfile *objfile = cu->objfile;
5950 struct context_stack *new;
5951 CORE_ADDR lowpc;
5952 CORE_ADDR highpc;
5953 struct die_info *child_die;
5954 struct attribute *attr, *call_line, *call_file;
5955 char *name;
5956 CORE_ADDR baseaddr;
5957 struct block *block;
5958 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5959 VEC (symbolp) *template_args = NULL;
5960 struct template_symbol *templ_func = NULL;
5961
5962 if (inlined_func)
5963 {
5964 /* If we do not have call site information, we can't show the
5965 caller of this inlined function. That's too confusing, so
5966 only use the scope for local variables. */
5967 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5968 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5969 if (call_line == NULL || call_file == NULL)
5970 {
5971 read_lexical_block_scope (die, cu);
5972 return;
5973 }
5974 }
5975
5976 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5977
5978 name = dwarf2_name (die, cu);
5979
5980 /* Ignore functions with missing or empty names. These are actually
5981 illegal according to the DWARF standard. */
5982 if (name == NULL)
5983 {
5984 complaint (&symfile_complaints,
5985 _("missing name for subprogram DIE at %d"), die->offset);
5986 return;
5987 }
5988
5989 /* Ignore functions with missing or invalid low and high pc attributes. */
5990 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5991 {
5992 attr = dwarf2_attr (die, DW_AT_external, cu);
5993 if (!attr || !DW_UNSND (attr))
5994 complaint (&symfile_complaints,
5995 _("cannot get low and high bounds "
5996 "for subprogram DIE at %d"),
5997 die->offset);
5998 return;
5999 }
6000
6001 lowpc += baseaddr;
6002 highpc += baseaddr;
6003
6004 /* If we have any template arguments, then we must allocate a
6005 different sort of symbol. */
6006 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6007 {
6008 if (child_die->tag == DW_TAG_template_type_param
6009 || child_die->tag == DW_TAG_template_value_param)
6010 {
6011 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6012 struct template_symbol);
6013 templ_func->base.is_cplus_template_function = 1;
6014 break;
6015 }
6016 }
6017
6018 new = push_context (0, lowpc);
6019 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6020 (struct symbol *) templ_func);
6021
6022 /* If there is a location expression for DW_AT_frame_base, record
6023 it. */
6024 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6025 if (attr)
6026 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6027 expression is being recorded directly in the function's symbol
6028 and not in a separate frame-base object. I guess this hack is
6029 to avoid adding some sort of frame-base adjunct/annex to the
6030 function's symbol :-(. The problem with doing this is that it
6031 results in a function symbol with a location expression that
6032 has nothing to do with the location of the function, ouch! The
6033 relationship should be: a function's symbol has-a frame base; a
6034 frame-base has-a location expression. */
6035 dwarf2_symbol_mark_computed (attr, new->name, cu);
6036
6037 cu->list_in_scope = &local_symbols;
6038
6039 if (die->child != NULL)
6040 {
6041 child_die = die->child;
6042 while (child_die && child_die->tag)
6043 {
6044 if (child_die->tag == DW_TAG_template_type_param
6045 || child_die->tag == DW_TAG_template_value_param)
6046 {
6047 struct symbol *arg = new_symbol (child_die, NULL, cu);
6048
6049 if (arg != NULL)
6050 VEC_safe_push (symbolp, template_args, arg);
6051 }
6052 else
6053 process_die (child_die, cu);
6054 child_die = sibling_die (child_die);
6055 }
6056 }
6057
6058 inherit_abstract_dies (die, cu);
6059
6060 /* If we have a DW_AT_specification, we might need to import using
6061 directives from the context of the specification DIE. See the
6062 comment in determine_prefix. */
6063 if (cu->language == language_cplus
6064 && dwarf2_attr (die, DW_AT_specification, cu))
6065 {
6066 struct dwarf2_cu *spec_cu = cu;
6067 struct die_info *spec_die = die_specification (die, &spec_cu);
6068
6069 while (spec_die)
6070 {
6071 child_die = spec_die->child;
6072 while (child_die && child_die->tag)
6073 {
6074 if (child_die->tag == DW_TAG_imported_module)
6075 process_die (child_die, spec_cu);
6076 child_die = sibling_die (child_die);
6077 }
6078
6079 /* In some cases, GCC generates specification DIEs that
6080 themselves contain DW_AT_specification attributes. */
6081 spec_die = die_specification (spec_die, &spec_cu);
6082 }
6083 }
6084
6085 new = pop_context ();
6086 /* Make a block for the local symbols within. */
6087 block = finish_block (new->name, &local_symbols, new->old_blocks,
6088 lowpc, highpc, objfile);
6089
6090 /* For C++, set the block's scope. */
6091 if (cu->language == language_cplus || cu->language == language_fortran)
6092 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6093 determine_prefix (die, cu),
6094 processing_has_namespace_info);
6095
6096 /* If we have address ranges, record them. */
6097 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6098
6099 /* Attach template arguments to function. */
6100 if (! VEC_empty (symbolp, template_args))
6101 {
6102 gdb_assert (templ_func != NULL);
6103
6104 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6105 templ_func->template_arguments
6106 = obstack_alloc (&objfile->objfile_obstack,
6107 (templ_func->n_template_arguments
6108 * sizeof (struct symbol *)));
6109 memcpy (templ_func->template_arguments,
6110 VEC_address (symbolp, template_args),
6111 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6112 VEC_free (symbolp, template_args);
6113 }
6114
6115 /* In C++, we can have functions nested inside functions (e.g., when
6116 a function declares a class that has methods). This means that
6117 when we finish processing a function scope, we may need to go
6118 back to building a containing block's symbol lists. */
6119 local_symbols = new->locals;
6120 param_symbols = new->params;
6121 using_directives = new->using_directives;
6122
6123 /* If we've finished processing a top-level function, subsequent
6124 symbols go in the file symbol list. */
6125 if (outermost_context_p ())
6126 cu->list_in_scope = &file_symbols;
6127 }
6128
6129 /* Process all the DIES contained within a lexical block scope. Start
6130 a new scope, process the dies, and then close the scope. */
6131
6132 static void
6133 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6134 {
6135 struct objfile *objfile = cu->objfile;
6136 struct context_stack *new;
6137 CORE_ADDR lowpc, highpc;
6138 struct die_info *child_die;
6139 CORE_ADDR baseaddr;
6140
6141 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6142
6143 /* Ignore blocks with missing or invalid low and high pc attributes. */
6144 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6145 as multiple lexical blocks? Handling children in a sane way would
6146 be nasty. Might be easier to properly extend generic blocks to
6147 describe ranges. */
6148 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6149 return;
6150 lowpc += baseaddr;
6151 highpc += baseaddr;
6152
6153 push_context (0, lowpc);
6154 if (die->child != NULL)
6155 {
6156 child_die = die->child;
6157 while (child_die && child_die->tag)
6158 {
6159 process_die (child_die, cu);
6160 child_die = sibling_die (child_die);
6161 }
6162 }
6163 new = pop_context ();
6164
6165 if (local_symbols != NULL || using_directives != NULL)
6166 {
6167 struct block *block
6168 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6169 highpc, objfile);
6170
6171 /* Note that recording ranges after traversing children, as we
6172 do here, means that recording a parent's ranges entails
6173 walking across all its children's ranges as they appear in
6174 the address map, which is quadratic behavior.
6175
6176 It would be nicer to record the parent's ranges before
6177 traversing its children, simply overriding whatever you find
6178 there. But since we don't even decide whether to create a
6179 block until after we've traversed its children, that's hard
6180 to do. */
6181 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6182 }
6183 local_symbols = new->locals;
6184 using_directives = new->using_directives;
6185 }
6186
6187 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6188
6189 static void
6190 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6191 {
6192 struct objfile *objfile = cu->objfile;
6193 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6194 CORE_ADDR pc, baseaddr;
6195 struct attribute *attr;
6196 struct call_site *call_site, call_site_local;
6197 void **slot;
6198 int nparams;
6199 struct die_info *child_die;
6200
6201 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6202
6203 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6204 if (!attr)
6205 {
6206 complaint (&symfile_complaints,
6207 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6208 "DIE 0x%x [in module %s]"),
6209 die->offset, objfile->name);
6210 return;
6211 }
6212 pc = DW_ADDR (attr) + baseaddr;
6213
6214 if (cu->call_site_htab == NULL)
6215 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6216 NULL, &objfile->objfile_obstack,
6217 hashtab_obstack_allocate, NULL);
6218 call_site_local.pc = pc;
6219 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6220 if (*slot != NULL)
6221 {
6222 complaint (&symfile_complaints,
6223 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6224 "DIE 0x%x [in module %s]"),
6225 paddress (gdbarch, pc), die->offset, objfile->name);
6226 return;
6227 }
6228
6229 /* Count parameters at the caller. */
6230
6231 nparams = 0;
6232 for (child_die = die->child; child_die && child_die->tag;
6233 child_die = sibling_die (child_die))
6234 {
6235 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6236 {
6237 complaint (&symfile_complaints,
6238 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6239 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6240 child_die->tag, child_die->offset, objfile->name);
6241 continue;
6242 }
6243
6244 nparams++;
6245 }
6246
6247 call_site = obstack_alloc (&objfile->objfile_obstack,
6248 (sizeof (*call_site)
6249 + (sizeof (*call_site->parameter)
6250 * (nparams - 1))));
6251 *slot = call_site;
6252 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6253 call_site->pc = pc;
6254
6255 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6256 {
6257 struct die_info *func_die;
6258
6259 /* Skip also over DW_TAG_inlined_subroutine. */
6260 for (func_die = die->parent;
6261 func_die && func_die->tag != DW_TAG_subprogram
6262 && func_die->tag != DW_TAG_subroutine_type;
6263 func_die = func_die->parent);
6264
6265 /* DW_AT_GNU_all_call_sites is a superset
6266 of DW_AT_GNU_all_tail_call_sites. */
6267 if (func_die
6268 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6269 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6270 {
6271 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6272 not complete. But keep CALL_SITE for look ups via call_site_htab,
6273 both the initial caller containing the real return address PC and
6274 the final callee containing the current PC of a chain of tail
6275 calls do not need to have the tail call list complete. But any
6276 function candidate for a virtual tail call frame searched via
6277 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6278 determined unambiguously. */
6279 }
6280 else
6281 {
6282 struct type *func_type = NULL;
6283
6284 if (func_die)
6285 func_type = get_die_type (func_die, cu);
6286 if (func_type != NULL)
6287 {
6288 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6289
6290 /* Enlist this call site to the function. */
6291 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6292 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6293 }
6294 else
6295 complaint (&symfile_complaints,
6296 _("Cannot find function owning DW_TAG_GNU_call_site "
6297 "DIE 0x%x [in module %s]"),
6298 die->offset, objfile->name);
6299 }
6300 }
6301
6302 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6303 if (attr == NULL)
6304 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6305 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6306 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6307 /* Keep NULL DWARF_BLOCK. */;
6308 else if (attr_form_is_block (attr))
6309 {
6310 struct dwarf2_locexpr_baton *dlbaton;
6311
6312 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6313 dlbaton->data = DW_BLOCK (attr)->data;
6314 dlbaton->size = DW_BLOCK (attr)->size;
6315 dlbaton->per_cu = cu->per_cu;
6316
6317 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6318 }
6319 else if (is_ref_attr (attr))
6320 {
6321 struct dwarf2_cu *target_cu = cu;
6322 struct die_info *target_die;
6323
6324 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6325 gdb_assert (target_cu->objfile == objfile);
6326 if (die_is_declaration (target_die, target_cu))
6327 {
6328 const char *target_physname;
6329
6330 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6331 if (target_physname == NULL)
6332 complaint (&symfile_complaints,
6333 _("DW_AT_GNU_call_site_target target DIE has invalid "
6334 "physname, for referencing DIE 0x%x [in module %s]"),
6335 die->offset, objfile->name);
6336 else
6337 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6338 }
6339 else
6340 {
6341 CORE_ADDR lowpc;
6342
6343 /* DW_AT_entry_pc should be preferred. */
6344 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6345 complaint (&symfile_complaints,
6346 _("DW_AT_GNU_call_site_target target DIE has invalid "
6347 "low pc, for referencing DIE 0x%x [in module %s]"),
6348 die->offset, objfile->name);
6349 else
6350 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6351 }
6352 }
6353 else
6354 complaint (&symfile_complaints,
6355 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6356 "block nor reference, for DIE 0x%x [in module %s]"),
6357 die->offset, objfile->name);
6358
6359 call_site->per_cu = cu->per_cu;
6360
6361 for (child_die = die->child;
6362 child_die && child_die->tag;
6363 child_die = sibling_die (child_die))
6364 {
6365 struct dwarf2_locexpr_baton *dlbaton;
6366 struct call_site_parameter *parameter;
6367
6368 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6369 {
6370 /* Already printed the complaint above. */
6371 continue;
6372 }
6373
6374 gdb_assert (call_site->parameter_count < nparams);
6375 parameter = &call_site->parameter[call_site->parameter_count];
6376
6377 /* DW_AT_location specifies the register number. Value of the data
6378 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6379
6380 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6381 if (!attr || !attr_form_is_block (attr))
6382 {
6383 complaint (&symfile_complaints,
6384 _("No DW_FORM_block* DW_AT_location for "
6385 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6386 child_die->offset, objfile->name);
6387 continue;
6388 }
6389 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6390 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6391 if (parameter->dwarf_reg == -1
6392 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6393 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6394 &parameter->fb_offset))
6395 {
6396 complaint (&symfile_complaints,
6397 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6398 "for DW_FORM_block* DW_AT_location for "
6399 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6400 child_die->offset, objfile->name);
6401 continue;
6402 }
6403
6404 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6405 if (!attr_form_is_block (attr))
6406 {
6407 complaint (&symfile_complaints,
6408 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6409 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6410 child_die->offset, objfile->name);
6411 continue;
6412 }
6413 parameter->value = DW_BLOCK (attr)->data;
6414 parameter->value_size = DW_BLOCK (attr)->size;
6415
6416 /* Parameters are not pre-cleared by memset above. */
6417 parameter->data_value = NULL;
6418 parameter->data_value_size = 0;
6419 call_site->parameter_count++;
6420
6421 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6422 if (attr)
6423 {
6424 if (!attr_form_is_block (attr))
6425 complaint (&symfile_complaints,
6426 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6427 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6428 child_die->offset, objfile->name);
6429 else
6430 {
6431 parameter->data_value = DW_BLOCK (attr)->data;
6432 parameter->data_value_size = DW_BLOCK (attr)->size;
6433 }
6434 }
6435 }
6436 }
6437
6438 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6439 Return 1 if the attributes are present and valid, otherwise, return 0.
6440 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6441
6442 static int
6443 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6444 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6445 struct partial_symtab *ranges_pst)
6446 {
6447 struct objfile *objfile = cu->objfile;
6448 struct comp_unit_head *cu_header = &cu->header;
6449 bfd *obfd = objfile->obfd;
6450 unsigned int addr_size = cu_header->addr_size;
6451 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6452 /* Base address selection entry. */
6453 CORE_ADDR base;
6454 int found_base;
6455 unsigned int dummy;
6456 gdb_byte *buffer;
6457 CORE_ADDR marker;
6458 int low_set;
6459 CORE_ADDR low = 0;
6460 CORE_ADDR high = 0;
6461 CORE_ADDR baseaddr;
6462
6463 found_base = cu->base_known;
6464 base = cu->base_address;
6465
6466 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6467 if (offset >= dwarf2_per_objfile->ranges.size)
6468 {
6469 complaint (&symfile_complaints,
6470 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6471 offset);
6472 return 0;
6473 }
6474 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6475
6476 /* Read in the largest possible address. */
6477 marker = read_address (obfd, buffer, cu, &dummy);
6478 if ((marker & mask) == mask)
6479 {
6480 /* If we found the largest possible address, then
6481 read the base address. */
6482 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6483 buffer += 2 * addr_size;
6484 offset += 2 * addr_size;
6485 found_base = 1;
6486 }
6487
6488 low_set = 0;
6489
6490 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6491
6492 while (1)
6493 {
6494 CORE_ADDR range_beginning, range_end;
6495
6496 range_beginning = read_address (obfd, buffer, cu, &dummy);
6497 buffer += addr_size;
6498 range_end = read_address (obfd, buffer, cu, &dummy);
6499 buffer += addr_size;
6500 offset += 2 * addr_size;
6501
6502 /* An end of list marker is a pair of zero addresses. */
6503 if (range_beginning == 0 && range_end == 0)
6504 /* Found the end of list entry. */
6505 break;
6506
6507 /* Each base address selection entry is a pair of 2 values.
6508 The first is the largest possible address, the second is
6509 the base address. Check for a base address here. */
6510 if ((range_beginning & mask) == mask)
6511 {
6512 /* If we found the largest possible address, then
6513 read the base address. */
6514 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6515 found_base = 1;
6516 continue;
6517 }
6518
6519 if (!found_base)
6520 {
6521 /* We have no valid base address for the ranges
6522 data. */
6523 complaint (&symfile_complaints,
6524 _("Invalid .debug_ranges data (no base address)"));
6525 return 0;
6526 }
6527
6528 if (range_beginning > range_end)
6529 {
6530 /* Inverted range entries are invalid. */
6531 complaint (&symfile_complaints,
6532 _("Invalid .debug_ranges data (inverted range)"));
6533 return 0;
6534 }
6535
6536 /* Empty range entries have no effect. */
6537 if (range_beginning == range_end)
6538 continue;
6539
6540 range_beginning += base;
6541 range_end += base;
6542
6543 if (ranges_pst != NULL)
6544 addrmap_set_empty (objfile->psymtabs_addrmap,
6545 range_beginning + baseaddr,
6546 range_end - 1 + baseaddr,
6547 ranges_pst);
6548
6549 /* FIXME: This is recording everything as a low-high
6550 segment of consecutive addresses. We should have a
6551 data structure for discontiguous block ranges
6552 instead. */
6553 if (! low_set)
6554 {
6555 low = range_beginning;
6556 high = range_end;
6557 low_set = 1;
6558 }
6559 else
6560 {
6561 if (range_beginning < low)
6562 low = range_beginning;
6563 if (range_end > high)
6564 high = range_end;
6565 }
6566 }
6567
6568 if (! low_set)
6569 /* If the first entry is an end-of-list marker, the range
6570 describes an empty scope, i.e. no instructions. */
6571 return 0;
6572
6573 if (low_return)
6574 *low_return = low;
6575 if (high_return)
6576 *high_return = high;
6577 return 1;
6578 }
6579
6580 /* Get low and high pc attributes from a die. Return 1 if the attributes
6581 are present and valid, otherwise, return 0. Return -1 if the range is
6582 discontinuous, i.e. derived from DW_AT_ranges information. */
6583 static int
6584 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6585 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6586 struct partial_symtab *pst)
6587 {
6588 struct attribute *attr;
6589 CORE_ADDR low = 0;
6590 CORE_ADDR high = 0;
6591 int ret = 0;
6592
6593 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6594 if (attr)
6595 {
6596 high = DW_ADDR (attr);
6597 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6598 if (attr)
6599 low = DW_ADDR (attr);
6600 else
6601 /* Found high w/o low attribute. */
6602 return 0;
6603
6604 /* Found consecutive range of addresses. */
6605 ret = 1;
6606 }
6607 else
6608 {
6609 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6610 if (attr != NULL)
6611 {
6612 /* Value of the DW_AT_ranges attribute is the offset in the
6613 .debug_ranges section. */
6614 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6615 return 0;
6616 /* Found discontinuous range of addresses. */
6617 ret = -1;
6618 }
6619 }
6620
6621 /* read_partial_die has also the strict LOW < HIGH requirement. */
6622 if (high <= low)
6623 return 0;
6624
6625 /* When using the GNU linker, .gnu.linkonce. sections are used to
6626 eliminate duplicate copies of functions and vtables and such.
6627 The linker will arbitrarily choose one and discard the others.
6628 The AT_*_pc values for such functions refer to local labels in
6629 these sections. If the section from that file was discarded, the
6630 labels are not in the output, so the relocs get a value of 0.
6631 If this is a discarded function, mark the pc bounds as invalid,
6632 so that GDB will ignore it. */
6633 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6634 return 0;
6635
6636 *lowpc = low;
6637 if (highpc)
6638 *highpc = high;
6639 return ret;
6640 }
6641
6642 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6643 its low and high PC addresses. Do nothing if these addresses could not
6644 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6645 and HIGHPC to the high address if greater than HIGHPC. */
6646
6647 static void
6648 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6649 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6650 struct dwarf2_cu *cu)
6651 {
6652 CORE_ADDR low, high;
6653 struct die_info *child = die->child;
6654
6655 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6656 {
6657 *lowpc = min (*lowpc, low);
6658 *highpc = max (*highpc, high);
6659 }
6660
6661 /* If the language does not allow nested subprograms (either inside
6662 subprograms or lexical blocks), we're done. */
6663 if (cu->language != language_ada)
6664 return;
6665
6666 /* Check all the children of the given DIE. If it contains nested
6667 subprograms, then check their pc bounds. Likewise, we need to
6668 check lexical blocks as well, as they may also contain subprogram
6669 definitions. */
6670 while (child && child->tag)
6671 {
6672 if (child->tag == DW_TAG_subprogram
6673 || child->tag == DW_TAG_lexical_block)
6674 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6675 child = sibling_die (child);
6676 }
6677 }
6678
6679 /* Get the low and high pc's represented by the scope DIE, and store
6680 them in *LOWPC and *HIGHPC. If the correct values can't be
6681 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6682
6683 static void
6684 get_scope_pc_bounds (struct die_info *die,
6685 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6686 struct dwarf2_cu *cu)
6687 {
6688 CORE_ADDR best_low = (CORE_ADDR) -1;
6689 CORE_ADDR best_high = (CORE_ADDR) 0;
6690 CORE_ADDR current_low, current_high;
6691
6692 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6693 {
6694 best_low = current_low;
6695 best_high = current_high;
6696 }
6697 else
6698 {
6699 struct die_info *child = die->child;
6700
6701 while (child && child->tag)
6702 {
6703 switch (child->tag) {
6704 case DW_TAG_subprogram:
6705 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6706 break;
6707 case DW_TAG_namespace:
6708 case DW_TAG_module:
6709 /* FIXME: carlton/2004-01-16: Should we do this for
6710 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6711 that current GCC's always emit the DIEs corresponding
6712 to definitions of methods of classes as children of a
6713 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6714 the DIEs giving the declarations, which could be
6715 anywhere). But I don't see any reason why the
6716 standards says that they have to be there. */
6717 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6718
6719 if (current_low != ((CORE_ADDR) -1))
6720 {
6721 best_low = min (best_low, current_low);
6722 best_high = max (best_high, current_high);
6723 }
6724 break;
6725 default:
6726 /* Ignore. */
6727 break;
6728 }
6729
6730 child = sibling_die (child);
6731 }
6732 }
6733
6734 *lowpc = best_low;
6735 *highpc = best_high;
6736 }
6737
6738 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6739 in DIE. */
6740 static void
6741 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6742 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6743 {
6744 struct objfile *objfile = cu->objfile;
6745 struct attribute *attr;
6746
6747 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6748 if (attr)
6749 {
6750 CORE_ADDR high = DW_ADDR (attr);
6751
6752 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6753 if (attr)
6754 {
6755 CORE_ADDR low = DW_ADDR (attr);
6756
6757 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6758 }
6759 }
6760
6761 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6762 if (attr)
6763 {
6764 bfd *obfd = objfile->obfd;
6765
6766 /* The value of the DW_AT_ranges attribute is the offset of the
6767 address range list in the .debug_ranges section. */
6768 unsigned long offset = DW_UNSND (attr);
6769 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6770
6771 /* For some target architectures, but not others, the
6772 read_address function sign-extends the addresses it returns.
6773 To recognize base address selection entries, we need a
6774 mask. */
6775 unsigned int addr_size = cu->header.addr_size;
6776 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6777
6778 /* The base address, to which the next pair is relative. Note
6779 that this 'base' is a DWARF concept: most entries in a range
6780 list are relative, to reduce the number of relocs against the
6781 debugging information. This is separate from this function's
6782 'baseaddr' argument, which GDB uses to relocate debugging
6783 information from a shared library based on the address at
6784 which the library was loaded. */
6785 CORE_ADDR base = cu->base_address;
6786 int base_known = cu->base_known;
6787
6788 gdb_assert (dwarf2_per_objfile->ranges.readin);
6789 if (offset >= dwarf2_per_objfile->ranges.size)
6790 {
6791 complaint (&symfile_complaints,
6792 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6793 offset);
6794 return;
6795 }
6796
6797 for (;;)
6798 {
6799 unsigned int bytes_read;
6800 CORE_ADDR start, end;
6801
6802 start = read_address (obfd, buffer, cu, &bytes_read);
6803 buffer += bytes_read;
6804 end = read_address (obfd, buffer, cu, &bytes_read);
6805 buffer += bytes_read;
6806
6807 /* Did we find the end of the range list? */
6808 if (start == 0 && end == 0)
6809 break;
6810
6811 /* Did we find a base address selection entry? */
6812 else if ((start & base_select_mask) == base_select_mask)
6813 {
6814 base = end;
6815 base_known = 1;
6816 }
6817
6818 /* We found an ordinary address range. */
6819 else
6820 {
6821 if (!base_known)
6822 {
6823 complaint (&symfile_complaints,
6824 _("Invalid .debug_ranges data "
6825 "(no base address)"));
6826 return;
6827 }
6828
6829 if (start > end)
6830 {
6831 /* Inverted range entries are invalid. */
6832 complaint (&symfile_complaints,
6833 _("Invalid .debug_ranges data "
6834 "(inverted range)"));
6835 return;
6836 }
6837
6838 /* Empty range entries have no effect. */
6839 if (start == end)
6840 continue;
6841
6842 record_block_range (block,
6843 baseaddr + base + start,
6844 baseaddr + base + end - 1);
6845 }
6846 }
6847 }
6848 }
6849
6850 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6851 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6852 during 4.6.0 experimental. */
6853
6854 static int
6855 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6856 {
6857 const char *cs;
6858 int major, minor, release;
6859
6860 if (cu->producer == NULL)
6861 {
6862 /* For unknown compilers expect their behavior is DWARF version
6863 compliant.
6864
6865 GCC started to support .debug_types sections by -gdwarf-4 since
6866 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6867 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6868 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6869 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6870
6871 return 0;
6872 }
6873
6874 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6875
6876 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6877 {
6878 /* For non-GCC compilers expect their behavior is DWARF version
6879 compliant. */
6880
6881 return 0;
6882 }
6883 cs = &cu->producer[strlen ("GNU ")];
6884 while (*cs && !isdigit (*cs))
6885 cs++;
6886 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6887 {
6888 /* Not recognized as GCC. */
6889
6890 return 0;
6891 }
6892
6893 return major < 4 || (major == 4 && minor < 6);
6894 }
6895
6896 /* Return the default accessibility type if it is not overriden by
6897 DW_AT_accessibility. */
6898
6899 static enum dwarf_access_attribute
6900 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6901 {
6902 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6903 {
6904 /* The default DWARF 2 accessibility for members is public, the default
6905 accessibility for inheritance is private. */
6906
6907 if (die->tag != DW_TAG_inheritance)
6908 return DW_ACCESS_public;
6909 else
6910 return DW_ACCESS_private;
6911 }
6912 else
6913 {
6914 /* DWARF 3+ defines the default accessibility a different way. The same
6915 rules apply now for DW_TAG_inheritance as for the members and it only
6916 depends on the container kind. */
6917
6918 if (die->parent->tag == DW_TAG_class_type)
6919 return DW_ACCESS_private;
6920 else
6921 return DW_ACCESS_public;
6922 }
6923 }
6924
6925 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6926 offset. If the attribute was not found return 0, otherwise return
6927 1. If it was found but could not properly be handled, set *OFFSET
6928 to 0. */
6929
6930 static int
6931 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6932 LONGEST *offset)
6933 {
6934 struct attribute *attr;
6935
6936 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6937 if (attr != NULL)
6938 {
6939 *offset = 0;
6940
6941 /* Note that we do not check for a section offset first here.
6942 This is because DW_AT_data_member_location is new in DWARF 4,
6943 so if we see it, we can assume that a constant form is really
6944 a constant and not a section offset. */
6945 if (attr_form_is_constant (attr))
6946 *offset = dwarf2_get_attr_constant_value (attr, 0);
6947 else if (attr_form_is_section_offset (attr))
6948 dwarf2_complex_location_expr_complaint ();
6949 else if (attr_form_is_block (attr))
6950 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6951 else
6952 dwarf2_complex_location_expr_complaint ();
6953
6954 return 1;
6955 }
6956
6957 return 0;
6958 }
6959
6960 /* Add an aggregate field to the field list. */
6961
6962 static void
6963 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6964 struct dwarf2_cu *cu)
6965 {
6966 struct objfile *objfile = cu->objfile;
6967 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6968 struct nextfield *new_field;
6969 struct attribute *attr;
6970 struct field *fp;
6971 char *fieldname = "";
6972
6973 /* Allocate a new field list entry and link it in. */
6974 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6975 make_cleanup (xfree, new_field);
6976 memset (new_field, 0, sizeof (struct nextfield));
6977
6978 if (die->tag == DW_TAG_inheritance)
6979 {
6980 new_field->next = fip->baseclasses;
6981 fip->baseclasses = new_field;
6982 }
6983 else
6984 {
6985 new_field->next = fip->fields;
6986 fip->fields = new_field;
6987 }
6988 fip->nfields++;
6989
6990 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6991 if (attr)
6992 new_field->accessibility = DW_UNSND (attr);
6993 else
6994 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6995 if (new_field->accessibility != DW_ACCESS_public)
6996 fip->non_public_fields = 1;
6997
6998 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6999 if (attr)
7000 new_field->virtuality = DW_UNSND (attr);
7001 else
7002 new_field->virtuality = DW_VIRTUALITY_none;
7003
7004 fp = &new_field->field;
7005
7006 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7007 {
7008 LONGEST offset;
7009
7010 /* Data member other than a C++ static data member. */
7011
7012 /* Get type of field. */
7013 fp->type = die_type (die, cu);
7014
7015 SET_FIELD_BITPOS (*fp, 0);
7016
7017 /* Get bit size of field (zero if none). */
7018 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7019 if (attr)
7020 {
7021 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7022 }
7023 else
7024 {
7025 FIELD_BITSIZE (*fp) = 0;
7026 }
7027
7028 /* Get bit offset of field. */
7029 if (handle_data_member_location (die, cu, &offset))
7030 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7031 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7032 if (attr)
7033 {
7034 if (gdbarch_bits_big_endian (gdbarch))
7035 {
7036 /* For big endian bits, the DW_AT_bit_offset gives the
7037 additional bit offset from the MSB of the containing
7038 anonymous object to the MSB of the field. We don't
7039 have to do anything special since we don't need to
7040 know the size of the anonymous object. */
7041 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7042 }
7043 else
7044 {
7045 /* For little endian bits, compute the bit offset to the
7046 MSB of the anonymous object, subtract off the number of
7047 bits from the MSB of the field to the MSB of the
7048 object, and then subtract off the number of bits of
7049 the field itself. The result is the bit offset of
7050 the LSB of the field. */
7051 int anonymous_size;
7052 int bit_offset = DW_UNSND (attr);
7053
7054 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7055 if (attr)
7056 {
7057 /* The size of the anonymous object containing
7058 the bit field is explicit, so use the
7059 indicated size (in bytes). */
7060 anonymous_size = DW_UNSND (attr);
7061 }
7062 else
7063 {
7064 /* The size of the anonymous object containing
7065 the bit field must be inferred from the type
7066 attribute of the data member containing the
7067 bit field. */
7068 anonymous_size = TYPE_LENGTH (fp->type);
7069 }
7070 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7071 - bit_offset - FIELD_BITSIZE (*fp);
7072 }
7073 }
7074
7075 /* Get name of field. */
7076 fieldname = dwarf2_name (die, cu);
7077 if (fieldname == NULL)
7078 fieldname = "";
7079
7080 /* The name is already allocated along with this objfile, so we don't
7081 need to duplicate it for the type. */
7082 fp->name = fieldname;
7083
7084 /* Change accessibility for artificial fields (e.g. virtual table
7085 pointer or virtual base class pointer) to private. */
7086 if (dwarf2_attr (die, DW_AT_artificial, cu))
7087 {
7088 FIELD_ARTIFICIAL (*fp) = 1;
7089 new_field->accessibility = DW_ACCESS_private;
7090 fip->non_public_fields = 1;
7091 }
7092 }
7093 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7094 {
7095 /* C++ static member. */
7096
7097 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7098 is a declaration, but all versions of G++ as of this writing
7099 (so through at least 3.2.1) incorrectly generate
7100 DW_TAG_variable tags. */
7101
7102 const char *physname;
7103
7104 /* Get name of field. */
7105 fieldname = dwarf2_name (die, cu);
7106 if (fieldname == NULL)
7107 return;
7108
7109 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7110 if (attr
7111 /* Only create a symbol if this is an external value.
7112 new_symbol checks this and puts the value in the global symbol
7113 table, which we want. If it is not external, new_symbol
7114 will try to put the value in cu->list_in_scope which is wrong. */
7115 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7116 {
7117 /* A static const member, not much different than an enum as far as
7118 we're concerned, except that we can support more types. */
7119 new_symbol (die, NULL, cu);
7120 }
7121
7122 /* Get physical name. */
7123 physname = dwarf2_physname (fieldname, die, cu);
7124
7125 /* The name is already allocated along with this objfile, so we don't
7126 need to duplicate it for the type. */
7127 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7128 FIELD_TYPE (*fp) = die_type (die, cu);
7129 FIELD_NAME (*fp) = fieldname;
7130 }
7131 else if (die->tag == DW_TAG_inheritance)
7132 {
7133 LONGEST offset;
7134
7135 /* C++ base class field. */
7136 if (handle_data_member_location (die, cu, &offset))
7137 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7138 FIELD_BITSIZE (*fp) = 0;
7139 FIELD_TYPE (*fp) = die_type (die, cu);
7140 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7141 fip->nbaseclasses++;
7142 }
7143 }
7144
7145 /* Add a typedef defined in the scope of the FIP's class. */
7146
7147 static void
7148 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7149 struct dwarf2_cu *cu)
7150 {
7151 struct objfile *objfile = cu->objfile;
7152 struct typedef_field_list *new_field;
7153 struct attribute *attr;
7154 struct typedef_field *fp;
7155 char *fieldname = "";
7156
7157 /* Allocate a new field list entry and link it in. */
7158 new_field = xzalloc (sizeof (*new_field));
7159 make_cleanup (xfree, new_field);
7160
7161 gdb_assert (die->tag == DW_TAG_typedef);
7162
7163 fp = &new_field->field;
7164
7165 /* Get name of field. */
7166 fp->name = dwarf2_name (die, cu);
7167 if (fp->name == NULL)
7168 return;
7169
7170 fp->type = read_type_die (die, cu);
7171
7172 new_field->next = fip->typedef_field_list;
7173 fip->typedef_field_list = new_field;
7174 fip->typedef_field_list_count++;
7175 }
7176
7177 /* Create the vector of fields, and attach it to the type. */
7178
7179 static void
7180 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7181 struct dwarf2_cu *cu)
7182 {
7183 int nfields = fip->nfields;
7184
7185 /* Record the field count, allocate space for the array of fields,
7186 and create blank accessibility bitfields if necessary. */
7187 TYPE_NFIELDS (type) = nfields;
7188 TYPE_FIELDS (type) = (struct field *)
7189 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7190 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7191
7192 if (fip->non_public_fields && cu->language != language_ada)
7193 {
7194 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7195
7196 TYPE_FIELD_PRIVATE_BITS (type) =
7197 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7198 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7199
7200 TYPE_FIELD_PROTECTED_BITS (type) =
7201 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7202 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7203
7204 TYPE_FIELD_IGNORE_BITS (type) =
7205 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7206 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7207 }
7208
7209 /* If the type has baseclasses, allocate and clear a bit vector for
7210 TYPE_FIELD_VIRTUAL_BITS. */
7211 if (fip->nbaseclasses && cu->language != language_ada)
7212 {
7213 int num_bytes = B_BYTES (fip->nbaseclasses);
7214 unsigned char *pointer;
7215
7216 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7217 pointer = TYPE_ALLOC (type, num_bytes);
7218 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7219 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7220 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7221 }
7222
7223 /* Copy the saved-up fields into the field vector. Start from the head of
7224 the list, adding to the tail of the field array, so that they end up in
7225 the same order in the array in which they were added to the list. */
7226 while (nfields-- > 0)
7227 {
7228 struct nextfield *fieldp;
7229
7230 if (fip->fields)
7231 {
7232 fieldp = fip->fields;
7233 fip->fields = fieldp->next;
7234 }
7235 else
7236 {
7237 fieldp = fip->baseclasses;
7238 fip->baseclasses = fieldp->next;
7239 }
7240
7241 TYPE_FIELD (type, nfields) = fieldp->field;
7242 switch (fieldp->accessibility)
7243 {
7244 case DW_ACCESS_private:
7245 if (cu->language != language_ada)
7246 SET_TYPE_FIELD_PRIVATE (type, nfields);
7247 break;
7248
7249 case DW_ACCESS_protected:
7250 if (cu->language != language_ada)
7251 SET_TYPE_FIELD_PROTECTED (type, nfields);
7252 break;
7253
7254 case DW_ACCESS_public:
7255 break;
7256
7257 default:
7258 /* Unknown accessibility. Complain and treat it as public. */
7259 {
7260 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7261 fieldp->accessibility);
7262 }
7263 break;
7264 }
7265 if (nfields < fip->nbaseclasses)
7266 {
7267 switch (fieldp->virtuality)
7268 {
7269 case DW_VIRTUALITY_virtual:
7270 case DW_VIRTUALITY_pure_virtual:
7271 if (cu->language == language_ada)
7272 error (_("unexpected virtuality in component of Ada type"));
7273 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7274 break;
7275 }
7276 }
7277 }
7278 }
7279
7280 /* Add a member function to the proper fieldlist. */
7281
7282 static void
7283 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7284 struct type *type, struct dwarf2_cu *cu)
7285 {
7286 struct objfile *objfile = cu->objfile;
7287 struct attribute *attr;
7288 struct fnfieldlist *flp;
7289 int i;
7290 struct fn_field *fnp;
7291 char *fieldname;
7292 struct nextfnfield *new_fnfield;
7293 struct type *this_type;
7294 enum dwarf_access_attribute accessibility;
7295
7296 if (cu->language == language_ada)
7297 error (_("unexpected member function in Ada type"));
7298
7299 /* Get name of member function. */
7300 fieldname = dwarf2_name (die, cu);
7301 if (fieldname == NULL)
7302 return;
7303
7304 /* Look up member function name in fieldlist. */
7305 for (i = 0; i < fip->nfnfields; i++)
7306 {
7307 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7308 break;
7309 }
7310
7311 /* Create new list element if necessary. */
7312 if (i < fip->nfnfields)
7313 flp = &fip->fnfieldlists[i];
7314 else
7315 {
7316 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7317 {
7318 fip->fnfieldlists = (struct fnfieldlist *)
7319 xrealloc (fip->fnfieldlists,
7320 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7321 * sizeof (struct fnfieldlist));
7322 if (fip->nfnfields == 0)
7323 make_cleanup (free_current_contents, &fip->fnfieldlists);
7324 }
7325 flp = &fip->fnfieldlists[fip->nfnfields];
7326 flp->name = fieldname;
7327 flp->length = 0;
7328 flp->head = NULL;
7329 i = fip->nfnfields++;
7330 }
7331
7332 /* Create a new member function field and chain it to the field list
7333 entry. */
7334 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7335 make_cleanup (xfree, new_fnfield);
7336 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7337 new_fnfield->next = flp->head;
7338 flp->head = new_fnfield;
7339 flp->length++;
7340
7341 /* Fill in the member function field info. */
7342 fnp = &new_fnfield->fnfield;
7343
7344 /* Delay processing of the physname until later. */
7345 if (cu->language == language_cplus || cu->language == language_java)
7346 {
7347 add_to_method_list (type, i, flp->length - 1, fieldname,
7348 die, cu);
7349 }
7350 else
7351 {
7352 const char *physname = dwarf2_physname (fieldname, die, cu);
7353 fnp->physname = physname ? physname : "";
7354 }
7355
7356 fnp->type = alloc_type (objfile);
7357 this_type = read_type_die (die, cu);
7358 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7359 {
7360 int nparams = TYPE_NFIELDS (this_type);
7361
7362 /* TYPE is the domain of this method, and THIS_TYPE is the type
7363 of the method itself (TYPE_CODE_METHOD). */
7364 smash_to_method_type (fnp->type, type,
7365 TYPE_TARGET_TYPE (this_type),
7366 TYPE_FIELDS (this_type),
7367 TYPE_NFIELDS (this_type),
7368 TYPE_VARARGS (this_type));
7369
7370 /* Handle static member functions.
7371 Dwarf2 has no clean way to discern C++ static and non-static
7372 member functions. G++ helps GDB by marking the first
7373 parameter for non-static member functions (which is the this
7374 pointer) as artificial. We obtain this information from
7375 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7376 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7377 fnp->voffset = VOFFSET_STATIC;
7378 }
7379 else
7380 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7381 dwarf2_full_name (fieldname, die, cu));
7382
7383 /* Get fcontext from DW_AT_containing_type if present. */
7384 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7385 fnp->fcontext = die_containing_type (die, cu);
7386
7387 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7388 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7389
7390 /* Get accessibility. */
7391 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7392 if (attr)
7393 accessibility = DW_UNSND (attr);
7394 else
7395 accessibility = dwarf2_default_access_attribute (die, cu);
7396 switch (accessibility)
7397 {
7398 case DW_ACCESS_private:
7399 fnp->is_private = 1;
7400 break;
7401 case DW_ACCESS_protected:
7402 fnp->is_protected = 1;
7403 break;
7404 }
7405
7406 /* Check for artificial methods. */
7407 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7408 if (attr && DW_UNSND (attr) != 0)
7409 fnp->is_artificial = 1;
7410
7411 /* Get index in virtual function table if it is a virtual member
7412 function. For older versions of GCC, this is an offset in the
7413 appropriate virtual table, as specified by DW_AT_containing_type.
7414 For everyone else, it is an expression to be evaluated relative
7415 to the object address. */
7416
7417 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7418 if (attr)
7419 {
7420 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7421 {
7422 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7423 {
7424 /* Old-style GCC. */
7425 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7426 }
7427 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7428 || (DW_BLOCK (attr)->size > 1
7429 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7430 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7431 {
7432 struct dwarf_block blk;
7433 int offset;
7434
7435 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7436 ? 1 : 2);
7437 blk.size = DW_BLOCK (attr)->size - offset;
7438 blk.data = DW_BLOCK (attr)->data + offset;
7439 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7440 if ((fnp->voffset % cu->header.addr_size) != 0)
7441 dwarf2_complex_location_expr_complaint ();
7442 else
7443 fnp->voffset /= cu->header.addr_size;
7444 fnp->voffset += 2;
7445 }
7446 else
7447 dwarf2_complex_location_expr_complaint ();
7448
7449 if (!fnp->fcontext)
7450 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7451 }
7452 else if (attr_form_is_section_offset (attr))
7453 {
7454 dwarf2_complex_location_expr_complaint ();
7455 }
7456 else
7457 {
7458 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7459 fieldname);
7460 }
7461 }
7462 else
7463 {
7464 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7465 if (attr && DW_UNSND (attr))
7466 {
7467 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7468 complaint (&symfile_complaints,
7469 _("Member function \"%s\" (offset %d) is virtual "
7470 "but the vtable offset is not specified"),
7471 fieldname, die->offset);
7472 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7473 TYPE_CPLUS_DYNAMIC (type) = 1;
7474 }
7475 }
7476 }
7477
7478 /* Create the vector of member function fields, and attach it to the type. */
7479
7480 static void
7481 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7482 struct dwarf2_cu *cu)
7483 {
7484 struct fnfieldlist *flp;
7485 int total_length = 0;
7486 int i;
7487
7488 if (cu->language == language_ada)
7489 error (_("unexpected member functions in Ada type"));
7490
7491 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7492 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7493 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7494
7495 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7496 {
7497 struct nextfnfield *nfp = flp->head;
7498 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7499 int k;
7500
7501 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7502 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7503 fn_flp->fn_fields = (struct fn_field *)
7504 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7505 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7506 fn_flp->fn_fields[k] = nfp->fnfield;
7507
7508 total_length += flp->length;
7509 }
7510
7511 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7512 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7513 }
7514
7515 /* Returns non-zero if NAME is the name of a vtable member in CU's
7516 language, zero otherwise. */
7517 static int
7518 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7519 {
7520 static const char vptr[] = "_vptr";
7521 static const char vtable[] = "vtable";
7522
7523 /* Look for the C++ and Java forms of the vtable. */
7524 if ((cu->language == language_java
7525 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7526 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7527 && is_cplus_marker (name[sizeof (vptr) - 1])))
7528 return 1;
7529
7530 return 0;
7531 }
7532
7533 /* GCC outputs unnamed structures that are really pointers to member
7534 functions, with the ABI-specified layout. If TYPE describes
7535 such a structure, smash it into a member function type.
7536
7537 GCC shouldn't do this; it should just output pointer to member DIEs.
7538 This is GCC PR debug/28767. */
7539
7540 static void
7541 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7542 {
7543 struct type *pfn_type, *domain_type, *new_type;
7544
7545 /* Check for a structure with no name and two children. */
7546 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7547 return;
7548
7549 /* Check for __pfn and __delta members. */
7550 if (TYPE_FIELD_NAME (type, 0) == NULL
7551 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7552 || TYPE_FIELD_NAME (type, 1) == NULL
7553 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7554 return;
7555
7556 /* Find the type of the method. */
7557 pfn_type = TYPE_FIELD_TYPE (type, 0);
7558 if (pfn_type == NULL
7559 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7560 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7561 return;
7562
7563 /* Look for the "this" argument. */
7564 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7565 if (TYPE_NFIELDS (pfn_type) == 0
7566 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7567 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7568 return;
7569
7570 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7571 new_type = alloc_type (objfile);
7572 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7573 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7574 TYPE_VARARGS (pfn_type));
7575 smash_to_methodptr_type (type, new_type);
7576 }
7577
7578 /* Called when we find the DIE that starts a structure or union scope
7579 (definition) to create a type for the structure or union. Fill in
7580 the type's name and general properties; the members will not be
7581 processed until process_structure_type.
7582
7583 NOTE: we need to call these functions regardless of whether or not the
7584 DIE has a DW_AT_name attribute, since it might be an anonymous
7585 structure or union. This gets the type entered into our set of
7586 user defined types.
7587
7588 However, if the structure is incomplete (an opaque struct/union)
7589 then suppress creating a symbol table entry for it since gdb only
7590 wants to find the one with the complete definition. Note that if
7591 it is complete, we just call new_symbol, which does it's own
7592 checking about whether the struct/union is anonymous or not (and
7593 suppresses creating a symbol table entry itself). */
7594
7595 static struct type *
7596 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7597 {
7598 struct objfile *objfile = cu->objfile;
7599 struct type *type;
7600 struct attribute *attr;
7601 char *name;
7602
7603 /* If the definition of this type lives in .debug_types, read that type.
7604 Don't follow DW_AT_specification though, that will take us back up
7605 the chain and we want to go down. */
7606 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7607 if (attr)
7608 {
7609 struct dwarf2_cu *type_cu = cu;
7610 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7611
7612 /* We could just recurse on read_structure_type, but we need to call
7613 get_die_type to ensure only one type for this DIE is created.
7614 This is important, for example, because for c++ classes we need
7615 TYPE_NAME set which is only done by new_symbol. Blech. */
7616 type = read_type_die (type_die, type_cu);
7617
7618 /* TYPE_CU may not be the same as CU.
7619 Ensure TYPE is recorded in CU's type_hash table. */
7620 return set_die_type (die, type, cu);
7621 }
7622
7623 type = alloc_type (objfile);
7624 INIT_CPLUS_SPECIFIC (type);
7625
7626 name = dwarf2_name (die, cu);
7627 if (name != NULL)
7628 {
7629 if (cu->language == language_cplus
7630 || cu->language == language_java)
7631 {
7632 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7633
7634 /* dwarf2_full_name might have already finished building the DIE's
7635 type. If so, there is no need to continue. */
7636 if (get_die_type (die, cu) != NULL)
7637 return get_die_type (die, cu);
7638
7639 TYPE_TAG_NAME (type) = full_name;
7640 if (die->tag == DW_TAG_structure_type
7641 || die->tag == DW_TAG_class_type)
7642 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7643 }
7644 else
7645 {
7646 /* The name is already allocated along with this objfile, so
7647 we don't need to duplicate it for the type. */
7648 TYPE_TAG_NAME (type) = (char *) name;
7649 if (die->tag == DW_TAG_class_type)
7650 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7651 }
7652 }
7653
7654 if (die->tag == DW_TAG_structure_type)
7655 {
7656 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7657 }
7658 else if (die->tag == DW_TAG_union_type)
7659 {
7660 TYPE_CODE (type) = TYPE_CODE_UNION;
7661 }
7662 else
7663 {
7664 TYPE_CODE (type) = TYPE_CODE_CLASS;
7665 }
7666
7667 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7668 TYPE_DECLARED_CLASS (type) = 1;
7669
7670 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7671 if (attr)
7672 {
7673 TYPE_LENGTH (type) = DW_UNSND (attr);
7674 }
7675 else
7676 {
7677 TYPE_LENGTH (type) = 0;
7678 }
7679
7680 TYPE_STUB_SUPPORTED (type) = 1;
7681 if (die_is_declaration (die, cu))
7682 TYPE_STUB (type) = 1;
7683 else if (attr == NULL && die->child == NULL
7684 && producer_is_realview (cu->producer))
7685 /* RealView does not output the required DW_AT_declaration
7686 on incomplete types. */
7687 TYPE_STUB (type) = 1;
7688
7689 /* We need to add the type field to the die immediately so we don't
7690 infinitely recurse when dealing with pointers to the structure
7691 type within the structure itself. */
7692 set_die_type (die, type, cu);
7693
7694 /* set_die_type should be already done. */
7695 set_descriptive_type (type, die, cu);
7696
7697 return type;
7698 }
7699
7700 /* Finish creating a structure or union type, including filling in
7701 its members and creating a symbol for it. */
7702
7703 static void
7704 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7705 {
7706 struct objfile *objfile = cu->objfile;
7707 struct die_info *child_die = die->child;
7708 struct type *type;
7709
7710 type = get_die_type (die, cu);
7711 if (type == NULL)
7712 type = read_structure_type (die, cu);
7713
7714 if (die->child != NULL && ! die_is_declaration (die, cu))
7715 {
7716 struct field_info fi;
7717 struct die_info *child_die;
7718 VEC (symbolp) *template_args = NULL;
7719 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7720
7721 memset (&fi, 0, sizeof (struct field_info));
7722
7723 child_die = die->child;
7724
7725 while (child_die && child_die->tag)
7726 {
7727 if (child_die->tag == DW_TAG_member
7728 || child_die->tag == DW_TAG_variable)
7729 {
7730 /* NOTE: carlton/2002-11-05: A C++ static data member
7731 should be a DW_TAG_member that is a declaration, but
7732 all versions of G++ as of this writing (so through at
7733 least 3.2.1) incorrectly generate DW_TAG_variable
7734 tags for them instead. */
7735 dwarf2_add_field (&fi, child_die, cu);
7736 }
7737 else if (child_die->tag == DW_TAG_subprogram)
7738 {
7739 /* C++ member function. */
7740 dwarf2_add_member_fn (&fi, child_die, type, cu);
7741 }
7742 else if (child_die->tag == DW_TAG_inheritance)
7743 {
7744 /* C++ base class field. */
7745 dwarf2_add_field (&fi, child_die, cu);
7746 }
7747 else if (child_die->tag == DW_TAG_typedef)
7748 dwarf2_add_typedef (&fi, child_die, cu);
7749 else if (child_die->tag == DW_TAG_template_type_param
7750 || child_die->tag == DW_TAG_template_value_param)
7751 {
7752 struct symbol *arg = new_symbol (child_die, NULL, cu);
7753
7754 if (arg != NULL)
7755 VEC_safe_push (symbolp, template_args, arg);
7756 }
7757
7758 child_die = sibling_die (child_die);
7759 }
7760
7761 /* Attach template arguments to type. */
7762 if (! VEC_empty (symbolp, template_args))
7763 {
7764 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7765 TYPE_N_TEMPLATE_ARGUMENTS (type)
7766 = VEC_length (symbolp, template_args);
7767 TYPE_TEMPLATE_ARGUMENTS (type)
7768 = obstack_alloc (&objfile->objfile_obstack,
7769 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7770 * sizeof (struct symbol *)));
7771 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7772 VEC_address (symbolp, template_args),
7773 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7774 * sizeof (struct symbol *)));
7775 VEC_free (symbolp, template_args);
7776 }
7777
7778 /* Attach fields and member functions to the type. */
7779 if (fi.nfields)
7780 dwarf2_attach_fields_to_type (&fi, type, cu);
7781 if (fi.nfnfields)
7782 {
7783 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7784
7785 /* Get the type which refers to the base class (possibly this
7786 class itself) which contains the vtable pointer for the current
7787 class from the DW_AT_containing_type attribute. This use of
7788 DW_AT_containing_type is a GNU extension. */
7789
7790 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7791 {
7792 struct type *t = die_containing_type (die, cu);
7793
7794 TYPE_VPTR_BASETYPE (type) = t;
7795 if (type == t)
7796 {
7797 int i;
7798
7799 /* Our own class provides vtbl ptr. */
7800 for (i = TYPE_NFIELDS (t) - 1;
7801 i >= TYPE_N_BASECLASSES (t);
7802 --i)
7803 {
7804 char *fieldname = TYPE_FIELD_NAME (t, i);
7805
7806 if (is_vtable_name (fieldname, cu))
7807 {
7808 TYPE_VPTR_FIELDNO (type) = i;
7809 break;
7810 }
7811 }
7812
7813 /* Complain if virtual function table field not found. */
7814 if (i < TYPE_N_BASECLASSES (t))
7815 complaint (&symfile_complaints,
7816 _("virtual function table pointer "
7817 "not found when defining class '%s'"),
7818 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7819 "");
7820 }
7821 else
7822 {
7823 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7824 }
7825 }
7826 else if (cu->producer
7827 && strncmp (cu->producer,
7828 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7829 {
7830 /* The IBM XLC compiler does not provide direct indication
7831 of the containing type, but the vtable pointer is
7832 always named __vfp. */
7833
7834 int i;
7835
7836 for (i = TYPE_NFIELDS (type) - 1;
7837 i >= TYPE_N_BASECLASSES (type);
7838 --i)
7839 {
7840 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7841 {
7842 TYPE_VPTR_FIELDNO (type) = i;
7843 TYPE_VPTR_BASETYPE (type) = type;
7844 break;
7845 }
7846 }
7847 }
7848 }
7849
7850 /* Copy fi.typedef_field_list linked list elements content into the
7851 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7852 if (fi.typedef_field_list)
7853 {
7854 int i = fi.typedef_field_list_count;
7855
7856 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7857 TYPE_TYPEDEF_FIELD_ARRAY (type)
7858 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7859 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7860
7861 /* Reverse the list order to keep the debug info elements order. */
7862 while (--i >= 0)
7863 {
7864 struct typedef_field *dest, *src;
7865
7866 dest = &TYPE_TYPEDEF_FIELD (type, i);
7867 src = &fi.typedef_field_list->field;
7868 fi.typedef_field_list = fi.typedef_field_list->next;
7869 *dest = *src;
7870 }
7871 }
7872
7873 do_cleanups (back_to);
7874
7875 if (HAVE_CPLUS_STRUCT (type))
7876 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7877 }
7878
7879 quirk_gcc_member_function_pointer (type, objfile);
7880
7881 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7882 snapshots) has been known to create a die giving a declaration
7883 for a class that has, as a child, a die giving a definition for a
7884 nested class. So we have to process our children even if the
7885 current die is a declaration. Normally, of course, a declaration
7886 won't have any children at all. */
7887
7888 while (child_die != NULL && child_die->tag)
7889 {
7890 if (child_die->tag == DW_TAG_member
7891 || child_die->tag == DW_TAG_variable
7892 || child_die->tag == DW_TAG_inheritance
7893 || child_die->tag == DW_TAG_template_value_param
7894 || child_die->tag == DW_TAG_template_type_param)
7895 {
7896 /* Do nothing. */
7897 }
7898 else
7899 process_die (child_die, cu);
7900
7901 child_die = sibling_die (child_die);
7902 }
7903
7904 /* Do not consider external references. According to the DWARF standard,
7905 these DIEs are identified by the fact that they have no byte_size
7906 attribute, and a declaration attribute. */
7907 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7908 || !die_is_declaration (die, cu))
7909 new_symbol (die, type, cu);
7910 }
7911
7912 /* Given a DW_AT_enumeration_type die, set its type. We do not
7913 complete the type's fields yet, or create any symbols. */
7914
7915 static struct type *
7916 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7917 {
7918 struct objfile *objfile = cu->objfile;
7919 struct type *type;
7920 struct attribute *attr;
7921 const char *name;
7922
7923 /* If the definition of this type lives in .debug_types, read that type.
7924 Don't follow DW_AT_specification though, that will take us back up
7925 the chain and we want to go down. */
7926 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7927 if (attr)
7928 {
7929 struct dwarf2_cu *type_cu = cu;
7930 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7931
7932 type = read_type_die (type_die, type_cu);
7933
7934 /* TYPE_CU may not be the same as CU.
7935 Ensure TYPE is recorded in CU's type_hash table. */
7936 return set_die_type (die, type, cu);
7937 }
7938
7939 type = alloc_type (objfile);
7940
7941 TYPE_CODE (type) = TYPE_CODE_ENUM;
7942 name = dwarf2_full_name (NULL, die, cu);
7943 if (name != NULL)
7944 TYPE_TAG_NAME (type) = (char *) name;
7945
7946 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7947 if (attr)
7948 {
7949 TYPE_LENGTH (type) = DW_UNSND (attr);
7950 }
7951 else
7952 {
7953 TYPE_LENGTH (type) = 0;
7954 }
7955
7956 /* The enumeration DIE can be incomplete. In Ada, any type can be
7957 declared as private in the package spec, and then defined only
7958 inside the package body. Such types are known as Taft Amendment
7959 Types. When another package uses such a type, an incomplete DIE
7960 may be generated by the compiler. */
7961 if (die_is_declaration (die, cu))
7962 TYPE_STUB (type) = 1;
7963
7964 return set_die_type (die, type, cu);
7965 }
7966
7967 /* Given a pointer to a die which begins an enumeration, process all
7968 the dies that define the members of the enumeration, and create the
7969 symbol for the enumeration type.
7970
7971 NOTE: We reverse the order of the element list. */
7972
7973 static void
7974 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7975 {
7976 struct type *this_type;
7977
7978 this_type = get_die_type (die, cu);
7979 if (this_type == NULL)
7980 this_type = read_enumeration_type (die, cu);
7981
7982 if (die->child != NULL)
7983 {
7984 struct die_info *child_die;
7985 struct symbol *sym;
7986 struct field *fields = NULL;
7987 int num_fields = 0;
7988 int unsigned_enum = 1;
7989 char *name;
7990
7991 child_die = die->child;
7992 while (child_die && child_die->tag)
7993 {
7994 if (child_die->tag != DW_TAG_enumerator)
7995 {
7996 process_die (child_die, cu);
7997 }
7998 else
7999 {
8000 name = dwarf2_name (child_die, cu);
8001 if (name)
8002 {
8003 sym = new_symbol (child_die, this_type, cu);
8004 if (SYMBOL_VALUE (sym) < 0)
8005 unsigned_enum = 0;
8006
8007 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8008 {
8009 fields = (struct field *)
8010 xrealloc (fields,
8011 (num_fields + DW_FIELD_ALLOC_CHUNK)
8012 * sizeof (struct field));
8013 }
8014
8015 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8016 FIELD_TYPE (fields[num_fields]) = NULL;
8017 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8018 FIELD_BITSIZE (fields[num_fields]) = 0;
8019
8020 num_fields++;
8021 }
8022 }
8023
8024 child_die = sibling_die (child_die);
8025 }
8026
8027 if (num_fields)
8028 {
8029 TYPE_NFIELDS (this_type) = num_fields;
8030 TYPE_FIELDS (this_type) = (struct field *)
8031 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8032 memcpy (TYPE_FIELDS (this_type), fields,
8033 sizeof (struct field) * num_fields);
8034 xfree (fields);
8035 }
8036 if (unsigned_enum)
8037 TYPE_UNSIGNED (this_type) = 1;
8038 }
8039
8040 /* If we are reading an enum from a .debug_types unit, and the enum
8041 is a declaration, and the enum is not the signatured type in the
8042 unit, then we do not want to add a symbol for it. Adding a
8043 symbol would in some cases obscure the true definition of the
8044 enum, giving users an incomplete type when the definition is
8045 actually available. Note that we do not want to do this for all
8046 enums which are just declarations, because C++0x allows forward
8047 enum declarations. */
8048 if (cu->per_cu->debug_types_section
8049 && die_is_declaration (die, cu))
8050 {
8051 struct signatured_type *type_sig;
8052
8053 type_sig
8054 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8055 cu->per_cu->debug_types_section,
8056 cu->per_cu->offset);
8057 if (type_sig->type_offset != die->offset)
8058 return;
8059 }
8060
8061 new_symbol (die, this_type, cu);
8062 }
8063
8064 /* Extract all information from a DW_TAG_array_type DIE and put it in
8065 the DIE's type field. For now, this only handles one dimensional
8066 arrays. */
8067
8068 static struct type *
8069 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8070 {
8071 struct objfile *objfile = cu->objfile;
8072 struct die_info *child_die;
8073 struct type *type;
8074 struct type *element_type, *range_type, *index_type;
8075 struct type **range_types = NULL;
8076 struct attribute *attr;
8077 int ndim = 0;
8078 struct cleanup *back_to;
8079 char *name;
8080
8081 element_type = die_type (die, cu);
8082
8083 /* The die_type call above may have already set the type for this DIE. */
8084 type = get_die_type (die, cu);
8085 if (type)
8086 return type;
8087
8088 /* Irix 6.2 native cc creates array types without children for
8089 arrays with unspecified length. */
8090 if (die->child == NULL)
8091 {
8092 index_type = objfile_type (objfile)->builtin_int;
8093 range_type = create_range_type (NULL, index_type, 0, -1);
8094 type = create_array_type (NULL, element_type, range_type);
8095 return set_die_type (die, type, cu);
8096 }
8097
8098 back_to = make_cleanup (null_cleanup, NULL);
8099 child_die = die->child;
8100 while (child_die && child_die->tag)
8101 {
8102 if (child_die->tag == DW_TAG_subrange_type)
8103 {
8104 struct type *child_type = read_type_die (child_die, cu);
8105
8106 if (child_type != NULL)
8107 {
8108 /* The range type was succesfully read. Save it for the
8109 array type creation. */
8110 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8111 {
8112 range_types = (struct type **)
8113 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8114 * sizeof (struct type *));
8115 if (ndim == 0)
8116 make_cleanup (free_current_contents, &range_types);
8117 }
8118 range_types[ndim++] = child_type;
8119 }
8120 }
8121 child_die = sibling_die (child_die);
8122 }
8123
8124 /* Dwarf2 dimensions are output from left to right, create the
8125 necessary array types in backwards order. */
8126
8127 type = element_type;
8128
8129 if (read_array_order (die, cu) == DW_ORD_col_major)
8130 {
8131 int i = 0;
8132
8133 while (i < ndim)
8134 type = create_array_type (NULL, type, range_types[i++]);
8135 }
8136 else
8137 {
8138 while (ndim-- > 0)
8139 type = create_array_type (NULL, type, range_types[ndim]);
8140 }
8141
8142 /* Understand Dwarf2 support for vector types (like they occur on
8143 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8144 array type. This is not part of the Dwarf2/3 standard yet, but a
8145 custom vendor extension. The main difference between a regular
8146 array and the vector variant is that vectors are passed by value
8147 to functions. */
8148 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8149 if (attr)
8150 make_vector_type (type);
8151
8152 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8153 implementation may choose to implement triple vectors using this
8154 attribute. */
8155 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8156 if (attr)
8157 {
8158 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8159 TYPE_LENGTH (type) = DW_UNSND (attr);
8160 else
8161 complaint (&symfile_complaints,
8162 _("DW_AT_byte_size for array type smaller "
8163 "than the total size of elements"));
8164 }
8165
8166 name = dwarf2_name (die, cu);
8167 if (name)
8168 TYPE_NAME (type) = name;
8169
8170 /* Install the type in the die. */
8171 set_die_type (die, type, cu);
8172
8173 /* set_die_type should be already done. */
8174 set_descriptive_type (type, die, cu);
8175
8176 do_cleanups (back_to);
8177
8178 return type;
8179 }
8180
8181 static enum dwarf_array_dim_ordering
8182 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8183 {
8184 struct attribute *attr;
8185
8186 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8187
8188 if (attr) return DW_SND (attr);
8189
8190 /* GNU F77 is a special case, as at 08/2004 array type info is the
8191 opposite order to the dwarf2 specification, but data is still
8192 laid out as per normal fortran.
8193
8194 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8195 version checking. */
8196
8197 if (cu->language == language_fortran
8198 && cu->producer && strstr (cu->producer, "GNU F77"))
8199 {
8200 return DW_ORD_row_major;
8201 }
8202
8203 switch (cu->language_defn->la_array_ordering)
8204 {
8205 case array_column_major:
8206 return DW_ORD_col_major;
8207 case array_row_major:
8208 default:
8209 return DW_ORD_row_major;
8210 };
8211 }
8212
8213 /* Extract all information from a DW_TAG_set_type DIE and put it in
8214 the DIE's type field. */
8215
8216 static struct type *
8217 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8218 {
8219 struct type *domain_type, *set_type;
8220 struct attribute *attr;
8221
8222 domain_type = die_type (die, cu);
8223
8224 /* The die_type call above may have already set the type for this DIE. */
8225 set_type = get_die_type (die, cu);
8226 if (set_type)
8227 return set_type;
8228
8229 set_type = create_set_type (NULL, domain_type);
8230
8231 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8232 if (attr)
8233 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8234
8235 return set_die_type (die, set_type, cu);
8236 }
8237
8238 /* First cut: install each common block member as a global variable. */
8239
8240 static void
8241 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8242 {
8243 struct die_info *child_die;
8244 struct attribute *attr;
8245 struct symbol *sym;
8246 CORE_ADDR base = (CORE_ADDR) 0;
8247
8248 attr = dwarf2_attr (die, DW_AT_location, cu);
8249 if (attr)
8250 {
8251 /* Support the .debug_loc offsets. */
8252 if (attr_form_is_block (attr))
8253 {
8254 base = decode_locdesc (DW_BLOCK (attr), cu);
8255 }
8256 else if (attr_form_is_section_offset (attr))
8257 {
8258 dwarf2_complex_location_expr_complaint ();
8259 }
8260 else
8261 {
8262 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8263 "common block member");
8264 }
8265 }
8266 if (die->child != NULL)
8267 {
8268 child_die = die->child;
8269 while (child_die && child_die->tag)
8270 {
8271 LONGEST offset;
8272
8273 sym = new_symbol (child_die, NULL, cu);
8274 if (sym != NULL
8275 && handle_data_member_location (child_die, cu, &offset))
8276 {
8277 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8278 add_symbol_to_list (sym, &global_symbols);
8279 }
8280 child_die = sibling_die (child_die);
8281 }
8282 }
8283 }
8284
8285 /* Create a type for a C++ namespace. */
8286
8287 static struct type *
8288 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8289 {
8290 struct objfile *objfile = cu->objfile;
8291 const char *previous_prefix, *name;
8292 int is_anonymous;
8293 struct type *type;
8294
8295 /* For extensions, reuse the type of the original namespace. */
8296 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8297 {
8298 struct die_info *ext_die;
8299 struct dwarf2_cu *ext_cu = cu;
8300
8301 ext_die = dwarf2_extension (die, &ext_cu);
8302 type = read_type_die (ext_die, ext_cu);
8303
8304 /* EXT_CU may not be the same as CU.
8305 Ensure TYPE is recorded in CU's type_hash table. */
8306 return set_die_type (die, type, cu);
8307 }
8308
8309 name = namespace_name (die, &is_anonymous, cu);
8310
8311 /* Now build the name of the current namespace. */
8312
8313 previous_prefix = determine_prefix (die, cu);
8314 if (previous_prefix[0] != '\0')
8315 name = typename_concat (&objfile->objfile_obstack,
8316 previous_prefix, name, 0, cu);
8317
8318 /* Create the type. */
8319 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8320 objfile);
8321 TYPE_NAME (type) = (char *) name;
8322 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8323
8324 return set_die_type (die, type, cu);
8325 }
8326
8327 /* Read a C++ namespace. */
8328
8329 static void
8330 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8331 {
8332 struct objfile *objfile = cu->objfile;
8333 int is_anonymous;
8334
8335 /* Add a symbol associated to this if we haven't seen the namespace
8336 before. Also, add a using directive if it's an anonymous
8337 namespace. */
8338
8339 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8340 {
8341 struct type *type;
8342
8343 type = read_type_die (die, cu);
8344 new_symbol (die, type, cu);
8345
8346 namespace_name (die, &is_anonymous, cu);
8347 if (is_anonymous)
8348 {
8349 const char *previous_prefix = determine_prefix (die, cu);
8350
8351 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8352 NULL, NULL, &objfile->objfile_obstack);
8353 }
8354 }
8355
8356 if (die->child != NULL)
8357 {
8358 struct die_info *child_die = die->child;
8359
8360 while (child_die && child_die->tag)
8361 {
8362 process_die (child_die, cu);
8363 child_die = sibling_die (child_die);
8364 }
8365 }
8366 }
8367
8368 /* Read a Fortran module as type. This DIE can be only a declaration used for
8369 imported module. Still we need that type as local Fortran "use ... only"
8370 declaration imports depend on the created type in determine_prefix. */
8371
8372 static struct type *
8373 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8374 {
8375 struct objfile *objfile = cu->objfile;
8376 char *module_name;
8377 struct type *type;
8378
8379 module_name = dwarf2_name (die, cu);
8380 if (!module_name)
8381 complaint (&symfile_complaints,
8382 _("DW_TAG_module has no name, offset 0x%x"),
8383 die->offset);
8384 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8385
8386 /* determine_prefix uses TYPE_TAG_NAME. */
8387 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8388
8389 return set_die_type (die, type, cu);
8390 }
8391
8392 /* Read a Fortran module. */
8393
8394 static void
8395 read_module (struct die_info *die, struct dwarf2_cu *cu)
8396 {
8397 struct die_info *child_die = die->child;
8398
8399 while (child_die && child_die->tag)
8400 {
8401 process_die (child_die, cu);
8402 child_die = sibling_die (child_die);
8403 }
8404 }
8405
8406 /* Return the name of the namespace represented by DIE. Set
8407 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8408 namespace. */
8409
8410 static const char *
8411 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8412 {
8413 struct die_info *current_die;
8414 const char *name = NULL;
8415
8416 /* Loop through the extensions until we find a name. */
8417
8418 for (current_die = die;
8419 current_die != NULL;
8420 current_die = dwarf2_extension (die, &cu))
8421 {
8422 name = dwarf2_name (current_die, cu);
8423 if (name != NULL)
8424 break;
8425 }
8426
8427 /* Is it an anonymous namespace? */
8428
8429 *is_anonymous = (name == NULL);
8430 if (*is_anonymous)
8431 name = CP_ANONYMOUS_NAMESPACE_STR;
8432
8433 return name;
8434 }
8435
8436 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8437 the user defined type vector. */
8438
8439 static struct type *
8440 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8441 {
8442 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8443 struct comp_unit_head *cu_header = &cu->header;
8444 struct type *type;
8445 struct attribute *attr_byte_size;
8446 struct attribute *attr_address_class;
8447 int byte_size, addr_class;
8448 struct type *target_type;
8449
8450 target_type = die_type (die, cu);
8451
8452 /* The die_type call above may have already set the type for this DIE. */
8453 type = get_die_type (die, cu);
8454 if (type)
8455 return type;
8456
8457 type = lookup_pointer_type (target_type);
8458
8459 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8460 if (attr_byte_size)
8461 byte_size = DW_UNSND (attr_byte_size);
8462 else
8463 byte_size = cu_header->addr_size;
8464
8465 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8466 if (attr_address_class)
8467 addr_class = DW_UNSND (attr_address_class);
8468 else
8469 addr_class = DW_ADDR_none;
8470
8471 /* If the pointer size or address class is different than the
8472 default, create a type variant marked as such and set the
8473 length accordingly. */
8474 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8475 {
8476 if (gdbarch_address_class_type_flags_p (gdbarch))
8477 {
8478 int type_flags;
8479
8480 type_flags = gdbarch_address_class_type_flags
8481 (gdbarch, byte_size, addr_class);
8482 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8483 == 0);
8484 type = make_type_with_address_space (type, type_flags);
8485 }
8486 else if (TYPE_LENGTH (type) != byte_size)
8487 {
8488 complaint (&symfile_complaints,
8489 _("invalid pointer size %d"), byte_size);
8490 }
8491 else
8492 {
8493 /* Should we also complain about unhandled address classes? */
8494 }
8495 }
8496
8497 TYPE_LENGTH (type) = byte_size;
8498 return set_die_type (die, type, cu);
8499 }
8500
8501 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8502 the user defined type vector. */
8503
8504 static struct type *
8505 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8506 {
8507 struct type *type;
8508 struct type *to_type;
8509 struct type *domain;
8510
8511 to_type = die_type (die, cu);
8512 domain = die_containing_type (die, cu);
8513
8514 /* The calls above may have already set the type for this DIE. */
8515 type = get_die_type (die, cu);
8516 if (type)
8517 return type;
8518
8519 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8520 type = lookup_methodptr_type (to_type);
8521 else
8522 type = lookup_memberptr_type (to_type, domain);
8523
8524 return set_die_type (die, type, cu);
8525 }
8526
8527 /* Extract all information from a DW_TAG_reference_type DIE and add to
8528 the user defined type vector. */
8529
8530 static struct type *
8531 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8532 {
8533 struct comp_unit_head *cu_header = &cu->header;
8534 struct type *type, *target_type;
8535 struct attribute *attr;
8536
8537 target_type = die_type (die, cu);
8538
8539 /* The die_type call above may have already set the type for this DIE. */
8540 type = get_die_type (die, cu);
8541 if (type)
8542 return type;
8543
8544 type = lookup_reference_type (target_type);
8545 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8546 if (attr)
8547 {
8548 TYPE_LENGTH (type) = DW_UNSND (attr);
8549 }
8550 else
8551 {
8552 TYPE_LENGTH (type) = cu_header->addr_size;
8553 }
8554 return set_die_type (die, type, cu);
8555 }
8556
8557 static struct type *
8558 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8559 {
8560 struct type *base_type, *cv_type;
8561
8562 base_type = die_type (die, cu);
8563
8564 /* The die_type call above may have already set the type for this DIE. */
8565 cv_type = get_die_type (die, cu);
8566 if (cv_type)
8567 return cv_type;
8568
8569 /* In case the const qualifier is applied to an array type, the element type
8570 is so qualified, not the array type (section 6.7.3 of C99). */
8571 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8572 {
8573 struct type *el_type, *inner_array;
8574
8575 base_type = copy_type (base_type);
8576 inner_array = base_type;
8577
8578 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8579 {
8580 TYPE_TARGET_TYPE (inner_array) =
8581 copy_type (TYPE_TARGET_TYPE (inner_array));
8582 inner_array = TYPE_TARGET_TYPE (inner_array);
8583 }
8584
8585 el_type = TYPE_TARGET_TYPE (inner_array);
8586 TYPE_TARGET_TYPE (inner_array) =
8587 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8588
8589 return set_die_type (die, base_type, cu);
8590 }
8591
8592 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8593 return set_die_type (die, cv_type, cu);
8594 }
8595
8596 static struct type *
8597 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8598 {
8599 struct type *base_type, *cv_type;
8600
8601 base_type = die_type (die, cu);
8602
8603 /* The die_type call above may have already set the type for this DIE. */
8604 cv_type = get_die_type (die, cu);
8605 if (cv_type)
8606 return cv_type;
8607
8608 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8609 return set_die_type (die, cv_type, cu);
8610 }
8611
8612 /* Extract all information from a DW_TAG_string_type DIE and add to
8613 the user defined type vector. It isn't really a user defined type,
8614 but it behaves like one, with other DIE's using an AT_user_def_type
8615 attribute to reference it. */
8616
8617 static struct type *
8618 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8619 {
8620 struct objfile *objfile = cu->objfile;
8621 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8622 struct type *type, *range_type, *index_type, *char_type;
8623 struct attribute *attr;
8624 unsigned int length;
8625
8626 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8627 if (attr)
8628 {
8629 length = DW_UNSND (attr);
8630 }
8631 else
8632 {
8633 /* Check for the DW_AT_byte_size attribute. */
8634 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8635 if (attr)
8636 {
8637 length = DW_UNSND (attr);
8638 }
8639 else
8640 {
8641 length = 1;
8642 }
8643 }
8644
8645 index_type = objfile_type (objfile)->builtin_int;
8646 range_type = create_range_type (NULL, index_type, 1, length);
8647 char_type = language_string_char_type (cu->language_defn, gdbarch);
8648 type = create_string_type (NULL, char_type, range_type);
8649
8650 return set_die_type (die, type, cu);
8651 }
8652
8653 /* Handle DIES due to C code like:
8654
8655 struct foo
8656 {
8657 int (*funcp)(int a, long l);
8658 int b;
8659 };
8660
8661 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8662
8663 static struct type *
8664 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8665 {
8666 struct objfile *objfile = cu->objfile;
8667 struct type *type; /* Type that this function returns. */
8668 struct type *ftype; /* Function that returns above type. */
8669 struct attribute *attr;
8670
8671 type = die_type (die, cu);
8672
8673 /* The die_type call above may have already set the type for this DIE. */
8674 ftype = get_die_type (die, cu);
8675 if (ftype)
8676 return ftype;
8677
8678 ftype = lookup_function_type (type);
8679
8680 /* All functions in C++, Pascal and Java have prototypes. */
8681 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8682 if ((attr && (DW_UNSND (attr) != 0))
8683 || cu->language == language_cplus
8684 || cu->language == language_java
8685 || cu->language == language_pascal)
8686 TYPE_PROTOTYPED (ftype) = 1;
8687 else if (producer_is_realview (cu->producer))
8688 /* RealView does not emit DW_AT_prototyped. We can not
8689 distinguish prototyped and unprototyped functions; default to
8690 prototyped, since that is more common in modern code (and
8691 RealView warns about unprototyped functions). */
8692 TYPE_PROTOTYPED (ftype) = 1;
8693
8694 /* Store the calling convention in the type if it's available in
8695 the subroutine die. Otherwise set the calling convention to
8696 the default value DW_CC_normal. */
8697 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8698 if (attr)
8699 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8700 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8701 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8702 else
8703 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8704
8705 /* We need to add the subroutine type to the die immediately so
8706 we don't infinitely recurse when dealing with parameters
8707 declared as the same subroutine type. */
8708 set_die_type (die, ftype, cu);
8709
8710 if (die->child != NULL)
8711 {
8712 struct type *void_type = objfile_type (objfile)->builtin_void;
8713 struct die_info *child_die;
8714 int nparams, iparams;
8715
8716 /* Count the number of parameters.
8717 FIXME: GDB currently ignores vararg functions, but knows about
8718 vararg member functions. */
8719 nparams = 0;
8720 child_die = die->child;
8721 while (child_die && child_die->tag)
8722 {
8723 if (child_die->tag == DW_TAG_formal_parameter)
8724 nparams++;
8725 else if (child_die->tag == DW_TAG_unspecified_parameters)
8726 TYPE_VARARGS (ftype) = 1;
8727 child_die = sibling_die (child_die);
8728 }
8729
8730 /* Allocate storage for parameters and fill them in. */
8731 TYPE_NFIELDS (ftype) = nparams;
8732 TYPE_FIELDS (ftype) = (struct field *)
8733 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8734
8735 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8736 even if we error out during the parameters reading below. */
8737 for (iparams = 0; iparams < nparams; iparams++)
8738 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8739
8740 iparams = 0;
8741 child_die = die->child;
8742 while (child_die && child_die->tag)
8743 {
8744 if (child_die->tag == DW_TAG_formal_parameter)
8745 {
8746 struct type *arg_type;
8747
8748 /* DWARF version 2 has no clean way to discern C++
8749 static and non-static member functions. G++ helps
8750 GDB by marking the first parameter for non-static
8751 member functions (which is the this pointer) as
8752 artificial. We pass this information to
8753 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8754
8755 DWARF version 3 added DW_AT_object_pointer, which GCC
8756 4.5 does not yet generate. */
8757 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8758 if (attr)
8759 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8760 else
8761 {
8762 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8763
8764 /* GCC/43521: In java, the formal parameter
8765 "this" is sometimes not marked with DW_AT_artificial. */
8766 if (cu->language == language_java)
8767 {
8768 const char *name = dwarf2_name (child_die, cu);
8769
8770 if (name && !strcmp (name, "this"))
8771 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8772 }
8773 }
8774 arg_type = die_type (child_die, cu);
8775
8776 /* RealView does not mark THIS as const, which the testsuite
8777 expects. GCC marks THIS as const in method definitions,
8778 but not in the class specifications (GCC PR 43053). */
8779 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8780 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8781 {
8782 int is_this = 0;
8783 struct dwarf2_cu *arg_cu = cu;
8784 const char *name = dwarf2_name (child_die, cu);
8785
8786 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8787 if (attr)
8788 {
8789 /* If the compiler emits this, use it. */
8790 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8791 is_this = 1;
8792 }
8793 else if (name && strcmp (name, "this") == 0)
8794 /* Function definitions will have the argument names. */
8795 is_this = 1;
8796 else if (name == NULL && iparams == 0)
8797 /* Declarations may not have the names, so like
8798 elsewhere in GDB, assume an artificial first
8799 argument is "this". */
8800 is_this = 1;
8801
8802 if (is_this)
8803 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8804 arg_type, 0);
8805 }
8806
8807 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8808 iparams++;
8809 }
8810 child_die = sibling_die (child_die);
8811 }
8812 }
8813
8814 return ftype;
8815 }
8816
8817 static struct type *
8818 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8819 {
8820 struct objfile *objfile = cu->objfile;
8821 const char *name = NULL;
8822 struct type *this_type, *target_type;
8823
8824 name = dwarf2_full_name (NULL, die, cu);
8825 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8826 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8827 TYPE_NAME (this_type) = (char *) name;
8828 set_die_type (die, this_type, cu);
8829 target_type = die_type (die, cu);
8830 if (target_type != this_type)
8831 TYPE_TARGET_TYPE (this_type) = target_type;
8832 else
8833 {
8834 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8835 spec and cause infinite loops in GDB. */
8836 complaint (&symfile_complaints,
8837 _("Self-referential DW_TAG_typedef "
8838 "- DIE at 0x%x [in module %s]"),
8839 die->offset, objfile->name);
8840 TYPE_TARGET_TYPE (this_type) = NULL;
8841 }
8842 return this_type;
8843 }
8844
8845 /* Find a representation of a given base type and install
8846 it in the TYPE field of the die. */
8847
8848 static struct type *
8849 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8850 {
8851 struct objfile *objfile = cu->objfile;
8852 struct type *type;
8853 struct attribute *attr;
8854 int encoding = 0, size = 0;
8855 char *name;
8856 enum type_code code = TYPE_CODE_INT;
8857 int type_flags = 0;
8858 struct type *target_type = NULL;
8859
8860 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8861 if (attr)
8862 {
8863 encoding = DW_UNSND (attr);
8864 }
8865 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8866 if (attr)
8867 {
8868 size = DW_UNSND (attr);
8869 }
8870 name = dwarf2_name (die, cu);
8871 if (!name)
8872 {
8873 complaint (&symfile_complaints,
8874 _("DW_AT_name missing from DW_TAG_base_type"));
8875 }
8876
8877 switch (encoding)
8878 {
8879 case DW_ATE_address:
8880 /* Turn DW_ATE_address into a void * pointer. */
8881 code = TYPE_CODE_PTR;
8882 type_flags |= TYPE_FLAG_UNSIGNED;
8883 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8884 break;
8885 case DW_ATE_boolean:
8886 code = TYPE_CODE_BOOL;
8887 type_flags |= TYPE_FLAG_UNSIGNED;
8888 break;
8889 case DW_ATE_complex_float:
8890 code = TYPE_CODE_COMPLEX;
8891 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8892 break;
8893 case DW_ATE_decimal_float:
8894 code = TYPE_CODE_DECFLOAT;
8895 break;
8896 case DW_ATE_float:
8897 code = TYPE_CODE_FLT;
8898 break;
8899 case DW_ATE_signed:
8900 break;
8901 case DW_ATE_unsigned:
8902 type_flags |= TYPE_FLAG_UNSIGNED;
8903 if (cu->language == language_fortran
8904 && name
8905 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8906 code = TYPE_CODE_CHAR;
8907 break;
8908 case DW_ATE_signed_char:
8909 if (cu->language == language_ada || cu->language == language_m2
8910 || cu->language == language_pascal
8911 || cu->language == language_fortran)
8912 code = TYPE_CODE_CHAR;
8913 break;
8914 case DW_ATE_unsigned_char:
8915 if (cu->language == language_ada || cu->language == language_m2
8916 || cu->language == language_pascal
8917 || cu->language == language_fortran)
8918 code = TYPE_CODE_CHAR;
8919 type_flags |= TYPE_FLAG_UNSIGNED;
8920 break;
8921 case DW_ATE_UTF:
8922 /* We just treat this as an integer and then recognize the
8923 type by name elsewhere. */
8924 break;
8925
8926 default:
8927 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8928 dwarf_type_encoding_name (encoding));
8929 break;
8930 }
8931
8932 type = init_type (code, size, type_flags, NULL, objfile);
8933 TYPE_NAME (type) = name;
8934 TYPE_TARGET_TYPE (type) = target_type;
8935
8936 if (name && strcmp (name, "char") == 0)
8937 TYPE_NOSIGN (type) = 1;
8938
8939 return set_die_type (die, type, cu);
8940 }
8941
8942 /* Read the given DW_AT_subrange DIE. */
8943
8944 static struct type *
8945 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8946 {
8947 struct type *base_type;
8948 struct type *range_type;
8949 struct attribute *attr;
8950 LONGEST low = 0;
8951 LONGEST high = -1;
8952 char *name;
8953 LONGEST negative_mask;
8954
8955 base_type = die_type (die, cu);
8956 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8957 check_typedef (base_type);
8958
8959 /* The die_type call above may have already set the type for this DIE. */
8960 range_type = get_die_type (die, cu);
8961 if (range_type)
8962 return range_type;
8963
8964 if (cu->language == language_fortran)
8965 {
8966 /* FORTRAN implies a lower bound of 1, if not given. */
8967 low = 1;
8968 }
8969
8970 /* FIXME: For variable sized arrays either of these could be
8971 a variable rather than a constant value. We'll allow it,
8972 but we don't know how to handle it. */
8973 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8974 if (attr)
8975 low = dwarf2_get_attr_constant_value (attr, 0);
8976
8977 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8978 if (attr)
8979 {
8980 if (attr_form_is_block (attr) || is_ref_attr (attr))
8981 {
8982 /* GCC encodes arrays with unspecified or dynamic length
8983 with a DW_FORM_block1 attribute or a reference attribute.
8984 FIXME: GDB does not yet know how to handle dynamic
8985 arrays properly, treat them as arrays with unspecified
8986 length for now.
8987
8988 FIXME: jimb/2003-09-22: GDB does not really know
8989 how to handle arrays of unspecified length
8990 either; we just represent them as zero-length
8991 arrays. Choose an appropriate upper bound given
8992 the lower bound we've computed above. */
8993 high = low - 1;
8994 }
8995 else
8996 high = dwarf2_get_attr_constant_value (attr, 1);
8997 }
8998 else
8999 {
9000 attr = dwarf2_attr (die, DW_AT_count, cu);
9001 if (attr)
9002 {
9003 int count = dwarf2_get_attr_constant_value (attr, 1);
9004 high = low + count - 1;
9005 }
9006 else
9007 {
9008 /* Unspecified array length. */
9009 high = low - 1;
9010 }
9011 }
9012
9013 /* Dwarf-2 specifications explicitly allows to create subrange types
9014 without specifying a base type.
9015 In that case, the base type must be set to the type of
9016 the lower bound, upper bound or count, in that order, if any of these
9017 three attributes references an object that has a type.
9018 If no base type is found, the Dwarf-2 specifications say that
9019 a signed integer type of size equal to the size of an address should
9020 be used.
9021 For the following C code: `extern char gdb_int [];'
9022 GCC produces an empty range DIE.
9023 FIXME: muller/2010-05-28: Possible references to object for low bound,
9024 high bound or count are not yet handled by this code. */
9025 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9026 {
9027 struct objfile *objfile = cu->objfile;
9028 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9029 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9030 struct type *int_type = objfile_type (objfile)->builtin_int;
9031
9032 /* Test "int", "long int", and "long long int" objfile types,
9033 and select the first one having a size above or equal to the
9034 architecture address size. */
9035 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9036 base_type = int_type;
9037 else
9038 {
9039 int_type = objfile_type (objfile)->builtin_long;
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_long;
9045 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9046 base_type = int_type;
9047 }
9048 }
9049 }
9050
9051 negative_mask =
9052 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9053 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9054 low |= negative_mask;
9055 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9056 high |= negative_mask;
9057
9058 range_type = create_range_type (NULL, base_type, low, high);
9059
9060 /* Mark arrays with dynamic length at least as an array of unspecified
9061 length. GDB could check the boundary but before it gets implemented at
9062 least allow accessing the array elements. */
9063 if (attr && attr_form_is_block (attr))
9064 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9065
9066 /* Ada expects an empty array on no boundary attributes. */
9067 if (attr == NULL && cu->language != language_ada)
9068 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9069
9070 name = dwarf2_name (die, cu);
9071 if (name)
9072 TYPE_NAME (range_type) = name;
9073
9074 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9075 if (attr)
9076 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9077
9078 set_die_type (die, range_type, cu);
9079
9080 /* set_die_type should be already done. */
9081 set_descriptive_type (range_type, die, cu);
9082
9083 return range_type;
9084 }
9085
9086 static struct type *
9087 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9088 {
9089 struct type *type;
9090
9091 /* For now, we only support the C meaning of an unspecified type: void. */
9092
9093 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9094 TYPE_NAME (type) = dwarf2_name (die, cu);
9095
9096 return set_die_type (die, type, cu);
9097 }
9098
9099 /* Trivial hash function for die_info: the hash value of a DIE
9100 is its offset in .debug_info for this objfile. */
9101
9102 static hashval_t
9103 die_hash (const void *item)
9104 {
9105 const struct die_info *die = item;
9106
9107 return die->offset;
9108 }
9109
9110 /* Trivial comparison function for die_info structures: two DIEs
9111 are equal if they have the same offset. */
9112
9113 static int
9114 die_eq (const void *item_lhs, const void *item_rhs)
9115 {
9116 const struct die_info *die_lhs = item_lhs;
9117 const struct die_info *die_rhs = item_rhs;
9118
9119 return die_lhs->offset == die_rhs->offset;
9120 }
9121
9122 /* Read a whole compilation unit into a linked list of dies. */
9123
9124 static struct die_info *
9125 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9126 {
9127 struct die_reader_specs reader_specs;
9128 int read_abbrevs = 0;
9129 struct cleanup *back_to = NULL;
9130 struct die_info *die;
9131
9132 if (cu->dwarf2_abbrevs == NULL)
9133 {
9134 dwarf2_read_abbrevs (cu);
9135 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9136 read_abbrevs = 1;
9137 }
9138
9139 gdb_assert (cu->die_hash == NULL);
9140 cu->die_hash
9141 = htab_create_alloc_ex (cu->header.length / 12,
9142 die_hash,
9143 die_eq,
9144 NULL,
9145 &cu->comp_unit_obstack,
9146 hashtab_obstack_allocate,
9147 dummy_obstack_deallocate);
9148
9149 init_cu_die_reader (&reader_specs, cu);
9150
9151 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9152
9153 if (read_abbrevs)
9154 do_cleanups (back_to);
9155
9156 return die;
9157 }
9158
9159 /* Main entry point for reading a DIE and all children.
9160 Read the DIE and dump it if requested. */
9161
9162 static struct die_info *
9163 read_die_and_children (const struct die_reader_specs *reader,
9164 gdb_byte *info_ptr,
9165 gdb_byte **new_info_ptr,
9166 struct die_info *parent)
9167 {
9168 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9169 new_info_ptr, parent);
9170
9171 if (dwarf2_die_debug)
9172 {
9173 fprintf_unfiltered (gdb_stdlog,
9174 "\nRead die from %s of %s:\n",
9175 (reader->cu->per_cu->debug_types_section
9176 ? ".debug_types"
9177 : ".debug_info"),
9178 reader->abfd->filename);
9179 dump_die (result, dwarf2_die_debug);
9180 }
9181
9182 return result;
9183 }
9184
9185 /* Read a single die and all its descendents. Set the die's sibling
9186 field to NULL; set other fields in the die correctly, and set all
9187 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9188 location of the info_ptr after reading all of those dies. PARENT
9189 is the parent of the die in question. */
9190
9191 static struct die_info *
9192 read_die_and_children_1 (const struct die_reader_specs *reader,
9193 gdb_byte *info_ptr,
9194 gdb_byte **new_info_ptr,
9195 struct die_info *parent)
9196 {
9197 struct die_info *die;
9198 gdb_byte *cur_ptr;
9199 int has_children;
9200
9201 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9202 if (die == NULL)
9203 {
9204 *new_info_ptr = cur_ptr;
9205 return NULL;
9206 }
9207 store_in_ref_table (die, reader->cu);
9208
9209 if (has_children)
9210 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9211 else
9212 {
9213 die->child = NULL;
9214 *new_info_ptr = cur_ptr;
9215 }
9216
9217 die->sibling = NULL;
9218 die->parent = parent;
9219 return die;
9220 }
9221
9222 /* Read a die, all of its descendents, and all of its siblings; set
9223 all of the fields of all of the dies correctly. Arguments are as
9224 in read_die_and_children. */
9225
9226 static struct die_info *
9227 read_die_and_siblings (const struct die_reader_specs *reader,
9228 gdb_byte *info_ptr,
9229 gdb_byte **new_info_ptr,
9230 struct die_info *parent)
9231 {
9232 struct die_info *first_die, *last_sibling;
9233 gdb_byte *cur_ptr;
9234
9235 cur_ptr = info_ptr;
9236 first_die = last_sibling = NULL;
9237
9238 while (1)
9239 {
9240 struct die_info *die
9241 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9242
9243 if (die == NULL)
9244 {
9245 *new_info_ptr = cur_ptr;
9246 return first_die;
9247 }
9248
9249 if (!first_die)
9250 first_die = die;
9251 else
9252 last_sibling->sibling = die;
9253
9254 last_sibling = die;
9255 }
9256 }
9257
9258 /* Read the die from the .debug_info section buffer. Set DIEP to
9259 point to a newly allocated die with its information, except for its
9260 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9261 whether the die has children or not. */
9262
9263 static gdb_byte *
9264 read_full_die (const struct die_reader_specs *reader,
9265 struct die_info **diep, gdb_byte *info_ptr,
9266 int *has_children)
9267 {
9268 unsigned int abbrev_number, bytes_read, i, offset;
9269 struct abbrev_info *abbrev;
9270 struct die_info *die;
9271 struct dwarf2_cu *cu = reader->cu;
9272 bfd *abfd = reader->abfd;
9273
9274 offset = info_ptr - reader->buffer;
9275 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9276 info_ptr += bytes_read;
9277 if (!abbrev_number)
9278 {
9279 *diep = NULL;
9280 *has_children = 0;
9281 return info_ptr;
9282 }
9283
9284 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9285 if (!abbrev)
9286 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9287 abbrev_number,
9288 bfd_get_filename (abfd));
9289
9290 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9291 die->offset = offset;
9292 die->tag = abbrev->tag;
9293 die->abbrev = abbrev_number;
9294
9295 die->num_attrs = abbrev->num_attrs;
9296
9297 for (i = 0; i < abbrev->num_attrs; ++i)
9298 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9299 abfd, info_ptr, cu);
9300
9301 *diep = die;
9302 *has_children = abbrev->has_children;
9303 return info_ptr;
9304 }
9305
9306 /* In DWARF version 2, the description of the debugging information is
9307 stored in a separate .debug_abbrev section. Before we read any
9308 dies from a section we read in all abbreviations and install them
9309 in a hash table. This function also sets flags in CU describing
9310 the data found in the abbrev table. */
9311
9312 static void
9313 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9314 {
9315 bfd *abfd = cu->objfile->obfd;
9316 struct comp_unit_head *cu_header = &cu->header;
9317 gdb_byte *abbrev_ptr;
9318 struct abbrev_info *cur_abbrev;
9319 unsigned int abbrev_number, bytes_read, abbrev_name;
9320 unsigned int abbrev_form, hash_number;
9321 struct attr_abbrev *cur_attrs;
9322 unsigned int allocated_attrs;
9323
9324 /* Initialize dwarf2 abbrevs. */
9325 obstack_init (&cu->abbrev_obstack);
9326 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9327 (ABBREV_HASH_SIZE
9328 * sizeof (struct abbrev_info *)));
9329 memset (cu->dwarf2_abbrevs, 0,
9330 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9331
9332 dwarf2_read_section (dwarf2_per_objfile->objfile,
9333 &dwarf2_per_objfile->abbrev);
9334 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9335 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9336 abbrev_ptr += bytes_read;
9337
9338 allocated_attrs = ATTR_ALLOC_CHUNK;
9339 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9340
9341 /* Loop until we reach an abbrev number of 0. */
9342 while (abbrev_number)
9343 {
9344 cur_abbrev = dwarf_alloc_abbrev (cu);
9345
9346 /* read in abbrev header */
9347 cur_abbrev->number = abbrev_number;
9348 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9349 abbrev_ptr += bytes_read;
9350 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9351 abbrev_ptr += 1;
9352
9353 if (cur_abbrev->tag == DW_TAG_namespace)
9354 cu->has_namespace_info = 1;
9355
9356 /* now read in declarations */
9357 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9358 abbrev_ptr += bytes_read;
9359 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9360 abbrev_ptr += bytes_read;
9361 while (abbrev_name)
9362 {
9363 if (cur_abbrev->num_attrs == allocated_attrs)
9364 {
9365 allocated_attrs += ATTR_ALLOC_CHUNK;
9366 cur_attrs
9367 = xrealloc (cur_attrs, (allocated_attrs
9368 * sizeof (struct attr_abbrev)));
9369 }
9370
9371 /* Record whether this compilation unit might have
9372 inter-compilation-unit references. If we don't know what form
9373 this attribute will have, then it might potentially be a
9374 DW_FORM_ref_addr, so we conservatively expect inter-CU
9375 references. */
9376
9377 if (abbrev_form == DW_FORM_ref_addr
9378 || abbrev_form == DW_FORM_indirect)
9379 cu->has_form_ref_addr = 1;
9380
9381 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9382 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9383 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9384 abbrev_ptr += bytes_read;
9385 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9386 abbrev_ptr += bytes_read;
9387 }
9388
9389 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9390 (cur_abbrev->num_attrs
9391 * sizeof (struct attr_abbrev)));
9392 memcpy (cur_abbrev->attrs, cur_attrs,
9393 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9394
9395 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9396 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9397 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9398
9399 /* Get next abbreviation.
9400 Under Irix6 the abbreviations for a compilation unit are not
9401 always properly terminated with an abbrev number of 0.
9402 Exit loop if we encounter an abbreviation which we have
9403 already read (which means we are about to read the abbreviations
9404 for the next compile unit) or if the end of the abbreviation
9405 table is reached. */
9406 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9407 >= dwarf2_per_objfile->abbrev.size)
9408 break;
9409 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9410 abbrev_ptr += bytes_read;
9411 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9412 break;
9413 }
9414
9415 xfree (cur_attrs);
9416 }
9417
9418 /* Release the memory used by the abbrev table for a compilation unit. */
9419
9420 static void
9421 dwarf2_free_abbrev_table (void *ptr_to_cu)
9422 {
9423 struct dwarf2_cu *cu = ptr_to_cu;
9424
9425 obstack_free (&cu->abbrev_obstack, NULL);
9426 cu->dwarf2_abbrevs = NULL;
9427 }
9428
9429 /* Lookup an abbrev_info structure in the abbrev hash table. */
9430
9431 static struct abbrev_info *
9432 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9433 {
9434 unsigned int hash_number;
9435 struct abbrev_info *abbrev;
9436
9437 hash_number = number % ABBREV_HASH_SIZE;
9438 abbrev = cu->dwarf2_abbrevs[hash_number];
9439
9440 while (abbrev)
9441 {
9442 if (abbrev->number == number)
9443 return abbrev;
9444 else
9445 abbrev = abbrev->next;
9446 }
9447 return NULL;
9448 }
9449
9450 /* Returns nonzero if TAG represents a type that we might generate a partial
9451 symbol for. */
9452
9453 static int
9454 is_type_tag_for_partial (int tag)
9455 {
9456 switch (tag)
9457 {
9458 #if 0
9459 /* Some types that would be reasonable to generate partial symbols for,
9460 that we don't at present. */
9461 case DW_TAG_array_type:
9462 case DW_TAG_file_type:
9463 case DW_TAG_ptr_to_member_type:
9464 case DW_TAG_set_type:
9465 case DW_TAG_string_type:
9466 case DW_TAG_subroutine_type:
9467 #endif
9468 case DW_TAG_base_type:
9469 case DW_TAG_class_type:
9470 case DW_TAG_interface_type:
9471 case DW_TAG_enumeration_type:
9472 case DW_TAG_structure_type:
9473 case DW_TAG_subrange_type:
9474 case DW_TAG_typedef:
9475 case DW_TAG_union_type:
9476 return 1;
9477 default:
9478 return 0;
9479 }
9480 }
9481
9482 /* Load all DIEs that are interesting for partial symbols into memory. */
9483
9484 static struct partial_die_info *
9485 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9486 int building_psymtab, struct dwarf2_cu *cu)
9487 {
9488 struct objfile *objfile = cu->objfile;
9489 struct partial_die_info *part_die;
9490 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9491 struct abbrev_info *abbrev;
9492 unsigned int bytes_read;
9493 unsigned int load_all = 0;
9494
9495 int nesting_level = 1;
9496
9497 parent_die = NULL;
9498 last_die = NULL;
9499
9500 if (cu->per_cu && cu->per_cu->load_all_dies)
9501 load_all = 1;
9502
9503 cu->partial_dies
9504 = htab_create_alloc_ex (cu->header.length / 12,
9505 partial_die_hash,
9506 partial_die_eq,
9507 NULL,
9508 &cu->comp_unit_obstack,
9509 hashtab_obstack_allocate,
9510 dummy_obstack_deallocate);
9511
9512 part_die = obstack_alloc (&cu->comp_unit_obstack,
9513 sizeof (struct partial_die_info));
9514
9515 while (1)
9516 {
9517 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9518
9519 /* A NULL abbrev means the end of a series of children. */
9520 if (abbrev == NULL)
9521 {
9522 if (--nesting_level == 0)
9523 {
9524 /* PART_DIE was probably the last thing allocated on the
9525 comp_unit_obstack, so we could call obstack_free
9526 here. We don't do that because the waste is small,
9527 and will be cleaned up when we're done with this
9528 compilation unit. This way, we're also more robust
9529 against other users of the comp_unit_obstack. */
9530 return first_die;
9531 }
9532 info_ptr += bytes_read;
9533 last_die = parent_die;
9534 parent_die = parent_die->die_parent;
9535 continue;
9536 }
9537
9538 /* Check for template arguments. We never save these; if
9539 they're seen, we just mark the parent, and go on our way. */
9540 if (parent_die != NULL
9541 && cu->language == language_cplus
9542 && (abbrev->tag == DW_TAG_template_type_param
9543 || abbrev->tag == DW_TAG_template_value_param))
9544 {
9545 parent_die->has_template_arguments = 1;
9546
9547 if (!load_all)
9548 {
9549 /* We don't need a partial DIE for the template argument. */
9550 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9551 cu);
9552 continue;
9553 }
9554 }
9555
9556 /* We only recurse into subprograms looking for template arguments.
9557 Skip their other children. */
9558 if (!load_all
9559 && cu->language == language_cplus
9560 && parent_die != NULL
9561 && parent_die->tag == DW_TAG_subprogram)
9562 {
9563 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9564 continue;
9565 }
9566
9567 /* Check whether this DIE is interesting enough to save. Normally
9568 we would not be interested in members here, but there may be
9569 later variables referencing them via DW_AT_specification (for
9570 static members). */
9571 if (!load_all
9572 && !is_type_tag_for_partial (abbrev->tag)
9573 && abbrev->tag != DW_TAG_constant
9574 && abbrev->tag != DW_TAG_enumerator
9575 && abbrev->tag != DW_TAG_subprogram
9576 && abbrev->tag != DW_TAG_lexical_block
9577 && abbrev->tag != DW_TAG_variable
9578 && abbrev->tag != DW_TAG_namespace
9579 && abbrev->tag != DW_TAG_module
9580 && abbrev->tag != DW_TAG_member)
9581 {
9582 /* Otherwise we skip to the next sibling, if any. */
9583 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9584 continue;
9585 }
9586
9587 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9588 buffer, info_ptr, cu);
9589
9590 /* This two-pass algorithm for processing partial symbols has a
9591 high cost in cache pressure. Thus, handle some simple cases
9592 here which cover the majority of C partial symbols. DIEs
9593 which neither have specification tags in them, nor could have
9594 specification tags elsewhere pointing at them, can simply be
9595 processed and discarded.
9596
9597 This segment is also optional; scan_partial_symbols and
9598 add_partial_symbol will handle these DIEs if we chain
9599 them in normally. When compilers which do not emit large
9600 quantities of duplicate debug information are more common,
9601 this code can probably be removed. */
9602
9603 /* Any complete simple types at the top level (pretty much all
9604 of them, for a language without namespaces), can be processed
9605 directly. */
9606 if (parent_die == NULL
9607 && part_die->has_specification == 0
9608 && part_die->is_declaration == 0
9609 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9610 || part_die->tag == DW_TAG_base_type
9611 || part_die->tag == DW_TAG_subrange_type))
9612 {
9613 if (building_psymtab && part_die->name != NULL)
9614 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9615 VAR_DOMAIN, LOC_TYPEDEF,
9616 &objfile->static_psymbols,
9617 0, (CORE_ADDR) 0, cu->language, objfile);
9618 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9619 continue;
9620 }
9621
9622 /* The exception for DW_TAG_typedef with has_children above is
9623 a workaround of GCC PR debug/47510. In the case of this complaint
9624 type_name_no_tag_or_error will error on such types later.
9625
9626 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9627 it could not find the child DIEs referenced later, this is checked
9628 above. In correct DWARF DW_TAG_typedef should have no children. */
9629
9630 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9631 complaint (&symfile_complaints,
9632 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9633 "- DIE at 0x%x [in module %s]"),
9634 part_die->offset, objfile->name);
9635
9636 /* If we're at the second level, and we're an enumerator, and
9637 our parent has no specification (meaning possibly lives in a
9638 namespace elsewhere), then we can add the partial symbol now
9639 instead of queueing it. */
9640 if (part_die->tag == DW_TAG_enumerator
9641 && parent_die != NULL
9642 && parent_die->die_parent == NULL
9643 && parent_die->tag == DW_TAG_enumeration_type
9644 && parent_die->has_specification == 0)
9645 {
9646 if (part_die->name == NULL)
9647 complaint (&symfile_complaints,
9648 _("malformed enumerator DIE ignored"));
9649 else if (building_psymtab)
9650 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9651 VAR_DOMAIN, LOC_CONST,
9652 (cu->language == language_cplus
9653 || cu->language == language_java)
9654 ? &objfile->global_psymbols
9655 : &objfile->static_psymbols,
9656 0, (CORE_ADDR) 0, cu->language, objfile);
9657
9658 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9659 continue;
9660 }
9661
9662 /* We'll save this DIE so link it in. */
9663 part_die->die_parent = parent_die;
9664 part_die->die_sibling = NULL;
9665 part_die->die_child = NULL;
9666
9667 if (last_die && last_die == parent_die)
9668 last_die->die_child = part_die;
9669 else if (last_die)
9670 last_die->die_sibling = part_die;
9671
9672 last_die = part_die;
9673
9674 if (first_die == NULL)
9675 first_die = part_die;
9676
9677 /* Maybe add the DIE to the hash table. Not all DIEs that we
9678 find interesting need to be in the hash table, because we
9679 also have the parent/sibling/child chains; only those that we
9680 might refer to by offset later during partial symbol reading.
9681
9682 For now this means things that might have be the target of a
9683 DW_AT_specification, DW_AT_abstract_origin, or
9684 DW_AT_extension. DW_AT_extension will refer only to
9685 namespaces; DW_AT_abstract_origin refers to functions (and
9686 many things under the function DIE, but we do not recurse
9687 into function DIEs during partial symbol reading) and
9688 possibly variables as well; DW_AT_specification refers to
9689 declarations. Declarations ought to have the DW_AT_declaration
9690 flag. It happens that GCC forgets to put it in sometimes, but
9691 only for functions, not for types.
9692
9693 Adding more things than necessary to the hash table is harmless
9694 except for the performance cost. Adding too few will result in
9695 wasted time in find_partial_die, when we reread the compilation
9696 unit with load_all_dies set. */
9697
9698 if (load_all
9699 || abbrev->tag == DW_TAG_constant
9700 || abbrev->tag == DW_TAG_subprogram
9701 || abbrev->tag == DW_TAG_variable
9702 || abbrev->tag == DW_TAG_namespace
9703 || part_die->is_declaration)
9704 {
9705 void **slot;
9706
9707 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9708 part_die->offset, INSERT);
9709 *slot = part_die;
9710 }
9711
9712 part_die = obstack_alloc (&cu->comp_unit_obstack,
9713 sizeof (struct partial_die_info));
9714
9715 /* For some DIEs we want to follow their children (if any). For C
9716 we have no reason to follow the children of structures; for other
9717 languages we have to, so that we can get at method physnames
9718 to infer fully qualified class names, for DW_AT_specification,
9719 and for C++ template arguments. For C++, we also look one level
9720 inside functions to find template arguments (if the name of the
9721 function does not already contain the template arguments).
9722
9723 For Ada, we need to scan the children of subprograms and lexical
9724 blocks as well because Ada allows the definition of nested
9725 entities that could be interesting for the debugger, such as
9726 nested subprograms for instance. */
9727 if (last_die->has_children
9728 && (load_all
9729 || last_die->tag == DW_TAG_namespace
9730 || last_die->tag == DW_TAG_module
9731 || last_die->tag == DW_TAG_enumeration_type
9732 || (cu->language == language_cplus
9733 && last_die->tag == DW_TAG_subprogram
9734 && (last_die->name == NULL
9735 || strchr (last_die->name, '<') == NULL))
9736 || (cu->language != language_c
9737 && (last_die->tag == DW_TAG_class_type
9738 || last_die->tag == DW_TAG_interface_type
9739 || last_die->tag == DW_TAG_structure_type
9740 || last_die->tag == DW_TAG_union_type))
9741 || (cu->language == language_ada
9742 && (last_die->tag == DW_TAG_subprogram
9743 || last_die->tag == DW_TAG_lexical_block))))
9744 {
9745 nesting_level++;
9746 parent_die = last_die;
9747 continue;
9748 }
9749
9750 /* Otherwise we skip to the next sibling, if any. */
9751 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9752
9753 /* Back to the top, do it again. */
9754 }
9755 }
9756
9757 /* Read a minimal amount of information into the minimal die structure. */
9758
9759 static gdb_byte *
9760 read_partial_die (struct partial_die_info *part_die,
9761 struct abbrev_info *abbrev,
9762 unsigned int abbrev_len, bfd *abfd,
9763 gdb_byte *buffer, gdb_byte *info_ptr,
9764 struct dwarf2_cu *cu)
9765 {
9766 struct objfile *objfile = cu->objfile;
9767 unsigned int i;
9768 struct attribute attr;
9769 int has_low_pc_attr = 0;
9770 int has_high_pc_attr = 0;
9771
9772 memset (part_die, 0, sizeof (struct partial_die_info));
9773
9774 part_die->offset = info_ptr - buffer;
9775
9776 info_ptr += abbrev_len;
9777
9778 if (abbrev == NULL)
9779 return info_ptr;
9780
9781 part_die->tag = abbrev->tag;
9782 part_die->has_children = abbrev->has_children;
9783
9784 for (i = 0; i < abbrev->num_attrs; ++i)
9785 {
9786 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9787
9788 /* Store the data if it is of an attribute we want to keep in a
9789 partial symbol table. */
9790 switch (attr.name)
9791 {
9792 case DW_AT_name:
9793 switch (part_die->tag)
9794 {
9795 case DW_TAG_compile_unit:
9796 case DW_TAG_type_unit:
9797 /* Compilation units have a DW_AT_name that is a filename, not
9798 a source language identifier. */
9799 case DW_TAG_enumeration_type:
9800 case DW_TAG_enumerator:
9801 /* These tags always have simple identifiers already; no need
9802 to canonicalize them. */
9803 part_die->name = DW_STRING (&attr);
9804 break;
9805 default:
9806 part_die->name
9807 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9808 &objfile->objfile_obstack);
9809 break;
9810 }
9811 break;
9812 case DW_AT_linkage_name:
9813 case DW_AT_MIPS_linkage_name:
9814 /* Note that both forms of linkage name might appear. We
9815 assume they will be the same, and we only store the last
9816 one we see. */
9817 if (cu->language == language_ada)
9818 part_die->name = DW_STRING (&attr);
9819 part_die->linkage_name = DW_STRING (&attr);
9820 break;
9821 case DW_AT_low_pc:
9822 has_low_pc_attr = 1;
9823 part_die->lowpc = DW_ADDR (&attr);
9824 break;
9825 case DW_AT_high_pc:
9826 has_high_pc_attr = 1;
9827 part_die->highpc = DW_ADDR (&attr);
9828 break;
9829 case DW_AT_location:
9830 /* Support the .debug_loc offsets. */
9831 if (attr_form_is_block (&attr))
9832 {
9833 part_die->locdesc = DW_BLOCK (&attr);
9834 }
9835 else if (attr_form_is_section_offset (&attr))
9836 {
9837 dwarf2_complex_location_expr_complaint ();
9838 }
9839 else
9840 {
9841 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9842 "partial symbol information");
9843 }
9844 break;
9845 case DW_AT_external:
9846 part_die->is_external = DW_UNSND (&attr);
9847 break;
9848 case DW_AT_declaration:
9849 part_die->is_declaration = DW_UNSND (&attr);
9850 break;
9851 case DW_AT_type:
9852 part_die->has_type = 1;
9853 break;
9854 case DW_AT_abstract_origin:
9855 case DW_AT_specification:
9856 case DW_AT_extension:
9857 part_die->has_specification = 1;
9858 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9859 break;
9860 case DW_AT_sibling:
9861 /* Ignore absolute siblings, they might point outside of
9862 the current compile unit. */
9863 if (attr.form == DW_FORM_ref_addr)
9864 complaint (&symfile_complaints,
9865 _("ignoring absolute DW_AT_sibling"));
9866 else
9867 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9868 break;
9869 case DW_AT_byte_size:
9870 part_die->has_byte_size = 1;
9871 break;
9872 case DW_AT_calling_convention:
9873 /* DWARF doesn't provide a way to identify a program's source-level
9874 entry point. DW_AT_calling_convention attributes are only meant
9875 to describe functions' calling conventions.
9876
9877 However, because it's a necessary piece of information in
9878 Fortran, and because DW_CC_program is the only piece of debugging
9879 information whose definition refers to a 'main program' at all,
9880 several compilers have begun marking Fortran main programs with
9881 DW_CC_program --- even when those functions use the standard
9882 calling conventions.
9883
9884 So until DWARF specifies a way to provide this information and
9885 compilers pick up the new representation, we'll support this
9886 practice. */
9887 if (DW_UNSND (&attr) == DW_CC_program
9888 && cu->language == language_fortran)
9889 {
9890 set_main_name (part_die->name);
9891
9892 /* As this DIE has a static linkage the name would be difficult
9893 to look up later. */
9894 language_of_main = language_fortran;
9895 }
9896 break;
9897 default:
9898 break;
9899 }
9900 }
9901
9902 if (has_low_pc_attr && has_high_pc_attr)
9903 {
9904 /* When using the GNU linker, .gnu.linkonce. sections are used to
9905 eliminate duplicate copies of functions and vtables and such.
9906 The linker will arbitrarily choose one and discard the others.
9907 The AT_*_pc values for such functions refer to local labels in
9908 these sections. If the section from that file was discarded, the
9909 labels are not in the output, so the relocs get a value of 0.
9910 If this is a discarded function, mark the pc bounds as invalid,
9911 so that GDB will ignore it. */
9912 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9913 {
9914 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9915
9916 complaint (&symfile_complaints,
9917 _("DW_AT_low_pc %s is zero "
9918 "for DIE at 0x%x [in module %s]"),
9919 paddress (gdbarch, part_die->lowpc),
9920 part_die->offset, objfile->name);
9921 }
9922 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9923 else if (part_die->lowpc >= part_die->highpc)
9924 {
9925 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9926
9927 complaint (&symfile_complaints,
9928 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9929 "for DIE at 0x%x [in module %s]"),
9930 paddress (gdbarch, part_die->lowpc),
9931 paddress (gdbarch, part_die->highpc),
9932 part_die->offset, objfile->name);
9933 }
9934 else
9935 part_die->has_pc_info = 1;
9936 }
9937
9938 return info_ptr;
9939 }
9940
9941 /* Find a cached partial DIE at OFFSET in CU. */
9942
9943 static struct partial_die_info *
9944 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9945 {
9946 struct partial_die_info *lookup_die = NULL;
9947 struct partial_die_info part_die;
9948
9949 part_die.offset = offset;
9950 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9951
9952 return lookup_die;
9953 }
9954
9955 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9956 except in the case of .debug_types DIEs which do not reference
9957 outside their CU (they do however referencing other types via
9958 DW_FORM_ref_sig8). */
9959
9960 static struct partial_die_info *
9961 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9962 {
9963 struct objfile *objfile = cu->objfile;
9964 struct dwarf2_per_cu_data *per_cu = NULL;
9965 struct partial_die_info *pd = NULL;
9966
9967 if (cu->per_cu->debug_types_section)
9968 {
9969 pd = find_partial_die_in_comp_unit (offset, cu);
9970 if (pd != NULL)
9971 return pd;
9972 goto not_found;
9973 }
9974
9975 if (offset_in_cu_p (&cu->header, offset))
9976 {
9977 pd = find_partial_die_in_comp_unit (offset, cu);
9978 if (pd != NULL)
9979 return pd;
9980 }
9981
9982 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9983
9984 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9985 load_partial_comp_unit (per_cu);
9986
9987 per_cu->cu->last_used = 0;
9988 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9989
9990 if (pd == NULL && per_cu->load_all_dies == 0)
9991 {
9992 struct cleanup *back_to;
9993 struct partial_die_info comp_unit_die;
9994 struct abbrev_info *abbrev;
9995 unsigned int bytes_read;
9996 char *info_ptr;
9997
9998 per_cu->load_all_dies = 1;
9999
10000 /* Re-read the DIEs. */
10001 back_to = make_cleanup (null_cleanup, 0);
10002 if (per_cu->cu->dwarf2_abbrevs == NULL)
10003 {
10004 dwarf2_read_abbrevs (per_cu->cu);
10005 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10006 }
10007 info_ptr = (dwarf2_per_objfile->info.buffer
10008 + per_cu->cu->header.offset
10009 + per_cu->cu->header.first_die_offset);
10010 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10011 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10012 objfile->obfd,
10013 dwarf2_per_objfile->info.buffer, info_ptr,
10014 per_cu->cu);
10015 if (comp_unit_die.has_children)
10016 load_partial_dies (objfile->obfd,
10017 dwarf2_per_objfile->info.buffer, info_ptr,
10018 0, per_cu->cu);
10019 do_cleanups (back_to);
10020
10021 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10022 }
10023
10024 not_found:
10025
10026 if (pd == NULL)
10027 internal_error (__FILE__, __LINE__,
10028 _("could not find partial DIE 0x%x "
10029 "in cache [from module %s]\n"),
10030 offset, bfd_get_filename (objfile->obfd));
10031 return pd;
10032 }
10033
10034 /* See if we can figure out if the class lives in a namespace. We do
10035 this by looking for a member function; its demangled name will
10036 contain namespace info, if there is any. */
10037
10038 static void
10039 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10040 struct dwarf2_cu *cu)
10041 {
10042 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10043 what template types look like, because the demangler
10044 frequently doesn't give the same name as the debug info. We
10045 could fix this by only using the demangled name to get the
10046 prefix (but see comment in read_structure_type). */
10047
10048 struct partial_die_info *real_pdi;
10049 struct partial_die_info *child_pdi;
10050
10051 /* If this DIE (this DIE's specification, if any) has a parent, then
10052 we should not do this. We'll prepend the parent's fully qualified
10053 name when we create the partial symbol. */
10054
10055 real_pdi = struct_pdi;
10056 while (real_pdi->has_specification)
10057 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10058
10059 if (real_pdi->die_parent != NULL)
10060 return;
10061
10062 for (child_pdi = struct_pdi->die_child;
10063 child_pdi != NULL;
10064 child_pdi = child_pdi->die_sibling)
10065 {
10066 if (child_pdi->tag == DW_TAG_subprogram
10067 && child_pdi->linkage_name != NULL)
10068 {
10069 char *actual_class_name
10070 = language_class_name_from_physname (cu->language_defn,
10071 child_pdi->linkage_name);
10072 if (actual_class_name != NULL)
10073 {
10074 struct_pdi->name
10075 = obsavestring (actual_class_name,
10076 strlen (actual_class_name),
10077 &cu->objfile->objfile_obstack);
10078 xfree (actual_class_name);
10079 }
10080 break;
10081 }
10082 }
10083 }
10084
10085 /* Adjust PART_DIE before generating a symbol for it. This function
10086 may set the is_external flag or change the DIE's name. */
10087
10088 static void
10089 fixup_partial_die (struct partial_die_info *part_die,
10090 struct dwarf2_cu *cu)
10091 {
10092 /* Once we've fixed up a die, there's no point in doing so again.
10093 This also avoids a memory leak if we were to call
10094 guess_partial_die_structure_name multiple times. */
10095 if (part_die->fixup_called)
10096 return;
10097
10098 /* If we found a reference attribute and the DIE has no name, try
10099 to find a name in the referred to DIE. */
10100
10101 if (part_die->name == NULL && part_die->has_specification)
10102 {
10103 struct partial_die_info *spec_die;
10104
10105 spec_die = find_partial_die (part_die->spec_offset, cu);
10106
10107 fixup_partial_die (spec_die, cu);
10108
10109 if (spec_die->name)
10110 {
10111 part_die->name = spec_die->name;
10112
10113 /* Copy DW_AT_external attribute if it is set. */
10114 if (spec_die->is_external)
10115 part_die->is_external = spec_die->is_external;
10116 }
10117 }
10118
10119 /* Set default names for some unnamed DIEs. */
10120
10121 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10122 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10123
10124 /* If there is no parent die to provide a namespace, and there are
10125 children, see if we can determine the namespace from their linkage
10126 name.
10127 NOTE: We need to do this even if cu->has_namespace_info != 0.
10128 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10129 if (cu->language == language_cplus
10130 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10131 && part_die->die_parent == NULL
10132 && part_die->has_children
10133 && (part_die->tag == DW_TAG_class_type
10134 || part_die->tag == DW_TAG_structure_type
10135 || part_die->tag == DW_TAG_union_type))
10136 guess_partial_die_structure_name (part_die, cu);
10137
10138 /* GCC might emit a nameless struct or union that has a linkage
10139 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10140 if (part_die->name == NULL
10141 && (part_die->tag == DW_TAG_class_type
10142 || part_die->tag == DW_TAG_interface_type
10143 || part_die->tag == DW_TAG_structure_type
10144 || part_die->tag == DW_TAG_union_type)
10145 && part_die->linkage_name != NULL)
10146 {
10147 char *demangled;
10148
10149 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10150 if (demangled)
10151 {
10152 const char *base;
10153
10154 /* Strip any leading namespaces/classes, keep only the base name.
10155 DW_AT_name for named DIEs does not contain the prefixes. */
10156 base = strrchr (demangled, ':');
10157 if (base && base > demangled && base[-1] == ':')
10158 base++;
10159 else
10160 base = demangled;
10161
10162 part_die->name = obsavestring (base, strlen (base),
10163 &cu->objfile->objfile_obstack);
10164 xfree (demangled);
10165 }
10166 }
10167
10168 part_die->fixup_called = 1;
10169 }
10170
10171 /* Read an attribute value described by an attribute form. */
10172
10173 static gdb_byte *
10174 read_attribute_value (struct attribute *attr, unsigned form,
10175 bfd *abfd, gdb_byte *info_ptr,
10176 struct dwarf2_cu *cu)
10177 {
10178 struct comp_unit_head *cu_header = &cu->header;
10179 unsigned int bytes_read;
10180 struct dwarf_block *blk;
10181
10182 attr->form = form;
10183 switch (form)
10184 {
10185 case DW_FORM_ref_addr:
10186 if (cu->header.version == 2)
10187 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10188 else
10189 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10190 &cu->header, &bytes_read);
10191 info_ptr += bytes_read;
10192 break;
10193 case DW_FORM_addr:
10194 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10195 info_ptr += bytes_read;
10196 break;
10197 case DW_FORM_block2:
10198 blk = dwarf_alloc_block (cu);
10199 blk->size = read_2_bytes (abfd, info_ptr);
10200 info_ptr += 2;
10201 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10202 info_ptr += blk->size;
10203 DW_BLOCK (attr) = blk;
10204 break;
10205 case DW_FORM_block4:
10206 blk = dwarf_alloc_block (cu);
10207 blk->size = read_4_bytes (abfd, info_ptr);
10208 info_ptr += 4;
10209 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10210 info_ptr += blk->size;
10211 DW_BLOCK (attr) = blk;
10212 break;
10213 case DW_FORM_data2:
10214 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10215 info_ptr += 2;
10216 break;
10217 case DW_FORM_data4:
10218 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10219 info_ptr += 4;
10220 break;
10221 case DW_FORM_data8:
10222 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10223 info_ptr += 8;
10224 break;
10225 case DW_FORM_sec_offset:
10226 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10227 info_ptr += bytes_read;
10228 break;
10229 case DW_FORM_string:
10230 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10231 DW_STRING_IS_CANONICAL (attr) = 0;
10232 info_ptr += bytes_read;
10233 break;
10234 case DW_FORM_strp:
10235 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10236 &bytes_read);
10237 DW_STRING_IS_CANONICAL (attr) = 0;
10238 info_ptr += bytes_read;
10239 break;
10240 case DW_FORM_exprloc:
10241 case DW_FORM_block:
10242 blk = dwarf_alloc_block (cu);
10243 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10244 info_ptr += bytes_read;
10245 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10246 info_ptr += blk->size;
10247 DW_BLOCK (attr) = blk;
10248 break;
10249 case DW_FORM_block1:
10250 blk = dwarf_alloc_block (cu);
10251 blk->size = read_1_byte (abfd, info_ptr);
10252 info_ptr += 1;
10253 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10254 info_ptr += blk->size;
10255 DW_BLOCK (attr) = blk;
10256 break;
10257 case DW_FORM_data1:
10258 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10259 info_ptr += 1;
10260 break;
10261 case DW_FORM_flag:
10262 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10263 info_ptr += 1;
10264 break;
10265 case DW_FORM_flag_present:
10266 DW_UNSND (attr) = 1;
10267 break;
10268 case DW_FORM_sdata:
10269 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10270 info_ptr += bytes_read;
10271 break;
10272 case DW_FORM_udata:
10273 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10274 info_ptr += bytes_read;
10275 break;
10276 case DW_FORM_ref1:
10277 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10278 info_ptr += 1;
10279 break;
10280 case DW_FORM_ref2:
10281 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10282 info_ptr += 2;
10283 break;
10284 case DW_FORM_ref4:
10285 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10286 info_ptr += 4;
10287 break;
10288 case DW_FORM_ref8:
10289 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10290 info_ptr += 8;
10291 break;
10292 case DW_FORM_ref_sig8:
10293 /* Convert the signature to something we can record in DW_UNSND
10294 for later lookup.
10295 NOTE: This is NULL if the type wasn't found. */
10296 DW_SIGNATURED_TYPE (attr) =
10297 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10298 info_ptr += 8;
10299 break;
10300 case DW_FORM_ref_udata:
10301 DW_ADDR (attr) = (cu->header.offset
10302 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10303 info_ptr += bytes_read;
10304 break;
10305 case DW_FORM_indirect:
10306 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10307 info_ptr += bytes_read;
10308 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10309 break;
10310 default:
10311 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10312 dwarf_form_name (form),
10313 bfd_get_filename (abfd));
10314 }
10315
10316 /* We have seen instances where the compiler tried to emit a byte
10317 size attribute of -1 which ended up being encoded as an unsigned
10318 0xffffffff. Although 0xffffffff is technically a valid size value,
10319 an object of this size seems pretty unlikely so we can relatively
10320 safely treat these cases as if the size attribute was invalid and
10321 treat them as zero by default. */
10322 if (attr->name == DW_AT_byte_size
10323 && form == DW_FORM_data4
10324 && DW_UNSND (attr) >= 0xffffffff)
10325 {
10326 complaint
10327 (&symfile_complaints,
10328 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10329 hex_string (DW_UNSND (attr)));
10330 DW_UNSND (attr) = 0;
10331 }
10332
10333 return info_ptr;
10334 }
10335
10336 /* Read an attribute described by an abbreviated attribute. */
10337
10338 static gdb_byte *
10339 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10340 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10341 {
10342 attr->name = abbrev->name;
10343 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10344 }
10345
10346 /* Read dwarf information from a buffer. */
10347
10348 static unsigned int
10349 read_1_byte (bfd *abfd, gdb_byte *buf)
10350 {
10351 return bfd_get_8 (abfd, buf);
10352 }
10353
10354 static int
10355 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10356 {
10357 return bfd_get_signed_8 (abfd, buf);
10358 }
10359
10360 static unsigned int
10361 read_2_bytes (bfd *abfd, gdb_byte *buf)
10362 {
10363 return bfd_get_16 (abfd, buf);
10364 }
10365
10366 static int
10367 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10368 {
10369 return bfd_get_signed_16 (abfd, buf);
10370 }
10371
10372 static unsigned int
10373 read_4_bytes (bfd *abfd, gdb_byte *buf)
10374 {
10375 return bfd_get_32 (abfd, buf);
10376 }
10377
10378 static int
10379 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10380 {
10381 return bfd_get_signed_32 (abfd, buf);
10382 }
10383
10384 static ULONGEST
10385 read_8_bytes (bfd *abfd, gdb_byte *buf)
10386 {
10387 return bfd_get_64 (abfd, buf);
10388 }
10389
10390 static CORE_ADDR
10391 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10392 unsigned int *bytes_read)
10393 {
10394 struct comp_unit_head *cu_header = &cu->header;
10395 CORE_ADDR retval = 0;
10396
10397 if (cu_header->signed_addr_p)
10398 {
10399 switch (cu_header->addr_size)
10400 {
10401 case 2:
10402 retval = bfd_get_signed_16 (abfd, buf);
10403 break;
10404 case 4:
10405 retval = bfd_get_signed_32 (abfd, buf);
10406 break;
10407 case 8:
10408 retval = bfd_get_signed_64 (abfd, buf);
10409 break;
10410 default:
10411 internal_error (__FILE__, __LINE__,
10412 _("read_address: bad switch, signed [in module %s]"),
10413 bfd_get_filename (abfd));
10414 }
10415 }
10416 else
10417 {
10418 switch (cu_header->addr_size)
10419 {
10420 case 2:
10421 retval = bfd_get_16 (abfd, buf);
10422 break;
10423 case 4:
10424 retval = bfd_get_32 (abfd, buf);
10425 break;
10426 case 8:
10427 retval = bfd_get_64 (abfd, buf);
10428 break;
10429 default:
10430 internal_error (__FILE__, __LINE__,
10431 _("read_address: bad switch, "
10432 "unsigned [in module %s]"),
10433 bfd_get_filename (abfd));
10434 }
10435 }
10436
10437 *bytes_read = cu_header->addr_size;
10438 return retval;
10439 }
10440
10441 /* Read the initial length from a section. The (draft) DWARF 3
10442 specification allows the initial length to take up either 4 bytes
10443 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10444 bytes describe the length and all offsets will be 8 bytes in length
10445 instead of 4.
10446
10447 An older, non-standard 64-bit format is also handled by this
10448 function. The older format in question stores the initial length
10449 as an 8-byte quantity without an escape value. Lengths greater
10450 than 2^32 aren't very common which means that the initial 4 bytes
10451 is almost always zero. Since a length value of zero doesn't make
10452 sense for the 32-bit format, this initial zero can be considered to
10453 be an escape value which indicates the presence of the older 64-bit
10454 format. As written, the code can't detect (old format) lengths
10455 greater than 4GB. If it becomes necessary to handle lengths
10456 somewhat larger than 4GB, we could allow other small values (such
10457 as the non-sensical values of 1, 2, and 3) to also be used as
10458 escape values indicating the presence of the old format.
10459
10460 The value returned via bytes_read should be used to increment the
10461 relevant pointer after calling read_initial_length().
10462
10463 [ Note: read_initial_length() and read_offset() are based on the
10464 document entitled "DWARF Debugging Information Format", revision
10465 3, draft 8, dated November 19, 2001. This document was obtained
10466 from:
10467
10468 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10469
10470 This document is only a draft and is subject to change. (So beware.)
10471
10472 Details regarding the older, non-standard 64-bit format were
10473 determined empirically by examining 64-bit ELF files produced by
10474 the SGI toolchain on an IRIX 6.5 machine.
10475
10476 - Kevin, July 16, 2002
10477 ] */
10478
10479 static LONGEST
10480 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10481 {
10482 LONGEST length = bfd_get_32 (abfd, buf);
10483
10484 if (length == 0xffffffff)
10485 {
10486 length = bfd_get_64 (abfd, buf + 4);
10487 *bytes_read = 12;
10488 }
10489 else if (length == 0)
10490 {
10491 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10492 length = bfd_get_64 (abfd, buf);
10493 *bytes_read = 8;
10494 }
10495 else
10496 {
10497 *bytes_read = 4;
10498 }
10499
10500 return length;
10501 }
10502
10503 /* Cover function for read_initial_length.
10504 Returns the length of the object at BUF, and stores the size of the
10505 initial length in *BYTES_READ and stores the size that offsets will be in
10506 *OFFSET_SIZE.
10507 If the initial length size is not equivalent to that specified in
10508 CU_HEADER then issue a complaint.
10509 This is useful when reading non-comp-unit headers. */
10510
10511 static LONGEST
10512 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10513 const struct comp_unit_head *cu_header,
10514 unsigned int *bytes_read,
10515 unsigned int *offset_size)
10516 {
10517 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10518
10519 gdb_assert (cu_header->initial_length_size == 4
10520 || cu_header->initial_length_size == 8
10521 || cu_header->initial_length_size == 12);
10522
10523 if (cu_header->initial_length_size != *bytes_read)
10524 complaint (&symfile_complaints,
10525 _("intermixed 32-bit and 64-bit DWARF sections"));
10526
10527 *offset_size = (*bytes_read == 4) ? 4 : 8;
10528 return length;
10529 }
10530
10531 /* Read an offset from the data stream. The size of the offset is
10532 given by cu_header->offset_size. */
10533
10534 static LONGEST
10535 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10536 unsigned int *bytes_read)
10537 {
10538 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10539
10540 *bytes_read = cu_header->offset_size;
10541 return offset;
10542 }
10543
10544 /* Read an offset from the data stream. */
10545
10546 static LONGEST
10547 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10548 {
10549 LONGEST retval = 0;
10550
10551 switch (offset_size)
10552 {
10553 case 4:
10554 retval = bfd_get_32 (abfd, buf);
10555 break;
10556 case 8:
10557 retval = bfd_get_64 (abfd, buf);
10558 break;
10559 default:
10560 internal_error (__FILE__, __LINE__,
10561 _("read_offset_1: bad switch [in module %s]"),
10562 bfd_get_filename (abfd));
10563 }
10564
10565 return retval;
10566 }
10567
10568 static gdb_byte *
10569 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10570 {
10571 /* If the size of a host char is 8 bits, we can return a pointer
10572 to the buffer, otherwise we have to copy the data to a buffer
10573 allocated on the temporary obstack. */
10574 gdb_assert (HOST_CHAR_BIT == 8);
10575 return buf;
10576 }
10577
10578 static char *
10579 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10580 {
10581 /* If the size of a host char is 8 bits, we can return a pointer
10582 to the string, otherwise we have to copy the string to a buffer
10583 allocated on the temporary obstack. */
10584 gdb_assert (HOST_CHAR_BIT == 8);
10585 if (*buf == '\0')
10586 {
10587 *bytes_read_ptr = 1;
10588 return NULL;
10589 }
10590 *bytes_read_ptr = strlen ((char *) buf) + 1;
10591 return (char *) buf;
10592 }
10593
10594 static char *
10595 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10596 {
10597 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10598 if (dwarf2_per_objfile->str.buffer == NULL)
10599 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10600 bfd_get_filename (abfd));
10601 if (str_offset >= dwarf2_per_objfile->str.size)
10602 error (_("DW_FORM_strp pointing outside of "
10603 ".debug_str section [in module %s]"),
10604 bfd_get_filename (abfd));
10605 gdb_assert (HOST_CHAR_BIT == 8);
10606 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10607 return NULL;
10608 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10609 }
10610
10611 static char *
10612 read_indirect_string (bfd *abfd, gdb_byte *buf,
10613 const struct comp_unit_head *cu_header,
10614 unsigned int *bytes_read_ptr)
10615 {
10616 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10617
10618 return read_indirect_string_at_offset (abfd, str_offset);
10619 }
10620
10621 static unsigned long
10622 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10623 {
10624 unsigned long result;
10625 unsigned int num_read;
10626 int i, shift;
10627 unsigned char byte;
10628
10629 result = 0;
10630 shift = 0;
10631 num_read = 0;
10632 i = 0;
10633 while (1)
10634 {
10635 byte = bfd_get_8 (abfd, buf);
10636 buf++;
10637 num_read++;
10638 result |= ((unsigned long)(byte & 127) << shift);
10639 if ((byte & 128) == 0)
10640 {
10641 break;
10642 }
10643 shift += 7;
10644 }
10645 *bytes_read_ptr = num_read;
10646 return result;
10647 }
10648
10649 static long
10650 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10651 {
10652 long result;
10653 int i, shift, num_read;
10654 unsigned char byte;
10655
10656 result = 0;
10657 shift = 0;
10658 num_read = 0;
10659 i = 0;
10660 while (1)
10661 {
10662 byte = bfd_get_8 (abfd, buf);
10663 buf++;
10664 num_read++;
10665 result |= ((long)(byte & 127) << shift);
10666 shift += 7;
10667 if ((byte & 128) == 0)
10668 {
10669 break;
10670 }
10671 }
10672 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10673 result |= -(((long)1) << shift);
10674 *bytes_read_ptr = num_read;
10675 return result;
10676 }
10677
10678 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10679
10680 static gdb_byte *
10681 skip_leb128 (bfd *abfd, gdb_byte *buf)
10682 {
10683 int byte;
10684
10685 while (1)
10686 {
10687 byte = bfd_get_8 (abfd, buf);
10688 buf++;
10689 if ((byte & 128) == 0)
10690 return buf;
10691 }
10692 }
10693
10694 static void
10695 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10696 {
10697 switch (lang)
10698 {
10699 case DW_LANG_C89:
10700 case DW_LANG_C99:
10701 case DW_LANG_C:
10702 cu->language = language_c;
10703 break;
10704 case DW_LANG_C_plus_plus:
10705 cu->language = language_cplus;
10706 break;
10707 case DW_LANG_D:
10708 cu->language = language_d;
10709 break;
10710 case DW_LANG_Fortran77:
10711 case DW_LANG_Fortran90:
10712 case DW_LANG_Fortran95:
10713 cu->language = language_fortran;
10714 break;
10715 case DW_LANG_Mips_Assembler:
10716 cu->language = language_asm;
10717 break;
10718 case DW_LANG_Java:
10719 cu->language = language_java;
10720 break;
10721 case DW_LANG_Ada83:
10722 case DW_LANG_Ada95:
10723 cu->language = language_ada;
10724 break;
10725 case DW_LANG_Modula2:
10726 cu->language = language_m2;
10727 break;
10728 case DW_LANG_Pascal83:
10729 cu->language = language_pascal;
10730 break;
10731 case DW_LANG_ObjC:
10732 cu->language = language_objc;
10733 break;
10734 case DW_LANG_Cobol74:
10735 case DW_LANG_Cobol85:
10736 default:
10737 cu->language = language_minimal;
10738 break;
10739 }
10740 cu->language_defn = language_def (cu->language);
10741 }
10742
10743 /* Return the named attribute or NULL if not there. */
10744
10745 static struct attribute *
10746 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10747 {
10748 unsigned int i;
10749 struct attribute *spec = NULL;
10750
10751 for (i = 0; i < die->num_attrs; ++i)
10752 {
10753 if (die->attrs[i].name == name)
10754 return &die->attrs[i];
10755 if (die->attrs[i].name == DW_AT_specification
10756 || die->attrs[i].name == DW_AT_abstract_origin)
10757 spec = &die->attrs[i];
10758 }
10759
10760 if (spec)
10761 {
10762 die = follow_die_ref (die, spec, &cu);
10763 return dwarf2_attr (die, name, cu);
10764 }
10765
10766 return NULL;
10767 }
10768
10769 /* Return the named attribute or NULL if not there,
10770 but do not follow DW_AT_specification, etc.
10771 This is for use in contexts where we're reading .debug_types dies.
10772 Following DW_AT_specification, DW_AT_abstract_origin will take us
10773 back up the chain, and we want to go down. */
10774
10775 static struct attribute *
10776 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10777 struct dwarf2_cu *cu)
10778 {
10779 unsigned int i;
10780
10781 for (i = 0; i < die->num_attrs; ++i)
10782 if (die->attrs[i].name == name)
10783 return &die->attrs[i];
10784
10785 return NULL;
10786 }
10787
10788 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10789 and holds a non-zero value. This function should only be used for
10790 DW_FORM_flag or DW_FORM_flag_present attributes. */
10791
10792 static int
10793 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10794 {
10795 struct attribute *attr = dwarf2_attr (die, name, cu);
10796
10797 return (attr && DW_UNSND (attr));
10798 }
10799
10800 static int
10801 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10802 {
10803 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10804 which value is non-zero. However, we have to be careful with
10805 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10806 (via dwarf2_flag_true_p) follows this attribute. So we may
10807 end up accidently finding a declaration attribute that belongs
10808 to a different DIE referenced by the specification attribute,
10809 even though the given DIE does not have a declaration attribute. */
10810 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10811 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10812 }
10813
10814 /* Return the die giving the specification for DIE, if there is
10815 one. *SPEC_CU is the CU containing DIE on input, and the CU
10816 containing the return value on output. If there is no
10817 specification, but there is an abstract origin, that is
10818 returned. */
10819
10820 static struct die_info *
10821 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10822 {
10823 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10824 *spec_cu);
10825
10826 if (spec_attr == NULL)
10827 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10828
10829 if (spec_attr == NULL)
10830 return NULL;
10831 else
10832 return follow_die_ref (die, spec_attr, spec_cu);
10833 }
10834
10835 /* Free the line_header structure *LH, and any arrays and strings it
10836 refers to.
10837 NOTE: This is also used as a "cleanup" function. */
10838
10839 static void
10840 free_line_header (struct line_header *lh)
10841 {
10842 if (lh->standard_opcode_lengths)
10843 xfree (lh->standard_opcode_lengths);
10844
10845 /* Remember that all the lh->file_names[i].name pointers are
10846 pointers into debug_line_buffer, and don't need to be freed. */
10847 if (lh->file_names)
10848 xfree (lh->file_names);
10849
10850 /* Similarly for the include directory names. */
10851 if (lh->include_dirs)
10852 xfree (lh->include_dirs);
10853
10854 xfree (lh);
10855 }
10856
10857 /* Add an entry to LH's include directory table. */
10858
10859 static void
10860 add_include_dir (struct line_header *lh, char *include_dir)
10861 {
10862 /* Grow the array if necessary. */
10863 if (lh->include_dirs_size == 0)
10864 {
10865 lh->include_dirs_size = 1; /* for testing */
10866 lh->include_dirs = xmalloc (lh->include_dirs_size
10867 * sizeof (*lh->include_dirs));
10868 }
10869 else if (lh->num_include_dirs >= lh->include_dirs_size)
10870 {
10871 lh->include_dirs_size *= 2;
10872 lh->include_dirs = xrealloc (lh->include_dirs,
10873 (lh->include_dirs_size
10874 * sizeof (*lh->include_dirs)));
10875 }
10876
10877 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10878 }
10879
10880 /* Add an entry to LH's file name table. */
10881
10882 static void
10883 add_file_name (struct line_header *lh,
10884 char *name,
10885 unsigned int dir_index,
10886 unsigned int mod_time,
10887 unsigned int length)
10888 {
10889 struct file_entry *fe;
10890
10891 /* Grow the array if necessary. */
10892 if (lh->file_names_size == 0)
10893 {
10894 lh->file_names_size = 1; /* for testing */
10895 lh->file_names = xmalloc (lh->file_names_size
10896 * sizeof (*lh->file_names));
10897 }
10898 else if (lh->num_file_names >= lh->file_names_size)
10899 {
10900 lh->file_names_size *= 2;
10901 lh->file_names = xrealloc (lh->file_names,
10902 (lh->file_names_size
10903 * sizeof (*lh->file_names)));
10904 }
10905
10906 fe = &lh->file_names[lh->num_file_names++];
10907 fe->name = name;
10908 fe->dir_index = dir_index;
10909 fe->mod_time = mod_time;
10910 fe->length = length;
10911 fe->included_p = 0;
10912 fe->symtab = NULL;
10913 }
10914
10915 /* Read the statement program header starting at OFFSET in
10916 .debug_line, according to the endianness of ABFD. Return a pointer
10917 to a struct line_header, allocated using xmalloc.
10918
10919 NOTE: the strings in the include directory and file name tables of
10920 the returned object point into debug_line_buffer, and must not be
10921 freed. */
10922
10923 static struct line_header *
10924 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10925 struct dwarf2_cu *cu)
10926 {
10927 struct cleanup *back_to;
10928 struct line_header *lh;
10929 gdb_byte *line_ptr;
10930 unsigned int bytes_read, offset_size;
10931 int i;
10932 char *cur_dir, *cur_file;
10933
10934 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10935 if (dwarf2_per_objfile->line.buffer == NULL)
10936 {
10937 complaint (&symfile_complaints, _("missing .debug_line section"));
10938 return 0;
10939 }
10940
10941 /* Make sure that at least there's room for the total_length field.
10942 That could be 12 bytes long, but we're just going to fudge that. */
10943 if (offset + 4 >= dwarf2_per_objfile->line.size)
10944 {
10945 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10946 return 0;
10947 }
10948
10949 lh = xmalloc (sizeof (*lh));
10950 memset (lh, 0, sizeof (*lh));
10951 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10952 (void *) lh);
10953
10954 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10955
10956 /* Read in the header. */
10957 lh->total_length =
10958 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10959 &bytes_read, &offset_size);
10960 line_ptr += bytes_read;
10961 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10962 + dwarf2_per_objfile->line.size))
10963 {
10964 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10965 return 0;
10966 }
10967 lh->statement_program_end = line_ptr + lh->total_length;
10968 lh->version = read_2_bytes (abfd, line_ptr);
10969 line_ptr += 2;
10970 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10971 line_ptr += offset_size;
10972 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10973 line_ptr += 1;
10974 if (lh->version >= 4)
10975 {
10976 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10977 line_ptr += 1;
10978 }
10979 else
10980 lh->maximum_ops_per_instruction = 1;
10981
10982 if (lh->maximum_ops_per_instruction == 0)
10983 {
10984 lh->maximum_ops_per_instruction = 1;
10985 complaint (&symfile_complaints,
10986 _("invalid maximum_ops_per_instruction "
10987 "in `.debug_line' section"));
10988 }
10989
10990 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10991 line_ptr += 1;
10992 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10993 line_ptr += 1;
10994 lh->line_range = read_1_byte (abfd, line_ptr);
10995 line_ptr += 1;
10996 lh->opcode_base = read_1_byte (abfd, line_ptr);
10997 line_ptr += 1;
10998 lh->standard_opcode_lengths
10999 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11000
11001 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11002 for (i = 1; i < lh->opcode_base; ++i)
11003 {
11004 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11005 line_ptr += 1;
11006 }
11007
11008 /* Read directory table. */
11009 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11010 {
11011 line_ptr += bytes_read;
11012 add_include_dir (lh, cur_dir);
11013 }
11014 line_ptr += bytes_read;
11015
11016 /* Read file name table. */
11017 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11018 {
11019 unsigned int dir_index, mod_time, length;
11020
11021 line_ptr += bytes_read;
11022 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11023 line_ptr += bytes_read;
11024 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11025 line_ptr += bytes_read;
11026 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11027 line_ptr += bytes_read;
11028
11029 add_file_name (lh, cur_file, dir_index, mod_time, length);
11030 }
11031 line_ptr += bytes_read;
11032 lh->statement_program_start = line_ptr;
11033
11034 if (line_ptr > (dwarf2_per_objfile->line.buffer
11035 + dwarf2_per_objfile->line.size))
11036 complaint (&symfile_complaints,
11037 _("line number info header doesn't "
11038 "fit in `.debug_line' section"));
11039
11040 discard_cleanups (back_to);
11041 return lh;
11042 }
11043
11044 /* Subroutine of dwarf_decode_lines to simplify it.
11045 Return the file name of the psymtab for included file FILE_INDEX
11046 in line header LH of PST.
11047 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11048 If space for the result is malloc'd, it will be freed by a cleanup.
11049 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11050
11051 static char *
11052 psymtab_include_file_name (const struct line_header *lh, int file_index,
11053 const struct partial_symtab *pst,
11054 const char *comp_dir)
11055 {
11056 const struct file_entry fe = lh->file_names [file_index];
11057 char *include_name = fe.name;
11058 char *include_name_to_compare = include_name;
11059 char *dir_name = NULL;
11060 const char *pst_filename;
11061 char *copied_name = NULL;
11062 int file_is_pst;
11063
11064 if (fe.dir_index)
11065 dir_name = lh->include_dirs[fe.dir_index - 1];
11066
11067 if (!IS_ABSOLUTE_PATH (include_name)
11068 && (dir_name != NULL || comp_dir != NULL))
11069 {
11070 /* Avoid creating a duplicate psymtab for PST.
11071 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11072 Before we do the comparison, however, we need to account
11073 for DIR_NAME and COMP_DIR.
11074 First prepend dir_name (if non-NULL). If we still don't
11075 have an absolute path prepend comp_dir (if non-NULL).
11076 However, the directory we record in the include-file's
11077 psymtab does not contain COMP_DIR (to match the
11078 corresponding symtab(s)).
11079
11080 Example:
11081
11082 bash$ cd /tmp
11083 bash$ gcc -g ./hello.c
11084 include_name = "hello.c"
11085 dir_name = "."
11086 DW_AT_comp_dir = comp_dir = "/tmp"
11087 DW_AT_name = "./hello.c" */
11088
11089 if (dir_name != NULL)
11090 {
11091 include_name = concat (dir_name, SLASH_STRING,
11092 include_name, (char *)NULL);
11093 include_name_to_compare = include_name;
11094 make_cleanup (xfree, include_name);
11095 }
11096 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11097 {
11098 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11099 include_name, (char *)NULL);
11100 }
11101 }
11102
11103 pst_filename = pst->filename;
11104 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11105 {
11106 copied_name = concat (pst->dirname, SLASH_STRING,
11107 pst_filename, (char *)NULL);
11108 pst_filename = copied_name;
11109 }
11110
11111 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11112
11113 if (include_name_to_compare != include_name)
11114 xfree (include_name_to_compare);
11115 if (copied_name != NULL)
11116 xfree (copied_name);
11117
11118 if (file_is_pst)
11119 return NULL;
11120 return include_name;
11121 }
11122
11123 /* Ignore this record_line request. */
11124
11125 static void
11126 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11127 {
11128 return;
11129 }
11130
11131 /* Decode the Line Number Program (LNP) for the given line_header
11132 structure and CU. The actual information extracted and the type
11133 of structures created from the LNP depends on the value of PST.
11134
11135 1. If PST is NULL, then this procedure uses the data from the program
11136 to create all necessary symbol tables, and their linetables.
11137
11138 2. If PST is not NULL, this procedure reads the program to determine
11139 the list of files included by the unit represented by PST, and
11140 builds all the associated partial symbol tables.
11141
11142 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11143 It is used for relative paths in the line table.
11144 NOTE: When processing partial symtabs (pst != NULL),
11145 comp_dir == pst->dirname.
11146
11147 NOTE: It is important that psymtabs have the same file name (via strcmp)
11148 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11149 symtab we don't use it in the name of the psymtabs we create.
11150 E.g. expand_line_sal requires this when finding psymtabs to expand.
11151 A good testcase for this is mb-inline.exp. */
11152
11153 static void
11154 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11155 struct dwarf2_cu *cu, struct partial_symtab *pst)
11156 {
11157 gdb_byte *line_ptr, *extended_end;
11158 gdb_byte *line_end;
11159 unsigned int bytes_read, extended_len;
11160 unsigned char op_code, extended_op, adj_opcode;
11161 CORE_ADDR baseaddr;
11162 struct objfile *objfile = cu->objfile;
11163 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11164 const int decode_for_pst_p = (pst != NULL);
11165 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11166 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11167 = record_line;
11168
11169 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11170
11171 line_ptr = lh->statement_program_start;
11172 line_end = lh->statement_program_end;
11173
11174 /* Read the statement sequences until there's nothing left. */
11175 while (line_ptr < line_end)
11176 {
11177 /* state machine registers */
11178 CORE_ADDR address = 0;
11179 unsigned int file = 1;
11180 unsigned int line = 1;
11181 unsigned int column = 0;
11182 int is_stmt = lh->default_is_stmt;
11183 int basic_block = 0;
11184 int end_sequence = 0;
11185 CORE_ADDR addr;
11186 unsigned char op_index = 0;
11187
11188 if (!decode_for_pst_p && lh->num_file_names >= file)
11189 {
11190 /* Start a subfile for the current file of the state machine. */
11191 /* lh->include_dirs and lh->file_names are 0-based, but the
11192 directory and file name numbers in the statement program
11193 are 1-based. */
11194 struct file_entry *fe = &lh->file_names[file - 1];
11195 char *dir = NULL;
11196
11197 if (fe->dir_index)
11198 dir = lh->include_dirs[fe->dir_index - 1];
11199
11200 dwarf2_start_subfile (fe->name, dir, comp_dir);
11201 }
11202
11203 /* Decode the table. */
11204 while (!end_sequence)
11205 {
11206 op_code = read_1_byte (abfd, line_ptr);
11207 line_ptr += 1;
11208 if (line_ptr > line_end)
11209 {
11210 dwarf2_debug_line_missing_end_sequence_complaint ();
11211 break;
11212 }
11213
11214 if (op_code >= lh->opcode_base)
11215 {
11216 /* Special operand. */
11217 adj_opcode = op_code - lh->opcode_base;
11218 address += (((op_index + (adj_opcode / lh->line_range))
11219 / lh->maximum_ops_per_instruction)
11220 * lh->minimum_instruction_length);
11221 op_index = ((op_index + (adj_opcode / lh->line_range))
11222 % lh->maximum_ops_per_instruction);
11223 line += lh->line_base + (adj_opcode % lh->line_range);
11224 if (lh->num_file_names < file || file == 0)
11225 dwarf2_debug_line_missing_file_complaint ();
11226 /* For now we ignore lines not starting on an
11227 instruction boundary. */
11228 else if (op_index == 0)
11229 {
11230 lh->file_names[file - 1].included_p = 1;
11231 if (!decode_for_pst_p && is_stmt)
11232 {
11233 if (last_subfile != current_subfile)
11234 {
11235 addr = gdbarch_addr_bits_remove (gdbarch, address);
11236 if (last_subfile)
11237 (*p_record_line) (last_subfile, 0, addr);
11238 last_subfile = current_subfile;
11239 }
11240 /* Append row to matrix using current values. */
11241 addr = gdbarch_addr_bits_remove (gdbarch, address);
11242 (*p_record_line) (current_subfile, line, addr);
11243 }
11244 }
11245 basic_block = 0;
11246 }
11247 else switch (op_code)
11248 {
11249 case DW_LNS_extended_op:
11250 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11251 &bytes_read);
11252 line_ptr += bytes_read;
11253 extended_end = line_ptr + extended_len;
11254 extended_op = read_1_byte (abfd, line_ptr);
11255 line_ptr += 1;
11256 switch (extended_op)
11257 {
11258 case DW_LNE_end_sequence:
11259 p_record_line = record_line;
11260 end_sequence = 1;
11261 break;
11262 case DW_LNE_set_address:
11263 address = read_address (abfd, line_ptr, cu, &bytes_read);
11264
11265 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11266 {
11267 /* This line table is for a function which has been
11268 GCd by the linker. Ignore it. PR gdb/12528 */
11269
11270 long line_offset
11271 = line_ptr - dwarf2_per_objfile->line.buffer;
11272
11273 complaint (&symfile_complaints,
11274 _(".debug_line address at offset 0x%lx is 0 "
11275 "[in module %s]"),
11276 line_offset, objfile->name);
11277 p_record_line = noop_record_line;
11278 }
11279
11280 op_index = 0;
11281 line_ptr += bytes_read;
11282 address += baseaddr;
11283 break;
11284 case DW_LNE_define_file:
11285 {
11286 char *cur_file;
11287 unsigned int dir_index, mod_time, length;
11288
11289 cur_file = read_direct_string (abfd, line_ptr,
11290 &bytes_read);
11291 line_ptr += bytes_read;
11292 dir_index =
11293 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11294 line_ptr += bytes_read;
11295 mod_time =
11296 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11297 line_ptr += bytes_read;
11298 length =
11299 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11300 line_ptr += bytes_read;
11301 add_file_name (lh, cur_file, dir_index, mod_time, length);
11302 }
11303 break;
11304 case DW_LNE_set_discriminator:
11305 /* The discriminator is not interesting to the debugger;
11306 just ignore it. */
11307 line_ptr = extended_end;
11308 break;
11309 default:
11310 complaint (&symfile_complaints,
11311 _("mangled .debug_line section"));
11312 return;
11313 }
11314 /* Make sure that we parsed the extended op correctly. If e.g.
11315 we expected a different address size than the producer used,
11316 we may have read the wrong number of bytes. */
11317 if (line_ptr != extended_end)
11318 {
11319 complaint (&symfile_complaints,
11320 _("mangled .debug_line section"));
11321 return;
11322 }
11323 break;
11324 case DW_LNS_copy:
11325 if (lh->num_file_names < file || file == 0)
11326 dwarf2_debug_line_missing_file_complaint ();
11327 else
11328 {
11329 lh->file_names[file - 1].included_p = 1;
11330 if (!decode_for_pst_p && is_stmt)
11331 {
11332 if (last_subfile != current_subfile)
11333 {
11334 addr = gdbarch_addr_bits_remove (gdbarch, address);
11335 if (last_subfile)
11336 (*p_record_line) (last_subfile, 0, addr);
11337 last_subfile = current_subfile;
11338 }
11339 addr = gdbarch_addr_bits_remove (gdbarch, address);
11340 (*p_record_line) (current_subfile, line, addr);
11341 }
11342 }
11343 basic_block = 0;
11344 break;
11345 case DW_LNS_advance_pc:
11346 {
11347 CORE_ADDR adjust
11348 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11349
11350 address += (((op_index + adjust)
11351 / lh->maximum_ops_per_instruction)
11352 * lh->minimum_instruction_length);
11353 op_index = ((op_index + adjust)
11354 % lh->maximum_ops_per_instruction);
11355 line_ptr += bytes_read;
11356 }
11357 break;
11358 case DW_LNS_advance_line:
11359 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11360 line_ptr += bytes_read;
11361 break;
11362 case DW_LNS_set_file:
11363 {
11364 /* The arrays lh->include_dirs and lh->file_names are
11365 0-based, but the directory and file name numbers in
11366 the statement program are 1-based. */
11367 struct file_entry *fe;
11368 char *dir = NULL;
11369
11370 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11371 line_ptr += bytes_read;
11372 if (lh->num_file_names < file || file == 0)
11373 dwarf2_debug_line_missing_file_complaint ();
11374 else
11375 {
11376 fe = &lh->file_names[file - 1];
11377 if (fe->dir_index)
11378 dir = lh->include_dirs[fe->dir_index - 1];
11379 if (!decode_for_pst_p)
11380 {
11381 last_subfile = current_subfile;
11382 dwarf2_start_subfile (fe->name, dir, comp_dir);
11383 }
11384 }
11385 }
11386 break;
11387 case DW_LNS_set_column:
11388 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11389 line_ptr += bytes_read;
11390 break;
11391 case DW_LNS_negate_stmt:
11392 is_stmt = (!is_stmt);
11393 break;
11394 case DW_LNS_set_basic_block:
11395 basic_block = 1;
11396 break;
11397 /* Add to the address register of the state machine the
11398 address increment value corresponding to special opcode
11399 255. I.e., this value is scaled by the minimum
11400 instruction length since special opcode 255 would have
11401 scaled the increment. */
11402 case DW_LNS_const_add_pc:
11403 {
11404 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11405
11406 address += (((op_index + adjust)
11407 / lh->maximum_ops_per_instruction)
11408 * lh->minimum_instruction_length);
11409 op_index = ((op_index + adjust)
11410 % lh->maximum_ops_per_instruction);
11411 }
11412 break;
11413 case DW_LNS_fixed_advance_pc:
11414 address += read_2_bytes (abfd, line_ptr);
11415 op_index = 0;
11416 line_ptr += 2;
11417 break;
11418 default:
11419 {
11420 /* Unknown standard opcode, ignore it. */
11421 int i;
11422
11423 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11424 {
11425 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11426 line_ptr += bytes_read;
11427 }
11428 }
11429 }
11430 }
11431 if (lh->num_file_names < file || file == 0)
11432 dwarf2_debug_line_missing_file_complaint ();
11433 else
11434 {
11435 lh->file_names[file - 1].included_p = 1;
11436 if (!decode_for_pst_p)
11437 {
11438 addr = gdbarch_addr_bits_remove (gdbarch, address);
11439 (*p_record_line) (current_subfile, 0, addr);
11440 }
11441 }
11442 }
11443
11444 if (decode_for_pst_p)
11445 {
11446 int file_index;
11447
11448 /* Now that we're done scanning the Line Header Program, we can
11449 create the psymtab of each included file. */
11450 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11451 if (lh->file_names[file_index].included_p == 1)
11452 {
11453 char *include_name =
11454 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11455 if (include_name != NULL)
11456 dwarf2_create_include_psymtab (include_name, pst, objfile);
11457 }
11458 }
11459 else
11460 {
11461 /* Make sure a symtab is created for every file, even files
11462 which contain only variables (i.e. no code with associated
11463 line numbers). */
11464
11465 int i;
11466 struct file_entry *fe;
11467
11468 for (i = 0; i < lh->num_file_names; i++)
11469 {
11470 char *dir = NULL;
11471
11472 fe = &lh->file_names[i];
11473 if (fe->dir_index)
11474 dir = lh->include_dirs[fe->dir_index - 1];
11475 dwarf2_start_subfile (fe->name, dir, comp_dir);
11476
11477 /* Skip the main file; we don't need it, and it must be
11478 allocated last, so that it will show up before the
11479 non-primary symtabs in the objfile's symtab list. */
11480 if (current_subfile == first_subfile)
11481 continue;
11482
11483 if (current_subfile->symtab == NULL)
11484 current_subfile->symtab = allocate_symtab (current_subfile->name,
11485 objfile);
11486 fe->symtab = current_subfile->symtab;
11487 }
11488 }
11489 }
11490
11491 /* Start a subfile for DWARF. FILENAME is the name of the file and
11492 DIRNAME the name of the source directory which contains FILENAME
11493 or NULL if not known. COMP_DIR is the compilation directory for the
11494 linetable's compilation unit or NULL if not known.
11495 This routine tries to keep line numbers from identical absolute and
11496 relative file names in a common subfile.
11497
11498 Using the `list' example from the GDB testsuite, which resides in
11499 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11500 of /srcdir/list0.c yields the following debugging information for list0.c:
11501
11502 DW_AT_name: /srcdir/list0.c
11503 DW_AT_comp_dir: /compdir
11504 files.files[0].name: list0.h
11505 files.files[0].dir: /srcdir
11506 files.files[1].name: list0.c
11507 files.files[1].dir: /srcdir
11508
11509 The line number information for list0.c has to end up in a single
11510 subfile, so that `break /srcdir/list0.c:1' works as expected.
11511 start_subfile will ensure that this happens provided that we pass the
11512 concatenation of files.files[1].dir and files.files[1].name as the
11513 subfile's name. */
11514
11515 static void
11516 dwarf2_start_subfile (char *filename, const char *dirname,
11517 const char *comp_dir)
11518 {
11519 char *fullname;
11520
11521 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11522 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11523 second argument to start_subfile. To be consistent, we do the
11524 same here. In order not to lose the line information directory,
11525 we concatenate it to the filename when it makes sense.
11526 Note that the Dwarf3 standard says (speaking of filenames in line
11527 information): ``The directory index is ignored for file names
11528 that represent full path names''. Thus ignoring dirname in the
11529 `else' branch below isn't an issue. */
11530
11531 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11532 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11533 else
11534 fullname = filename;
11535
11536 start_subfile (fullname, comp_dir);
11537
11538 if (fullname != filename)
11539 xfree (fullname);
11540 }
11541
11542 static void
11543 var_decode_location (struct attribute *attr, struct symbol *sym,
11544 struct dwarf2_cu *cu)
11545 {
11546 struct objfile *objfile = cu->objfile;
11547 struct comp_unit_head *cu_header = &cu->header;
11548
11549 /* NOTE drow/2003-01-30: There used to be a comment and some special
11550 code here to turn a symbol with DW_AT_external and a
11551 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11552 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11553 with some versions of binutils) where shared libraries could have
11554 relocations against symbols in their debug information - the
11555 minimal symbol would have the right address, but the debug info
11556 would not. It's no longer necessary, because we will explicitly
11557 apply relocations when we read in the debug information now. */
11558
11559 /* A DW_AT_location attribute with no contents indicates that a
11560 variable has been optimized away. */
11561 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11562 {
11563 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11564 return;
11565 }
11566
11567 /* Handle one degenerate form of location expression specially, to
11568 preserve GDB's previous behavior when section offsets are
11569 specified. If this is just a DW_OP_addr then mark this symbol
11570 as LOC_STATIC. */
11571
11572 if (attr_form_is_block (attr)
11573 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11574 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11575 {
11576 unsigned int dummy;
11577
11578 SYMBOL_VALUE_ADDRESS (sym) =
11579 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11580 SYMBOL_CLASS (sym) = LOC_STATIC;
11581 fixup_symbol_section (sym, objfile);
11582 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11583 SYMBOL_SECTION (sym));
11584 return;
11585 }
11586
11587 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11588 expression evaluator, and use LOC_COMPUTED only when necessary
11589 (i.e. when the value of a register or memory location is
11590 referenced, or a thread-local block, etc.). Then again, it might
11591 not be worthwhile. I'm assuming that it isn't unless performance
11592 or memory numbers show me otherwise. */
11593
11594 dwarf2_symbol_mark_computed (attr, sym, cu);
11595 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11596
11597 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11598 cu->has_loclist = 1;
11599 }
11600
11601 /* Given a pointer to a DWARF information entry, figure out if we need
11602 to make a symbol table entry for it, and if so, create a new entry
11603 and return a pointer to it.
11604 If TYPE is NULL, determine symbol type from the die, otherwise
11605 used the passed type.
11606 If SPACE is not NULL, use it to hold the new symbol. If it is
11607 NULL, allocate a new symbol on the objfile's obstack. */
11608
11609 static struct symbol *
11610 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11611 struct symbol *space)
11612 {
11613 struct objfile *objfile = cu->objfile;
11614 struct symbol *sym = NULL;
11615 char *name;
11616 struct attribute *attr = NULL;
11617 struct attribute *attr2 = NULL;
11618 CORE_ADDR baseaddr;
11619 struct pending **list_to_add = NULL;
11620
11621 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11622
11623 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11624
11625 name = dwarf2_name (die, cu);
11626 if (name)
11627 {
11628 const char *linkagename;
11629 int suppress_add = 0;
11630
11631 if (space)
11632 sym = space;
11633 else
11634 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11635 OBJSTAT (objfile, n_syms++);
11636
11637 /* Cache this symbol's name and the name's demangled form (if any). */
11638 SYMBOL_SET_LANGUAGE (sym, cu->language);
11639 linkagename = dwarf2_physname (name, die, cu);
11640 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11641
11642 /* Fortran does not have mangling standard and the mangling does differ
11643 between gfortran, iFort etc. */
11644 if (cu->language == language_fortran
11645 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11646 symbol_set_demangled_name (&(sym->ginfo),
11647 (char *) dwarf2_full_name (name, die, cu),
11648 NULL);
11649
11650 /* Default assumptions.
11651 Use the passed type or decode it from the die. */
11652 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11653 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11654 if (type != NULL)
11655 SYMBOL_TYPE (sym) = type;
11656 else
11657 SYMBOL_TYPE (sym) = die_type (die, cu);
11658 attr = dwarf2_attr (die,
11659 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11660 cu);
11661 if (attr)
11662 {
11663 SYMBOL_LINE (sym) = DW_UNSND (attr);
11664 }
11665
11666 attr = dwarf2_attr (die,
11667 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11668 cu);
11669 if (attr)
11670 {
11671 int file_index = DW_UNSND (attr);
11672
11673 if (cu->line_header == NULL
11674 || file_index > cu->line_header->num_file_names)
11675 complaint (&symfile_complaints,
11676 _("file index out of range"));
11677 else if (file_index > 0)
11678 {
11679 struct file_entry *fe;
11680
11681 fe = &cu->line_header->file_names[file_index - 1];
11682 SYMBOL_SYMTAB (sym) = fe->symtab;
11683 }
11684 }
11685
11686 switch (die->tag)
11687 {
11688 case DW_TAG_label:
11689 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11690 if (attr)
11691 {
11692 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11693 }
11694 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11695 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11696 SYMBOL_CLASS (sym) = LOC_LABEL;
11697 add_symbol_to_list (sym, cu->list_in_scope);
11698 break;
11699 case DW_TAG_subprogram:
11700 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11701 finish_block. */
11702 SYMBOL_CLASS (sym) = LOC_BLOCK;
11703 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11704 if ((attr2 && (DW_UNSND (attr2) != 0))
11705 || cu->language == language_ada)
11706 {
11707 /* Subprograms marked external are stored as a global symbol.
11708 Ada subprograms, whether marked external or not, are always
11709 stored as a global symbol, because we want to be able to
11710 access them globally. For instance, we want to be able
11711 to break on a nested subprogram without having to
11712 specify the context. */
11713 list_to_add = &global_symbols;
11714 }
11715 else
11716 {
11717 list_to_add = cu->list_in_scope;
11718 }
11719 break;
11720 case DW_TAG_inlined_subroutine:
11721 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11722 finish_block. */
11723 SYMBOL_CLASS (sym) = LOC_BLOCK;
11724 SYMBOL_INLINED (sym) = 1;
11725 /* Do not add the symbol to any lists. It will be found via
11726 BLOCK_FUNCTION from the blockvector. */
11727 break;
11728 case DW_TAG_template_value_param:
11729 suppress_add = 1;
11730 /* Fall through. */
11731 case DW_TAG_constant:
11732 case DW_TAG_variable:
11733 case DW_TAG_member:
11734 /* Compilation with minimal debug info may result in
11735 variables with missing type entries. Change the
11736 misleading `void' type to something sensible. */
11737 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11738 SYMBOL_TYPE (sym)
11739 = objfile_type (objfile)->nodebug_data_symbol;
11740
11741 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11742 /* In the case of DW_TAG_member, we should only be called for
11743 static const members. */
11744 if (die->tag == DW_TAG_member)
11745 {
11746 /* dwarf2_add_field uses die_is_declaration,
11747 so we do the same. */
11748 gdb_assert (die_is_declaration (die, cu));
11749 gdb_assert (attr);
11750 }
11751 if (attr)
11752 {
11753 dwarf2_const_value (attr, sym, cu);
11754 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11755 if (!suppress_add)
11756 {
11757 if (attr2 && (DW_UNSND (attr2) != 0))
11758 list_to_add = &global_symbols;
11759 else
11760 list_to_add = cu->list_in_scope;
11761 }
11762 break;
11763 }
11764 attr = dwarf2_attr (die, DW_AT_location, cu);
11765 if (attr)
11766 {
11767 var_decode_location (attr, sym, cu);
11768 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11769 if (SYMBOL_CLASS (sym) == LOC_STATIC
11770 && SYMBOL_VALUE_ADDRESS (sym) == 0
11771 && !dwarf2_per_objfile->has_section_at_zero)
11772 {
11773 /* When a static variable is eliminated by the linker,
11774 the corresponding debug information is not stripped
11775 out, but the variable address is set to null;
11776 do not add such variables into symbol table. */
11777 }
11778 else if (attr2 && (DW_UNSND (attr2) != 0))
11779 {
11780 /* Workaround gfortran PR debug/40040 - it uses
11781 DW_AT_location for variables in -fPIC libraries which may
11782 get overriden by other libraries/executable and get
11783 a different address. Resolve it by the minimal symbol
11784 which may come from inferior's executable using copy
11785 relocation. Make this workaround only for gfortran as for
11786 other compilers GDB cannot guess the minimal symbol
11787 Fortran mangling kind. */
11788 if (cu->language == language_fortran && die->parent
11789 && die->parent->tag == DW_TAG_module
11790 && cu->producer
11791 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11792 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11793
11794 /* A variable with DW_AT_external is never static,
11795 but it may be block-scoped. */
11796 list_to_add = (cu->list_in_scope == &file_symbols
11797 ? &global_symbols : cu->list_in_scope);
11798 }
11799 else
11800 list_to_add = cu->list_in_scope;
11801 }
11802 else
11803 {
11804 /* We do not know the address of this symbol.
11805 If it is an external symbol and we have type information
11806 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11807 The address of the variable will then be determined from
11808 the minimal symbol table whenever the variable is
11809 referenced. */
11810 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11811 if (attr2 && (DW_UNSND (attr2) != 0)
11812 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11813 {
11814 /* A variable with DW_AT_external is never static, but it
11815 may be block-scoped. */
11816 list_to_add = (cu->list_in_scope == &file_symbols
11817 ? &global_symbols : cu->list_in_scope);
11818
11819 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11820 }
11821 else if (!die_is_declaration (die, cu))
11822 {
11823 /* Use the default LOC_OPTIMIZED_OUT class. */
11824 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11825 if (!suppress_add)
11826 list_to_add = cu->list_in_scope;
11827 }
11828 }
11829 break;
11830 case DW_TAG_formal_parameter:
11831 /* If we are inside a function, mark this as an argument. If
11832 not, we might be looking at an argument to an inlined function
11833 when we do not have enough information to show inlined frames;
11834 pretend it's a local variable in that case so that the user can
11835 still see it. */
11836 if (context_stack_depth > 0
11837 && context_stack[context_stack_depth - 1].name != NULL)
11838 SYMBOL_IS_ARGUMENT (sym) = 1;
11839 attr = dwarf2_attr (die, DW_AT_location, cu);
11840 if (attr)
11841 {
11842 var_decode_location (attr, sym, cu);
11843 }
11844 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11845 if (attr)
11846 {
11847 dwarf2_const_value (attr, sym, cu);
11848 }
11849
11850 list_to_add = cu->list_in_scope;
11851 break;
11852 case DW_TAG_unspecified_parameters:
11853 /* From varargs functions; gdb doesn't seem to have any
11854 interest in this information, so just ignore it for now.
11855 (FIXME?) */
11856 break;
11857 case DW_TAG_template_type_param:
11858 suppress_add = 1;
11859 /* Fall through. */
11860 case DW_TAG_class_type:
11861 case DW_TAG_interface_type:
11862 case DW_TAG_structure_type:
11863 case DW_TAG_union_type:
11864 case DW_TAG_set_type:
11865 case DW_TAG_enumeration_type:
11866 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11867 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11868
11869 {
11870 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11871 really ever be static objects: otherwise, if you try
11872 to, say, break of a class's method and you're in a file
11873 which doesn't mention that class, it won't work unless
11874 the check for all static symbols in lookup_symbol_aux
11875 saves you. See the OtherFileClass tests in
11876 gdb.c++/namespace.exp. */
11877
11878 if (!suppress_add)
11879 {
11880 list_to_add = (cu->list_in_scope == &file_symbols
11881 && (cu->language == language_cplus
11882 || cu->language == language_java)
11883 ? &global_symbols : cu->list_in_scope);
11884
11885 /* The semantics of C++ state that "struct foo {
11886 ... }" also defines a typedef for "foo". A Java
11887 class declaration also defines a typedef for the
11888 class. */
11889 if (cu->language == language_cplus
11890 || cu->language == language_java
11891 || cu->language == language_ada)
11892 {
11893 /* The symbol's name is already allocated along
11894 with this objfile, so we don't need to
11895 duplicate it for the type. */
11896 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11897 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11898 }
11899 }
11900 }
11901 break;
11902 case DW_TAG_typedef:
11903 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11904 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11905 list_to_add = cu->list_in_scope;
11906 break;
11907 case DW_TAG_base_type:
11908 case DW_TAG_subrange_type:
11909 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11910 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11911 list_to_add = cu->list_in_scope;
11912 break;
11913 case DW_TAG_enumerator:
11914 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11915 if (attr)
11916 {
11917 dwarf2_const_value (attr, sym, cu);
11918 }
11919 {
11920 /* NOTE: carlton/2003-11-10: See comment above in the
11921 DW_TAG_class_type, etc. block. */
11922
11923 list_to_add = (cu->list_in_scope == &file_symbols
11924 && (cu->language == language_cplus
11925 || cu->language == language_java)
11926 ? &global_symbols : cu->list_in_scope);
11927 }
11928 break;
11929 case DW_TAG_namespace:
11930 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11931 list_to_add = &global_symbols;
11932 break;
11933 default:
11934 /* Not a tag we recognize. Hopefully we aren't processing
11935 trash data, but since we must specifically ignore things
11936 we don't recognize, there is nothing else we should do at
11937 this point. */
11938 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11939 dwarf_tag_name (die->tag));
11940 break;
11941 }
11942
11943 if (suppress_add)
11944 {
11945 sym->hash_next = objfile->template_symbols;
11946 objfile->template_symbols = sym;
11947 list_to_add = NULL;
11948 }
11949
11950 if (list_to_add != NULL)
11951 add_symbol_to_list (sym, list_to_add);
11952
11953 /* For the benefit of old versions of GCC, check for anonymous
11954 namespaces based on the demangled name. */
11955 if (!processing_has_namespace_info
11956 && cu->language == language_cplus)
11957 cp_scan_for_anonymous_namespaces (sym, objfile);
11958 }
11959 return (sym);
11960 }
11961
11962 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11963
11964 static struct symbol *
11965 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11966 {
11967 return new_symbol_full (die, type, cu, NULL);
11968 }
11969
11970 /* Given an attr with a DW_FORM_dataN value in host byte order,
11971 zero-extend it as appropriate for the symbol's type. The DWARF
11972 standard (v4) is not entirely clear about the meaning of using
11973 DW_FORM_dataN for a constant with a signed type, where the type is
11974 wider than the data. The conclusion of a discussion on the DWARF
11975 list was that this is unspecified. We choose to always zero-extend
11976 because that is the interpretation long in use by GCC. */
11977
11978 static gdb_byte *
11979 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11980 const char *name, struct obstack *obstack,
11981 struct dwarf2_cu *cu, long *value, int bits)
11982 {
11983 struct objfile *objfile = cu->objfile;
11984 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11985 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11986 LONGEST l = DW_UNSND (attr);
11987
11988 if (bits < sizeof (*value) * 8)
11989 {
11990 l &= ((LONGEST) 1 << bits) - 1;
11991 *value = l;
11992 }
11993 else if (bits == sizeof (*value) * 8)
11994 *value = l;
11995 else
11996 {
11997 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11998 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11999 return bytes;
12000 }
12001
12002 return NULL;
12003 }
12004
12005 /* Read a constant value from an attribute. Either set *VALUE, or if
12006 the value does not fit in *VALUE, set *BYTES - either already
12007 allocated on the objfile obstack, or newly allocated on OBSTACK,
12008 or, set *BATON, if we translated the constant to a location
12009 expression. */
12010
12011 static void
12012 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12013 const char *name, struct obstack *obstack,
12014 struct dwarf2_cu *cu,
12015 long *value, gdb_byte **bytes,
12016 struct dwarf2_locexpr_baton **baton)
12017 {
12018 struct objfile *objfile = cu->objfile;
12019 struct comp_unit_head *cu_header = &cu->header;
12020 struct dwarf_block *blk;
12021 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12022 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12023
12024 *value = 0;
12025 *bytes = NULL;
12026 *baton = NULL;
12027
12028 switch (attr->form)
12029 {
12030 case DW_FORM_addr:
12031 {
12032 gdb_byte *data;
12033
12034 if (TYPE_LENGTH (type) != cu_header->addr_size)
12035 dwarf2_const_value_length_mismatch_complaint (name,
12036 cu_header->addr_size,
12037 TYPE_LENGTH (type));
12038 /* Symbols of this form are reasonably rare, so we just
12039 piggyback on the existing location code rather than writing
12040 a new implementation of symbol_computed_ops. */
12041 *baton = obstack_alloc (&objfile->objfile_obstack,
12042 sizeof (struct dwarf2_locexpr_baton));
12043 (*baton)->per_cu = cu->per_cu;
12044 gdb_assert ((*baton)->per_cu);
12045
12046 (*baton)->size = 2 + cu_header->addr_size;
12047 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12048 (*baton)->data = data;
12049
12050 data[0] = DW_OP_addr;
12051 store_unsigned_integer (&data[1], cu_header->addr_size,
12052 byte_order, DW_ADDR (attr));
12053 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12054 }
12055 break;
12056 case DW_FORM_string:
12057 case DW_FORM_strp:
12058 /* DW_STRING is already allocated on the objfile obstack, point
12059 directly to it. */
12060 *bytes = (gdb_byte *) DW_STRING (attr);
12061 break;
12062 case DW_FORM_block1:
12063 case DW_FORM_block2:
12064 case DW_FORM_block4:
12065 case DW_FORM_block:
12066 case DW_FORM_exprloc:
12067 blk = DW_BLOCK (attr);
12068 if (TYPE_LENGTH (type) != blk->size)
12069 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12070 TYPE_LENGTH (type));
12071 *bytes = blk->data;
12072 break;
12073
12074 /* The DW_AT_const_value attributes are supposed to carry the
12075 symbol's value "represented as it would be on the target
12076 architecture." By the time we get here, it's already been
12077 converted to host endianness, so we just need to sign- or
12078 zero-extend it as appropriate. */
12079 case DW_FORM_data1:
12080 *bytes = dwarf2_const_value_data (attr, type, name,
12081 obstack, cu, value, 8);
12082 break;
12083 case DW_FORM_data2:
12084 *bytes = dwarf2_const_value_data (attr, type, name,
12085 obstack, cu, value, 16);
12086 break;
12087 case DW_FORM_data4:
12088 *bytes = dwarf2_const_value_data (attr, type, name,
12089 obstack, cu, value, 32);
12090 break;
12091 case DW_FORM_data8:
12092 *bytes = dwarf2_const_value_data (attr, type, name,
12093 obstack, cu, value, 64);
12094 break;
12095
12096 case DW_FORM_sdata:
12097 *value = DW_SND (attr);
12098 break;
12099
12100 case DW_FORM_udata:
12101 *value = DW_UNSND (attr);
12102 break;
12103
12104 default:
12105 complaint (&symfile_complaints,
12106 _("unsupported const value attribute form: '%s'"),
12107 dwarf_form_name (attr->form));
12108 *value = 0;
12109 break;
12110 }
12111 }
12112
12113
12114 /* Copy constant value from an attribute to a symbol. */
12115
12116 static void
12117 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12118 struct dwarf2_cu *cu)
12119 {
12120 struct objfile *objfile = cu->objfile;
12121 struct comp_unit_head *cu_header = &cu->header;
12122 long value;
12123 gdb_byte *bytes;
12124 struct dwarf2_locexpr_baton *baton;
12125
12126 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12127 SYMBOL_PRINT_NAME (sym),
12128 &objfile->objfile_obstack, cu,
12129 &value, &bytes, &baton);
12130
12131 if (baton != NULL)
12132 {
12133 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12134 SYMBOL_LOCATION_BATON (sym) = baton;
12135 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12136 }
12137 else if (bytes != NULL)
12138 {
12139 SYMBOL_VALUE_BYTES (sym) = bytes;
12140 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12141 }
12142 else
12143 {
12144 SYMBOL_VALUE (sym) = value;
12145 SYMBOL_CLASS (sym) = LOC_CONST;
12146 }
12147 }
12148
12149 /* Return the type of the die in question using its DW_AT_type attribute. */
12150
12151 static struct type *
12152 die_type (struct die_info *die, struct dwarf2_cu *cu)
12153 {
12154 struct attribute *type_attr;
12155
12156 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12157 if (!type_attr)
12158 {
12159 /* A missing DW_AT_type represents a void type. */
12160 return objfile_type (cu->objfile)->builtin_void;
12161 }
12162
12163 return lookup_die_type (die, type_attr, cu);
12164 }
12165
12166 /* True iff CU's producer generates GNAT Ada auxiliary information
12167 that allows to find parallel types through that information instead
12168 of having to do expensive parallel lookups by type name. */
12169
12170 static int
12171 need_gnat_info (struct dwarf2_cu *cu)
12172 {
12173 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12174 of GNAT produces this auxiliary information, without any indication
12175 that it is produced. Part of enhancing the FSF version of GNAT
12176 to produce that information will be to put in place an indicator
12177 that we can use in order to determine whether the descriptive type
12178 info is available or not. One suggestion that has been made is
12179 to use a new attribute, attached to the CU die. For now, assume
12180 that the descriptive type info is not available. */
12181 return 0;
12182 }
12183
12184 /* Return the auxiliary type of the die in question using its
12185 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12186 attribute is not present. */
12187
12188 static struct type *
12189 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12190 {
12191 struct attribute *type_attr;
12192
12193 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12194 if (!type_attr)
12195 return NULL;
12196
12197 return lookup_die_type (die, type_attr, cu);
12198 }
12199
12200 /* If DIE has a descriptive_type attribute, then set the TYPE's
12201 descriptive type accordingly. */
12202
12203 static void
12204 set_descriptive_type (struct type *type, struct die_info *die,
12205 struct dwarf2_cu *cu)
12206 {
12207 struct type *descriptive_type = die_descriptive_type (die, cu);
12208
12209 if (descriptive_type)
12210 {
12211 ALLOCATE_GNAT_AUX_TYPE (type);
12212 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12213 }
12214 }
12215
12216 /* Return the containing type of the die in question using its
12217 DW_AT_containing_type attribute. */
12218
12219 static struct type *
12220 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12221 {
12222 struct attribute *type_attr;
12223
12224 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12225 if (!type_attr)
12226 error (_("Dwarf Error: Problem turning containing type into gdb type "
12227 "[in module %s]"), cu->objfile->name);
12228
12229 return lookup_die_type (die, type_attr, cu);
12230 }
12231
12232 /* Look up the type of DIE in CU using its type attribute ATTR.
12233 If there is no type substitute an error marker. */
12234
12235 static struct type *
12236 lookup_die_type (struct die_info *die, struct attribute *attr,
12237 struct dwarf2_cu *cu)
12238 {
12239 struct objfile *objfile = cu->objfile;
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, 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), 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 objfile->name,
12296 cu->header.offset,
12297 die->offset);
12298 saved = obstack_copy0 (&objfile->objfile_obstack,
12299 message, strlen (message));
12300 xfree (message);
12301
12302 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, 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);
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);
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);
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->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->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 (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 /* Load the DIEs associated with type unit PER_CU into memory. */
14368
14369 static void
14370 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14371 {
14372 struct objfile *objfile = per_cu->objfile;
14373 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14374 unsigned int offset = per_cu->offset;
14375 struct signatured_type *type_sig;
14376
14377 dwarf2_read_section (objfile, sect);
14378
14379 /* We have the section offset, but we need the signature to do the
14380 hash table lookup. */
14381 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14382 the signature to assert we found the right one.
14383 Ok, but it's a lot of work. We should simplify things so any needed
14384 assert doesn't require all this clumsiness. */
14385 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14386
14387 gdb_assert (type_sig->per_cu.cu == NULL);
14388
14389 read_signatured_type (type_sig);
14390
14391 gdb_assert (type_sig->per_cu.cu != NULL);
14392 }
14393
14394 /* Read in a signatured type and build its CU and DIEs. */
14395
14396 static void
14397 read_signatured_type (struct signatured_type *type_sig)
14398 {
14399 struct objfile *objfile = type_sig->per_cu.objfile;
14400 gdb_byte *types_ptr;
14401 struct die_reader_specs reader_specs;
14402 struct dwarf2_cu *cu;
14403 ULONGEST signature;
14404 struct cleanup *back_to, *free_cu_cleanup;
14405 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14406
14407 dwarf2_read_section (objfile, section);
14408 types_ptr = section->buffer + type_sig->per_cu.offset;
14409
14410 gdb_assert (type_sig->per_cu.cu == NULL);
14411
14412 cu = xmalloc (sizeof (*cu));
14413 init_one_comp_unit (cu, &type_sig->per_cu);
14414
14415 /* If an error occurs while loading, release our storage. */
14416 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14417
14418 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14419 types_ptr, objfile->obfd);
14420 gdb_assert (signature == type_sig->signature);
14421
14422 cu->die_hash
14423 = htab_create_alloc_ex (cu->header.length / 12,
14424 die_hash,
14425 die_eq,
14426 NULL,
14427 &cu->comp_unit_obstack,
14428 hashtab_obstack_allocate,
14429 dummy_obstack_deallocate);
14430
14431 dwarf2_read_abbrevs (cu);
14432 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14433
14434 init_cu_die_reader (&reader_specs, cu);
14435
14436 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14437 NULL /*parent*/);
14438
14439 /* We try not to read any attributes in this function, because not
14440 all CUs needed for references have been loaded yet, and symbol
14441 table processing isn't initialized. But we have to set the CU language,
14442 or we won't be able to build types correctly. */
14443 prepare_one_comp_unit (cu, cu->dies);
14444
14445 do_cleanups (back_to);
14446
14447 /* We've successfully allocated this compilation unit. Let our caller
14448 clean it up when finished with it. */
14449 discard_cleanups (free_cu_cleanup);
14450
14451 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14452 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14453 }
14454
14455 /* Decode simple location descriptions.
14456 Given a pointer to a dwarf block that defines a location, compute
14457 the location and return the value.
14458
14459 NOTE drow/2003-11-18: This function is called in two situations
14460 now: for the address of static or global variables (partial symbols
14461 only) and for offsets into structures which are expected to be
14462 (more or less) constant. The partial symbol case should go away,
14463 and only the constant case should remain. That will let this
14464 function complain more accurately. A few special modes are allowed
14465 without complaint for global variables (for instance, global
14466 register values and thread-local values).
14467
14468 A location description containing no operations indicates that the
14469 object is optimized out. The return value is 0 for that case.
14470 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14471 callers will only want a very basic result and this can become a
14472 complaint.
14473
14474 Note that stack[0] is unused except as a default error return. */
14475
14476 static CORE_ADDR
14477 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14478 {
14479 struct objfile *objfile = cu->objfile;
14480 int i;
14481 int size = blk->size;
14482 gdb_byte *data = blk->data;
14483 CORE_ADDR stack[64];
14484 int stacki;
14485 unsigned int bytes_read, unsnd;
14486 gdb_byte op;
14487
14488 i = 0;
14489 stacki = 0;
14490 stack[stacki] = 0;
14491 stack[++stacki] = 0;
14492
14493 while (i < size)
14494 {
14495 op = data[i++];
14496 switch (op)
14497 {
14498 case DW_OP_lit0:
14499 case DW_OP_lit1:
14500 case DW_OP_lit2:
14501 case DW_OP_lit3:
14502 case DW_OP_lit4:
14503 case DW_OP_lit5:
14504 case DW_OP_lit6:
14505 case DW_OP_lit7:
14506 case DW_OP_lit8:
14507 case DW_OP_lit9:
14508 case DW_OP_lit10:
14509 case DW_OP_lit11:
14510 case DW_OP_lit12:
14511 case DW_OP_lit13:
14512 case DW_OP_lit14:
14513 case DW_OP_lit15:
14514 case DW_OP_lit16:
14515 case DW_OP_lit17:
14516 case DW_OP_lit18:
14517 case DW_OP_lit19:
14518 case DW_OP_lit20:
14519 case DW_OP_lit21:
14520 case DW_OP_lit22:
14521 case DW_OP_lit23:
14522 case DW_OP_lit24:
14523 case DW_OP_lit25:
14524 case DW_OP_lit26:
14525 case DW_OP_lit27:
14526 case DW_OP_lit28:
14527 case DW_OP_lit29:
14528 case DW_OP_lit30:
14529 case DW_OP_lit31:
14530 stack[++stacki] = op - DW_OP_lit0;
14531 break;
14532
14533 case DW_OP_reg0:
14534 case DW_OP_reg1:
14535 case DW_OP_reg2:
14536 case DW_OP_reg3:
14537 case DW_OP_reg4:
14538 case DW_OP_reg5:
14539 case DW_OP_reg6:
14540 case DW_OP_reg7:
14541 case DW_OP_reg8:
14542 case DW_OP_reg9:
14543 case DW_OP_reg10:
14544 case DW_OP_reg11:
14545 case DW_OP_reg12:
14546 case DW_OP_reg13:
14547 case DW_OP_reg14:
14548 case DW_OP_reg15:
14549 case DW_OP_reg16:
14550 case DW_OP_reg17:
14551 case DW_OP_reg18:
14552 case DW_OP_reg19:
14553 case DW_OP_reg20:
14554 case DW_OP_reg21:
14555 case DW_OP_reg22:
14556 case DW_OP_reg23:
14557 case DW_OP_reg24:
14558 case DW_OP_reg25:
14559 case DW_OP_reg26:
14560 case DW_OP_reg27:
14561 case DW_OP_reg28:
14562 case DW_OP_reg29:
14563 case DW_OP_reg30:
14564 case DW_OP_reg31:
14565 stack[++stacki] = op - DW_OP_reg0;
14566 if (i < size)
14567 dwarf2_complex_location_expr_complaint ();
14568 break;
14569
14570 case DW_OP_regx:
14571 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14572 i += bytes_read;
14573 stack[++stacki] = unsnd;
14574 if (i < size)
14575 dwarf2_complex_location_expr_complaint ();
14576 break;
14577
14578 case DW_OP_addr:
14579 stack[++stacki] = read_address (objfile->obfd, &data[i],
14580 cu, &bytes_read);
14581 i += bytes_read;
14582 break;
14583
14584 case DW_OP_const1u:
14585 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14586 i += 1;
14587 break;
14588
14589 case DW_OP_const1s:
14590 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14591 i += 1;
14592 break;
14593
14594 case DW_OP_const2u:
14595 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14596 i += 2;
14597 break;
14598
14599 case DW_OP_const2s:
14600 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14601 i += 2;
14602 break;
14603
14604 case DW_OP_const4u:
14605 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14606 i += 4;
14607 break;
14608
14609 case DW_OP_const4s:
14610 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14611 i += 4;
14612 break;
14613
14614 case DW_OP_const8u:
14615 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14616 i += 8;
14617 break;
14618
14619 case DW_OP_constu:
14620 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14621 &bytes_read);
14622 i += bytes_read;
14623 break;
14624
14625 case DW_OP_consts:
14626 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14627 i += bytes_read;
14628 break;
14629
14630 case DW_OP_dup:
14631 stack[stacki + 1] = stack[stacki];
14632 stacki++;
14633 break;
14634
14635 case DW_OP_plus:
14636 stack[stacki - 1] += stack[stacki];
14637 stacki--;
14638 break;
14639
14640 case DW_OP_plus_uconst:
14641 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14642 &bytes_read);
14643 i += bytes_read;
14644 break;
14645
14646 case DW_OP_minus:
14647 stack[stacki - 1] -= stack[stacki];
14648 stacki--;
14649 break;
14650
14651 case DW_OP_deref:
14652 /* If we're not the last op, then we definitely can't encode
14653 this using GDB's address_class enum. This is valid for partial
14654 global symbols, although the variable's address will be bogus
14655 in the psymtab. */
14656 if (i < size)
14657 dwarf2_complex_location_expr_complaint ();
14658 break;
14659
14660 case DW_OP_GNU_push_tls_address:
14661 /* The top of the stack has the offset from the beginning
14662 of the thread control block at which the variable is located. */
14663 /* Nothing should follow this operator, so the top of stack would
14664 be returned. */
14665 /* This is valid for partial global symbols, but the variable's
14666 address will be bogus in the psymtab. Make it always at least
14667 non-zero to not look as a variable garbage collected by linker
14668 which have DW_OP_addr 0. */
14669 if (i < size)
14670 dwarf2_complex_location_expr_complaint ();
14671 stack[stacki]++;
14672 break;
14673
14674 case DW_OP_GNU_uninit:
14675 break;
14676
14677 default:
14678 {
14679 const char *name = dwarf_stack_op_name (op);
14680
14681 if (name)
14682 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14683 name);
14684 else
14685 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14686 op);
14687 }
14688
14689 return (stack[stacki]);
14690 }
14691
14692 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14693 outside of the allocated space. Also enforce minimum>0. */
14694 if (stacki >= ARRAY_SIZE (stack) - 1)
14695 {
14696 complaint (&symfile_complaints,
14697 _("location description stack overflow"));
14698 return 0;
14699 }
14700
14701 if (stacki <= 0)
14702 {
14703 complaint (&symfile_complaints,
14704 _("location description stack underflow"));
14705 return 0;
14706 }
14707 }
14708 return (stack[stacki]);
14709 }
14710
14711 /* memory allocation interface */
14712
14713 static struct dwarf_block *
14714 dwarf_alloc_block (struct dwarf2_cu *cu)
14715 {
14716 struct dwarf_block *blk;
14717
14718 blk = (struct dwarf_block *)
14719 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14720 return (blk);
14721 }
14722
14723 static struct abbrev_info *
14724 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14725 {
14726 struct abbrev_info *abbrev;
14727
14728 abbrev = (struct abbrev_info *)
14729 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14730 memset (abbrev, 0, sizeof (struct abbrev_info));
14731 return (abbrev);
14732 }
14733
14734 static struct die_info *
14735 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14736 {
14737 struct die_info *die;
14738 size_t size = sizeof (struct die_info);
14739
14740 if (num_attrs > 1)
14741 size += (num_attrs - 1) * sizeof (struct attribute);
14742
14743 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14744 memset (die, 0, sizeof (struct die_info));
14745 return (die);
14746 }
14747
14748 \f
14749 /* Macro support. */
14750
14751 /* Return the full name of file number I in *LH's file name table.
14752 Use COMP_DIR as the name of the current directory of the
14753 compilation. The result is allocated using xmalloc; the caller is
14754 responsible for freeing it. */
14755 static char *
14756 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14757 {
14758 /* Is the file number a valid index into the line header's file name
14759 table? Remember that file numbers start with one, not zero. */
14760 if (1 <= file && file <= lh->num_file_names)
14761 {
14762 struct file_entry *fe = &lh->file_names[file - 1];
14763
14764 if (IS_ABSOLUTE_PATH (fe->name))
14765 return xstrdup (fe->name);
14766 else
14767 {
14768 const char *dir;
14769 int dir_len;
14770 char *full_name;
14771
14772 if (fe->dir_index)
14773 dir = lh->include_dirs[fe->dir_index - 1];
14774 else
14775 dir = comp_dir;
14776
14777 if (dir)
14778 {
14779 dir_len = strlen (dir);
14780 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14781 strcpy (full_name, dir);
14782 full_name[dir_len] = '/';
14783 strcpy (full_name + dir_len + 1, fe->name);
14784 return full_name;
14785 }
14786 else
14787 return xstrdup (fe->name);
14788 }
14789 }
14790 else
14791 {
14792 /* The compiler produced a bogus file number. We can at least
14793 record the macro definitions made in the file, even if we
14794 won't be able to find the file by name. */
14795 char fake_name[80];
14796
14797 sprintf (fake_name, "<bad macro file number %d>", file);
14798
14799 complaint (&symfile_complaints,
14800 _("bad file number in macro information (%d)"),
14801 file);
14802
14803 return xstrdup (fake_name);
14804 }
14805 }
14806
14807
14808 static struct macro_source_file *
14809 macro_start_file (int file, int line,
14810 struct macro_source_file *current_file,
14811 const char *comp_dir,
14812 struct line_header *lh, struct objfile *objfile)
14813 {
14814 /* The full name of this source file. */
14815 char *full_name = file_full_name (file, lh, comp_dir);
14816
14817 /* We don't create a macro table for this compilation unit
14818 at all until we actually get a filename. */
14819 if (! pending_macros)
14820 pending_macros = new_macro_table (&objfile->objfile_obstack,
14821 objfile->macro_cache);
14822
14823 if (! current_file)
14824 /* If we have no current file, then this must be the start_file
14825 directive for the compilation unit's main source file. */
14826 current_file = macro_set_main (pending_macros, full_name);
14827 else
14828 current_file = macro_include (current_file, line, full_name);
14829
14830 xfree (full_name);
14831
14832 return current_file;
14833 }
14834
14835
14836 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14837 followed by a null byte. */
14838 static char *
14839 copy_string (const char *buf, int len)
14840 {
14841 char *s = xmalloc (len + 1);
14842
14843 memcpy (s, buf, len);
14844 s[len] = '\0';
14845 return s;
14846 }
14847
14848
14849 static const char *
14850 consume_improper_spaces (const char *p, const char *body)
14851 {
14852 if (*p == ' ')
14853 {
14854 complaint (&symfile_complaints,
14855 _("macro definition contains spaces "
14856 "in formal argument list:\n`%s'"),
14857 body);
14858
14859 while (*p == ' ')
14860 p++;
14861 }
14862
14863 return p;
14864 }
14865
14866
14867 static void
14868 parse_macro_definition (struct macro_source_file *file, int line,
14869 const char *body)
14870 {
14871 const char *p;
14872
14873 /* The body string takes one of two forms. For object-like macro
14874 definitions, it should be:
14875
14876 <macro name> " " <definition>
14877
14878 For function-like macro definitions, it should be:
14879
14880 <macro name> "() " <definition>
14881 or
14882 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14883
14884 Spaces may appear only where explicitly indicated, and in the
14885 <definition>.
14886
14887 The Dwarf 2 spec says that an object-like macro's name is always
14888 followed by a space, but versions of GCC around March 2002 omit
14889 the space when the macro's definition is the empty string.
14890
14891 The Dwarf 2 spec says that there should be no spaces between the
14892 formal arguments in a function-like macro's formal argument list,
14893 but versions of GCC around March 2002 include spaces after the
14894 commas. */
14895
14896
14897 /* Find the extent of the macro name. The macro name is terminated
14898 by either a space or null character (for an object-like macro) or
14899 an opening paren (for a function-like macro). */
14900 for (p = body; *p; p++)
14901 if (*p == ' ' || *p == '(')
14902 break;
14903
14904 if (*p == ' ' || *p == '\0')
14905 {
14906 /* It's an object-like macro. */
14907 int name_len = p - body;
14908 char *name = copy_string (body, name_len);
14909 const char *replacement;
14910
14911 if (*p == ' ')
14912 replacement = body + name_len + 1;
14913 else
14914 {
14915 dwarf2_macro_malformed_definition_complaint (body);
14916 replacement = body + name_len;
14917 }
14918
14919 macro_define_object (file, line, name, replacement);
14920
14921 xfree (name);
14922 }
14923 else if (*p == '(')
14924 {
14925 /* It's a function-like macro. */
14926 char *name = copy_string (body, p - body);
14927 int argc = 0;
14928 int argv_size = 1;
14929 char **argv = xmalloc (argv_size * sizeof (*argv));
14930
14931 p++;
14932
14933 p = consume_improper_spaces (p, body);
14934
14935 /* Parse the formal argument list. */
14936 while (*p && *p != ')')
14937 {
14938 /* Find the extent of the current argument name. */
14939 const char *arg_start = p;
14940
14941 while (*p && *p != ',' && *p != ')' && *p != ' ')
14942 p++;
14943
14944 if (! *p || p == arg_start)
14945 dwarf2_macro_malformed_definition_complaint (body);
14946 else
14947 {
14948 /* Make sure argv has room for the new argument. */
14949 if (argc >= argv_size)
14950 {
14951 argv_size *= 2;
14952 argv = xrealloc (argv, argv_size * sizeof (*argv));
14953 }
14954
14955 argv[argc++] = copy_string (arg_start, p - arg_start);
14956 }
14957
14958 p = consume_improper_spaces (p, body);
14959
14960 /* Consume the comma, if present. */
14961 if (*p == ',')
14962 {
14963 p++;
14964
14965 p = consume_improper_spaces (p, body);
14966 }
14967 }
14968
14969 if (*p == ')')
14970 {
14971 p++;
14972
14973 if (*p == ' ')
14974 /* Perfectly formed definition, no complaints. */
14975 macro_define_function (file, line, name,
14976 argc, (const char **) argv,
14977 p + 1);
14978 else if (*p == '\0')
14979 {
14980 /* Complain, but do define it. */
14981 dwarf2_macro_malformed_definition_complaint (body);
14982 macro_define_function (file, line, name,
14983 argc, (const char **) argv,
14984 p);
14985 }
14986 else
14987 /* Just complain. */
14988 dwarf2_macro_malformed_definition_complaint (body);
14989 }
14990 else
14991 /* Just complain. */
14992 dwarf2_macro_malformed_definition_complaint (body);
14993
14994 xfree (name);
14995 {
14996 int i;
14997
14998 for (i = 0; i < argc; i++)
14999 xfree (argv[i]);
15000 }
15001 xfree (argv);
15002 }
15003 else
15004 dwarf2_macro_malformed_definition_complaint (body);
15005 }
15006
15007 /* Skip some bytes from BYTES according to the form given in FORM.
15008 Returns the new pointer. */
15009
15010 static gdb_byte *
15011 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15012 enum dwarf_form form,
15013 unsigned int offset_size,
15014 struct dwarf2_section_info *section)
15015 {
15016 unsigned int bytes_read;
15017
15018 switch (form)
15019 {
15020 case DW_FORM_data1:
15021 case DW_FORM_flag:
15022 ++bytes;
15023 break;
15024
15025 case DW_FORM_data2:
15026 bytes += 2;
15027 break;
15028
15029 case DW_FORM_data4:
15030 bytes += 4;
15031 break;
15032
15033 case DW_FORM_data8:
15034 bytes += 8;
15035 break;
15036
15037 case DW_FORM_string:
15038 read_direct_string (abfd, bytes, &bytes_read);
15039 bytes += bytes_read;
15040 break;
15041
15042 case DW_FORM_sec_offset:
15043 case DW_FORM_strp:
15044 bytes += offset_size;
15045 break;
15046
15047 case DW_FORM_block:
15048 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15049 bytes += bytes_read;
15050 break;
15051
15052 case DW_FORM_block1:
15053 bytes += 1 + read_1_byte (abfd, bytes);
15054 break;
15055 case DW_FORM_block2:
15056 bytes += 2 + read_2_bytes (abfd, bytes);
15057 break;
15058 case DW_FORM_block4:
15059 bytes += 4 + read_4_bytes (abfd, bytes);
15060 break;
15061
15062 case DW_FORM_sdata:
15063 case DW_FORM_udata:
15064 bytes = skip_leb128 (abfd, bytes);
15065 break;
15066
15067 default:
15068 {
15069 complain:
15070 complaint (&symfile_complaints,
15071 _("invalid form 0x%x in `%s'"),
15072 form,
15073 section->asection->name);
15074 return NULL;
15075 }
15076 }
15077
15078 return bytes;
15079 }
15080
15081 /* A helper for dwarf_decode_macros that handles skipping an unknown
15082 opcode. Returns an updated pointer to the macro data buffer; or,
15083 on error, issues a complaint and returns NULL. */
15084
15085 static gdb_byte *
15086 skip_unknown_opcode (unsigned int opcode,
15087 gdb_byte **opcode_definitions,
15088 gdb_byte *mac_ptr,
15089 bfd *abfd,
15090 unsigned int offset_size,
15091 struct dwarf2_section_info *section)
15092 {
15093 unsigned int bytes_read, i;
15094 unsigned long arg;
15095 gdb_byte *defn;
15096
15097 if (opcode_definitions[opcode] == NULL)
15098 {
15099 complaint (&symfile_complaints,
15100 _("unrecognized DW_MACFINO opcode 0x%x"),
15101 opcode);
15102 return NULL;
15103 }
15104
15105 defn = opcode_definitions[opcode];
15106 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15107 defn += bytes_read;
15108
15109 for (i = 0; i < arg; ++i)
15110 {
15111 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15112 if (mac_ptr == NULL)
15113 {
15114 /* skip_form_bytes already issued the complaint. */
15115 return NULL;
15116 }
15117 }
15118
15119 return mac_ptr;
15120 }
15121
15122 /* A helper function which parses the header of a macro section.
15123 If the macro section is the extended (for now called "GNU") type,
15124 then this updates *OFFSET_SIZE. Returns a pointer to just after
15125 the header, or issues a complaint and returns NULL on error. */
15126
15127 static gdb_byte *
15128 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15129 bfd *abfd,
15130 gdb_byte *mac_ptr,
15131 unsigned int *offset_size,
15132 int section_is_gnu)
15133 {
15134 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15135
15136 if (section_is_gnu)
15137 {
15138 unsigned int version, flags;
15139
15140 version = read_2_bytes (abfd, mac_ptr);
15141 if (version != 4)
15142 {
15143 complaint (&symfile_complaints,
15144 _("unrecognized version `%d' in .debug_macro section"),
15145 version);
15146 return NULL;
15147 }
15148 mac_ptr += 2;
15149
15150 flags = read_1_byte (abfd, mac_ptr);
15151 ++mac_ptr;
15152 *offset_size = (flags & 1) ? 8 : 4;
15153
15154 if ((flags & 2) != 0)
15155 /* We don't need the line table offset. */
15156 mac_ptr += *offset_size;
15157
15158 /* Vendor opcode descriptions. */
15159 if ((flags & 4) != 0)
15160 {
15161 unsigned int i, count;
15162
15163 count = read_1_byte (abfd, mac_ptr);
15164 ++mac_ptr;
15165 for (i = 0; i < count; ++i)
15166 {
15167 unsigned int opcode, bytes_read;
15168 unsigned long arg;
15169
15170 opcode = read_1_byte (abfd, mac_ptr);
15171 ++mac_ptr;
15172 opcode_definitions[opcode] = mac_ptr;
15173 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15174 mac_ptr += bytes_read;
15175 mac_ptr += arg;
15176 }
15177 }
15178 }
15179
15180 return mac_ptr;
15181 }
15182
15183 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15184 including DW_GNU_MACINFO_transparent_include. */
15185
15186 static void
15187 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15188 struct macro_source_file *current_file,
15189 struct line_header *lh, char *comp_dir,
15190 struct dwarf2_section_info *section,
15191 int section_is_gnu,
15192 unsigned int offset_size,
15193 struct objfile *objfile)
15194 {
15195 enum dwarf_macro_record_type macinfo_type;
15196 int at_commandline;
15197 gdb_byte *opcode_definitions[256];
15198
15199 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15200 &offset_size, section_is_gnu);
15201 if (mac_ptr == NULL)
15202 {
15203 /* We already issued a complaint. */
15204 return;
15205 }
15206
15207 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15208 GDB is still reading the definitions from command line. First
15209 DW_MACINFO_start_file will need to be ignored as it was already executed
15210 to create CURRENT_FILE for the main source holding also the command line
15211 definitions. On first met DW_MACINFO_start_file this flag is reset to
15212 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15213
15214 at_commandline = 1;
15215
15216 do
15217 {
15218 /* Do we at least have room for a macinfo type byte? */
15219 if (mac_ptr >= mac_end)
15220 {
15221 dwarf2_macros_too_long_complaint (section);
15222 break;
15223 }
15224
15225 macinfo_type = read_1_byte (abfd, mac_ptr);
15226 mac_ptr++;
15227
15228 /* Note that we rely on the fact that the corresponding GNU and
15229 DWARF constants are the same. */
15230 switch (macinfo_type)
15231 {
15232 /* A zero macinfo type indicates the end of the macro
15233 information. */
15234 case 0:
15235 break;
15236
15237 case DW_MACRO_GNU_define:
15238 case DW_MACRO_GNU_undef:
15239 case DW_MACRO_GNU_define_indirect:
15240 case DW_MACRO_GNU_undef_indirect:
15241 {
15242 unsigned int bytes_read;
15243 int line;
15244 char *body;
15245 int is_define;
15246
15247 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15248 mac_ptr += bytes_read;
15249
15250 if (macinfo_type == DW_MACRO_GNU_define
15251 || macinfo_type == DW_MACRO_GNU_undef)
15252 {
15253 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15254 mac_ptr += bytes_read;
15255 }
15256 else
15257 {
15258 LONGEST str_offset;
15259
15260 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15261 mac_ptr += offset_size;
15262
15263 body = read_indirect_string_at_offset (abfd, str_offset);
15264 }
15265
15266 is_define = (macinfo_type == DW_MACRO_GNU_define
15267 || macinfo_type == DW_MACRO_GNU_define_indirect);
15268 if (! current_file)
15269 {
15270 /* DWARF violation as no main source is present. */
15271 complaint (&symfile_complaints,
15272 _("debug info with no main source gives macro %s "
15273 "on line %d: %s"),
15274 is_define ? _("definition") : _("undefinition"),
15275 line, body);
15276 break;
15277 }
15278 if ((line == 0 && !at_commandline)
15279 || (line != 0 && at_commandline))
15280 complaint (&symfile_complaints,
15281 _("debug info gives %s macro %s with %s line %d: %s"),
15282 at_commandline ? _("command-line") : _("in-file"),
15283 is_define ? _("definition") : _("undefinition"),
15284 line == 0 ? _("zero") : _("non-zero"), line, body);
15285
15286 if (is_define)
15287 parse_macro_definition (current_file, line, body);
15288 else
15289 {
15290 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15291 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15292 macro_undef (current_file, line, body);
15293 }
15294 }
15295 break;
15296
15297 case DW_MACRO_GNU_start_file:
15298 {
15299 unsigned int bytes_read;
15300 int line, file;
15301
15302 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15303 mac_ptr += bytes_read;
15304 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15305 mac_ptr += bytes_read;
15306
15307 if ((line == 0 && !at_commandline)
15308 || (line != 0 && at_commandline))
15309 complaint (&symfile_complaints,
15310 _("debug info gives source %d included "
15311 "from %s at %s line %d"),
15312 file, at_commandline ? _("command-line") : _("file"),
15313 line == 0 ? _("zero") : _("non-zero"), line);
15314
15315 if (at_commandline)
15316 {
15317 /* This DW_MACRO_GNU_start_file was executed in the
15318 pass one. */
15319 at_commandline = 0;
15320 }
15321 else
15322 current_file = macro_start_file (file, line,
15323 current_file, comp_dir,
15324 lh, objfile);
15325 }
15326 break;
15327
15328 case DW_MACRO_GNU_end_file:
15329 if (! current_file)
15330 complaint (&symfile_complaints,
15331 _("macro debug info has an unmatched "
15332 "`close_file' directive"));
15333 else
15334 {
15335 current_file = current_file->included_by;
15336 if (! current_file)
15337 {
15338 enum dwarf_macro_record_type next_type;
15339
15340 /* GCC circa March 2002 doesn't produce the zero
15341 type byte marking the end of the compilation
15342 unit. Complain if it's not there, but exit no
15343 matter what. */
15344
15345 /* Do we at least have room for a macinfo type byte? */
15346 if (mac_ptr >= mac_end)
15347 {
15348 dwarf2_macros_too_long_complaint (section);
15349 return;
15350 }
15351
15352 /* We don't increment mac_ptr here, so this is just
15353 a look-ahead. */
15354 next_type = read_1_byte (abfd, mac_ptr);
15355 if (next_type != 0)
15356 complaint (&symfile_complaints,
15357 _("no terminating 0-type entry for "
15358 "macros in `.debug_macinfo' section"));
15359
15360 return;
15361 }
15362 }
15363 break;
15364
15365 case DW_MACRO_GNU_transparent_include:
15366 {
15367 LONGEST offset;
15368
15369 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15370 mac_ptr += offset_size;
15371
15372 dwarf_decode_macro_bytes (abfd,
15373 section->buffer + offset,
15374 mac_end, current_file,
15375 lh, comp_dir,
15376 section, section_is_gnu,
15377 offset_size, objfile);
15378 }
15379 break;
15380
15381 case DW_MACINFO_vendor_ext:
15382 if (!section_is_gnu)
15383 {
15384 unsigned int bytes_read;
15385 int constant;
15386
15387 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15388 mac_ptr += bytes_read;
15389 read_direct_string (abfd, mac_ptr, &bytes_read);
15390 mac_ptr += bytes_read;
15391
15392 /* We don't recognize any vendor extensions. */
15393 break;
15394 }
15395 /* FALLTHROUGH */
15396
15397 default:
15398 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15399 mac_ptr, abfd, offset_size,
15400 section);
15401 if (mac_ptr == NULL)
15402 return;
15403 break;
15404 }
15405 } while (macinfo_type != 0);
15406 }
15407
15408 static void
15409 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15410 char *comp_dir, bfd *abfd,
15411 struct dwarf2_cu *cu,
15412 struct dwarf2_section_info *section,
15413 int section_is_gnu)
15414 {
15415 struct objfile *objfile = dwarf2_per_objfile->objfile;
15416 gdb_byte *mac_ptr, *mac_end;
15417 struct macro_source_file *current_file = 0;
15418 enum dwarf_macro_record_type macinfo_type;
15419 unsigned int offset_size = cu->header.offset_size;
15420 gdb_byte *opcode_definitions[256];
15421
15422 dwarf2_read_section (objfile, section);
15423 if (section->buffer == NULL)
15424 {
15425 complaint (&symfile_complaints, _("missing %s section"),
15426 section->asection->name);
15427 return;
15428 }
15429
15430 /* First pass: Find the name of the base filename.
15431 This filename is needed in order to process all macros whose definition
15432 (or undefinition) comes from the command line. These macros are defined
15433 before the first DW_MACINFO_start_file entry, and yet still need to be
15434 associated to the base file.
15435
15436 To determine the base file name, we scan the macro definitions until we
15437 reach the first DW_MACINFO_start_file entry. We then initialize
15438 CURRENT_FILE accordingly so that any macro definition found before the
15439 first DW_MACINFO_start_file can still be associated to the base file. */
15440
15441 mac_ptr = section->buffer + offset;
15442 mac_end = section->buffer + section->size;
15443
15444 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15445 &offset_size, section_is_gnu);
15446 if (mac_ptr == NULL)
15447 {
15448 /* We already issued a complaint. */
15449 return;
15450 }
15451
15452 do
15453 {
15454 /* Do we at least have room for a macinfo type byte? */
15455 if (mac_ptr >= mac_end)
15456 {
15457 /* Complaint is printed during the second pass as GDB will probably
15458 stop the first pass earlier upon finding
15459 DW_MACINFO_start_file. */
15460 break;
15461 }
15462
15463 macinfo_type = read_1_byte (abfd, mac_ptr);
15464 mac_ptr++;
15465
15466 /* Note that we rely on the fact that the corresponding GNU and
15467 DWARF constants are the same. */
15468 switch (macinfo_type)
15469 {
15470 /* A zero macinfo type indicates the end of the macro
15471 information. */
15472 case 0:
15473 break;
15474
15475 case DW_MACRO_GNU_define:
15476 case DW_MACRO_GNU_undef:
15477 /* Only skip the data by MAC_PTR. */
15478 {
15479 unsigned int bytes_read;
15480
15481 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15482 mac_ptr += bytes_read;
15483 read_direct_string (abfd, mac_ptr, &bytes_read);
15484 mac_ptr += bytes_read;
15485 }
15486 break;
15487
15488 case DW_MACRO_GNU_start_file:
15489 {
15490 unsigned int bytes_read;
15491 int line, file;
15492
15493 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15494 mac_ptr += bytes_read;
15495 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15496 mac_ptr += bytes_read;
15497
15498 current_file = macro_start_file (file, line, current_file,
15499 comp_dir, lh, objfile);
15500 }
15501 break;
15502
15503 case DW_MACRO_GNU_end_file:
15504 /* No data to skip by MAC_PTR. */
15505 break;
15506
15507 case DW_MACRO_GNU_define_indirect:
15508 case DW_MACRO_GNU_undef_indirect:
15509 {
15510 unsigned int bytes_read;
15511
15512 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15513 mac_ptr += bytes_read;
15514 mac_ptr += offset_size;
15515 }
15516 break;
15517
15518 case DW_MACRO_GNU_transparent_include:
15519 /* Note that, according to the spec, a transparent include
15520 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15521 skip this opcode. */
15522 mac_ptr += offset_size;
15523 break;
15524
15525 case DW_MACINFO_vendor_ext:
15526 /* Only skip the data by MAC_PTR. */
15527 if (!section_is_gnu)
15528 {
15529 unsigned int bytes_read;
15530
15531 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15532 mac_ptr += bytes_read;
15533 read_direct_string (abfd, mac_ptr, &bytes_read);
15534 mac_ptr += bytes_read;
15535 }
15536 /* FALLTHROUGH */
15537
15538 default:
15539 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15540 mac_ptr, abfd, offset_size,
15541 section);
15542 if (mac_ptr == NULL)
15543 return;
15544 break;
15545 }
15546 } while (macinfo_type != 0 && current_file == NULL);
15547
15548 /* Second pass: Process all entries.
15549
15550 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15551 command-line macro definitions/undefinitions. This flag is unset when we
15552 reach the first DW_MACINFO_start_file entry. */
15553
15554 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15555 current_file, lh, comp_dir, section, section_is_gnu,
15556 offset_size, objfile);
15557 }
15558
15559 /* Check if the attribute's form is a DW_FORM_block*
15560 if so return true else false. */
15561 static int
15562 attr_form_is_block (struct attribute *attr)
15563 {
15564 return (attr == NULL ? 0 :
15565 attr->form == DW_FORM_block1
15566 || attr->form == DW_FORM_block2
15567 || attr->form == DW_FORM_block4
15568 || attr->form == DW_FORM_block
15569 || attr->form == DW_FORM_exprloc);
15570 }
15571
15572 /* Return non-zero if ATTR's value is a section offset --- classes
15573 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15574 You may use DW_UNSND (attr) to retrieve such offsets.
15575
15576 Section 7.5.4, "Attribute Encodings", explains that no attribute
15577 may have a value that belongs to more than one of these classes; it
15578 would be ambiguous if we did, because we use the same forms for all
15579 of them. */
15580 static int
15581 attr_form_is_section_offset (struct attribute *attr)
15582 {
15583 return (attr->form == DW_FORM_data4
15584 || attr->form == DW_FORM_data8
15585 || attr->form == DW_FORM_sec_offset);
15586 }
15587
15588
15589 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15590 zero otherwise. When this function returns true, you can apply
15591 dwarf2_get_attr_constant_value to it.
15592
15593 However, note that for some attributes you must check
15594 attr_form_is_section_offset before using this test. DW_FORM_data4
15595 and DW_FORM_data8 are members of both the constant class, and of
15596 the classes that contain offsets into other debug sections
15597 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15598 that, if an attribute's can be either a constant or one of the
15599 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15600 taken as section offsets, not constants. */
15601 static int
15602 attr_form_is_constant (struct attribute *attr)
15603 {
15604 switch (attr->form)
15605 {
15606 case DW_FORM_sdata:
15607 case DW_FORM_udata:
15608 case DW_FORM_data1:
15609 case DW_FORM_data2:
15610 case DW_FORM_data4:
15611 case DW_FORM_data8:
15612 return 1;
15613 default:
15614 return 0;
15615 }
15616 }
15617
15618 /* A helper function that fills in a dwarf2_loclist_baton. */
15619
15620 static void
15621 fill_in_loclist_baton (struct dwarf2_cu *cu,
15622 struct dwarf2_loclist_baton *baton,
15623 struct attribute *attr)
15624 {
15625 dwarf2_read_section (dwarf2_per_objfile->objfile,
15626 &dwarf2_per_objfile->loc);
15627
15628 baton->per_cu = cu->per_cu;
15629 gdb_assert (baton->per_cu);
15630 /* We don't know how long the location list is, but make sure we
15631 don't run off the edge of the section. */
15632 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15633 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15634 baton->base_address = cu->base_address;
15635 }
15636
15637 static void
15638 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15639 struct dwarf2_cu *cu)
15640 {
15641 struct objfile *objfile = dwarf2_per_objfile->objfile;
15642
15643 if (attr_form_is_section_offset (attr)
15644 /* ".debug_loc" may not exist at all, or the offset may be outside
15645 the section. If so, fall through to the complaint in the
15646 other branch. */
15647 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15648 &dwarf2_per_objfile->loc))
15649 {
15650 struct dwarf2_loclist_baton *baton;
15651
15652 baton = obstack_alloc (&objfile->objfile_obstack,
15653 sizeof (struct dwarf2_loclist_baton));
15654
15655 fill_in_loclist_baton (cu, baton, attr);
15656
15657 if (cu->base_known == 0)
15658 complaint (&symfile_complaints,
15659 _("Location list used without "
15660 "specifying the CU base address."));
15661
15662 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15663 SYMBOL_LOCATION_BATON (sym) = baton;
15664 }
15665 else
15666 {
15667 struct dwarf2_locexpr_baton *baton;
15668
15669 baton = obstack_alloc (&objfile->objfile_obstack,
15670 sizeof (struct dwarf2_locexpr_baton));
15671 baton->per_cu = cu->per_cu;
15672 gdb_assert (baton->per_cu);
15673
15674 if (attr_form_is_block (attr))
15675 {
15676 /* Note that we're just copying the block's data pointer
15677 here, not the actual data. We're still pointing into the
15678 info_buffer for SYM's objfile; right now we never release
15679 that buffer, but when we do clean up properly this may
15680 need to change. */
15681 baton->size = DW_BLOCK (attr)->size;
15682 baton->data = DW_BLOCK (attr)->data;
15683 }
15684 else
15685 {
15686 dwarf2_invalid_attrib_class_complaint ("location description",
15687 SYMBOL_NATURAL_NAME (sym));
15688 baton->size = 0;
15689 }
15690
15691 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15692 SYMBOL_LOCATION_BATON (sym) = baton;
15693 }
15694 }
15695
15696 /* Return the OBJFILE associated with the compilation unit CU. If CU
15697 came from a separate debuginfo file, then the master objfile is
15698 returned. */
15699
15700 struct objfile *
15701 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15702 {
15703 struct objfile *objfile = per_cu->objfile;
15704
15705 /* Return the master objfile, so that we can report and look up the
15706 correct file containing this variable. */
15707 if (objfile->separate_debug_objfile_backlink)
15708 objfile = objfile->separate_debug_objfile_backlink;
15709
15710 return objfile;
15711 }
15712
15713 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15714 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15715 CU_HEADERP first. */
15716
15717 static const struct comp_unit_head *
15718 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15719 struct dwarf2_per_cu_data *per_cu)
15720 {
15721 struct objfile *objfile;
15722 struct dwarf2_per_objfile *per_objfile;
15723 gdb_byte *info_ptr;
15724
15725 if (per_cu->cu)
15726 return &per_cu->cu->header;
15727
15728 objfile = per_cu->objfile;
15729 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15730 info_ptr = per_objfile->info.buffer + per_cu->offset;
15731
15732 memset (cu_headerp, 0, sizeof (*cu_headerp));
15733 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15734
15735 return cu_headerp;
15736 }
15737
15738 /* Return the address size given in the compilation unit header for CU. */
15739
15740 CORE_ADDR
15741 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15742 {
15743 struct comp_unit_head cu_header_local;
15744 const struct comp_unit_head *cu_headerp;
15745
15746 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15747
15748 return cu_headerp->addr_size;
15749 }
15750
15751 /* Return the offset size given in the compilation unit header for CU. */
15752
15753 int
15754 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15755 {
15756 struct comp_unit_head cu_header_local;
15757 const struct comp_unit_head *cu_headerp;
15758
15759 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15760
15761 return cu_headerp->offset_size;
15762 }
15763
15764 /* See its dwarf2loc.h declaration. */
15765
15766 int
15767 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15768 {
15769 struct comp_unit_head cu_header_local;
15770 const struct comp_unit_head *cu_headerp;
15771
15772 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15773
15774 if (cu_headerp->version == 2)
15775 return cu_headerp->addr_size;
15776 else
15777 return cu_headerp->offset_size;
15778 }
15779
15780 /* Return the text offset of the CU. The returned offset comes from
15781 this CU's objfile. If this objfile came from a separate debuginfo
15782 file, then the offset may be different from the corresponding
15783 offset in the parent objfile. */
15784
15785 CORE_ADDR
15786 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15787 {
15788 struct objfile *objfile = per_cu->objfile;
15789
15790 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15791 }
15792
15793 /* Locate the .debug_info compilation unit from CU's objfile which contains
15794 the DIE at OFFSET. Raises an error on failure. */
15795
15796 static struct dwarf2_per_cu_data *
15797 dwarf2_find_containing_comp_unit (unsigned int offset,
15798 struct objfile *objfile)
15799 {
15800 struct dwarf2_per_cu_data *this_cu;
15801 int low, high;
15802
15803 low = 0;
15804 high = dwarf2_per_objfile->n_comp_units - 1;
15805 while (high > low)
15806 {
15807 int mid = low + (high - low) / 2;
15808
15809 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15810 high = mid;
15811 else
15812 low = mid + 1;
15813 }
15814 gdb_assert (low == high);
15815 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15816 {
15817 if (low == 0)
15818 error (_("Dwarf Error: could not find partial DIE containing "
15819 "offset 0x%lx [in module %s]"),
15820 (long) offset, bfd_get_filename (objfile->obfd));
15821
15822 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15823 return dwarf2_per_objfile->all_comp_units[low-1];
15824 }
15825 else
15826 {
15827 this_cu = dwarf2_per_objfile->all_comp_units[low];
15828 if (low == dwarf2_per_objfile->n_comp_units - 1
15829 && offset >= this_cu->offset + this_cu->length)
15830 error (_("invalid dwarf2 offset %u"), offset);
15831 gdb_assert (offset < this_cu->offset + this_cu->length);
15832 return this_cu;
15833 }
15834 }
15835
15836 /* Locate the compilation unit from OBJFILE which is located at exactly
15837 OFFSET. Raises an error on failure. */
15838
15839 static struct dwarf2_per_cu_data *
15840 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15841 {
15842 struct dwarf2_per_cu_data *this_cu;
15843
15844 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15845 if (this_cu->offset != offset)
15846 error (_("no compilation unit with offset %u."), offset);
15847 return this_cu;
15848 }
15849
15850 /* Initialize dwarf2_cu CU, owned by PER_CU. */
15851
15852 static void
15853 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15854 {
15855 memset (cu, 0, sizeof (*cu));
15856 per_cu->cu = cu;
15857 cu->per_cu = per_cu;
15858 cu->objfile = per_cu->objfile;
15859 obstack_init (&cu->comp_unit_obstack);
15860 }
15861
15862 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15863
15864 static void
15865 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15866 {
15867 struct attribute *attr;
15868
15869 /* Set the language we're debugging. */
15870 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15871 if (attr)
15872 set_cu_language (DW_UNSND (attr), cu);
15873 else
15874 {
15875 cu->language = language_minimal;
15876 cu->language_defn = language_def (cu->language);
15877 }
15878 }
15879
15880 /* Release one cached compilation unit, CU. We unlink it from the tree
15881 of compilation units, but we don't remove it from the read_in_chain;
15882 the caller is responsible for that.
15883 NOTE: DATA is a void * because this function is also used as a
15884 cleanup routine. */
15885
15886 static void
15887 free_heap_comp_unit (void *data)
15888 {
15889 struct dwarf2_cu *cu = data;
15890
15891 gdb_assert (cu->per_cu != NULL);
15892 cu->per_cu->cu = NULL;
15893 cu->per_cu = NULL;
15894
15895 obstack_free (&cu->comp_unit_obstack, NULL);
15896
15897 xfree (cu);
15898 }
15899
15900 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15901 when we're finished with it. We can't free the pointer itself, but be
15902 sure to unlink it from the cache. Also release any associated storage
15903 and perform cache maintenance.
15904
15905 Only used during partial symbol parsing. */
15906
15907 static void
15908 free_stack_comp_unit (void *data)
15909 {
15910 struct dwarf2_cu *cu = data;
15911
15912 gdb_assert (cu->per_cu != NULL);
15913 cu->per_cu->cu = NULL;
15914 cu->per_cu = NULL;
15915
15916 obstack_free (&cu->comp_unit_obstack, NULL);
15917 cu->partial_dies = NULL;
15918
15919 /* The previous code only did this if per_cu != NULL.
15920 But that would always succeed, so now we just unconditionally do
15921 the aging. This seems like the wrong place to do such aging,
15922 but cleaning that up is left for later. */
15923 age_cached_comp_units ();
15924 }
15925
15926 /* Free all cached compilation units. */
15927
15928 static void
15929 free_cached_comp_units (void *data)
15930 {
15931 struct dwarf2_per_cu_data *per_cu, **last_chain;
15932
15933 per_cu = dwarf2_per_objfile->read_in_chain;
15934 last_chain = &dwarf2_per_objfile->read_in_chain;
15935 while (per_cu != NULL)
15936 {
15937 struct dwarf2_per_cu_data *next_cu;
15938
15939 next_cu = per_cu->cu->read_in_chain;
15940
15941 free_heap_comp_unit (per_cu->cu);
15942 *last_chain = next_cu;
15943
15944 per_cu = next_cu;
15945 }
15946 }
15947
15948 /* Increase the age counter on each cached compilation unit, and free
15949 any that are too old. */
15950
15951 static void
15952 age_cached_comp_units (void)
15953 {
15954 struct dwarf2_per_cu_data *per_cu, **last_chain;
15955
15956 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15957 per_cu = dwarf2_per_objfile->read_in_chain;
15958 while (per_cu != NULL)
15959 {
15960 per_cu->cu->last_used ++;
15961 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15962 dwarf2_mark (per_cu->cu);
15963 per_cu = per_cu->cu->read_in_chain;
15964 }
15965
15966 per_cu = dwarf2_per_objfile->read_in_chain;
15967 last_chain = &dwarf2_per_objfile->read_in_chain;
15968 while (per_cu != NULL)
15969 {
15970 struct dwarf2_per_cu_data *next_cu;
15971
15972 next_cu = per_cu->cu->read_in_chain;
15973
15974 if (!per_cu->cu->mark)
15975 {
15976 free_heap_comp_unit (per_cu->cu);
15977 *last_chain = next_cu;
15978 }
15979 else
15980 last_chain = &per_cu->cu->read_in_chain;
15981
15982 per_cu = next_cu;
15983 }
15984 }
15985
15986 /* Remove a single compilation unit from the cache. */
15987
15988 static void
15989 free_one_cached_comp_unit (void *target_cu)
15990 {
15991 struct dwarf2_per_cu_data *per_cu, **last_chain;
15992
15993 per_cu = dwarf2_per_objfile->read_in_chain;
15994 last_chain = &dwarf2_per_objfile->read_in_chain;
15995 while (per_cu != NULL)
15996 {
15997 struct dwarf2_per_cu_data *next_cu;
15998
15999 next_cu = per_cu->cu->read_in_chain;
16000
16001 if (per_cu->cu == target_cu)
16002 {
16003 free_heap_comp_unit (per_cu->cu);
16004 *last_chain = next_cu;
16005 break;
16006 }
16007 else
16008 last_chain = &per_cu->cu->read_in_chain;
16009
16010 per_cu = next_cu;
16011 }
16012 }
16013
16014 /* Release all extra memory associated with OBJFILE. */
16015
16016 void
16017 dwarf2_free_objfile (struct objfile *objfile)
16018 {
16019 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16020
16021 if (dwarf2_per_objfile == NULL)
16022 return;
16023
16024 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16025 free_cached_comp_units (NULL);
16026
16027 if (dwarf2_per_objfile->quick_file_names_table)
16028 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16029
16030 /* Everything else should be on the objfile obstack. */
16031 }
16032
16033 /* A pair of DIE offset and GDB type pointer. We store these
16034 in a hash table separate from the DIEs, and preserve them
16035 when the DIEs are flushed out of cache. */
16036
16037 struct dwarf2_offset_and_type
16038 {
16039 unsigned int offset;
16040 struct type *type;
16041 };
16042
16043 /* Hash function for a dwarf2_offset_and_type. */
16044
16045 static hashval_t
16046 offset_and_type_hash (const void *item)
16047 {
16048 const struct dwarf2_offset_and_type *ofs = item;
16049
16050 return ofs->offset;
16051 }
16052
16053 /* Equality function for a dwarf2_offset_and_type. */
16054
16055 static int
16056 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16057 {
16058 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16059 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16060
16061 return ofs_lhs->offset == ofs_rhs->offset;
16062 }
16063
16064 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16065 table if necessary. For convenience, return TYPE.
16066
16067 The DIEs reading must have careful ordering to:
16068 * Not cause infite loops trying to read in DIEs as a prerequisite for
16069 reading current DIE.
16070 * Not trying to dereference contents of still incompletely read in types
16071 while reading in other DIEs.
16072 * Enable referencing still incompletely read in types just by a pointer to
16073 the type without accessing its fields.
16074
16075 Therefore caller should follow these rules:
16076 * Try to fetch any prerequisite types we may need to build this DIE type
16077 before building the type and calling set_die_type.
16078 * After building type call set_die_type for current DIE as soon as
16079 possible before fetching more types to complete the current type.
16080 * Make the type as complete as possible before fetching more types. */
16081
16082 static struct type *
16083 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16084 {
16085 struct dwarf2_offset_and_type **slot, ofs;
16086 struct objfile *objfile = cu->objfile;
16087 htab_t *type_hash_ptr;
16088
16089 /* For Ada types, make sure that the gnat-specific data is always
16090 initialized (if not already set). There are a few types where
16091 we should not be doing so, because the type-specific area is
16092 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16093 where the type-specific area is used to store the floatformat).
16094 But this is not a problem, because the gnat-specific information
16095 is actually not needed for these types. */
16096 if (need_gnat_info (cu)
16097 && TYPE_CODE (type) != TYPE_CODE_FUNC
16098 && TYPE_CODE (type) != TYPE_CODE_FLT
16099 && !HAVE_GNAT_AUX_INFO (type))
16100 INIT_GNAT_SPECIFIC (type);
16101
16102 if (cu->per_cu->debug_types_section)
16103 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16104 else
16105 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16106
16107 if (*type_hash_ptr == NULL)
16108 {
16109 *type_hash_ptr
16110 = htab_create_alloc_ex (127,
16111 offset_and_type_hash,
16112 offset_and_type_eq,
16113 NULL,
16114 &objfile->objfile_obstack,
16115 hashtab_obstack_allocate,
16116 dummy_obstack_deallocate);
16117 }
16118
16119 ofs.offset = die->offset;
16120 ofs.type = type;
16121 slot = (struct dwarf2_offset_and_type **)
16122 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16123 if (*slot)
16124 complaint (&symfile_complaints,
16125 _("A problem internal to GDB: DIE 0x%x has type already set"),
16126 die->offset);
16127 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16128 **slot = ofs;
16129 return type;
16130 }
16131
16132 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16133 table, or return NULL if the die does not have a saved type. */
16134
16135 static struct type *
16136 get_die_type_at_offset (unsigned int offset,
16137 struct dwarf2_per_cu_data *per_cu)
16138 {
16139 struct dwarf2_offset_and_type *slot, ofs;
16140 htab_t type_hash;
16141
16142 if (per_cu->debug_types_section)
16143 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16144 else
16145 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16146 if (type_hash == NULL)
16147 return NULL;
16148
16149 ofs.offset = offset;
16150 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16151 if (slot)
16152 return slot->type;
16153 else
16154 return NULL;
16155 }
16156
16157 /* Look up the type for DIE in the appropriate type_hash table,
16158 or return NULL if DIE does not have a saved type. */
16159
16160 static struct type *
16161 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16162 {
16163 return get_die_type_at_offset (die->offset, cu->per_cu);
16164 }
16165
16166 /* Add a dependence relationship from CU to REF_PER_CU. */
16167
16168 static void
16169 dwarf2_add_dependence (struct dwarf2_cu *cu,
16170 struct dwarf2_per_cu_data *ref_per_cu)
16171 {
16172 void **slot;
16173
16174 if (cu->dependencies == NULL)
16175 cu->dependencies
16176 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16177 NULL, &cu->comp_unit_obstack,
16178 hashtab_obstack_allocate,
16179 dummy_obstack_deallocate);
16180
16181 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16182 if (*slot == NULL)
16183 *slot = ref_per_cu;
16184 }
16185
16186 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16187 Set the mark field in every compilation unit in the
16188 cache that we must keep because we are keeping CU. */
16189
16190 static int
16191 dwarf2_mark_helper (void **slot, void *data)
16192 {
16193 struct dwarf2_per_cu_data *per_cu;
16194
16195 per_cu = (struct dwarf2_per_cu_data *) *slot;
16196
16197 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16198 reading of the chain. As such dependencies remain valid it is not much
16199 useful to track and undo them during QUIT cleanups. */
16200 if (per_cu->cu == NULL)
16201 return 1;
16202
16203 if (per_cu->cu->mark)
16204 return 1;
16205 per_cu->cu->mark = 1;
16206
16207 if (per_cu->cu->dependencies != NULL)
16208 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16209
16210 return 1;
16211 }
16212
16213 /* Set the mark field in CU and in every other compilation unit in the
16214 cache that we must keep because we are keeping CU. */
16215
16216 static void
16217 dwarf2_mark (struct dwarf2_cu *cu)
16218 {
16219 if (cu->mark)
16220 return;
16221 cu->mark = 1;
16222 if (cu->dependencies != NULL)
16223 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16224 }
16225
16226 static void
16227 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16228 {
16229 while (per_cu)
16230 {
16231 per_cu->cu->mark = 0;
16232 per_cu = per_cu->cu->read_in_chain;
16233 }
16234 }
16235
16236 /* Trivial hash function for partial_die_info: the hash value of a DIE
16237 is its offset in .debug_info for this objfile. */
16238
16239 static hashval_t
16240 partial_die_hash (const void *item)
16241 {
16242 const struct partial_die_info *part_die = item;
16243
16244 return part_die->offset;
16245 }
16246
16247 /* Trivial comparison function for partial_die_info structures: two DIEs
16248 are equal if they have the same offset. */
16249
16250 static int
16251 partial_die_eq (const void *item_lhs, const void *item_rhs)
16252 {
16253 const struct partial_die_info *part_die_lhs = item_lhs;
16254 const struct partial_die_info *part_die_rhs = item_rhs;
16255
16256 return part_die_lhs->offset == part_die_rhs->offset;
16257 }
16258
16259 static struct cmd_list_element *set_dwarf2_cmdlist;
16260 static struct cmd_list_element *show_dwarf2_cmdlist;
16261
16262 static void
16263 set_dwarf2_cmd (char *args, int from_tty)
16264 {
16265 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16266 }
16267
16268 static void
16269 show_dwarf2_cmd (char *args, int from_tty)
16270 {
16271 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16272 }
16273
16274 /* If section described by INFO was mmapped, munmap it now. */
16275
16276 static void
16277 munmap_section_buffer (struct dwarf2_section_info *info)
16278 {
16279 if (info->map_addr != NULL)
16280 {
16281 #ifdef HAVE_MMAP
16282 int res;
16283
16284 res = munmap (info->map_addr, info->map_len);
16285 gdb_assert (res == 0);
16286 #else
16287 /* Without HAVE_MMAP, we should never be here to begin with. */
16288 gdb_assert_not_reached ("no mmap support");
16289 #endif
16290 }
16291 }
16292
16293 /* munmap debug sections for OBJFILE, if necessary. */
16294
16295 static void
16296 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16297 {
16298 struct dwarf2_per_objfile *data = d;
16299 int ix;
16300 struct dwarf2_section_info *section;
16301
16302 /* This is sorted according to the order they're defined in to make it easier
16303 to keep in sync. */
16304 munmap_section_buffer (&data->info);
16305 munmap_section_buffer (&data->abbrev);
16306 munmap_section_buffer (&data->line);
16307 munmap_section_buffer (&data->loc);
16308 munmap_section_buffer (&data->macinfo);
16309 munmap_section_buffer (&data->macro);
16310 munmap_section_buffer (&data->str);
16311 munmap_section_buffer (&data->ranges);
16312 munmap_section_buffer (&data->frame);
16313 munmap_section_buffer (&data->eh_frame);
16314 munmap_section_buffer (&data->gdb_index);
16315
16316 for (ix = 0;
16317 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16318 ++ix)
16319 munmap_section_buffer (section);
16320
16321 VEC_free (dwarf2_section_info_def, data->types);
16322 }
16323
16324 \f
16325 /* The "save gdb-index" command. */
16326
16327 /* The contents of the hash table we create when building the string
16328 table. */
16329 struct strtab_entry
16330 {
16331 offset_type offset;
16332 const char *str;
16333 };
16334
16335 /* Hash function for a strtab_entry.
16336
16337 Function is used only during write_hash_table so no index format backward
16338 compatibility is needed. */
16339
16340 static hashval_t
16341 hash_strtab_entry (const void *e)
16342 {
16343 const struct strtab_entry *entry = e;
16344 return mapped_index_string_hash (INT_MAX, entry->str);
16345 }
16346
16347 /* Equality function for a strtab_entry. */
16348
16349 static int
16350 eq_strtab_entry (const void *a, const void *b)
16351 {
16352 const struct strtab_entry *ea = a;
16353 const struct strtab_entry *eb = b;
16354 return !strcmp (ea->str, eb->str);
16355 }
16356
16357 /* Create a strtab_entry hash table. */
16358
16359 static htab_t
16360 create_strtab (void)
16361 {
16362 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16363 xfree, xcalloc, xfree);
16364 }
16365
16366 /* Add a string to the constant pool. Return the string's offset in
16367 host order. */
16368
16369 static offset_type
16370 add_string (htab_t table, struct obstack *cpool, const char *str)
16371 {
16372 void **slot;
16373 struct strtab_entry entry;
16374 struct strtab_entry *result;
16375
16376 entry.str = str;
16377 slot = htab_find_slot (table, &entry, INSERT);
16378 if (*slot)
16379 result = *slot;
16380 else
16381 {
16382 result = XNEW (struct strtab_entry);
16383 result->offset = obstack_object_size (cpool);
16384 result->str = str;
16385 obstack_grow_str0 (cpool, str);
16386 *slot = result;
16387 }
16388 return result->offset;
16389 }
16390
16391 /* An entry in the symbol table. */
16392 struct symtab_index_entry
16393 {
16394 /* The name of the symbol. */
16395 const char *name;
16396 /* The offset of the name in the constant pool. */
16397 offset_type index_offset;
16398 /* A sorted vector of the indices of all the CUs that hold an object
16399 of this name. */
16400 VEC (offset_type) *cu_indices;
16401 };
16402
16403 /* The symbol table. This is a power-of-2-sized hash table. */
16404 struct mapped_symtab
16405 {
16406 offset_type n_elements;
16407 offset_type size;
16408 struct symtab_index_entry **data;
16409 };
16410
16411 /* Hash function for a symtab_index_entry. */
16412
16413 static hashval_t
16414 hash_symtab_entry (const void *e)
16415 {
16416 const struct symtab_index_entry *entry = e;
16417 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16418 sizeof (offset_type) * VEC_length (offset_type,
16419 entry->cu_indices),
16420 0);
16421 }
16422
16423 /* Equality function for a symtab_index_entry. */
16424
16425 static int
16426 eq_symtab_entry (const void *a, const void *b)
16427 {
16428 const struct symtab_index_entry *ea = a;
16429 const struct symtab_index_entry *eb = b;
16430 int len = VEC_length (offset_type, ea->cu_indices);
16431 if (len != VEC_length (offset_type, eb->cu_indices))
16432 return 0;
16433 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16434 VEC_address (offset_type, eb->cu_indices),
16435 sizeof (offset_type) * len);
16436 }
16437
16438 /* Destroy a symtab_index_entry. */
16439
16440 static void
16441 delete_symtab_entry (void *p)
16442 {
16443 struct symtab_index_entry *entry = p;
16444 VEC_free (offset_type, entry->cu_indices);
16445 xfree (entry);
16446 }
16447
16448 /* Create a hash table holding symtab_index_entry objects. */
16449
16450 static htab_t
16451 create_symbol_hash_table (void)
16452 {
16453 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16454 delete_symtab_entry, xcalloc, xfree);
16455 }
16456
16457 /* Create a new mapped symtab object. */
16458
16459 static struct mapped_symtab *
16460 create_mapped_symtab (void)
16461 {
16462 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16463 symtab->n_elements = 0;
16464 symtab->size = 1024;
16465 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16466 return symtab;
16467 }
16468
16469 /* Destroy a mapped_symtab. */
16470
16471 static void
16472 cleanup_mapped_symtab (void *p)
16473 {
16474 struct mapped_symtab *symtab = p;
16475 /* The contents of the array are freed when the other hash table is
16476 destroyed. */
16477 xfree (symtab->data);
16478 xfree (symtab);
16479 }
16480
16481 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16482 the slot.
16483
16484 Function is used only during write_hash_table so no index format backward
16485 compatibility is needed. */
16486
16487 static struct symtab_index_entry **
16488 find_slot (struct mapped_symtab *symtab, const char *name)
16489 {
16490 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16491
16492 index = hash & (symtab->size - 1);
16493 step = ((hash * 17) & (symtab->size - 1)) | 1;
16494
16495 for (;;)
16496 {
16497 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16498 return &symtab->data[index];
16499 index = (index + step) & (symtab->size - 1);
16500 }
16501 }
16502
16503 /* Expand SYMTAB's hash table. */
16504
16505 static void
16506 hash_expand (struct mapped_symtab *symtab)
16507 {
16508 offset_type old_size = symtab->size;
16509 offset_type i;
16510 struct symtab_index_entry **old_entries = symtab->data;
16511
16512 symtab->size *= 2;
16513 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16514
16515 for (i = 0; i < old_size; ++i)
16516 {
16517 if (old_entries[i])
16518 {
16519 struct symtab_index_entry **slot = find_slot (symtab,
16520 old_entries[i]->name);
16521 *slot = old_entries[i];
16522 }
16523 }
16524
16525 xfree (old_entries);
16526 }
16527
16528 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16529 is the index of the CU in which the symbol appears. */
16530
16531 static void
16532 add_index_entry (struct mapped_symtab *symtab, const char *name,
16533 offset_type cu_index)
16534 {
16535 struct symtab_index_entry **slot;
16536
16537 ++symtab->n_elements;
16538 if (4 * symtab->n_elements / 3 >= symtab->size)
16539 hash_expand (symtab);
16540
16541 slot = find_slot (symtab, name);
16542 if (!*slot)
16543 {
16544 *slot = XNEW (struct symtab_index_entry);
16545 (*slot)->name = name;
16546 (*slot)->cu_indices = NULL;
16547 }
16548 /* Don't push an index twice. Due to how we add entries we only
16549 have to check the last one. */
16550 if (VEC_empty (offset_type, (*slot)->cu_indices)
16551 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16552 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16553 }
16554
16555 /* Add a vector of indices to the constant pool. */
16556
16557 static offset_type
16558 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16559 struct symtab_index_entry *entry)
16560 {
16561 void **slot;
16562
16563 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16564 if (!*slot)
16565 {
16566 offset_type len = VEC_length (offset_type, entry->cu_indices);
16567 offset_type val = MAYBE_SWAP (len);
16568 offset_type iter;
16569 int i;
16570
16571 *slot = entry;
16572 entry->index_offset = obstack_object_size (cpool);
16573
16574 obstack_grow (cpool, &val, sizeof (val));
16575 for (i = 0;
16576 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16577 ++i)
16578 {
16579 val = MAYBE_SWAP (iter);
16580 obstack_grow (cpool, &val, sizeof (val));
16581 }
16582 }
16583 else
16584 {
16585 struct symtab_index_entry *old_entry = *slot;
16586 entry->index_offset = old_entry->index_offset;
16587 entry = old_entry;
16588 }
16589 return entry->index_offset;
16590 }
16591
16592 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16593 constant pool entries going into the obstack CPOOL. */
16594
16595 static void
16596 write_hash_table (struct mapped_symtab *symtab,
16597 struct obstack *output, struct obstack *cpool)
16598 {
16599 offset_type i;
16600 htab_t symbol_hash_table;
16601 htab_t str_table;
16602
16603 symbol_hash_table = create_symbol_hash_table ();
16604 str_table = create_strtab ();
16605
16606 /* We add all the index vectors to the constant pool first, to
16607 ensure alignment is ok. */
16608 for (i = 0; i < symtab->size; ++i)
16609 {
16610 if (symtab->data[i])
16611 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16612 }
16613
16614 /* Now write out the hash table. */
16615 for (i = 0; i < symtab->size; ++i)
16616 {
16617 offset_type str_off, vec_off;
16618
16619 if (symtab->data[i])
16620 {
16621 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16622 vec_off = symtab->data[i]->index_offset;
16623 }
16624 else
16625 {
16626 /* While 0 is a valid constant pool index, it is not valid
16627 to have 0 for both offsets. */
16628 str_off = 0;
16629 vec_off = 0;
16630 }
16631
16632 str_off = MAYBE_SWAP (str_off);
16633 vec_off = MAYBE_SWAP (vec_off);
16634
16635 obstack_grow (output, &str_off, sizeof (str_off));
16636 obstack_grow (output, &vec_off, sizeof (vec_off));
16637 }
16638
16639 htab_delete (str_table);
16640 htab_delete (symbol_hash_table);
16641 }
16642
16643 /* Struct to map psymtab to CU index in the index file. */
16644 struct psymtab_cu_index_map
16645 {
16646 struct partial_symtab *psymtab;
16647 unsigned int cu_index;
16648 };
16649
16650 static hashval_t
16651 hash_psymtab_cu_index (const void *item)
16652 {
16653 const struct psymtab_cu_index_map *map = item;
16654
16655 return htab_hash_pointer (map->psymtab);
16656 }
16657
16658 static int
16659 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16660 {
16661 const struct psymtab_cu_index_map *lhs = item_lhs;
16662 const struct psymtab_cu_index_map *rhs = item_rhs;
16663
16664 return lhs->psymtab == rhs->psymtab;
16665 }
16666
16667 /* Helper struct for building the address table. */
16668 struct addrmap_index_data
16669 {
16670 struct objfile *objfile;
16671 struct obstack *addr_obstack;
16672 htab_t cu_index_htab;
16673
16674 /* Non-zero if the previous_* fields are valid.
16675 We can't write an entry until we see the next entry (since it is only then
16676 that we know the end of the entry). */
16677 int previous_valid;
16678 /* Index of the CU in the table of all CUs in the index file. */
16679 unsigned int previous_cu_index;
16680 /* Start address of the CU. */
16681 CORE_ADDR previous_cu_start;
16682 };
16683
16684 /* Write an address entry to OBSTACK. */
16685
16686 static void
16687 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16688 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16689 {
16690 offset_type cu_index_to_write;
16691 char addr[8];
16692 CORE_ADDR baseaddr;
16693
16694 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16695
16696 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16697 obstack_grow (obstack, addr, 8);
16698 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16699 obstack_grow (obstack, addr, 8);
16700 cu_index_to_write = MAYBE_SWAP (cu_index);
16701 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16702 }
16703
16704 /* Worker function for traversing an addrmap to build the address table. */
16705
16706 static int
16707 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16708 {
16709 struct addrmap_index_data *data = datap;
16710 struct partial_symtab *pst = obj;
16711 offset_type cu_index;
16712 void **slot;
16713
16714 if (data->previous_valid)
16715 add_address_entry (data->objfile, data->addr_obstack,
16716 data->previous_cu_start, start_addr,
16717 data->previous_cu_index);
16718
16719 data->previous_cu_start = start_addr;
16720 if (pst != NULL)
16721 {
16722 struct psymtab_cu_index_map find_map, *map;
16723 find_map.psymtab = pst;
16724 map = htab_find (data->cu_index_htab, &find_map);
16725 gdb_assert (map != NULL);
16726 data->previous_cu_index = map->cu_index;
16727 data->previous_valid = 1;
16728 }
16729 else
16730 data->previous_valid = 0;
16731
16732 return 0;
16733 }
16734
16735 /* Write OBJFILE's address map to OBSTACK.
16736 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16737 in the index file. */
16738
16739 static void
16740 write_address_map (struct objfile *objfile, struct obstack *obstack,
16741 htab_t cu_index_htab)
16742 {
16743 struct addrmap_index_data addrmap_index_data;
16744
16745 /* When writing the address table, we have to cope with the fact that
16746 the addrmap iterator only provides the start of a region; we have to
16747 wait until the next invocation to get the start of the next region. */
16748
16749 addrmap_index_data.objfile = objfile;
16750 addrmap_index_data.addr_obstack = obstack;
16751 addrmap_index_data.cu_index_htab = cu_index_htab;
16752 addrmap_index_data.previous_valid = 0;
16753
16754 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16755 &addrmap_index_data);
16756
16757 /* It's highly unlikely the last entry (end address = 0xff...ff)
16758 is valid, but we should still handle it.
16759 The end address is recorded as the start of the next region, but that
16760 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16761 anyway. */
16762 if (addrmap_index_data.previous_valid)
16763 add_address_entry (objfile, obstack,
16764 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16765 addrmap_index_data.previous_cu_index);
16766 }
16767
16768 /* Add a list of partial symbols to SYMTAB. */
16769
16770 static void
16771 write_psymbols (struct mapped_symtab *symtab,
16772 htab_t psyms_seen,
16773 struct partial_symbol **psymp,
16774 int count,
16775 offset_type cu_index,
16776 int is_static)
16777 {
16778 for (; count-- > 0; ++psymp)
16779 {
16780 void **slot, *lookup;
16781
16782 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16783 error (_("Ada is not currently supported by the index"));
16784
16785 /* We only want to add a given psymbol once. However, we also
16786 want to account for whether it is global or static. So, we
16787 may add it twice, using slightly different values. */
16788 if (is_static)
16789 {
16790 uintptr_t val = 1 | (uintptr_t) *psymp;
16791
16792 lookup = (void *) val;
16793 }
16794 else
16795 lookup = *psymp;
16796
16797 /* Only add a given psymbol once. */
16798 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16799 if (!*slot)
16800 {
16801 *slot = lookup;
16802 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16803 }
16804 }
16805 }
16806
16807 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16808 exception if there is an error. */
16809
16810 static void
16811 write_obstack (FILE *file, struct obstack *obstack)
16812 {
16813 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16814 file)
16815 != obstack_object_size (obstack))
16816 error (_("couldn't data write to file"));
16817 }
16818
16819 /* Unlink a file if the argument is not NULL. */
16820
16821 static void
16822 unlink_if_set (void *p)
16823 {
16824 char **filename = p;
16825 if (*filename)
16826 unlink (*filename);
16827 }
16828
16829 /* A helper struct used when iterating over debug_types. */
16830 struct signatured_type_index_data
16831 {
16832 struct objfile *objfile;
16833 struct mapped_symtab *symtab;
16834 struct obstack *types_list;
16835 htab_t psyms_seen;
16836 int cu_index;
16837 };
16838
16839 /* A helper function that writes a single signatured_type to an
16840 obstack. */
16841
16842 static int
16843 write_one_signatured_type (void **slot, void *d)
16844 {
16845 struct signatured_type_index_data *info = d;
16846 struct signatured_type *entry = (struct signatured_type *) *slot;
16847 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16848 struct partial_symtab *psymtab = per_cu->v.psymtab;
16849 gdb_byte val[8];
16850
16851 write_psymbols (info->symtab,
16852 info->psyms_seen,
16853 info->objfile->global_psymbols.list
16854 + psymtab->globals_offset,
16855 psymtab->n_global_syms, info->cu_index,
16856 0);
16857 write_psymbols (info->symtab,
16858 info->psyms_seen,
16859 info->objfile->static_psymbols.list
16860 + psymtab->statics_offset,
16861 psymtab->n_static_syms, info->cu_index,
16862 1);
16863
16864 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16865 obstack_grow (info->types_list, val, 8);
16866 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16867 obstack_grow (info->types_list, val, 8);
16868 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16869 obstack_grow (info->types_list, val, 8);
16870
16871 ++info->cu_index;
16872
16873 return 1;
16874 }
16875
16876 /* Create an index file for OBJFILE in the directory DIR. */
16877
16878 static void
16879 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16880 {
16881 struct cleanup *cleanup;
16882 char *filename, *cleanup_filename;
16883 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16884 struct obstack cu_list, types_cu_list;
16885 int i;
16886 FILE *out_file;
16887 struct mapped_symtab *symtab;
16888 offset_type val, size_of_contents, total_len;
16889 struct stat st;
16890 char buf[8];
16891 htab_t psyms_seen;
16892 htab_t cu_index_htab;
16893 struct psymtab_cu_index_map *psymtab_cu_index_map;
16894
16895 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16896 return;
16897
16898 if (dwarf2_per_objfile->using_index)
16899 error (_("Cannot use an index to create the index"));
16900
16901 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16902 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16903
16904 if (stat (objfile->name, &st) < 0)
16905 perror_with_name (objfile->name);
16906
16907 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16908 INDEX_SUFFIX, (char *) NULL);
16909 cleanup = make_cleanup (xfree, filename);
16910
16911 out_file = fopen (filename, "wb");
16912 if (!out_file)
16913 error (_("Can't open `%s' for writing"), filename);
16914
16915 cleanup_filename = filename;
16916 make_cleanup (unlink_if_set, &cleanup_filename);
16917
16918 symtab = create_mapped_symtab ();
16919 make_cleanup (cleanup_mapped_symtab, symtab);
16920
16921 obstack_init (&addr_obstack);
16922 make_cleanup_obstack_free (&addr_obstack);
16923
16924 obstack_init (&cu_list);
16925 make_cleanup_obstack_free (&cu_list);
16926
16927 obstack_init (&types_cu_list);
16928 make_cleanup_obstack_free (&types_cu_list);
16929
16930 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16931 NULL, xcalloc, xfree);
16932 make_cleanup_htab_delete (psyms_seen);
16933
16934 /* While we're scanning CU's create a table that maps a psymtab pointer
16935 (which is what addrmap records) to its index (which is what is recorded
16936 in the index file). This will later be needed to write the address
16937 table. */
16938 cu_index_htab = htab_create_alloc (100,
16939 hash_psymtab_cu_index,
16940 eq_psymtab_cu_index,
16941 NULL, xcalloc, xfree);
16942 make_cleanup_htab_delete (cu_index_htab);
16943 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16944 xmalloc (sizeof (struct psymtab_cu_index_map)
16945 * dwarf2_per_objfile->n_comp_units);
16946 make_cleanup (xfree, psymtab_cu_index_map);
16947
16948 /* The CU list is already sorted, so we don't need to do additional
16949 work here. Also, the debug_types entries do not appear in
16950 all_comp_units, but only in their own hash table. */
16951 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16952 {
16953 struct dwarf2_per_cu_data *per_cu
16954 = dwarf2_per_objfile->all_comp_units[i];
16955 struct partial_symtab *psymtab = per_cu->v.psymtab;
16956 gdb_byte val[8];
16957 struct psymtab_cu_index_map *map;
16958 void **slot;
16959
16960 write_psymbols (symtab,
16961 psyms_seen,
16962 objfile->global_psymbols.list + psymtab->globals_offset,
16963 psymtab->n_global_syms, i,
16964 0);
16965 write_psymbols (symtab,
16966 psyms_seen,
16967 objfile->static_psymbols.list + psymtab->statics_offset,
16968 psymtab->n_static_syms, i,
16969 1);
16970
16971 map = &psymtab_cu_index_map[i];
16972 map->psymtab = psymtab;
16973 map->cu_index = i;
16974 slot = htab_find_slot (cu_index_htab, map, INSERT);
16975 gdb_assert (slot != NULL);
16976 gdb_assert (*slot == NULL);
16977 *slot = map;
16978
16979 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16980 obstack_grow (&cu_list, val, 8);
16981 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16982 obstack_grow (&cu_list, val, 8);
16983 }
16984
16985 /* Dump the address map. */
16986 write_address_map (objfile, &addr_obstack, cu_index_htab);
16987
16988 /* Write out the .debug_type entries, if any. */
16989 if (dwarf2_per_objfile->signatured_types)
16990 {
16991 struct signatured_type_index_data sig_data;
16992
16993 sig_data.objfile = objfile;
16994 sig_data.symtab = symtab;
16995 sig_data.types_list = &types_cu_list;
16996 sig_data.psyms_seen = psyms_seen;
16997 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16998 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16999 write_one_signatured_type, &sig_data);
17000 }
17001
17002 obstack_init (&constant_pool);
17003 make_cleanup_obstack_free (&constant_pool);
17004 obstack_init (&symtab_obstack);
17005 make_cleanup_obstack_free (&symtab_obstack);
17006 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17007
17008 obstack_init (&contents);
17009 make_cleanup_obstack_free (&contents);
17010 size_of_contents = 6 * sizeof (offset_type);
17011 total_len = size_of_contents;
17012
17013 /* The version number. */
17014 val = MAYBE_SWAP (5);
17015 obstack_grow (&contents, &val, sizeof (val));
17016
17017 /* The offset of the 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 (&cu_list);
17021
17022 /* The offset of the types CU list 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 (&types_cu_list);
17026
17027 /* The offset of the address 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 (&addr_obstack);
17031
17032 /* The offset of the symbol table 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 (&symtab_obstack);
17036
17037 /* The offset of the constant pool from the start of the file. */
17038 val = MAYBE_SWAP (total_len);
17039 obstack_grow (&contents, &val, sizeof (val));
17040 total_len += obstack_object_size (&constant_pool);
17041
17042 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17043
17044 write_obstack (out_file, &contents);
17045 write_obstack (out_file, &cu_list);
17046 write_obstack (out_file, &types_cu_list);
17047 write_obstack (out_file, &addr_obstack);
17048 write_obstack (out_file, &symtab_obstack);
17049 write_obstack (out_file, &constant_pool);
17050
17051 fclose (out_file);
17052
17053 /* We want to keep the file, so we set cleanup_filename to NULL
17054 here. See unlink_if_set. */
17055 cleanup_filename = NULL;
17056
17057 do_cleanups (cleanup);
17058 }
17059
17060 /* Implementation of the `save gdb-index' command.
17061
17062 Note that the file format used by this command is documented in the
17063 GDB manual. Any changes here must be documented there. */
17064
17065 static void
17066 save_gdb_index_command (char *arg, int from_tty)
17067 {
17068 struct objfile *objfile;
17069
17070 if (!arg || !*arg)
17071 error (_("usage: save gdb-index DIRECTORY"));
17072
17073 ALL_OBJFILES (objfile)
17074 {
17075 struct stat st;
17076
17077 /* If the objfile does not correspond to an actual file, skip it. */
17078 if (stat (objfile->name, &st) < 0)
17079 continue;
17080
17081 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17082 if (dwarf2_per_objfile)
17083 {
17084 volatile struct gdb_exception except;
17085
17086 TRY_CATCH (except, RETURN_MASK_ERROR)
17087 {
17088 write_psymtabs_to_index (objfile, arg);
17089 }
17090 if (except.reason < 0)
17091 exception_fprintf (gdb_stderr, except,
17092 _("Error while writing index for `%s': "),
17093 objfile->name);
17094 }
17095 }
17096 }
17097
17098 \f
17099
17100 int dwarf2_always_disassemble;
17101
17102 static void
17103 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17104 struct cmd_list_element *c, const char *value)
17105 {
17106 fprintf_filtered (file,
17107 _("Whether to always disassemble "
17108 "DWARF expressions is %s.\n"),
17109 value);
17110 }
17111
17112 static void
17113 show_check_physname (struct ui_file *file, int from_tty,
17114 struct cmd_list_element *c, const char *value)
17115 {
17116 fprintf_filtered (file,
17117 _("Whether to check \"physname\" is %s.\n"),
17118 value);
17119 }
17120
17121 void _initialize_dwarf2_read (void);
17122
17123 void
17124 _initialize_dwarf2_read (void)
17125 {
17126 struct cmd_list_element *c;
17127
17128 dwarf2_objfile_data_key
17129 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17130
17131 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17132 Set DWARF 2 specific variables.\n\
17133 Configure DWARF 2 variables such as the cache size"),
17134 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17135 0/*allow-unknown*/, &maintenance_set_cmdlist);
17136
17137 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17138 Show DWARF 2 specific variables\n\
17139 Show DWARF 2 variables such as the cache size"),
17140 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17141 0/*allow-unknown*/, &maintenance_show_cmdlist);
17142
17143 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17144 &dwarf2_max_cache_age, _("\
17145 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17146 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17147 A higher limit means that cached compilation units will be stored\n\
17148 in memory longer, and more total memory will be used. Zero disables\n\
17149 caching, which can slow down startup."),
17150 NULL,
17151 show_dwarf2_max_cache_age,
17152 &set_dwarf2_cmdlist,
17153 &show_dwarf2_cmdlist);
17154
17155 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17156 &dwarf2_always_disassemble, _("\
17157 Set whether `info address' always disassembles DWARF expressions."), _("\
17158 Show whether `info address' always disassembles DWARF expressions."), _("\
17159 When enabled, DWARF expressions are always printed in an assembly-like\n\
17160 syntax. When disabled, expressions will be printed in a more\n\
17161 conversational style, when possible."),
17162 NULL,
17163 show_dwarf2_always_disassemble,
17164 &set_dwarf2_cmdlist,
17165 &show_dwarf2_cmdlist);
17166
17167 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17168 Set debugging of the dwarf2 DIE reader."), _("\
17169 Show debugging of the dwarf2 DIE reader."), _("\
17170 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17171 The value is the maximum depth to print."),
17172 NULL,
17173 NULL,
17174 &setdebuglist, &showdebuglist);
17175
17176 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17177 Set cross-checking of \"physname\" code against demangler."), _("\
17178 Show cross-checking of \"physname\" code against demangler."), _("\
17179 When enabled, GDB's internal \"physname\" code is checked against\n\
17180 the demangler."),
17181 NULL, show_check_physname,
17182 &setdebuglist, &showdebuglist);
17183
17184 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17185 _("\
17186 Save a gdb-index file.\n\
17187 Usage: save gdb-index DIRECTORY"),
17188 &save_cmdlist);
17189 set_cmd_completer (c, filename_completer);
17190 }
This page took 0.5227 seconds and 4 git commands to generate.