* dwarf2read.c (dw2_find_symbol_file): Don't crash if there are no files.
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* 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
81 typedef struct symbol *symbolp;
82 DEF_VEC_P (symbolp);
83
84 /* When non-zero, dump DIEs after they are read in. */
85 static int dwarf2_die_debug = 0;
86
87 /* When non-zero, cross-check physname against demangler. */
88 static int check_physname = 0;
89
90 /* When non-zero, do not reject deprecated .gdb_index sections. */
91 int use_deprecated_index_sections = 0;
92
93 static 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
99 static int processing_has_namespace_info;
100
101 static const struct objfile_data *dwarf2_objfile_data_key;
102
103 struct 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
116 typedef struct dwarf2_section_info dwarf2_section_info_def;
117 DEF_VEC_O (dwarf2_section_info_def);
118
119 /* All offsets in the index are of this type. It must be
120 architecture-independent. */
121 typedef uint32_t offset_type;
122
123 DEF_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. */
127 struct 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
154 struct 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
226 static 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
233 static 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
253 static 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 }
263 dwo_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. */
283 struct 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. */
308 struct 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
326 typedef struct delayed_method_info delayed_method_info;
327 DEF_VEC_O (delayed_method_info);
328
329 /* Internal state when decoding a particular compilation unit. */
330 struct 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
442 struct 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
497 struct 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
519 struct 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
532 struct 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
555 struct 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
581 struct 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_cutu_and_read_dies, et.al. */
601 typedef 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. */
610 struct 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. */
656 struct 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. */
722 struct 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
732 struct 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. */
739 struct 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. */
762 struct 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. */
805 struct 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. */
825 static 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. */
830 struct 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. */
887 struct dwarf2_queue_item
888 {
889 struct dwarf2_per_cu_data *per_cu;
890 struct dwarf2_queue_item *next;
891 };
892
893 /* The current queue. */
894 static 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. */
901 static int dwarf2_max_cache_age = 5;
902 static void
903 show_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
914 static void
915 dwarf2_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
921 static void
922 dwarf2_debug_line_missing_file_complaint (void)
923 {
924 complaint (&symfile_complaints,
925 _(".debug_line section has line data without a file"));
926 }
927
928 static void
929 dwarf2_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
936 static void
937 dwarf2_complex_location_expr_complaint (void)
938 {
939 complaint (&symfile_complaints, _("location expression too complex"));
940 }
941
942 static void
943 dwarf2_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
951 static void
952 dwarf2_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
959 static void
960 dwarf2_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
968 static void
969 dwarf2_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
978 static void dwarf2_locate_sections (bfd *, asection *, void *);
979
980 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
981 struct objfile *);
982
983 static void dwarf2_find_base_address (struct die_info *die,
984 struct dwarf2_cu *cu);
985
986 static void dwarf2_build_psymtabs_hard (struct objfile *);
987
988 static void scan_partial_symbols (struct partial_die_info *,
989 CORE_ADDR *, CORE_ADDR *,
990 int, struct dwarf2_cu *);
991
992 static void add_partial_symbol (struct partial_die_info *,
993 struct dwarf2_cu *);
994
995 static 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
999 static 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
1003 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1004 struct dwarf2_cu *cu);
1005
1006 static 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
1010 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1011
1012 static void psymtab_to_symtab_1 (struct partial_symtab *);
1013
1014 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1015 struct dwarf2_section_info *);
1016
1017 static void dwarf2_free_abbrev_table (void *);
1018
1019 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1020
1021 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
1022 struct dwarf2_cu *);
1023
1024 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
1025 struct dwarf2_cu *);
1026
1027 static struct partial_die_info *load_partial_dies
1028 (const struct die_reader_specs *, gdb_byte *, int);
1029
1030 static 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
1036 static struct partial_die_info *find_partial_die (sect_offset,
1037 struct dwarf2_cu *);
1038
1039 static void fixup_partial_die (struct partial_die_info *,
1040 struct dwarf2_cu *);
1041
1042 static gdb_byte *read_attribute (const struct die_reader_specs *,
1043 struct attribute *, struct attr_abbrev *,
1044 gdb_byte *);
1045
1046 static unsigned int read_1_byte (bfd *, gdb_byte *);
1047
1048 static int read_1_signed_byte (bfd *, gdb_byte *);
1049
1050 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1051
1052 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1053
1054 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1055
1056 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1057 unsigned int *);
1058
1059 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1060
1061 static LONGEST read_checked_initial_length_and_offset
1062 (bfd *, gdb_byte *, const struct comp_unit_head *,
1063 unsigned int *, unsigned int *);
1064
1065 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1066 unsigned int *);
1067
1068 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1069
1070 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1071
1072 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1073
1074 static char *read_indirect_string (bfd *, gdb_byte *,
1075 const struct comp_unit_head *,
1076 unsigned int *);
1077
1078 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1079
1080 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1081
1082 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1083 unsigned int *);
1084
1085 static char *read_str_index (const struct die_reader_specs *reader,
1086 struct dwarf2_cu *cu, ULONGEST str_index);
1087
1088 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1089
1090 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1091
1092 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1093 struct dwarf2_cu *);
1094
1095 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1096 unsigned int,
1097 struct dwarf2_cu *);
1098
1099 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1100 struct dwarf2_cu *cu);
1101
1102 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1103
1104 static struct die_info *die_specification (struct die_info *die,
1105 struct dwarf2_cu **);
1106
1107 static void free_line_header (struct line_header *lh);
1108
1109 static void add_file_name (struct line_header *, char *, unsigned int,
1110 unsigned int, unsigned int);
1111
1112 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1113 struct dwarf2_cu *cu);
1114
1115 static void dwarf_decode_lines (struct line_header *, const char *,
1116 struct dwarf2_cu *, struct partial_symtab *,
1117 int);
1118
1119 static void dwarf2_start_subfile (char *, const char *, const char *);
1120
1121 static struct symbol *new_symbol (struct die_info *, struct type *,
1122 struct dwarf2_cu *);
1123
1124 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1125 struct dwarf2_cu *, struct symbol *);
1126
1127 static void dwarf2_const_value (struct attribute *, struct symbol *,
1128 struct dwarf2_cu *);
1129
1130 static 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
1138 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1139
1140 static int need_gnat_info (struct dwarf2_cu *);
1141
1142 static struct type *die_descriptive_type (struct die_info *,
1143 struct dwarf2_cu *);
1144
1145 static void set_descriptive_type (struct type *, struct die_info *,
1146 struct dwarf2_cu *);
1147
1148 static struct type *die_containing_type (struct die_info *,
1149 struct dwarf2_cu *);
1150
1151 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1152 struct dwarf2_cu *);
1153
1154 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1155
1156 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1157
1158 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1159
1160 static char *typename_concat (struct obstack *obs, const char *prefix,
1161 const char *suffix, int physname,
1162 struct dwarf2_cu *cu);
1163
1164 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1165
1166 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1167
1168 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1169
1170 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1171
1172 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1173
1174 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1175 struct dwarf2_cu *, struct partial_symtab *);
1176
1177 static int dwarf2_get_pc_bounds (struct die_info *,
1178 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1179 struct partial_symtab *);
1180
1181 static void get_scope_pc_bounds (struct die_info *,
1182 CORE_ADDR *, CORE_ADDR *,
1183 struct dwarf2_cu *);
1184
1185 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1186 CORE_ADDR, struct dwarf2_cu *);
1187
1188 static void dwarf2_add_field (struct field_info *, struct die_info *,
1189 struct dwarf2_cu *);
1190
1191 static void dwarf2_attach_fields_to_type (struct field_info *,
1192 struct type *, struct dwarf2_cu *);
1193
1194 static void dwarf2_add_member_fn (struct field_info *,
1195 struct die_info *, struct type *,
1196 struct dwarf2_cu *);
1197
1198 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1199 struct type *,
1200 struct dwarf2_cu *);
1201
1202 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1203
1204 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1205
1206 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1207
1208 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1209
1210 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1211
1212 static struct type *read_module_type (struct die_info *die,
1213 struct dwarf2_cu *cu);
1214
1215 static const char *namespace_name (struct die_info *die,
1216 int *is_anonymous, struct dwarf2_cu *);
1217
1218 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1219
1220 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1221
1222 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1223 struct dwarf2_cu *);
1224
1225 static 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
1230 static 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
1235 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1236 struct die_info **, gdb_byte *, int *, int);
1237
1238 static gdb_byte *read_full_die (const struct die_reader_specs *,
1239 struct die_info **, gdb_byte *, int *);
1240
1241 static void process_die (struct die_info *, struct dwarf2_cu *);
1242
1243 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1244 struct obstack *);
1245
1246 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1247
1248 static const char *dwarf2_full_name (char *name,
1249 struct die_info *die,
1250 struct dwarf2_cu *cu);
1251
1252 static struct die_info *dwarf2_extension (struct die_info *die,
1253 struct dwarf2_cu **);
1254
1255 static char *dwarf_tag_name (unsigned int);
1256
1257 static char *dwarf_attr_name (unsigned int);
1258
1259 static char *dwarf_form_name (unsigned int);
1260
1261 static char *dwarf_bool_name (unsigned int);
1262
1263 static char *dwarf_type_encoding_name (unsigned int);
1264
1265 #if 0
1266 static char *dwarf_cfi_name (unsigned int);
1267 #endif
1268
1269 static struct die_info *sibling_die (struct die_info *);
1270
1271 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1272
1273 static void dump_die_for_error (struct die_info *);
1274
1275 static 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
1280 static void store_in_ref_table (struct die_info *,
1281 struct dwarf2_cu *);
1282
1283 static int is_ref_attr (struct attribute *);
1284
1285 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1286
1287 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1288
1289 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1290 struct attribute *,
1291 struct dwarf2_cu **);
1292
1293 static struct die_info *follow_die_ref (struct die_info *,
1294 struct attribute *,
1295 struct dwarf2_cu **);
1296
1297 static struct die_info *follow_die_sig (struct die_info *,
1298 struct attribute *,
1299 struct dwarf2_cu **);
1300
1301 static struct signatured_type *lookup_signatured_type_at_offset
1302 (struct objfile *objfile,
1303 struct dwarf2_section_info *section, sect_offset offset);
1304
1305 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1306
1307 static void read_signatured_type (struct signatured_type *);
1308
1309 /* memory allocation interface */
1310
1311 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1312
1313 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1314
1315 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1316
1317 static void dwarf_decode_macros (struct line_header *, unsigned int,
1318 char *, bfd *, struct dwarf2_cu *,
1319 struct dwarf2_section_info *,
1320 int, const char *);
1321
1322 static int attr_form_is_block (struct attribute *);
1323
1324 static int attr_form_is_section_offset (struct attribute *);
1325
1326 static int attr_form_is_constant (struct attribute *);
1327
1328 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1329 struct dwarf2_loclist_baton *baton,
1330 struct attribute *attr);
1331
1332 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1333 struct symbol *sym,
1334 struct dwarf2_cu *cu);
1335
1336 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1337 gdb_byte *info_ptr,
1338 struct abbrev_info *abbrev);
1339
1340 static void free_stack_comp_unit (void *);
1341
1342 static hashval_t partial_die_hash (const void *item);
1343
1344 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1345
1346 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1347 (sect_offset offset, struct objfile *objfile);
1348
1349 static void init_one_comp_unit (struct dwarf2_cu *cu,
1350 struct dwarf2_per_cu_data *per_cu);
1351
1352 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1353 struct die_info *comp_unit_die);
1354
1355 static void free_heap_comp_unit (void *);
1356
1357 static void free_cached_comp_units (void *);
1358
1359 static void age_cached_comp_units (void);
1360
1361 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1362
1363 static struct type *set_die_type (struct die_info *, struct type *,
1364 struct dwarf2_cu *);
1365
1366 static void create_all_comp_units (struct objfile *);
1367
1368 static int create_all_type_units (struct objfile *);
1369
1370 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1371
1372 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1373
1374 static void dwarf2_add_dependence (struct dwarf2_cu *,
1375 struct dwarf2_per_cu_data *);
1376
1377 static void dwarf2_mark (struct dwarf2_cu *);
1378
1379 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1380
1381 static struct type *get_die_type_at_offset (sect_offset,
1382 struct dwarf2_per_cu_data *per_cu);
1383
1384 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1385
1386 static void dwarf2_release_queue (void *dummy);
1387
1388 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1389
1390 static void process_queue (void);
1391
1392 static void find_file_and_directory (struct die_info *die,
1393 struct dwarf2_cu *cu,
1394 char **name, char **comp_dir);
1395
1396 static char *file_full_name (int file, struct line_header *lh,
1397 const char *comp_dir);
1398
1399 static 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
1404 static void init_cutu_and_read_dies
1405 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
1406 die_reader_func_ftype *die_reader_func, void *data);
1407
1408 static 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
1412 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1413
1414 static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *);
1415
1416 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1417
1418 static struct dwo_unit *lookup_dwo_comp_unit
1419 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1420
1421 static struct dwo_unit *lookup_dwo_type_unit
1422 (struct signatured_type *, char *, const char *);
1423
1424 static void free_dwo_file_cleanup (void *);
1425
1426 static void munmap_section_buffer (struct dwarf2_section_info *);
1427
1428 #if WORDS_BIGENDIAN
1429
1430 /* Convert VALUE between big- and little-endian. */
1431 static offset_type
1432 byte_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
1452 static 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
1460 int
1461 dwarf2_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
1486 static int
1487 section_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
1503 static void
1504 dwarf2_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
1598 static void
1599 zlib_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 or not present. */
1676
1677 static int
1678 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1679 {
1680 return info->asection == NULL || info->size == 0;
1681 }
1682
1683 /* Read the contents of the section INFO.
1684 OBJFILE is the main object file, but not necessarily the file where
1685 the section comes from. E.g., for DWO files INFO->asection->owner
1686 is the bfd of the DWO file.
1687 If the section is compressed, uncompress it before returning. */
1688
1689 static void
1690 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1691 {
1692 asection *sectp = info->asection;
1693 bfd *abfd;
1694 gdb_byte *buf, *retbuf;
1695 unsigned char header[4];
1696
1697 if (info->readin)
1698 return;
1699 info->buffer = NULL;
1700 info->map_addr = NULL;
1701 info->readin = 1;
1702
1703 if (dwarf2_section_empty_p (info))
1704 return;
1705
1706 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1707 abfd = sectp->owner;
1708
1709 /* Check if the file has a 4-byte header indicating compression. */
1710 if (info->size > sizeof (header)
1711 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1712 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1713 {
1714 /* Upon decompression, update the buffer and its size. */
1715 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1716 {
1717 zlib_decompress_section (objfile, sectp, &info->buffer,
1718 &info->size);
1719 return;
1720 }
1721 }
1722
1723 #ifdef HAVE_MMAP
1724 if (pagesize == 0)
1725 pagesize = getpagesize ();
1726
1727 /* Only try to mmap sections which are large enough: we don't want to
1728 waste space due to fragmentation. Also, only try mmap for sections
1729 without relocations. */
1730
1731 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1732 {
1733 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1734 MAP_PRIVATE, sectp->filepos,
1735 &info->map_addr, &info->map_len);
1736
1737 if ((caddr_t)info->buffer != MAP_FAILED)
1738 {
1739 #if HAVE_POSIX_MADVISE
1740 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1741 #endif
1742 return;
1743 }
1744 }
1745 #endif
1746
1747 /* If we get here, we are a normal, not-compressed section. */
1748 info->buffer = buf
1749 = obstack_alloc (&objfile->objfile_obstack, info->size);
1750
1751 /* When debugging .o files, we may need to apply relocations; see
1752 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1753 We never compress sections in .o files, so we only need to
1754 try this when the section is not compressed. */
1755 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1756 if (retbuf != NULL)
1757 {
1758 info->buffer = retbuf;
1759 return;
1760 }
1761
1762 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1763 || bfd_bread (buf, info->size, abfd) != info->size)
1764 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1765 bfd_get_filename (abfd));
1766 }
1767
1768 /* A helper function that returns the size of a section in a safe way.
1769 If you are positive that the section has been read before using the
1770 size, then it is safe to refer to the dwarf2_section_info object's
1771 "size" field directly. In other cases, you must call this
1772 function, because for compressed sections the size field is not set
1773 correctly until the section has been read. */
1774
1775 static bfd_size_type
1776 dwarf2_section_size (struct objfile *objfile,
1777 struct dwarf2_section_info *info)
1778 {
1779 if (!info->readin)
1780 dwarf2_read_section (objfile, info);
1781 return info->size;
1782 }
1783
1784 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1785 SECTION_NAME. */
1786
1787 void
1788 dwarf2_get_section_info (struct objfile *objfile,
1789 enum dwarf2_section_enum sect,
1790 asection **sectp, gdb_byte **bufp,
1791 bfd_size_type *sizep)
1792 {
1793 struct dwarf2_per_objfile *data
1794 = objfile_data (objfile, dwarf2_objfile_data_key);
1795 struct dwarf2_section_info *info;
1796
1797 /* We may see an objfile without any DWARF, in which case we just
1798 return nothing. */
1799 if (data == NULL)
1800 {
1801 *sectp = NULL;
1802 *bufp = NULL;
1803 *sizep = 0;
1804 return;
1805 }
1806 switch (sect)
1807 {
1808 case DWARF2_DEBUG_FRAME:
1809 info = &data->frame;
1810 break;
1811 case DWARF2_EH_FRAME:
1812 info = &data->eh_frame;
1813 break;
1814 default:
1815 gdb_assert_not_reached ("unexpected section");
1816 }
1817
1818 dwarf2_read_section (objfile, info);
1819
1820 *sectp = info->asection;
1821 *bufp = info->buffer;
1822 *sizep = info->size;
1823 }
1824
1825 \f
1826 /* DWARF quick_symbols_functions support. */
1827
1828 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1829 unique line tables, so we maintain a separate table of all .debug_line
1830 derived entries to support the sharing.
1831 All the quick functions need is the list of file names. We discard the
1832 line_header when we're done and don't need to record it here. */
1833 struct quick_file_names
1834 {
1835 /* The offset in .debug_line of the line table. We hash on this. */
1836 unsigned int offset;
1837
1838 /* The number of entries in file_names, real_names. */
1839 unsigned int num_file_names;
1840
1841 /* The file names from the line table, after being run through
1842 file_full_name. */
1843 const char **file_names;
1844
1845 /* The file names from the line table after being run through
1846 gdb_realpath. These are computed lazily. */
1847 const char **real_names;
1848 };
1849
1850 /* When using the index (and thus not using psymtabs), each CU has an
1851 object of this type. This is used to hold information needed by
1852 the various "quick" methods. */
1853 struct dwarf2_per_cu_quick_data
1854 {
1855 /* The file table. This can be NULL if there was no file table
1856 or it's currently not read in.
1857 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1858 struct quick_file_names *file_names;
1859
1860 /* The corresponding symbol table. This is NULL if symbols for this
1861 CU have not yet been read. */
1862 struct symtab *symtab;
1863
1864 /* A temporary mark bit used when iterating over all CUs in
1865 expand_symtabs_matching. */
1866 unsigned int mark : 1;
1867
1868 /* True if we've tried to read the file table and found there isn't one.
1869 There will be no point in trying to read it again next time. */
1870 unsigned int no_file_data : 1;
1871 };
1872
1873 /* Hash function for a quick_file_names. */
1874
1875 static hashval_t
1876 hash_file_name_entry (const void *e)
1877 {
1878 const struct quick_file_names *file_data = e;
1879
1880 return file_data->offset;
1881 }
1882
1883 /* Equality function for a quick_file_names. */
1884
1885 static int
1886 eq_file_name_entry (const void *a, const void *b)
1887 {
1888 const struct quick_file_names *ea = a;
1889 const struct quick_file_names *eb = b;
1890
1891 return ea->offset == eb->offset;
1892 }
1893
1894 /* Delete function for a quick_file_names. */
1895
1896 static void
1897 delete_file_name_entry (void *e)
1898 {
1899 struct quick_file_names *file_data = e;
1900 int i;
1901
1902 for (i = 0; i < file_data->num_file_names; ++i)
1903 {
1904 xfree ((void*) file_data->file_names[i]);
1905 if (file_data->real_names)
1906 xfree ((void*) file_data->real_names[i]);
1907 }
1908
1909 /* The space for the struct itself lives on objfile_obstack,
1910 so we don't free it here. */
1911 }
1912
1913 /* Create a quick_file_names hash table. */
1914
1915 static htab_t
1916 create_quick_file_names_table (unsigned int nr_initial_entries)
1917 {
1918 return htab_create_alloc (nr_initial_entries,
1919 hash_file_name_entry, eq_file_name_entry,
1920 delete_file_name_entry, xcalloc, xfree);
1921 }
1922
1923 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1924 have to be created afterwards. You should call age_cached_comp_units after
1925 processing PER_CU->CU. dw2_setup must have been already called. */
1926
1927 static void
1928 load_cu (struct dwarf2_per_cu_data *per_cu)
1929 {
1930 if (per_cu->is_debug_types)
1931 load_full_type_unit (per_cu);
1932 else
1933 load_full_comp_unit (per_cu);
1934
1935 gdb_assert (per_cu->cu != NULL);
1936
1937 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1938 }
1939
1940 /* Read in the symbols for PER_CU. */
1941
1942 static void
1943 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1944 {
1945 struct cleanup *back_to;
1946
1947 back_to = make_cleanup (dwarf2_release_queue, NULL);
1948
1949 queue_comp_unit (per_cu);
1950
1951 load_cu (per_cu);
1952
1953 process_queue ();
1954
1955 /* Age the cache, releasing compilation units that have not
1956 been used recently. */
1957 age_cached_comp_units ();
1958
1959 do_cleanups (back_to);
1960 }
1961
1962 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1963 the objfile from which this CU came. Returns the resulting symbol
1964 table. */
1965
1966 static struct symtab *
1967 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1968 {
1969 if (!per_cu->v.quick->symtab)
1970 {
1971 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1972 increment_reading_symtab ();
1973 dw2_do_instantiate_symtab (per_cu);
1974 do_cleanups (back_to);
1975 }
1976 return per_cu->v.quick->symtab;
1977 }
1978
1979 /* Return the CU given its index. */
1980
1981 static struct dwarf2_per_cu_data *
1982 dw2_get_cu (int index)
1983 {
1984 if (index >= dwarf2_per_objfile->n_comp_units)
1985 {
1986 index -= dwarf2_per_objfile->n_comp_units;
1987 return dwarf2_per_objfile->all_type_units[index];
1988 }
1989 return dwarf2_per_objfile->all_comp_units[index];
1990 }
1991
1992 /* A helper function that knows how to read a 64-bit value in a way
1993 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1994 otherwise. */
1995
1996 static int
1997 extract_cu_value (const char *bytes, ULONGEST *result)
1998 {
1999 if (sizeof (ULONGEST) < 8)
2000 {
2001 int i;
2002
2003 /* Ignore the upper 4 bytes if they are all zero. */
2004 for (i = 0; i < 4; ++i)
2005 if (bytes[i + 4] != 0)
2006 return 0;
2007
2008 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2009 }
2010 else
2011 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2012 return 1;
2013 }
2014
2015 /* Read the CU list from the mapped index, and use it to create all
2016 the CU objects for this objfile. Return 0 if something went wrong,
2017 1 if everything went ok. */
2018
2019 static int
2020 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2021 offset_type cu_list_elements)
2022 {
2023 offset_type i;
2024
2025 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2026 dwarf2_per_objfile->all_comp_units
2027 = obstack_alloc (&objfile->objfile_obstack,
2028 dwarf2_per_objfile->n_comp_units
2029 * sizeof (struct dwarf2_per_cu_data *));
2030
2031 for (i = 0; i < cu_list_elements; i += 2)
2032 {
2033 struct dwarf2_per_cu_data *the_cu;
2034 ULONGEST offset, length;
2035
2036 if (!extract_cu_value (cu_list, &offset)
2037 || !extract_cu_value (cu_list + 8, &length))
2038 return 0;
2039 cu_list += 2 * 8;
2040
2041 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2042 struct dwarf2_per_cu_data);
2043 the_cu->offset.sect_off = offset;
2044 the_cu->length = length;
2045 the_cu->objfile = objfile;
2046 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2047 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2048 struct dwarf2_per_cu_quick_data);
2049 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2050 }
2051
2052 return 1;
2053 }
2054
2055 /* Create the signatured type hash table from the index. */
2056
2057 static int
2058 create_signatured_type_table_from_index (struct objfile *objfile,
2059 struct dwarf2_section_info *section,
2060 const gdb_byte *bytes,
2061 offset_type elements)
2062 {
2063 offset_type i;
2064 htab_t sig_types_hash;
2065
2066 dwarf2_per_objfile->n_type_units = elements / 3;
2067 dwarf2_per_objfile->all_type_units
2068 = obstack_alloc (&objfile->objfile_obstack,
2069 dwarf2_per_objfile->n_type_units
2070 * sizeof (struct dwarf2_per_cu_data *));
2071
2072 sig_types_hash = allocate_signatured_type_table (objfile);
2073
2074 for (i = 0; i < elements; i += 3)
2075 {
2076 struct signatured_type *sig_type;
2077 ULONGEST offset, type_offset_in_tu, signature;
2078 void **slot;
2079
2080 if (!extract_cu_value (bytes, &offset)
2081 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2082 return 0;
2083 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2084 bytes += 3 * 8;
2085
2086 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2087 struct signatured_type);
2088 sig_type->signature = signature;
2089 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2090 sig_type->per_cu.is_debug_types = 1;
2091 sig_type->per_cu.info_or_types_section = section;
2092 sig_type->per_cu.offset.sect_off = offset;
2093 sig_type->per_cu.objfile = objfile;
2094 sig_type->per_cu.v.quick
2095 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2096 struct dwarf2_per_cu_quick_data);
2097
2098 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2099 *slot = sig_type;
2100
2101 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2102 }
2103
2104 dwarf2_per_objfile->signatured_types = sig_types_hash;
2105
2106 return 1;
2107 }
2108
2109 /* Read the address map data from the mapped index, and use it to
2110 populate the objfile's psymtabs_addrmap. */
2111
2112 static void
2113 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2114 {
2115 const gdb_byte *iter, *end;
2116 struct obstack temp_obstack;
2117 struct addrmap *mutable_map;
2118 struct cleanup *cleanup;
2119 CORE_ADDR baseaddr;
2120
2121 obstack_init (&temp_obstack);
2122 cleanup = make_cleanup_obstack_free (&temp_obstack);
2123 mutable_map = addrmap_create_mutable (&temp_obstack);
2124
2125 iter = index->address_table;
2126 end = iter + index->address_table_size;
2127
2128 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2129
2130 while (iter < end)
2131 {
2132 ULONGEST hi, lo, cu_index;
2133 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2134 iter += 8;
2135 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2136 iter += 8;
2137 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2138 iter += 4;
2139
2140 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2141 dw2_get_cu (cu_index));
2142 }
2143
2144 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2145 &objfile->objfile_obstack);
2146 do_cleanups (cleanup);
2147 }
2148
2149 /* The hash function for strings in the mapped index. This is the same as
2150 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2151 implementation. This is necessary because the hash function is tied to the
2152 format of the mapped index file. The hash values do not have to match with
2153 SYMBOL_HASH_NEXT.
2154
2155 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2156
2157 static hashval_t
2158 mapped_index_string_hash (int index_version, const void *p)
2159 {
2160 const unsigned char *str = (const unsigned char *) p;
2161 hashval_t r = 0;
2162 unsigned char c;
2163
2164 while ((c = *str++) != 0)
2165 {
2166 if (index_version >= 5)
2167 c = tolower (c);
2168 r = r * 67 + c - 113;
2169 }
2170
2171 return r;
2172 }
2173
2174 /* Find a slot in the mapped index INDEX for the object named NAME.
2175 If NAME is found, set *VEC_OUT to point to the CU vector in the
2176 constant pool and return 1. If NAME cannot be found, return 0. */
2177
2178 static int
2179 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2180 offset_type **vec_out)
2181 {
2182 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2183 offset_type hash;
2184 offset_type slot, step;
2185 int (*cmp) (const char *, const char *);
2186
2187 if (current_language->la_language == language_cplus
2188 || current_language->la_language == language_java
2189 || current_language->la_language == language_fortran)
2190 {
2191 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2192 not contain any. */
2193 const char *paren = strchr (name, '(');
2194
2195 if (paren)
2196 {
2197 char *dup;
2198
2199 dup = xmalloc (paren - name + 1);
2200 memcpy (dup, name, paren - name);
2201 dup[paren - name] = 0;
2202
2203 make_cleanup (xfree, dup);
2204 name = dup;
2205 }
2206 }
2207
2208 /* Index version 4 did not support case insensitive searches. But the
2209 indices for case insensitive languages are built in lowercase, therefore
2210 simulate our NAME being searched is also lowercased. */
2211 hash = mapped_index_string_hash ((index->version == 4
2212 && case_sensitivity == case_sensitive_off
2213 ? 5 : index->version),
2214 name);
2215
2216 slot = hash & (index->symbol_table_slots - 1);
2217 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2218 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2219
2220 for (;;)
2221 {
2222 /* Convert a slot number to an offset into the table. */
2223 offset_type i = 2 * slot;
2224 const char *str;
2225 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2226 {
2227 do_cleanups (back_to);
2228 return 0;
2229 }
2230
2231 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2232 if (!cmp (name, str))
2233 {
2234 *vec_out = (offset_type *) (index->constant_pool
2235 + MAYBE_SWAP (index->symbol_table[i + 1]));
2236 do_cleanups (back_to);
2237 return 1;
2238 }
2239
2240 slot = (slot + step) & (index->symbol_table_slots - 1);
2241 }
2242 }
2243
2244 /* Read the index file. If everything went ok, initialize the "quick"
2245 elements of all the CUs and return 1. Otherwise, return 0. */
2246
2247 static int
2248 dwarf2_read_index (struct objfile *objfile)
2249 {
2250 char *addr;
2251 struct mapped_index *map;
2252 offset_type *metadata;
2253 const gdb_byte *cu_list;
2254 const gdb_byte *types_list = NULL;
2255 offset_type version, cu_list_elements;
2256 offset_type types_list_elements = 0;
2257 int i;
2258
2259 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2260 return 0;
2261
2262 /* Older elfutils strip versions could keep the section in the main
2263 executable while splitting it for the separate debug info file. */
2264 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2265 & SEC_HAS_CONTENTS) == 0)
2266 return 0;
2267
2268 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2269
2270 addr = dwarf2_per_objfile->gdb_index.buffer;
2271 /* Version check. */
2272 version = MAYBE_SWAP (*(offset_type *) addr);
2273 /* Versions earlier than 3 emitted every copy of a psymbol. This
2274 causes the index to behave very poorly for certain requests. Version 3
2275 contained incomplete addrmap. So, it seems better to just ignore such
2276 indices. */
2277 if (version < 4)
2278 {
2279 static int warning_printed = 0;
2280 if (!warning_printed)
2281 {
2282 warning (_("Skipping obsolete .gdb_index section in %s."),
2283 objfile->name);
2284 warning_printed = 1;
2285 }
2286 return 0;
2287 }
2288 /* Index version 4 uses a different hash function than index version
2289 5 and later.
2290
2291 Versions earlier than 6 did not emit psymbols for inlined
2292 functions. Using these files will cause GDB not to be able to
2293 set breakpoints on inlined functions by name, so we ignore these
2294 indices unless the --use-deprecated-index-sections command line
2295 option was supplied. */
2296 if (version < 6 && !use_deprecated_index_sections)
2297 {
2298 static int warning_printed = 0;
2299 if (!warning_printed)
2300 {
2301 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2302 "--use-deprecated-index-sections to use them anyway"),
2303 objfile->name);
2304 warning_printed = 1;
2305 }
2306 return 0;
2307 }
2308 /* Indexes with higher version than the one supported by GDB may be no
2309 longer backward compatible. */
2310 if (version > 6)
2311 return 0;
2312
2313 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2314 map->version = version;
2315 map->total_size = dwarf2_per_objfile->gdb_index.size;
2316
2317 metadata = (offset_type *) (addr + sizeof (offset_type));
2318
2319 i = 0;
2320 cu_list = addr + MAYBE_SWAP (metadata[i]);
2321 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2322 / 8);
2323 ++i;
2324
2325 types_list = addr + MAYBE_SWAP (metadata[i]);
2326 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2327 - MAYBE_SWAP (metadata[i]))
2328 / 8);
2329 ++i;
2330
2331 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2332 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2333 - MAYBE_SWAP (metadata[i]));
2334 ++i;
2335
2336 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2337 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2338 - MAYBE_SWAP (metadata[i]))
2339 / (2 * sizeof (offset_type)));
2340 ++i;
2341
2342 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2343
2344 /* Don't use the index if it's empty. */
2345 if (map->symbol_table_slots == 0)
2346 return 0;
2347
2348 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2349 return 0;
2350
2351 if (types_list_elements)
2352 {
2353 struct dwarf2_section_info *section;
2354
2355 /* We can only handle a single .debug_types when we have an
2356 index. */
2357 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2358 return 0;
2359
2360 section = VEC_index (dwarf2_section_info_def,
2361 dwarf2_per_objfile->types, 0);
2362
2363 if (!create_signatured_type_table_from_index (objfile, section,
2364 types_list,
2365 types_list_elements))
2366 return 0;
2367 }
2368
2369 create_addrmap_from_index (objfile, map);
2370
2371 dwarf2_per_objfile->index_table = map;
2372 dwarf2_per_objfile->using_index = 1;
2373 dwarf2_per_objfile->quick_file_names_table =
2374 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2375
2376 return 1;
2377 }
2378
2379 /* A helper for the "quick" functions which sets the global
2380 dwarf2_per_objfile according to OBJFILE. */
2381
2382 static void
2383 dw2_setup (struct objfile *objfile)
2384 {
2385 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2386 gdb_assert (dwarf2_per_objfile);
2387 }
2388
2389 /* die_reader_func for dw2_get_file_names. */
2390
2391 static void
2392 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2393 gdb_byte *info_ptr,
2394 struct die_info *comp_unit_die,
2395 int has_children,
2396 void *data)
2397 {
2398 struct dwarf2_cu *cu = reader->cu;
2399 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2400 struct objfile *objfile = dwarf2_per_objfile->objfile;
2401 struct line_header *lh;
2402 struct attribute *attr;
2403 int i;
2404 unsigned int bytes_read;
2405 char *name, *comp_dir;
2406 void **slot;
2407 struct quick_file_names *qfn;
2408 unsigned int line_offset;
2409
2410 lh = NULL;
2411 slot = NULL;
2412 line_offset = 0;
2413
2414 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2415 if (attr)
2416 {
2417 struct quick_file_names find_entry;
2418
2419 line_offset = DW_UNSND (attr);
2420
2421 /* We may have already read in this line header (TU line header sharing).
2422 If we have we're done. */
2423 find_entry.offset = line_offset;
2424 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2425 &find_entry, INSERT);
2426 if (*slot != NULL)
2427 {
2428 this_cu->v.quick->file_names = *slot;
2429 return;
2430 }
2431
2432 lh = dwarf_decode_line_header (line_offset, cu);
2433 }
2434 if (lh == NULL)
2435 {
2436 this_cu->v.quick->no_file_data = 1;
2437 return;
2438 }
2439
2440 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2441 qfn->offset = line_offset;
2442 gdb_assert (slot != NULL);
2443 *slot = qfn;
2444
2445 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2446
2447 qfn->num_file_names = lh->num_file_names;
2448 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2449 lh->num_file_names * sizeof (char *));
2450 for (i = 0; i < lh->num_file_names; ++i)
2451 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2452 qfn->real_names = NULL;
2453
2454 free_line_header (lh);
2455
2456 this_cu->v.quick->file_names = qfn;
2457 }
2458
2459 /* A helper for the "quick" functions which attempts to read the line
2460 table for THIS_CU. */
2461
2462 static struct quick_file_names *
2463 dw2_get_file_names (struct objfile *objfile,
2464 struct dwarf2_per_cu_data *this_cu)
2465 {
2466 if (this_cu->v.quick->file_names != NULL)
2467 return this_cu->v.quick->file_names;
2468 /* If we know there is no line data, no point in looking again. */
2469 if (this_cu->v.quick->no_file_data)
2470 return NULL;
2471
2472 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2473 in the stub for CUs, there's is no need to lookup the DWO file.
2474 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2475 DWO file. */
2476 if (this_cu->is_debug_types)
2477 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2478 else
2479 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2480
2481 if (this_cu->v.quick->no_file_data)
2482 return NULL;
2483 return this_cu->v.quick->file_names;
2484 }
2485
2486 /* A helper for the "quick" functions which computes and caches the
2487 real path for a given file name from the line table. */
2488
2489 static const char *
2490 dw2_get_real_path (struct objfile *objfile,
2491 struct quick_file_names *qfn, int index)
2492 {
2493 if (qfn->real_names == NULL)
2494 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2495 qfn->num_file_names, sizeof (char *));
2496
2497 if (qfn->real_names[index] == NULL)
2498 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2499
2500 return qfn->real_names[index];
2501 }
2502
2503 static struct symtab *
2504 dw2_find_last_source_symtab (struct objfile *objfile)
2505 {
2506 int index;
2507
2508 dw2_setup (objfile);
2509 index = dwarf2_per_objfile->n_comp_units - 1;
2510 return dw2_instantiate_symtab (dw2_get_cu (index));
2511 }
2512
2513 /* Traversal function for dw2_forget_cached_source_info. */
2514
2515 static int
2516 dw2_free_cached_file_names (void **slot, void *info)
2517 {
2518 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2519
2520 if (file_data->real_names)
2521 {
2522 int i;
2523
2524 for (i = 0; i < file_data->num_file_names; ++i)
2525 {
2526 xfree ((void*) file_data->real_names[i]);
2527 file_data->real_names[i] = NULL;
2528 }
2529 }
2530
2531 return 1;
2532 }
2533
2534 static void
2535 dw2_forget_cached_source_info (struct objfile *objfile)
2536 {
2537 dw2_setup (objfile);
2538
2539 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2540 dw2_free_cached_file_names, NULL);
2541 }
2542
2543 /* Helper function for dw2_map_symtabs_matching_filename that expands
2544 the symtabs and calls the iterator. */
2545
2546 static int
2547 dw2_map_expand_apply (struct objfile *objfile,
2548 struct dwarf2_per_cu_data *per_cu,
2549 const char *name,
2550 const char *full_path, const char *real_path,
2551 int (*callback) (struct symtab *, void *),
2552 void *data)
2553 {
2554 struct symtab *last_made = objfile->symtabs;
2555
2556 /* Don't visit already-expanded CUs. */
2557 if (per_cu->v.quick->symtab)
2558 return 0;
2559
2560 /* This may expand more than one symtab, and we want to iterate over
2561 all of them. */
2562 dw2_instantiate_symtab (per_cu);
2563
2564 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2565 objfile->symtabs, last_made);
2566 }
2567
2568 /* Implementation of the map_symtabs_matching_filename method. */
2569
2570 static int
2571 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2572 const char *full_path, const char *real_path,
2573 int (*callback) (struct symtab *, void *),
2574 void *data)
2575 {
2576 int i;
2577 const char *name_basename = lbasename (name);
2578 int name_len = strlen (name);
2579 int is_abs = IS_ABSOLUTE_PATH (name);
2580
2581 dw2_setup (objfile);
2582
2583 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2584 + dwarf2_per_objfile->n_type_units); ++i)
2585 {
2586 int j;
2587 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2588 struct quick_file_names *file_data;
2589
2590 /* We only need to look at symtabs not already expanded. */
2591 if (per_cu->v.quick->symtab)
2592 continue;
2593
2594 file_data = dw2_get_file_names (objfile, per_cu);
2595 if (file_data == NULL)
2596 continue;
2597
2598 for (j = 0; j < file_data->num_file_names; ++j)
2599 {
2600 const char *this_name = file_data->file_names[j];
2601
2602 if (FILENAME_CMP (name, this_name) == 0
2603 || (!is_abs && compare_filenames_for_search (this_name,
2604 name, name_len)))
2605 {
2606 if (dw2_map_expand_apply (objfile, per_cu,
2607 name, full_path, real_path,
2608 callback, data))
2609 return 1;
2610 }
2611
2612 /* Before we invoke realpath, which can get expensive when many
2613 files are involved, do a quick comparison of the basenames. */
2614 if (! basenames_may_differ
2615 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2616 continue;
2617
2618 if (full_path != NULL)
2619 {
2620 const char *this_real_name = dw2_get_real_path (objfile,
2621 file_data, j);
2622
2623 if (this_real_name != NULL
2624 && (FILENAME_CMP (full_path, this_real_name) == 0
2625 || (!is_abs
2626 && compare_filenames_for_search (this_real_name,
2627 name, name_len))))
2628 {
2629 if (dw2_map_expand_apply (objfile, per_cu,
2630 name, full_path, real_path,
2631 callback, data))
2632 return 1;
2633 }
2634 }
2635
2636 if (real_path != NULL)
2637 {
2638 const char *this_real_name = dw2_get_real_path (objfile,
2639 file_data, j);
2640
2641 if (this_real_name != NULL
2642 && (FILENAME_CMP (real_path, this_real_name) == 0
2643 || (!is_abs
2644 && compare_filenames_for_search (this_real_name,
2645 name, name_len))))
2646 {
2647 if (dw2_map_expand_apply (objfile, per_cu,
2648 name, full_path, real_path,
2649 callback, data))
2650 return 1;
2651 }
2652 }
2653 }
2654 }
2655
2656 return 0;
2657 }
2658
2659 static struct symtab *
2660 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2661 const char *name, domain_enum domain)
2662 {
2663 /* We do all the work in the pre_expand_symtabs_matching hook
2664 instead. */
2665 return NULL;
2666 }
2667
2668 /* A helper function that expands all symtabs that hold an object
2669 named NAME. */
2670
2671 static void
2672 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2673 {
2674 dw2_setup (objfile);
2675
2676 /* index_table is NULL if OBJF_READNOW. */
2677 if (dwarf2_per_objfile->index_table)
2678 {
2679 offset_type *vec;
2680
2681 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2682 name, &vec))
2683 {
2684 offset_type i, len = MAYBE_SWAP (*vec);
2685 for (i = 0; i < len; ++i)
2686 {
2687 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2688 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2689
2690 dw2_instantiate_symtab (per_cu);
2691 }
2692 }
2693 }
2694 }
2695
2696 static void
2697 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2698 enum block_enum block_kind, const char *name,
2699 domain_enum domain)
2700 {
2701 dw2_do_expand_symtabs_matching (objfile, name);
2702 }
2703
2704 static void
2705 dw2_print_stats (struct objfile *objfile)
2706 {
2707 int i, count;
2708
2709 dw2_setup (objfile);
2710 count = 0;
2711 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2712 + dwarf2_per_objfile->n_type_units); ++i)
2713 {
2714 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2715
2716 if (!per_cu->v.quick->symtab)
2717 ++count;
2718 }
2719 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2720 }
2721
2722 static void
2723 dw2_dump (struct objfile *objfile)
2724 {
2725 /* Nothing worth printing. */
2726 }
2727
2728 static void
2729 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2730 struct section_offsets *delta)
2731 {
2732 /* There's nothing to relocate here. */
2733 }
2734
2735 static void
2736 dw2_expand_symtabs_for_function (struct objfile *objfile,
2737 const char *func_name)
2738 {
2739 dw2_do_expand_symtabs_matching (objfile, func_name);
2740 }
2741
2742 static void
2743 dw2_expand_all_symtabs (struct objfile *objfile)
2744 {
2745 int i;
2746
2747 dw2_setup (objfile);
2748
2749 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2750 + dwarf2_per_objfile->n_type_units); ++i)
2751 {
2752 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2753
2754 dw2_instantiate_symtab (per_cu);
2755 }
2756 }
2757
2758 static void
2759 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2760 const char *filename)
2761 {
2762 int i;
2763
2764 dw2_setup (objfile);
2765
2766 /* We don't need to consider type units here.
2767 This is only called for examining code, e.g. expand_line_sal.
2768 There can be an order of magnitude (or more) more type units
2769 than comp units, and we avoid them if we can. */
2770
2771 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2772 {
2773 int j;
2774 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2775 struct quick_file_names *file_data;
2776
2777 /* We only need to look at symtabs not already expanded. */
2778 if (per_cu->v.quick->symtab)
2779 continue;
2780
2781 file_data = dw2_get_file_names (objfile, per_cu);
2782 if (file_data == NULL)
2783 continue;
2784
2785 for (j = 0; j < file_data->num_file_names; ++j)
2786 {
2787 const char *this_name = file_data->file_names[j];
2788 if (FILENAME_CMP (this_name, filename) == 0)
2789 {
2790 dw2_instantiate_symtab (per_cu);
2791 break;
2792 }
2793 }
2794 }
2795 }
2796
2797 static const char *
2798 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2799 {
2800 struct dwarf2_per_cu_data *per_cu;
2801 offset_type *vec;
2802 struct quick_file_names *file_data;
2803
2804 dw2_setup (objfile);
2805
2806 /* index_table is NULL if OBJF_READNOW. */
2807 if (!dwarf2_per_objfile->index_table)
2808 {
2809 struct symtab *s;
2810
2811 ALL_OBJFILE_SYMTABS (objfile, s)
2812 if (s->primary)
2813 {
2814 struct blockvector *bv = BLOCKVECTOR (s);
2815 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2816 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2817
2818 if (sym)
2819 return sym->symtab->filename;
2820 }
2821 return NULL;
2822 }
2823
2824 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2825 name, &vec))
2826 return NULL;
2827
2828 /* Note that this just looks at the very first one named NAME -- but
2829 actually we are looking for a function. find_main_filename
2830 should be rewritten so that it doesn't require a custom hook. It
2831 could just use the ordinary symbol tables. */
2832 /* vec[0] is the length, which must always be >0. */
2833 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2834
2835 file_data = dw2_get_file_names (objfile, per_cu);
2836 if (file_data == NULL
2837 || file_data->num_file_names == 0)
2838 return NULL;
2839
2840 return file_data->file_names[file_data->num_file_names - 1];
2841 }
2842
2843 static void
2844 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2845 struct objfile *objfile, int global,
2846 int (*callback) (struct block *,
2847 struct symbol *, void *),
2848 void *data, symbol_compare_ftype *match,
2849 symbol_compare_ftype *ordered_compare)
2850 {
2851 /* Currently unimplemented; used for Ada. The function can be called if the
2852 current language is Ada for a non-Ada objfile using GNU index. As Ada
2853 does not look for non-Ada symbols this function should just return. */
2854 }
2855
2856 static void
2857 dw2_expand_symtabs_matching
2858 (struct objfile *objfile,
2859 int (*file_matcher) (const char *, void *),
2860 int (*name_matcher) (const char *, void *),
2861 enum search_domain kind,
2862 void *data)
2863 {
2864 int i;
2865 offset_type iter;
2866 struct mapped_index *index;
2867
2868 dw2_setup (objfile);
2869
2870 /* index_table is NULL if OBJF_READNOW. */
2871 if (!dwarf2_per_objfile->index_table)
2872 return;
2873 index = dwarf2_per_objfile->index_table;
2874
2875 if (file_matcher != NULL)
2876 {
2877 struct cleanup *cleanup;
2878 htab_t visited_found, visited_not_found;
2879
2880 visited_found = htab_create_alloc (10,
2881 htab_hash_pointer, htab_eq_pointer,
2882 NULL, xcalloc, xfree);
2883 cleanup = make_cleanup_htab_delete (visited_found);
2884 visited_not_found = htab_create_alloc (10,
2885 htab_hash_pointer, htab_eq_pointer,
2886 NULL, xcalloc, xfree);
2887 make_cleanup_htab_delete (visited_not_found);
2888
2889 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2890 + dwarf2_per_objfile->n_type_units); ++i)
2891 {
2892 int j;
2893 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2894 struct quick_file_names *file_data;
2895 void **slot;
2896
2897 per_cu->v.quick->mark = 0;
2898
2899 /* We only need to look at symtabs not already expanded. */
2900 if (per_cu->v.quick->symtab)
2901 continue;
2902
2903 file_data = dw2_get_file_names (objfile, per_cu);
2904 if (file_data == NULL)
2905 continue;
2906
2907 if (htab_find (visited_not_found, file_data) != NULL)
2908 continue;
2909 else if (htab_find (visited_found, file_data) != NULL)
2910 {
2911 per_cu->v.quick->mark = 1;
2912 continue;
2913 }
2914
2915 for (j = 0; j < file_data->num_file_names; ++j)
2916 {
2917 if (file_matcher (file_data->file_names[j], data))
2918 {
2919 per_cu->v.quick->mark = 1;
2920 break;
2921 }
2922 }
2923
2924 slot = htab_find_slot (per_cu->v.quick->mark
2925 ? visited_found
2926 : visited_not_found,
2927 file_data, INSERT);
2928 *slot = file_data;
2929 }
2930
2931 do_cleanups (cleanup);
2932 }
2933
2934 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2935 {
2936 offset_type idx = 2 * iter;
2937 const char *name;
2938 offset_type *vec, vec_len, vec_idx;
2939
2940 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2941 continue;
2942
2943 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2944
2945 if (! (*name_matcher) (name, data))
2946 continue;
2947
2948 /* The name was matched, now expand corresponding CUs that were
2949 marked. */
2950 vec = (offset_type *) (index->constant_pool
2951 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2952 vec_len = MAYBE_SWAP (vec[0]);
2953 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2954 {
2955 struct dwarf2_per_cu_data *per_cu;
2956
2957 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2958 if (file_matcher == NULL || per_cu->v.quick->mark)
2959 dw2_instantiate_symtab (per_cu);
2960 }
2961 }
2962 }
2963
2964 static struct symtab *
2965 dw2_find_pc_sect_symtab (struct objfile *objfile,
2966 struct minimal_symbol *msymbol,
2967 CORE_ADDR pc,
2968 struct obj_section *section,
2969 int warn_if_readin)
2970 {
2971 struct dwarf2_per_cu_data *data;
2972
2973 dw2_setup (objfile);
2974
2975 if (!objfile->psymtabs_addrmap)
2976 return NULL;
2977
2978 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2979 if (!data)
2980 return NULL;
2981
2982 if (warn_if_readin && data->v.quick->symtab)
2983 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2984 paddress (get_objfile_arch (objfile), pc));
2985
2986 return dw2_instantiate_symtab (data);
2987 }
2988
2989 static void
2990 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2991 void *data, int need_fullname)
2992 {
2993 int i;
2994 struct cleanup *cleanup;
2995 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2996 NULL, xcalloc, xfree);
2997
2998 cleanup = make_cleanup_htab_delete (visited);
2999 dw2_setup (objfile);
3000
3001 /* We can ignore file names coming from already-expanded CUs. */
3002 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3003 + dwarf2_per_objfile->n_type_units); ++i)
3004 {
3005 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3006
3007 if (per_cu->v.quick->symtab)
3008 {
3009 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3010 INSERT);
3011
3012 *slot = per_cu->v.quick->file_names;
3013 }
3014 }
3015
3016 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3017 + dwarf2_per_objfile->n_type_units); ++i)
3018 {
3019 int j;
3020 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3021 struct quick_file_names *file_data;
3022 void **slot;
3023
3024 /* We only need to look at symtabs not already expanded. */
3025 if (per_cu->v.quick->symtab)
3026 continue;
3027
3028 file_data = dw2_get_file_names (objfile, per_cu);
3029 if (file_data == NULL)
3030 continue;
3031
3032 slot = htab_find_slot (visited, file_data, INSERT);
3033 if (*slot)
3034 {
3035 /* Already visited. */
3036 continue;
3037 }
3038 *slot = file_data;
3039
3040 for (j = 0; j < file_data->num_file_names; ++j)
3041 {
3042 const char *this_real_name;
3043
3044 if (need_fullname)
3045 this_real_name = dw2_get_real_path (objfile, file_data, j);
3046 else
3047 this_real_name = NULL;
3048 (*fun) (file_data->file_names[j], this_real_name, data);
3049 }
3050 }
3051
3052 do_cleanups (cleanup);
3053 }
3054
3055 static int
3056 dw2_has_symbols (struct objfile *objfile)
3057 {
3058 return 1;
3059 }
3060
3061 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3062 {
3063 dw2_has_symbols,
3064 dw2_find_last_source_symtab,
3065 dw2_forget_cached_source_info,
3066 dw2_map_symtabs_matching_filename,
3067 dw2_lookup_symbol,
3068 dw2_pre_expand_symtabs_matching,
3069 dw2_print_stats,
3070 dw2_dump,
3071 dw2_relocate,
3072 dw2_expand_symtabs_for_function,
3073 dw2_expand_all_symtabs,
3074 dw2_expand_symtabs_with_filename,
3075 dw2_find_symbol_file,
3076 dw2_map_matching_symbols,
3077 dw2_expand_symtabs_matching,
3078 dw2_find_pc_sect_symtab,
3079 dw2_map_symbol_filenames
3080 };
3081
3082 /* Initialize for reading DWARF for this objfile. Return 0 if this
3083 file will use psymtabs, or 1 if using the GNU index. */
3084
3085 int
3086 dwarf2_initialize_objfile (struct objfile *objfile)
3087 {
3088 /* If we're about to read full symbols, don't bother with the
3089 indices. In this case we also don't care if some other debug
3090 format is making psymtabs, because they are all about to be
3091 expanded anyway. */
3092 if ((objfile->flags & OBJF_READNOW))
3093 {
3094 int i;
3095
3096 dwarf2_per_objfile->using_index = 1;
3097 create_all_comp_units (objfile);
3098 create_all_type_units (objfile);
3099 dwarf2_per_objfile->quick_file_names_table =
3100 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3101
3102 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3103 + dwarf2_per_objfile->n_type_units); ++i)
3104 {
3105 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3106
3107 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3108 struct dwarf2_per_cu_quick_data);
3109 }
3110
3111 /* Return 1 so that gdb sees the "quick" functions. However,
3112 these functions will be no-ops because we will have expanded
3113 all symtabs. */
3114 return 1;
3115 }
3116
3117 if (dwarf2_read_index (objfile))
3118 return 1;
3119
3120 return 0;
3121 }
3122
3123 \f
3124
3125 /* Build a partial symbol table. */
3126
3127 void
3128 dwarf2_build_psymtabs (struct objfile *objfile)
3129 {
3130 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3131 {
3132 init_psymbol_list (objfile, 1024);
3133 }
3134
3135 dwarf2_build_psymtabs_hard (objfile);
3136 }
3137
3138 /* Return TRUE if OFFSET is within CU_HEADER. */
3139
3140 static inline int
3141 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3142 {
3143 sect_offset bottom = { cu_header->offset.sect_off };
3144 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3145 + cu_header->initial_length_size) };
3146
3147 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3148 }
3149
3150 /* Read in the comp unit header information from the debug_info at info_ptr.
3151 NOTE: This leaves members offset, first_die_offset to be filled in
3152 by the caller. */
3153
3154 static gdb_byte *
3155 read_comp_unit_head (struct comp_unit_head *cu_header,
3156 gdb_byte *info_ptr, bfd *abfd)
3157 {
3158 int signed_addr;
3159 unsigned int bytes_read;
3160
3161 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3162 cu_header->initial_length_size = bytes_read;
3163 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3164 info_ptr += bytes_read;
3165 cu_header->version = read_2_bytes (abfd, info_ptr);
3166 info_ptr += 2;
3167 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3168 &bytes_read);
3169 info_ptr += bytes_read;
3170 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3171 info_ptr += 1;
3172 signed_addr = bfd_get_sign_extend_vma (abfd);
3173 if (signed_addr < 0)
3174 internal_error (__FILE__, __LINE__,
3175 _("read_comp_unit_head: dwarf from non elf file"));
3176 cu_header->signed_addr_p = signed_addr;
3177
3178 return info_ptr;
3179 }
3180
3181 /* Subroutine of read_and_check_comp_unit_head and
3182 read_and_check_type_unit_head to simplify them.
3183 Perform various error checking on the header. */
3184
3185 static void
3186 error_check_comp_unit_head (struct comp_unit_head *header,
3187 struct dwarf2_section_info *section)
3188 {
3189 bfd *abfd = section->asection->owner;
3190 const char *filename = bfd_get_filename (abfd);
3191
3192 if (header->version != 2 && header->version != 3 && header->version != 4)
3193 error (_("Dwarf Error: wrong version in compilation unit header "
3194 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3195 filename);
3196
3197 if (header->abbrev_offset.sect_off
3198 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3199 &dwarf2_per_objfile->abbrev))
3200 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3201 "(offset 0x%lx + 6) [in module %s]"),
3202 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3203 filename);
3204
3205 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3206 avoid potential 32-bit overflow. */
3207 if (((unsigned long) header->offset.sect_off
3208 + header->length + header->initial_length_size)
3209 > section->size)
3210 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3211 "(offset 0x%lx + 0) [in module %s]"),
3212 (long) header->length, (long) header->offset.sect_off,
3213 filename);
3214 }
3215
3216 /* Read in a CU/TU header and perform some basic error checking.
3217 The contents of the header are stored in HEADER.
3218 The result is a pointer to the start of the first DIE. */
3219
3220 static gdb_byte *
3221 read_and_check_comp_unit_head (struct comp_unit_head *header,
3222 struct dwarf2_section_info *section,
3223 gdb_byte *info_ptr,
3224 int is_debug_types_section)
3225 {
3226 gdb_byte *beg_of_comp_unit = info_ptr;
3227 bfd *abfd = section->asection->owner;
3228
3229 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3230
3231 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3232
3233 /* If we're reading a type unit, skip over the signature and
3234 type_offset fields. */
3235 if (is_debug_types_section)
3236 info_ptr += 8 /*signature*/ + header->offset_size;
3237
3238 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3239
3240 error_check_comp_unit_head (header, section);
3241
3242 return info_ptr;
3243 }
3244
3245 /* Read in the types comp unit header information from .debug_types entry at
3246 types_ptr. The result is a pointer to one past the end of the header. */
3247
3248 static gdb_byte *
3249 read_and_check_type_unit_head (struct comp_unit_head *header,
3250 struct dwarf2_section_info *section,
3251 gdb_byte *info_ptr,
3252 ULONGEST *signature,
3253 cu_offset *type_offset_in_tu)
3254 {
3255 gdb_byte *beg_of_comp_unit = info_ptr;
3256 bfd *abfd = section->asection->owner;
3257
3258 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3259
3260 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3261
3262 /* If we're reading a type unit, skip over the signature and
3263 type_offset fields. */
3264 if (signature != NULL)
3265 *signature = read_8_bytes (abfd, info_ptr);
3266 info_ptr += 8;
3267 if (type_offset_in_tu != NULL)
3268 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3269 header->offset_size);
3270 info_ptr += header->offset_size;
3271
3272 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3273
3274 error_check_comp_unit_head (header, section);
3275
3276 return info_ptr;
3277 }
3278
3279 /* Allocate a new partial symtab for file named NAME and mark this new
3280 partial symtab as being an include of PST. */
3281
3282 static void
3283 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3284 struct objfile *objfile)
3285 {
3286 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3287
3288 subpst->section_offsets = pst->section_offsets;
3289 subpst->textlow = 0;
3290 subpst->texthigh = 0;
3291
3292 subpst->dependencies = (struct partial_symtab **)
3293 obstack_alloc (&objfile->objfile_obstack,
3294 sizeof (struct partial_symtab *));
3295 subpst->dependencies[0] = pst;
3296 subpst->number_of_dependencies = 1;
3297
3298 subpst->globals_offset = 0;
3299 subpst->n_global_syms = 0;
3300 subpst->statics_offset = 0;
3301 subpst->n_static_syms = 0;
3302 subpst->symtab = NULL;
3303 subpst->read_symtab = pst->read_symtab;
3304 subpst->readin = 0;
3305
3306 /* No private part is necessary for include psymtabs. This property
3307 can be used to differentiate between such include psymtabs and
3308 the regular ones. */
3309 subpst->read_symtab_private = NULL;
3310 }
3311
3312 /* Read the Line Number Program data and extract the list of files
3313 included by the source file represented by PST. Build an include
3314 partial symtab for each of these included files. */
3315
3316 static void
3317 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3318 struct die_info *die,
3319 struct partial_symtab *pst)
3320 {
3321 struct line_header *lh = NULL;
3322 struct attribute *attr;
3323
3324 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3325 if (attr)
3326 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3327 if (lh == NULL)
3328 return; /* No linetable, so no includes. */
3329
3330 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3331 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3332
3333 free_line_header (lh);
3334 }
3335
3336 static hashval_t
3337 hash_signatured_type (const void *item)
3338 {
3339 const struct signatured_type *sig_type = item;
3340
3341 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3342 return sig_type->signature;
3343 }
3344
3345 static int
3346 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3347 {
3348 const struct signatured_type *lhs = item_lhs;
3349 const struct signatured_type *rhs = item_rhs;
3350
3351 return lhs->signature == rhs->signature;
3352 }
3353
3354 /* Allocate a hash table for signatured types. */
3355
3356 static htab_t
3357 allocate_signatured_type_table (struct objfile *objfile)
3358 {
3359 return htab_create_alloc_ex (41,
3360 hash_signatured_type,
3361 eq_signatured_type,
3362 NULL,
3363 &objfile->objfile_obstack,
3364 hashtab_obstack_allocate,
3365 dummy_obstack_deallocate);
3366 }
3367
3368 /* A helper function to add a signatured type CU to a table. */
3369
3370 static int
3371 add_signatured_type_cu_to_table (void **slot, void *datum)
3372 {
3373 struct signatured_type *sigt = *slot;
3374 struct dwarf2_per_cu_data ***datap = datum;
3375
3376 **datap = &sigt->per_cu;
3377 ++*datap;
3378
3379 return 1;
3380 }
3381
3382 /* Create the hash table of all entries in the .debug_types section.
3383 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3384 The result is a pointer to the hash table or NULL if there are
3385 no types. */
3386
3387 static htab_t
3388 create_debug_types_hash_table (struct dwo_file *dwo_file,
3389 VEC (dwarf2_section_info_def) *types)
3390 {
3391 struct objfile *objfile = dwarf2_per_objfile->objfile;
3392 htab_t types_htab = NULL;
3393 int ix;
3394 struct dwarf2_section_info *section;
3395
3396 if (VEC_empty (dwarf2_section_info_def, types))
3397 return NULL;
3398
3399 for (ix = 0;
3400 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3401 ++ix)
3402 {
3403 bfd *abfd;
3404 gdb_byte *info_ptr, *end_ptr;
3405
3406 dwarf2_read_section (objfile, section);
3407 info_ptr = section->buffer;
3408
3409 if (info_ptr == NULL)
3410 continue;
3411
3412 /* We can't set abfd until now because the section may be empty or
3413 not present, in which case section->asection will be NULL. */
3414 abfd = section->asection->owner;
3415
3416 if (types_htab == NULL)
3417 {
3418 if (dwo_file)
3419 types_htab = allocate_dwo_unit_table (objfile);
3420 else
3421 types_htab = allocate_signatured_type_table (objfile);
3422 }
3423
3424 if (dwarf2_die_debug)
3425 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3426 bfd_get_filename (abfd));
3427
3428 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3429 because we don't need to read any dies: the signature is in the
3430 header. */
3431
3432 end_ptr = info_ptr + section->size;
3433 while (info_ptr < end_ptr)
3434 {
3435 sect_offset offset;
3436 cu_offset type_offset_in_tu;
3437 ULONGEST signature;
3438 struct signatured_type *sig_type;
3439 struct dwo_unit *dwo_tu;
3440 void **slot;
3441 gdb_byte *ptr = info_ptr;
3442 struct comp_unit_head header;
3443 unsigned int length;
3444
3445 offset.sect_off = ptr - section->buffer;
3446
3447 /* We need to read the type's signature in order to build the hash
3448 table, but we don't need anything else just yet. */
3449
3450 ptr = read_and_check_type_unit_head (&header, section, ptr,
3451 &signature, &type_offset_in_tu);
3452
3453 length = header.initial_length_size + header.length;
3454
3455 /* Skip dummy type units. */
3456 if (ptr >= info_ptr + length
3457 || peek_abbrev_code (abfd, ptr) == 0)
3458 {
3459 info_ptr += header.initial_length_size + header.length;
3460 continue;
3461 }
3462
3463 if (dwo_file)
3464 {
3465 sig_type = NULL;
3466 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3467 struct dwo_unit);
3468 dwo_tu->dwo_file = dwo_file;
3469 dwo_tu->signature = signature;
3470 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3471 dwo_tu->info_or_types_section = section;
3472 dwo_tu->offset = offset;
3473 dwo_tu->length = length;
3474 }
3475 else
3476 {
3477 /* N.B.: type_offset is not usable if this type uses a DWO file.
3478 The real type_offset is in the DWO file. */
3479 dwo_tu = NULL;
3480 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3481 struct signatured_type);
3482 sig_type->signature = signature;
3483 sig_type->type_offset_in_tu = type_offset_in_tu;
3484 sig_type->per_cu.objfile = objfile;
3485 sig_type->per_cu.is_debug_types = 1;
3486 sig_type->per_cu.info_or_types_section = section;
3487 sig_type->per_cu.offset = offset;
3488 sig_type->per_cu.length = length;
3489 }
3490
3491 slot = htab_find_slot (types_htab,
3492 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3493 INSERT);
3494 gdb_assert (slot != NULL);
3495 if (*slot != NULL)
3496 {
3497 sect_offset dup_offset;
3498
3499 if (dwo_file)
3500 {
3501 const struct dwo_unit *dup_tu = *slot;
3502
3503 dup_offset = dup_tu->offset;
3504 }
3505 else
3506 {
3507 const struct signatured_type *dup_tu = *slot;
3508
3509 dup_offset = dup_tu->per_cu.offset;
3510 }
3511
3512 complaint (&symfile_complaints,
3513 _("debug type entry at offset 0x%x is duplicate to the "
3514 "entry at offset 0x%x, signature 0x%s"),
3515 offset.sect_off, dup_offset.sect_off,
3516 phex (signature, sizeof (signature)));
3517 }
3518 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3519
3520 if (dwarf2_die_debug)
3521 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3522 offset.sect_off,
3523 phex (signature, sizeof (signature)));
3524
3525 info_ptr += length;
3526 }
3527 }
3528
3529 return types_htab;
3530 }
3531
3532 /* Create the hash table of all entries in the .debug_types section,
3533 and initialize all_type_units.
3534 The result is zero if there is an error (e.g. missing .debug_types section),
3535 otherwise non-zero. */
3536
3537 static int
3538 create_all_type_units (struct objfile *objfile)
3539 {
3540 htab_t types_htab;
3541 struct dwarf2_per_cu_data **iter;
3542
3543 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3544 if (types_htab == NULL)
3545 {
3546 dwarf2_per_objfile->signatured_types = NULL;
3547 return 0;
3548 }
3549
3550 dwarf2_per_objfile->signatured_types = types_htab;
3551
3552 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3553 dwarf2_per_objfile->all_type_units
3554 = obstack_alloc (&objfile->objfile_obstack,
3555 dwarf2_per_objfile->n_type_units
3556 * sizeof (struct dwarf2_per_cu_data *));
3557 iter = &dwarf2_per_objfile->all_type_units[0];
3558 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3559 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3560 == dwarf2_per_objfile->n_type_units);
3561
3562 return 1;
3563 }
3564
3565 /* Lookup a signature based type for DW_FORM_ref_sig8.
3566 Returns NULL if signature SIG is not present in the table. */
3567
3568 static struct signatured_type *
3569 lookup_signatured_type (ULONGEST sig)
3570 {
3571 struct signatured_type find_entry, *entry;
3572
3573 if (dwarf2_per_objfile->signatured_types == NULL)
3574 {
3575 complaint (&symfile_complaints,
3576 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3577 return NULL;
3578 }
3579
3580 find_entry.signature = sig;
3581 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3582 return entry;
3583 }
3584
3585 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3586
3587 static void
3588 init_cu_die_reader (struct die_reader_specs *reader,
3589 struct dwarf2_cu *cu,
3590 struct dwarf2_section_info *section,
3591 struct dwo_file *dwo_file)
3592 {
3593 gdb_assert (section->readin && section->buffer != NULL);
3594 reader->abfd = section->asection->owner;
3595 reader->cu = cu;
3596 reader->dwo_file = dwo_file;
3597 reader->die_section = section;
3598 reader->buffer = section->buffer;
3599 }
3600
3601 /* Find the base address of the compilation unit for range lists and
3602 location lists. It will normally be specified by DW_AT_low_pc.
3603 In DWARF-3 draft 4, the base address could be overridden by
3604 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3605 compilation units with discontinuous ranges. */
3606
3607 static void
3608 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3609 {
3610 struct attribute *attr;
3611
3612 cu->base_known = 0;
3613 cu->base_address = 0;
3614
3615 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3616 if (attr)
3617 {
3618 cu->base_address = DW_ADDR (attr);
3619 cu->base_known = 1;
3620 }
3621 else
3622 {
3623 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3624 if (attr)
3625 {
3626 cu->base_address = DW_ADDR (attr);
3627 cu->base_known = 1;
3628 }
3629 }
3630 }
3631
3632 /* Initialize a CU (or TU) and read its DIEs.
3633 If the CU defers to a DWO file, read the DWO file as well.
3634
3635 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3636 Otherwise, a new CU is allocated with xmalloc.
3637
3638 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3639 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3640
3641 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3642 linker) then DIE_READER_FUNC will not get called. */
3643
3644 static void
3645 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3646 int use_existing_cu, int keep,
3647 die_reader_func_ftype *die_reader_func,
3648 void *data)
3649 {
3650 struct objfile *objfile = dwarf2_per_objfile->objfile;
3651 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3652 bfd *abfd = section->asection->owner;
3653 struct dwarf2_cu *cu;
3654 gdb_byte *begin_info_ptr, *info_ptr;
3655 struct die_reader_specs reader;
3656 struct die_info *comp_unit_die;
3657 int has_children;
3658 struct attribute *attr;
3659 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3660 struct signatured_type *sig_type = NULL;
3661
3662 if (use_existing_cu)
3663 gdb_assert (keep);
3664
3665 cleanups = make_cleanup (null_cleanup, NULL);
3666
3667 /* This is cheap if the section is already read in. */
3668 dwarf2_read_section (objfile, section);
3669
3670 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3671
3672 if (use_existing_cu && this_cu->cu != NULL)
3673 {
3674 cu = this_cu->cu;
3675 info_ptr += cu->header.first_die_offset.cu_off;
3676 }
3677 else
3678 {
3679 /* If !use_existing_cu, this_cu->cu must be NULL. */
3680 gdb_assert (this_cu->cu == NULL);
3681
3682 cu = xmalloc (sizeof (*cu));
3683 init_one_comp_unit (cu, this_cu);
3684
3685 /* If an error occurs while loading, release our storage. */
3686 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3687
3688 if (this_cu->is_debug_types)
3689 {
3690 ULONGEST signature;
3691
3692 info_ptr = read_and_check_type_unit_head (&cu->header,
3693 section, info_ptr,
3694 &signature, NULL);
3695
3696 /* There's no way to get from PER_CU to its containing
3697 struct signatured_type.
3698 But we have the signature so we can use that. */
3699 sig_type = lookup_signatured_type (signature);
3700 /* We've already scanned all the signatured types,
3701 this must succeed. */
3702 gdb_assert (sig_type != NULL);
3703 gdb_assert (&sig_type->per_cu == this_cu);
3704 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3705
3706 /* LENGTH has not been set yet for type units. */
3707 this_cu->length = cu->header.length + cu->header.initial_length_size;
3708
3709 /* Establish the type offset that can be used to lookup the type. */
3710 sig_type->type_offset_in_section.sect_off =
3711 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3712 }
3713 else
3714 {
3715 info_ptr = read_and_check_comp_unit_head (&cu->header,
3716 section, info_ptr, 0);
3717
3718 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3719 gdb_assert (this_cu->length
3720 == cu->header.length + cu->header.initial_length_size);
3721 }
3722 }
3723
3724 /* Skip dummy compilation units. */
3725 if (info_ptr >= begin_info_ptr + this_cu->length
3726 || peek_abbrev_code (abfd, info_ptr) == 0)
3727 {
3728 do_cleanups (cleanups);
3729 return;
3730 }
3731
3732 /* Read the abbrevs for this compilation unit into a table. */
3733 if (cu->dwarf2_abbrevs == NULL)
3734 {
3735 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3736 make_cleanup (dwarf2_free_abbrev_table, cu);
3737 }
3738
3739 /* Read the top level CU/TU die. */
3740 init_cu_die_reader (&reader, cu, section, NULL);
3741 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3742
3743 /* If we have a DWO stub, process it and then read in the DWO file.
3744 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3745 a DWO CU, that this test will fail. */
3746 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3747 if (attr)
3748 {
3749 char *dwo_name = DW_STRING (attr);
3750 const char *comp_dir;
3751 struct dwo_unit *dwo_unit;
3752 ULONGEST signature; /* Or dwo_id. */
3753 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3754 int i,num_extra_attrs;
3755
3756 if (has_children)
3757 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3758 " has children (offset 0x%x) [in module %s]"),
3759 this_cu->offset.sect_off, bfd_get_filename (abfd));
3760
3761 /* These attributes aren't processed until later:
3762 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3763 However, the attribute is found in the stub which we won't have later.
3764 In order to not impose this complication on the rest of the code,
3765 we read them here and copy them to the DWO CU/TU die. */
3766 stmt_list = low_pc = high_pc = ranges = NULL;
3767
3768 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3769 DWO file. */
3770 if (! this_cu->is_debug_types)
3771 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3772 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3773 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3774 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3775
3776 /* There should be a DW_AT_addr_base attribute here (if needed).
3777 We need the value before we can process DW_FORM_GNU_addr_index. */
3778 cu->addr_base = 0;
3779 cu->have_addr_base = 0;
3780 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3781 if (attr)
3782 {
3783 cu->addr_base = DW_UNSND (attr);
3784 cu->have_addr_base = 1;
3785 }
3786
3787 if (this_cu->is_debug_types)
3788 {
3789 gdb_assert (sig_type != NULL);
3790 signature = sig_type->signature;
3791 }
3792 else
3793 {
3794 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3795 if (! attr)
3796 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3797 dwo_name);
3798 signature = DW_UNSND (attr);
3799 }
3800
3801 /* We may need the comp_dir in order to find the DWO file. */
3802 comp_dir = NULL;
3803 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3804 if (attr)
3805 comp_dir = DW_STRING (attr);
3806
3807 if (this_cu->is_debug_types)
3808 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3809 else
3810 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3811 signature);
3812
3813 if (dwo_unit == NULL)
3814 {
3815 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3816 " with ID %s [in module %s]"),
3817 this_cu->offset.sect_off,
3818 phex (signature, sizeof (signature)),
3819 objfile->name);
3820 }
3821
3822 /* Set up for reading the DWO CU/TU. */
3823 cu->dwo_unit = dwo_unit;
3824 section = dwo_unit->info_or_types_section;
3825 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3826 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3827
3828 if (this_cu->is_debug_types)
3829 {
3830 ULONGEST signature;
3831
3832 info_ptr = read_and_check_type_unit_head (&cu->header,
3833 section, info_ptr,
3834 &signature, NULL);
3835 gdb_assert (sig_type->signature == signature);
3836 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3837 gdb_assert (dwo_unit->length
3838 == cu->header.length + cu->header.initial_length_size);
3839
3840 /* Establish the type offset that can be used to lookup the type.
3841 For DWO files, we don't know it until now. */
3842 sig_type->type_offset_in_section.sect_off =
3843 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3844 }
3845 else
3846 {
3847 info_ptr = read_and_check_comp_unit_head (&cu->header,
3848 section, info_ptr, 0);
3849 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3850 gdb_assert (dwo_unit->length
3851 == cu->header.length + cu->header.initial_length_size);
3852 }
3853
3854 /* Discard the original CU's abbrev table, and read the DWO's. */
3855 dwarf2_free_abbrev_table (cu);
3856 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3857
3858 /* Read in the die, but leave space to copy over the attributes
3859 from the stub. This has the benefit of simplifying the rest of
3860 the code - all the real work is done here. */
3861 num_extra_attrs = ((stmt_list != NULL)
3862 + (low_pc != NULL)
3863 + (high_pc != NULL)
3864 + (ranges != NULL));
3865 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3866 &has_children, num_extra_attrs);
3867
3868 /* Copy over the attributes from the stub to the DWO die. */
3869 i = comp_unit_die->num_attrs;
3870 if (stmt_list != NULL)
3871 comp_unit_die->attrs[i++] = *stmt_list;
3872 if (low_pc != NULL)
3873 comp_unit_die->attrs[i++] = *low_pc;
3874 if (high_pc != NULL)
3875 comp_unit_die->attrs[i++] = *high_pc;
3876 if (ranges != NULL)
3877 comp_unit_die->attrs[i++] = *ranges;
3878 comp_unit_die->num_attrs += num_extra_attrs;
3879
3880 /* Skip dummy compilation units. */
3881 if (info_ptr >= begin_info_ptr + dwo_unit->length
3882 || peek_abbrev_code (abfd, info_ptr) == 0)
3883 {
3884 do_cleanups (cleanups);
3885 return;
3886 }
3887 }
3888
3889 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3890
3891 if (free_cu_cleanup != NULL)
3892 {
3893 if (keep)
3894 {
3895 /* We've successfully allocated this compilation unit. Let our
3896 caller clean it up when finished with it. */
3897 discard_cleanups (free_cu_cleanup);
3898
3899 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3900 So we have to manually free the abbrev table. */
3901 dwarf2_free_abbrev_table (cu);
3902
3903 /* Link this CU into read_in_chain. */
3904 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3905 dwarf2_per_objfile->read_in_chain = this_cu;
3906 }
3907 else
3908 do_cleanups (free_cu_cleanup);
3909 }
3910
3911 do_cleanups (cleanups);
3912 }
3913
3914 /* Read CU/TU THIS_CU in section SECTION,
3915 but do not follow DW_AT_GNU_dwo_name if present.
3916 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3917 have already done the lookup to find the DWO file).
3918
3919 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3920 THIS_CU->is_debug_types, but nothing else.
3921
3922 We fill in THIS_CU->length.
3923
3924 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3925 linker) then DIE_READER_FUNC will not get called.
3926
3927 THIS_CU->cu is always freed when done.
3928 This is done in order to not leave THIS_CU->cu in a state where we have
3929 to care whether it refers to the "main" CU or the DWO CU. */
3930
3931 static void
3932 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3933 struct dwarf2_section_info *abbrev_section,
3934 struct dwo_file *dwo_file,
3935 die_reader_func_ftype *die_reader_func,
3936 void *data)
3937 {
3938 struct objfile *objfile = dwarf2_per_objfile->objfile;
3939 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3940 bfd *abfd = section->asection->owner;
3941 struct dwarf2_cu cu;
3942 gdb_byte *begin_info_ptr, *info_ptr;
3943 struct die_reader_specs reader;
3944 struct cleanup *cleanups;
3945 struct die_info *comp_unit_die;
3946 int has_children;
3947
3948 gdb_assert (this_cu->cu == NULL);
3949
3950 /* This is cheap if the section is already read in. */
3951 dwarf2_read_section (objfile, section);
3952
3953 init_one_comp_unit (&cu, this_cu);
3954
3955 cleanups = make_cleanup (free_stack_comp_unit, &cu);
3956
3957 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3958 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3959 this_cu->is_debug_types);
3960
3961 this_cu->length = cu.header.length + cu.header.initial_length_size;
3962
3963 /* Skip dummy compilation units. */
3964 if (info_ptr >= begin_info_ptr + this_cu->length
3965 || peek_abbrev_code (abfd, info_ptr) == 0)
3966 {
3967 do_cleanups (cleanups);
3968 return;
3969 }
3970
3971 dwarf2_read_abbrevs (&cu, abbrev_section);
3972 make_cleanup (dwarf2_free_abbrev_table, &cu);
3973
3974 init_cu_die_reader (&reader, &cu, section, dwo_file);
3975 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3976
3977 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3978
3979 do_cleanups (cleanups);
3980 }
3981
3982 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
3983 does not lookup the specified DWO file.
3984 This cannot be used to read DWO files.
3985
3986 THIS_CU->cu is always freed when done.
3987 This is done in order to not leave THIS_CU->cu in a state where we have
3988 to care whether it refers to the "main" CU or the DWO CU.
3989 We can revisit this if the data shows there's a performance issue. */
3990
3991 static void
3992 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
3993 die_reader_func_ftype *die_reader_func,
3994 void *data)
3995 {
3996 init_cutu_and_read_dies_no_follow (this_cu,
3997 &dwarf2_per_objfile->abbrev,
3998 NULL,
3999 die_reader_func, data);
4000 }
4001
4002 /* die_reader_func for process_psymtab_comp_unit. */
4003
4004 static void
4005 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4006 gdb_byte *info_ptr,
4007 struct die_info *comp_unit_die,
4008 int has_children,
4009 void *data)
4010 {
4011 struct dwarf2_cu *cu = reader->cu;
4012 struct objfile *objfile = cu->objfile;
4013 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4014 bfd *abfd = objfile->obfd;
4015 struct attribute *attr;
4016 CORE_ADDR baseaddr;
4017 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4018 struct partial_symtab *pst;
4019 int has_pc_info;
4020 const char *filename;
4021
4022 if (comp_unit_die->tag == DW_TAG_partial_unit)
4023 return;
4024
4025 prepare_one_comp_unit (cu, comp_unit_die);
4026
4027 cu->list_in_scope = &file_symbols;
4028
4029 /* Allocate a new partial symbol table structure. */
4030 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4031 if (attr == NULL || !DW_STRING (attr))
4032 filename = "";
4033 else
4034 filename = DW_STRING (attr);
4035 pst = start_psymtab_common (objfile, objfile->section_offsets,
4036 filename,
4037 /* TEXTLOW and TEXTHIGH are set below. */
4038 0,
4039 objfile->global_psymbols.next,
4040 objfile->static_psymbols.next);
4041 pst->psymtabs_addrmap_supported = 1;
4042
4043 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4044 if (attr != NULL)
4045 pst->dirname = DW_STRING (attr);
4046
4047 pst->read_symtab_private = per_cu;
4048
4049 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4050
4051 /* Store the function that reads in the rest of the symbol table. */
4052 pst->read_symtab = dwarf2_psymtab_to_symtab;
4053
4054 per_cu->v.psymtab = pst;
4055
4056 dwarf2_find_base_address (comp_unit_die, cu);
4057
4058 /* Possibly set the default values of LOWPC and HIGHPC from
4059 `DW_AT_ranges'. */
4060 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4061 &best_highpc, cu, pst);
4062 if (has_pc_info == 1 && best_lowpc < best_highpc)
4063 /* Store the contiguous range if it is not empty; it can be empty for
4064 CUs with no code. */
4065 addrmap_set_empty (objfile->psymtabs_addrmap,
4066 best_lowpc + baseaddr,
4067 best_highpc + baseaddr - 1, pst);
4068
4069 /* Check if comp unit has_children.
4070 If so, read the rest of the partial symbols from this comp unit.
4071 If not, there's no more debug_info for this comp unit. */
4072 if (has_children)
4073 {
4074 struct partial_die_info *first_die;
4075 CORE_ADDR lowpc, highpc;
4076
4077 lowpc = ((CORE_ADDR) -1);
4078 highpc = ((CORE_ADDR) 0);
4079
4080 first_die = load_partial_dies (reader, info_ptr, 1);
4081
4082 scan_partial_symbols (first_die, &lowpc, &highpc,
4083 ! has_pc_info, cu);
4084
4085 /* If we didn't find a lowpc, set it to highpc to avoid
4086 complaints from `maint check'. */
4087 if (lowpc == ((CORE_ADDR) -1))
4088 lowpc = highpc;
4089
4090 /* If the compilation unit didn't have an explicit address range,
4091 then use the information extracted from its child dies. */
4092 if (! has_pc_info)
4093 {
4094 best_lowpc = lowpc;
4095 best_highpc = highpc;
4096 }
4097 }
4098 pst->textlow = best_lowpc + baseaddr;
4099 pst->texthigh = best_highpc + baseaddr;
4100
4101 pst->n_global_syms = objfile->global_psymbols.next -
4102 (objfile->global_psymbols.list + pst->globals_offset);
4103 pst->n_static_syms = objfile->static_psymbols.next -
4104 (objfile->static_psymbols.list + pst->statics_offset);
4105 sort_pst_symbols (pst);
4106
4107 if (per_cu->is_debug_types)
4108 {
4109 /* It's not clear we want to do anything with stmt lists here.
4110 Waiting to see what gcc ultimately does. */
4111 }
4112 else
4113 {
4114 /* Get the list of files included in the current compilation unit,
4115 and build a psymtab for each of them. */
4116 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4117 }
4118 }
4119
4120 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4121 Process compilation unit THIS_CU for a psymtab. */
4122
4123 static void
4124 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu)
4125 {
4126 /* If this compilation unit was already read in, free the
4127 cached copy in order to read it in again. This is
4128 necessary because we skipped some symbols when we first
4129 read in the compilation unit (see load_partial_dies).
4130 This problem could be avoided, but the benefit is unclear. */
4131 if (this_cu->cu != NULL)
4132 free_one_cached_comp_unit (this_cu);
4133
4134 gdb_assert (! this_cu->is_debug_types);
4135 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4136 NULL);
4137
4138 /* Age out any secondary CUs. */
4139 age_cached_comp_units ();
4140 }
4141
4142 /* Traversal function for htab_traverse_noresize.
4143 Process one .debug_types comp-unit. */
4144
4145 static int
4146 process_psymtab_type_unit (void **slot, void *info)
4147 {
4148 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4149 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4150
4151 gdb_assert (per_cu->is_debug_types);
4152 gdb_assert (info == NULL);
4153
4154 /* If this compilation unit was already read in, free the
4155 cached copy in order to read it in again. This is
4156 necessary because we skipped some symbols when we first
4157 read in the compilation unit (see load_partial_dies).
4158 This problem could be avoided, but the benefit is unclear. */
4159 if (per_cu->cu != NULL)
4160 free_one_cached_comp_unit (per_cu);
4161
4162 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4163 NULL);
4164
4165 /* Age out any secondary CUs. */
4166 age_cached_comp_units ();
4167
4168 return 1;
4169 }
4170
4171 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4172 Build partial symbol tables for the .debug_types comp-units. */
4173
4174 static void
4175 build_type_psymtabs (struct objfile *objfile)
4176 {
4177 if (! create_all_type_units (objfile))
4178 return;
4179
4180 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4181 process_psymtab_type_unit, NULL);
4182 }
4183
4184 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4185
4186 static void
4187 psymtabs_addrmap_cleanup (void *o)
4188 {
4189 struct objfile *objfile = o;
4190
4191 objfile->psymtabs_addrmap = NULL;
4192 }
4193
4194 /* Build the partial symbol table by doing a quick pass through the
4195 .debug_info and .debug_abbrev sections. */
4196
4197 static void
4198 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4199 {
4200 struct cleanup *back_to, *addrmap_cleanup;
4201 struct obstack temp_obstack;
4202 int i;
4203
4204 dwarf2_per_objfile->reading_partial_symbols = 1;
4205
4206 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4207
4208 /* Any cached compilation units will be linked by the per-objfile
4209 read_in_chain. Make sure to free them when we're done. */
4210 back_to = make_cleanup (free_cached_comp_units, NULL);
4211
4212 build_type_psymtabs (objfile);
4213
4214 create_all_comp_units (objfile);
4215
4216 /* Create a temporary address map on a temporary obstack. We later
4217 copy this to the final obstack. */
4218 obstack_init (&temp_obstack);
4219 make_cleanup_obstack_free (&temp_obstack);
4220 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4221 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4222
4223 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4224 {
4225 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4226
4227 process_psymtab_comp_unit (per_cu);
4228 }
4229
4230 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4231 &objfile->objfile_obstack);
4232 discard_cleanups (addrmap_cleanup);
4233
4234 do_cleanups (back_to);
4235 }
4236
4237 /* die_reader_func for load_partial_comp_unit. */
4238
4239 static void
4240 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4241 gdb_byte *info_ptr,
4242 struct die_info *comp_unit_die,
4243 int has_children,
4244 void *data)
4245 {
4246 struct dwarf2_cu *cu = reader->cu;
4247
4248 prepare_one_comp_unit (cu, comp_unit_die);
4249
4250 /* Check if comp unit has_children.
4251 If so, read the rest of the partial symbols from this comp unit.
4252 If not, there's no more debug_info for this comp unit. */
4253 if (has_children)
4254 load_partial_dies (reader, info_ptr, 0);
4255 }
4256
4257 /* Load the partial DIEs for a secondary CU into memory.
4258 This is also used when rereading a primary CU with load_all_dies. */
4259
4260 static void
4261 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4262 {
4263 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4264 }
4265
4266 /* Create a list of all compilation units in OBJFILE.
4267 This is only done for -readnow and building partial symtabs. */
4268
4269 static void
4270 create_all_comp_units (struct objfile *objfile)
4271 {
4272 int n_allocated;
4273 int n_comp_units;
4274 struct dwarf2_per_cu_data **all_comp_units;
4275 gdb_byte *info_ptr;
4276
4277 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4278 info_ptr = dwarf2_per_objfile->info.buffer;
4279
4280 n_comp_units = 0;
4281 n_allocated = 10;
4282 all_comp_units = xmalloc (n_allocated
4283 * sizeof (struct dwarf2_per_cu_data *));
4284
4285 while (info_ptr < dwarf2_per_objfile->info.buffer
4286 + dwarf2_per_objfile->info.size)
4287 {
4288 unsigned int length, initial_length_size;
4289 struct dwarf2_per_cu_data *this_cu;
4290 sect_offset offset;
4291
4292 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4293
4294 /* Read just enough information to find out where the next
4295 compilation unit is. */
4296 length = read_initial_length (objfile->obfd, info_ptr,
4297 &initial_length_size);
4298
4299 /* Save the compilation unit for later lookup. */
4300 this_cu = obstack_alloc (&objfile->objfile_obstack,
4301 sizeof (struct dwarf2_per_cu_data));
4302 memset (this_cu, 0, sizeof (*this_cu));
4303 this_cu->offset = offset;
4304 this_cu->length = length + initial_length_size;
4305 this_cu->objfile = objfile;
4306 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4307
4308 if (n_comp_units == n_allocated)
4309 {
4310 n_allocated *= 2;
4311 all_comp_units = xrealloc (all_comp_units,
4312 n_allocated
4313 * sizeof (struct dwarf2_per_cu_data *));
4314 }
4315 all_comp_units[n_comp_units++] = this_cu;
4316
4317 info_ptr = info_ptr + this_cu->length;
4318 }
4319
4320 dwarf2_per_objfile->all_comp_units
4321 = obstack_alloc (&objfile->objfile_obstack,
4322 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4323 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4324 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4325 xfree (all_comp_units);
4326 dwarf2_per_objfile->n_comp_units = n_comp_units;
4327 }
4328
4329 /* Process all loaded DIEs for compilation unit CU, starting at
4330 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4331 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4332 DW_AT_ranges). If NEED_PC is set, then this function will set
4333 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4334 and record the covered ranges in the addrmap. */
4335
4336 static void
4337 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4338 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4339 {
4340 struct partial_die_info *pdi;
4341
4342 /* Now, march along the PDI's, descending into ones which have
4343 interesting children but skipping the children of the other ones,
4344 until we reach the end of the compilation unit. */
4345
4346 pdi = first_die;
4347
4348 while (pdi != NULL)
4349 {
4350 fixup_partial_die (pdi, cu);
4351
4352 /* Anonymous namespaces or modules have no name but have interesting
4353 children, so we need to look at them. Ditto for anonymous
4354 enums. */
4355
4356 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4357 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
4358 {
4359 switch (pdi->tag)
4360 {
4361 case DW_TAG_subprogram:
4362 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4363 break;
4364 case DW_TAG_constant:
4365 case DW_TAG_variable:
4366 case DW_TAG_typedef:
4367 case DW_TAG_union_type:
4368 if (!pdi->is_declaration)
4369 {
4370 add_partial_symbol (pdi, cu);
4371 }
4372 break;
4373 case DW_TAG_class_type:
4374 case DW_TAG_interface_type:
4375 case DW_TAG_structure_type:
4376 if (!pdi->is_declaration)
4377 {
4378 add_partial_symbol (pdi, cu);
4379 }
4380 break;
4381 case DW_TAG_enumeration_type:
4382 if (!pdi->is_declaration)
4383 add_partial_enumeration (pdi, cu);
4384 break;
4385 case DW_TAG_base_type:
4386 case DW_TAG_subrange_type:
4387 /* File scope base type definitions are added to the partial
4388 symbol table. */
4389 add_partial_symbol (pdi, cu);
4390 break;
4391 case DW_TAG_namespace:
4392 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4393 break;
4394 case DW_TAG_module:
4395 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4396 break;
4397 default:
4398 break;
4399 }
4400 }
4401
4402 /* If the die has a sibling, skip to the sibling. */
4403
4404 pdi = pdi->die_sibling;
4405 }
4406 }
4407
4408 /* Functions used to compute the fully scoped name of a partial DIE.
4409
4410 Normally, this is simple. For C++, the parent DIE's fully scoped
4411 name is concatenated with "::" and the partial DIE's name. For
4412 Java, the same thing occurs except that "." is used instead of "::".
4413 Enumerators are an exception; they use the scope of their parent
4414 enumeration type, i.e. the name of the enumeration type is not
4415 prepended to the enumerator.
4416
4417 There are two complexities. One is DW_AT_specification; in this
4418 case "parent" means the parent of the target of the specification,
4419 instead of the direct parent of the DIE. The other is compilers
4420 which do not emit DW_TAG_namespace; in this case we try to guess
4421 the fully qualified name of structure types from their members'
4422 linkage names. This must be done using the DIE's children rather
4423 than the children of any DW_AT_specification target. We only need
4424 to do this for structures at the top level, i.e. if the target of
4425 any DW_AT_specification (if any; otherwise the DIE itself) does not
4426 have a parent. */
4427
4428 /* Compute the scope prefix associated with PDI's parent, in
4429 compilation unit CU. The result will be allocated on CU's
4430 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4431 field. NULL is returned if no prefix is necessary. */
4432 static char *
4433 partial_die_parent_scope (struct partial_die_info *pdi,
4434 struct dwarf2_cu *cu)
4435 {
4436 char *grandparent_scope;
4437 struct partial_die_info *parent, *real_pdi;
4438
4439 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4440 then this means the parent of the specification DIE. */
4441
4442 real_pdi = pdi;
4443 while (real_pdi->has_specification)
4444 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4445
4446 parent = real_pdi->die_parent;
4447 if (parent == NULL)
4448 return NULL;
4449
4450 if (parent->scope_set)
4451 return parent->scope;
4452
4453 fixup_partial_die (parent, cu);
4454
4455 grandparent_scope = partial_die_parent_scope (parent, cu);
4456
4457 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4458 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4459 Work around this problem here. */
4460 if (cu->language == language_cplus
4461 && parent->tag == DW_TAG_namespace
4462 && strcmp (parent->name, "::") == 0
4463 && grandparent_scope == NULL)
4464 {
4465 parent->scope = NULL;
4466 parent->scope_set = 1;
4467 return NULL;
4468 }
4469
4470 if (pdi->tag == DW_TAG_enumerator)
4471 /* Enumerators should not get the name of the enumeration as a prefix. */
4472 parent->scope = grandparent_scope;
4473 else if (parent->tag == DW_TAG_namespace
4474 || parent->tag == DW_TAG_module
4475 || parent->tag == DW_TAG_structure_type
4476 || parent->tag == DW_TAG_class_type
4477 || parent->tag == DW_TAG_interface_type
4478 || parent->tag == DW_TAG_union_type
4479 || parent->tag == DW_TAG_enumeration_type)
4480 {
4481 if (grandparent_scope == NULL)
4482 parent->scope = parent->name;
4483 else
4484 parent->scope = typename_concat (&cu->comp_unit_obstack,
4485 grandparent_scope,
4486 parent->name, 0, cu);
4487 }
4488 else
4489 {
4490 /* FIXME drow/2004-04-01: What should we be doing with
4491 function-local names? For partial symbols, we should probably be
4492 ignoring them. */
4493 complaint (&symfile_complaints,
4494 _("unhandled containing DIE tag %d for DIE at %d"),
4495 parent->tag, pdi->offset.sect_off);
4496 parent->scope = grandparent_scope;
4497 }
4498
4499 parent->scope_set = 1;
4500 return parent->scope;
4501 }
4502
4503 /* Return the fully scoped name associated with PDI, from compilation unit
4504 CU. The result will be allocated with malloc. */
4505
4506 static char *
4507 partial_die_full_name (struct partial_die_info *pdi,
4508 struct dwarf2_cu *cu)
4509 {
4510 char *parent_scope;
4511
4512 /* If this is a template instantiation, we can not work out the
4513 template arguments from partial DIEs. So, unfortunately, we have
4514 to go through the full DIEs. At least any work we do building
4515 types here will be reused if full symbols are loaded later. */
4516 if (pdi->has_template_arguments)
4517 {
4518 fixup_partial_die (pdi, cu);
4519
4520 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4521 {
4522 struct die_info *die;
4523 struct attribute attr;
4524 struct dwarf2_cu *ref_cu = cu;
4525
4526 /* DW_FORM_ref_addr is using section offset. */
4527 attr.name = 0;
4528 attr.form = DW_FORM_ref_addr;
4529 attr.u.unsnd = pdi->offset.sect_off;
4530 die = follow_die_ref (NULL, &attr, &ref_cu);
4531
4532 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4533 }
4534 }
4535
4536 parent_scope = partial_die_parent_scope (pdi, cu);
4537 if (parent_scope == NULL)
4538 return NULL;
4539 else
4540 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4541 }
4542
4543 static void
4544 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4545 {
4546 struct objfile *objfile = cu->objfile;
4547 CORE_ADDR addr = 0;
4548 char *actual_name = NULL;
4549 CORE_ADDR baseaddr;
4550 int built_actual_name = 0;
4551
4552 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4553
4554 actual_name = partial_die_full_name (pdi, cu);
4555 if (actual_name)
4556 built_actual_name = 1;
4557
4558 if (actual_name == NULL)
4559 actual_name = pdi->name;
4560
4561 switch (pdi->tag)
4562 {
4563 case DW_TAG_subprogram:
4564 if (pdi->is_external || cu->language == language_ada)
4565 {
4566 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4567 of the global scope. But in Ada, we want to be able to access
4568 nested procedures globally. So all Ada subprograms are stored
4569 in the global scope. */
4570 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4571 mst_text, objfile); */
4572 add_psymbol_to_list (actual_name, strlen (actual_name),
4573 built_actual_name,
4574 VAR_DOMAIN, LOC_BLOCK,
4575 &objfile->global_psymbols,
4576 0, pdi->lowpc + baseaddr,
4577 cu->language, objfile);
4578 }
4579 else
4580 {
4581 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4582 mst_file_text, objfile); */
4583 add_psymbol_to_list (actual_name, strlen (actual_name),
4584 built_actual_name,
4585 VAR_DOMAIN, LOC_BLOCK,
4586 &objfile->static_psymbols,
4587 0, pdi->lowpc + baseaddr,
4588 cu->language, objfile);
4589 }
4590 break;
4591 case DW_TAG_constant:
4592 {
4593 struct psymbol_allocation_list *list;
4594
4595 if (pdi->is_external)
4596 list = &objfile->global_psymbols;
4597 else
4598 list = &objfile->static_psymbols;
4599 add_psymbol_to_list (actual_name, strlen (actual_name),
4600 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4601 list, 0, 0, cu->language, objfile);
4602 }
4603 break;
4604 case DW_TAG_variable:
4605 if (pdi->locdesc)
4606 addr = decode_locdesc (pdi->locdesc, cu);
4607
4608 if (pdi->locdesc
4609 && addr == 0
4610 && !dwarf2_per_objfile->has_section_at_zero)
4611 {
4612 /* A global or static variable may also have been stripped
4613 out by the linker if unused, in which case its address
4614 will be nullified; do not add such variables into partial
4615 symbol table then. */
4616 }
4617 else if (pdi->is_external)
4618 {
4619 /* Global Variable.
4620 Don't enter into the minimal symbol tables as there is
4621 a minimal symbol table entry from the ELF symbols already.
4622 Enter into partial symbol table if it has a location
4623 descriptor or a type.
4624 If the location descriptor is missing, new_symbol will create
4625 a LOC_UNRESOLVED symbol, the address of the variable will then
4626 be determined from the minimal symbol table whenever the variable
4627 is referenced.
4628 The address for the partial symbol table entry is not
4629 used by GDB, but it comes in handy for debugging partial symbol
4630 table building. */
4631
4632 if (pdi->locdesc || pdi->has_type)
4633 add_psymbol_to_list (actual_name, strlen (actual_name),
4634 built_actual_name,
4635 VAR_DOMAIN, LOC_STATIC,
4636 &objfile->global_psymbols,
4637 0, addr + baseaddr,
4638 cu->language, objfile);
4639 }
4640 else
4641 {
4642 /* Static Variable. Skip symbols without location descriptors. */
4643 if (pdi->locdesc == NULL)
4644 {
4645 if (built_actual_name)
4646 xfree (actual_name);
4647 return;
4648 }
4649 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4650 mst_file_data, objfile); */
4651 add_psymbol_to_list (actual_name, strlen (actual_name),
4652 built_actual_name,
4653 VAR_DOMAIN, LOC_STATIC,
4654 &objfile->static_psymbols,
4655 0, addr + baseaddr,
4656 cu->language, objfile);
4657 }
4658 break;
4659 case DW_TAG_typedef:
4660 case DW_TAG_base_type:
4661 case DW_TAG_subrange_type:
4662 add_psymbol_to_list (actual_name, strlen (actual_name),
4663 built_actual_name,
4664 VAR_DOMAIN, LOC_TYPEDEF,
4665 &objfile->static_psymbols,
4666 0, (CORE_ADDR) 0, cu->language, objfile);
4667 break;
4668 case DW_TAG_namespace:
4669 add_psymbol_to_list (actual_name, strlen (actual_name),
4670 built_actual_name,
4671 VAR_DOMAIN, LOC_TYPEDEF,
4672 &objfile->global_psymbols,
4673 0, (CORE_ADDR) 0, cu->language, objfile);
4674 break;
4675 case DW_TAG_class_type:
4676 case DW_TAG_interface_type:
4677 case DW_TAG_structure_type:
4678 case DW_TAG_union_type:
4679 case DW_TAG_enumeration_type:
4680 /* Skip external references. The DWARF standard says in the section
4681 about "Structure, Union, and Class Type Entries": "An incomplete
4682 structure, union or class type is represented by a structure,
4683 union or class entry that does not have a byte size attribute
4684 and that has a DW_AT_declaration attribute." */
4685 if (!pdi->has_byte_size && pdi->is_declaration)
4686 {
4687 if (built_actual_name)
4688 xfree (actual_name);
4689 return;
4690 }
4691
4692 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4693 static vs. global. */
4694 add_psymbol_to_list (actual_name, strlen (actual_name),
4695 built_actual_name,
4696 STRUCT_DOMAIN, LOC_TYPEDEF,
4697 (cu->language == language_cplus
4698 || cu->language == language_java)
4699 ? &objfile->global_psymbols
4700 : &objfile->static_psymbols,
4701 0, (CORE_ADDR) 0, cu->language, objfile);
4702
4703 break;
4704 case DW_TAG_enumerator:
4705 add_psymbol_to_list (actual_name, strlen (actual_name),
4706 built_actual_name,
4707 VAR_DOMAIN, LOC_CONST,
4708 (cu->language == language_cplus
4709 || cu->language == language_java)
4710 ? &objfile->global_psymbols
4711 : &objfile->static_psymbols,
4712 0, (CORE_ADDR) 0, cu->language, objfile);
4713 break;
4714 default:
4715 break;
4716 }
4717
4718 if (built_actual_name)
4719 xfree (actual_name);
4720 }
4721
4722 /* Read a partial die corresponding to a namespace; also, add a symbol
4723 corresponding to that namespace to the symbol table. NAMESPACE is
4724 the name of the enclosing namespace. */
4725
4726 static void
4727 add_partial_namespace (struct partial_die_info *pdi,
4728 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4729 int need_pc, struct dwarf2_cu *cu)
4730 {
4731 /* Add a symbol for the namespace. */
4732
4733 add_partial_symbol (pdi, cu);
4734
4735 /* Now scan partial symbols in that namespace. */
4736
4737 if (pdi->has_children)
4738 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4739 }
4740
4741 /* Read a partial die corresponding to a Fortran module. */
4742
4743 static void
4744 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4745 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4746 {
4747 /* Now scan partial symbols in that module. */
4748
4749 if (pdi->has_children)
4750 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4751 }
4752
4753 /* Read a partial die corresponding to a subprogram and create a partial
4754 symbol for that subprogram. When the CU language allows it, this
4755 routine also defines a partial symbol for each nested subprogram
4756 that this subprogram contains.
4757
4758 DIE my also be a lexical block, in which case we simply search
4759 recursively for suprograms defined inside that lexical block.
4760 Again, this is only performed when the CU language allows this
4761 type of definitions. */
4762
4763 static void
4764 add_partial_subprogram (struct partial_die_info *pdi,
4765 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4766 int need_pc, struct dwarf2_cu *cu)
4767 {
4768 if (pdi->tag == DW_TAG_subprogram)
4769 {
4770 if (pdi->has_pc_info)
4771 {
4772 if (pdi->lowpc < *lowpc)
4773 *lowpc = pdi->lowpc;
4774 if (pdi->highpc > *highpc)
4775 *highpc = pdi->highpc;
4776 if (need_pc)
4777 {
4778 CORE_ADDR baseaddr;
4779 struct objfile *objfile = cu->objfile;
4780
4781 baseaddr = ANOFFSET (objfile->section_offsets,
4782 SECT_OFF_TEXT (objfile));
4783 addrmap_set_empty (objfile->psymtabs_addrmap,
4784 pdi->lowpc + baseaddr,
4785 pdi->highpc - 1 + baseaddr,
4786 cu->per_cu->v.psymtab);
4787 }
4788 }
4789
4790 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4791 {
4792 if (!pdi->is_declaration)
4793 /* Ignore subprogram DIEs that do not have a name, they are
4794 illegal. Do not emit a complaint at this point, we will
4795 do so when we convert this psymtab into a symtab. */
4796 if (pdi->name)
4797 add_partial_symbol (pdi, cu);
4798 }
4799 }
4800
4801 if (! pdi->has_children)
4802 return;
4803
4804 if (cu->language == language_ada)
4805 {
4806 pdi = pdi->die_child;
4807 while (pdi != NULL)
4808 {
4809 fixup_partial_die (pdi, cu);
4810 if (pdi->tag == DW_TAG_subprogram
4811 || pdi->tag == DW_TAG_lexical_block)
4812 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4813 pdi = pdi->die_sibling;
4814 }
4815 }
4816 }
4817
4818 /* Read a partial die corresponding to an enumeration type. */
4819
4820 static void
4821 add_partial_enumeration (struct partial_die_info *enum_pdi,
4822 struct dwarf2_cu *cu)
4823 {
4824 struct partial_die_info *pdi;
4825
4826 if (enum_pdi->name != NULL)
4827 add_partial_symbol (enum_pdi, cu);
4828
4829 pdi = enum_pdi->die_child;
4830 while (pdi)
4831 {
4832 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4833 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4834 else
4835 add_partial_symbol (pdi, cu);
4836 pdi = pdi->die_sibling;
4837 }
4838 }
4839
4840 /* Return the initial uleb128 in the die at INFO_PTR. */
4841
4842 static unsigned int
4843 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4844 {
4845 unsigned int bytes_read;
4846
4847 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4848 }
4849
4850 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4851 Return the corresponding abbrev, or NULL if the number is zero (indicating
4852 an empty DIE). In either case *BYTES_READ will be set to the length of
4853 the initial number. */
4854
4855 static struct abbrev_info *
4856 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4857 struct dwarf2_cu *cu)
4858 {
4859 bfd *abfd = cu->objfile->obfd;
4860 unsigned int abbrev_number;
4861 struct abbrev_info *abbrev;
4862
4863 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4864
4865 if (abbrev_number == 0)
4866 return NULL;
4867
4868 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4869 if (!abbrev)
4870 {
4871 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4872 abbrev_number, bfd_get_filename (abfd));
4873 }
4874
4875 return abbrev;
4876 }
4877
4878 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4879 Returns a pointer to the end of a series of DIEs, terminated by an empty
4880 DIE. Any children of the skipped DIEs will also be skipped. */
4881
4882 static gdb_byte *
4883 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4884 {
4885 struct dwarf2_cu *cu = reader->cu;
4886 struct abbrev_info *abbrev;
4887 unsigned int bytes_read;
4888
4889 while (1)
4890 {
4891 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4892 if (abbrev == NULL)
4893 return info_ptr + bytes_read;
4894 else
4895 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4896 }
4897 }
4898
4899 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4900 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4901 abbrev corresponding to that skipped uleb128 should be passed in
4902 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4903 children. */
4904
4905 static gdb_byte *
4906 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
4907 struct abbrev_info *abbrev)
4908 {
4909 unsigned int bytes_read;
4910 struct attribute attr;
4911 bfd *abfd = reader->abfd;
4912 struct dwarf2_cu *cu = reader->cu;
4913 gdb_byte *buffer = reader->buffer;
4914 unsigned int form, i;
4915
4916 for (i = 0; i < abbrev->num_attrs; i++)
4917 {
4918 /* The only abbrev we care about is DW_AT_sibling. */
4919 if (abbrev->attrs[i].name == DW_AT_sibling)
4920 {
4921 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4922 if (attr.form == DW_FORM_ref_addr)
4923 complaint (&symfile_complaints,
4924 _("ignoring absolute DW_AT_sibling"));
4925 else
4926 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4927 }
4928
4929 /* If it isn't DW_AT_sibling, skip this attribute. */
4930 form = abbrev->attrs[i].form;
4931 skip_attribute:
4932 switch (form)
4933 {
4934 case DW_FORM_ref_addr:
4935 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4936 and later it is offset sized. */
4937 if (cu->header.version == 2)
4938 info_ptr += cu->header.addr_size;
4939 else
4940 info_ptr += cu->header.offset_size;
4941 break;
4942 case DW_FORM_addr:
4943 info_ptr += cu->header.addr_size;
4944 break;
4945 case DW_FORM_data1:
4946 case DW_FORM_ref1:
4947 case DW_FORM_flag:
4948 info_ptr += 1;
4949 break;
4950 case DW_FORM_flag_present:
4951 break;
4952 case DW_FORM_data2:
4953 case DW_FORM_ref2:
4954 info_ptr += 2;
4955 break;
4956 case DW_FORM_data4:
4957 case DW_FORM_ref4:
4958 info_ptr += 4;
4959 break;
4960 case DW_FORM_data8:
4961 case DW_FORM_ref8:
4962 case DW_FORM_ref_sig8:
4963 info_ptr += 8;
4964 break;
4965 case DW_FORM_string:
4966 read_direct_string (abfd, info_ptr, &bytes_read);
4967 info_ptr += bytes_read;
4968 break;
4969 case DW_FORM_sec_offset:
4970 case DW_FORM_strp:
4971 info_ptr += cu->header.offset_size;
4972 break;
4973 case DW_FORM_exprloc:
4974 case DW_FORM_block:
4975 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4976 info_ptr += bytes_read;
4977 break;
4978 case DW_FORM_block1:
4979 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4980 break;
4981 case DW_FORM_block2:
4982 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4983 break;
4984 case DW_FORM_block4:
4985 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4986 break;
4987 case DW_FORM_sdata:
4988 case DW_FORM_udata:
4989 case DW_FORM_ref_udata:
4990 case DW_FORM_GNU_addr_index:
4991 case DW_FORM_GNU_str_index:
4992 info_ptr = skip_leb128 (abfd, info_ptr);
4993 break;
4994 case DW_FORM_indirect:
4995 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4996 info_ptr += bytes_read;
4997 /* We need to continue parsing from here, so just go back to
4998 the top. */
4999 goto skip_attribute;
5000
5001 default:
5002 error (_("Dwarf Error: Cannot handle %s "
5003 "in DWARF reader [in module %s]"),
5004 dwarf_form_name (form),
5005 bfd_get_filename (abfd));
5006 }
5007 }
5008
5009 if (abbrev->has_children)
5010 return skip_children (reader, info_ptr);
5011 else
5012 return info_ptr;
5013 }
5014
5015 /* Locate ORIG_PDI's sibling.
5016 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5017
5018 static gdb_byte *
5019 locate_pdi_sibling (const struct die_reader_specs *reader,
5020 struct partial_die_info *orig_pdi,
5021 gdb_byte *info_ptr)
5022 {
5023 /* Do we know the sibling already? */
5024
5025 if (orig_pdi->sibling)
5026 return orig_pdi->sibling;
5027
5028 /* Are there any children to deal with? */
5029
5030 if (!orig_pdi->has_children)
5031 return info_ptr;
5032
5033 /* Skip the children the long way. */
5034
5035 return skip_children (reader, info_ptr);
5036 }
5037
5038 /* Expand this partial symbol table into a full symbol table. */
5039
5040 static void
5041 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5042 {
5043 if (pst != NULL)
5044 {
5045 if (pst->readin)
5046 {
5047 warning (_("bug: psymtab for %s is already read in."),
5048 pst->filename);
5049 }
5050 else
5051 {
5052 if (info_verbose)
5053 {
5054 printf_filtered (_("Reading in symbols for %s..."),
5055 pst->filename);
5056 gdb_flush (gdb_stdout);
5057 }
5058
5059 /* Restore our global data. */
5060 dwarf2_per_objfile = objfile_data (pst->objfile,
5061 dwarf2_objfile_data_key);
5062
5063 /* If this psymtab is constructed from a debug-only objfile, the
5064 has_section_at_zero flag will not necessarily be correct. We
5065 can get the correct value for this flag by looking at the data
5066 associated with the (presumably stripped) associated objfile. */
5067 if (pst->objfile->separate_debug_objfile_backlink)
5068 {
5069 struct dwarf2_per_objfile *dpo_backlink
5070 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5071 dwarf2_objfile_data_key);
5072
5073 dwarf2_per_objfile->has_section_at_zero
5074 = dpo_backlink->has_section_at_zero;
5075 }
5076
5077 dwarf2_per_objfile->reading_partial_symbols = 0;
5078
5079 psymtab_to_symtab_1 (pst);
5080
5081 /* Finish up the debug error message. */
5082 if (info_verbose)
5083 printf_filtered (_("done.\n"));
5084 }
5085 }
5086 }
5087 \f
5088 /* Reading in full CUs. */
5089
5090 /* Add PER_CU to the queue. */
5091
5092 static void
5093 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
5094 {
5095 struct dwarf2_queue_item *item;
5096
5097 per_cu->queued = 1;
5098 item = xmalloc (sizeof (*item));
5099 item->per_cu = per_cu;
5100 item->next = NULL;
5101
5102 if (dwarf2_queue == NULL)
5103 dwarf2_queue = item;
5104 else
5105 dwarf2_queue_tail->next = item;
5106
5107 dwarf2_queue_tail = item;
5108 }
5109
5110 /* Process the queue. */
5111
5112 static void
5113 process_queue (void)
5114 {
5115 struct dwarf2_queue_item *item, *next_item;
5116
5117 /* The queue starts out with one item, but following a DIE reference
5118 may load a new CU, adding it to the end of the queue. */
5119 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5120 {
5121 if (dwarf2_per_objfile->using_index
5122 ? !item->per_cu->v.quick->symtab
5123 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5124 process_full_comp_unit (item->per_cu);
5125
5126 item->per_cu->queued = 0;
5127 next_item = item->next;
5128 xfree (item);
5129 }
5130
5131 dwarf2_queue_tail = NULL;
5132 }
5133
5134 /* Free all allocated queue entries. This function only releases anything if
5135 an error was thrown; if the queue was processed then it would have been
5136 freed as we went along. */
5137
5138 static void
5139 dwarf2_release_queue (void *dummy)
5140 {
5141 struct dwarf2_queue_item *item, *last;
5142
5143 item = dwarf2_queue;
5144 while (item)
5145 {
5146 /* Anything still marked queued is likely to be in an
5147 inconsistent state, so discard it. */
5148 if (item->per_cu->queued)
5149 {
5150 if (item->per_cu->cu != NULL)
5151 free_one_cached_comp_unit (item->per_cu);
5152 item->per_cu->queued = 0;
5153 }
5154
5155 last = item;
5156 item = item->next;
5157 xfree (last);
5158 }
5159
5160 dwarf2_queue = dwarf2_queue_tail = NULL;
5161 }
5162
5163 /* Read in full symbols for PST, and anything it depends on. */
5164
5165 static void
5166 psymtab_to_symtab_1 (struct partial_symtab *pst)
5167 {
5168 struct dwarf2_per_cu_data *per_cu;
5169 struct cleanup *back_to;
5170 int i;
5171
5172 for (i = 0; i < pst->number_of_dependencies; i++)
5173 if (!pst->dependencies[i]->readin)
5174 {
5175 /* Inform about additional files that need to be read in. */
5176 if (info_verbose)
5177 {
5178 /* FIXME: i18n: Need to make this a single string. */
5179 fputs_filtered (" ", gdb_stdout);
5180 wrap_here ("");
5181 fputs_filtered ("and ", gdb_stdout);
5182 wrap_here ("");
5183 printf_filtered ("%s...", pst->dependencies[i]->filename);
5184 wrap_here (""); /* Flush output. */
5185 gdb_flush (gdb_stdout);
5186 }
5187 psymtab_to_symtab_1 (pst->dependencies[i]);
5188 }
5189
5190 per_cu = pst->read_symtab_private;
5191
5192 if (per_cu == NULL)
5193 {
5194 /* It's an include file, no symbols to read for it.
5195 Everything is in the parent symtab. */
5196 pst->readin = 1;
5197 return;
5198 }
5199
5200 dw2_do_instantiate_symtab (per_cu);
5201 }
5202
5203 /* Trivial hash function for die_info: the hash value of a DIE
5204 is its offset in .debug_info for this objfile. */
5205
5206 static hashval_t
5207 die_hash (const void *item)
5208 {
5209 const struct die_info *die = item;
5210
5211 return die->offset.sect_off;
5212 }
5213
5214 /* Trivial comparison function for die_info structures: two DIEs
5215 are equal if they have the same offset. */
5216
5217 static int
5218 die_eq (const void *item_lhs, const void *item_rhs)
5219 {
5220 const struct die_info *die_lhs = item_lhs;
5221 const struct die_info *die_rhs = item_rhs;
5222
5223 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5224 }
5225
5226 /* die_reader_func for load_full_comp_unit.
5227 This is identical to read_signatured_type_reader,
5228 but is kept separate for now. */
5229
5230 static void
5231 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5232 gdb_byte *info_ptr,
5233 struct die_info *comp_unit_die,
5234 int has_children,
5235 void *data)
5236 {
5237 struct dwarf2_cu *cu = reader->cu;
5238 struct attribute *attr;
5239
5240 gdb_assert (cu->die_hash == NULL);
5241 cu->die_hash =
5242 htab_create_alloc_ex (cu->header.length / 12,
5243 die_hash,
5244 die_eq,
5245 NULL,
5246 &cu->comp_unit_obstack,
5247 hashtab_obstack_allocate,
5248 dummy_obstack_deallocate);
5249
5250 if (has_children)
5251 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5252 &info_ptr, comp_unit_die);
5253 cu->dies = comp_unit_die;
5254 /* comp_unit_die is not stored in die_hash, no need. */
5255
5256 /* We try not to read any attributes in this function, because not
5257 all CUs needed for references have been loaded yet, and symbol
5258 table processing isn't initialized. But we have to set the CU language,
5259 or we won't be able to build types correctly.
5260 Similarly, if we do not read the producer, we can not apply
5261 producer-specific interpretation. */
5262 prepare_one_comp_unit (cu, cu->dies);
5263 }
5264
5265 /* Load the DIEs associated with PER_CU into memory. */
5266
5267 static void
5268 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu)
5269 {
5270 gdb_assert (! this_cu->is_debug_types);
5271
5272 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader, NULL);
5273 }
5274
5275 /* Add a DIE to the delayed physname list. */
5276
5277 static void
5278 add_to_method_list (struct type *type, int fnfield_index, int index,
5279 const char *name, struct die_info *die,
5280 struct dwarf2_cu *cu)
5281 {
5282 struct delayed_method_info mi;
5283 mi.type = type;
5284 mi.fnfield_index = fnfield_index;
5285 mi.index = index;
5286 mi.name = name;
5287 mi.die = die;
5288 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5289 }
5290
5291 /* A cleanup for freeing the delayed method list. */
5292
5293 static void
5294 free_delayed_list (void *ptr)
5295 {
5296 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5297 if (cu->method_list != NULL)
5298 {
5299 VEC_free (delayed_method_info, cu->method_list);
5300 cu->method_list = NULL;
5301 }
5302 }
5303
5304 /* Compute the physnames of any methods on the CU's method list.
5305
5306 The computation of method physnames is delayed in order to avoid the
5307 (bad) condition that one of the method's formal parameters is of an as yet
5308 incomplete type. */
5309
5310 static void
5311 compute_delayed_physnames (struct dwarf2_cu *cu)
5312 {
5313 int i;
5314 struct delayed_method_info *mi;
5315 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5316 {
5317 const char *physname;
5318 struct fn_fieldlist *fn_flp
5319 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5320 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5321 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5322 }
5323 }
5324
5325 /* Go objects should be embedded in a DW_TAG_module DIE,
5326 and it's not clear if/how imported objects will appear.
5327 To keep Go support simple until that's worked out,
5328 go back through what we've read and create something usable.
5329 We could do this while processing each DIE, and feels kinda cleaner,
5330 but that way is more invasive.
5331 This is to, for example, allow the user to type "p var" or "b main"
5332 without having to specify the package name, and allow lookups
5333 of module.object to work in contexts that use the expression
5334 parser. */
5335
5336 static void
5337 fixup_go_packaging (struct dwarf2_cu *cu)
5338 {
5339 char *package_name = NULL;
5340 struct pending *list;
5341 int i;
5342
5343 for (list = global_symbols; list != NULL; list = list->next)
5344 {
5345 for (i = 0; i < list->nsyms; ++i)
5346 {
5347 struct symbol *sym = list->symbol[i];
5348
5349 if (SYMBOL_LANGUAGE (sym) == language_go
5350 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5351 {
5352 char *this_package_name = go_symbol_package_name (sym);
5353
5354 if (this_package_name == NULL)
5355 continue;
5356 if (package_name == NULL)
5357 package_name = this_package_name;
5358 else
5359 {
5360 if (strcmp (package_name, this_package_name) != 0)
5361 complaint (&symfile_complaints,
5362 _("Symtab %s has objects from two different Go packages: %s and %s"),
5363 (sym->symtab && sym->symtab->filename
5364 ? sym->symtab->filename
5365 : cu->objfile->name),
5366 this_package_name, package_name);
5367 xfree (this_package_name);
5368 }
5369 }
5370 }
5371 }
5372
5373 if (package_name != NULL)
5374 {
5375 struct objfile *objfile = cu->objfile;
5376 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5377 package_name, objfile);
5378 struct symbol *sym;
5379
5380 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5381
5382 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5383 SYMBOL_SET_LANGUAGE (sym, language_go);
5384 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5385 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5386 e.g., "main" finds the "main" module and not C's main(). */
5387 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5388 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5389 SYMBOL_TYPE (sym) = type;
5390
5391 add_symbol_to_list (sym, &global_symbols);
5392
5393 xfree (package_name);
5394 }
5395 }
5396
5397 /* Generate full symbol information for PER_CU, whose DIEs have
5398 already been loaded into memory. */
5399
5400 static void
5401 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
5402 {
5403 struct dwarf2_cu *cu = per_cu->cu;
5404 struct objfile *objfile = per_cu->objfile;
5405 CORE_ADDR lowpc, highpc;
5406 struct symtab *symtab;
5407 struct cleanup *back_to, *delayed_list_cleanup;
5408 CORE_ADDR baseaddr;
5409
5410 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5411
5412 buildsym_init ();
5413 back_to = make_cleanup (really_free_pendings, NULL);
5414 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5415
5416 cu->list_in_scope = &file_symbols;
5417
5418 /* Do line number decoding in read_file_scope () */
5419 process_die (cu->dies, cu);
5420
5421 /* For now fudge the Go package. */
5422 if (cu->language == language_go)
5423 fixup_go_packaging (cu);
5424
5425 /* Now that we have processed all the DIEs in the CU, all the types
5426 should be complete, and it should now be safe to compute all of the
5427 physnames. */
5428 compute_delayed_physnames (cu);
5429 do_cleanups (delayed_list_cleanup);
5430
5431 /* Some compilers don't define a DW_AT_high_pc attribute for the
5432 compilation unit. If the DW_AT_high_pc is missing, synthesize
5433 it, by scanning the DIE's below the compilation unit. */
5434 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5435
5436 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5437
5438 if (symtab != NULL)
5439 {
5440 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5441
5442 /* Set symtab language to language from DW_AT_language. If the
5443 compilation is from a C file generated by language preprocessors, do
5444 not set the language if it was already deduced by start_subfile. */
5445 if (!(cu->language == language_c && symtab->language != language_c))
5446 symtab->language = cu->language;
5447
5448 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5449 produce DW_AT_location with location lists but it can be possibly
5450 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5451 there were bugs in prologue debug info, fixed later in GCC-4.5
5452 by "unwind info for epilogues" patch (which is not directly related).
5453
5454 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5455 needed, it would be wrong due to missing DW_AT_producer there.
5456
5457 Still one can confuse GDB by using non-standard GCC compilation
5458 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5459 */
5460 if (cu->has_loclist && gcc_4_minor >= 5)
5461 symtab->locations_valid = 1;
5462
5463 if (gcc_4_minor >= 5)
5464 symtab->epilogue_unwind_valid = 1;
5465
5466 symtab->call_site_htab = cu->call_site_htab;
5467 }
5468
5469 if (dwarf2_per_objfile->using_index)
5470 per_cu->v.quick->symtab = symtab;
5471 else
5472 {
5473 struct partial_symtab *pst = per_cu->v.psymtab;
5474 pst->symtab = symtab;
5475 pst->readin = 1;
5476 }
5477
5478 do_cleanups (back_to);
5479 }
5480
5481 /* Process a die and its children. */
5482
5483 static void
5484 process_die (struct die_info *die, struct dwarf2_cu *cu)
5485 {
5486 switch (die->tag)
5487 {
5488 case DW_TAG_padding:
5489 break;
5490 case DW_TAG_compile_unit:
5491 read_file_scope (die, cu);
5492 break;
5493 case DW_TAG_type_unit:
5494 read_type_unit_scope (die, cu);
5495 break;
5496 case DW_TAG_subprogram:
5497 case DW_TAG_inlined_subroutine:
5498 read_func_scope (die, cu);
5499 break;
5500 case DW_TAG_lexical_block:
5501 case DW_TAG_try_block:
5502 case DW_TAG_catch_block:
5503 read_lexical_block_scope (die, cu);
5504 break;
5505 case DW_TAG_GNU_call_site:
5506 read_call_site_scope (die, cu);
5507 break;
5508 case DW_TAG_class_type:
5509 case DW_TAG_interface_type:
5510 case DW_TAG_structure_type:
5511 case DW_TAG_union_type:
5512 process_structure_scope (die, cu);
5513 break;
5514 case DW_TAG_enumeration_type:
5515 process_enumeration_scope (die, cu);
5516 break;
5517
5518 /* These dies have a type, but processing them does not create
5519 a symbol or recurse to process the children. Therefore we can
5520 read them on-demand through read_type_die. */
5521 case DW_TAG_subroutine_type:
5522 case DW_TAG_set_type:
5523 case DW_TAG_array_type:
5524 case DW_TAG_pointer_type:
5525 case DW_TAG_ptr_to_member_type:
5526 case DW_TAG_reference_type:
5527 case DW_TAG_string_type:
5528 break;
5529
5530 case DW_TAG_base_type:
5531 case DW_TAG_subrange_type:
5532 case DW_TAG_typedef:
5533 /* Add a typedef symbol for the type definition, if it has a
5534 DW_AT_name. */
5535 new_symbol (die, read_type_die (die, cu), cu);
5536 break;
5537 case DW_TAG_common_block:
5538 read_common_block (die, cu);
5539 break;
5540 case DW_TAG_common_inclusion:
5541 break;
5542 case DW_TAG_namespace:
5543 processing_has_namespace_info = 1;
5544 read_namespace (die, cu);
5545 break;
5546 case DW_TAG_module:
5547 processing_has_namespace_info = 1;
5548 read_module (die, cu);
5549 break;
5550 case DW_TAG_imported_declaration:
5551 case DW_TAG_imported_module:
5552 processing_has_namespace_info = 1;
5553 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5554 || cu->language != language_fortran))
5555 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5556 dwarf_tag_name (die->tag));
5557 read_import_statement (die, cu);
5558 break;
5559 default:
5560 new_symbol (die, NULL, cu);
5561 break;
5562 }
5563 }
5564
5565 /* A helper function for dwarf2_compute_name which determines whether DIE
5566 needs to have the name of the scope prepended to the name listed in the
5567 die. */
5568
5569 static int
5570 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5571 {
5572 struct attribute *attr;
5573
5574 switch (die->tag)
5575 {
5576 case DW_TAG_namespace:
5577 case DW_TAG_typedef:
5578 case DW_TAG_class_type:
5579 case DW_TAG_interface_type:
5580 case DW_TAG_structure_type:
5581 case DW_TAG_union_type:
5582 case DW_TAG_enumeration_type:
5583 case DW_TAG_enumerator:
5584 case DW_TAG_subprogram:
5585 case DW_TAG_member:
5586 return 1;
5587
5588 case DW_TAG_variable:
5589 case DW_TAG_constant:
5590 /* We only need to prefix "globally" visible variables. These include
5591 any variable marked with DW_AT_external or any variable that
5592 lives in a namespace. [Variables in anonymous namespaces
5593 require prefixing, but they are not DW_AT_external.] */
5594
5595 if (dwarf2_attr (die, DW_AT_specification, cu))
5596 {
5597 struct dwarf2_cu *spec_cu = cu;
5598
5599 return die_needs_namespace (die_specification (die, &spec_cu),
5600 spec_cu);
5601 }
5602
5603 attr = dwarf2_attr (die, DW_AT_external, cu);
5604 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5605 && die->parent->tag != DW_TAG_module)
5606 return 0;
5607 /* A variable in a lexical block of some kind does not need a
5608 namespace, even though in C++ such variables may be external
5609 and have a mangled name. */
5610 if (die->parent->tag == DW_TAG_lexical_block
5611 || die->parent->tag == DW_TAG_try_block
5612 || die->parent->tag == DW_TAG_catch_block
5613 || die->parent->tag == DW_TAG_subprogram)
5614 return 0;
5615 return 1;
5616
5617 default:
5618 return 0;
5619 }
5620 }
5621
5622 /* Retrieve the last character from a mem_file. */
5623
5624 static void
5625 do_ui_file_peek_last (void *object, const char *buffer, long length)
5626 {
5627 char *last_char_p = (char *) object;
5628
5629 if (length > 0)
5630 *last_char_p = buffer[length - 1];
5631 }
5632
5633 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5634 compute the physname for the object, which include a method's:
5635 - formal parameters (C++/Java),
5636 - receiver type (Go),
5637 - return type (Java).
5638
5639 The term "physname" is a bit confusing.
5640 For C++, for example, it is the demangled name.
5641 For Go, for example, it's the mangled name.
5642
5643 For Ada, return the DIE's linkage name rather than the fully qualified
5644 name. PHYSNAME is ignored..
5645
5646 The result is allocated on the objfile_obstack and canonicalized. */
5647
5648 static const char *
5649 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5650 int physname)
5651 {
5652 struct objfile *objfile = cu->objfile;
5653
5654 if (name == NULL)
5655 name = dwarf2_name (die, cu);
5656
5657 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5658 compute it by typename_concat inside GDB. */
5659 if (cu->language == language_ada
5660 || (cu->language == language_fortran && physname))
5661 {
5662 /* For Ada unit, we prefer the linkage name over the name, as
5663 the former contains the exported name, which the user expects
5664 to be able to reference. Ideally, we want the user to be able
5665 to reference this entity using either natural or linkage name,
5666 but we haven't started looking at this enhancement yet. */
5667 struct attribute *attr;
5668
5669 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5670 if (attr == NULL)
5671 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5672 if (attr && DW_STRING (attr))
5673 return DW_STRING (attr);
5674 }
5675
5676 /* These are the only languages we know how to qualify names in. */
5677 if (name != NULL
5678 && (cu->language == language_cplus || cu->language == language_java
5679 || cu->language == language_fortran))
5680 {
5681 if (die_needs_namespace (die, cu))
5682 {
5683 long length;
5684 const char *prefix;
5685 struct ui_file *buf;
5686
5687 prefix = determine_prefix (die, cu);
5688 buf = mem_fileopen ();
5689 if (*prefix != '\0')
5690 {
5691 char *prefixed_name = typename_concat (NULL, prefix, name,
5692 physname, cu);
5693
5694 fputs_unfiltered (prefixed_name, buf);
5695 xfree (prefixed_name);
5696 }
5697 else
5698 fputs_unfiltered (name, buf);
5699
5700 /* Template parameters may be specified in the DIE's DW_AT_name, or
5701 as children with DW_TAG_template_type_param or
5702 DW_TAG_value_type_param. If the latter, add them to the name
5703 here. If the name already has template parameters, then
5704 skip this step; some versions of GCC emit both, and
5705 it is more efficient to use the pre-computed name.
5706
5707 Something to keep in mind about this process: it is very
5708 unlikely, or in some cases downright impossible, to produce
5709 something that will match the mangled name of a function.
5710 If the definition of the function has the same debug info,
5711 we should be able to match up with it anyway. But fallbacks
5712 using the minimal symbol, for instance to find a method
5713 implemented in a stripped copy of libstdc++, will not work.
5714 If we do not have debug info for the definition, we will have to
5715 match them up some other way.
5716
5717 When we do name matching there is a related problem with function
5718 templates; two instantiated function templates are allowed to
5719 differ only by their return types, which we do not add here. */
5720
5721 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5722 {
5723 struct attribute *attr;
5724 struct die_info *child;
5725 int first = 1;
5726
5727 die->building_fullname = 1;
5728
5729 for (child = die->child; child != NULL; child = child->sibling)
5730 {
5731 struct type *type;
5732 LONGEST value;
5733 gdb_byte *bytes;
5734 struct dwarf2_locexpr_baton *baton;
5735 struct value *v;
5736
5737 if (child->tag != DW_TAG_template_type_param
5738 && child->tag != DW_TAG_template_value_param)
5739 continue;
5740
5741 if (first)
5742 {
5743 fputs_unfiltered ("<", buf);
5744 first = 0;
5745 }
5746 else
5747 fputs_unfiltered (", ", buf);
5748
5749 attr = dwarf2_attr (child, DW_AT_type, cu);
5750 if (attr == NULL)
5751 {
5752 complaint (&symfile_complaints,
5753 _("template parameter missing DW_AT_type"));
5754 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5755 continue;
5756 }
5757 type = die_type (child, cu);
5758
5759 if (child->tag == DW_TAG_template_type_param)
5760 {
5761 c_print_type (type, "", buf, -1, 0);
5762 continue;
5763 }
5764
5765 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5766 if (attr == NULL)
5767 {
5768 complaint (&symfile_complaints,
5769 _("template parameter missing "
5770 "DW_AT_const_value"));
5771 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5772 continue;
5773 }
5774
5775 dwarf2_const_value_attr (attr, type, name,
5776 &cu->comp_unit_obstack, cu,
5777 &value, &bytes, &baton);
5778
5779 if (TYPE_NOSIGN (type))
5780 /* GDB prints characters as NUMBER 'CHAR'. If that's
5781 changed, this can use value_print instead. */
5782 c_printchar (value, type, buf);
5783 else
5784 {
5785 struct value_print_options opts;
5786
5787 if (baton != NULL)
5788 v = dwarf2_evaluate_loc_desc (type, NULL,
5789 baton->data,
5790 baton->size,
5791 baton->per_cu);
5792 else if (bytes != NULL)
5793 {
5794 v = allocate_value (type);
5795 memcpy (value_contents_writeable (v), bytes,
5796 TYPE_LENGTH (type));
5797 }
5798 else
5799 v = value_from_longest (type, value);
5800
5801 /* Specify decimal so that we do not depend on
5802 the radix. */
5803 get_formatted_print_options (&opts, 'd');
5804 opts.raw = 1;
5805 value_print (v, buf, &opts);
5806 release_value (v);
5807 value_free (v);
5808 }
5809 }
5810
5811 die->building_fullname = 0;
5812
5813 if (!first)
5814 {
5815 /* Close the argument list, with a space if necessary
5816 (nested templates). */
5817 char last_char = '\0';
5818 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5819 if (last_char == '>')
5820 fputs_unfiltered (" >", buf);
5821 else
5822 fputs_unfiltered (">", buf);
5823 }
5824 }
5825
5826 /* For Java and C++ methods, append formal parameter type
5827 information, if PHYSNAME. */
5828
5829 if (physname && die->tag == DW_TAG_subprogram
5830 && (cu->language == language_cplus
5831 || cu->language == language_java))
5832 {
5833 struct type *type = read_type_die (die, cu);
5834
5835 c_type_print_args (type, buf, 1, cu->language);
5836
5837 if (cu->language == language_java)
5838 {
5839 /* For java, we must append the return type to method
5840 names. */
5841 if (die->tag == DW_TAG_subprogram)
5842 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5843 0, 0);
5844 }
5845 else if (cu->language == language_cplus)
5846 {
5847 /* Assume that an artificial first parameter is
5848 "this", but do not crash if it is not. RealView
5849 marks unnamed (and thus unused) parameters as
5850 artificial; there is no way to differentiate
5851 the two cases. */
5852 if (TYPE_NFIELDS (type) > 0
5853 && TYPE_FIELD_ARTIFICIAL (type, 0)
5854 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5855 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5856 0))))
5857 fputs_unfiltered (" const", buf);
5858 }
5859 }
5860
5861 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5862 &length);
5863 ui_file_delete (buf);
5864
5865 if (cu->language == language_cplus)
5866 {
5867 char *cname
5868 = dwarf2_canonicalize_name (name, cu,
5869 &objfile->objfile_obstack);
5870
5871 if (cname != NULL)
5872 name = cname;
5873 }
5874 }
5875 }
5876
5877 return name;
5878 }
5879
5880 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5881 If scope qualifiers are appropriate they will be added. The result
5882 will be allocated on the objfile_obstack, or NULL if the DIE does
5883 not have a name. NAME may either be from a previous call to
5884 dwarf2_name or NULL.
5885
5886 The output string will be canonicalized (if C++/Java). */
5887
5888 static const char *
5889 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5890 {
5891 return dwarf2_compute_name (name, die, cu, 0);
5892 }
5893
5894 /* Construct a physname for the given DIE in CU. NAME may either be
5895 from a previous call to dwarf2_name or NULL. The result will be
5896 allocated on the objfile_objstack or NULL if the DIE does not have a
5897 name.
5898
5899 The output string will be canonicalized (if C++/Java). */
5900
5901 static const char *
5902 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5903 {
5904 struct objfile *objfile = cu->objfile;
5905 struct attribute *attr;
5906 const char *retval, *mangled = NULL, *canon = NULL;
5907 struct cleanup *back_to;
5908 int need_copy = 1;
5909
5910 /* In this case dwarf2_compute_name is just a shortcut not building anything
5911 on its own. */
5912 if (!die_needs_namespace (die, cu))
5913 return dwarf2_compute_name (name, die, cu, 1);
5914
5915 back_to = make_cleanup (null_cleanup, NULL);
5916
5917 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5918 if (!attr)
5919 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5920
5921 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5922 has computed. */
5923 if (attr && DW_STRING (attr))
5924 {
5925 char *demangled;
5926
5927 mangled = DW_STRING (attr);
5928
5929 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5930 type. It is easier for GDB users to search for such functions as
5931 `name(params)' than `long name(params)'. In such case the minimal
5932 symbol names do not match the full symbol names but for template
5933 functions there is never a need to look up their definition from their
5934 declaration so the only disadvantage remains the minimal symbol
5935 variant `long name(params)' does not have the proper inferior type.
5936 */
5937
5938 if (cu->language == language_go)
5939 {
5940 /* This is a lie, but we already lie to the caller new_symbol_full.
5941 new_symbol_full assumes we return the mangled name.
5942 This just undoes that lie until things are cleaned up. */
5943 demangled = NULL;
5944 }
5945 else
5946 {
5947 demangled = cplus_demangle (mangled,
5948 (DMGL_PARAMS | DMGL_ANSI
5949 | (cu->language == language_java
5950 ? DMGL_JAVA | DMGL_RET_POSTFIX
5951 : DMGL_RET_DROP)));
5952 }
5953 if (demangled)
5954 {
5955 make_cleanup (xfree, demangled);
5956 canon = demangled;
5957 }
5958 else
5959 {
5960 canon = mangled;
5961 need_copy = 0;
5962 }
5963 }
5964
5965 if (canon == NULL || check_physname)
5966 {
5967 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5968
5969 if (canon != NULL && strcmp (physname, canon) != 0)
5970 {
5971 /* It may not mean a bug in GDB. The compiler could also
5972 compute DW_AT_linkage_name incorrectly. But in such case
5973 GDB would need to be bug-to-bug compatible. */
5974
5975 complaint (&symfile_complaints,
5976 _("Computed physname <%s> does not match demangled <%s> "
5977 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5978 physname, canon, mangled, die->offset.sect_off, objfile->name);
5979
5980 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5981 is available here - over computed PHYSNAME. It is safer
5982 against both buggy GDB and buggy compilers. */
5983
5984 retval = canon;
5985 }
5986 else
5987 {
5988 retval = physname;
5989 need_copy = 0;
5990 }
5991 }
5992 else
5993 retval = canon;
5994
5995 if (need_copy)
5996 retval = obsavestring (retval, strlen (retval),
5997 &objfile->objfile_obstack);
5998
5999 do_cleanups (back_to);
6000 return retval;
6001 }
6002
6003 /* Read the import statement specified by the given die and record it. */
6004
6005 static void
6006 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6007 {
6008 struct objfile *objfile = cu->objfile;
6009 struct attribute *import_attr;
6010 struct die_info *imported_die, *child_die;
6011 struct dwarf2_cu *imported_cu;
6012 const char *imported_name;
6013 const char *imported_name_prefix;
6014 const char *canonical_name;
6015 const char *import_alias;
6016 const char *imported_declaration = NULL;
6017 const char *import_prefix;
6018 VEC (const_char_ptr) *excludes = NULL;
6019 struct cleanup *cleanups;
6020
6021 char *temp;
6022
6023 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6024 if (import_attr == NULL)
6025 {
6026 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6027 dwarf_tag_name (die->tag));
6028 return;
6029 }
6030
6031 imported_cu = cu;
6032 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6033 imported_name = dwarf2_name (imported_die, imported_cu);
6034 if (imported_name == NULL)
6035 {
6036 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6037
6038 The import in the following code:
6039 namespace A
6040 {
6041 typedef int B;
6042 }
6043
6044 int main ()
6045 {
6046 using A::B;
6047 B b;
6048 return b;
6049 }
6050
6051 ...
6052 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6053 <52> DW_AT_decl_file : 1
6054 <53> DW_AT_decl_line : 6
6055 <54> DW_AT_import : <0x75>
6056 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6057 <59> DW_AT_name : B
6058 <5b> DW_AT_decl_file : 1
6059 <5c> DW_AT_decl_line : 2
6060 <5d> DW_AT_type : <0x6e>
6061 ...
6062 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6063 <76> DW_AT_byte_size : 4
6064 <77> DW_AT_encoding : 5 (signed)
6065
6066 imports the wrong die ( 0x75 instead of 0x58 ).
6067 This case will be ignored until the gcc bug is fixed. */
6068 return;
6069 }
6070
6071 /* Figure out the local name after import. */
6072 import_alias = dwarf2_name (die, cu);
6073
6074 /* Figure out where the statement is being imported to. */
6075 import_prefix = determine_prefix (die, cu);
6076
6077 /* Figure out what the scope of the imported die is and prepend it
6078 to the name of the imported die. */
6079 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6080
6081 if (imported_die->tag != DW_TAG_namespace
6082 && imported_die->tag != DW_TAG_module)
6083 {
6084 imported_declaration = imported_name;
6085 canonical_name = imported_name_prefix;
6086 }
6087 else if (strlen (imported_name_prefix) > 0)
6088 {
6089 temp = alloca (strlen (imported_name_prefix)
6090 + 2 + strlen (imported_name) + 1);
6091 strcpy (temp, imported_name_prefix);
6092 strcat (temp, "::");
6093 strcat (temp, imported_name);
6094 canonical_name = temp;
6095 }
6096 else
6097 canonical_name = imported_name;
6098
6099 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6100
6101 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6102 for (child_die = die->child; child_die && child_die->tag;
6103 child_die = sibling_die (child_die))
6104 {
6105 /* DWARF-4: A Fortran use statement with a “rename list” may be
6106 represented by an imported module entry with an import attribute
6107 referring to the module and owned entries corresponding to those
6108 entities that are renamed as part of being imported. */
6109
6110 if (child_die->tag != DW_TAG_imported_declaration)
6111 {
6112 complaint (&symfile_complaints,
6113 _("child DW_TAG_imported_declaration expected "
6114 "- DIE at 0x%x [in module %s]"),
6115 child_die->offset.sect_off, objfile->name);
6116 continue;
6117 }
6118
6119 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6120 if (import_attr == NULL)
6121 {
6122 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6123 dwarf_tag_name (child_die->tag));
6124 continue;
6125 }
6126
6127 imported_cu = cu;
6128 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6129 &imported_cu);
6130 imported_name = dwarf2_name (imported_die, imported_cu);
6131 if (imported_name == NULL)
6132 {
6133 complaint (&symfile_complaints,
6134 _("child DW_TAG_imported_declaration has unknown "
6135 "imported name - DIE at 0x%x [in module %s]"),
6136 child_die->offset.sect_off, objfile->name);
6137 continue;
6138 }
6139
6140 VEC_safe_push (const_char_ptr, excludes, imported_name);
6141
6142 process_die (child_die, cu);
6143 }
6144
6145 cp_add_using_directive (import_prefix,
6146 canonical_name,
6147 import_alias,
6148 imported_declaration,
6149 excludes,
6150 &objfile->objfile_obstack);
6151
6152 do_cleanups (cleanups);
6153 }
6154
6155 /* Cleanup function for read_file_scope. */
6156
6157 static void
6158 free_cu_line_header (void *arg)
6159 {
6160 struct dwarf2_cu *cu = arg;
6161
6162 free_line_header (cu->line_header);
6163 cu->line_header = NULL;
6164 }
6165
6166 static void
6167 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6168 char **name, char **comp_dir)
6169 {
6170 struct attribute *attr;
6171
6172 *name = NULL;
6173 *comp_dir = NULL;
6174
6175 /* Find the filename. Do not use dwarf2_name here, since the filename
6176 is not a source language identifier. */
6177 attr = dwarf2_attr (die, DW_AT_name, cu);
6178 if (attr)
6179 {
6180 *name = DW_STRING (attr);
6181 }
6182
6183 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6184 if (attr)
6185 *comp_dir = DW_STRING (attr);
6186 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6187 {
6188 *comp_dir = ldirname (*name);
6189 if (*comp_dir != NULL)
6190 make_cleanup (xfree, *comp_dir);
6191 }
6192 if (*comp_dir != NULL)
6193 {
6194 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6195 directory, get rid of it. */
6196 char *cp = strchr (*comp_dir, ':');
6197
6198 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6199 *comp_dir = cp + 1;
6200 }
6201
6202 if (*name == NULL)
6203 *name = "<unknown>";
6204 }
6205
6206 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6207 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6208 COMP_DIR is the compilation directory.
6209 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6210
6211 static void
6212 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6213 const char *comp_dir, int want_line_info)
6214 {
6215 struct attribute *attr;
6216
6217 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6218 if (attr)
6219 {
6220 unsigned int line_offset = DW_UNSND (attr);
6221 struct line_header *line_header
6222 = dwarf_decode_line_header (line_offset, cu);
6223
6224 if (line_header)
6225 {
6226 cu->line_header = line_header;
6227 make_cleanup (free_cu_line_header, cu);
6228 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6229 }
6230 }
6231 }
6232
6233 /* Process DW_TAG_compile_unit. */
6234
6235 static void
6236 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6237 {
6238 struct objfile *objfile = dwarf2_per_objfile->objfile;
6239 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6240 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6241 CORE_ADDR highpc = ((CORE_ADDR) 0);
6242 struct attribute *attr;
6243 char *name = NULL;
6244 char *comp_dir = NULL;
6245 struct die_info *child_die;
6246 bfd *abfd = objfile->obfd;
6247 CORE_ADDR baseaddr;
6248
6249 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6250
6251 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6252
6253 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6254 from finish_block. */
6255 if (lowpc == ((CORE_ADDR) -1))
6256 lowpc = highpc;
6257 lowpc += baseaddr;
6258 highpc += baseaddr;
6259
6260 find_file_and_directory (die, cu, &name, &comp_dir);
6261
6262 prepare_one_comp_unit (cu, die);
6263
6264 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6265 standardised yet. As a workaround for the language detection we fall
6266 back to the DW_AT_producer string. */
6267 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6268 cu->language = language_opencl;
6269
6270 /* Similar hack for Go. */
6271 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6272 set_cu_language (DW_LANG_Go, cu);
6273
6274 /* We assume that we're processing GCC output. */
6275 processing_gcc_compilation = 2;
6276
6277 processing_has_namespace_info = 0;
6278
6279 start_symtab (name, comp_dir, lowpc);
6280 record_debugformat ("DWARF 2");
6281 record_producer (cu->producer);
6282
6283 /* Decode line number information if present. We do this before
6284 processing child DIEs, so that the line header table is available
6285 for DW_AT_decl_file. */
6286 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6287
6288 /* Process all dies in compilation unit. */
6289 if (die->child != NULL)
6290 {
6291 child_die = die->child;
6292 while (child_die && child_die->tag)
6293 {
6294 process_die (child_die, cu);
6295 child_die = sibling_die (child_die);
6296 }
6297 }
6298
6299 /* Decode macro information, if present. Dwarf 2 macro information
6300 refers to information in the line number info statement program
6301 header, so we can only read it if we've read the header
6302 successfully. */
6303 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6304 if (attr && cu->line_header)
6305 {
6306 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6307 complaint (&symfile_complaints,
6308 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6309
6310 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6311 comp_dir, abfd, cu,
6312 &dwarf2_per_objfile->macro, 1,
6313 ".debug_macro");
6314 }
6315 else
6316 {
6317 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6318 if (attr && cu->line_header)
6319 {
6320 unsigned int macro_offset = DW_UNSND (attr);
6321
6322 dwarf_decode_macros (cu->line_header, macro_offset,
6323 comp_dir, abfd, cu,
6324 &dwarf2_per_objfile->macinfo, 0,
6325 ".debug_macinfo");
6326 }
6327 }
6328
6329 do_cleanups (back_to);
6330 }
6331
6332 /* Process DW_TAG_type_unit.
6333 For TUs we want to skip the first top level sibling if it's not the
6334 actual type being defined by this TU. In this case the first top
6335 level sibling is there to provide context only. */
6336
6337 static void
6338 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6339 {
6340 struct objfile *objfile = cu->objfile;
6341 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6342 CORE_ADDR lowpc;
6343 struct attribute *attr;
6344 char *name = NULL;
6345 char *comp_dir = NULL;
6346 struct die_info *child_die;
6347 bfd *abfd = objfile->obfd;
6348
6349 /* start_symtab needs a low pc, but we don't really have one.
6350 Do what read_file_scope would do in the absence of such info. */
6351 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6352
6353 /* Find the filename. Do not use dwarf2_name here, since the filename
6354 is not a source language identifier. */
6355 attr = dwarf2_attr (die, DW_AT_name, cu);
6356 if (attr)
6357 name = DW_STRING (attr);
6358
6359 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6360 if (attr)
6361 comp_dir = DW_STRING (attr);
6362 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6363 {
6364 comp_dir = ldirname (name);
6365 if (comp_dir != NULL)
6366 make_cleanup (xfree, comp_dir);
6367 }
6368
6369 if (name == NULL)
6370 name = "<unknown>";
6371
6372 prepare_one_comp_unit (cu, die);
6373
6374 /* We assume that we're processing GCC output. */
6375 processing_gcc_compilation = 2;
6376
6377 processing_has_namespace_info = 0;
6378
6379 start_symtab (name, comp_dir, lowpc);
6380 record_debugformat ("DWARF 2");
6381 record_producer (cu->producer);
6382
6383 /* Decode line number information if present. We do this before
6384 processing child DIEs, so that the line header table is available
6385 for DW_AT_decl_file.
6386 We don't need the pc/line-number mapping for type units. */
6387 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6388
6389 /* Process the dies in the type unit. */
6390 if (die->child == NULL)
6391 {
6392 dump_die_for_error (die);
6393 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6394 bfd_get_filename (abfd));
6395 }
6396
6397 child_die = die->child;
6398
6399 while (child_die && child_die->tag)
6400 {
6401 process_die (child_die, cu);
6402
6403 child_die = sibling_die (child_die);
6404 }
6405
6406 do_cleanups (back_to);
6407 }
6408 \f
6409 /* DWO files. */
6410
6411 static hashval_t
6412 hash_dwo_file (const void *item)
6413 {
6414 const struct dwo_file *dwo_file = item;
6415
6416 return htab_hash_string (dwo_file->dwo_name);
6417 }
6418
6419 static int
6420 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6421 {
6422 const struct dwo_file *lhs = item_lhs;
6423 const struct dwo_file *rhs = item_rhs;
6424
6425 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6426 }
6427
6428 /* Allocate a hash table for DWO files. */
6429
6430 static htab_t
6431 allocate_dwo_file_hash_table (void)
6432 {
6433 struct objfile *objfile = dwarf2_per_objfile->objfile;
6434
6435 return htab_create_alloc_ex (41,
6436 hash_dwo_file,
6437 eq_dwo_file,
6438 NULL,
6439 &objfile->objfile_obstack,
6440 hashtab_obstack_allocate,
6441 dummy_obstack_deallocate);
6442 }
6443
6444 static hashval_t
6445 hash_dwo_unit (const void *item)
6446 {
6447 const struct dwo_unit *dwo_unit = item;
6448
6449 /* This drops the top 32 bits of the id, but is ok for a hash. */
6450 return dwo_unit->signature;
6451 }
6452
6453 static int
6454 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6455 {
6456 const struct dwo_unit *lhs = item_lhs;
6457 const struct dwo_unit *rhs = item_rhs;
6458
6459 /* The signature is assumed to be unique within the DWO file.
6460 So while object file CU dwo_id's always have the value zero,
6461 that's OK, assuming each object file DWO file has only one CU,
6462 and that's the rule for now. */
6463 return lhs->signature == rhs->signature;
6464 }
6465
6466 /* Allocate a hash table for DWO CUs,TUs.
6467 There is one of these tables for each of CUs,TUs for each DWO file. */
6468
6469 static htab_t
6470 allocate_dwo_unit_table (struct objfile *objfile)
6471 {
6472 /* Start out with a pretty small number.
6473 Generally DWO files contain only one CU and maybe some TUs. */
6474 return htab_create_alloc_ex (3,
6475 hash_dwo_unit,
6476 eq_dwo_unit,
6477 NULL,
6478 &objfile->objfile_obstack,
6479 hashtab_obstack_allocate,
6480 dummy_obstack_deallocate);
6481 }
6482
6483 /* This function is mapped across the sections and remembers the offset and
6484 size of each of the DWO debugging sections we are interested in. */
6485
6486 static void
6487 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6488 {
6489 struct dwo_file *dwo_file = dwo_file_ptr;
6490 const struct dwo_section_names *names = &dwo_section_names;
6491
6492 if (section_is_p (sectp->name, &names->abbrev_dwo))
6493 {
6494 dwo_file->sections.abbrev.asection = sectp;
6495 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6496 }
6497 else if (section_is_p (sectp->name, &names->info_dwo))
6498 {
6499 dwo_file->sections.info.asection = sectp;
6500 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6501 }
6502 else if (section_is_p (sectp->name, &names->line_dwo))
6503 {
6504 dwo_file->sections.line.asection = sectp;
6505 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6506 }
6507 else if (section_is_p (sectp->name, &names->loc_dwo))
6508 {
6509 dwo_file->sections.loc.asection = sectp;
6510 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6511 }
6512 else if (section_is_p (sectp->name, &names->str_dwo))
6513 {
6514 dwo_file->sections.str.asection = sectp;
6515 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6516 }
6517 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6518 {
6519 dwo_file->sections.str_offsets.asection = sectp;
6520 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6521 }
6522 else if (section_is_p (sectp->name, &names->types_dwo))
6523 {
6524 struct dwarf2_section_info type_section;
6525
6526 memset (&type_section, 0, sizeof (type_section));
6527 type_section.asection = sectp;
6528 type_section.size = bfd_get_section_size (sectp);
6529 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6530 &type_section);
6531 }
6532 }
6533
6534 /* Structure used to pass data to create_debug_info_hash_table_reader. */
6535
6536 struct create_dwo_info_table_data
6537 {
6538 struct dwo_file *dwo_file;
6539 htab_t cu_htab;
6540 };
6541
6542 /* die_reader_func for create_debug_info_hash_table. */
6543
6544 static void
6545 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6546 gdb_byte *info_ptr,
6547 struct die_info *comp_unit_die,
6548 int has_children,
6549 void *datap)
6550 {
6551 struct dwarf2_cu *cu = reader->cu;
6552 struct objfile *objfile = dwarf2_per_objfile->objfile;
6553 sect_offset offset = cu->per_cu->offset;
6554 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6555 struct create_dwo_info_table_data *data = datap;
6556 struct dwo_file *dwo_file = data->dwo_file;
6557 htab_t cu_htab = data->cu_htab;
6558 void **slot;
6559 struct attribute *attr;
6560 struct dwo_unit *dwo_unit;
6561
6562 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6563 if (attr == NULL)
6564 {
6565 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6566 " its dwo_id [in module %s]"),
6567 offset.sect_off, dwo_file->dwo_name);
6568 return;
6569 }
6570
6571 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6572 dwo_unit->dwo_file = dwo_file;
6573 dwo_unit->signature = DW_UNSND (attr);
6574 dwo_unit->info_or_types_section = section;
6575 dwo_unit->offset = offset;
6576 dwo_unit->length = cu->per_cu->length;
6577
6578 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6579 gdb_assert (slot != NULL);
6580 if (*slot != NULL)
6581 {
6582 const struct dwo_unit *dup_dwo_unit = *slot;
6583
6584 complaint (&symfile_complaints,
6585 _("debug entry at offset 0x%x is duplicate to the entry at"
6586 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6587 offset.sect_off, dup_dwo_unit->offset.sect_off,
6588 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6589 dwo_file->dwo_name);
6590 }
6591 else
6592 *slot = dwo_unit;
6593
6594 if (dwarf2_die_debug)
6595 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6596 offset.sect_off,
6597 phex (dwo_unit->signature,
6598 sizeof (dwo_unit->signature)));
6599 }
6600
6601 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6602
6603 static htab_t
6604 create_debug_info_hash_table (struct dwo_file *dwo_file)
6605 {
6606 struct objfile *objfile = dwarf2_per_objfile->objfile;
6607 struct dwarf2_section_info *section = &dwo_file->sections.info;
6608 bfd *abfd;
6609 htab_t cu_htab;
6610 gdb_byte *info_ptr, *end_ptr;
6611 struct create_dwo_info_table_data create_dwo_info_table_data;
6612
6613 dwarf2_read_section (objfile, section);
6614 info_ptr = section->buffer;
6615
6616 if (info_ptr == NULL)
6617 return NULL;
6618
6619 /* We can't set abfd until now because the section may be empty or
6620 not present, in which case section->asection will be NULL. */
6621 abfd = section->asection->owner;
6622
6623 if (dwarf2_die_debug)
6624 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6625 bfd_get_filename (abfd));
6626
6627 cu_htab = allocate_dwo_unit_table (objfile);
6628
6629 create_dwo_info_table_data.dwo_file = dwo_file;
6630 create_dwo_info_table_data.cu_htab = cu_htab;
6631
6632 end_ptr = info_ptr + section->size;
6633 while (info_ptr < end_ptr)
6634 {
6635 struct dwarf2_per_cu_data per_cu;
6636
6637 memset (&per_cu, 0, sizeof (per_cu));
6638 per_cu.objfile = objfile;
6639 per_cu.is_debug_types = 0;
6640 per_cu.offset.sect_off = info_ptr - section->buffer;
6641 per_cu.info_or_types_section = section;
6642
6643 init_cutu_and_read_dies_no_follow (&per_cu,
6644 &dwo_file->sections.abbrev,
6645 dwo_file,
6646 create_debug_info_hash_table_reader,
6647 &create_dwo_info_table_data);
6648
6649 info_ptr += per_cu.length;
6650 }
6651
6652 return cu_htab;
6653 }
6654
6655 /* Subroutine of open_dwo_file to simplify it.
6656 Open the file specified by FILE_NAME and hand it off to BFD for
6657 preliminary analysis. Return a newly initialized bfd *, which
6658 includes a canonicalized copy of FILE_NAME.
6659 In case of trouble, return NULL.
6660 NOTE: This function is derived from symfile_bfd_open. */
6661
6662 static bfd *
6663 try_open_dwo_file (const char *file_name)
6664 {
6665 bfd *sym_bfd;
6666 int desc;
6667 char *absolute_name;
6668 char *name;
6669
6670 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6671 O_RDONLY | O_BINARY, &absolute_name);
6672 if (desc < 0)
6673 return NULL;
6674
6675 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6676 if (!sym_bfd)
6677 {
6678 close (desc);
6679 xfree (absolute_name);
6680 return NULL;
6681 }
6682 bfd_set_cacheable (sym_bfd, 1);
6683
6684 if (!bfd_check_format (sym_bfd, bfd_object))
6685 {
6686 bfd_close (sym_bfd); /* This also closes desc. */
6687 xfree (absolute_name);
6688 return NULL;
6689 }
6690
6691 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6692 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6693
6694 return sym_bfd;
6695 }
6696
6697 /* Try to open DWO file DWO_NAME.
6698 COMP_DIR is the DW_AT_comp_dir attribute.
6699 The result is the bfd handle of the file.
6700 If there is a problem finding or opening the file, return NULL.
6701 Upon success, the canonicalized path of the file is stored in the bfd,
6702 same as symfile_bfd_open. */
6703
6704 static bfd *
6705 open_dwo_file (const char *dwo_name, const char *comp_dir)
6706 {
6707 bfd *abfd;
6708 char *path_to_try, *debug_dir;
6709
6710 if (IS_ABSOLUTE_PATH (dwo_name))
6711 return try_open_dwo_file (dwo_name);
6712
6713 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6714
6715 if (comp_dir != NULL)
6716 {
6717 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6718
6719 /* NOTE: If comp_dir is a relative path, this will also try the
6720 search path, which seems useful. */
6721 abfd = try_open_dwo_file (path_to_try);
6722 xfree (path_to_try);
6723 if (abfd != NULL)
6724 return abfd;
6725 }
6726
6727 /* That didn't work, try debug-file-directory, which, despite its name,
6728 is a list of paths. */
6729
6730 if (*debug_file_directory == '\0')
6731 return NULL;
6732
6733 return try_open_dwo_file (dwo_name);
6734 }
6735
6736 /* Initialize the use of the DWO file specified by DWO_NAME. */
6737
6738 static struct dwo_file *
6739 init_dwo_file (const char *dwo_name, const char *comp_dir)
6740 {
6741 struct objfile *objfile = dwarf2_per_objfile->objfile;
6742 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6743 struct dwo_file);
6744 bfd *abfd;
6745 struct cleanup *cleanups;
6746
6747 if (dwarf2_die_debug)
6748 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
6749
6750 abfd = open_dwo_file (dwo_name, comp_dir);
6751 if (abfd == NULL)
6752 return NULL;
6753 dwo_file->dwo_name = dwo_name;
6754 dwo_file->dwo_bfd = abfd;
6755
6756 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
6757
6758 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
6759
6760 dwo_file->cus = create_debug_info_hash_table (dwo_file);
6761
6762 dwo_file->tus = create_debug_types_hash_table (dwo_file,
6763 dwo_file->sections.types);
6764
6765 discard_cleanups (cleanups);
6766
6767 return dwo_file;
6768 }
6769
6770 /* Lookup DWO file DWO_NAME. */
6771
6772 static struct dwo_file *
6773 lookup_dwo_file (char *dwo_name, const char *comp_dir)
6774 {
6775 struct dwo_file *dwo_file;
6776 struct dwo_file find_entry;
6777 void **slot;
6778
6779 if (dwarf2_per_objfile->dwo_files == NULL)
6780 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
6781
6782 /* Have we already seen this DWO file? */
6783 find_entry.dwo_name = dwo_name;
6784 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
6785
6786 /* If not, read it in and build a table of the DWOs it contains. */
6787 if (*slot == NULL)
6788 *slot = init_dwo_file (dwo_name, comp_dir);
6789
6790 /* NOTE: This will be NULL if unable to open the file. */
6791 dwo_file = *slot;
6792
6793 return dwo_file;
6794 }
6795
6796 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
6797 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
6798 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
6799 nomenclature as TUs).
6800 The result is the DWO CU or NULL if we didn't find it
6801 (dwo_id mismatch or couldn't find the DWO file). */
6802
6803 static struct dwo_unit *
6804 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
6805 char *dwo_name, const char *comp_dir,
6806 ULONGEST signature)
6807 {
6808 struct objfile *objfile = dwarf2_per_objfile->objfile;
6809 struct dwo_file *dwo_file;
6810
6811 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
6812 if (dwo_file == NULL)
6813 return NULL;
6814
6815 /* Look up the DWO using its signature(dwo_id). */
6816
6817 if (dwo_file->cus != NULL)
6818 {
6819 struct dwo_unit find_dwo_cu, *dwo_cu;
6820
6821 find_dwo_cu.signature = signature;
6822 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
6823
6824 if (dwo_cu != NULL)
6825 return dwo_cu;
6826 }
6827
6828 /* We didn't find it. This must mean a dwo_id mismatch. */
6829
6830 complaint (&symfile_complaints,
6831 _("Could not find DWO CU referenced by CU at offset 0x%x"
6832 " [in module %s]"),
6833 this_cu->offset.sect_off, objfile->name);
6834 return NULL;
6835 }
6836
6837 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
6838 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
6839 The result is the DWO CU or NULL if we didn't find it
6840 (dwo_id mismatch or couldn't find the DWO file). */
6841
6842 static struct dwo_unit *
6843 lookup_dwo_type_unit (struct signatured_type *this_tu,
6844 char *dwo_name, const char *comp_dir)
6845 {
6846 struct objfile *objfile = dwarf2_per_objfile->objfile;
6847 struct dwo_file *dwo_file;
6848 struct dwo_unit find_dwo_tu, *dwo_tu;
6849
6850 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
6851 if (dwo_file == NULL)
6852 return NULL;
6853
6854 /* Look up the DWO using its signature(dwo_id). */
6855
6856 if (dwo_file->tus != NULL)
6857 {
6858 struct dwo_unit find_dwo_tu, *dwo_tu;
6859
6860 find_dwo_tu.signature = this_tu->signature;
6861 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
6862
6863 if (dwo_tu != NULL)
6864 return dwo_tu;
6865 }
6866
6867 /* We didn't find it. This must mean a dwo_id mismatch. */
6868
6869 complaint (&symfile_complaints,
6870 _("Could not find DWO TU referenced by TU at offset 0x%x"
6871 " [in module %s]"),
6872 this_tu->per_cu.offset.sect_off, objfile->name);
6873 return NULL;
6874 }
6875
6876 /* Free all resources associated with DWO_FILE.
6877 Close the DWO file and munmap the sections.
6878 All memory should be on the objfile obstack. */
6879
6880 static void
6881 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
6882 {
6883 int ix;
6884 struct dwarf2_section_info *section;
6885
6886 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
6887 bfd_close (dwo_file->dwo_bfd);
6888
6889 munmap_section_buffer (&dwo_file->sections.abbrev);
6890 munmap_section_buffer (&dwo_file->sections.info);
6891 munmap_section_buffer (&dwo_file->sections.line);
6892 munmap_section_buffer (&dwo_file->sections.loc);
6893 munmap_section_buffer (&dwo_file->sections.str);
6894 munmap_section_buffer (&dwo_file->sections.str_offsets);
6895
6896 for (ix = 0;
6897 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
6898 ix, section);
6899 ++ix)
6900 munmap_section_buffer (section);
6901
6902 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
6903 }
6904
6905 /* Wrapper for free_dwo_file for use in cleanups. */
6906
6907 static void
6908 free_dwo_file_cleanup (void *arg)
6909 {
6910 struct dwo_file *dwo_file = (struct dwo_file *) arg;
6911 struct objfile *objfile = dwarf2_per_objfile->objfile;
6912
6913 free_dwo_file (dwo_file, objfile);
6914 }
6915
6916 /* Traversal function for free_dwo_files. */
6917
6918 static int
6919 free_dwo_file_from_slot (void **slot, void *info)
6920 {
6921 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6922 struct objfile *objfile = (struct objfile *) info;
6923
6924 free_dwo_file (dwo_file, objfile);
6925
6926 return 1;
6927 }
6928
6929 /* Free all resources associated with DWO_FILES. */
6930
6931 static void
6932 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
6933 {
6934 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
6935 }
6936 \f
6937 /* Read in various DIEs. */
6938
6939 /* qsort helper for inherit_abstract_dies. */
6940
6941 static int
6942 unsigned_int_compar (const void *ap, const void *bp)
6943 {
6944 unsigned int a = *(unsigned int *) ap;
6945 unsigned int b = *(unsigned int *) bp;
6946
6947 return (a > b) - (b > a);
6948 }
6949
6950 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
6951 Inherit only the children of the DW_AT_abstract_origin DIE not being
6952 already referenced by DW_AT_abstract_origin from the children of the
6953 current DIE. */
6954
6955 static void
6956 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
6957 {
6958 struct die_info *child_die;
6959 unsigned die_children_count;
6960 /* CU offsets which were referenced by children of the current DIE. */
6961 sect_offset *offsets;
6962 sect_offset *offsets_end, *offsetp;
6963 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
6964 struct die_info *origin_die;
6965 /* Iterator of the ORIGIN_DIE children. */
6966 struct die_info *origin_child_die;
6967 struct cleanup *cleanups;
6968 struct attribute *attr;
6969 struct dwarf2_cu *origin_cu;
6970 struct pending **origin_previous_list_in_scope;
6971
6972 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6973 if (!attr)
6974 return;
6975
6976 /* Note that following die references may follow to a die in a
6977 different cu. */
6978
6979 origin_cu = cu;
6980 origin_die = follow_die_ref (die, attr, &origin_cu);
6981
6982 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
6983 symbols in. */
6984 origin_previous_list_in_scope = origin_cu->list_in_scope;
6985 origin_cu->list_in_scope = cu->list_in_scope;
6986
6987 if (die->tag != origin_die->tag
6988 && !(die->tag == DW_TAG_inlined_subroutine
6989 && origin_die->tag == DW_TAG_subprogram))
6990 complaint (&symfile_complaints,
6991 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
6992 die->offset.sect_off, origin_die->offset.sect_off);
6993
6994 child_die = die->child;
6995 die_children_count = 0;
6996 while (child_die && child_die->tag)
6997 {
6998 child_die = sibling_die (child_die);
6999 die_children_count++;
7000 }
7001 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7002 cleanups = make_cleanup (xfree, offsets);
7003
7004 offsets_end = offsets;
7005 child_die = die->child;
7006 while (child_die && child_die->tag)
7007 {
7008 /* For each CHILD_DIE, find the corresponding child of
7009 ORIGIN_DIE. If there is more than one layer of
7010 DW_AT_abstract_origin, follow them all; there shouldn't be,
7011 but GCC versions at least through 4.4 generate this (GCC PR
7012 40573). */
7013 struct die_info *child_origin_die = child_die;
7014 struct dwarf2_cu *child_origin_cu = cu;
7015
7016 while (1)
7017 {
7018 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7019 child_origin_cu);
7020 if (attr == NULL)
7021 break;
7022 child_origin_die = follow_die_ref (child_origin_die, attr,
7023 &child_origin_cu);
7024 }
7025
7026 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7027 counterpart may exist. */
7028 if (child_origin_die != child_die)
7029 {
7030 if (child_die->tag != child_origin_die->tag
7031 && !(child_die->tag == DW_TAG_inlined_subroutine
7032 && child_origin_die->tag == DW_TAG_subprogram))
7033 complaint (&symfile_complaints,
7034 _("Child DIE 0x%x and its abstract origin 0x%x have "
7035 "different tags"), child_die->offset.sect_off,
7036 child_origin_die->offset.sect_off);
7037 if (child_origin_die->parent != origin_die)
7038 complaint (&symfile_complaints,
7039 _("Child DIE 0x%x and its abstract origin 0x%x have "
7040 "different parents"), child_die->offset.sect_off,
7041 child_origin_die->offset.sect_off);
7042 else
7043 *offsets_end++ = child_origin_die->offset;
7044 }
7045 child_die = sibling_die (child_die);
7046 }
7047 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7048 unsigned_int_compar);
7049 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7050 if (offsetp[-1].sect_off == offsetp->sect_off)
7051 complaint (&symfile_complaints,
7052 _("Multiple children of DIE 0x%x refer "
7053 "to DIE 0x%x as their abstract origin"),
7054 die->offset.sect_off, offsetp->sect_off);
7055
7056 offsetp = offsets;
7057 origin_child_die = origin_die->child;
7058 while (origin_child_die && origin_child_die->tag)
7059 {
7060 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7061 while (offsetp < offsets_end
7062 && offsetp->sect_off < origin_child_die->offset.sect_off)
7063 offsetp++;
7064 if (offsetp >= offsets_end
7065 || offsetp->sect_off > origin_child_die->offset.sect_off)
7066 {
7067 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7068 process_die (origin_child_die, origin_cu);
7069 }
7070 origin_child_die = sibling_die (origin_child_die);
7071 }
7072 origin_cu->list_in_scope = origin_previous_list_in_scope;
7073
7074 do_cleanups (cleanups);
7075 }
7076
7077 static void
7078 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7079 {
7080 struct objfile *objfile = cu->objfile;
7081 struct context_stack *new;
7082 CORE_ADDR lowpc;
7083 CORE_ADDR highpc;
7084 struct die_info *child_die;
7085 struct attribute *attr, *call_line, *call_file;
7086 char *name;
7087 CORE_ADDR baseaddr;
7088 struct block *block;
7089 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7090 VEC (symbolp) *template_args = NULL;
7091 struct template_symbol *templ_func = NULL;
7092
7093 if (inlined_func)
7094 {
7095 /* If we do not have call site information, we can't show the
7096 caller of this inlined function. That's too confusing, so
7097 only use the scope for local variables. */
7098 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7099 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7100 if (call_line == NULL || call_file == NULL)
7101 {
7102 read_lexical_block_scope (die, cu);
7103 return;
7104 }
7105 }
7106
7107 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7108
7109 name = dwarf2_name (die, cu);
7110
7111 /* Ignore functions with missing or empty names. These are actually
7112 illegal according to the DWARF standard. */
7113 if (name == NULL)
7114 {
7115 complaint (&symfile_complaints,
7116 _("missing name for subprogram DIE at %d"),
7117 die->offset.sect_off);
7118 return;
7119 }
7120
7121 /* Ignore functions with missing or invalid low and high pc attributes. */
7122 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7123 {
7124 attr = dwarf2_attr (die, DW_AT_external, cu);
7125 if (!attr || !DW_UNSND (attr))
7126 complaint (&symfile_complaints,
7127 _("cannot get low and high bounds "
7128 "for subprogram DIE at %d"),
7129 die->offset.sect_off);
7130 return;
7131 }
7132
7133 lowpc += baseaddr;
7134 highpc += baseaddr;
7135
7136 /* If we have any template arguments, then we must allocate a
7137 different sort of symbol. */
7138 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7139 {
7140 if (child_die->tag == DW_TAG_template_type_param
7141 || child_die->tag == DW_TAG_template_value_param)
7142 {
7143 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7144 struct template_symbol);
7145 templ_func->base.is_cplus_template_function = 1;
7146 break;
7147 }
7148 }
7149
7150 new = push_context (0, lowpc);
7151 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7152 (struct symbol *) templ_func);
7153
7154 /* If there is a location expression for DW_AT_frame_base, record
7155 it. */
7156 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7157 if (attr)
7158 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7159 expression is being recorded directly in the function's symbol
7160 and not in a separate frame-base object. I guess this hack is
7161 to avoid adding some sort of frame-base adjunct/annex to the
7162 function's symbol :-(. The problem with doing this is that it
7163 results in a function symbol with a location expression that
7164 has nothing to do with the location of the function, ouch! The
7165 relationship should be: a function's symbol has-a frame base; a
7166 frame-base has-a location expression. */
7167 dwarf2_symbol_mark_computed (attr, new->name, cu);
7168
7169 cu->list_in_scope = &local_symbols;
7170
7171 if (die->child != NULL)
7172 {
7173 child_die = die->child;
7174 while (child_die && child_die->tag)
7175 {
7176 if (child_die->tag == DW_TAG_template_type_param
7177 || child_die->tag == DW_TAG_template_value_param)
7178 {
7179 struct symbol *arg = new_symbol (child_die, NULL, cu);
7180
7181 if (arg != NULL)
7182 VEC_safe_push (symbolp, template_args, arg);
7183 }
7184 else
7185 process_die (child_die, cu);
7186 child_die = sibling_die (child_die);
7187 }
7188 }
7189
7190 inherit_abstract_dies (die, cu);
7191
7192 /* If we have a DW_AT_specification, we might need to import using
7193 directives from the context of the specification DIE. See the
7194 comment in determine_prefix. */
7195 if (cu->language == language_cplus
7196 && dwarf2_attr (die, DW_AT_specification, cu))
7197 {
7198 struct dwarf2_cu *spec_cu = cu;
7199 struct die_info *spec_die = die_specification (die, &spec_cu);
7200
7201 while (spec_die)
7202 {
7203 child_die = spec_die->child;
7204 while (child_die && child_die->tag)
7205 {
7206 if (child_die->tag == DW_TAG_imported_module)
7207 process_die (child_die, spec_cu);
7208 child_die = sibling_die (child_die);
7209 }
7210
7211 /* In some cases, GCC generates specification DIEs that
7212 themselves contain DW_AT_specification attributes. */
7213 spec_die = die_specification (spec_die, &spec_cu);
7214 }
7215 }
7216
7217 new = pop_context ();
7218 /* Make a block for the local symbols within. */
7219 block = finish_block (new->name, &local_symbols, new->old_blocks,
7220 lowpc, highpc, objfile);
7221
7222 /* For C++, set the block's scope. */
7223 if (cu->language == language_cplus || cu->language == language_fortran)
7224 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7225 determine_prefix (die, cu),
7226 processing_has_namespace_info);
7227
7228 /* If we have address ranges, record them. */
7229 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7230
7231 /* Attach template arguments to function. */
7232 if (! VEC_empty (symbolp, template_args))
7233 {
7234 gdb_assert (templ_func != NULL);
7235
7236 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7237 templ_func->template_arguments
7238 = obstack_alloc (&objfile->objfile_obstack,
7239 (templ_func->n_template_arguments
7240 * sizeof (struct symbol *)));
7241 memcpy (templ_func->template_arguments,
7242 VEC_address (symbolp, template_args),
7243 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7244 VEC_free (symbolp, template_args);
7245 }
7246
7247 /* In C++, we can have functions nested inside functions (e.g., when
7248 a function declares a class that has methods). This means that
7249 when we finish processing a function scope, we may need to go
7250 back to building a containing block's symbol lists. */
7251 local_symbols = new->locals;
7252 param_symbols = new->params;
7253 using_directives = new->using_directives;
7254
7255 /* If we've finished processing a top-level function, subsequent
7256 symbols go in the file symbol list. */
7257 if (outermost_context_p ())
7258 cu->list_in_scope = &file_symbols;
7259 }
7260
7261 /* Process all the DIES contained within a lexical block scope. Start
7262 a new scope, process the dies, and then close the scope. */
7263
7264 static void
7265 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7266 {
7267 struct objfile *objfile = cu->objfile;
7268 struct context_stack *new;
7269 CORE_ADDR lowpc, highpc;
7270 struct die_info *child_die;
7271 CORE_ADDR baseaddr;
7272
7273 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7274
7275 /* Ignore blocks with missing or invalid low and high pc attributes. */
7276 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7277 as multiple lexical blocks? Handling children in a sane way would
7278 be nasty. Might be easier to properly extend generic blocks to
7279 describe ranges. */
7280 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7281 return;
7282 lowpc += baseaddr;
7283 highpc += baseaddr;
7284
7285 push_context (0, lowpc);
7286 if (die->child != NULL)
7287 {
7288 child_die = die->child;
7289 while (child_die && child_die->tag)
7290 {
7291 process_die (child_die, cu);
7292 child_die = sibling_die (child_die);
7293 }
7294 }
7295 new = pop_context ();
7296
7297 if (local_symbols != NULL || using_directives != NULL)
7298 {
7299 struct block *block
7300 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7301 highpc, objfile);
7302
7303 /* Note that recording ranges after traversing children, as we
7304 do here, means that recording a parent's ranges entails
7305 walking across all its children's ranges as they appear in
7306 the address map, which is quadratic behavior.
7307
7308 It would be nicer to record the parent's ranges before
7309 traversing its children, simply overriding whatever you find
7310 there. But since we don't even decide whether to create a
7311 block until after we've traversed its children, that's hard
7312 to do. */
7313 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7314 }
7315 local_symbols = new->locals;
7316 using_directives = new->using_directives;
7317 }
7318
7319 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7320
7321 static void
7322 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7323 {
7324 struct objfile *objfile = cu->objfile;
7325 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7326 CORE_ADDR pc, baseaddr;
7327 struct attribute *attr;
7328 struct call_site *call_site, call_site_local;
7329 void **slot;
7330 int nparams;
7331 struct die_info *child_die;
7332
7333 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7334
7335 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7336 if (!attr)
7337 {
7338 complaint (&symfile_complaints,
7339 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7340 "DIE 0x%x [in module %s]"),
7341 die->offset.sect_off, objfile->name);
7342 return;
7343 }
7344 pc = DW_ADDR (attr) + baseaddr;
7345
7346 if (cu->call_site_htab == NULL)
7347 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7348 NULL, &objfile->objfile_obstack,
7349 hashtab_obstack_allocate, NULL);
7350 call_site_local.pc = pc;
7351 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7352 if (*slot != NULL)
7353 {
7354 complaint (&symfile_complaints,
7355 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7356 "DIE 0x%x [in module %s]"),
7357 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7358 return;
7359 }
7360
7361 /* Count parameters at the caller. */
7362
7363 nparams = 0;
7364 for (child_die = die->child; child_die && child_die->tag;
7365 child_die = sibling_die (child_die))
7366 {
7367 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7368 {
7369 complaint (&symfile_complaints,
7370 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7371 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7372 child_die->tag, child_die->offset.sect_off, objfile->name);
7373 continue;
7374 }
7375
7376 nparams++;
7377 }
7378
7379 call_site = obstack_alloc (&objfile->objfile_obstack,
7380 (sizeof (*call_site)
7381 + (sizeof (*call_site->parameter)
7382 * (nparams - 1))));
7383 *slot = call_site;
7384 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7385 call_site->pc = pc;
7386
7387 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7388 {
7389 struct die_info *func_die;
7390
7391 /* Skip also over DW_TAG_inlined_subroutine. */
7392 for (func_die = die->parent;
7393 func_die && func_die->tag != DW_TAG_subprogram
7394 && func_die->tag != DW_TAG_subroutine_type;
7395 func_die = func_die->parent);
7396
7397 /* DW_AT_GNU_all_call_sites is a superset
7398 of DW_AT_GNU_all_tail_call_sites. */
7399 if (func_die
7400 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7401 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7402 {
7403 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7404 not complete. But keep CALL_SITE for look ups via call_site_htab,
7405 both the initial caller containing the real return address PC and
7406 the final callee containing the current PC of a chain of tail
7407 calls do not need to have the tail call list complete. But any
7408 function candidate for a virtual tail call frame searched via
7409 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7410 determined unambiguously. */
7411 }
7412 else
7413 {
7414 struct type *func_type = NULL;
7415
7416 if (func_die)
7417 func_type = get_die_type (func_die, cu);
7418 if (func_type != NULL)
7419 {
7420 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7421
7422 /* Enlist this call site to the function. */
7423 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7424 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7425 }
7426 else
7427 complaint (&symfile_complaints,
7428 _("Cannot find function owning DW_TAG_GNU_call_site "
7429 "DIE 0x%x [in module %s]"),
7430 die->offset.sect_off, objfile->name);
7431 }
7432 }
7433
7434 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7435 if (attr == NULL)
7436 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7437 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7438 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7439 /* Keep NULL DWARF_BLOCK. */;
7440 else if (attr_form_is_block (attr))
7441 {
7442 struct dwarf2_locexpr_baton *dlbaton;
7443
7444 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7445 dlbaton->data = DW_BLOCK (attr)->data;
7446 dlbaton->size = DW_BLOCK (attr)->size;
7447 dlbaton->per_cu = cu->per_cu;
7448
7449 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7450 }
7451 else if (is_ref_attr (attr))
7452 {
7453 struct dwarf2_cu *target_cu = cu;
7454 struct die_info *target_die;
7455
7456 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7457 gdb_assert (target_cu->objfile == objfile);
7458 if (die_is_declaration (target_die, target_cu))
7459 {
7460 const char *target_physname;
7461
7462 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7463 if (target_physname == NULL)
7464 complaint (&symfile_complaints,
7465 _("DW_AT_GNU_call_site_target target DIE has invalid "
7466 "physname, for referencing DIE 0x%x [in module %s]"),
7467 die->offset.sect_off, objfile->name);
7468 else
7469 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7470 }
7471 else
7472 {
7473 CORE_ADDR lowpc;
7474
7475 /* DW_AT_entry_pc should be preferred. */
7476 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7477 complaint (&symfile_complaints,
7478 _("DW_AT_GNU_call_site_target target DIE has invalid "
7479 "low pc, for referencing DIE 0x%x [in module %s]"),
7480 die->offset.sect_off, objfile->name);
7481 else
7482 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7483 }
7484 }
7485 else
7486 complaint (&symfile_complaints,
7487 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7488 "block nor reference, for DIE 0x%x [in module %s]"),
7489 die->offset.sect_off, objfile->name);
7490
7491 call_site->per_cu = cu->per_cu;
7492
7493 for (child_die = die->child;
7494 child_die && child_die->tag;
7495 child_die = sibling_die (child_die))
7496 {
7497 struct dwarf2_locexpr_baton *dlbaton;
7498 struct call_site_parameter *parameter;
7499
7500 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7501 {
7502 /* Already printed the complaint above. */
7503 continue;
7504 }
7505
7506 gdb_assert (call_site->parameter_count < nparams);
7507 parameter = &call_site->parameter[call_site->parameter_count];
7508
7509 /* DW_AT_location specifies the register number. Value of the data
7510 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7511
7512 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7513 if (!attr || !attr_form_is_block (attr))
7514 {
7515 complaint (&symfile_complaints,
7516 _("No DW_FORM_block* DW_AT_location for "
7517 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7518 child_die->offset.sect_off, objfile->name);
7519 continue;
7520 }
7521 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7522 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7523 if (parameter->dwarf_reg == -1
7524 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7525 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7526 &parameter->fb_offset))
7527 {
7528 complaint (&symfile_complaints,
7529 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7530 "for DW_FORM_block* DW_AT_location for "
7531 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7532 child_die->offset.sect_off, objfile->name);
7533 continue;
7534 }
7535
7536 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7537 if (!attr_form_is_block (attr))
7538 {
7539 complaint (&symfile_complaints,
7540 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7541 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7542 child_die->offset.sect_off, objfile->name);
7543 continue;
7544 }
7545 parameter->value = DW_BLOCK (attr)->data;
7546 parameter->value_size = DW_BLOCK (attr)->size;
7547
7548 /* Parameters are not pre-cleared by memset above. */
7549 parameter->data_value = NULL;
7550 parameter->data_value_size = 0;
7551 call_site->parameter_count++;
7552
7553 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7554 if (attr)
7555 {
7556 if (!attr_form_is_block (attr))
7557 complaint (&symfile_complaints,
7558 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7559 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7560 child_die->offset.sect_off, objfile->name);
7561 else
7562 {
7563 parameter->data_value = DW_BLOCK (attr)->data;
7564 parameter->data_value_size = DW_BLOCK (attr)->size;
7565 }
7566 }
7567 }
7568 }
7569
7570 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
7571 Return 1 if the attributes are present and valid, otherwise, return 0.
7572 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
7573
7574 static int
7575 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
7576 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7577 struct partial_symtab *ranges_pst)
7578 {
7579 struct objfile *objfile = cu->objfile;
7580 struct comp_unit_head *cu_header = &cu->header;
7581 bfd *obfd = objfile->obfd;
7582 unsigned int addr_size = cu_header->addr_size;
7583 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7584 /* Base address selection entry. */
7585 CORE_ADDR base;
7586 int found_base;
7587 unsigned int dummy;
7588 gdb_byte *buffer;
7589 CORE_ADDR marker;
7590 int low_set;
7591 CORE_ADDR low = 0;
7592 CORE_ADDR high = 0;
7593 CORE_ADDR baseaddr;
7594
7595 found_base = cu->base_known;
7596 base = cu->base_address;
7597
7598 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
7599 if (offset >= dwarf2_per_objfile->ranges.size)
7600 {
7601 complaint (&symfile_complaints,
7602 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7603 offset);
7604 return 0;
7605 }
7606 buffer = dwarf2_per_objfile->ranges.buffer + offset;
7607
7608 /* Read in the largest possible address. */
7609 marker = read_address (obfd, buffer, cu, &dummy);
7610 if ((marker & mask) == mask)
7611 {
7612 /* If we found the largest possible address, then
7613 read the base address. */
7614 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7615 buffer += 2 * addr_size;
7616 offset += 2 * addr_size;
7617 found_base = 1;
7618 }
7619
7620 low_set = 0;
7621
7622 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7623
7624 while (1)
7625 {
7626 CORE_ADDR range_beginning, range_end;
7627
7628 range_beginning = read_address (obfd, buffer, cu, &dummy);
7629 buffer += addr_size;
7630 range_end = read_address (obfd, buffer, cu, &dummy);
7631 buffer += addr_size;
7632 offset += 2 * addr_size;
7633
7634 /* An end of list marker is a pair of zero addresses. */
7635 if (range_beginning == 0 && range_end == 0)
7636 /* Found the end of list entry. */
7637 break;
7638
7639 /* Each base address selection entry is a pair of 2 values.
7640 The first is the largest possible address, the second is
7641 the base address. Check for a base address here. */
7642 if ((range_beginning & mask) == mask)
7643 {
7644 /* If we found the largest possible address, then
7645 read the base address. */
7646 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7647 found_base = 1;
7648 continue;
7649 }
7650
7651 if (!found_base)
7652 {
7653 /* We have no valid base address for the ranges
7654 data. */
7655 complaint (&symfile_complaints,
7656 _("Invalid .debug_ranges data (no base address)"));
7657 return 0;
7658 }
7659
7660 if (range_beginning > range_end)
7661 {
7662 /* Inverted range entries are invalid. */
7663 complaint (&symfile_complaints,
7664 _("Invalid .debug_ranges data (inverted range)"));
7665 return 0;
7666 }
7667
7668 /* Empty range entries have no effect. */
7669 if (range_beginning == range_end)
7670 continue;
7671
7672 range_beginning += base;
7673 range_end += base;
7674
7675 if (ranges_pst != NULL)
7676 addrmap_set_empty (objfile->psymtabs_addrmap,
7677 range_beginning + baseaddr,
7678 range_end - 1 + baseaddr,
7679 ranges_pst);
7680
7681 /* FIXME: This is recording everything as a low-high
7682 segment of consecutive addresses. We should have a
7683 data structure for discontiguous block ranges
7684 instead. */
7685 if (! low_set)
7686 {
7687 low = range_beginning;
7688 high = range_end;
7689 low_set = 1;
7690 }
7691 else
7692 {
7693 if (range_beginning < low)
7694 low = range_beginning;
7695 if (range_end > high)
7696 high = range_end;
7697 }
7698 }
7699
7700 if (! low_set)
7701 /* If the first entry is an end-of-list marker, the range
7702 describes an empty scope, i.e. no instructions. */
7703 return 0;
7704
7705 if (low_return)
7706 *low_return = low;
7707 if (high_return)
7708 *high_return = high;
7709 return 1;
7710 }
7711
7712 /* Get low and high pc attributes from a die. Return 1 if the attributes
7713 are present and valid, otherwise, return 0. Return -1 if the range is
7714 discontinuous, i.e. derived from DW_AT_ranges information. */
7715
7716 static int
7717 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
7718 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7719 struct partial_symtab *pst)
7720 {
7721 struct attribute *attr;
7722 struct attribute *attr_high;
7723 CORE_ADDR low = 0;
7724 CORE_ADDR high = 0;
7725 int ret = 0;
7726
7727 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7728 if (attr_high)
7729 {
7730 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7731 if (attr)
7732 {
7733 low = DW_ADDR (attr);
7734 if (attr_high->form == DW_FORM_addr
7735 || attr_high->form == DW_FORM_GNU_addr_index)
7736 high = DW_ADDR (attr_high);
7737 else
7738 high = low + DW_UNSND (attr_high);
7739 }
7740 else
7741 /* Found high w/o low attribute. */
7742 return 0;
7743
7744 /* Found consecutive range of addresses. */
7745 ret = 1;
7746 }
7747 else
7748 {
7749 attr = dwarf2_attr (die, DW_AT_ranges, cu);
7750 if (attr != NULL)
7751 {
7752 /* Value of the DW_AT_ranges attribute is the offset in the
7753 .debug_ranges section. */
7754 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
7755 return 0;
7756 /* Found discontinuous range of addresses. */
7757 ret = -1;
7758 }
7759 }
7760
7761 /* read_partial_die has also the strict LOW < HIGH requirement. */
7762 if (high <= low)
7763 return 0;
7764
7765 /* When using the GNU linker, .gnu.linkonce. sections are used to
7766 eliminate duplicate copies of functions and vtables and such.
7767 The linker will arbitrarily choose one and discard the others.
7768 The AT_*_pc values for such functions refer to local labels in
7769 these sections. If the section from that file was discarded, the
7770 labels are not in the output, so the relocs get a value of 0.
7771 If this is a discarded function, mark the pc bounds as invalid,
7772 so that GDB will ignore it. */
7773 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
7774 return 0;
7775
7776 *lowpc = low;
7777 if (highpc)
7778 *highpc = high;
7779 return ret;
7780 }
7781
7782 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
7783 its low and high PC addresses. Do nothing if these addresses could not
7784 be determined. Otherwise, set LOWPC to the low address if it is smaller,
7785 and HIGHPC to the high address if greater than HIGHPC. */
7786
7787 static void
7788 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
7789 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7790 struct dwarf2_cu *cu)
7791 {
7792 CORE_ADDR low, high;
7793 struct die_info *child = die->child;
7794
7795 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
7796 {
7797 *lowpc = min (*lowpc, low);
7798 *highpc = max (*highpc, high);
7799 }
7800
7801 /* If the language does not allow nested subprograms (either inside
7802 subprograms or lexical blocks), we're done. */
7803 if (cu->language != language_ada)
7804 return;
7805
7806 /* Check all the children of the given DIE. If it contains nested
7807 subprograms, then check their pc bounds. Likewise, we need to
7808 check lexical blocks as well, as they may also contain subprogram
7809 definitions. */
7810 while (child && child->tag)
7811 {
7812 if (child->tag == DW_TAG_subprogram
7813 || child->tag == DW_TAG_lexical_block)
7814 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
7815 child = sibling_die (child);
7816 }
7817 }
7818
7819 /* Get the low and high pc's represented by the scope DIE, and store
7820 them in *LOWPC and *HIGHPC. If the correct values can't be
7821 determined, set *LOWPC to -1 and *HIGHPC to 0. */
7822
7823 static void
7824 get_scope_pc_bounds (struct die_info *die,
7825 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7826 struct dwarf2_cu *cu)
7827 {
7828 CORE_ADDR best_low = (CORE_ADDR) -1;
7829 CORE_ADDR best_high = (CORE_ADDR) 0;
7830 CORE_ADDR current_low, current_high;
7831
7832 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
7833 {
7834 best_low = current_low;
7835 best_high = current_high;
7836 }
7837 else
7838 {
7839 struct die_info *child = die->child;
7840
7841 while (child && child->tag)
7842 {
7843 switch (child->tag) {
7844 case DW_TAG_subprogram:
7845 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
7846 break;
7847 case DW_TAG_namespace:
7848 case DW_TAG_module:
7849 /* FIXME: carlton/2004-01-16: Should we do this for
7850 DW_TAG_class_type/DW_TAG_structure_type, too? I think
7851 that current GCC's always emit the DIEs corresponding
7852 to definitions of methods of classes as children of a
7853 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
7854 the DIEs giving the declarations, which could be
7855 anywhere). But I don't see any reason why the
7856 standards says that they have to be there. */
7857 get_scope_pc_bounds (child, &current_low, &current_high, cu);
7858
7859 if (current_low != ((CORE_ADDR) -1))
7860 {
7861 best_low = min (best_low, current_low);
7862 best_high = max (best_high, current_high);
7863 }
7864 break;
7865 default:
7866 /* Ignore. */
7867 break;
7868 }
7869
7870 child = sibling_die (child);
7871 }
7872 }
7873
7874 *lowpc = best_low;
7875 *highpc = best_high;
7876 }
7877
7878 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
7879 in DIE. */
7880
7881 static void
7882 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
7883 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
7884 {
7885 struct objfile *objfile = cu->objfile;
7886 struct attribute *attr;
7887 struct attribute *attr_high;
7888
7889 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7890 if (attr_high)
7891 {
7892 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7893 if (attr)
7894 {
7895 CORE_ADDR low = DW_ADDR (attr);
7896 CORE_ADDR high;
7897 if (attr_high->form == DW_FORM_addr
7898 || attr_high->form == DW_FORM_GNU_addr_index)
7899 high = DW_ADDR (attr_high);
7900 else
7901 high = low + DW_UNSND (attr_high);
7902
7903 record_block_range (block, baseaddr + low, baseaddr + high - 1);
7904 }
7905 }
7906
7907 attr = dwarf2_attr (die, DW_AT_ranges, cu);
7908 if (attr)
7909 {
7910 bfd *obfd = objfile->obfd;
7911
7912 /* The value of the DW_AT_ranges attribute is the offset of the
7913 address range list in the .debug_ranges section. */
7914 unsigned long offset = DW_UNSND (attr);
7915 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
7916
7917 /* For some target architectures, but not others, the
7918 read_address function sign-extends the addresses it returns.
7919 To recognize base address selection entries, we need a
7920 mask. */
7921 unsigned int addr_size = cu->header.addr_size;
7922 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7923
7924 /* The base address, to which the next pair is relative. Note
7925 that this 'base' is a DWARF concept: most entries in a range
7926 list are relative, to reduce the number of relocs against the
7927 debugging information. This is separate from this function's
7928 'baseaddr' argument, which GDB uses to relocate debugging
7929 information from a shared library based on the address at
7930 which the library was loaded. */
7931 CORE_ADDR base = cu->base_address;
7932 int base_known = cu->base_known;
7933
7934 gdb_assert (dwarf2_per_objfile->ranges.readin);
7935 if (offset >= dwarf2_per_objfile->ranges.size)
7936 {
7937 complaint (&symfile_complaints,
7938 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
7939 offset);
7940 return;
7941 }
7942
7943 for (;;)
7944 {
7945 unsigned int bytes_read;
7946 CORE_ADDR start, end;
7947
7948 start = read_address (obfd, buffer, cu, &bytes_read);
7949 buffer += bytes_read;
7950 end = read_address (obfd, buffer, cu, &bytes_read);
7951 buffer += bytes_read;
7952
7953 /* Did we find the end of the range list? */
7954 if (start == 0 && end == 0)
7955 break;
7956
7957 /* Did we find a base address selection entry? */
7958 else if ((start & base_select_mask) == base_select_mask)
7959 {
7960 base = end;
7961 base_known = 1;
7962 }
7963
7964 /* We found an ordinary address range. */
7965 else
7966 {
7967 if (!base_known)
7968 {
7969 complaint (&symfile_complaints,
7970 _("Invalid .debug_ranges data "
7971 "(no base address)"));
7972 return;
7973 }
7974
7975 if (start > end)
7976 {
7977 /* Inverted range entries are invalid. */
7978 complaint (&symfile_complaints,
7979 _("Invalid .debug_ranges data "
7980 "(inverted range)"));
7981 return;
7982 }
7983
7984 /* Empty range entries have no effect. */
7985 if (start == end)
7986 continue;
7987
7988 record_block_range (block,
7989 baseaddr + base + start,
7990 baseaddr + base + end - 1);
7991 }
7992 }
7993 }
7994 }
7995
7996 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
7997 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
7998 during 4.6.0 experimental. */
7999
8000 static int
8001 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8002 {
8003 const char *cs;
8004 int major, minor, release;
8005 int result = 0;
8006
8007 if (cu->producer == NULL)
8008 {
8009 /* For unknown compilers expect their behavior is DWARF version
8010 compliant.
8011
8012 GCC started to support .debug_types sections by -gdwarf-4 since
8013 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8014 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8015 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8016 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8017
8018 return 0;
8019 }
8020
8021 if (cu->checked_producer)
8022 return cu->producer_is_gxx_lt_4_6;
8023
8024 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8025
8026 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8027 {
8028 /* For non-GCC compilers expect their behavior is DWARF version
8029 compliant. */
8030 }
8031 else
8032 {
8033 cs = &cu->producer[strlen ("GNU ")];
8034 while (*cs && !isdigit (*cs))
8035 cs++;
8036 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8037 {
8038 /* Not recognized as GCC. */
8039 }
8040 else
8041 result = major < 4 || (major == 4 && minor < 6);
8042 }
8043
8044 cu->checked_producer = 1;
8045 cu->producer_is_gxx_lt_4_6 = result;
8046
8047 return result;
8048 }
8049
8050 /* Return the default accessibility type if it is not overriden by
8051 DW_AT_accessibility. */
8052
8053 static enum dwarf_access_attribute
8054 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8055 {
8056 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8057 {
8058 /* The default DWARF 2 accessibility for members is public, the default
8059 accessibility for inheritance is private. */
8060
8061 if (die->tag != DW_TAG_inheritance)
8062 return DW_ACCESS_public;
8063 else
8064 return DW_ACCESS_private;
8065 }
8066 else
8067 {
8068 /* DWARF 3+ defines the default accessibility a different way. The same
8069 rules apply now for DW_TAG_inheritance as for the members and it only
8070 depends on the container kind. */
8071
8072 if (die->parent->tag == DW_TAG_class_type)
8073 return DW_ACCESS_private;
8074 else
8075 return DW_ACCESS_public;
8076 }
8077 }
8078
8079 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8080 offset. If the attribute was not found return 0, otherwise return
8081 1. If it was found but could not properly be handled, set *OFFSET
8082 to 0. */
8083
8084 static int
8085 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8086 LONGEST *offset)
8087 {
8088 struct attribute *attr;
8089
8090 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8091 if (attr != NULL)
8092 {
8093 *offset = 0;
8094
8095 /* Note that we do not check for a section offset first here.
8096 This is because DW_AT_data_member_location is new in DWARF 4,
8097 so if we see it, we can assume that a constant form is really
8098 a constant and not a section offset. */
8099 if (attr_form_is_constant (attr))
8100 *offset = dwarf2_get_attr_constant_value (attr, 0);
8101 else if (attr_form_is_section_offset (attr))
8102 dwarf2_complex_location_expr_complaint ();
8103 else if (attr_form_is_block (attr))
8104 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8105 else
8106 dwarf2_complex_location_expr_complaint ();
8107
8108 return 1;
8109 }
8110
8111 return 0;
8112 }
8113
8114 /* Add an aggregate field to the field list. */
8115
8116 static void
8117 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8118 struct dwarf2_cu *cu)
8119 {
8120 struct objfile *objfile = cu->objfile;
8121 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8122 struct nextfield *new_field;
8123 struct attribute *attr;
8124 struct field *fp;
8125 char *fieldname = "";
8126
8127 /* Allocate a new field list entry and link it in. */
8128 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8129 make_cleanup (xfree, new_field);
8130 memset (new_field, 0, sizeof (struct nextfield));
8131
8132 if (die->tag == DW_TAG_inheritance)
8133 {
8134 new_field->next = fip->baseclasses;
8135 fip->baseclasses = new_field;
8136 }
8137 else
8138 {
8139 new_field->next = fip->fields;
8140 fip->fields = new_field;
8141 }
8142 fip->nfields++;
8143
8144 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8145 if (attr)
8146 new_field->accessibility = DW_UNSND (attr);
8147 else
8148 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8149 if (new_field->accessibility != DW_ACCESS_public)
8150 fip->non_public_fields = 1;
8151
8152 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8153 if (attr)
8154 new_field->virtuality = DW_UNSND (attr);
8155 else
8156 new_field->virtuality = DW_VIRTUALITY_none;
8157
8158 fp = &new_field->field;
8159
8160 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8161 {
8162 LONGEST offset;
8163
8164 /* Data member other than a C++ static data member. */
8165
8166 /* Get type of field. */
8167 fp->type = die_type (die, cu);
8168
8169 SET_FIELD_BITPOS (*fp, 0);
8170
8171 /* Get bit size of field (zero if none). */
8172 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8173 if (attr)
8174 {
8175 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8176 }
8177 else
8178 {
8179 FIELD_BITSIZE (*fp) = 0;
8180 }
8181
8182 /* Get bit offset of field. */
8183 if (handle_data_member_location (die, cu, &offset))
8184 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8185 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8186 if (attr)
8187 {
8188 if (gdbarch_bits_big_endian (gdbarch))
8189 {
8190 /* For big endian bits, the DW_AT_bit_offset gives the
8191 additional bit offset from the MSB of the containing
8192 anonymous object to the MSB of the field. We don't
8193 have to do anything special since we don't need to
8194 know the size of the anonymous object. */
8195 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8196 }
8197 else
8198 {
8199 /* For little endian bits, compute the bit offset to the
8200 MSB of the anonymous object, subtract off the number of
8201 bits from the MSB of the field to the MSB of the
8202 object, and then subtract off the number of bits of
8203 the field itself. The result is the bit offset of
8204 the LSB of the field. */
8205 int anonymous_size;
8206 int bit_offset = DW_UNSND (attr);
8207
8208 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8209 if (attr)
8210 {
8211 /* The size of the anonymous object containing
8212 the bit field is explicit, so use the
8213 indicated size (in bytes). */
8214 anonymous_size = DW_UNSND (attr);
8215 }
8216 else
8217 {
8218 /* The size of the anonymous object containing
8219 the bit field must be inferred from the type
8220 attribute of the data member containing the
8221 bit field. */
8222 anonymous_size = TYPE_LENGTH (fp->type);
8223 }
8224 SET_FIELD_BITPOS (*fp,
8225 (FIELD_BITPOS (*fp)
8226 + anonymous_size * bits_per_byte
8227 - bit_offset - FIELD_BITSIZE (*fp)));
8228 }
8229 }
8230
8231 /* Get name of field. */
8232 fieldname = dwarf2_name (die, cu);
8233 if (fieldname == NULL)
8234 fieldname = "";
8235
8236 /* The name is already allocated along with this objfile, so we don't
8237 need to duplicate it for the type. */
8238 fp->name = fieldname;
8239
8240 /* Change accessibility for artificial fields (e.g. virtual table
8241 pointer or virtual base class pointer) to private. */
8242 if (dwarf2_attr (die, DW_AT_artificial, cu))
8243 {
8244 FIELD_ARTIFICIAL (*fp) = 1;
8245 new_field->accessibility = DW_ACCESS_private;
8246 fip->non_public_fields = 1;
8247 }
8248 }
8249 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8250 {
8251 /* C++ static member. */
8252
8253 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8254 is a declaration, but all versions of G++ as of this writing
8255 (so through at least 3.2.1) incorrectly generate
8256 DW_TAG_variable tags. */
8257
8258 const char *physname;
8259
8260 /* Get name of field. */
8261 fieldname = dwarf2_name (die, cu);
8262 if (fieldname == NULL)
8263 return;
8264
8265 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8266 if (attr
8267 /* Only create a symbol if this is an external value.
8268 new_symbol checks this and puts the value in the global symbol
8269 table, which we want. If it is not external, new_symbol
8270 will try to put the value in cu->list_in_scope which is wrong. */
8271 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8272 {
8273 /* A static const member, not much different than an enum as far as
8274 we're concerned, except that we can support more types. */
8275 new_symbol (die, NULL, cu);
8276 }
8277
8278 /* Get physical name. */
8279 physname = dwarf2_physname (fieldname, die, cu);
8280
8281 /* The name is already allocated along with this objfile, so we don't
8282 need to duplicate it for the type. */
8283 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8284 FIELD_TYPE (*fp) = die_type (die, cu);
8285 FIELD_NAME (*fp) = fieldname;
8286 }
8287 else if (die->tag == DW_TAG_inheritance)
8288 {
8289 LONGEST offset;
8290
8291 /* C++ base class field. */
8292 if (handle_data_member_location (die, cu, &offset))
8293 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8294 FIELD_BITSIZE (*fp) = 0;
8295 FIELD_TYPE (*fp) = die_type (die, cu);
8296 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8297 fip->nbaseclasses++;
8298 }
8299 }
8300
8301 /* Add a typedef defined in the scope of the FIP's class. */
8302
8303 static void
8304 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8305 struct dwarf2_cu *cu)
8306 {
8307 struct objfile *objfile = cu->objfile;
8308 struct typedef_field_list *new_field;
8309 struct attribute *attr;
8310 struct typedef_field *fp;
8311 char *fieldname = "";
8312
8313 /* Allocate a new field list entry and link it in. */
8314 new_field = xzalloc (sizeof (*new_field));
8315 make_cleanup (xfree, new_field);
8316
8317 gdb_assert (die->tag == DW_TAG_typedef);
8318
8319 fp = &new_field->field;
8320
8321 /* Get name of field. */
8322 fp->name = dwarf2_name (die, cu);
8323 if (fp->name == NULL)
8324 return;
8325
8326 fp->type = read_type_die (die, cu);
8327
8328 new_field->next = fip->typedef_field_list;
8329 fip->typedef_field_list = new_field;
8330 fip->typedef_field_list_count++;
8331 }
8332
8333 /* Create the vector of fields, and attach it to the type. */
8334
8335 static void
8336 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8337 struct dwarf2_cu *cu)
8338 {
8339 int nfields = fip->nfields;
8340
8341 /* Record the field count, allocate space for the array of fields,
8342 and create blank accessibility bitfields if necessary. */
8343 TYPE_NFIELDS (type) = nfields;
8344 TYPE_FIELDS (type) = (struct field *)
8345 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8346 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8347
8348 if (fip->non_public_fields && cu->language != language_ada)
8349 {
8350 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8351
8352 TYPE_FIELD_PRIVATE_BITS (type) =
8353 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8354 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8355
8356 TYPE_FIELD_PROTECTED_BITS (type) =
8357 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8358 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8359
8360 TYPE_FIELD_IGNORE_BITS (type) =
8361 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8362 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8363 }
8364
8365 /* If the type has baseclasses, allocate and clear a bit vector for
8366 TYPE_FIELD_VIRTUAL_BITS. */
8367 if (fip->nbaseclasses && cu->language != language_ada)
8368 {
8369 int num_bytes = B_BYTES (fip->nbaseclasses);
8370 unsigned char *pointer;
8371
8372 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8373 pointer = TYPE_ALLOC (type, num_bytes);
8374 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8375 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8376 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8377 }
8378
8379 /* Copy the saved-up fields into the field vector. Start from the head of
8380 the list, adding to the tail of the field array, so that they end up in
8381 the same order in the array in which they were added to the list. */
8382 while (nfields-- > 0)
8383 {
8384 struct nextfield *fieldp;
8385
8386 if (fip->fields)
8387 {
8388 fieldp = fip->fields;
8389 fip->fields = fieldp->next;
8390 }
8391 else
8392 {
8393 fieldp = fip->baseclasses;
8394 fip->baseclasses = fieldp->next;
8395 }
8396
8397 TYPE_FIELD (type, nfields) = fieldp->field;
8398 switch (fieldp->accessibility)
8399 {
8400 case DW_ACCESS_private:
8401 if (cu->language != language_ada)
8402 SET_TYPE_FIELD_PRIVATE (type, nfields);
8403 break;
8404
8405 case DW_ACCESS_protected:
8406 if (cu->language != language_ada)
8407 SET_TYPE_FIELD_PROTECTED (type, nfields);
8408 break;
8409
8410 case DW_ACCESS_public:
8411 break;
8412
8413 default:
8414 /* Unknown accessibility. Complain and treat it as public. */
8415 {
8416 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8417 fieldp->accessibility);
8418 }
8419 break;
8420 }
8421 if (nfields < fip->nbaseclasses)
8422 {
8423 switch (fieldp->virtuality)
8424 {
8425 case DW_VIRTUALITY_virtual:
8426 case DW_VIRTUALITY_pure_virtual:
8427 if (cu->language == language_ada)
8428 error (_("unexpected virtuality in component of Ada type"));
8429 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8430 break;
8431 }
8432 }
8433 }
8434 }
8435
8436 /* Add a member function to the proper fieldlist. */
8437
8438 static void
8439 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8440 struct type *type, struct dwarf2_cu *cu)
8441 {
8442 struct objfile *objfile = cu->objfile;
8443 struct attribute *attr;
8444 struct fnfieldlist *flp;
8445 int i;
8446 struct fn_field *fnp;
8447 char *fieldname;
8448 struct nextfnfield *new_fnfield;
8449 struct type *this_type;
8450 enum dwarf_access_attribute accessibility;
8451
8452 if (cu->language == language_ada)
8453 error (_("unexpected member function in Ada type"));
8454
8455 /* Get name of member function. */
8456 fieldname = dwarf2_name (die, cu);
8457 if (fieldname == NULL)
8458 return;
8459
8460 /* Look up member function name in fieldlist. */
8461 for (i = 0; i < fip->nfnfields; i++)
8462 {
8463 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8464 break;
8465 }
8466
8467 /* Create new list element if necessary. */
8468 if (i < fip->nfnfields)
8469 flp = &fip->fnfieldlists[i];
8470 else
8471 {
8472 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8473 {
8474 fip->fnfieldlists = (struct fnfieldlist *)
8475 xrealloc (fip->fnfieldlists,
8476 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8477 * sizeof (struct fnfieldlist));
8478 if (fip->nfnfields == 0)
8479 make_cleanup (free_current_contents, &fip->fnfieldlists);
8480 }
8481 flp = &fip->fnfieldlists[fip->nfnfields];
8482 flp->name = fieldname;
8483 flp->length = 0;
8484 flp->head = NULL;
8485 i = fip->nfnfields++;
8486 }
8487
8488 /* Create a new member function field and chain it to the field list
8489 entry. */
8490 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8491 make_cleanup (xfree, new_fnfield);
8492 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8493 new_fnfield->next = flp->head;
8494 flp->head = new_fnfield;
8495 flp->length++;
8496
8497 /* Fill in the member function field info. */
8498 fnp = &new_fnfield->fnfield;
8499
8500 /* Delay processing of the physname until later. */
8501 if (cu->language == language_cplus || cu->language == language_java)
8502 {
8503 add_to_method_list (type, i, flp->length - 1, fieldname,
8504 die, cu);
8505 }
8506 else
8507 {
8508 const char *physname = dwarf2_physname (fieldname, die, cu);
8509 fnp->physname = physname ? physname : "";
8510 }
8511
8512 fnp->type = alloc_type (objfile);
8513 this_type = read_type_die (die, cu);
8514 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
8515 {
8516 int nparams = TYPE_NFIELDS (this_type);
8517
8518 /* TYPE is the domain of this method, and THIS_TYPE is the type
8519 of the method itself (TYPE_CODE_METHOD). */
8520 smash_to_method_type (fnp->type, type,
8521 TYPE_TARGET_TYPE (this_type),
8522 TYPE_FIELDS (this_type),
8523 TYPE_NFIELDS (this_type),
8524 TYPE_VARARGS (this_type));
8525
8526 /* Handle static member functions.
8527 Dwarf2 has no clean way to discern C++ static and non-static
8528 member functions. G++ helps GDB by marking the first
8529 parameter for non-static member functions (which is the this
8530 pointer) as artificial. We obtain this information from
8531 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
8532 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
8533 fnp->voffset = VOFFSET_STATIC;
8534 }
8535 else
8536 complaint (&symfile_complaints, _("member function type missing for '%s'"),
8537 dwarf2_full_name (fieldname, die, cu));
8538
8539 /* Get fcontext from DW_AT_containing_type if present. */
8540 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8541 fnp->fcontext = die_containing_type (die, cu);
8542
8543 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8544 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
8545
8546 /* Get accessibility. */
8547 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8548 if (attr)
8549 accessibility = DW_UNSND (attr);
8550 else
8551 accessibility = dwarf2_default_access_attribute (die, cu);
8552 switch (accessibility)
8553 {
8554 case DW_ACCESS_private:
8555 fnp->is_private = 1;
8556 break;
8557 case DW_ACCESS_protected:
8558 fnp->is_protected = 1;
8559 break;
8560 }
8561
8562 /* Check for artificial methods. */
8563 attr = dwarf2_attr (die, DW_AT_artificial, cu);
8564 if (attr && DW_UNSND (attr) != 0)
8565 fnp->is_artificial = 1;
8566
8567 /* Get index in virtual function table if it is a virtual member
8568 function. For older versions of GCC, this is an offset in the
8569 appropriate virtual table, as specified by DW_AT_containing_type.
8570 For everyone else, it is an expression to be evaluated relative
8571 to the object address. */
8572
8573 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
8574 if (attr)
8575 {
8576 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8577 {
8578 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8579 {
8580 /* Old-style GCC. */
8581 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8582 }
8583 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8584 || (DW_BLOCK (attr)->size > 1
8585 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8586 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8587 {
8588 struct dwarf_block blk;
8589 int offset;
8590
8591 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8592 ? 1 : 2);
8593 blk.size = DW_BLOCK (attr)->size - offset;
8594 blk.data = DW_BLOCK (attr)->data + offset;
8595 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8596 if ((fnp->voffset % cu->header.addr_size) != 0)
8597 dwarf2_complex_location_expr_complaint ();
8598 else
8599 fnp->voffset /= cu->header.addr_size;
8600 fnp->voffset += 2;
8601 }
8602 else
8603 dwarf2_complex_location_expr_complaint ();
8604
8605 if (!fnp->fcontext)
8606 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8607 }
8608 else if (attr_form_is_section_offset (attr))
8609 {
8610 dwarf2_complex_location_expr_complaint ();
8611 }
8612 else
8613 {
8614 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8615 fieldname);
8616 }
8617 }
8618 else
8619 {
8620 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8621 if (attr && DW_UNSND (attr))
8622 {
8623 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8624 complaint (&symfile_complaints,
8625 _("Member function \"%s\" (offset %d) is virtual "
8626 "but the vtable offset is not specified"),
8627 fieldname, die->offset.sect_off);
8628 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8629 TYPE_CPLUS_DYNAMIC (type) = 1;
8630 }
8631 }
8632 }
8633
8634 /* Create the vector of member function fields, and attach it to the type. */
8635
8636 static void
8637 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
8638 struct dwarf2_cu *cu)
8639 {
8640 struct fnfieldlist *flp;
8641 int i;
8642
8643 if (cu->language == language_ada)
8644 error (_("unexpected member functions in Ada type"));
8645
8646 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8647 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8648 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8649
8650 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8651 {
8652 struct nextfnfield *nfp = flp->head;
8653 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8654 int k;
8655
8656 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8657 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8658 fn_flp->fn_fields = (struct fn_field *)
8659 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8660 for (k = flp->length; (k--, nfp); nfp = nfp->next)
8661 fn_flp->fn_fields[k] = nfp->fnfield;
8662 }
8663
8664 TYPE_NFN_FIELDS (type) = fip->nfnfields;
8665 }
8666
8667 /* Returns non-zero if NAME is the name of a vtable member in CU's
8668 language, zero otherwise. */
8669 static int
8670 is_vtable_name (const char *name, struct dwarf2_cu *cu)
8671 {
8672 static const char vptr[] = "_vptr";
8673 static const char vtable[] = "vtable";
8674
8675 /* Look for the C++ and Java forms of the vtable. */
8676 if ((cu->language == language_java
8677 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8678 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8679 && is_cplus_marker (name[sizeof (vptr) - 1])))
8680 return 1;
8681
8682 return 0;
8683 }
8684
8685 /* GCC outputs unnamed structures that are really pointers to member
8686 functions, with the ABI-specified layout. If TYPE describes
8687 such a structure, smash it into a member function type.
8688
8689 GCC shouldn't do this; it should just output pointer to member DIEs.
8690 This is GCC PR debug/28767. */
8691
8692 static void
8693 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
8694 {
8695 struct type *pfn_type, *domain_type, *new_type;
8696
8697 /* Check for a structure with no name and two children. */
8698 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8699 return;
8700
8701 /* Check for __pfn and __delta members. */
8702 if (TYPE_FIELD_NAME (type, 0) == NULL
8703 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8704 || TYPE_FIELD_NAME (type, 1) == NULL
8705 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8706 return;
8707
8708 /* Find the type of the method. */
8709 pfn_type = TYPE_FIELD_TYPE (type, 0);
8710 if (pfn_type == NULL
8711 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8712 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
8713 return;
8714
8715 /* Look for the "this" argument. */
8716 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8717 if (TYPE_NFIELDS (pfn_type) == 0
8718 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
8719 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
8720 return;
8721
8722 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
8723 new_type = alloc_type (objfile);
8724 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
8725 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
8726 TYPE_VARARGS (pfn_type));
8727 smash_to_methodptr_type (type, new_type);
8728 }
8729
8730 /* Called when we find the DIE that starts a structure or union scope
8731 (definition) to create a type for the structure or union. Fill in
8732 the type's name and general properties; the members will not be
8733 processed until process_structure_type.
8734
8735 NOTE: we need to call these functions regardless of whether or not the
8736 DIE has a DW_AT_name attribute, since it might be an anonymous
8737 structure or union. This gets the type entered into our set of
8738 user defined types.
8739
8740 However, if the structure is incomplete (an opaque struct/union)
8741 then suppress creating a symbol table entry for it since gdb only
8742 wants to find the one with the complete definition. Note that if
8743 it is complete, we just call new_symbol, which does it's own
8744 checking about whether the struct/union is anonymous or not (and
8745 suppresses creating a symbol table entry itself). */
8746
8747 static struct type *
8748 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
8749 {
8750 struct objfile *objfile = cu->objfile;
8751 struct type *type;
8752 struct attribute *attr;
8753 char *name;
8754
8755 /* If the definition of this type lives in .debug_types, read that type.
8756 Don't follow DW_AT_specification though, that will take us back up
8757 the chain and we want to go down. */
8758 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
8759 if (attr)
8760 {
8761 struct dwarf2_cu *type_cu = cu;
8762 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
8763
8764 /* We could just recurse on read_structure_type, but we need to call
8765 get_die_type to ensure only one type for this DIE is created.
8766 This is important, for example, because for c++ classes we need
8767 TYPE_NAME set which is only done by new_symbol. Blech. */
8768 type = read_type_die (type_die, type_cu);
8769
8770 /* TYPE_CU may not be the same as CU.
8771 Ensure TYPE is recorded in CU's type_hash table. */
8772 return set_die_type (die, type, cu);
8773 }
8774
8775 type = alloc_type (objfile);
8776 INIT_CPLUS_SPECIFIC (type);
8777
8778 name = dwarf2_name (die, cu);
8779 if (name != NULL)
8780 {
8781 if (cu->language == language_cplus
8782 || cu->language == language_java)
8783 {
8784 char *full_name = (char *) dwarf2_full_name (name, die, cu);
8785
8786 /* dwarf2_full_name might have already finished building the DIE's
8787 type. If so, there is no need to continue. */
8788 if (get_die_type (die, cu) != NULL)
8789 return get_die_type (die, cu);
8790
8791 TYPE_TAG_NAME (type) = full_name;
8792 if (die->tag == DW_TAG_structure_type
8793 || die->tag == DW_TAG_class_type)
8794 TYPE_NAME (type) = TYPE_TAG_NAME (type);
8795 }
8796 else
8797 {
8798 /* The name is already allocated along with this objfile, so
8799 we don't need to duplicate it for the type. */
8800 TYPE_TAG_NAME (type) = (char *) name;
8801 if (die->tag == DW_TAG_class_type)
8802 TYPE_NAME (type) = TYPE_TAG_NAME (type);
8803 }
8804 }
8805
8806 if (die->tag == DW_TAG_structure_type)
8807 {
8808 TYPE_CODE (type) = TYPE_CODE_STRUCT;
8809 }
8810 else if (die->tag == DW_TAG_union_type)
8811 {
8812 TYPE_CODE (type) = TYPE_CODE_UNION;
8813 }
8814 else
8815 {
8816 TYPE_CODE (type) = TYPE_CODE_CLASS;
8817 }
8818
8819 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
8820 TYPE_DECLARED_CLASS (type) = 1;
8821
8822 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8823 if (attr)
8824 {
8825 TYPE_LENGTH (type) = DW_UNSND (attr);
8826 }
8827 else
8828 {
8829 TYPE_LENGTH (type) = 0;
8830 }
8831
8832 TYPE_STUB_SUPPORTED (type) = 1;
8833 if (die_is_declaration (die, cu))
8834 TYPE_STUB (type) = 1;
8835 else if (attr == NULL && die->child == NULL
8836 && producer_is_realview (cu->producer))
8837 /* RealView does not output the required DW_AT_declaration
8838 on incomplete types. */
8839 TYPE_STUB (type) = 1;
8840
8841 /* We need to add the type field to the die immediately so we don't
8842 infinitely recurse when dealing with pointers to the structure
8843 type within the structure itself. */
8844 set_die_type (die, type, cu);
8845
8846 /* set_die_type should be already done. */
8847 set_descriptive_type (type, die, cu);
8848
8849 return type;
8850 }
8851
8852 /* Finish creating a structure or union type, including filling in
8853 its members and creating a symbol for it. */
8854
8855 static void
8856 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
8857 {
8858 struct objfile *objfile = cu->objfile;
8859 struct die_info *child_die = die->child;
8860 struct type *type;
8861
8862 type = get_die_type (die, cu);
8863 if (type == NULL)
8864 type = read_structure_type (die, cu);
8865
8866 if (die->child != NULL && ! die_is_declaration (die, cu))
8867 {
8868 struct field_info fi;
8869 struct die_info *child_die;
8870 VEC (symbolp) *template_args = NULL;
8871 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8872
8873 memset (&fi, 0, sizeof (struct field_info));
8874
8875 child_die = die->child;
8876
8877 while (child_die && child_die->tag)
8878 {
8879 if (child_die->tag == DW_TAG_member
8880 || child_die->tag == DW_TAG_variable)
8881 {
8882 /* NOTE: carlton/2002-11-05: A C++ static data member
8883 should be a DW_TAG_member that is a declaration, but
8884 all versions of G++ as of this writing (so through at
8885 least 3.2.1) incorrectly generate DW_TAG_variable
8886 tags for them instead. */
8887 dwarf2_add_field (&fi, child_die, cu);
8888 }
8889 else if (child_die->tag == DW_TAG_subprogram)
8890 {
8891 /* C++ member function. */
8892 dwarf2_add_member_fn (&fi, child_die, type, cu);
8893 }
8894 else if (child_die->tag == DW_TAG_inheritance)
8895 {
8896 /* C++ base class field. */
8897 dwarf2_add_field (&fi, child_die, cu);
8898 }
8899 else if (child_die->tag == DW_TAG_typedef)
8900 dwarf2_add_typedef (&fi, child_die, cu);
8901 else if (child_die->tag == DW_TAG_template_type_param
8902 || child_die->tag == DW_TAG_template_value_param)
8903 {
8904 struct symbol *arg = new_symbol (child_die, NULL, cu);
8905
8906 if (arg != NULL)
8907 VEC_safe_push (symbolp, template_args, arg);
8908 }
8909
8910 child_die = sibling_die (child_die);
8911 }
8912
8913 /* Attach template arguments to type. */
8914 if (! VEC_empty (symbolp, template_args))
8915 {
8916 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8917 TYPE_N_TEMPLATE_ARGUMENTS (type)
8918 = VEC_length (symbolp, template_args);
8919 TYPE_TEMPLATE_ARGUMENTS (type)
8920 = obstack_alloc (&objfile->objfile_obstack,
8921 (TYPE_N_TEMPLATE_ARGUMENTS (type)
8922 * sizeof (struct symbol *)));
8923 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
8924 VEC_address (symbolp, template_args),
8925 (TYPE_N_TEMPLATE_ARGUMENTS (type)
8926 * sizeof (struct symbol *)));
8927 VEC_free (symbolp, template_args);
8928 }
8929
8930 /* Attach fields and member functions to the type. */
8931 if (fi.nfields)
8932 dwarf2_attach_fields_to_type (&fi, type, cu);
8933 if (fi.nfnfields)
8934 {
8935 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
8936
8937 /* Get the type which refers to the base class (possibly this
8938 class itself) which contains the vtable pointer for the current
8939 class from the DW_AT_containing_type attribute. This use of
8940 DW_AT_containing_type is a GNU extension. */
8941
8942 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8943 {
8944 struct type *t = die_containing_type (die, cu);
8945
8946 TYPE_VPTR_BASETYPE (type) = t;
8947 if (type == t)
8948 {
8949 int i;
8950
8951 /* Our own class provides vtbl ptr. */
8952 for (i = TYPE_NFIELDS (t) - 1;
8953 i >= TYPE_N_BASECLASSES (t);
8954 --i)
8955 {
8956 const char *fieldname = TYPE_FIELD_NAME (t, i);
8957
8958 if (is_vtable_name (fieldname, cu))
8959 {
8960 TYPE_VPTR_FIELDNO (type) = i;
8961 break;
8962 }
8963 }
8964
8965 /* Complain if virtual function table field not found. */
8966 if (i < TYPE_N_BASECLASSES (t))
8967 complaint (&symfile_complaints,
8968 _("virtual function table pointer "
8969 "not found when defining class '%s'"),
8970 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
8971 "");
8972 }
8973 else
8974 {
8975 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
8976 }
8977 }
8978 else if (cu->producer
8979 && strncmp (cu->producer,
8980 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
8981 {
8982 /* The IBM XLC compiler does not provide direct indication
8983 of the containing type, but the vtable pointer is
8984 always named __vfp. */
8985
8986 int i;
8987
8988 for (i = TYPE_NFIELDS (type) - 1;
8989 i >= TYPE_N_BASECLASSES (type);
8990 --i)
8991 {
8992 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
8993 {
8994 TYPE_VPTR_FIELDNO (type) = i;
8995 TYPE_VPTR_BASETYPE (type) = type;
8996 break;
8997 }
8998 }
8999 }
9000 }
9001
9002 /* Copy fi.typedef_field_list linked list elements content into the
9003 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9004 if (fi.typedef_field_list)
9005 {
9006 int i = fi.typedef_field_list_count;
9007
9008 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9009 TYPE_TYPEDEF_FIELD_ARRAY (type)
9010 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9011 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9012
9013 /* Reverse the list order to keep the debug info elements order. */
9014 while (--i >= 0)
9015 {
9016 struct typedef_field *dest, *src;
9017
9018 dest = &TYPE_TYPEDEF_FIELD (type, i);
9019 src = &fi.typedef_field_list->field;
9020 fi.typedef_field_list = fi.typedef_field_list->next;
9021 *dest = *src;
9022 }
9023 }
9024
9025 do_cleanups (back_to);
9026
9027 if (HAVE_CPLUS_STRUCT (type))
9028 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9029 }
9030
9031 quirk_gcc_member_function_pointer (type, objfile);
9032
9033 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9034 snapshots) has been known to create a die giving a declaration
9035 for a class that has, as a child, a die giving a definition for a
9036 nested class. So we have to process our children even if the
9037 current die is a declaration. Normally, of course, a declaration
9038 won't have any children at all. */
9039
9040 while (child_die != NULL && child_die->tag)
9041 {
9042 if (child_die->tag == DW_TAG_member
9043 || child_die->tag == DW_TAG_variable
9044 || child_die->tag == DW_TAG_inheritance
9045 || child_die->tag == DW_TAG_template_value_param
9046 || child_die->tag == DW_TAG_template_type_param)
9047 {
9048 /* Do nothing. */
9049 }
9050 else
9051 process_die (child_die, cu);
9052
9053 child_die = sibling_die (child_die);
9054 }
9055
9056 /* Do not consider external references. According to the DWARF standard,
9057 these DIEs are identified by the fact that they have no byte_size
9058 attribute, and a declaration attribute. */
9059 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9060 || !die_is_declaration (die, cu))
9061 new_symbol (die, type, cu);
9062 }
9063
9064 /* Given a DW_AT_enumeration_type die, set its type. We do not
9065 complete the type's fields yet, or create any symbols. */
9066
9067 static struct type *
9068 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9069 {
9070 struct objfile *objfile = cu->objfile;
9071 struct type *type;
9072 struct attribute *attr;
9073 const char *name;
9074
9075 /* If the definition of this type lives in .debug_types, read that type.
9076 Don't follow DW_AT_specification though, that will take us back up
9077 the chain and we want to go down. */
9078 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9079 if (attr)
9080 {
9081 struct dwarf2_cu *type_cu = cu;
9082 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9083
9084 type = read_type_die (type_die, type_cu);
9085
9086 /* TYPE_CU may not be the same as CU.
9087 Ensure TYPE is recorded in CU's type_hash table. */
9088 return set_die_type (die, type, cu);
9089 }
9090
9091 type = alloc_type (objfile);
9092
9093 TYPE_CODE (type) = TYPE_CODE_ENUM;
9094 name = dwarf2_full_name (NULL, die, cu);
9095 if (name != NULL)
9096 TYPE_TAG_NAME (type) = (char *) name;
9097
9098 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9099 if (attr)
9100 {
9101 TYPE_LENGTH (type) = DW_UNSND (attr);
9102 }
9103 else
9104 {
9105 TYPE_LENGTH (type) = 0;
9106 }
9107
9108 /* The enumeration DIE can be incomplete. In Ada, any type can be
9109 declared as private in the package spec, and then defined only
9110 inside the package body. Such types are known as Taft Amendment
9111 Types. When another package uses such a type, an incomplete DIE
9112 may be generated by the compiler. */
9113 if (die_is_declaration (die, cu))
9114 TYPE_STUB (type) = 1;
9115
9116 return set_die_type (die, type, cu);
9117 }
9118
9119 /* Given a pointer to a die which begins an enumeration, process all
9120 the dies that define the members of the enumeration, and create the
9121 symbol for the enumeration type.
9122
9123 NOTE: We reverse the order of the element list. */
9124
9125 static void
9126 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9127 {
9128 struct type *this_type;
9129
9130 this_type = get_die_type (die, cu);
9131 if (this_type == NULL)
9132 this_type = read_enumeration_type (die, cu);
9133
9134 if (die->child != NULL)
9135 {
9136 struct die_info *child_die;
9137 struct symbol *sym;
9138 struct field *fields = NULL;
9139 int num_fields = 0;
9140 int unsigned_enum = 1;
9141 char *name;
9142 int flag_enum = 1;
9143 ULONGEST mask = 0;
9144
9145 child_die = die->child;
9146 while (child_die && child_die->tag)
9147 {
9148 if (child_die->tag != DW_TAG_enumerator)
9149 {
9150 process_die (child_die, cu);
9151 }
9152 else
9153 {
9154 name = dwarf2_name (child_die, cu);
9155 if (name)
9156 {
9157 sym = new_symbol (child_die, this_type, cu);
9158 if (SYMBOL_VALUE (sym) < 0)
9159 {
9160 unsigned_enum = 0;
9161 flag_enum = 0;
9162 }
9163 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9164 flag_enum = 0;
9165 else
9166 mask |= SYMBOL_VALUE (sym);
9167
9168 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9169 {
9170 fields = (struct field *)
9171 xrealloc (fields,
9172 (num_fields + DW_FIELD_ALLOC_CHUNK)
9173 * sizeof (struct field));
9174 }
9175
9176 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9177 FIELD_TYPE (fields[num_fields]) = NULL;
9178 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9179 FIELD_BITSIZE (fields[num_fields]) = 0;
9180
9181 num_fields++;
9182 }
9183 }
9184
9185 child_die = sibling_die (child_die);
9186 }
9187
9188 if (num_fields)
9189 {
9190 TYPE_NFIELDS (this_type) = num_fields;
9191 TYPE_FIELDS (this_type) = (struct field *)
9192 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9193 memcpy (TYPE_FIELDS (this_type), fields,
9194 sizeof (struct field) * num_fields);
9195 xfree (fields);
9196 }
9197 if (unsigned_enum)
9198 TYPE_UNSIGNED (this_type) = 1;
9199 if (flag_enum)
9200 TYPE_FLAG_ENUM (this_type) = 1;
9201 }
9202
9203 /* If we are reading an enum from a .debug_types unit, and the enum
9204 is a declaration, and the enum is not the signatured type in the
9205 unit, then we do not want to add a symbol for it. Adding a
9206 symbol would in some cases obscure the true definition of the
9207 enum, giving users an incomplete type when the definition is
9208 actually available. Note that we do not want to do this for all
9209 enums which are just declarations, because C++0x allows forward
9210 enum declarations. */
9211 if (cu->per_cu->is_debug_types
9212 && die_is_declaration (die, cu))
9213 {
9214 struct signatured_type *sig_type;
9215
9216 sig_type
9217 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9218 cu->per_cu->info_or_types_section,
9219 cu->per_cu->offset);
9220 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9221 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9222 return;
9223 }
9224
9225 new_symbol (die, this_type, cu);
9226 }
9227
9228 /* Extract all information from a DW_TAG_array_type DIE and put it in
9229 the DIE's type field. For now, this only handles one dimensional
9230 arrays. */
9231
9232 static struct type *
9233 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9234 {
9235 struct objfile *objfile = cu->objfile;
9236 struct die_info *child_die;
9237 struct type *type;
9238 struct type *element_type, *range_type, *index_type;
9239 struct type **range_types = NULL;
9240 struct attribute *attr;
9241 int ndim = 0;
9242 struct cleanup *back_to;
9243 char *name;
9244
9245 element_type = die_type (die, cu);
9246
9247 /* The die_type call above may have already set the type for this DIE. */
9248 type = get_die_type (die, cu);
9249 if (type)
9250 return type;
9251
9252 /* Irix 6.2 native cc creates array types without children for
9253 arrays with unspecified length. */
9254 if (die->child == NULL)
9255 {
9256 index_type = objfile_type (objfile)->builtin_int;
9257 range_type = create_range_type (NULL, index_type, 0, -1);
9258 type = create_array_type (NULL, element_type, range_type);
9259 return set_die_type (die, type, cu);
9260 }
9261
9262 back_to = make_cleanup (null_cleanup, NULL);
9263 child_die = die->child;
9264 while (child_die && child_die->tag)
9265 {
9266 if (child_die->tag == DW_TAG_subrange_type)
9267 {
9268 struct type *child_type = read_type_die (child_die, cu);
9269
9270 if (child_type != NULL)
9271 {
9272 /* The range type was succesfully read. Save it for the
9273 array type creation. */
9274 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9275 {
9276 range_types = (struct type **)
9277 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9278 * sizeof (struct type *));
9279 if (ndim == 0)
9280 make_cleanup (free_current_contents, &range_types);
9281 }
9282 range_types[ndim++] = child_type;
9283 }
9284 }
9285 child_die = sibling_die (child_die);
9286 }
9287
9288 /* Dwarf2 dimensions are output from left to right, create the
9289 necessary array types in backwards order. */
9290
9291 type = element_type;
9292
9293 if (read_array_order (die, cu) == DW_ORD_col_major)
9294 {
9295 int i = 0;
9296
9297 while (i < ndim)
9298 type = create_array_type (NULL, type, range_types[i++]);
9299 }
9300 else
9301 {
9302 while (ndim-- > 0)
9303 type = create_array_type (NULL, type, range_types[ndim]);
9304 }
9305
9306 /* Understand Dwarf2 support for vector types (like they occur on
9307 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9308 array type. This is not part of the Dwarf2/3 standard yet, but a
9309 custom vendor extension. The main difference between a regular
9310 array and the vector variant is that vectors are passed by value
9311 to functions. */
9312 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9313 if (attr)
9314 make_vector_type (type);
9315
9316 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9317 implementation may choose to implement triple vectors using this
9318 attribute. */
9319 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9320 if (attr)
9321 {
9322 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9323 TYPE_LENGTH (type) = DW_UNSND (attr);
9324 else
9325 complaint (&symfile_complaints,
9326 _("DW_AT_byte_size for array type smaller "
9327 "than the total size of elements"));
9328 }
9329
9330 name = dwarf2_name (die, cu);
9331 if (name)
9332 TYPE_NAME (type) = name;
9333
9334 /* Install the type in the die. */
9335 set_die_type (die, type, cu);
9336
9337 /* set_die_type should be already done. */
9338 set_descriptive_type (type, die, cu);
9339
9340 do_cleanups (back_to);
9341
9342 return type;
9343 }
9344
9345 static enum dwarf_array_dim_ordering
9346 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9347 {
9348 struct attribute *attr;
9349
9350 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9351
9352 if (attr) return DW_SND (attr);
9353
9354 /* GNU F77 is a special case, as at 08/2004 array type info is the
9355 opposite order to the dwarf2 specification, but data is still
9356 laid out as per normal fortran.
9357
9358 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9359 version checking. */
9360
9361 if (cu->language == language_fortran
9362 && cu->producer && strstr (cu->producer, "GNU F77"))
9363 {
9364 return DW_ORD_row_major;
9365 }
9366
9367 switch (cu->language_defn->la_array_ordering)
9368 {
9369 case array_column_major:
9370 return DW_ORD_col_major;
9371 case array_row_major:
9372 default:
9373 return DW_ORD_row_major;
9374 };
9375 }
9376
9377 /* Extract all information from a DW_TAG_set_type DIE and put it in
9378 the DIE's type field. */
9379
9380 static struct type *
9381 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9382 {
9383 struct type *domain_type, *set_type;
9384 struct attribute *attr;
9385
9386 domain_type = die_type (die, cu);
9387
9388 /* The die_type call above may have already set the type for this DIE. */
9389 set_type = get_die_type (die, cu);
9390 if (set_type)
9391 return set_type;
9392
9393 set_type = create_set_type (NULL, domain_type);
9394
9395 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9396 if (attr)
9397 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9398
9399 return set_die_type (die, set_type, cu);
9400 }
9401
9402 /* First cut: install each common block member as a global variable. */
9403
9404 static void
9405 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9406 {
9407 struct die_info *child_die;
9408 struct attribute *attr;
9409 struct symbol *sym;
9410 CORE_ADDR base = (CORE_ADDR) 0;
9411
9412 attr = dwarf2_attr (die, DW_AT_location, cu);
9413 if (attr)
9414 {
9415 /* Support the .debug_loc offsets. */
9416 if (attr_form_is_block (attr))
9417 {
9418 base = decode_locdesc (DW_BLOCK (attr), cu);
9419 }
9420 else if (attr_form_is_section_offset (attr))
9421 {
9422 dwarf2_complex_location_expr_complaint ();
9423 }
9424 else
9425 {
9426 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9427 "common block member");
9428 }
9429 }
9430 if (die->child != NULL)
9431 {
9432 child_die = die->child;
9433 while (child_die && child_die->tag)
9434 {
9435 LONGEST offset;
9436
9437 sym = new_symbol (child_die, NULL, cu);
9438 if (sym != NULL
9439 && handle_data_member_location (child_die, cu, &offset))
9440 {
9441 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9442 add_symbol_to_list (sym, &global_symbols);
9443 }
9444 child_die = sibling_die (child_die);
9445 }
9446 }
9447 }
9448
9449 /* Create a type for a C++ namespace. */
9450
9451 static struct type *
9452 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9453 {
9454 struct objfile *objfile = cu->objfile;
9455 const char *previous_prefix, *name;
9456 int is_anonymous;
9457 struct type *type;
9458
9459 /* For extensions, reuse the type of the original namespace. */
9460 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9461 {
9462 struct die_info *ext_die;
9463 struct dwarf2_cu *ext_cu = cu;
9464
9465 ext_die = dwarf2_extension (die, &ext_cu);
9466 type = read_type_die (ext_die, ext_cu);
9467
9468 /* EXT_CU may not be the same as CU.
9469 Ensure TYPE is recorded in CU's type_hash table. */
9470 return set_die_type (die, type, cu);
9471 }
9472
9473 name = namespace_name (die, &is_anonymous, cu);
9474
9475 /* Now build the name of the current namespace. */
9476
9477 previous_prefix = determine_prefix (die, cu);
9478 if (previous_prefix[0] != '\0')
9479 name = typename_concat (&objfile->objfile_obstack,
9480 previous_prefix, name, 0, cu);
9481
9482 /* Create the type. */
9483 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9484 objfile);
9485 TYPE_NAME (type) = (char *) name;
9486 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9487
9488 return set_die_type (die, type, cu);
9489 }
9490
9491 /* Read a C++ namespace. */
9492
9493 static void
9494 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9495 {
9496 struct objfile *objfile = cu->objfile;
9497 int is_anonymous;
9498
9499 /* Add a symbol associated to this if we haven't seen the namespace
9500 before. Also, add a using directive if it's an anonymous
9501 namespace. */
9502
9503 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
9504 {
9505 struct type *type;
9506
9507 type = read_type_die (die, cu);
9508 new_symbol (die, type, cu);
9509
9510 namespace_name (die, &is_anonymous, cu);
9511 if (is_anonymous)
9512 {
9513 const char *previous_prefix = determine_prefix (die, cu);
9514
9515 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
9516 NULL, NULL, &objfile->objfile_obstack);
9517 }
9518 }
9519
9520 if (die->child != NULL)
9521 {
9522 struct die_info *child_die = die->child;
9523
9524 while (child_die && child_die->tag)
9525 {
9526 process_die (child_die, cu);
9527 child_die = sibling_die (child_die);
9528 }
9529 }
9530 }
9531
9532 /* Read a Fortran module as type. This DIE can be only a declaration used for
9533 imported module. Still we need that type as local Fortran "use ... only"
9534 declaration imports depend on the created type in determine_prefix. */
9535
9536 static struct type *
9537 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9538 {
9539 struct objfile *objfile = cu->objfile;
9540 char *module_name;
9541 struct type *type;
9542
9543 module_name = dwarf2_name (die, cu);
9544 if (!module_name)
9545 complaint (&symfile_complaints,
9546 _("DW_TAG_module has no name, offset 0x%x"),
9547 die->offset.sect_off);
9548 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9549
9550 /* determine_prefix uses TYPE_TAG_NAME. */
9551 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9552
9553 return set_die_type (die, type, cu);
9554 }
9555
9556 /* Read a Fortran module. */
9557
9558 static void
9559 read_module (struct die_info *die, struct dwarf2_cu *cu)
9560 {
9561 struct die_info *child_die = die->child;
9562
9563 while (child_die && child_die->tag)
9564 {
9565 process_die (child_die, cu);
9566 child_die = sibling_die (child_die);
9567 }
9568 }
9569
9570 /* Return the name of the namespace represented by DIE. Set
9571 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9572 namespace. */
9573
9574 static const char *
9575 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
9576 {
9577 struct die_info *current_die;
9578 const char *name = NULL;
9579
9580 /* Loop through the extensions until we find a name. */
9581
9582 for (current_die = die;
9583 current_die != NULL;
9584 current_die = dwarf2_extension (die, &cu))
9585 {
9586 name = dwarf2_name (current_die, cu);
9587 if (name != NULL)
9588 break;
9589 }
9590
9591 /* Is it an anonymous namespace? */
9592
9593 *is_anonymous = (name == NULL);
9594 if (*is_anonymous)
9595 name = CP_ANONYMOUS_NAMESPACE_STR;
9596
9597 return name;
9598 }
9599
9600 /* Extract all information from a DW_TAG_pointer_type DIE and add to
9601 the user defined type vector. */
9602
9603 static struct type *
9604 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
9605 {
9606 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9607 struct comp_unit_head *cu_header = &cu->header;
9608 struct type *type;
9609 struct attribute *attr_byte_size;
9610 struct attribute *attr_address_class;
9611 int byte_size, addr_class;
9612 struct type *target_type;
9613
9614 target_type = die_type (die, cu);
9615
9616 /* The die_type call above may have already set the type for this DIE. */
9617 type = get_die_type (die, cu);
9618 if (type)
9619 return type;
9620
9621 type = lookup_pointer_type (target_type);
9622
9623 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
9624 if (attr_byte_size)
9625 byte_size = DW_UNSND (attr_byte_size);
9626 else
9627 byte_size = cu_header->addr_size;
9628
9629 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
9630 if (attr_address_class)
9631 addr_class = DW_UNSND (attr_address_class);
9632 else
9633 addr_class = DW_ADDR_none;
9634
9635 /* If the pointer size or address class is different than the
9636 default, create a type variant marked as such and set the
9637 length accordingly. */
9638 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
9639 {
9640 if (gdbarch_address_class_type_flags_p (gdbarch))
9641 {
9642 int type_flags;
9643
9644 type_flags = gdbarch_address_class_type_flags
9645 (gdbarch, byte_size, addr_class);
9646 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9647 == 0);
9648 type = make_type_with_address_space (type, type_flags);
9649 }
9650 else if (TYPE_LENGTH (type) != byte_size)
9651 {
9652 complaint (&symfile_complaints,
9653 _("invalid pointer size %d"), byte_size);
9654 }
9655 else
9656 {
9657 /* Should we also complain about unhandled address classes? */
9658 }
9659 }
9660
9661 TYPE_LENGTH (type) = byte_size;
9662 return set_die_type (die, type, cu);
9663 }
9664
9665 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9666 the user defined type vector. */
9667
9668 static struct type *
9669 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
9670 {
9671 struct type *type;
9672 struct type *to_type;
9673 struct type *domain;
9674
9675 to_type = die_type (die, cu);
9676 domain = die_containing_type (die, cu);
9677
9678 /* The calls above may have already set the type for this DIE. */
9679 type = get_die_type (die, cu);
9680 if (type)
9681 return type;
9682
9683 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9684 type = lookup_methodptr_type (to_type);
9685 else
9686 type = lookup_memberptr_type (to_type, domain);
9687
9688 return set_die_type (die, type, cu);
9689 }
9690
9691 /* Extract all information from a DW_TAG_reference_type DIE and add to
9692 the user defined type vector. */
9693
9694 static struct type *
9695 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
9696 {
9697 struct comp_unit_head *cu_header = &cu->header;
9698 struct type *type, *target_type;
9699 struct attribute *attr;
9700
9701 target_type = die_type (die, cu);
9702
9703 /* The die_type call above may have already set the type for this DIE. */
9704 type = get_die_type (die, cu);
9705 if (type)
9706 return type;
9707
9708 type = lookup_reference_type (target_type);
9709 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9710 if (attr)
9711 {
9712 TYPE_LENGTH (type) = DW_UNSND (attr);
9713 }
9714 else
9715 {
9716 TYPE_LENGTH (type) = cu_header->addr_size;
9717 }
9718 return set_die_type (die, type, cu);
9719 }
9720
9721 static struct type *
9722 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
9723 {
9724 struct type *base_type, *cv_type;
9725
9726 base_type = die_type (die, cu);
9727
9728 /* The die_type call above may have already set the type for this DIE. */
9729 cv_type = get_die_type (die, cu);
9730 if (cv_type)
9731 return cv_type;
9732
9733 /* In case the const qualifier is applied to an array type, the element type
9734 is so qualified, not the array type (section 6.7.3 of C99). */
9735 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
9736 {
9737 struct type *el_type, *inner_array;
9738
9739 base_type = copy_type (base_type);
9740 inner_array = base_type;
9741
9742 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
9743 {
9744 TYPE_TARGET_TYPE (inner_array) =
9745 copy_type (TYPE_TARGET_TYPE (inner_array));
9746 inner_array = TYPE_TARGET_TYPE (inner_array);
9747 }
9748
9749 el_type = TYPE_TARGET_TYPE (inner_array);
9750 TYPE_TARGET_TYPE (inner_array) =
9751 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
9752
9753 return set_die_type (die, base_type, cu);
9754 }
9755
9756 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
9757 return set_die_type (die, cv_type, cu);
9758 }
9759
9760 static struct type *
9761 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
9762 {
9763 struct type *base_type, *cv_type;
9764
9765 base_type = die_type (die, cu);
9766
9767 /* The die_type call above may have already set the type for this DIE. */
9768 cv_type = get_die_type (die, cu);
9769 if (cv_type)
9770 return cv_type;
9771
9772 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
9773 return set_die_type (die, cv_type, cu);
9774 }
9775
9776 /* Extract all information from a DW_TAG_string_type DIE and add to
9777 the user defined type vector. It isn't really a user defined type,
9778 but it behaves like one, with other DIE's using an AT_user_def_type
9779 attribute to reference it. */
9780
9781 static struct type *
9782 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
9783 {
9784 struct objfile *objfile = cu->objfile;
9785 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9786 struct type *type, *range_type, *index_type, *char_type;
9787 struct attribute *attr;
9788 unsigned int length;
9789
9790 attr = dwarf2_attr (die, DW_AT_string_length, cu);
9791 if (attr)
9792 {
9793 length = DW_UNSND (attr);
9794 }
9795 else
9796 {
9797 /* Check for the DW_AT_byte_size attribute. */
9798 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9799 if (attr)
9800 {
9801 length = DW_UNSND (attr);
9802 }
9803 else
9804 {
9805 length = 1;
9806 }
9807 }
9808
9809 index_type = objfile_type (objfile)->builtin_int;
9810 range_type = create_range_type (NULL, index_type, 1, length);
9811 char_type = language_string_char_type (cu->language_defn, gdbarch);
9812 type = create_string_type (NULL, char_type, range_type);
9813
9814 return set_die_type (die, type, cu);
9815 }
9816
9817 /* Handle DIES due to C code like:
9818
9819 struct foo
9820 {
9821 int (*funcp)(int a, long l);
9822 int b;
9823 };
9824
9825 ('funcp' generates a DW_TAG_subroutine_type DIE). */
9826
9827 static struct type *
9828 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
9829 {
9830 struct objfile *objfile = cu->objfile;
9831 struct type *type; /* Type that this function returns. */
9832 struct type *ftype; /* Function that returns above type. */
9833 struct attribute *attr;
9834
9835 type = die_type (die, cu);
9836
9837 /* The die_type call above may have already set the type for this DIE. */
9838 ftype = get_die_type (die, cu);
9839 if (ftype)
9840 return ftype;
9841
9842 ftype = lookup_function_type (type);
9843
9844 /* All functions in C++, Pascal and Java have prototypes. */
9845 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
9846 if ((attr && (DW_UNSND (attr) != 0))
9847 || cu->language == language_cplus
9848 || cu->language == language_java
9849 || cu->language == language_pascal)
9850 TYPE_PROTOTYPED (ftype) = 1;
9851 else if (producer_is_realview (cu->producer))
9852 /* RealView does not emit DW_AT_prototyped. We can not
9853 distinguish prototyped and unprototyped functions; default to
9854 prototyped, since that is more common in modern code (and
9855 RealView warns about unprototyped functions). */
9856 TYPE_PROTOTYPED (ftype) = 1;
9857
9858 /* Store the calling convention in the type if it's available in
9859 the subroutine die. Otherwise set the calling convention to
9860 the default value DW_CC_normal. */
9861 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
9862 if (attr)
9863 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
9864 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
9865 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
9866 else
9867 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
9868
9869 /* We need to add the subroutine type to the die immediately so
9870 we don't infinitely recurse when dealing with parameters
9871 declared as the same subroutine type. */
9872 set_die_type (die, ftype, cu);
9873
9874 if (die->child != NULL)
9875 {
9876 struct type *void_type = objfile_type (objfile)->builtin_void;
9877 struct die_info *child_die;
9878 int nparams, iparams;
9879
9880 /* Count the number of parameters.
9881 FIXME: GDB currently ignores vararg functions, but knows about
9882 vararg member functions. */
9883 nparams = 0;
9884 child_die = die->child;
9885 while (child_die && child_die->tag)
9886 {
9887 if (child_die->tag == DW_TAG_formal_parameter)
9888 nparams++;
9889 else if (child_die->tag == DW_TAG_unspecified_parameters)
9890 TYPE_VARARGS (ftype) = 1;
9891 child_die = sibling_die (child_die);
9892 }
9893
9894 /* Allocate storage for parameters and fill them in. */
9895 TYPE_NFIELDS (ftype) = nparams;
9896 TYPE_FIELDS (ftype) = (struct field *)
9897 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
9898
9899 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
9900 even if we error out during the parameters reading below. */
9901 for (iparams = 0; iparams < nparams; iparams++)
9902 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
9903
9904 iparams = 0;
9905 child_die = die->child;
9906 while (child_die && child_die->tag)
9907 {
9908 if (child_die->tag == DW_TAG_formal_parameter)
9909 {
9910 struct type *arg_type;
9911
9912 /* DWARF version 2 has no clean way to discern C++
9913 static and non-static member functions. G++ helps
9914 GDB by marking the first parameter for non-static
9915 member functions (which is the this pointer) as
9916 artificial. We pass this information to
9917 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
9918
9919 DWARF version 3 added DW_AT_object_pointer, which GCC
9920 4.5 does not yet generate. */
9921 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
9922 if (attr)
9923 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
9924 else
9925 {
9926 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
9927
9928 /* GCC/43521: In java, the formal parameter
9929 "this" is sometimes not marked with DW_AT_artificial. */
9930 if (cu->language == language_java)
9931 {
9932 const char *name = dwarf2_name (child_die, cu);
9933
9934 if (name && !strcmp (name, "this"))
9935 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
9936 }
9937 }
9938 arg_type = die_type (child_die, cu);
9939
9940 /* RealView does not mark THIS as const, which the testsuite
9941 expects. GCC marks THIS as const in method definitions,
9942 but not in the class specifications (GCC PR 43053). */
9943 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
9944 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
9945 {
9946 int is_this = 0;
9947 struct dwarf2_cu *arg_cu = cu;
9948 const char *name = dwarf2_name (child_die, cu);
9949
9950 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
9951 if (attr)
9952 {
9953 /* If the compiler emits this, use it. */
9954 if (follow_die_ref (die, attr, &arg_cu) == child_die)
9955 is_this = 1;
9956 }
9957 else if (name && strcmp (name, "this") == 0)
9958 /* Function definitions will have the argument names. */
9959 is_this = 1;
9960 else if (name == NULL && iparams == 0)
9961 /* Declarations may not have the names, so like
9962 elsewhere in GDB, assume an artificial first
9963 argument is "this". */
9964 is_this = 1;
9965
9966 if (is_this)
9967 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
9968 arg_type, 0);
9969 }
9970
9971 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
9972 iparams++;
9973 }
9974 child_die = sibling_die (child_die);
9975 }
9976 }
9977
9978 return ftype;
9979 }
9980
9981 static struct type *
9982 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
9983 {
9984 struct objfile *objfile = cu->objfile;
9985 const char *name = NULL;
9986 struct type *this_type, *target_type;
9987
9988 name = dwarf2_full_name (NULL, die, cu);
9989 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
9990 TYPE_FLAG_TARGET_STUB, NULL, objfile);
9991 TYPE_NAME (this_type) = (char *) name;
9992 set_die_type (die, this_type, cu);
9993 target_type = die_type (die, cu);
9994 if (target_type != this_type)
9995 TYPE_TARGET_TYPE (this_type) = target_type;
9996 else
9997 {
9998 /* Self-referential typedefs are, it seems, not allowed by the DWARF
9999 spec and cause infinite loops in GDB. */
10000 complaint (&symfile_complaints,
10001 _("Self-referential DW_TAG_typedef "
10002 "- DIE at 0x%x [in module %s]"),
10003 die->offset.sect_off, objfile->name);
10004 TYPE_TARGET_TYPE (this_type) = NULL;
10005 }
10006 return this_type;
10007 }
10008
10009 /* Find a representation of a given base type and install
10010 it in the TYPE field of the die. */
10011
10012 static struct type *
10013 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10014 {
10015 struct objfile *objfile = cu->objfile;
10016 struct type *type;
10017 struct attribute *attr;
10018 int encoding = 0, size = 0;
10019 char *name;
10020 enum type_code code = TYPE_CODE_INT;
10021 int type_flags = 0;
10022 struct type *target_type = NULL;
10023
10024 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10025 if (attr)
10026 {
10027 encoding = DW_UNSND (attr);
10028 }
10029 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10030 if (attr)
10031 {
10032 size = DW_UNSND (attr);
10033 }
10034 name = dwarf2_name (die, cu);
10035 if (!name)
10036 {
10037 complaint (&symfile_complaints,
10038 _("DW_AT_name missing from DW_TAG_base_type"));
10039 }
10040
10041 switch (encoding)
10042 {
10043 case DW_ATE_address:
10044 /* Turn DW_ATE_address into a void * pointer. */
10045 code = TYPE_CODE_PTR;
10046 type_flags |= TYPE_FLAG_UNSIGNED;
10047 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10048 break;
10049 case DW_ATE_boolean:
10050 code = TYPE_CODE_BOOL;
10051 type_flags |= TYPE_FLAG_UNSIGNED;
10052 break;
10053 case DW_ATE_complex_float:
10054 code = TYPE_CODE_COMPLEX;
10055 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10056 break;
10057 case DW_ATE_decimal_float:
10058 code = TYPE_CODE_DECFLOAT;
10059 break;
10060 case DW_ATE_float:
10061 code = TYPE_CODE_FLT;
10062 break;
10063 case DW_ATE_signed:
10064 break;
10065 case DW_ATE_unsigned:
10066 type_flags |= TYPE_FLAG_UNSIGNED;
10067 if (cu->language == language_fortran
10068 && name
10069 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10070 code = TYPE_CODE_CHAR;
10071 break;
10072 case DW_ATE_signed_char:
10073 if (cu->language == language_ada || cu->language == language_m2
10074 || cu->language == language_pascal
10075 || cu->language == language_fortran)
10076 code = TYPE_CODE_CHAR;
10077 break;
10078 case DW_ATE_unsigned_char:
10079 if (cu->language == language_ada || cu->language == language_m2
10080 || cu->language == language_pascal
10081 || cu->language == language_fortran)
10082 code = TYPE_CODE_CHAR;
10083 type_flags |= TYPE_FLAG_UNSIGNED;
10084 break;
10085 case DW_ATE_UTF:
10086 /* We just treat this as an integer and then recognize the
10087 type by name elsewhere. */
10088 break;
10089
10090 default:
10091 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10092 dwarf_type_encoding_name (encoding));
10093 break;
10094 }
10095
10096 type = init_type (code, size, type_flags, NULL, objfile);
10097 TYPE_NAME (type) = name;
10098 TYPE_TARGET_TYPE (type) = target_type;
10099
10100 if (name && strcmp (name, "char") == 0)
10101 TYPE_NOSIGN (type) = 1;
10102
10103 return set_die_type (die, type, cu);
10104 }
10105
10106 /* Read the given DW_AT_subrange DIE. */
10107
10108 static struct type *
10109 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10110 {
10111 struct type *base_type;
10112 struct type *range_type;
10113 struct attribute *attr;
10114 LONGEST low, high;
10115 int low_default_is_valid;
10116 char *name;
10117 LONGEST negative_mask;
10118
10119 base_type = die_type (die, cu);
10120 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10121 check_typedef (base_type);
10122
10123 /* The die_type call above may have already set the type for this DIE. */
10124 range_type = get_die_type (die, cu);
10125 if (range_type)
10126 return range_type;
10127
10128 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10129 omitting DW_AT_lower_bound. */
10130 switch (cu->language)
10131 {
10132 case language_c:
10133 case language_cplus:
10134 low = 0;
10135 low_default_is_valid = 1;
10136 break;
10137 case language_fortran:
10138 low = 1;
10139 low_default_is_valid = 1;
10140 break;
10141 case language_d:
10142 case language_java:
10143 case language_objc:
10144 low = 0;
10145 low_default_is_valid = (cu->header.version >= 4);
10146 break;
10147 case language_ada:
10148 case language_m2:
10149 case language_pascal:
10150 low = 1;
10151 low_default_is_valid = (cu->header.version >= 4);
10152 break;
10153 default:
10154 low = 0;
10155 low_default_is_valid = 0;
10156 break;
10157 }
10158
10159 /* FIXME: For variable sized arrays either of these could be
10160 a variable rather than a constant value. We'll allow it,
10161 but we don't know how to handle it. */
10162 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10163 if (attr)
10164 low = dwarf2_get_attr_constant_value (attr, low);
10165 else if (!low_default_is_valid)
10166 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10167 "- DIE at 0x%x [in module %s]"),
10168 die->offset.sect_off, cu->objfile->name);
10169
10170 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10171 if (attr)
10172 {
10173 if (attr_form_is_block (attr) || is_ref_attr (attr))
10174 {
10175 /* GCC encodes arrays with unspecified or dynamic length
10176 with a DW_FORM_block1 attribute or a reference attribute.
10177 FIXME: GDB does not yet know how to handle dynamic
10178 arrays properly, treat them as arrays with unspecified
10179 length for now.
10180
10181 FIXME: jimb/2003-09-22: GDB does not really know
10182 how to handle arrays of unspecified length
10183 either; we just represent them as zero-length
10184 arrays. Choose an appropriate upper bound given
10185 the lower bound we've computed above. */
10186 high = low - 1;
10187 }
10188 else
10189 high = dwarf2_get_attr_constant_value (attr, 1);
10190 }
10191 else
10192 {
10193 attr = dwarf2_attr (die, DW_AT_count, cu);
10194 if (attr)
10195 {
10196 int count = dwarf2_get_attr_constant_value (attr, 1);
10197 high = low + count - 1;
10198 }
10199 else
10200 {
10201 /* Unspecified array length. */
10202 high = low - 1;
10203 }
10204 }
10205
10206 /* Dwarf-2 specifications explicitly allows to create subrange types
10207 without specifying a base type.
10208 In that case, the base type must be set to the type of
10209 the lower bound, upper bound or count, in that order, if any of these
10210 three attributes references an object that has a type.
10211 If no base type is found, the Dwarf-2 specifications say that
10212 a signed integer type of size equal to the size of an address should
10213 be used.
10214 For the following C code: `extern char gdb_int [];'
10215 GCC produces an empty range DIE.
10216 FIXME: muller/2010-05-28: Possible references to object for low bound,
10217 high bound or count are not yet handled by this code. */
10218 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10219 {
10220 struct objfile *objfile = cu->objfile;
10221 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10222 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10223 struct type *int_type = objfile_type (objfile)->builtin_int;
10224
10225 /* Test "int", "long int", and "long long int" objfile types,
10226 and select the first one having a size above or equal to the
10227 architecture address size. */
10228 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10229 base_type = int_type;
10230 else
10231 {
10232 int_type = objfile_type (objfile)->builtin_long;
10233 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10234 base_type = int_type;
10235 else
10236 {
10237 int_type = objfile_type (objfile)->builtin_long_long;
10238 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10239 base_type = int_type;
10240 }
10241 }
10242 }
10243
10244 negative_mask =
10245 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10246 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10247 low |= negative_mask;
10248 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10249 high |= negative_mask;
10250
10251 range_type = create_range_type (NULL, base_type, low, high);
10252
10253 /* Mark arrays with dynamic length at least as an array of unspecified
10254 length. GDB could check the boundary but before it gets implemented at
10255 least allow accessing the array elements. */
10256 if (attr && attr_form_is_block (attr))
10257 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10258
10259 /* Ada expects an empty array on no boundary attributes. */
10260 if (attr == NULL && cu->language != language_ada)
10261 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10262
10263 name = dwarf2_name (die, cu);
10264 if (name)
10265 TYPE_NAME (range_type) = name;
10266
10267 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10268 if (attr)
10269 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10270
10271 set_die_type (die, range_type, cu);
10272
10273 /* set_die_type should be already done. */
10274 set_descriptive_type (range_type, die, cu);
10275
10276 return range_type;
10277 }
10278
10279 static struct type *
10280 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10281 {
10282 struct type *type;
10283
10284 /* For now, we only support the C meaning of an unspecified type: void. */
10285
10286 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10287 TYPE_NAME (type) = dwarf2_name (die, cu);
10288
10289 return set_die_type (die, type, cu);
10290 }
10291
10292 /* Read a single die and all its descendents. Set the die's sibling
10293 field to NULL; set other fields in the die correctly, and set all
10294 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10295 location of the info_ptr after reading all of those dies. PARENT
10296 is the parent of the die in question. */
10297
10298 static struct die_info *
10299 read_die_and_children (const struct die_reader_specs *reader,
10300 gdb_byte *info_ptr,
10301 gdb_byte **new_info_ptr,
10302 struct die_info *parent)
10303 {
10304 struct die_info *die;
10305 gdb_byte *cur_ptr;
10306 int has_children;
10307
10308 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10309 if (die == NULL)
10310 {
10311 *new_info_ptr = cur_ptr;
10312 return NULL;
10313 }
10314 store_in_ref_table (die, reader->cu);
10315
10316 if (has_children)
10317 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10318 else
10319 {
10320 die->child = NULL;
10321 *new_info_ptr = cur_ptr;
10322 }
10323
10324 die->sibling = NULL;
10325 die->parent = parent;
10326 return die;
10327 }
10328
10329 /* Read a die, all of its descendents, and all of its siblings; set
10330 all of the fields of all of the dies correctly. Arguments are as
10331 in read_die_and_children. */
10332
10333 static struct die_info *
10334 read_die_and_siblings (const struct die_reader_specs *reader,
10335 gdb_byte *info_ptr,
10336 gdb_byte **new_info_ptr,
10337 struct die_info *parent)
10338 {
10339 struct die_info *first_die, *last_sibling;
10340 gdb_byte *cur_ptr;
10341
10342 cur_ptr = info_ptr;
10343 first_die = last_sibling = NULL;
10344
10345 while (1)
10346 {
10347 struct die_info *die
10348 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10349
10350 if (die == NULL)
10351 {
10352 *new_info_ptr = cur_ptr;
10353 return first_die;
10354 }
10355
10356 if (!first_die)
10357 first_die = die;
10358 else
10359 last_sibling->sibling = die;
10360
10361 last_sibling = die;
10362 }
10363 }
10364
10365 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10366 attributes.
10367 The caller is responsible for filling in the extra attributes
10368 and updating (*DIEP)->num_attrs.
10369 Set DIEP to point to a newly allocated die with its information,
10370 except for its child, sibling, and parent fields.
10371 Set HAS_CHILDREN to tell whether the die has children or not. */
10372
10373 static gdb_byte *
10374 read_full_die_1 (const struct die_reader_specs *reader,
10375 struct die_info **diep, gdb_byte *info_ptr,
10376 int *has_children, int num_extra_attrs)
10377 {
10378 unsigned int abbrev_number, bytes_read, i;
10379 sect_offset offset;
10380 struct abbrev_info *abbrev;
10381 struct die_info *die;
10382 struct dwarf2_cu *cu = reader->cu;
10383 bfd *abfd = reader->abfd;
10384
10385 offset.sect_off = info_ptr - reader->buffer;
10386 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10387 info_ptr += bytes_read;
10388 if (!abbrev_number)
10389 {
10390 *diep = NULL;
10391 *has_children = 0;
10392 return info_ptr;
10393 }
10394
10395 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10396 if (!abbrev)
10397 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10398 abbrev_number,
10399 bfd_get_filename (abfd));
10400
10401 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10402 die->offset = offset;
10403 die->tag = abbrev->tag;
10404 die->abbrev = abbrev_number;
10405
10406 /* Make the result usable.
10407 The caller needs to update num_attrs after adding the extra
10408 attributes. */
10409 die->num_attrs = abbrev->num_attrs;
10410
10411 for (i = 0; i < abbrev->num_attrs; ++i)
10412 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10413 info_ptr);
10414
10415 *diep = die;
10416 *has_children = abbrev->has_children;
10417 return info_ptr;
10418 }
10419
10420 /* Read a die and all its attributes.
10421 Set DIEP to point to a newly allocated die with its information,
10422 except for its child, sibling, and parent fields.
10423 Set HAS_CHILDREN to tell whether the die has children or not. */
10424
10425 static gdb_byte *
10426 read_full_die (const struct die_reader_specs *reader,
10427 struct die_info **diep, gdb_byte *info_ptr,
10428 int *has_children)
10429 {
10430 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10431 }
10432
10433 /* In DWARF version 2, the description of the debugging information is
10434 stored in a separate .debug_abbrev section. Before we read any
10435 dies from a section we read in all abbreviations and install them
10436 in a hash table. This function also sets flags in CU describing
10437 the data found in the abbrev table. */
10438
10439 static void
10440 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10441 struct dwarf2_section_info *abbrev_section)
10442
10443 {
10444 bfd *abfd = abbrev_section->asection->owner;
10445 struct comp_unit_head *cu_header = &cu->header;
10446 gdb_byte *abbrev_ptr;
10447 struct abbrev_info *cur_abbrev;
10448 unsigned int abbrev_number, bytes_read, abbrev_name;
10449 unsigned int abbrev_form, hash_number;
10450 struct attr_abbrev *cur_attrs;
10451 unsigned int allocated_attrs;
10452
10453 /* Initialize dwarf2 abbrevs. */
10454 obstack_init (&cu->abbrev_obstack);
10455 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10456 (ABBREV_HASH_SIZE
10457 * sizeof (struct abbrev_info *)));
10458 memset (cu->dwarf2_abbrevs, 0,
10459 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10460
10461 dwarf2_read_section (cu->objfile, abbrev_section);
10462 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10463 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10464 abbrev_ptr += bytes_read;
10465
10466 allocated_attrs = ATTR_ALLOC_CHUNK;
10467 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10468
10469 /* Loop until we reach an abbrev number of 0. */
10470 while (abbrev_number)
10471 {
10472 cur_abbrev = dwarf_alloc_abbrev (cu);
10473
10474 /* read in abbrev header */
10475 cur_abbrev->number = abbrev_number;
10476 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10477 abbrev_ptr += bytes_read;
10478 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10479 abbrev_ptr += 1;
10480
10481 /* now read in declarations */
10482 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10483 abbrev_ptr += bytes_read;
10484 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10485 abbrev_ptr += bytes_read;
10486 while (abbrev_name)
10487 {
10488 if (cur_abbrev->num_attrs == allocated_attrs)
10489 {
10490 allocated_attrs += ATTR_ALLOC_CHUNK;
10491 cur_attrs
10492 = xrealloc (cur_attrs, (allocated_attrs
10493 * sizeof (struct attr_abbrev)));
10494 }
10495
10496 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10497 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
10498 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10499 abbrev_ptr += bytes_read;
10500 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10501 abbrev_ptr += bytes_read;
10502 }
10503
10504 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10505 (cur_abbrev->num_attrs
10506 * sizeof (struct attr_abbrev)));
10507 memcpy (cur_abbrev->attrs, cur_attrs,
10508 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10509
10510 hash_number = abbrev_number % ABBREV_HASH_SIZE;
10511 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10512 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
10513
10514 /* Get next abbreviation.
10515 Under Irix6 the abbreviations for a compilation unit are not
10516 always properly terminated with an abbrev number of 0.
10517 Exit loop if we encounter an abbreviation which we have
10518 already read (which means we are about to read the abbreviations
10519 for the next compile unit) or if the end of the abbreviation
10520 table is reached. */
10521 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10522 >= abbrev_section->size)
10523 break;
10524 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10525 abbrev_ptr += bytes_read;
10526 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
10527 break;
10528 }
10529
10530 xfree (cur_attrs);
10531 }
10532
10533 /* Release the memory used by the abbrev table for a compilation unit. */
10534
10535 static void
10536 dwarf2_free_abbrev_table (void *ptr_to_cu)
10537 {
10538 struct dwarf2_cu *cu = ptr_to_cu;
10539
10540 obstack_free (&cu->abbrev_obstack, NULL);
10541 cu->dwarf2_abbrevs = NULL;
10542 }
10543
10544 /* Lookup an abbrev_info structure in the abbrev hash table. */
10545
10546 static struct abbrev_info *
10547 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
10548 {
10549 unsigned int hash_number;
10550 struct abbrev_info *abbrev;
10551
10552 hash_number = number % ABBREV_HASH_SIZE;
10553 abbrev = cu->dwarf2_abbrevs[hash_number];
10554
10555 while (abbrev)
10556 {
10557 if (abbrev->number == number)
10558 return abbrev;
10559 else
10560 abbrev = abbrev->next;
10561 }
10562 return NULL;
10563 }
10564
10565 /* Returns nonzero if TAG represents a type that we might generate a partial
10566 symbol for. */
10567
10568 static int
10569 is_type_tag_for_partial (int tag)
10570 {
10571 switch (tag)
10572 {
10573 #if 0
10574 /* Some types that would be reasonable to generate partial symbols for,
10575 that we don't at present. */
10576 case DW_TAG_array_type:
10577 case DW_TAG_file_type:
10578 case DW_TAG_ptr_to_member_type:
10579 case DW_TAG_set_type:
10580 case DW_TAG_string_type:
10581 case DW_TAG_subroutine_type:
10582 #endif
10583 case DW_TAG_base_type:
10584 case DW_TAG_class_type:
10585 case DW_TAG_interface_type:
10586 case DW_TAG_enumeration_type:
10587 case DW_TAG_structure_type:
10588 case DW_TAG_subrange_type:
10589 case DW_TAG_typedef:
10590 case DW_TAG_union_type:
10591 return 1;
10592 default:
10593 return 0;
10594 }
10595 }
10596
10597 /* Load all DIEs that are interesting for partial symbols into memory. */
10598
10599 static struct partial_die_info *
10600 load_partial_dies (const struct die_reader_specs *reader,
10601 gdb_byte *info_ptr, int building_psymtab)
10602 {
10603 struct dwarf2_cu *cu = reader->cu;
10604 struct objfile *objfile = cu->objfile;
10605 struct partial_die_info *part_die;
10606 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10607 struct abbrev_info *abbrev;
10608 unsigned int bytes_read;
10609 unsigned int load_all = 0;
10610 int nesting_level = 1;
10611
10612 parent_die = NULL;
10613 last_die = NULL;
10614
10615 gdb_assert (cu->per_cu != NULL);
10616 if (cu->per_cu->load_all_dies)
10617 load_all = 1;
10618
10619 cu->partial_dies
10620 = htab_create_alloc_ex (cu->header.length / 12,
10621 partial_die_hash,
10622 partial_die_eq,
10623 NULL,
10624 &cu->comp_unit_obstack,
10625 hashtab_obstack_allocate,
10626 dummy_obstack_deallocate);
10627
10628 part_die = obstack_alloc (&cu->comp_unit_obstack,
10629 sizeof (struct partial_die_info));
10630
10631 while (1)
10632 {
10633 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10634
10635 /* A NULL abbrev means the end of a series of children. */
10636 if (abbrev == NULL)
10637 {
10638 if (--nesting_level == 0)
10639 {
10640 /* PART_DIE was probably the last thing allocated on the
10641 comp_unit_obstack, so we could call obstack_free
10642 here. We don't do that because the waste is small,
10643 and will be cleaned up when we're done with this
10644 compilation unit. This way, we're also more robust
10645 against other users of the comp_unit_obstack. */
10646 return first_die;
10647 }
10648 info_ptr += bytes_read;
10649 last_die = parent_die;
10650 parent_die = parent_die->die_parent;
10651 continue;
10652 }
10653
10654 /* Check for template arguments. We never save these; if
10655 they're seen, we just mark the parent, and go on our way. */
10656 if (parent_die != NULL
10657 && cu->language == language_cplus
10658 && (abbrev->tag == DW_TAG_template_type_param
10659 || abbrev->tag == DW_TAG_template_value_param))
10660 {
10661 parent_die->has_template_arguments = 1;
10662
10663 if (!load_all)
10664 {
10665 /* We don't need a partial DIE for the template argument. */
10666 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10667 continue;
10668 }
10669 }
10670
10671 /* We only recurse into c++ subprograms looking for template arguments.
10672 Skip their other children. */
10673 if (!load_all
10674 && cu->language == language_cplus
10675 && parent_die != NULL
10676 && parent_die->tag == DW_TAG_subprogram)
10677 {
10678 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10679 continue;
10680 }
10681
10682 /* Check whether this DIE is interesting enough to save. Normally
10683 we would not be interested in members here, but there may be
10684 later variables referencing them via DW_AT_specification (for
10685 static members). */
10686 if (!load_all
10687 && !is_type_tag_for_partial (abbrev->tag)
10688 && abbrev->tag != DW_TAG_constant
10689 && abbrev->tag != DW_TAG_enumerator
10690 && abbrev->tag != DW_TAG_subprogram
10691 && abbrev->tag != DW_TAG_lexical_block
10692 && abbrev->tag != DW_TAG_variable
10693 && abbrev->tag != DW_TAG_namespace
10694 && abbrev->tag != DW_TAG_module
10695 && abbrev->tag != DW_TAG_member)
10696 {
10697 /* Otherwise we skip to the next sibling, if any. */
10698 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10699 continue;
10700 }
10701
10702 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10703 info_ptr);
10704
10705 /* This two-pass algorithm for processing partial symbols has a
10706 high cost in cache pressure. Thus, handle some simple cases
10707 here which cover the majority of C partial symbols. DIEs
10708 which neither have specification tags in them, nor could have
10709 specification tags elsewhere pointing at them, can simply be
10710 processed and discarded.
10711
10712 This segment is also optional; scan_partial_symbols and
10713 add_partial_symbol will handle these DIEs if we chain
10714 them in normally. When compilers which do not emit large
10715 quantities of duplicate debug information are more common,
10716 this code can probably be removed. */
10717
10718 /* Any complete simple types at the top level (pretty much all
10719 of them, for a language without namespaces), can be processed
10720 directly. */
10721 if (parent_die == NULL
10722 && part_die->has_specification == 0
10723 && part_die->is_declaration == 0
10724 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
10725 || part_die->tag == DW_TAG_base_type
10726 || part_die->tag == DW_TAG_subrange_type))
10727 {
10728 if (building_psymtab && part_die->name != NULL)
10729 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
10730 VAR_DOMAIN, LOC_TYPEDEF,
10731 &objfile->static_psymbols,
10732 0, (CORE_ADDR) 0, cu->language, objfile);
10733 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
10734 continue;
10735 }
10736
10737 /* The exception for DW_TAG_typedef with has_children above is
10738 a workaround of GCC PR debug/47510. In the case of this complaint
10739 type_name_no_tag_or_error will error on such types later.
10740
10741 GDB skipped children of DW_TAG_typedef by the shortcut above and then
10742 it could not find the child DIEs referenced later, this is checked
10743 above. In correct DWARF DW_TAG_typedef should have no children. */
10744
10745 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
10746 complaint (&symfile_complaints,
10747 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
10748 "- DIE at 0x%x [in module %s]"),
10749 part_die->offset.sect_off, objfile->name);
10750
10751 /* If we're at the second level, and we're an enumerator, and
10752 our parent has no specification (meaning possibly lives in a
10753 namespace elsewhere), then we can add the partial symbol now
10754 instead of queueing it. */
10755 if (part_die->tag == DW_TAG_enumerator
10756 && parent_die != NULL
10757 && parent_die->die_parent == NULL
10758 && parent_die->tag == DW_TAG_enumeration_type
10759 && parent_die->has_specification == 0)
10760 {
10761 if (part_die->name == NULL)
10762 complaint (&symfile_complaints,
10763 _("malformed enumerator DIE ignored"));
10764 else if (building_psymtab)
10765 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
10766 VAR_DOMAIN, LOC_CONST,
10767 (cu->language == language_cplus
10768 || cu->language == language_java)
10769 ? &objfile->global_psymbols
10770 : &objfile->static_psymbols,
10771 0, (CORE_ADDR) 0, cu->language, objfile);
10772
10773 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
10774 continue;
10775 }
10776
10777 /* We'll save this DIE so link it in. */
10778 part_die->die_parent = parent_die;
10779 part_die->die_sibling = NULL;
10780 part_die->die_child = NULL;
10781
10782 if (last_die && last_die == parent_die)
10783 last_die->die_child = part_die;
10784 else if (last_die)
10785 last_die->die_sibling = part_die;
10786
10787 last_die = part_die;
10788
10789 if (first_die == NULL)
10790 first_die = part_die;
10791
10792 /* Maybe add the DIE to the hash table. Not all DIEs that we
10793 find interesting need to be in the hash table, because we
10794 also have the parent/sibling/child chains; only those that we
10795 might refer to by offset later during partial symbol reading.
10796
10797 For now this means things that might have be the target of a
10798 DW_AT_specification, DW_AT_abstract_origin, or
10799 DW_AT_extension. DW_AT_extension will refer only to
10800 namespaces; DW_AT_abstract_origin refers to functions (and
10801 many things under the function DIE, but we do not recurse
10802 into function DIEs during partial symbol reading) and
10803 possibly variables as well; DW_AT_specification refers to
10804 declarations. Declarations ought to have the DW_AT_declaration
10805 flag. It happens that GCC forgets to put it in sometimes, but
10806 only for functions, not for types.
10807
10808 Adding more things than necessary to the hash table is harmless
10809 except for the performance cost. Adding too few will result in
10810 wasted time in find_partial_die, when we reread the compilation
10811 unit with load_all_dies set. */
10812
10813 if (load_all
10814 || abbrev->tag == DW_TAG_constant
10815 || abbrev->tag == DW_TAG_subprogram
10816 || abbrev->tag == DW_TAG_variable
10817 || abbrev->tag == DW_TAG_namespace
10818 || part_die->is_declaration)
10819 {
10820 void **slot;
10821
10822 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
10823 part_die->offset.sect_off, INSERT);
10824 *slot = part_die;
10825 }
10826
10827 part_die = obstack_alloc (&cu->comp_unit_obstack,
10828 sizeof (struct partial_die_info));
10829
10830 /* For some DIEs we want to follow their children (if any). For C
10831 we have no reason to follow the children of structures; for other
10832 languages we have to, so that we can get at method physnames
10833 to infer fully qualified class names, for DW_AT_specification,
10834 and for C++ template arguments. For C++, we also look one level
10835 inside functions to find template arguments (if the name of the
10836 function does not already contain the template arguments).
10837
10838 For Ada, we need to scan the children of subprograms and lexical
10839 blocks as well because Ada allows the definition of nested
10840 entities that could be interesting for the debugger, such as
10841 nested subprograms for instance. */
10842 if (last_die->has_children
10843 && (load_all
10844 || last_die->tag == DW_TAG_namespace
10845 || last_die->tag == DW_TAG_module
10846 || last_die->tag == DW_TAG_enumeration_type
10847 || (cu->language == language_cplus
10848 && last_die->tag == DW_TAG_subprogram
10849 && (last_die->name == NULL
10850 || strchr (last_die->name, '<') == NULL))
10851 || (cu->language != language_c
10852 && (last_die->tag == DW_TAG_class_type
10853 || last_die->tag == DW_TAG_interface_type
10854 || last_die->tag == DW_TAG_structure_type
10855 || last_die->tag == DW_TAG_union_type))
10856 || (cu->language == language_ada
10857 && (last_die->tag == DW_TAG_subprogram
10858 || last_die->tag == DW_TAG_lexical_block))))
10859 {
10860 nesting_level++;
10861 parent_die = last_die;
10862 continue;
10863 }
10864
10865 /* Otherwise we skip to the next sibling, if any. */
10866 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
10867
10868 /* Back to the top, do it again. */
10869 }
10870 }
10871
10872 /* Read a minimal amount of information into the minimal die structure. */
10873
10874 static gdb_byte *
10875 read_partial_die (const struct die_reader_specs *reader,
10876 struct partial_die_info *part_die,
10877 struct abbrev_info *abbrev, unsigned int abbrev_len,
10878 gdb_byte *info_ptr)
10879 {
10880 struct dwarf2_cu *cu = reader->cu;
10881 struct objfile *objfile = cu->objfile;
10882 gdb_byte *buffer = reader->buffer;
10883 unsigned int i;
10884 struct attribute attr;
10885 int has_low_pc_attr = 0;
10886 int has_high_pc_attr = 0;
10887 int high_pc_relative = 0;
10888
10889 memset (part_die, 0, sizeof (struct partial_die_info));
10890
10891 part_die->offset.sect_off = info_ptr - buffer;
10892
10893 info_ptr += abbrev_len;
10894
10895 if (abbrev == NULL)
10896 return info_ptr;
10897
10898 part_die->tag = abbrev->tag;
10899 part_die->has_children = abbrev->has_children;
10900
10901 for (i = 0; i < abbrev->num_attrs; ++i)
10902 {
10903 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
10904
10905 /* Store the data if it is of an attribute we want to keep in a
10906 partial symbol table. */
10907 switch (attr.name)
10908 {
10909 case DW_AT_name:
10910 switch (part_die->tag)
10911 {
10912 case DW_TAG_compile_unit:
10913 case DW_TAG_type_unit:
10914 /* Compilation units have a DW_AT_name that is a filename, not
10915 a source language identifier. */
10916 case DW_TAG_enumeration_type:
10917 case DW_TAG_enumerator:
10918 /* These tags always have simple identifiers already; no need
10919 to canonicalize them. */
10920 part_die->name = DW_STRING (&attr);
10921 break;
10922 default:
10923 part_die->name
10924 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
10925 &objfile->objfile_obstack);
10926 break;
10927 }
10928 break;
10929 case DW_AT_linkage_name:
10930 case DW_AT_MIPS_linkage_name:
10931 /* Note that both forms of linkage name might appear. We
10932 assume they will be the same, and we only store the last
10933 one we see. */
10934 if (cu->language == language_ada)
10935 part_die->name = DW_STRING (&attr);
10936 part_die->linkage_name = DW_STRING (&attr);
10937 break;
10938 case DW_AT_low_pc:
10939 has_low_pc_attr = 1;
10940 part_die->lowpc = DW_ADDR (&attr);
10941 break;
10942 case DW_AT_high_pc:
10943 has_high_pc_attr = 1;
10944 if (attr.form == DW_FORM_addr
10945 || attr.form == DW_FORM_GNU_addr_index)
10946 part_die->highpc = DW_ADDR (&attr);
10947 else
10948 {
10949 high_pc_relative = 1;
10950 part_die->highpc = DW_UNSND (&attr);
10951 }
10952 break;
10953 case DW_AT_location:
10954 /* Support the .debug_loc offsets. */
10955 if (attr_form_is_block (&attr))
10956 {
10957 part_die->locdesc = DW_BLOCK (&attr);
10958 }
10959 else if (attr_form_is_section_offset (&attr))
10960 {
10961 dwarf2_complex_location_expr_complaint ();
10962 }
10963 else
10964 {
10965 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10966 "partial symbol information");
10967 }
10968 break;
10969 case DW_AT_external:
10970 part_die->is_external = DW_UNSND (&attr);
10971 break;
10972 case DW_AT_declaration:
10973 part_die->is_declaration = DW_UNSND (&attr);
10974 break;
10975 case DW_AT_type:
10976 part_die->has_type = 1;
10977 break;
10978 case DW_AT_abstract_origin:
10979 case DW_AT_specification:
10980 case DW_AT_extension:
10981 part_die->has_specification = 1;
10982 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
10983 break;
10984 case DW_AT_sibling:
10985 /* Ignore absolute siblings, they might point outside of
10986 the current compile unit. */
10987 if (attr.form == DW_FORM_ref_addr)
10988 complaint (&symfile_complaints,
10989 _("ignoring absolute DW_AT_sibling"));
10990 else
10991 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
10992 break;
10993 case DW_AT_byte_size:
10994 part_die->has_byte_size = 1;
10995 break;
10996 case DW_AT_calling_convention:
10997 /* DWARF doesn't provide a way to identify a program's source-level
10998 entry point. DW_AT_calling_convention attributes are only meant
10999 to describe functions' calling conventions.
11000
11001 However, because it's a necessary piece of information in
11002 Fortran, and because DW_CC_program is the only piece of debugging
11003 information whose definition refers to a 'main program' at all,
11004 several compilers have begun marking Fortran main programs with
11005 DW_CC_program --- even when those functions use the standard
11006 calling conventions.
11007
11008 So until DWARF specifies a way to provide this information and
11009 compilers pick up the new representation, we'll support this
11010 practice. */
11011 if (DW_UNSND (&attr) == DW_CC_program
11012 && cu->language == language_fortran)
11013 {
11014 set_main_name (part_die->name);
11015
11016 /* As this DIE has a static linkage the name would be difficult
11017 to look up later. */
11018 language_of_main = language_fortran;
11019 }
11020 break;
11021 case DW_AT_inline:
11022 if (DW_UNSND (&attr) == DW_INL_inlined
11023 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11024 part_die->may_be_inlined = 1;
11025 break;
11026 default:
11027 break;
11028 }
11029 }
11030
11031 if (high_pc_relative)
11032 part_die->highpc += part_die->lowpc;
11033
11034 if (has_low_pc_attr && has_high_pc_attr)
11035 {
11036 /* When using the GNU linker, .gnu.linkonce. sections are used to
11037 eliminate duplicate copies of functions and vtables and such.
11038 The linker will arbitrarily choose one and discard the others.
11039 The AT_*_pc values for such functions refer to local labels in
11040 these sections. If the section from that file was discarded, the
11041 labels are not in the output, so the relocs get a value of 0.
11042 If this is a discarded function, mark the pc bounds as invalid,
11043 so that GDB will ignore it. */
11044 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11045 {
11046 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11047
11048 complaint (&symfile_complaints,
11049 _("DW_AT_low_pc %s is zero "
11050 "for DIE at 0x%x [in module %s]"),
11051 paddress (gdbarch, part_die->lowpc),
11052 part_die->offset.sect_off, objfile->name);
11053 }
11054 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11055 else if (part_die->lowpc >= part_die->highpc)
11056 {
11057 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11058
11059 complaint (&symfile_complaints,
11060 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11061 "for DIE at 0x%x [in module %s]"),
11062 paddress (gdbarch, part_die->lowpc),
11063 paddress (gdbarch, part_die->highpc),
11064 part_die->offset.sect_off, objfile->name);
11065 }
11066 else
11067 part_die->has_pc_info = 1;
11068 }
11069
11070 return info_ptr;
11071 }
11072
11073 /* Find a cached partial DIE at OFFSET in CU. */
11074
11075 static struct partial_die_info *
11076 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11077 {
11078 struct partial_die_info *lookup_die = NULL;
11079 struct partial_die_info part_die;
11080
11081 part_die.offset = offset;
11082 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11083 offset.sect_off);
11084
11085 return lookup_die;
11086 }
11087
11088 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11089 except in the case of .debug_types DIEs which do not reference
11090 outside their CU (they do however referencing other types via
11091 DW_FORM_ref_sig8). */
11092
11093 static struct partial_die_info *
11094 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11095 {
11096 struct objfile *objfile = cu->objfile;
11097 struct dwarf2_per_cu_data *per_cu = NULL;
11098 struct partial_die_info *pd = NULL;
11099
11100 if (offset_in_cu_p (&cu->header, offset))
11101 {
11102 pd = find_partial_die_in_comp_unit (offset, cu);
11103 if (pd != NULL)
11104 return pd;
11105 /* We missed recording what we needed.
11106 Load all dies and try again. */
11107 per_cu = cu->per_cu;
11108 }
11109 else
11110 {
11111 /* TUs don't reference other CUs/TUs (except via type signatures). */
11112 if (cu->per_cu->is_debug_types)
11113 {
11114 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11115 " external reference to offset 0x%lx [in module %s].\n"),
11116 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11117 bfd_get_filename (objfile->obfd));
11118 }
11119 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11120
11121 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11122 load_partial_comp_unit (per_cu);
11123
11124 per_cu->cu->last_used = 0;
11125 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11126 }
11127
11128 /* If we didn't find it, and not all dies have been loaded,
11129 load them all and try again. */
11130
11131 if (pd == NULL && per_cu->load_all_dies == 0)
11132 {
11133 per_cu->load_all_dies = 1;
11134
11135 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11136 THIS_CU->cu may already be in use. So we can't just free it and
11137 replace its DIEs with the ones we read in. Instead, we leave those
11138 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11139 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11140 set. */
11141 load_partial_comp_unit (per_cu);
11142
11143 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11144 }
11145
11146 if (pd == NULL)
11147 internal_error (__FILE__, __LINE__,
11148 _("could not find partial DIE 0x%x "
11149 "in cache [from module %s]\n"),
11150 offset.sect_off, bfd_get_filename (objfile->obfd));
11151 return pd;
11152 }
11153
11154 /* See if we can figure out if the class lives in a namespace. We do
11155 this by looking for a member function; its demangled name will
11156 contain namespace info, if there is any. */
11157
11158 static void
11159 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11160 struct dwarf2_cu *cu)
11161 {
11162 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11163 what template types look like, because the demangler
11164 frequently doesn't give the same name as the debug info. We
11165 could fix this by only using the demangled name to get the
11166 prefix (but see comment in read_structure_type). */
11167
11168 struct partial_die_info *real_pdi;
11169 struct partial_die_info *child_pdi;
11170
11171 /* If this DIE (this DIE's specification, if any) has a parent, then
11172 we should not do this. We'll prepend the parent's fully qualified
11173 name when we create the partial symbol. */
11174
11175 real_pdi = struct_pdi;
11176 while (real_pdi->has_specification)
11177 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11178
11179 if (real_pdi->die_parent != NULL)
11180 return;
11181
11182 for (child_pdi = struct_pdi->die_child;
11183 child_pdi != NULL;
11184 child_pdi = child_pdi->die_sibling)
11185 {
11186 if (child_pdi->tag == DW_TAG_subprogram
11187 && child_pdi->linkage_name != NULL)
11188 {
11189 char *actual_class_name
11190 = language_class_name_from_physname (cu->language_defn,
11191 child_pdi->linkage_name);
11192 if (actual_class_name != NULL)
11193 {
11194 struct_pdi->name
11195 = obsavestring (actual_class_name,
11196 strlen (actual_class_name),
11197 &cu->objfile->objfile_obstack);
11198 xfree (actual_class_name);
11199 }
11200 break;
11201 }
11202 }
11203 }
11204
11205 /* Adjust PART_DIE before generating a symbol for it. This function
11206 may set the is_external flag or change the DIE's name. */
11207
11208 static void
11209 fixup_partial_die (struct partial_die_info *part_die,
11210 struct dwarf2_cu *cu)
11211 {
11212 /* Once we've fixed up a die, there's no point in doing so again.
11213 This also avoids a memory leak if we were to call
11214 guess_partial_die_structure_name multiple times. */
11215 if (part_die->fixup_called)
11216 return;
11217
11218 /* If we found a reference attribute and the DIE has no name, try
11219 to find a name in the referred to DIE. */
11220
11221 if (part_die->name == NULL && part_die->has_specification)
11222 {
11223 struct partial_die_info *spec_die;
11224
11225 spec_die = find_partial_die (part_die->spec_offset, cu);
11226
11227 fixup_partial_die (spec_die, cu);
11228
11229 if (spec_die->name)
11230 {
11231 part_die->name = spec_die->name;
11232
11233 /* Copy DW_AT_external attribute if it is set. */
11234 if (spec_die->is_external)
11235 part_die->is_external = spec_die->is_external;
11236 }
11237 }
11238
11239 /* Set default names for some unnamed DIEs. */
11240
11241 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11242 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11243
11244 /* If there is no parent die to provide a namespace, and there are
11245 children, see if we can determine the namespace from their linkage
11246 name. */
11247 if (cu->language == language_cplus
11248 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11249 && part_die->die_parent == NULL
11250 && part_die->has_children
11251 && (part_die->tag == DW_TAG_class_type
11252 || part_die->tag == DW_TAG_structure_type
11253 || part_die->tag == DW_TAG_union_type))
11254 guess_partial_die_structure_name (part_die, cu);
11255
11256 /* GCC might emit a nameless struct or union that has a linkage
11257 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11258 if (part_die->name == NULL
11259 && (part_die->tag == DW_TAG_class_type
11260 || part_die->tag == DW_TAG_interface_type
11261 || part_die->tag == DW_TAG_structure_type
11262 || part_die->tag == DW_TAG_union_type)
11263 && part_die->linkage_name != NULL)
11264 {
11265 char *demangled;
11266
11267 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11268 if (demangled)
11269 {
11270 const char *base;
11271
11272 /* Strip any leading namespaces/classes, keep only the base name.
11273 DW_AT_name for named DIEs does not contain the prefixes. */
11274 base = strrchr (demangled, ':');
11275 if (base && base > demangled && base[-1] == ':')
11276 base++;
11277 else
11278 base = demangled;
11279
11280 part_die->name = obsavestring (base, strlen (base),
11281 &cu->objfile->objfile_obstack);
11282 xfree (demangled);
11283 }
11284 }
11285
11286 part_die->fixup_called = 1;
11287 }
11288
11289 /* Read an attribute value described by an attribute form. */
11290
11291 static gdb_byte *
11292 read_attribute_value (const struct die_reader_specs *reader,
11293 struct attribute *attr, unsigned form,
11294 gdb_byte *info_ptr)
11295 {
11296 struct dwarf2_cu *cu = reader->cu;
11297 bfd *abfd = reader->abfd;
11298 struct comp_unit_head *cu_header = &cu->header;
11299 unsigned int bytes_read;
11300 struct dwarf_block *blk;
11301
11302 attr->form = form;
11303 switch (form)
11304 {
11305 case DW_FORM_ref_addr:
11306 if (cu->header.version == 2)
11307 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11308 else
11309 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11310 &cu->header, &bytes_read);
11311 info_ptr += bytes_read;
11312 break;
11313 case DW_FORM_addr:
11314 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11315 info_ptr += bytes_read;
11316 break;
11317 case DW_FORM_block2:
11318 blk = dwarf_alloc_block (cu);
11319 blk->size = read_2_bytes (abfd, info_ptr);
11320 info_ptr += 2;
11321 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11322 info_ptr += blk->size;
11323 DW_BLOCK (attr) = blk;
11324 break;
11325 case DW_FORM_block4:
11326 blk = dwarf_alloc_block (cu);
11327 blk->size = read_4_bytes (abfd, info_ptr);
11328 info_ptr += 4;
11329 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11330 info_ptr += blk->size;
11331 DW_BLOCK (attr) = blk;
11332 break;
11333 case DW_FORM_data2:
11334 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11335 info_ptr += 2;
11336 break;
11337 case DW_FORM_data4:
11338 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11339 info_ptr += 4;
11340 break;
11341 case DW_FORM_data8:
11342 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11343 info_ptr += 8;
11344 break;
11345 case DW_FORM_sec_offset:
11346 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11347 info_ptr += bytes_read;
11348 break;
11349 case DW_FORM_string:
11350 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11351 DW_STRING_IS_CANONICAL (attr) = 0;
11352 info_ptr += bytes_read;
11353 break;
11354 case DW_FORM_strp:
11355 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11356 &bytes_read);
11357 DW_STRING_IS_CANONICAL (attr) = 0;
11358 info_ptr += bytes_read;
11359 break;
11360 case DW_FORM_exprloc:
11361 case DW_FORM_block:
11362 blk = dwarf_alloc_block (cu);
11363 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11364 info_ptr += bytes_read;
11365 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11366 info_ptr += blk->size;
11367 DW_BLOCK (attr) = blk;
11368 break;
11369 case DW_FORM_block1:
11370 blk = dwarf_alloc_block (cu);
11371 blk->size = read_1_byte (abfd, info_ptr);
11372 info_ptr += 1;
11373 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11374 info_ptr += blk->size;
11375 DW_BLOCK (attr) = blk;
11376 break;
11377 case DW_FORM_data1:
11378 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11379 info_ptr += 1;
11380 break;
11381 case DW_FORM_flag:
11382 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11383 info_ptr += 1;
11384 break;
11385 case DW_FORM_flag_present:
11386 DW_UNSND (attr) = 1;
11387 break;
11388 case DW_FORM_sdata:
11389 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11390 info_ptr += bytes_read;
11391 break;
11392 case DW_FORM_udata:
11393 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11394 info_ptr += bytes_read;
11395 break;
11396 case DW_FORM_ref1:
11397 DW_UNSND (attr) = (cu->header.offset.sect_off
11398 + read_1_byte (abfd, info_ptr));
11399 info_ptr += 1;
11400 break;
11401 case DW_FORM_ref2:
11402 DW_UNSND (attr) = (cu->header.offset.sect_off
11403 + read_2_bytes (abfd, info_ptr));
11404 info_ptr += 2;
11405 break;
11406 case DW_FORM_ref4:
11407 DW_UNSND (attr) = (cu->header.offset.sect_off
11408 + read_4_bytes (abfd, info_ptr));
11409 info_ptr += 4;
11410 break;
11411 case DW_FORM_ref8:
11412 DW_UNSND (attr) = (cu->header.offset.sect_off
11413 + read_8_bytes (abfd, info_ptr));
11414 info_ptr += 8;
11415 break;
11416 case DW_FORM_ref_sig8:
11417 /* Convert the signature to something we can record in DW_UNSND
11418 for later lookup.
11419 NOTE: This is NULL if the type wasn't found. */
11420 DW_SIGNATURED_TYPE (attr) =
11421 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11422 info_ptr += 8;
11423 break;
11424 case DW_FORM_ref_udata:
11425 DW_UNSND (attr) = (cu->header.offset.sect_off
11426 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11427 info_ptr += bytes_read;
11428 break;
11429 case DW_FORM_indirect:
11430 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11431 info_ptr += bytes_read;
11432 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11433 break;
11434 case DW_FORM_GNU_addr_index:
11435 if (reader->dwo_file == NULL)
11436 {
11437 /* For now flag a hard error.
11438 Later we can turn this into a complaint. */
11439 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11440 dwarf_form_name (form),
11441 bfd_get_filename (abfd));
11442 }
11443 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11444 info_ptr += bytes_read;
11445 break;
11446 case DW_FORM_GNU_str_index:
11447 if (reader->dwo_file == NULL)
11448 {
11449 /* For now flag a hard error.
11450 Later we can turn this into a complaint if warranted. */
11451 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11452 dwarf_form_name (form),
11453 bfd_get_filename (abfd));
11454 }
11455 {
11456 ULONGEST str_index =
11457 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11458
11459 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11460 DW_STRING_IS_CANONICAL (attr) = 0;
11461 info_ptr += bytes_read;
11462 }
11463 break;
11464 default:
11465 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11466 dwarf_form_name (form),
11467 bfd_get_filename (abfd));
11468 }
11469
11470 /* We have seen instances where the compiler tried to emit a byte
11471 size attribute of -1 which ended up being encoded as an unsigned
11472 0xffffffff. Although 0xffffffff is technically a valid size value,
11473 an object of this size seems pretty unlikely so we can relatively
11474 safely treat these cases as if the size attribute was invalid and
11475 treat them as zero by default. */
11476 if (attr->name == DW_AT_byte_size
11477 && form == DW_FORM_data4
11478 && DW_UNSND (attr) >= 0xffffffff)
11479 {
11480 complaint
11481 (&symfile_complaints,
11482 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11483 hex_string (DW_UNSND (attr)));
11484 DW_UNSND (attr) = 0;
11485 }
11486
11487 return info_ptr;
11488 }
11489
11490 /* Read an attribute described by an abbreviated attribute. */
11491
11492 static gdb_byte *
11493 read_attribute (const struct die_reader_specs *reader,
11494 struct attribute *attr, struct attr_abbrev *abbrev,
11495 gdb_byte *info_ptr)
11496 {
11497 attr->name = abbrev->name;
11498 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
11499 }
11500
11501 /* Read dwarf information from a buffer. */
11502
11503 static unsigned int
11504 read_1_byte (bfd *abfd, gdb_byte *buf)
11505 {
11506 return bfd_get_8 (abfd, buf);
11507 }
11508
11509 static int
11510 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
11511 {
11512 return bfd_get_signed_8 (abfd, buf);
11513 }
11514
11515 static unsigned int
11516 read_2_bytes (bfd *abfd, gdb_byte *buf)
11517 {
11518 return bfd_get_16 (abfd, buf);
11519 }
11520
11521 static int
11522 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11523 {
11524 return bfd_get_signed_16 (abfd, buf);
11525 }
11526
11527 static unsigned int
11528 read_4_bytes (bfd *abfd, gdb_byte *buf)
11529 {
11530 return bfd_get_32 (abfd, buf);
11531 }
11532
11533 static int
11534 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11535 {
11536 return bfd_get_signed_32 (abfd, buf);
11537 }
11538
11539 static ULONGEST
11540 read_8_bytes (bfd *abfd, gdb_byte *buf)
11541 {
11542 return bfd_get_64 (abfd, buf);
11543 }
11544
11545 static CORE_ADDR
11546 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
11547 unsigned int *bytes_read)
11548 {
11549 struct comp_unit_head *cu_header = &cu->header;
11550 CORE_ADDR retval = 0;
11551
11552 if (cu_header->signed_addr_p)
11553 {
11554 switch (cu_header->addr_size)
11555 {
11556 case 2:
11557 retval = bfd_get_signed_16 (abfd, buf);
11558 break;
11559 case 4:
11560 retval = bfd_get_signed_32 (abfd, buf);
11561 break;
11562 case 8:
11563 retval = bfd_get_signed_64 (abfd, buf);
11564 break;
11565 default:
11566 internal_error (__FILE__, __LINE__,
11567 _("read_address: bad switch, signed [in module %s]"),
11568 bfd_get_filename (abfd));
11569 }
11570 }
11571 else
11572 {
11573 switch (cu_header->addr_size)
11574 {
11575 case 2:
11576 retval = bfd_get_16 (abfd, buf);
11577 break;
11578 case 4:
11579 retval = bfd_get_32 (abfd, buf);
11580 break;
11581 case 8:
11582 retval = bfd_get_64 (abfd, buf);
11583 break;
11584 default:
11585 internal_error (__FILE__, __LINE__,
11586 _("read_address: bad switch, "
11587 "unsigned [in module %s]"),
11588 bfd_get_filename (abfd));
11589 }
11590 }
11591
11592 *bytes_read = cu_header->addr_size;
11593 return retval;
11594 }
11595
11596 /* Read the initial length from a section. The (draft) DWARF 3
11597 specification allows the initial length to take up either 4 bytes
11598 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11599 bytes describe the length and all offsets will be 8 bytes in length
11600 instead of 4.
11601
11602 An older, non-standard 64-bit format is also handled by this
11603 function. The older format in question stores the initial length
11604 as an 8-byte quantity without an escape value. Lengths greater
11605 than 2^32 aren't very common which means that the initial 4 bytes
11606 is almost always zero. Since a length value of zero doesn't make
11607 sense for the 32-bit format, this initial zero can be considered to
11608 be an escape value which indicates the presence of the older 64-bit
11609 format. As written, the code can't detect (old format) lengths
11610 greater than 4GB. If it becomes necessary to handle lengths
11611 somewhat larger than 4GB, we could allow other small values (such
11612 as the non-sensical values of 1, 2, and 3) to also be used as
11613 escape values indicating the presence of the old format.
11614
11615 The value returned via bytes_read should be used to increment the
11616 relevant pointer after calling read_initial_length().
11617
11618 [ Note: read_initial_length() and read_offset() are based on the
11619 document entitled "DWARF Debugging Information Format", revision
11620 3, draft 8, dated November 19, 2001. This document was obtained
11621 from:
11622
11623 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
11624
11625 This document is only a draft and is subject to change. (So beware.)
11626
11627 Details regarding the older, non-standard 64-bit format were
11628 determined empirically by examining 64-bit ELF files produced by
11629 the SGI toolchain on an IRIX 6.5 machine.
11630
11631 - Kevin, July 16, 2002
11632 ] */
11633
11634 static LONGEST
11635 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
11636 {
11637 LONGEST length = bfd_get_32 (abfd, buf);
11638
11639 if (length == 0xffffffff)
11640 {
11641 length = bfd_get_64 (abfd, buf + 4);
11642 *bytes_read = 12;
11643 }
11644 else if (length == 0)
11645 {
11646 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
11647 length = bfd_get_64 (abfd, buf);
11648 *bytes_read = 8;
11649 }
11650 else
11651 {
11652 *bytes_read = 4;
11653 }
11654
11655 return length;
11656 }
11657
11658 /* Cover function for read_initial_length.
11659 Returns the length of the object at BUF, and stores the size of the
11660 initial length in *BYTES_READ and stores the size that offsets will be in
11661 *OFFSET_SIZE.
11662 If the initial length size is not equivalent to that specified in
11663 CU_HEADER then issue a complaint.
11664 This is useful when reading non-comp-unit headers. */
11665
11666 static LONGEST
11667 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11668 const struct comp_unit_head *cu_header,
11669 unsigned int *bytes_read,
11670 unsigned int *offset_size)
11671 {
11672 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11673
11674 gdb_assert (cu_header->initial_length_size == 4
11675 || cu_header->initial_length_size == 8
11676 || cu_header->initial_length_size == 12);
11677
11678 if (cu_header->initial_length_size != *bytes_read)
11679 complaint (&symfile_complaints,
11680 _("intermixed 32-bit and 64-bit DWARF sections"));
11681
11682 *offset_size = (*bytes_read == 4) ? 4 : 8;
11683 return length;
11684 }
11685
11686 /* Read an offset from the data stream. The size of the offset is
11687 given by cu_header->offset_size. */
11688
11689 static LONGEST
11690 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
11691 unsigned int *bytes_read)
11692 {
11693 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
11694
11695 *bytes_read = cu_header->offset_size;
11696 return offset;
11697 }
11698
11699 /* Read an offset from the data stream. */
11700
11701 static LONGEST
11702 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
11703 {
11704 LONGEST retval = 0;
11705
11706 switch (offset_size)
11707 {
11708 case 4:
11709 retval = bfd_get_32 (abfd, buf);
11710 break;
11711 case 8:
11712 retval = bfd_get_64 (abfd, buf);
11713 break;
11714 default:
11715 internal_error (__FILE__, __LINE__,
11716 _("read_offset_1: bad switch [in module %s]"),
11717 bfd_get_filename (abfd));
11718 }
11719
11720 return retval;
11721 }
11722
11723 static gdb_byte *
11724 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
11725 {
11726 /* If the size of a host char is 8 bits, we can return a pointer
11727 to the buffer, otherwise we have to copy the data to a buffer
11728 allocated on the temporary obstack. */
11729 gdb_assert (HOST_CHAR_BIT == 8);
11730 return buf;
11731 }
11732
11733 static char *
11734 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11735 {
11736 /* If the size of a host char is 8 bits, we can return a pointer
11737 to the string, otherwise we have to copy the string to a buffer
11738 allocated on the temporary obstack. */
11739 gdb_assert (HOST_CHAR_BIT == 8);
11740 if (*buf == '\0')
11741 {
11742 *bytes_read_ptr = 1;
11743 return NULL;
11744 }
11745 *bytes_read_ptr = strlen ((char *) buf) + 1;
11746 return (char *) buf;
11747 }
11748
11749 static char *
11750 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
11751 {
11752 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
11753 if (dwarf2_per_objfile->str.buffer == NULL)
11754 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
11755 bfd_get_filename (abfd));
11756 if (str_offset >= dwarf2_per_objfile->str.size)
11757 error (_("DW_FORM_strp pointing outside of "
11758 ".debug_str section [in module %s]"),
11759 bfd_get_filename (abfd));
11760 gdb_assert (HOST_CHAR_BIT == 8);
11761 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
11762 return NULL;
11763 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
11764 }
11765
11766 static char *
11767 read_indirect_string (bfd *abfd, gdb_byte *buf,
11768 const struct comp_unit_head *cu_header,
11769 unsigned int *bytes_read_ptr)
11770 {
11771 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
11772
11773 return read_indirect_string_at_offset (abfd, str_offset);
11774 }
11775
11776 static ULONGEST
11777 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11778 {
11779 ULONGEST result;
11780 unsigned int num_read;
11781 int i, shift;
11782 unsigned char byte;
11783
11784 result = 0;
11785 shift = 0;
11786 num_read = 0;
11787 i = 0;
11788 while (1)
11789 {
11790 byte = bfd_get_8 (abfd, buf);
11791 buf++;
11792 num_read++;
11793 result |= ((ULONGEST) (byte & 127) << shift);
11794 if ((byte & 128) == 0)
11795 {
11796 break;
11797 }
11798 shift += 7;
11799 }
11800 *bytes_read_ptr = num_read;
11801 return result;
11802 }
11803
11804 static LONGEST
11805 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11806 {
11807 LONGEST result;
11808 int i, shift, num_read;
11809 unsigned char byte;
11810
11811 result = 0;
11812 shift = 0;
11813 num_read = 0;
11814 i = 0;
11815 while (1)
11816 {
11817 byte = bfd_get_8 (abfd, buf);
11818 buf++;
11819 num_read++;
11820 result |= ((LONGEST) (byte & 127) << shift);
11821 shift += 7;
11822 if ((byte & 128) == 0)
11823 {
11824 break;
11825 }
11826 }
11827 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
11828 result |= -(((LONGEST) 1) << shift);
11829 *bytes_read_ptr = num_read;
11830 return result;
11831 }
11832
11833 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
11834 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
11835 ADDR_SIZE is the size of addresses from the CU header. */
11836
11837 static CORE_ADDR
11838 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
11839 {
11840 struct objfile *objfile = dwarf2_per_objfile->objfile;
11841 bfd *abfd = objfile->obfd;
11842 const gdb_byte *info_ptr;
11843
11844 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
11845 if (dwarf2_per_objfile->addr.buffer == NULL)
11846 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
11847 objfile->name);
11848 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
11849 error (_("DW_FORM_addr_index pointing outside of "
11850 ".debug_addr section [in module %s]"),
11851 objfile->name);
11852 info_ptr = (dwarf2_per_objfile->addr.buffer
11853 + addr_base + addr_index * addr_size);
11854 if (addr_size == 4)
11855 return bfd_get_32 (abfd, info_ptr);
11856 else
11857 return bfd_get_64 (abfd, info_ptr);
11858 }
11859
11860 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
11861
11862 static CORE_ADDR
11863 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
11864 {
11865 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
11866 }
11867
11868 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
11869
11870 static CORE_ADDR
11871 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
11872 unsigned int *bytes_read)
11873 {
11874 bfd *abfd = cu->objfile->obfd;
11875 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
11876
11877 return read_addr_index (cu, addr_index);
11878 }
11879
11880 /* Data structure to pass results from dwarf2_read_addr_index_reader
11881 back to dwarf2_read_addr_index. */
11882
11883 struct dwarf2_read_addr_index_data
11884 {
11885 ULONGEST addr_base;
11886 int addr_size;
11887 };
11888
11889 /* die_reader_func for dwarf2_read_addr_index. */
11890
11891 static void
11892 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
11893 gdb_byte *info_ptr,
11894 struct die_info *comp_unit_die,
11895 int has_children,
11896 void *data)
11897 {
11898 struct dwarf2_cu *cu = reader->cu;
11899 struct dwarf2_read_addr_index_data *aidata =
11900 (struct dwarf2_read_addr_index_data *) data;
11901
11902 aidata->addr_base = cu->addr_base;
11903 aidata->addr_size = cu->header.addr_size;
11904 }
11905
11906 /* Given an index in .debug_addr, fetch the value.
11907 NOTE: This can be called during dwarf expression evaluation,
11908 long after the debug information has been read, and thus per_cu->cu
11909 may no longer exist. */
11910
11911 CORE_ADDR
11912 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
11913 unsigned int addr_index)
11914 {
11915 struct objfile *objfile = per_cu->objfile;
11916 struct dwarf2_cu *cu = per_cu->cu;
11917 ULONGEST addr_base;
11918 int addr_size;
11919
11920 /* This is intended to be called from outside this file. */
11921 dw2_setup (objfile);
11922
11923 /* We need addr_base and addr_size.
11924 If we don't have PER_CU->cu, we have to get it.
11925 Nasty, but the alternative is storing the needed info in PER_CU,
11926 which at this point doesn't seem justified: it's not clear how frequently
11927 it would get used and it would increase the size of every PER_CU.
11928 Entry points like dwarf2_per_cu_addr_size do a similar thing
11929 so we're not in uncharted territory here.
11930 Alas we need to be a bit more complicated as addr_base is contained
11931 in the DIE.
11932
11933 We don't need to read the entire CU(/TU).
11934 We just need the header and top level die.
11935 IWBN to use the aging mechanism to let us lazily later discard the CU.
11936 See however init_cutu_and_read_dies_simple. */
11937
11938 if (cu != NULL)
11939 {
11940 addr_base = cu->addr_base;
11941 addr_size = cu->header.addr_size;
11942 }
11943 else
11944 {
11945 struct dwarf2_read_addr_index_data aidata;
11946
11947 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
11948 &aidata);
11949 addr_base = aidata.addr_base;
11950 addr_size = aidata.addr_size;
11951 }
11952
11953 return read_addr_index_1 (addr_index, addr_base, addr_size);
11954 }
11955
11956 /* Given a DW_AT_str_index, fetch the string. */
11957
11958 static char *
11959 read_str_index (const struct die_reader_specs *reader,
11960 struct dwarf2_cu *cu, ULONGEST str_index)
11961 {
11962 struct objfile *objfile = dwarf2_per_objfile->objfile;
11963 const char *dwo_name = objfile->name;
11964 bfd *abfd = objfile->obfd;
11965 struct dwo_sections *sections = &reader->dwo_file->sections;
11966 gdb_byte *info_ptr;
11967 ULONGEST str_offset;
11968
11969 dwarf2_read_section (objfile, &sections->str);
11970 dwarf2_read_section (objfile, &sections->str_offsets);
11971 if (sections->str.buffer == NULL)
11972 error (_("DW_FORM_str_index used without .debug_str.dwo section"
11973 " in CU at offset 0x%lx [in module %s]"),
11974 (long) cu->header.offset.sect_off, dwo_name);
11975 if (sections->str_offsets.buffer == NULL)
11976 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
11977 " in CU at offset 0x%lx [in module %s]"),
11978 (long) cu->header.offset.sect_off, dwo_name);
11979 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
11980 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
11981 " section in CU at offset 0x%lx [in module %s]"),
11982 (long) cu->header.offset.sect_off, dwo_name);
11983 info_ptr = (sections->str_offsets.buffer
11984 + str_index * cu->header.offset_size);
11985 if (cu->header.offset_size == 4)
11986 str_offset = bfd_get_32 (abfd, info_ptr);
11987 else
11988 str_offset = bfd_get_64 (abfd, info_ptr);
11989 if (str_offset >= sections->str.size)
11990 error (_("Offset from DW_FORM_str_index pointing outside of"
11991 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
11992 (long) cu->header.offset.sect_off, dwo_name);
11993 return (char *) (sections->str.buffer + str_offset);
11994 }
11995
11996 /* Return a pointer to just past the end of an LEB128 number in BUF. */
11997
11998 static gdb_byte *
11999 skip_leb128 (bfd *abfd, gdb_byte *buf)
12000 {
12001 int byte;
12002
12003 while (1)
12004 {
12005 byte = bfd_get_8 (abfd, buf);
12006 buf++;
12007 if ((byte & 128) == 0)
12008 return buf;
12009 }
12010 }
12011
12012 /* Return the length of an LEB128 number in BUF. */
12013
12014 static int
12015 leb128_size (const gdb_byte *buf)
12016 {
12017 const gdb_byte *begin = buf;
12018 gdb_byte byte;
12019
12020 while (1)
12021 {
12022 byte = *buf++;
12023 if ((byte & 128) == 0)
12024 return buf - begin;
12025 }
12026 }
12027
12028 static void
12029 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12030 {
12031 switch (lang)
12032 {
12033 case DW_LANG_C89:
12034 case DW_LANG_C99:
12035 case DW_LANG_C:
12036 cu->language = language_c;
12037 break;
12038 case DW_LANG_C_plus_plus:
12039 cu->language = language_cplus;
12040 break;
12041 case DW_LANG_D:
12042 cu->language = language_d;
12043 break;
12044 case DW_LANG_Fortran77:
12045 case DW_LANG_Fortran90:
12046 case DW_LANG_Fortran95:
12047 cu->language = language_fortran;
12048 break;
12049 case DW_LANG_Go:
12050 cu->language = language_go;
12051 break;
12052 case DW_LANG_Mips_Assembler:
12053 cu->language = language_asm;
12054 break;
12055 case DW_LANG_Java:
12056 cu->language = language_java;
12057 break;
12058 case DW_LANG_Ada83:
12059 case DW_LANG_Ada95:
12060 cu->language = language_ada;
12061 break;
12062 case DW_LANG_Modula2:
12063 cu->language = language_m2;
12064 break;
12065 case DW_LANG_Pascal83:
12066 cu->language = language_pascal;
12067 break;
12068 case DW_LANG_ObjC:
12069 cu->language = language_objc;
12070 break;
12071 case DW_LANG_Cobol74:
12072 case DW_LANG_Cobol85:
12073 default:
12074 cu->language = language_minimal;
12075 break;
12076 }
12077 cu->language_defn = language_def (cu->language);
12078 }
12079
12080 /* Return the named attribute or NULL if not there. */
12081
12082 static struct attribute *
12083 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12084 {
12085 for (;;)
12086 {
12087 unsigned int i;
12088 struct attribute *spec = NULL;
12089
12090 for (i = 0; i < die->num_attrs; ++i)
12091 {
12092 if (die->attrs[i].name == name)
12093 return &die->attrs[i];
12094 if (die->attrs[i].name == DW_AT_specification
12095 || die->attrs[i].name == DW_AT_abstract_origin)
12096 spec = &die->attrs[i];
12097 }
12098
12099 if (!spec)
12100 break;
12101
12102 die = follow_die_ref (die, spec, &cu);
12103 }
12104
12105 return NULL;
12106 }
12107
12108 /* Return the named attribute or NULL if not there,
12109 but do not follow DW_AT_specification, etc.
12110 This is for use in contexts where we're reading .debug_types dies.
12111 Following DW_AT_specification, DW_AT_abstract_origin will take us
12112 back up the chain, and we want to go down. */
12113
12114 static struct attribute *
12115 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12116 struct dwarf2_cu *cu)
12117 {
12118 unsigned int i;
12119
12120 for (i = 0; i < die->num_attrs; ++i)
12121 if (die->attrs[i].name == name)
12122 return &die->attrs[i];
12123
12124 return NULL;
12125 }
12126
12127 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12128 and holds a non-zero value. This function should only be used for
12129 DW_FORM_flag or DW_FORM_flag_present attributes. */
12130
12131 static int
12132 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12133 {
12134 struct attribute *attr = dwarf2_attr (die, name, cu);
12135
12136 return (attr && DW_UNSND (attr));
12137 }
12138
12139 static int
12140 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12141 {
12142 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12143 which value is non-zero. However, we have to be careful with
12144 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12145 (via dwarf2_flag_true_p) follows this attribute. So we may
12146 end up accidently finding a declaration attribute that belongs
12147 to a different DIE referenced by the specification attribute,
12148 even though the given DIE does not have a declaration attribute. */
12149 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12150 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12151 }
12152
12153 /* Return the die giving the specification for DIE, if there is
12154 one. *SPEC_CU is the CU containing DIE on input, and the CU
12155 containing the return value on output. If there is no
12156 specification, but there is an abstract origin, that is
12157 returned. */
12158
12159 static struct die_info *
12160 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12161 {
12162 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12163 *spec_cu);
12164
12165 if (spec_attr == NULL)
12166 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12167
12168 if (spec_attr == NULL)
12169 return NULL;
12170 else
12171 return follow_die_ref (die, spec_attr, spec_cu);
12172 }
12173
12174 /* Free the line_header structure *LH, and any arrays and strings it
12175 refers to.
12176 NOTE: This is also used as a "cleanup" function. */
12177
12178 static void
12179 free_line_header (struct line_header *lh)
12180 {
12181 if (lh->standard_opcode_lengths)
12182 xfree (lh->standard_opcode_lengths);
12183
12184 /* Remember that all the lh->file_names[i].name pointers are
12185 pointers into debug_line_buffer, and don't need to be freed. */
12186 if (lh->file_names)
12187 xfree (lh->file_names);
12188
12189 /* Similarly for the include directory names. */
12190 if (lh->include_dirs)
12191 xfree (lh->include_dirs);
12192
12193 xfree (lh);
12194 }
12195
12196 /* Add an entry to LH's include directory table. */
12197
12198 static void
12199 add_include_dir (struct line_header *lh, char *include_dir)
12200 {
12201 /* Grow the array if necessary. */
12202 if (lh->include_dirs_size == 0)
12203 {
12204 lh->include_dirs_size = 1; /* for testing */
12205 lh->include_dirs = xmalloc (lh->include_dirs_size
12206 * sizeof (*lh->include_dirs));
12207 }
12208 else if (lh->num_include_dirs >= lh->include_dirs_size)
12209 {
12210 lh->include_dirs_size *= 2;
12211 lh->include_dirs = xrealloc (lh->include_dirs,
12212 (lh->include_dirs_size
12213 * sizeof (*lh->include_dirs)));
12214 }
12215
12216 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12217 }
12218
12219 /* Add an entry to LH's file name table. */
12220
12221 static void
12222 add_file_name (struct line_header *lh,
12223 char *name,
12224 unsigned int dir_index,
12225 unsigned int mod_time,
12226 unsigned int length)
12227 {
12228 struct file_entry *fe;
12229
12230 /* Grow the array if necessary. */
12231 if (lh->file_names_size == 0)
12232 {
12233 lh->file_names_size = 1; /* for testing */
12234 lh->file_names = xmalloc (lh->file_names_size
12235 * sizeof (*lh->file_names));
12236 }
12237 else if (lh->num_file_names >= lh->file_names_size)
12238 {
12239 lh->file_names_size *= 2;
12240 lh->file_names = xrealloc (lh->file_names,
12241 (lh->file_names_size
12242 * sizeof (*lh->file_names)));
12243 }
12244
12245 fe = &lh->file_names[lh->num_file_names++];
12246 fe->name = name;
12247 fe->dir_index = dir_index;
12248 fe->mod_time = mod_time;
12249 fe->length = length;
12250 fe->included_p = 0;
12251 fe->symtab = NULL;
12252 }
12253
12254 /* Read the statement program header starting at OFFSET in
12255 .debug_line, or .debug_line.dwo. Return a pointer
12256 to a struct line_header, allocated using xmalloc.
12257
12258 NOTE: the strings in the include directory and file name tables of
12259 the returned object point into the dwarf line section buffer,
12260 and must not be freed. */
12261
12262 static struct line_header *
12263 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12264 {
12265 struct cleanup *back_to;
12266 struct line_header *lh;
12267 gdb_byte *line_ptr;
12268 unsigned int bytes_read, offset_size;
12269 int i;
12270 char *cur_dir, *cur_file;
12271 struct dwarf2_section_info *section;
12272 bfd *abfd;
12273
12274 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12275 DWO file. */
12276 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12277 section = &cu->dwo_unit->dwo_file->sections.line;
12278 else
12279 section = &dwarf2_per_objfile->line;
12280
12281 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12282 if (section->buffer == NULL)
12283 {
12284 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12285 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12286 else
12287 complaint (&symfile_complaints, _("missing .debug_line section"));
12288 return 0;
12289 }
12290
12291 /* We can't do this until we know the section is non-empty.
12292 Only then do we know we have such a section. */
12293 abfd = section->asection->owner;
12294
12295 /* Make sure that at least there's room for the total_length field.
12296 That could be 12 bytes long, but we're just going to fudge that. */
12297 if (offset + 4 >= section->size)
12298 {
12299 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12300 return 0;
12301 }
12302
12303 lh = xmalloc (sizeof (*lh));
12304 memset (lh, 0, sizeof (*lh));
12305 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12306 (void *) lh);
12307
12308 line_ptr = section->buffer + offset;
12309
12310 /* Read in the header. */
12311 lh->total_length =
12312 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12313 &bytes_read, &offset_size);
12314 line_ptr += bytes_read;
12315 if (line_ptr + lh->total_length > (section->buffer + section->size))
12316 {
12317 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12318 return 0;
12319 }
12320 lh->statement_program_end = line_ptr + lh->total_length;
12321 lh->version = read_2_bytes (abfd, line_ptr);
12322 line_ptr += 2;
12323 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12324 line_ptr += offset_size;
12325 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12326 line_ptr += 1;
12327 if (lh->version >= 4)
12328 {
12329 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12330 line_ptr += 1;
12331 }
12332 else
12333 lh->maximum_ops_per_instruction = 1;
12334
12335 if (lh->maximum_ops_per_instruction == 0)
12336 {
12337 lh->maximum_ops_per_instruction = 1;
12338 complaint (&symfile_complaints,
12339 _("invalid maximum_ops_per_instruction "
12340 "in `.debug_line' section"));
12341 }
12342
12343 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12344 line_ptr += 1;
12345 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12346 line_ptr += 1;
12347 lh->line_range = read_1_byte (abfd, line_ptr);
12348 line_ptr += 1;
12349 lh->opcode_base = read_1_byte (abfd, line_ptr);
12350 line_ptr += 1;
12351 lh->standard_opcode_lengths
12352 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12353
12354 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12355 for (i = 1; i < lh->opcode_base; ++i)
12356 {
12357 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12358 line_ptr += 1;
12359 }
12360
12361 /* Read directory table. */
12362 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12363 {
12364 line_ptr += bytes_read;
12365 add_include_dir (lh, cur_dir);
12366 }
12367 line_ptr += bytes_read;
12368
12369 /* Read file name table. */
12370 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12371 {
12372 unsigned int dir_index, mod_time, length;
12373
12374 line_ptr += bytes_read;
12375 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12376 line_ptr += bytes_read;
12377 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12378 line_ptr += bytes_read;
12379 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12380 line_ptr += bytes_read;
12381
12382 add_file_name (lh, cur_file, dir_index, mod_time, length);
12383 }
12384 line_ptr += bytes_read;
12385 lh->statement_program_start = line_ptr;
12386
12387 if (line_ptr > (section->buffer + section->size))
12388 complaint (&symfile_complaints,
12389 _("line number info header doesn't "
12390 "fit in `.debug_line' section"));
12391
12392 discard_cleanups (back_to);
12393 return lh;
12394 }
12395
12396 /* Subroutine of dwarf_decode_lines to simplify it.
12397 Return the file name of the psymtab for included file FILE_INDEX
12398 in line header LH of PST.
12399 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12400 If space for the result is malloc'd, it will be freed by a cleanup.
12401 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12402
12403 static char *
12404 psymtab_include_file_name (const struct line_header *lh, int file_index,
12405 const struct partial_symtab *pst,
12406 const char *comp_dir)
12407 {
12408 const struct file_entry fe = lh->file_names [file_index];
12409 char *include_name = fe.name;
12410 char *include_name_to_compare = include_name;
12411 char *dir_name = NULL;
12412 const char *pst_filename;
12413 char *copied_name = NULL;
12414 int file_is_pst;
12415
12416 if (fe.dir_index)
12417 dir_name = lh->include_dirs[fe.dir_index - 1];
12418
12419 if (!IS_ABSOLUTE_PATH (include_name)
12420 && (dir_name != NULL || comp_dir != NULL))
12421 {
12422 /* Avoid creating a duplicate psymtab for PST.
12423 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12424 Before we do the comparison, however, we need to account
12425 for DIR_NAME and COMP_DIR.
12426 First prepend dir_name (if non-NULL). If we still don't
12427 have an absolute path prepend comp_dir (if non-NULL).
12428 However, the directory we record in the include-file's
12429 psymtab does not contain COMP_DIR (to match the
12430 corresponding symtab(s)).
12431
12432 Example:
12433
12434 bash$ cd /tmp
12435 bash$ gcc -g ./hello.c
12436 include_name = "hello.c"
12437 dir_name = "."
12438 DW_AT_comp_dir = comp_dir = "/tmp"
12439 DW_AT_name = "./hello.c" */
12440
12441 if (dir_name != NULL)
12442 {
12443 include_name = concat (dir_name, SLASH_STRING,
12444 include_name, (char *)NULL);
12445 include_name_to_compare = include_name;
12446 make_cleanup (xfree, include_name);
12447 }
12448 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12449 {
12450 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12451 include_name, (char *)NULL);
12452 }
12453 }
12454
12455 pst_filename = pst->filename;
12456 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12457 {
12458 copied_name = concat (pst->dirname, SLASH_STRING,
12459 pst_filename, (char *)NULL);
12460 pst_filename = copied_name;
12461 }
12462
12463 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12464
12465 if (include_name_to_compare != include_name)
12466 xfree (include_name_to_compare);
12467 if (copied_name != NULL)
12468 xfree (copied_name);
12469
12470 if (file_is_pst)
12471 return NULL;
12472 return include_name;
12473 }
12474
12475 /* Ignore this record_line request. */
12476
12477 static void
12478 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12479 {
12480 return;
12481 }
12482
12483 /* Subroutine of dwarf_decode_lines to simplify it.
12484 Process the line number information in LH. */
12485
12486 static void
12487 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12488 struct dwarf2_cu *cu, struct partial_symtab *pst)
12489 {
12490 gdb_byte *line_ptr, *extended_end;
12491 gdb_byte *line_end;
12492 unsigned int bytes_read, extended_len;
12493 unsigned char op_code, extended_op, adj_opcode;
12494 CORE_ADDR baseaddr;
12495 struct objfile *objfile = cu->objfile;
12496 bfd *abfd = objfile->obfd;
12497 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12498 const int decode_for_pst_p = (pst != NULL);
12499 struct subfile *last_subfile = NULL;
12500 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12501 = record_line;
12502
12503 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12504
12505 line_ptr = lh->statement_program_start;
12506 line_end = lh->statement_program_end;
12507
12508 /* Read the statement sequences until there's nothing left. */
12509 while (line_ptr < line_end)
12510 {
12511 /* state machine registers */
12512 CORE_ADDR address = 0;
12513 unsigned int file = 1;
12514 unsigned int line = 1;
12515 unsigned int column = 0;
12516 int is_stmt = lh->default_is_stmt;
12517 int basic_block = 0;
12518 int end_sequence = 0;
12519 CORE_ADDR addr;
12520 unsigned char op_index = 0;
12521
12522 if (!decode_for_pst_p && lh->num_file_names >= file)
12523 {
12524 /* Start a subfile for the current file of the state machine. */
12525 /* lh->include_dirs and lh->file_names are 0-based, but the
12526 directory and file name numbers in the statement program
12527 are 1-based. */
12528 struct file_entry *fe = &lh->file_names[file - 1];
12529 char *dir = NULL;
12530
12531 if (fe->dir_index)
12532 dir = lh->include_dirs[fe->dir_index - 1];
12533
12534 dwarf2_start_subfile (fe->name, dir, comp_dir);
12535 }
12536
12537 /* Decode the table. */
12538 while (!end_sequence)
12539 {
12540 op_code = read_1_byte (abfd, line_ptr);
12541 line_ptr += 1;
12542 if (line_ptr > line_end)
12543 {
12544 dwarf2_debug_line_missing_end_sequence_complaint ();
12545 break;
12546 }
12547
12548 if (op_code >= lh->opcode_base)
12549 {
12550 /* Special operand. */
12551 adj_opcode = op_code - lh->opcode_base;
12552 address += (((op_index + (adj_opcode / lh->line_range))
12553 / lh->maximum_ops_per_instruction)
12554 * lh->minimum_instruction_length);
12555 op_index = ((op_index + (adj_opcode / lh->line_range))
12556 % lh->maximum_ops_per_instruction);
12557 line += lh->line_base + (adj_opcode % lh->line_range);
12558 if (lh->num_file_names < file || file == 0)
12559 dwarf2_debug_line_missing_file_complaint ();
12560 /* For now we ignore lines not starting on an
12561 instruction boundary. */
12562 else if (op_index == 0)
12563 {
12564 lh->file_names[file - 1].included_p = 1;
12565 if (!decode_for_pst_p && is_stmt)
12566 {
12567 if (last_subfile != current_subfile)
12568 {
12569 addr = gdbarch_addr_bits_remove (gdbarch, address);
12570 if (last_subfile)
12571 (*p_record_line) (last_subfile, 0, addr);
12572 last_subfile = current_subfile;
12573 }
12574 /* Append row to matrix using current values. */
12575 addr = gdbarch_addr_bits_remove (gdbarch, address);
12576 (*p_record_line) (current_subfile, line, addr);
12577 }
12578 }
12579 basic_block = 0;
12580 }
12581 else switch (op_code)
12582 {
12583 case DW_LNS_extended_op:
12584 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12585 &bytes_read);
12586 line_ptr += bytes_read;
12587 extended_end = line_ptr + extended_len;
12588 extended_op = read_1_byte (abfd, line_ptr);
12589 line_ptr += 1;
12590 switch (extended_op)
12591 {
12592 case DW_LNE_end_sequence:
12593 p_record_line = record_line;
12594 end_sequence = 1;
12595 break;
12596 case DW_LNE_set_address:
12597 address = read_address (abfd, line_ptr, cu, &bytes_read);
12598
12599 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12600 {
12601 /* This line table is for a function which has been
12602 GCd by the linker. Ignore it. PR gdb/12528 */
12603
12604 long line_offset
12605 = line_ptr - dwarf2_per_objfile->line.buffer;
12606
12607 complaint (&symfile_complaints,
12608 _(".debug_line address at offset 0x%lx is 0 "
12609 "[in module %s]"),
12610 line_offset, objfile->name);
12611 p_record_line = noop_record_line;
12612 }
12613
12614 op_index = 0;
12615 line_ptr += bytes_read;
12616 address += baseaddr;
12617 break;
12618 case DW_LNE_define_file:
12619 {
12620 char *cur_file;
12621 unsigned int dir_index, mod_time, length;
12622
12623 cur_file = read_direct_string (abfd, line_ptr,
12624 &bytes_read);
12625 line_ptr += bytes_read;
12626 dir_index =
12627 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12628 line_ptr += bytes_read;
12629 mod_time =
12630 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12631 line_ptr += bytes_read;
12632 length =
12633 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12634 line_ptr += bytes_read;
12635 add_file_name (lh, cur_file, dir_index, mod_time, length);
12636 }
12637 break;
12638 case DW_LNE_set_discriminator:
12639 /* The discriminator is not interesting to the debugger;
12640 just ignore it. */
12641 line_ptr = extended_end;
12642 break;
12643 default:
12644 complaint (&symfile_complaints,
12645 _("mangled .debug_line section"));
12646 return;
12647 }
12648 /* Make sure that we parsed the extended op correctly. If e.g.
12649 we expected a different address size than the producer used,
12650 we may have read the wrong number of bytes. */
12651 if (line_ptr != extended_end)
12652 {
12653 complaint (&symfile_complaints,
12654 _("mangled .debug_line section"));
12655 return;
12656 }
12657 break;
12658 case DW_LNS_copy:
12659 if (lh->num_file_names < file || file == 0)
12660 dwarf2_debug_line_missing_file_complaint ();
12661 else
12662 {
12663 lh->file_names[file - 1].included_p = 1;
12664 if (!decode_for_pst_p && is_stmt)
12665 {
12666 if (last_subfile != current_subfile)
12667 {
12668 addr = gdbarch_addr_bits_remove (gdbarch, address);
12669 if (last_subfile)
12670 (*p_record_line) (last_subfile, 0, addr);
12671 last_subfile = current_subfile;
12672 }
12673 addr = gdbarch_addr_bits_remove (gdbarch, address);
12674 (*p_record_line) (current_subfile, line, addr);
12675 }
12676 }
12677 basic_block = 0;
12678 break;
12679 case DW_LNS_advance_pc:
12680 {
12681 CORE_ADDR adjust
12682 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12683
12684 address += (((op_index + adjust)
12685 / lh->maximum_ops_per_instruction)
12686 * lh->minimum_instruction_length);
12687 op_index = ((op_index + adjust)
12688 % lh->maximum_ops_per_instruction);
12689 line_ptr += bytes_read;
12690 }
12691 break;
12692 case DW_LNS_advance_line:
12693 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12694 line_ptr += bytes_read;
12695 break;
12696 case DW_LNS_set_file:
12697 {
12698 /* The arrays lh->include_dirs and lh->file_names are
12699 0-based, but the directory and file name numbers in
12700 the statement program are 1-based. */
12701 struct file_entry *fe;
12702 char *dir = NULL;
12703
12704 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12705 line_ptr += bytes_read;
12706 if (lh->num_file_names < file || file == 0)
12707 dwarf2_debug_line_missing_file_complaint ();
12708 else
12709 {
12710 fe = &lh->file_names[file - 1];
12711 if (fe->dir_index)
12712 dir = lh->include_dirs[fe->dir_index - 1];
12713 if (!decode_for_pst_p)
12714 {
12715 last_subfile = current_subfile;
12716 dwarf2_start_subfile (fe->name, dir, comp_dir);
12717 }
12718 }
12719 }
12720 break;
12721 case DW_LNS_set_column:
12722 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12723 line_ptr += bytes_read;
12724 break;
12725 case DW_LNS_negate_stmt:
12726 is_stmt = (!is_stmt);
12727 break;
12728 case DW_LNS_set_basic_block:
12729 basic_block = 1;
12730 break;
12731 /* Add to the address register of the state machine the
12732 address increment value corresponding to special opcode
12733 255. I.e., this value is scaled by the minimum
12734 instruction length since special opcode 255 would have
12735 scaled the increment. */
12736 case DW_LNS_const_add_pc:
12737 {
12738 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
12739
12740 address += (((op_index + adjust)
12741 / lh->maximum_ops_per_instruction)
12742 * lh->minimum_instruction_length);
12743 op_index = ((op_index + adjust)
12744 % lh->maximum_ops_per_instruction);
12745 }
12746 break;
12747 case DW_LNS_fixed_advance_pc:
12748 address += read_2_bytes (abfd, line_ptr);
12749 op_index = 0;
12750 line_ptr += 2;
12751 break;
12752 default:
12753 {
12754 /* Unknown standard opcode, ignore it. */
12755 int i;
12756
12757 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
12758 {
12759 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12760 line_ptr += bytes_read;
12761 }
12762 }
12763 }
12764 }
12765 if (lh->num_file_names < file || file == 0)
12766 dwarf2_debug_line_missing_file_complaint ();
12767 else
12768 {
12769 lh->file_names[file - 1].included_p = 1;
12770 if (!decode_for_pst_p)
12771 {
12772 addr = gdbarch_addr_bits_remove (gdbarch, address);
12773 (*p_record_line) (current_subfile, 0, addr);
12774 }
12775 }
12776 }
12777 }
12778
12779 /* Decode the Line Number Program (LNP) for the given line_header
12780 structure and CU. The actual information extracted and the type
12781 of structures created from the LNP depends on the value of PST.
12782
12783 1. If PST is NULL, then this procedure uses the data from the program
12784 to create all necessary symbol tables, and their linetables.
12785
12786 2. If PST is not NULL, this procedure reads the program to determine
12787 the list of files included by the unit represented by PST, and
12788 builds all the associated partial symbol tables.
12789
12790 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12791 It is used for relative paths in the line table.
12792 NOTE: When processing partial symtabs (pst != NULL),
12793 comp_dir == pst->dirname.
12794
12795 NOTE: It is important that psymtabs have the same file name (via strcmp)
12796 as the corresponding symtab. Since COMP_DIR is not used in the name of the
12797 symtab we don't use it in the name of the psymtabs we create.
12798 E.g. expand_line_sal requires this when finding psymtabs to expand.
12799 A good testcase for this is mb-inline.exp. */
12800
12801 static void
12802 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
12803 struct dwarf2_cu *cu, struct partial_symtab *pst,
12804 int want_line_info)
12805 {
12806 struct objfile *objfile = cu->objfile;
12807 const int decode_for_pst_p = (pst != NULL);
12808 struct subfile *first_subfile = current_subfile;
12809
12810 if (want_line_info)
12811 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
12812
12813 if (decode_for_pst_p)
12814 {
12815 int file_index;
12816
12817 /* Now that we're done scanning the Line Header Program, we can
12818 create the psymtab of each included file. */
12819 for (file_index = 0; file_index < lh->num_file_names; file_index++)
12820 if (lh->file_names[file_index].included_p == 1)
12821 {
12822 char *include_name =
12823 psymtab_include_file_name (lh, file_index, pst, comp_dir);
12824 if (include_name != NULL)
12825 dwarf2_create_include_psymtab (include_name, pst, objfile);
12826 }
12827 }
12828 else
12829 {
12830 /* Make sure a symtab is created for every file, even files
12831 which contain only variables (i.e. no code with associated
12832 line numbers). */
12833 int i;
12834
12835 for (i = 0; i < lh->num_file_names; i++)
12836 {
12837 char *dir = NULL;
12838 struct file_entry *fe;
12839
12840 fe = &lh->file_names[i];
12841 if (fe->dir_index)
12842 dir = lh->include_dirs[fe->dir_index - 1];
12843 dwarf2_start_subfile (fe->name, dir, comp_dir);
12844
12845 /* Skip the main file; we don't need it, and it must be
12846 allocated last, so that it will show up before the
12847 non-primary symtabs in the objfile's symtab list. */
12848 if (current_subfile == first_subfile)
12849 continue;
12850
12851 if (current_subfile->symtab == NULL)
12852 current_subfile->symtab = allocate_symtab (current_subfile->name,
12853 objfile);
12854 fe->symtab = current_subfile->symtab;
12855 }
12856 }
12857 }
12858
12859 /* Start a subfile for DWARF. FILENAME is the name of the file and
12860 DIRNAME the name of the source directory which contains FILENAME
12861 or NULL if not known. COMP_DIR is the compilation directory for the
12862 linetable's compilation unit or NULL if not known.
12863 This routine tries to keep line numbers from identical absolute and
12864 relative file names in a common subfile.
12865
12866 Using the `list' example from the GDB testsuite, which resides in
12867 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
12868 of /srcdir/list0.c yields the following debugging information for list0.c:
12869
12870 DW_AT_name: /srcdir/list0.c
12871 DW_AT_comp_dir: /compdir
12872 files.files[0].name: list0.h
12873 files.files[0].dir: /srcdir
12874 files.files[1].name: list0.c
12875 files.files[1].dir: /srcdir
12876
12877 The line number information for list0.c has to end up in a single
12878 subfile, so that `break /srcdir/list0.c:1' works as expected.
12879 start_subfile will ensure that this happens provided that we pass the
12880 concatenation of files.files[1].dir and files.files[1].name as the
12881 subfile's name. */
12882
12883 static void
12884 dwarf2_start_subfile (char *filename, const char *dirname,
12885 const char *comp_dir)
12886 {
12887 char *fullname;
12888
12889 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
12890 `start_symtab' will always pass the contents of DW_AT_comp_dir as
12891 second argument to start_subfile. To be consistent, we do the
12892 same here. In order not to lose the line information directory,
12893 we concatenate it to the filename when it makes sense.
12894 Note that the Dwarf3 standard says (speaking of filenames in line
12895 information): ``The directory index is ignored for file names
12896 that represent full path names''. Thus ignoring dirname in the
12897 `else' branch below isn't an issue. */
12898
12899 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
12900 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
12901 else
12902 fullname = filename;
12903
12904 start_subfile (fullname, comp_dir);
12905
12906 if (fullname != filename)
12907 xfree (fullname);
12908 }
12909
12910 static void
12911 var_decode_location (struct attribute *attr, struct symbol *sym,
12912 struct dwarf2_cu *cu)
12913 {
12914 struct objfile *objfile = cu->objfile;
12915 struct comp_unit_head *cu_header = &cu->header;
12916
12917 /* NOTE drow/2003-01-30: There used to be a comment and some special
12918 code here to turn a symbol with DW_AT_external and a
12919 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
12920 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
12921 with some versions of binutils) where shared libraries could have
12922 relocations against symbols in their debug information - the
12923 minimal symbol would have the right address, but the debug info
12924 would not. It's no longer necessary, because we will explicitly
12925 apply relocations when we read in the debug information now. */
12926
12927 /* A DW_AT_location attribute with no contents indicates that a
12928 variable has been optimized away. */
12929 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
12930 {
12931 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
12932 return;
12933 }
12934
12935 /* Handle one degenerate form of location expression specially, to
12936 preserve GDB's previous behavior when section offsets are
12937 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
12938 then mark this symbol as LOC_STATIC. */
12939
12940 if (attr_form_is_block (attr)
12941 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
12942 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
12943 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
12944 && (DW_BLOCK (attr)->size
12945 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
12946 {
12947 unsigned int dummy;
12948
12949 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
12950 SYMBOL_VALUE_ADDRESS (sym) =
12951 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
12952 else
12953 SYMBOL_VALUE_ADDRESS (sym) =
12954 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
12955 SYMBOL_CLASS (sym) = LOC_STATIC;
12956 fixup_symbol_section (sym, objfile);
12957 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
12958 SYMBOL_SECTION (sym));
12959 return;
12960 }
12961
12962 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
12963 expression evaluator, and use LOC_COMPUTED only when necessary
12964 (i.e. when the value of a register or memory location is
12965 referenced, or a thread-local block, etc.). Then again, it might
12966 not be worthwhile. I'm assuming that it isn't unless performance
12967 or memory numbers show me otherwise. */
12968
12969 dwarf2_symbol_mark_computed (attr, sym, cu);
12970 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12971
12972 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
12973 cu->has_loclist = 1;
12974 }
12975
12976 /* Given a pointer to a DWARF information entry, figure out if we need
12977 to make a symbol table entry for it, and if so, create a new entry
12978 and return a pointer to it.
12979 If TYPE is NULL, determine symbol type from the die, otherwise
12980 used the passed type.
12981 If SPACE is not NULL, use it to hold the new symbol. If it is
12982 NULL, allocate a new symbol on the objfile's obstack. */
12983
12984 static struct symbol *
12985 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
12986 struct symbol *space)
12987 {
12988 struct objfile *objfile = cu->objfile;
12989 struct symbol *sym = NULL;
12990 char *name;
12991 struct attribute *attr = NULL;
12992 struct attribute *attr2 = NULL;
12993 CORE_ADDR baseaddr;
12994 struct pending **list_to_add = NULL;
12995
12996 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12997
12998 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12999
13000 name = dwarf2_name (die, cu);
13001 if (name)
13002 {
13003 const char *linkagename;
13004 int suppress_add = 0;
13005
13006 if (space)
13007 sym = space;
13008 else
13009 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13010 OBJSTAT (objfile, n_syms++);
13011
13012 /* Cache this symbol's name and the name's demangled form (if any). */
13013 SYMBOL_SET_LANGUAGE (sym, cu->language);
13014 linkagename = dwarf2_physname (name, die, cu);
13015 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13016
13017 /* Fortran does not have mangling standard and the mangling does differ
13018 between gfortran, iFort etc. */
13019 if (cu->language == language_fortran
13020 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13021 symbol_set_demangled_name (&(sym->ginfo),
13022 (char *) dwarf2_full_name (name, die, cu),
13023 NULL);
13024
13025 /* Default assumptions.
13026 Use the passed type or decode it from the die. */
13027 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13028 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13029 if (type != NULL)
13030 SYMBOL_TYPE (sym) = type;
13031 else
13032 SYMBOL_TYPE (sym) = die_type (die, cu);
13033 attr = dwarf2_attr (die,
13034 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13035 cu);
13036 if (attr)
13037 {
13038 SYMBOL_LINE (sym) = DW_UNSND (attr);
13039 }
13040
13041 attr = dwarf2_attr (die,
13042 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13043 cu);
13044 if (attr)
13045 {
13046 int file_index = DW_UNSND (attr);
13047
13048 if (cu->line_header == NULL
13049 || file_index > cu->line_header->num_file_names)
13050 complaint (&symfile_complaints,
13051 _("file index out of range"));
13052 else if (file_index > 0)
13053 {
13054 struct file_entry *fe;
13055
13056 fe = &cu->line_header->file_names[file_index - 1];
13057 SYMBOL_SYMTAB (sym) = fe->symtab;
13058 }
13059 }
13060
13061 switch (die->tag)
13062 {
13063 case DW_TAG_label:
13064 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13065 if (attr)
13066 {
13067 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13068 }
13069 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13070 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13071 SYMBOL_CLASS (sym) = LOC_LABEL;
13072 add_symbol_to_list (sym, cu->list_in_scope);
13073 break;
13074 case DW_TAG_subprogram:
13075 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13076 finish_block. */
13077 SYMBOL_CLASS (sym) = LOC_BLOCK;
13078 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13079 if ((attr2 && (DW_UNSND (attr2) != 0))
13080 || cu->language == language_ada)
13081 {
13082 /* Subprograms marked external are stored as a global symbol.
13083 Ada subprograms, whether marked external or not, are always
13084 stored as a global symbol, because we want to be able to
13085 access them globally. For instance, we want to be able
13086 to break on a nested subprogram without having to
13087 specify the context. */
13088 list_to_add = &global_symbols;
13089 }
13090 else
13091 {
13092 list_to_add = cu->list_in_scope;
13093 }
13094 break;
13095 case DW_TAG_inlined_subroutine:
13096 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13097 finish_block. */
13098 SYMBOL_CLASS (sym) = LOC_BLOCK;
13099 SYMBOL_INLINED (sym) = 1;
13100 list_to_add = cu->list_in_scope;
13101 break;
13102 case DW_TAG_template_value_param:
13103 suppress_add = 1;
13104 /* Fall through. */
13105 case DW_TAG_constant:
13106 case DW_TAG_variable:
13107 case DW_TAG_member:
13108 /* Compilation with minimal debug info may result in
13109 variables with missing type entries. Change the
13110 misleading `void' type to something sensible. */
13111 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13112 SYMBOL_TYPE (sym)
13113 = objfile_type (objfile)->nodebug_data_symbol;
13114
13115 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13116 /* In the case of DW_TAG_member, we should only be called for
13117 static const members. */
13118 if (die->tag == DW_TAG_member)
13119 {
13120 /* dwarf2_add_field uses die_is_declaration,
13121 so we do the same. */
13122 gdb_assert (die_is_declaration (die, cu));
13123 gdb_assert (attr);
13124 }
13125 if (attr)
13126 {
13127 dwarf2_const_value (attr, sym, cu);
13128 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13129 if (!suppress_add)
13130 {
13131 if (attr2 && (DW_UNSND (attr2) != 0))
13132 list_to_add = &global_symbols;
13133 else
13134 list_to_add = cu->list_in_scope;
13135 }
13136 break;
13137 }
13138 attr = dwarf2_attr (die, DW_AT_location, cu);
13139 if (attr)
13140 {
13141 var_decode_location (attr, sym, cu);
13142 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13143 if (SYMBOL_CLASS (sym) == LOC_STATIC
13144 && SYMBOL_VALUE_ADDRESS (sym) == 0
13145 && !dwarf2_per_objfile->has_section_at_zero)
13146 {
13147 /* When a static variable is eliminated by the linker,
13148 the corresponding debug information is not stripped
13149 out, but the variable address is set to null;
13150 do not add such variables into symbol table. */
13151 }
13152 else if (attr2 && (DW_UNSND (attr2) != 0))
13153 {
13154 /* Workaround gfortran PR debug/40040 - it uses
13155 DW_AT_location for variables in -fPIC libraries which may
13156 get overriden by other libraries/executable and get
13157 a different address. Resolve it by the minimal symbol
13158 which may come from inferior's executable using copy
13159 relocation. Make this workaround only for gfortran as for
13160 other compilers GDB cannot guess the minimal symbol
13161 Fortran mangling kind. */
13162 if (cu->language == language_fortran && die->parent
13163 && die->parent->tag == DW_TAG_module
13164 && cu->producer
13165 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13166 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13167
13168 /* A variable with DW_AT_external is never static,
13169 but it may be block-scoped. */
13170 list_to_add = (cu->list_in_scope == &file_symbols
13171 ? &global_symbols : cu->list_in_scope);
13172 }
13173 else
13174 list_to_add = cu->list_in_scope;
13175 }
13176 else
13177 {
13178 /* We do not know the address of this symbol.
13179 If it is an external symbol and we have type information
13180 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13181 The address of the variable will then be determined from
13182 the minimal symbol table whenever the variable is
13183 referenced. */
13184 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13185 if (attr2 && (DW_UNSND (attr2) != 0)
13186 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13187 {
13188 /* A variable with DW_AT_external is never static, but it
13189 may be block-scoped. */
13190 list_to_add = (cu->list_in_scope == &file_symbols
13191 ? &global_symbols : cu->list_in_scope);
13192
13193 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13194 }
13195 else if (!die_is_declaration (die, cu))
13196 {
13197 /* Use the default LOC_OPTIMIZED_OUT class. */
13198 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13199 if (!suppress_add)
13200 list_to_add = cu->list_in_scope;
13201 }
13202 }
13203 break;
13204 case DW_TAG_formal_parameter:
13205 /* If we are inside a function, mark this as an argument. If
13206 not, we might be looking at an argument to an inlined function
13207 when we do not have enough information to show inlined frames;
13208 pretend it's a local variable in that case so that the user can
13209 still see it. */
13210 if (context_stack_depth > 0
13211 && context_stack[context_stack_depth - 1].name != NULL)
13212 SYMBOL_IS_ARGUMENT (sym) = 1;
13213 attr = dwarf2_attr (die, DW_AT_location, cu);
13214 if (attr)
13215 {
13216 var_decode_location (attr, sym, cu);
13217 }
13218 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13219 if (attr)
13220 {
13221 dwarf2_const_value (attr, sym, cu);
13222 }
13223
13224 list_to_add = cu->list_in_scope;
13225 break;
13226 case DW_TAG_unspecified_parameters:
13227 /* From varargs functions; gdb doesn't seem to have any
13228 interest in this information, so just ignore it for now.
13229 (FIXME?) */
13230 break;
13231 case DW_TAG_template_type_param:
13232 suppress_add = 1;
13233 /* Fall through. */
13234 case DW_TAG_class_type:
13235 case DW_TAG_interface_type:
13236 case DW_TAG_structure_type:
13237 case DW_TAG_union_type:
13238 case DW_TAG_set_type:
13239 case DW_TAG_enumeration_type:
13240 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13241 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13242
13243 {
13244 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13245 really ever be static objects: otherwise, if you try
13246 to, say, break of a class's method and you're in a file
13247 which doesn't mention that class, it won't work unless
13248 the check for all static symbols in lookup_symbol_aux
13249 saves you. See the OtherFileClass tests in
13250 gdb.c++/namespace.exp. */
13251
13252 if (!suppress_add)
13253 {
13254 list_to_add = (cu->list_in_scope == &file_symbols
13255 && (cu->language == language_cplus
13256 || cu->language == language_java)
13257 ? &global_symbols : cu->list_in_scope);
13258
13259 /* The semantics of C++ state that "struct foo {
13260 ... }" also defines a typedef for "foo". A Java
13261 class declaration also defines a typedef for the
13262 class. */
13263 if (cu->language == language_cplus
13264 || cu->language == language_java
13265 || cu->language == language_ada)
13266 {
13267 /* The symbol's name is already allocated along
13268 with this objfile, so we don't need to
13269 duplicate it for the type. */
13270 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13271 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13272 }
13273 }
13274 }
13275 break;
13276 case DW_TAG_typedef:
13277 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13278 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13279 list_to_add = cu->list_in_scope;
13280 break;
13281 case DW_TAG_base_type:
13282 case DW_TAG_subrange_type:
13283 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13284 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13285 list_to_add = cu->list_in_scope;
13286 break;
13287 case DW_TAG_enumerator:
13288 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13289 if (attr)
13290 {
13291 dwarf2_const_value (attr, sym, cu);
13292 }
13293 {
13294 /* NOTE: carlton/2003-11-10: See comment above in the
13295 DW_TAG_class_type, etc. block. */
13296
13297 list_to_add = (cu->list_in_scope == &file_symbols
13298 && (cu->language == language_cplus
13299 || cu->language == language_java)
13300 ? &global_symbols : cu->list_in_scope);
13301 }
13302 break;
13303 case DW_TAG_namespace:
13304 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13305 list_to_add = &global_symbols;
13306 break;
13307 default:
13308 /* Not a tag we recognize. Hopefully we aren't processing
13309 trash data, but since we must specifically ignore things
13310 we don't recognize, there is nothing else we should do at
13311 this point. */
13312 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13313 dwarf_tag_name (die->tag));
13314 break;
13315 }
13316
13317 if (suppress_add)
13318 {
13319 sym->hash_next = objfile->template_symbols;
13320 objfile->template_symbols = sym;
13321 list_to_add = NULL;
13322 }
13323
13324 if (list_to_add != NULL)
13325 add_symbol_to_list (sym, list_to_add);
13326
13327 /* For the benefit of old versions of GCC, check for anonymous
13328 namespaces based on the demangled name. */
13329 if (!processing_has_namespace_info
13330 && cu->language == language_cplus)
13331 cp_scan_for_anonymous_namespaces (sym, objfile);
13332 }
13333 return (sym);
13334 }
13335
13336 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13337
13338 static struct symbol *
13339 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13340 {
13341 return new_symbol_full (die, type, cu, NULL);
13342 }
13343
13344 /* Given an attr with a DW_FORM_dataN value in host byte order,
13345 zero-extend it as appropriate for the symbol's type. The DWARF
13346 standard (v4) is not entirely clear about the meaning of using
13347 DW_FORM_dataN for a constant with a signed type, where the type is
13348 wider than the data. The conclusion of a discussion on the DWARF
13349 list was that this is unspecified. We choose to always zero-extend
13350 because that is the interpretation long in use by GCC. */
13351
13352 static gdb_byte *
13353 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13354 const char *name, struct obstack *obstack,
13355 struct dwarf2_cu *cu, LONGEST *value, int bits)
13356 {
13357 struct objfile *objfile = cu->objfile;
13358 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13359 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13360 LONGEST l = DW_UNSND (attr);
13361
13362 if (bits < sizeof (*value) * 8)
13363 {
13364 l &= ((LONGEST) 1 << bits) - 1;
13365 *value = l;
13366 }
13367 else if (bits == sizeof (*value) * 8)
13368 *value = l;
13369 else
13370 {
13371 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13372 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13373 return bytes;
13374 }
13375
13376 return NULL;
13377 }
13378
13379 /* Read a constant value from an attribute. Either set *VALUE, or if
13380 the value does not fit in *VALUE, set *BYTES - either already
13381 allocated on the objfile obstack, or newly allocated on OBSTACK,
13382 or, set *BATON, if we translated the constant to a location
13383 expression. */
13384
13385 static void
13386 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13387 const char *name, struct obstack *obstack,
13388 struct dwarf2_cu *cu,
13389 LONGEST *value, gdb_byte **bytes,
13390 struct dwarf2_locexpr_baton **baton)
13391 {
13392 struct objfile *objfile = cu->objfile;
13393 struct comp_unit_head *cu_header = &cu->header;
13394 struct dwarf_block *blk;
13395 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13396 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13397
13398 *value = 0;
13399 *bytes = NULL;
13400 *baton = NULL;
13401
13402 switch (attr->form)
13403 {
13404 case DW_FORM_addr:
13405 case DW_FORM_GNU_addr_index:
13406 {
13407 gdb_byte *data;
13408
13409 if (TYPE_LENGTH (type) != cu_header->addr_size)
13410 dwarf2_const_value_length_mismatch_complaint (name,
13411 cu_header->addr_size,
13412 TYPE_LENGTH (type));
13413 /* Symbols of this form are reasonably rare, so we just
13414 piggyback on the existing location code rather than writing
13415 a new implementation of symbol_computed_ops. */
13416 *baton = obstack_alloc (&objfile->objfile_obstack,
13417 sizeof (struct dwarf2_locexpr_baton));
13418 (*baton)->per_cu = cu->per_cu;
13419 gdb_assert ((*baton)->per_cu);
13420
13421 (*baton)->size = 2 + cu_header->addr_size;
13422 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13423 (*baton)->data = data;
13424
13425 data[0] = DW_OP_addr;
13426 store_unsigned_integer (&data[1], cu_header->addr_size,
13427 byte_order, DW_ADDR (attr));
13428 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13429 }
13430 break;
13431 case DW_FORM_string:
13432 case DW_FORM_strp:
13433 case DW_FORM_GNU_str_index:
13434 /* DW_STRING is already allocated on the objfile obstack, point
13435 directly to it. */
13436 *bytes = (gdb_byte *) DW_STRING (attr);
13437 break;
13438 case DW_FORM_block1:
13439 case DW_FORM_block2:
13440 case DW_FORM_block4:
13441 case DW_FORM_block:
13442 case DW_FORM_exprloc:
13443 blk = DW_BLOCK (attr);
13444 if (TYPE_LENGTH (type) != blk->size)
13445 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13446 TYPE_LENGTH (type));
13447 *bytes = blk->data;
13448 break;
13449
13450 /* The DW_AT_const_value attributes are supposed to carry the
13451 symbol's value "represented as it would be on the target
13452 architecture." By the time we get here, it's already been
13453 converted to host endianness, so we just need to sign- or
13454 zero-extend it as appropriate. */
13455 case DW_FORM_data1:
13456 *bytes = dwarf2_const_value_data (attr, type, name,
13457 obstack, cu, value, 8);
13458 break;
13459 case DW_FORM_data2:
13460 *bytes = dwarf2_const_value_data (attr, type, name,
13461 obstack, cu, value, 16);
13462 break;
13463 case DW_FORM_data4:
13464 *bytes = dwarf2_const_value_data (attr, type, name,
13465 obstack, cu, value, 32);
13466 break;
13467 case DW_FORM_data8:
13468 *bytes = dwarf2_const_value_data (attr, type, name,
13469 obstack, cu, value, 64);
13470 break;
13471
13472 case DW_FORM_sdata:
13473 *value = DW_SND (attr);
13474 break;
13475
13476 case DW_FORM_udata:
13477 *value = DW_UNSND (attr);
13478 break;
13479
13480 default:
13481 complaint (&symfile_complaints,
13482 _("unsupported const value attribute form: '%s'"),
13483 dwarf_form_name (attr->form));
13484 *value = 0;
13485 break;
13486 }
13487 }
13488
13489
13490 /* Copy constant value from an attribute to a symbol. */
13491
13492 static void
13493 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13494 struct dwarf2_cu *cu)
13495 {
13496 struct objfile *objfile = cu->objfile;
13497 struct comp_unit_head *cu_header = &cu->header;
13498 LONGEST value;
13499 gdb_byte *bytes;
13500 struct dwarf2_locexpr_baton *baton;
13501
13502 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13503 SYMBOL_PRINT_NAME (sym),
13504 &objfile->objfile_obstack, cu,
13505 &value, &bytes, &baton);
13506
13507 if (baton != NULL)
13508 {
13509 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13510 SYMBOL_LOCATION_BATON (sym) = baton;
13511 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13512 }
13513 else if (bytes != NULL)
13514 {
13515 SYMBOL_VALUE_BYTES (sym) = bytes;
13516 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13517 }
13518 else
13519 {
13520 SYMBOL_VALUE (sym) = value;
13521 SYMBOL_CLASS (sym) = LOC_CONST;
13522 }
13523 }
13524
13525 /* Return the type of the die in question using its DW_AT_type attribute. */
13526
13527 static struct type *
13528 die_type (struct die_info *die, struct dwarf2_cu *cu)
13529 {
13530 struct attribute *type_attr;
13531
13532 type_attr = dwarf2_attr (die, DW_AT_type, cu);
13533 if (!type_attr)
13534 {
13535 /* A missing DW_AT_type represents a void type. */
13536 return objfile_type (cu->objfile)->builtin_void;
13537 }
13538
13539 return lookup_die_type (die, type_attr, cu);
13540 }
13541
13542 /* True iff CU's producer generates GNAT Ada auxiliary information
13543 that allows to find parallel types through that information instead
13544 of having to do expensive parallel lookups by type name. */
13545
13546 static int
13547 need_gnat_info (struct dwarf2_cu *cu)
13548 {
13549 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13550 of GNAT produces this auxiliary information, without any indication
13551 that it is produced. Part of enhancing the FSF version of GNAT
13552 to produce that information will be to put in place an indicator
13553 that we can use in order to determine whether the descriptive type
13554 info is available or not. One suggestion that has been made is
13555 to use a new attribute, attached to the CU die. For now, assume
13556 that the descriptive type info is not available. */
13557 return 0;
13558 }
13559
13560 /* Return the auxiliary type of the die in question using its
13561 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13562 attribute is not present. */
13563
13564 static struct type *
13565 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13566 {
13567 struct attribute *type_attr;
13568
13569 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13570 if (!type_attr)
13571 return NULL;
13572
13573 return lookup_die_type (die, type_attr, cu);
13574 }
13575
13576 /* If DIE has a descriptive_type attribute, then set the TYPE's
13577 descriptive type accordingly. */
13578
13579 static void
13580 set_descriptive_type (struct type *type, struct die_info *die,
13581 struct dwarf2_cu *cu)
13582 {
13583 struct type *descriptive_type = die_descriptive_type (die, cu);
13584
13585 if (descriptive_type)
13586 {
13587 ALLOCATE_GNAT_AUX_TYPE (type);
13588 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13589 }
13590 }
13591
13592 /* Return the containing type of the die in question using its
13593 DW_AT_containing_type attribute. */
13594
13595 static struct type *
13596 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13597 {
13598 struct attribute *type_attr;
13599
13600 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
13601 if (!type_attr)
13602 error (_("Dwarf Error: Problem turning containing type into gdb type "
13603 "[in module %s]"), cu->objfile->name);
13604
13605 return lookup_die_type (die, type_attr, cu);
13606 }
13607
13608 /* Look up the type of DIE in CU using its type attribute ATTR.
13609 If there is no type substitute an error marker. */
13610
13611 static struct type *
13612 lookup_die_type (struct die_info *die, struct attribute *attr,
13613 struct dwarf2_cu *cu)
13614 {
13615 struct objfile *objfile = cu->objfile;
13616 struct type *this_type;
13617
13618 /* First see if we have it cached. */
13619
13620 if (is_ref_attr (attr))
13621 {
13622 sect_offset offset = dwarf2_get_ref_die_offset (attr);
13623
13624 this_type = get_die_type_at_offset (offset, cu->per_cu);
13625 }
13626 else if (attr->form == DW_FORM_ref_sig8)
13627 {
13628 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13629
13630 /* sig_type will be NULL if the signatured type is missing from
13631 the debug info. */
13632 if (sig_type == NULL)
13633 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13634 "at 0x%x [in module %s]"),
13635 die->offset.sect_off, objfile->name);
13636
13637 gdb_assert (sig_type->per_cu.is_debug_types);
13638 /* If we haven't filled in type_offset_in_section yet, then we
13639 haven't read the type in yet. */
13640 this_type = NULL;
13641 if (sig_type->type_offset_in_section.sect_off != 0)
13642 {
13643 this_type =
13644 get_die_type_at_offset (sig_type->type_offset_in_section,
13645 &sig_type->per_cu);
13646 }
13647 }
13648 else
13649 {
13650 dump_die_for_error (die);
13651 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
13652 dwarf_attr_name (attr->name), objfile->name);
13653 }
13654
13655 /* If not cached we need to read it in. */
13656
13657 if (this_type == NULL)
13658 {
13659 struct die_info *type_die;
13660 struct dwarf2_cu *type_cu = cu;
13661
13662 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
13663 /* If we found the type now, it's probably because the type came
13664 from an inter-CU reference and the type's CU got expanded before
13665 ours. */
13666 this_type = get_die_type (type_die, type_cu);
13667 if (this_type == NULL)
13668 this_type = read_type_die_1 (type_die, type_cu);
13669 }
13670
13671 /* If we still don't have a type use an error marker. */
13672
13673 if (this_type == NULL)
13674 {
13675 char *message, *saved;
13676
13677 /* read_type_die already issued a complaint. */
13678 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
13679 objfile->name,
13680 cu->header.offset.sect_off,
13681 die->offset.sect_off);
13682 saved = obstack_copy0 (&objfile->objfile_obstack,
13683 message, strlen (message));
13684 xfree (message);
13685
13686 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
13687 }
13688
13689 return this_type;
13690 }
13691
13692 /* Return the type in DIE, CU.
13693 Returns NULL for invalid types.
13694
13695 This first does a lookup in the appropriate type_hash table,
13696 and only reads the die in if necessary.
13697
13698 NOTE: This can be called when reading in partial or full symbols. */
13699
13700 static struct type *
13701 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
13702 {
13703 struct type *this_type;
13704
13705 this_type = get_die_type (die, cu);
13706 if (this_type)
13707 return this_type;
13708
13709 return read_type_die_1 (die, cu);
13710 }
13711
13712 /* Read the type in DIE, CU.
13713 Returns NULL for invalid types. */
13714
13715 static struct type *
13716 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
13717 {
13718 struct type *this_type = NULL;
13719
13720 switch (die->tag)
13721 {
13722 case DW_TAG_class_type:
13723 case DW_TAG_interface_type:
13724 case DW_TAG_structure_type:
13725 case DW_TAG_union_type:
13726 this_type = read_structure_type (die, cu);
13727 break;
13728 case DW_TAG_enumeration_type:
13729 this_type = read_enumeration_type (die, cu);
13730 break;
13731 case DW_TAG_subprogram:
13732 case DW_TAG_subroutine_type:
13733 case DW_TAG_inlined_subroutine:
13734 this_type = read_subroutine_type (die, cu);
13735 break;
13736 case DW_TAG_array_type:
13737 this_type = read_array_type (die, cu);
13738 break;
13739 case DW_TAG_set_type:
13740 this_type = read_set_type (die, cu);
13741 break;
13742 case DW_TAG_pointer_type:
13743 this_type = read_tag_pointer_type (die, cu);
13744 break;
13745 case DW_TAG_ptr_to_member_type:
13746 this_type = read_tag_ptr_to_member_type (die, cu);
13747 break;
13748 case DW_TAG_reference_type:
13749 this_type = read_tag_reference_type (die, cu);
13750 break;
13751 case DW_TAG_const_type:
13752 this_type = read_tag_const_type (die, cu);
13753 break;
13754 case DW_TAG_volatile_type:
13755 this_type = read_tag_volatile_type (die, cu);
13756 break;
13757 case DW_TAG_string_type:
13758 this_type = read_tag_string_type (die, cu);
13759 break;
13760 case DW_TAG_typedef:
13761 this_type = read_typedef (die, cu);
13762 break;
13763 case DW_TAG_subrange_type:
13764 this_type = read_subrange_type (die, cu);
13765 break;
13766 case DW_TAG_base_type:
13767 this_type = read_base_type (die, cu);
13768 break;
13769 case DW_TAG_unspecified_type:
13770 this_type = read_unspecified_type (die, cu);
13771 break;
13772 case DW_TAG_namespace:
13773 this_type = read_namespace_type (die, cu);
13774 break;
13775 case DW_TAG_module:
13776 this_type = read_module_type (die, cu);
13777 break;
13778 default:
13779 complaint (&symfile_complaints,
13780 _("unexpected tag in read_type_die: '%s'"),
13781 dwarf_tag_name (die->tag));
13782 break;
13783 }
13784
13785 return this_type;
13786 }
13787
13788 /* See if we can figure out if the class lives in a namespace. We do
13789 this by looking for a member function; its demangled name will
13790 contain namespace info, if there is any.
13791 Return the computed name or NULL.
13792 Space for the result is allocated on the objfile's obstack.
13793 This is the full-die version of guess_partial_die_structure_name.
13794 In this case we know DIE has no useful parent. */
13795
13796 static char *
13797 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
13798 {
13799 struct die_info *spec_die;
13800 struct dwarf2_cu *spec_cu;
13801 struct die_info *child;
13802
13803 spec_cu = cu;
13804 spec_die = die_specification (die, &spec_cu);
13805 if (spec_die != NULL)
13806 {
13807 die = spec_die;
13808 cu = spec_cu;
13809 }
13810
13811 for (child = die->child;
13812 child != NULL;
13813 child = child->sibling)
13814 {
13815 if (child->tag == DW_TAG_subprogram)
13816 {
13817 struct attribute *attr;
13818
13819 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
13820 if (attr == NULL)
13821 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
13822 if (attr != NULL)
13823 {
13824 char *actual_name
13825 = language_class_name_from_physname (cu->language_defn,
13826 DW_STRING (attr));
13827 char *name = NULL;
13828
13829 if (actual_name != NULL)
13830 {
13831 char *die_name = dwarf2_name (die, cu);
13832
13833 if (die_name != NULL
13834 && strcmp (die_name, actual_name) != 0)
13835 {
13836 /* Strip off the class name from the full name.
13837 We want the prefix. */
13838 int die_name_len = strlen (die_name);
13839 int actual_name_len = strlen (actual_name);
13840
13841 /* Test for '::' as a sanity check. */
13842 if (actual_name_len > die_name_len + 2
13843 && actual_name[actual_name_len
13844 - die_name_len - 1] == ':')
13845 name =
13846 obsavestring (actual_name,
13847 actual_name_len - die_name_len - 2,
13848 &cu->objfile->objfile_obstack);
13849 }
13850 }
13851 xfree (actual_name);
13852 return name;
13853 }
13854 }
13855 }
13856
13857 return NULL;
13858 }
13859
13860 /* GCC might emit a nameless typedef that has a linkage name. Determine the
13861 prefix part in such case. See
13862 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13863
13864 static char *
13865 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
13866 {
13867 struct attribute *attr;
13868 char *base;
13869
13870 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
13871 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
13872 return NULL;
13873
13874 attr = dwarf2_attr (die, DW_AT_name, cu);
13875 if (attr != NULL && DW_STRING (attr) != NULL)
13876 return NULL;
13877
13878 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
13879 if (attr == NULL)
13880 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
13881 if (attr == NULL || DW_STRING (attr) == NULL)
13882 return NULL;
13883
13884 /* dwarf2_name had to be already called. */
13885 gdb_assert (DW_STRING_IS_CANONICAL (attr));
13886
13887 /* Strip the base name, keep any leading namespaces/classes. */
13888 base = strrchr (DW_STRING (attr), ':');
13889 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
13890 return "";
13891
13892 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
13893 &cu->objfile->objfile_obstack);
13894 }
13895
13896 /* Return the name of the namespace/class that DIE is defined within,
13897 or "" if we can't tell. The caller should not xfree the result.
13898
13899 For example, if we're within the method foo() in the following
13900 code:
13901
13902 namespace N {
13903 class C {
13904 void foo () {
13905 }
13906 };
13907 }
13908
13909 then determine_prefix on foo's die will return "N::C". */
13910
13911 static const char *
13912 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
13913 {
13914 struct die_info *parent, *spec_die;
13915 struct dwarf2_cu *spec_cu;
13916 struct type *parent_type;
13917 char *retval;
13918
13919 if (cu->language != language_cplus && cu->language != language_java
13920 && cu->language != language_fortran)
13921 return "";
13922
13923 retval = anonymous_struct_prefix (die, cu);
13924 if (retval)
13925 return retval;
13926
13927 /* We have to be careful in the presence of DW_AT_specification.
13928 For example, with GCC 3.4, given the code
13929
13930 namespace N {
13931 void foo() {
13932 // Definition of N::foo.
13933 }
13934 }
13935
13936 then we'll have a tree of DIEs like this:
13937
13938 1: DW_TAG_compile_unit
13939 2: DW_TAG_namespace // N
13940 3: DW_TAG_subprogram // declaration of N::foo
13941 4: DW_TAG_subprogram // definition of N::foo
13942 DW_AT_specification // refers to die #3
13943
13944 Thus, when processing die #4, we have to pretend that we're in
13945 the context of its DW_AT_specification, namely the contex of die
13946 #3. */
13947 spec_cu = cu;
13948 spec_die = die_specification (die, &spec_cu);
13949 if (spec_die == NULL)
13950 parent = die->parent;
13951 else
13952 {
13953 parent = spec_die->parent;
13954 cu = spec_cu;
13955 }
13956
13957 if (parent == NULL)
13958 return "";
13959 else if (parent->building_fullname)
13960 {
13961 const char *name;
13962 const char *parent_name;
13963
13964 /* It has been seen on RealView 2.2 built binaries,
13965 DW_TAG_template_type_param types actually _defined_ as
13966 children of the parent class:
13967
13968 enum E {};
13969 template class <class Enum> Class{};
13970 Class<enum E> class_e;
13971
13972 1: DW_TAG_class_type (Class)
13973 2: DW_TAG_enumeration_type (E)
13974 3: DW_TAG_enumerator (enum1:0)
13975 3: DW_TAG_enumerator (enum2:1)
13976 ...
13977 2: DW_TAG_template_type_param
13978 DW_AT_type DW_FORM_ref_udata (E)
13979
13980 Besides being broken debug info, it can put GDB into an
13981 infinite loop. Consider:
13982
13983 When we're building the full name for Class<E>, we'll start
13984 at Class, and go look over its template type parameters,
13985 finding E. We'll then try to build the full name of E, and
13986 reach here. We're now trying to build the full name of E,
13987 and look over the parent DIE for containing scope. In the
13988 broken case, if we followed the parent DIE of E, we'd again
13989 find Class, and once again go look at its template type
13990 arguments, etc., etc. Simply don't consider such parent die
13991 as source-level parent of this die (it can't be, the language
13992 doesn't allow it), and break the loop here. */
13993 name = dwarf2_name (die, cu);
13994 parent_name = dwarf2_name (parent, cu);
13995 complaint (&symfile_complaints,
13996 _("template param type '%s' defined within parent '%s'"),
13997 name ? name : "<unknown>",
13998 parent_name ? parent_name : "<unknown>");
13999 return "";
14000 }
14001 else
14002 switch (parent->tag)
14003 {
14004 case DW_TAG_namespace:
14005 parent_type = read_type_die (parent, cu);
14006 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14007 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14008 Work around this problem here. */
14009 if (cu->language == language_cplus
14010 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14011 return "";
14012 /* We give a name to even anonymous namespaces. */
14013 return TYPE_TAG_NAME (parent_type);
14014 case DW_TAG_class_type:
14015 case DW_TAG_interface_type:
14016 case DW_TAG_structure_type:
14017 case DW_TAG_union_type:
14018 case DW_TAG_module:
14019 parent_type = read_type_die (parent, cu);
14020 if (TYPE_TAG_NAME (parent_type) != NULL)
14021 return TYPE_TAG_NAME (parent_type);
14022 else
14023 /* An anonymous structure is only allowed non-static data
14024 members; no typedefs, no member functions, et cetera.
14025 So it does not need a prefix. */
14026 return "";
14027 case DW_TAG_compile_unit:
14028 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14029 if (cu->language == language_cplus
14030 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14031 && die->child != NULL
14032 && (die->tag == DW_TAG_class_type
14033 || die->tag == DW_TAG_structure_type
14034 || die->tag == DW_TAG_union_type))
14035 {
14036 char *name = guess_full_die_structure_name (die, cu);
14037 if (name != NULL)
14038 return name;
14039 }
14040 return "";
14041 default:
14042 return determine_prefix (parent, cu);
14043 }
14044 }
14045
14046 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14047 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14048 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14049 an obconcat, otherwise allocate storage for the result. The CU argument is
14050 used to determine the language and hence, the appropriate separator. */
14051
14052 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14053
14054 static char *
14055 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14056 int physname, struct dwarf2_cu *cu)
14057 {
14058 const char *lead = "";
14059 const char *sep;
14060
14061 if (suffix == NULL || suffix[0] == '\0'
14062 || prefix == NULL || prefix[0] == '\0')
14063 sep = "";
14064 else if (cu->language == language_java)
14065 sep = ".";
14066 else if (cu->language == language_fortran && physname)
14067 {
14068 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14069 DW_AT_MIPS_linkage_name is preferred and used instead. */
14070
14071 lead = "__";
14072 sep = "_MOD_";
14073 }
14074 else
14075 sep = "::";
14076
14077 if (prefix == NULL)
14078 prefix = "";
14079 if (suffix == NULL)
14080 suffix = "";
14081
14082 if (obs == NULL)
14083 {
14084 char *retval
14085 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14086
14087 strcpy (retval, lead);
14088 strcat (retval, prefix);
14089 strcat (retval, sep);
14090 strcat (retval, suffix);
14091 return retval;
14092 }
14093 else
14094 {
14095 /* We have an obstack. */
14096 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14097 }
14098 }
14099
14100 /* Return sibling of die, NULL if no sibling. */
14101
14102 static struct die_info *
14103 sibling_die (struct die_info *die)
14104 {
14105 return die->sibling;
14106 }
14107
14108 /* Get name of a die, return NULL if not found. */
14109
14110 static char *
14111 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14112 struct obstack *obstack)
14113 {
14114 if (name && cu->language == language_cplus)
14115 {
14116 char *canon_name = cp_canonicalize_string (name);
14117
14118 if (canon_name != NULL)
14119 {
14120 if (strcmp (canon_name, name) != 0)
14121 name = obsavestring (canon_name, strlen (canon_name),
14122 obstack);
14123 xfree (canon_name);
14124 }
14125 }
14126
14127 return name;
14128 }
14129
14130 /* Get name of a die, return NULL if not found. */
14131
14132 static char *
14133 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14134 {
14135 struct attribute *attr;
14136
14137 attr = dwarf2_attr (die, DW_AT_name, cu);
14138 if ((!attr || !DW_STRING (attr))
14139 && die->tag != DW_TAG_class_type
14140 && die->tag != DW_TAG_interface_type
14141 && die->tag != DW_TAG_structure_type
14142 && die->tag != DW_TAG_union_type)
14143 return NULL;
14144
14145 switch (die->tag)
14146 {
14147 case DW_TAG_compile_unit:
14148 /* Compilation units have a DW_AT_name that is a filename, not
14149 a source language identifier. */
14150 case DW_TAG_enumeration_type:
14151 case DW_TAG_enumerator:
14152 /* These tags always have simple identifiers already; no need
14153 to canonicalize them. */
14154 return DW_STRING (attr);
14155
14156 case DW_TAG_subprogram:
14157 /* Java constructors will all be named "<init>", so return
14158 the class name when we see this special case. */
14159 if (cu->language == language_java
14160 && DW_STRING (attr) != NULL
14161 && strcmp (DW_STRING (attr), "<init>") == 0)
14162 {
14163 struct dwarf2_cu *spec_cu = cu;
14164 struct die_info *spec_die;
14165
14166 /* GCJ will output '<init>' for Java constructor names.
14167 For this special case, return the name of the parent class. */
14168
14169 /* GCJ may output suprogram DIEs with AT_specification set.
14170 If so, use the name of the specified DIE. */
14171 spec_die = die_specification (die, &spec_cu);
14172 if (spec_die != NULL)
14173 return dwarf2_name (spec_die, spec_cu);
14174
14175 do
14176 {
14177 die = die->parent;
14178 if (die->tag == DW_TAG_class_type)
14179 return dwarf2_name (die, cu);
14180 }
14181 while (die->tag != DW_TAG_compile_unit);
14182 }
14183 break;
14184
14185 case DW_TAG_class_type:
14186 case DW_TAG_interface_type:
14187 case DW_TAG_structure_type:
14188 case DW_TAG_union_type:
14189 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14190 structures or unions. These were of the form "._%d" in GCC 4.1,
14191 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14192 and GCC 4.4. We work around this problem by ignoring these. */
14193 if (attr && DW_STRING (attr)
14194 && (strncmp (DW_STRING (attr), "._", 2) == 0
14195 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14196 return NULL;
14197
14198 /* GCC might emit a nameless typedef that has a linkage name. See
14199 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14200 if (!attr || DW_STRING (attr) == NULL)
14201 {
14202 char *demangled = NULL;
14203
14204 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14205 if (attr == NULL)
14206 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14207
14208 if (attr == NULL || DW_STRING (attr) == NULL)
14209 return NULL;
14210
14211 /* Avoid demangling DW_STRING (attr) the second time on a second
14212 call for the same DIE. */
14213 if (!DW_STRING_IS_CANONICAL (attr))
14214 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14215
14216 if (demangled)
14217 {
14218 char *base;
14219
14220 /* FIXME: we already did this for the partial symbol... */
14221 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14222 &cu->objfile->objfile_obstack);
14223 DW_STRING_IS_CANONICAL (attr) = 1;
14224 xfree (demangled);
14225
14226 /* Strip any leading namespaces/classes, keep only the base name.
14227 DW_AT_name for named DIEs does not contain the prefixes. */
14228 base = strrchr (DW_STRING (attr), ':');
14229 if (base && base > DW_STRING (attr) && base[-1] == ':')
14230 return &base[1];
14231 else
14232 return DW_STRING (attr);
14233 }
14234 }
14235 break;
14236
14237 default:
14238 break;
14239 }
14240
14241 if (!DW_STRING_IS_CANONICAL (attr))
14242 {
14243 DW_STRING (attr)
14244 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14245 &cu->objfile->objfile_obstack);
14246 DW_STRING_IS_CANONICAL (attr) = 1;
14247 }
14248 return DW_STRING (attr);
14249 }
14250
14251 /* Return the die that this die in an extension of, or NULL if there
14252 is none. *EXT_CU is the CU containing DIE on input, and the CU
14253 containing the return value on output. */
14254
14255 static struct die_info *
14256 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14257 {
14258 struct attribute *attr;
14259
14260 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14261 if (attr == NULL)
14262 return NULL;
14263
14264 return follow_die_ref (die, attr, ext_cu);
14265 }
14266
14267 /* Convert a DIE tag into its string name. */
14268
14269 static char *
14270 dwarf_tag_name (unsigned tag)
14271 {
14272 switch (tag)
14273 {
14274 case DW_TAG_padding:
14275 return "DW_TAG_padding";
14276 case DW_TAG_array_type:
14277 return "DW_TAG_array_type";
14278 case DW_TAG_class_type:
14279 return "DW_TAG_class_type";
14280 case DW_TAG_entry_point:
14281 return "DW_TAG_entry_point";
14282 case DW_TAG_enumeration_type:
14283 return "DW_TAG_enumeration_type";
14284 case DW_TAG_formal_parameter:
14285 return "DW_TAG_formal_parameter";
14286 case DW_TAG_imported_declaration:
14287 return "DW_TAG_imported_declaration";
14288 case DW_TAG_label:
14289 return "DW_TAG_label";
14290 case DW_TAG_lexical_block:
14291 return "DW_TAG_lexical_block";
14292 case DW_TAG_member:
14293 return "DW_TAG_member";
14294 case DW_TAG_pointer_type:
14295 return "DW_TAG_pointer_type";
14296 case DW_TAG_reference_type:
14297 return "DW_TAG_reference_type";
14298 case DW_TAG_compile_unit:
14299 return "DW_TAG_compile_unit";
14300 case DW_TAG_string_type:
14301 return "DW_TAG_string_type";
14302 case DW_TAG_structure_type:
14303 return "DW_TAG_structure_type";
14304 case DW_TAG_subroutine_type:
14305 return "DW_TAG_subroutine_type";
14306 case DW_TAG_typedef:
14307 return "DW_TAG_typedef";
14308 case DW_TAG_union_type:
14309 return "DW_TAG_union_type";
14310 case DW_TAG_unspecified_parameters:
14311 return "DW_TAG_unspecified_parameters";
14312 case DW_TAG_variant:
14313 return "DW_TAG_variant";
14314 case DW_TAG_common_block:
14315 return "DW_TAG_common_block";
14316 case DW_TAG_common_inclusion:
14317 return "DW_TAG_common_inclusion";
14318 case DW_TAG_inheritance:
14319 return "DW_TAG_inheritance";
14320 case DW_TAG_inlined_subroutine:
14321 return "DW_TAG_inlined_subroutine";
14322 case DW_TAG_module:
14323 return "DW_TAG_module";
14324 case DW_TAG_ptr_to_member_type:
14325 return "DW_TAG_ptr_to_member_type";
14326 case DW_TAG_set_type:
14327 return "DW_TAG_set_type";
14328 case DW_TAG_subrange_type:
14329 return "DW_TAG_subrange_type";
14330 case DW_TAG_with_stmt:
14331 return "DW_TAG_with_stmt";
14332 case DW_TAG_access_declaration:
14333 return "DW_TAG_access_declaration";
14334 case DW_TAG_base_type:
14335 return "DW_TAG_base_type";
14336 case DW_TAG_catch_block:
14337 return "DW_TAG_catch_block";
14338 case DW_TAG_const_type:
14339 return "DW_TAG_const_type";
14340 case DW_TAG_constant:
14341 return "DW_TAG_constant";
14342 case DW_TAG_enumerator:
14343 return "DW_TAG_enumerator";
14344 case DW_TAG_file_type:
14345 return "DW_TAG_file_type";
14346 case DW_TAG_friend:
14347 return "DW_TAG_friend";
14348 case DW_TAG_namelist:
14349 return "DW_TAG_namelist";
14350 case DW_TAG_namelist_item:
14351 return "DW_TAG_namelist_item";
14352 case DW_TAG_packed_type:
14353 return "DW_TAG_packed_type";
14354 case DW_TAG_subprogram:
14355 return "DW_TAG_subprogram";
14356 case DW_TAG_template_type_param:
14357 return "DW_TAG_template_type_param";
14358 case DW_TAG_template_value_param:
14359 return "DW_TAG_template_value_param";
14360 case DW_TAG_thrown_type:
14361 return "DW_TAG_thrown_type";
14362 case DW_TAG_try_block:
14363 return "DW_TAG_try_block";
14364 case DW_TAG_variant_part:
14365 return "DW_TAG_variant_part";
14366 case DW_TAG_variable:
14367 return "DW_TAG_variable";
14368 case DW_TAG_volatile_type:
14369 return "DW_TAG_volatile_type";
14370 case DW_TAG_dwarf_procedure:
14371 return "DW_TAG_dwarf_procedure";
14372 case DW_TAG_restrict_type:
14373 return "DW_TAG_restrict_type";
14374 case DW_TAG_interface_type:
14375 return "DW_TAG_interface_type";
14376 case DW_TAG_namespace:
14377 return "DW_TAG_namespace";
14378 case DW_TAG_imported_module:
14379 return "DW_TAG_imported_module";
14380 case DW_TAG_unspecified_type:
14381 return "DW_TAG_unspecified_type";
14382 case DW_TAG_partial_unit:
14383 return "DW_TAG_partial_unit";
14384 case DW_TAG_imported_unit:
14385 return "DW_TAG_imported_unit";
14386 case DW_TAG_condition:
14387 return "DW_TAG_condition";
14388 case DW_TAG_shared_type:
14389 return "DW_TAG_shared_type";
14390 case DW_TAG_type_unit:
14391 return "DW_TAG_type_unit";
14392 case DW_TAG_MIPS_loop:
14393 return "DW_TAG_MIPS_loop";
14394 case DW_TAG_HP_array_descriptor:
14395 return "DW_TAG_HP_array_descriptor";
14396 case DW_TAG_format_label:
14397 return "DW_TAG_format_label";
14398 case DW_TAG_function_template:
14399 return "DW_TAG_function_template";
14400 case DW_TAG_class_template:
14401 return "DW_TAG_class_template";
14402 case DW_TAG_GNU_BINCL:
14403 return "DW_TAG_GNU_BINCL";
14404 case DW_TAG_GNU_EINCL:
14405 return "DW_TAG_GNU_EINCL";
14406 case DW_TAG_upc_shared_type:
14407 return "DW_TAG_upc_shared_type";
14408 case DW_TAG_upc_strict_type:
14409 return "DW_TAG_upc_strict_type";
14410 case DW_TAG_upc_relaxed_type:
14411 return "DW_TAG_upc_relaxed_type";
14412 case DW_TAG_PGI_kanji_type:
14413 return "DW_TAG_PGI_kanji_type";
14414 case DW_TAG_PGI_interface_block:
14415 return "DW_TAG_PGI_interface_block";
14416 case DW_TAG_GNU_call_site:
14417 return "DW_TAG_GNU_call_site";
14418 default:
14419 return "DW_TAG_<unknown>";
14420 }
14421 }
14422
14423 /* Convert a DWARF attribute code into its string name. */
14424
14425 static char *
14426 dwarf_attr_name (unsigned attr)
14427 {
14428 switch (attr)
14429 {
14430 case DW_AT_sibling:
14431 return "DW_AT_sibling";
14432 case DW_AT_location:
14433 return "DW_AT_location";
14434 case DW_AT_name:
14435 return "DW_AT_name";
14436 case DW_AT_ordering:
14437 return "DW_AT_ordering";
14438 case DW_AT_subscr_data:
14439 return "DW_AT_subscr_data";
14440 case DW_AT_byte_size:
14441 return "DW_AT_byte_size";
14442 case DW_AT_bit_offset:
14443 return "DW_AT_bit_offset";
14444 case DW_AT_bit_size:
14445 return "DW_AT_bit_size";
14446 case DW_AT_element_list:
14447 return "DW_AT_element_list";
14448 case DW_AT_stmt_list:
14449 return "DW_AT_stmt_list";
14450 case DW_AT_low_pc:
14451 return "DW_AT_low_pc";
14452 case DW_AT_high_pc:
14453 return "DW_AT_high_pc";
14454 case DW_AT_language:
14455 return "DW_AT_language";
14456 case DW_AT_member:
14457 return "DW_AT_member";
14458 case DW_AT_discr:
14459 return "DW_AT_discr";
14460 case DW_AT_discr_value:
14461 return "DW_AT_discr_value";
14462 case DW_AT_visibility:
14463 return "DW_AT_visibility";
14464 case DW_AT_import:
14465 return "DW_AT_import";
14466 case DW_AT_string_length:
14467 return "DW_AT_string_length";
14468 case DW_AT_common_reference:
14469 return "DW_AT_common_reference";
14470 case DW_AT_comp_dir:
14471 return "DW_AT_comp_dir";
14472 case DW_AT_const_value:
14473 return "DW_AT_const_value";
14474 case DW_AT_containing_type:
14475 return "DW_AT_containing_type";
14476 case DW_AT_default_value:
14477 return "DW_AT_default_value";
14478 case DW_AT_inline:
14479 return "DW_AT_inline";
14480 case DW_AT_is_optional:
14481 return "DW_AT_is_optional";
14482 case DW_AT_lower_bound:
14483 return "DW_AT_lower_bound";
14484 case DW_AT_producer:
14485 return "DW_AT_producer";
14486 case DW_AT_prototyped:
14487 return "DW_AT_prototyped";
14488 case DW_AT_return_addr:
14489 return "DW_AT_return_addr";
14490 case DW_AT_start_scope:
14491 return "DW_AT_start_scope";
14492 case DW_AT_bit_stride:
14493 return "DW_AT_bit_stride";
14494 case DW_AT_upper_bound:
14495 return "DW_AT_upper_bound";
14496 case DW_AT_abstract_origin:
14497 return "DW_AT_abstract_origin";
14498 case DW_AT_accessibility:
14499 return "DW_AT_accessibility";
14500 case DW_AT_address_class:
14501 return "DW_AT_address_class";
14502 case DW_AT_artificial:
14503 return "DW_AT_artificial";
14504 case DW_AT_base_types:
14505 return "DW_AT_base_types";
14506 case DW_AT_calling_convention:
14507 return "DW_AT_calling_convention";
14508 case DW_AT_count:
14509 return "DW_AT_count";
14510 case DW_AT_data_member_location:
14511 return "DW_AT_data_member_location";
14512 case DW_AT_decl_column:
14513 return "DW_AT_decl_column";
14514 case DW_AT_decl_file:
14515 return "DW_AT_decl_file";
14516 case DW_AT_decl_line:
14517 return "DW_AT_decl_line";
14518 case DW_AT_declaration:
14519 return "DW_AT_declaration";
14520 case DW_AT_discr_list:
14521 return "DW_AT_discr_list";
14522 case DW_AT_encoding:
14523 return "DW_AT_encoding";
14524 case DW_AT_external:
14525 return "DW_AT_external";
14526 case DW_AT_frame_base:
14527 return "DW_AT_frame_base";
14528 case DW_AT_friend:
14529 return "DW_AT_friend";
14530 case DW_AT_identifier_case:
14531 return "DW_AT_identifier_case";
14532 case DW_AT_macro_info:
14533 return "DW_AT_macro_info";
14534 case DW_AT_namelist_items:
14535 return "DW_AT_namelist_items";
14536 case DW_AT_priority:
14537 return "DW_AT_priority";
14538 case DW_AT_segment:
14539 return "DW_AT_segment";
14540 case DW_AT_specification:
14541 return "DW_AT_specification";
14542 case DW_AT_static_link:
14543 return "DW_AT_static_link";
14544 case DW_AT_type:
14545 return "DW_AT_type";
14546 case DW_AT_use_location:
14547 return "DW_AT_use_location";
14548 case DW_AT_variable_parameter:
14549 return "DW_AT_variable_parameter";
14550 case DW_AT_virtuality:
14551 return "DW_AT_virtuality";
14552 case DW_AT_vtable_elem_location:
14553 return "DW_AT_vtable_elem_location";
14554 /* DWARF 3 values. */
14555 case DW_AT_allocated:
14556 return "DW_AT_allocated";
14557 case DW_AT_associated:
14558 return "DW_AT_associated";
14559 case DW_AT_data_location:
14560 return "DW_AT_data_location";
14561 case DW_AT_byte_stride:
14562 return "DW_AT_byte_stride";
14563 case DW_AT_entry_pc:
14564 return "DW_AT_entry_pc";
14565 case DW_AT_use_UTF8:
14566 return "DW_AT_use_UTF8";
14567 case DW_AT_extension:
14568 return "DW_AT_extension";
14569 case DW_AT_ranges:
14570 return "DW_AT_ranges";
14571 case DW_AT_trampoline:
14572 return "DW_AT_trampoline";
14573 case DW_AT_call_column:
14574 return "DW_AT_call_column";
14575 case DW_AT_call_file:
14576 return "DW_AT_call_file";
14577 case DW_AT_call_line:
14578 return "DW_AT_call_line";
14579 case DW_AT_description:
14580 return "DW_AT_description";
14581 case DW_AT_binary_scale:
14582 return "DW_AT_binary_scale";
14583 case DW_AT_decimal_scale:
14584 return "DW_AT_decimal_scale";
14585 case DW_AT_small:
14586 return "DW_AT_small";
14587 case DW_AT_decimal_sign:
14588 return "DW_AT_decimal_sign";
14589 case DW_AT_digit_count:
14590 return "DW_AT_digit_count";
14591 case DW_AT_picture_string:
14592 return "DW_AT_picture_string";
14593 case DW_AT_mutable:
14594 return "DW_AT_mutable";
14595 case DW_AT_threads_scaled:
14596 return "DW_AT_threads_scaled";
14597 case DW_AT_explicit:
14598 return "DW_AT_explicit";
14599 case DW_AT_object_pointer:
14600 return "DW_AT_object_pointer";
14601 case DW_AT_endianity:
14602 return "DW_AT_endianity";
14603 case DW_AT_elemental:
14604 return "DW_AT_elemental";
14605 case DW_AT_pure:
14606 return "DW_AT_pure";
14607 case DW_AT_recursive:
14608 return "DW_AT_recursive";
14609 /* DWARF 4 values. */
14610 case DW_AT_signature:
14611 return "DW_AT_signature";
14612 case DW_AT_linkage_name:
14613 return "DW_AT_linkage_name";
14614 /* Tentative Fission values. */
14615 case DW_AT_GNU_dwo_name:
14616 return "DW_AT_GNU_dwo_name";
14617 case DW_AT_GNU_dwo_id:
14618 return "DW_AT_GNU_dwo_id";
14619 case DW_AT_GNU_addr_base:
14620 return "DW_AT_GNU_addr_base";
14621 case DW_AT_GNU_pubnames:
14622 return "DW_AT_GNU_pubnames";
14623 case DW_AT_GNU_pubtypes:
14624 return "DW_AT_GNU_pubtypes";
14625 /* SGI/MIPS extensions. */
14626 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14627 case DW_AT_MIPS_fde:
14628 return "DW_AT_MIPS_fde";
14629 #endif
14630 case DW_AT_MIPS_loop_begin:
14631 return "DW_AT_MIPS_loop_begin";
14632 case DW_AT_MIPS_tail_loop_begin:
14633 return "DW_AT_MIPS_tail_loop_begin";
14634 case DW_AT_MIPS_epilog_begin:
14635 return "DW_AT_MIPS_epilog_begin";
14636 case DW_AT_MIPS_loop_unroll_factor:
14637 return "DW_AT_MIPS_loop_unroll_factor";
14638 case DW_AT_MIPS_software_pipeline_depth:
14639 return "DW_AT_MIPS_software_pipeline_depth";
14640 case DW_AT_MIPS_linkage_name:
14641 return "DW_AT_MIPS_linkage_name";
14642 case DW_AT_MIPS_stride:
14643 return "DW_AT_MIPS_stride";
14644 case DW_AT_MIPS_abstract_name:
14645 return "DW_AT_MIPS_abstract_name";
14646 case DW_AT_MIPS_clone_origin:
14647 return "DW_AT_MIPS_clone_origin";
14648 case DW_AT_MIPS_has_inlines:
14649 return "DW_AT_MIPS_has_inlines";
14650 /* HP extensions. */
14651 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
14652 case DW_AT_HP_block_index:
14653 return "DW_AT_HP_block_index";
14654 #endif
14655 case DW_AT_HP_unmodifiable:
14656 return "DW_AT_HP_unmodifiable";
14657 case DW_AT_HP_actuals_stmt_list:
14658 return "DW_AT_HP_actuals_stmt_list";
14659 case DW_AT_HP_proc_per_section:
14660 return "DW_AT_HP_proc_per_section";
14661 case DW_AT_HP_raw_data_ptr:
14662 return "DW_AT_HP_raw_data_ptr";
14663 case DW_AT_HP_pass_by_reference:
14664 return "DW_AT_HP_pass_by_reference";
14665 case DW_AT_HP_opt_level:
14666 return "DW_AT_HP_opt_level";
14667 case DW_AT_HP_prof_version_id:
14668 return "DW_AT_HP_prof_version_id";
14669 case DW_AT_HP_opt_flags:
14670 return "DW_AT_HP_opt_flags";
14671 case DW_AT_HP_cold_region_low_pc:
14672 return "DW_AT_HP_cold_region_low_pc";
14673 case DW_AT_HP_cold_region_high_pc:
14674 return "DW_AT_HP_cold_region_high_pc";
14675 case DW_AT_HP_all_variables_modifiable:
14676 return "DW_AT_HP_all_variables_modifiable";
14677 case DW_AT_HP_linkage_name:
14678 return "DW_AT_HP_linkage_name";
14679 case DW_AT_HP_prof_flags:
14680 return "DW_AT_HP_prof_flags";
14681 /* GNU extensions. */
14682 case DW_AT_sf_names:
14683 return "DW_AT_sf_names";
14684 case DW_AT_src_info:
14685 return "DW_AT_src_info";
14686 case DW_AT_mac_info:
14687 return "DW_AT_mac_info";
14688 case DW_AT_src_coords:
14689 return "DW_AT_src_coords";
14690 case DW_AT_body_begin:
14691 return "DW_AT_body_begin";
14692 case DW_AT_body_end:
14693 return "DW_AT_body_end";
14694 case DW_AT_GNU_vector:
14695 return "DW_AT_GNU_vector";
14696 case DW_AT_GNU_odr_signature:
14697 return "DW_AT_GNU_odr_signature";
14698 /* VMS extensions. */
14699 case DW_AT_VMS_rtnbeg_pd_address:
14700 return "DW_AT_VMS_rtnbeg_pd_address";
14701 /* UPC extension. */
14702 case DW_AT_upc_threads_scaled:
14703 return "DW_AT_upc_threads_scaled";
14704 /* PGI (STMicroelectronics) extensions. */
14705 case DW_AT_PGI_lbase:
14706 return "DW_AT_PGI_lbase";
14707 case DW_AT_PGI_soffset:
14708 return "DW_AT_PGI_soffset";
14709 case DW_AT_PGI_lstride:
14710 return "DW_AT_PGI_lstride";
14711 default:
14712 return "DW_AT_<unknown>";
14713 }
14714 }
14715
14716 /* Convert a DWARF value form code into its string name. */
14717
14718 static char *
14719 dwarf_form_name (unsigned form)
14720 {
14721 switch (form)
14722 {
14723 case DW_FORM_addr:
14724 return "DW_FORM_addr";
14725 case DW_FORM_block2:
14726 return "DW_FORM_block2";
14727 case DW_FORM_block4:
14728 return "DW_FORM_block4";
14729 case DW_FORM_data2:
14730 return "DW_FORM_data2";
14731 case DW_FORM_data4:
14732 return "DW_FORM_data4";
14733 case DW_FORM_data8:
14734 return "DW_FORM_data8";
14735 case DW_FORM_string:
14736 return "DW_FORM_string";
14737 case DW_FORM_block:
14738 return "DW_FORM_block";
14739 case DW_FORM_block1:
14740 return "DW_FORM_block1";
14741 case DW_FORM_data1:
14742 return "DW_FORM_data1";
14743 case DW_FORM_flag:
14744 return "DW_FORM_flag";
14745 case DW_FORM_sdata:
14746 return "DW_FORM_sdata";
14747 case DW_FORM_strp:
14748 return "DW_FORM_strp";
14749 case DW_FORM_udata:
14750 return "DW_FORM_udata";
14751 case DW_FORM_ref_addr:
14752 return "DW_FORM_ref_addr";
14753 case DW_FORM_ref1:
14754 return "DW_FORM_ref1";
14755 case DW_FORM_ref2:
14756 return "DW_FORM_ref2";
14757 case DW_FORM_ref4:
14758 return "DW_FORM_ref4";
14759 case DW_FORM_ref8:
14760 return "DW_FORM_ref8";
14761 case DW_FORM_ref_udata:
14762 return "DW_FORM_ref_udata";
14763 case DW_FORM_indirect:
14764 return "DW_FORM_indirect";
14765 case DW_FORM_sec_offset:
14766 return "DW_FORM_sec_offset";
14767 case DW_FORM_exprloc:
14768 return "DW_FORM_exprloc";
14769 case DW_FORM_flag_present:
14770 return "DW_FORM_flag_present";
14771 case DW_FORM_ref_sig8:
14772 return "DW_FORM_ref_sig8";
14773 case DW_FORM_GNU_addr_index:
14774 return "DW_FORM_GNU_addr_index";
14775 case DW_FORM_GNU_str_index:
14776 return "DW_FORM_GNU_str_index";
14777 default:
14778 return "DW_FORM_<unknown>";
14779 }
14780 }
14781
14782 /* Convert a DWARF stack opcode into its string name. */
14783
14784 const char *
14785 dwarf_stack_op_name (unsigned op)
14786 {
14787 switch (op)
14788 {
14789 case DW_OP_addr:
14790 return "DW_OP_addr";
14791 case DW_OP_deref:
14792 return "DW_OP_deref";
14793 case DW_OP_const1u:
14794 return "DW_OP_const1u";
14795 case DW_OP_const1s:
14796 return "DW_OP_const1s";
14797 case DW_OP_const2u:
14798 return "DW_OP_const2u";
14799 case DW_OP_const2s:
14800 return "DW_OP_const2s";
14801 case DW_OP_const4u:
14802 return "DW_OP_const4u";
14803 case DW_OP_const4s:
14804 return "DW_OP_const4s";
14805 case DW_OP_const8u:
14806 return "DW_OP_const8u";
14807 case DW_OP_const8s:
14808 return "DW_OP_const8s";
14809 case DW_OP_constu:
14810 return "DW_OP_constu";
14811 case DW_OP_consts:
14812 return "DW_OP_consts";
14813 case DW_OP_dup:
14814 return "DW_OP_dup";
14815 case DW_OP_drop:
14816 return "DW_OP_drop";
14817 case DW_OP_over:
14818 return "DW_OP_over";
14819 case DW_OP_pick:
14820 return "DW_OP_pick";
14821 case DW_OP_swap:
14822 return "DW_OP_swap";
14823 case DW_OP_rot:
14824 return "DW_OP_rot";
14825 case DW_OP_xderef:
14826 return "DW_OP_xderef";
14827 case DW_OP_abs:
14828 return "DW_OP_abs";
14829 case DW_OP_and:
14830 return "DW_OP_and";
14831 case DW_OP_div:
14832 return "DW_OP_div";
14833 case DW_OP_minus:
14834 return "DW_OP_minus";
14835 case DW_OP_mod:
14836 return "DW_OP_mod";
14837 case DW_OP_mul:
14838 return "DW_OP_mul";
14839 case DW_OP_neg:
14840 return "DW_OP_neg";
14841 case DW_OP_not:
14842 return "DW_OP_not";
14843 case DW_OP_or:
14844 return "DW_OP_or";
14845 case DW_OP_plus:
14846 return "DW_OP_plus";
14847 case DW_OP_plus_uconst:
14848 return "DW_OP_plus_uconst";
14849 case DW_OP_shl:
14850 return "DW_OP_shl";
14851 case DW_OP_shr:
14852 return "DW_OP_shr";
14853 case DW_OP_shra:
14854 return "DW_OP_shra";
14855 case DW_OP_xor:
14856 return "DW_OP_xor";
14857 case DW_OP_bra:
14858 return "DW_OP_bra";
14859 case DW_OP_eq:
14860 return "DW_OP_eq";
14861 case DW_OP_ge:
14862 return "DW_OP_ge";
14863 case DW_OP_gt:
14864 return "DW_OP_gt";
14865 case DW_OP_le:
14866 return "DW_OP_le";
14867 case DW_OP_lt:
14868 return "DW_OP_lt";
14869 case DW_OP_ne:
14870 return "DW_OP_ne";
14871 case DW_OP_skip:
14872 return "DW_OP_skip";
14873 case DW_OP_lit0:
14874 return "DW_OP_lit0";
14875 case DW_OP_lit1:
14876 return "DW_OP_lit1";
14877 case DW_OP_lit2:
14878 return "DW_OP_lit2";
14879 case DW_OP_lit3:
14880 return "DW_OP_lit3";
14881 case DW_OP_lit4:
14882 return "DW_OP_lit4";
14883 case DW_OP_lit5:
14884 return "DW_OP_lit5";
14885 case DW_OP_lit6:
14886 return "DW_OP_lit6";
14887 case DW_OP_lit7:
14888 return "DW_OP_lit7";
14889 case DW_OP_lit8:
14890 return "DW_OP_lit8";
14891 case DW_OP_lit9:
14892 return "DW_OP_lit9";
14893 case DW_OP_lit10:
14894 return "DW_OP_lit10";
14895 case DW_OP_lit11:
14896 return "DW_OP_lit11";
14897 case DW_OP_lit12:
14898 return "DW_OP_lit12";
14899 case DW_OP_lit13:
14900 return "DW_OP_lit13";
14901 case DW_OP_lit14:
14902 return "DW_OP_lit14";
14903 case DW_OP_lit15:
14904 return "DW_OP_lit15";
14905 case DW_OP_lit16:
14906 return "DW_OP_lit16";
14907 case DW_OP_lit17:
14908 return "DW_OP_lit17";
14909 case DW_OP_lit18:
14910 return "DW_OP_lit18";
14911 case DW_OP_lit19:
14912 return "DW_OP_lit19";
14913 case DW_OP_lit20:
14914 return "DW_OP_lit20";
14915 case DW_OP_lit21:
14916 return "DW_OP_lit21";
14917 case DW_OP_lit22:
14918 return "DW_OP_lit22";
14919 case DW_OP_lit23:
14920 return "DW_OP_lit23";
14921 case DW_OP_lit24:
14922 return "DW_OP_lit24";
14923 case DW_OP_lit25:
14924 return "DW_OP_lit25";
14925 case DW_OP_lit26:
14926 return "DW_OP_lit26";
14927 case DW_OP_lit27:
14928 return "DW_OP_lit27";
14929 case DW_OP_lit28:
14930 return "DW_OP_lit28";
14931 case DW_OP_lit29:
14932 return "DW_OP_lit29";
14933 case DW_OP_lit30:
14934 return "DW_OP_lit30";
14935 case DW_OP_lit31:
14936 return "DW_OP_lit31";
14937 case DW_OP_reg0:
14938 return "DW_OP_reg0";
14939 case DW_OP_reg1:
14940 return "DW_OP_reg1";
14941 case DW_OP_reg2:
14942 return "DW_OP_reg2";
14943 case DW_OP_reg3:
14944 return "DW_OP_reg3";
14945 case DW_OP_reg4:
14946 return "DW_OP_reg4";
14947 case DW_OP_reg5:
14948 return "DW_OP_reg5";
14949 case DW_OP_reg6:
14950 return "DW_OP_reg6";
14951 case DW_OP_reg7:
14952 return "DW_OP_reg7";
14953 case DW_OP_reg8:
14954 return "DW_OP_reg8";
14955 case DW_OP_reg9:
14956 return "DW_OP_reg9";
14957 case DW_OP_reg10:
14958 return "DW_OP_reg10";
14959 case DW_OP_reg11:
14960 return "DW_OP_reg11";
14961 case DW_OP_reg12:
14962 return "DW_OP_reg12";
14963 case DW_OP_reg13:
14964 return "DW_OP_reg13";
14965 case DW_OP_reg14:
14966 return "DW_OP_reg14";
14967 case DW_OP_reg15:
14968 return "DW_OP_reg15";
14969 case DW_OP_reg16:
14970 return "DW_OP_reg16";
14971 case DW_OP_reg17:
14972 return "DW_OP_reg17";
14973 case DW_OP_reg18:
14974 return "DW_OP_reg18";
14975 case DW_OP_reg19:
14976 return "DW_OP_reg19";
14977 case DW_OP_reg20:
14978 return "DW_OP_reg20";
14979 case DW_OP_reg21:
14980 return "DW_OP_reg21";
14981 case DW_OP_reg22:
14982 return "DW_OP_reg22";
14983 case DW_OP_reg23:
14984 return "DW_OP_reg23";
14985 case DW_OP_reg24:
14986 return "DW_OP_reg24";
14987 case DW_OP_reg25:
14988 return "DW_OP_reg25";
14989 case DW_OP_reg26:
14990 return "DW_OP_reg26";
14991 case DW_OP_reg27:
14992 return "DW_OP_reg27";
14993 case DW_OP_reg28:
14994 return "DW_OP_reg28";
14995 case DW_OP_reg29:
14996 return "DW_OP_reg29";
14997 case DW_OP_reg30:
14998 return "DW_OP_reg30";
14999 case DW_OP_reg31:
15000 return "DW_OP_reg31";
15001 case DW_OP_breg0:
15002 return "DW_OP_breg0";
15003 case DW_OP_breg1:
15004 return "DW_OP_breg1";
15005 case DW_OP_breg2:
15006 return "DW_OP_breg2";
15007 case DW_OP_breg3:
15008 return "DW_OP_breg3";
15009 case DW_OP_breg4:
15010 return "DW_OP_breg4";
15011 case DW_OP_breg5:
15012 return "DW_OP_breg5";
15013 case DW_OP_breg6:
15014 return "DW_OP_breg6";
15015 case DW_OP_breg7:
15016 return "DW_OP_breg7";
15017 case DW_OP_breg8:
15018 return "DW_OP_breg8";
15019 case DW_OP_breg9:
15020 return "DW_OP_breg9";
15021 case DW_OP_breg10:
15022 return "DW_OP_breg10";
15023 case DW_OP_breg11:
15024 return "DW_OP_breg11";
15025 case DW_OP_breg12:
15026 return "DW_OP_breg12";
15027 case DW_OP_breg13:
15028 return "DW_OP_breg13";
15029 case DW_OP_breg14:
15030 return "DW_OP_breg14";
15031 case DW_OP_breg15:
15032 return "DW_OP_breg15";
15033 case DW_OP_breg16:
15034 return "DW_OP_breg16";
15035 case DW_OP_breg17:
15036 return "DW_OP_breg17";
15037 case DW_OP_breg18:
15038 return "DW_OP_breg18";
15039 case DW_OP_breg19:
15040 return "DW_OP_breg19";
15041 case DW_OP_breg20:
15042 return "DW_OP_breg20";
15043 case DW_OP_breg21:
15044 return "DW_OP_breg21";
15045 case DW_OP_breg22:
15046 return "DW_OP_breg22";
15047 case DW_OP_breg23:
15048 return "DW_OP_breg23";
15049 case DW_OP_breg24:
15050 return "DW_OP_breg24";
15051 case DW_OP_breg25:
15052 return "DW_OP_breg25";
15053 case DW_OP_breg26:
15054 return "DW_OP_breg26";
15055 case DW_OP_breg27:
15056 return "DW_OP_breg27";
15057 case DW_OP_breg28:
15058 return "DW_OP_breg28";
15059 case DW_OP_breg29:
15060 return "DW_OP_breg29";
15061 case DW_OP_breg30:
15062 return "DW_OP_breg30";
15063 case DW_OP_breg31:
15064 return "DW_OP_breg31";
15065 case DW_OP_regx:
15066 return "DW_OP_regx";
15067 case DW_OP_fbreg:
15068 return "DW_OP_fbreg";
15069 case DW_OP_bregx:
15070 return "DW_OP_bregx";
15071 case DW_OP_piece:
15072 return "DW_OP_piece";
15073 case DW_OP_deref_size:
15074 return "DW_OP_deref_size";
15075 case DW_OP_xderef_size:
15076 return "DW_OP_xderef_size";
15077 case DW_OP_nop:
15078 return "DW_OP_nop";
15079 /* DWARF 3 extensions. */
15080 case DW_OP_push_object_address:
15081 return "DW_OP_push_object_address";
15082 case DW_OP_call2:
15083 return "DW_OP_call2";
15084 case DW_OP_call4:
15085 return "DW_OP_call4";
15086 case DW_OP_call_ref:
15087 return "DW_OP_call_ref";
15088 case DW_OP_form_tls_address:
15089 return "DW_OP_form_tls_address";
15090 case DW_OP_call_frame_cfa:
15091 return "DW_OP_call_frame_cfa";
15092 case DW_OP_bit_piece:
15093 return "DW_OP_bit_piece";
15094 /* DWARF 4 extensions. */
15095 case DW_OP_implicit_value:
15096 return "DW_OP_implicit_value";
15097 case DW_OP_stack_value:
15098 return "DW_OP_stack_value";
15099 /* GNU extensions. */
15100 case DW_OP_GNU_push_tls_address:
15101 return "DW_OP_GNU_push_tls_address";
15102 case DW_OP_GNU_uninit:
15103 return "DW_OP_GNU_uninit";
15104 case DW_OP_GNU_encoded_addr:
15105 return "DW_OP_GNU_encoded_addr";
15106 case DW_OP_GNU_implicit_pointer:
15107 return "DW_OP_GNU_implicit_pointer";
15108 case DW_OP_GNU_entry_value:
15109 return "DW_OP_GNU_entry_value";
15110 case DW_OP_GNU_const_type:
15111 return "DW_OP_GNU_const_type";
15112 case DW_OP_GNU_regval_type:
15113 return "DW_OP_GNU_regval_type";
15114 case DW_OP_GNU_deref_type:
15115 return "DW_OP_GNU_deref_type";
15116 case DW_OP_GNU_convert:
15117 return "DW_OP_GNU_convert";
15118 case DW_OP_GNU_reinterpret:
15119 return "DW_OP_GNU_reinterpret";
15120 case DW_OP_GNU_parameter_ref:
15121 return "DW_OP_GNU_parameter_ref";
15122 default:
15123 return NULL;
15124 }
15125 }
15126
15127 static char *
15128 dwarf_bool_name (unsigned mybool)
15129 {
15130 if (mybool)
15131 return "TRUE";
15132 else
15133 return "FALSE";
15134 }
15135
15136 /* Convert a DWARF type code into its string name. */
15137
15138 static char *
15139 dwarf_type_encoding_name (unsigned enc)
15140 {
15141 switch (enc)
15142 {
15143 case DW_ATE_void:
15144 return "DW_ATE_void";
15145 case DW_ATE_address:
15146 return "DW_ATE_address";
15147 case DW_ATE_boolean:
15148 return "DW_ATE_boolean";
15149 case DW_ATE_complex_float:
15150 return "DW_ATE_complex_float";
15151 case DW_ATE_float:
15152 return "DW_ATE_float";
15153 case DW_ATE_signed:
15154 return "DW_ATE_signed";
15155 case DW_ATE_signed_char:
15156 return "DW_ATE_signed_char";
15157 case DW_ATE_unsigned:
15158 return "DW_ATE_unsigned";
15159 case DW_ATE_unsigned_char:
15160 return "DW_ATE_unsigned_char";
15161 /* DWARF 3. */
15162 case DW_ATE_imaginary_float:
15163 return "DW_ATE_imaginary_float";
15164 case DW_ATE_packed_decimal:
15165 return "DW_ATE_packed_decimal";
15166 case DW_ATE_numeric_string:
15167 return "DW_ATE_numeric_string";
15168 case DW_ATE_edited:
15169 return "DW_ATE_edited";
15170 case DW_ATE_signed_fixed:
15171 return "DW_ATE_signed_fixed";
15172 case DW_ATE_unsigned_fixed:
15173 return "DW_ATE_unsigned_fixed";
15174 case DW_ATE_decimal_float:
15175 return "DW_ATE_decimal_float";
15176 /* DWARF 4. */
15177 case DW_ATE_UTF:
15178 return "DW_ATE_UTF";
15179 /* HP extensions. */
15180 case DW_ATE_HP_float80:
15181 return "DW_ATE_HP_float80";
15182 case DW_ATE_HP_complex_float80:
15183 return "DW_ATE_HP_complex_float80";
15184 case DW_ATE_HP_float128:
15185 return "DW_ATE_HP_float128";
15186 case DW_ATE_HP_complex_float128:
15187 return "DW_ATE_HP_complex_float128";
15188 case DW_ATE_HP_floathpintel:
15189 return "DW_ATE_HP_floathpintel";
15190 case DW_ATE_HP_imaginary_float80:
15191 return "DW_ATE_HP_imaginary_float80";
15192 case DW_ATE_HP_imaginary_float128:
15193 return "DW_ATE_HP_imaginary_float128";
15194 default:
15195 return "DW_ATE_<unknown>";
15196 }
15197 }
15198
15199 /* Convert a DWARF call frame info operation to its string name. */
15200
15201 #if 0
15202 static char *
15203 dwarf_cfi_name (unsigned cfi_opc)
15204 {
15205 switch (cfi_opc)
15206 {
15207 case DW_CFA_advance_loc:
15208 return "DW_CFA_advance_loc";
15209 case DW_CFA_offset:
15210 return "DW_CFA_offset";
15211 case DW_CFA_restore:
15212 return "DW_CFA_restore";
15213 case DW_CFA_nop:
15214 return "DW_CFA_nop";
15215 case DW_CFA_set_loc:
15216 return "DW_CFA_set_loc";
15217 case DW_CFA_advance_loc1:
15218 return "DW_CFA_advance_loc1";
15219 case DW_CFA_advance_loc2:
15220 return "DW_CFA_advance_loc2";
15221 case DW_CFA_advance_loc4:
15222 return "DW_CFA_advance_loc4";
15223 case DW_CFA_offset_extended:
15224 return "DW_CFA_offset_extended";
15225 case DW_CFA_restore_extended:
15226 return "DW_CFA_restore_extended";
15227 case DW_CFA_undefined:
15228 return "DW_CFA_undefined";
15229 case DW_CFA_same_value:
15230 return "DW_CFA_same_value";
15231 case DW_CFA_register:
15232 return "DW_CFA_register";
15233 case DW_CFA_remember_state:
15234 return "DW_CFA_remember_state";
15235 case DW_CFA_restore_state:
15236 return "DW_CFA_restore_state";
15237 case DW_CFA_def_cfa:
15238 return "DW_CFA_def_cfa";
15239 case DW_CFA_def_cfa_register:
15240 return "DW_CFA_def_cfa_register";
15241 case DW_CFA_def_cfa_offset:
15242 return "DW_CFA_def_cfa_offset";
15243 /* DWARF 3. */
15244 case DW_CFA_def_cfa_expression:
15245 return "DW_CFA_def_cfa_expression";
15246 case DW_CFA_expression:
15247 return "DW_CFA_expression";
15248 case DW_CFA_offset_extended_sf:
15249 return "DW_CFA_offset_extended_sf";
15250 case DW_CFA_def_cfa_sf:
15251 return "DW_CFA_def_cfa_sf";
15252 case DW_CFA_def_cfa_offset_sf:
15253 return "DW_CFA_def_cfa_offset_sf";
15254 case DW_CFA_val_offset:
15255 return "DW_CFA_val_offset";
15256 case DW_CFA_val_offset_sf:
15257 return "DW_CFA_val_offset_sf";
15258 case DW_CFA_val_expression:
15259 return "DW_CFA_val_expression";
15260 /* SGI/MIPS specific. */
15261 case DW_CFA_MIPS_advance_loc8:
15262 return "DW_CFA_MIPS_advance_loc8";
15263 /* GNU extensions. */
15264 case DW_CFA_GNU_window_save:
15265 return "DW_CFA_GNU_window_save";
15266 case DW_CFA_GNU_args_size:
15267 return "DW_CFA_GNU_args_size";
15268 case DW_CFA_GNU_negative_offset_extended:
15269 return "DW_CFA_GNU_negative_offset_extended";
15270 default:
15271 return "DW_CFA_<unknown>";
15272 }
15273 }
15274 #endif
15275
15276 static void
15277 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
15278 {
15279 unsigned int i;
15280
15281 print_spaces (indent, f);
15282 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
15283 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
15284
15285 if (die->parent != NULL)
15286 {
15287 print_spaces (indent, f);
15288 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
15289 die->parent->offset.sect_off);
15290 }
15291
15292 print_spaces (indent, f);
15293 fprintf_unfiltered (f, " has children: %s\n",
15294 dwarf_bool_name (die->child != NULL));
15295
15296 print_spaces (indent, f);
15297 fprintf_unfiltered (f, " attributes:\n");
15298
15299 for (i = 0; i < die->num_attrs; ++i)
15300 {
15301 print_spaces (indent, f);
15302 fprintf_unfiltered (f, " %s (%s) ",
15303 dwarf_attr_name (die->attrs[i].name),
15304 dwarf_form_name (die->attrs[i].form));
15305
15306 switch (die->attrs[i].form)
15307 {
15308 case DW_FORM_addr:
15309 case DW_FORM_GNU_addr_index:
15310 fprintf_unfiltered (f, "address: ");
15311 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
15312 break;
15313 case DW_FORM_block2:
15314 case DW_FORM_block4:
15315 case DW_FORM_block:
15316 case DW_FORM_block1:
15317 fprintf_unfiltered (f, "block: size %d",
15318 DW_BLOCK (&die->attrs[i])->size);
15319 break;
15320 case DW_FORM_exprloc:
15321 fprintf_unfiltered (f, "expression: size %u",
15322 DW_BLOCK (&die->attrs[i])->size);
15323 break;
15324 case DW_FORM_ref_addr:
15325 fprintf_unfiltered (f, "ref address: ");
15326 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
15327 break;
15328 case DW_FORM_ref1:
15329 case DW_FORM_ref2:
15330 case DW_FORM_ref4:
15331 case DW_FORM_ref8:
15332 case DW_FORM_ref_udata:
15333 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
15334 (long) (DW_UNSND (&die->attrs[i])));
15335 break;
15336 case DW_FORM_data1:
15337 case DW_FORM_data2:
15338 case DW_FORM_data4:
15339 case DW_FORM_data8:
15340 case DW_FORM_udata:
15341 case DW_FORM_sdata:
15342 fprintf_unfiltered (f, "constant: %s",
15343 pulongest (DW_UNSND (&die->attrs[i])));
15344 break;
15345 case DW_FORM_sec_offset:
15346 fprintf_unfiltered (f, "section offset: %s",
15347 pulongest (DW_UNSND (&die->attrs[i])));
15348 break;
15349 case DW_FORM_ref_sig8:
15350 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15351 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
15352 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
15353 else
15354 fprintf_unfiltered (f, "signatured type, offset: unknown");
15355 break;
15356 case DW_FORM_string:
15357 case DW_FORM_strp:
15358 case DW_FORM_GNU_str_index:
15359 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
15360 DW_STRING (&die->attrs[i])
15361 ? DW_STRING (&die->attrs[i]) : "",
15362 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
15363 break;
15364 case DW_FORM_flag:
15365 if (DW_UNSND (&die->attrs[i]))
15366 fprintf_unfiltered (f, "flag: TRUE");
15367 else
15368 fprintf_unfiltered (f, "flag: FALSE");
15369 break;
15370 case DW_FORM_flag_present:
15371 fprintf_unfiltered (f, "flag: TRUE");
15372 break;
15373 case DW_FORM_indirect:
15374 /* The reader will have reduced the indirect form to
15375 the "base form" so this form should not occur. */
15376 fprintf_unfiltered (f,
15377 "unexpected attribute form: DW_FORM_indirect");
15378 break;
15379 default:
15380 fprintf_unfiltered (f, "unsupported attribute form: %d.",
15381 die->attrs[i].form);
15382 break;
15383 }
15384 fprintf_unfiltered (f, "\n");
15385 }
15386 }
15387
15388 static void
15389 dump_die_for_error (struct die_info *die)
15390 {
15391 dump_die_shallow (gdb_stderr, 0, die);
15392 }
15393
15394 static void
15395 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
15396 {
15397 int indent = level * 4;
15398
15399 gdb_assert (die != NULL);
15400
15401 if (level >= max_level)
15402 return;
15403
15404 dump_die_shallow (f, indent, die);
15405
15406 if (die->child != NULL)
15407 {
15408 print_spaces (indent, f);
15409 fprintf_unfiltered (f, " Children:");
15410 if (level + 1 < max_level)
15411 {
15412 fprintf_unfiltered (f, "\n");
15413 dump_die_1 (f, level + 1, max_level, die->child);
15414 }
15415 else
15416 {
15417 fprintf_unfiltered (f,
15418 " [not printed, max nesting level reached]\n");
15419 }
15420 }
15421
15422 if (die->sibling != NULL && level > 0)
15423 {
15424 dump_die_1 (f, level, max_level, die->sibling);
15425 }
15426 }
15427
15428 /* This is called from the pdie macro in gdbinit.in.
15429 It's not static so gcc will keep a copy callable from gdb. */
15430
15431 void
15432 dump_die (struct die_info *die, int max_level)
15433 {
15434 dump_die_1 (gdb_stdlog, 0, max_level, die);
15435 }
15436
15437 static void
15438 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
15439 {
15440 void **slot;
15441
15442 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15443 INSERT);
15444
15445 *slot = die;
15446 }
15447
15448 /* DW_ADDR is always stored already as sect_offset; despite for the forms
15449 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15450
15451 static int
15452 is_ref_attr (struct attribute *attr)
15453 {
15454 switch (attr->form)
15455 {
15456 case DW_FORM_ref_addr:
15457 case DW_FORM_ref1:
15458 case DW_FORM_ref2:
15459 case DW_FORM_ref4:
15460 case DW_FORM_ref8:
15461 case DW_FORM_ref_udata:
15462 return 1;
15463 default:
15464 return 0;
15465 }
15466 }
15467
15468 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15469 required kind. */
15470
15471 static sect_offset
15472 dwarf2_get_ref_die_offset (struct attribute *attr)
15473 {
15474 sect_offset retval = { DW_UNSND (attr) };
15475
15476 if (is_ref_attr (attr))
15477 return retval;
15478
15479 retval.sect_off = 0;
15480 complaint (&symfile_complaints,
15481 _("unsupported die ref attribute form: '%s'"),
15482 dwarf_form_name (attr->form));
15483 return retval;
15484 }
15485
15486 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15487 * the value held by the attribute is not constant. */
15488
15489 static LONGEST
15490 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15491 {
15492 if (attr->form == DW_FORM_sdata)
15493 return DW_SND (attr);
15494 else if (attr->form == DW_FORM_udata
15495 || attr->form == DW_FORM_data1
15496 || attr->form == DW_FORM_data2
15497 || attr->form == DW_FORM_data4
15498 || attr->form == DW_FORM_data8)
15499 return DW_UNSND (attr);
15500 else
15501 {
15502 complaint (&symfile_complaints,
15503 _("Attribute value is not a constant (%s)"),
15504 dwarf_form_name (attr->form));
15505 return default_value;
15506 }
15507 }
15508
15509 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
15510 unit and add it to our queue.
15511 The result is non-zero if PER_CU was queued, otherwise the result is zero
15512 meaning either PER_CU is already queued or it is already loaded. */
15513
15514 static int
15515 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
15516 struct dwarf2_per_cu_data *per_cu)
15517 {
15518 /* We may arrive here during partial symbol reading, if we need full
15519 DIEs to process an unusual case (e.g. template arguments). Do
15520 not queue PER_CU, just tell our caller to load its DIEs. */
15521 if (dwarf2_per_objfile->reading_partial_symbols)
15522 {
15523 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
15524 return 1;
15525 return 0;
15526 }
15527
15528 /* Mark the dependence relation so that we don't flush PER_CU
15529 too early. */
15530 dwarf2_add_dependence (this_cu, per_cu);
15531
15532 /* If it's already on the queue, we have nothing to do. */
15533 if (per_cu->queued)
15534 return 0;
15535
15536 /* If the compilation unit is already loaded, just mark it as
15537 used. */
15538 if (per_cu->cu != NULL)
15539 {
15540 per_cu->cu->last_used = 0;
15541 return 0;
15542 }
15543
15544 /* Add it to the queue. */
15545 queue_comp_unit (per_cu);
15546
15547 return 1;
15548 }
15549
15550 /* Follow reference or signature attribute ATTR of SRC_DIE.
15551 On entry *REF_CU is the CU of SRC_DIE.
15552 On exit *REF_CU is the CU of the result. */
15553
15554 static struct die_info *
15555 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15556 struct dwarf2_cu **ref_cu)
15557 {
15558 struct die_info *die;
15559
15560 if (is_ref_attr (attr))
15561 die = follow_die_ref (src_die, attr, ref_cu);
15562 else if (attr->form == DW_FORM_ref_sig8)
15563 die = follow_die_sig (src_die, attr, ref_cu);
15564 else
15565 {
15566 dump_die_for_error (src_die);
15567 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15568 (*ref_cu)->objfile->name);
15569 }
15570
15571 return die;
15572 }
15573
15574 /* Follow reference OFFSET.
15575 On entry *REF_CU is the CU of the source die referencing OFFSET.
15576 On exit *REF_CU is the CU of the result.
15577 Returns NULL if OFFSET is invalid. */
15578
15579 static struct die_info *
15580 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
15581 {
15582 struct die_info temp_die;
15583 struct dwarf2_cu *target_cu, *cu = *ref_cu;
15584
15585 gdb_assert (cu->per_cu != NULL);
15586
15587 target_cu = cu;
15588
15589 if (cu->per_cu->is_debug_types)
15590 {
15591 /* .debug_types CUs cannot reference anything outside their CU.
15592 If they need to, they have to reference a signatured type via
15593 DW_FORM_ref_sig8. */
15594 if (! offset_in_cu_p (&cu->header, offset))
15595 return NULL;
15596 }
15597 else if (! offset_in_cu_p (&cu->header, offset))
15598 {
15599 struct dwarf2_per_cu_data *per_cu;
15600
15601 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
15602
15603 /* If necessary, add it to the queue and load its DIEs. */
15604 if (maybe_queue_comp_unit (cu, per_cu))
15605 load_full_comp_unit (per_cu);
15606
15607 target_cu = per_cu->cu;
15608 }
15609 else if (cu->dies == NULL)
15610 {
15611 /* We're loading full DIEs during partial symbol reading. */
15612 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
15613 load_full_comp_unit (cu->per_cu);
15614 }
15615
15616 *ref_cu = target_cu;
15617 temp_die.offset = offset;
15618 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
15619 }
15620
15621 /* Follow reference attribute ATTR of SRC_DIE.
15622 On entry *REF_CU is the CU of SRC_DIE.
15623 On exit *REF_CU is the CU of the result. */
15624
15625 static struct die_info *
15626 follow_die_ref (struct die_info *src_die, struct attribute *attr,
15627 struct dwarf2_cu **ref_cu)
15628 {
15629 sect_offset offset = dwarf2_get_ref_die_offset (attr);
15630 struct dwarf2_cu *cu = *ref_cu;
15631 struct die_info *die;
15632
15633 die = follow_die_offset (offset, ref_cu);
15634 if (!die)
15635 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15636 "at 0x%x [in module %s]"),
15637 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
15638
15639 return die;
15640 }
15641
15642 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15643 Returned value is intended for DW_OP_call*. Returned
15644 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
15645
15646 struct dwarf2_locexpr_baton
15647 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
15648 struct dwarf2_per_cu_data *per_cu,
15649 CORE_ADDR (*get_frame_pc) (void *baton),
15650 void *baton)
15651 {
15652 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
15653 struct dwarf2_cu *cu;
15654 struct die_info *die;
15655 struct attribute *attr;
15656 struct dwarf2_locexpr_baton retval;
15657
15658 dw2_setup (per_cu->objfile);
15659
15660 if (per_cu->cu == NULL)
15661 load_cu (per_cu);
15662 cu = per_cu->cu;
15663
15664 die = follow_die_offset (offset, &cu);
15665 if (!die)
15666 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15667 offset.sect_off, per_cu->objfile->name);
15668
15669 attr = dwarf2_attr (die, DW_AT_location, cu);
15670 if (!attr)
15671 {
15672 /* DWARF: "If there is no such attribute, then there is no effect.".
15673 DATA is ignored if SIZE is 0. */
15674
15675 retval.data = NULL;
15676 retval.size = 0;
15677 }
15678 else if (attr_form_is_section_offset (attr))
15679 {
15680 struct dwarf2_loclist_baton loclist_baton;
15681 CORE_ADDR pc = (*get_frame_pc) (baton);
15682 size_t size;
15683
15684 fill_in_loclist_baton (cu, &loclist_baton, attr);
15685
15686 retval.data = dwarf2_find_location_expression (&loclist_baton,
15687 &size, pc);
15688 retval.size = size;
15689 }
15690 else
15691 {
15692 if (!attr_form_is_block (attr))
15693 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15694 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15695 offset.sect_off, per_cu->objfile->name);
15696
15697 retval.data = DW_BLOCK (attr)->data;
15698 retval.size = DW_BLOCK (attr)->size;
15699 }
15700 retval.per_cu = cu->per_cu;
15701
15702 age_cached_comp_units ();
15703
15704 return retval;
15705 }
15706
15707 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15708 PER_CU. */
15709
15710 struct type *
15711 dwarf2_get_die_type (cu_offset die_offset,
15712 struct dwarf2_per_cu_data *per_cu)
15713 {
15714 sect_offset die_offset_sect;
15715
15716 dw2_setup (per_cu->objfile);
15717
15718 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15719 return get_die_type_at_offset (die_offset_sect, per_cu);
15720 }
15721
15722 /* Follow the signature attribute ATTR in SRC_DIE.
15723 On entry *REF_CU is the CU of SRC_DIE.
15724 On exit *REF_CU is the CU of the result. */
15725
15726 static struct die_info *
15727 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15728 struct dwarf2_cu **ref_cu)
15729 {
15730 struct objfile *objfile = (*ref_cu)->objfile;
15731 struct die_info temp_die;
15732 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15733 struct dwarf2_cu *sig_cu;
15734 struct die_info *die;
15735
15736 /* sig_type will be NULL if the signatured type is missing from
15737 the debug info. */
15738 if (sig_type == NULL)
15739 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15740 "at 0x%x [in module %s]"),
15741 src_die->offset.sect_off, objfile->name);
15742
15743 /* If necessary, add it to the queue and load its DIEs. */
15744
15745 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
15746 read_signatured_type (sig_type);
15747
15748 gdb_assert (sig_type->per_cu.cu != NULL);
15749
15750 sig_cu = sig_type->per_cu.cu;
15751 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15752 temp_die.offset = sig_type->type_offset_in_section;
15753 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15754 temp_die.offset.sect_off);
15755 if (die)
15756 {
15757 *ref_cu = sig_cu;
15758 return die;
15759 }
15760
15761 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15762 "from DIE at 0x%x [in module %s]"),
15763 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15764 }
15765
15766 /* Given an offset of a signatured type, return its signatured_type. */
15767
15768 static struct signatured_type *
15769 lookup_signatured_type_at_offset (struct objfile *objfile,
15770 struct dwarf2_section_info *section,
15771 sect_offset offset)
15772 {
15773 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15774 unsigned int length, initial_length_size;
15775 unsigned int sig_offset;
15776 struct signatured_type find_entry, *sig_type;
15777
15778 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15779 sig_offset = (initial_length_size
15780 + 2 /*version*/
15781 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15782 + 1 /*address_size*/);
15783 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15784 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15785
15786 /* This is only used to lookup previously recorded types.
15787 If we didn't find it, it's our bug. */
15788 gdb_assert (sig_type != NULL);
15789 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15790
15791 return sig_type;
15792 }
15793
15794 /* Load the DIEs associated with type unit PER_CU into memory. */
15795
15796 static void
15797 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15798 {
15799 struct objfile *objfile = per_cu->objfile;
15800 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15801 sect_offset offset = per_cu->offset;
15802 struct signatured_type *sig_type;
15803
15804 dwarf2_read_section (objfile, sect);
15805
15806 /* We have the section offset, but we need the signature to do the
15807 hash table lookup. */
15808 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15809 the signature to assert we found the right one.
15810 Ok, but it's a lot of work. We should simplify things so any needed
15811 assert doesn't require all this clumsiness. */
15812 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15813
15814 gdb_assert (&sig_type->per_cu == per_cu);
15815 gdb_assert (sig_type->per_cu.cu == NULL);
15816
15817 read_signatured_type (sig_type);
15818
15819 gdb_assert (sig_type->per_cu.cu != NULL);
15820 }
15821
15822 /* die_reader_func for read_signatured_type.
15823 This is identical to load_full_comp_unit_reader,
15824 but is kept separate for now. */
15825
15826 static void
15827 read_signatured_type_reader (const struct die_reader_specs *reader,
15828 gdb_byte *info_ptr,
15829 struct die_info *comp_unit_die,
15830 int has_children,
15831 void *data)
15832 {
15833 struct dwarf2_cu *cu = reader->cu;
15834 struct attribute *attr;
15835
15836 gdb_assert (cu->die_hash == NULL);
15837 cu->die_hash =
15838 htab_create_alloc_ex (cu->header.length / 12,
15839 die_hash,
15840 die_eq,
15841 NULL,
15842 &cu->comp_unit_obstack,
15843 hashtab_obstack_allocate,
15844 dummy_obstack_deallocate);
15845
15846 if (has_children)
15847 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15848 &info_ptr, comp_unit_die);
15849 cu->dies = comp_unit_die;
15850 /* comp_unit_die is not stored in die_hash, no need. */
15851
15852 /* We try not to read any attributes in this function, because not
15853 all CUs needed for references have been loaded yet, and symbol
15854 table processing isn't initialized. But we have to set the CU language,
15855 or we won't be able to build types correctly.
15856 Similarly, if we do not read the producer, we can not apply
15857 producer-specific interpretation. */
15858 prepare_one_comp_unit (cu, cu->dies);
15859 }
15860
15861 /* Read in a signatured type and build its CU and DIEs.
15862 If the type is a stub for the real type in a DWO file,
15863 read in the real type from the DWO file as well. */
15864
15865 static void
15866 read_signatured_type (struct signatured_type *sig_type)
15867 {
15868 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15869
15870 gdb_assert (per_cu->is_debug_types);
15871 gdb_assert (per_cu->cu == NULL);
15872
15873 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15874 }
15875
15876 /* Decode simple location descriptions.
15877 Given a pointer to a dwarf block that defines a location, compute
15878 the location and return the value.
15879
15880 NOTE drow/2003-11-18: This function is called in two situations
15881 now: for the address of static or global variables (partial symbols
15882 only) and for offsets into structures which are expected to be
15883 (more or less) constant. The partial symbol case should go away,
15884 and only the constant case should remain. That will let this
15885 function complain more accurately. A few special modes are allowed
15886 without complaint for global variables (for instance, global
15887 register values and thread-local values).
15888
15889 A location description containing no operations indicates that the
15890 object is optimized out. The return value is 0 for that case.
15891 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15892 callers will only want a very basic result and this can become a
15893 complaint.
15894
15895 Note that stack[0] is unused except as a default error return. */
15896
15897 static CORE_ADDR
15898 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15899 {
15900 struct objfile *objfile = cu->objfile;
15901 int i;
15902 int size = blk->size;
15903 gdb_byte *data = blk->data;
15904 CORE_ADDR stack[64];
15905 int stacki;
15906 unsigned int bytes_read, unsnd;
15907 gdb_byte op;
15908
15909 i = 0;
15910 stacki = 0;
15911 stack[stacki] = 0;
15912 stack[++stacki] = 0;
15913
15914 while (i < size)
15915 {
15916 op = data[i++];
15917 switch (op)
15918 {
15919 case DW_OP_lit0:
15920 case DW_OP_lit1:
15921 case DW_OP_lit2:
15922 case DW_OP_lit3:
15923 case DW_OP_lit4:
15924 case DW_OP_lit5:
15925 case DW_OP_lit6:
15926 case DW_OP_lit7:
15927 case DW_OP_lit8:
15928 case DW_OP_lit9:
15929 case DW_OP_lit10:
15930 case DW_OP_lit11:
15931 case DW_OP_lit12:
15932 case DW_OP_lit13:
15933 case DW_OP_lit14:
15934 case DW_OP_lit15:
15935 case DW_OP_lit16:
15936 case DW_OP_lit17:
15937 case DW_OP_lit18:
15938 case DW_OP_lit19:
15939 case DW_OP_lit20:
15940 case DW_OP_lit21:
15941 case DW_OP_lit22:
15942 case DW_OP_lit23:
15943 case DW_OP_lit24:
15944 case DW_OP_lit25:
15945 case DW_OP_lit26:
15946 case DW_OP_lit27:
15947 case DW_OP_lit28:
15948 case DW_OP_lit29:
15949 case DW_OP_lit30:
15950 case DW_OP_lit31:
15951 stack[++stacki] = op - DW_OP_lit0;
15952 break;
15953
15954 case DW_OP_reg0:
15955 case DW_OP_reg1:
15956 case DW_OP_reg2:
15957 case DW_OP_reg3:
15958 case DW_OP_reg4:
15959 case DW_OP_reg5:
15960 case DW_OP_reg6:
15961 case DW_OP_reg7:
15962 case DW_OP_reg8:
15963 case DW_OP_reg9:
15964 case DW_OP_reg10:
15965 case DW_OP_reg11:
15966 case DW_OP_reg12:
15967 case DW_OP_reg13:
15968 case DW_OP_reg14:
15969 case DW_OP_reg15:
15970 case DW_OP_reg16:
15971 case DW_OP_reg17:
15972 case DW_OP_reg18:
15973 case DW_OP_reg19:
15974 case DW_OP_reg20:
15975 case DW_OP_reg21:
15976 case DW_OP_reg22:
15977 case DW_OP_reg23:
15978 case DW_OP_reg24:
15979 case DW_OP_reg25:
15980 case DW_OP_reg26:
15981 case DW_OP_reg27:
15982 case DW_OP_reg28:
15983 case DW_OP_reg29:
15984 case DW_OP_reg30:
15985 case DW_OP_reg31:
15986 stack[++stacki] = op - DW_OP_reg0;
15987 if (i < size)
15988 dwarf2_complex_location_expr_complaint ();
15989 break;
15990
15991 case DW_OP_regx:
15992 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15993 i += bytes_read;
15994 stack[++stacki] = unsnd;
15995 if (i < size)
15996 dwarf2_complex_location_expr_complaint ();
15997 break;
15998
15999 case DW_OP_addr:
16000 stack[++stacki] = read_address (objfile->obfd, &data[i],
16001 cu, &bytes_read);
16002 i += bytes_read;
16003 break;
16004
16005 case DW_OP_const1u:
16006 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16007 i += 1;
16008 break;
16009
16010 case DW_OP_const1s:
16011 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16012 i += 1;
16013 break;
16014
16015 case DW_OP_const2u:
16016 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16017 i += 2;
16018 break;
16019
16020 case DW_OP_const2s:
16021 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16022 i += 2;
16023 break;
16024
16025 case DW_OP_const4u:
16026 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16027 i += 4;
16028 break;
16029
16030 case DW_OP_const4s:
16031 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16032 i += 4;
16033 break;
16034
16035 case DW_OP_const8u:
16036 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16037 i += 8;
16038 break;
16039
16040 case DW_OP_constu:
16041 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16042 &bytes_read);
16043 i += bytes_read;
16044 break;
16045
16046 case DW_OP_consts:
16047 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16048 i += bytes_read;
16049 break;
16050
16051 case DW_OP_dup:
16052 stack[stacki + 1] = stack[stacki];
16053 stacki++;
16054 break;
16055
16056 case DW_OP_plus:
16057 stack[stacki - 1] += stack[stacki];
16058 stacki--;
16059 break;
16060
16061 case DW_OP_plus_uconst:
16062 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16063 &bytes_read);
16064 i += bytes_read;
16065 break;
16066
16067 case DW_OP_minus:
16068 stack[stacki - 1] -= stack[stacki];
16069 stacki--;
16070 break;
16071
16072 case DW_OP_deref:
16073 /* If we're not the last op, then we definitely can't encode
16074 this using GDB's address_class enum. This is valid for partial
16075 global symbols, although the variable's address will be bogus
16076 in the psymtab. */
16077 if (i < size)
16078 dwarf2_complex_location_expr_complaint ();
16079 break;
16080
16081 case DW_OP_GNU_push_tls_address:
16082 /* The top of the stack has the offset from the beginning
16083 of the thread control block at which the variable is located. */
16084 /* Nothing should follow this operator, so the top of stack would
16085 be returned. */
16086 /* This is valid for partial global symbols, but the variable's
16087 address will be bogus in the psymtab. Make it always at least
16088 non-zero to not look as a variable garbage collected by linker
16089 which have DW_OP_addr 0. */
16090 if (i < size)
16091 dwarf2_complex_location_expr_complaint ();
16092 stack[stacki]++;
16093 break;
16094
16095 case DW_OP_GNU_uninit:
16096 break;
16097
16098 case DW_OP_GNU_addr_index:
16099 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16100 &bytes_read);
16101 i += bytes_read;
16102 break;
16103
16104 default:
16105 {
16106 const char *name = dwarf_stack_op_name (op);
16107
16108 if (name)
16109 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16110 name);
16111 else
16112 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16113 op);
16114 }
16115
16116 return (stack[stacki]);
16117 }
16118
16119 /* Enforce maximum stack depth of SIZE-1 to avoid writing
16120 outside of the allocated space. Also enforce minimum>0. */
16121 if (stacki >= ARRAY_SIZE (stack) - 1)
16122 {
16123 complaint (&symfile_complaints,
16124 _("location description stack overflow"));
16125 return 0;
16126 }
16127
16128 if (stacki <= 0)
16129 {
16130 complaint (&symfile_complaints,
16131 _("location description stack underflow"));
16132 return 0;
16133 }
16134 }
16135 return (stack[stacki]);
16136 }
16137
16138 /* memory allocation interface */
16139
16140 static struct dwarf_block *
16141 dwarf_alloc_block (struct dwarf2_cu *cu)
16142 {
16143 struct dwarf_block *blk;
16144
16145 blk = (struct dwarf_block *)
16146 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
16147 return (blk);
16148 }
16149
16150 static struct abbrev_info *
16151 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
16152 {
16153 struct abbrev_info *abbrev;
16154
16155 abbrev = (struct abbrev_info *)
16156 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
16157 memset (abbrev, 0, sizeof (struct abbrev_info));
16158 return (abbrev);
16159 }
16160
16161 static struct die_info *
16162 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
16163 {
16164 struct die_info *die;
16165 size_t size = sizeof (struct die_info);
16166
16167 if (num_attrs > 1)
16168 size += (num_attrs - 1) * sizeof (struct attribute);
16169
16170 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
16171 memset (die, 0, sizeof (struct die_info));
16172 return (die);
16173 }
16174
16175 \f
16176 /* Macro support. */
16177
16178 /* Return the full name of file number I in *LH's file name table.
16179 Use COMP_DIR as the name of the current directory of the
16180 compilation. The result is allocated using xmalloc; the caller is
16181 responsible for freeing it. */
16182 static char *
16183 file_full_name (int file, struct line_header *lh, const char *comp_dir)
16184 {
16185 /* Is the file number a valid index into the line header's file name
16186 table? Remember that file numbers start with one, not zero. */
16187 if (1 <= file && file <= lh->num_file_names)
16188 {
16189 struct file_entry *fe = &lh->file_names[file - 1];
16190
16191 if (IS_ABSOLUTE_PATH (fe->name))
16192 return xstrdup (fe->name);
16193 else
16194 {
16195 const char *dir;
16196 int dir_len;
16197 char *full_name;
16198
16199 if (fe->dir_index)
16200 dir = lh->include_dirs[fe->dir_index - 1];
16201 else
16202 dir = comp_dir;
16203
16204 if (dir)
16205 {
16206 dir_len = strlen (dir);
16207 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
16208 strcpy (full_name, dir);
16209 full_name[dir_len] = '/';
16210 strcpy (full_name + dir_len + 1, fe->name);
16211 return full_name;
16212 }
16213 else
16214 return xstrdup (fe->name);
16215 }
16216 }
16217 else
16218 {
16219 /* The compiler produced a bogus file number. We can at least
16220 record the macro definitions made in the file, even if we
16221 won't be able to find the file by name. */
16222 char fake_name[80];
16223
16224 sprintf (fake_name, "<bad macro file number %d>", file);
16225
16226 complaint (&symfile_complaints,
16227 _("bad file number in macro information (%d)"),
16228 file);
16229
16230 return xstrdup (fake_name);
16231 }
16232 }
16233
16234
16235 static struct macro_source_file *
16236 macro_start_file (int file, int line,
16237 struct macro_source_file *current_file,
16238 const char *comp_dir,
16239 struct line_header *lh, struct objfile *objfile)
16240 {
16241 /* The full name of this source file. */
16242 char *full_name = file_full_name (file, lh, comp_dir);
16243
16244 /* We don't create a macro table for this compilation unit
16245 at all until we actually get a filename. */
16246 if (! pending_macros)
16247 pending_macros = new_macro_table (&objfile->objfile_obstack,
16248 objfile->macro_cache);
16249
16250 if (! current_file)
16251 /* If we have no current file, then this must be the start_file
16252 directive for the compilation unit's main source file. */
16253 current_file = macro_set_main (pending_macros, full_name);
16254 else
16255 current_file = macro_include (current_file, line, full_name);
16256
16257 xfree (full_name);
16258
16259 return current_file;
16260 }
16261
16262
16263 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
16264 followed by a null byte. */
16265 static char *
16266 copy_string (const char *buf, int len)
16267 {
16268 char *s = xmalloc (len + 1);
16269
16270 memcpy (s, buf, len);
16271 s[len] = '\0';
16272 return s;
16273 }
16274
16275
16276 static const char *
16277 consume_improper_spaces (const char *p, const char *body)
16278 {
16279 if (*p == ' ')
16280 {
16281 complaint (&symfile_complaints,
16282 _("macro definition contains spaces "
16283 "in formal argument list:\n`%s'"),
16284 body);
16285
16286 while (*p == ' ')
16287 p++;
16288 }
16289
16290 return p;
16291 }
16292
16293
16294 static void
16295 parse_macro_definition (struct macro_source_file *file, int line,
16296 const char *body)
16297 {
16298 const char *p;
16299
16300 /* The body string takes one of two forms. For object-like macro
16301 definitions, it should be:
16302
16303 <macro name> " " <definition>
16304
16305 For function-like macro definitions, it should be:
16306
16307 <macro name> "() " <definition>
16308 or
16309 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
16310
16311 Spaces may appear only where explicitly indicated, and in the
16312 <definition>.
16313
16314 The Dwarf 2 spec says that an object-like macro's name is always
16315 followed by a space, but versions of GCC around March 2002 omit
16316 the space when the macro's definition is the empty string.
16317
16318 The Dwarf 2 spec says that there should be no spaces between the
16319 formal arguments in a function-like macro's formal argument list,
16320 but versions of GCC around March 2002 include spaces after the
16321 commas. */
16322
16323
16324 /* Find the extent of the macro name. The macro name is terminated
16325 by either a space or null character (for an object-like macro) or
16326 an opening paren (for a function-like macro). */
16327 for (p = body; *p; p++)
16328 if (*p == ' ' || *p == '(')
16329 break;
16330
16331 if (*p == ' ' || *p == '\0')
16332 {
16333 /* It's an object-like macro. */
16334 int name_len = p - body;
16335 char *name = copy_string (body, name_len);
16336 const char *replacement;
16337
16338 if (*p == ' ')
16339 replacement = body + name_len + 1;
16340 else
16341 {
16342 dwarf2_macro_malformed_definition_complaint (body);
16343 replacement = body + name_len;
16344 }
16345
16346 macro_define_object (file, line, name, replacement);
16347
16348 xfree (name);
16349 }
16350 else if (*p == '(')
16351 {
16352 /* It's a function-like macro. */
16353 char *name = copy_string (body, p - body);
16354 int argc = 0;
16355 int argv_size = 1;
16356 char **argv = xmalloc (argv_size * sizeof (*argv));
16357
16358 p++;
16359
16360 p = consume_improper_spaces (p, body);
16361
16362 /* Parse the formal argument list. */
16363 while (*p && *p != ')')
16364 {
16365 /* Find the extent of the current argument name. */
16366 const char *arg_start = p;
16367
16368 while (*p && *p != ',' && *p != ')' && *p != ' ')
16369 p++;
16370
16371 if (! *p || p == arg_start)
16372 dwarf2_macro_malformed_definition_complaint (body);
16373 else
16374 {
16375 /* Make sure argv has room for the new argument. */
16376 if (argc >= argv_size)
16377 {
16378 argv_size *= 2;
16379 argv = xrealloc (argv, argv_size * sizeof (*argv));
16380 }
16381
16382 argv[argc++] = copy_string (arg_start, p - arg_start);
16383 }
16384
16385 p = consume_improper_spaces (p, body);
16386
16387 /* Consume the comma, if present. */
16388 if (*p == ',')
16389 {
16390 p++;
16391
16392 p = consume_improper_spaces (p, body);
16393 }
16394 }
16395
16396 if (*p == ')')
16397 {
16398 p++;
16399
16400 if (*p == ' ')
16401 /* Perfectly formed definition, no complaints. */
16402 macro_define_function (file, line, name,
16403 argc, (const char **) argv,
16404 p + 1);
16405 else if (*p == '\0')
16406 {
16407 /* Complain, but do define it. */
16408 dwarf2_macro_malformed_definition_complaint (body);
16409 macro_define_function (file, line, name,
16410 argc, (const char **) argv,
16411 p);
16412 }
16413 else
16414 /* Just complain. */
16415 dwarf2_macro_malformed_definition_complaint (body);
16416 }
16417 else
16418 /* Just complain. */
16419 dwarf2_macro_malformed_definition_complaint (body);
16420
16421 xfree (name);
16422 {
16423 int i;
16424
16425 for (i = 0; i < argc; i++)
16426 xfree (argv[i]);
16427 }
16428 xfree (argv);
16429 }
16430 else
16431 dwarf2_macro_malformed_definition_complaint (body);
16432 }
16433
16434 /* Skip some bytes from BYTES according to the form given in FORM.
16435 Returns the new pointer. */
16436
16437 static gdb_byte *
16438 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
16439 enum dwarf_form form,
16440 unsigned int offset_size,
16441 struct dwarf2_section_info *section)
16442 {
16443 unsigned int bytes_read;
16444
16445 switch (form)
16446 {
16447 case DW_FORM_data1:
16448 case DW_FORM_flag:
16449 ++bytes;
16450 break;
16451
16452 case DW_FORM_data2:
16453 bytes += 2;
16454 break;
16455
16456 case DW_FORM_data4:
16457 bytes += 4;
16458 break;
16459
16460 case DW_FORM_data8:
16461 bytes += 8;
16462 break;
16463
16464 case DW_FORM_string:
16465 read_direct_string (abfd, bytes, &bytes_read);
16466 bytes += bytes_read;
16467 break;
16468
16469 case DW_FORM_sec_offset:
16470 case DW_FORM_strp:
16471 bytes += offset_size;
16472 break;
16473
16474 case DW_FORM_block:
16475 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16476 bytes += bytes_read;
16477 break;
16478
16479 case DW_FORM_block1:
16480 bytes += 1 + read_1_byte (abfd, bytes);
16481 break;
16482 case DW_FORM_block2:
16483 bytes += 2 + read_2_bytes (abfd, bytes);
16484 break;
16485 case DW_FORM_block4:
16486 bytes += 4 + read_4_bytes (abfd, bytes);
16487 break;
16488
16489 case DW_FORM_sdata:
16490 case DW_FORM_udata:
16491 case DW_FORM_GNU_addr_index:
16492 case DW_FORM_GNU_str_index:
16493 bytes = skip_leb128 (abfd, bytes);
16494 break;
16495
16496 default:
16497 {
16498 complain:
16499 complaint (&symfile_complaints,
16500 _("invalid form 0x%x in `%s'"),
16501 form,
16502 section->asection->name);
16503 return NULL;
16504 }
16505 }
16506
16507 return bytes;
16508 }
16509
16510 /* A helper for dwarf_decode_macros that handles skipping an unknown
16511 opcode. Returns an updated pointer to the macro data buffer; or,
16512 on error, issues a complaint and returns NULL. */
16513
16514 static gdb_byte *
16515 skip_unknown_opcode (unsigned int opcode,
16516 gdb_byte **opcode_definitions,
16517 gdb_byte *mac_ptr,
16518 bfd *abfd,
16519 unsigned int offset_size,
16520 struct dwarf2_section_info *section)
16521 {
16522 unsigned int bytes_read, i;
16523 unsigned long arg;
16524 gdb_byte *defn;
16525
16526 if (opcode_definitions[opcode] == NULL)
16527 {
16528 complaint (&symfile_complaints,
16529 _("unrecognized DW_MACFINO opcode 0x%x"),
16530 opcode);
16531 return NULL;
16532 }
16533
16534 defn = opcode_definitions[opcode];
16535 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16536 defn += bytes_read;
16537
16538 for (i = 0; i < arg; ++i)
16539 {
16540 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
16541 if (mac_ptr == NULL)
16542 {
16543 /* skip_form_bytes already issued the complaint. */
16544 return NULL;
16545 }
16546 }
16547
16548 return mac_ptr;
16549 }
16550
16551 /* A helper function which parses the header of a macro section.
16552 If the macro section is the extended (for now called "GNU") type,
16553 then this updates *OFFSET_SIZE. Returns a pointer to just after
16554 the header, or issues a complaint and returns NULL on error. */
16555
16556 static gdb_byte *
16557 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16558 bfd *abfd,
16559 gdb_byte *mac_ptr,
16560 unsigned int *offset_size,
16561 int section_is_gnu)
16562 {
16563 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
16564
16565 if (section_is_gnu)
16566 {
16567 unsigned int version, flags;
16568
16569 version = read_2_bytes (abfd, mac_ptr);
16570 if (version != 4)
16571 {
16572 complaint (&symfile_complaints,
16573 _("unrecognized version `%d' in .debug_macro section"),
16574 version);
16575 return NULL;
16576 }
16577 mac_ptr += 2;
16578
16579 flags = read_1_byte (abfd, mac_ptr);
16580 ++mac_ptr;
16581 *offset_size = (flags & 1) ? 8 : 4;
16582
16583 if ((flags & 2) != 0)
16584 /* We don't need the line table offset. */
16585 mac_ptr += *offset_size;
16586
16587 /* Vendor opcode descriptions. */
16588 if ((flags & 4) != 0)
16589 {
16590 unsigned int i, count;
16591
16592 count = read_1_byte (abfd, mac_ptr);
16593 ++mac_ptr;
16594 for (i = 0; i < count; ++i)
16595 {
16596 unsigned int opcode, bytes_read;
16597 unsigned long arg;
16598
16599 opcode = read_1_byte (abfd, mac_ptr);
16600 ++mac_ptr;
16601 opcode_definitions[opcode] = mac_ptr;
16602 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16603 mac_ptr += bytes_read;
16604 mac_ptr += arg;
16605 }
16606 }
16607 }
16608
16609 return mac_ptr;
16610 }
16611
16612 /* A helper for dwarf_decode_macros that handles the GNU extensions,
16613 including DW_MACRO_GNU_transparent_include. */
16614
16615 static void
16616 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16617 struct macro_source_file *current_file,
16618 struct line_header *lh, char *comp_dir,
16619 struct dwarf2_section_info *section,
16620 int section_is_gnu,
16621 unsigned int offset_size,
16622 struct objfile *objfile,
16623 htab_t include_hash)
16624 {
16625 enum dwarf_macro_record_type macinfo_type;
16626 int at_commandline;
16627 gdb_byte *opcode_definitions[256];
16628
16629 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16630 &offset_size, section_is_gnu);
16631 if (mac_ptr == NULL)
16632 {
16633 /* We already issued a complaint. */
16634 return;
16635 }
16636
16637 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16638 GDB is still reading the definitions from command line. First
16639 DW_MACINFO_start_file will need to be ignored as it was already executed
16640 to create CURRENT_FILE for the main source holding also the command line
16641 definitions. On first met DW_MACINFO_start_file this flag is reset to
16642 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16643
16644 at_commandline = 1;
16645
16646 do
16647 {
16648 /* Do we at least have room for a macinfo type byte? */
16649 if (mac_ptr >= mac_end)
16650 {
16651 dwarf2_macros_too_long_complaint (section);
16652 break;
16653 }
16654
16655 macinfo_type = read_1_byte (abfd, mac_ptr);
16656 mac_ptr++;
16657
16658 /* Note that we rely on the fact that the corresponding GNU and
16659 DWARF constants are the same. */
16660 switch (macinfo_type)
16661 {
16662 /* A zero macinfo type indicates the end of the macro
16663 information. */
16664 case 0:
16665 break;
16666
16667 case DW_MACRO_GNU_define:
16668 case DW_MACRO_GNU_undef:
16669 case DW_MACRO_GNU_define_indirect:
16670 case DW_MACRO_GNU_undef_indirect:
16671 {
16672 unsigned int bytes_read;
16673 int line;
16674 char *body;
16675 int is_define;
16676
16677 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16678 mac_ptr += bytes_read;
16679
16680 if (macinfo_type == DW_MACRO_GNU_define
16681 || macinfo_type == DW_MACRO_GNU_undef)
16682 {
16683 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16684 mac_ptr += bytes_read;
16685 }
16686 else
16687 {
16688 LONGEST str_offset;
16689
16690 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16691 mac_ptr += offset_size;
16692
16693 body = read_indirect_string_at_offset (abfd, str_offset);
16694 }
16695
16696 is_define = (macinfo_type == DW_MACRO_GNU_define
16697 || macinfo_type == DW_MACRO_GNU_define_indirect);
16698 if (! current_file)
16699 {
16700 /* DWARF violation as no main source is present. */
16701 complaint (&symfile_complaints,
16702 _("debug info with no main source gives macro %s "
16703 "on line %d: %s"),
16704 is_define ? _("definition") : _("undefinition"),
16705 line, body);
16706 break;
16707 }
16708 if ((line == 0 && !at_commandline)
16709 || (line != 0 && at_commandline))
16710 complaint (&symfile_complaints,
16711 _("debug info gives %s macro %s with %s line %d: %s"),
16712 at_commandline ? _("command-line") : _("in-file"),
16713 is_define ? _("definition") : _("undefinition"),
16714 line == 0 ? _("zero") : _("non-zero"), line, body);
16715
16716 if (is_define)
16717 parse_macro_definition (current_file, line, body);
16718 else
16719 {
16720 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16721 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16722 macro_undef (current_file, line, body);
16723 }
16724 }
16725 break;
16726
16727 case DW_MACRO_GNU_start_file:
16728 {
16729 unsigned int bytes_read;
16730 int line, file;
16731
16732 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16733 mac_ptr += bytes_read;
16734 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16735 mac_ptr += bytes_read;
16736
16737 if ((line == 0 && !at_commandline)
16738 || (line != 0 && at_commandline))
16739 complaint (&symfile_complaints,
16740 _("debug info gives source %d included "
16741 "from %s at %s line %d"),
16742 file, at_commandline ? _("command-line") : _("file"),
16743 line == 0 ? _("zero") : _("non-zero"), line);
16744
16745 if (at_commandline)
16746 {
16747 /* This DW_MACRO_GNU_start_file was executed in the
16748 pass one. */
16749 at_commandline = 0;
16750 }
16751 else
16752 current_file = macro_start_file (file, line,
16753 current_file, comp_dir,
16754 lh, objfile);
16755 }
16756 break;
16757
16758 case DW_MACRO_GNU_end_file:
16759 if (! current_file)
16760 complaint (&symfile_complaints,
16761 _("macro debug info has an unmatched "
16762 "`close_file' directive"));
16763 else
16764 {
16765 current_file = current_file->included_by;
16766 if (! current_file)
16767 {
16768 enum dwarf_macro_record_type next_type;
16769
16770 /* GCC circa March 2002 doesn't produce the zero
16771 type byte marking the end of the compilation
16772 unit. Complain if it's not there, but exit no
16773 matter what. */
16774
16775 /* Do we at least have room for a macinfo type byte? */
16776 if (mac_ptr >= mac_end)
16777 {
16778 dwarf2_macros_too_long_complaint (section);
16779 return;
16780 }
16781
16782 /* We don't increment mac_ptr here, so this is just
16783 a look-ahead. */
16784 next_type = read_1_byte (abfd, mac_ptr);
16785 if (next_type != 0)
16786 complaint (&symfile_complaints,
16787 _("no terminating 0-type entry for "
16788 "macros in `.debug_macinfo' section"));
16789
16790 return;
16791 }
16792 }
16793 break;
16794
16795 case DW_MACRO_GNU_transparent_include:
16796 {
16797 LONGEST offset;
16798 void **slot;
16799
16800 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16801 mac_ptr += offset_size;
16802
16803 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16804 if (*slot != NULL)
16805 {
16806 /* This has actually happened; see
16807 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16808 complaint (&symfile_complaints,
16809 _("recursive DW_MACRO_GNU_transparent_include in "
16810 ".debug_macro section"));
16811 }
16812 else
16813 {
16814 *slot = mac_ptr;
16815
16816 dwarf_decode_macro_bytes (abfd,
16817 section->buffer + offset,
16818 mac_end, current_file,
16819 lh, comp_dir,
16820 section, section_is_gnu,
16821 offset_size, objfile, include_hash);
16822
16823 htab_remove_elt (include_hash, mac_ptr);
16824 }
16825 }
16826 break;
16827
16828 case DW_MACINFO_vendor_ext:
16829 if (!section_is_gnu)
16830 {
16831 unsigned int bytes_read;
16832 int constant;
16833
16834 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16835 mac_ptr += bytes_read;
16836 read_direct_string (abfd, mac_ptr, &bytes_read);
16837 mac_ptr += bytes_read;
16838
16839 /* We don't recognize any vendor extensions. */
16840 break;
16841 }
16842 /* FALLTHROUGH */
16843
16844 default:
16845 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16846 mac_ptr, abfd, offset_size,
16847 section);
16848 if (mac_ptr == NULL)
16849 return;
16850 break;
16851 }
16852 } while (macinfo_type != 0);
16853 }
16854
16855 static void
16856 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16857 char *comp_dir, bfd *abfd,
16858 struct dwarf2_cu *cu,
16859 struct dwarf2_section_info *section,
16860 int section_is_gnu, const char *section_name)
16861 {
16862 struct objfile *objfile = dwarf2_per_objfile->objfile;
16863 gdb_byte *mac_ptr, *mac_end;
16864 struct macro_source_file *current_file = 0;
16865 enum dwarf_macro_record_type macinfo_type;
16866 unsigned int offset_size = cu->header.offset_size;
16867 gdb_byte *opcode_definitions[256];
16868 struct cleanup *cleanup;
16869 htab_t include_hash;
16870 void **slot;
16871
16872 dwarf2_read_section (objfile, section);
16873 if (section->buffer == NULL)
16874 {
16875 complaint (&symfile_complaints, _("missing %s section"), section_name);
16876 return;
16877 }
16878
16879 /* First pass: Find the name of the base filename.
16880 This filename is needed in order to process all macros whose definition
16881 (or undefinition) comes from the command line. These macros are defined
16882 before the first DW_MACINFO_start_file entry, and yet still need to be
16883 associated to the base file.
16884
16885 To determine the base file name, we scan the macro definitions until we
16886 reach the first DW_MACINFO_start_file entry. We then initialize
16887 CURRENT_FILE accordingly so that any macro definition found before the
16888 first DW_MACINFO_start_file can still be associated to the base file. */
16889
16890 mac_ptr = section->buffer + offset;
16891 mac_end = section->buffer + section->size;
16892
16893 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16894 &offset_size, section_is_gnu);
16895 if (mac_ptr == NULL)
16896 {
16897 /* We already issued a complaint. */
16898 return;
16899 }
16900
16901 do
16902 {
16903 /* Do we at least have room for a macinfo type byte? */
16904 if (mac_ptr >= mac_end)
16905 {
16906 /* Complaint is printed during the second pass as GDB will probably
16907 stop the first pass earlier upon finding
16908 DW_MACINFO_start_file. */
16909 break;
16910 }
16911
16912 macinfo_type = read_1_byte (abfd, mac_ptr);
16913 mac_ptr++;
16914
16915 /* Note that we rely on the fact that the corresponding GNU and
16916 DWARF constants are the same. */
16917 switch (macinfo_type)
16918 {
16919 /* A zero macinfo type indicates the end of the macro
16920 information. */
16921 case 0:
16922 break;
16923
16924 case DW_MACRO_GNU_define:
16925 case DW_MACRO_GNU_undef:
16926 /* Only skip the data by MAC_PTR. */
16927 {
16928 unsigned int bytes_read;
16929
16930 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16931 mac_ptr += bytes_read;
16932 read_direct_string (abfd, mac_ptr, &bytes_read);
16933 mac_ptr += bytes_read;
16934 }
16935 break;
16936
16937 case DW_MACRO_GNU_start_file:
16938 {
16939 unsigned int bytes_read;
16940 int line, file;
16941
16942 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16943 mac_ptr += bytes_read;
16944 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16945 mac_ptr += bytes_read;
16946
16947 current_file = macro_start_file (file, line, current_file,
16948 comp_dir, lh, objfile);
16949 }
16950 break;
16951
16952 case DW_MACRO_GNU_end_file:
16953 /* No data to skip by MAC_PTR. */
16954 break;
16955
16956 case DW_MACRO_GNU_define_indirect:
16957 case DW_MACRO_GNU_undef_indirect:
16958 {
16959 unsigned int bytes_read;
16960
16961 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16962 mac_ptr += bytes_read;
16963 mac_ptr += offset_size;
16964 }
16965 break;
16966
16967 case DW_MACRO_GNU_transparent_include:
16968 /* Note that, according to the spec, a transparent include
16969 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16970 skip this opcode. */
16971 mac_ptr += offset_size;
16972 break;
16973
16974 case DW_MACINFO_vendor_ext:
16975 /* Only skip the data by MAC_PTR. */
16976 if (!section_is_gnu)
16977 {
16978 unsigned int bytes_read;
16979
16980 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16981 mac_ptr += bytes_read;
16982 read_direct_string (abfd, mac_ptr, &bytes_read);
16983 mac_ptr += bytes_read;
16984 }
16985 /* FALLTHROUGH */
16986
16987 default:
16988 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16989 mac_ptr, abfd, offset_size,
16990 section);
16991 if (mac_ptr == NULL)
16992 return;
16993 break;
16994 }
16995 } while (macinfo_type != 0 && current_file == NULL);
16996
16997 /* Second pass: Process all entries.
16998
16999 Use the AT_COMMAND_LINE flag to determine whether we are still processing
17000 command-line macro definitions/undefinitions. This flag is unset when we
17001 reach the first DW_MACINFO_start_file entry. */
17002
17003 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
17004 NULL, xcalloc, xfree);
17005 cleanup = make_cleanup_htab_delete (include_hash);
17006 mac_ptr = section->buffer + offset;
17007 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17008 *slot = mac_ptr;
17009 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
17010 current_file, lh, comp_dir, section, section_is_gnu,
17011 offset_size, objfile, include_hash);
17012 do_cleanups (cleanup);
17013 }
17014
17015 /* Check if the attribute's form is a DW_FORM_block*
17016 if so return true else false. */
17017
17018 static int
17019 attr_form_is_block (struct attribute *attr)
17020 {
17021 return (attr == NULL ? 0 :
17022 attr->form == DW_FORM_block1
17023 || attr->form == DW_FORM_block2
17024 || attr->form == DW_FORM_block4
17025 || attr->form == DW_FORM_block
17026 || attr->form == DW_FORM_exprloc);
17027 }
17028
17029 /* Return non-zero if ATTR's value is a section offset --- classes
17030 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
17031 You may use DW_UNSND (attr) to retrieve such offsets.
17032
17033 Section 7.5.4, "Attribute Encodings", explains that no attribute
17034 may have a value that belongs to more than one of these classes; it
17035 would be ambiguous if we did, because we use the same forms for all
17036 of them. */
17037
17038 static int
17039 attr_form_is_section_offset (struct attribute *attr)
17040 {
17041 return (attr->form == DW_FORM_data4
17042 || attr->form == DW_FORM_data8
17043 || attr->form == DW_FORM_sec_offset);
17044 }
17045
17046 /* Return non-zero if ATTR's value falls in the 'constant' class, or
17047 zero otherwise. When this function returns true, you can apply
17048 dwarf2_get_attr_constant_value to it.
17049
17050 However, note that for some attributes you must check
17051 attr_form_is_section_offset before using this test. DW_FORM_data4
17052 and DW_FORM_data8 are members of both the constant class, and of
17053 the classes that contain offsets into other debug sections
17054 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
17055 that, if an attribute's can be either a constant or one of the
17056 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
17057 taken as section offsets, not constants. */
17058
17059 static int
17060 attr_form_is_constant (struct attribute *attr)
17061 {
17062 switch (attr->form)
17063 {
17064 case DW_FORM_sdata:
17065 case DW_FORM_udata:
17066 case DW_FORM_data1:
17067 case DW_FORM_data2:
17068 case DW_FORM_data4:
17069 case DW_FORM_data8:
17070 return 1;
17071 default:
17072 return 0;
17073 }
17074 }
17075
17076 /* Return the .debug_loc section to use for CU.
17077 For DWO files use .debug_loc.dwo. */
17078
17079 static struct dwarf2_section_info *
17080 cu_debug_loc_section (struct dwarf2_cu *cu)
17081 {
17082 if (cu->dwo_unit)
17083 return &cu->dwo_unit->dwo_file->sections.loc;
17084 return &dwarf2_per_objfile->loc;
17085 }
17086
17087 /* A helper function that fills in a dwarf2_loclist_baton. */
17088
17089 static void
17090 fill_in_loclist_baton (struct dwarf2_cu *cu,
17091 struct dwarf2_loclist_baton *baton,
17092 struct attribute *attr)
17093 {
17094 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17095
17096 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17097
17098 baton->per_cu = cu->per_cu;
17099 gdb_assert (baton->per_cu);
17100 /* We don't know how long the location list is, but make sure we
17101 don't run off the edge of the section. */
17102 baton->size = section->size - DW_UNSND (attr);
17103 baton->data = section->buffer + DW_UNSND (attr);
17104 baton->base_address = cu->base_address;
17105 }
17106
17107 static void
17108 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
17109 struct dwarf2_cu *cu)
17110 {
17111 struct objfile *objfile = dwarf2_per_objfile->objfile;
17112 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17113
17114 if (attr_form_is_section_offset (attr)
17115 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
17116 the section. If so, fall through to the complaint in the
17117 other branch. */
17118 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
17119 {
17120 struct dwarf2_loclist_baton *baton;
17121
17122 baton = obstack_alloc (&objfile->objfile_obstack,
17123 sizeof (struct dwarf2_loclist_baton));
17124
17125 fill_in_loclist_baton (cu, baton, attr);
17126
17127 if (cu->base_known == 0)
17128 complaint (&symfile_complaints,
17129 _("Location list used without "
17130 "specifying the CU base address."));
17131
17132 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
17133 SYMBOL_LOCATION_BATON (sym) = baton;
17134 }
17135 else
17136 {
17137 struct dwarf2_locexpr_baton *baton;
17138
17139 baton = obstack_alloc (&objfile->objfile_obstack,
17140 sizeof (struct dwarf2_locexpr_baton));
17141 baton->per_cu = cu->per_cu;
17142 gdb_assert (baton->per_cu);
17143
17144 if (attr_form_is_block (attr))
17145 {
17146 /* Note that we're just copying the block's data pointer
17147 here, not the actual data. We're still pointing into the
17148 info_buffer for SYM's objfile; right now we never release
17149 that buffer, but when we do clean up properly this may
17150 need to change. */
17151 baton->size = DW_BLOCK (attr)->size;
17152 baton->data = DW_BLOCK (attr)->data;
17153 }
17154 else
17155 {
17156 dwarf2_invalid_attrib_class_complaint ("location description",
17157 SYMBOL_NATURAL_NAME (sym));
17158 baton->size = 0;
17159 }
17160
17161 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
17162 SYMBOL_LOCATION_BATON (sym) = baton;
17163 }
17164 }
17165
17166 /* Return the OBJFILE associated with the compilation unit CU. If CU
17167 came from a separate debuginfo file, then the master objfile is
17168 returned. */
17169
17170 struct objfile *
17171 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
17172 {
17173 struct objfile *objfile = per_cu->objfile;
17174
17175 /* Return the master objfile, so that we can report and look up the
17176 correct file containing this variable. */
17177 if (objfile->separate_debug_objfile_backlink)
17178 objfile = objfile->separate_debug_objfile_backlink;
17179
17180 return objfile;
17181 }
17182
17183 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
17184 (CU_HEADERP is unused in such case) or prepare a temporary copy at
17185 CU_HEADERP first. */
17186
17187 static const struct comp_unit_head *
17188 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
17189 struct dwarf2_per_cu_data *per_cu)
17190 {
17191 struct objfile *objfile;
17192 struct dwarf2_per_objfile *per_objfile;
17193 gdb_byte *info_ptr;
17194
17195 if (per_cu->cu)
17196 return &per_cu->cu->header;
17197
17198 objfile = per_cu->objfile;
17199 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17200 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
17201
17202 memset (cu_headerp, 0, sizeof (*cu_headerp));
17203 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
17204
17205 return cu_headerp;
17206 }
17207
17208 /* Return the address size given in the compilation unit header for CU. */
17209
17210 int
17211 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
17212 {
17213 struct comp_unit_head cu_header_local;
17214 const struct comp_unit_head *cu_headerp;
17215
17216 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17217
17218 return cu_headerp->addr_size;
17219 }
17220
17221 /* Return the offset size given in the compilation unit header for CU. */
17222
17223 int
17224 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
17225 {
17226 struct comp_unit_head cu_header_local;
17227 const struct comp_unit_head *cu_headerp;
17228
17229 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17230
17231 return cu_headerp->offset_size;
17232 }
17233
17234 /* See its dwarf2loc.h declaration. */
17235
17236 int
17237 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
17238 {
17239 struct comp_unit_head cu_header_local;
17240 const struct comp_unit_head *cu_headerp;
17241
17242 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
17243
17244 if (cu_headerp->version == 2)
17245 return cu_headerp->addr_size;
17246 else
17247 return cu_headerp->offset_size;
17248 }
17249
17250 /* Return the text offset of the CU. The returned offset comes from
17251 this CU's objfile. If this objfile came from a separate debuginfo
17252 file, then the offset may be different from the corresponding
17253 offset in the parent objfile. */
17254
17255 CORE_ADDR
17256 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
17257 {
17258 struct objfile *objfile = per_cu->objfile;
17259
17260 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17261 }
17262
17263 /* Locate the .debug_info compilation unit from CU's objfile which contains
17264 the DIE at OFFSET. Raises an error on failure. */
17265
17266 static struct dwarf2_per_cu_data *
17267 dwarf2_find_containing_comp_unit (sect_offset offset,
17268 struct objfile *objfile)
17269 {
17270 struct dwarf2_per_cu_data *this_cu;
17271 int low, high;
17272
17273 low = 0;
17274 high = dwarf2_per_objfile->n_comp_units - 1;
17275 while (high > low)
17276 {
17277 int mid = low + (high - low) / 2;
17278
17279 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
17280 >= offset.sect_off)
17281 high = mid;
17282 else
17283 low = mid + 1;
17284 }
17285 gdb_assert (low == high);
17286 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
17287 > offset.sect_off)
17288 {
17289 if (low == 0)
17290 error (_("Dwarf Error: could not find partial DIE containing "
17291 "offset 0x%lx [in module %s]"),
17292 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
17293
17294 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
17295 <= offset.sect_off);
17296 return dwarf2_per_objfile->all_comp_units[low-1];
17297 }
17298 else
17299 {
17300 this_cu = dwarf2_per_objfile->all_comp_units[low];
17301 if (low == dwarf2_per_objfile->n_comp_units - 1
17302 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
17303 error (_("invalid dwarf2 offset %u"), offset.sect_off);
17304 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
17305 return this_cu;
17306 }
17307 }
17308
17309 /* Initialize dwarf2_cu CU, owned by PER_CU. */
17310
17311 static void
17312 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
17313 {
17314 memset (cu, 0, sizeof (*cu));
17315 per_cu->cu = cu;
17316 cu->per_cu = per_cu;
17317 cu->objfile = per_cu->objfile;
17318 obstack_init (&cu->comp_unit_obstack);
17319 }
17320
17321 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17322
17323 static void
17324 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
17325 {
17326 struct attribute *attr;
17327
17328 /* Set the language we're debugging. */
17329 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17330 if (attr)
17331 set_cu_language (DW_UNSND (attr), cu);
17332 else
17333 {
17334 cu->language = language_minimal;
17335 cu->language_defn = language_def (cu->language);
17336 }
17337
17338 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17339 if (attr)
17340 cu->producer = DW_STRING (attr);
17341 }
17342
17343 /* Release one cached compilation unit, CU. We unlink it from the tree
17344 of compilation units, but we don't remove it from the read_in_chain;
17345 the caller is responsible for that.
17346 NOTE: DATA is a void * because this function is also used as a
17347 cleanup routine. */
17348
17349 static void
17350 free_heap_comp_unit (void *data)
17351 {
17352 struct dwarf2_cu *cu = data;
17353
17354 gdb_assert (cu->per_cu != NULL);
17355 cu->per_cu->cu = NULL;
17356 cu->per_cu = NULL;
17357
17358 obstack_free (&cu->comp_unit_obstack, NULL);
17359
17360 xfree (cu);
17361 }
17362
17363 /* This cleanup function is passed the address of a dwarf2_cu on the stack
17364 when we're finished with it. We can't free the pointer itself, but be
17365 sure to unlink it from the cache. Also release any associated storage. */
17366
17367 static void
17368 free_stack_comp_unit (void *data)
17369 {
17370 struct dwarf2_cu *cu = data;
17371
17372 gdb_assert (cu->per_cu != NULL);
17373 cu->per_cu->cu = NULL;
17374 cu->per_cu = NULL;
17375
17376 obstack_free (&cu->comp_unit_obstack, NULL);
17377 cu->partial_dies = NULL;
17378 }
17379
17380 /* Free all cached compilation units. */
17381
17382 static void
17383 free_cached_comp_units (void *data)
17384 {
17385 struct dwarf2_per_cu_data *per_cu, **last_chain;
17386
17387 per_cu = dwarf2_per_objfile->read_in_chain;
17388 last_chain = &dwarf2_per_objfile->read_in_chain;
17389 while (per_cu != NULL)
17390 {
17391 struct dwarf2_per_cu_data *next_cu;
17392
17393 next_cu = per_cu->cu->read_in_chain;
17394
17395 free_heap_comp_unit (per_cu->cu);
17396 *last_chain = next_cu;
17397
17398 per_cu = next_cu;
17399 }
17400 }
17401
17402 /* Increase the age counter on each cached compilation unit, and free
17403 any that are too old. */
17404
17405 static void
17406 age_cached_comp_units (void)
17407 {
17408 struct dwarf2_per_cu_data *per_cu, **last_chain;
17409
17410 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17411 per_cu = dwarf2_per_objfile->read_in_chain;
17412 while (per_cu != NULL)
17413 {
17414 per_cu->cu->last_used ++;
17415 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17416 dwarf2_mark (per_cu->cu);
17417 per_cu = per_cu->cu->read_in_chain;
17418 }
17419
17420 per_cu = dwarf2_per_objfile->read_in_chain;
17421 last_chain = &dwarf2_per_objfile->read_in_chain;
17422 while (per_cu != NULL)
17423 {
17424 struct dwarf2_per_cu_data *next_cu;
17425
17426 next_cu = per_cu->cu->read_in_chain;
17427
17428 if (!per_cu->cu->mark)
17429 {
17430 free_heap_comp_unit (per_cu->cu);
17431 *last_chain = next_cu;
17432 }
17433 else
17434 last_chain = &per_cu->cu->read_in_chain;
17435
17436 per_cu = next_cu;
17437 }
17438 }
17439
17440 /* Remove a single compilation unit from the cache. */
17441
17442 static void
17443 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
17444 {
17445 struct dwarf2_per_cu_data *per_cu, **last_chain;
17446
17447 per_cu = dwarf2_per_objfile->read_in_chain;
17448 last_chain = &dwarf2_per_objfile->read_in_chain;
17449 while (per_cu != NULL)
17450 {
17451 struct dwarf2_per_cu_data *next_cu;
17452
17453 next_cu = per_cu->cu->read_in_chain;
17454
17455 if (per_cu == target_per_cu)
17456 {
17457 free_heap_comp_unit (per_cu->cu);
17458 per_cu->cu = NULL;
17459 *last_chain = next_cu;
17460 break;
17461 }
17462 else
17463 last_chain = &per_cu->cu->read_in_chain;
17464
17465 per_cu = next_cu;
17466 }
17467 }
17468
17469 /* Release all extra memory associated with OBJFILE. */
17470
17471 void
17472 dwarf2_free_objfile (struct objfile *objfile)
17473 {
17474 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17475
17476 if (dwarf2_per_objfile == NULL)
17477 return;
17478
17479 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17480 free_cached_comp_units (NULL);
17481
17482 if (dwarf2_per_objfile->quick_file_names_table)
17483 htab_delete (dwarf2_per_objfile->quick_file_names_table);
17484
17485 /* Everything else should be on the objfile obstack. */
17486 }
17487
17488 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17489 We store these in a hash table separate from the DIEs, and preserve them
17490 when the DIEs are flushed out of cache.
17491
17492 The CU "per_cu" pointer is needed because offset alone is not enough to
17493 uniquely identify the type. A file may have multiple .debug_types sections,
17494 or the type may come from a DWO file. We have to use something in
17495 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17496 routine, get_die_type_at_offset, from outside this file, and thus won't
17497 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17498 of the objfile. */
17499
17500 struct dwarf2_per_cu_offset_and_type
17501 {
17502 const struct dwarf2_per_cu_data *per_cu;
17503 sect_offset offset;
17504 struct type *type;
17505 };
17506
17507 /* Hash function for a dwarf2_per_cu_offset_and_type. */
17508
17509 static hashval_t
17510 per_cu_offset_and_type_hash (const void *item)
17511 {
17512 const struct dwarf2_per_cu_offset_and_type *ofs = item;
17513
17514 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
17515 }
17516
17517 /* Equality function for a dwarf2_per_cu_offset_and_type. */
17518
17519 static int
17520 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
17521 {
17522 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17523 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
17524
17525 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17526 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
17527 }
17528
17529 /* Set the type associated with DIE to TYPE. Save it in CU's hash
17530 table if necessary. For convenience, return TYPE.
17531
17532 The DIEs reading must have careful ordering to:
17533 * Not cause infite loops trying to read in DIEs as a prerequisite for
17534 reading current DIE.
17535 * Not trying to dereference contents of still incompletely read in types
17536 while reading in other DIEs.
17537 * Enable referencing still incompletely read in types just by a pointer to
17538 the type without accessing its fields.
17539
17540 Therefore caller should follow these rules:
17541 * Try to fetch any prerequisite types we may need to build this DIE type
17542 before building the type and calling set_die_type.
17543 * After building type call set_die_type for current DIE as soon as
17544 possible before fetching more types to complete the current type.
17545 * Make the type as complete as possible before fetching more types. */
17546
17547 static struct type *
17548 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17549 {
17550 struct dwarf2_per_cu_offset_and_type **slot, ofs;
17551 struct objfile *objfile = cu->objfile;
17552
17553 /* For Ada types, make sure that the gnat-specific data is always
17554 initialized (if not already set). There are a few types where
17555 we should not be doing so, because the type-specific area is
17556 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17557 where the type-specific area is used to store the floatformat).
17558 But this is not a problem, because the gnat-specific information
17559 is actually not needed for these types. */
17560 if (need_gnat_info (cu)
17561 && TYPE_CODE (type) != TYPE_CODE_FUNC
17562 && TYPE_CODE (type) != TYPE_CODE_FLT
17563 && !HAVE_GNAT_AUX_INFO (type))
17564 INIT_GNAT_SPECIFIC (type);
17565
17566 if (dwarf2_per_objfile->die_type_hash == NULL)
17567 {
17568 dwarf2_per_objfile->die_type_hash =
17569 htab_create_alloc_ex (127,
17570 per_cu_offset_and_type_hash,
17571 per_cu_offset_and_type_eq,
17572 NULL,
17573 &objfile->objfile_obstack,
17574 hashtab_obstack_allocate,
17575 dummy_obstack_deallocate);
17576 }
17577
17578 ofs.per_cu = cu->per_cu;
17579 ofs.offset = die->offset;
17580 ofs.type = type;
17581 slot = (struct dwarf2_per_cu_offset_and_type **)
17582 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
17583 if (*slot)
17584 complaint (&symfile_complaints,
17585 _("A problem internal to GDB: DIE 0x%x has type already set"),
17586 die->offset.sect_off);
17587 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
17588 **slot = ofs;
17589 return type;
17590 }
17591
17592 /* Look up the type for the die at OFFSET in the appropriate type_hash
17593 table, or return NULL if the die does not have a saved type. */
17594
17595 static struct type *
17596 get_die_type_at_offset (sect_offset offset,
17597 struct dwarf2_per_cu_data *per_cu)
17598 {
17599 struct dwarf2_per_cu_offset_and_type *slot, ofs;
17600
17601 if (dwarf2_per_objfile->die_type_hash == NULL)
17602 return NULL;
17603
17604 ofs.per_cu = per_cu;
17605 ofs.offset = offset;
17606 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
17607 if (slot)
17608 return slot->type;
17609 else
17610 return NULL;
17611 }
17612
17613 /* Look up the type for DIE in the appropriate type_hash table,
17614 or return NULL if DIE does not have a saved type. */
17615
17616 static struct type *
17617 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17618 {
17619 return get_die_type_at_offset (die->offset, cu->per_cu);
17620 }
17621
17622 /* Add a dependence relationship from CU to REF_PER_CU. */
17623
17624 static void
17625 dwarf2_add_dependence (struct dwarf2_cu *cu,
17626 struct dwarf2_per_cu_data *ref_per_cu)
17627 {
17628 void **slot;
17629
17630 if (cu->dependencies == NULL)
17631 cu->dependencies
17632 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17633 NULL, &cu->comp_unit_obstack,
17634 hashtab_obstack_allocate,
17635 dummy_obstack_deallocate);
17636
17637 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17638 if (*slot == NULL)
17639 *slot = ref_per_cu;
17640 }
17641
17642 /* Subroutine of dwarf2_mark to pass to htab_traverse.
17643 Set the mark field in every compilation unit in the
17644 cache that we must keep because we are keeping CU. */
17645
17646 static int
17647 dwarf2_mark_helper (void **slot, void *data)
17648 {
17649 struct dwarf2_per_cu_data *per_cu;
17650
17651 per_cu = (struct dwarf2_per_cu_data *) *slot;
17652
17653 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17654 reading of the chain. As such dependencies remain valid it is not much
17655 useful to track and undo them during QUIT cleanups. */
17656 if (per_cu->cu == NULL)
17657 return 1;
17658
17659 if (per_cu->cu->mark)
17660 return 1;
17661 per_cu->cu->mark = 1;
17662
17663 if (per_cu->cu->dependencies != NULL)
17664 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17665
17666 return 1;
17667 }
17668
17669 /* Set the mark field in CU and in every other compilation unit in the
17670 cache that we must keep because we are keeping CU. */
17671
17672 static void
17673 dwarf2_mark (struct dwarf2_cu *cu)
17674 {
17675 if (cu->mark)
17676 return;
17677 cu->mark = 1;
17678 if (cu->dependencies != NULL)
17679 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17680 }
17681
17682 static void
17683 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17684 {
17685 while (per_cu)
17686 {
17687 per_cu->cu->mark = 0;
17688 per_cu = per_cu->cu->read_in_chain;
17689 }
17690 }
17691
17692 /* Trivial hash function for partial_die_info: the hash value of a DIE
17693 is its offset in .debug_info for this objfile. */
17694
17695 static hashval_t
17696 partial_die_hash (const void *item)
17697 {
17698 const struct partial_die_info *part_die = item;
17699
17700 return part_die->offset.sect_off;
17701 }
17702
17703 /* Trivial comparison function for partial_die_info structures: two DIEs
17704 are equal if they have the same offset. */
17705
17706 static int
17707 partial_die_eq (const void *item_lhs, const void *item_rhs)
17708 {
17709 const struct partial_die_info *part_die_lhs = item_lhs;
17710 const struct partial_die_info *part_die_rhs = item_rhs;
17711
17712 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17713 }
17714
17715 static struct cmd_list_element *set_dwarf2_cmdlist;
17716 static struct cmd_list_element *show_dwarf2_cmdlist;
17717
17718 static void
17719 set_dwarf2_cmd (char *args, int from_tty)
17720 {
17721 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17722 }
17723
17724 static void
17725 show_dwarf2_cmd (char *args, int from_tty)
17726 {
17727 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17728 }
17729
17730 /* If section described by INFO was mmapped, munmap it now. */
17731
17732 static void
17733 munmap_section_buffer (struct dwarf2_section_info *info)
17734 {
17735 if (info->map_addr != NULL)
17736 {
17737 #ifdef HAVE_MMAP
17738 int res;
17739
17740 res = munmap (info->map_addr, info->map_len);
17741 gdb_assert (res == 0);
17742 #else
17743 /* Without HAVE_MMAP, we should never be here to begin with. */
17744 gdb_assert_not_reached ("no mmap support");
17745 #endif
17746 }
17747 }
17748
17749 /* munmap debug sections for OBJFILE, if necessary. */
17750
17751 static void
17752 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17753 {
17754 struct dwarf2_per_objfile *data = d;
17755 int ix;
17756 struct dwarf2_section_info *section;
17757
17758 /* This is sorted according to the order they're defined in to make it easier
17759 to keep in sync. */
17760 munmap_section_buffer (&data->info);
17761 munmap_section_buffer (&data->abbrev);
17762 munmap_section_buffer (&data->line);
17763 munmap_section_buffer (&data->loc);
17764 munmap_section_buffer (&data->macinfo);
17765 munmap_section_buffer (&data->macro);
17766 munmap_section_buffer (&data->str);
17767 munmap_section_buffer (&data->ranges);
17768 munmap_section_buffer (&data->addr);
17769 munmap_section_buffer (&data->frame);
17770 munmap_section_buffer (&data->eh_frame);
17771 munmap_section_buffer (&data->gdb_index);
17772
17773 for (ix = 0;
17774 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17775 ++ix)
17776 munmap_section_buffer (section);
17777
17778 VEC_free (dwarf2_section_info_def, data->types);
17779
17780 if (data->dwo_files)
17781 free_dwo_files (data->dwo_files, objfile);
17782 }
17783
17784 \f
17785 /* The "save gdb-index" command. */
17786
17787 /* The contents of the hash table we create when building the string
17788 table. */
17789 struct strtab_entry
17790 {
17791 offset_type offset;
17792 const char *str;
17793 };
17794
17795 /* Hash function for a strtab_entry.
17796
17797 Function is used only during write_hash_table so no index format backward
17798 compatibility is needed. */
17799
17800 static hashval_t
17801 hash_strtab_entry (const void *e)
17802 {
17803 const struct strtab_entry *entry = e;
17804 return mapped_index_string_hash (INT_MAX, entry->str);
17805 }
17806
17807 /* Equality function for a strtab_entry. */
17808
17809 static int
17810 eq_strtab_entry (const void *a, const void *b)
17811 {
17812 const struct strtab_entry *ea = a;
17813 const struct strtab_entry *eb = b;
17814 return !strcmp (ea->str, eb->str);
17815 }
17816
17817 /* Create a strtab_entry hash table. */
17818
17819 static htab_t
17820 create_strtab (void)
17821 {
17822 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17823 xfree, xcalloc, xfree);
17824 }
17825
17826 /* Add a string to the constant pool. Return the string's offset in
17827 host order. */
17828
17829 static offset_type
17830 add_string (htab_t table, struct obstack *cpool, const char *str)
17831 {
17832 void **slot;
17833 struct strtab_entry entry;
17834 struct strtab_entry *result;
17835
17836 entry.str = str;
17837 slot = htab_find_slot (table, &entry, INSERT);
17838 if (*slot)
17839 result = *slot;
17840 else
17841 {
17842 result = XNEW (struct strtab_entry);
17843 result->offset = obstack_object_size (cpool);
17844 result->str = str;
17845 obstack_grow_str0 (cpool, str);
17846 *slot = result;
17847 }
17848 return result->offset;
17849 }
17850
17851 /* An entry in the symbol table. */
17852 struct symtab_index_entry
17853 {
17854 /* The name of the symbol. */
17855 const char *name;
17856 /* The offset of the name in the constant pool. */
17857 offset_type index_offset;
17858 /* A sorted vector of the indices of all the CUs that hold an object
17859 of this name. */
17860 VEC (offset_type) *cu_indices;
17861 };
17862
17863 /* The symbol table. This is a power-of-2-sized hash table. */
17864 struct mapped_symtab
17865 {
17866 offset_type n_elements;
17867 offset_type size;
17868 struct symtab_index_entry **data;
17869 };
17870
17871 /* Hash function for a symtab_index_entry. */
17872
17873 static hashval_t
17874 hash_symtab_entry (const void *e)
17875 {
17876 const struct symtab_index_entry *entry = e;
17877 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17878 sizeof (offset_type) * VEC_length (offset_type,
17879 entry->cu_indices),
17880 0);
17881 }
17882
17883 /* Equality function for a symtab_index_entry. */
17884
17885 static int
17886 eq_symtab_entry (const void *a, const void *b)
17887 {
17888 const struct symtab_index_entry *ea = a;
17889 const struct symtab_index_entry *eb = b;
17890 int len = VEC_length (offset_type, ea->cu_indices);
17891 if (len != VEC_length (offset_type, eb->cu_indices))
17892 return 0;
17893 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17894 VEC_address (offset_type, eb->cu_indices),
17895 sizeof (offset_type) * len);
17896 }
17897
17898 /* Destroy a symtab_index_entry. */
17899
17900 static void
17901 delete_symtab_entry (void *p)
17902 {
17903 struct symtab_index_entry *entry = p;
17904 VEC_free (offset_type, entry->cu_indices);
17905 xfree (entry);
17906 }
17907
17908 /* Create a hash table holding symtab_index_entry objects. */
17909
17910 static htab_t
17911 create_symbol_hash_table (void)
17912 {
17913 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17914 delete_symtab_entry, xcalloc, xfree);
17915 }
17916
17917 /* Create a new mapped symtab object. */
17918
17919 static struct mapped_symtab *
17920 create_mapped_symtab (void)
17921 {
17922 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17923 symtab->n_elements = 0;
17924 symtab->size = 1024;
17925 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17926 return symtab;
17927 }
17928
17929 /* Destroy a mapped_symtab. */
17930
17931 static void
17932 cleanup_mapped_symtab (void *p)
17933 {
17934 struct mapped_symtab *symtab = p;
17935 /* The contents of the array are freed when the other hash table is
17936 destroyed. */
17937 xfree (symtab->data);
17938 xfree (symtab);
17939 }
17940
17941 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17942 the slot.
17943
17944 Function is used only during write_hash_table so no index format backward
17945 compatibility is needed. */
17946
17947 static struct symtab_index_entry **
17948 find_slot (struct mapped_symtab *symtab, const char *name)
17949 {
17950 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17951
17952 index = hash & (symtab->size - 1);
17953 step = ((hash * 17) & (symtab->size - 1)) | 1;
17954
17955 for (;;)
17956 {
17957 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17958 return &symtab->data[index];
17959 index = (index + step) & (symtab->size - 1);
17960 }
17961 }
17962
17963 /* Expand SYMTAB's hash table. */
17964
17965 static void
17966 hash_expand (struct mapped_symtab *symtab)
17967 {
17968 offset_type old_size = symtab->size;
17969 offset_type i;
17970 struct symtab_index_entry **old_entries = symtab->data;
17971
17972 symtab->size *= 2;
17973 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17974
17975 for (i = 0; i < old_size; ++i)
17976 {
17977 if (old_entries[i])
17978 {
17979 struct symtab_index_entry **slot = find_slot (symtab,
17980 old_entries[i]->name);
17981 *slot = old_entries[i];
17982 }
17983 }
17984
17985 xfree (old_entries);
17986 }
17987
17988 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17989 is the index of the CU in which the symbol appears. */
17990
17991 static void
17992 add_index_entry (struct mapped_symtab *symtab, const char *name,
17993 offset_type cu_index)
17994 {
17995 struct symtab_index_entry **slot;
17996
17997 ++symtab->n_elements;
17998 if (4 * symtab->n_elements / 3 >= symtab->size)
17999 hash_expand (symtab);
18000
18001 slot = find_slot (symtab, name);
18002 if (!*slot)
18003 {
18004 *slot = XNEW (struct symtab_index_entry);
18005 (*slot)->name = name;
18006 (*slot)->cu_indices = NULL;
18007 }
18008 /* Don't push an index twice. Due to how we add entries we only
18009 have to check the last one. */
18010 if (VEC_empty (offset_type, (*slot)->cu_indices)
18011 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
18012 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
18013 }
18014
18015 /* Add a vector of indices to the constant pool. */
18016
18017 static offset_type
18018 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
18019 struct symtab_index_entry *entry)
18020 {
18021 void **slot;
18022
18023 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
18024 if (!*slot)
18025 {
18026 offset_type len = VEC_length (offset_type, entry->cu_indices);
18027 offset_type val = MAYBE_SWAP (len);
18028 offset_type iter;
18029 int i;
18030
18031 *slot = entry;
18032 entry->index_offset = obstack_object_size (cpool);
18033
18034 obstack_grow (cpool, &val, sizeof (val));
18035 for (i = 0;
18036 VEC_iterate (offset_type, entry->cu_indices, i, iter);
18037 ++i)
18038 {
18039 val = MAYBE_SWAP (iter);
18040 obstack_grow (cpool, &val, sizeof (val));
18041 }
18042 }
18043 else
18044 {
18045 struct symtab_index_entry *old_entry = *slot;
18046 entry->index_offset = old_entry->index_offset;
18047 entry = old_entry;
18048 }
18049 return entry->index_offset;
18050 }
18051
18052 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
18053 constant pool entries going into the obstack CPOOL. */
18054
18055 static void
18056 write_hash_table (struct mapped_symtab *symtab,
18057 struct obstack *output, struct obstack *cpool)
18058 {
18059 offset_type i;
18060 htab_t symbol_hash_table;
18061 htab_t str_table;
18062
18063 symbol_hash_table = create_symbol_hash_table ();
18064 str_table = create_strtab ();
18065
18066 /* We add all the index vectors to the constant pool first, to
18067 ensure alignment is ok. */
18068 for (i = 0; i < symtab->size; ++i)
18069 {
18070 if (symtab->data[i])
18071 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
18072 }
18073
18074 /* Now write out the hash table. */
18075 for (i = 0; i < symtab->size; ++i)
18076 {
18077 offset_type str_off, vec_off;
18078
18079 if (symtab->data[i])
18080 {
18081 str_off = add_string (str_table, cpool, symtab->data[i]->name);
18082 vec_off = symtab->data[i]->index_offset;
18083 }
18084 else
18085 {
18086 /* While 0 is a valid constant pool index, it is not valid
18087 to have 0 for both offsets. */
18088 str_off = 0;
18089 vec_off = 0;
18090 }
18091
18092 str_off = MAYBE_SWAP (str_off);
18093 vec_off = MAYBE_SWAP (vec_off);
18094
18095 obstack_grow (output, &str_off, sizeof (str_off));
18096 obstack_grow (output, &vec_off, sizeof (vec_off));
18097 }
18098
18099 htab_delete (str_table);
18100 htab_delete (symbol_hash_table);
18101 }
18102
18103 /* Struct to map psymtab to CU index in the index file. */
18104 struct psymtab_cu_index_map
18105 {
18106 struct partial_symtab *psymtab;
18107 unsigned int cu_index;
18108 };
18109
18110 static hashval_t
18111 hash_psymtab_cu_index (const void *item)
18112 {
18113 const struct psymtab_cu_index_map *map = item;
18114
18115 return htab_hash_pointer (map->psymtab);
18116 }
18117
18118 static int
18119 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
18120 {
18121 const struct psymtab_cu_index_map *lhs = item_lhs;
18122 const struct psymtab_cu_index_map *rhs = item_rhs;
18123
18124 return lhs->psymtab == rhs->psymtab;
18125 }
18126
18127 /* Helper struct for building the address table. */
18128 struct addrmap_index_data
18129 {
18130 struct objfile *objfile;
18131 struct obstack *addr_obstack;
18132 htab_t cu_index_htab;
18133
18134 /* Non-zero if the previous_* fields are valid.
18135 We can't write an entry until we see the next entry (since it is only then
18136 that we know the end of the entry). */
18137 int previous_valid;
18138 /* Index of the CU in the table of all CUs in the index file. */
18139 unsigned int previous_cu_index;
18140 /* Start address of the CU. */
18141 CORE_ADDR previous_cu_start;
18142 };
18143
18144 /* Write an address entry to OBSTACK. */
18145
18146 static void
18147 add_address_entry (struct objfile *objfile, struct obstack *obstack,
18148 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
18149 {
18150 offset_type cu_index_to_write;
18151 char addr[8];
18152 CORE_ADDR baseaddr;
18153
18154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18155
18156 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
18157 obstack_grow (obstack, addr, 8);
18158 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
18159 obstack_grow (obstack, addr, 8);
18160 cu_index_to_write = MAYBE_SWAP (cu_index);
18161 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
18162 }
18163
18164 /* Worker function for traversing an addrmap to build the address table. */
18165
18166 static int
18167 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
18168 {
18169 struct addrmap_index_data *data = datap;
18170 struct partial_symtab *pst = obj;
18171 offset_type cu_index;
18172 void **slot;
18173
18174 if (data->previous_valid)
18175 add_address_entry (data->objfile, data->addr_obstack,
18176 data->previous_cu_start, start_addr,
18177 data->previous_cu_index);
18178
18179 data->previous_cu_start = start_addr;
18180 if (pst != NULL)
18181 {
18182 struct psymtab_cu_index_map find_map, *map;
18183 find_map.psymtab = pst;
18184 map = htab_find (data->cu_index_htab, &find_map);
18185 gdb_assert (map != NULL);
18186 data->previous_cu_index = map->cu_index;
18187 data->previous_valid = 1;
18188 }
18189 else
18190 data->previous_valid = 0;
18191
18192 return 0;
18193 }
18194
18195 /* Write OBJFILE's address map to OBSTACK.
18196 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
18197 in the index file. */
18198
18199 static void
18200 write_address_map (struct objfile *objfile, struct obstack *obstack,
18201 htab_t cu_index_htab)
18202 {
18203 struct addrmap_index_data addrmap_index_data;
18204
18205 /* When writing the address table, we have to cope with the fact that
18206 the addrmap iterator only provides the start of a region; we have to
18207 wait until the next invocation to get the start of the next region. */
18208
18209 addrmap_index_data.objfile = objfile;
18210 addrmap_index_data.addr_obstack = obstack;
18211 addrmap_index_data.cu_index_htab = cu_index_htab;
18212 addrmap_index_data.previous_valid = 0;
18213
18214 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
18215 &addrmap_index_data);
18216
18217 /* It's highly unlikely the last entry (end address = 0xff...ff)
18218 is valid, but we should still handle it.
18219 The end address is recorded as the start of the next region, but that
18220 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
18221 anyway. */
18222 if (addrmap_index_data.previous_valid)
18223 add_address_entry (objfile, obstack,
18224 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
18225 addrmap_index_data.previous_cu_index);
18226 }
18227
18228 /* Add a list of partial symbols to SYMTAB. */
18229
18230 static void
18231 write_psymbols (struct mapped_symtab *symtab,
18232 htab_t psyms_seen,
18233 struct partial_symbol **psymp,
18234 int count,
18235 offset_type cu_index,
18236 int is_static)
18237 {
18238 for (; count-- > 0; ++psymp)
18239 {
18240 void **slot, *lookup;
18241
18242 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
18243 error (_("Ada is not currently supported by the index"));
18244
18245 /* We only want to add a given psymbol once. However, we also
18246 want to account for whether it is global or static. So, we
18247 may add it twice, using slightly different values. */
18248 if (is_static)
18249 {
18250 uintptr_t val = 1 | (uintptr_t) *psymp;
18251
18252 lookup = (void *) val;
18253 }
18254 else
18255 lookup = *psymp;
18256
18257 /* Only add a given psymbol once. */
18258 slot = htab_find_slot (psyms_seen, lookup, INSERT);
18259 if (!*slot)
18260 {
18261 *slot = lookup;
18262 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
18263 }
18264 }
18265 }
18266
18267 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
18268 exception if there is an error. */
18269
18270 static void
18271 write_obstack (FILE *file, struct obstack *obstack)
18272 {
18273 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18274 file)
18275 != obstack_object_size (obstack))
18276 error (_("couldn't data write to file"));
18277 }
18278
18279 /* Unlink a file if the argument is not NULL. */
18280
18281 static void
18282 unlink_if_set (void *p)
18283 {
18284 char **filename = p;
18285 if (*filename)
18286 unlink (*filename);
18287 }
18288
18289 /* A helper struct used when iterating over debug_types. */
18290 struct signatured_type_index_data
18291 {
18292 struct objfile *objfile;
18293 struct mapped_symtab *symtab;
18294 struct obstack *types_list;
18295 htab_t psyms_seen;
18296 int cu_index;
18297 };
18298
18299 /* A helper function that writes a single signatured_type to an
18300 obstack. */
18301
18302 static int
18303 write_one_signatured_type (void **slot, void *d)
18304 {
18305 struct signatured_type_index_data *info = d;
18306 struct signatured_type *entry = (struct signatured_type *) *slot;
18307 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18308 struct partial_symtab *psymtab = per_cu->v.psymtab;
18309 gdb_byte val[8];
18310
18311 write_psymbols (info->symtab,
18312 info->psyms_seen,
18313 info->objfile->global_psymbols.list
18314 + psymtab->globals_offset,
18315 psymtab->n_global_syms, info->cu_index,
18316 0);
18317 write_psymbols (info->symtab,
18318 info->psyms_seen,
18319 info->objfile->static_psymbols.list
18320 + psymtab->statics_offset,
18321 psymtab->n_static_syms, info->cu_index,
18322 1);
18323
18324 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18325 entry->per_cu.offset.sect_off);
18326 obstack_grow (info->types_list, val, 8);
18327 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18328 entry->type_offset_in_tu.cu_off);
18329 obstack_grow (info->types_list, val, 8);
18330 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18331 obstack_grow (info->types_list, val, 8);
18332
18333 ++info->cu_index;
18334
18335 return 1;
18336 }
18337
18338 /* Create an index file for OBJFILE in the directory DIR. */
18339
18340 static void
18341 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18342 {
18343 struct cleanup *cleanup;
18344 char *filename, *cleanup_filename;
18345 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18346 struct obstack cu_list, types_cu_list;
18347 int i;
18348 FILE *out_file;
18349 struct mapped_symtab *symtab;
18350 offset_type val, size_of_contents, total_len;
18351 struct stat st;
18352 char buf[8];
18353 htab_t psyms_seen;
18354 htab_t cu_index_htab;
18355 struct psymtab_cu_index_map *psymtab_cu_index_map;
18356
18357 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
18358 return;
18359
18360 if (dwarf2_per_objfile->using_index)
18361 error (_("Cannot use an index to create the index"));
18362
18363 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18364 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18365
18366 if (stat (objfile->name, &st) < 0)
18367 perror_with_name (objfile->name);
18368
18369 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18370 INDEX_SUFFIX, (char *) NULL);
18371 cleanup = make_cleanup (xfree, filename);
18372
18373 out_file = fopen (filename, "wb");
18374 if (!out_file)
18375 error (_("Can't open `%s' for writing"), filename);
18376
18377 cleanup_filename = filename;
18378 make_cleanup (unlink_if_set, &cleanup_filename);
18379
18380 symtab = create_mapped_symtab ();
18381 make_cleanup (cleanup_mapped_symtab, symtab);
18382
18383 obstack_init (&addr_obstack);
18384 make_cleanup_obstack_free (&addr_obstack);
18385
18386 obstack_init (&cu_list);
18387 make_cleanup_obstack_free (&cu_list);
18388
18389 obstack_init (&types_cu_list);
18390 make_cleanup_obstack_free (&types_cu_list);
18391
18392 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18393 NULL, xcalloc, xfree);
18394 make_cleanup_htab_delete (psyms_seen);
18395
18396 /* While we're scanning CU's create a table that maps a psymtab pointer
18397 (which is what addrmap records) to its index (which is what is recorded
18398 in the index file). This will later be needed to write the address
18399 table. */
18400 cu_index_htab = htab_create_alloc (100,
18401 hash_psymtab_cu_index,
18402 eq_psymtab_cu_index,
18403 NULL, xcalloc, xfree);
18404 make_cleanup_htab_delete (cu_index_htab);
18405 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18406 xmalloc (sizeof (struct psymtab_cu_index_map)
18407 * dwarf2_per_objfile->n_comp_units);
18408 make_cleanup (xfree, psymtab_cu_index_map);
18409
18410 /* The CU list is already sorted, so we don't need to do additional
18411 work here. Also, the debug_types entries do not appear in
18412 all_comp_units, but only in their own hash table. */
18413 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18414 {
18415 struct dwarf2_per_cu_data *per_cu
18416 = dwarf2_per_objfile->all_comp_units[i];
18417 struct partial_symtab *psymtab = per_cu->v.psymtab;
18418 gdb_byte val[8];
18419 struct psymtab_cu_index_map *map;
18420 void **slot;
18421
18422 write_psymbols (symtab,
18423 psyms_seen,
18424 objfile->global_psymbols.list + psymtab->globals_offset,
18425 psymtab->n_global_syms, i,
18426 0);
18427 write_psymbols (symtab,
18428 psyms_seen,
18429 objfile->static_psymbols.list + psymtab->statics_offset,
18430 psymtab->n_static_syms, i,
18431 1);
18432
18433 map = &psymtab_cu_index_map[i];
18434 map->psymtab = psymtab;
18435 map->cu_index = i;
18436 slot = htab_find_slot (cu_index_htab, map, INSERT);
18437 gdb_assert (slot != NULL);
18438 gdb_assert (*slot == NULL);
18439 *slot = map;
18440
18441 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18442 per_cu->offset.sect_off);
18443 obstack_grow (&cu_list, val, 8);
18444 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
18445 obstack_grow (&cu_list, val, 8);
18446 }
18447
18448 /* Dump the address map. */
18449 write_address_map (objfile, &addr_obstack, cu_index_htab);
18450
18451 /* Write out the .debug_type entries, if any. */
18452 if (dwarf2_per_objfile->signatured_types)
18453 {
18454 struct signatured_type_index_data sig_data;
18455
18456 sig_data.objfile = objfile;
18457 sig_data.symtab = symtab;
18458 sig_data.types_list = &types_cu_list;
18459 sig_data.psyms_seen = psyms_seen;
18460 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18461 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18462 write_one_signatured_type, &sig_data);
18463 }
18464
18465 obstack_init (&constant_pool);
18466 make_cleanup_obstack_free (&constant_pool);
18467 obstack_init (&symtab_obstack);
18468 make_cleanup_obstack_free (&symtab_obstack);
18469 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18470
18471 obstack_init (&contents);
18472 make_cleanup_obstack_free (&contents);
18473 size_of_contents = 6 * sizeof (offset_type);
18474 total_len = size_of_contents;
18475
18476 /* The version number. */
18477 val = MAYBE_SWAP (6);
18478 obstack_grow (&contents, &val, sizeof (val));
18479
18480 /* The offset of the CU list from the start of the file. */
18481 val = MAYBE_SWAP (total_len);
18482 obstack_grow (&contents, &val, sizeof (val));
18483 total_len += obstack_object_size (&cu_list);
18484
18485 /* The offset of the types CU list from the start of the file. */
18486 val = MAYBE_SWAP (total_len);
18487 obstack_grow (&contents, &val, sizeof (val));
18488 total_len += obstack_object_size (&types_cu_list);
18489
18490 /* The offset of the address table from the start of the file. */
18491 val = MAYBE_SWAP (total_len);
18492 obstack_grow (&contents, &val, sizeof (val));
18493 total_len += obstack_object_size (&addr_obstack);
18494
18495 /* The offset of the symbol table from the start of the file. */
18496 val = MAYBE_SWAP (total_len);
18497 obstack_grow (&contents, &val, sizeof (val));
18498 total_len += obstack_object_size (&symtab_obstack);
18499
18500 /* The offset of the constant pool from the start of the file. */
18501 val = MAYBE_SWAP (total_len);
18502 obstack_grow (&contents, &val, sizeof (val));
18503 total_len += obstack_object_size (&constant_pool);
18504
18505 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18506
18507 write_obstack (out_file, &contents);
18508 write_obstack (out_file, &cu_list);
18509 write_obstack (out_file, &types_cu_list);
18510 write_obstack (out_file, &addr_obstack);
18511 write_obstack (out_file, &symtab_obstack);
18512 write_obstack (out_file, &constant_pool);
18513
18514 fclose (out_file);
18515
18516 /* We want to keep the file, so we set cleanup_filename to NULL
18517 here. See unlink_if_set. */
18518 cleanup_filename = NULL;
18519
18520 do_cleanups (cleanup);
18521 }
18522
18523 /* Implementation of the `save gdb-index' command.
18524
18525 Note that the file format used by this command is documented in the
18526 GDB manual. Any changes here must be documented there. */
18527
18528 static void
18529 save_gdb_index_command (char *arg, int from_tty)
18530 {
18531 struct objfile *objfile;
18532
18533 if (!arg || !*arg)
18534 error (_("usage: save gdb-index DIRECTORY"));
18535
18536 ALL_OBJFILES (objfile)
18537 {
18538 struct stat st;
18539
18540 /* If the objfile does not correspond to an actual file, skip it. */
18541 if (stat (objfile->name, &st) < 0)
18542 continue;
18543
18544 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18545 if (dwarf2_per_objfile)
18546 {
18547 volatile struct gdb_exception except;
18548
18549 TRY_CATCH (except, RETURN_MASK_ERROR)
18550 {
18551 write_psymtabs_to_index (objfile, arg);
18552 }
18553 if (except.reason < 0)
18554 exception_fprintf (gdb_stderr, except,
18555 _("Error while writing index for `%s': "),
18556 objfile->name);
18557 }
18558 }
18559 }
18560
18561 \f
18562
18563 int dwarf2_always_disassemble;
18564
18565 static void
18566 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18567 struct cmd_list_element *c, const char *value)
18568 {
18569 fprintf_filtered (file,
18570 _("Whether to always disassemble "
18571 "DWARF expressions is %s.\n"),
18572 value);
18573 }
18574
18575 static void
18576 show_check_physname (struct ui_file *file, int from_tty,
18577 struct cmd_list_element *c, const char *value)
18578 {
18579 fprintf_filtered (file,
18580 _("Whether to check \"physname\" is %s.\n"),
18581 value);
18582 }
18583
18584 void _initialize_dwarf2_read (void);
18585
18586 void
18587 _initialize_dwarf2_read (void)
18588 {
18589 struct cmd_list_element *c;
18590
18591 dwarf2_objfile_data_key
18592 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
18593
18594 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18595 Set DWARF 2 specific variables.\n\
18596 Configure DWARF 2 variables such as the cache size"),
18597 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18598 0/*allow-unknown*/, &maintenance_set_cmdlist);
18599
18600 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18601 Show DWARF 2 specific variables\n\
18602 Show DWARF 2 variables such as the cache size"),
18603 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18604 0/*allow-unknown*/, &maintenance_show_cmdlist);
18605
18606 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
18607 &dwarf2_max_cache_age, _("\
18608 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18609 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18610 A higher limit means that cached compilation units will be stored\n\
18611 in memory longer, and more total memory will be used. Zero disables\n\
18612 caching, which can slow down startup."),
18613 NULL,
18614 show_dwarf2_max_cache_age,
18615 &set_dwarf2_cmdlist,
18616 &show_dwarf2_cmdlist);
18617
18618 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18619 &dwarf2_always_disassemble, _("\
18620 Set whether `info address' always disassembles DWARF expressions."), _("\
18621 Show whether `info address' always disassembles DWARF expressions."), _("\
18622 When enabled, DWARF expressions are always printed in an assembly-like\n\
18623 syntax. When disabled, expressions will be printed in a more\n\
18624 conversational style, when possible."),
18625 NULL,
18626 show_dwarf2_always_disassemble,
18627 &set_dwarf2_cmdlist,
18628 &show_dwarf2_cmdlist);
18629
18630 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18631 Set debugging of the dwarf2 DIE reader."), _("\
18632 Show debugging of the dwarf2 DIE reader."), _("\
18633 When enabled (non-zero), DIEs are dumped after they are read in.\n\
18634 The value is the maximum depth to print."),
18635 NULL,
18636 NULL,
18637 &setdebuglist, &showdebuglist);
18638
18639 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18640 Set cross-checking of \"physname\" code against demangler."), _("\
18641 Show cross-checking of \"physname\" code against demangler."), _("\
18642 When enabled, GDB's internal \"physname\" code is checked against\n\
18643 the demangler."),
18644 NULL, show_check_physname,
18645 &setdebuglist, &showdebuglist);
18646
18647 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18648 _("\
18649 Save a gdb-index file.\n\
18650 Usage: save gdb-index DIRECTORY"),
18651 &save_cmdlist);
18652 set_cmd_completer (c, filename_completer);
18653 }