Initial support for Fission. http://gcc.gnu.org/wiki/DebugFission
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
... / ...
CommitLineData
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/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31#include "defs.h"
32#include "bfd.h"
33#include "symtab.h"
34#include "gdbtypes.h"
35#include "objfiles.h"
36#include "dwarf2.h"
37#include "buildsym.h"
38#include "demangle.h"
39#include "gdb-demangle.h"
40#include "expression.h"
41#include "filenames.h" /* for DOSish file names */
42#include "macrotab.h"
43#include "language.h"
44#include "complaints.h"
45#include "bcache.h"
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
48#include "cp-support.h"
49#include "hashtab.h"
50#include "command.h"
51#include "gdbcmd.h"
52#include "block.h"
53#include "addrmap.h"
54#include "typeprint.h"
55#include "jv-lang.h"
56#include "psympriv.h"
57#include "exceptions.h"
58#include "gdb_stat.h"
59#include "completer.h"
60#include "vec.h"
61#include "c-lang.h"
62#include "go-lang.h"
63#include "valprint.h"
64#include "gdbcore.h" /* for gnutarget */
65#include <ctype.h>
66
67#include <fcntl.h>
68#include "gdb_string.h"
69#include "gdb_assert.h"
70#include <sys/types.h>
71#ifdef HAVE_ZLIB_H
72#include <zlib.h>
73#endif
74#ifdef HAVE_MMAP
75#include <sys/mman.h>
76#ifndef MAP_FAILED
77#define MAP_FAILED ((void *) -1)
78#endif
79#endif
80
81typedef struct symbol *symbolp;
82DEF_VEC_P (symbolp);
83
84/* When non-zero, dump DIEs after they are read in. */
85static int dwarf2_die_debug = 0;
86
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
90/* When non-zero, do not reject deprecated .gdb_index sections. */
91int use_deprecated_index_sections = 0;
92
93static int pagesize;
94
95/* When set, the file that we're processing is known to have debugging
96 info for C++ namespaces. GCC 3.3.x did not produce this information,
97 but later versions do. */
98
99static int processing_has_namespace_info;
100
101static const struct objfile_data *dwarf2_objfile_data_key;
102
103struct dwarf2_section_info
104{
105 asection *asection;
106 gdb_byte *buffer;
107 bfd_size_type size;
108 /* Not NULL if the section was actually mmapped. */
109 void *map_addr;
110 /* Page aligned size of mmapped area. */
111 bfd_size_type map_len;
112 /* True if we have tried to read this section. */
113 int readin;
114};
115
116typedef struct dwarf2_section_info dwarf2_section_info_def;
117DEF_VEC_O (dwarf2_section_info_def);
118
119/* All offsets in the index are of this type. It must be
120 architecture-independent. */
121typedef uint32_t offset_type;
122
123DEF_VEC_I (offset_type);
124
125/* A description of the mapped index. The file format is described in
126 a comment by the code that writes the index. */
127struct mapped_index
128{
129 /* Index data format version. */
130 int version;
131
132 /* The total length of the buffer. */
133 off_t total_size;
134
135 /* A pointer to the address table data. */
136 const gdb_byte *address_table;
137
138 /* Size of the address table data in bytes. */
139 offset_type address_table_size;
140
141 /* The symbol table, implemented as a hash table. */
142 const offset_type *symbol_table;
143
144 /* Size in slots, each slot is 2 offset_types. */
145 offset_type symbol_table_slots;
146
147 /* A pointer to the constant pool. */
148 const char *constant_pool;
149};
150
151/* Collection of data recorded per objfile.
152 This hangs off of dwarf2_objfile_data_key. */
153
154struct dwarf2_per_objfile
155{
156 struct dwarf2_section_info info;
157 struct dwarf2_section_info abbrev;
158 struct dwarf2_section_info line;
159 struct dwarf2_section_info loc;
160 struct dwarf2_section_info macinfo;
161 struct dwarf2_section_info macro;
162 struct dwarf2_section_info str;
163 struct dwarf2_section_info ranges;
164 struct dwarf2_section_info addr;
165 struct dwarf2_section_info frame;
166 struct dwarf2_section_info eh_frame;
167 struct dwarf2_section_info gdb_index;
168
169 VEC (dwarf2_section_info_def) *types;
170
171 /* Back link. */
172 struct objfile *objfile;
173
174 /* Table of all the compilation units. This is used to locate
175 the target compilation unit of a particular reference. */
176 struct dwarf2_per_cu_data **all_comp_units;
177
178 /* The number of compilation units in ALL_COMP_UNITS. */
179 int n_comp_units;
180
181 /* The number of .debug_types-related CUs. */
182 int n_type_units;
183
184 /* The .debug_types-related CUs (TUs). */
185 struct dwarf2_per_cu_data **all_type_units;
186
187 /* A chain of compilation units that are currently read in, so that
188 they can be freed later. */
189 struct dwarf2_per_cu_data *read_in_chain;
190
191 /* A table mapping .debug_types signatures to its signatured_type entry.
192 This is NULL if the .debug_types section hasn't been read in yet. */
193 htab_t signatured_types;
194
195 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
196 This is NULL if the table hasn't been allocated yet. */
197 htab_t dwo_files;
198
199 /* A flag indicating wether this objfile has a section loaded at a
200 VMA of 0. */
201 int has_section_at_zero;
202
203 /* True if we are using the mapped index,
204 or we are faking it for OBJF_READNOW's sake. */
205 unsigned char using_index;
206
207 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
208 struct mapped_index *index_table;
209
210 /* When using index_table, this keeps track of all quick_file_names entries.
211 TUs can share line table entries with CUs or other TUs, and there can be
212 a lot more TUs than unique line tables, so we maintain a separate table
213 of all line table entries to support the sharing. */
214 htab_t quick_file_names_table;
215
216 /* Set during partial symbol reading, to prevent queueing of full
217 symbols. */
218 int reading_partial_symbols;
219
220 /* Table mapping type DIEs to their struct type *.
221 This is NULL if not allocated yet.
222 The mapping is done via (CU/TU signature + DIE offset) -> type. */
223 htab_t die_type_hash;
224};
225
226static struct dwarf2_per_objfile *dwarf2_per_objfile;
227
228/* Default names of the debugging sections. */
229
230/* Note that if the debugging section has been compressed, it might
231 have a name like .zdebug_info. */
232
233static const struct dwarf2_debug_sections dwarf2_elf_names =
234{
235 { ".debug_info", ".zdebug_info" },
236 { ".debug_abbrev", ".zdebug_abbrev" },
237 { ".debug_line", ".zdebug_line" },
238 { ".debug_loc", ".zdebug_loc" },
239 { ".debug_macinfo", ".zdebug_macinfo" },
240 { ".debug_macro", ".zdebug_macro" },
241 { ".debug_str", ".zdebug_str" },
242 { ".debug_ranges", ".zdebug_ranges" },
243 { ".debug_types", ".zdebug_types" },
244 { ".debug_addr", ".zdebug_addr" },
245 { ".debug_frame", ".zdebug_frame" },
246 { ".eh_frame", NULL },
247 { ".gdb_index", ".zgdb_index" },
248 23
249};
250
251/* List of DWO sections. */
252
253static const struct dwo_section_names
254{
255 struct dwarf2_section_names abbrev_dwo;
256 struct dwarf2_section_names info_dwo;
257 struct dwarf2_section_names line_dwo;
258 struct dwarf2_section_names loc_dwo;
259 struct dwarf2_section_names str_dwo;
260 struct dwarf2_section_names str_offsets_dwo;
261 struct dwarf2_section_names types_dwo;
262}
263dwo_section_names =
264{
265 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
266 { ".debug_info.dwo", ".zdebug_info.dwo" },
267 { ".debug_line.dwo", ".zdebug_line.dwo" },
268 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
269 { ".debug_str.dwo", ".zdebug_str.dwo" },
270 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
271 { ".debug_types.dwo", ".zdebug_types.dwo" },
272};
273
274/* local data types */
275
276/* We hold several abbreviation tables in memory at the same time. */
277#ifndef ABBREV_HASH_SIZE
278#define ABBREV_HASH_SIZE 121
279#endif
280
281/* The data in a compilation unit header, after target2host
282 translation, looks like this. */
283struct comp_unit_head
284{
285 unsigned int length;
286 short version;
287 unsigned char addr_size;
288 unsigned char signed_addr_p;
289 sect_offset abbrev_offset;
290
291 /* Size of file offsets; either 4 or 8. */
292 unsigned int offset_size;
293
294 /* Size of the length field; either 4 or 12. */
295 unsigned int initial_length_size;
296
297 /* Offset to the first byte of this compilation unit header in the
298 .debug_info section, for resolving relative reference dies. */
299 sect_offset offset;
300
301 /* Offset to first die in this cu from the start of the cu.
302 This will be the first byte following the compilation unit header. */
303 cu_offset first_die_offset;
304};
305
306/* Type used for delaying computation of method physnames.
307 See comments for compute_delayed_physnames. */
308struct delayed_method_info
309{
310 /* The type to which the method is attached, i.e., its parent class. */
311 struct type *type;
312
313 /* The index of the method in the type's function fieldlists. */
314 int fnfield_index;
315
316 /* The index of the method in the fieldlist. */
317 int index;
318
319 /* The name of the DIE. */
320 const char *name;
321
322 /* The DIE associated with this method. */
323 struct die_info *die;
324};
325
326typedef struct delayed_method_info delayed_method_info;
327DEF_VEC_O (delayed_method_info);
328
329/* Internal state when decoding a particular compilation unit. */
330struct dwarf2_cu
331{
332 /* The objfile containing this compilation unit. */
333 struct objfile *objfile;
334
335 /* The header of the compilation unit. */
336 struct comp_unit_head header;
337
338 /* Base address of this compilation unit. */
339 CORE_ADDR base_address;
340
341 /* Non-zero if base_address has been set. */
342 int base_known;
343
344 /* The language we are debugging. */
345 enum language language;
346 const struct language_defn *language_defn;
347
348 const char *producer;
349
350 /* The generic symbol table building routines have separate lists for
351 file scope symbols and all all other scopes (local scopes). So
352 we need to select the right one to pass to add_symbol_to_list().
353 We do it by keeping a pointer to the correct list in list_in_scope.
354
355 FIXME: The original dwarf code just treated the file scope as the
356 first local scope, and all other local scopes as nested local
357 scopes, and worked fine. Check to see if we really need to
358 distinguish these in buildsym.c. */
359 struct pending **list_in_scope;
360
361 /* DWARF abbreviation table associated with this compilation unit. */
362 struct abbrev_info **dwarf2_abbrevs;
363
364 /* Storage for the abbrev table. */
365 struct obstack abbrev_obstack;
366
367 /* Hash table holding all the loaded partial DIEs
368 with partial_die->offset.SECT_OFF as hash. */
369 htab_t partial_dies;
370
371 /* Storage for things with the same lifetime as this read-in compilation
372 unit, including partial DIEs. */
373 struct obstack comp_unit_obstack;
374
375 /* When multiple dwarf2_cu structures are living in memory, this field
376 chains them all together, so that they can be released efficiently.
377 We will probably also want a generation counter so that most-recently-used
378 compilation units are cached... */
379 struct dwarf2_per_cu_data *read_in_chain;
380
381 /* Backchain to our per_cu entry if the tree has been built. */
382 struct dwarf2_per_cu_data *per_cu;
383
384 /* How many compilation units ago was this CU last referenced? */
385 int last_used;
386
387 /* A hash table of DIE cu_offset for following references with
388 die_info->offset.sect_off as hash. */
389 htab_t die_hash;
390
391 /* Full DIEs if read in. */
392 struct die_info *dies;
393
394 /* A set of pointers to dwarf2_per_cu_data objects for compilation
395 units referenced by this one. Only set during full symbol processing;
396 partial symbol tables do not have dependencies. */
397 htab_t dependencies;
398
399 /* Header data from the line table, during full symbol processing. */
400 struct line_header *line_header;
401
402 /* A list of methods which need to have physnames computed
403 after all type information has been read. */
404 VEC (delayed_method_info) *method_list;
405
406 /* To be copied to symtab->call_site_htab. */
407 htab_t call_site_htab;
408
409 /* Non-NULL if this CU came from a DWO file. */
410 struct dwo_unit *dwo_unit;
411
412 /* The DW_AT_addr_base attribute if present, zero otherwise
413 (zero is a valid value though).
414 Note this value comes from the stub CU/TU's DIE. */
415 ULONGEST addr_base;
416
417 /* Mark used when releasing cached dies. */
418 unsigned int mark : 1;
419
420 /* This CU references .debug_loc. See the symtab->locations_valid field.
421 This test is imperfect as there may exist optimized debug code not using
422 any location list and still facing inlining issues if handled as
423 unoptimized code. For a future better test see GCC PR other/32998. */
424 unsigned int has_loclist : 1;
425
426 /* These cache the results of producer_is_gxx_lt_4_6.
427 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
428 information is cached because profiling CU expansion showed
429 excessive time spent in producer_is_gxx_lt_4_6. */
430 unsigned int checked_producer : 1;
431 unsigned int producer_is_gxx_lt_4_6 : 1;
432
433 /* Non-zero if DW_AT_addr_base was found.
434 Used when processing DWO files. */
435 unsigned int have_addr_base : 1;
436};
437
438/* Persistent data held for a compilation unit, even when not
439 processing it. We put a pointer to this structure in the
440 read_symtab_private field of the psymtab. */
441
442struct dwarf2_per_cu_data
443{
444 /* The start offset and length of this compilation unit. 2**29-1
445 bytes should suffice to store the length of any compilation unit
446 - if it doesn't, GDB will fall over anyway.
447 NOTE: Unlike comp_unit_head.length, this length includes
448 initial_length_size.
449 If the DIE refers to a DWO file, this is always of the original die,
450 not the DWO file. */
451 sect_offset offset;
452 unsigned int length : 29;
453
454 /* Flag indicating this compilation unit will be read in before
455 any of the current compilation units are processed. */
456 unsigned int queued : 1;
457
458 /* This flag will be set when reading partial DIEs if we need to load
459 absolutely all DIEs for this compilation unit, instead of just the ones
460 we think are interesting. It gets set if we look for a DIE in the
461 hash table and don't find it. */
462 unsigned int load_all_dies : 1;
463
464 /* Non-zero if this CU is from .debug_types. */
465 unsigned int is_debug_types : 1;
466
467 /* The section this CU/TU lives in.
468 If the DIE refers to a DWO file, this is always the original die,
469 not the DWO file. */
470 struct dwarf2_section_info *info_or_types_section;
471
472 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
473 of the CU cache it gets reset to NULL again. */
474 struct dwarf2_cu *cu;
475
476 /* The corresponding objfile.
477 Normally we can get the objfile from dwarf2_per_objfile.
478 However we can enter this file with just a "per_cu" handle. */
479 struct objfile *objfile;
480
481 /* When using partial symbol tables, the 'psymtab' field is active.
482 Otherwise the 'quick' field is active. */
483 union
484 {
485 /* The partial symbol table associated with this compilation unit,
486 or NULL for partial units (which do not have an associated
487 symtab). */
488 struct partial_symtab *psymtab;
489
490 /* Data needed by the "quick" functions. */
491 struct dwarf2_per_cu_quick_data *quick;
492 } v;
493};
494
495/* Entry in the signatured_types hash table. */
496
497struct signatured_type
498{
499 /* The type's signature. */
500 ULONGEST signature;
501
502 /* Offset in the TU of the type's DIE, as read from the TU header.
503 If the definition lives in a DWO file, this value is unusable. */
504 cu_offset type_offset_in_tu;
505
506 /* Offset in the section of the type's DIE.
507 If the definition lives in a DWO file, this is the offset in the
508 .debug_types.dwo section.
509 The value is zero until the actual value is known.
510 Zero is otherwise not a valid section offset. */
511 sect_offset type_offset_in_section;
512
513 /* The CU(/TU) of this type. */
514 struct dwarf2_per_cu_data per_cu;
515};
516
517/* These sections are what may appear in a "dwo" file. */
518
519struct dwo_sections
520{
521 struct dwarf2_section_info abbrev;
522 struct dwarf2_section_info info;
523 struct dwarf2_section_info line;
524 struct dwarf2_section_info loc;
525 struct dwarf2_section_info str;
526 struct dwarf2_section_info str_offsets;
527 VEC (dwarf2_section_info_def) *types;
528};
529
530/* Common bits of DWO CUs/TUs. */
531
532struct dwo_unit
533{
534 /* Backlink to the containing struct dwo_file. */
535 struct dwo_file *dwo_file;
536
537 /* The "id" that distinguishes this CU/TU.
538 .debug_info calls this "dwo_id", .debug_types calls this "signature".
539 Since signatures came first, we stick with it for consistency. */
540 ULONGEST signature;
541
542 /* The section this CU/TU lives in, in the DWO file. */
543 struct dwarf2_section_info *info_or_types_section;
544
545 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
546 sect_offset offset;
547 unsigned int length;
548
549 /* For types, offset in the type's DIE of the type defined by this TU. */
550 cu_offset type_offset_in_tu;
551};
552
553/* Data for one DWO file. */
554
555struct dwo_file
556{
557 /* The DW_AT_GNU_dwo_name attribute.
558 We don't manage space for this, it's an attribute. */
559 const char *dwo_name;
560
561 /* The bfd, when the file is open. Otherwise this is NULL. */
562 bfd *dwo_bfd;
563
564 /* Section info for this file. */
565 struct dwo_sections sections;
566
567 /* Table of CUs in the file.
568 Each element is a struct dwo_unit. */
569 htab_t cus;
570
571 /* Table of TUs in the file.
572 Each element is a struct dwo_unit. */
573 htab_t tus;
574};
575
576/* Struct used to pass misc. parameters to read_die_and_children, et
577 al. which are used for both .debug_info and .debug_types dies.
578 All parameters here are unchanging for the life of the call. This
579 struct exists to abstract away the constant parameters of die reading. */
580
581struct die_reader_specs
582{
583 /* die_section->asection->owner. */
584 bfd* abfd;
585
586 /* The CU of the DIE we are parsing. */
587 struct dwarf2_cu *cu;
588
589 /* Non-NULL if reading a DWO file. */
590 struct dwo_file *dwo_file;
591
592 /* The section the die comes from.
593 This is either .debug_info or .debug_types, or the .dwo variants. */
594 struct dwarf2_section_info *die_section;
595
596 /* die_section->buffer. */
597 gdb_byte *buffer;
598};
599
600/* Type of function passed to init_cu_and_read_dies, et.al. */
601typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
602 gdb_byte *info_ptr,
603 struct die_info *comp_unit_die,
604 int has_children,
605 void *data);
606
607/* The line number information for a compilation unit (found in the
608 .debug_line section) begins with a "statement program header",
609 which contains the following information. */
610struct line_header
611{
612 unsigned int total_length;
613 unsigned short version;
614 unsigned int header_length;
615 unsigned char minimum_instruction_length;
616 unsigned char maximum_ops_per_instruction;
617 unsigned char default_is_stmt;
618 int line_base;
619 unsigned char line_range;
620 unsigned char opcode_base;
621
622 /* standard_opcode_lengths[i] is the number of operands for the
623 standard opcode whose value is i. This means that
624 standard_opcode_lengths[0] is unused, and the last meaningful
625 element is standard_opcode_lengths[opcode_base - 1]. */
626 unsigned char *standard_opcode_lengths;
627
628 /* The include_directories table. NOTE! These strings are not
629 allocated with xmalloc; instead, they are pointers into
630 debug_line_buffer. If you try to free them, `free' will get
631 indigestion. */
632 unsigned int num_include_dirs, include_dirs_size;
633 char **include_dirs;
634
635 /* The file_names table. NOTE! These strings are not allocated
636 with xmalloc; instead, they are pointers into debug_line_buffer.
637 Don't try to free them directly. */
638 unsigned int num_file_names, file_names_size;
639 struct file_entry
640 {
641 char *name;
642 unsigned int dir_index;
643 unsigned int mod_time;
644 unsigned int length;
645 int included_p; /* Non-zero if referenced by the Line Number Program. */
646 struct symtab *symtab; /* The associated symbol table, if any. */
647 } *file_names;
648
649 /* The start and end of the statement program following this
650 header. These point into dwarf2_per_objfile->line_buffer. */
651 gdb_byte *statement_program_start, *statement_program_end;
652};
653
654/* When we construct a partial symbol table entry we only
655 need this much information. */
656struct partial_die_info
657 {
658 /* Offset of this DIE. */
659 sect_offset offset;
660
661 /* DWARF-2 tag for this DIE. */
662 ENUM_BITFIELD(dwarf_tag) tag : 16;
663
664 /* Assorted flags describing the data found in this DIE. */
665 unsigned int has_children : 1;
666 unsigned int is_external : 1;
667 unsigned int is_declaration : 1;
668 unsigned int has_type : 1;
669 unsigned int has_specification : 1;
670 unsigned int has_pc_info : 1;
671 unsigned int may_be_inlined : 1;
672
673 /* Flag set if the SCOPE field of this structure has been
674 computed. */
675 unsigned int scope_set : 1;
676
677 /* Flag set if the DIE has a byte_size attribute. */
678 unsigned int has_byte_size : 1;
679
680 /* Flag set if any of the DIE's children are template arguments. */
681 unsigned int has_template_arguments : 1;
682
683 /* Flag set if fixup_partial_die has been called on this die. */
684 unsigned int fixup_called : 1;
685
686 /* The name of this DIE. Normally the value of DW_AT_name, but
687 sometimes a default name for unnamed DIEs. */
688 char *name;
689
690 /* The linkage name, if present. */
691 const char *linkage_name;
692
693 /* The scope to prepend to our children. This is generally
694 allocated on the comp_unit_obstack, so will disappear
695 when this compilation unit leaves the cache. */
696 char *scope;
697
698 /* The location description associated with this DIE, if any. */
699 struct dwarf_block *locdesc;
700
701 /* If HAS_PC_INFO, the PC range associated with this DIE. */
702 CORE_ADDR lowpc;
703 CORE_ADDR highpc;
704
705 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
706 DW_AT_sibling, if any. */
707 /* NOTE: This member isn't strictly necessary, read_partial_die could
708 return DW_AT_sibling values to its caller load_partial_dies. */
709 gdb_byte *sibling;
710
711 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
712 DW_AT_specification (or DW_AT_abstract_origin or
713 DW_AT_extension). */
714 sect_offset spec_offset;
715
716 /* Pointers to this DIE's parent, first child, and next sibling,
717 if any. */
718 struct partial_die_info *die_parent, *die_child, *die_sibling;
719 };
720
721/* This data structure holds the information of an abbrev. */
722struct abbrev_info
723 {
724 unsigned int number; /* number identifying abbrev */
725 enum dwarf_tag tag; /* dwarf tag */
726 unsigned short has_children; /* boolean */
727 unsigned short num_attrs; /* number of attributes */
728 struct attr_abbrev *attrs; /* an array of attribute descriptions */
729 struct abbrev_info *next; /* next in chain */
730 };
731
732struct attr_abbrev
733 {
734 ENUM_BITFIELD(dwarf_attribute) name : 16;
735 ENUM_BITFIELD(dwarf_form) form : 16;
736 };
737
738/* Attributes have a name and a value. */
739struct attribute
740 {
741 ENUM_BITFIELD(dwarf_attribute) name : 16;
742 ENUM_BITFIELD(dwarf_form) form : 15;
743
744 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
745 field should be in u.str (existing only for DW_STRING) but it is kept
746 here for better struct attribute alignment. */
747 unsigned int string_is_canonical : 1;
748
749 union
750 {
751 char *str;
752 struct dwarf_block *blk;
753 ULONGEST unsnd;
754 LONGEST snd;
755 CORE_ADDR addr;
756 struct signatured_type *signatured_type;
757 }
758 u;
759 };
760
761/* This data structure holds a complete die structure. */
762struct die_info
763 {
764 /* DWARF-2 tag for this DIE. */
765 ENUM_BITFIELD(dwarf_tag) tag : 16;
766
767 /* Number of attributes */
768 unsigned char num_attrs;
769
770 /* True if we're presently building the full type name for the
771 type derived from this DIE. */
772 unsigned char building_fullname : 1;
773
774 /* Abbrev number */
775 unsigned int abbrev;
776
777 /* Offset in .debug_info or .debug_types section. */
778 sect_offset offset;
779
780 /* The dies in a compilation unit form an n-ary tree. PARENT
781 points to this die's parent; CHILD points to the first child of
782 this node; and all the children of a given node are chained
783 together via their SIBLING fields. */
784 struct die_info *child; /* Its first child, if any. */
785 struct die_info *sibling; /* Its next sibling, if any. */
786 struct die_info *parent; /* Its parent, if any. */
787
788 /* An array of attributes, with NUM_ATTRS elements. There may be
789 zero, but it's not common and zero-sized arrays are not
790 sufficiently portable C. */
791 struct attribute attrs[1];
792 };
793
794/* Get at parts of an attribute structure. */
795
796#define DW_STRING(attr) ((attr)->u.str)
797#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
798#define DW_UNSND(attr) ((attr)->u.unsnd)
799#define DW_BLOCK(attr) ((attr)->u.blk)
800#define DW_SND(attr) ((attr)->u.snd)
801#define DW_ADDR(attr) ((attr)->u.addr)
802#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
803
804/* Blocks are a bunch of untyped bytes. */
805struct dwarf_block
806 {
807 unsigned int size;
808
809 /* Valid only if SIZE is not zero. */
810 gdb_byte *data;
811 };
812
813#ifndef ATTR_ALLOC_CHUNK
814#define ATTR_ALLOC_CHUNK 4
815#endif
816
817/* Allocate fields for structs, unions and enums in this size. */
818#ifndef DW_FIELD_ALLOC_CHUNK
819#define DW_FIELD_ALLOC_CHUNK 4
820#endif
821
822/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
823 but this would require a corresponding change in unpack_field_as_long
824 and friends. */
825static int bits_per_byte = 8;
826
827/* The routines that read and process dies for a C struct or C++ class
828 pass lists of data member fields and lists of member function fields
829 in an instance of a field_info structure, as defined below. */
830struct field_info
831 {
832 /* List of data member and baseclasses fields. */
833 struct nextfield
834 {
835 struct nextfield *next;
836 int accessibility;
837 int virtuality;
838 struct field field;
839 }
840 *fields, *baseclasses;
841
842 /* Number of fields (including baseclasses). */
843 int nfields;
844
845 /* Number of baseclasses. */
846 int nbaseclasses;
847
848 /* Set if the accesibility of one of the fields is not public. */
849 int non_public_fields;
850
851 /* Member function fields array, entries are allocated in the order they
852 are encountered in the object file. */
853 struct nextfnfield
854 {
855 struct nextfnfield *next;
856 struct fn_field fnfield;
857 }
858 *fnfields;
859
860 /* Member function fieldlist array, contains name of possibly overloaded
861 member function, number of overloaded member functions and a pointer
862 to the head of the member function field chain. */
863 struct fnfieldlist
864 {
865 char *name;
866 int length;
867 struct nextfnfield *head;
868 }
869 *fnfieldlists;
870
871 /* Number of entries in the fnfieldlists array. */
872 int nfnfields;
873
874 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
875 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
876 struct typedef_field_list
877 {
878 struct typedef_field field;
879 struct typedef_field_list *next;
880 }
881 *typedef_field_list;
882 unsigned typedef_field_list_count;
883 };
884
885/* One item on the queue of compilation units to read in full symbols
886 for. */
887struct dwarf2_queue_item
888{
889 struct dwarf2_per_cu_data *per_cu;
890 struct dwarf2_queue_item *next;
891};
892
893/* The current queue. */
894static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
895
896/* Loaded secondary compilation units are kept in memory until they
897 have not been referenced for the processing of this many
898 compilation units. Set this to zero to disable caching. Cache
899 sizes of up to at least twenty will improve startup time for
900 typical inter-CU-reference binaries, at an obvious memory cost. */
901static int dwarf2_max_cache_age = 5;
902static void
903show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
904 struct cmd_list_element *c, const char *value)
905{
906 fprintf_filtered (file, _("The upper bound on the age of cached "
907 "dwarf2 compilation units is %s.\n"),
908 value);
909}
910
911
912/* Various complaints about symbol reading that don't abort the process. */
913
914static void
915dwarf2_statement_list_fits_in_line_number_section_complaint (void)
916{
917 complaint (&symfile_complaints,
918 _("statement list doesn't fit in .debug_line section"));
919}
920
921static void
922dwarf2_debug_line_missing_file_complaint (void)
923{
924 complaint (&symfile_complaints,
925 _(".debug_line section has line data without a file"));
926}
927
928static void
929dwarf2_debug_line_missing_end_sequence_complaint (void)
930{
931 complaint (&symfile_complaints,
932 _(".debug_line section has line "
933 "program sequence without an end"));
934}
935
936static void
937dwarf2_complex_location_expr_complaint (void)
938{
939 complaint (&symfile_complaints, _("location expression too complex"));
940}
941
942static void
943dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
944 int arg3)
945{
946 complaint (&symfile_complaints,
947 _("const value length mismatch for '%s', got %d, expected %d"),
948 arg1, arg2, arg3);
949}
950
951static void
952dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
953{
954 complaint (&symfile_complaints,
955 _("macro info runs off end of `%s' section"),
956 section->asection->name);
957}
958
959static void
960dwarf2_macro_malformed_definition_complaint (const char *arg1)
961{
962 complaint (&symfile_complaints,
963 _("macro debug info contains a "
964 "malformed macro definition:\n`%s'"),
965 arg1);
966}
967
968static void
969dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
970{
971 complaint (&symfile_complaints,
972 _("invalid attribute class or form for '%s' in '%s'"),
973 arg1, arg2);
974}
975
976/* local function prototypes */
977
978static void dwarf2_locate_sections (bfd *, asection *, void *);
979
980static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
981 struct objfile *);
982
983static void dwarf2_find_base_address (struct die_info *die,
984 struct dwarf2_cu *cu);
985
986static void dwarf2_build_psymtabs_hard (struct objfile *);
987
988static void scan_partial_symbols (struct partial_die_info *,
989 CORE_ADDR *, CORE_ADDR *,
990 int, struct dwarf2_cu *);
991
992static void add_partial_symbol (struct partial_die_info *,
993 struct dwarf2_cu *);
994
995static void add_partial_namespace (struct partial_die_info *pdi,
996 CORE_ADDR *lowpc, CORE_ADDR *highpc,
997 int need_pc, struct dwarf2_cu *cu);
998
999static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1000 CORE_ADDR *highpc, int need_pc,
1001 struct dwarf2_cu *cu);
1002
1003static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1004 struct dwarf2_cu *cu);
1005
1006static void add_partial_subprogram (struct partial_die_info *pdi,
1007 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1008 int need_pc, struct dwarf2_cu *cu);
1009
1010static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1011
1012static void psymtab_to_symtab_1 (struct partial_symtab *);
1013
1014static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1015 struct dwarf2_section_info *);
1016
1017static void dwarf2_free_abbrev_table (void *);
1018
1019static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1020
1021static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
1022 struct dwarf2_cu *);
1023
1024static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
1025 struct dwarf2_cu *);
1026
1027static struct partial_die_info *load_partial_dies
1028 (const struct die_reader_specs *, gdb_byte *, int);
1029
1030static gdb_byte *read_partial_die (const struct die_reader_specs *,
1031 struct partial_die_info *,
1032 struct abbrev_info *,
1033 unsigned int,
1034 gdb_byte *);
1035
1036static struct partial_die_info *find_partial_die (sect_offset,
1037 struct dwarf2_cu *);
1038
1039static void fixup_partial_die (struct partial_die_info *,
1040 struct dwarf2_cu *);
1041
1042static gdb_byte *read_attribute (const struct die_reader_specs *,
1043 struct attribute *, struct attr_abbrev *,
1044 gdb_byte *);
1045
1046static unsigned int read_1_byte (bfd *, gdb_byte *);
1047
1048static int read_1_signed_byte (bfd *, gdb_byte *);
1049
1050static unsigned int read_2_bytes (bfd *, gdb_byte *);
1051
1052static unsigned int read_4_bytes (bfd *, gdb_byte *);
1053
1054static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1055
1056static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1057 unsigned int *);
1058
1059static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1060
1061static LONGEST read_checked_initial_length_and_offset
1062 (bfd *, gdb_byte *, const struct comp_unit_head *,
1063 unsigned int *, unsigned int *);
1064
1065static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1066 unsigned int *);
1067
1068static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1069
1070static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1071
1072static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1073
1074static char *read_indirect_string (bfd *, gdb_byte *,
1075 const struct comp_unit_head *,
1076 unsigned int *);
1077
1078static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1079
1080static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1081
1082static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1083 unsigned int *);
1084
1085static char *read_str_index (const struct die_reader_specs *reader,
1086 struct dwarf2_cu *cu, ULONGEST str_index);
1087
1088static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1089
1090static void set_cu_language (unsigned int, struct dwarf2_cu *);
1091
1092static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1093 struct dwarf2_cu *);
1094
1095static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1096 unsigned int,
1097 struct dwarf2_cu *);
1098
1099static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1100 struct dwarf2_cu *cu);
1101
1102static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1103
1104static struct die_info *die_specification (struct die_info *die,
1105 struct dwarf2_cu **);
1106
1107static void free_line_header (struct line_header *lh);
1108
1109static void add_file_name (struct line_header *, char *, unsigned int,
1110 unsigned int, unsigned int);
1111
1112static struct line_header *dwarf_decode_line_header (unsigned int offset,
1113 struct dwarf2_cu *cu);
1114
1115static void dwarf_decode_lines (struct line_header *, const char *,
1116 struct dwarf2_cu *, struct partial_symtab *,
1117 int);
1118
1119static void dwarf2_start_subfile (char *, const char *, const char *);
1120
1121static struct symbol *new_symbol (struct die_info *, struct type *,
1122 struct dwarf2_cu *);
1123
1124static struct symbol *new_symbol_full (struct die_info *, struct type *,
1125 struct dwarf2_cu *, struct symbol *);
1126
1127static void dwarf2_const_value (struct attribute *, struct symbol *,
1128 struct dwarf2_cu *);
1129
1130static void dwarf2_const_value_attr (struct attribute *attr,
1131 struct type *type,
1132 const char *name,
1133 struct obstack *obstack,
1134 struct dwarf2_cu *cu, LONGEST *value,
1135 gdb_byte **bytes,
1136 struct dwarf2_locexpr_baton **baton);
1137
1138static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1139
1140static int need_gnat_info (struct dwarf2_cu *);
1141
1142static struct type *die_descriptive_type (struct die_info *,
1143 struct dwarf2_cu *);
1144
1145static void set_descriptive_type (struct type *, struct die_info *,
1146 struct dwarf2_cu *);
1147
1148static struct type *die_containing_type (struct die_info *,
1149 struct dwarf2_cu *);
1150
1151static struct type *lookup_die_type (struct die_info *, struct attribute *,
1152 struct dwarf2_cu *);
1153
1154static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1155
1156static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1157
1158static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1159
1160static char *typename_concat (struct obstack *obs, const char *prefix,
1161 const char *suffix, int physname,
1162 struct dwarf2_cu *cu);
1163
1164static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1165
1166static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1167
1168static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1169
1170static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1171
1172static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1173
1174static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1175 struct dwarf2_cu *, struct partial_symtab *);
1176
1177static int dwarf2_get_pc_bounds (struct die_info *,
1178 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1179 struct partial_symtab *);
1180
1181static void get_scope_pc_bounds (struct die_info *,
1182 CORE_ADDR *, CORE_ADDR *,
1183 struct dwarf2_cu *);
1184
1185static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1186 CORE_ADDR, struct dwarf2_cu *);
1187
1188static void dwarf2_add_field (struct field_info *, struct die_info *,
1189 struct dwarf2_cu *);
1190
1191static void dwarf2_attach_fields_to_type (struct field_info *,
1192 struct type *, struct dwarf2_cu *);
1193
1194static void dwarf2_add_member_fn (struct field_info *,
1195 struct die_info *, struct type *,
1196 struct dwarf2_cu *);
1197
1198static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1199 struct type *,
1200 struct dwarf2_cu *);
1201
1202static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1203
1204static void read_common_block (struct die_info *, struct dwarf2_cu *);
1205
1206static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1207
1208static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1209
1210static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1211
1212static struct type *read_module_type (struct die_info *die,
1213 struct dwarf2_cu *cu);
1214
1215static const char *namespace_name (struct die_info *die,
1216 int *is_anonymous, struct dwarf2_cu *);
1217
1218static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1219
1220static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1221
1222static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1223 struct dwarf2_cu *);
1224
1225static struct die_info *read_die_and_children (const struct die_reader_specs *,
1226 gdb_byte *info_ptr,
1227 gdb_byte **new_info_ptr,
1228 struct die_info *parent);
1229
1230static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1231 gdb_byte *info_ptr,
1232 gdb_byte **new_info_ptr,
1233 struct die_info *parent);
1234
1235static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1236 struct die_info **, gdb_byte *, int *, int);
1237
1238static gdb_byte *read_full_die (const struct die_reader_specs *,
1239 struct die_info **, gdb_byte *, int *);
1240
1241static void process_die (struct die_info *, struct dwarf2_cu *);
1242
1243static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1244 struct obstack *);
1245
1246static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1247
1248static const char *dwarf2_full_name (char *name,
1249 struct die_info *die,
1250 struct dwarf2_cu *cu);
1251
1252static struct die_info *dwarf2_extension (struct die_info *die,
1253 struct dwarf2_cu **);
1254
1255static char *dwarf_tag_name (unsigned int);
1256
1257static char *dwarf_attr_name (unsigned int);
1258
1259static char *dwarf_form_name (unsigned int);
1260
1261static char *dwarf_bool_name (unsigned int);
1262
1263static char *dwarf_type_encoding_name (unsigned int);
1264
1265#if 0
1266static char *dwarf_cfi_name (unsigned int);
1267#endif
1268
1269static struct die_info *sibling_die (struct die_info *);
1270
1271static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1272
1273static void dump_die_for_error (struct die_info *);
1274
1275static void dump_die_1 (struct ui_file *, int level, int max_level,
1276 struct die_info *);
1277
1278/*static*/ void dump_die (struct die_info *, int max_level);
1279
1280static void store_in_ref_table (struct die_info *,
1281 struct dwarf2_cu *);
1282
1283static int is_ref_attr (struct attribute *);
1284
1285static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1286
1287static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1288
1289static struct die_info *follow_die_ref_or_sig (struct die_info *,
1290 struct attribute *,
1291 struct dwarf2_cu **);
1292
1293static struct die_info *follow_die_ref (struct die_info *,
1294 struct attribute *,
1295 struct dwarf2_cu **);
1296
1297static struct die_info *follow_die_sig (struct die_info *,
1298 struct attribute *,
1299 struct dwarf2_cu **);
1300
1301static struct signatured_type *lookup_signatured_type_at_offset
1302 (struct objfile *objfile,
1303 struct dwarf2_section_info *section, sect_offset offset);
1304
1305static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1306
1307static void read_signatured_type (struct signatured_type *);
1308
1309/* memory allocation interface */
1310
1311static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1312
1313static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1314
1315static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1316
1317static void dwarf_decode_macros (struct line_header *, unsigned int,
1318 char *, bfd *, struct dwarf2_cu *,
1319 struct dwarf2_section_info *,
1320 int);
1321
1322static int attr_form_is_block (struct attribute *);
1323
1324static int attr_form_is_section_offset (struct attribute *);
1325
1326static int attr_form_is_constant (struct attribute *);
1327
1328static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1329 struct dwarf2_loclist_baton *baton,
1330 struct attribute *attr);
1331
1332static void dwarf2_symbol_mark_computed (struct attribute *attr,
1333 struct symbol *sym,
1334 struct dwarf2_cu *cu);
1335
1336static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1337 gdb_byte *info_ptr,
1338 struct abbrev_info *abbrev);
1339
1340static void free_stack_comp_unit (void *);
1341
1342static hashval_t partial_die_hash (const void *item);
1343
1344static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1345
1346static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1347 (sect_offset offset, struct objfile *objfile);
1348
1349static void init_one_comp_unit (struct dwarf2_cu *cu,
1350 struct dwarf2_per_cu_data *per_cu);
1351
1352static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1353 struct die_info *comp_unit_die);
1354
1355static void free_heap_comp_unit (void *);
1356
1357static void free_cached_comp_units (void *);
1358
1359static void age_cached_comp_units (void);
1360
1361static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1362
1363static struct type *set_die_type (struct die_info *, struct type *,
1364 struct dwarf2_cu *);
1365
1366static void create_all_comp_units (struct objfile *);
1367
1368static int create_all_type_units (struct objfile *);
1369
1370static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1371
1372static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1373
1374static void dwarf2_add_dependence (struct dwarf2_cu *,
1375 struct dwarf2_per_cu_data *);
1376
1377static void dwarf2_mark (struct dwarf2_cu *);
1378
1379static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1380
1381static struct type *get_die_type_at_offset (sect_offset,
1382 struct dwarf2_per_cu_data *per_cu);
1383
1384static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1385
1386static void dwarf2_release_queue (void *dummy);
1387
1388static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1389
1390static void process_queue (void);
1391
1392static void find_file_and_directory (struct die_info *die,
1393 struct dwarf2_cu *cu,
1394 char **name, char **comp_dir);
1395
1396static char *file_full_name (int file, struct line_header *lh,
1397 const char *comp_dir);
1398
1399static gdb_byte *read_and_check_comp_unit_head
1400 (struct comp_unit_head *header,
1401 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1402 int is_debug_types_section);
1403
1404static void init_tu_and_read_dies
1405 (struct dwarf2_per_cu_data *this_cu, int keep,
1406 die_reader_func_ftype *die_reader_func, void *data);
1407
1408static void init_cutu_and_read_dies_simple
1409 (struct dwarf2_per_cu_data *this_cu,
1410 die_reader_func_ftype *die_reader_func, void *data);
1411
1412static htab_t allocate_signatured_type_table (struct objfile *objfile);
1413
1414static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *);
1415
1416static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1417
1418static struct dwo_unit *lookup_dwo_comp_unit
1419 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1420
1421static struct dwo_unit *lookup_dwo_type_unit
1422 (struct signatured_type *, char *, const char *);
1423
1424static void free_dwo_file_cleanup (void *);
1425
1426static void munmap_section_buffer (struct dwarf2_section_info *);
1427
1428#if WORDS_BIGENDIAN
1429
1430/* Convert VALUE between big- and little-endian. */
1431static offset_type
1432byte_swap (offset_type value)
1433{
1434 offset_type result;
1435
1436 result = (value & 0xff) << 24;
1437 result |= (value & 0xff00) << 8;
1438 result |= (value & 0xff0000) >> 8;
1439 result |= (value & 0xff000000) >> 24;
1440 return result;
1441}
1442
1443#define MAYBE_SWAP(V) byte_swap (V)
1444
1445#else
1446#define MAYBE_SWAP(V) (V)
1447#endif /* WORDS_BIGENDIAN */
1448
1449/* The suffix for an index file. */
1450#define INDEX_SUFFIX ".gdb-index"
1451
1452static const char *dwarf2_physname (char *name, struct die_info *die,
1453 struct dwarf2_cu *cu);
1454
1455/* Try to locate the sections we need for DWARF 2 debugging
1456 information and return true if we have enough to do something.
1457 NAMES points to the dwarf2 section names, or is NULL if the standard
1458 ELF names are used. */
1459
1460int
1461dwarf2_has_info (struct objfile *objfile,
1462 const struct dwarf2_debug_sections *names)
1463{
1464 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1465 if (!dwarf2_per_objfile)
1466 {
1467 /* Initialize per-objfile state. */
1468 struct dwarf2_per_objfile *data
1469 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1470
1471 memset (data, 0, sizeof (*data));
1472 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1473 dwarf2_per_objfile = data;
1474
1475 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1476 (void *) names);
1477 dwarf2_per_objfile->objfile = objfile;
1478 }
1479 return (dwarf2_per_objfile->info.asection != NULL
1480 && dwarf2_per_objfile->abbrev.asection != NULL);
1481}
1482
1483/* When loading sections, we look either for uncompressed section or for
1484 compressed section names. */
1485
1486static int
1487section_is_p (const char *section_name,
1488 const struct dwarf2_section_names *names)
1489{
1490 if (names->normal != NULL
1491 && strcmp (section_name, names->normal) == 0)
1492 return 1;
1493 if (names->compressed != NULL
1494 && strcmp (section_name, names->compressed) == 0)
1495 return 1;
1496 return 0;
1497}
1498
1499/* This function is mapped across the sections and remembers the
1500 offset and size of each of the debugging sections we are interested
1501 in. */
1502
1503static void
1504dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1505{
1506 const struct dwarf2_debug_sections *names;
1507
1508 if (vnames == NULL)
1509 names = &dwarf2_elf_names;
1510 else
1511 names = (const struct dwarf2_debug_sections *) vnames;
1512
1513 if (section_is_p (sectp->name, &names->info))
1514 {
1515 dwarf2_per_objfile->info.asection = sectp;
1516 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1517 }
1518 else if (section_is_p (sectp->name, &names->abbrev))
1519 {
1520 dwarf2_per_objfile->abbrev.asection = sectp;
1521 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1522 }
1523 else if (section_is_p (sectp->name, &names->line))
1524 {
1525 dwarf2_per_objfile->line.asection = sectp;
1526 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1527 }
1528 else if (section_is_p (sectp->name, &names->loc))
1529 {
1530 dwarf2_per_objfile->loc.asection = sectp;
1531 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1532 }
1533 else if (section_is_p (sectp->name, &names->macinfo))
1534 {
1535 dwarf2_per_objfile->macinfo.asection = sectp;
1536 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1537 }
1538 else if (section_is_p (sectp->name, &names->macro))
1539 {
1540 dwarf2_per_objfile->macro.asection = sectp;
1541 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1542 }
1543 else if (section_is_p (sectp->name, &names->str))
1544 {
1545 dwarf2_per_objfile->str.asection = sectp;
1546 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1547 }
1548 else if (section_is_p (sectp->name, &names->addr))
1549 {
1550 dwarf2_per_objfile->addr.asection = sectp;
1551 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1552 }
1553 else if (section_is_p (sectp->name, &names->frame))
1554 {
1555 dwarf2_per_objfile->frame.asection = sectp;
1556 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1557 }
1558 else if (section_is_p (sectp->name, &names->eh_frame))
1559 {
1560 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1561
1562 if (aflag & SEC_HAS_CONTENTS)
1563 {
1564 dwarf2_per_objfile->eh_frame.asection = sectp;
1565 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1566 }
1567 }
1568 else if (section_is_p (sectp->name, &names->ranges))
1569 {
1570 dwarf2_per_objfile->ranges.asection = sectp;
1571 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1572 }
1573 else if (section_is_p (sectp->name, &names->types))
1574 {
1575 struct dwarf2_section_info type_section;
1576
1577 memset (&type_section, 0, sizeof (type_section));
1578 type_section.asection = sectp;
1579 type_section.size = bfd_get_section_size (sectp);
1580
1581 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1582 &type_section);
1583 }
1584 else if (section_is_p (sectp->name, &names->gdb_index))
1585 {
1586 dwarf2_per_objfile->gdb_index.asection = sectp;
1587 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1588 }
1589
1590 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1591 && bfd_section_vma (abfd, sectp) == 0)
1592 dwarf2_per_objfile->has_section_at_zero = 1;
1593}
1594
1595/* Decompress a section that was compressed using zlib. Store the
1596 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1597
1598static void
1599zlib_decompress_section (struct objfile *objfile, asection *sectp,
1600 gdb_byte **outbuf, bfd_size_type *outsize)
1601{
1602 bfd *abfd = sectp->owner;
1603#ifndef HAVE_ZLIB_H
1604 error (_("Support for zlib-compressed DWARF data (from '%s') "
1605 "is disabled in this copy of GDB"),
1606 bfd_get_filename (abfd));
1607#else
1608 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1609 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1610 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1611 bfd_size_type uncompressed_size;
1612 gdb_byte *uncompressed_buffer;
1613 z_stream strm;
1614 int rc;
1615 int header_size = 12;
1616
1617 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1618 || bfd_bread (compressed_buffer,
1619 compressed_size, abfd) != compressed_size)
1620 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1621 bfd_get_filename (abfd));
1622
1623 /* Read the zlib header. In this case, it should be "ZLIB" followed
1624 by the uncompressed section size, 8 bytes in big-endian order. */
1625 if (compressed_size < header_size
1626 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1627 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1628 bfd_get_filename (abfd));
1629 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1630 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1631 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1632 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1633 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1634 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1635 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1636 uncompressed_size += compressed_buffer[11];
1637
1638 /* It is possible the section consists of several compressed
1639 buffers concatenated together, so we uncompress in a loop. */
1640 strm.zalloc = NULL;
1641 strm.zfree = NULL;
1642 strm.opaque = NULL;
1643 strm.avail_in = compressed_size - header_size;
1644 strm.next_in = (Bytef*) compressed_buffer + header_size;
1645 strm.avail_out = uncompressed_size;
1646 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1647 uncompressed_size);
1648 rc = inflateInit (&strm);
1649 while (strm.avail_in > 0)
1650 {
1651 if (rc != Z_OK)
1652 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1653 bfd_get_filename (abfd), rc);
1654 strm.next_out = ((Bytef*) uncompressed_buffer
1655 + (uncompressed_size - strm.avail_out));
1656 rc = inflate (&strm, Z_FINISH);
1657 if (rc != Z_STREAM_END)
1658 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1659 bfd_get_filename (abfd), rc);
1660 rc = inflateReset (&strm);
1661 }
1662 rc = inflateEnd (&strm);
1663 if (rc != Z_OK
1664 || strm.avail_out != 0)
1665 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1666 bfd_get_filename (abfd), rc);
1667
1668 do_cleanups (cleanup);
1669 *outbuf = uncompressed_buffer;
1670 *outsize = uncompressed_size;
1671#endif
1672}
1673
1674/* A helper function that decides whether a section is empty. */
1675
1676static int
1677dwarf2_section_empty_p (struct dwarf2_section_info *info)
1678{
1679 return info->asection == NULL || info->size == 0;
1680}
1681
1682/* Read the contents of the section INFO.
1683 OBJFILE is the main object file, but not necessarily the file where
1684 the section comes from. E.g., for DWO files INFO->asection->owner
1685 is the bfd of the DWO file.
1686 If the section is compressed, uncompress it before returning. */
1687
1688static void
1689dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1690{
1691 asection *sectp = info->asection;
1692 bfd *abfd;
1693 gdb_byte *buf, *retbuf;
1694 unsigned char header[4];
1695
1696 if (info->readin)
1697 return;
1698 info->buffer = NULL;
1699 info->map_addr = NULL;
1700 info->readin = 1;
1701
1702 if (dwarf2_section_empty_p (info))
1703 return;
1704
1705 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1706 abfd = sectp->owner;
1707
1708 /* Check if the file has a 4-byte header indicating compression. */
1709 if (info->size > sizeof (header)
1710 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1711 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1712 {
1713 /* Upon decompression, update the buffer and its size. */
1714 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1715 {
1716 zlib_decompress_section (objfile, sectp, &info->buffer,
1717 &info->size);
1718 return;
1719 }
1720 }
1721
1722#ifdef HAVE_MMAP
1723 if (pagesize == 0)
1724 pagesize = getpagesize ();
1725
1726 /* Only try to mmap sections which are large enough: we don't want to
1727 waste space due to fragmentation. Also, only try mmap for sections
1728 without relocations. */
1729
1730 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1731 {
1732 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1733 MAP_PRIVATE, sectp->filepos,
1734 &info->map_addr, &info->map_len);
1735
1736 if ((caddr_t)info->buffer != MAP_FAILED)
1737 {
1738#if HAVE_POSIX_MADVISE
1739 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1740#endif
1741 return;
1742 }
1743 }
1744#endif
1745
1746 /* If we get here, we are a normal, not-compressed section. */
1747 info->buffer = buf
1748 = obstack_alloc (&objfile->objfile_obstack, info->size);
1749
1750 /* When debugging .o files, we may need to apply relocations; see
1751 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1752 We never compress sections in .o files, so we only need to
1753 try this when the section is not compressed. */
1754 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1755 if (retbuf != NULL)
1756 {
1757 info->buffer = retbuf;
1758 return;
1759 }
1760
1761 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1762 || bfd_bread (buf, info->size, abfd) != info->size)
1763 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1764 bfd_get_filename (abfd));
1765}
1766
1767/* A helper function that returns the size of a section in a safe way.
1768 If you are positive that the section has been read before using the
1769 size, then it is safe to refer to the dwarf2_section_info object's
1770 "size" field directly. In other cases, you must call this
1771 function, because for compressed sections the size field is not set
1772 correctly until the section has been read. */
1773
1774static bfd_size_type
1775dwarf2_section_size (struct objfile *objfile,
1776 struct dwarf2_section_info *info)
1777{
1778 if (!info->readin)
1779 dwarf2_read_section (objfile, info);
1780 return info->size;
1781}
1782
1783/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1784 SECTION_NAME. */
1785
1786void
1787dwarf2_get_section_info (struct objfile *objfile,
1788 enum dwarf2_section_enum sect,
1789 asection **sectp, gdb_byte **bufp,
1790 bfd_size_type *sizep)
1791{
1792 struct dwarf2_per_objfile *data
1793 = objfile_data (objfile, dwarf2_objfile_data_key);
1794 struct dwarf2_section_info *info;
1795
1796 /* We may see an objfile without any DWARF, in which case we just
1797 return nothing. */
1798 if (data == NULL)
1799 {
1800 *sectp = NULL;
1801 *bufp = NULL;
1802 *sizep = 0;
1803 return;
1804 }
1805 switch (sect)
1806 {
1807 case DWARF2_DEBUG_FRAME:
1808 info = &data->frame;
1809 break;
1810 case DWARF2_EH_FRAME:
1811 info = &data->eh_frame;
1812 break;
1813 default:
1814 gdb_assert_not_reached ("unexpected section");
1815 }
1816
1817 dwarf2_read_section (objfile, info);
1818
1819 *sectp = info->asection;
1820 *bufp = info->buffer;
1821 *sizep = info->size;
1822}
1823
1824\f
1825/* DWARF quick_symbols_functions support. */
1826
1827/* TUs can share .debug_line entries, and there can be a lot more TUs than
1828 unique line tables, so we maintain a separate table of all .debug_line
1829 derived entries to support the sharing.
1830 All the quick functions need is the list of file names. We discard the
1831 line_header when we're done and don't need to record it here. */
1832struct quick_file_names
1833{
1834 /* The offset in .debug_line of the line table. We hash on this. */
1835 unsigned int offset;
1836
1837 /* The number of entries in file_names, real_names. */
1838 unsigned int num_file_names;
1839
1840 /* The file names from the line table, after being run through
1841 file_full_name. */
1842 const char **file_names;
1843
1844 /* The file names from the line table after being run through
1845 gdb_realpath. These are computed lazily. */
1846 const char **real_names;
1847};
1848
1849/* When using the index (and thus not using psymtabs), each CU has an
1850 object of this type. This is used to hold information needed by
1851 the various "quick" methods. */
1852struct dwarf2_per_cu_quick_data
1853{
1854 /* The file table. This can be NULL if there was no file table
1855 or it's currently not read in.
1856 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1857 struct quick_file_names *file_names;
1858
1859 /* The corresponding symbol table. This is NULL if symbols for this
1860 CU have not yet been read. */
1861 struct symtab *symtab;
1862
1863 /* A temporary mark bit used when iterating over all CUs in
1864 expand_symtabs_matching. */
1865 unsigned int mark : 1;
1866
1867 /* True if we've tried to read the file table and found there isn't one.
1868 There will be no point in trying to read it again next time. */
1869 unsigned int no_file_data : 1;
1870};
1871
1872/* Hash function for a quick_file_names. */
1873
1874static hashval_t
1875hash_file_name_entry (const void *e)
1876{
1877 const struct quick_file_names *file_data = e;
1878
1879 return file_data->offset;
1880}
1881
1882/* Equality function for a quick_file_names. */
1883
1884static int
1885eq_file_name_entry (const void *a, const void *b)
1886{
1887 const struct quick_file_names *ea = a;
1888 const struct quick_file_names *eb = b;
1889
1890 return ea->offset == eb->offset;
1891}
1892
1893/* Delete function for a quick_file_names. */
1894
1895static void
1896delete_file_name_entry (void *e)
1897{
1898 struct quick_file_names *file_data = e;
1899 int i;
1900
1901 for (i = 0; i < file_data->num_file_names; ++i)
1902 {
1903 xfree ((void*) file_data->file_names[i]);
1904 if (file_data->real_names)
1905 xfree ((void*) file_data->real_names[i]);
1906 }
1907
1908 /* The space for the struct itself lives on objfile_obstack,
1909 so we don't free it here. */
1910}
1911
1912/* Create a quick_file_names hash table. */
1913
1914static htab_t
1915create_quick_file_names_table (unsigned int nr_initial_entries)
1916{
1917 return htab_create_alloc (nr_initial_entries,
1918 hash_file_name_entry, eq_file_name_entry,
1919 delete_file_name_entry, xcalloc, xfree);
1920}
1921
1922/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1923 have to be created afterwards. You should call age_cached_comp_units after
1924 processing PER_CU->CU. dw2_setup must have been already called. */
1925
1926static void
1927load_cu (struct dwarf2_per_cu_data *per_cu)
1928{
1929 if (per_cu->is_debug_types)
1930 load_full_type_unit (per_cu);
1931 else
1932 load_full_comp_unit (per_cu);
1933
1934 gdb_assert (per_cu->cu != NULL);
1935
1936 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1937}
1938
1939/* Read in the symbols for PER_CU. */
1940
1941static void
1942dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1943{
1944 struct cleanup *back_to;
1945
1946 back_to = make_cleanup (dwarf2_release_queue, NULL);
1947
1948 queue_comp_unit (per_cu);
1949
1950 load_cu (per_cu);
1951
1952 process_queue ();
1953
1954 /* Age the cache, releasing compilation units that have not
1955 been used recently. */
1956 age_cached_comp_units ();
1957
1958 do_cleanups (back_to);
1959}
1960
1961/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1962 the objfile from which this CU came. Returns the resulting symbol
1963 table. */
1964
1965static struct symtab *
1966dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1967{
1968 if (!per_cu->v.quick->symtab)
1969 {
1970 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1971 increment_reading_symtab ();
1972 dw2_do_instantiate_symtab (per_cu);
1973 do_cleanups (back_to);
1974 }
1975 return per_cu->v.quick->symtab;
1976}
1977
1978/* Return the CU given its index. */
1979
1980static struct dwarf2_per_cu_data *
1981dw2_get_cu (int index)
1982{
1983 if (index >= dwarf2_per_objfile->n_comp_units)
1984 {
1985 index -= dwarf2_per_objfile->n_comp_units;
1986 return dwarf2_per_objfile->all_type_units[index];
1987 }
1988 return dwarf2_per_objfile->all_comp_units[index];
1989}
1990
1991/* A helper function that knows how to read a 64-bit value in a way
1992 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1993 otherwise. */
1994
1995static int
1996extract_cu_value (const char *bytes, ULONGEST *result)
1997{
1998 if (sizeof (ULONGEST) < 8)
1999 {
2000 int i;
2001
2002 /* Ignore the upper 4 bytes if they are all zero. */
2003 for (i = 0; i < 4; ++i)
2004 if (bytes[i + 4] != 0)
2005 return 0;
2006
2007 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2008 }
2009 else
2010 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2011 return 1;
2012}
2013
2014/* Read the CU list from the mapped index, and use it to create all
2015 the CU objects for this objfile. Return 0 if something went wrong,
2016 1 if everything went ok. */
2017
2018static int
2019create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2020 offset_type cu_list_elements)
2021{
2022 offset_type i;
2023
2024 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2025 dwarf2_per_objfile->all_comp_units
2026 = obstack_alloc (&objfile->objfile_obstack,
2027 dwarf2_per_objfile->n_comp_units
2028 * sizeof (struct dwarf2_per_cu_data *));
2029
2030 for (i = 0; i < cu_list_elements; i += 2)
2031 {
2032 struct dwarf2_per_cu_data *the_cu;
2033 ULONGEST offset, length;
2034
2035 if (!extract_cu_value (cu_list, &offset)
2036 || !extract_cu_value (cu_list + 8, &length))
2037 return 0;
2038 cu_list += 2 * 8;
2039
2040 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2041 struct dwarf2_per_cu_data);
2042 the_cu->offset.sect_off = offset;
2043 the_cu->length = length;
2044 the_cu->objfile = objfile;
2045 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2046 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2047 struct dwarf2_per_cu_quick_data);
2048 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2049 }
2050
2051 return 1;
2052}
2053
2054/* Create the signatured type hash table from the index. */
2055
2056static int
2057create_signatured_type_table_from_index (struct objfile *objfile,
2058 struct dwarf2_section_info *section,
2059 const gdb_byte *bytes,
2060 offset_type elements)
2061{
2062 offset_type i;
2063 htab_t sig_types_hash;
2064
2065 dwarf2_per_objfile->n_type_units = elements / 3;
2066 dwarf2_per_objfile->all_type_units
2067 = obstack_alloc (&objfile->objfile_obstack,
2068 dwarf2_per_objfile->n_type_units
2069 * sizeof (struct dwarf2_per_cu_data *));
2070
2071 sig_types_hash = allocate_signatured_type_table (objfile);
2072
2073 for (i = 0; i < elements; i += 3)
2074 {
2075 struct signatured_type *sig_type;
2076 ULONGEST offset, type_offset_in_tu, signature;
2077 void **slot;
2078
2079 if (!extract_cu_value (bytes, &offset)
2080 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2081 return 0;
2082 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2083 bytes += 3 * 8;
2084
2085 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2086 struct signatured_type);
2087 sig_type->signature = signature;
2088 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2089 sig_type->per_cu.is_debug_types = 1;
2090 sig_type->per_cu.info_or_types_section = section;
2091 sig_type->per_cu.offset.sect_off = offset;
2092 sig_type->per_cu.objfile = objfile;
2093 sig_type->per_cu.v.quick
2094 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2095 struct dwarf2_per_cu_quick_data);
2096
2097 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2098 *slot = sig_type;
2099
2100 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2101 }
2102
2103 dwarf2_per_objfile->signatured_types = sig_types_hash;
2104
2105 return 1;
2106}
2107
2108/* Read the address map data from the mapped index, and use it to
2109 populate the objfile's psymtabs_addrmap. */
2110
2111static void
2112create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2113{
2114 const gdb_byte *iter, *end;
2115 struct obstack temp_obstack;
2116 struct addrmap *mutable_map;
2117 struct cleanup *cleanup;
2118 CORE_ADDR baseaddr;
2119
2120 obstack_init (&temp_obstack);
2121 cleanup = make_cleanup_obstack_free (&temp_obstack);
2122 mutable_map = addrmap_create_mutable (&temp_obstack);
2123
2124 iter = index->address_table;
2125 end = iter + index->address_table_size;
2126
2127 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2128
2129 while (iter < end)
2130 {
2131 ULONGEST hi, lo, cu_index;
2132 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2133 iter += 8;
2134 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2135 iter += 8;
2136 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2137 iter += 4;
2138
2139 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2140 dw2_get_cu (cu_index));
2141 }
2142
2143 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2144 &objfile->objfile_obstack);
2145 do_cleanups (cleanup);
2146}
2147
2148/* The hash function for strings in the mapped index. This is the same as
2149 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2150 implementation. This is necessary because the hash function is tied to the
2151 format of the mapped index file. The hash values do not have to match with
2152 SYMBOL_HASH_NEXT.
2153
2154 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2155
2156static hashval_t
2157mapped_index_string_hash (int index_version, const void *p)
2158{
2159 const unsigned char *str = (const unsigned char *) p;
2160 hashval_t r = 0;
2161 unsigned char c;
2162
2163 while ((c = *str++) != 0)
2164 {
2165 if (index_version >= 5)
2166 c = tolower (c);
2167 r = r * 67 + c - 113;
2168 }
2169
2170 return r;
2171}
2172
2173/* Find a slot in the mapped index INDEX for the object named NAME.
2174 If NAME is found, set *VEC_OUT to point to the CU vector in the
2175 constant pool and return 1. If NAME cannot be found, return 0. */
2176
2177static int
2178find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2179 offset_type **vec_out)
2180{
2181 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2182 offset_type hash;
2183 offset_type slot, step;
2184 int (*cmp) (const char *, const char *);
2185
2186 if (current_language->la_language == language_cplus
2187 || current_language->la_language == language_java
2188 || current_language->la_language == language_fortran)
2189 {
2190 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2191 not contain any. */
2192 const char *paren = strchr (name, '(');
2193
2194 if (paren)
2195 {
2196 char *dup;
2197
2198 dup = xmalloc (paren - name + 1);
2199 memcpy (dup, name, paren - name);
2200 dup[paren - name] = 0;
2201
2202 make_cleanup (xfree, dup);
2203 name = dup;
2204 }
2205 }
2206
2207 /* Index version 4 did not support case insensitive searches. But the
2208 indices for case insensitive languages are built in lowercase, therefore
2209 simulate our NAME being searched is also lowercased. */
2210 hash = mapped_index_string_hash ((index->version == 4
2211 && case_sensitivity == case_sensitive_off
2212 ? 5 : index->version),
2213 name);
2214
2215 slot = hash & (index->symbol_table_slots - 1);
2216 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2217 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2218
2219 for (;;)
2220 {
2221 /* Convert a slot number to an offset into the table. */
2222 offset_type i = 2 * slot;
2223 const char *str;
2224 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2225 {
2226 do_cleanups (back_to);
2227 return 0;
2228 }
2229
2230 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2231 if (!cmp (name, str))
2232 {
2233 *vec_out = (offset_type *) (index->constant_pool
2234 + MAYBE_SWAP (index->symbol_table[i + 1]));
2235 do_cleanups (back_to);
2236 return 1;
2237 }
2238
2239 slot = (slot + step) & (index->symbol_table_slots - 1);
2240 }
2241}
2242
2243/* Read the index file. If everything went ok, initialize the "quick"
2244 elements of all the CUs and return 1. Otherwise, return 0. */
2245
2246static int
2247dwarf2_read_index (struct objfile *objfile)
2248{
2249 char *addr;
2250 struct mapped_index *map;
2251 offset_type *metadata;
2252 const gdb_byte *cu_list;
2253 const gdb_byte *types_list = NULL;
2254 offset_type version, cu_list_elements;
2255 offset_type types_list_elements = 0;
2256 int i;
2257
2258 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2259 return 0;
2260
2261 /* Older elfutils strip versions could keep the section in the main
2262 executable while splitting it for the separate debug info file. */
2263 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2264 & SEC_HAS_CONTENTS) == 0)
2265 return 0;
2266
2267 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2268
2269 addr = dwarf2_per_objfile->gdb_index.buffer;
2270 /* Version check. */
2271 version = MAYBE_SWAP (*(offset_type *) addr);
2272 /* Versions earlier than 3 emitted every copy of a psymbol. This
2273 causes the index to behave very poorly for certain requests. Version 3
2274 contained incomplete addrmap. So, it seems better to just ignore such
2275 indices. */
2276 if (version < 4)
2277 {
2278 static int warning_printed = 0;
2279 if (!warning_printed)
2280 {
2281 warning (_("Skipping obsolete .gdb_index section in %s."),
2282 objfile->name);
2283 warning_printed = 1;
2284 }
2285 return 0;
2286 }
2287 /* Index version 4 uses a different hash function than index version
2288 5 and later.
2289
2290 Versions earlier than 6 did not emit psymbols for inlined
2291 functions. Using these files will cause GDB not to be able to
2292 set breakpoints on inlined functions by name, so we ignore these
2293 indices unless the --use-deprecated-index-sections command line
2294 option was supplied. */
2295 if (version < 6 && !use_deprecated_index_sections)
2296 {
2297 static int warning_printed = 0;
2298 if (!warning_printed)
2299 {
2300 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2301 "--use-deprecated-index-sections to use them anyway"),
2302 objfile->name);
2303 warning_printed = 1;
2304 }
2305 return 0;
2306 }
2307 /* Indexes with higher version than the one supported by GDB may be no
2308 longer backward compatible. */
2309 if (version > 6)
2310 return 0;
2311
2312 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2313 map->version = version;
2314 map->total_size = dwarf2_per_objfile->gdb_index.size;
2315
2316 metadata = (offset_type *) (addr + sizeof (offset_type));
2317
2318 i = 0;
2319 cu_list = addr + MAYBE_SWAP (metadata[i]);
2320 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2321 / 8);
2322 ++i;
2323
2324 types_list = addr + MAYBE_SWAP (metadata[i]);
2325 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2326 - MAYBE_SWAP (metadata[i]))
2327 / 8);
2328 ++i;
2329
2330 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2331 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2332 - MAYBE_SWAP (metadata[i]));
2333 ++i;
2334
2335 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2336 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2337 - MAYBE_SWAP (metadata[i]))
2338 / (2 * sizeof (offset_type)));
2339 ++i;
2340
2341 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2342
2343 /* Don't use the index if it's empty. */
2344 if (map->symbol_table_slots == 0)
2345 return 0;
2346
2347 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2348 return 0;
2349
2350 if (types_list_elements)
2351 {
2352 struct dwarf2_section_info *section;
2353
2354 /* We can only handle a single .debug_types when we have an
2355 index. */
2356 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2357 return 0;
2358
2359 section = VEC_index (dwarf2_section_info_def,
2360 dwarf2_per_objfile->types, 0);
2361
2362 if (!create_signatured_type_table_from_index (objfile, section,
2363 types_list,
2364 types_list_elements))
2365 return 0;
2366 }
2367
2368 create_addrmap_from_index (objfile, map);
2369
2370 dwarf2_per_objfile->index_table = map;
2371 dwarf2_per_objfile->using_index = 1;
2372 dwarf2_per_objfile->quick_file_names_table =
2373 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2374
2375 return 1;
2376}
2377
2378/* A helper for the "quick" functions which sets the global
2379 dwarf2_per_objfile according to OBJFILE. */
2380
2381static void
2382dw2_setup (struct objfile *objfile)
2383{
2384 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2385 gdb_assert (dwarf2_per_objfile);
2386}
2387
2388/* die_reader_func for dw2_get_file_names. */
2389
2390static void
2391dw2_get_file_names_reader (const struct die_reader_specs *reader,
2392 gdb_byte *info_ptr,
2393 struct die_info *comp_unit_die,
2394 int has_children,
2395 void *data)
2396{
2397 struct dwarf2_cu *cu = reader->cu;
2398 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2399 struct objfile *objfile = dwarf2_per_objfile->objfile;
2400 struct line_header *lh;
2401 struct attribute *attr;
2402 int i;
2403 unsigned int bytes_read;
2404 char *name, *comp_dir;
2405 void **slot;
2406 struct quick_file_names *qfn;
2407 unsigned int line_offset;
2408
2409 lh = NULL;
2410 slot = NULL;
2411 line_offset = 0;
2412
2413 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2414 if (attr)
2415 {
2416 struct quick_file_names find_entry;
2417
2418 line_offset = DW_UNSND (attr);
2419
2420 /* We may have already read in this line header (TU line header sharing).
2421 If we have we're done. */
2422 find_entry.offset = line_offset;
2423 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2424 &find_entry, INSERT);
2425 if (*slot != NULL)
2426 {
2427 this_cu->v.quick->file_names = *slot;
2428 return;
2429 }
2430
2431 lh = dwarf_decode_line_header (line_offset, cu);
2432 }
2433 if (lh == NULL)
2434 {
2435 this_cu->v.quick->no_file_data = 1;
2436 return;
2437 }
2438
2439 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2440 qfn->offset = line_offset;
2441 gdb_assert (slot != NULL);
2442 *slot = qfn;
2443
2444 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2445
2446 qfn->num_file_names = lh->num_file_names;
2447 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2448 lh->num_file_names * sizeof (char *));
2449 for (i = 0; i < lh->num_file_names; ++i)
2450 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2451 qfn->real_names = NULL;
2452
2453 free_line_header (lh);
2454
2455 this_cu->v.quick->file_names = qfn;
2456}
2457
2458/* A helper for the "quick" functions which attempts to read the line
2459 table for THIS_CU. */
2460
2461static struct quick_file_names *
2462dw2_get_file_names (struct objfile *objfile,
2463 struct dwarf2_per_cu_data *this_cu)
2464{
2465 if (this_cu->v.quick->file_names != NULL)
2466 return this_cu->v.quick->file_names;
2467 /* If we know there is no line data, no point in looking again. */
2468 if (this_cu->v.quick->no_file_data)
2469 return NULL;
2470
2471 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2472 in the stub for CUs, there's is no need to lookup the DWO file.
2473 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2474 DWO file. */
2475 if (this_cu->is_debug_types)
2476 init_tu_and_read_dies (this_cu, 0, dw2_get_file_names_reader, NULL);
2477 else
2478 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2479
2480 if (this_cu->v.quick->no_file_data)
2481 return NULL;
2482 return this_cu->v.quick->file_names;
2483}
2484
2485/* A helper for the "quick" functions which computes and caches the
2486 real path for a given file name from the line table. */
2487
2488static const char *
2489dw2_get_real_path (struct objfile *objfile,
2490 struct quick_file_names *qfn, int index)
2491{
2492 if (qfn->real_names == NULL)
2493 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2494 qfn->num_file_names, sizeof (char *));
2495
2496 if (qfn->real_names[index] == NULL)
2497 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2498
2499 return qfn->real_names[index];
2500}
2501
2502static struct symtab *
2503dw2_find_last_source_symtab (struct objfile *objfile)
2504{
2505 int index;
2506
2507 dw2_setup (objfile);
2508 index = dwarf2_per_objfile->n_comp_units - 1;
2509 return dw2_instantiate_symtab (dw2_get_cu (index));
2510}
2511
2512/* Traversal function for dw2_forget_cached_source_info. */
2513
2514static int
2515dw2_free_cached_file_names (void **slot, void *info)
2516{
2517 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2518
2519 if (file_data->real_names)
2520 {
2521 int i;
2522
2523 for (i = 0; i < file_data->num_file_names; ++i)
2524 {
2525 xfree ((void*) file_data->real_names[i]);
2526 file_data->real_names[i] = NULL;
2527 }
2528 }
2529
2530 return 1;
2531}
2532
2533static void
2534dw2_forget_cached_source_info (struct objfile *objfile)
2535{
2536 dw2_setup (objfile);
2537
2538 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2539 dw2_free_cached_file_names, NULL);
2540}
2541
2542/* Helper function for dw2_map_symtabs_matching_filename that expands
2543 the symtabs and calls the iterator. */
2544
2545static int
2546dw2_map_expand_apply (struct objfile *objfile,
2547 struct dwarf2_per_cu_data *per_cu,
2548 const char *name,
2549 const char *full_path, const char *real_path,
2550 int (*callback) (struct symtab *, void *),
2551 void *data)
2552{
2553 struct symtab *last_made = objfile->symtabs;
2554
2555 /* Don't visit already-expanded CUs. */
2556 if (per_cu->v.quick->symtab)
2557 return 0;
2558
2559 /* This may expand more than one symtab, and we want to iterate over
2560 all of them. */
2561 dw2_instantiate_symtab (per_cu);
2562
2563 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2564 objfile->symtabs, last_made);
2565}
2566
2567/* Implementation of the map_symtabs_matching_filename method. */
2568
2569static int
2570dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2571 const char *full_path, const char *real_path,
2572 int (*callback) (struct symtab *, void *),
2573 void *data)
2574{
2575 int i;
2576 const char *name_basename = lbasename (name);
2577 int name_len = strlen (name);
2578 int is_abs = IS_ABSOLUTE_PATH (name);
2579
2580 dw2_setup (objfile);
2581
2582 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2583 + dwarf2_per_objfile->n_type_units); ++i)
2584 {
2585 int j;
2586 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2587 struct quick_file_names *file_data;
2588
2589 /* We only need to look at symtabs not already expanded. */
2590 if (per_cu->v.quick->symtab)
2591 continue;
2592
2593 file_data = dw2_get_file_names (objfile, per_cu);
2594 if (file_data == NULL)
2595 continue;
2596
2597 for (j = 0; j < file_data->num_file_names; ++j)
2598 {
2599 const char *this_name = file_data->file_names[j];
2600
2601 if (FILENAME_CMP (name, this_name) == 0
2602 || (!is_abs && compare_filenames_for_search (this_name,
2603 name, name_len)))
2604 {
2605 if (dw2_map_expand_apply (objfile, per_cu,
2606 name, full_path, real_path,
2607 callback, data))
2608 return 1;
2609 }
2610
2611 /* Before we invoke realpath, which can get expensive when many
2612 files are involved, do a quick comparison of the basenames. */
2613 if (! basenames_may_differ
2614 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2615 continue;
2616
2617 if (full_path != NULL)
2618 {
2619 const char *this_real_name = dw2_get_real_path (objfile,
2620 file_data, j);
2621
2622 if (this_real_name != NULL
2623 && (FILENAME_CMP (full_path, this_real_name) == 0
2624 || (!is_abs
2625 && compare_filenames_for_search (this_real_name,
2626 name, name_len))))
2627 {
2628 if (dw2_map_expand_apply (objfile, per_cu,
2629 name, full_path, real_path,
2630 callback, data))
2631 return 1;
2632 }
2633 }
2634
2635 if (real_path != NULL)
2636 {
2637 const char *this_real_name = dw2_get_real_path (objfile,
2638 file_data, j);
2639
2640 if (this_real_name != NULL
2641 && (FILENAME_CMP (real_path, this_real_name) == 0
2642 || (!is_abs
2643 && compare_filenames_for_search (this_real_name,
2644 name, name_len))))
2645 {
2646 if (dw2_map_expand_apply (objfile, per_cu,
2647 name, full_path, real_path,
2648 callback, data))
2649 return 1;
2650 }
2651 }
2652 }
2653 }
2654
2655 return 0;
2656}
2657
2658static struct symtab *
2659dw2_lookup_symbol (struct objfile *objfile, int block_index,
2660 const char *name, domain_enum domain)
2661{
2662 /* We do all the work in the pre_expand_symtabs_matching hook
2663 instead. */
2664 return NULL;
2665}
2666
2667/* A helper function that expands all symtabs that hold an object
2668 named NAME. */
2669
2670static void
2671dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2672{
2673 dw2_setup (objfile);
2674
2675 /* index_table is NULL if OBJF_READNOW. */
2676 if (dwarf2_per_objfile->index_table)
2677 {
2678 offset_type *vec;
2679
2680 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2681 name, &vec))
2682 {
2683 offset_type i, len = MAYBE_SWAP (*vec);
2684 for (i = 0; i < len; ++i)
2685 {
2686 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2687 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2688
2689 dw2_instantiate_symtab (per_cu);
2690 }
2691 }
2692 }
2693}
2694
2695static void
2696dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2697 enum block_enum block_kind, const char *name,
2698 domain_enum domain)
2699{
2700 dw2_do_expand_symtabs_matching (objfile, name);
2701}
2702
2703static void
2704dw2_print_stats (struct objfile *objfile)
2705{
2706 int i, count;
2707
2708 dw2_setup (objfile);
2709 count = 0;
2710 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2711 + dwarf2_per_objfile->n_type_units); ++i)
2712 {
2713 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2714
2715 if (!per_cu->v.quick->symtab)
2716 ++count;
2717 }
2718 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2719}
2720
2721static void
2722dw2_dump (struct objfile *objfile)
2723{
2724 /* Nothing worth printing. */
2725}
2726
2727static void
2728dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2729 struct section_offsets *delta)
2730{
2731 /* There's nothing to relocate here. */
2732}
2733
2734static void
2735dw2_expand_symtabs_for_function (struct objfile *objfile,
2736 const char *func_name)
2737{
2738 dw2_do_expand_symtabs_matching (objfile, func_name);
2739}
2740
2741static void
2742dw2_expand_all_symtabs (struct objfile *objfile)
2743{
2744 int i;
2745
2746 dw2_setup (objfile);
2747
2748 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2749 + dwarf2_per_objfile->n_type_units); ++i)
2750 {
2751 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2752
2753 dw2_instantiate_symtab (per_cu);
2754 }
2755}
2756
2757static void
2758dw2_expand_symtabs_with_filename (struct objfile *objfile,
2759 const char *filename)
2760{
2761 int i;
2762
2763 dw2_setup (objfile);
2764
2765 /* We don't need to consider type units here.
2766 This is only called for examining code, e.g. expand_line_sal.
2767 There can be an order of magnitude (or more) more type units
2768 than comp units, and we avoid them if we can. */
2769
2770 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2771 {
2772 int j;
2773 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2774 struct quick_file_names *file_data;
2775
2776 /* We only need to look at symtabs not already expanded. */
2777 if (per_cu->v.quick->symtab)
2778 continue;
2779
2780 file_data = dw2_get_file_names (objfile, per_cu);
2781 if (file_data == NULL)
2782 continue;
2783
2784 for (j = 0; j < file_data->num_file_names; ++j)
2785 {
2786 const char *this_name = file_data->file_names[j];
2787 if (FILENAME_CMP (this_name, filename) == 0)
2788 {
2789 dw2_instantiate_symtab (per_cu);
2790 break;
2791 }
2792 }
2793 }
2794}
2795
2796static const char *
2797dw2_find_symbol_file (struct objfile *objfile, const char *name)
2798{
2799 struct dwarf2_per_cu_data *per_cu;
2800 offset_type *vec;
2801 struct quick_file_names *file_data;
2802
2803 dw2_setup (objfile);
2804
2805 /* index_table is NULL if OBJF_READNOW. */
2806 if (!dwarf2_per_objfile->index_table)
2807 {
2808 struct symtab *s;
2809
2810 ALL_OBJFILE_SYMTABS (objfile, s)
2811 if (s->primary)
2812 {
2813 struct blockvector *bv = BLOCKVECTOR (s);
2814 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2815 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2816
2817 if (sym)
2818 return sym->symtab->filename;
2819 }
2820 return NULL;
2821 }
2822
2823 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2824 name, &vec))
2825 return NULL;
2826
2827 /* Note that this just looks at the very first one named NAME -- but
2828 actually we are looking for a function. find_main_filename
2829 should be rewritten so that it doesn't require a custom hook. It
2830 could just use the ordinary symbol tables. */
2831 /* vec[0] is the length, which must always be >0. */
2832 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2833
2834 file_data = dw2_get_file_names (objfile, per_cu);
2835 if (file_data == NULL)
2836 return NULL;
2837
2838 return file_data->file_names[file_data->num_file_names - 1];
2839}
2840
2841static void
2842dw2_map_matching_symbols (const char * name, domain_enum namespace,
2843 struct objfile *objfile, int global,
2844 int (*callback) (struct block *,
2845 struct symbol *, void *),
2846 void *data, symbol_compare_ftype *match,
2847 symbol_compare_ftype *ordered_compare)
2848{
2849 /* Currently unimplemented; used for Ada. The function can be called if the
2850 current language is Ada for a non-Ada objfile using GNU index. As Ada
2851 does not look for non-Ada symbols this function should just return. */
2852}
2853
2854static void
2855dw2_expand_symtabs_matching
2856 (struct objfile *objfile,
2857 int (*file_matcher) (const char *, void *),
2858 int (*name_matcher) (const char *, void *),
2859 enum search_domain kind,
2860 void *data)
2861{
2862 int i;
2863 offset_type iter;
2864 struct mapped_index *index;
2865
2866 dw2_setup (objfile);
2867
2868 /* index_table is NULL if OBJF_READNOW. */
2869 if (!dwarf2_per_objfile->index_table)
2870 return;
2871 index = dwarf2_per_objfile->index_table;
2872
2873 if (file_matcher != NULL)
2874 {
2875 struct cleanup *cleanup;
2876 htab_t visited_found, visited_not_found;
2877
2878 visited_found = htab_create_alloc (10,
2879 htab_hash_pointer, htab_eq_pointer,
2880 NULL, xcalloc, xfree);
2881 cleanup = make_cleanup_htab_delete (visited_found);
2882 visited_not_found = htab_create_alloc (10,
2883 htab_hash_pointer, htab_eq_pointer,
2884 NULL, xcalloc, xfree);
2885 make_cleanup_htab_delete (visited_not_found);
2886
2887 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2888 + dwarf2_per_objfile->n_type_units); ++i)
2889 {
2890 int j;
2891 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2892 struct quick_file_names *file_data;
2893 void **slot;
2894
2895 per_cu->v.quick->mark = 0;
2896
2897 /* We only need to look at symtabs not already expanded. */
2898 if (per_cu->v.quick->symtab)
2899 continue;
2900
2901 file_data = dw2_get_file_names (objfile, per_cu);
2902 if (file_data == NULL)
2903 continue;
2904
2905 if (htab_find (visited_not_found, file_data) != NULL)
2906 continue;
2907 else if (htab_find (visited_found, file_data) != NULL)
2908 {
2909 per_cu->v.quick->mark = 1;
2910 continue;
2911 }
2912
2913 for (j = 0; j < file_data->num_file_names; ++j)
2914 {
2915 if (file_matcher (file_data->file_names[j], data))
2916 {
2917 per_cu->v.quick->mark = 1;
2918 break;
2919 }
2920 }
2921
2922 slot = htab_find_slot (per_cu->v.quick->mark
2923 ? visited_found
2924 : visited_not_found,
2925 file_data, INSERT);
2926 *slot = file_data;
2927 }
2928
2929 do_cleanups (cleanup);
2930 }
2931
2932 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2933 {
2934 offset_type idx = 2 * iter;
2935 const char *name;
2936 offset_type *vec, vec_len, vec_idx;
2937
2938 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2939 continue;
2940
2941 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2942
2943 if (! (*name_matcher) (name, data))
2944 continue;
2945
2946 /* The name was matched, now expand corresponding CUs that were
2947 marked. */
2948 vec = (offset_type *) (index->constant_pool
2949 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2950 vec_len = MAYBE_SWAP (vec[0]);
2951 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2952 {
2953 struct dwarf2_per_cu_data *per_cu;
2954
2955 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2956 if (file_matcher == NULL || per_cu->v.quick->mark)
2957 dw2_instantiate_symtab (per_cu);
2958 }
2959 }
2960}
2961
2962static struct symtab *
2963dw2_find_pc_sect_symtab (struct objfile *objfile,
2964 struct minimal_symbol *msymbol,
2965 CORE_ADDR pc,
2966 struct obj_section *section,
2967 int warn_if_readin)
2968{
2969 struct dwarf2_per_cu_data *data;
2970
2971 dw2_setup (objfile);
2972
2973 if (!objfile->psymtabs_addrmap)
2974 return NULL;
2975
2976 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2977 if (!data)
2978 return NULL;
2979
2980 if (warn_if_readin && data->v.quick->symtab)
2981 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2982 paddress (get_objfile_arch (objfile), pc));
2983
2984 return dw2_instantiate_symtab (data);
2985}
2986
2987static void
2988dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2989 void *data, int need_fullname)
2990{
2991 int i;
2992 struct cleanup *cleanup;
2993 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2994 NULL, xcalloc, xfree);
2995
2996 cleanup = make_cleanup_htab_delete (visited);
2997 dw2_setup (objfile);
2998
2999 /* We can ignore file names coming from already-expanded CUs. */
3000 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3001 + dwarf2_per_objfile->n_type_units); ++i)
3002 {
3003 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3004
3005 if (per_cu->v.quick->symtab)
3006 {
3007 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3008 INSERT);
3009
3010 *slot = per_cu->v.quick->file_names;
3011 }
3012 }
3013
3014 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3015 + dwarf2_per_objfile->n_type_units); ++i)
3016 {
3017 int j;
3018 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3019 struct quick_file_names *file_data;
3020 void **slot;
3021
3022 /* We only need to look at symtabs not already expanded. */
3023 if (per_cu->v.quick->symtab)
3024 continue;
3025
3026 file_data = dw2_get_file_names (objfile, per_cu);
3027 if (file_data == NULL)
3028 continue;
3029
3030 slot = htab_find_slot (visited, file_data, INSERT);
3031 if (*slot)
3032 {
3033 /* Already visited. */
3034 continue;
3035 }
3036 *slot = file_data;
3037
3038 for (j = 0; j < file_data->num_file_names; ++j)
3039 {
3040 const char *this_real_name;
3041
3042 if (need_fullname)
3043 this_real_name = dw2_get_real_path (objfile, file_data, j);
3044 else
3045 this_real_name = NULL;
3046 (*fun) (file_data->file_names[j], this_real_name, data);
3047 }
3048 }
3049
3050 do_cleanups (cleanup);
3051}
3052
3053static int
3054dw2_has_symbols (struct objfile *objfile)
3055{
3056 return 1;
3057}
3058
3059const struct quick_symbol_functions dwarf2_gdb_index_functions =
3060{
3061 dw2_has_symbols,
3062 dw2_find_last_source_symtab,
3063 dw2_forget_cached_source_info,
3064 dw2_map_symtabs_matching_filename,
3065 dw2_lookup_symbol,
3066 dw2_pre_expand_symtabs_matching,
3067 dw2_print_stats,
3068 dw2_dump,
3069 dw2_relocate,
3070 dw2_expand_symtabs_for_function,
3071 dw2_expand_all_symtabs,
3072 dw2_expand_symtabs_with_filename,
3073 dw2_find_symbol_file,
3074 dw2_map_matching_symbols,
3075 dw2_expand_symtabs_matching,
3076 dw2_find_pc_sect_symtab,
3077 dw2_map_symbol_filenames
3078};
3079
3080/* Initialize for reading DWARF for this objfile. Return 0 if this
3081 file will use psymtabs, or 1 if using the GNU index. */
3082
3083int
3084dwarf2_initialize_objfile (struct objfile *objfile)
3085{
3086 /* If we're about to read full symbols, don't bother with the
3087 indices. In this case we also don't care if some other debug
3088 format is making psymtabs, because they are all about to be
3089 expanded anyway. */
3090 if ((objfile->flags & OBJF_READNOW))
3091 {
3092 int i;
3093
3094 dwarf2_per_objfile->using_index = 1;
3095 create_all_comp_units (objfile);
3096 create_all_type_units (objfile);
3097 dwarf2_per_objfile->quick_file_names_table =
3098 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3099
3100 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3101 + dwarf2_per_objfile->n_type_units); ++i)
3102 {
3103 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3104
3105 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3106 struct dwarf2_per_cu_quick_data);
3107 }
3108
3109 /* Return 1 so that gdb sees the "quick" functions. However,
3110 these functions will be no-ops because we will have expanded
3111 all symtabs. */
3112 return 1;
3113 }
3114
3115 if (dwarf2_read_index (objfile))
3116 return 1;
3117
3118 return 0;
3119}
3120
3121\f
3122
3123/* Build a partial symbol table. */
3124
3125void
3126dwarf2_build_psymtabs (struct objfile *objfile)
3127{
3128 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3129 {
3130 init_psymbol_list (objfile, 1024);
3131 }
3132
3133 dwarf2_build_psymtabs_hard (objfile);
3134}
3135
3136/* Return TRUE if OFFSET is within CU_HEADER. */
3137
3138static inline int
3139offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3140{
3141 sect_offset bottom = { cu_header->offset.sect_off };
3142 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3143 + cu_header->initial_length_size) };
3144
3145 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3146}
3147
3148/* Read in the comp unit header information from the debug_info at info_ptr.
3149 NOTE: This leaves members offset, first_die_offset to be filled in
3150 by the caller. */
3151
3152static gdb_byte *
3153read_comp_unit_head (struct comp_unit_head *cu_header,
3154 gdb_byte *info_ptr, bfd *abfd)
3155{
3156 int signed_addr;
3157 unsigned int bytes_read;
3158
3159 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3160 cu_header->initial_length_size = bytes_read;
3161 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3162 info_ptr += bytes_read;
3163 cu_header->version = read_2_bytes (abfd, info_ptr);
3164 info_ptr += 2;
3165 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3166 &bytes_read);
3167 info_ptr += bytes_read;
3168 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3169 info_ptr += 1;
3170 signed_addr = bfd_get_sign_extend_vma (abfd);
3171 if (signed_addr < 0)
3172 internal_error (__FILE__, __LINE__,
3173 _("read_comp_unit_head: dwarf from non elf file"));
3174 cu_header->signed_addr_p = signed_addr;
3175
3176 return info_ptr;
3177}
3178
3179/* Subroutine of read_and_check_comp_unit_head and
3180 read_and_check_type_unit_head to simplify them.
3181 Perform various error checking on the header. */
3182
3183static void
3184error_check_comp_unit_head (struct comp_unit_head *header,
3185 struct dwarf2_section_info *section)
3186{
3187 bfd *abfd = section->asection->owner;
3188 const char *filename = bfd_get_filename (abfd);
3189
3190 if (header->version != 2 && header->version != 3 && header->version != 4)
3191 error (_("Dwarf Error: wrong version in compilation unit header "
3192 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3193 filename);
3194
3195 if (header->abbrev_offset.sect_off
3196 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3197 &dwarf2_per_objfile->abbrev))
3198 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3199 "(offset 0x%lx + 6) [in module %s]"),
3200 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3201 filename);
3202
3203 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3204 avoid potential 32-bit overflow. */
3205 if (((unsigned long) header->offset.sect_off
3206 + header->length + header->initial_length_size)
3207 > section->size)
3208 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3209 "(offset 0x%lx + 0) [in module %s]"),
3210 (long) header->length, (long) header->offset.sect_off,
3211 filename);
3212}
3213
3214/* Read in a CU/TU header and perform some basic error checking.
3215 The contents of the header are stored in HEADER.
3216 The result is a pointer to the start of the first DIE. */
3217
3218static gdb_byte *
3219read_and_check_comp_unit_head (struct comp_unit_head *header,
3220 struct dwarf2_section_info *section,
3221 gdb_byte *info_ptr,
3222 int is_debug_types_section)
3223{
3224 gdb_byte *beg_of_comp_unit = info_ptr;
3225 bfd *abfd = section->asection->owner;
3226
3227 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3228
3229 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3230
3231 /* If we're reading a type unit, skip over the signature and
3232 type_offset fields. */
3233 if (is_debug_types_section)
3234 info_ptr += 8 /*signature*/ + header->offset_size;
3235
3236 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3237
3238 error_check_comp_unit_head (header, section);
3239
3240 return info_ptr;
3241}
3242
3243/* Read in the types comp unit header information from .debug_types entry at
3244 types_ptr. The result is a pointer to one past the end of the header. */
3245
3246static gdb_byte *
3247read_and_check_type_unit_head (struct comp_unit_head *header,
3248 struct dwarf2_section_info *section,
3249 gdb_byte *info_ptr,
3250 ULONGEST *signature,
3251 cu_offset *type_offset_in_tu)
3252{
3253 gdb_byte *beg_of_comp_unit = info_ptr;
3254 bfd *abfd = section->asection->owner;
3255
3256 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3257
3258 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3259
3260 /* If we're reading a type unit, skip over the signature and
3261 type_offset fields. */
3262 if (signature != NULL)
3263 *signature = read_8_bytes (abfd, info_ptr);
3264 info_ptr += 8;
3265 if (type_offset_in_tu != NULL)
3266 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3267 header->offset_size);
3268 info_ptr += header->offset_size;
3269
3270 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3271
3272 error_check_comp_unit_head (header, section);
3273
3274 return info_ptr;
3275}
3276
3277/* Allocate a new partial symtab for file named NAME and mark this new
3278 partial symtab as being an include of PST. */
3279
3280static void
3281dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3282 struct objfile *objfile)
3283{
3284 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3285
3286 subpst->section_offsets = pst->section_offsets;
3287 subpst->textlow = 0;
3288 subpst->texthigh = 0;
3289
3290 subpst->dependencies = (struct partial_symtab **)
3291 obstack_alloc (&objfile->objfile_obstack,
3292 sizeof (struct partial_symtab *));
3293 subpst->dependencies[0] = pst;
3294 subpst->number_of_dependencies = 1;
3295
3296 subpst->globals_offset = 0;
3297 subpst->n_global_syms = 0;
3298 subpst->statics_offset = 0;
3299 subpst->n_static_syms = 0;
3300 subpst->symtab = NULL;
3301 subpst->read_symtab = pst->read_symtab;
3302 subpst->readin = 0;
3303
3304 /* No private part is necessary for include psymtabs. This property
3305 can be used to differentiate between such include psymtabs and
3306 the regular ones. */
3307 subpst->read_symtab_private = NULL;
3308}
3309
3310/* Read the Line Number Program data and extract the list of files
3311 included by the source file represented by PST. Build an include
3312 partial symtab for each of these included files. */
3313
3314static void
3315dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3316 struct die_info *die,
3317 struct partial_symtab *pst)
3318{
3319 struct line_header *lh = NULL;
3320 struct attribute *attr;
3321
3322 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3323 if (attr)
3324 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3325 if (lh == NULL)
3326 return; /* No linetable, so no includes. */
3327
3328 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3329 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3330
3331 free_line_header (lh);
3332}
3333
3334static hashval_t
3335hash_signatured_type (const void *item)
3336{
3337 const struct signatured_type *sig_type = item;
3338
3339 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3340 return sig_type->signature;
3341}
3342
3343static int
3344eq_signatured_type (const void *item_lhs, const void *item_rhs)
3345{
3346 const struct signatured_type *lhs = item_lhs;
3347 const struct signatured_type *rhs = item_rhs;
3348
3349 return lhs->signature == rhs->signature;
3350}
3351
3352/* Allocate a hash table for signatured types. */
3353
3354static htab_t
3355allocate_signatured_type_table (struct objfile *objfile)
3356{
3357 return htab_create_alloc_ex (41,
3358 hash_signatured_type,
3359 eq_signatured_type,
3360 NULL,
3361 &objfile->objfile_obstack,
3362 hashtab_obstack_allocate,
3363 dummy_obstack_deallocate);
3364}
3365
3366/* A helper function to add a signatured type CU to a table. */
3367
3368static int
3369add_signatured_type_cu_to_table (void **slot, void *datum)
3370{
3371 struct signatured_type *sigt = *slot;
3372 struct dwarf2_per_cu_data ***datap = datum;
3373
3374 **datap = &sigt->per_cu;
3375 ++*datap;
3376
3377 return 1;
3378}
3379
3380/* Create the hash table of all entries in the .debug_types section.
3381 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3382 The result is a pointer to the hash table or NULL if there are
3383 no types. */
3384
3385static htab_t
3386create_debug_types_hash_table (struct dwo_file *dwo_file,
3387 VEC (dwarf2_section_info_def) *types)
3388{
3389 struct objfile *objfile = dwarf2_per_objfile->objfile;
3390 htab_t types_htab = NULL;
3391 int ix;
3392 struct dwarf2_section_info *section;
3393
3394 if (VEC_empty (dwarf2_section_info_def, types))
3395 return NULL;
3396
3397 for (ix = 0;
3398 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3399 ++ix)
3400 {
3401 bfd *abfd;
3402 gdb_byte *info_ptr, *end_ptr;
3403
3404 dwarf2_read_section (objfile, section);
3405 info_ptr = section->buffer;
3406
3407 if (info_ptr == NULL)
3408 continue;
3409
3410 /* We can't set abfd until now because the section may be empty or
3411 not present, in which case section->asection will be NULL. */
3412 abfd = section->asection->owner;
3413
3414 if (types_htab == NULL)
3415 {
3416 if (dwo_file)
3417 types_htab = allocate_dwo_unit_table (objfile);
3418 else
3419 types_htab = allocate_signatured_type_table (objfile);
3420 }
3421
3422 if (dwarf2_die_debug)
3423 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3424 bfd_get_filename (abfd));
3425
3426 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3427 because we don't need to read any dies: the signature is in the
3428 header. */
3429
3430 end_ptr = info_ptr + section->size;
3431 while (info_ptr < end_ptr)
3432 {
3433 sect_offset offset;
3434 cu_offset type_offset_in_tu;
3435 ULONGEST signature;
3436 struct signatured_type *sig_type;
3437 struct dwo_unit *dwo_tu;
3438 void **slot;
3439 gdb_byte *ptr = info_ptr;
3440 struct comp_unit_head header;
3441 unsigned int length;
3442
3443 offset.sect_off = ptr - section->buffer;
3444
3445 /* We need to read the type's signature in order to build the hash
3446 table, but we don't need anything else just yet. */
3447
3448 ptr = read_and_check_type_unit_head (&header, section, ptr,
3449 &signature, &type_offset_in_tu);
3450
3451 length = header.initial_length_size + header.length;
3452
3453 /* Skip dummy type units. */
3454 if (ptr >= info_ptr + length
3455 || peek_abbrev_code (abfd, ptr) == 0)
3456 {
3457 info_ptr += header.initial_length_size + header.length;
3458 continue;
3459 }
3460
3461 if (dwo_file)
3462 {
3463 sig_type = NULL;
3464 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3465 struct dwo_unit);
3466 dwo_tu->dwo_file = dwo_file;
3467 dwo_tu->signature = signature;
3468 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3469 dwo_tu->info_or_types_section = section;
3470 dwo_tu->offset = offset;
3471 dwo_tu->length = length;
3472 }
3473 else
3474 {
3475 /* N.B.: type_offset is not usable if this type uses a DWO file.
3476 The real type_offset is in the DWO file. */
3477 dwo_tu = NULL;
3478 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3479 struct signatured_type);
3480 sig_type->signature = signature;
3481 sig_type->type_offset_in_tu = type_offset_in_tu;
3482 sig_type->per_cu.objfile = objfile;
3483 sig_type->per_cu.is_debug_types = 1;
3484 sig_type->per_cu.info_or_types_section = section;
3485 sig_type->per_cu.offset = offset;
3486 sig_type->per_cu.length = length;
3487 }
3488
3489 slot = htab_find_slot (types_htab,
3490 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3491 INSERT);
3492 gdb_assert (slot != NULL);
3493 if (*slot != NULL)
3494 {
3495 sect_offset dup_offset;
3496
3497 if (dwo_file)
3498 {
3499 const struct dwo_unit *dup_tu = *slot;
3500
3501 dup_offset = dup_tu->offset;
3502 }
3503 else
3504 {
3505 const struct signatured_type *dup_tu = *slot;
3506
3507 dup_offset = dup_tu->per_cu.offset;
3508 }
3509
3510 complaint (&symfile_complaints,
3511 _("debug type entry at offset 0x%x is duplicate to the "
3512 "entry at offset 0x%x, signature 0x%s"),
3513 offset.sect_off, dup_offset.sect_off,
3514 phex (signature, sizeof (signature)));
3515 }
3516 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3517
3518 if (dwarf2_die_debug)
3519 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3520 offset.sect_off,
3521 phex (signature, sizeof (signature)));
3522
3523 info_ptr += length;
3524 }
3525 }
3526
3527 return types_htab;
3528}
3529
3530/* Create the hash table of all entries in the .debug_types section,
3531 and initialize all_type_units.
3532 The result is zero if there is an error (e.g. missing .debug_types section),
3533 otherwise non-zero. */
3534
3535static int
3536create_all_type_units (struct objfile *objfile)
3537{
3538 htab_t types_htab;
3539 struct dwarf2_per_cu_data **iter;
3540
3541 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3542 if (types_htab == NULL)
3543 {
3544 dwarf2_per_objfile->signatured_types = NULL;
3545 return 0;
3546 }
3547
3548 dwarf2_per_objfile->signatured_types = types_htab;
3549
3550 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3551 dwarf2_per_objfile->all_type_units
3552 = obstack_alloc (&objfile->objfile_obstack,
3553 dwarf2_per_objfile->n_type_units
3554 * sizeof (struct dwarf2_per_cu_data *));
3555 iter = &dwarf2_per_objfile->all_type_units[0];
3556 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3557 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3558 == dwarf2_per_objfile->n_type_units);
3559
3560 return 1;
3561}
3562
3563/* Lookup a signature based type for DW_FORM_ref_sig8.
3564 Returns NULL if signature SIG is not present in the table. */
3565
3566static struct signatured_type *
3567lookup_signatured_type (ULONGEST sig)
3568{
3569 struct signatured_type find_entry, *entry;
3570
3571 if (dwarf2_per_objfile->signatured_types == NULL)
3572 {
3573 complaint (&symfile_complaints,
3574 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3575 return NULL;
3576 }
3577
3578 find_entry.signature = sig;
3579 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3580 return entry;
3581}
3582
3583/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3584
3585static void
3586init_cu_die_reader (struct die_reader_specs *reader,
3587 struct dwarf2_cu *cu,
3588 struct dwarf2_section_info *section,
3589 struct dwo_file *dwo_file)
3590{
3591 gdb_assert (section->readin);
3592 reader->abfd = section->asection->owner;
3593 reader->cu = cu;
3594 reader->dwo_file = dwo_file;
3595 reader->die_section = section;
3596 reader->buffer = section->buffer;
3597}
3598
3599/* Find the base address of the compilation unit for range lists and
3600 location lists. It will normally be specified by DW_AT_low_pc.
3601 In DWARF-3 draft 4, the base address could be overridden by
3602 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3603 compilation units with discontinuous ranges. */
3604
3605static void
3606dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3607{
3608 struct attribute *attr;
3609
3610 cu->base_known = 0;
3611 cu->base_address = 0;
3612
3613 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3614 if (attr)
3615 {
3616 cu->base_address = DW_ADDR (attr);
3617 cu->base_known = 1;
3618 }
3619 else
3620 {
3621 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3622 if (attr)
3623 {
3624 cu->base_address = DW_ADDR (attr);
3625 cu->base_known = 1;
3626 }
3627 }
3628}
3629
3630/* Subroutine of init_{cu,tu}_and_read_dies.
3631 Do all the work necessary to initialize THIS_CU->cu and read in its DIE(s).
3632 If the CU defers to a DWO file, read the DWO file as well.
3633
3634 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3635 Otherwise, a new CU is allocated with xmalloc.
3636
3637 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3638 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3639
3640 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3641 linker) then DIE_READER_FUNC will not get called.
3642
3643 FIXME: As an implementation detail between our callers and us,
3644 USE_EXISTING_CU and KEEP are OK. But bubbling them up into their callers
3645 isn't as clean as I'd like. Having more callers with good names
3646 may be the way to go. */
3647
3648static void
3649init_and_read_dies_worker (struct dwarf2_per_cu_data *this_cu,
3650 int use_existing_cu, int keep,
3651 die_reader_func_ftype *die_reader_func,
3652 void *data)
3653{
3654 struct objfile *objfile = dwarf2_per_objfile->objfile;
3655 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3656 bfd *abfd = section->asection->owner;
3657 struct dwarf2_cu *cu;
3658 gdb_byte *begin_info_ptr, *info_ptr;
3659 struct die_reader_specs reader;
3660 struct die_info *comp_unit_die;
3661 int has_children;
3662 struct attribute *attr;
3663 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3664 struct signatured_type *sig_type = NULL;
3665
3666 if (use_existing_cu)
3667 gdb_assert (keep);
3668 if (this_cu->is_debug_types)
3669 gdb_assert (! use_existing_cu);
3670
3671 cleanups = make_cleanup (null_cleanup, NULL);
3672
3673 /* This is cheap if the section is already read in. */
3674 dwarf2_read_section (objfile, section);
3675
3676 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3677
3678 if (use_existing_cu && this_cu->cu != NULL)
3679 {
3680 cu = this_cu->cu;
3681 info_ptr += cu->header.first_die_offset.cu_off;
3682 }
3683 else
3684 {
3685 /* If !use_existing_cu, this_cu->cu must be NULL. */
3686 gdb_assert (this_cu->cu == NULL);
3687
3688 cu = xmalloc (sizeof (*cu));
3689 init_one_comp_unit (cu, this_cu);
3690
3691 /* If an error occurs while loading, release our storage. */
3692 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3693
3694 if (this_cu->is_debug_types)
3695 {
3696 ULONGEST signature;
3697
3698 info_ptr = read_and_check_type_unit_head (&cu->header,
3699 section, info_ptr,
3700 &signature, NULL);
3701
3702 /* There's no way to get from PER_CU to its containing
3703 struct signatured_type.
3704 But we have the signature so we can use that. */
3705 sig_type = lookup_signatured_type (signature);
3706 /* We've already scanned all the signatured types,
3707 this must succeed. */
3708 gdb_assert (sig_type != NULL);
3709 gdb_assert (&sig_type->per_cu == this_cu);
3710 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3711
3712 /* LENGTH has not been set yet for type units. */
3713 this_cu->length = cu->header.length + cu->header.initial_length_size;
3714
3715 /* Establish the type offset that can be used to lookup the type. */
3716 sig_type->type_offset_in_section.sect_off =
3717 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3718 }
3719 else
3720 {
3721 info_ptr = read_and_check_comp_unit_head (&cu->header,
3722 section, info_ptr, 0);
3723
3724 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3725 gdb_assert (this_cu->length
3726 == cu->header.length + cu->header.initial_length_size);
3727 }
3728 }
3729
3730 /* Skip dummy compilation units. */
3731 if (info_ptr >= begin_info_ptr + this_cu->length
3732 || peek_abbrev_code (abfd, info_ptr) == 0)
3733 {
3734 do_cleanups (cleanups);
3735 return;
3736 }
3737
3738 /* Read the abbrevs for this compilation unit into a table. */
3739 if (cu->dwarf2_abbrevs == NULL)
3740 {
3741 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3742 make_cleanup (dwarf2_free_abbrev_table, cu);
3743 }
3744
3745 /* Read the top level CU/TU die. */
3746 init_cu_die_reader (&reader, cu, section, NULL);
3747 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3748
3749 /* If we have a DWO stub, process it and then read in the DWO file.
3750 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3751 a DWO CU, that this test will fail. */
3752 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3753 if (attr)
3754 {
3755 char *dwo_name = DW_STRING (attr);
3756 const char *comp_dir;
3757 struct dwo_unit *dwo_unit;
3758 ULONGEST signature; /* Or dwo_id. */
3759 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3760 int i,num_extra_attrs;
3761
3762 if (has_children)
3763 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3764 " has children (offset 0x%x) [in module %s]"),
3765 this_cu->offset.sect_off, bfd_get_filename (abfd));
3766
3767 /* These attributes aren't processed until later:
3768 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3769 However, the attribute is found in the stub which we won't have later.
3770 In order to not impose this complication on the rest of the code,
3771 we read them here and copy them to the DWO CU/TU die. */
3772 stmt_list = low_pc = high_pc = ranges = NULL;
3773
3774 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3775 DWO file. */
3776 if (! this_cu->is_debug_types)
3777 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3778 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3779 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3780 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3781
3782 /* There should be a DW_AT_addr_base attribute here (if needed).
3783 We need the value before we can process DW_FORM_GNU_addr_index. */
3784 cu->addr_base = 0;
3785 cu->have_addr_base = 0;
3786 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3787 if (attr)
3788 {
3789 cu->addr_base = DW_UNSND (attr);
3790 cu->have_addr_base = 1;
3791 }
3792
3793 if (this_cu->is_debug_types)
3794 {
3795 gdb_assert (sig_type != NULL);
3796 signature = sig_type->signature;
3797 }
3798 else
3799 {
3800 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3801 if (! attr)
3802 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3803 dwo_name);
3804 signature = DW_UNSND (attr);
3805 }
3806
3807 /* We may need the comp_dir in order to find the DWO file. */
3808 comp_dir = NULL;
3809 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3810 if (attr)
3811 comp_dir = DW_STRING (attr);
3812
3813 if (this_cu->is_debug_types)
3814 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3815 else
3816 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3817 signature);
3818
3819 if (dwo_unit == NULL)
3820 {
3821 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3822 " with ID %s [in module %s]"),
3823 this_cu->offset.sect_off,
3824 phex (signature, sizeof (signature)),
3825 objfile->name);
3826 }
3827
3828 /* Set up for reading the DWO CU/TU. */
3829 cu->dwo_unit = dwo_unit;
3830 section = dwo_unit->info_or_types_section;
3831 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3832 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3833
3834 if (this_cu->is_debug_types)
3835 {
3836 ULONGEST signature;
3837
3838 info_ptr = read_and_check_type_unit_head (&cu->header,
3839 section, info_ptr,
3840 &signature, NULL);
3841 gdb_assert (sig_type->signature == signature);
3842 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3843 gdb_assert (dwo_unit->length
3844 == cu->header.length + cu->header.initial_length_size);
3845
3846 /* Establish the type offset that can be used to lookup the type.
3847 For DWO files, we don't know it until now. */
3848 sig_type->type_offset_in_section.sect_off =
3849 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3850 }
3851 else
3852 {
3853 info_ptr = read_and_check_comp_unit_head (&cu->header,
3854 section, info_ptr, 0);
3855 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3856 gdb_assert (dwo_unit->length
3857 == cu->header.length + cu->header.initial_length_size);
3858 }
3859
3860 /* Discard the original CU's abbrev table, and read the DWO's. */
3861 dwarf2_free_abbrev_table (cu);
3862 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3863
3864 /* Read in the die, but leave space to copy over the attributes
3865 from the stub. This has the benefit of simplifying the rest of
3866 the code - all the real work is done here. */
3867 num_extra_attrs = ((stmt_list != NULL)
3868 + (low_pc != NULL)
3869 + (high_pc != NULL)
3870 + (ranges != NULL));
3871 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3872 &has_children, num_extra_attrs);
3873
3874 /* Copy over the attributes from the stub to the DWO die. */
3875 i = comp_unit_die->num_attrs;
3876 if (stmt_list != NULL)
3877 comp_unit_die->attrs[i++] = *stmt_list;
3878 if (low_pc != NULL)
3879 comp_unit_die->attrs[i++] = *low_pc;
3880 if (high_pc != NULL)
3881 comp_unit_die->attrs[i++] = *high_pc;
3882 if (ranges != NULL)
3883 comp_unit_die->attrs[i++] = *ranges;
3884 comp_unit_die->num_attrs += num_extra_attrs;
3885
3886 /* Skip dummy compilation units. */
3887 if (info_ptr >= begin_info_ptr + dwo_unit->length
3888 || peek_abbrev_code (abfd, info_ptr) == 0)
3889 {
3890 do_cleanups (cleanups);
3891 return;
3892 }
3893 }
3894
3895 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3896
3897 if (free_cu_cleanup != NULL)
3898 {
3899 if (keep)
3900 {
3901 /* We've successfully allocated this compilation unit. Let our
3902 caller clean it up when finished with it. */
3903 discard_cleanups (free_cu_cleanup);
3904
3905 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3906 So we have to manually free the abbrev table. */
3907 dwarf2_free_abbrev_table (cu);
3908
3909 /* Link this CU into read_in_chain. */
3910 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3911 dwarf2_per_objfile->read_in_chain = this_cu;
3912 }
3913 else
3914 do_cleanups (free_cu_cleanup);
3915 }
3916
3917 do_cleanups (cleanups);
3918}
3919
3920/* Main entry point for reading a CU.
3921 Do all the work necessary to initialize THIS_CU->cu and read in its DIE(s).
3922 If the CU defers to a DWO file, read the DWO file as well.
3923
3924 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3925 Otherwise, a new CU is allocated with xmalloc.
3926
3927 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3928 read_in_chain. Otherwise the dwarf2_cu data is freed at the end. */
3929
3930static void
3931init_cu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3932 int use_existing_cu, int keep,
3933 die_reader_func_ftype *die_reader_func,
3934 void *data)
3935{
3936 init_and_read_dies_worker (this_cu, use_existing_cu, keep,
3937 die_reader_func, data);
3938}
3939
3940/* Main entry point for reading a TU.
3941 Do all the work necessary to initialize THIS_CU->cu and read in its DIE(s).
3942 If the TU defers to a DWO file, read the DWO file as well.
3943
3944 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3945 read_in_chain. Otherwise the dwarf2_cu data is freed at the end. */
3946
3947static void
3948init_tu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3949 int keep,
3950 die_reader_func_ftype *die_reader_func,
3951 void *data)
3952{
3953 gdb_assert (this_cu->is_debug_types);
3954 init_and_read_dies_worker (this_cu, 0, keep, die_reader_func, data);
3955}
3956
3957/* Read CU/TU THIS_CU in section SECTION,
3958 but do not follow DW_AT_GNU_dwo_name if present.
3959 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3960 have already done the lookup to find the DWO file).
3961
3962 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3963 THIS_CU->is_debug_types, but nothing else.
3964
3965 We fill in THIS_CU->length.
3966
3967 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3968 linker) then DIE_READER_FUNC will not get called.
3969
3970 THIS_CU->cu is always freed when done.
3971 This is done in order to not leave THIS_CU->cu in a state where we have
3972 to care whether it refers to the "main" CU or the DWO CU. */
3973
3974static void
3975init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3976 struct dwarf2_section_info *abbrev_section,
3977 struct dwo_file *dwo_file,
3978 die_reader_func_ftype *die_reader_func,
3979 void *data)
3980{
3981 struct objfile *objfile = dwarf2_per_objfile->objfile;
3982 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3983 bfd *abfd = section->asection->owner;
3984 struct dwarf2_cu cu;
3985 gdb_byte *begin_info_ptr, *info_ptr;
3986 struct die_reader_specs reader;
3987 struct cleanup *cleanups;
3988 struct die_info *comp_unit_die;
3989 int has_children;
3990
3991 gdb_assert (this_cu->cu == NULL);
3992
3993 /* This is cheap if the section is already read in. */
3994 dwarf2_read_section (objfile, section);
3995
3996 init_one_comp_unit (&cu, this_cu);
3997
3998 cleanups = make_cleanup (free_stack_comp_unit, &cu);
3999
4000 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4001 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
4002 this_cu->is_debug_types);
4003
4004 this_cu->length = cu.header.length + cu.header.initial_length_size;
4005
4006 /* Skip dummy compilation units. */
4007 if (info_ptr >= begin_info_ptr + this_cu->length
4008 || peek_abbrev_code (abfd, info_ptr) == 0)
4009 {
4010 do_cleanups (cleanups);
4011 return;
4012 }
4013
4014 dwarf2_read_abbrevs (&cu, abbrev_section);
4015 make_cleanup (dwarf2_free_abbrev_table, &cu);
4016
4017 init_cu_die_reader (&reader, &cu, section, dwo_file);
4018 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4019
4020 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4021
4022 do_cleanups (cleanups);
4023}
4024
4025/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4026 does not lookup the specified DWO file.
4027 This cannot be used to read DWO files.
4028
4029 THIS_CU->cu is always freed when done.
4030 This is done in order to not leave THIS_CU->cu in a state where we have
4031 to care whether it refers to the "main" CU or the DWO CU.
4032 We can revisit this if the data shows there's a performance issue. */
4033
4034static void
4035init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4036 die_reader_func_ftype *die_reader_func,
4037 void *data)
4038{
4039 init_cutu_and_read_dies_no_follow (this_cu,
4040 &dwarf2_per_objfile->abbrev,
4041 NULL,
4042 die_reader_func, data);
4043}
4044
4045/* die_reader_func for process_psymtab_comp_unit. */
4046
4047static void
4048process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4049 gdb_byte *info_ptr,
4050 struct die_info *comp_unit_die,
4051 int has_children,
4052 void *data)
4053{
4054 struct dwarf2_cu *cu = reader->cu;
4055 struct objfile *objfile = cu->objfile;
4056 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4057 bfd *abfd = objfile->obfd;
4058 struct attribute *attr;
4059 CORE_ADDR baseaddr;
4060 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4061 struct partial_symtab *pst;
4062 int has_pc_info;
4063 const char *filename;
4064
4065 if (comp_unit_die->tag == DW_TAG_partial_unit)
4066 return;
4067
4068 prepare_one_comp_unit (cu, comp_unit_die);
4069
4070 cu->list_in_scope = &file_symbols;
4071
4072 /* Allocate a new partial symbol table structure. */
4073 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4074 if (attr == NULL || !DW_STRING (attr))
4075 filename = "";
4076 else
4077 filename = DW_STRING (attr);
4078 pst = start_psymtab_common (objfile, objfile->section_offsets,
4079 filename,
4080 /* TEXTLOW and TEXTHIGH are set below. */
4081 0,
4082 objfile->global_psymbols.next,
4083 objfile->static_psymbols.next);
4084 pst->psymtabs_addrmap_supported = 1;
4085
4086 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4087 if (attr != NULL)
4088 pst->dirname = DW_STRING (attr);
4089
4090 pst->read_symtab_private = per_cu;
4091
4092 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4093
4094 /* Store the function that reads in the rest of the symbol table. */
4095 pst->read_symtab = dwarf2_psymtab_to_symtab;
4096
4097 per_cu->v.psymtab = pst;
4098
4099 dwarf2_find_base_address (comp_unit_die, cu);
4100
4101 /* Possibly set the default values of LOWPC and HIGHPC from
4102 `DW_AT_ranges'. */
4103 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4104 &best_highpc, cu, pst);
4105 if (has_pc_info == 1 && best_lowpc < best_highpc)
4106 /* Store the contiguous range if it is not empty; it can be empty for
4107 CUs with no code. */
4108 addrmap_set_empty (objfile->psymtabs_addrmap,
4109 best_lowpc + baseaddr,
4110 best_highpc + baseaddr - 1, pst);
4111
4112 /* Check if comp unit has_children.
4113 If so, read the rest of the partial symbols from this comp unit.
4114 If not, there's no more debug_info for this comp unit. */
4115 if (has_children)
4116 {
4117 struct partial_die_info *first_die;
4118 CORE_ADDR lowpc, highpc;
4119
4120 lowpc = ((CORE_ADDR) -1);
4121 highpc = ((CORE_ADDR) 0);
4122
4123 first_die = load_partial_dies (reader, info_ptr, 1);
4124
4125 scan_partial_symbols (first_die, &lowpc, &highpc,
4126 ! has_pc_info, cu);
4127
4128 /* If we didn't find a lowpc, set it to highpc to avoid
4129 complaints from `maint check'. */
4130 if (lowpc == ((CORE_ADDR) -1))
4131 lowpc = highpc;
4132
4133 /* If the compilation unit didn't have an explicit address range,
4134 then use the information extracted from its child dies. */
4135 if (! has_pc_info)
4136 {
4137 best_lowpc = lowpc;
4138 best_highpc = highpc;
4139 }
4140 }
4141 pst->textlow = best_lowpc + baseaddr;
4142 pst->texthigh = best_highpc + baseaddr;
4143
4144 pst->n_global_syms = objfile->global_psymbols.next -
4145 (objfile->global_psymbols.list + pst->globals_offset);
4146 pst->n_static_syms = objfile->static_psymbols.next -
4147 (objfile->static_psymbols.list + pst->statics_offset);
4148 sort_pst_symbols (pst);
4149
4150 if (per_cu->is_debug_types)
4151 {
4152 /* It's not clear we want to do anything with stmt lists here.
4153 Waiting to see what gcc ultimately does. */
4154 }
4155 else
4156 {
4157 /* Get the list of files included in the current compilation unit,
4158 and build a psymtab for each of them. */
4159 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4160 }
4161}
4162
4163/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4164 Process compilation unit THIS_CU for a psymtab. */
4165
4166static void
4167process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu)
4168{
4169 /* If this compilation unit was already read in, free the
4170 cached copy in order to read it in again. This is
4171 necessary because we skipped some symbols when we first
4172 read in the compilation unit (see load_partial_dies).
4173 This problem could be avoided, but the benefit is unclear. */
4174 if (this_cu->cu != NULL)
4175 free_one_cached_comp_unit (this_cu);
4176
4177 gdb_assert (! this_cu->is_debug_types);
4178 init_cu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4179 NULL);
4180
4181 /* Age out any secondary CUs. */
4182 age_cached_comp_units ();
4183}
4184
4185/* Traversal function for htab_traverse_noresize.
4186 Process one .debug_types comp-unit. */
4187
4188static int
4189process_psymtab_type_unit (void **slot, void *info)
4190{
4191 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4192 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4193
4194 gdb_assert (info == NULL);
4195
4196 /* If this compilation unit was already read in, free the
4197 cached copy in order to read it in again. This is
4198 necessary because we skipped some symbols when we first
4199 read in the compilation unit (see load_partial_dies).
4200 This problem could be avoided, but the benefit is unclear. */
4201 if (per_cu->cu != NULL)
4202 free_one_cached_comp_unit (per_cu);
4203
4204 gdb_assert (per_cu->is_debug_types);
4205 init_tu_and_read_dies (per_cu, 0, process_psymtab_comp_unit_reader, NULL);
4206
4207 /* Age out any secondary CUs. */
4208 age_cached_comp_units ();
4209
4210 return 1;
4211}
4212
4213/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4214 Build partial symbol tables for the .debug_types comp-units. */
4215
4216static void
4217build_type_psymtabs (struct objfile *objfile)
4218{
4219 if (! create_all_type_units (objfile))
4220 return;
4221
4222 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4223 process_psymtab_type_unit, NULL);
4224}
4225
4226/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4227
4228static void
4229psymtabs_addrmap_cleanup (void *o)
4230{
4231 struct objfile *objfile = o;
4232
4233 objfile->psymtabs_addrmap = NULL;
4234}
4235
4236/* Build the partial symbol table by doing a quick pass through the
4237 .debug_info and .debug_abbrev sections. */
4238
4239static void
4240dwarf2_build_psymtabs_hard (struct objfile *objfile)
4241{
4242 struct cleanup *back_to, *addrmap_cleanup;
4243 struct obstack temp_obstack;
4244 int i;
4245
4246 dwarf2_per_objfile->reading_partial_symbols = 1;
4247
4248 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4249
4250 /* Any cached compilation units will be linked by the per-objfile
4251 read_in_chain. Make sure to free them when we're done. */
4252 back_to = make_cleanup (free_cached_comp_units, NULL);
4253
4254 build_type_psymtabs (objfile);
4255
4256 create_all_comp_units (objfile);
4257
4258 /* Create a temporary address map on a temporary obstack. We later
4259 copy this to the final obstack. */
4260 obstack_init (&temp_obstack);
4261 make_cleanup_obstack_free (&temp_obstack);
4262 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4263 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4264
4265 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4266 {
4267 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4268
4269 process_psymtab_comp_unit (per_cu);
4270 }
4271
4272 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4273 &objfile->objfile_obstack);
4274 discard_cleanups (addrmap_cleanup);
4275
4276 do_cleanups (back_to);
4277}
4278
4279/* die_reader_func for load_partial_comp_unit. */
4280
4281static void
4282load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4283 gdb_byte *info_ptr,
4284 struct die_info *comp_unit_die,
4285 int has_children,
4286 void *data)
4287{
4288 struct dwarf2_cu *cu = reader->cu;
4289
4290 prepare_one_comp_unit (cu, comp_unit_die);
4291
4292 /* Check if comp unit has_children.
4293 If so, read the rest of the partial symbols from this comp unit.
4294 If not, there's no more debug_info for this comp unit. */
4295 if (has_children)
4296 load_partial_dies (reader, info_ptr, 0);
4297}
4298
4299/* Load the partial DIEs for a secondary CU into memory.
4300 This is also used when rereading a primary CU with load_all_dies. */
4301
4302static void
4303load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4304{
4305 if (this_cu->is_debug_types)
4306 init_tu_and_read_dies (this_cu, 1, load_partial_comp_unit_reader, NULL);
4307 else
4308 init_cu_and_read_dies (this_cu, 0, 1, load_partial_comp_unit_reader, NULL);
4309}
4310
4311/* Create a list of all compilation units in OBJFILE.
4312 This is only done for -readnow and building partial symtabs. */
4313
4314static void
4315create_all_comp_units (struct objfile *objfile)
4316{
4317 int n_allocated;
4318 int n_comp_units;
4319 struct dwarf2_per_cu_data **all_comp_units;
4320 gdb_byte *info_ptr;
4321
4322 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4323 info_ptr = dwarf2_per_objfile->info.buffer;
4324
4325 n_comp_units = 0;
4326 n_allocated = 10;
4327 all_comp_units = xmalloc (n_allocated
4328 * sizeof (struct dwarf2_per_cu_data *));
4329
4330 while (info_ptr < dwarf2_per_objfile->info.buffer
4331 + dwarf2_per_objfile->info.size)
4332 {
4333 unsigned int length, initial_length_size;
4334 struct dwarf2_per_cu_data *this_cu;
4335 sect_offset offset;
4336
4337 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4338
4339 /* Read just enough information to find out where the next
4340 compilation unit is. */
4341 length = read_initial_length (objfile->obfd, info_ptr,
4342 &initial_length_size);
4343
4344 /* Save the compilation unit for later lookup. */
4345 this_cu = obstack_alloc (&objfile->objfile_obstack,
4346 sizeof (struct dwarf2_per_cu_data));
4347 memset (this_cu, 0, sizeof (*this_cu));
4348 this_cu->offset = offset;
4349 this_cu->length = length + initial_length_size;
4350 this_cu->objfile = objfile;
4351 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4352
4353 if (n_comp_units == n_allocated)
4354 {
4355 n_allocated *= 2;
4356 all_comp_units = xrealloc (all_comp_units,
4357 n_allocated
4358 * sizeof (struct dwarf2_per_cu_data *));
4359 }
4360 all_comp_units[n_comp_units++] = this_cu;
4361
4362 info_ptr = info_ptr + this_cu->length;
4363 }
4364
4365 dwarf2_per_objfile->all_comp_units
4366 = obstack_alloc (&objfile->objfile_obstack,
4367 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4368 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4369 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4370 xfree (all_comp_units);
4371 dwarf2_per_objfile->n_comp_units = n_comp_units;
4372}
4373
4374/* Process all loaded DIEs for compilation unit CU, starting at
4375 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4376 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4377 DW_AT_ranges). If NEED_PC is set, then this function will set
4378 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4379 and record the covered ranges in the addrmap. */
4380
4381static void
4382scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4383 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4384{
4385 struct partial_die_info *pdi;
4386
4387 /* Now, march along the PDI's, descending into ones which have
4388 interesting children but skipping the children of the other ones,
4389 until we reach the end of the compilation unit. */
4390
4391 pdi = first_die;
4392
4393 while (pdi != NULL)
4394 {
4395 fixup_partial_die (pdi, cu);
4396
4397 /* Anonymous namespaces or modules have no name but have interesting
4398 children, so we need to look at them. Ditto for anonymous
4399 enums. */
4400
4401 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4402 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
4403 {
4404 switch (pdi->tag)
4405 {
4406 case DW_TAG_subprogram:
4407 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4408 break;
4409 case DW_TAG_constant:
4410 case DW_TAG_variable:
4411 case DW_TAG_typedef:
4412 case DW_TAG_union_type:
4413 if (!pdi->is_declaration)
4414 {
4415 add_partial_symbol (pdi, cu);
4416 }
4417 break;
4418 case DW_TAG_class_type:
4419 case DW_TAG_interface_type:
4420 case DW_TAG_structure_type:
4421 if (!pdi->is_declaration)
4422 {
4423 add_partial_symbol (pdi, cu);
4424 }
4425 break;
4426 case DW_TAG_enumeration_type:
4427 if (!pdi->is_declaration)
4428 add_partial_enumeration (pdi, cu);
4429 break;
4430 case DW_TAG_base_type:
4431 case DW_TAG_subrange_type:
4432 /* File scope base type definitions are added to the partial
4433 symbol table. */
4434 add_partial_symbol (pdi, cu);
4435 break;
4436 case DW_TAG_namespace:
4437 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4438 break;
4439 case DW_TAG_module:
4440 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4441 break;
4442 default:
4443 break;
4444 }
4445 }
4446
4447 /* If the die has a sibling, skip to the sibling. */
4448
4449 pdi = pdi->die_sibling;
4450 }
4451}
4452
4453/* Functions used to compute the fully scoped name of a partial DIE.
4454
4455 Normally, this is simple. For C++, the parent DIE's fully scoped
4456 name is concatenated with "::" and the partial DIE's name. For
4457 Java, the same thing occurs except that "." is used instead of "::".
4458 Enumerators are an exception; they use the scope of their parent
4459 enumeration type, i.e. the name of the enumeration type is not
4460 prepended to the enumerator.
4461
4462 There are two complexities. One is DW_AT_specification; in this
4463 case "parent" means the parent of the target of the specification,
4464 instead of the direct parent of the DIE. The other is compilers
4465 which do not emit DW_TAG_namespace; in this case we try to guess
4466 the fully qualified name of structure types from their members'
4467 linkage names. This must be done using the DIE's children rather
4468 than the children of any DW_AT_specification target. We only need
4469 to do this for structures at the top level, i.e. if the target of
4470 any DW_AT_specification (if any; otherwise the DIE itself) does not
4471 have a parent. */
4472
4473/* Compute the scope prefix associated with PDI's parent, in
4474 compilation unit CU. The result will be allocated on CU's
4475 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4476 field. NULL is returned if no prefix is necessary. */
4477static char *
4478partial_die_parent_scope (struct partial_die_info *pdi,
4479 struct dwarf2_cu *cu)
4480{
4481 char *grandparent_scope;
4482 struct partial_die_info *parent, *real_pdi;
4483
4484 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4485 then this means the parent of the specification DIE. */
4486
4487 real_pdi = pdi;
4488 while (real_pdi->has_specification)
4489 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4490
4491 parent = real_pdi->die_parent;
4492 if (parent == NULL)
4493 return NULL;
4494
4495 if (parent->scope_set)
4496 return parent->scope;
4497
4498 fixup_partial_die (parent, cu);
4499
4500 grandparent_scope = partial_die_parent_scope (parent, cu);
4501
4502 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4503 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4504 Work around this problem here. */
4505 if (cu->language == language_cplus
4506 && parent->tag == DW_TAG_namespace
4507 && strcmp (parent->name, "::") == 0
4508 && grandparent_scope == NULL)
4509 {
4510 parent->scope = NULL;
4511 parent->scope_set = 1;
4512 return NULL;
4513 }
4514
4515 if (pdi->tag == DW_TAG_enumerator)
4516 /* Enumerators should not get the name of the enumeration as a prefix. */
4517 parent->scope = grandparent_scope;
4518 else if (parent->tag == DW_TAG_namespace
4519 || parent->tag == DW_TAG_module
4520 || parent->tag == DW_TAG_structure_type
4521 || parent->tag == DW_TAG_class_type
4522 || parent->tag == DW_TAG_interface_type
4523 || parent->tag == DW_TAG_union_type
4524 || parent->tag == DW_TAG_enumeration_type)
4525 {
4526 if (grandparent_scope == NULL)
4527 parent->scope = parent->name;
4528 else
4529 parent->scope = typename_concat (&cu->comp_unit_obstack,
4530 grandparent_scope,
4531 parent->name, 0, cu);
4532 }
4533 else
4534 {
4535 /* FIXME drow/2004-04-01: What should we be doing with
4536 function-local names? For partial symbols, we should probably be
4537 ignoring them. */
4538 complaint (&symfile_complaints,
4539 _("unhandled containing DIE tag %d for DIE at %d"),
4540 parent->tag, pdi->offset.sect_off);
4541 parent->scope = grandparent_scope;
4542 }
4543
4544 parent->scope_set = 1;
4545 return parent->scope;
4546}
4547
4548/* Return the fully scoped name associated with PDI, from compilation unit
4549 CU. The result will be allocated with malloc. */
4550
4551static char *
4552partial_die_full_name (struct partial_die_info *pdi,
4553 struct dwarf2_cu *cu)
4554{
4555 char *parent_scope;
4556
4557 /* If this is a template instantiation, we can not work out the
4558 template arguments from partial DIEs. So, unfortunately, we have
4559 to go through the full DIEs. At least any work we do building
4560 types here will be reused if full symbols are loaded later. */
4561 if (pdi->has_template_arguments)
4562 {
4563 fixup_partial_die (pdi, cu);
4564
4565 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4566 {
4567 struct die_info *die;
4568 struct attribute attr;
4569 struct dwarf2_cu *ref_cu = cu;
4570
4571 /* DW_FORM_ref_addr is using section offset. */
4572 attr.name = 0;
4573 attr.form = DW_FORM_ref_addr;
4574 attr.u.unsnd = pdi->offset.sect_off;
4575 die = follow_die_ref (NULL, &attr, &ref_cu);
4576
4577 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4578 }
4579 }
4580
4581 parent_scope = partial_die_parent_scope (pdi, cu);
4582 if (parent_scope == NULL)
4583 return NULL;
4584 else
4585 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4586}
4587
4588static void
4589add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4590{
4591 struct objfile *objfile = cu->objfile;
4592 CORE_ADDR addr = 0;
4593 char *actual_name = NULL;
4594 CORE_ADDR baseaddr;
4595 int built_actual_name = 0;
4596
4597 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4598
4599 actual_name = partial_die_full_name (pdi, cu);
4600 if (actual_name)
4601 built_actual_name = 1;
4602
4603 if (actual_name == NULL)
4604 actual_name = pdi->name;
4605
4606 switch (pdi->tag)
4607 {
4608 case DW_TAG_subprogram:
4609 if (pdi->is_external || cu->language == language_ada)
4610 {
4611 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4612 of the global scope. But in Ada, we want to be able to access
4613 nested procedures globally. So all Ada subprograms are stored
4614 in the global scope. */
4615 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4616 mst_text, objfile); */
4617 add_psymbol_to_list (actual_name, strlen (actual_name),
4618 built_actual_name,
4619 VAR_DOMAIN, LOC_BLOCK,
4620 &objfile->global_psymbols,
4621 0, pdi->lowpc + baseaddr,
4622 cu->language, objfile);
4623 }
4624 else
4625 {
4626 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4627 mst_file_text, objfile); */
4628 add_psymbol_to_list (actual_name, strlen (actual_name),
4629 built_actual_name,
4630 VAR_DOMAIN, LOC_BLOCK,
4631 &objfile->static_psymbols,
4632 0, pdi->lowpc + baseaddr,
4633 cu->language, objfile);
4634 }
4635 break;
4636 case DW_TAG_constant:
4637 {
4638 struct psymbol_allocation_list *list;
4639
4640 if (pdi->is_external)
4641 list = &objfile->global_psymbols;
4642 else
4643 list = &objfile->static_psymbols;
4644 add_psymbol_to_list (actual_name, strlen (actual_name),
4645 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4646 list, 0, 0, cu->language, objfile);
4647 }
4648 break;
4649 case DW_TAG_variable:
4650 if (pdi->locdesc)
4651 addr = decode_locdesc (pdi->locdesc, cu);
4652
4653 if (pdi->locdesc
4654 && addr == 0
4655 && !dwarf2_per_objfile->has_section_at_zero)
4656 {
4657 /* A global or static variable may also have been stripped
4658 out by the linker if unused, in which case its address
4659 will be nullified; do not add such variables into partial
4660 symbol table then. */
4661 }
4662 else if (pdi->is_external)
4663 {
4664 /* Global Variable.
4665 Don't enter into the minimal symbol tables as there is
4666 a minimal symbol table entry from the ELF symbols already.
4667 Enter into partial symbol table if it has a location
4668 descriptor or a type.
4669 If the location descriptor is missing, new_symbol will create
4670 a LOC_UNRESOLVED symbol, the address of the variable will then
4671 be determined from the minimal symbol table whenever the variable
4672 is referenced.
4673 The address for the partial symbol table entry is not
4674 used by GDB, but it comes in handy for debugging partial symbol
4675 table building. */
4676
4677 if (pdi->locdesc || pdi->has_type)
4678 add_psymbol_to_list (actual_name, strlen (actual_name),
4679 built_actual_name,
4680 VAR_DOMAIN, LOC_STATIC,
4681 &objfile->global_psymbols,
4682 0, addr + baseaddr,
4683 cu->language, objfile);
4684 }
4685 else
4686 {
4687 /* Static Variable. Skip symbols without location descriptors. */
4688 if (pdi->locdesc == NULL)
4689 {
4690 if (built_actual_name)
4691 xfree (actual_name);
4692 return;
4693 }
4694 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4695 mst_file_data, objfile); */
4696 add_psymbol_to_list (actual_name, strlen (actual_name),
4697 built_actual_name,
4698 VAR_DOMAIN, LOC_STATIC,
4699 &objfile->static_psymbols,
4700 0, addr + baseaddr,
4701 cu->language, objfile);
4702 }
4703 break;
4704 case DW_TAG_typedef:
4705 case DW_TAG_base_type:
4706 case DW_TAG_subrange_type:
4707 add_psymbol_to_list (actual_name, strlen (actual_name),
4708 built_actual_name,
4709 VAR_DOMAIN, LOC_TYPEDEF,
4710 &objfile->static_psymbols,
4711 0, (CORE_ADDR) 0, cu->language, objfile);
4712 break;
4713 case DW_TAG_namespace:
4714 add_psymbol_to_list (actual_name, strlen (actual_name),
4715 built_actual_name,
4716 VAR_DOMAIN, LOC_TYPEDEF,
4717 &objfile->global_psymbols,
4718 0, (CORE_ADDR) 0, cu->language, objfile);
4719 break;
4720 case DW_TAG_class_type:
4721 case DW_TAG_interface_type:
4722 case DW_TAG_structure_type:
4723 case DW_TAG_union_type:
4724 case DW_TAG_enumeration_type:
4725 /* Skip external references. The DWARF standard says in the section
4726 about "Structure, Union, and Class Type Entries": "An incomplete
4727 structure, union or class type is represented by a structure,
4728 union or class entry that does not have a byte size attribute
4729 and that has a DW_AT_declaration attribute." */
4730 if (!pdi->has_byte_size && pdi->is_declaration)
4731 {
4732 if (built_actual_name)
4733 xfree (actual_name);
4734 return;
4735 }
4736
4737 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4738 static vs. global. */
4739 add_psymbol_to_list (actual_name, strlen (actual_name),
4740 built_actual_name,
4741 STRUCT_DOMAIN, LOC_TYPEDEF,
4742 (cu->language == language_cplus
4743 || cu->language == language_java)
4744 ? &objfile->global_psymbols
4745 : &objfile->static_psymbols,
4746 0, (CORE_ADDR) 0, cu->language, objfile);
4747
4748 break;
4749 case DW_TAG_enumerator:
4750 add_psymbol_to_list (actual_name, strlen (actual_name),
4751 built_actual_name,
4752 VAR_DOMAIN, LOC_CONST,
4753 (cu->language == language_cplus
4754 || cu->language == language_java)
4755 ? &objfile->global_psymbols
4756 : &objfile->static_psymbols,
4757 0, (CORE_ADDR) 0, cu->language, objfile);
4758 break;
4759 default:
4760 break;
4761 }
4762
4763 if (built_actual_name)
4764 xfree (actual_name);
4765}
4766
4767/* Read a partial die corresponding to a namespace; also, add a symbol
4768 corresponding to that namespace to the symbol table. NAMESPACE is
4769 the name of the enclosing namespace. */
4770
4771static void
4772add_partial_namespace (struct partial_die_info *pdi,
4773 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4774 int need_pc, struct dwarf2_cu *cu)
4775{
4776 /* Add a symbol for the namespace. */
4777
4778 add_partial_symbol (pdi, cu);
4779
4780 /* Now scan partial symbols in that namespace. */
4781
4782 if (pdi->has_children)
4783 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4784}
4785
4786/* Read a partial die corresponding to a Fortran module. */
4787
4788static void
4789add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4790 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4791{
4792 /* Now scan partial symbols in that module. */
4793
4794 if (pdi->has_children)
4795 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4796}
4797
4798/* Read a partial die corresponding to a subprogram and create a partial
4799 symbol for that subprogram. When the CU language allows it, this
4800 routine also defines a partial symbol for each nested subprogram
4801 that this subprogram contains.
4802
4803 DIE my also be a lexical block, in which case we simply search
4804 recursively for suprograms defined inside that lexical block.
4805 Again, this is only performed when the CU language allows this
4806 type of definitions. */
4807
4808static void
4809add_partial_subprogram (struct partial_die_info *pdi,
4810 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4811 int need_pc, struct dwarf2_cu *cu)
4812{
4813 if (pdi->tag == DW_TAG_subprogram)
4814 {
4815 if (pdi->has_pc_info)
4816 {
4817 if (pdi->lowpc < *lowpc)
4818 *lowpc = pdi->lowpc;
4819 if (pdi->highpc > *highpc)
4820 *highpc = pdi->highpc;
4821 if (need_pc)
4822 {
4823 CORE_ADDR baseaddr;
4824 struct objfile *objfile = cu->objfile;
4825
4826 baseaddr = ANOFFSET (objfile->section_offsets,
4827 SECT_OFF_TEXT (objfile));
4828 addrmap_set_empty (objfile->psymtabs_addrmap,
4829 pdi->lowpc + baseaddr,
4830 pdi->highpc - 1 + baseaddr,
4831 cu->per_cu->v.psymtab);
4832 }
4833 }
4834
4835 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4836 {
4837 if (!pdi->is_declaration)
4838 /* Ignore subprogram DIEs that do not have a name, they are
4839 illegal. Do not emit a complaint at this point, we will
4840 do so when we convert this psymtab into a symtab. */
4841 if (pdi->name)
4842 add_partial_symbol (pdi, cu);
4843 }
4844 }
4845
4846 if (! pdi->has_children)
4847 return;
4848
4849 if (cu->language == language_ada)
4850 {
4851 pdi = pdi->die_child;
4852 while (pdi != NULL)
4853 {
4854 fixup_partial_die (pdi, cu);
4855 if (pdi->tag == DW_TAG_subprogram
4856 || pdi->tag == DW_TAG_lexical_block)
4857 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4858 pdi = pdi->die_sibling;
4859 }
4860 }
4861}
4862
4863/* Read a partial die corresponding to an enumeration type. */
4864
4865static void
4866add_partial_enumeration (struct partial_die_info *enum_pdi,
4867 struct dwarf2_cu *cu)
4868{
4869 struct partial_die_info *pdi;
4870
4871 if (enum_pdi->name != NULL)
4872 add_partial_symbol (enum_pdi, cu);
4873
4874 pdi = enum_pdi->die_child;
4875 while (pdi)
4876 {
4877 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4878 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4879 else
4880 add_partial_symbol (pdi, cu);
4881 pdi = pdi->die_sibling;
4882 }
4883}
4884
4885/* Return the initial uleb128 in the die at INFO_PTR. */
4886
4887static unsigned int
4888peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4889{
4890 unsigned int bytes_read;
4891
4892 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4893}
4894
4895/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4896 Return the corresponding abbrev, or NULL if the number is zero (indicating
4897 an empty DIE). In either case *BYTES_READ will be set to the length of
4898 the initial number. */
4899
4900static struct abbrev_info *
4901peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4902 struct dwarf2_cu *cu)
4903{
4904 bfd *abfd = cu->objfile->obfd;
4905 unsigned int abbrev_number;
4906 struct abbrev_info *abbrev;
4907
4908 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4909
4910 if (abbrev_number == 0)
4911 return NULL;
4912
4913 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4914 if (!abbrev)
4915 {
4916 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4917 abbrev_number, bfd_get_filename (abfd));
4918 }
4919
4920 return abbrev;
4921}
4922
4923/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4924 Returns a pointer to the end of a series of DIEs, terminated by an empty
4925 DIE. Any children of the skipped DIEs will also be skipped. */
4926
4927static gdb_byte *
4928skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4929{
4930 struct dwarf2_cu *cu = reader->cu;
4931 struct abbrev_info *abbrev;
4932 unsigned int bytes_read;
4933
4934 while (1)
4935 {
4936 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4937 if (abbrev == NULL)
4938 return info_ptr + bytes_read;
4939 else
4940 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4941 }
4942}
4943
4944/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4945 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4946 abbrev corresponding to that skipped uleb128 should be passed in
4947 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4948 children. */
4949
4950static gdb_byte *
4951skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
4952 struct abbrev_info *abbrev)
4953{
4954 unsigned int bytes_read;
4955 struct attribute attr;
4956 bfd *abfd = reader->abfd;
4957 struct dwarf2_cu *cu = reader->cu;
4958 gdb_byte *buffer = reader->buffer;
4959 unsigned int form, i;
4960
4961 for (i = 0; i < abbrev->num_attrs; i++)
4962 {
4963 /* The only abbrev we care about is DW_AT_sibling. */
4964 if (abbrev->attrs[i].name == DW_AT_sibling)
4965 {
4966 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4967 if (attr.form == DW_FORM_ref_addr)
4968 complaint (&symfile_complaints,
4969 _("ignoring absolute DW_AT_sibling"));
4970 else
4971 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4972 }
4973
4974 /* If it isn't DW_AT_sibling, skip this attribute. */
4975 form = abbrev->attrs[i].form;
4976 skip_attribute:
4977 switch (form)
4978 {
4979 case DW_FORM_ref_addr:
4980 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4981 and later it is offset sized. */
4982 if (cu->header.version == 2)
4983 info_ptr += cu->header.addr_size;
4984 else
4985 info_ptr += cu->header.offset_size;
4986 break;
4987 case DW_FORM_addr:
4988 info_ptr += cu->header.addr_size;
4989 break;
4990 case DW_FORM_data1:
4991 case DW_FORM_ref1:
4992 case DW_FORM_flag:
4993 info_ptr += 1;
4994 break;
4995 case DW_FORM_flag_present:
4996 break;
4997 case DW_FORM_data2:
4998 case DW_FORM_ref2:
4999 info_ptr += 2;
5000 break;
5001 case DW_FORM_data4:
5002 case DW_FORM_ref4:
5003 info_ptr += 4;
5004 break;
5005 case DW_FORM_data8:
5006 case DW_FORM_ref8:
5007 case DW_FORM_ref_sig8:
5008 info_ptr += 8;
5009 break;
5010 case DW_FORM_string:
5011 read_direct_string (abfd, info_ptr, &bytes_read);
5012 info_ptr += bytes_read;
5013 break;
5014 case DW_FORM_sec_offset:
5015 case DW_FORM_strp:
5016 info_ptr += cu->header.offset_size;
5017 break;
5018 case DW_FORM_exprloc:
5019 case DW_FORM_block:
5020 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5021 info_ptr += bytes_read;
5022 break;
5023 case DW_FORM_block1:
5024 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5025 break;
5026 case DW_FORM_block2:
5027 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5028 break;
5029 case DW_FORM_block4:
5030 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5031 break;
5032 case DW_FORM_sdata:
5033 case DW_FORM_udata:
5034 case DW_FORM_ref_udata:
5035 case DW_FORM_GNU_addr_index:
5036 case DW_FORM_GNU_str_index:
5037 info_ptr = skip_leb128 (abfd, info_ptr);
5038 break;
5039 case DW_FORM_indirect:
5040 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5041 info_ptr += bytes_read;
5042 /* We need to continue parsing from here, so just go back to
5043 the top. */
5044 goto skip_attribute;
5045
5046 default:
5047 error (_("Dwarf Error: Cannot handle %s "
5048 "in DWARF reader [in module %s]"),
5049 dwarf_form_name (form),
5050 bfd_get_filename (abfd));
5051 }
5052 }
5053
5054 if (abbrev->has_children)
5055 return skip_children (reader, info_ptr);
5056 else
5057 return info_ptr;
5058}
5059
5060/* Locate ORIG_PDI's sibling.
5061 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5062
5063static gdb_byte *
5064locate_pdi_sibling (const struct die_reader_specs *reader,
5065 struct partial_die_info *orig_pdi,
5066 gdb_byte *info_ptr)
5067{
5068 /* Do we know the sibling already? */
5069
5070 if (orig_pdi->sibling)
5071 return orig_pdi->sibling;
5072
5073 /* Are there any children to deal with? */
5074
5075 if (!orig_pdi->has_children)
5076 return info_ptr;
5077
5078 /* Skip the children the long way. */
5079
5080 return skip_children (reader, info_ptr);
5081}
5082
5083/* Expand this partial symbol table into a full symbol table. */
5084
5085static void
5086dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5087{
5088 if (pst != NULL)
5089 {
5090 if (pst->readin)
5091 {
5092 warning (_("bug: psymtab for %s is already read in."),
5093 pst->filename);
5094 }
5095 else
5096 {
5097 if (info_verbose)
5098 {
5099 printf_filtered (_("Reading in symbols for %s..."),
5100 pst->filename);
5101 gdb_flush (gdb_stdout);
5102 }
5103
5104 /* Restore our global data. */
5105 dwarf2_per_objfile = objfile_data (pst->objfile,
5106 dwarf2_objfile_data_key);
5107
5108 /* If this psymtab is constructed from a debug-only objfile, the
5109 has_section_at_zero flag will not necessarily be correct. We
5110 can get the correct value for this flag by looking at the data
5111 associated with the (presumably stripped) associated objfile. */
5112 if (pst->objfile->separate_debug_objfile_backlink)
5113 {
5114 struct dwarf2_per_objfile *dpo_backlink
5115 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5116 dwarf2_objfile_data_key);
5117
5118 dwarf2_per_objfile->has_section_at_zero
5119 = dpo_backlink->has_section_at_zero;
5120 }
5121
5122 dwarf2_per_objfile->reading_partial_symbols = 0;
5123
5124 psymtab_to_symtab_1 (pst);
5125
5126 /* Finish up the debug error message. */
5127 if (info_verbose)
5128 printf_filtered (_("done.\n"));
5129 }
5130 }
5131}
5132\f
5133/* Reading in full CUs. */
5134
5135/* Add PER_CU to the queue. */
5136
5137static void
5138queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
5139{
5140 struct dwarf2_queue_item *item;
5141
5142 per_cu->queued = 1;
5143 item = xmalloc (sizeof (*item));
5144 item->per_cu = per_cu;
5145 item->next = NULL;
5146
5147 if (dwarf2_queue == NULL)
5148 dwarf2_queue = item;
5149 else
5150 dwarf2_queue_tail->next = item;
5151
5152 dwarf2_queue_tail = item;
5153}
5154
5155/* Process the queue. */
5156
5157static void
5158process_queue (void)
5159{
5160 struct dwarf2_queue_item *item, *next_item;
5161
5162 /* The queue starts out with one item, but following a DIE reference
5163 may load a new CU, adding it to the end of the queue. */
5164 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5165 {
5166 if (dwarf2_per_objfile->using_index
5167 ? !item->per_cu->v.quick->symtab
5168 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5169 process_full_comp_unit (item->per_cu);
5170
5171 item->per_cu->queued = 0;
5172 next_item = item->next;
5173 xfree (item);
5174 }
5175
5176 dwarf2_queue_tail = NULL;
5177}
5178
5179/* Free all allocated queue entries. This function only releases anything if
5180 an error was thrown; if the queue was processed then it would have been
5181 freed as we went along. */
5182
5183static void
5184dwarf2_release_queue (void *dummy)
5185{
5186 struct dwarf2_queue_item *item, *last;
5187
5188 item = dwarf2_queue;
5189 while (item)
5190 {
5191 /* Anything still marked queued is likely to be in an
5192 inconsistent state, so discard it. */
5193 if (item->per_cu->queued)
5194 {
5195 if (item->per_cu->cu != NULL)
5196 free_one_cached_comp_unit (item->per_cu);
5197 item->per_cu->queued = 0;
5198 }
5199
5200 last = item;
5201 item = item->next;
5202 xfree (last);
5203 }
5204
5205 dwarf2_queue = dwarf2_queue_tail = NULL;
5206}
5207
5208/* Read in full symbols for PST, and anything it depends on. */
5209
5210static void
5211psymtab_to_symtab_1 (struct partial_symtab *pst)
5212{
5213 struct dwarf2_per_cu_data *per_cu;
5214 struct cleanup *back_to;
5215 int i;
5216
5217 for (i = 0; i < pst->number_of_dependencies; i++)
5218 if (!pst->dependencies[i]->readin)
5219 {
5220 /* Inform about additional files that need to be read in. */
5221 if (info_verbose)
5222 {
5223 /* FIXME: i18n: Need to make this a single string. */
5224 fputs_filtered (" ", gdb_stdout);
5225 wrap_here ("");
5226 fputs_filtered ("and ", gdb_stdout);
5227 wrap_here ("");
5228 printf_filtered ("%s...", pst->dependencies[i]->filename);
5229 wrap_here (""); /* Flush output. */
5230 gdb_flush (gdb_stdout);
5231 }
5232 psymtab_to_symtab_1 (pst->dependencies[i]);
5233 }
5234
5235 per_cu = pst->read_symtab_private;
5236
5237 if (per_cu == NULL)
5238 {
5239 /* It's an include file, no symbols to read for it.
5240 Everything is in the parent symtab. */
5241 pst->readin = 1;
5242 return;
5243 }
5244
5245 dw2_do_instantiate_symtab (per_cu);
5246}
5247
5248/* Trivial hash function for die_info: the hash value of a DIE
5249 is its offset in .debug_info for this objfile. */
5250
5251static hashval_t
5252die_hash (const void *item)
5253{
5254 const struct die_info *die = item;
5255
5256 return die->offset.sect_off;
5257}
5258
5259/* Trivial comparison function for die_info structures: two DIEs
5260 are equal if they have the same offset. */
5261
5262static int
5263die_eq (const void *item_lhs, const void *item_rhs)
5264{
5265 const struct die_info *die_lhs = item_lhs;
5266 const struct die_info *die_rhs = item_rhs;
5267
5268 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5269}
5270
5271/* die_reader_func for load_full_comp_unit.
5272 This is identical to read_signatured_type_reader,
5273 but is kept separate for now. */
5274
5275static void
5276load_full_comp_unit_reader (const struct die_reader_specs *reader,
5277 gdb_byte *info_ptr,
5278 struct die_info *comp_unit_die,
5279 int has_children,
5280 void *data)
5281{
5282 struct dwarf2_cu *cu = reader->cu;
5283 struct attribute *attr;
5284
5285 gdb_assert (cu->die_hash == NULL);
5286 cu->die_hash =
5287 htab_create_alloc_ex (cu->header.length / 12,
5288 die_hash,
5289 die_eq,
5290 NULL,
5291 &cu->comp_unit_obstack,
5292 hashtab_obstack_allocate,
5293 dummy_obstack_deallocate);
5294
5295 if (has_children)
5296 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5297 &info_ptr, comp_unit_die);
5298 cu->dies = comp_unit_die;
5299 /* comp_unit_die is not stored in die_hash, no need. */
5300
5301 /* We try not to read any attributes in this function, because not
5302 all CUs needed for references have been loaded yet, and symbol
5303 table processing isn't initialized. But we have to set the CU language,
5304 or we won't be able to build types correctly.
5305 Similarly, if we do not read the producer, we can not apply
5306 producer-specific interpretation. */
5307 prepare_one_comp_unit (cu, cu->dies);
5308}
5309
5310/* Load the DIEs associated with PER_CU into memory. */
5311
5312static void
5313load_full_comp_unit (struct dwarf2_per_cu_data *this_cu)
5314{
5315 gdb_assert (! this_cu->is_debug_types);
5316
5317 init_cu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader, NULL);
5318}
5319
5320/* Add a DIE to the delayed physname list. */
5321
5322static void
5323add_to_method_list (struct type *type, int fnfield_index, int index,
5324 const char *name, struct die_info *die,
5325 struct dwarf2_cu *cu)
5326{
5327 struct delayed_method_info mi;
5328 mi.type = type;
5329 mi.fnfield_index = fnfield_index;
5330 mi.index = index;
5331 mi.name = name;
5332 mi.die = die;
5333 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5334}
5335
5336/* A cleanup for freeing the delayed method list. */
5337
5338static void
5339free_delayed_list (void *ptr)
5340{
5341 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5342 if (cu->method_list != NULL)
5343 {
5344 VEC_free (delayed_method_info, cu->method_list);
5345 cu->method_list = NULL;
5346 }
5347}
5348
5349/* Compute the physnames of any methods on the CU's method list.
5350
5351 The computation of method physnames is delayed in order to avoid the
5352 (bad) condition that one of the method's formal parameters is of an as yet
5353 incomplete type. */
5354
5355static void
5356compute_delayed_physnames (struct dwarf2_cu *cu)
5357{
5358 int i;
5359 struct delayed_method_info *mi;
5360 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5361 {
5362 const char *physname;
5363 struct fn_fieldlist *fn_flp
5364 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5365 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5366 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5367 }
5368}
5369
5370/* Go objects should be embedded in a DW_TAG_module DIE,
5371 and it's not clear if/how imported objects will appear.
5372 To keep Go support simple until that's worked out,
5373 go back through what we've read and create something usable.
5374 We could do this while processing each DIE, and feels kinda cleaner,
5375 but that way is more invasive.
5376 This is to, for example, allow the user to type "p var" or "b main"
5377 without having to specify the package name, and allow lookups
5378 of module.object to work in contexts that use the expression
5379 parser. */
5380
5381static void
5382fixup_go_packaging (struct dwarf2_cu *cu)
5383{
5384 char *package_name = NULL;
5385 struct pending *list;
5386 int i;
5387
5388 for (list = global_symbols; list != NULL; list = list->next)
5389 {
5390 for (i = 0; i < list->nsyms; ++i)
5391 {
5392 struct symbol *sym = list->symbol[i];
5393
5394 if (SYMBOL_LANGUAGE (sym) == language_go
5395 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5396 {
5397 char *this_package_name = go_symbol_package_name (sym);
5398
5399 if (this_package_name == NULL)
5400 continue;
5401 if (package_name == NULL)
5402 package_name = this_package_name;
5403 else
5404 {
5405 if (strcmp (package_name, this_package_name) != 0)
5406 complaint (&symfile_complaints,
5407 _("Symtab %s has objects from two different Go packages: %s and %s"),
5408 (sym->symtab && sym->symtab->filename
5409 ? sym->symtab->filename
5410 : cu->objfile->name),
5411 this_package_name, package_name);
5412 xfree (this_package_name);
5413 }
5414 }
5415 }
5416 }
5417
5418 if (package_name != NULL)
5419 {
5420 struct objfile *objfile = cu->objfile;
5421 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5422 package_name, objfile);
5423 struct symbol *sym;
5424
5425 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5426
5427 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5428 SYMBOL_SET_LANGUAGE (sym, language_go);
5429 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5430 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5431 e.g., "main" finds the "main" module and not C's main(). */
5432 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5433 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5434 SYMBOL_TYPE (sym) = type;
5435
5436 add_symbol_to_list (sym, &global_symbols);
5437
5438 xfree (package_name);
5439 }
5440}
5441
5442/* Generate full symbol information for PER_CU, whose DIEs have
5443 already been loaded into memory. */
5444
5445static void
5446process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
5447{
5448 struct dwarf2_cu *cu = per_cu->cu;
5449 struct objfile *objfile = per_cu->objfile;
5450 CORE_ADDR lowpc, highpc;
5451 struct symtab *symtab;
5452 struct cleanup *back_to, *delayed_list_cleanup;
5453 CORE_ADDR baseaddr;
5454
5455 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5456
5457 buildsym_init ();
5458 back_to = make_cleanup (really_free_pendings, NULL);
5459 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5460
5461 cu->list_in_scope = &file_symbols;
5462
5463 /* Do line number decoding in read_file_scope () */
5464 process_die (cu->dies, cu);
5465
5466 /* For now fudge the Go package. */
5467 if (cu->language == language_go)
5468 fixup_go_packaging (cu);
5469
5470 /* Now that we have processed all the DIEs in the CU, all the types
5471 should be complete, and it should now be safe to compute all of the
5472 physnames. */
5473 compute_delayed_physnames (cu);
5474 do_cleanups (delayed_list_cleanup);
5475
5476 /* Some compilers don't define a DW_AT_high_pc attribute for the
5477 compilation unit. If the DW_AT_high_pc is missing, synthesize
5478 it, by scanning the DIE's below the compilation unit. */
5479 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5480
5481 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5482
5483 if (symtab != NULL)
5484 {
5485 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5486
5487 /* Set symtab language to language from DW_AT_language. If the
5488 compilation is from a C file generated by language preprocessors, do
5489 not set the language if it was already deduced by start_subfile. */
5490 if (!(cu->language == language_c && symtab->language != language_c))
5491 symtab->language = cu->language;
5492
5493 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5494 produce DW_AT_location with location lists but it can be possibly
5495 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5496 there were bugs in prologue debug info, fixed later in GCC-4.5
5497 by "unwind info for epilogues" patch (which is not directly related).
5498
5499 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5500 needed, it would be wrong due to missing DW_AT_producer there.
5501
5502 Still one can confuse GDB by using non-standard GCC compilation
5503 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5504 */
5505 if (cu->has_loclist && gcc_4_minor >= 5)
5506 symtab->locations_valid = 1;
5507
5508 if (gcc_4_minor >= 5)
5509 symtab->epilogue_unwind_valid = 1;
5510
5511 symtab->call_site_htab = cu->call_site_htab;
5512 }
5513
5514 if (dwarf2_per_objfile->using_index)
5515 per_cu->v.quick->symtab = symtab;
5516 else
5517 {
5518 struct partial_symtab *pst = per_cu->v.psymtab;
5519 pst->symtab = symtab;
5520 pst->readin = 1;
5521 }
5522
5523 do_cleanups (back_to);
5524}
5525
5526/* Process a die and its children. */
5527
5528static void
5529process_die (struct die_info *die, struct dwarf2_cu *cu)
5530{
5531 switch (die->tag)
5532 {
5533 case DW_TAG_padding:
5534 break;
5535 case DW_TAG_compile_unit:
5536 read_file_scope (die, cu);
5537 break;
5538 case DW_TAG_type_unit:
5539 read_type_unit_scope (die, cu);
5540 break;
5541 case DW_TAG_subprogram:
5542 case DW_TAG_inlined_subroutine:
5543 read_func_scope (die, cu);
5544 break;
5545 case DW_TAG_lexical_block:
5546 case DW_TAG_try_block:
5547 case DW_TAG_catch_block:
5548 read_lexical_block_scope (die, cu);
5549 break;
5550 case DW_TAG_GNU_call_site:
5551 read_call_site_scope (die, cu);
5552 break;
5553 case DW_TAG_class_type:
5554 case DW_TAG_interface_type:
5555 case DW_TAG_structure_type:
5556 case DW_TAG_union_type:
5557 process_structure_scope (die, cu);
5558 break;
5559 case DW_TAG_enumeration_type:
5560 process_enumeration_scope (die, cu);
5561 break;
5562
5563 /* These dies have a type, but processing them does not create
5564 a symbol or recurse to process the children. Therefore we can
5565 read them on-demand through read_type_die. */
5566 case DW_TAG_subroutine_type:
5567 case DW_TAG_set_type:
5568 case DW_TAG_array_type:
5569 case DW_TAG_pointer_type:
5570 case DW_TAG_ptr_to_member_type:
5571 case DW_TAG_reference_type:
5572 case DW_TAG_string_type:
5573 break;
5574
5575 case DW_TAG_base_type:
5576 case DW_TAG_subrange_type:
5577 case DW_TAG_typedef:
5578 /* Add a typedef symbol for the type definition, if it has a
5579 DW_AT_name. */
5580 new_symbol (die, read_type_die (die, cu), cu);
5581 break;
5582 case DW_TAG_common_block:
5583 read_common_block (die, cu);
5584 break;
5585 case DW_TAG_common_inclusion:
5586 break;
5587 case DW_TAG_namespace:
5588 processing_has_namespace_info = 1;
5589 read_namespace (die, cu);
5590 break;
5591 case DW_TAG_module:
5592 processing_has_namespace_info = 1;
5593 read_module (die, cu);
5594 break;
5595 case DW_TAG_imported_declaration:
5596 case DW_TAG_imported_module:
5597 processing_has_namespace_info = 1;
5598 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5599 || cu->language != language_fortran))
5600 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5601 dwarf_tag_name (die->tag));
5602 read_import_statement (die, cu);
5603 break;
5604 default:
5605 new_symbol (die, NULL, cu);
5606 break;
5607 }
5608}
5609
5610/* A helper function for dwarf2_compute_name which determines whether DIE
5611 needs to have the name of the scope prepended to the name listed in the
5612 die. */
5613
5614static int
5615die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5616{
5617 struct attribute *attr;
5618
5619 switch (die->tag)
5620 {
5621 case DW_TAG_namespace:
5622 case DW_TAG_typedef:
5623 case DW_TAG_class_type:
5624 case DW_TAG_interface_type:
5625 case DW_TAG_structure_type:
5626 case DW_TAG_union_type:
5627 case DW_TAG_enumeration_type:
5628 case DW_TAG_enumerator:
5629 case DW_TAG_subprogram:
5630 case DW_TAG_member:
5631 return 1;
5632
5633 case DW_TAG_variable:
5634 case DW_TAG_constant:
5635 /* We only need to prefix "globally" visible variables. These include
5636 any variable marked with DW_AT_external or any variable that
5637 lives in a namespace. [Variables in anonymous namespaces
5638 require prefixing, but they are not DW_AT_external.] */
5639
5640 if (dwarf2_attr (die, DW_AT_specification, cu))
5641 {
5642 struct dwarf2_cu *spec_cu = cu;
5643
5644 return die_needs_namespace (die_specification (die, &spec_cu),
5645 spec_cu);
5646 }
5647
5648 attr = dwarf2_attr (die, DW_AT_external, cu);
5649 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5650 && die->parent->tag != DW_TAG_module)
5651 return 0;
5652 /* A variable in a lexical block of some kind does not need a
5653 namespace, even though in C++ such variables may be external
5654 and have a mangled name. */
5655 if (die->parent->tag == DW_TAG_lexical_block
5656 || die->parent->tag == DW_TAG_try_block
5657 || die->parent->tag == DW_TAG_catch_block
5658 || die->parent->tag == DW_TAG_subprogram)
5659 return 0;
5660 return 1;
5661
5662 default:
5663 return 0;
5664 }
5665}
5666
5667/* Retrieve the last character from a mem_file. */
5668
5669static void
5670do_ui_file_peek_last (void *object, const char *buffer, long length)
5671{
5672 char *last_char_p = (char *) object;
5673
5674 if (length > 0)
5675 *last_char_p = buffer[length - 1];
5676}
5677
5678/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5679 compute the physname for the object, which include a method's:
5680 - formal parameters (C++/Java),
5681 - receiver type (Go),
5682 - return type (Java).
5683
5684 The term "physname" is a bit confusing.
5685 For C++, for example, it is the demangled name.
5686 For Go, for example, it's the mangled name.
5687
5688 For Ada, return the DIE's linkage name rather than the fully qualified
5689 name. PHYSNAME is ignored..
5690
5691 The result is allocated on the objfile_obstack and canonicalized. */
5692
5693static const char *
5694dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5695 int physname)
5696{
5697 struct objfile *objfile = cu->objfile;
5698
5699 if (name == NULL)
5700 name = dwarf2_name (die, cu);
5701
5702 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5703 compute it by typename_concat inside GDB. */
5704 if (cu->language == language_ada
5705 || (cu->language == language_fortran && physname))
5706 {
5707 /* For Ada unit, we prefer the linkage name over the name, as
5708 the former contains the exported name, which the user expects
5709 to be able to reference. Ideally, we want the user to be able
5710 to reference this entity using either natural or linkage name,
5711 but we haven't started looking at this enhancement yet. */
5712 struct attribute *attr;
5713
5714 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5715 if (attr == NULL)
5716 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5717 if (attr && DW_STRING (attr))
5718 return DW_STRING (attr);
5719 }
5720
5721 /* These are the only languages we know how to qualify names in. */
5722 if (name != NULL
5723 && (cu->language == language_cplus || cu->language == language_java
5724 || cu->language == language_fortran))
5725 {
5726 if (die_needs_namespace (die, cu))
5727 {
5728 long length;
5729 const char *prefix;
5730 struct ui_file *buf;
5731
5732 prefix = determine_prefix (die, cu);
5733 buf = mem_fileopen ();
5734 if (*prefix != '\0')
5735 {
5736 char *prefixed_name = typename_concat (NULL, prefix, name,
5737 physname, cu);
5738
5739 fputs_unfiltered (prefixed_name, buf);
5740 xfree (prefixed_name);
5741 }
5742 else
5743 fputs_unfiltered (name, buf);
5744
5745 /* Template parameters may be specified in the DIE's DW_AT_name, or
5746 as children with DW_TAG_template_type_param or
5747 DW_TAG_value_type_param. If the latter, add them to the name
5748 here. If the name already has template parameters, then
5749 skip this step; some versions of GCC emit both, and
5750 it is more efficient to use the pre-computed name.
5751
5752 Something to keep in mind about this process: it is very
5753 unlikely, or in some cases downright impossible, to produce
5754 something that will match the mangled name of a function.
5755 If the definition of the function has the same debug info,
5756 we should be able to match up with it anyway. But fallbacks
5757 using the minimal symbol, for instance to find a method
5758 implemented in a stripped copy of libstdc++, will not work.
5759 If we do not have debug info for the definition, we will have to
5760 match them up some other way.
5761
5762 When we do name matching there is a related problem with function
5763 templates; two instantiated function templates are allowed to
5764 differ only by their return types, which we do not add here. */
5765
5766 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5767 {
5768 struct attribute *attr;
5769 struct die_info *child;
5770 int first = 1;
5771
5772 die->building_fullname = 1;
5773
5774 for (child = die->child; child != NULL; child = child->sibling)
5775 {
5776 struct type *type;
5777 LONGEST value;
5778 gdb_byte *bytes;
5779 struct dwarf2_locexpr_baton *baton;
5780 struct value *v;
5781
5782 if (child->tag != DW_TAG_template_type_param
5783 && child->tag != DW_TAG_template_value_param)
5784 continue;
5785
5786 if (first)
5787 {
5788 fputs_unfiltered ("<", buf);
5789 first = 0;
5790 }
5791 else
5792 fputs_unfiltered (", ", buf);
5793
5794 attr = dwarf2_attr (child, DW_AT_type, cu);
5795 if (attr == NULL)
5796 {
5797 complaint (&symfile_complaints,
5798 _("template parameter missing DW_AT_type"));
5799 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5800 continue;
5801 }
5802 type = die_type (child, cu);
5803
5804 if (child->tag == DW_TAG_template_type_param)
5805 {
5806 c_print_type (type, "", buf, -1, 0);
5807 continue;
5808 }
5809
5810 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5811 if (attr == NULL)
5812 {
5813 complaint (&symfile_complaints,
5814 _("template parameter missing "
5815 "DW_AT_const_value"));
5816 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5817 continue;
5818 }
5819
5820 dwarf2_const_value_attr (attr, type, name,
5821 &cu->comp_unit_obstack, cu,
5822 &value, &bytes, &baton);
5823
5824 if (TYPE_NOSIGN (type))
5825 /* GDB prints characters as NUMBER 'CHAR'. If that's
5826 changed, this can use value_print instead. */
5827 c_printchar (value, type, buf);
5828 else
5829 {
5830 struct value_print_options opts;
5831
5832 if (baton != NULL)
5833 v = dwarf2_evaluate_loc_desc (type, NULL,
5834 baton->data,
5835 baton->size,
5836 baton->per_cu);
5837 else if (bytes != NULL)
5838 {
5839 v = allocate_value (type);
5840 memcpy (value_contents_writeable (v), bytes,
5841 TYPE_LENGTH (type));
5842 }
5843 else
5844 v = value_from_longest (type, value);
5845
5846 /* Specify decimal so that we do not depend on
5847 the radix. */
5848 get_formatted_print_options (&opts, 'd');
5849 opts.raw = 1;
5850 value_print (v, buf, &opts);
5851 release_value (v);
5852 value_free (v);
5853 }
5854 }
5855
5856 die->building_fullname = 0;
5857
5858 if (!first)
5859 {
5860 /* Close the argument list, with a space if necessary
5861 (nested templates). */
5862 char last_char = '\0';
5863 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5864 if (last_char == '>')
5865 fputs_unfiltered (" >", buf);
5866 else
5867 fputs_unfiltered (">", buf);
5868 }
5869 }
5870
5871 /* For Java and C++ methods, append formal parameter type
5872 information, if PHYSNAME. */
5873
5874 if (physname && die->tag == DW_TAG_subprogram
5875 && (cu->language == language_cplus
5876 || cu->language == language_java))
5877 {
5878 struct type *type = read_type_die (die, cu);
5879
5880 c_type_print_args (type, buf, 1, cu->language);
5881
5882 if (cu->language == language_java)
5883 {
5884 /* For java, we must append the return type to method
5885 names. */
5886 if (die->tag == DW_TAG_subprogram)
5887 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5888 0, 0);
5889 }
5890 else if (cu->language == language_cplus)
5891 {
5892 /* Assume that an artificial first parameter is
5893 "this", but do not crash if it is not. RealView
5894 marks unnamed (and thus unused) parameters as
5895 artificial; there is no way to differentiate
5896 the two cases. */
5897 if (TYPE_NFIELDS (type) > 0
5898 && TYPE_FIELD_ARTIFICIAL (type, 0)
5899 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5900 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5901 0))))
5902 fputs_unfiltered (" const", buf);
5903 }
5904 }
5905
5906 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5907 &length);
5908 ui_file_delete (buf);
5909
5910 if (cu->language == language_cplus)
5911 {
5912 char *cname
5913 = dwarf2_canonicalize_name (name, cu,
5914 &objfile->objfile_obstack);
5915
5916 if (cname != NULL)
5917 name = cname;
5918 }
5919 }
5920 }
5921
5922 return name;
5923}
5924
5925/* Return the fully qualified name of DIE, based on its DW_AT_name.
5926 If scope qualifiers are appropriate they will be added. The result
5927 will be allocated on the objfile_obstack, or NULL if the DIE does
5928 not have a name. NAME may either be from a previous call to
5929 dwarf2_name or NULL.
5930
5931 The output string will be canonicalized (if C++/Java). */
5932
5933static const char *
5934dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5935{
5936 return dwarf2_compute_name (name, die, cu, 0);
5937}
5938
5939/* Construct a physname for the given DIE in CU. NAME may either be
5940 from a previous call to dwarf2_name or NULL. The result will be
5941 allocated on the objfile_objstack or NULL if the DIE does not have a
5942 name.
5943
5944 The output string will be canonicalized (if C++/Java). */
5945
5946static const char *
5947dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5948{
5949 struct objfile *objfile = cu->objfile;
5950 struct attribute *attr;
5951 const char *retval, *mangled = NULL, *canon = NULL;
5952 struct cleanup *back_to;
5953 int need_copy = 1;
5954
5955 /* In this case dwarf2_compute_name is just a shortcut not building anything
5956 on its own. */
5957 if (!die_needs_namespace (die, cu))
5958 return dwarf2_compute_name (name, die, cu, 1);
5959
5960 back_to = make_cleanup (null_cleanup, NULL);
5961
5962 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5963 if (!attr)
5964 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5965
5966 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5967 has computed. */
5968 if (attr && DW_STRING (attr))
5969 {
5970 char *demangled;
5971
5972 mangled = DW_STRING (attr);
5973
5974 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5975 type. It is easier for GDB users to search for such functions as
5976 `name(params)' than `long name(params)'. In such case the minimal
5977 symbol names do not match the full symbol names but for template
5978 functions there is never a need to look up their definition from their
5979 declaration so the only disadvantage remains the minimal symbol
5980 variant `long name(params)' does not have the proper inferior type.
5981 */
5982
5983 if (cu->language == language_go)
5984 {
5985 /* This is a lie, but we already lie to the caller new_symbol_full.
5986 new_symbol_full assumes we return the mangled name.
5987 This just undoes that lie until things are cleaned up. */
5988 demangled = NULL;
5989 }
5990 else
5991 {
5992 demangled = cplus_demangle (mangled,
5993 (DMGL_PARAMS | DMGL_ANSI
5994 | (cu->language == language_java
5995 ? DMGL_JAVA | DMGL_RET_POSTFIX
5996 : DMGL_RET_DROP)));
5997 }
5998 if (demangled)
5999 {
6000 make_cleanup (xfree, demangled);
6001 canon = demangled;
6002 }
6003 else
6004 {
6005 canon = mangled;
6006 need_copy = 0;
6007 }
6008 }
6009
6010 if (canon == NULL || check_physname)
6011 {
6012 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6013
6014 if (canon != NULL && strcmp (physname, canon) != 0)
6015 {
6016 /* It may not mean a bug in GDB. The compiler could also
6017 compute DW_AT_linkage_name incorrectly. But in such case
6018 GDB would need to be bug-to-bug compatible. */
6019
6020 complaint (&symfile_complaints,
6021 _("Computed physname <%s> does not match demangled <%s> "
6022 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6023 physname, canon, mangled, die->offset.sect_off, objfile->name);
6024
6025 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6026 is available here - over computed PHYSNAME. It is safer
6027 against both buggy GDB and buggy compilers. */
6028
6029 retval = canon;
6030 }
6031 else
6032 {
6033 retval = physname;
6034 need_copy = 0;
6035 }
6036 }
6037 else
6038 retval = canon;
6039
6040 if (need_copy)
6041 retval = obsavestring (retval, strlen (retval),
6042 &objfile->objfile_obstack);
6043
6044 do_cleanups (back_to);
6045 return retval;
6046}
6047
6048/* Read the import statement specified by the given die and record it. */
6049
6050static void
6051read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6052{
6053 struct objfile *objfile = cu->objfile;
6054 struct attribute *import_attr;
6055 struct die_info *imported_die, *child_die;
6056 struct dwarf2_cu *imported_cu;
6057 const char *imported_name;
6058 const char *imported_name_prefix;
6059 const char *canonical_name;
6060 const char *import_alias;
6061 const char *imported_declaration = NULL;
6062 const char *import_prefix;
6063 VEC (const_char_ptr) *excludes = NULL;
6064 struct cleanup *cleanups;
6065
6066 char *temp;
6067
6068 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6069 if (import_attr == NULL)
6070 {
6071 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6072 dwarf_tag_name (die->tag));
6073 return;
6074 }
6075
6076 imported_cu = cu;
6077 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6078 imported_name = dwarf2_name (imported_die, imported_cu);
6079 if (imported_name == NULL)
6080 {
6081 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6082
6083 The import in the following code:
6084 namespace A
6085 {
6086 typedef int B;
6087 }
6088
6089 int main ()
6090 {
6091 using A::B;
6092 B b;
6093 return b;
6094 }
6095
6096 ...
6097 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6098 <52> DW_AT_decl_file : 1
6099 <53> DW_AT_decl_line : 6
6100 <54> DW_AT_import : <0x75>
6101 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6102 <59> DW_AT_name : B
6103 <5b> DW_AT_decl_file : 1
6104 <5c> DW_AT_decl_line : 2
6105 <5d> DW_AT_type : <0x6e>
6106 ...
6107 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6108 <76> DW_AT_byte_size : 4
6109 <77> DW_AT_encoding : 5 (signed)
6110
6111 imports the wrong die ( 0x75 instead of 0x58 ).
6112 This case will be ignored until the gcc bug is fixed. */
6113 return;
6114 }
6115
6116 /* Figure out the local name after import. */
6117 import_alias = dwarf2_name (die, cu);
6118
6119 /* Figure out where the statement is being imported to. */
6120 import_prefix = determine_prefix (die, cu);
6121
6122 /* Figure out what the scope of the imported die is and prepend it
6123 to the name of the imported die. */
6124 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6125
6126 if (imported_die->tag != DW_TAG_namespace
6127 && imported_die->tag != DW_TAG_module)
6128 {
6129 imported_declaration = imported_name;
6130 canonical_name = imported_name_prefix;
6131 }
6132 else if (strlen (imported_name_prefix) > 0)
6133 {
6134 temp = alloca (strlen (imported_name_prefix)
6135 + 2 + strlen (imported_name) + 1);
6136 strcpy (temp, imported_name_prefix);
6137 strcat (temp, "::");
6138 strcat (temp, imported_name);
6139 canonical_name = temp;
6140 }
6141 else
6142 canonical_name = imported_name;
6143
6144 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6145
6146 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6147 for (child_die = die->child; child_die && child_die->tag;
6148 child_die = sibling_die (child_die))
6149 {
6150 /* DWARF-4: A Fortran use statement with a “rename list” may be
6151 represented by an imported module entry with an import attribute
6152 referring to the module and owned entries corresponding to those
6153 entities that are renamed as part of being imported. */
6154
6155 if (child_die->tag != DW_TAG_imported_declaration)
6156 {
6157 complaint (&symfile_complaints,
6158 _("child DW_TAG_imported_declaration expected "
6159 "- DIE at 0x%x [in module %s]"),
6160 child_die->offset.sect_off, objfile->name);
6161 continue;
6162 }
6163
6164 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6165 if (import_attr == NULL)
6166 {
6167 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6168 dwarf_tag_name (child_die->tag));
6169 continue;
6170 }
6171
6172 imported_cu = cu;
6173 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6174 &imported_cu);
6175 imported_name = dwarf2_name (imported_die, imported_cu);
6176 if (imported_name == NULL)
6177 {
6178 complaint (&symfile_complaints,
6179 _("child DW_TAG_imported_declaration has unknown "
6180 "imported name - DIE at 0x%x [in module %s]"),
6181 child_die->offset.sect_off, objfile->name);
6182 continue;
6183 }
6184
6185 VEC_safe_push (const_char_ptr, excludes, imported_name);
6186
6187 process_die (child_die, cu);
6188 }
6189
6190 cp_add_using_directive (import_prefix,
6191 canonical_name,
6192 import_alias,
6193 imported_declaration,
6194 excludes,
6195 &objfile->objfile_obstack);
6196
6197 do_cleanups (cleanups);
6198}
6199
6200/* Cleanup function for read_file_scope. */
6201
6202static void
6203free_cu_line_header (void *arg)
6204{
6205 struct dwarf2_cu *cu = arg;
6206
6207 free_line_header (cu->line_header);
6208 cu->line_header = NULL;
6209}
6210
6211static void
6212find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6213 char **name, char **comp_dir)
6214{
6215 struct attribute *attr;
6216
6217 *name = NULL;
6218 *comp_dir = NULL;
6219
6220 /* Find the filename. Do not use dwarf2_name here, since the filename
6221 is not a source language identifier. */
6222 attr = dwarf2_attr (die, DW_AT_name, cu);
6223 if (attr)
6224 {
6225 *name = DW_STRING (attr);
6226 }
6227
6228 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6229 if (attr)
6230 *comp_dir = DW_STRING (attr);
6231 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6232 {
6233 *comp_dir = ldirname (*name);
6234 if (*comp_dir != NULL)
6235 make_cleanup (xfree, *comp_dir);
6236 }
6237 if (*comp_dir != NULL)
6238 {
6239 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6240 directory, get rid of it. */
6241 char *cp = strchr (*comp_dir, ':');
6242
6243 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6244 *comp_dir = cp + 1;
6245 }
6246
6247 if (*name == NULL)
6248 *name = "<unknown>";
6249}
6250
6251/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6252 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6253 COMP_DIR is the compilation directory.
6254 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6255
6256static void
6257handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6258 const char *comp_dir, int want_line_info)
6259{
6260 struct attribute *attr;
6261
6262 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6263 if (attr)
6264 {
6265 unsigned int line_offset = DW_UNSND (attr);
6266 struct line_header *line_header
6267 = dwarf_decode_line_header (line_offset, cu);
6268
6269 if (line_header)
6270 {
6271 cu->line_header = line_header;
6272 make_cleanup (free_cu_line_header, cu);
6273 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6274 }
6275 }
6276}
6277
6278/* Process DW_TAG_compile_unit. */
6279
6280static void
6281read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6282{
6283 struct objfile *objfile = dwarf2_per_objfile->objfile;
6284 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6285 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6286 CORE_ADDR highpc = ((CORE_ADDR) 0);
6287 struct attribute *attr;
6288 char *name = NULL;
6289 char *comp_dir = NULL;
6290 struct die_info *child_die;
6291 bfd *abfd = objfile->obfd;
6292 CORE_ADDR baseaddr;
6293
6294 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6295
6296 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6297
6298 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6299 from finish_block. */
6300 if (lowpc == ((CORE_ADDR) -1))
6301 lowpc = highpc;
6302 lowpc += baseaddr;
6303 highpc += baseaddr;
6304
6305 find_file_and_directory (die, cu, &name, &comp_dir);
6306
6307 prepare_one_comp_unit (cu, die);
6308
6309 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6310 standardised yet. As a workaround for the language detection we fall
6311 back to the DW_AT_producer string. */
6312 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6313 cu->language = language_opencl;
6314
6315 /* Similar hack for Go. */
6316 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6317 set_cu_language (DW_LANG_Go, cu);
6318
6319 /* We assume that we're processing GCC output. */
6320 processing_gcc_compilation = 2;
6321
6322 processing_has_namespace_info = 0;
6323
6324 start_symtab (name, comp_dir, lowpc);
6325 record_debugformat ("DWARF 2");
6326 record_producer (cu->producer);
6327
6328 /* Decode line number information if present. We do this before
6329 processing child DIEs, so that the line header table is available
6330 for DW_AT_decl_file. */
6331 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6332
6333 /* Process all dies in compilation unit. */
6334 if (die->child != NULL)
6335 {
6336 child_die = die->child;
6337 while (child_die && child_die->tag)
6338 {
6339 process_die (child_die, cu);
6340 child_die = sibling_die (child_die);
6341 }
6342 }
6343
6344 /* Decode macro information, if present. Dwarf 2 macro information
6345 refers to information in the line number info statement program
6346 header, so we can only read it if we've read the header
6347 successfully. */
6348 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6349 if (attr && cu->line_header)
6350 {
6351 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6352 complaint (&symfile_complaints,
6353 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6354
6355 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6356 comp_dir, abfd, cu,
6357 &dwarf2_per_objfile->macro, 1);
6358 }
6359 else
6360 {
6361 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6362 if (attr && cu->line_header)
6363 {
6364 unsigned int macro_offset = DW_UNSND (attr);
6365
6366 dwarf_decode_macros (cu->line_header, macro_offset,
6367 comp_dir, abfd, cu,
6368 &dwarf2_per_objfile->macinfo, 0);
6369 }
6370 }
6371
6372 do_cleanups (back_to);
6373}
6374
6375/* Process DW_TAG_type_unit.
6376 For TUs we want to skip the first top level sibling if it's not the
6377 actual type being defined by this TU. In this case the first top
6378 level sibling is there to provide context only. */
6379
6380static void
6381read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6382{
6383 struct objfile *objfile = cu->objfile;
6384 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6385 CORE_ADDR lowpc;
6386 struct attribute *attr;
6387 char *name = NULL;
6388 char *comp_dir = NULL;
6389 struct die_info *child_die;
6390 bfd *abfd = objfile->obfd;
6391
6392 /* start_symtab needs a low pc, but we don't really have one.
6393 Do what read_file_scope would do in the absence of such info. */
6394 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6395
6396 /* Find the filename. Do not use dwarf2_name here, since the filename
6397 is not a source language identifier. */
6398 attr = dwarf2_attr (die, DW_AT_name, cu);
6399 if (attr)
6400 name = DW_STRING (attr);
6401
6402 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6403 if (attr)
6404 comp_dir = DW_STRING (attr);
6405 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6406 {
6407 comp_dir = ldirname (name);
6408 if (comp_dir != NULL)
6409 make_cleanup (xfree, comp_dir);
6410 }
6411
6412 if (name == NULL)
6413 name = "<unknown>";
6414
6415 prepare_one_comp_unit (cu, die);
6416
6417 /* We assume that we're processing GCC output. */
6418 processing_gcc_compilation = 2;
6419
6420 processing_has_namespace_info = 0;
6421
6422 start_symtab (name, comp_dir, lowpc);
6423 record_debugformat ("DWARF 2");
6424 record_producer (cu->producer);
6425
6426 /* Decode line number information if present. We do this before
6427 processing child DIEs, so that the line header table is available
6428 for DW_AT_decl_file.
6429 We don't need the pc/line-number mapping for type units. */
6430 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6431
6432 /* Process the dies in the type unit. */
6433 if (die->child == NULL)
6434 {
6435 dump_die_for_error (die);
6436 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6437 bfd_get_filename (abfd));
6438 }
6439
6440 child_die = die->child;
6441
6442 while (child_die && child_die->tag)
6443 {
6444 process_die (child_die, cu);
6445
6446 child_die = sibling_die (child_die);
6447 }
6448
6449 do_cleanups (back_to);
6450}
6451\f
6452/* DWO files. */
6453
6454static hashval_t
6455hash_dwo_file (const void *item)
6456{
6457 const struct dwo_file *dwo_file = item;
6458
6459 return htab_hash_string (dwo_file->dwo_name);
6460}
6461
6462static int
6463eq_dwo_file (const void *item_lhs, const void *item_rhs)
6464{
6465 const struct dwo_file *lhs = item_lhs;
6466 const struct dwo_file *rhs = item_rhs;
6467
6468 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6469}
6470
6471/* Allocate a hash table for DWO files. */
6472
6473static htab_t
6474allocate_dwo_file_hash_table (void)
6475{
6476 struct objfile *objfile = dwarf2_per_objfile->objfile;
6477
6478 return htab_create_alloc_ex (41,
6479 hash_dwo_file,
6480 eq_dwo_file,
6481 NULL,
6482 &objfile->objfile_obstack,
6483 hashtab_obstack_allocate,
6484 dummy_obstack_deallocate);
6485}
6486
6487static hashval_t
6488hash_dwo_unit (const void *item)
6489{
6490 const struct dwo_unit *dwo_unit = item;
6491
6492 /* This drops the top 32 bits of the id, but is ok for a hash. */
6493 return dwo_unit->signature;
6494}
6495
6496static int
6497eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6498{
6499 const struct dwo_unit *lhs = item_lhs;
6500 const struct dwo_unit *rhs = item_rhs;
6501
6502 /* The signature is assumed to be unique within the DWO file.
6503 So while object file CU dwo_id's always have the value zero,
6504 that's OK, assuming each object file DWO file has only one CU,
6505 and that's the rule for now. */
6506 return lhs->signature == rhs->signature;
6507}
6508
6509/* Allocate a hash table for DWO CUs,TUs.
6510 There is one of these tables for each of CUs,TUs for each DWO file. */
6511
6512static htab_t
6513allocate_dwo_unit_table (struct objfile *objfile)
6514{
6515 /* Start out with a pretty small number.
6516 Generally DWO files contain only one CU and maybe some TUs. */
6517 return htab_create_alloc_ex (3,
6518 hash_dwo_unit,
6519 eq_dwo_unit,
6520 NULL,
6521 &objfile->objfile_obstack,
6522 hashtab_obstack_allocate,
6523 dummy_obstack_deallocate);
6524}
6525
6526/* This function is mapped across the sections and remembers the offset and
6527 size of each of the DWO debugging sections we are interested in. */
6528
6529static void
6530dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6531{
6532 struct dwo_file *dwo_file = dwo_file_ptr;
6533 const struct dwo_section_names *names = &dwo_section_names;
6534
6535 if (section_is_p (sectp->name, &names->abbrev_dwo))
6536 {
6537 dwo_file->sections.abbrev.asection = sectp;
6538 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6539 }
6540 else if (section_is_p (sectp->name, &names->info_dwo))
6541 {
6542 dwo_file->sections.info.asection = sectp;
6543 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6544 }
6545 else if (section_is_p (sectp->name, &names->line_dwo))
6546 {
6547 dwo_file->sections.line.asection = sectp;
6548 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6549 }
6550 else if (section_is_p (sectp->name, &names->loc_dwo))
6551 {
6552 dwo_file->sections.loc.asection = sectp;
6553 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6554 }
6555 else if (section_is_p (sectp->name, &names->str_dwo))
6556 {
6557 dwo_file->sections.str.asection = sectp;
6558 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6559 }
6560 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6561 {
6562 dwo_file->sections.str_offsets.asection = sectp;
6563 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6564 }
6565 else if (section_is_p (sectp->name, &names->types_dwo))
6566 {
6567 struct dwarf2_section_info type_section;
6568
6569 memset (&type_section, 0, sizeof (type_section));
6570 type_section.asection = sectp;
6571 type_section.size = bfd_get_section_size (sectp);
6572 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6573 &type_section);
6574 }
6575}
6576
6577/* Structure used to pass data to create_debug_info_hash_table_reader. */
6578
6579struct create_dwo_info_table_data
6580{
6581 struct dwo_file *dwo_file;
6582 htab_t cu_htab;
6583};
6584
6585/* die_reader_func for create_debug_info_hash_table. */
6586
6587static void
6588create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6589 gdb_byte *info_ptr,
6590 struct die_info *comp_unit_die,
6591 int has_children,
6592 void *datap)
6593{
6594 struct dwarf2_cu *cu = reader->cu;
6595 struct objfile *objfile = dwarf2_per_objfile->objfile;
6596 sect_offset offset = cu->per_cu->offset;
6597 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6598 struct create_dwo_info_table_data *data = datap;
6599 struct dwo_file *dwo_file = data->dwo_file;
6600 htab_t cu_htab = data->cu_htab;
6601 void **slot;
6602 struct attribute *attr;
6603 struct dwo_unit *dwo_unit;
6604
6605 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6606 if (attr == NULL)
6607 {
6608 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6609 " its dwo_id [in module %s]"),
6610 offset.sect_off, dwo_file->dwo_name);
6611 return;
6612 }
6613
6614 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6615 dwo_unit->dwo_file = dwo_file;
6616 dwo_unit->signature = DW_UNSND (attr);
6617 dwo_unit->info_or_types_section = section;
6618 dwo_unit->offset = offset;
6619 dwo_unit->length = cu->per_cu->length;
6620
6621 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6622 gdb_assert (slot != NULL);
6623 if (*slot != NULL)
6624 {
6625 const struct dwo_unit *dup_dwo_unit = *slot;
6626
6627 complaint (&symfile_complaints,
6628 _("debug entry at offset 0x%x is duplicate to the entry at"
6629 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6630 offset.sect_off, dup_dwo_unit->offset.sect_off,
6631 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6632 dwo_file->dwo_name);
6633 }
6634 else
6635 *slot = dwo_unit;
6636
6637 if (dwarf2_die_debug)
6638 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6639 offset.sect_off,
6640 phex (dwo_unit->signature,
6641 sizeof (dwo_unit->signature)));
6642}
6643
6644/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6645
6646static htab_t
6647create_debug_info_hash_table (struct dwo_file *dwo_file)
6648{
6649 struct objfile *objfile = dwarf2_per_objfile->objfile;
6650 struct dwarf2_section_info *section = &dwo_file->sections.info;
6651 bfd *abfd;
6652 htab_t cu_htab;
6653 gdb_byte *info_ptr, *end_ptr;
6654 struct create_dwo_info_table_data create_dwo_info_table_data;
6655
6656 dwarf2_read_section (objfile, section);
6657 info_ptr = section->buffer;
6658
6659 if (info_ptr == NULL)
6660 return NULL;
6661
6662 /* We can't set abfd until now because the section may be empty or
6663 not present, in which case section->asection will be NULL. */
6664 abfd = section->asection->owner;
6665
6666 if (dwarf2_die_debug)
6667 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6668 bfd_get_filename (abfd));
6669
6670 cu_htab = allocate_dwo_unit_table (objfile);
6671
6672 create_dwo_info_table_data.dwo_file = dwo_file;
6673 create_dwo_info_table_data.cu_htab = cu_htab;
6674
6675 end_ptr = info_ptr + section->size;
6676 while (info_ptr < end_ptr)
6677 {
6678 struct dwarf2_per_cu_data per_cu;
6679
6680 memset (&per_cu, 0, sizeof (per_cu));
6681 per_cu.objfile = objfile;
6682 per_cu.is_debug_types = 0;
6683 per_cu.offset.sect_off = info_ptr - section->buffer;
6684 per_cu.info_or_types_section = section;
6685
6686 init_cutu_and_read_dies_no_follow (&per_cu,
6687 &dwo_file->sections.abbrev,
6688 dwo_file,
6689 create_debug_info_hash_table_reader,
6690 &create_dwo_info_table_data);
6691
6692 info_ptr += per_cu.length;
6693 }
6694
6695 return cu_htab;
6696}
6697
6698/* Subroutine of open_dwo_file to simplify it.
6699 Open the file specified by FILE_NAME and hand it off to BFD for
6700 preliminary analysis. Return a newly initialized bfd *, which
6701 includes a canonicalized copy of FILE_NAME.
6702 In case of trouble, return NULL.
6703 NOTE: This function is derived from symfile_bfd_open. */
6704
6705static bfd *
6706try_open_dwo_file (const char *file_name)
6707{
6708 bfd *sym_bfd;
6709 int desc;
6710 char *absolute_name;
6711 char *name;
6712
6713 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6714 O_RDONLY | O_BINARY, &absolute_name);
6715 if (desc < 0)
6716 return NULL;
6717
6718 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6719 if (!sym_bfd)
6720 {
6721 close (desc);
6722 xfree (absolute_name);
6723 return NULL;
6724 }
6725 bfd_set_cacheable (sym_bfd, 1);
6726
6727 if (!bfd_check_format (sym_bfd, bfd_object))
6728 {
6729 bfd_close (sym_bfd); /* This also closes desc. */
6730 xfree (absolute_name);
6731 return NULL;
6732 }
6733
6734 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6735 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6736
6737 return sym_bfd;
6738}
6739
6740/* Try to open DWO file DWO_NAME.
6741 COMP_DIR is the DW_AT_comp_dir attribute.
6742 The result is the bfd handle of the file.
6743 If there is a problem finding or opening the file, return NULL.
6744 Upon success, the canonicalized path of the file is stored in the bfd,
6745 same as symfile_bfd_open. */
6746
6747static bfd *
6748open_dwo_file (const char *dwo_name, const char *comp_dir)
6749{
6750 bfd *abfd;
6751 char *path_to_try, *debug_dir;
6752
6753 if (IS_ABSOLUTE_PATH (dwo_name))
6754 return try_open_dwo_file (dwo_name);
6755
6756 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6757
6758 if (comp_dir != NULL)
6759 {
6760 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6761
6762 /* NOTE: If comp_dir is a relative path, this will also try the
6763 search path, which seems useful. */
6764 abfd = try_open_dwo_file (path_to_try);
6765 xfree (path_to_try);
6766 if (abfd != NULL)
6767 return abfd;
6768 }
6769
6770 /* That didn't work, try debug-file-directory, which, despite its name,
6771 is a list of paths. */
6772
6773 if (*debug_file_directory == '\0')
6774 return NULL;
6775
6776 return try_open_dwo_file (dwo_name);
6777}
6778
6779/* Initialize the use of the DWO file specified by DWO_NAME. */
6780
6781static struct dwo_file *
6782init_dwo_file (const char *dwo_name, const char *comp_dir)
6783{
6784 struct objfile *objfile = dwarf2_per_objfile->objfile;
6785 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6786 struct dwo_file);
6787 bfd *abfd;
6788 struct cleanup *cleanups;
6789
6790 if (dwarf2_die_debug)
6791 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
6792
6793 abfd = open_dwo_file (dwo_name, comp_dir);
6794 if (abfd == NULL)
6795 return NULL;
6796 dwo_file->dwo_name = dwo_name;
6797 dwo_file->dwo_bfd = abfd;
6798
6799 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
6800
6801 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
6802
6803 dwo_file->cus = create_debug_info_hash_table (dwo_file);
6804
6805 dwo_file->tus = create_debug_types_hash_table (dwo_file,
6806 dwo_file->sections.types);
6807
6808 discard_cleanups (cleanups);
6809
6810 return dwo_file;
6811}
6812
6813/* Lookup DWO file DWO_NAME. */
6814
6815static struct dwo_file *
6816lookup_dwo_file (char *dwo_name, const char *comp_dir)
6817{
6818 struct dwo_file *dwo_file;
6819 struct dwo_file find_entry;
6820 void **slot;
6821
6822 if (dwarf2_per_objfile->dwo_files == NULL)
6823 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
6824
6825 /* Have we already seen this DWO file? */
6826 find_entry.dwo_name = dwo_name;
6827 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
6828
6829 /* If not, read it in and build a table of the DWOs it contains. */
6830 if (*slot == NULL)
6831 *slot = init_dwo_file (dwo_name, comp_dir);
6832
6833 /* NOTE: This will be NULL if unable to open the file. */
6834 dwo_file = *slot;
6835
6836 return dwo_file;
6837}
6838
6839/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
6840 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
6841 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
6842 nomenclature as TUs).
6843 The result is the DWO CU or NULL if we didn't find it
6844 (dwo_id mismatch or couldn't find the DWO file). */
6845
6846static struct dwo_unit *
6847lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
6848 char *dwo_name, const char *comp_dir,
6849 ULONGEST signature)
6850{
6851 struct objfile *objfile = dwarf2_per_objfile->objfile;
6852 struct dwo_file *dwo_file;
6853
6854 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
6855 if (dwo_file == NULL)
6856 return NULL;
6857
6858 /* Look up the DWO using its signature(dwo_id). */
6859
6860 if (dwo_file->cus != NULL)
6861 {
6862 struct dwo_unit find_dwo_cu, *dwo_cu;
6863
6864 find_dwo_cu.signature = signature;
6865 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
6866
6867 if (dwo_cu != NULL)
6868 return dwo_cu;
6869 }
6870
6871 /* We didn't find it. This must mean a dwo_id mismatch. */
6872
6873 complaint (&symfile_complaints,
6874 _("Could not find DWO CU referenced by CU at offset 0x%x"
6875 " [in module %s]"),
6876 this_cu->offset.sect_off, objfile->name);
6877 return NULL;
6878}
6879
6880/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
6881 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
6882 The result is the DWO CU or NULL if we didn't find it
6883 (dwo_id mismatch or couldn't find the DWO file). */
6884
6885static struct dwo_unit *
6886lookup_dwo_type_unit (struct signatured_type *this_tu,
6887 char *dwo_name, const char *comp_dir)
6888{
6889 struct objfile *objfile = dwarf2_per_objfile->objfile;
6890 struct dwo_file *dwo_file;
6891 struct dwo_unit find_dwo_tu, *dwo_tu;
6892
6893 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
6894 if (dwo_file == NULL)
6895 return NULL;
6896
6897 /* Look up the DWO using its signature(dwo_id). */
6898
6899 if (dwo_file->tus != NULL)
6900 {
6901 struct dwo_unit find_dwo_tu, *dwo_tu;
6902
6903 find_dwo_tu.signature = this_tu->signature;
6904 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
6905
6906 if (dwo_tu != NULL)
6907 return dwo_tu;
6908 }
6909
6910 /* We didn't find it. This must mean a dwo_id mismatch. */
6911
6912 complaint (&symfile_complaints,
6913 _("Could not find DWO TU referenced by TU at offset 0x%x"
6914 " [in module %s]"),
6915 this_tu->per_cu.offset.sect_off, objfile->name);
6916 return NULL;
6917}
6918
6919/* Free all resources associated with DWO_FILE.
6920 Close the DWO file and munmap the sections.
6921 All memory should be on the objfile obstack. */
6922
6923static void
6924free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
6925{
6926 int ix;
6927 struct dwarf2_section_info *section;
6928
6929 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
6930 bfd_close (dwo_file->dwo_bfd);
6931
6932 munmap_section_buffer (&dwo_file->sections.abbrev);
6933 munmap_section_buffer (&dwo_file->sections.info);
6934 munmap_section_buffer (&dwo_file->sections.line);
6935 munmap_section_buffer (&dwo_file->sections.loc);
6936 munmap_section_buffer (&dwo_file->sections.str);
6937 munmap_section_buffer (&dwo_file->sections.str_offsets);
6938
6939 for (ix = 0;
6940 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
6941 ix, section);
6942 ++ix)
6943 munmap_section_buffer (section);
6944
6945 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
6946}
6947
6948/* Wrapper for free_dwo_file for use in cleanups. */
6949
6950static void
6951free_dwo_file_cleanup (void *arg)
6952{
6953 struct dwo_file *dwo_file = (struct dwo_file *) arg;
6954 struct objfile *objfile = dwarf2_per_objfile->objfile;
6955
6956 free_dwo_file (dwo_file, objfile);
6957}
6958
6959/* Traversal function for free_dwo_files. */
6960
6961static int
6962free_dwo_file_from_slot (void **slot, void *info)
6963{
6964 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6965 struct objfile *objfile = (struct objfile *) info;
6966
6967 free_dwo_file (dwo_file, objfile);
6968
6969 return 1;
6970}
6971
6972/* Free all resources associated with DWO_FILES. */
6973
6974static void
6975free_dwo_files (htab_t dwo_files, struct objfile *objfile)
6976{
6977 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
6978}
6979\f
6980/* Read in various DIEs. */
6981
6982/* qsort helper for inherit_abstract_dies. */
6983
6984static int
6985unsigned_int_compar (const void *ap, const void *bp)
6986{
6987 unsigned int a = *(unsigned int *) ap;
6988 unsigned int b = *(unsigned int *) bp;
6989
6990 return (a > b) - (b > a);
6991}
6992
6993/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
6994 Inherit only the children of the DW_AT_abstract_origin DIE not being
6995 already referenced by DW_AT_abstract_origin from the children of the
6996 current DIE. */
6997
6998static void
6999inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7000{
7001 struct die_info *child_die;
7002 unsigned die_children_count;
7003 /* CU offsets which were referenced by children of the current DIE. */
7004 sect_offset *offsets;
7005 sect_offset *offsets_end, *offsetp;
7006 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7007 struct die_info *origin_die;
7008 /* Iterator of the ORIGIN_DIE children. */
7009 struct die_info *origin_child_die;
7010 struct cleanup *cleanups;
7011 struct attribute *attr;
7012 struct dwarf2_cu *origin_cu;
7013 struct pending **origin_previous_list_in_scope;
7014
7015 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7016 if (!attr)
7017 return;
7018
7019 /* Note that following die references may follow to a die in a
7020 different cu. */
7021
7022 origin_cu = cu;
7023 origin_die = follow_die_ref (die, attr, &origin_cu);
7024
7025 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7026 symbols in. */
7027 origin_previous_list_in_scope = origin_cu->list_in_scope;
7028 origin_cu->list_in_scope = cu->list_in_scope;
7029
7030 if (die->tag != origin_die->tag
7031 && !(die->tag == DW_TAG_inlined_subroutine
7032 && origin_die->tag == DW_TAG_subprogram))
7033 complaint (&symfile_complaints,
7034 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7035 die->offset.sect_off, origin_die->offset.sect_off);
7036
7037 child_die = die->child;
7038 die_children_count = 0;
7039 while (child_die && child_die->tag)
7040 {
7041 child_die = sibling_die (child_die);
7042 die_children_count++;
7043 }
7044 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7045 cleanups = make_cleanup (xfree, offsets);
7046
7047 offsets_end = offsets;
7048 child_die = die->child;
7049 while (child_die && child_die->tag)
7050 {
7051 /* For each CHILD_DIE, find the corresponding child of
7052 ORIGIN_DIE. If there is more than one layer of
7053 DW_AT_abstract_origin, follow them all; there shouldn't be,
7054 but GCC versions at least through 4.4 generate this (GCC PR
7055 40573). */
7056 struct die_info *child_origin_die = child_die;
7057 struct dwarf2_cu *child_origin_cu = cu;
7058
7059 while (1)
7060 {
7061 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7062 child_origin_cu);
7063 if (attr == NULL)
7064 break;
7065 child_origin_die = follow_die_ref (child_origin_die, attr,
7066 &child_origin_cu);
7067 }
7068
7069 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7070 counterpart may exist. */
7071 if (child_origin_die != child_die)
7072 {
7073 if (child_die->tag != child_origin_die->tag
7074 && !(child_die->tag == DW_TAG_inlined_subroutine
7075 && child_origin_die->tag == DW_TAG_subprogram))
7076 complaint (&symfile_complaints,
7077 _("Child DIE 0x%x and its abstract origin 0x%x have "
7078 "different tags"), child_die->offset.sect_off,
7079 child_origin_die->offset.sect_off);
7080 if (child_origin_die->parent != origin_die)
7081 complaint (&symfile_complaints,
7082 _("Child DIE 0x%x and its abstract origin 0x%x have "
7083 "different parents"), child_die->offset.sect_off,
7084 child_origin_die->offset.sect_off);
7085 else
7086 *offsets_end++ = child_origin_die->offset;
7087 }
7088 child_die = sibling_die (child_die);
7089 }
7090 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7091 unsigned_int_compar);
7092 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7093 if (offsetp[-1].sect_off == offsetp->sect_off)
7094 complaint (&symfile_complaints,
7095 _("Multiple children of DIE 0x%x refer "
7096 "to DIE 0x%x as their abstract origin"),
7097 die->offset.sect_off, offsetp->sect_off);
7098
7099 offsetp = offsets;
7100 origin_child_die = origin_die->child;
7101 while (origin_child_die && origin_child_die->tag)
7102 {
7103 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7104 while (offsetp < offsets_end
7105 && offsetp->sect_off < origin_child_die->offset.sect_off)
7106 offsetp++;
7107 if (offsetp >= offsets_end
7108 || offsetp->sect_off > origin_child_die->offset.sect_off)
7109 {
7110 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7111 process_die (origin_child_die, origin_cu);
7112 }
7113 origin_child_die = sibling_die (origin_child_die);
7114 }
7115 origin_cu->list_in_scope = origin_previous_list_in_scope;
7116
7117 do_cleanups (cleanups);
7118}
7119
7120static void
7121read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7122{
7123 struct objfile *objfile = cu->objfile;
7124 struct context_stack *new;
7125 CORE_ADDR lowpc;
7126 CORE_ADDR highpc;
7127 struct die_info *child_die;
7128 struct attribute *attr, *call_line, *call_file;
7129 char *name;
7130 CORE_ADDR baseaddr;
7131 struct block *block;
7132 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7133 VEC (symbolp) *template_args = NULL;
7134 struct template_symbol *templ_func = NULL;
7135
7136 if (inlined_func)
7137 {
7138 /* If we do not have call site information, we can't show the
7139 caller of this inlined function. That's too confusing, so
7140 only use the scope for local variables. */
7141 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7142 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7143 if (call_line == NULL || call_file == NULL)
7144 {
7145 read_lexical_block_scope (die, cu);
7146 return;
7147 }
7148 }
7149
7150 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7151
7152 name = dwarf2_name (die, cu);
7153
7154 /* Ignore functions with missing or empty names. These are actually
7155 illegal according to the DWARF standard. */
7156 if (name == NULL)
7157 {
7158 complaint (&symfile_complaints,
7159 _("missing name for subprogram DIE at %d"),
7160 die->offset.sect_off);
7161 return;
7162 }
7163
7164 /* Ignore functions with missing or invalid low and high pc attributes. */
7165 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7166 {
7167 attr = dwarf2_attr (die, DW_AT_external, cu);
7168 if (!attr || !DW_UNSND (attr))
7169 complaint (&symfile_complaints,
7170 _("cannot get low and high bounds "
7171 "for subprogram DIE at %d"),
7172 die->offset.sect_off);
7173 return;
7174 }
7175
7176 lowpc += baseaddr;
7177 highpc += baseaddr;
7178
7179 /* If we have any template arguments, then we must allocate a
7180 different sort of symbol. */
7181 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7182 {
7183 if (child_die->tag == DW_TAG_template_type_param
7184 || child_die->tag == DW_TAG_template_value_param)
7185 {
7186 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7187 struct template_symbol);
7188 templ_func->base.is_cplus_template_function = 1;
7189 break;
7190 }
7191 }
7192
7193 new = push_context (0, lowpc);
7194 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7195 (struct symbol *) templ_func);
7196
7197 /* If there is a location expression for DW_AT_frame_base, record
7198 it. */
7199 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7200 if (attr)
7201 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7202 expression is being recorded directly in the function's symbol
7203 and not in a separate frame-base object. I guess this hack is
7204 to avoid adding some sort of frame-base adjunct/annex to the
7205 function's symbol :-(. The problem with doing this is that it
7206 results in a function symbol with a location expression that
7207 has nothing to do with the location of the function, ouch! The
7208 relationship should be: a function's symbol has-a frame base; a
7209 frame-base has-a location expression. */
7210 dwarf2_symbol_mark_computed (attr, new->name, cu);
7211
7212 cu->list_in_scope = &local_symbols;
7213
7214 if (die->child != NULL)
7215 {
7216 child_die = die->child;
7217 while (child_die && child_die->tag)
7218 {
7219 if (child_die->tag == DW_TAG_template_type_param
7220 || child_die->tag == DW_TAG_template_value_param)
7221 {
7222 struct symbol *arg = new_symbol (child_die, NULL, cu);
7223
7224 if (arg != NULL)
7225 VEC_safe_push (symbolp, template_args, arg);
7226 }
7227 else
7228 process_die (child_die, cu);
7229 child_die = sibling_die (child_die);
7230 }
7231 }
7232
7233 inherit_abstract_dies (die, cu);
7234
7235 /* If we have a DW_AT_specification, we might need to import using
7236 directives from the context of the specification DIE. See the
7237 comment in determine_prefix. */
7238 if (cu->language == language_cplus
7239 && dwarf2_attr (die, DW_AT_specification, cu))
7240 {
7241 struct dwarf2_cu *spec_cu = cu;
7242 struct die_info *spec_die = die_specification (die, &spec_cu);
7243
7244 while (spec_die)
7245 {
7246 child_die = spec_die->child;
7247 while (child_die && child_die->tag)
7248 {
7249 if (child_die->tag == DW_TAG_imported_module)
7250 process_die (child_die, spec_cu);
7251 child_die = sibling_die (child_die);
7252 }
7253
7254 /* In some cases, GCC generates specification DIEs that
7255 themselves contain DW_AT_specification attributes. */
7256 spec_die = die_specification (spec_die, &spec_cu);
7257 }
7258 }
7259
7260 new = pop_context ();
7261 /* Make a block for the local symbols within. */
7262 block = finish_block (new->name, &local_symbols, new->old_blocks,
7263 lowpc, highpc, objfile);
7264
7265 /* For C++, set the block's scope. */
7266 if (cu->language == language_cplus || cu->language == language_fortran)
7267 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7268 determine_prefix (die, cu),
7269 processing_has_namespace_info);
7270
7271 /* If we have address ranges, record them. */
7272 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7273
7274 /* Attach template arguments to function. */
7275 if (! VEC_empty (symbolp, template_args))
7276 {
7277 gdb_assert (templ_func != NULL);
7278
7279 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7280 templ_func->template_arguments
7281 = obstack_alloc (&objfile->objfile_obstack,
7282 (templ_func->n_template_arguments
7283 * sizeof (struct symbol *)));
7284 memcpy (templ_func->template_arguments,
7285 VEC_address (symbolp, template_args),
7286 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7287 VEC_free (symbolp, template_args);
7288 }
7289
7290 /* In C++, we can have functions nested inside functions (e.g., when
7291 a function declares a class that has methods). This means that
7292 when we finish processing a function scope, we may need to go
7293 back to building a containing block's symbol lists. */
7294 local_symbols = new->locals;
7295 param_symbols = new->params;
7296 using_directives = new->using_directives;
7297
7298 /* If we've finished processing a top-level function, subsequent
7299 symbols go in the file symbol list. */
7300 if (outermost_context_p ())
7301 cu->list_in_scope = &file_symbols;
7302}
7303
7304/* Process all the DIES contained within a lexical block scope. Start
7305 a new scope, process the dies, and then close the scope. */
7306
7307static void
7308read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7309{
7310 struct objfile *objfile = cu->objfile;
7311 struct context_stack *new;
7312 CORE_ADDR lowpc, highpc;
7313 struct die_info *child_die;
7314 CORE_ADDR baseaddr;
7315
7316 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7317
7318 /* Ignore blocks with missing or invalid low and high pc attributes. */
7319 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7320 as multiple lexical blocks? Handling children in a sane way would
7321 be nasty. Might be easier to properly extend generic blocks to
7322 describe ranges. */
7323 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7324 return;
7325 lowpc += baseaddr;
7326 highpc += baseaddr;
7327
7328 push_context (0, lowpc);
7329 if (die->child != NULL)
7330 {
7331 child_die = die->child;
7332 while (child_die && child_die->tag)
7333 {
7334 process_die (child_die, cu);
7335 child_die = sibling_die (child_die);
7336 }
7337 }
7338 new = pop_context ();
7339
7340 if (local_symbols != NULL || using_directives != NULL)
7341 {
7342 struct block *block
7343 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7344 highpc, objfile);
7345
7346 /* Note that recording ranges after traversing children, as we
7347 do here, means that recording a parent's ranges entails
7348 walking across all its children's ranges as they appear in
7349 the address map, which is quadratic behavior.
7350
7351 It would be nicer to record the parent's ranges before
7352 traversing its children, simply overriding whatever you find
7353 there. But since we don't even decide whether to create a
7354 block until after we've traversed its children, that's hard
7355 to do. */
7356 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7357 }
7358 local_symbols = new->locals;
7359 using_directives = new->using_directives;
7360}
7361
7362/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7363
7364static void
7365read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7366{
7367 struct objfile *objfile = cu->objfile;
7368 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7369 CORE_ADDR pc, baseaddr;
7370 struct attribute *attr;
7371 struct call_site *call_site, call_site_local;
7372 void **slot;
7373 int nparams;
7374 struct die_info *child_die;
7375
7376 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7377
7378 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7379 if (!attr)
7380 {
7381 complaint (&symfile_complaints,
7382 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7383 "DIE 0x%x [in module %s]"),
7384 die->offset.sect_off, objfile->name);
7385 return;
7386 }
7387 pc = DW_ADDR (attr) + baseaddr;
7388
7389 if (cu->call_site_htab == NULL)
7390 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7391 NULL, &objfile->objfile_obstack,
7392 hashtab_obstack_allocate, NULL);
7393 call_site_local.pc = pc;
7394 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7395 if (*slot != NULL)
7396 {
7397 complaint (&symfile_complaints,
7398 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7399 "DIE 0x%x [in module %s]"),
7400 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7401 return;
7402 }
7403
7404 /* Count parameters at the caller. */
7405
7406 nparams = 0;
7407 for (child_die = die->child; child_die && child_die->tag;
7408 child_die = sibling_die (child_die))
7409 {
7410 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7411 {
7412 complaint (&symfile_complaints,
7413 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7414 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7415 child_die->tag, child_die->offset.sect_off, objfile->name);
7416 continue;
7417 }
7418
7419 nparams++;
7420 }
7421
7422 call_site = obstack_alloc (&objfile->objfile_obstack,
7423 (sizeof (*call_site)
7424 + (sizeof (*call_site->parameter)
7425 * (nparams - 1))));
7426 *slot = call_site;
7427 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7428 call_site->pc = pc;
7429
7430 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7431 {
7432 struct die_info *func_die;
7433
7434 /* Skip also over DW_TAG_inlined_subroutine. */
7435 for (func_die = die->parent;
7436 func_die && func_die->tag != DW_TAG_subprogram
7437 && func_die->tag != DW_TAG_subroutine_type;
7438 func_die = func_die->parent);
7439
7440 /* DW_AT_GNU_all_call_sites is a superset
7441 of DW_AT_GNU_all_tail_call_sites. */
7442 if (func_die
7443 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7444 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7445 {
7446 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7447 not complete. But keep CALL_SITE for look ups via call_site_htab,
7448 both the initial caller containing the real return address PC and
7449 the final callee containing the current PC of a chain of tail
7450 calls do not need to have the tail call list complete. But any
7451 function candidate for a virtual tail call frame searched via
7452 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7453 determined unambiguously. */
7454 }
7455 else
7456 {
7457 struct type *func_type = NULL;
7458
7459 if (func_die)
7460 func_type = get_die_type (func_die, cu);
7461 if (func_type != NULL)
7462 {
7463 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7464
7465 /* Enlist this call site to the function. */
7466 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7467 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7468 }
7469 else
7470 complaint (&symfile_complaints,
7471 _("Cannot find function owning DW_TAG_GNU_call_site "
7472 "DIE 0x%x [in module %s]"),
7473 die->offset.sect_off, objfile->name);
7474 }
7475 }
7476
7477 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7478 if (attr == NULL)
7479 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7480 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7481 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7482 /* Keep NULL DWARF_BLOCK. */;
7483 else if (attr_form_is_block (attr))
7484 {
7485 struct dwarf2_locexpr_baton *dlbaton;
7486
7487 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7488 dlbaton->data = DW_BLOCK (attr)->data;
7489 dlbaton->size = DW_BLOCK (attr)->size;
7490 dlbaton->per_cu = cu->per_cu;
7491
7492 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7493 }
7494 else if (is_ref_attr (attr))
7495 {
7496 struct dwarf2_cu *target_cu = cu;
7497 struct die_info *target_die;
7498
7499 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7500 gdb_assert (target_cu->objfile == objfile);
7501 if (die_is_declaration (target_die, target_cu))
7502 {
7503 const char *target_physname;
7504
7505 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7506 if (target_physname == NULL)
7507 complaint (&symfile_complaints,
7508 _("DW_AT_GNU_call_site_target target DIE has invalid "
7509 "physname, for referencing DIE 0x%x [in module %s]"),
7510 die->offset.sect_off, objfile->name);
7511 else
7512 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7513 }
7514 else
7515 {
7516 CORE_ADDR lowpc;
7517
7518 /* DW_AT_entry_pc should be preferred. */
7519 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7520 complaint (&symfile_complaints,
7521 _("DW_AT_GNU_call_site_target target DIE has invalid "
7522 "low pc, for referencing DIE 0x%x [in module %s]"),
7523 die->offset.sect_off, objfile->name);
7524 else
7525 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7526 }
7527 }
7528 else
7529 complaint (&symfile_complaints,
7530 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7531 "block nor reference, for DIE 0x%x [in module %s]"),
7532 die->offset.sect_off, objfile->name);
7533
7534 call_site->per_cu = cu->per_cu;
7535
7536 for (child_die = die->child;
7537 child_die && child_die->tag;
7538 child_die = sibling_die (child_die))
7539 {
7540 struct dwarf2_locexpr_baton *dlbaton;
7541 struct call_site_parameter *parameter;
7542
7543 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7544 {
7545 /* Already printed the complaint above. */
7546 continue;
7547 }
7548
7549 gdb_assert (call_site->parameter_count < nparams);
7550 parameter = &call_site->parameter[call_site->parameter_count];
7551
7552 /* DW_AT_location specifies the register number. Value of the data
7553 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7554
7555 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7556 if (!attr || !attr_form_is_block (attr))
7557 {
7558 complaint (&symfile_complaints,
7559 _("No DW_FORM_block* DW_AT_location for "
7560 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7561 child_die->offset.sect_off, objfile->name);
7562 continue;
7563 }
7564 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7565 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7566 if (parameter->dwarf_reg == -1
7567 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7568 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7569 &parameter->fb_offset))
7570 {
7571 complaint (&symfile_complaints,
7572 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7573 "for DW_FORM_block* DW_AT_location for "
7574 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7575 child_die->offset.sect_off, objfile->name);
7576 continue;
7577 }
7578
7579 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7580 if (!attr_form_is_block (attr))
7581 {
7582 complaint (&symfile_complaints,
7583 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7584 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7585 child_die->offset.sect_off, objfile->name);
7586 continue;
7587 }
7588 parameter->value = DW_BLOCK (attr)->data;
7589 parameter->value_size = DW_BLOCK (attr)->size;
7590
7591 /* Parameters are not pre-cleared by memset above. */
7592 parameter->data_value = NULL;
7593 parameter->data_value_size = 0;
7594 call_site->parameter_count++;
7595
7596 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7597 if (attr)
7598 {
7599 if (!attr_form_is_block (attr))
7600 complaint (&symfile_complaints,
7601 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7602 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7603 child_die->offset.sect_off, objfile->name);
7604 else
7605 {
7606 parameter->data_value = DW_BLOCK (attr)->data;
7607 parameter->data_value_size = DW_BLOCK (attr)->size;
7608 }
7609 }
7610 }
7611}
7612
7613/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
7614 Return 1 if the attributes are present and valid, otherwise, return 0.
7615 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
7616
7617static int
7618dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
7619 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7620 struct partial_symtab *ranges_pst)
7621{
7622 struct objfile *objfile = cu->objfile;
7623 struct comp_unit_head *cu_header = &cu->header;
7624 bfd *obfd = objfile->obfd;
7625 unsigned int addr_size = cu_header->addr_size;
7626 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7627 /* Base address selection entry. */
7628 CORE_ADDR base;
7629 int found_base;
7630 unsigned int dummy;
7631 gdb_byte *buffer;
7632 CORE_ADDR marker;
7633 int low_set;
7634 CORE_ADDR low = 0;
7635 CORE_ADDR high = 0;
7636 CORE_ADDR baseaddr;
7637
7638 found_base = cu->base_known;
7639 base = cu->base_address;
7640
7641 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
7642 if (offset >= dwarf2_per_objfile->ranges.size)
7643 {
7644 complaint (&symfile_complaints,
7645 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7646 offset);
7647 return 0;
7648 }
7649 buffer = dwarf2_per_objfile->ranges.buffer + offset;
7650
7651 /* Read in the largest possible address. */
7652 marker = read_address (obfd, buffer, cu, &dummy);
7653 if ((marker & mask) == mask)
7654 {
7655 /* If we found the largest possible address, then
7656 read the base address. */
7657 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7658 buffer += 2 * addr_size;
7659 offset += 2 * addr_size;
7660 found_base = 1;
7661 }
7662
7663 low_set = 0;
7664
7665 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7666
7667 while (1)
7668 {
7669 CORE_ADDR range_beginning, range_end;
7670
7671 range_beginning = read_address (obfd, buffer, cu, &dummy);
7672 buffer += addr_size;
7673 range_end = read_address (obfd, buffer, cu, &dummy);
7674 buffer += addr_size;
7675 offset += 2 * addr_size;
7676
7677 /* An end of list marker is a pair of zero addresses. */
7678 if (range_beginning == 0 && range_end == 0)
7679 /* Found the end of list entry. */
7680 break;
7681
7682 /* Each base address selection entry is a pair of 2 values.
7683 The first is the largest possible address, the second is
7684 the base address. Check for a base address here. */
7685 if ((range_beginning & mask) == mask)
7686 {
7687 /* If we found the largest possible address, then
7688 read the base address. */
7689 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7690 found_base = 1;
7691 continue;
7692 }
7693
7694 if (!found_base)
7695 {
7696 /* We have no valid base address for the ranges
7697 data. */
7698 complaint (&symfile_complaints,
7699 _("Invalid .debug_ranges data (no base address)"));
7700 return 0;
7701 }
7702
7703 if (range_beginning > range_end)
7704 {
7705 /* Inverted range entries are invalid. */
7706 complaint (&symfile_complaints,
7707 _("Invalid .debug_ranges data (inverted range)"));
7708 return 0;
7709 }
7710
7711 /* Empty range entries have no effect. */
7712 if (range_beginning == range_end)
7713 continue;
7714
7715 range_beginning += base;
7716 range_end += base;
7717
7718 if (ranges_pst != NULL)
7719 addrmap_set_empty (objfile->psymtabs_addrmap,
7720 range_beginning + baseaddr,
7721 range_end - 1 + baseaddr,
7722 ranges_pst);
7723
7724 /* FIXME: This is recording everything as a low-high
7725 segment of consecutive addresses. We should have a
7726 data structure for discontiguous block ranges
7727 instead. */
7728 if (! low_set)
7729 {
7730 low = range_beginning;
7731 high = range_end;
7732 low_set = 1;
7733 }
7734 else
7735 {
7736 if (range_beginning < low)
7737 low = range_beginning;
7738 if (range_end > high)
7739 high = range_end;
7740 }
7741 }
7742
7743 if (! low_set)
7744 /* If the first entry is an end-of-list marker, the range
7745 describes an empty scope, i.e. no instructions. */
7746 return 0;
7747
7748 if (low_return)
7749 *low_return = low;
7750 if (high_return)
7751 *high_return = high;
7752 return 1;
7753}
7754
7755/* Get low and high pc attributes from a die. Return 1 if the attributes
7756 are present and valid, otherwise, return 0. Return -1 if the range is
7757 discontinuous, i.e. derived from DW_AT_ranges information. */
7758
7759static int
7760dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
7761 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7762 struct partial_symtab *pst)
7763{
7764 struct attribute *attr;
7765 struct attribute *attr_high;
7766 CORE_ADDR low = 0;
7767 CORE_ADDR high = 0;
7768 int ret = 0;
7769
7770 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7771 if (attr_high)
7772 {
7773 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7774 if (attr)
7775 {
7776 low = DW_ADDR (attr);
7777 if (attr_high->form == DW_FORM_addr
7778 || attr_high->form == DW_FORM_GNU_addr_index)
7779 high = DW_ADDR (attr_high);
7780 else
7781 high = low + DW_UNSND (attr_high);
7782 }
7783 else
7784 /* Found high w/o low attribute. */
7785 return 0;
7786
7787 /* Found consecutive range of addresses. */
7788 ret = 1;
7789 }
7790 else
7791 {
7792 attr = dwarf2_attr (die, DW_AT_ranges, cu);
7793 if (attr != NULL)
7794 {
7795 /* Value of the DW_AT_ranges attribute is the offset in the
7796 .debug_ranges section. */
7797 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
7798 return 0;
7799 /* Found discontinuous range of addresses. */
7800 ret = -1;
7801 }
7802 }
7803
7804 /* read_partial_die has also the strict LOW < HIGH requirement. */
7805 if (high <= low)
7806 return 0;
7807
7808 /* When using the GNU linker, .gnu.linkonce. sections are used to
7809 eliminate duplicate copies of functions and vtables and such.
7810 The linker will arbitrarily choose one and discard the others.
7811 The AT_*_pc values for such functions refer to local labels in
7812 these sections. If the section from that file was discarded, the
7813 labels are not in the output, so the relocs get a value of 0.
7814 If this is a discarded function, mark the pc bounds as invalid,
7815 so that GDB will ignore it. */
7816 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
7817 return 0;
7818
7819 *lowpc = low;
7820 if (highpc)
7821 *highpc = high;
7822 return ret;
7823}
7824
7825/* Assuming that DIE represents a subprogram DIE or a lexical block, get
7826 its low and high PC addresses. Do nothing if these addresses could not
7827 be determined. Otherwise, set LOWPC to the low address if it is smaller,
7828 and HIGHPC to the high address if greater than HIGHPC. */
7829
7830static void
7831dwarf2_get_subprogram_pc_bounds (struct die_info *die,
7832 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7833 struct dwarf2_cu *cu)
7834{
7835 CORE_ADDR low, high;
7836 struct die_info *child = die->child;
7837
7838 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
7839 {
7840 *lowpc = min (*lowpc, low);
7841 *highpc = max (*highpc, high);
7842 }
7843
7844 /* If the language does not allow nested subprograms (either inside
7845 subprograms or lexical blocks), we're done. */
7846 if (cu->language != language_ada)
7847 return;
7848
7849 /* Check all the children of the given DIE. If it contains nested
7850 subprograms, then check their pc bounds. Likewise, we need to
7851 check lexical blocks as well, as they may also contain subprogram
7852 definitions. */
7853 while (child && child->tag)
7854 {
7855 if (child->tag == DW_TAG_subprogram
7856 || child->tag == DW_TAG_lexical_block)
7857 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
7858 child = sibling_die (child);
7859 }
7860}
7861
7862/* Get the low and high pc's represented by the scope DIE, and store
7863 them in *LOWPC and *HIGHPC. If the correct values can't be
7864 determined, set *LOWPC to -1 and *HIGHPC to 0. */
7865
7866static void
7867get_scope_pc_bounds (struct die_info *die,
7868 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7869 struct dwarf2_cu *cu)
7870{
7871 CORE_ADDR best_low = (CORE_ADDR) -1;
7872 CORE_ADDR best_high = (CORE_ADDR) 0;
7873 CORE_ADDR current_low, current_high;
7874
7875 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
7876 {
7877 best_low = current_low;
7878 best_high = current_high;
7879 }
7880 else
7881 {
7882 struct die_info *child = die->child;
7883
7884 while (child && child->tag)
7885 {
7886 switch (child->tag) {
7887 case DW_TAG_subprogram:
7888 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
7889 break;
7890 case DW_TAG_namespace:
7891 case DW_TAG_module:
7892 /* FIXME: carlton/2004-01-16: Should we do this for
7893 DW_TAG_class_type/DW_TAG_structure_type, too? I think
7894 that current GCC's always emit the DIEs corresponding
7895 to definitions of methods of classes as children of a
7896 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
7897 the DIEs giving the declarations, which could be
7898 anywhere). But I don't see any reason why the
7899 standards says that they have to be there. */
7900 get_scope_pc_bounds (child, &current_low, &current_high, cu);
7901
7902 if (current_low != ((CORE_ADDR) -1))
7903 {
7904 best_low = min (best_low, current_low);
7905 best_high = max (best_high, current_high);
7906 }
7907 break;
7908 default:
7909 /* Ignore. */
7910 break;
7911 }
7912
7913 child = sibling_die (child);
7914 }
7915 }
7916
7917 *lowpc = best_low;
7918 *highpc = best_high;
7919}
7920
7921/* Record the address ranges for BLOCK, offset by BASEADDR, as given
7922 in DIE. */
7923
7924static void
7925dwarf2_record_block_ranges (struct die_info *die, struct block *block,
7926 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
7927{
7928 struct objfile *objfile = cu->objfile;
7929 struct attribute *attr;
7930 struct attribute *attr_high;
7931
7932 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7933 if (attr_high)
7934 {
7935 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7936 if (attr)
7937 {
7938 CORE_ADDR low = DW_ADDR (attr);
7939 CORE_ADDR high;
7940 if (attr_high->form == DW_FORM_addr
7941 || attr_high->form == DW_FORM_GNU_addr_index)
7942 high = DW_ADDR (attr_high);
7943 else
7944 high = low + DW_UNSND (attr_high);
7945
7946 record_block_range (block, baseaddr + low, baseaddr + high - 1);
7947 }
7948 }
7949
7950 attr = dwarf2_attr (die, DW_AT_ranges, cu);
7951 if (attr)
7952 {
7953 bfd *obfd = objfile->obfd;
7954
7955 /* The value of the DW_AT_ranges attribute is the offset of the
7956 address range list in the .debug_ranges section. */
7957 unsigned long offset = DW_UNSND (attr);
7958 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
7959
7960 /* For some target architectures, but not others, the
7961 read_address function sign-extends the addresses it returns.
7962 To recognize base address selection entries, we need a
7963 mask. */
7964 unsigned int addr_size = cu->header.addr_size;
7965 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7966
7967 /* The base address, to which the next pair is relative. Note
7968 that this 'base' is a DWARF concept: most entries in a range
7969 list are relative, to reduce the number of relocs against the
7970 debugging information. This is separate from this function's
7971 'baseaddr' argument, which GDB uses to relocate debugging
7972 information from a shared library based on the address at
7973 which the library was loaded. */
7974 CORE_ADDR base = cu->base_address;
7975 int base_known = cu->base_known;
7976
7977 gdb_assert (dwarf2_per_objfile->ranges.readin);
7978 if (offset >= dwarf2_per_objfile->ranges.size)
7979 {
7980 complaint (&symfile_complaints,
7981 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
7982 offset);
7983 return;
7984 }
7985
7986 for (;;)
7987 {
7988 unsigned int bytes_read;
7989 CORE_ADDR start, end;
7990
7991 start = read_address (obfd, buffer, cu, &bytes_read);
7992 buffer += bytes_read;
7993 end = read_address (obfd, buffer, cu, &bytes_read);
7994 buffer += bytes_read;
7995
7996 /* Did we find the end of the range list? */
7997 if (start == 0 && end == 0)
7998 break;
7999
8000 /* Did we find a base address selection entry? */
8001 else if ((start & base_select_mask) == base_select_mask)
8002 {
8003 base = end;
8004 base_known = 1;
8005 }
8006
8007 /* We found an ordinary address range. */
8008 else
8009 {
8010 if (!base_known)
8011 {
8012 complaint (&symfile_complaints,
8013 _("Invalid .debug_ranges data "
8014 "(no base address)"));
8015 return;
8016 }
8017
8018 if (start > end)
8019 {
8020 /* Inverted range entries are invalid. */
8021 complaint (&symfile_complaints,
8022 _("Invalid .debug_ranges data "
8023 "(inverted range)"));
8024 return;
8025 }
8026
8027 /* Empty range entries have no effect. */
8028 if (start == end)
8029 continue;
8030
8031 record_block_range (block,
8032 baseaddr + base + start,
8033 baseaddr + base + end - 1);
8034 }
8035 }
8036 }
8037}
8038
8039/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8040 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8041 during 4.6.0 experimental. */
8042
8043static int
8044producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8045{
8046 const char *cs;
8047 int major, minor, release;
8048 int result = 0;
8049
8050 if (cu->producer == NULL)
8051 {
8052 /* For unknown compilers expect their behavior is DWARF version
8053 compliant.
8054
8055 GCC started to support .debug_types sections by -gdwarf-4 since
8056 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8057 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8058 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8059 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8060
8061 return 0;
8062 }
8063
8064 if (cu->checked_producer)
8065 return cu->producer_is_gxx_lt_4_6;
8066
8067 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8068
8069 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8070 {
8071 /* For non-GCC compilers expect their behavior is DWARF version
8072 compliant. */
8073 }
8074 else
8075 {
8076 cs = &cu->producer[strlen ("GNU ")];
8077 while (*cs && !isdigit (*cs))
8078 cs++;
8079 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8080 {
8081 /* Not recognized as GCC. */
8082 }
8083 else
8084 result = major < 4 || (major == 4 && minor < 6);
8085 }
8086
8087 cu->checked_producer = 1;
8088 cu->producer_is_gxx_lt_4_6 = result;
8089
8090 return result;
8091}
8092
8093/* Return the default accessibility type if it is not overriden by
8094 DW_AT_accessibility. */
8095
8096static enum dwarf_access_attribute
8097dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8098{
8099 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8100 {
8101 /* The default DWARF 2 accessibility for members is public, the default
8102 accessibility for inheritance is private. */
8103
8104 if (die->tag != DW_TAG_inheritance)
8105 return DW_ACCESS_public;
8106 else
8107 return DW_ACCESS_private;
8108 }
8109 else
8110 {
8111 /* DWARF 3+ defines the default accessibility a different way. The same
8112 rules apply now for DW_TAG_inheritance as for the members and it only
8113 depends on the container kind. */
8114
8115 if (die->parent->tag == DW_TAG_class_type)
8116 return DW_ACCESS_private;
8117 else
8118 return DW_ACCESS_public;
8119 }
8120}
8121
8122/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8123 offset. If the attribute was not found return 0, otherwise return
8124 1. If it was found but could not properly be handled, set *OFFSET
8125 to 0. */
8126
8127static int
8128handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8129 LONGEST *offset)
8130{
8131 struct attribute *attr;
8132
8133 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8134 if (attr != NULL)
8135 {
8136 *offset = 0;
8137
8138 /* Note that we do not check for a section offset first here.
8139 This is because DW_AT_data_member_location is new in DWARF 4,
8140 so if we see it, we can assume that a constant form is really
8141 a constant and not a section offset. */
8142 if (attr_form_is_constant (attr))
8143 *offset = dwarf2_get_attr_constant_value (attr, 0);
8144 else if (attr_form_is_section_offset (attr))
8145 dwarf2_complex_location_expr_complaint ();
8146 else if (attr_form_is_block (attr))
8147 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8148 else
8149 dwarf2_complex_location_expr_complaint ();
8150
8151 return 1;
8152 }
8153
8154 return 0;
8155}
8156
8157/* Add an aggregate field to the field list. */
8158
8159static void
8160dwarf2_add_field (struct field_info *fip, struct die_info *die,
8161 struct dwarf2_cu *cu)
8162{
8163 struct objfile *objfile = cu->objfile;
8164 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8165 struct nextfield *new_field;
8166 struct attribute *attr;
8167 struct field *fp;
8168 char *fieldname = "";
8169
8170 /* Allocate a new field list entry and link it in. */
8171 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8172 make_cleanup (xfree, new_field);
8173 memset (new_field, 0, sizeof (struct nextfield));
8174
8175 if (die->tag == DW_TAG_inheritance)
8176 {
8177 new_field->next = fip->baseclasses;
8178 fip->baseclasses = new_field;
8179 }
8180 else
8181 {
8182 new_field->next = fip->fields;
8183 fip->fields = new_field;
8184 }
8185 fip->nfields++;
8186
8187 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8188 if (attr)
8189 new_field->accessibility = DW_UNSND (attr);
8190 else
8191 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8192 if (new_field->accessibility != DW_ACCESS_public)
8193 fip->non_public_fields = 1;
8194
8195 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8196 if (attr)
8197 new_field->virtuality = DW_UNSND (attr);
8198 else
8199 new_field->virtuality = DW_VIRTUALITY_none;
8200
8201 fp = &new_field->field;
8202
8203 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8204 {
8205 LONGEST offset;
8206
8207 /* Data member other than a C++ static data member. */
8208
8209 /* Get type of field. */
8210 fp->type = die_type (die, cu);
8211
8212 SET_FIELD_BITPOS (*fp, 0);
8213
8214 /* Get bit size of field (zero if none). */
8215 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8216 if (attr)
8217 {
8218 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8219 }
8220 else
8221 {
8222 FIELD_BITSIZE (*fp) = 0;
8223 }
8224
8225 /* Get bit offset of field. */
8226 if (handle_data_member_location (die, cu, &offset))
8227 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8228 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8229 if (attr)
8230 {
8231 if (gdbarch_bits_big_endian (gdbarch))
8232 {
8233 /* For big endian bits, the DW_AT_bit_offset gives the
8234 additional bit offset from the MSB of the containing
8235 anonymous object to the MSB of the field. We don't
8236 have to do anything special since we don't need to
8237 know the size of the anonymous object. */
8238 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8239 }
8240 else
8241 {
8242 /* For little endian bits, compute the bit offset to the
8243 MSB of the anonymous object, subtract off the number of
8244 bits from the MSB of the field to the MSB of the
8245 object, and then subtract off the number of bits of
8246 the field itself. The result is the bit offset of
8247 the LSB of the field. */
8248 int anonymous_size;
8249 int bit_offset = DW_UNSND (attr);
8250
8251 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8252 if (attr)
8253 {
8254 /* The size of the anonymous object containing
8255 the bit field is explicit, so use the
8256 indicated size (in bytes). */
8257 anonymous_size = DW_UNSND (attr);
8258 }
8259 else
8260 {
8261 /* The size of the anonymous object containing
8262 the bit field must be inferred from the type
8263 attribute of the data member containing the
8264 bit field. */
8265 anonymous_size = TYPE_LENGTH (fp->type);
8266 }
8267 SET_FIELD_BITPOS (*fp,
8268 (FIELD_BITPOS (*fp)
8269 + anonymous_size * bits_per_byte
8270 - bit_offset - FIELD_BITSIZE (*fp)));
8271 }
8272 }
8273
8274 /* Get name of field. */
8275 fieldname = dwarf2_name (die, cu);
8276 if (fieldname == NULL)
8277 fieldname = "";
8278
8279 /* The name is already allocated along with this objfile, so we don't
8280 need to duplicate it for the type. */
8281 fp->name = fieldname;
8282
8283 /* Change accessibility for artificial fields (e.g. virtual table
8284 pointer or virtual base class pointer) to private. */
8285 if (dwarf2_attr (die, DW_AT_artificial, cu))
8286 {
8287 FIELD_ARTIFICIAL (*fp) = 1;
8288 new_field->accessibility = DW_ACCESS_private;
8289 fip->non_public_fields = 1;
8290 }
8291 }
8292 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8293 {
8294 /* C++ static member. */
8295
8296 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8297 is a declaration, but all versions of G++ as of this writing
8298 (so through at least 3.2.1) incorrectly generate
8299 DW_TAG_variable tags. */
8300
8301 const char *physname;
8302
8303 /* Get name of field. */
8304 fieldname = dwarf2_name (die, cu);
8305 if (fieldname == NULL)
8306 return;
8307
8308 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8309 if (attr
8310 /* Only create a symbol if this is an external value.
8311 new_symbol checks this and puts the value in the global symbol
8312 table, which we want. If it is not external, new_symbol
8313 will try to put the value in cu->list_in_scope which is wrong. */
8314 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8315 {
8316 /* A static const member, not much different than an enum as far as
8317 we're concerned, except that we can support more types. */
8318 new_symbol (die, NULL, cu);
8319 }
8320
8321 /* Get physical name. */
8322 physname = dwarf2_physname (fieldname, die, cu);
8323
8324 /* The name is already allocated along with this objfile, so we don't
8325 need to duplicate it for the type. */
8326 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8327 FIELD_TYPE (*fp) = die_type (die, cu);
8328 FIELD_NAME (*fp) = fieldname;
8329 }
8330 else if (die->tag == DW_TAG_inheritance)
8331 {
8332 LONGEST offset;
8333
8334 /* C++ base class field. */
8335 if (handle_data_member_location (die, cu, &offset))
8336 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8337 FIELD_BITSIZE (*fp) = 0;
8338 FIELD_TYPE (*fp) = die_type (die, cu);
8339 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8340 fip->nbaseclasses++;
8341 }
8342}
8343
8344/* Add a typedef defined in the scope of the FIP's class. */
8345
8346static void
8347dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8348 struct dwarf2_cu *cu)
8349{
8350 struct objfile *objfile = cu->objfile;
8351 struct typedef_field_list *new_field;
8352 struct attribute *attr;
8353 struct typedef_field *fp;
8354 char *fieldname = "";
8355
8356 /* Allocate a new field list entry and link it in. */
8357 new_field = xzalloc (sizeof (*new_field));
8358 make_cleanup (xfree, new_field);
8359
8360 gdb_assert (die->tag == DW_TAG_typedef);
8361
8362 fp = &new_field->field;
8363
8364 /* Get name of field. */
8365 fp->name = dwarf2_name (die, cu);
8366 if (fp->name == NULL)
8367 return;
8368
8369 fp->type = read_type_die (die, cu);
8370
8371 new_field->next = fip->typedef_field_list;
8372 fip->typedef_field_list = new_field;
8373 fip->typedef_field_list_count++;
8374}
8375
8376/* Create the vector of fields, and attach it to the type. */
8377
8378static void
8379dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8380 struct dwarf2_cu *cu)
8381{
8382 int nfields = fip->nfields;
8383
8384 /* Record the field count, allocate space for the array of fields,
8385 and create blank accessibility bitfields if necessary. */
8386 TYPE_NFIELDS (type) = nfields;
8387 TYPE_FIELDS (type) = (struct field *)
8388 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8389 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8390
8391 if (fip->non_public_fields && cu->language != language_ada)
8392 {
8393 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8394
8395 TYPE_FIELD_PRIVATE_BITS (type) =
8396 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8397 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8398
8399 TYPE_FIELD_PROTECTED_BITS (type) =
8400 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8401 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8402
8403 TYPE_FIELD_IGNORE_BITS (type) =
8404 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8405 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8406 }
8407
8408 /* If the type has baseclasses, allocate and clear a bit vector for
8409 TYPE_FIELD_VIRTUAL_BITS. */
8410 if (fip->nbaseclasses && cu->language != language_ada)
8411 {
8412 int num_bytes = B_BYTES (fip->nbaseclasses);
8413 unsigned char *pointer;
8414
8415 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8416 pointer = TYPE_ALLOC (type, num_bytes);
8417 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8418 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8419 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8420 }
8421
8422 /* Copy the saved-up fields into the field vector. Start from the head of
8423 the list, adding to the tail of the field array, so that they end up in
8424 the same order in the array in which they were added to the list. */
8425 while (nfields-- > 0)
8426 {
8427 struct nextfield *fieldp;
8428
8429 if (fip->fields)
8430 {
8431 fieldp = fip->fields;
8432 fip->fields = fieldp->next;
8433 }
8434 else
8435 {
8436 fieldp = fip->baseclasses;
8437 fip->baseclasses = fieldp->next;
8438 }
8439
8440 TYPE_FIELD (type, nfields) = fieldp->field;
8441 switch (fieldp->accessibility)
8442 {
8443 case DW_ACCESS_private:
8444 if (cu->language != language_ada)
8445 SET_TYPE_FIELD_PRIVATE (type, nfields);
8446 break;
8447
8448 case DW_ACCESS_protected:
8449 if (cu->language != language_ada)
8450 SET_TYPE_FIELD_PROTECTED (type, nfields);
8451 break;
8452
8453 case DW_ACCESS_public:
8454 break;
8455
8456 default:
8457 /* Unknown accessibility. Complain and treat it as public. */
8458 {
8459 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8460 fieldp->accessibility);
8461 }
8462 break;
8463 }
8464 if (nfields < fip->nbaseclasses)
8465 {
8466 switch (fieldp->virtuality)
8467 {
8468 case DW_VIRTUALITY_virtual:
8469 case DW_VIRTUALITY_pure_virtual:
8470 if (cu->language == language_ada)
8471 error (_("unexpected virtuality in component of Ada type"));
8472 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8473 break;
8474 }
8475 }
8476 }
8477}
8478
8479/* Add a member function to the proper fieldlist. */
8480
8481static void
8482dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8483 struct type *type, struct dwarf2_cu *cu)
8484{
8485 struct objfile *objfile = cu->objfile;
8486 struct attribute *attr;
8487 struct fnfieldlist *flp;
8488 int i;
8489 struct fn_field *fnp;
8490 char *fieldname;
8491 struct nextfnfield *new_fnfield;
8492 struct type *this_type;
8493 enum dwarf_access_attribute accessibility;
8494
8495 if (cu->language == language_ada)
8496 error (_("unexpected member function in Ada type"));
8497
8498 /* Get name of member function. */
8499 fieldname = dwarf2_name (die, cu);
8500 if (fieldname == NULL)
8501 return;
8502
8503 /* Look up member function name in fieldlist. */
8504 for (i = 0; i < fip->nfnfields; i++)
8505 {
8506 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8507 break;
8508 }
8509
8510 /* Create new list element if necessary. */
8511 if (i < fip->nfnfields)
8512 flp = &fip->fnfieldlists[i];
8513 else
8514 {
8515 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8516 {
8517 fip->fnfieldlists = (struct fnfieldlist *)
8518 xrealloc (fip->fnfieldlists,
8519 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8520 * sizeof (struct fnfieldlist));
8521 if (fip->nfnfields == 0)
8522 make_cleanup (free_current_contents, &fip->fnfieldlists);
8523 }
8524 flp = &fip->fnfieldlists[fip->nfnfields];
8525 flp->name = fieldname;
8526 flp->length = 0;
8527 flp->head = NULL;
8528 i = fip->nfnfields++;
8529 }
8530
8531 /* Create a new member function field and chain it to the field list
8532 entry. */
8533 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8534 make_cleanup (xfree, new_fnfield);
8535 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8536 new_fnfield->next = flp->head;
8537 flp->head = new_fnfield;
8538 flp->length++;
8539
8540 /* Fill in the member function field info. */
8541 fnp = &new_fnfield->fnfield;
8542
8543 /* Delay processing of the physname until later. */
8544 if (cu->language == language_cplus || cu->language == language_java)
8545 {
8546 add_to_method_list (type, i, flp->length - 1, fieldname,
8547 die, cu);
8548 }
8549 else
8550 {
8551 const char *physname = dwarf2_physname (fieldname, die, cu);
8552 fnp->physname = physname ? physname : "";
8553 }
8554
8555 fnp->type = alloc_type (objfile);
8556 this_type = read_type_die (die, cu);
8557 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
8558 {
8559 int nparams = TYPE_NFIELDS (this_type);
8560
8561 /* TYPE is the domain of this method, and THIS_TYPE is the type
8562 of the method itself (TYPE_CODE_METHOD). */
8563 smash_to_method_type (fnp->type, type,
8564 TYPE_TARGET_TYPE (this_type),
8565 TYPE_FIELDS (this_type),
8566 TYPE_NFIELDS (this_type),
8567 TYPE_VARARGS (this_type));
8568
8569 /* Handle static member functions.
8570 Dwarf2 has no clean way to discern C++ static and non-static
8571 member functions. G++ helps GDB by marking the first
8572 parameter for non-static member functions (which is the this
8573 pointer) as artificial. We obtain this information from
8574 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
8575 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
8576 fnp->voffset = VOFFSET_STATIC;
8577 }
8578 else
8579 complaint (&symfile_complaints, _("member function type missing for '%s'"),
8580 dwarf2_full_name (fieldname, die, cu));
8581
8582 /* Get fcontext from DW_AT_containing_type if present. */
8583 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8584 fnp->fcontext = die_containing_type (die, cu);
8585
8586 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8587 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
8588
8589 /* Get accessibility. */
8590 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8591 if (attr)
8592 accessibility = DW_UNSND (attr);
8593 else
8594 accessibility = dwarf2_default_access_attribute (die, cu);
8595 switch (accessibility)
8596 {
8597 case DW_ACCESS_private:
8598 fnp->is_private = 1;
8599 break;
8600 case DW_ACCESS_protected:
8601 fnp->is_protected = 1;
8602 break;
8603 }
8604
8605 /* Check for artificial methods. */
8606 attr = dwarf2_attr (die, DW_AT_artificial, cu);
8607 if (attr && DW_UNSND (attr) != 0)
8608 fnp->is_artificial = 1;
8609
8610 /* Get index in virtual function table if it is a virtual member
8611 function. For older versions of GCC, this is an offset in the
8612 appropriate virtual table, as specified by DW_AT_containing_type.
8613 For everyone else, it is an expression to be evaluated relative
8614 to the object address. */
8615
8616 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
8617 if (attr)
8618 {
8619 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8620 {
8621 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8622 {
8623 /* Old-style GCC. */
8624 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8625 }
8626 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8627 || (DW_BLOCK (attr)->size > 1
8628 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8629 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8630 {
8631 struct dwarf_block blk;
8632 int offset;
8633
8634 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8635 ? 1 : 2);
8636 blk.size = DW_BLOCK (attr)->size - offset;
8637 blk.data = DW_BLOCK (attr)->data + offset;
8638 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8639 if ((fnp->voffset % cu->header.addr_size) != 0)
8640 dwarf2_complex_location_expr_complaint ();
8641 else
8642 fnp->voffset /= cu->header.addr_size;
8643 fnp->voffset += 2;
8644 }
8645 else
8646 dwarf2_complex_location_expr_complaint ();
8647
8648 if (!fnp->fcontext)
8649 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8650 }
8651 else if (attr_form_is_section_offset (attr))
8652 {
8653 dwarf2_complex_location_expr_complaint ();
8654 }
8655 else
8656 {
8657 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8658 fieldname);
8659 }
8660 }
8661 else
8662 {
8663 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8664 if (attr && DW_UNSND (attr))
8665 {
8666 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8667 complaint (&symfile_complaints,
8668 _("Member function \"%s\" (offset %d) is virtual "
8669 "but the vtable offset is not specified"),
8670 fieldname, die->offset.sect_off);
8671 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8672 TYPE_CPLUS_DYNAMIC (type) = 1;
8673 }
8674 }
8675}
8676
8677/* Create the vector of member function fields, and attach it to the type. */
8678
8679static void
8680dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
8681 struct dwarf2_cu *cu)
8682{
8683 struct fnfieldlist *flp;
8684 int i;
8685
8686 if (cu->language == language_ada)
8687 error (_("unexpected member functions in Ada type"));
8688
8689 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8690 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8691 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8692
8693 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8694 {
8695 struct nextfnfield *nfp = flp->head;
8696 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8697 int k;
8698
8699 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8700 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8701 fn_flp->fn_fields = (struct fn_field *)
8702 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8703 for (k = flp->length; (k--, nfp); nfp = nfp->next)
8704 fn_flp->fn_fields[k] = nfp->fnfield;
8705 }
8706
8707 TYPE_NFN_FIELDS (type) = fip->nfnfields;
8708}
8709
8710/* Returns non-zero if NAME is the name of a vtable member in CU's
8711 language, zero otherwise. */
8712static int
8713is_vtable_name (const char *name, struct dwarf2_cu *cu)
8714{
8715 static const char vptr[] = "_vptr";
8716 static const char vtable[] = "vtable";
8717
8718 /* Look for the C++ and Java forms of the vtable. */
8719 if ((cu->language == language_java
8720 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8721 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8722 && is_cplus_marker (name[sizeof (vptr) - 1])))
8723 return 1;
8724
8725 return 0;
8726}
8727
8728/* GCC outputs unnamed structures that are really pointers to member
8729 functions, with the ABI-specified layout. If TYPE describes
8730 such a structure, smash it into a member function type.
8731
8732 GCC shouldn't do this; it should just output pointer to member DIEs.
8733 This is GCC PR debug/28767. */
8734
8735static void
8736quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
8737{
8738 struct type *pfn_type, *domain_type, *new_type;
8739
8740 /* Check for a structure with no name and two children. */
8741 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8742 return;
8743
8744 /* Check for __pfn and __delta members. */
8745 if (TYPE_FIELD_NAME (type, 0) == NULL
8746 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8747 || TYPE_FIELD_NAME (type, 1) == NULL
8748 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8749 return;
8750
8751 /* Find the type of the method. */
8752 pfn_type = TYPE_FIELD_TYPE (type, 0);
8753 if (pfn_type == NULL
8754 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8755 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
8756 return;
8757
8758 /* Look for the "this" argument. */
8759 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8760 if (TYPE_NFIELDS (pfn_type) == 0
8761 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
8762 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
8763 return;
8764
8765 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
8766 new_type = alloc_type (objfile);
8767 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
8768 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
8769 TYPE_VARARGS (pfn_type));
8770 smash_to_methodptr_type (type, new_type);
8771}
8772
8773/* Called when we find the DIE that starts a structure or union scope
8774 (definition) to create a type for the structure or union. Fill in
8775 the type's name and general properties; the members will not be
8776 processed until process_structure_type.
8777
8778 NOTE: we need to call these functions regardless of whether or not the
8779 DIE has a DW_AT_name attribute, since it might be an anonymous
8780 structure or union. This gets the type entered into our set of
8781 user defined types.
8782
8783 However, if the structure is incomplete (an opaque struct/union)
8784 then suppress creating a symbol table entry for it since gdb only
8785 wants to find the one with the complete definition. Note that if
8786 it is complete, we just call new_symbol, which does it's own
8787 checking about whether the struct/union is anonymous or not (and
8788 suppresses creating a symbol table entry itself). */
8789
8790static struct type *
8791read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
8792{
8793 struct objfile *objfile = cu->objfile;
8794 struct type *type;
8795 struct attribute *attr;
8796 char *name;
8797
8798 /* If the definition of this type lives in .debug_types, read that type.
8799 Don't follow DW_AT_specification though, that will take us back up
8800 the chain and we want to go down. */
8801 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
8802 if (attr)
8803 {
8804 struct dwarf2_cu *type_cu = cu;
8805 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
8806
8807 /* We could just recurse on read_structure_type, but we need to call
8808 get_die_type to ensure only one type for this DIE is created.
8809 This is important, for example, because for c++ classes we need
8810 TYPE_NAME set which is only done by new_symbol. Blech. */
8811 type = read_type_die (type_die, type_cu);
8812
8813 /* TYPE_CU may not be the same as CU.
8814 Ensure TYPE is recorded in CU's type_hash table. */
8815 return set_die_type (die, type, cu);
8816 }
8817
8818 type = alloc_type (objfile);
8819 INIT_CPLUS_SPECIFIC (type);
8820
8821 name = dwarf2_name (die, cu);
8822 if (name != NULL)
8823 {
8824 if (cu->language == language_cplus
8825 || cu->language == language_java)
8826 {
8827 char *full_name = (char *) dwarf2_full_name (name, die, cu);
8828
8829 /* dwarf2_full_name might have already finished building the DIE's
8830 type. If so, there is no need to continue. */
8831 if (get_die_type (die, cu) != NULL)
8832 return get_die_type (die, cu);
8833
8834 TYPE_TAG_NAME (type) = full_name;
8835 if (die->tag == DW_TAG_structure_type
8836 || die->tag == DW_TAG_class_type)
8837 TYPE_NAME (type) = TYPE_TAG_NAME (type);
8838 }
8839 else
8840 {
8841 /* The name is already allocated along with this objfile, so
8842 we don't need to duplicate it for the type. */
8843 TYPE_TAG_NAME (type) = (char *) name;
8844 if (die->tag == DW_TAG_class_type)
8845 TYPE_NAME (type) = TYPE_TAG_NAME (type);
8846 }
8847 }
8848
8849 if (die->tag == DW_TAG_structure_type)
8850 {
8851 TYPE_CODE (type) = TYPE_CODE_STRUCT;
8852 }
8853 else if (die->tag == DW_TAG_union_type)
8854 {
8855 TYPE_CODE (type) = TYPE_CODE_UNION;
8856 }
8857 else
8858 {
8859 TYPE_CODE (type) = TYPE_CODE_CLASS;
8860 }
8861
8862 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
8863 TYPE_DECLARED_CLASS (type) = 1;
8864
8865 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8866 if (attr)
8867 {
8868 TYPE_LENGTH (type) = DW_UNSND (attr);
8869 }
8870 else
8871 {
8872 TYPE_LENGTH (type) = 0;
8873 }
8874
8875 TYPE_STUB_SUPPORTED (type) = 1;
8876 if (die_is_declaration (die, cu))
8877 TYPE_STUB (type) = 1;
8878 else if (attr == NULL && die->child == NULL
8879 && producer_is_realview (cu->producer))
8880 /* RealView does not output the required DW_AT_declaration
8881 on incomplete types. */
8882 TYPE_STUB (type) = 1;
8883
8884 /* We need to add the type field to the die immediately so we don't
8885 infinitely recurse when dealing with pointers to the structure
8886 type within the structure itself. */
8887 set_die_type (die, type, cu);
8888
8889 /* set_die_type should be already done. */
8890 set_descriptive_type (type, die, cu);
8891
8892 return type;
8893}
8894
8895/* Finish creating a structure or union type, including filling in
8896 its members and creating a symbol for it. */
8897
8898static void
8899process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
8900{
8901 struct objfile *objfile = cu->objfile;
8902 struct die_info *child_die = die->child;
8903 struct type *type;
8904
8905 type = get_die_type (die, cu);
8906 if (type == NULL)
8907 type = read_structure_type (die, cu);
8908
8909 if (die->child != NULL && ! die_is_declaration (die, cu))
8910 {
8911 struct field_info fi;
8912 struct die_info *child_die;
8913 VEC (symbolp) *template_args = NULL;
8914 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8915
8916 memset (&fi, 0, sizeof (struct field_info));
8917
8918 child_die = die->child;
8919
8920 while (child_die && child_die->tag)
8921 {
8922 if (child_die->tag == DW_TAG_member
8923 || child_die->tag == DW_TAG_variable)
8924 {
8925 /* NOTE: carlton/2002-11-05: A C++ static data member
8926 should be a DW_TAG_member that is a declaration, but
8927 all versions of G++ as of this writing (so through at
8928 least 3.2.1) incorrectly generate DW_TAG_variable
8929 tags for them instead. */
8930 dwarf2_add_field (&fi, child_die, cu);
8931 }
8932 else if (child_die->tag == DW_TAG_subprogram)
8933 {
8934 /* C++ member function. */
8935 dwarf2_add_member_fn (&fi, child_die, type, cu);
8936 }
8937 else if (child_die->tag == DW_TAG_inheritance)
8938 {
8939 /* C++ base class field. */
8940 dwarf2_add_field (&fi, child_die, cu);
8941 }
8942 else if (child_die->tag == DW_TAG_typedef)
8943 dwarf2_add_typedef (&fi, child_die, cu);
8944 else if (child_die->tag == DW_TAG_template_type_param
8945 || child_die->tag == DW_TAG_template_value_param)
8946 {
8947 struct symbol *arg = new_symbol (child_die, NULL, cu);
8948
8949 if (arg != NULL)
8950 VEC_safe_push (symbolp, template_args, arg);
8951 }
8952
8953 child_die = sibling_die (child_die);
8954 }
8955
8956 /* Attach template arguments to type. */
8957 if (! VEC_empty (symbolp, template_args))
8958 {
8959 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8960 TYPE_N_TEMPLATE_ARGUMENTS (type)
8961 = VEC_length (symbolp, template_args);
8962 TYPE_TEMPLATE_ARGUMENTS (type)
8963 = obstack_alloc (&objfile->objfile_obstack,
8964 (TYPE_N_TEMPLATE_ARGUMENTS (type)
8965 * sizeof (struct symbol *)));
8966 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
8967 VEC_address (symbolp, template_args),
8968 (TYPE_N_TEMPLATE_ARGUMENTS (type)
8969 * sizeof (struct symbol *)));
8970 VEC_free (symbolp, template_args);
8971 }
8972
8973 /* Attach fields and member functions to the type. */
8974 if (fi.nfields)
8975 dwarf2_attach_fields_to_type (&fi, type, cu);
8976 if (fi.nfnfields)
8977 {
8978 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
8979
8980 /* Get the type which refers to the base class (possibly this
8981 class itself) which contains the vtable pointer for the current
8982 class from the DW_AT_containing_type attribute. This use of
8983 DW_AT_containing_type is a GNU extension. */
8984
8985 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8986 {
8987 struct type *t = die_containing_type (die, cu);
8988
8989 TYPE_VPTR_BASETYPE (type) = t;
8990 if (type == t)
8991 {
8992 int i;
8993
8994 /* Our own class provides vtbl ptr. */
8995 for (i = TYPE_NFIELDS (t) - 1;
8996 i >= TYPE_N_BASECLASSES (t);
8997 --i)
8998 {
8999 const char *fieldname = TYPE_FIELD_NAME (t, i);
9000
9001 if (is_vtable_name (fieldname, cu))
9002 {
9003 TYPE_VPTR_FIELDNO (type) = i;
9004 break;
9005 }
9006 }
9007
9008 /* Complain if virtual function table field not found. */
9009 if (i < TYPE_N_BASECLASSES (t))
9010 complaint (&symfile_complaints,
9011 _("virtual function table pointer "
9012 "not found when defining class '%s'"),
9013 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9014 "");
9015 }
9016 else
9017 {
9018 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9019 }
9020 }
9021 else if (cu->producer
9022 && strncmp (cu->producer,
9023 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9024 {
9025 /* The IBM XLC compiler does not provide direct indication
9026 of the containing type, but the vtable pointer is
9027 always named __vfp. */
9028
9029 int i;
9030
9031 for (i = TYPE_NFIELDS (type) - 1;
9032 i >= TYPE_N_BASECLASSES (type);
9033 --i)
9034 {
9035 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9036 {
9037 TYPE_VPTR_FIELDNO (type) = i;
9038 TYPE_VPTR_BASETYPE (type) = type;
9039 break;
9040 }
9041 }
9042 }
9043 }
9044
9045 /* Copy fi.typedef_field_list linked list elements content into the
9046 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9047 if (fi.typedef_field_list)
9048 {
9049 int i = fi.typedef_field_list_count;
9050
9051 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9052 TYPE_TYPEDEF_FIELD_ARRAY (type)
9053 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9054 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9055
9056 /* Reverse the list order to keep the debug info elements order. */
9057 while (--i >= 0)
9058 {
9059 struct typedef_field *dest, *src;
9060
9061 dest = &TYPE_TYPEDEF_FIELD (type, i);
9062 src = &fi.typedef_field_list->field;
9063 fi.typedef_field_list = fi.typedef_field_list->next;
9064 *dest = *src;
9065 }
9066 }
9067
9068 do_cleanups (back_to);
9069
9070 if (HAVE_CPLUS_STRUCT (type))
9071 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9072 }
9073
9074 quirk_gcc_member_function_pointer (type, objfile);
9075
9076 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9077 snapshots) has been known to create a die giving a declaration
9078 for a class that has, as a child, a die giving a definition for a
9079 nested class. So we have to process our children even if the
9080 current die is a declaration. Normally, of course, a declaration
9081 won't have any children at all. */
9082
9083 while (child_die != NULL && child_die->tag)
9084 {
9085 if (child_die->tag == DW_TAG_member
9086 || child_die->tag == DW_TAG_variable
9087 || child_die->tag == DW_TAG_inheritance
9088 || child_die->tag == DW_TAG_template_value_param
9089 || child_die->tag == DW_TAG_template_type_param)
9090 {
9091 /* Do nothing. */
9092 }
9093 else
9094 process_die (child_die, cu);
9095
9096 child_die = sibling_die (child_die);
9097 }
9098
9099 /* Do not consider external references. According to the DWARF standard,
9100 these DIEs are identified by the fact that they have no byte_size
9101 attribute, and a declaration attribute. */
9102 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9103 || !die_is_declaration (die, cu))
9104 new_symbol (die, type, cu);
9105}
9106
9107/* Given a DW_AT_enumeration_type die, set its type. We do not
9108 complete the type's fields yet, or create any symbols. */
9109
9110static struct type *
9111read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9112{
9113 struct objfile *objfile = cu->objfile;
9114 struct type *type;
9115 struct attribute *attr;
9116 const char *name;
9117
9118 /* If the definition of this type lives in .debug_types, read that type.
9119 Don't follow DW_AT_specification though, that will take us back up
9120 the chain and we want to go down. */
9121 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9122 if (attr)
9123 {
9124 struct dwarf2_cu *type_cu = cu;
9125 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9126
9127 type = read_type_die (type_die, type_cu);
9128
9129 /* TYPE_CU may not be the same as CU.
9130 Ensure TYPE is recorded in CU's type_hash table. */
9131 return set_die_type (die, type, cu);
9132 }
9133
9134 type = alloc_type (objfile);
9135
9136 TYPE_CODE (type) = TYPE_CODE_ENUM;
9137 name = dwarf2_full_name (NULL, die, cu);
9138 if (name != NULL)
9139 TYPE_TAG_NAME (type) = (char *) name;
9140
9141 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9142 if (attr)
9143 {
9144 TYPE_LENGTH (type) = DW_UNSND (attr);
9145 }
9146 else
9147 {
9148 TYPE_LENGTH (type) = 0;
9149 }
9150
9151 /* The enumeration DIE can be incomplete. In Ada, any type can be
9152 declared as private in the package spec, and then defined only
9153 inside the package body. Such types are known as Taft Amendment
9154 Types. When another package uses such a type, an incomplete DIE
9155 may be generated by the compiler. */
9156 if (die_is_declaration (die, cu))
9157 TYPE_STUB (type) = 1;
9158
9159 return set_die_type (die, type, cu);
9160}
9161
9162/* Given a pointer to a die which begins an enumeration, process all
9163 the dies that define the members of the enumeration, and create the
9164 symbol for the enumeration type.
9165
9166 NOTE: We reverse the order of the element list. */
9167
9168static void
9169process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9170{
9171 struct type *this_type;
9172
9173 this_type = get_die_type (die, cu);
9174 if (this_type == NULL)
9175 this_type = read_enumeration_type (die, cu);
9176
9177 if (die->child != NULL)
9178 {
9179 struct die_info *child_die;
9180 struct symbol *sym;
9181 struct field *fields = NULL;
9182 int num_fields = 0;
9183 int unsigned_enum = 1;
9184 char *name;
9185 int flag_enum = 1;
9186 ULONGEST mask = 0;
9187
9188 child_die = die->child;
9189 while (child_die && child_die->tag)
9190 {
9191 if (child_die->tag != DW_TAG_enumerator)
9192 {
9193 process_die (child_die, cu);
9194 }
9195 else
9196 {
9197 name = dwarf2_name (child_die, cu);
9198 if (name)
9199 {
9200 sym = new_symbol (child_die, this_type, cu);
9201 if (SYMBOL_VALUE (sym) < 0)
9202 {
9203 unsigned_enum = 0;
9204 flag_enum = 0;
9205 }
9206 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9207 flag_enum = 0;
9208 else
9209 mask |= SYMBOL_VALUE (sym);
9210
9211 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9212 {
9213 fields = (struct field *)
9214 xrealloc (fields,
9215 (num_fields + DW_FIELD_ALLOC_CHUNK)
9216 * sizeof (struct field));
9217 }
9218
9219 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9220 FIELD_TYPE (fields[num_fields]) = NULL;
9221 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9222 FIELD_BITSIZE (fields[num_fields]) = 0;
9223
9224 num_fields++;
9225 }
9226 }
9227
9228 child_die = sibling_die (child_die);
9229 }
9230
9231 if (num_fields)
9232 {
9233 TYPE_NFIELDS (this_type) = num_fields;
9234 TYPE_FIELDS (this_type) = (struct field *)
9235 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9236 memcpy (TYPE_FIELDS (this_type), fields,
9237 sizeof (struct field) * num_fields);
9238 xfree (fields);
9239 }
9240 if (unsigned_enum)
9241 TYPE_UNSIGNED (this_type) = 1;
9242 if (flag_enum)
9243 TYPE_FLAG_ENUM (this_type) = 1;
9244 }
9245
9246 /* If we are reading an enum from a .debug_types unit, and the enum
9247 is a declaration, and the enum is not the signatured type in the
9248 unit, then we do not want to add a symbol for it. Adding a
9249 symbol would in some cases obscure the true definition of the
9250 enum, giving users an incomplete type when the definition is
9251 actually available. Note that we do not want to do this for all
9252 enums which are just declarations, because C++0x allows forward
9253 enum declarations. */
9254 if (cu->per_cu->is_debug_types
9255 && die_is_declaration (die, cu))
9256 {
9257 struct signatured_type *sig_type;
9258
9259 sig_type
9260 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9261 cu->per_cu->info_or_types_section,
9262 cu->per_cu->offset);
9263 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9264 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9265 return;
9266 }
9267
9268 new_symbol (die, this_type, cu);
9269}
9270
9271/* Extract all information from a DW_TAG_array_type DIE and put it in
9272 the DIE's type field. For now, this only handles one dimensional
9273 arrays. */
9274
9275static struct type *
9276read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9277{
9278 struct objfile *objfile = cu->objfile;
9279 struct die_info *child_die;
9280 struct type *type;
9281 struct type *element_type, *range_type, *index_type;
9282 struct type **range_types = NULL;
9283 struct attribute *attr;
9284 int ndim = 0;
9285 struct cleanup *back_to;
9286 char *name;
9287
9288 element_type = die_type (die, cu);
9289
9290 /* The die_type call above may have already set the type for this DIE. */
9291 type = get_die_type (die, cu);
9292 if (type)
9293 return type;
9294
9295 /* Irix 6.2 native cc creates array types without children for
9296 arrays with unspecified length. */
9297 if (die->child == NULL)
9298 {
9299 index_type = objfile_type (objfile)->builtin_int;
9300 range_type = create_range_type (NULL, index_type, 0, -1);
9301 type = create_array_type (NULL, element_type, range_type);
9302 return set_die_type (die, type, cu);
9303 }
9304
9305 back_to = make_cleanup (null_cleanup, NULL);
9306 child_die = die->child;
9307 while (child_die && child_die->tag)
9308 {
9309 if (child_die->tag == DW_TAG_subrange_type)
9310 {
9311 struct type *child_type = read_type_die (child_die, cu);
9312
9313 if (child_type != NULL)
9314 {
9315 /* The range type was succesfully read. Save it for the
9316 array type creation. */
9317 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9318 {
9319 range_types = (struct type **)
9320 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9321 * sizeof (struct type *));
9322 if (ndim == 0)
9323 make_cleanup (free_current_contents, &range_types);
9324 }
9325 range_types[ndim++] = child_type;
9326 }
9327 }
9328 child_die = sibling_die (child_die);
9329 }
9330
9331 /* Dwarf2 dimensions are output from left to right, create the
9332 necessary array types in backwards order. */
9333
9334 type = element_type;
9335
9336 if (read_array_order (die, cu) == DW_ORD_col_major)
9337 {
9338 int i = 0;
9339
9340 while (i < ndim)
9341 type = create_array_type (NULL, type, range_types[i++]);
9342 }
9343 else
9344 {
9345 while (ndim-- > 0)
9346 type = create_array_type (NULL, type, range_types[ndim]);
9347 }
9348
9349 /* Understand Dwarf2 support for vector types (like they occur on
9350 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9351 array type. This is not part of the Dwarf2/3 standard yet, but a
9352 custom vendor extension. The main difference between a regular
9353 array and the vector variant is that vectors are passed by value
9354 to functions. */
9355 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9356 if (attr)
9357 make_vector_type (type);
9358
9359 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9360 implementation may choose to implement triple vectors using this
9361 attribute. */
9362 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9363 if (attr)
9364 {
9365 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9366 TYPE_LENGTH (type) = DW_UNSND (attr);
9367 else
9368 complaint (&symfile_complaints,
9369 _("DW_AT_byte_size for array type smaller "
9370 "than the total size of elements"));
9371 }
9372
9373 name = dwarf2_name (die, cu);
9374 if (name)
9375 TYPE_NAME (type) = name;
9376
9377 /* Install the type in the die. */
9378 set_die_type (die, type, cu);
9379
9380 /* set_die_type should be already done. */
9381 set_descriptive_type (type, die, cu);
9382
9383 do_cleanups (back_to);
9384
9385 return type;
9386}
9387
9388static enum dwarf_array_dim_ordering
9389read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9390{
9391 struct attribute *attr;
9392
9393 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9394
9395 if (attr) return DW_SND (attr);
9396
9397 /* GNU F77 is a special case, as at 08/2004 array type info is the
9398 opposite order to the dwarf2 specification, but data is still
9399 laid out as per normal fortran.
9400
9401 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9402 version checking. */
9403
9404 if (cu->language == language_fortran
9405 && cu->producer && strstr (cu->producer, "GNU F77"))
9406 {
9407 return DW_ORD_row_major;
9408 }
9409
9410 switch (cu->language_defn->la_array_ordering)
9411 {
9412 case array_column_major:
9413 return DW_ORD_col_major;
9414 case array_row_major:
9415 default:
9416 return DW_ORD_row_major;
9417 };
9418}
9419
9420/* Extract all information from a DW_TAG_set_type DIE and put it in
9421 the DIE's type field. */
9422
9423static struct type *
9424read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9425{
9426 struct type *domain_type, *set_type;
9427 struct attribute *attr;
9428
9429 domain_type = die_type (die, cu);
9430
9431 /* The die_type call above may have already set the type for this DIE. */
9432 set_type = get_die_type (die, cu);
9433 if (set_type)
9434 return set_type;
9435
9436 set_type = create_set_type (NULL, domain_type);
9437
9438 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9439 if (attr)
9440 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9441
9442 return set_die_type (die, set_type, cu);
9443}
9444
9445/* First cut: install each common block member as a global variable. */
9446
9447static void
9448read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9449{
9450 struct die_info *child_die;
9451 struct attribute *attr;
9452 struct symbol *sym;
9453 CORE_ADDR base = (CORE_ADDR) 0;
9454
9455 attr = dwarf2_attr (die, DW_AT_location, cu);
9456 if (attr)
9457 {
9458 /* Support the .debug_loc offsets. */
9459 if (attr_form_is_block (attr))
9460 {
9461 base = decode_locdesc (DW_BLOCK (attr), cu);
9462 }
9463 else if (attr_form_is_section_offset (attr))
9464 {
9465 dwarf2_complex_location_expr_complaint ();
9466 }
9467 else
9468 {
9469 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9470 "common block member");
9471 }
9472 }
9473 if (die->child != NULL)
9474 {
9475 child_die = die->child;
9476 while (child_die && child_die->tag)
9477 {
9478 LONGEST offset;
9479
9480 sym = new_symbol (child_die, NULL, cu);
9481 if (sym != NULL
9482 && handle_data_member_location (child_die, cu, &offset))
9483 {
9484 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9485 add_symbol_to_list (sym, &global_symbols);
9486 }
9487 child_die = sibling_die (child_die);
9488 }
9489 }
9490}
9491
9492/* Create a type for a C++ namespace. */
9493
9494static struct type *
9495read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9496{
9497 struct objfile *objfile = cu->objfile;
9498 const char *previous_prefix, *name;
9499 int is_anonymous;
9500 struct type *type;
9501
9502 /* For extensions, reuse the type of the original namespace. */
9503 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9504 {
9505 struct die_info *ext_die;
9506 struct dwarf2_cu *ext_cu = cu;
9507
9508 ext_die = dwarf2_extension (die, &ext_cu);
9509 type = read_type_die (ext_die, ext_cu);
9510
9511 /* EXT_CU may not be the same as CU.
9512 Ensure TYPE is recorded in CU's type_hash table. */
9513 return set_die_type (die, type, cu);
9514 }
9515
9516 name = namespace_name (die, &is_anonymous, cu);
9517
9518 /* Now build the name of the current namespace. */
9519
9520 previous_prefix = determine_prefix (die, cu);
9521 if (previous_prefix[0] != '\0')
9522 name = typename_concat (&objfile->objfile_obstack,
9523 previous_prefix, name, 0, cu);
9524
9525 /* Create the type. */
9526 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9527 objfile);
9528 TYPE_NAME (type) = (char *) name;
9529 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9530
9531 return set_die_type (die, type, cu);
9532}
9533
9534/* Read a C++ namespace. */
9535
9536static void
9537read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9538{
9539 struct objfile *objfile = cu->objfile;
9540 int is_anonymous;
9541
9542 /* Add a symbol associated to this if we haven't seen the namespace
9543 before. Also, add a using directive if it's an anonymous
9544 namespace. */
9545
9546 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
9547 {
9548 struct type *type;
9549
9550 type = read_type_die (die, cu);
9551 new_symbol (die, type, cu);
9552
9553 namespace_name (die, &is_anonymous, cu);
9554 if (is_anonymous)
9555 {
9556 const char *previous_prefix = determine_prefix (die, cu);
9557
9558 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
9559 NULL, NULL, &objfile->objfile_obstack);
9560 }
9561 }
9562
9563 if (die->child != NULL)
9564 {
9565 struct die_info *child_die = die->child;
9566
9567 while (child_die && child_die->tag)
9568 {
9569 process_die (child_die, cu);
9570 child_die = sibling_die (child_die);
9571 }
9572 }
9573}
9574
9575/* Read a Fortran module as type. This DIE can be only a declaration used for
9576 imported module. Still we need that type as local Fortran "use ... only"
9577 declaration imports depend on the created type in determine_prefix. */
9578
9579static struct type *
9580read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9581{
9582 struct objfile *objfile = cu->objfile;
9583 char *module_name;
9584 struct type *type;
9585
9586 module_name = dwarf2_name (die, cu);
9587 if (!module_name)
9588 complaint (&symfile_complaints,
9589 _("DW_TAG_module has no name, offset 0x%x"),
9590 die->offset.sect_off);
9591 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9592
9593 /* determine_prefix uses TYPE_TAG_NAME. */
9594 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9595
9596 return set_die_type (die, type, cu);
9597}
9598
9599/* Read a Fortran module. */
9600
9601static void
9602read_module (struct die_info *die, struct dwarf2_cu *cu)
9603{
9604 struct die_info *child_die = die->child;
9605
9606 while (child_die && child_die->tag)
9607 {
9608 process_die (child_die, cu);
9609 child_die = sibling_die (child_die);
9610 }
9611}
9612
9613/* Return the name of the namespace represented by DIE. Set
9614 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9615 namespace. */
9616
9617static const char *
9618namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
9619{
9620 struct die_info *current_die;
9621 const char *name = NULL;
9622
9623 /* Loop through the extensions until we find a name. */
9624
9625 for (current_die = die;
9626 current_die != NULL;
9627 current_die = dwarf2_extension (die, &cu))
9628 {
9629 name = dwarf2_name (current_die, cu);
9630 if (name != NULL)
9631 break;
9632 }
9633
9634 /* Is it an anonymous namespace? */
9635
9636 *is_anonymous = (name == NULL);
9637 if (*is_anonymous)
9638 name = CP_ANONYMOUS_NAMESPACE_STR;
9639
9640 return name;
9641}
9642
9643/* Extract all information from a DW_TAG_pointer_type DIE and add to
9644 the user defined type vector. */
9645
9646static struct type *
9647read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
9648{
9649 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9650 struct comp_unit_head *cu_header = &cu->header;
9651 struct type *type;
9652 struct attribute *attr_byte_size;
9653 struct attribute *attr_address_class;
9654 int byte_size, addr_class;
9655 struct type *target_type;
9656
9657 target_type = die_type (die, cu);
9658
9659 /* The die_type call above may have already set the type for this DIE. */
9660 type = get_die_type (die, cu);
9661 if (type)
9662 return type;
9663
9664 type = lookup_pointer_type (target_type);
9665
9666 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
9667 if (attr_byte_size)
9668 byte_size = DW_UNSND (attr_byte_size);
9669 else
9670 byte_size = cu_header->addr_size;
9671
9672 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
9673 if (attr_address_class)
9674 addr_class = DW_UNSND (attr_address_class);
9675 else
9676 addr_class = DW_ADDR_none;
9677
9678 /* If the pointer size or address class is different than the
9679 default, create a type variant marked as such and set the
9680 length accordingly. */
9681 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
9682 {
9683 if (gdbarch_address_class_type_flags_p (gdbarch))
9684 {
9685 int type_flags;
9686
9687 type_flags = gdbarch_address_class_type_flags
9688 (gdbarch, byte_size, addr_class);
9689 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9690 == 0);
9691 type = make_type_with_address_space (type, type_flags);
9692 }
9693 else if (TYPE_LENGTH (type) != byte_size)
9694 {
9695 complaint (&symfile_complaints,
9696 _("invalid pointer size %d"), byte_size);
9697 }
9698 else
9699 {
9700 /* Should we also complain about unhandled address classes? */
9701 }
9702 }
9703
9704 TYPE_LENGTH (type) = byte_size;
9705 return set_die_type (die, type, cu);
9706}
9707
9708/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9709 the user defined type vector. */
9710
9711static struct type *
9712read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
9713{
9714 struct type *type;
9715 struct type *to_type;
9716 struct type *domain;
9717
9718 to_type = die_type (die, cu);
9719 domain = die_containing_type (die, cu);
9720
9721 /* The calls above may have already set the type for this DIE. */
9722 type = get_die_type (die, cu);
9723 if (type)
9724 return type;
9725
9726 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9727 type = lookup_methodptr_type (to_type);
9728 else
9729 type = lookup_memberptr_type (to_type, domain);
9730
9731 return set_die_type (die, type, cu);
9732}
9733
9734/* Extract all information from a DW_TAG_reference_type DIE and add to
9735 the user defined type vector. */
9736
9737static struct type *
9738read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
9739{
9740 struct comp_unit_head *cu_header = &cu->header;
9741 struct type *type, *target_type;
9742 struct attribute *attr;
9743
9744 target_type = die_type (die, cu);
9745
9746 /* The die_type call above may have already set the type for this DIE. */
9747 type = get_die_type (die, cu);
9748 if (type)
9749 return type;
9750
9751 type = lookup_reference_type (target_type);
9752 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9753 if (attr)
9754 {
9755 TYPE_LENGTH (type) = DW_UNSND (attr);
9756 }
9757 else
9758 {
9759 TYPE_LENGTH (type) = cu_header->addr_size;
9760 }
9761 return set_die_type (die, type, cu);
9762}
9763
9764static struct type *
9765read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
9766{
9767 struct type *base_type, *cv_type;
9768
9769 base_type = die_type (die, cu);
9770
9771 /* The die_type call above may have already set the type for this DIE. */
9772 cv_type = get_die_type (die, cu);
9773 if (cv_type)
9774 return cv_type;
9775
9776 /* In case the const qualifier is applied to an array type, the element type
9777 is so qualified, not the array type (section 6.7.3 of C99). */
9778 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
9779 {
9780 struct type *el_type, *inner_array;
9781
9782 base_type = copy_type (base_type);
9783 inner_array = base_type;
9784
9785 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
9786 {
9787 TYPE_TARGET_TYPE (inner_array) =
9788 copy_type (TYPE_TARGET_TYPE (inner_array));
9789 inner_array = TYPE_TARGET_TYPE (inner_array);
9790 }
9791
9792 el_type = TYPE_TARGET_TYPE (inner_array);
9793 TYPE_TARGET_TYPE (inner_array) =
9794 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
9795
9796 return set_die_type (die, base_type, cu);
9797 }
9798
9799 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
9800 return set_die_type (die, cv_type, cu);
9801}
9802
9803static struct type *
9804read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
9805{
9806 struct type *base_type, *cv_type;
9807
9808 base_type = die_type (die, cu);
9809
9810 /* The die_type call above may have already set the type for this DIE. */
9811 cv_type = get_die_type (die, cu);
9812 if (cv_type)
9813 return cv_type;
9814
9815 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
9816 return set_die_type (die, cv_type, cu);
9817}
9818
9819/* Extract all information from a DW_TAG_string_type DIE and add to
9820 the user defined type vector. It isn't really a user defined type,
9821 but it behaves like one, with other DIE's using an AT_user_def_type
9822 attribute to reference it. */
9823
9824static struct type *
9825read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
9826{
9827 struct objfile *objfile = cu->objfile;
9828 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9829 struct type *type, *range_type, *index_type, *char_type;
9830 struct attribute *attr;
9831 unsigned int length;
9832
9833 attr = dwarf2_attr (die, DW_AT_string_length, cu);
9834 if (attr)
9835 {
9836 length = DW_UNSND (attr);
9837 }
9838 else
9839 {
9840 /* Check for the DW_AT_byte_size attribute. */
9841 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9842 if (attr)
9843 {
9844 length = DW_UNSND (attr);
9845 }
9846 else
9847 {
9848 length = 1;
9849 }
9850 }
9851
9852 index_type = objfile_type (objfile)->builtin_int;
9853 range_type = create_range_type (NULL, index_type, 1, length);
9854 char_type = language_string_char_type (cu->language_defn, gdbarch);
9855 type = create_string_type (NULL, char_type, range_type);
9856
9857 return set_die_type (die, type, cu);
9858}
9859
9860/* Handle DIES due to C code like:
9861
9862 struct foo
9863 {
9864 int (*funcp)(int a, long l);
9865 int b;
9866 };
9867
9868 ('funcp' generates a DW_TAG_subroutine_type DIE). */
9869
9870static struct type *
9871read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
9872{
9873 struct objfile *objfile = cu->objfile;
9874 struct type *type; /* Type that this function returns. */
9875 struct type *ftype; /* Function that returns above type. */
9876 struct attribute *attr;
9877
9878 type = die_type (die, cu);
9879
9880 /* The die_type call above may have already set the type for this DIE. */
9881 ftype = get_die_type (die, cu);
9882 if (ftype)
9883 return ftype;
9884
9885 ftype = lookup_function_type (type);
9886
9887 /* All functions in C++, Pascal and Java have prototypes. */
9888 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
9889 if ((attr && (DW_UNSND (attr) != 0))
9890 || cu->language == language_cplus
9891 || cu->language == language_java
9892 || cu->language == language_pascal)
9893 TYPE_PROTOTYPED (ftype) = 1;
9894 else if (producer_is_realview (cu->producer))
9895 /* RealView does not emit DW_AT_prototyped. We can not
9896 distinguish prototyped and unprototyped functions; default to
9897 prototyped, since that is more common in modern code (and
9898 RealView warns about unprototyped functions). */
9899 TYPE_PROTOTYPED (ftype) = 1;
9900
9901 /* Store the calling convention in the type if it's available in
9902 the subroutine die. Otherwise set the calling convention to
9903 the default value DW_CC_normal. */
9904 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
9905 if (attr)
9906 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
9907 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
9908 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
9909 else
9910 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
9911
9912 /* We need to add the subroutine type to the die immediately so
9913 we don't infinitely recurse when dealing with parameters
9914 declared as the same subroutine type. */
9915 set_die_type (die, ftype, cu);
9916
9917 if (die->child != NULL)
9918 {
9919 struct type *void_type = objfile_type (objfile)->builtin_void;
9920 struct die_info *child_die;
9921 int nparams, iparams;
9922
9923 /* Count the number of parameters.
9924 FIXME: GDB currently ignores vararg functions, but knows about
9925 vararg member functions. */
9926 nparams = 0;
9927 child_die = die->child;
9928 while (child_die && child_die->tag)
9929 {
9930 if (child_die->tag == DW_TAG_formal_parameter)
9931 nparams++;
9932 else if (child_die->tag == DW_TAG_unspecified_parameters)
9933 TYPE_VARARGS (ftype) = 1;
9934 child_die = sibling_die (child_die);
9935 }
9936
9937 /* Allocate storage for parameters and fill them in. */
9938 TYPE_NFIELDS (ftype) = nparams;
9939 TYPE_FIELDS (ftype) = (struct field *)
9940 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
9941
9942 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
9943 even if we error out during the parameters reading below. */
9944 for (iparams = 0; iparams < nparams; iparams++)
9945 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
9946
9947 iparams = 0;
9948 child_die = die->child;
9949 while (child_die && child_die->tag)
9950 {
9951 if (child_die->tag == DW_TAG_formal_parameter)
9952 {
9953 struct type *arg_type;
9954
9955 /* DWARF version 2 has no clean way to discern C++
9956 static and non-static member functions. G++ helps
9957 GDB by marking the first parameter for non-static
9958 member functions (which is the this pointer) as
9959 artificial. We pass this information to
9960 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
9961
9962 DWARF version 3 added DW_AT_object_pointer, which GCC
9963 4.5 does not yet generate. */
9964 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
9965 if (attr)
9966 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
9967 else
9968 {
9969 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
9970
9971 /* GCC/43521: In java, the formal parameter
9972 "this" is sometimes not marked with DW_AT_artificial. */
9973 if (cu->language == language_java)
9974 {
9975 const char *name = dwarf2_name (child_die, cu);
9976
9977 if (name && !strcmp (name, "this"))
9978 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
9979 }
9980 }
9981 arg_type = die_type (child_die, cu);
9982
9983 /* RealView does not mark THIS as const, which the testsuite
9984 expects. GCC marks THIS as const in method definitions,
9985 but not in the class specifications (GCC PR 43053). */
9986 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
9987 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
9988 {
9989 int is_this = 0;
9990 struct dwarf2_cu *arg_cu = cu;
9991 const char *name = dwarf2_name (child_die, cu);
9992
9993 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
9994 if (attr)
9995 {
9996 /* If the compiler emits this, use it. */
9997 if (follow_die_ref (die, attr, &arg_cu) == child_die)
9998 is_this = 1;
9999 }
10000 else if (name && strcmp (name, "this") == 0)
10001 /* Function definitions will have the argument names. */
10002 is_this = 1;
10003 else if (name == NULL && iparams == 0)
10004 /* Declarations may not have the names, so like
10005 elsewhere in GDB, assume an artificial first
10006 argument is "this". */
10007 is_this = 1;
10008
10009 if (is_this)
10010 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10011 arg_type, 0);
10012 }
10013
10014 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10015 iparams++;
10016 }
10017 child_die = sibling_die (child_die);
10018 }
10019 }
10020
10021 return ftype;
10022}
10023
10024static struct type *
10025read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10026{
10027 struct objfile *objfile = cu->objfile;
10028 const char *name = NULL;
10029 struct type *this_type, *target_type;
10030
10031 name = dwarf2_full_name (NULL, die, cu);
10032 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10033 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10034 TYPE_NAME (this_type) = (char *) name;
10035 set_die_type (die, this_type, cu);
10036 target_type = die_type (die, cu);
10037 if (target_type != this_type)
10038 TYPE_TARGET_TYPE (this_type) = target_type;
10039 else
10040 {
10041 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10042 spec and cause infinite loops in GDB. */
10043 complaint (&symfile_complaints,
10044 _("Self-referential DW_TAG_typedef "
10045 "- DIE at 0x%x [in module %s]"),
10046 die->offset.sect_off, objfile->name);
10047 TYPE_TARGET_TYPE (this_type) = NULL;
10048 }
10049 return this_type;
10050}
10051
10052/* Find a representation of a given base type and install
10053 it in the TYPE field of the die. */
10054
10055static struct type *
10056read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10057{
10058 struct objfile *objfile = cu->objfile;
10059 struct type *type;
10060 struct attribute *attr;
10061 int encoding = 0, size = 0;
10062 char *name;
10063 enum type_code code = TYPE_CODE_INT;
10064 int type_flags = 0;
10065 struct type *target_type = NULL;
10066
10067 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10068 if (attr)
10069 {
10070 encoding = DW_UNSND (attr);
10071 }
10072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10073 if (attr)
10074 {
10075 size = DW_UNSND (attr);
10076 }
10077 name = dwarf2_name (die, cu);
10078 if (!name)
10079 {
10080 complaint (&symfile_complaints,
10081 _("DW_AT_name missing from DW_TAG_base_type"));
10082 }
10083
10084 switch (encoding)
10085 {
10086 case DW_ATE_address:
10087 /* Turn DW_ATE_address into a void * pointer. */
10088 code = TYPE_CODE_PTR;
10089 type_flags |= TYPE_FLAG_UNSIGNED;
10090 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10091 break;
10092 case DW_ATE_boolean:
10093 code = TYPE_CODE_BOOL;
10094 type_flags |= TYPE_FLAG_UNSIGNED;
10095 break;
10096 case DW_ATE_complex_float:
10097 code = TYPE_CODE_COMPLEX;
10098 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10099 break;
10100 case DW_ATE_decimal_float:
10101 code = TYPE_CODE_DECFLOAT;
10102 break;
10103 case DW_ATE_float:
10104 code = TYPE_CODE_FLT;
10105 break;
10106 case DW_ATE_signed:
10107 break;
10108 case DW_ATE_unsigned:
10109 type_flags |= TYPE_FLAG_UNSIGNED;
10110 if (cu->language == language_fortran
10111 && name
10112 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10113 code = TYPE_CODE_CHAR;
10114 break;
10115 case DW_ATE_signed_char:
10116 if (cu->language == language_ada || cu->language == language_m2
10117 || cu->language == language_pascal
10118 || cu->language == language_fortran)
10119 code = TYPE_CODE_CHAR;
10120 break;
10121 case DW_ATE_unsigned_char:
10122 if (cu->language == language_ada || cu->language == language_m2
10123 || cu->language == language_pascal
10124 || cu->language == language_fortran)
10125 code = TYPE_CODE_CHAR;
10126 type_flags |= TYPE_FLAG_UNSIGNED;
10127 break;
10128 case DW_ATE_UTF:
10129 /* We just treat this as an integer and then recognize the
10130 type by name elsewhere. */
10131 break;
10132
10133 default:
10134 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10135 dwarf_type_encoding_name (encoding));
10136 break;
10137 }
10138
10139 type = init_type (code, size, type_flags, NULL, objfile);
10140 TYPE_NAME (type) = name;
10141 TYPE_TARGET_TYPE (type) = target_type;
10142
10143 if (name && strcmp (name, "char") == 0)
10144 TYPE_NOSIGN (type) = 1;
10145
10146 return set_die_type (die, type, cu);
10147}
10148
10149/* Read the given DW_AT_subrange DIE. */
10150
10151static struct type *
10152read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10153{
10154 struct type *base_type;
10155 struct type *range_type;
10156 struct attribute *attr;
10157 LONGEST low, high;
10158 int low_default_is_valid;
10159 char *name;
10160 LONGEST negative_mask;
10161
10162 base_type = die_type (die, cu);
10163 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10164 check_typedef (base_type);
10165
10166 /* The die_type call above may have already set the type for this DIE. */
10167 range_type = get_die_type (die, cu);
10168 if (range_type)
10169 return range_type;
10170
10171 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10172 omitting DW_AT_lower_bound. */
10173 switch (cu->language)
10174 {
10175 case language_c:
10176 case language_cplus:
10177 low = 0;
10178 low_default_is_valid = 1;
10179 break;
10180 case language_fortran:
10181 low = 1;
10182 low_default_is_valid = 1;
10183 break;
10184 case language_d:
10185 case language_java:
10186 case language_objc:
10187 low = 0;
10188 low_default_is_valid = (cu->header.version >= 4);
10189 break;
10190 case language_ada:
10191 case language_m2:
10192 case language_pascal:
10193 low = 1;
10194 low_default_is_valid = (cu->header.version >= 4);
10195 break;
10196 default:
10197 low = 0;
10198 low_default_is_valid = 0;
10199 break;
10200 }
10201
10202 /* FIXME: For variable sized arrays either of these could be
10203 a variable rather than a constant value. We'll allow it,
10204 but we don't know how to handle it. */
10205 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10206 if (attr)
10207 low = dwarf2_get_attr_constant_value (attr, low);
10208 else if (!low_default_is_valid)
10209 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10210 "- DIE at 0x%x [in module %s]"),
10211 die->offset.sect_off, cu->objfile->name);
10212
10213 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10214 if (attr)
10215 {
10216 if (attr_form_is_block (attr) || is_ref_attr (attr))
10217 {
10218 /* GCC encodes arrays with unspecified or dynamic length
10219 with a DW_FORM_block1 attribute or a reference attribute.
10220 FIXME: GDB does not yet know how to handle dynamic
10221 arrays properly, treat them as arrays with unspecified
10222 length for now.
10223
10224 FIXME: jimb/2003-09-22: GDB does not really know
10225 how to handle arrays of unspecified length
10226 either; we just represent them as zero-length
10227 arrays. Choose an appropriate upper bound given
10228 the lower bound we've computed above. */
10229 high = low - 1;
10230 }
10231 else
10232 high = dwarf2_get_attr_constant_value (attr, 1);
10233 }
10234 else
10235 {
10236 attr = dwarf2_attr (die, DW_AT_count, cu);
10237 if (attr)
10238 {
10239 int count = dwarf2_get_attr_constant_value (attr, 1);
10240 high = low + count - 1;
10241 }
10242 else
10243 {
10244 /* Unspecified array length. */
10245 high = low - 1;
10246 }
10247 }
10248
10249 /* Dwarf-2 specifications explicitly allows to create subrange types
10250 without specifying a base type.
10251 In that case, the base type must be set to the type of
10252 the lower bound, upper bound or count, in that order, if any of these
10253 three attributes references an object that has a type.
10254 If no base type is found, the Dwarf-2 specifications say that
10255 a signed integer type of size equal to the size of an address should
10256 be used.
10257 For the following C code: `extern char gdb_int [];'
10258 GCC produces an empty range DIE.
10259 FIXME: muller/2010-05-28: Possible references to object for low bound,
10260 high bound or count are not yet handled by this code. */
10261 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10262 {
10263 struct objfile *objfile = cu->objfile;
10264 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10265 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10266 struct type *int_type = objfile_type (objfile)->builtin_int;
10267
10268 /* Test "int", "long int", and "long long int" objfile types,
10269 and select the first one having a size above or equal to the
10270 architecture address size. */
10271 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10272 base_type = int_type;
10273 else
10274 {
10275 int_type = objfile_type (objfile)->builtin_long;
10276 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10277 base_type = int_type;
10278 else
10279 {
10280 int_type = objfile_type (objfile)->builtin_long_long;
10281 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10282 base_type = int_type;
10283 }
10284 }
10285 }
10286
10287 negative_mask =
10288 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10289 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10290 low |= negative_mask;
10291 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10292 high |= negative_mask;
10293
10294 range_type = create_range_type (NULL, base_type, low, high);
10295
10296 /* Mark arrays with dynamic length at least as an array of unspecified
10297 length. GDB could check the boundary but before it gets implemented at
10298 least allow accessing the array elements. */
10299 if (attr && attr_form_is_block (attr))
10300 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10301
10302 /* Ada expects an empty array on no boundary attributes. */
10303 if (attr == NULL && cu->language != language_ada)
10304 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10305
10306 name = dwarf2_name (die, cu);
10307 if (name)
10308 TYPE_NAME (range_type) = name;
10309
10310 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10311 if (attr)
10312 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10313
10314 set_die_type (die, range_type, cu);
10315
10316 /* set_die_type should be already done. */
10317 set_descriptive_type (range_type, die, cu);
10318
10319 return range_type;
10320}
10321
10322static struct type *
10323read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10324{
10325 struct type *type;
10326
10327 /* For now, we only support the C meaning of an unspecified type: void. */
10328
10329 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10330 TYPE_NAME (type) = dwarf2_name (die, cu);
10331
10332 return set_die_type (die, type, cu);
10333}
10334
10335/* Read a single die and all its descendents. Set the die's sibling
10336 field to NULL; set other fields in the die correctly, and set all
10337 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10338 location of the info_ptr after reading all of those dies. PARENT
10339 is the parent of the die in question. */
10340
10341static struct die_info *
10342read_die_and_children (const struct die_reader_specs *reader,
10343 gdb_byte *info_ptr,
10344 gdb_byte **new_info_ptr,
10345 struct die_info *parent)
10346{
10347 struct die_info *die;
10348 gdb_byte *cur_ptr;
10349 int has_children;
10350
10351 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10352 if (die == NULL)
10353 {
10354 *new_info_ptr = cur_ptr;
10355 return NULL;
10356 }
10357 store_in_ref_table (die, reader->cu);
10358
10359 if (has_children)
10360 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10361 else
10362 {
10363 die->child = NULL;
10364 *new_info_ptr = cur_ptr;
10365 }
10366
10367 die->sibling = NULL;
10368 die->parent = parent;
10369 return die;
10370}
10371
10372/* Read a die, all of its descendents, and all of its siblings; set
10373 all of the fields of all of the dies correctly. Arguments are as
10374 in read_die_and_children. */
10375
10376static struct die_info *
10377read_die_and_siblings (const struct die_reader_specs *reader,
10378 gdb_byte *info_ptr,
10379 gdb_byte **new_info_ptr,
10380 struct die_info *parent)
10381{
10382 struct die_info *first_die, *last_sibling;
10383 gdb_byte *cur_ptr;
10384
10385 cur_ptr = info_ptr;
10386 first_die = last_sibling = NULL;
10387
10388 while (1)
10389 {
10390 struct die_info *die
10391 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10392
10393 if (die == NULL)
10394 {
10395 *new_info_ptr = cur_ptr;
10396 return first_die;
10397 }
10398
10399 if (!first_die)
10400 first_die = die;
10401 else
10402 last_sibling->sibling = die;
10403
10404 last_sibling = die;
10405 }
10406}
10407
10408/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10409 attributes.
10410 The caller is responsible for filling in the extra attributes
10411 and updating (*DIEP)->num_attrs.
10412 Set DIEP to point to a newly allocated die with its information,
10413 except for its child, sibling, and parent fields.
10414 Set HAS_CHILDREN to tell whether the die has children or not. */
10415
10416static gdb_byte *
10417read_full_die_1 (const struct die_reader_specs *reader,
10418 struct die_info **diep, gdb_byte *info_ptr,
10419 int *has_children, int num_extra_attrs)
10420{
10421 unsigned int abbrev_number, bytes_read, i;
10422 sect_offset offset;
10423 struct abbrev_info *abbrev;
10424 struct die_info *die;
10425 struct dwarf2_cu *cu = reader->cu;
10426 bfd *abfd = reader->abfd;
10427
10428 offset.sect_off = info_ptr - reader->buffer;
10429 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10430 info_ptr += bytes_read;
10431 if (!abbrev_number)
10432 {
10433 *diep = NULL;
10434 *has_children = 0;
10435 return info_ptr;
10436 }
10437
10438 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10439 if (!abbrev)
10440 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10441 abbrev_number,
10442 bfd_get_filename (abfd));
10443
10444 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10445 die->offset = offset;
10446 die->tag = abbrev->tag;
10447 die->abbrev = abbrev_number;
10448
10449 /* Make the result usable.
10450 The caller needs to update num_attrs after adding the extra
10451 attributes. */
10452 die->num_attrs = abbrev->num_attrs;
10453
10454 for (i = 0; i < abbrev->num_attrs; ++i)
10455 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10456 info_ptr);
10457
10458 *diep = die;
10459 *has_children = abbrev->has_children;
10460 return info_ptr;
10461}
10462
10463/* Read a die and all its attributes.
10464 Set DIEP to point to a newly allocated die with its information,
10465 except for its child, sibling, and parent fields.
10466 Set HAS_CHILDREN to tell whether the die has children or not. */
10467
10468static gdb_byte *
10469read_full_die (const struct die_reader_specs *reader,
10470 struct die_info **diep, gdb_byte *info_ptr,
10471 int *has_children)
10472{
10473 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10474}
10475
10476/* In DWARF version 2, the description of the debugging information is
10477 stored in a separate .debug_abbrev section. Before we read any
10478 dies from a section we read in all abbreviations and install them
10479 in a hash table. This function also sets flags in CU describing
10480 the data found in the abbrev table. */
10481
10482static void
10483dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10484 struct dwarf2_section_info *abbrev_section)
10485
10486{
10487 bfd *abfd = abbrev_section->asection->owner;
10488 struct comp_unit_head *cu_header = &cu->header;
10489 gdb_byte *abbrev_ptr;
10490 struct abbrev_info *cur_abbrev;
10491 unsigned int abbrev_number, bytes_read, abbrev_name;
10492 unsigned int abbrev_form, hash_number;
10493 struct attr_abbrev *cur_attrs;
10494 unsigned int allocated_attrs;
10495
10496 /* Initialize dwarf2 abbrevs. */
10497 obstack_init (&cu->abbrev_obstack);
10498 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10499 (ABBREV_HASH_SIZE
10500 * sizeof (struct abbrev_info *)));
10501 memset (cu->dwarf2_abbrevs, 0,
10502 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10503
10504 dwarf2_read_section (cu->objfile, abbrev_section);
10505 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10506 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10507 abbrev_ptr += bytes_read;
10508
10509 allocated_attrs = ATTR_ALLOC_CHUNK;
10510 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10511
10512 /* Loop until we reach an abbrev number of 0. */
10513 while (abbrev_number)
10514 {
10515 cur_abbrev = dwarf_alloc_abbrev (cu);
10516
10517 /* read in abbrev header */
10518 cur_abbrev->number = abbrev_number;
10519 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10520 abbrev_ptr += bytes_read;
10521 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10522 abbrev_ptr += 1;
10523
10524 /* now read in declarations */
10525 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10526 abbrev_ptr += bytes_read;
10527 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10528 abbrev_ptr += bytes_read;
10529 while (abbrev_name)
10530 {
10531 if (cur_abbrev->num_attrs == allocated_attrs)
10532 {
10533 allocated_attrs += ATTR_ALLOC_CHUNK;
10534 cur_attrs
10535 = xrealloc (cur_attrs, (allocated_attrs
10536 * sizeof (struct attr_abbrev)));
10537 }
10538
10539 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10540 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
10541 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10542 abbrev_ptr += bytes_read;
10543 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10544 abbrev_ptr += bytes_read;
10545 }
10546
10547 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10548 (cur_abbrev->num_attrs
10549 * sizeof (struct attr_abbrev)));
10550 memcpy (cur_abbrev->attrs, cur_attrs,
10551 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10552
10553 hash_number = abbrev_number % ABBREV_HASH_SIZE;
10554 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10555 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
10556
10557 /* Get next abbreviation.
10558 Under Irix6 the abbreviations for a compilation unit are not
10559 always properly terminated with an abbrev number of 0.
10560 Exit loop if we encounter an abbreviation which we have
10561 already read (which means we are about to read the abbreviations
10562 for the next compile unit) or if the end of the abbreviation
10563 table is reached. */
10564 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10565 >= abbrev_section->size)
10566 break;
10567 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10568 abbrev_ptr += bytes_read;
10569 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
10570 break;
10571 }
10572
10573 xfree (cur_attrs);
10574}
10575
10576/* Release the memory used by the abbrev table for a compilation unit. */
10577
10578static void
10579dwarf2_free_abbrev_table (void *ptr_to_cu)
10580{
10581 struct dwarf2_cu *cu = ptr_to_cu;
10582
10583 obstack_free (&cu->abbrev_obstack, NULL);
10584 cu->dwarf2_abbrevs = NULL;
10585}
10586
10587/* Lookup an abbrev_info structure in the abbrev hash table. */
10588
10589static struct abbrev_info *
10590dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
10591{
10592 unsigned int hash_number;
10593 struct abbrev_info *abbrev;
10594
10595 hash_number = number % ABBREV_HASH_SIZE;
10596 abbrev = cu->dwarf2_abbrevs[hash_number];
10597
10598 while (abbrev)
10599 {
10600 if (abbrev->number == number)
10601 return abbrev;
10602 else
10603 abbrev = abbrev->next;
10604 }
10605 return NULL;
10606}
10607
10608/* Returns nonzero if TAG represents a type that we might generate a partial
10609 symbol for. */
10610
10611static int
10612is_type_tag_for_partial (int tag)
10613{
10614 switch (tag)
10615 {
10616#if 0
10617 /* Some types that would be reasonable to generate partial symbols for,
10618 that we don't at present. */
10619 case DW_TAG_array_type:
10620 case DW_TAG_file_type:
10621 case DW_TAG_ptr_to_member_type:
10622 case DW_TAG_set_type:
10623 case DW_TAG_string_type:
10624 case DW_TAG_subroutine_type:
10625#endif
10626 case DW_TAG_base_type:
10627 case DW_TAG_class_type:
10628 case DW_TAG_interface_type:
10629 case DW_TAG_enumeration_type:
10630 case DW_TAG_structure_type:
10631 case DW_TAG_subrange_type:
10632 case DW_TAG_typedef:
10633 case DW_TAG_union_type:
10634 return 1;
10635 default:
10636 return 0;
10637 }
10638}
10639
10640/* Load all DIEs that are interesting for partial symbols into memory. */
10641
10642static struct partial_die_info *
10643load_partial_dies (const struct die_reader_specs *reader,
10644 gdb_byte *info_ptr, int building_psymtab)
10645{
10646 struct dwarf2_cu *cu = reader->cu;
10647 struct objfile *objfile = cu->objfile;
10648 struct partial_die_info *part_die;
10649 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10650 struct abbrev_info *abbrev;
10651 unsigned int bytes_read;
10652 unsigned int load_all = 0;
10653 int nesting_level = 1;
10654
10655 parent_die = NULL;
10656 last_die = NULL;
10657
10658 gdb_assert (cu->per_cu != NULL);
10659 if (cu->per_cu->load_all_dies)
10660 load_all = 1;
10661
10662 cu->partial_dies
10663 = htab_create_alloc_ex (cu->header.length / 12,
10664 partial_die_hash,
10665 partial_die_eq,
10666 NULL,
10667 &cu->comp_unit_obstack,
10668 hashtab_obstack_allocate,
10669 dummy_obstack_deallocate);
10670
10671 part_die = obstack_alloc (&cu->comp_unit_obstack,
10672 sizeof (struct partial_die_info));
10673
10674 while (1)
10675 {
10676 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10677
10678 /* A NULL abbrev means the end of a series of children. */
10679 if (abbrev == NULL)
10680 {
10681 if (--nesting_level == 0)
10682 {
10683 /* PART_DIE was probably the last thing allocated on the
10684 comp_unit_obstack, so we could call obstack_free
10685 here. We don't do that because the waste is small,
10686 and will be cleaned up when we're done with this
10687 compilation unit. This way, we're also more robust
10688 against other users of the comp_unit_obstack. */
10689 return first_die;
10690 }
10691 info_ptr += bytes_read;
10692 last_die = parent_die;
10693 parent_die = parent_die->die_parent;
10694 continue;
10695 }
10696
10697 /* Check for template arguments. We never save these; if
10698 they're seen, we just mark the parent, and go on our way. */
10699 if (parent_die != NULL
10700 && cu->language == language_cplus
10701 && (abbrev->tag == DW_TAG_template_type_param
10702 || abbrev->tag == DW_TAG_template_value_param))
10703 {
10704 parent_die->has_template_arguments = 1;
10705
10706 if (!load_all)
10707 {
10708 /* We don't need a partial DIE for the template argument. */
10709 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10710 continue;
10711 }
10712 }
10713
10714 /* We only recurse into c++ subprograms looking for template arguments.
10715 Skip their other children. */
10716 if (!load_all
10717 && cu->language == language_cplus
10718 && parent_die != NULL
10719 && parent_die->tag == DW_TAG_subprogram)
10720 {
10721 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10722 continue;
10723 }
10724
10725 /* Check whether this DIE is interesting enough to save. Normally
10726 we would not be interested in members here, but there may be
10727 later variables referencing them via DW_AT_specification (for
10728 static members). */
10729 if (!load_all
10730 && !is_type_tag_for_partial (abbrev->tag)
10731 && abbrev->tag != DW_TAG_constant
10732 && abbrev->tag != DW_TAG_enumerator
10733 && abbrev->tag != DW_TAG_subprogram
10734 && abbrev->tag != DW_TAG_lexical_block
10735 && abbrev->tag != DW_TAG_variable
10736 && abbrev->tag != DW_TAG_namespace
10737 && abbrev->tag != DW_TAG_module
10738 && abbrev->tag != DW_TAG_member)
10739 {
10740 /* Otherwise we skip to the next sibling, if any. */
10741 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10742 continue;
10743 }
10744
10745 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10746 info_ptr);
10747
10748 /* This two-pass algorithm for processing partial symbols has a
10749 high cost in cache pressure. Thus, handle some simple cases
10750 here which cover the majority of C partial symbols. DIEs
10751 which neither have specification tags in them, nor could have
10752 specification tags elsewhere pointing at them, can simply be
10753 processed and discarded.
10754
10755 This segment is also optional; scan_partial_symbols and
10756 add_partial_symbol will handle these DIEs if we chain
10757 them in normally. When compilers which do not emit large
10758 quantities of duplicate debug information are more common,
10759 this code can probably be removed. */
10760
10761 /* Any complete simple types at the top level (pretty much all
10762 of them, for a language without namespaces), can be processed
10763 directly. */
10764 if (parent_die == NULL
10765 && part_die->has_specification == 0
10766 && part_die->is_declaration == 0
10767 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
10768 || part_die->tag == DW_TAG_base_type
10769 || part_die->tag == DW_TAG_subrange_type))
10770 {
10771 if (building_psymtab && part_die->name != NULL)
10772 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
10773 VAR_DOMAIN, LOC_TYPEDEF,
10774 &objfile->static_psymbols,
10775 0, (CORE_ADDR) 0, cu->language, objfile);
10776 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
10777 continue;
10778 }
10779
10780 /* The exception for DW_TAG_typedef with has_children above is
10781 a workaround of GCC PR debug/47510. In the case of this complaint
10782 type_name_no_tag_or_error will error on such types later.
10783
10784 GDB skipped children of DW_TAG_typedef by the shortcut above and then
10785 it could not find the child DIEs referenced later, this is checked
10786 above. In correct DWARF DW_TAG_typedef should have no children. */
10787
10788 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
10789 complaint (&symfile_complaints,
10790 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
10791 "- DIE at 0x%x [in module %s]"),
10792 part_die->offset.sect_off, objfile->name);
10793
10794 /* If we're at the second level, and we're an enumerator, and
10795 our parent has no specification (meaning possibly lives in a
10796 namespace elsewhere), then we can add the partial symbol now
10797 instead of queueing it. */
10798 if (part_die->tag == DW_TAG_enumerator
10799 && parent_die != NULL
10800 && parent_die->die_parent == NULL
10801 && parent_die->tag == DW_TAG_enumeration_type
10802 && parent_die->has_specification == 0)
10803 {
10804 if (part_die->name == NULL)
10805 complaint (&symfile_complaints,
10806 _("malformed enumerator DIE ignored"));
10807 else if (building_psymtab)
10808 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
10809 VAR_DOMAIN, LOC_CONST,
10810 (cu->language == language_cplus
10811 || cu->language == language_java)
10812 ? &objfile->global_psymbols
10813 : &objfile->static_psymbols,
10814 0, (CORE_ADDR) 0, cu->language, objfile);
10815
10816 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
10817 continue;
10818 }
10819
10820 /* We'll save this DIE so link it in. */
10821 part_die->die_parent = parent_die;
10822 part_die->die_sibling = NULL;
10823 part_die->die_child = NULL;
10824
10825 if (last_die && last_die == parent_die)
10826 last_die->die_child = part_die;
10827 else if (last_die)
10828 last_die->die_sibling = part_die;
10829
10830 last_die = part_die;
10831
10832 if (first_die == NULL)
10833 first_die = part_die;
10834
10835 /* Maybe add the DIE to the hash table. Not all DIEs that we
10836 find interesting need to be in the hash table, because we
10837 also have the parent/sibling/child chains; only those that we
10838 might refer to by offset later during partial symbol reading.
10839
10840 For now this means things that might have be the target of a
10841 DW_AT_specification, DW_AT_abstract_origin, or
10842 DW_AT_extension. DW_AT_extension will refer only to
10843 namespaces; DW_AT_abstract_origin refers to functions (and
10844 many things under the function DIE, but we do not recurse
10845 into function DIEs during partial symbol reading) and
10846 possibly variables as well; DW_AT_specification refers to
10847 declarations. Declarations ought to have the DW_AT_declaration
10848 flag. It happens that GCC forgets to put it in sometimes, but
10849 only for functions, not for types.
10850
10851 Adding more things than necessary to the hash table is harmless
10852 except for the performance cost. Adding too few will result in
10853 wasted time in find_partial_die, when we reread the compilation
10854 unit with load_all_dies set. */
10855
10856 if (load_all
10857 || abbrev->tag == DW_TAG_constant
10858 || abbrev->tag == DW_TAG_subprogram
10859 || abbrev->tag == DW_TAG_variable
10860 || abbrev->tag == DW_TAG_namespace
10861 || part_die->is_declaration)
10862 {
10863 void **slot;
10864
10865 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
10866 part_die->offset.sect_off, INSERT);
10867 *slot = part_die;
10868 }
10869
10870 part_die = obstack_alloc (&cu->comp_unit_obstack,
10871 sizeof (struct partial_die_info));
10872
10873 /* For some DIEs we want to follow their children (if any). For C
10874 we have no reason to follow the children of structures; for other
10875 languages we have to, so that we can get at method physnames
10876 to infer fully qualified class names, for DW_AT_specification,
10877 and for C++ template arguments. For C++, we also look one level
10878 inside functions to find template arguments (if the name of the
10879 function does not already contain the template arguments).
10880
10881 For Ada, we need to scan the children of subprograms and lexical
10882 blocks as well because Ada allows the definition of nested
10883 entities that could be interesting for the debugger, such as
10884 nested subprograms for instance. */
10885 if (last_die->has_children
10886 && (load_all
10887 || last_die->tag == DW_TAG_namespace
10888 || last_die->tag == DW_TAG_module
10889 || last_die->tag == DW_TAG_enumeration_type
10890 || (cu->language == language_cplus
10891 && last_die->tag == DW_TAG_subprogram
10892 && (last_die->name == NULL
10893 || strchr (last_die->name, '<') == NULL))
10894 || (cu->language != language_c
10895 && (last_die->tag == DW_TAG_class_type
10896 || last_die->tag == DW_TAG_interface_type
10897 || last_die->tag == DW_TAG_structure_type
10898 || last_die->tag == DW_TAG_union_type))
10899 || (cu->language == language_ada
10900 && (last_die->tag == DW_TAG_subprogram
10901 || last_die->tag == DW_TAG_lexical_block))))
10902 {
10903 nesting_level++;
10904 parent_die = last_die;
10905 continue;
10906 }
10907
10908 /* Otherwise we skip to the next sibling, if any. */
10909 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
10910
10911 /* Back to the top, do it again. */
10912 }
10913}
10914
10915/* Read a minimal amount of information into the minimal die structure. */
10916
10917static gdb_byte *
10918read_partial_die (const struct die_reader_specs *reader,
10919 struct partial_die_info *part_die,
10920 struct abbrev_info *abbrev, unsigned int abbrev_len,
10921 gdb_byte *info_ptr)
10922{
10923 struct dwarf2_cu *cu = reader->cu;
10924 struct objfile *objfile = cu->objfile;
10925 gdb_byte *buffer = reader->buffer;
10926 unsigned int i;
10927 struct attribute attr;
10928 int has_low_pc_attr = 0;
10929 int has_high_pc_attr = 0;
10930 int high_pc_relative = 0;
10931
10932 memset (part_die, 0, sizeof (struct partial_die_info));
10933
10934 part_die->offset.sect_off = info_ptr - buffer;
10935
10936 info_ptr += abbrev_len;
10937
10938 if (abbrev == NULL)
10939 return info_ptr;
10940
10941 part_die->tag = abbrev->tag;
10942 part_die->has_children = abbrev->has_children;
10943
10944 for (i = 0; i < abbrev->num_attrs; ++i)
10945 {
10946 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
10947
10948 /* Store the data if it is of an attribute we want to keep in a
10949 partial symbol table. */
10950 switch (attr.name)
10951 {
10952 case DW_AT_name:
10953 switch (part_die->tag)
10954 {
10955 case DW_TAG_compile_unit:
10956 case DW_TAG_type_unit:
10957 /* Compilation units have a DW_AT_name that is a filename, not
10958 a source language identifier. */
10959 case DW_TAG_enumeration_type:
10960 case DW_TAG_enumerator:
10961 /* These tags always have simple identifiers already; no need
10962 to canonicalize them. */
10963 part_die->name = DW_STRING (&attr);
10964 break;
10965 default:
10966 part_die->name
10967 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
10968 &objfile->objfile_obstack);
10969 break;
10970 }
10971 break;
10972 case DW_AT_linkage_name:
10973 case DW_AT_MIPS_linkage_name:
10974 /* Note that both forms of linkage name might appear. We
10975 assume they will be the same, and we only store the last
10976 one we see. */
10977 if (cu->language == language_ada)
10978 part_die->name = DW_STRING (&attr);
10979 part_die->linkage_name = DW_STRING (&attr);
10980 break;
10981 case DW_AT_low_pc:
10982 has_low_pc_attr = 1;
10983 part_die->lowpc = DW_ADDR (&attr);
10984 break;
10985 case DW_AT_high_pc:
10986 has_high_pc_attr = 1;
10987 if (attr.form == DW_FORM_addr
10988 || attr.form == DW_FORM_GNU_addr_index)
10989 part_die->highpc = DW_ADDR (&attr);
10990 else
10991 {
10992 high_pc_relative = 1;
10993 part_die->highpc = DW_UNSND (&attr);
10994 }
10995 break;
10996 case DW_AT_location:
10997 /* Support the .debug_loc offsets. */
10998 if (attr_form_is_block (&attr))
10999 {
11000 part_die->locdesc = DW_BLOCK (&attr);
11001 }
11002 else if (attr_form_is_section_offset (&attr))
11003 {
11004 dwarf2_complex_location_expr_complaint ();
11005 }
11006 else
11007 {
11008 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11009 "partial symbol information");
11010 }
11011 break;
11012 case DW_AT_external:
11013 part_die->is_external = DW_UNSND (&attr);
11014 break;
11015 case DW_AT_declaration:
11016 part_die->is_declaration = DW_UNSND (&attr);
11017 break;
11018 case DW_AT_type:
11019 part_die->has_type = 1;
11020 break;
11021 case DW_AT_abstract_origin:
11022 case DW_AT_specification:
11023 case DW_AT_extension:
11024 part_die->has_specification = 1;
11025 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11026 break;
11027 case DW_AT_sibling:
11028 /* Ignore absolute siblings, they might point outside of
11029 the current compile unit. */
11030 if (attr.form == DW_FORM_ref_addr)
11031 complaint (&symfile_complaints,
11032 _("ignoring absolute DW_AT_sibling"));
11033 else
11034 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11035 break;
11036 case DW_AT_byte_size:
11037 part_die->has_byte_size = 1;
11038 break;
11039 case DW_AT_calling_convention:
11040 /* DWARF doesn't provide a way to identify a program's source-level
11041 entry point. DW_AT_calling_convention attributes are only meant
11042 to describe functions' calling conventions.
11043
11044 However, because it's a necessary piece of information in
11045 Fortran, and because DW_CC_program is the only piece of debugging
11046 information whose definition refers to a 'main program' at all,
11047 several compilers have begun marking Fortran main programs with
11048 DW_CC_program --- even when those functions use the standard
11049 calling conventions.
11050
11051 So until DWARF specifies a way to provide this information and
11052 compilers pick up the new representation, we'll support this
11053 practice. */
11054 if (DW_UNSND (&attr) == DW_CC_program
11055 && cu->language == language_fortran)
11056 {
11057 set_main_name (part_die->name);
11058
11059 /* As this DIE has a static linkage the name would be difficult
11060 to look up later. */
11061 language_of_main = language_fortran;
11062 }
11063 break;
11064 case DW_AT_inline:
11065 if (DW_UNSND (&attr) == DW_INL_inlined
11066 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11067 part_die->may_be_inlined = 1;
11068 break;
11069 default:
11070 break;
11071 }
11072 }
11073
11074 if (high_pc_relative)
11075 part_die->highpc += part_die->lowpc;
11076
11077 if (has_low_pc_attr && has_high_pc_attr)
11078 {
11079 /* When using the GNU linker, .gnu.linkonce. sections are used to
11080 eliminate duplicate copies of functions and vtables and such.
11081 The linker will arbitrarily choose one and discard the others.
11082 The AT_*_pc values for such functions refer to local labels in
11083 these sections. If the section from that file was discarded, the
11084 labels are not in the output, so the relocs get a value of 0.
11085 If this is a discarded function, mark the pc bounds as invalid,
11086 so that GDB will ignore it. */
11087 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11088 {
11089 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11090
11091 complaint (&symfile_complaints,
11092 _("DW_AT_low_pc %s is zero "
11093 "for DIE at 0x%x [in module %s]"),
11094 paddress (gdbarch, part_die->lowpc),
11095 part_die->offset.sect_off, objfile->name);
11096 }
11097 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11098 else if (part_die->lowpc >= part_die->highpc)
11099 {
11100 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11101
11102 complaint (&symfile_complaints,
11103 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11104 "for DIE at 0x%x [in module %s]"),
11105 paddress (gdbarch, part_die->lowpc),
11106 paddress (gdbarch, part_die->highpc),
11107 part_die->offset.sect_off, objfile->name);
11108 }
11109 else
11110 part_die->has_pc_info = 1;
11111 }
11112
11113 return info_ptr;
11114}
11115
11116/* Find a cached partial DIE at OFFSET in CU. */
11117
11118static struct partial_die_info *
11119find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11120{
11121 struct partial_die_info *lookup_die = NULL;
11122 struct partial_die_info part_die;
11123
11124 part_die.offset = offset;
11125 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11126 offset.sect_off);
11127
11128 return lookup_die;
11129}
11130
11131/* Find a partial DIE at OFFSET, which may or may not be in CU,
11132 except in the case of .debug_types DIEs which do not reference
11133 outside their CU (they do however referencing other types via
11134 DW_FORM_ref_sig8). */
11135
11136static struct partial_die_info *
11137find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11138{
11139 struct objfile *objfile = cu->objfile;
11140 struct dwarf2_per_cu_data *per_cu = NULL;
11141 struct partial_die_info *pd = NULL;
11142
11143 if (offset_in_cu_p (&cu->header, offset))
11144 {
11145 pd = find_partial_die_in_comp_unit (offset, cu);
11146 if (pd != NULL)
11147 return pd;
11148 /* We missed recording what we needed.
11149 Load all dies and try again. */
11150 per_cu = cu->per_cu;
11151 }
11152 else
11153 {
11154 /* TUs don't reference other CUs/TUs (except via type signatures). */
11155 if (cu->per_cu->is_debug_types)
11156 {
11157 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11158 " external reference to offset 0x%lx [in module %s].\n"),
11159 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11160 bfd_get_filename (objfile->obfd));
11161 }
11162 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11163
11164 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11165 load_partial_comp_unit (per_cu);
11166
11167 per_cu->cu->last_used = 0;
11168 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11169 }
11170
11171 /* If we didn't find it, and not all dies have been loaded,
11172 load them all and try again. */
11173
11174 if (pd == NULL && per_cu->load_all_dies == 0)
11175 {
11176 /* FIXME: The testsuite doesn't trigger this code path.
11177 http://sourceware.org/bugzilla/show_bug.cgi?id=13961 */
11178
11179 free_one_cached_comp_unit (per_cu);
11180 per_cu->load_all_dies = 1;
11181 load_partial_comp_unit (per_cu);
11182
11183 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11184 }
11185
11186 if (pd == NULL)
11187 internal_error (__FILE__, __LINE__,
11188 _("could not find partial DIE 0x%x "
11189 "in cache [from module %s]\n"),
11190 offset.sect_off, bfd_get_filename (objfile->obfd));
11191 return pd;
11192}
11193
11194/* See if we can figure out if the class lives in a namespace. We do
11195 this by looking for a member function; its demangled name will
11196 contain namespace info, if there is any. */
11197
11198static void
11199guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11200 struct dwarf2_cu *cu)
11201{
11202 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11203 what template types look like, because the demangler
11204 frequently doesn't give the same name as the debug info. We
11205 could fix this by only using the demangled name to get the
11206 prefix (but see comment in read_structure_type). */
11207
11208 struct partial_die_info *real_pdi;
11209 struct partial_die_info *child_pdi;
11210
11211 /* If this DIE (this DIE's specification, if any) has a parent, then
11212 we should not do this. We'll prepend the parent's fully qualified
11213 name when we create the partial symbol. */
11214
11215 real_pdi = struct_pdi;
11216 while (real_pdi->has_specification)
11217 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11218
11219 if (real_pdi->die_parent != NULL)
11220 return;
11221
11222 for (child_pdi = struct_pdi->die_child;
11223 child_pdi != NULL;
11224 child_pdi = child_pdi->die_sibling)
11225 {
11226 if (child_pdi->tag == DW_TAG_subprogram
11227 && child_pdi->linkage_name != NULL)
11228 {
11229 char *actual_class_name
11230 = language_class_name_from_physname (cu->language_defn,
11231 child_pdi->linkage_name);
11232 if (actual_class_name != NULL)
11233 {
11234 struct_pdi->name
11235 = obsavestring (actual_class_name,
11236 strlen (actual_class_name),
11237 &cu->objfile->objfile_obstack);
11238 xfree (actual_class_name);
11239 }
11240 break;
11241 }
11242 }
11243}
11244
11245/* Adjust PART_DIE before generating a symbol for it. This function
11246 may set the is_external flag or change the DIE's name. */
11247
11248static void
11249fixup_partial_die (struct partial_die_info *part_die,
11250 struct dwarf2_cu *cu)
11251{
11252 /* Once we've fixed up a die, there's no point in doing so again.
11253 This also avoids a memory leak if we were to call
11254 guess_partial_die_structure_name multiple times. */
11255 if (part_die->fixup_called)
11256 return;
11257
11258 /* If we found a reference attribute and the DIE has no name, try
11259 to find a name in the referred to DIE. */
11260
11261 if (part_die->name == NULL && part_die->has_specification)
11262 {
11263 struct partial_die_info *spec_die;
11264
11265 spec_die = find_partial_die (part_die->spec_offset, cu);
11266
11267 fixup_partial_die (spec_die, cu);
11268
11269 if (spec_die->name)
11270 {
11271 part_die->name = spec_die->name;
11272
11273 /* Copy DW_AT_external attribute if it is set. */
11274 if (spec_die->is_external)
11275 part_die->is_external = spec_die->is_external;
11276 }
11277 }
11278
11279 /* Set default names for some unnamed DIEs. */
11280
11281 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11282 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11283
11284 /* If there is no parent die to provide a namespace, and there are
11285 children, see if we can determine the namespace from their linkage
11286 name. */
11287 if (cu->language == language_cplus
11288 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11289 && part_die->die_parent == NULL
11290 && part_die->has_children
11291 && (part_die->tag == DW_TAG_class_type
11292 || part_die->tag == DW_TAG_structure_type
11293 || part_die->tag == DW_TAG_union_type))
11294 guess_partial_die_structure_name (part_die, cu);
11295
11296 /* GCC might emit a nameless struct or union that has a linkage
11297 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11298 if (part_die->name == NULL
11299 && (part_die->tag == DW_TAG_class_type
11300 || part_die->tag == DW_TAG_interface_type
11301 || part_die->tag == DW_TAG_structure_type
11302 || part_die->tag == DW_TAG_union_type)
11303 && part_die->linkage_name != NULL)
11304 {
11305 char *demangled;
11306
11307 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11308 if (demangled)
11309 {
11310 const char *base;
11311
11312 /* Strip any leading namespaces/classes, keep only the base name.
11313 DW_AT_name for named DIEs does not contain the prefixes. */
11314 base = strrchr (demangled, ':');
11315 if (base && base > demangled && base[-1] == ':')
11316 base++;
11317 else
11318 base = demangled;
11319
11320 part_die->name = obsavestring (base, strlen (base),
11321 &cu->objfile->objfile_obstack);
11322 xfree (demangled);
11323 }
11324 }
11325
11326 part_die->fixup_called = 1;
11327}
11328
11329/* Read an attribute value described by an attribute form. */
11330
11331static gdb_byte *
11332read_attribute_value (const struct die_reader_specs *reader,
11333 struct attribute *attr, unsigned form,
11334 gdb_byte *info_ptr)
11335{
11336 struct dwarf2_cu *cu = reader->cu;
11337 bfd *abfd = reader->abfd;
11338 struct comp_unit_head *cu_header = &cu->header;
11339 unsigned int bytes_read;
11340 struct dwarf_block *blk;
11341
11342 attr->form = form;
11343 switch (form)
11344 {
11345 case DW_FORM_ref_addr:
11346 if (cu->header.version == 2)
11347 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11348 else
11349 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11350 &cu->header, &bytes_read);
11351 info_ptr += bytes_read;
11352 break;
11353 case DW_FORM_addr:
11354 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11355 info_ptr += bytes_read;
11356 break;
11357 case DW_FORM_block2:
11358 blk = dwarf_alloc_block (cu);
11359 blk->size = read_2_bytes (abfd, info_ptr);
11360 info_ptr += 2;
11361 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11362 info_ptr += blk->size;
11363 DW_BLOCK (attr) = blk;
11364 break;
11365 case DW_FORM_block4:
11366 blk = dwarf_alloc_block (cu);
11367 blk->size = read_4_bytes (abfd, info_ptr);
11368 info_ptr += 4;
11369 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11370 info_ptr += blk->size;
11371 DW_BLOCK (attr) = blk;
11372 break;
11373 case DW_FORM_data2:
11374 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11375 info_ptr += 2;
11376 break;
11377 case DW_FORM_data4:
11378 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11379 info_ptr += 4;
11380 break;
11381 case DW_FORM_data8:
11382 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11383 info_ptr += 8;
11384 break;
11385 case DW_FORM_sec_offset:
11386 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11387 info_ptr += bytes_read;
11388 break;
11389 case DW_FORM_string:
11390 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11391 DW_STRING_IS_CANONICAL (attr) = 0;
11392 info_ptr += bytes_read;
11393 break;
11394 case DW_FORM_strp:
11395 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11396 &bytes_read);
11397 DW_STRING_IS_CANONICAL (attr) = 0;
11398 info_ptr += bytes_read;
11399 break;
11400 case DW_FORM_exprloc:
11401 case DW_FORM_block:
11402 blk = dwarf_alloc_block (cu);
11403 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11404 info_ptr += bytes_read;
11405 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11406 info_ptr += blk->size;
11407 DW_BLOCK (attr) = blk;
11408 break;
11409 case DW_FORM_block1:
11410 blk = dwarf_alloc_block (cu);
11411 blk->size = read_1_byte (abfd, info_ptr);
11412 info_ptr += 1;
11413 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11414 info_ptr += blk->size;
11415 DW_BLOCK (attr) = blk;
11416 break;
11417 case DW_FORM_data1:
11418 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11419 info_ptr += 1;
11420 break;
11421 case DW_FORM_flag:
11422 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11423 info_ptr += 1;
11424 break;
11425 case DW_FORM_flag_present:
11426 DW_UNSND (attr) = 1;
11427 break;
11428 case DW_FORM_sdata:
11429 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11430 info_ptr += bytes_read;
11431 break;
11432 case DW_FORM_udata:
11433 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11434 info_ptr += bytes_read;
11435 break;
11436 case DW_FORM_ref1:
11437 DW_UNSND (attr) = (cu->header.offset.sect_off
11438 + read_1_byte (abfd, info_ptr));
11439 info_ptr += 1;
11440 break;
11441 case DW_FORM_ref2:
11442 DW_UNSND (attr) = (cu->header.offset.sect_off
11443 + read_2_bytes (abfd, info_ptr));
11444 info_ptr += 2;
11445 break;
11446 case DW_FORM_ref4:
11447 DW_UNSND (attr) = (cu->header.offset.sect_off
11448 + read_4_bytes (abfd, info_ptr));
11449 info_ptr += 4;
11450 break;
11451 case DW_FORM_ref8:
11452 DW_UNSND (attr) = (cu->header.offset.sect_off
11453 + read_8_bytes (abfd, info_ptr));
11454 info_ptr += 8;
11455 break;
11456 case DW_FORM_ref_sig8:
11457 /* Convert the signature to something we can record in DW_UNSND
11458 for later lookup.
11459 NOTE: This is NULL if the type wasn't found. */
11460 DW_SIGNATURED_TYPE (attr) =
11461 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11462 info_ptr += 8;
11463 break;
11464 case DW_FORM_ref_udata:
11465 DW_UNSND (attr) = (cu->header.offset.sect_off
11466 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11467 info_ptr += bytes_read;
11468 break;
11469 case DW_FORM_indirect:
11470 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11471 info_ptr += bytes_read;
11472 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11473 break;
11474 case DW_FORM_GNU_addr_index:
11475 if (reader->dwo_file == NULL)
11476 {
11477 /* For now flag a hard error.
11478 Later we can turn this into a complaint. */
11479 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11480 dwarf_form_name (form),
11481 bfd_get_filename (abfd));
11482 }
11483 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11484 info_ptr += bytes_read;
11485 break;
11486 case DW_FORM_GNU_str_index:
11487 if (reader->dwo_file == NULL)
11488 {
11489 /* For now flag a hard error.
11490 Later we can turn this into a complaint if warranted. */
11491 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11492 dwarf_form_name (form),
11493 bfd_get_filename (abfd));
11494 }
11495 {
11496 ULONGEST str_index =
11497 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11498
11499 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11500 DW_STRING_IS_CANONICAL (attr) = 0;
11501 info_ptr += bytes_read;
11502 }
11503 break;
11504 default:
11505 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11506 dwarf_form_name (form),
11507 bfd_get_filename (abfd));
11508 }
11509
11510 /* We have seen instances where the compiler tried to emit a byte
11511 size attribute of -1 which ended up being encoded as an unsigned
11512 0xffffffff. Although 0xffffffff is technically a valid size value,
11513 an object of this size seems pretty unlikely so we can relatively
11514 safely treat these cases as if the size attribute was invalid and
11515 treat them as zero by default. */
11516 if (attr->name == DW_AT_byte_size
11517 && form == DW_FORM_data4
11518 && DW_UNSND (attr) >= 0xffffffff)
11519 {
11520 complaint
11521 (&symfile_complaints,
11522 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11523 hex_string (DW_UNSND (attr)));
11524 DW_UNSND (attr) = 0;
11525 }
11526
11527 return info_ptr;
11528}
11529
11530/* Read an attribute described by an abbreviated attribute. */
11531
11532static gdb_byte *
11533read_attribute (const struct die_reader_specs *reader,
11534 struct attribute *attr, struct attr_abbrev *abbrev,
11535 gdb_byte *info_ptr)
11536{
11537 attr->name = abbrev->name;
11538 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
11539}
11540
11541/* Read dwarf information from a buffer. */
11542
11543static unsigned int
11544read_1_byte (bfd *abfd, gdb_byte *buf)
11545{
11546 return bfd_get_8 (abfd, buf);
11547}
11548
11549static int
11550read_1_signed_byte (bfd *abfd, gdb_byte *buf)
11551{
11552 return bfd_get_signed_8 (abfd, buf);
11553}
11554
11555static unsigned int
11556read_2_bytes (bfd *abfd, gdb_byte *buf)
11557{
11558 return bfd_get_16 (abfd, buf);
11559}
11560
11561static int
11562read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11563{
11564 return bfd_get_signed_16 (abfd, buf);
11565}
11566
11567static unsigned int
11568read_4_bytes (bfd *abfd, gdb_byte *buf)
11569{
11570 return bfd_get_32 (abfd, buf);
11571}
11572
11573static int
11574read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11575{
11576 return bfd_get_signed_32 (abfd, buf);
11577}
11578
11579static ULONGEST
11580read_8_bytes (bfd *abfd, gdb_byte *buf)
11581{
11582 return bfd_get_64 (abfd, buf);
11583}
11584
11585static CORE_ADDR
11586read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
11587 unsigned int *bytes_read)
11588{
11589 struct comp_unit_head *cu_header = &cu->header;
11590 CORE_ADDR retval = 0;
11591
11592 if (cu_header->signed_addr_p)
11593 {
11594 switch (cu_header->addr_size)
11595 {
11596 case 2:
11597 retval = bfd_get_signed_16 (abfd, buf);
11598 break;
11599 case 4:
11600 retval = bfd_get_signed_32 (abfd, buf);
11601 break;
11602 case 8:
11603 retval = bfd_get_signed_64 (abfd, buf);
11604 break;
11605 default:
11606 internal_error (__FILE__, __LINE__,
11607 _("read_address: bad switch, signed [in module %s]"),
11608 bfd_get_filename (abfd));
11609 }
11610 }
11611 else
11612 {
11613 switch (cu_header->addr_size)
11614 {
11615 case 2:
11616 retval = bfd_get_16 (abfd, buf);
11617 break;
11618 case 4:
11619 retval = bfd_get_32 (abfd, buf);
11620 break;
11621 case 8:
11622 retval = bfd_get_64 (abfd, buf);
11623 break;
11624 default:
11625 internal_error (__FILE__, __LINE__,
11626 _("read_address: bad switch, "
11627 "unsigned [in module %s]"),
11628 bfd_get_filename (abfd));
11629 }
11630 }
11631
11632 *bytes_read = cu_header->addr_size;
11633 return retval;
11634}
11635
11636/* Read the initial length from a section. The (draft) DWARF 3
11637 specification allows the initial length to take up either 4 bytes
11638 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11639 bytes describe the length and all offsets will be 8 bytes in length
11640 instead of 4.
11641
11642 An older, non-standard 64-bit format is also handled by this
11643 function. The older format in question stores the initial length
11644 as an 8-byte quantity without an escape value. Lengths greater
11645 than 2^32 aren't very common which means that the initial 4 bytes
11646 is almost always zero. Since a length value of zero doesn't make
11647 sense for the 32-bit format, this initial zero can be considered to
11648 be an escape value which indicates the presence of the older 64-bit
11649 format. As written, the code can't detect (old format) lengths
11650 greater than 4GB. If it becomes necessary to handle lengths
11651 somewhat larger than 4GB, we could allow other small values (such
11652 as the non-sensical values of 1, 2, and 3) to also be used as
11653 escape values indicating the presence of the old format.
11654
11655 The value returned via bytes_read should be used to increment the
11656 relevant pointer after calling read_initial_length().
11657
11658 [ Note: read_initial_length() and read_offset() are based on the
11659 document entitled "DWARF Debugging Information Format", revision
11660 3, draft 8, dated November 19, 2001. This document was obtained
11661 from:
11662
11663 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
11664
11665 This document is only a draft and is subject to change. (So beware.)
11666
11667 Details regarding the older, non-standard 64-bit format were
11668 determined empirically by examining 64-bit ELF files produced by
11669 the SGI toolchain on an IRIX 6.5 machine.
11670
11671 - Kevin, July 16, 2002
11672 ] */
11673
11674static LONGEST
11675read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
11676{
11677 LONGEST length = bfd_get_32 (abfd, buf);
11678
11679 if (length == 0xffffffff)
11680 {
11681 length = bfd_get_64 (abfd, buf + 4);
11682 *bytes_read = 12;
11683 }
11684 else if (length == 0)
11685 {
11686 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
11687 length = bfd_get_64 (abfd, buf);
11688 *bytes_read = 8;
11689 }
11690 else
11691 {
11692 *bytes_read = 4;
11693 }
11694
11695 return length;
11696}
11697
11698/* Cover function for read_initial_length.
11699 Returns the length of the object at BUF, and stores the size of the
11700 initial length in *BYTES_READ and stores the size that offsets will be in
11701 *OFFSET_SIZE.
11702 If the initial length size is not equivalent to that specified in
11703 CU_HEADER then issue a complaint.
11704 This is useful when reading non-comp-unit headers. */
11705
11706static LONGEST
11707read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11708 const struct comp_unit_head *cu_header,
11709 unsigned int *bytes_read,
11710 unsigned int *offset_size)
11711{
11712 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11713
11714 gdb_assert (cu_header->initial_length_size == 4
11715 || cu_header->initial_length_size == 8
11716 || cu_header->initial_length_size == 12);
11717
11718 if (cu_header->initial_length_size != *bytes_read)
11719 complaint (&symfile_complaints,
11720 _("intermixed 32-bit and 64-bit DWARF sections"));
11721
11722 *offset_size = (*bytes_read == 4) ? 4 : 8;
11723 return length;
11724}
11725
11726/* Read an offset from the data stream. The size of the offset is
11727 given by cu_header->offset_size. */
11728
11729static LONGEST
11730read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
11731 unsigned int *bytes_read)
11732{
11733 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
11734
11735 *bytes_read = cu_header->offset_size;
11736 return offset;
11737}
11738
11739/* Read an offset from the data stream. */
11740
11741static LONGEST
11742read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
11743{
11744 LONGEST retval = 0;
11745
11746 switch (offset_size)
11747 {
11748 case 4:
11749 retval = bfd_get_32 (abfd, buf);
11750 break;
11751 case 8:
11752 retval = bfd_get_64 (abfd, buf);
11753 break;
11754 default:
11755 internal_error (__FILE__, __LINE__,
11756 _("read_offset_1: bad switch [in module %s]"),
11757 bfd_get_filename (abfd));
11758 }
11759
11760 return retval;
11761}
11762
11763static gdb_byte *
11764read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
11765{
11766 /* If the size of a host char is 8 bits, we can return a pointer
11767 to the buffer, otherwise we have to copy the data to a buffer
11768 allocated on the temporary obstack. */
11769 gdb_assert (HOST_CHAR_BIT == 8);
11770 return buf;
11771}
11772
11773static char *
11774read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11775{
11776 /* If the size of a host char is 8 bits, we can return a pointer
11777 to the string, otherwise we have to copy the string to a buffer
11778 allocated on the temporary obstack. */
11779 gdb_assert (HOST_CHAR_BIT == 8);
11780 if (*buf == '\0')
11781 {
11782 *bytes_read_ptr = 1;
11783 return NULL;
11784 }
11785 *bytes_read_ptr = strlen ((char *) buf) + 1;
11786 return (char *) buf;
11787}
11788
11789static char *
11790read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
11791{
11792 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
11793 if (dwarf2_per_objfile->str.buffer == NULL)
11794 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
11795 bfd_get_filename (abfd));
11796 if (str_offset >= dwarf2_per_objfile->str.size)
11797 error (_("DW_FORM_strp pointing outside of "
11798 ".debug_str section [in module %s]"),
11799 bfd_get_filename (abfd));
11800 gdb_assert (HOST_CHAR_BIT == 8);
11801 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
11802 return NULL;
11803 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
11804}
11805
11806static char *
11807read_indirect_string (bfd *abfd, gdb_byte *buf,
11808 const struct comp_unit_head *cu_header,
11809 unsigned int *bytes_read_ptr)
11810{
11811 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
11812
11813 return read_indirect_string_at_offset (abfd, str_offset);
11814}
11815
11816static ULONGEST
11817read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11818{
11819 ULONGEST result;
11820 unsigned int num_read;
11821 int i, shift;
11822 unsigned char byte;
11823
11824 result = 0;
11825 shift = 0;
11826 num_read = 0;
11827 i = 0;
11828 while (1)
11829 {
11830 byte = bfd_get_8 (abfd, buf);
11831 buf++;
11832 num_read++;
11833 result |= ((ULONGEST) (byte & 127) << shift);
11834 if ((byte & 128) == 0)
11835 {
11836 break;
11837 }
11838 shift += 7;
11839 }
11840 *bytes_read_ptr = num_read;
11841 return result;
11842}
11843
11844static LONGEST
11845read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11846{
11847 LONGEST result;
11848 int i, shift, num_read;
11849 unsigned char byte;
11850
11851 result = 0;
11852 shift = 0;
11853 num_read = 0;
11854 i = 0;
11855 while (1)
11856 {
11857 byte = bfd_get_8 (abfd, buf);
11858 buf++;
11859 num_read++;
11860 result |= ((LONGEST) (byte & 127) << shift);
11861 shift += 7;
11862 if ((byte & 128) == 0)
11863 {
11864 break;
11865 }
11866 }
11867 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
11868 result |= -(((LONGEST) 1) << shift);
11869 *bytes_read_ptr = num_read;
11870 return result;
11871}
11872
11873/* Given index ADDR_INDEX in .debug_addr, fetch the value.
11874 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
11875 ADDR_SIZE is the size of addresses from the CU header. */
11876
11877static CORE_ADDR
11878read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
11879{
11880 struct objfile *objfile = dwarf2_per_objfile->objfile;
11881 bfd *abfd = objfile->obfd;
11882 const gdb_byte *info_ptr;
11883
11884 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
11885 if (dwarf2_per_objfile->addr.buffer == NULL)
11886 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
11887 objfile->name);
11888 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
11889 error (_("DW_FORM_addr_index pointing outside of "
11890 ".debug_addr section [in module %s]"),
11891 objfile->name);
11892 info_ptr = (dwarf2_per_objfile->addr.buffer
11893 + addr_base + addr_index * addr_size);
11894 if (addr_size == 4)
11895 return bfd_get_32 (abfd, info_ptr);
11896 else
11897 return bfd_get_64 (abfd, info_ptr);
11898}
11899
11900/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
11901
11902static CORE_ADDR
11903read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
11904{
11905 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
11906}
11907
11908/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
11909
11910static CORE_ADDR
11911read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
11912 unsigned int *bytes_read)
11913{
11914 bfd *abfd = cu->objfile->obfd;
11915 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
11916
11917 return read_addr_index (cu, addr_index);
11918}
11919
11920/* Data structure to pass results from dwarf2_read_addr_index_reader
11921 back to dwarf2_read_addr_index. */
11922
11923struct dwarf2_read_addr_index_data
11924{
11925 ULONGEST addr_base;
11926 int addr_size;
11927};
11928
11929/* die_reader_func for dwarf2_read_addr_index. */
11930
11931static void
11932dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
11933 gdb_byte *info_ptr,
11934 struct die_info *comp_unit_die,
11935 int has_children,
11936 void *data)
11937{
11938 struct dwarf2_cu *cu = reader->cu;
11939 struct dwarf2_read_addr_index_data *aidata =
11940 (struct dwarf2_read_addr_index_data *) data;
11941
11942 aidata->addr_base = cu->addr_base;
11943 aidata->addr_size = cu->header.addr_size;
11944}
11945
11946/* Given an index in .debug_addr, fetch the value.
11947 NOTE: This can be called during dwarf expression evaluation,
11948 long after the debug information has been read, and thus per_cu->cu
11949 may no longer exist. */
11950
11951CORE_ADDR
11952dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
11953 unsigned int addr_index)
11954{
11955 struct objfile *objfile = per_cu->objfile;
11956 struct dwarf2_cu *cu = per_cu->cu;
11957 ULONGEST addr_base;
11958 int addr_size;
11959
11960 /* This is intended to be called from outside this file. */
11961 dw2_setup (objfile);
11962
11963 /* We need addr_base and addr_size.
11964 If we don't have PER_CU->cu, we have to get it.
11965 Nasty, but the alternative is storing the needed info in PER_CU,
11966 which at this point doesn't seem justified: it's not clear how frequently
11967 it would get used and it would increase the size of every PER_CU.
11968 Entry points like dwarf2_per_cu_addr_size do a similar thing
11969 so we're not in uncharted territory here.
11970 Alas we need to be a bit more complicated as addr_base is contained
11971 in the DIE.
11972
11973 We don't need to read the entire CU(/TU).
11974 We just need the header and top level die.
11975 IWBN to use the aging mechanism to let us lazily later discard the CU.
11976 See however init_cutu_and_read_dies_simple. */
11977
11978 if (cu != NULL)
11979 {
11980 addr_base = cu->addr_base;
11981 addr_size = cu->header.addr_size;
11982 }
11983 else
11984 {
11985 struct dwarf2_read_addr_index_data aidata;
11986
11987 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
11988 &aidata);
11989 addr_base = aidata.addr_base;
11990 addr_size = aidata.addr_size;
11991 }
11992
11993 return read_addr_index_1 (addr_index, addr_base, addr_size);
11994}
11995
11996/* Given a DW_AT_str_index, fetch the string. */
11997
11998static char *
11999read_str_index (const struct die_reader_specs *reader,
12000 struct dwarf2_cu *cu, ULONGEST str_index)
12001{
12002 struct objfile *objfile = dwarf2_per_objfile->objfile;
12003 const char *dwo_name = objfile->name;
12004 bfd *abfd = objfile->obfd;
12005 struct dwo_sections *sections = &reader->dwo_file->sections;
12006 gdb_byte *info_ptr;
12007 ULONGEST str_offset;
12008
12009 dwarf2_read_section (objfile, &sections->str);
12010 dwarf2_read_section (objfile, &sections->str_offsets);
12011 if (sections->str.buffer == NULL)
12012 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12013 " in CU at offset 0x%lx [in module %s]"),
12014 (long) cu->header.offset.sect_off, dwo_name);
12015 if (sections->str_offsets.buffer == NULL)
12016 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12017 " in CU at offset 0x%lx [in module %s]"),
12018 (long) cu->header.offset.sect_off, dwo_name);
12019 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12020 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12021 " section in CU at offset 0x%lx [in module %s]"),
12022 (long) cu->header.offset.sect_off, dwo_name);
12023 info_ptr = (sections->str_offsets.buffer
12024 + str_index * cu->header.offset_size);
12025 if (cu->header.offset_size == 4)
12026 str_offset = bfd_get_32 (abfd, info_ptr);
12027 else
12028 str_offset = bfd_get_64 (abfd, info_ptr);
12029 if (str_offset >= sections->str.size)
12030 error (_("Offset from DW_FORM_str_index pointing outside of"
12031 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12032 (long) cu->header.offset.sect_off, dwo_name);
12033 return (char *) (sections->str.buffer + str_offset);
12034}
12035
12036/* Return a pointer to just past the end of an LEB128 number in BUF. */
12037
12038static gdb_byte *
12039skip_leb128 (bfd *abfd, gdb_byte *buf)
12040{
12041 int byte;
12042
12043 while (1)
12044 {
12045 byte = bfd_get_8 (abfd, buf);
12046 buf++;
12047 if ((byte & 128) == 0)
12048 return buf;
12049 }
12050}
12051
12052/* Return the length of an LEB128 number in BUF. */
12053
12054static int
12055leb128_size (const gdb_byte *buf)
12056{
12057 const gdb_byte *begin = buf;
12058 gdb_byte byte;
12059
12060 while (1)
12061 {
12062 byte = *buf++;
12063 if ((byte & 128) == 0)
12064 return buf - begin;
12065 }
12066}
12067
12068static void
12069set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12070{
12071 switch (lang)
12072 {
12073 case DW_LANG_C89:
12074 case DW_LANG_C99:
12075 case DW_LANG_C:
12076 cu->language = language_c;
12077 break;
12078 case DW_LANG_C_plus_plus:
12079 cu->language = language_cplus;
12080 break;
12081 case DW_LANG_D:
12082 cu->language = language_d;
12083 break;
12084 case DW_LANG_Fortran77:
12085 case DW_LANG_Fortran90:
12086 case DW_LANG_Fortran95:
12087 cu->language = language_fortran;
12088 break;
12089 case DW_LANG_Go:
12090 cu->language = language_go;
12091 break;
12092 case DW_LANG_Mips_Assembler:
12093 cu->language = language_asm;
12094 break;
12095 case DW_LANG_Java:
12096 cu->language = language_java;
12097 break;
12098 case DW_LANG_Ada83:
12099 case DW_LANG_Ada95:
12100 cu->language = language_ada;
12101 break;
12102 case DW_LANG_Modula2:
12103 cu->language = language_m2;
12104 break;
12105 case DW_LANG_Pascal83:
12106 cu->language = language_pascal;
12107 break;
12108 case DW_LANG_ObjC:
12109 cu->language = language_objc;
12110 break;
12111 case DW_LANG_Cobol74:
12112 case DW_LANG_Cobol85:
12113 default:
12114 cu->language = language_minimal;
12115 break;
12116 }
12117 cu->language_defn = language_def (cu->language);
12118}
12119
12120/* Return the named attribute or NULL if not there. */
12121
12122static struct attribute *
12123dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12124{
12125 for (;;)
12126 {
12127 unsigned int i;
12128 struct attribute *spec = NULL;
12129
12130 for (i = 0; i < die->num_attrs; ++i)
12131 {
12132 if (die->attrs[i].name == name)
12133 return &die->attrs[i];
12134 if (die->attrs[i].name == DW_AT_specification
12135 || die->attrs[i].name == DW_AT_abstract_origin)
12136 spec = &die->attrs[i];
12137 }
12138
12139 if (!spec)
12140 break;
12141
12142 die = follow_die_ref (die, spec, &cu);
12143 }
12144
12145 return NULL;
12146}
12147
12148/* Return the named attribute or NULL if not there,
12149 but do not follow DW_AT_specification, etc.
12150 This is for use in contexts where we're reading .debug_types dies.
12151 Following DW_AT_specification, DW_AT_abstract_origin will take us
12152 back up the chain, and we want to go down. */
12153
12154static struct attribute *
12155dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12156 struct dwarf2_cu *cu)
12157{
12158 unsigned int i;
12159
12160 for (i = 0; i < die->num_attrs; ++i)
12161 if (die->attrs[i].name == name)
12162 return &die->attrs[i];
12163
12164 return NULL;
12165}
12166
12167/* Return non-zero iff the attribute NAME is defined for the given DIE,
12168 and holds a non-zero value. This function should only be used for
12169 DW_FORM_flag or DW_FORM_flag_present attributes. */
12170
12171static int
12172dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12173{
12174 struct attribute *attr = dwarf2_attr (die, name, cu);
12175
12176 return (attr && DW_UNSND (attr));
12177}
12178
12179static int
12180die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12181{
12182 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12183 which value is non-zero. However, we have to be careful with
12184 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12185 (via dwarf2_flag_true_p) follows this attribute. So we may
12186 end up accidently finding a declaration attribute that belongs
12187 to a different DIE referenced by the specification attribute,
12188 even though the given DIE does not have a declaration attribute. */
12189 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12190 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12191}
12192
12193/* Return the die giving the specification for DIE, if there is
12194 one. *SPEC_CU is the CU containing DIE on input, and the CU
12195 containing the return value on output. If there is no
12196 specification, but there is an abstract origin, that is
12197 returned. */
12198
12199static struct die_info *
12200die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12201{
12202 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12203 *spec_cu);
12204
12205 if (spec_attr == NULL)
12206 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12207
12208 if (spec_attr == NULL)
12209 return NULL;
12210 else
12211 return follow_die_ref (die, spec_attr, spec_cu);
12212}
12213
12214/* Free the line_header structure *LH, and any arrays and strings it
12215 refers to.
12216 NOTE: This is also used as a "cleanup" function. */
12217
12218static void
12219free_line_header (struct line_header *lh)
12220{
12221 if (lh->standard_opcode_lengths)
12222 xfree (lh->standard_opcode_lengths);
12223
12224 /* Remember that all the lh->file_names[i].name pointers are
12225 pointers into debug_line_buffer, and don't need to be freed. */
12226 if (lh->file_names)
12227 xfree (lh->file_names);
12228
12229 /* Similarly for the include directory names. */
12230 if (lh->include_dirs)
12231 xfree (lh->include_dirs);
12232
12233 xfree (lh);
12234}
12235
12236/* Add an entry to LH's include directory table. */
12237
12238static void
12239add_include_dir (struct line_header *lh, char *include_dir)
12240{
12241 /* Grow the array if necessary. */
12242 if (lh->include_dirs_size == 0)
12243 {
12244 lh->include_dirs_size = 1; /* for testing */
12245 lh->include_dirs = xmalloc (lh->include_dirs_size
12246 * sizeof (*lh->include_dirs));
12247 }
12248 else if (lh->num_include_dirs >= lh->include_dirs_size)
12249 {
12250 lh->include_dirs_size *= 2;
12251 lh->include_dirs = xrealloc (lh->include_dirs,
12252 (lh->include_dirs_size
12253 * sizeof (*lh->include_dirs)));
12254 }
12255
12256 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12257}
12258
12259/* Add an entry to LH's file name table. */
12260
12261static void
12262add_file_name (struct line_header *lh,
12263 char *name,
12264 unsigned int dir_index,
12265 unsigned int mod_time,
12266 unsigned int length)
12267{
12268 struct file_entry *fe;
12269
12270 /* Grow the array if necessary. */
12271 if (lh->file_names_size == 0)
12272 {
12273 lh->file_names_size = 1; /* for testing */
12274 lh->file_names = xmalloc (lh->file_names_size
12275 * sizeof (*lh->file_names));
12276 }
12277 else if (lh->num_file_names >= lh->file_names_size)
12278 {
12279 lh->file_names_size *= 2;
12280 lh->file_names = xrealloc (lh->file_names,
12281 (lh->file_names_size
12282 * sizeof (*lh->file_names)));
12283 }
12284
12285 fe = &lh->file_names[lh->num_file_names++];
12286 fe->name = name;
12287 fe->dir_index = dir_index;
12288 fe->mod_time = mod_time;
12289 fe->length = length;
12290 fe->included_p = 0;
12291 fe->symtab = NULL;
12292}
12293
12294/* Read the statement program header starting at OFFSET in
12295 .debug_line, or .debug_line.dwo. Return a pointer
12296 to a struct line_header, allocated using xmalloc.
12297
12298 NOTE: the strings in the include directory and file name tables of
12299 the returned object point into the dwarf line section buffer,
12300 and must not be freed. */
12301
12302static struct line_header *
12303dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12304{
12305 struct cleanup *back_to;
12306 struct line_header *lh;
12307 gdb_byte *line_ptr;
12308 unsigned int bytes_read, offset_size;
12309 int i;
12310 char *cur_dir, *cur_file;
12311 struct dwarf2_section_info *section;
12312 bfd *abfd;
12313
12314 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12315 DWO file. */
12316 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12317 section = &cu->dwo_unit->dwo_file->sections.line;
12318 else
12319 section = &dwarf2_per_objfile->line;
12320 abfd = section->asection->owner;
12321
12322 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12323 if (section->buffer == NULL)
12324 {
12325 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12326 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12327 else
12328 complaint (&symfile_complaints, _("missing .debug_line section"));
12329 return 0;
12330 }
12331
12332 /* Make sure that at least there's room for the total_length field.
12333 That could be 12 bytes long, but we're just going to fudge that. */
12334 if (offset + 4 >= section->size)
12335 {
12336 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12337 return 0;
12338 }
12339
12340 lh = xmalloc (sizeof (*lh));
12341 memset (lh, 0, sizeof (*lh));
12342 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12343 (void *) lh);
12344
12345 line_ptr = section->buffer + offset;
12346
12347 /* Read in the header. */
12348 lh->total_length =
12349 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12350 &bytes_read, &offset_size);
12351 line_ptr += bytes_read;
12352 if (line_ptr + lh->total_length > (section->buffer + section->size))
12353 {
12354 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12355 return 0;
12356 }
12357 lh->statement_program_end = line_ptr + lh->total_length;
12358 lh->version = read_2_bytes (abfd, line_ptr);
12359 line_ptr += 2;
12360 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12361 line_ptr += offset_size;
12362 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12363 line_ptr += 1;
12364 if (lh->version >= 4)
12365 {
12366 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12367 line_ptr += 1;
12368 }
12369 else
12370 lh->maximum_ops_per_instruction = 1;
12371
12372 if (lh->maximum_ops_per_instruction == 0)
12373 {
12374 lh->maximum_ops_per_instruction = 1;
12375 complaint (&symfile_complaints,
12376 _("invalid maximum_ops_per_instruction "
12377 "in `.debug_line' section"));
12378 }
12379
12380 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12381 line_ptr += 1;
12382 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12383 line_ptr += 1;
12384 lh->line_range = read_1_byte (abfd, line_ptr);
12385 line_ptr += 1;
12386 lh->opcode_base = read_1_byte (abfd, line_ptr);
12387 line_ptr += 1;
12388 lh->standard_opcode_lengths
12389 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12390
12391 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12392 for (i = 1; i < lh->opcode_base; ++i)
12393 {
12394 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12395 line_ptr += 1;
12396 }
12397
12398 /* Read directory table. */
12399 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12400 {
12401 line_ptr += bytes_read;
12402 add_include_dir (lh, cur_dir);
12403 }
12404 line_ptr += bytes_read;
12405
12406 /* Read file name table. */
12407 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12408 {
12409 unsigned int dir_index, mod_time, length;
12410
12411 line_ptr += bytes_read;
12412 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12413 line_ptr += bytes_read;
12414 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12415 line_ptr += bytes_read;
12416 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12417 line_ptr += bytes_read;
12418
12419 add_file_name (lh, cur_file, dir_index, mod_time, length);
12420 }
12421 line_ptr += bytes_read;
12422 lh->statement_program_start = line_ptr;
12423
12424 if (line_ptr > (section->buffer + section->size))
12425 complaint (&symfile_complaints,
12426 _("line number info header doesn't "
12427 "fit in `.debug_line' section"));
12428
12429 discard_cleanups (back_to);
12430 return lh;
12431}
12432
12433/* Subroutine of dwarf_decode_lines to simplify it.
12434 Return the file name of the psymtab for included file FILE_INDEX
12435 in line header LH of PST.
12436 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12437 If space for the result is malloc'd, it will be freed by a cleanup.
12438 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12439
12440static char *
12441psymtab_include_file_name (const struct line_header *lh, int file_index,
12442 const struct partial_symtab *pst,
12443 const char *comp_dir)
12444{
12445 const struct file_entry fe = lh->file_names [file_index];
12446 char *include_name = fe.name;
12447 char *include_name_to_compare = include_name;
12448 char *dir_name = NULL;
12449 const char *pst_filename;
12450 char *copied_name = NULL;
12451 int file_is_pst;
12452
12453 if (fe.dir_index)
12454 dir_name = lh->include_dirs[fe.dir_index - 1];
12455
12456 if (!IS_ABSOLUTE_PATH (include_name)
12457 && (dir_name != NULL || comp_dir != NULL))
12458 {
12459 /* Avoid creating a duplicate psymtab for PST.
12460 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12461 Before we do the comparison, however, we need to account
12462 for DIR_NAME and COMP_DIR.
12463 First prepend dir_name (if non-NULL). If we still don't
12464 have an absolute path prepend comp_dir (if non-NULL).
12465 However, the directory we record in the include-file's
12466 psymtab does not contain COMP_DIR (to match the
12467 corresponding symtab(s)).
12468
12469 Example:
12470
12471 bash$ cd /tmp
12472 bash$ gcc -g ./hello.c
12473 include_name = "hello.c"
12474 dir_name = "."
12475 DW_AT_comp_dir = comp_dir = "/tmp"
12476 DW_AT_name = "./hello.c" */
12477
12478 if (dir_name != NULL)
12479 {
12480 include_name = concat (dir_name, SLASH_STRING,
12481 include_name, (char *)NULL);
12482 include_name_to_compare = include_name;
12483 make_cleanup (xfree, include_name);
12484 }
12485 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12486 {
12487 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12488 include_name, (char *)NULL);
12489 }
12490 }
12491
12492 pst_filename = pst->filename;
12493 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12494 {
12495 copied_name = concat (pst->dirname, SLASH_STRING,
12496 pst_filename, (char *)NULL);
12497 pst_filename = copied_name;
12498 }
12499
12500 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12501
12502 if (include_name_to_compare != include_name)
12503 xfree (include_name_to_compare);
12504 if (copied_name != NULL)
12505 xfree (copied_name);
12506
12507 if (file_is_pst)
12508 return NULL;
12509 return include_name;
12510}
12511
12512/* Ignore this record_line request. */
12513
12514static void
12515noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12516{
12517 return;
12518}
12519
12520/* Subroutine of dwarf_decode_lines to simplify it.
12521 Process the line number information in LH. */
12522
12523static void
12524dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12525 struct dwarf2_cu *cu, struct partial_symtab *pst)
12526{
12527 gdb_byte *line_ptr, *extended_end;
12528 gdb_byte *line_end;
12529 unsigned int bytes_read, extended_len;
12530 unsigned char op_code, extended_op, adj_opcode;
12531 CORE_ADDR baseaddr;
12532 struct objfile *objfile = cu->objfile;
12533 bfd *abfd = objfile->obfd;
12534 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12535 const int decode_for_pst_p = (pst != NULL);
12536 struct subfile *last_subfile = NULL;
12537 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12538 = record_line;
12539
12540 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12541
12542 line_ptr = lh->statement_program_start;
12543 line_end = lh->statement_program_end;
12544
12545 /* Read the statement sequences until there's nothing left. */
12546 while (line_ptr < line_end)
12547 {
12548 /* state machine registers */
12549 CORE_ADDR address = 0;
12550 unsigned int file = 1;
12551 unsigned int line = 1;
12552 unsigned int column = 0;
12553 int is_stmt = lh->default_is_stmt;
12554 int basic_block = 0;
12555 int end_sequence = 0;
12556 CORE_ADDR addr;
12557 unsigned char op_index = 0;
12558
12559 if (!decode_for_pst_p && lh->num_file_names >= file)
12560 {
12561 /* Start a subfile for the current file of the state machine. */
12562 /* lh->include_dirs and lh->file_names are 0-based, but the
12563 directory and file name numbers in the statement program
12564 are 1-based. */
12565 struct file_entry *fe = &lh->file_names[file - 1];
12566 char *dir = NULL;
12567
12568 if (fe->dir_index)
12569 dir = lh->include_dirs[fe->dir_index - 1];
12570
12571 dwarf2_start_subfile (fe->name, dir, comp_dir);
12572 }
12573
12574 /* Decode the table. */
12575 while (!end_sequence)
12576 {
12577 op_code = read_1_byte (abfd, line_ptr);
12578 line_ptr += 1;
12579 if (line_ptr > line_end)
12580 {
12581 dwarf2_debug_line_missing_end_sequence_complaint ();
12582 break;
12583 }
12584
12585 if (op_code >= lh->opcode_base)
12586 {
12587 /* Special operand. */
12588 adj_opcode = op_code - lh->opcode_base;
12589 address += (((op_index + (adj_opcode / lh->line_range))
12590 / lh->maximum_ops_per_instruction)
12591 * lh->minimum_instruction_length);
12592 op_index = ((op_index + (adj_opcode / lh->line_range))
12593 % lh->maximum_ops_per_instruction);
12594 line += lh->line_base + (adj_opcode % lh->line_range);
12595 if (lh->num_file_names < file || file == 0)
12596 dwarf2_debug_line_missing_file_complaint ();
12597 /* For now we ignore lines not starting on an
12598 instruction boundary. */
12599 else if (op_index == 0)
12600 {
12601 lh->file_names[file - 1].included_p = 1;
12602 if (!decode_for_pst_p && is_stmt)
12603 {
12604 if (last_subfile != current_subfile)
12605 {
12606 addr = gdbarch_addr_bits_remove (gdbarch, address);
12607 if (last_subfile)
12608 (*p_record_line) (last_subfile, 0, addr);
12609 last_subfile = current_subfile;
12610 }
12611 /* Append row to matrix using current values. */
12612 addr = gdbarch_addr_bits_remove (gdbarch, address);
12613 (*p_record_line) (current_subfile, line, addr);
12614 }
12615 }
12616 basic_block = 0;
12617 }
12618 else switch (op_code)
12619 {
12620 case DW_LNS_extended_op:
12621 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12622 &bytes_read);
12623 line_ptr += bytes_read;
12624 extended_end = line_ptr + extended_len;
12625 extended_op = read_1_byte (abfd, line_ptr);
12626 line_ptr += 1;
12627 switch (extended_op)
12628 {
12629 case DW_LNE_end_sequence:
12630 p_record_line = record_line;
12631 end_sequence = 1;
12632 break;
12633 case DW_LNE_set_address:
12634 address = read_address (abfd, line_ptr, cu, &bytes_read);
12635
12636 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12637 {
12638 /* This line table is for a function which has been
12639 GCd by the linker. Ignore it. PR gdb/12528 */
12640
12641 long line_offset
12642 = line_ptr - dwarf2_per_objfile->line.buffer;
12643
12644 complaint (&symfile_complaints,
12645 _(".debug_line address at offset 0x%lx is 0 "
12646 "[in module %s]"),
12647 line_offset, objfile->name);
12648 p_record_line = noop_record_line;
12649 }
12650
12651 op_index = 0;
12652 line_ptr += bytes_read;
12653 address += baseaddr;
12654 break;
12655 case DW_LNE_define_file:
12656 {
12657 char *cur_file;
12658 unsigned int dir_index, mod_time, length;
12659
12660 cur_file = read_direct_string (abfd, line_ptr,
12661 &bytes_read);
12662 line_ptr += bytes_read;
12663 dir_index =
12664 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12665 line_ptr += bytes_read;
12666 mod_time =
12667 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12668 line_ptr += bytes_read;
12669 length =
12670 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12671 line_ptr += bytes_read;
12672 add_file_name (lh, cur_file, dir_index, mod_time, length);
12673 }
12674 break;
12675 case DW_LNE_set_discriminator:
12676 /* The discriminator is not interesting to the debugger;
12677 just ignore it. */
12678 line_ptr = extended_end;
12679 break;
12680 default:
12681 complaint (&symfile_complaints,
12682 _("mangled .debug_line section"));
12683 return;
12684 }
12685 /* Make sure that we parsed the extended op correctly. If e.g.
12686 we expected a different address size than the producer used,
12687 we may have read the wrong number of bytes. */
12688 if (line_ptr != extended_end)
12689 {
12690 complaint (&symfile_complaints,
12691 _("mangled .debug_line section"));
12692 return;
12693 }
12694 break;
12695 case DW_LNS_copy:
12696 if (lh->num_file_names < file || file == 0)
12697 dwarf2_debug_line_missing_file_complaint ();
12698 else
12699 {
12700 lh->file_names[file - 1].included_p = 1;
12701 if (!decode_for_pst_p && is_stmt)
12702 {
12703 if (last_subfile != current_subfile)
12704 {
12705 addr = gdbarch_addr_bits_remove (gdbarch, address);
12706 if (last_subfile)
12707 (*p_record_line) (last_subfile, 0, addr);
12708 last_subfile = current_subfile;
12709 }
12710 addr = gdbarch_addr_bits_remove (gdbarch, address);
12711 (*p_record_line) (current_subfile, line, addr);
12712 }
12713 }
12714 basic_block = 0;
12715 break;
12716 case DW_LNS_advance_pc:
12717 {
12718 CORE_ADDR adjust
12719 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12720
12721 address += (((op_index + adjust)
12722 / lh->maximum_ops_per_instruction)
12723 * lh->minimum_instruction_length);
12724 op_index = ((op_index + adjust)
12725 % lh->maximum_ops_per_instruction);
12726 line_ptr += bytes_read;
12727 }
12728 break;
12729 case DW_LNS_advance_line:
12730 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12731 line_ptr += bytes_read;
12732 break;
12733 case DW_LNS_set_file:
12734 {
12735 /* The arrays lh->include_dirs and lh->file_names are
12736 0-based, but the directory and file name numbers in
12737 the statement program are 1-based. */
12738 struct file_entry *fe;
12739 char *dir = NULL;
12740
12741 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12742 line_ptr += bytes_read;
12743 if (lh->num_file_names < file || file == 0)
12744 dwarf2_debug_line_missing_file_complaint ();
12745 else
12746 {
12747 fe = &lh->file_names[file - 1];
12748 if (fe->dir_index)
12749 dir = lh->include_dirs[fe->dir_index - 1];
12750 if (!decode_for_pst_p)
12751 {
12752 last_subfile = current_subfile;
12753 dwarf2_start_subfile (fe->name, dir, comp_dir);
12754 }
12755 }
12756 }
12757 break;
12758 case DW_LNS_set_column:
12759 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12760 line_ptr += bytes_read;
12761 break;
12762 case DW_LNS_negate_stmt:
12763 is_stmt = (!is_stmt);
12764 break;
12765 case DW_LNS_set_basic_block:
12766 basic_block = 1;
12767 break;
12768 /* Add to the address register of the state machine the
12769 address increment value corresponding to special opcode
12770 255. I.e., this value is scaled by the minimum
12771 instruction length since special opcode 255 would have
12772 scaled the increment. */
12773 case DW_LNS_const_add_pc:
12774 {
12775 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
12776
12777 address += (((op_index + adjust)
12778 / lh->maximum_ops_per_instruction)
12779 * lh->minimum_instruction_length);
12780 op_index = ((op_index + adjust)
12781 % lh->maximum_ops_per_instruction);
12782 }
12783 break;
12784 case DW_LNS_fixed_advance_pc:
12785 address += read_2_bytes (abfd, line_ptr);
12786 op_index = 0;
12787 line_ptr += 2;
12788 break;
12789 default:
12790 {
12791 /* Unknown standard opcode, ignore it. */
12792 int i;
12793
12794 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
12795 {
12796 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12797 line_ptr += bytes_read;
12798 }
12799 }
12800 }
12801 }
12802 if (lh->num_file_names < file || file == 0)
12803 dwarf2_debug_line_missing_file_complaint ();
12804 else
12805 {
12806 lh->file_names[file - 1].included_p = 1;
12807 if (!decode_for_pst_p)
12808 {
12809 addr = gdbarch_addr_bits_remove (gdbarch, address);
12810 (*p_record_line) (current_subfile, 0, addr);
12811 }
12812 }
12813 }
12814}
12815
12816/* Decode the Line Number Program (LNP) for the given line_header
12817 structure and CU. The actual information extracted and the type
12818 of structures created from the LNP depends on the value of PST.
12819
12820 1. If PST is NULL, then this procedure uses the data from the program
12821 to create all necessary symbol tables, and their linetables.
12822
12823 2. If PST is not NULL, this procedure reads the program to determine
12824 the list of files included by the unit represented by PST, and
12825 builds all the associated partial symbol tables.
12826
12827 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12828 It is used for relative paths in the line table.
12829 NOTE: When processing partial symtabs (pst != NULL),
12830 comp_dir == pst->dirname.
12831
12832 NOTE: It is important that psymtabs have the same file name (via strcmp)
12833 as the corresponding symtab. Since COMP_DIR is not used in the name of the
12834 symtab we don't use it in the name of the psymtabs we create.
12835 E.g. expand_line_sal requires this when finding psymtabs to expand.
12836 A good testcase for this is mb-inline.exp. */
12837
12838static void
12839dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
12840 struct dwarf2_cu *cu, struct partial_symtab *pst,
12841 int want_line_info)
12842{
12843 struct objfile *objfile = cu->objfile;
12844 const int decode_for_pst_p = (pst != NULL);
12845 struct subfile *first_subfile = current_subfile;
12846
12847 if (want_line_info)
12848 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
12849
12850 if (decode_for_pst_p)
12851 {
12852 int file_index;
12853
12854 /* Now that we're done scanning the Line Header Program, we can
12855 create the psymtab of each included file. */
12856 for (file_index = 0; file_index < lh->num_file_names; file_index++)
12857 if (lh->file_names[file_index].included_p == 1)
12858 {
12859 char *include_name =
12860 psymtab_include_file_name (lh, file_index, pst, comp_dir);
12861 if (include_name != NULL)
12862 dwarf2_create_include_psymtab (include_name, pst, objfile);
12863 }
12864 }
12865 else
12866 {
12867 /* Make sure a symtab is created for every file, even files
12868 which contain only variables (i.e. no code with associated
12869 line numbers). */
12870 int i;
12871
12872 for (i = 0; i < lh->num_file_names; i++)
12873 {
12874 char *dir = NULL;
12875 struct file_entry *fe;
12876
12877 fe = &lh->file_names[i];
12878 if (fe->dir_index)
12879 dir = lh->include_dirs[fe->dir_index - 1];
12880 dwarf2_start_subfile (fe->name, dir, comp_dir);
12881
12882 /* Skip the main file; we don't need it, and it must be
12883 allocated last, so that it will show up before the
12884 non-primary symtabs in the objfile's symtab list. */
12885 if (current_subfile == first_subfile)
12886 continue;
12887
12888 if (current_subfile->symtab == NULL)
12889 current_subfile->symtab = allocate_symtab (current_subfile->name,
12890 objfile);
12891 fe->symtab = current_subfile->symtab;
12892 }
12893 }
12894}
12895
12896/* Start a subfile for DWARF. FILENAME is the name of the file and
12897 DIRNAME the name of the source directory which contains FILENAME
12898 or NULL if not known. COMP_DIR is the compilation directory for the
12899 linetable's compilation unit or NULL if not known.
12900 This routine tries to keep line numbers from identical absolute and
12901 relative file names in a common subfile.
12902
12903 Using the `list' example from the GDB testsuite, which resides in
12904 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
12905 of /srcdir/list0.c yields the following debugging information for list0.c:
12906
12907 DW_AT_name: /srcdir/list0.c
12908 DW_AT_comp_dir: /compdir
12909 files.files[0].name: list0.h
12910 files.files[0].dir: /srcdir
12911 files.files[1].name: list0.c
12912 files.files[1].dir: /srcdir
12913
12914 The line number information for list0.c has to end up in a single
12915 subfile, so that `break /srcdir/list0.c:1' works as expected.
12916 start_subfile will ensure that this happens provided that we pass the
12917 concatenation of files.files[1].dir and files.files[1].name as the
12918 subfile's name. */
12919
12920static void
12921dwarf2_start_subfile (char *filename, const char *dirname,
12922 const char *comp_dir)
12923{
12924 char *fullname;
12925
12926 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
12927 `start_symtab' will always pass the contents of DW_AT_comp_dir as
12928 second argument to start_subfile. To be consistent, we do the
12929 same here. In order not to lose the line information directory,
12930 we concatenate it to the filename when it makes sense.
12931 Note that the Dwarf3 standard says (speaking of filenames in line
12932 information): ``The directory index is ignored for file names
12933 that represent full path names''. Thus ignoring dirname in the
12934 `else' branch below isn't an issue. */
12935
12936 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
12937 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
12938 else
12939 fullname = filename;
12940
12941 start_subfile (fullname, comp_dir);
12942
12943 if (fullname != filename)
12944 xfree (fullname);
12945}
12946
12947static void
12948var_decode_location (struct attribute *attr, struct symbol *sym,
12949 struct dwarf2_cu *cu)
12950{
12951 struct objfile *objfile = cu->objfile;
12952 struct comp_unit_head *cu_header = &cu->header;
12953
12954 /* NOTE drow/2003-01-30: There used to be a comment and some special
12955 code here to turn a symbol with DW_AT_external and a
12956 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
12957 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
12958 with some versions of binutils) where shared libraries could have
12959 relocations against symbols in their debug information - the
12960 minimal symbol would have the right address, but the debug info
12961 would not. It's no longer necessary, because we will explicitly
12962 apply relocations when we read in the debug information now. */
12963
12964 /* A DW_AT_location attribute with no contents indicates that a
12965 variable has been optimized away. */
12966 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
12967 {
12968 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
12969 return;
12970 }
12971
12972 /* Handle one degenerate form of location expression specially, to
12973 preserve GDB's previous behavior when section offsets are
12974 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
12975 then mark this symbol as LOC_STATIC. */
12976
12977 if (attr_form_is_block (attr)
12978 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
12979 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
12980 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
12981 && (DW_BLOCK (attr)->size
12982 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
12983 {
12984 unsigned int dummy;
12985
12986 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
12987 SYMBOL_VALUE_ADDRESS (sym) =
12988 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
12989 else
12990 SYMBOL_VALUE_ADDRESS (sym) =
12991 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
12992 SYMBOL_CLASS (sym) = LOC_STATIC;
12993 fixup_symbol_section (sym, objfile);
12994 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
12995 SYMBOL_SECTION (sym));
12996 return;
12997 }
12998
12999 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13000 expression evaluator, and use LOC_COMPUTED only when necessary
13001 (i.e. when the value of a register or memory location is
13002 referenced, or a thread-local block, etc.). Then again, it might
13003 not be worthwhile. I'm assuming that it isn't unless performance
13004 or memory numbers show me otherwise. */
13005
13006 dwarf2_symbol_mark_computed (attr, sym, cu);
13007 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13008
13009 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13010 cu->has_loclist = 1;
13011}
13012
13013/* Given a pointer to a DWARF information entry, figure out if we need
13014 to make a symbol table entry for it, and if so, create a new entry
13015 and return a pointer to it.
13016 If TYPE is NULL, determine symbol type from the die, otherwise
13017 used the passed type.
13018 If SPACE is not NULL, use it to hold the new symbol. If it is
13019 NULL, allocate a new symbol on the objfile's obstack. */
13020
13021static struct symbol *
13022new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13023 struct symbol *space)
13024{
13025 struct objfile *objfile = cu->objfile;
13026 struct symbol *sym = NULL;
13027 char *name;
13028 struct attribute *attr = NULL;
13029 struct attribute *attr2 = NULL;
13030 CORE_ADDR baseaddr;
13031 struct pending **list_to_add = NULL;
13032
13033 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13034
13035 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13036
13037 name = dwarf2_name (die, cu);
13038 if (name)
13039 {
13040 const char *linkagename;
13041 int suppress_add = 0;
13042
13043 if (space)
13044 sym = space;
13045 else
13046 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13047 OBJSTAT (objfile, n_syms++);
13048
13049 /* Cache this symbol's name and the name's demangled form (if any). */
13050 SYMBOL_SET_LANGUAGE (sym, cu->language);
13051 linkagename = dwarf2_physname (name, die, cu);
13052 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13053
13054 /* Fortran does not have mangling standard and the mangling does differ
13055 between gfortran, iFort etc. */
13056 if (cu->language == language_fortran
13057 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13058 symbol_set_demangled_name (&(sym->ginfo),
13059 (char *) dwarf2_full_name (name, die, cu),
13060 NULL);
13061
13062 /* Default assumptions.
13063 Use the passed type or decode it from the die. */
13064 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13065 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13066 if (type != NULL)
13067 SYMBOL_TYPE (sym) = type;
13068 else
13069 SYMBOL_TYPE (sym) = die_type (die, cu);
13070 attr = dwarf2_attr (die,
13071 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13072 cu);
13073 if (attr)
13074 {
13075 SYMBOL_LINE (sym) = DW_UNSND (attr);
13076 }
13077
13078 attr = dwarf2_attr (die,
13079 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13080 cu);
13081 if (attr)
13082 {
13083 int file_index = DW_UNSND (attr);
13084
13085 if (cu->line_header == NULL
13086 || file_index > cu->line_header->num_file_names)
13087 complaint (&symfile_complaints,
13088 _("file index out of range"));
13089 else if (file_index > 0)
13090 {
13091 struct file_entry *fe;
13092
13093 fe = &cu->line_header->file_names[file_index - 1];
13094 SYMBOL_SYMTAB (sym) = fe->symtab;
13095 }
13096 }
13097
13098 switch (die->tag)
13099 {
13100 case DW_TAG_label:
13101 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13102 if (attr)
13103 {
13104 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13105 }
13106 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13107 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13108 SYMBOL_CLASS (sym) = LOC_LABEL;
13109 add_symbol_to_list (sym, cu->list_in_scope);
13110 break;
13111 case DW_TAG_subprogram:
13112 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13113 finish_block. */
13114 SYMBOL_CLASS (sym) = LOC_BLOCK;
13115 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13116 if ((attr2 && (DW_UNSND (attr2) != 0))
13117 || cu->language == language_ada)
13118 {
13119 /* Subprograms marked external are stored as a global symbol.
13120 Ada subprograms, whether marked external or not, are always
13121 stored as a global symbol, because we want to be able to
13122 access them globally. For instance, we want to be able
13123 to break on a nested subprogram without having to
13124 specify the context. */
13125 list_to_add = &global_symbols;
13126 }
13127 else
13128 {
13129 list_to_add = cu->list_in_scope;
13130 }
13131 break;
13132 case DW_TAG_inlined_subroutine:
13133 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13134 finish_block. */
13135 SYMBOL_CLASS (sym) = LOC_BLOCK;
13136 SYMBOL_INLINED (sym) = 1;
13137 list_to_add = cu->list_in_scope;
13138 break;
13139 case DW_TAG_template_value_param:
13140 suppress_add = 1;
13141 /* Fall through. */
13142 case DW_TAG_constant:
13143 case DW_TAG_variable:
13144 case DW_TAG_member:
13145 /* Compilation with minimal debug info may result in
13146 variables with missing type entries. Change the
13147 misleading `void' type to something sensible. */
13148 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13149 SYMBOL_TYPE (sym)
13150 = objfile_type (objfile)->nodebug_data_symbol;
13151
13152 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13153 /* In the case of DW_TAG_member, we should only be called for
13154 static const members. */
13155 if (die->tag == DW_TAG_member)
13156 {
13157 /* dwarf2_add_field uses die_is_declaration,
13158 so we do the same. */
13159 gdb_assert (die_is_declaration (die, cu));
13160 gdb_assert (attr);
13161 }
13162 if (attr)
13163 {
13164 dwarf2_const_value (attr, sym, cu);
13165 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13166 if (!suppress_add)
13167 {
13168 if (attr2 && (DW_UNSND (attr2) != 0))
13169 list_to_add = &global_symbols;
13170 else
13171 list_to_add = cu->list_in_scope;
13172 }
13173 break;
13174 }
13175 attr = dwarf2_attr (die, DW_AT_location, cu);
13176 if (attr)
13177 {
13178 var_decode_location (attr, sym, cu);
13179 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13180 if (SYMBOL_CLASS (sym) == LOC_STATIC
13181 && SYMBOL_VALUE_ADDRESS (sym) == 0
13182 && !dwarf2_per_objfile->has_section_at_zero)
13183 {
13184 /* When a static variable is eliminated by the linker,
13185 the corresponding debug information is not stripped
13186 out, but the variable address is set to null;
13187 do not add such variables into symbol table. */
13188 }
13189 else if (attr2 && (DW_UNSND (attr2) != 0))
13190 {
13191 /* Workaround gfortran PR debug/40040 - it uses
13192 DW_AT_location for variables in -fPIC libraries which may
13193 get overriden by other libraries/executable and get
13194 a different address. Resolve it by the minimal symbol
13195 which may come from inferior's executable using copy
13196 relocation. Make this workaround only for gfortran as for
13197 other compilers GDB cannot guess the minimal symbol
13198 Fortran mangling kind. */
13199 if (cu->language == language_fortran && die->parent
13200 && die->parent->tag == DW_TAG_module
13201 && cu->producer
13202 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13203 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13204
13205 /* A variable with DW_AT_external is never static,
13206 but it may be block-scoped. */
13207 list_to_add = (cu->list_in_scope == &file_symbols
13208 ? &global_symbols : cu->list_in_scope);
13209 }
13210 else
13211 list_to_add = cu->list_in_scope;
13212 }
13213 else
13214 {
13215 /* We do not know the address of this symbol.
13216 If it is an external symbol and we have type information
13217 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13218 The address of the variable will then be determined from
13219 the minimal symbol table whenever the variable is
13220 referenced. */
13221 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13222 if (attr2 && (DW_UNSND (attr2) != 0)
13223 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13224 {
13225 /* A variable with DW_AT_external is never static, but it
13226 may be block-scoped. */
13227 list_to_add = (cu->list_in_scope == &file_symbols
13228 ? &global_symbols : cu->list_in_scope);
13229
13230 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13231 }
13232 else if (!die_is_declaration (die, cu))
13233 {
13234 /* Use the default LOC_OPTIMIZED_OUT class. */
13235 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13236 if (!suppress_add)
13237 list_to_add = cu->list_in_scope;
13238 }
13239 }
13240 break;
13241 case DW_TAG_formal_parameter:
13242 /* If we are inside a function, mark this as an argument. If
13243 not, we might be looking at an argument to an inlined function
13244 when we do not have enough information to show inlined frames;
13245 pretend it's a local variable in that case so that the user can
13246 still see it. */
13247 if (context_stack_depth > 0
13248 && context_stack[context_stack_depth - 1].name != NULL)
13249 SYMBOL_IS_ARGUMENT (sym) = 1;
13250 attr = dwarf2_attr (die, DW_AT_location, cu);
13251 if (attr)
13252 {
13253 var_decode_location (attr, sym, cu);
13254 }
13255 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13256 if (attr)
13257 {
13258 dwarf2_const_value (attr, sym, cu);
13259 }
13260
13261 list_to_add = cu->list_in_scope;
13262 break;
13263 case DW_TAG_unspecified_parameters:
13264 /* From varargs functions; gdb doesn't seem to have any
13265 interest in this information, so just ignore it for now.
13266 (FIXME?) */
13267 break;
13268 case DW_TAG_template_type_param:
13269 suppress_add = 1;
13270 /* Fall through. */
13271 case DW_TAG_class_type:
13272 case DW_TAG_interface_type:
13273 case DW_TAG_structure_type:
13274 case DW_TAG_union_type:
13275 case DW_TAG_set_type:
13276 case DW_TAG_enumeration_type:
13277 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13278 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13279
13280 {
13281 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13282 really ever be static objects: otherwise, if you try
13283 to, say, break of a class's method and you're in a file
13284 which doesn't mention that class, it won't work unless
13285 the check for all static symbols in lookup_symbol_aux
13286 saves you. See the OtherFileClass tests in
13287 gdb.c++/namespace.exp. */
13288
13289 if (!suppress_add)
13290 {
13291 list_to_add = (cu->list_in_scope == &file_symbols
13292 && (cu->language == language_cplus
13293 || cu->language == language_java)
13294 ? &global_symbols : cu->list_in_scope);
13295
13296 /* The semantics of C++ state that "struct foo {
13297 ... }" also defines a typedef for "foo". A Java
13298 class declaration also defines a typedef for the
13299 class. */
13300 if (cu->language == language_cplus
13301 || cu->language == language_java
13302 || cu->language == language_ada)
13303 {
13304 /* The symbol's name is already allocated along
13305 with this objfile, so we don't need to
13306 duplicate it for the type. */
13307 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13308 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13309 }
13310 }
13311 }
13312 break;
13313 case DW_TAG_typedef:
13314 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13315 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13316 list_to_add = cu->list_in_scope;
13317 break;
13318 case DW_TAG_base_type:
13319 case DW_TAG_subrange_type:
13320 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13321 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13322 list_to_add = cu->list_in_scope;
13323 break;
13324 case DW_TAG_enumerator:
13325 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13326 if (attr)
13327 {
13328 dwarf2_const_value (attr, sym, cu);
13329 }
13330 {
13331 /* NOTE: carlton/2003-11-10: See comment above in the
13332 DW_TAG_class_type, etc. block. */
13333
13334 list_to_add = (cu->list_in_scope == &file_symbols
13335 && (cu->language == language_cplus
13336 || cu->language == language_java)
13337 ? &global_symbols : cu->list_in_scope);
13338 }
13339 break;
13340 case DW_TAG_namespace:
13341 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13342 list_to_add = &global_symbols;
13343 break;
13344 default:
13345 /* Not a tag we recognize. Hopefully we aren't processing
13346 trash data, but since we must specifically ignore things
13347 we don't recognize, there is nothing else we should do at
13348 this point. */
13349 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13350 dwarf_tag_name (die->tag));
13351 break;
13352 }
13353
13354 if (suppress_add)
13355 {
13356 sym->hash_next = objfile->template_symbols;
13357 objfile->template_symbols = sym;
13358 list_to_add = NULL;
13359 }
13360
13361 if (list_to_add != NULL)
13362 add_symbol_to_list (sym, list_to_add);
13363
13364 /* For the benefit of old versions of GCC, check for anonymous
13365 namespaces based on the demangled name. */
13366 if (!processing_has_namespace_info
13367 && cu->language == language_cplus)
13368 cp_scan_for_anonymous_namespaces (sym, objfile);
13369 }
13370 return (sym);
13371}
13372
13373/* A wrapper for new_symbol_full that always allocates a new symbol. */
13374
13375static struct symbol *
13376new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13377{
13378 return new_symbol_full (die, type, cu, NULL);
13379}
13380
13381/* Given an attr with a DW_FORM_dataN value in host byte order,
13382 zero-extend it as appropriate for the symbol's type. The DWARF
13383 standard (v4) is not entirely clear about the meaning of using
13384 DW_FORM_dataN for a constant with a signed type, where the type is
13385 wider than the data. The conclusion of a discussion on the DWARF
13386 list was that this is unspecified. We choose to always zero-extend
13387 because that is the interpretation long in use by GCC. */
13388
13389static gdb_byte *
13390dwarf2_const_value_data (struct attribute *attr, struct type *type,
13391 const char *name, struct obstack *obstack,
13392 struct dwarf2_cu *cu, LONGEST *value, int bits)
13393{
13394 struct objfile *objfile = cu->objfile;
13395 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13396 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13397 LONGEST l = DW_UNSND (attr);
13398
13399 if (bits < sizeof (*value) * 8)
13400 {
13401 l &= ((LONGEST) 1 << bits) - 1;
13402 *value = l;
13403 }
13404 else if (bits == sizeof (*value) * 8)
13405 *value = l;
13406 else
13407 {
13408 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13409 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13410 return bytes;
13411 }
13412
13413 return NULL;
13414}
13415
13416/* Read a constant value from an attribute. Either set *VALUE, or if
13417 the value does not fit in *VALUE, set *BYTES - either already
13418 allocated on the objfile obstack, or newly allocated on OBSTACK,
13419 or, set *BATON, if we translated the constant to a location
13420 expression. */
13421
13422static void
13423dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13424 const char *name, struct obstack *obstack,
13425 struct dwarf2_cu *cu,
13426 LONGEST *value, gdb_byte **bytes,
13427 struct dwarf2_locexpr_baton **baton)
13428{
13429 struct objfile *objfile = cu->objfile;
13430 struct comp_unit_head *cu_header = &cu->header;
13431 struct dwarf_block *blk;
13432 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13433 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13434
13435 *value = 0;
13436 *bytes = NULL;
13437 *baton = NULL;
13438
13439 switch (attr->form)
13440 {
13441 case DW_FORM_addr:
13442 case DW_FORM_GNU_addr_index:
13443 {
13444 gdb_byte *data;
13445
13446 if (TYPE_LENGTH (type) != cu_header->addr_size)
13447 dwarf2_const_value_length_mismatch_complaint (name,
13448 cu_header->addr_size,
13449 TYPE_LENGTH (type));
13450 /* Symbols of this form are reasonably rare, so we just
13451 piggyback on the existing location code rather than writing
13452 a new implementation of symbol_computed_ops. */
13453 *baton = obstack_alloc (&objfile->objfile_obstack,
13454 sizeof (struct dwarf2_locexpr_baton));
13455 (*baton)->per_cu = cu->per_cu;
13456 gdb_assert ((*baton)->per_cu);
13457
13458 (*baton)->size = 2 + cu_header->addr_size;
13459 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13460 (*baton)->data = data;
13461
13462 data[0] = DW_OP_addr;
13463 store_unsigned_integer (&data[1], cu_header->addr_size,
13464 byte_order, DW_ADDR (attr));
13465 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13466 }
13467 break;
13468 case DW_FORM_string:
13469 case DW_FORM_strp:
13470 case DW_FORM_GNU_str_index:
13471 /* DW_STRING is already allocated on the objfile obstack, point
13472 directly to it. */
13473 *bytes = (gdb_byte *) DW_STRING (attr);
13474 break;
13475 case DW_FORM_block1:
13476 case DW_FORM_block2:
13477 case DW_FORM_block4:
13478 case DW_FORM_block:
13479 case DW_FORM_exprloc:
13480 blk = DW_BLOCK (attr);
13481 if (TYPE_LENGTH (type) != blk->size)
13482 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13483 TYPE_LENGTH (type));
13484 *bytes = blk->data;
13485 break;
13486
13487 /* The DW_AT_const_value attributes are supposed to carry the
13488 symbol's value "represented as it would be on the target
13489 architecture." By the time we get here, it's already been
13490 converted to host endianness, so we just need to sign- or
13491 zero-extend it as appropriate. */
13492 case DW_FORM_data1:
13493 *bytes = dwarf2_const_value_data (attr, type, name,
13494 obstack, cu, value, 8);
13495 break;
13496 case DW_FORM_data2:
13497 *bytes = dwarf2_const_value_data (attr, type, name,
13498 obstack, cu, value, 16);
13499 break;
13500 case DW_FORM_data4:
13501 *bytes = dwarf2_const_value_data (attr, type, name,
13502 obstack, cu, value, 32);
13503 break;
13504 case DW_FORM_data8:
13505 *bytes = dwarf2_const_value_data (attr, type, name,
13506 obstack, cu, value, 64);
13507 break;
13508
13509 case DW_FORM_sdata:
13510 *value = DW_SND (attr);
13511 break;
13512
13513 case DW_FORM_udata:
13514 *value = DW_UNSND (attr);
13515 break;
13516
13517 default:
13518 complaint (&symfile_complaints,
13519 _("unsupported const value attribute form: '%s'"),
13520 dwarf_form_name (attr->form));
13521 *value = 0;
13522 break;
13523 }
13524}
13525
13526
13527/* Copy constant value from an attribute to a symbol. */
13528
13529static void
13530dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13531 struct dwarf2_cu *cu)
13532{
13533 struct objfile *objfile = cu->objfile;
13534 struct comp_unit_head *cu_header = &cu->header;
13535 LONGEST value;
13536 gdb_byte *bytes;
13537 struct dwarf2_locexpr_baton *baton;
13538
13539 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13540 SYMBOL_PRINT_NAME (sym),
13541 &objfile->objfile_obstack, cu,
13542 &value, &bytes, &baton);
13543
13544 if (baton != NULL)
13545 {
13546 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13547 SYMBOL_LOCATION_BATON (sym) = baton;
13548 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13549 }
13550 else if (bytes != NULL)
13551 {
13552 SYMBOL_VALUE_BYTES (sym) = bytes;
13553 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13554 }
13555 else
13556 {
13557 SYMBOL_VALUE (sym) = value;
13558 SYMBOL_CLASS (sym) = LOC_CONST;
13559 }
13560}
13561
13562/* Return the type of the die in question using its DW_AT_type attribute. */
13563
13564static struct type *
13565die_type (struct die_info *die, struct dwarf2_cu *cu)
13566{
13567 struct attribute *type_attr;
13568
13569 type_attr = dwarf2_attr (die, DW_AT_type, cu);
13570 if (!type_attr)
13571 {
13572 /* A missing DW_AT_type represents a void type. */
13573 return objfile_type (cu->objfile)->builtin_void;
13574 }
13575
13576 return lookup_die_type (die, type_attr, cu);
13577}
13578
13579/* True iff CU's producer generates GNAT Ada auxiliary information
13580 that allows to find parallel types through that information instead
13581 of having to do expensive parallel lookups by type name. */
13582
13583static int
13584need_gnat_info (struct dwarf2_cu *cu)
13585{
13586 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13587 of GNAT produces this auxiliary information, without any indication
13588 that it is produced. Part of enhancing the FSF version of GNAT
13589 to produce that information will be to put in place an indicator
13590 that we can use in order to determine whether the descriptive type
13591 info is available or not. One suggestion that has been made is
13592 to use a new attribute, attached to the CU die. For now, assume
13593 that the descriptive type info is not available. */
13594 return 0;
13595}
13596
13597/* Return the auxiliary type of the die in question using its
13598 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13599 attribute is not present. */
13600
13601static struct type *
13602die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13603{
13604 struct attribute *type_attr;
13605
13606 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13607 if (!type_attr)
13608 return NULL;
13609
13610 return lookup_die_type (die, type_attr, cu);
13611}
13612
13613/* If DIE has a descriptive_type attribute, then set the TYPE's
13614 descriptive type accordingly. */
13615
13616static void
13617set_descriptive_type (struct type *type, struct die_info *die,
13618 struct dwarf2_cu *cu)
13619{
13620 struct type *descriptive_type = die_descriptive_type (die, cu);
13621
13622 if (descriptive_type)
13623 {
13624 ALLOCATE_GNAT_AUX_TYPE (type);
13625 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13626 }
13627}
13628
13629/* Return the containing type of the die in question using its
13630 DW_AT_containing_type attribute. */
13631
13632static struct type *
13633die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13634{
13635 struct attribute *type_attr;
13636
13637 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
13638 if (!type_attr)
13639 error (_("Dwarf Error: Problem turning containing type into gdb type "
13640 "[in module %s]"), cu->objfile->name);
13641
13642 return lookup_die_type (die, type_attr, cu);
13643}
13644
13645/* Look up the type of DIE in CU using its type attribute ATTR.
13646 If there is no type substitute an error marker. */
13647
13648static struct type *
13649lookup_die_type (struct die_info *die, struct attribute *attr,
13650 struct dwarf2_cu *cu)
13651{
13652 struct objfile *objfile = cu->objfile;
13653 struct type *this_type;
13654
13655 /* First see if we have it cached. */
13656
13657 if (is_ref_attr (attr))
13658 {
13659 sect_offset offset = dwarf2_get_ref_die_offset (attr);
13660
13661 this_type = get_die_type_at_offset (offset, cu->per_cu);
13662 }
13663 else if (attr->form == DW_FORM_ref_sig8)
13664 {
13665 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13666
13667 /* sig_type will be NULL if the signatured type is missing from
13668 the debug info. */
13669 if (sig_type == NULL)
13670 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13671 "at 0x%x [in module %s]"),
13672 die->offset.sect_off, objfile->name);
13673
13674 gdb_assert (sig_type->per_cu.is_debug_types);
13675 /* If we haven't filled in type_offset_in_section yet, then we
13676 haven't read the type in yet. */
13677 this_type = NULL;
13678 if (sig_type->type_offset_in_section.sect_off != 0)
13679 {
13680 this_type =
13681 get_die_type_at_offset (sig_type->type_offset_in_section,
13682 &sig_type->per_cu);
13683 }
13684 }
13685 else
13686 {
13687 dump_die_for_error (die);
13688 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
13689 dwarf_attr_name (attr->name), objfile->name);
13690 }
13691
13692 /* If not cached we need to read it in. */
13693
13694 if (this_type == NULL)
13695 {
13696 struct die_info *type_die;
13697 struct dwarf2_cu *type_cu = cu;
13698
13699 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
13700 /* If we found the type now, it's probably because the type came
13701 from an inter-CU reference and the type's CU got expanded before
13702 ours. */
13703 this_type = get_die_type (type_die, type_cu);
13704 if (this_type == NULL)
13705 this_type = read_type_die_1 (type_die, type_cu);
13706 }
13707
13708 /* If we still don't have a type use an error marker. */
13709
13710 if (this_type == NULL)
13711 {
13712 char *message, *saved;
13713
13714 /* read_type_die already issued a complaint. */
13715 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
13716 objfile->name,
13717 cu->header.offset.sect_off,
13718 die->offset.sect_off);
13719 saved = obstack_copy0 (&objfile->objfile_obstack,
13720 message, strlen (message));
13721 xfree (message);
13722
13723 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
13724 }
13725
13726 return this_type;
13727}
13728
13729/* Return the type in DIE, CU.
13730 Returns NULL for invalid types.
13731
13732 This first does a lookup in the appropriate type_hash table,
13733 and only reads the die in if necessary.
13734
13735 NOTE: This can be called when reading in partial or full symbols. */
13736
13737static struct type *
13738read_type_die (struct die_info *die, struct dwarf2_cu *cu)
13739{
13740 struct type *this_type;
13741
13742 this_type = get_die_type (die, cu);
13743 if (this_type)
13744 return this_type;
13745
13746 return read_type_die_1 (die, cu);
13747}
13748
13749/* Read the type in DIE, CU.
13750 Returns NULL for invalid types. */
13751
13752static struct type *
13753read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
13754{
13755 struct type *this_type = NULL;
13756
13757 switch (die->tag)
13758 {
13759 case DW_TAG_class_type:
13760 case DW_TAG_interface_type:
13761 case DW_TAG_structure_type:
13762 case DW_TAG_union_type:
13763 this_type = read_structure_type (die, cu);
13764 break;
13765 case DW_TAG_enumeration_type:
13766 this_type = read_enumeration_type (die, cu);
13767 break;
13768 case DW_TAG_subprogram:
13769 case DW_TAG_subroutine_type:
13770 case DW_TAG_inlined_subroutine:
13771 this_type = read_subroutine_type (die, cu);
13772 break;
13773 case DW_TAG_array_type:
13774 this_type = read_array_type (die, cu);
13775 break;
13776 case DW_TAG_set_type:
13777 this_type = read_set_type (die, cu);
13778 break;
13779 case DW_TAG_pointer_type:
13780 this_type = read_tag_pointer_type (die, cu);
13781 break;
13782 case DW_TAG_ptr_to_member_type:
13783 this_type = read_tag_ptr_to_member_type (die, cu);
13784 break;
13785 case DW_TAG_reference_type:
13786 this_type = read_tag_reference_type (die, cu);
13787 break;
13788 case DW_TAG_const_type:
13789 this_type = read_tag_const_type (die, cu);
13790 break;
13791 case DW_TAG_volatile_type:
13792 this_type = read_tag_volatile_type (die, cu);
13793 break;
13794 case DW_TAG_string_type:
13795 this_type = read_tag_string_type (die, cu);
13796 break;
13797 case DW_TAG_typedef:
13798 this_type = read_typedef (die, cu);
13799 break;
13800 case DW_TAG_subrange_type:
13801 this_type = read_subrange_type (die, cu);
13802 break;
13803 case DW_TAG_base_type:
13804 this_type = read_base_type (die, cu);
13805 break;
13806 case DW_TAG_unspecified_type:
13807 this_type = read_unspecified_type (die, cu);
13808 break;
13809 case DW_TAG_namespace:
13810 this_type = read_namespace_type (die, cu);
13811 break;
13812 case DW_TAG_module:
13813 this_type = read_module_type (die, cu);
13814 break;
13815 default:
13816 complaint (&symfile_complaints,
13817 _("unexpected tag in read_type_die: '%s'"),
13818 dwarf_tag_name (die->tag));
13819 break;
13820 }
13821
13822 return this_type;
13823}
13824
13825/* See if we can figure out if the class lives in a namespace. We do
13826 this by looking for a member function; its demangled name will
13827 contain namespace info, if there is any.
13828 Return the computed name or NULL.
13829 Space for the result is allocated on the objfile's obstack.
13830 This is the full-die version of guess_partial_die_structure_name.
13831 In this case we know DIE has no useful parent. */
13832
13833static char *
13834guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
13835{
13836 struct die_info *spec_die;
13837 struct dwarf2_cu *spec_cu;
13838 struct die_info *child;
13839
13840 spec_cu = cu;
13841 spec_die = die_specification (die, &spec_cu);
13842 if (spec_die != NULL)
13843 {
13844 die = spec_die;
13845 cu = spec_cu;
13846 }
13847
13848 for (child = die->child;
13849 child != NULL;
13850 child = child->sibling)
13851 {
13852 if (child->tag == DW_TAG_subprogram)
13853 {
13854 struct attribute *attr;
13855
13856 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
13857 if (attr == NULL)
13858 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
13859 if (attr != NULL)
13860 {
13861 char *actual_name
13862 = language_class_name_from_physname (cu->language_defn,
13863 DW_STRING (attr));
13864 char *name = NULL;
13865
13866 if (actual_name != NULL)
13867 {
13868 char *die_name = dwarf2_name (die, cu);
13869
13870 if (die_name != NULL
13871 && strcmp (die_name, actual_name) != 0)
13872 {
13873 /* Strip off the class name from the full name.
13874 We want the prefix. */
13875 int die_name_len = strlen (die_name);
13876 int actual_name_len = strlen (actual_name);
13877
13878 /* Test for '::' as a sanity check. */
13879 if (actual_name_len > die_name_len + 2
13880 && actual_name[actual_name_len
13881 - die_name_len - 1] == ':')
13882 name =
13883 obsavestring (actual_name,
13884 actual_name_len - die_name_len - 2,
13885 &cu->objfile->objfile_obstack);
13886 }
13887 }
13888 xfree (actual_name);
13889 return name;
13890 }
13891 }
13892 }
13893
13894 return NULL;
13895}
13896
13897/* GCC might emit a nameless typedef that has a linkage name. Determine the
13898 prefix part in such case. See
13899 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13900
13901static char *
13902anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
13903{
13904 struct attribute *attr;
13905 char *base;
13906
13907 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
13908 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
13909 return NULL;
13910
13911 attr = dwarf2_attr (die, DW_AT_name, cu);
13912 if (attr != NULL && DW_STRING (attr) != NULL)
13913 return NULL;
13914
13915 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
13916 if (attr == NULL)
13917 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
13918 if (attr == NULL || DW_STRING (attr) == NULL)
13919 return NULL;
13920
13921 /* dwarf2_name had to be already called. */
13922 gdb_assert (DW_STRING_IS_CANONICAL (attr));
13923
13924 /* Strip the base name, keep any leading namespaces/classes. */
13925 base = strrchr (DW_STRING (attr), ':');
13926 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
13927 return "";
13928
13929 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
13930 &cu->objfile->objfile_obstack);
13931}
13932
13933/* Return the name of the namespace/class that DIE is defined within,
13934 or "" if we can't tell. The caller should not xfree the result.
13935
13936 For example, if we're within the method foo() in the following
13937 code:
13938
13939 namespace N {
13940 class C {
13941 void foo () {
13942 }
13943 };
13944 }
13945
13946 then determine_prefix on foo's die will return "N::C". */
13947
13948static const char *
13949determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
13950{
13951 struct die_info *parent, *spec_die;
13952 struct dwarf2_cu *spec_cu;
13953 struct type *parent_type;
13954 char *retval;
13955
13956 if (cu->language != language_cplus && cu->language != language_java
13957 && cu->language != language_fortran)
13958 return "";
13959
13960 retval = anonymous_struct_prefix (die, cu);
13961 if (retval)
13962 return retval;
13963
13964 /* We have to be careful in the presence of DW_AT_specification.
13965 For example, with GCC 3.4, given the code
13966
13967 namespace N {
13968 void foo() {
13969 // Definition of N::foo.
13970 }
13971 }
13972
13973 then we'll have a tree of DIEs like this:
13974
13975 1: DW_TAG_compile_unit
13976 2: DW_TAG_namespace // N
13977 3: DW_TAG_subprogram // declaration of N::foo
13978 4: DW_TAG_subprogram // definition of N::foo
13979 DW_AT_specification // refers to die #3
13980
13981 Thus, when processing die #4, we have to pretend that we're in
13982 the context of its DW_AT_specification, namely the contex of die
13983 #3. */
13984 spec_cu = cu;
13985 spec_die = die_specification (die, &spec_cu);
13986 if (spec_die == NULL)
13987 parent = die->parent;
13988 else
13989 {
13990 parent = spec_die->parent;
13991 cu = spec_cu;
13992 }
13993
13994 if (parent == NULL)
13995 return "";
13996 else if (parent->building_fullname)
13997 {
13998 const char *name;
13999 const char *parent_name;
14000
14001 /* It has been seen on RealView 2.2 built binaries,
14002 DW_TAG_template_type_param types actually _defined_ as
14003 children of the parent class:
14004
14005 enum E {};
14006 template class <class Enum> Class{};
14007 Class<enum E> class_e;
14008
14009 1: DW_TAG_class_type (Class)
14010 2: DW_TAG_enumeration_type (E)
14011 3: DW_TAG_enumerator (enum1:0)
14012 3: DW_TAG_enumerator (enum2:1)
14013 ...
14014 2: DW_TAG_template_type_param
14015 DW_AT_type DW_FORM_ref_udata (E)
14016
14017 Besides being broken debug info, it can put GDB into an
14018 infinite loop. Consider:
14019
14020 When we're building the full name for Class<E>, we'll start
14021 at Class, and go look over its template type parameters,
14022 finding E. We'll then try to build the full name of E, and
14023 reach here. We're now trying to build the full name of E,
14024 and look over the parent DIE for containing scope. In the
14025 broken case, if we followed the parent DIE of E, we'd again
14026 find Class, and once again go look at its template type
14027 arguments, etc., etc. Simply don't consider such parent die
14028 as source-level parent of this die (it can't be, the language
14029 doesn't allow it), and break the loop here. */
14030 name = dwarf2_name (die, cu);
14031 parent_name = dwarf2_name (parent, cu);
14032 complaint (&symfile_complaints,
14033 _("template param type '%s' defined within parent '%s'"),
14034 name ? name : "<unknown>",
14035 parent_name ? parent_name : "<unknown>");
14036 return "";
14037 }
14038 else
14039 switch (parent->tag)
14040 {
14041 case DW_TAG_namespace:
14042 parent_type = read_type_die (parent, cu);
14043 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14044 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14045 Work around this problem here. */
14046 if (cu->language == language_cplus
14047 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14048 return "";
14049 /* We give a name to even anonymous namespaces. */
14050 return TYPE_TAG_NAME (parent_type);
14051 case DW_TAG_class_type:
14052 case DW_TAG_interface_type:
14053 case DW_TAG_structure_type:
14054 case DW_TAG_union_type:
14055 case DW_TAG_module:
14056 parent_type = read_type_die (parent, cu);
14057 if (TYPE_TAG_NAME (parent_type) != NULL)
14058 return TYPE_TAG_NAME (parent_type);
14059 else
14060 /* An anonymous structure is only allowed non-static data
14061 members; no typedefs, no member functions, et cetera.
14062 So it does not need a prefix. */
14063 return "";
14064 case DW_TAG_compile_unit:
14065 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14066 if (cu->language == language_cplus
14067 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14068 && die->child != NULL
14069 && (die->tag == DW_TAG_class_type
14070 || die->tag == DW_TAG_structure_type
14071 || die->tag == DW_TAG_union_type))
14072 {
14073 char *name = guess_full_die_structure_name (die, cu);
14074 if (name != NULL)
14075 return name;
14076 }
14077 return "";
14078 default:
14079 return determine_prefix (parent, cu);
14080 }
14081}
14082
14083/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14084 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14085 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14086 an obconcat, otherwise allocate storage for the result. The CU argument is
14087 used to determine the language and hence, the appropriate separator. */
14088
14089#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14090
14091static char *
14092typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14093 int physname, struct dwarf2_cu *cu)
14094{
14095 const char *lead = "";
14096 const char *sep;
14097
14098 if (suffix == NULL || suffix[0] == '\0'
14099 || prefix == NULL || prefix[0] == '\0')
14100 sep = "";
14101 else if (cu->language == language_java)
14102 sep = ".";
14103 else if (cu->language == language_fortran && physname)
14104 {
14105 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14106 DW_AT_MIPS_linkage_name is preferred and used instead. */
14107
14108 lead = "__";
14109 sep = "_MOD_";
14110 }
14111 else
14112 sep = "::";
14113
14114 if (prefix == NULL)
14115 prefix = "";
14116 if (suffix == NULL)
14117 suffix = "";
14118
14119 if (obs == NULL)
14120 {
14121 char *retval
14122 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14123
14124 strcpy (retval, lead);
14125 strcat (retval, prefix);
14126 strcat (retval, sep);
14127 strcat (retval, suffix);
14128 return retval;
14129 }
14130 else
14131 {
14132 /* We have an obstack. */
14133 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14134 }
14135}
14136
14137/* Return sibling of die, NULL if no sibling. */
14138
14139static struct die_info *
14140sibling_die (struct die_info *die)
14141{
14142 return die->sibling;
14143}
14144
14145/* Get name of a die, return NULL if not found. */
14146
14147static char *
14148dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14149 struct obstack *obstack)
14150{
14151 if (name && cu->language == language_cplus)
14152 {
14153 char *canon_name = cp_canonicalize_string (name);
14154
14155 if (canon_name != NULL)
14156 {
14157 if (strcmp (canon_name, name) != 0)
14158 name = obsavestring (canon_name, strlen (canon_name),
14159 obstack);
14160 xfree (canon_name);
14161 }
14162 }
14163
14164 return name;
14165}
14166
14167/* Get name of a die, return NULL if not found. */
14168
14169static char *
14170dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14171{
14172 struct attribute *attr;
14173
14174 attr = dwarf2_attr (die, DW_AT_name, cu);
14175 if ((!attr || !DW_STRING (attr))
14176 && die->tag != DW_TAG_class_type
14177 && die->tag != DW_TAG_interface_type
14178 && die->tag != DW_TAG_structure_type
14179 && die->tag != DW_TAG_union_type)
14180 return NULL;
14181
14182 switch (die->tag)
14183 {
14184 case DW_TAG_compile_unit:
14185 /* Compilation units have a DW_AT_name that is a filename, not
14186 a source language identifier. */
14187 case DW_TAG_enumeration_type:
14188 case DW_TAG_enumerator:
14189 /* These tags always have simple identifiers already; no need
14190 to canonicalize them. */
14191 return DW_STRING (attr);
14192
14193 case DW_TAG_subprogram:
14194 /* Java constructors will all be named "<init>", so return
14195 the class name when we see this special case. */
14196 if (cu->language == language_java
14197 && DW_STRING (attr) != NULL
14198 && strcmp (DW_STRING (attr), "<init>") == 0)
14199 {
14200 struct dwarf2_cu *spec_cu = cu;
14201 struct die_info *spec_die;
14202
14203 /* GCJ will output '<init>' for Java constructor names.
14204 For this special case, return the name of the parent class. */
14205
14206 /* GCJ may output suprogram DIEs with AT_specification set.
14207 If so, use the name of the specified DIE. */
14208 spec_die = die_specification (die, &spec_cu);
14209 if (spec_die != NULL)
14210 return dwarf2_name (spec_die, spec_cu);
14211
14212 do
14213 {
14214 die = die->parent;
14215 if (die->tag == DW_TAG_class_type)
14216 return dwarf2_name (die, cu);
14217 }
14218 while (die->tag != DW_TAG_compile_unit);
14219 }
14220 break;
14221
14222 case DW_TAG_class_type:
14223 case DW_TAG_interface_type:
14224 case DW_TAG_structure_type:
14225 case DW_TAG_union_type:
14226 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14227 structures or unions. These were of the form "._%d" in GCC 4.1,
14228 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14229 and GCC 4.4. We work around this problem by ignoring these. */
14230 if (attr && DW_STRING (attr)
14231 && (strncmp (DW_STRING (attr), "._", 2) == 0
14232 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14233 return NULL;
14234
14235 /* GCC might emit a nameless typedef that has a linkage name. See
14236 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14237 if (!attr || DW_STRING (attr) == NULL)
14238 {
14239 char *demangled = NULL;
14240
14241 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14242 if (attr == NULL)
14243 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14244
14245 if (attr == NULL || DW_STRING (attr) == NULL)
14246 return NULL;
14247
14248 /* Avoid demangling DW_STRING (attr) the second time on a second
14249 call for the same DIE. */
14250 if (!DW_STRING_IS_CANONICAL (attr))
14251 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14252
14253 if (demangled)
14254 {
14255 char *base;
14256
14257 /* FIXME: we already did this for the partial symbol... */
14258 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14259 &cu->objfile->objfile_obstack);
14260 DW_STRING_IS_CANONICAL (attr) = 1;
14261 xfree (demangled);
14262
14263 /* Strip any leading namespaces/classes, keep only the base name.
14264 DW_AT_name for named DIEs does not contain the prefixes. */
14265 base = strrchr (DW_STRING (attr), ':');
14266 if (base && base > DW_STRING (attr) && base[-1] == ':')
14267 return &base[1];
14268 else
14269 return DW_STRING (attr);
14270 }
14271 }
14272 break;
14273
14274 default:
14275 break;
14276 }
14277
14278 if (!DW_STRING_IS_CANONICAL (attr))
14279 {
14280 DW_STRING (attr)
14281 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14282 &cu->objfile->objfile_obstack);
14283 DW_STRING_IS_CANONICAL (attr) = 1;
14284 }
14285 return DW_STRING (attr);
14286}
14287
14288/* Return the die that this die in an extension of, or NULL if there
14289 is none. *EXT_CU is the CU containing DIE on input, and the CU
14290 containing the return value on output. */
14291
14292static struct die_info *
14293dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14294{
14295 struct attribute *attr;
14296
14297 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14298 if (attr == NULL)
14299 return NULL;
14300
14301 return follow_die_ref (die, attr, ext_cu);
14302}
14303
14304/* Convert a DIE tag into its string name. */
14305
14306static char *
14307dwarf_tag_name (unsigned tag)
14308{
14309 switch (tag)
14310 {
14311 case DW_TAG_padding:
14312 return "DW_TAG_padding";
14313 case DW_TAG_array_type:
14314 return "DW_TAG_array_type";
14315 case DW_TAG_class_type:
14316 return "DW_TAG_class_type";
14317 case DW_TAG_entry_point:
14318 return "DW_TAG_entry_point";
14319 case DW_TAG_enumeration_type:
14320 return "DW_TAG_enumeration_type";
14321 case DW_TAG_formal_parameter:
14322 return "DW_TAG_formal_parameter";
14323 case DW_TAG_imported_declaration:
14324 return "DW_TAG_imported_declaration";
14325 case DW_TAG_label:
14326 return "DW_TAG_label";
14327 case DW_TAG_lexical_block:
14328 return "DW_TAG_lexical_block";
14329 case DW_TAG_member:
14330 return "DW_TAG_member";
14331 case DW_TAG_pointer_type:
14332 return "DW_TAG_pointer_type";
14333 case DW_TAG_reference_type:
14334 return "DW_TAG_reference_type";
14335 case DW_TAG_compile_unit:
14336 return "DW_TAG_compile_unit";
14337 case DW_TAG_string_type:
14338 return "DW_TAG_string_type";
14339 case DW_TAG_structure_type:
14340 return "DW_TAG_structure_type";
14341 case DW_TAG_subroutine_type:
14342 return "DW_TAG_subroutine_type";
14343 case DW_TAG_typedef:
14344 return "DW_TAG_typedef";
14345 case DW_TAG_union_type:
14346 return "DW_TAG_union_type";
14347 case DW_TAG_unspecified_parameters:
14348 return "DW_TAG_unspecified_parameters";
14349 case DW_TAG_variant:
14350 return "DW_TAG_variant";
14351 case DW_TAG_common_block:
14352 return "DW_TAG_common_block";
14353 case DW_TAG_common_inclusion:
14354 return "DW_TAG_common_inclusion";
14355 case DW_TAG_inheritance:
14356 return "DW_TAG_inheritance";
14357 case DW_TAG_inlined_subroutine:
14358 return "DW_TAG_inlined_subroutine";
14359 case DW_TAG_module:
14360 return "DW_TAG_module";
14361 case DW_TAG_ptr_to_member_type:
14362 return "DW_TAG_ptr_to_member_type";
14363 case DW_TAG_set_type:
14364 return "DW_TAG_set_type";
14365 case DW_TAG_subrange_type:
14366 return "DW_TAG_subrange_type";
14367 case DW_TAG_with_stmt:
14368 return "DW_TAG_with_stmt";
14369 case DW_TAG_access_declaration:
14370 return "DW_TAG_access_declaration";
14371 case DW_TAG_base_type:
14372 return "DW_TAG_base_type";
14373 case DW_TAG_catch_block:
14374 return "DW_TAG_catch_block";
14375 case DW_TAG_const_type:
14376 return "DW_TAG_const_type";
14377 case DW_TAG_constant:
14378 return "DW_TAG_constant";
14379 case DW_TAG_enumerator:
14380 return "DW_TAG_enumerator";
14381 case DW_TAG_file_type:
14382 return "DW_TAG_file_type";
14383 case DW_TAG_friend:
14384 return "DW_TAG_friend";
14385 case DW_TAG_namelist:
14386 return "DW_TAG_namelist";
14387 case DW_TAG_namelist_item:
14388 return "DW_TAG_namelist_item";
14389 case DW_TAG_packed_type:
14390 return "DW_TAG_packed_type";
14391 case DW_TAG_subprogram:
14392 return "DW_TAG_subprogram";
14393 case DW_TAG_template_type_param:
14394 return "DW_TAG_template_type_param";
14395 case DW_TAG_template_value_param:
14396 return "DW_TAG_template_value_param";
14397 case DW_TAG_thrown_type:
14398 return "DW_TAG_thrown_type";
14399 case DW_TAG_try_block:
14400 return "DW_TAG_try_block";
14401 case DW_TAG_variant_part:
14402 return "DW_TAG_variant_part";
14403 case DW_TAG_variable:
14404 return "DW_TAG_variable";
14405 case DW_TAG_volatile_type:
14406 return "DW_TAG_volatile_type";
14407 case DW_TAG_dwarf_procedure:
14408 return "DW_TAG_dwarf_procedure";
14409 case DW_TAG_restrict_type:
14410 return "DW_TAG_restrict_type";
14411 case DW_TAG_interface_type:
14412 return "DW_TAG_interface_type";
14413 case DW_TAG_namespace:
14414 return "DW_TAG_namespace";
14415 case DW_TAG_imported_module:
14416 return "DW_TAG_imported_module";
14417 case DW_TAG_unspecified_type:
14418 return "DW_TAG_unspecified_type";
14419 case DW_TAG_partial_unit:
14420 return "DW_TAG_partial_unit";
14421 case DW_TAG_imported_unit:
14422 return "DW_TAG_imported_unit";
14423 case DW_TAG_condition:
14424 return "DW_TAG_condition";
14425 case DW_TAG_shared_type:
14426 return "DW_TAG_shared_type";
14427 case DW_TAG_type_unit:
14428 return "DW_TAG_type_unit";
14429 case DW_TAG_MIPS_loop:
14430 return "DW_TAG_MIPS_loop";
14431 case DW_TAG_HP_array_descriptor:
14432 return "DW_TAG_HP_array_descriptor";
14433 case DW_TAG_format_label:
14434 return "DW_TAG_format_label";
14435 case DW_TAG_function_template:
14436 return "DW_TAG_function_template";
14437 case DW_TAG_class_template:
14438 return "DW_TAG_class_template";
14439 case DW_TAG_GNU_BINCL:
14440 return "DW_TAG_GNU_BINCL";
14441 case DW_TAG_GNU_EINCL:
14442 return "DW_TAG_GNU_EINCL";
14443 case DW_TAG_upc_shared_type:
14444 return "DW_TAG_upc_shared_type";
14445 case DW_TAG_upc_strict_type:
14446 return "DW_TAG_upc_strict_type";
14447 case DW_TAG_upc_relaxed_type:
14448 return "DW_TAG_upc_relaxed_type";
14449 case DW_TAG_PGI_kanji_type:
14450 return "DW_TAG_PGI_kanji_type";
14451 case DW_TAG_PGI_interface_block:
14452 return "DW_TAG_PGI_interface_block";
14453 case DW_TAG_GNU_call_site:
14454 return "DW_TAG_GNU_call_site";
14455 default:
14456 return "DW_TAG_<unknown>";
14457 }
14458}
14459
14460/* Convert a DWARF attribute code into its string name. */
14461
14462static char *
14463dwarf_attr_name (unsigned attr)
14464{
14465 switch (attr)
14466 {
14467 case DW_AT_sibling:
14468 return "DW_AT_sibling";
14469 case DW_AT_location:
14470 return "DW_AT_location";
14471 case DW_AT_name:
14472 return "DW_AT_name";
14473 case DW_AT_ordering:
14474 return "DW_AT_ordering";
14475 case DW_AT_subscr_data:
14476 return "DW_AT_subscr_data";
14477 case DW_AT_byte_size:
14478 return "DW_AT_byte_size";
14479 case DW_AT_bit_offset:
14480 return "DW_AT_bit_offset";
14481 case DW_AT_bit_size:
14482 return "DW_AT_bit_size";
14483 case DW_AT_element_list:
14484 return "DW_AT_element_list";
14485 case DW_AT_stmt_list:
14486 return "DW_AT_stmt_list";
14487 case DW_AT_low_pc:
14488 return "DW_AT_low_pc";
14489 case DW_AT_high_pc:
14490 return "DW_AT_high_pc";
14491 case DW_AT_language:
14492 return "DW_AT_language";
14493 case DW_AT_member:
14494 return "DW_AT_member";
14495 case DW_AT_discr:
14496 return "DW_AT_discr";
14497 case DW_AT_discr_value:
14498 return "DW_AT_discr_value";
14499 case DW_AT_visibility:
14500 return "DW_AT_visibility";
14501 case DW_AT_import:
14502 return "DW_AT_import";
14503 case DW_AT_string_length:
14504 return "DW_AT_string_length";
14505 case DW_AT_common_reference:
14506 return "DW_AT_common_reference";
14507 case DW_AT_comp_dir:
14508 return "DW_AT_comp_dir";
14509 case DW_AT_const_value:
14510 return "DW_AT_const_value";
14511 case DW_AT_containing_type:
14512 return "DW_AT_containing_type";
14513 case DW_AT_default_value:
14514 return "DW_AT_default_value";
14515 case DW_AT_inline:
14516 return "DW_AT_inline";
14517 case DW_AT_is_optional:
14518 return "DW_AT_is_optional";
14519 case DW_AT_lower_bound:
14520 return "DW_AT_lower_bound";
14521 case DW_AT_producer:
14522 return "DW_AT_producer";
14523 case DW_AT_prototyped:
14524 return "DW_AT_prototyped";
14525 case DW_AT_return_addr:
14526 return "DW_AT_return_addr";
14527 case DW_AT_start_scope:
14528 return "DW_AT_start_scope";
14529 case DW_AT_bit_stride:
14530 return "DW_AT_bit_stride";
14531 case DW_AT_upper_bound:
14532 return "DW_AT_upper_bound";
14533 case DW_AT_abstract_origin:
14534 return "DW_AT_abstract_origin";
14535 case DW_AT_accessibility:
14536 return "DW_AT_accessibility";
14537 case DW_AT_address_class:
14538 return "DW_AT_address_class";
14539 case DW_AT_artificial:
14540 return "DW_AT_artificial";
14541 case DW_AT_base_types:
14542 return "DW_AT_base_types";
14543 case DW_AT_calling_convention:
14544 return "DW_AT_calling_convention";
14545 case DW_AT_count:
14546 return "DW_AT_count";
14547 case DW_AT_data_member_location:
14548 return "DW_AT_data_member_location";
14549 case DW_AT_decl_column:
14550 return "DW_AT_decl_column";
14551 case DW_AT_decl_file:
14552 return "DW_AT_decl_file";
14553 case DW_AT_decl_line:
14554 return "DW_AT_decl_line";
14555 case DW_AT_declaration:
14556 return "DW_AT_declaration";
14557 case DW_AT_discr_list:
14558 return "DW_AT_discr_list";
14559 case DW_AT_encoding:
14560 return "DW_AT_encoding";
14561 case DW_AT_external:
14562 return "DW_AT_external";
14563 case DW_AT_frame_base:
14564 return "DW_AT_frame_base";
14565 case DW_AT_friend:
14566 return "DW_AT_friend";
14567 case DW_AT_identifier_case:
14568 return "DW_AT_identifier_case";
14569 case DW_AT_macro_info:
14570 return "DW_AT_macro_info";
14571 case DW_AT_namelist_items:
14572 return "DW_AT_namelist_items";
14573 case DW_AT_priority:
14574 return "DW_AT_priority";
14575 case DW_AT_segment:
14576 return "DW_AT_segment";
14577 case DW_AT_specification:
14578 return "DW_AT_specification";
14579 case DW_AT_static_link:
14580 return "DW_AT_static_link";
14581 case DW_AT_type:
14582 return "DW_AT_type";
14583 case DW_AT_use_location:
14584 return "DW_AT_use_location";
14585 case DW_AT_variable_parameter:
14586 return "DW_AT_variable_parameter";
14587 case DW_AT_virtuality:
14588 return "DW_AT_virtuality";
14589 case DW_AT_vtable_elem_location:
14590 return "DW_AT_vtable_elem_location";
14591 /* DWARF 3 values. */
14592 case DW_AT_allocated:
14593 return "DW_AT_allocated";
14594 case DW_AT_associated:
14595 return "DW_AT_associated";
14596 case DW_AT_data_location:
14597 return "DW_AT_data_location";
14598 case DW_AT_byte_stride:
14599 return "DW_AT_byte_stride";
14600 case DW_AT_entry_pc:
14601 return "DW_AT_entry_pc";
14602 case DW_AT_use_UTF8:
14603 return "DW_AT_use_UTF8";
14604 case DW_AT_extension:
14605 return "DW_AT_extension";
14606 case DW_AT_ranges:
14607 return "DW_AT_ranges";
14608 case DW_AT_trampoline:
14609 return "DW_AT_trampoline";
14610 case DW_AT_call_column:
14611 return "DW_AT_call_column";
14612 case DW_AT_call_file:
14613 return "DW_AT_call_file";
14614 case DW_AT_call_line:
14615 return "DW_AT_call_line";
14616 case DW_AT_description:
14617 return "DW_AT_description";
14618 case DW_AT_binary_scale:
14619 return "DW_AT_binary_scale";
14620 case DW_AT_decimal_scale:
14621 return "DW_AT_decimal_scale";
14622 case DW_AT_small:
14623 return "DW_AT_small";
14624 case DW_AT_decimal_sign:
14625 return "DW_AT_decimal_sign";
14626 case DW_AT_digit_count:
14627 return "DW_AT_digit_count";
14628 case DW_AT_picture_string:
14629 return "DW_AT_picture_string";
14630 case DW_AT_mutable:
14631 return "DW_AT_mutable";
14632 case DW_AT_threads_scaled:
14633 return "DW_AT_threads_scaled";
14634 case DW_AT_explicit:
14635 return "DW_AT_explicit";
14636 case DW_AT_object_pointer:
14637 return "DW_AT_object_pointer";
14638 case DW_AT_endianity:
14639 return "DW_AT_endianity";
14640 case DW_AT_elemental:
14641 return "DW_AT_elemental";
14642 case DW_AT_pure:
14643 return "DW_AT_pure";
14644 case DW_AT_recursive:
14645 return "DW_AT_recursive";
14646 /* DWARF 4 values. */
14647 case DW_AT_signature:
14648 return "DW_AT_signature";
14649 case DW_AT_linkage_name:
14650 return "DW_AT_linkage_name";
14651 /* Tentative Fission values. */
14652 case DW_AT_GNU_dwo_name:
14653 return "DW_AT_GNU_dwo_name";
14654 case DW_AT_GNU_dwo_id:
14655 return "DW_AT_GNU_dwo_id";
14656 case DW_AT_GNU_addr_base:
14657 return "DW_AT_GNU_addr_base";
14658 case DW_AT_GNU_pubnames:
14659 return "DW_AT_GNU_pubnames";
14660 case DW_AT_GNU_pubtypes:
14661 return "DW_AT_GNU_pubtypes";
14662 /* SGI/MIPS extensions. */
14663#ifdef MIPS /* collides with DW_AT_HP_block_index */
14664 case DW_AT_MIPS_fde:
14665 return "DW_AT_MIPS_fde";
14666#endif
14667 case DW_AT_MIPS_loop_begin:
14668 return "DW_AT_MIPS_loop_begin";
14669 case DW_AT_MIPS_tail_loop_begin:
14670 return "DW_AT_MIPS_tail_loop_begin";
14671 case DW_AT_MIPS_epilog_begin:
14672 return "DW_AT_MIPS_epilog_begin";
14673 case DW_AT_MIPS_loop_unroll_factor:
14674 return "DW_AT_MIPS_loop_unroll_factor";
14675 case DW_AT_MIPS_software_pipeline_depth:
14676 return "DW_AT_MIPS_software_pipeline_depth";
14677 case DW_AT_MIPS_linkage_name:
14678 return "DW_AT_MIPS_linkage_name";
14679 case DW_AT_MIPS_stride:
14680 return "DW_AT_MIPS_stride";
14681 case DW_AT_MIPS_abstract_name:
14682 return "DW_AT_MIPS_abstract_name";
14683 case DW_AT_MIPS_clone_origin:
14684 return "DW_AT_MIPS_clone_origin";
14685 case DW_AT_MIPS_has_inlines:
14686 return "DW_AT_MIPS_has_inlines";
14687 /* HP extensions. */
14688#ifndef MIPS /* collides with DW_AT_MIPS_fde */
14689 case DW_AT_HP_block_index:
14690 return "DW_AT_HP_block_index";
14691#endif
14692 case DW_AT_HP_unmodifiable:
14693 return "DW_AT_HP_unmodifiable";
14694 case DW_AT_HP_actuals_stmt_list:
14695 return "DW_AT_HP_actuals_stmt_list";
14696 case DW_AT_HP_proc_per_section:
14697 return "DW_AT_HP_proc_per_section";
14698 case DW_AT_HP_raw_data_ptr:
14699 return "DW_AT_HP_raw_data_ptr";
14700 case DW_AT_HP_pass_by_reference:
14701 return "DW_AT_HP_pass_by_reference";
14702 case DW_AT_HP_opt_level:
14703 return "DW_AT_HP_opt_level";
14704 case DW_AT_HP_prof_version_id:
14705 return "DW_AT_HP_prof_version_id";
14706 case DW_AT_HP_opt_flags:
14707 return "DW_AT_HP_opt_flags";
14708 case DW_AT_HP_cold_region_low_pc:
14709 return "DW_AT_HP_cold_region_low_pc";
14710 case DW_AT_HP_cold_region_high_pc:
14711 return "DW_AT_HP_cold_region_high_pc";
14712 case DW_AT_HP_all_variables_modifiable:
14713 return "DW_AT_HP_all_variables_modifiable";
14714 case DW_AT_HP_linkage_name:
14715 return "DW_AT_HP_linkage_name";
14716 case DW_AT_HP_prof_flags:
14717 return "DW_AT_HP_prof_flags";
14718 /* GNU extensions. */
14719 case DW_AT_sf_names:
14720 return "DW_AT_sf_names";
14721 case DW_AT_src_info:
14722 return "DW_AT_src_info";
14723 case DW_AT_mac_info:
14724 return "DW_AT_mac_info";
14725 case DW_AT_src_coords:
14726 return "DW_AT_src_coords";
14727 case DW_AT_body_begin:
14728 return "DW_AT_body_begin";
14729 case DW_AT_body_end:
14730 return "DW_AT_body_end";
14731 case DW_AT_GNU_vector:
14732 return "DW_AT_GNU_vector";
14733 case DW_AT_GNU_odr_signature:
14734 return "DW_AT_GNU_odr_signature";
14735 /* VMS extensions. */
14736 case DW_AT_VMS_rtnbeg_pd_address:
14737 return "DW_AT_VMS_rtnbeg_pd_address";
14738 /* UPC extension. */
14739 case DW_AT_upc_threads_scaled:
14740 return "DW_AT_upc_threads_scaled";
14741 /* PGI (STMicroelectronics) extensions. */
14742 case DW_AT_PGI_lbase:
14743 return "DW_AT_PGI_lbase";
14744 case DW_AT_PGI_soffset:
14745 return "DW_AT_PGI_soffset";
14746 case DW_AT_PGI_lstride:
14747 return "DW_AT_PGI_lstride";
14748 default:
14749 return "DW_AT_<unknown>";
14750 }
14751}
14752
14753/* Convert a DWARF value form code into its string name. */
14754
14755static char *
14756dwarf_form_name (unsigned form)
14757{
14758 switch (form)
14759 {
14760 case DW_FORM_addr:
14761 return "DW_FORM_addr";
14762 case DW_FORM_block2:
14763 return "DW_FORM_block2";
14764 case DW_FORM_block4:
14765 return "DW_FORM_block4";
14766 case DW_FORM_data2:
14767 return "DW_FORM_data2";
14768 case DW_FORM_data4:
14769 return "DW_FORM_data4";
14770 case DW_FORM_data8:
14771 return "DW_FORM_data8";
14772 case DW_FORM_string:
14773 return "DW_FORM_string";
14774 case DW_FORM_block:
14775 return "DW_FORM_block";
14776 case DW_FORM_block1:
14777 return "DW_FORM_block1";
14778 case DW_FORM_data1:
14779 return "DW_FORM_data1";
14780 case DW_FORM_flag:
14781 return "DW_FORM_flag";
14782 case DW_FORM_sdata:
14783 return "DW_FORM_sdata";
14784 case DW_FORM_strp:
14785 return "DW_FORM_strp";
14786 case DW_FORM_udata:
14787 return "DW_FORM_udata";
14788 case DW_FORM_ref_addr:
14789 return "DW_FORM_ref_addr";
14790 case DW_FORM_ref1:
14791 return "DW_FORM_ref1";
14792 case DW_FORM_ref2:
14793 return "DW_FORM_ref2";
14794 case DW_FORM_ref4:
14795 return "DW_FORM_ref4";
14796 case DW_FORM_ref8:
14797 return "DW_FORM_ref8";
14798 case DW_FORM_ref_udata:
14799 return "DW_FORM_ref_udata";
14800 case DW_FORM_indirect:
14801 return "DW_FORM_indirect";
14802 case DW_FORM_sec_offset:
14803 return "DW_FORM_sec_offset";
14804 case DW_FORM_exprloc:
14805 return "DW_FORM_exprloc";
14806 case DW_FORM_flag_present:
14807 return "DW_FORM_flag_present";
14808 case DW_FORM_ref_sig8:
14809 return "DW_FORM_ref_sig8";
14810 case DW_FORM_GNU_addr_index:
14811 return "DW_FORM_GNU_addr_index";
14812 case DW_FORM_GNU_str_index:
14813 return "DW_FORM_GNU_str_index";
14814 default:
14815 return "DW_FORM_<unknown>";
14816 }
14817}
14818
14819/* Convert a DWARF stack opcode into its string name. */
14820
14821const char *
14822dwarf_stack_op_name (unsigned op)
14823{
14824 switch (op)
14825 {
14826 case DW_OP_addr:
14827 return "DW_OP_addr";
14828 case DW_OP_deref:
14829 return "DW_OP_deref";
14830 case DW_OP_const1u:
14831 return "DW_OP_const1u";
14832 case DW_OP_const1s:
14833 return "DW_OP_const1s";
14834 case DW_OP_const2u:
14835 return "DW_OP_const2u";
14836 case DW_OP_const2s:
14837 return "DW_OP_const2s";
14838 case DW_OP_const4u:
14839 return "DW_OP_const4u";
14840 case DW_OP_const4s:
14841 return "DW_OP_const4s";
14842 case DW_OP_const8u:
14843 return "DW_OP_const8u";
14844 case DW_OP_const8s:
14845 return "DW_OP_const8s";
14846 case DW_OP_constu:
14847 return "DW_OP_constu";
14848 case DW_OP_consts:
14849 return "DW_OP_consts";
14850 case DW_OP_dup:
14851 return "DW_OP_dup";
14852 case DW_OP_drop:
14853 return "DW_OP_drop";
14854 case DW_OP_over:
14855 return "DW_OP_over";
14856 case DW_OP_pick:
14857 return "DW_OP_pick";
14858 case DW_OP_swap:
14859 return "DW_OP_swap";
14860 case DW_OP_rot:
14861 return "DW_OP_rot";
14862 case DW_OP_xderef:
14863 return "DW_OP_xderef";
14864 case DW_OP_abs:
14865 return "DW_OP_abs";
14866 case DW_OP_and:
14867 return "DW_OP_and";
14868 case DW_OP_div:
14869 return "DW_OP_div";
14870 case DW_OP_minus:
14871 return "DW_OP_minus";
14872 case DW_OP_mod:
14873 return "DW_OP_mod";
14874 case DW_OP_mul:
14875 return "DW_OP_mul";
14876 case DW_OP_neg:
14877 return "DW_OP_neg";
14878 case DW_OP_not:
14879 return "DW_OP_not";
14880 case DW_OP_or:
14881 return "DW_OP_or";
14882 case DW_OP_plus:
14883 return "DW_OP_plus";
14884 case DW_OP_plus_uconst:
14885 return "DW_OP_plus_uconst";
14886 case DW_OP_shl:
14887 return "DW_OP_shl";
14888 case DW_OP_shr:
14889 return "DW_OP_shr";
14890 case DW_OP_shra:
14891 return "DW_OP_shra";
14892 case DW_OP_xor:
14893 return "DW_OP_xor";
14894 case DW_OP_bra:
14895 return "DW_OP_bra";
14896 case DW_OP_eq:
14897 return "DW_OP_eq";
14898 case DW_OP_ge:
14899 return "DW_OP_ge";
14900 case DW_OP_gt:
14901 return "DW_OP_gt";
14902 case DW_OP_le:
14903 return "DW_OP_le";
14904 case DW_OP_lt:
14905 return "DW_OP_lt";
14906 case DW_OP_ne:
14907 return "DW_OP_ne";
14908 case DW_OP_skip:
14909 return "DW_OP_skip";
14910 case DW_OP_lit0:
14911 return "DW_OP_lit0";
14912 case DW_OP_lit1:
14913 return "DW_OP_lit1";
14914 case DW_OP_lit2:
14915 return "DW_OP_lit2";
14916 case DW_OP_lit3:
14917 return "DW_OP_lit3";
14918 case DW_OP_lit4:
14919 return "DW_OP_lit4";
14920 case DW_OP_lit5:
14921 return "DW_OP_lit5";
14922 case DW_OP_lit6:
14923 return "DW_OP_lit6";
14924 case DW_OP_lit7:
14925 return "DW_OP_lit7";
14926 case DW_OP_lit8:
14927 return "DW_OP_lit8";
14928 case DW_OP_lit9:
14929 return "DW_OP_lit9";
14930 case DW_OP_lit10:
14931 return "DW_OP_lit10";
14932 case DW_OP_lit11:
14933 return "DW_OP_lit11";
14934 case DW_OP_lit12:
14935 return "DW_OP_lit12";
14936 case DW_OP_lit13:
14937 return "DW_OP_lit13";
14938 case DW_OP_lit14:
14939 return "DW_OP_lit14";
14940 case DW_OP_lit15:
14941 return "DW_OP_lit15";
14942 case DW_OP_lit16:
14943 return "DW_OP_lit16";
14944 case DW_OP_lit17:
14945 return "DW_OP_lit17";
14946 case DW_OP_lit18:
14947 return "DW_OP_lit18";
14948 case DW_OP_lit19:
14949 return "DW_OP_lit19";
14950 case DW_OP_lit20:
14951 return "DW_OP_lit20";
14952 case DW_OP_lit21:
14953 return "DW_OP_lit21";
14954 case DW_OP_lit22:
14955 return "DW_OP_lit22";
14956 case DW_OP_lit23:
14957 return "DW_OP_lit23";
14958 case DW_OP_lit24:
14959 return "DW_OP_lit24";
14960 case DW_OP_lit25:
14961 return "DW_OP_lit25";
14962 case DW_OP_lit26:
14963 return "DW_OP_lit26";
14964 case DW_OP_lit27:
14965 return "DW_OP_lit27";
14966 case DW_OP_lit28:
14967 return "DW_OP_lit28";
14968 case DW_OP_lit29:
14969 return "DW_OP_lit29";
14970 case DW_OP_lit30:
14971 return "DW_OP_lit30";
14972 case DW_OP_lit31:
14973 return "DW_OP_lit31";
14974 case DW_OP_reg0:
14975 return "DW_OP_reg0";
14976 case DW_OP_reg1:
14977 return "DW_OP_reg1";
14978 case DW_OP_reg2:
14979 return "DW_OP_reg2";
14980 case DW_OP_reg3:
14981 return "DW_OP_reg3";
14982 case DW_OP_reg4:
14983 return "DW_OP_reg4";
14984 case DW_OP_reg5:
14985 return "DW_OP_reg5";
14986 case DW_OP_reg6:
14987 return "DW_OP_reg6";
14988 case DW_OP_reg7:
14989 return "DW_OP_reg7";
14990 case DW_OP_reg8:
14991 return "DW_OP_reg8";
14992 case DW_OP_reg9:
14993 return "DW_OP_reg9";
14994 case DW_OP_reg10:
14995 return "DW_OP_reg10";
14996 case DW_OP_reg11:
14997 return "DW_OP_reg11";
14998 case DW_OP_reg12:
14999 return "DW_OP_reg12";
15000 case DW_OP_reg13:
15001 return "DW_OP_reg13";
15002 case DW_OP_reg14:
15003 return "DW_OP_reg14";
15004 case DW_OP_reg15:
15005 return "DW_OP_reg15";
15006 case DW_OP_reg16:
15007 return "DW_OP_reg16";
15008 case DW_OP_reg17:
15009 return "DW_OP_reg17";
15010 case DW_OP_reg18:
15011 return "DW_OP_reg18";
15012 case DW_OP_reg19:
15013 return "DW_OP_reg19";
15014 case DW_OP_reg20:
15015 return "DW_OP_reg20";
15016 case DW_OP_reg21:
15017 return "DW_OP_reg21";
15018 case DW_OP_reg22:
15019 return "DW_OP_reg22";
15020 case DW_OP_reg23:
15021 return "DW_OP_reg23";
15022 case DW_OP_reg24:
15023 return "DW_OP_reg24";
15024 case DW_OP_reg25:
15025 return "DW_OP_reg25";
15026 case DW_OP_reg26:
15027 return "DW_OP_reg26";
15028 case DW_OP_reg27:
15029 return "DW_OP_reg27";
15030 case DW_OP_reg28:
15031 return "DW_OP_reg28";
15032 case DW_OP_reg29:
15033 return "DW_OP_reg29";
15034 case DW_OP_reg30:
15035 return "DW_OP_reg30";
15036 case DW_OP_reg31:
15037 return "DW_OP_reg31";
15038 case DW_OP_breg0:
15039 return "DW_OP_breg0";
15040 case DW_OP_breg1:
15041 return "DW_OP_breg1";
15042 case DW_OP_breg2:
15043 return "DW_OP_breg2";
15044 case DW_OP_breg3:
15045 return "DW_OP_breg3";
15046 case DW_OP_breg4:
15047 return "DW_OP_breg4";
15048 case DW_OP_breg5:
15049 return "DW_OP_breg5";
15050 case DW_OP_breg6:
15051 return "DW_OP_breg6";
15052 case DW_OP_breg7:
15053 return "DW_OP_breg7";
15054 case DW_OP_breg8:
15055 return "DW_OP_breg8";
15056 case DW_OP_breg9:
15057 return "DW_OP_breg9";
15058 case DW_OP_breg10:
15059 return "DW_OP_breg10";
15060 case DW_OP_breg11:
15061 return "DW_OP_breg11";
15062 case DW_OP_breg12:
15063 return "DW_OP_breg12";
15064 case DW_OP_breg13:
15065 return "DW_OP_breg13";
15066 case DW_OP_breg14:
15067 return "DW_OP_breg14";
15068 case DW_OP_breg15:
15069 return "DW_OP_breg15";
15070 case DW_OP_breg16:
15071 return "DW_OP_breg16";
15072 case DW_OP_breg17:
15073 return "DW_OP_breg17";
15074 case DW_OP_breg18:
15075 return "DW_OP_breg18";
15076 case DW_OP_breg19:
15077 return "DW_OP_breg19";
15078 case DW_OP_breg20:
15079 return "DW_OP_breg20";
15080 case DW_OP_breg21:
15081 return "DW_OP_breg21";
15082 case DW_OP_breg22:
15083 return "DW_OP_breg22";
15084 case DW_OP_breg23:
15085 return "DW_OP_breg23";
15086 case DW_OP_breg24:
15087 return "DW_OP_breg24";
15088 case DW_OP_breg25:
15089 return "DW_OP_breg25";
15090 case DW_OP_breg26:
15091 return "DW_OP_breg26";
15092 case DW_OP_breg27:
15093 return "DW_OP_breg27";
15094 case DW_OP_breg28:
15095 return "DW_OP_breg28";
15096 case DW_OP_breg29:
15097 return "DW_OP_breg29";
15098 case DW_OP_breg30:
15099 return "DW_OP_breg30";
15100 case DW_OP_breg31:
15101 return "DW_OP_breg31";
15102 case DW_OP_regx:
15103 return "DW_OP_regx";
15104 case DW_OP_fbreg:
15105 return "DW_OP_fbreg";
15106 case DW_OP_bregx:
15107 return "DW_OP_bregx";
15108 case DW_OP_piece:
15109 return "DW_OP_piece";
15110 case DW_OP_deref_size:
15111 return "DW_OP_deref_size";
15112 case DW_OP_xderef_size:
15113 return "DW_OP_xderef_size";
15114 case DW_OP_nop:
15115 return "DW_OP_nop";
15116 /* DWARF 3 extensions. */
15117 case DW_OP_push_object_address:
15118 return "DW_OP_push_object_address";
15119 case DW_OP_call2:
15120 return "DW_OP_call2";
15121 case DW_OP_call4:
15122 return "DW_OP_call4";
15123 case DW_OP_call_ref:
15124 return "DW_OP_call_ref";
15125 case DW_OP_form_tls_address:
15126 return "DW_OP_form_tls_address";
15127 case DW_OP_call_frame_cfa:
15128 return "DW_OP_call_frame_cfa";
15129 case DW_OP_bit_piece:
15130 return "DW_OP_bit_piece";
15131 /* DWARF 4 extensions. */
15132 case DW_OP_implicit_value:
15133 return "DW_OP_implicit_value";
15134 case DW_OP_stack_value:
15135 return "DW_OP_stack_value";
15136 /* GNU extensions. */
15137 case DW_OP_GNU_push_tls_address:
15138 return "DW_OP_GNU_push_tls_address";
15139 case DW_OP_GNU_uninit:
15140 return "DW_OP_GNU_uninit";
15141 case DW_OP_GNU_encoded_addr:
15142 return "DW_OP_GNU_encoded_addr";
15143 case DW_OP_GNU_implicit_pointer:
15144 return "DW_OP_GNU_implicit_pointer";
15145 case DW_OP_GNU_entry_value:
15146 return "DW_OP_GNU_entry_value";
15147 case DW_OP_GNU_const_type:
15148 return "DW_OP_GNU_const_type";
15149 case DW_OP_GNU_regval_type:
15150 return "DW_OP_GNU_regval_type";
15151 case DW_OP_GNU_deref_type:
15152 return "DW_OP_GNU_deref_type";
15153 case DW_OP_GNU_convert:
15154 return "DW_OP_GNU_convert";
15155 case DW_OP_GNU_reinterpret:
15156 return "DW_OP_GNU_reinterpret";
15157 case DW_OP_GNU_parameter_ref:
15158 return "DW_OP_GNU_parameter_ref";
15159 default:
15160 return NULL;
15161 }
15162}
15163
15164static char *
15165dwarf_bool_name (unsigned mybool)
15166{
15167 if (mybool)
15168 return "TRUE";
15169 else
15170 return "FALSE";
15171}
15172
15173/* Convert a DWARF type code into its string name. */
15174
15175static char *
15176dwarf_type_encoding_name (unsigned enc)
15177{
15178 switch (enc)
15179 {
15180 case DW_ATE_void:
15181 return "DW_ATE_void";
15182 case DW_ATE_address:
15183 return "DW_ATE_address";
15184 case DW_ATE_boolean:
15185 return "DW_ATE_boolean";
15186 case DW_ATE_complex_float:
15187 return "DW_ATE_complex_float";
15188 case DW_ATE_float:
15189 return "DW_ATE_float";
15190 case DW_ATE_signed:
15191 return "DW_ATE_signed";
15192 case DW_ATE_signed_char:
15193 return "DW_ATE_signed_char";
15194 case DW_ATE_unsigned:
15195 return "DW_ATE_unsigned";
15196 case DW_ATE_unsigned_char:
15197 return "DW_ATE_unsigned_char";
15198 /* DWARF 3. */
15199 case DW_ATE_imaginary_float:
15200 return "DW_ATE_imaginary_float";
15201 case DW_ATE_packed_decimal:
15202 return "DW_ATE_packed_decimal";
15203 case DW_ATE_numeric_string:
15204 return "DW_ATE_numeric_string";
15205 case DW_ATE_edited:
15206 return "DW_ATE_edited";
15207 case DW_ATE_signed_fixed:
15208 return "DW_ATE_signed_fixed";
15209 case DW_ATE_unsigned_fixed:
15210 return "DW_ATE_unsigned_fixed";
15211 case DW_ATE_decimal_float:
15212 return "DW_ATE_decimal_float";
15213 /* DWARF 4. */
15214 case DW_ATE_UTF:
15215 return "DW_ATE_UTF";
15216 /* HP extensions. */
15217 case DW_ATE_HP_float80:
15218 return "DW_ATE_HP_float80";
15219 case DW_ATE_HP_complex_float80:
15220 return "DW_ATE_HP_complex_float80";
15221 case DW_ATE_HP_float128:
15222 return "DW_ATE_HP_float128";
15223 case DW_ATE_HP_complex_float128:
15224 return "DW_ATE_HP_complex_float128";
15225 case DW_ATE_HP_floathpintel:
15226 return "DW_ATE_HP_floathpintel";
15227 case DW_ATE_HP_imaginary_float80:
15228 return "DW_ATE_HP_imaginary_float80";
15229 case DW_ATE_HP_imaginary_float128:
15230 return "DW_ATE_HP_imaginary_float128";
15231 default:
15232 return "DW_ATE_<unknown>";
15233 }
15234}
15235
15236/* Convert a DWARF call frame info operation to its string name. */
15237
15238#if 0
15239static char *
15240dwarf_cfi_name (unsigned cfi_opc)
15241{
15242 switch (cfi_opc)
15243 {
15244 case DW_CFA_advance_loc:
15245 return "DW_CFA_advance_loc";
15246 case DW_CFA_offset:
15247 return "DW_CFA_offset";
15248 case DW_CFA_restore:
15249 return "DW_CFA_restore";
15250 case DW_CFA_nop:
15251 return "DW_CFA_nop";
15252 case DW_CFA_set_loc:
15253 return "DW_CFA_set_loc";
15254 case DW_CFA_advance_loc1:
15255 return "DW_CFA_advance_loc1";
15256 case DW_CFA_advance_loc2:
15257 return "DW_CFA_advance_loc2";
15258 case DW_CFA_advance_loc4:
15259 return "DW_CFA_advance_loc4";
15260 case DW_CFA_offset_extended:
15261 return "DW_CFA_offset_extended";
15262 case DW_CFA_restore_extended:
15263 return "DW_CFA_restore_extended";
15264 case DW_CFA_undefined:
15265 return "DW_CFA_undefined";
15266 case DW_CFA_same_value:
15267 return "DW_CFA_same_value";
15268 case DW_CFA_register:
15269 return "DW_CFA_register";
15270 case DW_CFA_remember_state:
15271 return "DW_CFA_remember_state";
15272 case DW_CFA_restore_state:
15273 return "DW_CFA_restore_state";
15274 case DW_CFA_def_cfa:
15275 return "DW_CFA_def_cfa";
15276 case DW_CFA_def_cfa_register:
15277 return "DW_CFA_def_cfa_register";
15278 case DW_CFA_def_cfa_offset:
15279 return "DW_CFA_def_cfa_offset";
15280 /* DWARF 3. */
15281 case DW_CFA_def_cfa_expression:
15282 return "DW_CFA_def_cfa_expression";
15283 case DW_CFA_expression:
15284 return "DW_CFA_expression";
15285 case DW_CFA_offset_extended_sf:
15286 return "DW_CFA_offset_extended_sf";
15287 case DW_CFA_def_cfa_sf:
15288 return "DW_CFA_def_cfa_sf";
15289 case DW_CFA_def_cfa_offset_sf:
15290 return "DW_CFA_def_cfa_offset_sf";
15291 case DW_CFA_val_offset:
15292 return "DW_CFA_val_offset";
15293 case DW_CFA_val_offset_sf:
15294 return "DW_CFA_val_offset_sf";
15295 case DW_CFA_val_expression:
15296 return "DW_CFA_val_expression";
15297 /* SGI/MIPS specific. */
15298 case DW_CFA_MIPS_advance_loc8:
15299 return "DW_CFA_MIPS_advance_loc8";
15300 /* GNU extensions. */
15301 case DW_CFA_GNU_window_save:
15302 return "DW_CFA_GNU_window_save";
15303 case DW_CFA_GNU_args_size:
15304 return "DW_CFA_GNU_args_size";
15305 case DW_CFA_GNU_negative_offset_extended:
15306 return "DW_CFA_GNU_negative_offset_extended";
15307 default:
15308 return "DW_CFA_<unknown>";
15309 }
15310}
15311#endif
15312
15313static void
15314dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
15315{
15316 unsigned int i;
15317
15318 print_spaces (indent, f);
15319 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
15320 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
15321
15322 if (die->parent != NULL)
15323 {
15324 print_spaces (indent, f);
15325 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
15326 die->parent->offset.sect_off);
15327 }
15328
15329 print_spaces (indent, f);
15330 fprintf_unfiltered (f, " has children: %s\n",
15331 dwarf_bool_name (die->child != NULL));
15332
15333 print_spaces (indent, f);
15334 fprintf_unfiltered (f, " attributes:\n");
15335
15336 for (i = 0; i < die->num_attrs; ++i)
15337 {
15338 print_spaces (indent, f);
15339 fprintf_unfiltered (f, " %s (%s) ",
15340 dwarf_attr_name (die->attrs[i].name),
15341 dwarf_form_name (die->attrs[i].form));
15342
15343 switch (die->attrs[i].form)
15344 {
15345 case DW_FORM_addr:
15346 case DW_FORM_GNU_addr_index:
15347 fprintf_unfiltered (f, "address: ");
15348 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
15349 break;
15350 case DW_FORM_block2:
15351 case DW_FORM_block4:
15352 case DW_FORM_block:
15353 case DW_FORM_block1:
15354 fprintf_unfiltered (f, "block: size %d",
15355 DW_BLOCK (&die->attrs[i])->size);
15356 break;
15357 case DW_FORM_exprloc:
15358 fprintf_unfiltered (f, "expression: size %u",
15359 DW_BLOCK (&die->attrs[i])->size);
15360 break;
15361 case DW_FORM_ref_addr:
15362 fprintf_unfiltered (f, "ref address: ");
15363 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
15364 break;
15365 case DW_FORM_ref1:
15366 case DW_FORM_ref2:
15367 case DW_FORM_ref4:
15368 case DW_FORM_ref8:
15369 case DW_FORM_ref_udata:
15370 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
15371 (long) (DW_UNSND (&die->attrs[i])));
15372 break;
15373 case DW_FORM_data1:
15374 case DW_FORM_data2:
15375 case DW_FORM_data4:
15376 case DW_FORM_data8:
15377 case DW_FORM_udata:
15378 case DW_FORM_sdata:
15379 fprintf_unfiltered (f, "constant: %s",
15380 pulongest (DW_UNSND (&die->attrs[i])));
15381 break;
15382 case DW_FORM_sec_offset:
15383 fprintf_unfiltered (f, "section offset: %s",
15384 pulongest (DW_UNSND (&die->attrs[i])));
15385 break;
15386 case DW_FORM_ref_sig8:
15387 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15388 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
15389 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
15390 else
15391 fprintf_unfiltered (f, "signatured type, offset: unknown");
15392 break;
15393 case DW_FORM_string:
15394 case DW_FORM_strp:
15395 case DW_FORM_GNU_str_index:
15396 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
15397 DW_STRING (&die->attrs[i])
15398 ? DW_STRING (&die->attrs[i]) : "",
15399 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
15400 break;
15401 case DW_FORM_flag:
15402 if (DW_UNSND (&die->attrs[i]))
15403 fprintf_unfiltered (f, "flag: TRUE");
15404 else
15405 fprintf_unfiltered (f, "flag: FALSE");
15406 break;
15407 case DW_FORM_flag_present:
15408 fprintf_unfiltered (f, "flag: TRUE");
15409 break;
15410 case DW_FORM_indirect:
15411 /* The reader will have reduced the indirect form to
15412 the "base form" so this form should not occur. */
15413 fprintf_unfiltered (f,
15414 "unexpected attribute form: DW_FORM_indirect");
15415 break;
15416 default:
15417 fprintf_unfiltered (f, "unsupported attribute form: %d.",
15418 die->attrs[i].form);
15419 break;
15420 }
15421 fprintf_unfiltered (f, "\n");
15422 }
15423}
15424
15425static void
15426dump_die_for_error (struct die_info *die)
15427{
15428 dump_die_shallow (gdb_stderr, 0, die);
15429}
15430
15431static void
15432dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
15433{
15434 int indent = level * 4;
15435
15436 gdb_assert (die != NULL);
15437
15438 if (level >= max_level)
15439 return;
15440
15441 dump_die_shallow (f, indent, die);
15442
15443 if (die->child != NULL)
15444 {
15445 print_spaces (indent, f);
15446 fprintf_unfiltered (f, " Children:");
15447 if (level + 1 < max_level)
15448 {
15449 fprintf_unfiltered (f, "\n");
15450 dump_die_1 (f, level + 1, max_level, die->child);
15451 }
15452 else
15453 {
15454 fprintf_unfiltered (f,
15455 " [not printed, max nesting level reached]\n");
15456 }
15457 }
15458
15459 if (die->sibling != NULL && level > 0)
15460 {
15461 dump_die_1 (f, level, max_level, die->sibling);
15462 }
15463}
15464
15465/* This is called from the pdie macro in gdbinit.in.
15466 It's not static so gcc will keep a copy callable from gdb. */
15467
15468void
15469dump_die (struct die_info *die, int max_level)
15470{
15471 dump_die_1 (gdb_stdlog, 0, max_level, die);
15472}
15473
15474static void
15475store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
15476{
15477 void **slot;
15478
15479 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15480 INSERT);
15481
15482 *slot = die;
15483}
15484
15485/* DW_ADDR is always stored already as sect_offset; despite for the forms
15486 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15487
15488static int
15489is_ref_attr (struct attribute *attr)
15490{
15491 switch (attr->form)
15492 {
15493 case DW_FORM_ref_addr:
15494 case DW_FORM_ref1:
15495 case DW_FORM_ref2:
15496 case DW_FORM_ref4:
15497 case DW_FORM_ref8:
15498 case DW_FORM_ref_udata:
15499 return 1;
15500 default:
15501 return 0;
15502 }
15503}
15504
15505/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15506 required kind. */
15507
15508static sect_offset
15509dwarf2_get_ref_die_offset (struct attribute *attr)
15510{
15511 sect_offset retval = { DW_UNSND (attr) };
15512
15513 if (is_ref_attr (attr))
15514 return retval;
15515
15516 retval.sect_off = 0;
15517 complaint (&symfile_complaints,
15518 _("unsupported die ref attribute form: '%s'"),
15519 dwarf_form_name (attr->form));
15520 return retval;
15521}
15522
15523/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15524 * the value held by the attribute is not constant. */
15525
15526static LONGEST
15527dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15528{
15529 if (attr->form == DW_FORM_sdata)
15530 return DW_SND (attr);
15531 else if (attr->form == DW_FORM_udata
15532 || attr->form == DW_FORM_data1
15533 || attr->form == DW_FORM_data2
15534 || attr->form == DW_FORM_data4
15535 || attr->form == DW_FORM_data8)
15536 return DW_UNSND (attr);
15537 else
15538 {
15539 complaint (&symfile_complaints,
15540 _("Attribute value is not a constant (%s)"),
15541 dwarf_form_name (attr->form));
15542 return default_value;
15543 }
15544}
15545
15546/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
15547 unit and add it to our queue.
15548 The result is non-zero if PER_CU was queued, otherwise the result is zero
15549 meaning either PER_CU is already queued or it is already loaded. */
15550
15551static int
15552maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
15553 struct dwarf2_per_cu_data *per_cu)
15554{
15555 /* We may arrive here during partial symbol reading, if we need full
15556 DIEs to process an unusual case (e.g. template arguments). Do
15557 not queue PER_CU, just tell our caller to load its DIEs. */
15558 if (dwarf2_per_objfile->reading_partial_symbols)
15559 {
15560 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
15561 return 1;
15562 return 0;
15563 }
15564
15565 /* Mark the dependence relation so that we don't flush PER_CU
15566 too early. */
15567 dwarf2_add_dependence (this_cu, per_cu);
15568
15569 /* If it's already on the queue, we have nothing to do. */
15570 if (per_cu->queued)
15571 return 0;
15572
15573 /* If the compilation unit is already loaded, just mark it as
15574 used. */
15575 if (per_cu->cu != NULL)
15576 {
15577 per_cu->cu->last_used = 0;
15578 return 0;
15579 }
15580
15581 /* Add it to the queue. */
15582 queue_comp_unit (per_cu);
15583
15584 return 1;
15585}
15586
15587/* Follow reference or signature attribute ATTR of SRC_DIE.
15588 On entry *REF_CU is the CU of SRC_DIE.
15589 On exit *REF_CU is the CU of the result. */
15590
15591static struct die_info *
15592follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15593 struct dwarf2_cu **ref_cu)
15594{
15595 struct die_info *die;
15596
15597 if (is_ref_attr (attr))
15598 die = follow_die_ref (src_die, attr, ref_cu);
15599 else if (attr->form == DW_FORM_ref_sig8)
15600 die = follow_die_sig (src_die, attr, ref_cu);
15601 else
15602 {
15603 dump_die_for_error (src_die);
15604 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15605 (*ref_cu)->objfile->name);
15606 }
15607
15608 return die;
15609}
15610
15611/* Follow reference OFFSET.
15612 On entry *REF_CU is the CU of the source die referencing OFFSET.
15613 On exit *REF_CU is the CU of the result.
15614 Returns NULL if OFFSET is invalid. */
15615
15616static struct die_info *
15617follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
15618{
15619 struct die_info temp_die;
15620 struct dwarf2_cu *target_cu, *cu = *ref_cu;
15621
15622 gdb_assert (cu->per_cu != NULL);
15623
15624 target_cu = cu;
15625
15626 if (cu->per_cu->is_debug_types)
15627 {
15628 /* .debug_types CUs cannot reference anything outside their CU.
15629 If they need to, they have to reference a signatured type via
15630 DW_FORM_ref_sig8. */
15631 if (! offset_in_cu_p (&cu->header, offset))
15632 return NULL;
15633 }
15634 else if (! offset_in_cu_p (&cu->header, offset))
15635 {
15636 struct dwarf2_per_cu_data *per_cu;
15637
15638 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
15639
15640 /* If necessary, add it to the queue and load its DIEs. */
15641 if (maybe_queue_comp_unit (cu, per_cu))
15642 load_full_comp_unit (per_cu);
15643
15644 target_cu = per_cu->cu;
15645 }
15646 else if (cu->dies == NULL)
15647 {
15648 /* We're loading full DIEs during partial symbol reading. */
15649 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
15650 load_full_comp_unit (cu->per_cu);
15651 }
15652
15653 *ref_cu = target_cu;
15654 temp_die.offset = offset;
15655 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
15656}
15657
15658/* Follow reference attribute ATTR of SRC_DIE.
15659 On entry *REF_CU is the CU of SRC_DIE.
15660 On exit *REF_CU is the CU of the result. */
15661
15662static struct die_info *
15663follow_die_ref (struct die_info *src_die, struct attribute *attr,
15664 struct dwarf2_cu **ref_cu)
15665{
15666 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15667 struct dwarf2_cu *cu = *ref_cu;
15668 struct die_info *die;
15669
15670 die = follow_die_offset (offset, ref_cu);
15671 if (!die)
15672 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15673 "at 0x%x [in module %s]"),
15674 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
15675
15676 return die;
15677}
15678
15679/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15680 Returned value is intended for DW_OP_call*. Returned
15681 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
15682
15683struct dwarf2_locexpr_baton
15684dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
15685 struct dwarf2_per_cu_data *per_cu,
15686 CORE_ADDR (*get_frame_pc) (void *baton),
15687 void *baton)
15688{
15689 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
15690 struct dwarf2_cu *cu;
15691 struct die_info *die;
15692 struct attribute *attr;
15693 struct dwarf2_locexpr_baton retval;
15694
15695 dw2_setup (per_cu->objfile);
15696
15697 if (per_cu->cu == NULL)
15698 load_cu (per_cu);
15699 cu = per_cu->cu;
15700
15701 die = follow_die_offset (offset, &cu);
15702 if (!die)
15703 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15704 offset.sect_off, per_cu->objfile->name);
15705
15706 attr = dwarf2_attr (die, DW_AT_location, cu);
15707 if (!attr)
15708 {
15709 /* DWARF: "If there is no such attribute, then there is no effect.".
15710 DATA is ignored if SIZE is 0. */
15711
15712 retval.data = NULL;
15713 retval.size = 0;
15714 }
15715 else if (attr_form_is_section_offset (attr))
15716 {
15717 struct dwarf2_loclist_baton loclist_baton;
15718 CORE_ADDR pc = (*get_frame_pc) (baton);
15719 size_t size;
15720
15721 fill_in_loclist_baton (cu, &loclist_baton, attr);
15722
15723 retval.data = dwarf2_find_location_expression (&loclist_baton,
15724 &size, pc);
15725 retval.size = size;
15726 }
15727 else
15728 {
15729 if (!attr_form_is_block (attr))
15730 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15731 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15732 offset.sect_off, per_cu->objfile->name);
15733
15734 retval.data = DW_BLOCK (attr)->data;
15735 retval.size = DW_BLOCK (attr)->size;
15736 }
15737 retval.per_cu = cu->per_cu;
15738
15739 age_cached_comp_units ();
15740
15741 return retval;
15742}
15743
15744/* Return the type of the DIE at DIE_OFFSET in the CU named by
15745 PER_CU. */
15746
15747struct type *
15748dwarf2_get_die_type (cu_offset die_offset,
15749 struct dwarf2_per_cu_data *per_cu)
15750{
15751 sect_offset die_offset_sect;
15752
15753 dw2_setup (per_cu->objfile);
15754
15755 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15756 return get_die_type_at_offset (die_offset_sect, per_cu);
15757}
15758
15759/* Follow the signature attribute ATTR in SRC_DIE.
15760 On entry *REF_CU is the CU of SRC_DIE.
15761 On exit *REF_CU is the CU of the result. */
15762
15763static struct die_info *
15764follow_die_sig (struct die_info *src_die, struct attribute *attr,
15765 struct dwarf2_cu **ref_cu)
15766{
15767 struct objfile *objfile = (*ref_cu)->objfile;
15768 struct die_info temp_die;
15769 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15770 struct dwarf2_cu *sig_cu;
15771 struct die_info *die;
15772
15773 /* sig_type will be NULL if the signatured type is missing from
15774 the debug info. */
15775 if (sig_type == NULL)
15776 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15777 "at 0x%x [in module %s]"),
15778 src_die->offset.sect_off, objfile->name);
15779
15780 /* If necessary, add it to the queue and load its DIEs. */
15781
15782 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
15783 read_signatured_type (sig_type);
15784
15785 gdb_assert (sig_type->per_cu.cu != NULL);
15786
15787 sig_cu = sig_type->per_cu.cu;
15788 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15789 temp_die.offset = sig_type->type_offset_in_section;
15790 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15791 temp_die.offset.sect_off);
15792 if (die)
15793 {
15794 *ref_cu = sig_cu;
15795 return die;
15796 }
15797
15798 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15799 "from DIE at 0x%x [in module %s]"),
15800 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15801}
15802
15803/* Given an offset of a signatured type, return its signatured_type. */
15804
15805static struct signatured_type *
15806lookup_signatured_type_at_offset (struct objfile *objfile,
15807 struct dwarf2_section_info *section,
15808 sect_offset offset)
15809{
15810 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15811 unsigned int length, initial_length_size;
15812 unsigned int sig_offset;
15813 struct signatured_type find_entry, *sig_type;
15814
15815 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15816 sig_offset = (initial_length_size
15817 + 2 /*version*/
15818 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15819 + 1 /*address_size*/);
15820 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15821 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15822
15823 /* This is only used to lookup previously recorded types.
15824 If we didn't find it, it's our bug. */
15825 gdb_assert (sig_type != NULL);
15826 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15827
15828 return sig_type;
15829}
15830
15831/* Load the DIEs associated with type unit PER_CU into memory. */
15832
15833static void
15834load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15835{
15836 struct objfile *objfile = per_cu->objfile;
15837 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15838 sect_offset offset = per_cu->offset;
15839 struct signatured_type *sig_type;
15840
15841 dwarf2_read_section (objfile, sect);
15842
15843 /* We have the section offset, but we need the signature to do the
15844 hash table lookup. */
15845 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15846 the signature to assert we found the right one.
15847 Ok, but it's a lot of work. We should simplify things so any needed
15848 assert doesn't require all this clumsiness. */
15849 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15850
15851 gdb_assert (&sig_type->per_cu == per_cu);
15852 gdb_assert (sig_type->per_cu.cu == NULL);
15853
15854 read_signatured_type (sig_type);
15855
15856 gdb_assert (sig_type->per_cu.cu != NULL);
15857}
15858
15859/* die_reader_func for read_signatured_type.
15860 This is identical to load_full_comp_unit_reader,
15861 but is kept separate for now. */
15862
15863static void
15864read_signatured_type_reader (const struct die_reader_specs *reader,
15865 gdb_byte *info_ptr,
15866 struct die_info *comp_unit_die,
15867 int has_children,
15868 void *data)
15869{
15870 struct dwarf2_cu *cu = reader->cu;
15871 struct attribute *attr;
15872
15873 gdb_assert (cu->die_hash == NULL);
15874 cu->die_hash =
15875 htab_create_alloc_ex (cu->header.length / 12,
15876 die_hash,
15877 die_eq,
15878 NULL,
15879 &cu->comp_unit_obstack,
15880 hashtab_obstack_allocate,
15881 dummy_obstack_deallocate);
15882
15883 if (has_children)
15884 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15885 &info_ptr, comp_unit_die);
15886 cu->dies = comp_unit_die;
15887 /* comp_unit_die is not stored in die_hash, no need. */
15888
15889 /* We try not to read any attributes in this function, because not
15890 all CUs needed for references have been loaded yet, and symbol
15891 table processing isn't initialized. But we have to set the CU language,
15892 or we won't be able to build types correctly.
15893 Similarly, if we do not read the producer, we can not apply
15894 producer-specific interpretation. */
15895 prepare_one_comp_unit (cu, cu->dies);
15896}
15897
15898/* Read in a signatured type and build its CU and DIEs.
15899 If the type is a stub for the real type in a DWO file,
15900 read in the real type from the DWO file as well. */
15901
15902static void
15903read_signatured_type (struct signatured_type *sig_type)
15904{
15905 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15906
15907 gdb_assert (per_cu->is_debug_types);
15908 gdb_assert (per_cu->cu == NULL);
15909
15910 init_tu_and_read_dies (per_cu, 1, read_signatured_type_reader, NULL);
15911}
15912
15913/* Decode simple location descriptions.
15914 Given a pointer to a dwarf block that defines a location, compute
15915 the location and return the value.
15916
15917 NOTE drow/2003-11-18: This function is called in two situations
15918 now: for the address of static or global variables (partial symbols
15919 only) and for offsets into structures which are expected to be
15920 (more or less) constant. The partial symbol case should go away,
15921 and only the constant case should remain. That will let this
15922 function complain more accurately. A few special modes are allowed
15923 without complaint for global variables (for instance, global
15924 register values and thread-local values).
15925
15926 A location description containing no operations indicates that the
15927 object is optimized out. The return value is 0 for that case.
15928 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15929 callers will only want a very basic result and this can become a
15930 complaint.
15931
15932 Note that stack[0] is unused except as a default error return. */
15933
15934static CORE_ADDR
15935decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15936{
15937 struct objfile *objfile = cu->objfile;
15938 int i;
15939 int size = blk->size;
15940 gdb_byte *data = blk->data;
15941 CORE_ADDR stack[64];
15942 int stacki;
15943 unsigned int bytes_read, unsnd;
15944 gdb_byte op;
15945
15946 i = 0;
15947 stacki = 0;
15948 stack[stacki] = 0;
15949 stack[++stacki] = 0;
15950
15951 while (i < size)
15952 {
15953 op = data[i++];
15954 switch (op)
15955 {
15956 case DW_OP_lit0:
15957 case DW_OP_lit1:
15958 case DW_OP_lit2:
15959 case DW_OP_lit3:
15960 case DW_OP_lit4:
15961 case DW_OP_lit5:
15962 case DW_OP_lit6:
15963 case DW_OP_lit7:
15964 case DW_OP_lit8:
15965 case DW_OP_lit9:
15966 case DW_OP_lit10:
15967 case DW_OP_lit11:
15968 case DW_OP_lit12:
15969 case DW_OP_lit13:
15970 case DW_OP_lit14:
15971 case DW_OP_lit15:
15972 case DW_OP_lit16:
15973 case DW_OP_lit17:
15974 case DW_OP_lit18:
15975 case DW_OP_lit19:
15976 case DW_OP_lit20:
15977 case DW_OP_lit21:
15978 case DW_OP_lit22:
15979 case DW_OP_lit23:
15980 case DW_OP_lit24:
15981 case DW_OP_lit25:
15982 case DW_OP_lit26:
15983 case DW_OP_lit27:
15984 case DW_OP_lit28:
15985 case DW_OP_lit29:
15986 case DW_OP_lit30:
15987 case DW_OP_lit31:
15988 stack[++stacki] = op - DW_OP_lit0;
15989 break;
15990
15991 case DW_OP_reg0:
15992 case DW_OP_reg1:
15993 case DW_OP_reg2:
15994 case DW_OP_reg3:
15995 case DW_OP_reg4:
15996 case DW_OP_reg5:
15997 case DW_OP_reg6:
15998 case DW_OP_reg7:
15999 case DW_OP_reg8:
16000 case DW_OP_reg9:
16001 case DW_OP_reg10:
16002 case DW_OP_reg11:
16003 case DW_OP_reg12:
16004 case DW_OP_reg13:
16005 case DW_OP_reg14:
16006 case DW_OP_reg15:
16007 case DW_OP_reg16:
16008 case DW_OP_reg17:
16009 case DW_OP_reg18:
16010 case DW_OP_reg19:
16011 case DW_OP_reg20:
16012 case DW_OP_reg21:
16013 case DW_OP_reg22:
16014 case DW_OP_reg23:
16015 case DW_OP_reg24:
16016 case DW_OP_reg25:
16017 case DW_OP_reg26:
16018 case DW_OP_reg27:
16019 case DW_OP_reg28:
16020 case DW_OP_reg29:
16021 case DW_OP_reg30:
16022 case DW_OP_reg31:
16023 stack[++stacki] = op - DW_OP_reg0;
16024 if (i < size)
16025 dwarf2_complex_location_expr_complaint ();
16026 break;
16027
16028 case DW_OP_regx:
16029 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16030 i += bytes_read;
16031 stack[++stacki] = unsnd;
16032 if (i < size)
16033 dwarf2_complex_location_expr_complaint ();
16034 break;
16035
16036 case DW_OP_addr:
16037 stack[++stacki] = read_address (objfile->obfd, &data[i],
16038 cu, &bytes_read);
16039 i += bytes_read;
16040 break;
16041
16042 case DW_OP_const1u:
16043 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16044 i += 1;
16045 break;
16046
16047 case DW_OP_const1s:
16048 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16049 i += 1;
16050 break;
16051
16052 case DW_OP_const2u:
16053 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16054 i += 2;
16055 break;
16056
16057 case DW_OP_const2s:
16058 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16059 i += 2;
16060 break;
16061
16062 case DW_OP_const4u:
16063 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16064 i += 4;
16065 break;
16066
16067 case DW_OP_const4s:
16068 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16069 i += 4;
16070 break;
16071
16072 case DW_OP_const8u:
16073 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16074 i += 8;
16075 break;
16076
16077 case DW_OP_constu:
16078 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16079 &bytes_read);
16080 i += bytes_read;
16081 break;
16082
16083 case DW_OP_consts:
16084 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16085 i += bytes_read;
16086 break;
16087
16088 case DW_OP_dup:
16089 stack[stacki + 1] = stack[stacki];
16090 stacki++;
16091 break;
16092
16093 case DW_OP_plus:
16094 stack[stacki - 1] += stack[stacki];
16095 stacki--;
16096 break;
16097
16098 case DW_OP_plus_uconst:
16099 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16100 &bytes_read);
16101 i += bytes_read;
16102 break;
16103
16104 case DW_OP_minus:
16105 stack[stacki - 1] -= stack[stacki];
16106 stacki--;
16107 break;
16108
16109 case DW_OP_deref:
16110 /* If we're not the last op, then we definitely can't encode
16111 this using GDB's address_class enum. This is valid for partial
16112 global symbols, although the variable's address will be bogus
16113 in the psymtab. */
16114 if (i < size)
16115 dwarf2_complex_location_expr_complaint ();
16116 break;
16117
16118 case DW_OP_GNU_push_tls_address:
16119 /* The top of the stack has the offset from the beginning
16120 of the thread control block at which the variable is located. */
16121 /* Nothing should follow this operator, so the top of stack would
16122 be returned. */
16123 /* This is valid for partial global symbols, but the variable's
16124 address will be bogus in the psymtab. Make it always at least
16125 non-zero to not look as a variable garbage collected by linker
16126 which have DW_OP_addr 0. */
16127 if (i < size)
16128 dwarf2_complex_location_expr_complaint ();
16129 stack[stacki]++;
16130 break;
16131
16132 case DW_OP_GNU_uninit:
16133 break;
16134
16135 case DW_OP_GNU_addr_index:
16136 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16137 &bytes_read);
16138 i += bytes_read;
16139 break;
16140
16141 default:
16142 {
16143 const char *name = dwarf_stack_op_name (op);
16144
16145 if (name)
16146 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16147 name);
16148 else
16149 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16150 op);
16151 }
16152
16153 return (stack[stacki]);
16154 }
16155
16156 /* Enforce maximum stack depth of SIZE-1 to avoid writing
16157 outside of the allocated space. Also enforce minimum>0. */
16158 if (stacki >= ARRAY_SIZE (stack) - 1)
16159 {
16160 complaint (&symfile_complaints,
16161 _("location description stack overflow"));
16162 return 0;
16163 }
16164
16165 if (stacki <= 0)
16166 {
16167 complaint (&symfile_complaints,
16168 _("location description stack underflow"));
16169 return 0;
16170 }
16171 }
16172 return (stack[stacki]);
16173}
16174
16175/* memory allocation interface */
16176
16177static struct dwarf_block *
16178dwarf_alloc_block (struct dwarf2_cu *cu)
16179{
16180 struct dwarf_block *blk;
16181
16182 blk = (struct dwarf_block *)
16183 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
16184 return (blk);
16185}
16186
16187static struct abbrev_info *
16188dwarf_alloc_abbrev (struct dwarf2_cu *cu)
16189{
16190 struct abbrev_info *abbrev;
16191
16192 abbrev = (struct abbrev_info *)
16193 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
16194 memset (abbrev, 0, sizeof (struct abbrev_info));
16195 return (abbrev);
16196}
16197
16198static struct die_info *
16199dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
16200{
16201 struct die_info *die;
16202 size_t size = sizeof (struct die_info);
16203
16204 if (num_attrs > 1)
16205 size += (num_attrs - 1) * sizeof (struct attribute);
16206
16207 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
16208 memset (die, 0, sizeof (struct die_info));
16209 return (die);
16210}
16211
16212\f
16213/* Macro support. */
16214
16215/* Return the full name of file number I in *LH's file name table.
16216 Use COMP_DIR as the name of the current directory of the
16217 compilation. The result is allocated using xmalloc; the caller is
16218 responsible for freeing it. */
16219static char *
16220file_full_name (int file, struct line_header *lh, const char *comp_dir)
16221{
16222 /* Is the file number a valid index into the line header's file name
16223 table? Remember that file numbers start with one, not zero. */
16224 if (1 <= file && file <= lh->num_file_names)
16225 {
16226 struct file_entry *fe = &lh->file_names[file - 1];
16227
16228 if (IS_ABSOLUTE_PATH (fe->name))
16229 return xstrdup (fe->name);
16230 else
16231 {
16232 const char *dir;
16233 int dir_len;
16234 char *full_name;
16235
16236 if (fe->dir_index)
16237 dir = lh->include_dirs[fe->dir_index - 1];
16238 else
16239 dir = comp_dir;
16240
16241 if (dir)
16242 {
16243 dir_len = strlen (dir);
16244 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
16245 strcpy (full_name, dir);
16246 full_name[dir_len] = '/';
16247 strcpy (full_name + dir_len + 1, fe->name);
16248 return full_name;
16249 }
16250 else
16251 return xstrdup (fe->name);
16252 }
16253 }
16254 else
16255 {
16256 /* The compiler produced a bogus file number. We can at least
16257 record the macro definitions made in the file, even if we
16258 won't be able to find the file by name. */
16259 char fake_name[80];
16260
16261 sprintf (fake_name, "<bad macro file number %d>", file);
16262
16263 complaint (&symfile_complaints,
16264 _("bad file number in macro information (%d)"),
16265 file);
16266
16267 return xstrdup (fake_name);
16268 }
16269}
16270
16271
16272static struct macro_source_file *
16273macro_start_file (int file, int line,
16274 struct macro_source_file *current_file,
16275 const char *comp_dir,
16276 struct line_header *lh, struct objfile *objfile)
16277{
16278 /* The full name of this source file. */
16279 char *full_name = file_full_name (file, lh, comp_dir);
16280
16281 /* We don't create a macro table for this compilation unit
16282 at all until we actually get a filename. */
16283 if (! pending_macros)
16284 pending_macros = new_macro_table (&objfile->objfile_obstack,
16285 objfile->macro_cache);
16286
16287 if (! current_file)
16288 /* If we have no current file, then this must be the start_file
16289 directive for the compilation unit's main source file. */
16290 current_file = macro_set_main (pending_macros, full_name);
16291 else
16292 current_file = macro_include (current_file, line, full_name);
16293
16294 xfree (full_name);
16295
16296 return current_file;
16297}
16298
16299
16300/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
16301 followed by a null byte. */
16302static char *
16303copy_string (const char *buf, int len)
16304{
16305 char *s = xmalloc (len + 1);
16306
16307 memcpy (s, buf, len);
16308 s[len] = '\0';
16309 return s;
16310}
16311
16312
16313static const char *
16314consume_improper_spaces (const char *p, const char *body)
16315{
16316 if (*p == ' ')
16317 {
16318 complaint (&symfile_complaints,
16319 _("macro definition contains spaces "
16320 "in formal argument list:\n`%s'"),
16321 body);
16322
16323 while (*p == ' ')
16324 p++;
16325 }
16326
16327 return p;
16328}
16329
16330
16331static void
16332parse_macro_definition (struct macro_source_file *file, int line,
16333 const char *body)
16334{
16335 const char *p;
16336
16337 /* The body string takes one of two forms. For object-like macro
16338 definitions, it should be:
16339
16340 <macro name> " " <definition>
16341
16342 For function-like macro definitions, it should be:
16343
16344 <macro name> "() " <definition>
16345 or
16346 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
16347
16348 Spaces may appear only where explicitly indicated, and in the
16349 <definition>.
16350
16351 The Dwarf 2 spec says that an object-like macro's name is always
16352 followed by a space, but versions of GCC around March 2002 omit
16353 the space when the macro's definition is the empty string.
16354
16355 The Dwarf 2 spec says that there should be no spaces between the
16356 formal arguments in a function-like macro's formal argument list,
16357 but versions of GCC around March 2002 include spaces after the
16358 commas. */
16359
16360
16361 /* Find the extent of the macro name. The macro name is terminated
16362 by either a space or null character (for an object-like macro) or
16363 an opening paren (for a function-like macro). */
16364 for (p = body; *p; p++)
16365 if (*p == ' ' || *p == '(')
16366 break;
16367
16368 if (*p == ' ' || *p == '\0')
16369 {
16370 /* It's an object-like macro. */
16371 int name_len = p - body;
16372 char *name = copy_string (body, name_len);
16373 const char *replacement;
16374
16375 if (*p == ' ')
16376 replacement = body + name_len + 1;
16377 else
16378 {
16379 dwarf2_macro_malformed_definition_complaint (body);
16380 replacement = body + name_len;
16381 }
16382
16383 macro_define_object (file, line, name, replacement);
16384
16385 xfree (name);
16386 }
16387 else if (*p == '(')
16388 {
16389 /* It's a function-like macro. */
16390 char *name = copy_string (body, p - body);
16391 int argc = 0;
16392 int argv_size = 1;
16393 char **argv = xmalloc (argv_size * sizeof (*argv));
16394
16395 p++;
16396
16397 p = consume_improper_spaces (p, body);
16398
16399 /* Parse the formal argument list. */
16400 while (*p && *p != ')')
16401 {
16402 /* Find the extent of the current argument name. */
16403 const char *arg_start = p;
16404
16405 while (*p && *p != ',' && *p != ')' && *p != ' ')
16406 p++;
16407
16408 if (! *p || p == arg_start)
16409 dwarf2_macro_malformed_definition_complaint (body);
16410 else
16411 {
16412 /* Make sure argv has room for the new argument. */
16413 if (argc >= argv_size)
16414 {
16415 argv_size *= 2;
16416 argv = xrealloc (argv, argv_size * sizeof (*argv));
16417 }
16418
16419 argv[argc++] = copy_string (arg_start, p - arg_start);
16420 }
16421
16422 p = consume_improper_spaces (p, body);
16423
16424 /* Consume the comma, if present. */
16425 if (*p == ',')
16426 {
16427 p++;
16428
16429 p = consume_improper_spaces (p, body);
16430 }
16431 }
16432
16433 if (*p == ')')
16434 {
16435 p++;
16436
16437 if (*p == ' ')
16438 /* Perfectly formed definition, no complaints. */
16439 macro_define_function (file, line, name,
16440 argc, (const char **) argv,
16441 p + 1);
16442 else if (*p == '\0')
16443 {
16444 /* Complain, but do define it. */
16445 dwarf2_macro_malformed_definition_complaint (body);
16446 macro_define_function (file, line, name,
16447 argc, (const char **) argv,
16448 p);
16449 }
16450 else
16451 /* Just complain. */
16452 dwarf2_macro_malformed_definition_complaint (body);
16453 }
16454 else
16455 /* Just complain. */
16456 dwarf2_macro_malformed_definition_complaint (body);
16457
16458 xfree (name);
16459 {
16460 int i;
16461
16462 for (i = 0; i < argc; i++)
16463 xfree (argv[i]);
16464 }
16465 xfree (argv);
16466 }
16467 else
16468 dwarf2_macro_malformed_definition_complaint (body);
16469}
16470
16471/* Skip some bytes from BYTES according to the form given in FORM.
16472 Returns the new pointer. */
16473
16474static gdb_byte *
16475skip_form_bytes (bfd *abfd, gdb_byte *bytes,
16476 enum dwarf_form form,
16477 unsigned int offset_size,
16478 struct dwarf2_section_info *section)
16479{
16480 unsigned int bytes_read;
16481
16482 switch (form)
16483 {
16484 case DW_FORM_data1:
16485 case DW_FORM_flag:
16486 ++bytes;
16487 break;
16488
16489 case DW_FORM_data2:
16490 bytes += 2;
16491 break;
16492
16493 case DW_FORM_data4:
16494 bytes += 4;
16495 break;
16496
16497 case DW_FORM_data8:
16498 bytes += 8;
16499 break;
16500
16501 case DW_FORM_string:
16502 read_direct_string (abfd, bytes, &bytes_read);
16503 bytes += bytes_read;
16504 break;
16505
16506 case DW_FORM_sec_offset:
16507 case DW_FORM_strp:
16508 bytes += offset_size;
16509 break;
16510
16511 case DW_FORM_block:
16512 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16513 bytes += bytes_read;
16514 break;
16515
16516 case DW_FORM_block1:
16517 bytes += 1 + read_1_byte (abfd, bytes);
16518 break;
16519 case DW_FORM_block2:
16520 bytes += 2 + read_2_bytes (abfd, bytes);
16521 break;
16522 case DW_FORM_block4:
16523 bytes += 4 + read_4_bytes (abfd, bytes);
16524 break;
16525
16526 case DW_FORM_sdata:
16527 case DW_FORM_udata:
16528 case DW_FORM_GNU_addr_index:
16529 case DW_FORM_GNU_str_index:
16530 bytes = skip_leb128 (abfd, bytes);
16531 break;
16532
16533 default:
16534 {
16535 complain:
16536 complaint (&symfile_complaints,
16537 _("invalid form 0x%x in `%s'"),
16538 form,
16539 section->asection->name);
16540 return NULL;
16541 }
16542 }
16543
16544 return bytes;
16545}
16546
16547/* A helper for dwarf_decode_macros that handles skipping an unknown
16548 opcode. Returns an updated pointer to the macro data buffer; or,
16549 on error, issues a complaint and returns NULL. */
16550
16551static gdb_byte *
16552skip_unknown_opcode (unsigned int opcode,
16553 gdb_byte **opcode_definitions,
16554 gdb_byte *mac_ptr,
16555 bfd *abfd,
16556 unsigned int offset_size,
16557 struct dwarf2_section_info *section)
16558{
16559 unsigned int bytes_read, i;
16560 unsigned long arg;
16561 gdb_byte *defn;
16562
16563 if (opcode_definitions[opcode] == NULL)
16564 {
16565 complaint (&symfile_complaints,
16566 _("unrecognized DW_MACFINO opcode 0x%x"),
16567 opcode);
16568 return NULL;
16569 }
16570
16571 defn = opcode_definitions[opcode];
16572 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16573 defn += bytes_read;
16574
16575 for (i = 0; i < arg; ++i)
16576 {
16577 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
16578 if (mac_ptr == NULL)
16579 {
16580 /* skip_form_bytes already issued the complaint. */
16581 return NULL;
16582 }
16583 }
16584
16585 return mac_ptr;
16586}
16587
16588/* A helper function which parses the header of a macro section.
16589 If the macro section is the extended (for now called "GNU") type,
16590 then this updates *OFFSET_SIZE. Returns a pointer to just after
16591 the header, or issues a complaint and returns NULL on error. */
16592
16593static gdb_byte *
16594dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16595 bfd *abfd,
16596 gdb_byte *mac_ptr,
16597 unsigned int *offset_size,
16598 int section_is_gnu)
16599{
16600 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
16601
16602 if (section_is_gnu)
16603 {
16604 unsigned int version, flags;
16605
16606 version = read_2_bytes (abfd, mac_ptr);
16607 if (version != 4)
16608 {
16609 complaint (&symfile_complaints,
16610 _("unrecognized version `%d' in .debug_macro section"),
16611 version);
16612 return NULL;
16613 }
16614 mac_ptr += 2;
16615
16616 flags = read_1_byte (abfd, mac_ptr);
16617 ++mac_ptr;
16618 *offset_size = (flags & 1) ? 8 : 4;
16619
16620 if ((flags & 2) != 0)
16621 /* We don't need the line table offset. */
16622 mac_ptr += *offset_size;
16623
16624 /* Vendor opcode descriptions. */
16625 if ((flags & 4) != 0)
16626 {
16627 unsigned int i, count;
16628
16629 count = read_1_byte (abfd, mac_ptr);
16630 ++mac_ptr;
16631 for (i = 0; i < count; ++i)
16632 {
16633 unsigned int opcode, bytes_read;
16634 unsigned long arg;
16635
16636 opcode = read_1_byte (abfd, mac_ptr);
16637 ++mac_ptr;
16638 opcode_definitions[opcode] = mac_ptr;
16639 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16640 mac_ptr += bytes_read;
16641 mac_ptr += arg;
16642 }
16643 }
16644 }
16645
16646 return mac_ptr;
16647}
16648
16649/* A helper for dwarf_decode_macros that handles the GNU extensions,
16650 including DW_MACRO_GNU_transparent_include. */
16651
16652static void
16653dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16654 struct macro_source_file *current_file,
16655 struct line_header *lh, char *comp_dir,
16656 struct dwarf2_section_info *section,
16657 int section_is_gnu,
16658 unsigned int offset_size,
16659 struct objfile *objfile,
16660 htab_t include_hash)
16661{
16662 enum dwarf_macro_record_type macinfo_type;
16663 int at_commandline;
16664 gdb_byte *opcode_definitions[256];
16665
16666 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16667 &offset_size, section_is_gnu);
16668 if (mac_ptr == NULL)
16669 {
16670 /* We already issued a complaint. */
16671 return;
16672 }
16673
16674 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16675 GDB is still reading the definitions from command line. First
16676 DW_MACINFO_start_file will need to be ignored as it was already executed
16677 to create CURRENT_FILE for the main source holding also the command line
16678 definitions. On first met DW_MACINFO_start_file this flag is reset to
16679 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16680
16681 at_commandline = 1;
16682
16683 do
16684 {
16685 /* Do we at least have room for a macinfo type byte? */
16686 if (mac_ptr >= mac_end)
16687 {
16688 dwarf2_macros_too_long_complaint (section);
16689 break;
16690 }
16691
16692 macinfo_type = read_1_byte (abfd, mac_ptr);
16693 mac_ptr++;
16694
16695 /* Note that we rely on the fact that the corresponding GNU and
16696 DWARF constants are the same. */
16697 switch (macinfo_type)
16698 {
16699 /* A zero macinfo type indicates the end of the macro
16700 information. */
16701 case 0:
16702 break;
16703
16704 case DW_MACRO_GNU_define:
16705 case DW_MACRO_GNU_undef:
16706 case DW_MACRO_GNU_define_indirect:
16707 case DW_MACRO_GNU_undef_indirect:
16708 {
16709 unsigned int bytes_read;
16710 int line;
16711 char *body;
16712 int is_define;
16713
16714 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16715 mac_ptr += bytes_read;
16716
16717 if (macinfo_type == DW_MACRO_GNU_define
16718 || macinfo_type == DW_MACRO_GNU_undef)
16719 {
16720 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16721 mac_ptr += bytes_read;
16722 }
16723 else
16724 {
16725 LONGEST str_offset;
16726
16727 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16728 mac_ptr += offset_size;
16729
16730 body = read_indirect_string_at_offset (abfd, str_offset);
16731 }
16732
16733 is_define = (macinfo_type == DW_MACRO_GNU_define
16734 || macinfo_type == DW_MACRO_GNU_define_indirect);
16735 if (! current_file)
16736 {
16737 /* DWARF violation as no main source is present. */
16738 complaint (&symfile_complaints,
16739 _("debug info with no main source gives macro %s "
16740 "on line %d: %s"),
16741 is_define ? _("definition") : _("undefinition"),
16742 line, body);
16743 break;
16744 }
16745 if ((line == 0 && !at_commandline)
16746 || (line != 0 && at_commandline))
16747 complaint (&symfile_complaints,
16748 _("debug info gives %s macro %s with %s line %d: %s"),
16749 at_commandline ? _("command-line") : _("in-file"),
16750 is_define ? _("definition") : _("undefinition"),
16751 line == 0 ? _("zero") : _("non-zero"), line, body);
16752
16753 if (is_define)
16754 parse_macro_definition (current_file, line, body);
16755 else
16756 {
16757 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16758 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16759 macro_undef (current_file, line, body);
16760 }
16761 }
16762 break;
16763
16764 case DW_MACRO_GNU_start_file:
16765 {
16766 unsigned int bytes_read;
16767 int line, file;
16768
16769 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16770 mac_ptr += bytes_read;
16771 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16772 mac_ptr += bytes_read;
16773
16774 if ((line == 0 && !at_commandline)
16775 || (line != 0 && at_commandline))
16776 complaint (&symfile_complaints,
16777 _("debug info gives source %d included "
16778 "from %s at %s line %d"),
16779 file, at_commandline ? _("command-line") : _("file"),
16780 line == 0 ? _("zero") : _("non-zero"), line);
16781
16782 if (at_commandline)
16783 {
16784 /* This DW_MACRO_GNU_start_file was executed in the
16785 pass one. */
16786 at_commandline = 0;
16787 }
16788 else
16789 current_file = macro_start_file (file, line,
16790 current_file, comp_dir,
16791 lh, objfile);
16792 }
16793 break;
16794
16795 case DW_MACRO_GNU_end_file:
16796 if (! current_file)
16797 complaint (&symfile_complaints,
16798 _("macro debug info has an unmatched "
16799 "`close_file' directive"));
16800 else
16801 {
16802 current_file = current_file->included_by;
16803 if (! current_file)
16804 {
16805 enum dwarf_macro_record_type next_type;
16806
16807 /* GCC circa March 2002 doesn't produce the zero
16808 type byte marking the end of the compilation
16809 unit. Complain if it's not there, but exit no
16810 matter what. */
16811
16812 /* Do we at least have room for a macinfo type byte? */
16813 if (mac_ptr >= mac_end)
16814 {
16815 dwarf2_macros_too_long_complaint (section);
16816 return;
16817 }
16818
16819 /* We don't increment mac_ptr here, so this is just
16820 a look-ahead. */
16821 next_type = read_1_byte (abfd, mac_ptr);
16822 if (next_type != 0)
16823 complaint (&symfile_complaints,
16824 _("no terminating 0-type entry for "
16825 "macros in `.debug_macinfo' section"));
16826
16827 return;
16828 }
16829 }
16830 break;
16831
16832 case DW_MACRO_GNU_transparent_include:
16833 {
16834 LONGEST offset;
16835 void **slot;
16836
16837 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16838 mac_ptr += offset_size;
16839
16840 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16841 if (*slot != NULL)
16842 {
16843 /* This has actually happened; see
16844 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16845 complaint (&symfile_complaints,
16846 _("recursive DW_MACRO_GNU_transparent_include in "
16847 ".debug_macro section"));
16848 }
16849 else
16850 {
16851 *slot = mac_ptr;
16852
16853 dwarf_decode_macro_bytes (abfd,
16854 section->buffer + offset,
16855 mac_end, current_file,
16856 lh, comp_dir,
16857 section, section_is_gnu,
16858 offset_size, objfile, include_hash);
16859
16860 htab_remove_elt (include_hash, mac_ptr);
16861 }
16862 }
16863 break;
16864
16865 case DW_MACINFO_vendor_ext:
16866 if (!section_is_gnu)
16867 {
16868 unsigned int bytes_read;
16869 int constant;
16870
16871 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16872 mac_ptr += bytes_read;
16873 read_direct_string (abfd, mac_ptr, &bytes_read);
16874 mac_ptr += bytes_read;
16875
16876 /* We don't recognize any vendor extensions. */
16877 break;
16878 }
16879 /* FALLTHROUGH */
16880
16881 default:
16882 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16883 mac_ptr, abfd, offset_size,
16884 section);
16885 if (mac_ptr == NULL)
16886 return;
16887 break;
16888 }
16889 } while (macinfo_type != 0);
16890}
16891
16892static void
16893dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16894 char *comp_dir, bfd *abfd,
16895 struct dwarf2_cu *cu,
16896 struct dwarf2_section_info *section,
16897 int section_is_gnu)
16898{
16899 struct objfile *objfile = dwarf2_per_objfile->objfile;
16900 gdb_byte *mac_ptr, *mac_end;
16901 struct macro_source_file *current_file = 0;
16902 enum dwarf_macro_record_type macinfo_type;
16903 unsigned int offset_size = cu->header.offset_size;
16904 gdb_byte *opcode_definitions[256];
16905 struct cleanup *cleanup;
16906 htab_t include_hash;
16907 void **slot;
16908
16909 dwarf2_read_section (objfile, section);
16910 if (section->buffer == NULL)
16911 {
16912 complaint (&symfile_complaints, _("missing %s section"),
16913 section->asection->name);
16914 return;
16915 }
16916
16917 /* First pass: Find the name of the base filename.
16918 This filename is needed in order to process all macros whose definition
16919 (or undefinition) comes from the command line. These macros are defined
16920 before the first DW_MACINFO_start_file entry, and yet still need to be
16921 associated to the base file.
16922
16923 To determine the base file name, we scan the macro definitions until we
16924 reach the first DW_MACINFO_start_file entry. We then initialize
16925 CURRENT_FILE accordingly so that any macro definition found before the
16926 first DW_MACINFO_start_file can still be associated to the base file. */
16927
16928 mac_ptr = section->buffer + offset;
16929 mac_end = section->buffer + section->size;
16930
16931 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16932 &offset_size, section_is_gnu);
16933 if (mac_ptr == NULL)
16934 {
16935 /* We already issued a complaint. */
16936 return;
16937 }
16938
16939 do
16940 {
16941 /* Do we at least have room for a macinfo type byte? */
16942 if (mac_ptr >= mac_end)
16943 {
16944 /* Complaint is printed during the second pass as GDB will probably
16945 stop the first pass earlier upon finding
16946 DW_MACINFO_start_file. */
16947 break;
16948 }
16949
16950 macinfo_type = read_1_byte (abfd, mac_ptr);
16951 mac_ptr++;
16952
16953 /* Note that we rely on the fact that the corresponding GNU and
16954 DWARF constants are the same. */
16955 switch (macinfo_type)
16956 {
16957 /* A zero macinfo type indicates the end of the macro
16958 information. */
16959 case 0:
16960 break;
16961
16962 case DW_MACRO_GNU_define:
16963 case DW_MACRO_GNU_undef:
16964 /* Only skip the data by MAC_PTR. */
16965 {
16966 unsigned int bytes_read;
16967
16968 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16969 mac_ptr += bytes_read;
16970 read_direct_string (abfd, mac_ptr, &bytes_read);
16971 mac_ptr += bytes_read;
16972 }
16973 break;
16974
16975 case DW_MACRO_GNU_start_file:
16976 {
16977 unsigned int bytes_read;
16978 int line, file;
16979
16980 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16981 mac_ptr += bytes_read;
16982 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16983 mac_ptr += bytes_read;
16984
16985 current_file = macro_start_file (file, line, current_file,
16986 comp_dir, lh, objfile);
16987 }
16988 break;
16989
16990 case DW_MACRO_GNU_end_file:
16991 /* No data to skip by MAC_PTR. */
16992 break;
16993
16994 case DW_MACRO_GNU_define_indirect:
16995 case DW_MACRO_GNU_undef_indirect:
16996 {
16997 unsigned int bytes_read;
16998
16999 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17000 mac_ptr += bytes_read;
17001 mac_ptr += offset_size;
17002 }
17003 break;
17004
17005 case DW_MACRO_GNU_transparent_include:
17006 /* Note that, according to the spec, a transparent include
17007 chain cannot call DW_MACRO_GNU_start_file. So, we can just
17008 skip this opcode. */
17009 mac_ptr += offset_size;
17010 break;
17011
17012 case DW_MACINFO_vendor_ext:
17013 /* Only skip the data by MAC_PTR. */
17014 if (!section_is_gnu)
17015 {
17016 unsigned int bytes_read;
17017
17018 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17019 mac_ptr += bytes_read;
17020 read_direct_string (abfd, mac_ptr, &bytes_read);
17021 mac_ptr += bytes_read;
17022 }
17023 /* FALLTHROUGH */
17024
17025 default:
17026 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17027 mac_ptr, abfd, offset_size,
17028 section);
17029 if (mac_ptr == NULL)
17030 return;
17031 break;
17032 }
17033 } while (macinfo_type != 0 && current_file == NULL);
17034
17035 /* Second pass: Process all entries.
17036
17037 Use the AT_COMMAND_LINE flag to determine whether we are still processing
17038 command-line macro definitions/undefinitions. This flag is unset when we
17039 reach the first DW_MACINFO_start_file entry. */
17040
17041 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
17042 NULL, xcalloc, xfree);
17043 cleanup = make_cleanup_htab_delete (include_hash);
17044 mac_ptr = section->buffer + offset;
17045 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17046 *slot = mac_ptr;
17047 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
17048 current_file, lh, comp_dir, section, section_is_gnu,
17049 offset_size, objfile, include_hash);
17050 do_cleanups (cleanup);
17051}
17052
17053/* Check if the attribute's form is a DW_FORM_block*
17054 if so return true else false. */
17055
17056static int
17057attr_form_is_block (struct attribute *attr)
17058{
17059 return (attr == NULL ? 0 :
17060 attr->form == DW_FORM_block1
17061 || attr->form == DW_FORM_block2
17062 || attr->form == DW_FORM_block4
17063 || attr->form == DW_FORM_block
17064 || attr->form == DW_FORM_exprloc);
17065}
17066
17067/* Return non-zero if ATTR's value is a section offset --- classes
17068 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
17069 You may use DW_UNSND (attr) to retrieve such offsets.
17070
17071 Section 7.5.4, "Attribute Encodings", explains that no attribute
17072 may have a value that belongs to more than one of these classes; it
17073 would be ambiguous if we did, because we use the same forms for all
17074 of them. */
17075
17076static int
17077attr_form_is_section_offset (struct attribute *attr)
17078{
17079 return (attr->form == DW_FORM_data4
17080 || attr->form == DW_FORM_data8
17081 || attr->form == DW_FORM_sec_offset);
17082}
17083
17084/* Return non-zero if ATTR's value falls in the 'constant' class, or
17085 zero otherwise. When this function returns true, you can apply
17086 dwarf2_get_attr_constant_value to it.
17087
17088 However, note that for some attributes you must check
17089 attr_form_is_section_offset before using this test. DW_FORM_data4
17090 and DW_FORM_data8 are members of both the constant class, and of
17091 the classes that contain offsets into other debug sections
17092 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
17093 that, if an attribute's can be either a constant or one of the
17094 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
17095 taken as section offsets, not constants. */
17096
17097static int
17098attr_form_is_constant (struct attribute *attr)
17099{
17100 switch (attr->form)
17101 {
17102 case DW_FORM_sdata:
17103 case DW_FORM_udata:
17104 case DW_FORM_data1:
17105 case DW_FORM_data2:
17106 case DW_FORM_data4:
17107 case DW_FORM_data8:
17108 return 1;
17109 default:
17110 return 0;
17111 }
17112}
17113
17114/* Return the .debug_loc section to use for CU.
17115 For DWO files use .debug_loc.dwo. */
17116
17117static struct dwarf2_section_info *
17118cu_debug_loc_section (struct dwarf2_cu *cu)
17119{
17120 if (cu->dwo_unit)
17121 return &cu->dwo_unit->dwo_file->sections.loc;
17122 return &dwarf2_per_objfile->loc;
17123}
17124
17125/* A helper function that fills in a dwarf2_loclist_baton. */
17126
17127static void
17128fill_in_loclist_baton (struct dwarf2_cu *cu,
17129 struct dwarf2_loclist_baton *baton,
17130 struct attribute *attr)
17131{
17132 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17133
17134 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17135
17136 baton->per_cu = cu->per_cu;
17137 gdb_assert (baton->per_cu);
17138 /* We don't know how long the location list is, but make sure we
17139 don't run off the edge of the section. */
17140 baton->size = section->size - DW_UNSND (attr);
17141 baton->data = section->buffer + DW_UNSND (attr);
17142 baton->base_address = cu->base_address;
17143}
17144
17145static void
17146dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
17147 struct dwarf2_cu *cu)
17148{
17149 struct objfile *objfile = dwarf2_per_objfile->objfile;
17150 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17151
17152 if (attr_form_is_section_offset (attr)
17153 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
17154 the section. If so, fall through to the complaint in the
17155 other branch. */
17156 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
17157 {
17158 struct dwarf2_loclist_baton *baton;
17159
17160 baton = obstack_alloc (&objfile->objfile_obstack,
17161 sizeof (struct dwarf2_loclist_baton));
17162
17163 fill_in_loclist_baton (cu, baton, attr);
17164
17165 if (cu->base_known == 0)
17166 complaint (&symfile_complaints,
17167 _("Location list used without "
17168 "specifying the CU base address."));
17169
17170 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
17171 SYMBOL_LOCATION_BATON (sym) = baton;
17172 }
17173 else
17174 {
17175 struct dwarf2_locexpr_baton *baton;
17176
17177 baton = obstack_alloc (&objfile->objfile_obstack,
17178 sizeof (struct dwarf2_locexpr_baton));
17179 baton->per_cu = cu->per_cu;
17180 gdb_assert (baton->per_cu);
17181
17182 if (attr_form_is_block (attr))
17183 {
17184 /* Note that we're just copying the block's data pointer
17185 here, not the actual data. We're still pointing into the
17186 info_buffer for SYM's objfile; right now we never release
17187 that buffer, but when we do clean up properly this may
17188 need to change. */
17189 baton->size = DW_BLOCK (attr)->size;
17190 baton->data = DW_BLOCK (attr)->data;
17191 }
17192 else
17193 {
17194 dwarf2_invalid_attrib_class_complaint ("location description",
17195 SYMBOL_NATURAL_NAME (sym));
17196 baton->size = 0;
17197 }
17198
17199 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
17200 SYMBOL_LOCATION_BATON (sym) = baton;
17201 }
17202}
17203
17204/* Return the OBJFILE associated with the compilation unit CU. If CU
17205 came from a separate debuginfo file, then the master objfile is
17206 returned. */
17207
17208struct objfile *
17209dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
17210{
17211 struct objfile *objfile = per_cu->objfile;
17212
17213 /* Return the master objfile, so that we can report and look up the
17214 correct file containing this variable. */
17215 if (objfile->separate_debug_objfile_backlink)
17216 objfile = objfile->separate_debug_objfile_backlink;
17217
17218 return objfile;
17219}
17220
17221/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
17222 (CU_HEADERP is unused in such case) or prepare a temporary copy at
17223 CU_HEADERP first. */
17224
17225static const struct comp_unit_head *
17226per_cu_header_read_in (struct comp_unit_head *cu_headerp,
17227 struct dwarf2_per_cu_data *per_cu)
17228{
17229 struct objfile *objfile;
17230 struct dwarf2_per_objfile *per_objfile;
17231 gdb_byte *info_ptr;
17232
17233 if (per_cu->cu)
17234 return &per_cu->cu->header;
17235
17236 objfile = per_cu->objfile;
17237 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17238 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
17239
17240 memset (cu_headerp, 0, sizeof (*cu_headerp));
17241 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
17242
17243 return cu_headerp;
17244}
17245
17246/* Return the address size given in the compilation unit header for CU. */
17247
17248int
17249dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
17250{
17251 struct comp_unit_head cu_header_local;
17252 const struct comp_unit_head *cu_headerp;
17253
17254 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17255
17256 return cu_headerp->addr_size;
17257}
17258
17259/* Return the offset size given in the compilation unit header for CU. */
17260
17261int
17262dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
17263{
17264 struct comp_unit_head cu_header_local;
17265 const struct comp_unit_head *cu_headerp;
17266
17267 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17268
17269 return cu_headerp->offset_size;
17270}
17271
17272/* See its dwarf2loc.h declaration. */
17273
17274int
17275dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
17276{
17277 struct comp_unit_head cu_header_local;
17278 const struct comp_unit_head *cu_headerp;
17279
17280 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17281
17282 if (cu_headerp->version == 2)
17283 return cu_headerp->addr_size;
17284 else
17285 return cu_headerp->offset_size;
17286}
17287
17288/* Return the text offset of the CU. The returned offset comes from
17289 this CU's objfile. If this objfile came from a separate debuginfo
17290 file, then the offset may be different from the corresponding
17291 offset in the parent objfile. */
17292
17293CORE_ADDR
17294dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
17295{
17296 struct objfile *objfile = per_cu->objfile;
17297
17298 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17299}
17300
17301/* Locate the .debug_info compilation unit from CU's objfile which contains
17302 the DIE at OFFSET. Raises an error on failure. */
17303
17304static struct dwarf2_per_cu_data *
17305dwarf2_find_containing_comp_unit (sect_offset offset,
17306 struct objfile *objfile)
17307{
17308 struct dwarf2_per_cu_data *this_cu;
17309 int low, high;
17310
17311 low = 0;
17312 high = dwarf2_per_objfile->n_comp_units - 1;
17313 while (high > low)
17314 {
17315 int mid = low + (high - low) / 2;
17316
17317 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
17318 >= offset.sect_off)
17319 high = mid;
17320 else
17321 low = mid + 1;
17322 }
17323 gdb_assert (low == high);
17324 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
17325 > offset.sect_off)
17326 {
17327 if (low == 0)
17328 error (_("Dwarf Error: could not find partial DIE containing "
17329 "offset 0x%lx [in module %s]"),
17330 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
17331
17332 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
17333 <= offset.sect_off);
17334 return dwarf2_per_objfile->all_comp_units[low-1];
17335 }
17336 else
17337 {
17338 this_cu = dwarf2_per_objfile->all_comp_units[low];
17339 if (low == dwarf2_per_objfile->n_comp_units - 1
17340 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
17341 error (_("invalid dwarf2 offset %u"), offset.sect_off);
17342 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
17343 return this_cu;
17344 }
17345}
17346
17347/* Initialize dwarf2_cu CU, owned by PER_CU. */
17348
17349static void
17350init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
17351{
17352 memset (cu, 0, sizeof (*cu));
17353 per_cu->cu = cu;
17354 cu->per_cu = per_cu;
17355 cu->objfile = per_cu->objfile;
17356 obstack_init (&cu->comp_unit_obstack);
17357}
17358
17359/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17360
17361static void
17362prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
17363{
17364 struct attribute *attr;
17365
17366 /* Set the language we're debugging. */
17367 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17368 if (attr)
17369 set_cu_language (DW_UNSND (attr), cu);
17370 else
17371 {
17372 cu->language = language_minimal;
17373 cu->language_defn = language_def (cu->language);
17374 }
17375
17376 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17377 if (attr)
17378 cu->producer = DW_STRING (attr);
17379}
17380
17381/* Release one cached compilation unit, CU. We unlink it from the tree
17382 of compilation units, but we don't remove it from the read_in_chain;
17383 the caller is responsible for that.
17384 NOTE: DATA is a void * because this function is also used as a
17385 cleanup routine. */
17386
17387static void
17388free_heap_comp_unit (void *data)
17389{
17390 struct dwarf2_cu *cu = data;
17391
17392 gdb_assert (cu->per_cu != NULL);
17393 cu->per_cu->cu = NULL;
17394 cu->per_cu = NULL;
17395
17396 obstack_free (&cu->comp_unit_obstack, NULL);
17397
17398 xfree (cu);
17399}
17400
17401/* This cleanup function is passed the address of a dwarf2_cu on the stack
17402 when we're finished with it. We can't free the pointer itself, but be
17403 sure to unlink it from the cache. Also release any associated storage. */
17404
17405static void
17406free_stack_comp_unit (void *data)
17407{
17408 struct dwarf2_cu *cu = data;
17409
17410 gdb_assert (cu->per_cu != NULL);
17411 cu->per_cu->cu = NULL;
17412 cu->per_cu = NULL;
17413
17414 obstack_free (&cu->comp_unit_obstack, NULL);
17415 cu->partial_dies = NULL;
17416}
17417
17418/* Free all cached compilation units. */
17419
17420static void
17421free_cached_comp_units (void *data)
17422{
17423 struct dwarf2_per_cu_data *per_cu, **last_chain;
17424
17425 per_cu = dwarf2_per_objfile->read_in_chain;
17426 last_chain = &dwarf2_per_objfile->read_in_chain;
17427 while (per_cu != NULL)
17428 {
17429 struct dwarf2_per_cu_data *next_cu;
17430
17431 next_cu = per_cu->cu->read_in_chain;
17432
17433 free_heap_comp_unit (per_cu->cu);
17434 *last_chain = next_cu;
17435
17436 per_cu = next_cu;
17437 }
17438}
17439
17440/* Increase the age counter on each cached compilation unit, and free
17441 any that are too old. */
17442
17443static void
17444age_cached_comp_units (void)
17445{
17446 struct dwarf2_per_cu_data *per_cu, **last_chain;
17447
17448 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17449 per_cu = dwarf2_per_objfile->read_in_chain;
17450 while (per_cu != NULL)
17451 {
17452 per_cu->cu->last_used ++;
17453 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17454 dwarf2_mark (per_cu->cu);
17455 per_cu = per_cu->cu->read_in_chain;
17456 }
17457
17458 per_cu = dwarf2_per_objfile->read_in_chain;
17459 last_chain = &dwarf2_per_objfile->read_in_chain;
17460 while (per_cu != NULL)
17461 {
17462 struct dwarf2_per_cu_data *next_cu;
17463
17464 next_cu = per_cu->cu->read_in_chain;
17465
17466 if (!per_cu->cu->mark)
17467 {
17468 free_heap_comp_unit (per_cu->cu);
17469 *last_chain = next_cu;
17470 }
17471 else
17472 last_chain = &per_cu->cu->read_in_chain;
17473
17474 per_cu = next_cu;
17475 }
17476}
17477
17478/* Remove a single compilation unit from the cache. */
17479
17480static void
17481free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
17482{
17483 struct dwarf2_per_cu_data *per_cu, **last_chain;
17484
17485 per_cu = dwarf2_per_objfile->read_in_chain;
17486 last_chain = &dwarf2_per_objfile->read_in_chain;
17487 while (per_cu != NULL)
17488 {
17489 struct dwarf2_per_cu_data *next_cu;
17490
17491 next_cu = per_cu->cu->read_in_chain;
17492
17493 if (per_cu == target_per_cu)
17494 {
17495 free_heap_comp_unit (per_cu->cu);
17496 per_cu->cu = NULL;
17497 *last_chain = next_cu;
17498 break;
17499 }
17500 else
17501 last_chain = &per_cu->cu->read_in_chain;
17502
17503 per_cu = next_cu;
17504 }
17505}
17506
17507/* Release all extra memory associated with OBJFILE. */
17508
17509void
17510dwarf2_free_objfile (struct objfile *objfile)
17511{
17512 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17513
17514 if (dwarf2_per_objfile == NULL)
17515 return;
17516
17517 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17518 free_cached_comp_units (NULL);
17519
17520 if (dwarf2_per_objfile->quick_file_names_table)
17521 htab_delete (dwarf2_per_objfile->quick_file_names_table);
17522
17523 /* Everything else should be on the objfile obstack. */
17524}
17525
17526/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17527 We store these in a hash table separate from the DIEs, and preserve them
17528 when the DIEs are flushed out of cache.
17529
17530 The CU "per_cu" pointer is needed because offset alone is not enough to
17531 uniquely identify the type. A file may have multiple .debug_types sections,
17532 or the type may come from a DWO file. We have to use something in
17533 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17534 routine, get_die_type_at_offset, from outside this file, and thus won't
17535 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17536 of the objfile. */
17537
17538struct dwarf2_per_cu_offset_and_type
17539{
17540 const struct dwarf2_per_cu_data *per_cu;
17541 sect_offset offset;
17542 struct type *type;
17543};
17544
17545/* Hash function for a dwarf2_per_cu_offset_and_type. */
17546
17547static hashval_t
17548per_cu_offset_and_type_hash (const void *item)
17549{
17550 const struct dwarf2_per_cu_offset_and_type *ofs = item;
17551
17552 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
17553}
17554
17555/* Equality function for a dwarf2_per_cu_offset_and_type. */
17556
17557static int
17558per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
17559{
17560 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17561 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
17562
17563 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17564 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
17565}
17566
17567/* Set the type associated with DIE to TYPE. Save it in CU's hash
17568 table if necessary. For convenience, return TYPE.
17569
17570 The DIEs reading must have careful ordering to:
17571 * Not cause infite loops trying to read in DIEs as a prerequisite for
17572 reading current DIE.
17573 * Not trying to dereference contents of still incompletely read in types
17574 while reading in other DIEs.
17575 * Enable referencing still incompletely read in types just by a pointer to
17576 the type without accessing its fields.
17577
17578 Therefore caller should follow these rules:
17579 * Try to fetch any prerequisite types we may need to build this DIE type
17580 before building the type and calling set_die_type.
17581 * After building type call set_die_type for current DIE as soon as
17582 possible before fetching more types to complete the current type.
17583 * Make the type as complete as possible before fetching more types. */
17584
17585static struct type *
17586set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17587{
17588 struct dwarf2_per_cu_offset_and_type **slot, ofs;
17589 struct objfile *objfile = cu->objfile;
17590
17591 /* For Ada types, make sure that the gnat-specific data is always
17592 initialized (if not already set). There are a few types where
17593 we should not be doing so, because the type-specific area is
17594 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17595 where the type-specific area is used to store the floatformat).
17596 But this is not a problem, because the gnat-specific information
17597 is actually not needed for these types. */
17598 if (need_gnat_info (cu)
17599 && TYPE_CODE (type) != TYPE_CODE_FUNC
17600 && TYPE_CODE (type) != TYPE_CODE_FLT
17601 && !HAVE_GNAT_AUX_INFO (type))
17602 INIT_GNAT_SPECIFIC (type);
17603
17604 if (dwarf2_per_objfile->die_type_hash == NULL)
17605 {
17606 dwarf2_per_objfile->die_type_hash =
17607 htab_create_alloc_ex (127,
17608 per_cu_offset_and_type_hash,
17609 per_cu_offset_and_type_eq,
17610 NULL,
17611 &objfile->objfile_obstack,
17612 hashtab_obstack_allocate,
17613 dummy_obstack_deallocate);
17614 }
17615
17616 ofs.per_cu = cu->per_cu;
17617 ofs.offset = die->offset;
17618 ofs.type = type;
17619 slot = (struct dwarf2_per_cu_offset_and_type **)
17620 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
17621 if (*slot)
17622 complaint (&symfile_complaints,
17623 _("A problem internal to GDB: DIE 0x%x has type already set"),
17624 die->offset.sect_off);
17625 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
17626 **slot = ofs;
17627 return type;
17628}
17629
17630/* Look up the type for the die at OFFSET in the appropriate type_hash
17631 table, or return NULL if the die does not have a saved type. */
17632
17633static struct type *
17634get_die_type_at_offset (sect_offset offset,
17635 struct dwarf2_per_cu_data *per_cu)
17636{
17637 struct dwarf2_per_cu_offset_and_type *slot, ofs;
17638
17639 if (dwarf2_per_objfile->die_type_hash == NULL)
17640 return NULL;
17641
17642 ofs.per_cu = per_cu;
17643 ofs.offset = offset;
17644 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
17645 if (slot)
17646 return slot->type;
17647 else
17648 return NULL;
17649}
17650
17651/* Look up the type for DIE in the appropriate type_hash table,
17652 or return NULL if DIE does not have a saved type. */
17653
17654static struct type *
17655get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17656{
17657 return get_die_type_at_offset (die->offset, cu->per_cu);
17658}
17659
17660/* Add a dependence relationship from CU to REF_PER_CU. */
17661
17662static void
17663dwarf2_add_dependence (struct dwarf2_cu *cu,
17664 struct dwarf2_per_cu_data *ref_per_cu)
17665{
17666 void **slot;
17667
17668 if (cu->dependencies == NULL)
17669 cu->dependencies
17670 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17671 NULL, &cu->comp_unit_obstack,
17672 hashtab_obstack_allocate,
17673 dummy_obstack_deallocate);
17674
17675 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17676 if (*slot == NULL)
17677 *slot = ref_per_cu;
17678}
17679
17680/* Subroutine of dwarf2_mark to pass to htab_traverse.
17681 Set the mark field in every compilation unit in the
17682 cache that we must keep because we are keeping CU. */
17683
17684static int
17685dwarf2_mark_helper (void **slot, void *data)
17686{
17687 struct dwarf2_per_cu_data *per_cu;
17688
17689 per_cu = (struct dwarf2_per_cu_data *) *slot;
17690
17691 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17692 reading of the chain. As such dependencies remain valid it is not much
17693 useful to track and undo them during QUIT cleanups. */
17694 if (per_cu->cu == NULL)
17695 return 1;
17696
17697 if (per_cu->cu->mark)
17698 return 1;
17699 per_cu->cu->mark = 1;
17700
17701 if (per_cu->cu->dependencies != NULL)
17702 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17703
17704 return 1;
17705}
17706
17707/* Set the mark field in CU and in every other compilation unit in the
17708 cache that we must keep because we are keeping CU. */
17709
17710static void
17711dwarf2_mark (struct dwarf2_cu *cu)
17712{
17713 if (cu->mark)
17714 return;
17715 cu->mark = 1;
17716 if (cu->dependencies != NULL)
17717 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17718}
17719
17720static void
17721dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17722{
17723 while (per_cu)
17724 {
17725 per_cu->cu->mark = 0;
17726 per_cu = per_cu->cu->read_in_chain;
17727 }
17728}
17729
17730/* Trivial hash function for partial_die_info: the hash value of a DIE
17731 is its offset in .debug_info for this objfile. */
17732
17733static hashval_t
17734partial_die_hash (const void *item)
17735{
17736 const struct partial_die_info *part_die = item;
17737
17738 return part_die->offset.sect_off;
17739}
17740
17741/* Trivial comparison function for partial_die_info structures: two DIEs
17742 are equal if they have the same offset. */
17743
17744static int
17745partial_die_eq (const void *item_lhs, const void *item_rhs)
17746{
17747 const struct partial_die_info *part_die_lhs = item_lhs;
17748 const struct partial_die_info *part_die_rhs = item_rhs;
17749
17750 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17751}
17752
17753static struct cmd_list_element *set_dwarf2_cmdlist;
17754static struct cmd_list_element *show_dwarf2_cmdlist;
17755
17756static void
17757set_dwarf2_cmd (char *args, int from_tty)
17758{
17759 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17760}
17761
17762static void
17763show_dwarf2_cmd (char *args, int from_tty)
17764{
17765 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17766}
17767
17768/* If section described by INFO was mmapped, munmap it now. */
17769
17770static void
17771munmap_section_buffer (struct dwarf2_section_info *info)
17772{
17773 if (info->map_addr != NULL)
17774 {
17775#ifdef HAVE_MMAP
17776 int res;
17777
17778 res = munmap (info->map_addr, info->map_len);
17779 gdb_assert (res == 0);
17780#else
17781 /* Without HAVE_MMAP, we should never be here to begin with. */
17782 gdb_assert_not_reached ("no mmap support");
17783#endif
17784 }
17785}
17786
17787/* munmap debug sections for OBJFILE, if necessary. */
17788
17789static void
17790dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17791{
17792 struct dwarf2_per_objfile *data = d;
17793 int ix;
17794 struct dwarf2_section_info *section;
17795
17796 /* This is sorted according to the order they're defined in to make it easier
17797 to keep in sync. */
17798 munmap_section_buffer (&data->info);
17799 munmap_section_buffer (&data->abbrev);
17800 munmap_section_buffer (&data->line);
17801 munmap_section_buffer (&data->loc);
17802 munmap_section_buffer (&data->macinfo);
17803 munmap_section_buffer (&data->macro);
17804 munmap_section_buffer (&data->str);
17805 munmap_section_buffer (&data->ranges);
17806 munmap_section_buffer (&data->addr);
17807 munmap_section_buffer (&data->frame);
17808 munmap_section_buffer (&data->eh_frame);
17809 munmap_section_buffer (&data->gdb_index);
17810
17811 for (ix = 0;
17812 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17813 ++ix)
17814 munmap_section_buffer (section);
17815
17816 VEC_free (dwarf2_section_info_def, data->types);
17817
17818 if (data->dwo_files)
17819 free_dwo_files (data->dwo_files, objfile);
17820}
17821
17822\f
17823/* The "save gdb-index" command. */
17824
17825/* The contents of the hash table we create when building the string
17826 table. */
17827struct strtab_entry
17828{
17829 offset_type offset;
17830 const char *str;
17831};
17832
17833/* Hash function for a strtab_entry.
17834
17835 Function is used only during write_hash_table so no index format backward
17836 compatibility is needed. */
17837
17838static hashval_t
17839hash_strtab_entry (const void *e)
17840{
17841 const struct strtab_entry *entry = e;
17842 return mapped_index_string_hash (INT_MAX, entry->str);
17843}
17844
17845/* Equality function for a strtab_entry. */
17846
17847static int
17848eq_strtab_entry (const void *a, const void *b)
17849{
17850 const struct strtab_entry *ea = a;
17851 const struct strtab_entry *eb = b;
17852 return !strcmp (ea->str, eb->str);
17853}
17854
17855/* Create a strtab_entry hash table. */
17856
17857static htab_t
17858create_strtab (void)
17859{
17860 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17861 xfree, xcalloc, xfree);
17862}
17863
17864/* Add a string to the constant pool. Return the string's offset in
17865 host order. */
17866
17867static offset_type
17868add_string (htab_t table, struct obstack *cpool, const char *str)
17869{
17870 void **slot;
17871 struct strtab_entry entry;
17872 struct strtab_entry *result;
17873
17874 entry.str = str;
17875 slot = htab_find_slot (table, &entry, INSERT);
17876 if (*slot)
17877 result = *slot;
17878 else
17879 {
17880 result = XNEW (struct strtab_entry);
17881 result->offset = obstack_object_size (cpool);
17882 result->str = str;
17883 obstack_grow_str0 (cpool, str);
17884 *slot = result;
17885 }
17886 return result->offset;
17887}
17888
17889/* An entry in the symbol table. */
17890struct symtab_index_entry
17891{
17892 /* The name of the symbol. */
17893 const char *name;
17894 /* The offset of the name in the constant pool. */
17895 offset_type index_offset;
17896 /* A sorted vector of the indices of all the CUs that hold an object
17897 of this name. */
17898 VEC (offset_type) *cu_indices;
17899};
17900
17901/* The symbol table. This is a power-of-2-sized hash table. */
17902struct mapped_symtab
17903{
17904 offset_type n_elements;
17905 offset_type size;
17906 struct symtab_index_entry **data;
17907};
17908
17909/* Hash function for a symtab_index_entry. */
17910
17911static hashval_t
17912hash_symtab_entry (const void *e)
17913{
17914 const struct symtab_index_entry *entry = e;
17915 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17916 sizeof (offset_type) * VEC_length (offset_type,
17917 entry->cu_indices),
17918 0);
17919}
17920
17921/* Equality function for a symtab_index_entry. */
17922
17923static int
17924eq_symtab_entry (const void *a, const void *b)
17925{
17926 const struct symtab_index_entry *ea = a;
17927 const struct symtab_index_entry *eb = b;
17928 int len = VEC_length (offset_type, ea->cu_indices);
17929 if (len != VEC_length (offset_type, eb->cu_indices))
17930 return 0;
17931 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17932 VEC_address (offset_type, eb->cu_indices),
17933 sizeof (offset_type) * len);
17934}
17935
17936/* Destroy a symtab_index_entry. */
17937
17938static void
17939delete_symtab_entry (void *p)
17940{
17941 struct symtab_index_entry *entry = p;
17942 VEC_free (offset_type, entry->cu_indices);
17943 xfree (entry);
17944}
17945
17946/* Create a hash table holding symtab_index_entry objects. */
17947
17948static htab_t
17949create_symbol_hash_table (void)
17950{
17951 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17952 delete_symtab_entry, xcalloc, xfree);
17953}
17954
17955/* Create a new mapped symtab object. */
17956
17957static struct mapped_symtab *
17958create_mapped_symtab (void)
17959{
17960 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17961 symtab->n_elements = 0;
17962 symtab->size = 1024;
17963 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17964 return symtab;
17965}
17966
17967/* Destroy a mapped_symtab. */
17968
17969static void
17970cleanup_mapped_symtab (void *p)
17971{
17972 struct mapped_symtab *symtab = p;
17973 /* The contents of the array are freed when the other hash table is
17974 destroyed. */
17975 xfree (symtab->data);
17976 xfree (symtab);
17977}
17978
17979/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17980 the slot.
17981
17982 Function is used only during write_hash_table so no index format backward
17983 compatibility is needed. */
17984
17985static struct symtab_index_entry **
17986find_slot (struct mapped_symtab *symtab, const char *name)
17987{
17988 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17989
17990 index = hash & (symtab->size - 1);
17991 step = ((hash * 17) & (symtab->size - 1)) | 1;
17992
17993 for (;;)
17994 {
17995 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17996 return &symtab->data[index];
17997 index = (index + step) & (symtab->size - 1);
17998 }
17999}
18000
18001/* Expand SYMTAB's hash table. */
18002
18003static void
18004hash_expand (struct mapped_symtab *symtab)
18005{
18006 offset_type old_size = symtab->size;
18007 offset_type i;
18008 struct symtab_index_entry **old_entries = symtab->data;
18009
18010 symtab->size *= 2;
18011 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18012
18013 for (i = 0; i < old_size; ++i)
18014 {
18015 if (old_entries[i])
18016 {
18017 struct symtab_index_entry **slot = find_slot (symtab,
18018 old_entries[i]->name);
18019 *slot = old_entries[i];
18020 }
18021 }
18022
18023 xfree (old_entries);
18024}
18025
18026/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
18027 is the index of the CU in which the symbol appears. */
18028
18029static void
18030add_index_entry (struct mapped_symtab *symtab, const char *name,
18031 offset_type cu_index)
18032{
18033 struct symtab_index_entry **slot;
18034
18035 ++symtab->n_elements;
18036 if (4 * symtab->n_elements / 3 >= symtab->size)
18037 hash_expand (symtab);
18038
18039 slot = find_slot (symtab, name);
18040 if (!*slot)
18041 {
18042 *slot = XNEW (struct symtab_index_entry);
18043 (*slot)->name = name;
18044 (*slot)->cu_indices = NULL;
18045 }
18046 /* Don't push an index twice. Due to how we add entries we only
18047 have to check the last one. */
18048 if (VEC_empty (offset_type, (*slot)->cu_indices)
18049 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
18050 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
18051}
18052
18053/* Add a vector of indices to the constant pool. */
18054
18055static offset_type
18056add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
18057 struct symtab_index_entry *entry)
18058{
18059 void **slot;
18060
18061 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
18062 if (!*slot)
18063 {
18064 offset_type len = VEC_length (offset_type, entry->cu_indices);
18065 offset_type val = MAYBE_SWAP (len);
18066 offset_type iter;
18067 int i;
18068
18069 *slot = entry;
18070 entry->index_offset = obstack_object_size (cpool);
18071
18072 obstack_grow (cpool, &val, sizeof (val));
18073 for (i = 0;
18074 VEC_iterate (offset_type, entry->cu_indices, i, iter);
18075 ++i)
18076 {
18077 val = MAYBE_SWAP (iter);
18078 obstack_grow (cpool, &val, sizeof (val));
18079 }
18080 }
18081 else
18082 {
18083 struct symtab_index_entry *old_entry = *slot;
18084 entry->index_offset = old_entry->index_offset;
18085 entry = old_entry;
18086 }
18087 return entry->index_offset;
18088}
18089
18090/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
18091 constant pool entries going into the obstack CPOOL. */
18092
18093static void
18094write_hash_table (struct mapped_symtab *symtab,
18095 struct obstack *output, struct obstack *cpool)
18096{
18097 offset_type i;
18098 htab_t symbol_hash_table;
18099 htab_t str_table;
18100
18101 symbol_hash_table = create_symbol_hash_table ();
18102 str_table = create_strtab ();
18103
18104 /* We add all the index vectors to the constant pool first, to
18105 ensure alignment is ok. */
18106 for (i = 0; i < symtab->size; ++i)
18107 {
18108 if (symtab->data[i])
18109 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
18110 }
18111
18112 /* Now write out the hash table. */
18113 for (i = 0; i < symtab->size; ++i)
18114 {
18115 offset_type str_off, vec_off;
18116
18117 if (symtab->data[i])
18118 {
18119 str_off = add_string (str_table, cpool, symtab->data[i]->name);
18120 vec_off = symtab->data[i]->index_offset;
18121 }
18122 else
18123 {
18124 /* While 0 is a valid constant pool index, it is not valid
18125 to have 0 for both offsets. */
18126 str_off = 0;
18127 vec_off = 0;
18128 }
18129
18130 str_off = MAYBE_SWAP (str_off);
18131 vec_off = MAYBE_SWAP (vec_off);
18132
18133 obstack_grow (output, &str_off, sizeof (str_off));
18134 obstack_grow (output, &vec_off, sizeof (vec_off));
18135 }
18136
18137 htab_delete (str_table);
18138 htab_delete (symbol_hash_table);
18139}
18140
18141/* Struct to map psymtab to CU index in the index file. */
18142struct psymtab_cu_index_map
18143{
18144 struct partial_symtab *psymtab;
18145 unsigned int cu_index;
18146};
18147
18148static hashval_t
18149hash_psymtab_cu_index (const void *item)
18150{
18151 const struct psymtab_cu_index_map *map = item;
18152
18153 return htab_hash_pointer (map->psymtab);
18154}
18155
18156static int
18157eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
18158{
18159 const struct psymtab_cu_index_map *lhs = item_lhs;
18160 const struct psymtab_cu_index_map *rhs = item_rhs;
18161
18162 return lhs->psymtab == rhs->psymtab;
18163}
18164
18165/* Helper struct for building the address table. */
18166struct addrmap_index_data
18167{
18168 struct objfile *objfile;
18169 struct obstack *addr_obstack;
18170 htab_t cu_index_htab;
18171
18172 /* Non-zero if the previous_* fields are valid.
18173 We can't write an entry until we see the next entry (since it is only then
18174 that we know the end of the entry). */
18175 int previous_valid;
18176 /* Index of the CU in the table of all CUs in the index file. */
18177 unsigned int previous_cu_index;
18178 /* Start address of the CU. */
18179 CORE_ADDR previous_cu_start;
18180};
18181
18182/* Write an address entry to OBSTACK. */
18183
18184static void
18185add_address_entry (struct objfile *objfile, struct obstack *obstack,
18186 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
18187{
18188 offset_type cu_index_to_write;
18189 char addr[8];
18190 CORE_ADDR baseaddr;
18191
18192 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18193
18194 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
18195 obstack_grow (obstack, addr, 8);
18196 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
18197 obstack_grow (obstack, addr, 8);
18198 cu_index_to_write = MAYBE_SWAP (cu_index);
18199 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
18200}
18201
18202/* Worker function for traversing an addrmap to build the address table. */
18203
18204static int
18205add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
18206{
18207 struct addrmap_index_data *data = datap;
18208 struct partial_symtab *pst = obj;
18209 offset_type cu_index;
18210 void **slot;
18211
18212 if (data->previous_valid)
18213 add_address_entry (data->objfile, data->addr_obstack,
18214 data->previous_cu_start, start_addr,
18215 data->previous_cu_index);
18216
18217 data->previous_cu_start = start_addr;
18218 if (pst != NULL)
18219 {
18220 struct psymtab_cu_index_map find_map, *map;
18221 find_map.psymtab = pst;
18222 map = htab_find (data->cu_index_htab, &find_map);
18223 gdb_assert (map != NULL);
18224 data->previous_cu_index = map->cu_index;
18225 data->previous_valid = 1;
18226 }
18227 else
18228 data->previous_valid = 0;
18229
18230 return 0;
18231}
18232
18233/* Write OBJFILE's address map to OBSTACK.
18234 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
18235 in the index file. */
18236
18237static void
18238write_address_map (struct objfile *objfile, struct obstack *obstack,
18239 htab_t cu_index_htab)
18240{
18241 struct addrmap_index_data addrmap_index_data;
18242
18243 /* When writing the address table, we have to cope with the fact that
18244 the addrmap iterator only provides the start of a region; we have to
18245 wait until the next invocation to get the start of the next region. */
18246
18247 addrmap_index_data.objfile = objfile;
18248 addrmap_index_data.addr_obstack = obstack;
18249 addrmap_index_data.cu_index_htab = cu_index_htab;
18250 addrmap_index_data.previous_valid = 0;
18251
18252 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
18253 &addrmap_index_data);
18254
18255 /* It's highly unlikely the last entry (end address = 0xff...ff)
18256 is valid, but we should still handle it.
18257 The end address is recorded as the start of the next region, but that
18258 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
18259 anyway. */
18260 if (addrmap_index_data.previous_valid)
18261 add_address_entry (objfile, obstack,
18262 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
18263 addrmap_index_data.previous_cu_index);
18264}
18265
18266/* Add a list of partial symbols to SYMTAB. */
18267
18268static void
18269write_psymbols (struct mapped_symtab *symtab,
18270 htab_t psyms_seen,
18271 struct partial_symbol **psymp,
18272 int count,
18273 offset_type cu_index,
18274 int is_static)
18275{
18276 for (; count-- > 0; ++psymp)
18277 {
18278 void **slot, *lookup;
18279
18280 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
18281 error (_("Ada is not currently supported by the index"));
18282
18283 /* We only want to add a given psymbol once. However, we also
18284 want to account for whether it is global or static. So, we
18285 may add it twice, using slightly different values. */
18286 if (is_static)
18287 {
18288 uintptr_t val = 1 | (uintptr_t) *psymp;
18289
18290 lookup = (void *) val;
18291 }
18292 else
18293 lookup = *psymp;
18294
18295 /* Only add a given psymbol once. */
18296 slot = htab_find_slot (psyms_seen, lookup, INSERT);
18297 if (!*slot)
18298 {
18299 *slot = lookup;
18300 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
18301 }
18302 }
18303}
18304
18305/* Write the contents of an ("unfinished") obstack to FILE. Throw an
18306 exception if there is an error. */
18307
18308static void
18309write_obstack (FILE *file, struct obstack *obstack)
18310{
18311 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18312 file)
18313 != obstack_object_size (obstack))
18314 error (_("couldn't data write to file"));
18315}
18316
18317/* Unlink a file if the argument is not NULL. */
18318
18319static void
18320unlink_if_set (void *p)
18321{
18322 char **filename = p;
18323 if (*filename)
18324 unlink (*filename);
18325}
18326
18327/* A helper struct used when iterating over debug_types. */
18328struct signatured_type_index_data
18329{
18330 struct objfile *objfile;
18331 struct mapped_symtab *symtab;
18332 struct obstack *types_list;
18333 htab_t psyms_seen;
18334 int cu_index;
18335};
18336
18337/* A helper function that writes a single signatured_type to an
18338 obstack. */
18339
18340static int
18341write_one_signatured_type (void **slot, void *d)
18342{
18343 struct signatured_type_index_data *info = d;
18344 struct signatured_type *entry = (struct signatured_type *) *slot;
18345 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18346 struct partial_symtab *psymtab = per_cu->v.psymtab;
18347 gdb_byte val[8];
18348
18349 write_psymbols (info->symtab,
18350 info->psyms_seen,
18351 info->objfile->global_psymbols.list
18352 + psymtab->globals_offset,
18353 psymtab->n_global_syms, info->cu_index,
18354 0);
18355 write_psymbols (info->symtab,
18356 info->psyms_seen,
18357 info->objfile->static_psymbols.list
18358 + psymtab->statics_offset,
18359 psymtab->n_static_syms, info->cu_index,
18360 1);
18361
18362 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18363 entry->per_cu.offset.sect_off);
18364 obstack_grow (info->types_list, val, 8);
18365 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18366 entry->type_offset_in_tu.cu_off);
18367 obstack_grow (info->types_list, val, 8);
18368 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18369 obstack_grow (info->types_list, val, 8);
18370
18371 ++info->cu_index;
18372
18373 return 1;
18374}
18375
18376/* Create an index file for OBJFILE in the directory DIR. */
18377
18378static void
18379write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18380{
18381 struct cleanup *cleanup;
18382 char *filename, *cleanup_filename;
18383 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18384 struct obstack cu_list, types_cu_list;
18385 int i;
18386 FILE *out_file;
18387 struct mapped_symtab *symtab;
18388 offset_type val, size_of_contents, total_len;
18389 struct stat st;
18390 char buf[8];
18391 htab_t psyms_seen;
18392 htab_t cu_index_htab;
18393 struct psymtab_cu_index_map *psymtab_cu_index_map;
18394
18395 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
18396 return;
18397
18398 if (dwarf2_per_objfile->using_index)
18399 error (_("Cannot use an index to create the index"));
18400
18401 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18402 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18403
18404 if (stat (objfile->name, &st) < 0)
18405 perror_with_name (objfile->name);
18406
18407 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18408 INDEX_SUFFIX, (char *) NULL);
18409 cleanup = make_cleanup (xfree, filename);
18410
18411 out_file = fopen (filename, "wb");
18412 if (!out_file)
18413 error (_("Can't open `%s' for writing"), filename);
18414
18415 cleanup_filename = filename;
18416 make_cleanup (unlink_if_set, &cleanup_filename);
18417
18418 symtab = create_mapped_symtab ();
18419 make_cleanup (cleanup_mapped_symtab, symtab);
18420
18421 obstack_init (&addr_obstack);
18422 make_cleanup_obstack_free (&addr_obstack);
18423
18424 obstack_init (&cu_list);
18425 make_cleanup_obstack_free (&cu_list);
18426
18427 obstack_init (&types_cu_list);
18428 make_cleanup_obstack_free (&types_cu_list);
18429
18430 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18431 NULL, xcalloc, xfree);
18432 make_cleanup_htab_delete (psyms_seen);
18433
18434 /* While we're scanning CU's create a table that maps a psymtab pointer
18435 (which is what addrmap records) to its index (which is what is recorded
18436 in the index file). This will later be needed to write the address
18437 table. */
18438 cu_index_htab = htab_create_alloc (100,
18439 hash_psymtab_cu_index,
18440 eq_psymtab_cu_index,
18441 NULL, xcalloc, xfree);
18442 make_cleanup_htab_delete (cu_index_htab);
18443 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18444 xmalloc (sizeof (struct psymtab_cu_index_map)
18445 * dwarf2_per_objfile->n_comp_units);
18446 make_cleanup (xfree, psymtab_cu_index_map);
18447
18448 /* The CU list is already sorted, so we don't need to do additional
18449 work here. Also, the debug_types entries do not appear in
18450 all_comp_units, but only in their own hash table. */
18451 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18452 {
18453 struct dwarf2_per_cu_data *per_cu
18454 = dwarf2_per_objfile->all_comp_units[i];
18455 struct partial_symtab *psymtab = per_cu->v.psymtab;
18456 gdb_byte val[8];
18457 struct psymtab_cu_index_map *map;
18458 void **slot;
18459
18460 write_psymbols (symtab,
18461 psyms_seen,
18462 objfile->global_psymbols.list + psymtab->globals_offset,
18463 psymtab->n_global_syms, i,
18464 0);
18465 write_psymbols (symtab,
18466 psyms_seen,
18467 objfile->static_psymbols.list + psymtab->statics_offset,
18468 psymtab->n_static_syms, i,
18469 1);
18470
18471 map = &psymtab_cu_index_map[i];
18472 map->psymtab = psymtab;
18473 map->cu_index = i;
18474 slot = htab_find_slot (cu_index_htab, map, INSERT);
18475 gdb_assert (slot != NULL);
18476 gdb_assert (*slot == NULL);
18477 *slot = map;
18478
18479 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18480 per_cu->offset.sect_off);
18481 obstack_grow (&cu_list, val, 8);
18482 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
18483 obstack_grow (&cu_list, val, 8);
18484 }
18485
18486 /* Dump the address map. */
18487 write_address_map (objfile, &addr_obstack, cu_index_htab);
18488
18489 /* Write out the .debug_type entries, if any. */
18490 if (dwarf2_per_objfile->signatured_types)
18491 {
18492 struct signatured_type_index_data sig_data;
18493
18494 sig_data.objfile = objfile;
18495 sig_data.symtab = symtab;
18496 sig_data.types_list = &types_cu_list;
18497 sig_data.psyms_seen = psyms_seen;
18498 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18499 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18500 write_one_signatured_type, &sig_data);
18501 }
18502
18503 obstack_init (&constant_pool);
18504 make_cleanup_obstack_free (&constant_pool);
18505 obstack_init (&symtab_obstack);
18506 make_cleanup_obstack_free (&symtab_obstack);
18507 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18508
18509 obstack_init (&contents);
18510 make_cleanup_obstack_free (&contents);
18511 size_of_contents = 6 * sizeof (offset_type);
18512 total_len = size_of_contents;
18513
18514 /* The version number. */
18515 val = MAYBE_SWAP (6);
18516 obstack_grow (&contents, &val, sizeof (val));
18517
18518 /* The offset of the CU list from the start of the file. */
18519 val = MAYBE_SWAP (total_len);
18520 obstack_grow (&contents, &val, sizeof (val));
18521 total_len += obstack_object_size (&cu_list);
18522
18523 /* The offset of the types CU list from the start of the file. */
18524 val = MAYBE_SWAP (total_len);
18525 obstack_grow (&contents, &val, sizeof (val));
18526 total_len += obstack_object_size (&types_cu_list);
18527
18528 /* The offset of the address table from the start of the file. */
18529 val = MAYBE_SWAP (total_len);
18530 obstack_grow (&contents, &val, sizeof (val));
18531 total_len += obstack_object_size (&addr_obstack);
18532
18533 /* The offset of the symbol table from the start of the file. */
18534 val = MAYBE_SWAP (total_len);
18535 obstack_grow (&contents, &val, sizeof (val));
18536 total_len += obstack_object_size (&symtab_obstack);
18537
18538 /* The offset of the constant pool from the start of the file. */
18539 val = MAYBE_SWAP (total_len);
18540 obstack_grow (&contents, &val, sizeof (val));
18541 total_len += obstack_object_size (&constant_pool);
18542
18543 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18544
18545 write_obstack (out_file, &contents);
18546 write_obstack (out_file, &cu_list);
18547 write_obstack (out_file, &types_cu_list);
18548 write_obstack (out_file, &addr_obstack);
18549 write_obstack (out_file, &symtab_obstack);
18550 write_obstack (out_file, &constant_pool);
18551
18552 fclose (out_file);
18553
18554 /* We want to keep the file, so we set cleanup_filename to NULL
18555 here. See unlink_if_set. */
18556 cleanup_filename = NULL;
18557
18558 do_cleanups (cleanup);
18559}
18560
18561/* Implementation of the `save gdb-index' command.
18562
18563 Note that the file format used by this command is documented in the
18564 GDB manual. Any changes here must be documented there. */
18565
18566static void
18567save_gdb_index_command (char *arg, int from_tty)
18568{
18569 struct objfile *objfile;
18570
18571 if (!arg || !*arg)
18572 error (_("usage: save gdb-index DIRECTORY"));
18573
18574 ALL_OBJFILES (objfile)
18575 {
18576 struct stat st;
18577
18578 /* If the objfile does not correspond to an actual file, skip it. */
18579 if (stat (objfile->name, &st) < 0)
18580 continue;
18581
18582 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18583 if (dwarf2_per_objfile)
18584 {
18585 volatile struct gdb_exception except;
18586
18587 TRY_CATCH (except, RETURN_MASK_ERROR)
18588 {
18589 write_psymtabs_to_index (objfile, arg);
18590 }
18591 if (except.reason < 0)
18592 exception_fprintf (gdb_stderr, except,
18593 _("Error while writing index for `%s': "),
18594 objfile->name);
18595 }
18596 }
18597}
18598
18599\f
18600
18601int dwarf2_always_disassemble;
18602
18603static void
18604show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18605 struct cmd_list_element *c, const char *value)
18606{
18607 fprintf_filtered (file,
18608 _("Whether to always disassemble "
18609 "DWARF expressions is %s.\n"),
18610 value);
18611}
18612
18613static void
18614show_check_physname (struct ui_file *file, int from_tty,
18615 struct cmd_list_element *c, const char *value)
18616{
18617 fprintf_filtered (file,
18618 _("Whether to check \"physname\" is %s.\n"),
18619 value);
18620}
18621
18622void _initialize_dwarf2_read (void);
18623
18624void
18625_initialize_dwarf2_read (void)
18626{
18627 struct cmd_list_element *c;
18628
18629 dwarf2_objfile_data_key
18630 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
18631
18632 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18633Set DWARF 2 specific variables.\n\
18634Configure DWARF 2 variables such as the cache size"),
18635 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18636 0/*allow-unknown*/, &maintenance_set_cmdlist);
18637
18638 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18639Show DWARF 2 specific variables\n\
18640Show DWARF 2 variables such as the cache size"),
18641 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18642 0/*allow-unknown*/, &maintenance_show_cmdlist);
18643
18644 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
18645 &dwarf2_max_cache_age, _("\
18646Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18647Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18648A higher limit means that cached compilation units will be stored\n\
18649in memory longer, and more total memory will be used. Zero disables\n\
18650caching, which can slow down startup."),
18651 NULL,
18652 show_dwarf2_max_cache_age,
18653 &set_dwarf2_cmdlist,
18654 &show_dwarf2_cmdlist);
18655
18656 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18657 &dwarf2_always_disassemble, _("\
18658Set whether `info address' always disassembles DWARF expressions."), _("\
18659Show whether `info address' always disassembles DWARF expressions."), _("\
18660When enabled, DWARF expressions are always printed in an assembly-like\n\
18661syntax. When disabled, expressions will be printed in a more\n\
18662conversational style, when possible."),
18663 NULL,
18664 show_dwarf2_always_disassemble,
18665 &set_dwarf2_cmdlist,
18666 &show_dwarf2_cmdlist);
18667
18668 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18669Set debugging of the dwarf2 DIE reader."), _("\
18670Show debugging of the dwarf2 DIE reader."), _("\
18671When enabled (non-zero), DIEs are dumped after they are read in.\n\
18672The value is the maximum depth to print."),
18673 NULL,
18674 NULL,
18675 &setdebuglist, &showdebuglist);
18676
18677 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18678Set cross-checking of \"physname\" code against demangler."), _("\
18679Show cross-checking of \"physname\" code against demangler."), _("\
18680When enabled, GDB's internal \"physname\" code is checked against\n\
18681the demangler."),
18682 NULL, show_check_physname,
18683 &setdebuglist, &showdebuglist);
18684
18685 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18686 _("\
18687Save a gdb-index file.\n\
18688Usage: save gdb-index DIRECTORY"),
18689 &save_cmdlist);
18690 set_cmd_completer (c, filename_completer);
18691}
This page took 0.088902 seconds and 4 git commands to generate.