bfd/
[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 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
152 DEF_VEC_P (dwarf2_per_cu_ptr);
153
154 /* Collection of data recorded per objfile.
155 This hangs off of dwarf2_objfile_data_key. */
156
157 struct dwarf2_per_objfile
158 {
159 struct dwarf2_section_info info;
160 struct dwarf2_section_info abbrev;
161 struct dwarf2_section_info line;
162 struct dwarf2_section_info loc;
163 struct dwarf2_section_info macinfo;
164 struct dwarf2_section_info macro;
165 struct dwarf2_section_info str;
166 struct dwarf2_section_info ranges;
167 struct dwarf2_section_info addr;
168 struct dwarf2_section_info frame;
169 struct dwarf2_section_info eh_frame;
170 struct dwarf2_section_info gdb_index;
171
172 VEC (dwarf2_section_info_def) *types;
173
174 /* Back link. */
175 struct objfile *objfile;
176
177 /* Table of all the compilation units. This is used to locate
178 the target compilation unit of a particular reference. */
179 struct dwarf2_per_cu_data **all_comp_units;
180
181 /* The number of compilation units in ALL_COMP_UNITS. */
182 int n_comp_units;
183
184 /* The number of .debug_types-related CUs. */
185 int n_type_units;
186
187 /* The .debug_types-related CUs (TUs). */
188 struct dwarf2_per_cu_data **all_type_units;
189
190 /* A chain of compilation units that are currently read in, so that
191 they can be freed later. */
192 struct dwarf2_per_cu_data *read_in_chain;
193
194 /* A table mapping .debug_types signatures to its signatured_type entry.
195 This is NULL if the .debug_types section hasn't been read in yet. */
196 htab_t signatured_types;
197
198 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
199 This is NULL if the table hasn't been allocated yet. */
200 htab_t dwo_files;
201
202 /* A flag indicating wether this objfile has a section loaded at a
203 VMA of 0. */
204 int has_section_at_zero;
205
206 /* True if we are using the mapped index,
207 or we are faking it for OBJF_READNOW's sake. */
208 unsigned char using_index;
209
210 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
211 struct mapped_index *index_table;
212
213 /* When using index_table, this keeps track of all quick_file_names entries.
214 TUs can share line table entries with CUs or other TUs, and there can be
215 a lot more TUs than unique line tables, so we maintain a separate table
216 of all line table entries to support the sharing. */
217 htab_t quick_file_names_table;
218
219 /* Set during partial symbol reading, to prevent queueing of full
220 symbols. */
221 int reading_partial_symbols;
222
223 /* Table mapping type DIEs to their struct type *.
224 This is NULL if not allocated yet.
225 The mapping is done via (CU/TU signature + DIE offset) -> type. */
226 htab_t die_type_hash;
227
228 /* The CUs we recently read. */
229 VEC (dwarf2_per_cu_ptr) *just_read_cus;
230 };
231
232 static struct dwarf2_per_objfile *dwarf2_per_objfile;
233
234 /* Default names of the debugging sections. */
235
236 /* Note that if the debugging section has been compressed, it might
237 have a name like .zdebug_info. */
238
239 static const struct dwarf2_debug_sections dwarf2_elf_names =
240 {
241 { ".debug_info", ".zdebug_info" },
242 { ".debug_abbrev", ".zdebug_abbrev" },
243 { ".debug_line", ".zdebug_line" },
244 { ".debug_loc", ".zdebug_loc" },
245 { ".debug_macinfo", ".zdebug_macinfo" },
246 { ".debug_macro", ".zdebug_macro" },
247 { ".debug_str", ".zdebug_str" },
248 { ".debug_ranges", ".zdebug_ranges" },
249 { ".debug_types", ".zdebug_types" },
250 { ".debug_addr", ".zdebug_addr" },
251 { ".debug_frame", ".zdebug_frame" },
252 { ".eh_frame", NULL },
253 { ".gdb_index", ".zgdb_index" },
254 23
255 };
256
257 /* List of DWO sections. */
258
259 static const struct dwo_section_names
260 {
261 struct dwarf2_section_names abbrev_dwo;
262 struct dwarf2_section_names info_dwo;
263 struct dwarf2_section_names line_dwo;
264 struct dwarf2_section_names loc_dwo;
265 struct dwarf2_section_names str_dwo;
266 struct dwarf2_section_names str_offsets_dwo;
267 struct dwarf2_section_names types_dwo;
268 }
269 dwo_section_names =
270 {
271 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
272 { ".debug_info.dwo", ".zdebug_info.dwo" },
273 { ".debug_line.dwo", ".zdebug_line.dwo" },
274 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
275 { ".debug_str.dwo", ".zdebug_str.dwo" },
276 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
277 { ".debug_types.dwo", ".zdebug_types.dwo" },
278 };
279
280 /* local data types */
281
282 /* We hold several abbreviation tables in memory at the same time. */
283 #ifndef ABBREV_HASH_SIZE
284 #define ABBREV_HASH_SIZE 121
285 #endif
286
287 /* The data in a compilation unit header, after target2host
288 translation, looks like this. */
289 struct comp_unit_head
290 {
291 unsigned int length;
292 short version;
293 unsigned char addr_size;
294 unsigned char signed_addr_p;
295 sect_offset abbrev_offset;
296
297 /* Size of file offsets; either 4 or 8. */
298 unsigned int offset_size;
299
300 /* Size of the length field; either 4 or 12. */
301 unsigned int initial_length_size;
302
303 /* Offset to the first byte of this compilation unit header in the
304 .debug_info section, for resolving relative reference dies. */
305 sect_offset offset;
306
307 /* Offset to first die in this cu from the start of the cu.
308 This will be the first byte following the compilation unit header. */
309 cu_offset first_die_offset;
310 };
311
312 /* Type used for delaying computation of method physnames.
313 See comments for compute_delayed_physnames. */
314 struct delayed_method_info
315 {
316 /* The type to which the method is attached, i.e., its parent class. */
317 struct type *type;
318
319 /* The index of the method in the type's function fieldlists. */
320 int fnfield_index;
321
322 /* The index of the method in the fieldlist. */
323 int index;
324
325 /* The name of the DIE. */
326 const char *name;
327
328 /* The DIE associated with this method. */
329 struct die_info *die;
330 };
331
332 typedef struct delayed_method_info delayed_method_info;
333 DEF_VEC_O (delayed_method_info);
334
335 /* Internal state when decoding a particular compilation unit. */
336 struct dwarf2_cu
337 {
338 /* The objfile containing this compilation unit. */
339 struct objfile *objfile;
340
341 /* The header of the compilation unit. */
342 struct comp_unit_head header;
343
344 /* Base address of this compilation unit. */
345 CORE_ADDR base_address;
346
347 /* Non-zero if base_address has been set. */
348 int base_known;
349
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
354 const char *producer;
355
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
372
373 /* Hash table holding all the loaded partial DIEs
374 with partial_die->offset.SECT_OFF as hash. */
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
393 /* A hash table of DIE cu_offset for following references with
394 die_info->offset.sect_off as hash. */
395 htab_t die_hash;
396
397 /* Full DIEs if read in. */
398 struct die_info *dies;
399
400 /* A set of pointers to dwarf2_per_cu_data objects for compilation
401 units referenced by this one. Only set during full symbol processing;
402 partial symbol tables do not have dependencies. */
403 htab_t dependencies;
404
405 /* Header data from the line table, during full symbol processing. */
406 struct line_header *line_header;
407
408 /* A list of methods which need to have physnames computed
409 after all type information has been read. */
410 VEC (delayed_method_info) *method_list;
411
412 /* To be copied to symtab->call_site_htab. */
413 htab_t call_site_htab;
414
415 /* Non-NULL if this CU came from a DWO file. */
416 struct dwo_unit *dwo_unit;
417
418 /* The DW_AT_addr_base attribute if present, zero otherwise
419 (zero is a valid value though).
420 Note this value comes from the stub CU/TU's DIE. */
421 ULONGEST addr_base;
422
423 /* Mark used when releasing cached dies. */
424 unsigned int mark : 1;
425
426 /* This CU references .debug_loc. See the symtab->locations_valid field.
427 This test is imperfect as there may exist optimized debug code not using
428 any location list and still facing inlining issues if handled as
429 unoptimized code. For a future better test see GCC PR other/32998. */
430 unsigned int has_loclist : 1;
431
432 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
433 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
434 are valid. This information is cached because profiling CU expansion
435 showed excessive time spent in producer_is_gxx_lt_4_6. */
436 unsigned int checked_producer : 1;
437 unsigned int producer_is_gxx_lt_4_6 : 1;
438 unsigned int producer_is_icc : 1;
439
440 /* Non-zero if DW_AT_addr_base was found.
441 Used when processing DWO files. */
442 unsigned int have_addr_base : 1;
443 };
444
445 /* Persistent data held for a compilation unit, even when not
446 processing it. We put a pointer to this structure in the
447 read_symtab_private field of the psymtab. */
448
449 struct dwarf2_per_cu_data
450 {
451 /* The start offset and length of this compilation unit. 2**29-1
452 bytes should suffice to store the length of any compilation unit
453 - if it doesn't, GDB will fall over anyway.
454 NOTE: Unlike comp_unit_head.length, this length includes
455 initial_length_size.
456 If the DIE refers to a DWO file, this is always of the original die,
457 not the DWO file. */
458 sect_offset offset;
459 unsigned int length : 29;
460
461 /* Flag indicating this compilation unit will be read in before
462 any of the current compilation units are processed. */
463 unsigned int queued : 1;
464
465 /* This flag will be set when reading partial DIEs if we need to load
466 absolutely all DIEs for this compilation unit, instead of just the ones
467 we think are interesting. It gets set if we look for a DIE in the
468 hash table and don't find it. */
469 unsigned int load_all_dies : 1;
470
471 /* Non-zero if this CU is from .debug_types. */
472 unsigned int is_debug_types : 1;
473
474 /* The section this CU/TU lives in.
475 If the DIE refers to a DWO file, this is always the original die,
476 not the DWO file. */
477 struct dwarf2_section_info *info_or_types_section;
478
479 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
480 of the CU cache it gets reset to NULL again. */
481 struct dwarf2_cu *cu;
482
483 /* The corresponding objfile.
484 Normally we can get the objfile from dwarf2_per_objfile.
485 However we can enter this file with just a "per_cu" handle. */
486 struct objfile *objfile;
487
488 /* When using partial symbol tables, the 'psymtab' field is active.
489 Otherwise the 'quick' field is active. */
490 union
491 {
492 /* The partial symbol table associated with this compilation unit,
493 or NULL for unread partial units. */
494 struct partial_symtab *psymtab;
495
496 /* Data needed by the "quick" functions. */
497 struct dwarf2_per_cu_quick_data *quick;
498 } v;
499
500 /* The CUs we import using DW_TAG_imported_unit. This is filled in
501 while reading psymtabs, used to compute the psymtab dependencies,
502 and then cleared. Then it is filled in again while reading full
503 symbols, and only deleted when the objfile is destroyed. */
504 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
505 };
506
507 /* Entry in the signatured_types hash table. */
508
509 struct signatured_type
510 {
511 /* The type's signature. */
512 ULONGEST signature;
513
514 /* Offset in the TU of the type's DIE, as read from the TU header.
515 If the definition lives in a DWO file, this value is unusable. */
516 cu_offset type_offset_in_tu;
517
518 /* Offset in the section of the type's DIE.
519 If the definition lives in a DWO file, this is the offset in the
520 .debug_types.dwo section.
521 The value is zero until the actual value is known.
522 Zero is otherwise not a valid section offset. */
523 sect_offset type_offset_in_section;
524
525 /* The CU(/TU) of this type. */
526 struct dwarf2_per_cu_data per_cu;
527 };
528
529 /* These sections are what may appear in a "dwo" file. */
530
531 struct dwo_sections
532 {
533 struct dwarf2_section_info abbrev;
534 struct dwarf2_section_info info;
535 struct dwarf2_section_info line;
536 struct dwarf2_section_info loc;
537 struct dwarf2_section_info str;
538 struct dwarf2_section_info str_offsets;
539 VEC (dwarf2_section_info_def) *types;
540 };
541
542 /* Common bits of DWO CUs/TUs. */
543
544 struct dwo_unit
545 {
546 /* Backlink to the containing struct dwo_file. */
547 struct dwo_file *dwo_file;
548
549 /* The "id" that distinguishes this CU/TU.
550 .debug_info calls this "dwo_id", .debug_types calls this "signature".
551 Since signatures came first, we stick with it for consistency. */
552 ULONGEST signature;
553
554 /* The section this CU/TU lives in, in the DWO file. */
555 struct dwarf2_section_info *info_or_types_section;
556
557 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
558 sect_offset offset;
559 unsigned int length;
560
561 /* For types, offset in the type's DIE of the type defined by this TU. */
562 cu_offset type_offset_in_tu;
563 };
564
565 /* Data for one DWO file. */
566
567 struct dwo_file
568 {
569 /* The DW_AT_GNU_dwo_name attribute.
570 We don't manage space for this, it's an attribute. */
571 const char *dwo_name;
572
573 /* The bfd, when the file is open. Otherwise this is NULL. */
574 bfd *dwo_bfd;
575
576 /* Section info for this file. */
577 struct dwo_sections sections;
578
579 /* Table of CUs in the file.
580 Each element is a struct dwo_unit. */
581 htab_t cus;
582
583 /* Table of TUs in the file.
584 Each element is a struct dwo_unit. */
585 htab_t tus;
586 };
587
588 /* Struct used to pass misc. parameters to read_die_and_children, et
589 al. which are used for both .debug_info and .debug_types dies.
590 All parameters here are unchanging for the life of the call. This
591 struct exists to abstract away the constant parameters of die reading. */
592
593 struct die_reader_specs
594 {
595 /* die_section->asection->owner. */
596 bfd* abfd;
597
598 /* The CU of the DIE we are parsing. */
599 struct dwarf2_cu *cu;
600
601 /* Non-NULL if reading a DWO file. */
602 struct dwo_file *dwo_file;
603
604 /* The section the die comes from.
605 This is either .debug_info or .debug_types, or the .dwo variants. */
606 struct dwarf2_section_info *die_section;
607
608 /* die_section->buffer. */
609 gdb_byte *buffer;
610
611 /* The end of the buffer. */
612 const gdb_byte *buffer_end;
613 };
614
615 /* Type of function passed to init_cutu_and_read_dies, et.al. */
616 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
617 gdb_byte *info_ptr,
618 struct die_info *comp_unit_die,
619 int has_children,
620 void *data);
621
622 /* The line number information for a compilation unit (found in the
623 .debug_line section) begins with a "statement program header",
624 which contains the following information. */
625 struct line_header
626 {
627 unsigned int total_length;
628 unsigned short version;
629 unsigned int header_length;
630 unsigned char minimum_instruction_length;
631 unsigned char maximum_ops_per_instruction;
632 unsigned char default_is_stmt;
633 int line_base;
634 unsigned char line_range;
635 unsigned char opcode_base;
636
637 /* standard_opcode_lengths[i] is the number of operands for the
638 standard opcode whose value is i. This means that
639 standard_opcode_lengths[0] is unused, and the last meaningful
640 element is standard_opcode_lengths[opcode_base - 1]. */
641 unsigned char *standard_opcode_lengths;
642
643 /* The include_directories table. NOTE! These strings are not
644 allocated with xmalloc; instead, they are pointers into
645 debug_line_buffer. If you try to free them, `free' will get
646 indigestion. */
647 unsigned int num_include_dirs, include_dirs_size;
648 char **include_dirs;
649
650 /* The file_names table. NOTE! These strings are not allocated
651 with xmalloc; instead, they are pointers into debug_line_buffer.
652 Don't try to free them directly. */
653 unsigned int num_file_names, file_names_size;
654 struct file_entry
655 {
656 char *name;
657 unsigned int dir_index;
658 unsigned int mod_time;
659 unsigned int length;
660 int included_p; /* Non-zero if referenced by the Line Number Program. */
661 struct symtab *symtab; /* The associated symbol table, if any. */
662 } *file_names;
663
664 /* The start and end of the statement program following this
665 header. These point into dwarf2_per_objfile->line_buffer. */
666 gdb_byte *statement_program_start, *statement_program_end;
667 };
668
669 /* When we construct a partial symbol table entry we only
670 need this much information. */
671 struct partial_die_info
672 {
673 /* Offset of this DIE. */
674 sect_offset offset;
675
676 /* DWARF-2 tag for this DIE. */
677 ENUM_BITFIELD(dwarf_tag) tag : 16;
678
679 /* Assorted flags describing the data found in this DIE. */
680 unsigned int has_children : 1;
681 unsigned int is_external : 1;
682 unsigned int is_declaration : 1;
683 unsigned int has_type : 1;
684 unsigned int has_specification : 1;
685 unsigned int has_pc_info : 1;
686 unsigned int may_be_inlined : 1;
687
688 /* Flag set if the SCOPE field of this structure has been
689 computed. */
690 unsigned int scope_set : 1;
691
692 /* Flag set if the DIE has a byte_size attribute. */
693 unsigned int has_byte_size : 1;
694
695 /* Flag set if any of the DIE's children are template arguments. */
696 unsigned int has_template_arguments : 1;
697
698 /* Flag set if fixup_partial_die has been called on this die. */
699 unsigned int fixup_called : 1;
700
701 /* The name of this DIE. Normally the value of DW_AT_name, but
702 sometimes a default name for unnamed DIEs. */
703 char *name;
704
705 /* The linkage name, if present. */
706 const char *linkage_name;
707
708 /* The scope to prepend to our children. This is generally
709 allocated on the comp_unit_obstack, so will disappear
710 when this compilation unit leaves the cache. */
711 char *scope;
712
713 /* Some data associated with the partial DIE. The tag determines
714 which field is live. */
715 union
716 {
717 /* The location description associated with this DIE, if any. */
718 struct dwarf_block *locdesc;
719 /* The offset of an import, for DW_TAG_imported_unit. */
720 sect_offset offset;
721 } d;
722
723 /* If HAS_PC_INFO, the PC range associated with this DIE. */
724 CORE_ADDR lowpc;
725 CORE_ADDR highpc;
726
727 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
728 DW_AT_sibling, if any. */
729 /* NOTE: This member isn't strictly necessary, read_partial_die could
730 return DW_AT_sibling values to its caller load_partial_dies. */
731 gdb_byte *sibling;
732
733 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
734 DW_AT_specification (or DW_AT_abstract_origin or
735 DW_AT_extension). */
736 sect_offset spec_offset;
737
738 /* Pointers to this DIE's parent, first child, and next sibling,
739 if any. */
740 struct partial_die_info *die_parent, *die_child, *die_sibling;
741 };
742
743 /* This data structure holds the information of an abbrev. */
744 struct abbrev_info
745 {
746 unsigned int number; /* number identifying abbrev */
747 enum dwarf_tag tag; /* dwarf tag */
748 unsigned short has_children; /* boolean */
749 unsigned short num_attrs; /* number of attributes */
750 struct attr_abbrev *attrs; /* an array of attribute descriptions */
751 struct abbrev_info *next; /* next in chain */
752 };
753
754 struct attr_abbrev
755 {
756 ENUM_BITFIELD(dwarf_attribute) name : 16;
757 ENUM_BITFIELD(dwarf_form) form : 16;
758 };
759
760 /* Attributes have a name and a value. */
761 struct attribute
762 {
763 ENUM_BITFIELD(dwarf_attribute) name : 16;
764 ENUM_BITFIELD(dwarf_form) form : 15;
765
766 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
767 field should be in u.str (existing only for DW_STRING) but it is kept
768 here for better struct attribute alignment. */
769 unsigned int string_is_canonical : 1;
770
771 union
772 {
773 char *str;
774 struct dwarf_block *blk;
775 ULONGEST unsnd;
776 LONGEST snd;
777 CORE_ADDR addr;
778 struct signatured_type *signatured_type;
779 }
780 u;
781 };
782
783 /* This data structure holds a complete die structure. */
784 struct die_info
785 {
786 /* DWARF-2 tag for this DIE. */
787 ENUM_BITFIELD(dwarf_tag) tag : 16;
788
789 /* Number of attributes */
790 unsigned char num_attrs;
791
792 /* True if we're presently building the full type name for the
793 type derived from this DIE. */
794 unsigned char building_fullname : 1;
795
796 /* Abbrev number */
797 unsigned int abbrev;
798
799 /* Offset in .debug_info or .debug_types section. */
800 sect_offset offset;
801
802 /* The dies in a compilation unit form an n-ary tree. PARENT
803 points to this die's parent; CHILD points to the first child of
804 this node; and all the children of a given node are chained
805 together via their SIBLING fields. */
806 struct die_info *child; /* Its first child, if any. */
807 struct die_info *sibling; /* Its next sibling, if any. */
808 struct die_info *parent; /* Its parent, if any. */
809
810 /* An array of attributes, with NUM_ATTRS elements. There may be
811 zero, but it's not common and zero-sized arrays are not
812 sufficiently portable C. */
813 struct attribute attrs[1];
814 };
815
816 /* Get at parts of an attribute structure. */
817
818 #define DW_STRING(attr) ((attr)->u.str)
819 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
820 #define DW_UNSND(attr) ((attr)->u.unsnd)
821 #define DW_BLOCK(attr) ((attr)->u.blk)
822 #define DW_SND(attr) ((attr)->u.snd)
823 #define DW_ADDR(attr) ((attr)->u.addr)
824 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
825
826 /* Blocks are a bunch of untyped bytes. */
827 struct dwarf_block
828 {
829 unsigned int size;
830
831 /* Valid only if SIZE is not zero. */
832 gdb_byte *data;
833 };
834
835 #ifndef ATTR_ALLOC_CHUNK
836 #define ATTR_ALLOC_CHUNK 4
837 #endif
838
839 /* Allocate fields for structs, unions and enums in this size. */
840 #ifndef DW_FIELD_ALLOC_CHUNK
841 #define DW_FIELD_ALLOC_CHUNK 4
842 #endif
843
844 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
845 but this would require a corresponding change in unpack_field_as_long
846 and friends. */
847 static int bits_per_byte = 8;
848
849 /* The routines that read and process dies for a C struct or C++ class
850 pass lists of data member fields and lists of member function fields
851 in an instance of a field_info structure, as defined below. */
852 struct field_info
853 {
854 /* List of data member and baseclasses fields. */
855 struct nextfield
856 {
857 struct nextfield *next;
858 int accessibility;
859 int virtuality;
860 struct field field;
861 }
862 *fields, *baseclasses;
863
864 /* Number of fields (including baseclasses). */
865 int nfields;
866
867 /* Number of baseclasses. */
868 int nbaseclasses;
869
870 /* Set if the accesibility of one of the fields is not public. */
871 int non_public_fields;
872
873 /* Member function fields array, entries are allocated in the order they
874 are encountered in the object file. */
875 struct nextfnfield
876 {
877 struct nextfnfield *next;
878 struct fn_field fnfield;
879 }
880 *fnfields;
881
882 /* Member function fieldlist array, contains name of possibly overloaded
883 member function, number of overloaded member functions and a pointer
884 to the head of the member function field chain. */
885 struct fnfieldlist
886 {
887 char *name;
888 int length;
889 struct nextfnfield *head;
890 }
891 *fnfieldlists;
892
893 /* Number of entries in the fnfieldlists array. */
894 int nfnfields;
895
896 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
897 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
898 struct typedef_field_list
899 {
900 struct typedef_field field;
901 struct typedef_field_list *next;
902 }
903 *typedef_field_list;
904 unsigned typedef_field_list_count;
905 };
906
907 /* One item on the queue of compilation units to read in full symbols
908 for. */
909 struct dwarf2_queue_item
910 {
911 struct dwarf2_per_cu_data *per_cu;
912 enum language pretend_language;
913 struct dwarf2_queue_item *next;
914 };
915
916 /* The current queue. */
917 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
918
919 /* Loaded secondary compilation units are kept in memory until they
920 have not been referenced for the processing of this many
921 compilation units. Set this to zero to disable caching. Cache
922 sizes of up to at least twenty will improve startup time for
923 typical inter-CU-reference binaries, at an obvious memory cost. */
924 static int dwarf2_max_cache_age = 5;
925 static void
926 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
927 struct cmd_list_element *c, const char *value)
928 {
929 fprintf_filtered (file, _("The upper bound on the age of cached "
930 "dwarf2 compilation units is %s.\n"),
931 value);
932 }
933
934
935 /* Various complaints about symbol reading that don't abort the process. */
936
937 static void
938 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
939 {
940 complaint (&symfile_complaints,
941 _("statement list doesn't fit in .debug_line section"));
942 }
943
944 static void
945 dwarf2_debug_line_missing_file_complaint (void)
946 {
947 complaint (&symfile_complaints,
948 _(".debug_line section has line data without a file"));
949 }
950
951 static void
952 dwarf2_debug_line_missing_end_sequence_complaint (void)
953 {
954 complaint (&symfile_complaints,
955 _(".debug_line section has line "
956 "program sequence without an end"));
957 }
958
959 static void
960 dwarf2_complex_location_expr_complaint (void)
961 {
962 complaint (&symfile_complaints, _("location expression too complex"));
963 }
964
965 static void
966 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
967 int arg3)
968 {
969 complaint (&symfile_complaints,
970 _("const value length mismatch for '%s', got %d, expected %d"),
971 arg1, arg2, arg3);
972 }
973
974 static void
975 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
976 {
977 complaint (&symfile_complaints,
978 _("debug info runs off end of %s section"
979 " [in module %s]"),
980 section->asection->name,
981 bfd_get_filename (section->asection->owner));
982 }
983
984 static void
985 dwarf2_macro_malformed_definition_complaint (const char *arg1)
986 {
987 complaint (&symfile_complaints,
988 _("macro debug info contains a "
989 "malformed macro definition:\n`%s'"),
990 arg1);
991 }
992
993 static void
994 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
995 {
996 complaint (&symfile_complaints,
997 _("invalid attribute class or form for '%s' in '%s'"),
998 arg1, arg2);
999 }
1000
1001 /* local function prototypes */
1002
1003 static void dwarf2_locate_sections (bfd *, asection *, void *);
1004
1005 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1006 struct objfile *);
1007
1008 static void dwarf2_find_base_address (struct die_info *die,
1009 struct dwarf2_cu *cu);
1010
1011 static void dwarf2_build_psymtabs_hard (struct objfile *);
1012
1013 static void scan_partial_symbols (struct partial_die_info *,
1014 CORE_ADDR *, CORE_ADDR *,
1015 int, struct dwarf2_cu *);
1016
1017 static void add_partial_symbol (struct partial_die_info *,
1018 struct dwarf2_cu *);
1019
1020 static void add_partial_namespace (struct partial_die_info *pdi,
1021 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1022 int need_pc, struct dwarf2_cu *cu);
1023
1024 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1025 CORE_ADDR *highpc, int need_pc,
1026 struct dwarf2_cu *cu);
1027
1028 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1029 struct dwarf2_cu *cu);
1030
1031 static void add_partial_subprogram (struct partial_die_info *pdi,
1032 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1033 int need_pc, struct dwarf2_cu *cu);
1034
1035 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1036
1037 static void psymtab_to_symtab_1 (struct partial_symtab *);
1038
1039 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1040 struct dwarf2_section_info *);
1041
1042 static void dwarf2_free_abbrev_table (void *);
1043
1044 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1045
1046 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
1047 struct dwarf2_cu *);
1048
1049 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
1050 struct dwarf2_cu *);
1051
1052 static struct partial_die_info *load_partial_dies
1053 (const struct die_reader_specs *, gdb_byte *, int);
1054
1055 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1056 struct partial_die_info *,
1057 struct abbrev_info *,
1058 unsigned int,
1059 gdb_byte *);
1060
1061 static struct partial_die_info *find_partial_die (sect_offset,
1062 struct dwarf2_cu *);
1063
1064 static void fixup_partial_die (struct partial_die_info *,
1065 struct dwarf2_cu *);
1066
1067 static gdb_byte *read_attribute (const struct die_reader_specs *,
1068 struct attribute *, struct attr_abbrev *,
1069 gdb_byte *);
1070
1071 static unsigned int read_1_byte (bfd *, gdb_byte *);
1072
1073 static int read_1_signed_byte (bfd *, gdb_byte *);
1074
1075 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1076
1077 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1078
1079 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1080
1081 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1082 unsigned int *);
1083
1084 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1085
1086 static LONGEST read_checked_initial_length_and_offset
1087 (bfd *, gdb_byte *, const struct comp_unit_head *,
1088 unsigned int *, unsigned int *);
1089
1090 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1091 unsigned int *);
1092
1093 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1094
1095 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1096
1097 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1098
1099 static char *read_indirect_string (bfd *, gdb_byte *,
1100 const struct comp_unit_head *,
1101 unsigned int *);
1102
1103 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1104
1105 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1106
1107 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1108 unsigned int *);
1109
1110 static char *read_str_index (const struct die_reader_specs *reader,
1111 struct dwarf2_cu *cu, ULONGEST str_index);
1112
1113 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1114
1115 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1116 struct dwarf2_cu *);
1117
1118 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1119 unsigned int,
1120 struct dwarf2_cu *);
1121
1122 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1123 struct dwarf2_cu *cu);
1124
1125 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1126
1127 static struct die_info *die_specification (struct die_info *die,
1128 struct dwarf2_cu **);
1129
1130 static void free_line_header (struct line_header *lh);
1131
1132 static void add_file_name (struct line_header *, char *, unsigned int,
1133 unsigned int, unsigned int);
1134
1135 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1136 struct dwarf2_cu *cu);
1137
1138 static void dwarf_decode_lines (struct line_header *, const char *,
1139 struct dwarf2_cu *, struct partial_symtab *,
1140 int);
1141
1142 static void dwarf2_start_subfile (char *, const char *, const char *);
1143
1144 static struct symbol *new_symbol (struct die_info *, struct type *,
1145 struct dwarf2_cu *);
1146
1147 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1148 struct dwarf2_cu *, struct symbol *);
1149
1150 static void dwarf2_const_value (struct attribute *, struct symbol *,
1151 struct dwarf2_cu *);
1152
1153 static void dwarf2_const_value_attr (struct attribute *attr,
1154 struct type *type,
1155 const char *name,
1156 struct obstack *obstack,
1157 struct dwarf2_cu *cu, LONGEST *value,
1158 gdb_byte **bytes,
1159 struct dwarf2_locexpr_baton **baton);
1160
1161 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1162
1163 static int need_gnat_info (struct dwarf2_cu *);
1164
1165 static struct type *die_descriptive_type (struct die_info *,
1166 struct dwarf2_cu *);
1167
1168 static void set_descriptive_type (struct type *, struct die_info *,
1169 struct dwarf2_cu *);
1170
1171 static struct type *die_containing_type (struct die_info *,
1172 struct dwarf2_cu *);
1173
1174 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1175 struct dwarf2_cu *);
1176
1177 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1178
1179 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1180
1181 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1182
1183 static char *typename_concat (struct obstack *obs, const char *prefix,
1184 const char *suffix, int physname,
1185 struct dwarf2_cu *cu);
1186
1187 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1188
1189 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1190
1191 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1192
1193 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1194
1195 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1196
1197 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1198 struct dwarf2_cu *, struct partial_symtab *);
1199
1200 static int dwarf2_get_pc_bounds (struct die_info *,
1201 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1202 struct partial_symtab *);
1203
1204 static void get_scope_pc_bounds (struct die_info *,
1205 CORE_ADDR *, CORE_ADDR *,
1206 struct dwarf2_cu *);
1207
1208 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1209 CORE_ADDR, struct dwarf2_cu *);
1210
1211 static void dwarf2_add_field (struct field_info *, struct die_info *,
1212 struct dwarf2_cu *);
1213
1214 static void dwarf2_attach_fields_to_type (struct field_info *,
1215 struct type *, struct dwarf2_cu *);
1216
1217 static void dwarf2_add_member_fn (struct field_info *,
1218 struct die_info *, struct type *,
1219 struct dwarf2_cu *);
1220
1221 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1222 struct type *,
1223 struct dwarf2_cu *);
1224
1225 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1226
1227 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1228
1229 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1230
1231 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1232
1233 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1234
1235 static struct type *read_module_type (struct die_info *die,
1236 struct dwarf2_cu *cu);
1237
1238 static const char *namespace_name (struct die_info *die,
1239 int *is_anonymous, struct dwarf2_cu *);
1240
1241 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1242
1243 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1244
1245 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1246 struct dwarf2_cu *);
1247
1248 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1249 gdb_byte *info_ptr,
1250 gdb_byte **new_info_ptr,
1251 struct die_info *parent);
1252
1253 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1254 gdb_byte *info_ptr,
1255 gdb_byte **new_info_ptr,
1256 struct die_info *parent);
1257
1258 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1259 struct die_info **, gdb_byte *, int *, int);
1260
1261 static gdb_byte *read_full_die (const struct die_reader_specs *,
1262 struct die_info **, gdb_byte *, int *);
1263
1264 static void process_die (struct die_info *, struct dwarf2_cu *);
1265
1266 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1267 struct obstack *);
1268
1269 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1270
1271 static const char *dwarf2_full_name (char *name,
1272 struct die_info *die,
1273 struct dwarf2_cu *cu);
1274
1275 static struct die_info *dwarf2_extension (struct die_info *die,
1276 struct dwarf2_cu **);
1277
1278 static const char *dwarf_tag_name (unsigned int);
1279
1280 static const char *dwarf_attr_name (unsigned int);
1281
1282 static const char *dwarf_form_name (unsigned int);
1283
1284 static char *dwarf_bool_name (unsigned int);
1285
1286 static const char *dwarf_type_encoding_name (unsigned int);
1287
1288 static struct die_info *sibling_die (struct die_info *);
1289
1290 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1291
1292 static void dump_die_for_error (struct die_info *);
1293
1294 static void dump_die_1 (struct ui_file *, int level, int max_level,
1295 struct die_info *);
1296
1297 /*static*/ void dump_die (struct die_info *, int max_level);
1298
1299 static void store_in_ref_table (struct die_info *,
1300 struct dwarf2_cu *);
1301
1302 static int is_ref_attr (struct attribute *);
1303
1304 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1305
1306 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1307
1308 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1309 struct attribute *,
1310 struct dwarf2_cu **);
1311
1312 static struct die_info *follow_die_ref (struct die_info *,
1313 struct attribute *,
1314 struct dwarf2_cu **);
1315
1316 static struct die_info *follow_die_sig (struct die_info *,
1317 struct attribute *,
1318 struct dwarf2_cu **);
1319
1320 static struct signatured_type *lookup_signatured_type_at_offset
1321 (struct objfile *objfile,
1322 struct dwarf2_section_info *section, sect_offset offset);
1323
1324 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1325
1326 static void read_signatured_type (struct signatured_type *);
1327
1328 /* memory allocation interface */
1329
1330 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1331
1332 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1333
1334 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1335
1336 static void dwarf_decode_macros (struct line_header *, unsigned int,
1337 char *, bfd *, struct dwarf2_cu *,
1338 struct dwarf2_section_info *,
1339 int, const char *);
1340
1341 static int attr_form_is_block (struct attribute *);
1342
1343 static int attr_form_is_section_offset (struct attribute *);
1344
1345 static int attr_form_is_constant (struct attribute *);
1346
1347 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1348 struct dwarf2_loclist_baton *baton,
1349 struct attribute *attr);
1350
1351 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1352 struct symbol *sym,
1353 struct dwarf2_cu *cu);
1354
1355 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1356 gdb_byte *info_ptr,
1357 struct abbrev_info *abbrev);
1358
1359 static void free_stack_comp_unit (void *);
1360
1361 static hashval_t partial_die_hash (const void *item);
1362
1363 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1364
1365 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1366 (sect_offset offset, struct objfile *objfile);
1367
1368 static void init_one_comp_unit (struct dwarf2_cu *cu,
1369 struct dwarf2_per_cu_data *per_cu);
1370
1371 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1372 struct die_info *comp_unit_die,
1373 enum language pretend_language);
1374
1375 static void free_heap_comp_unit (void *);
1376
1377 static void free_cached_comp_units (void *);
1378
1379 static void age_cached_comp_units (void);
1380
1381 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1382
1383 static struct type *set_die_type (struct die_info *, struct type *,
1384 struct dwarf2_cu *);
1385
1386 static void create_all_comp_units (struct objfile *);
1387
1388 static int create_all_type_units (struct objfile *);
1389
1390 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1391 enum language);
1392
1393 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1394 enum language);
1395
1396 static void dwarf2_add_dependence (struct dwarf2_cu *,
1397 struct dwarf2_per_cu_data *);
1398
1399 static void dwarf2_mark (struct dwarf2_cu *);
1400
1401 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1402
1403 static struct type *get_die_type_at_offset (sect_offset,
1404 struct dwarf2_per_cu_data *per_cu);
1405
1406 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1407
1408 static void dwarf2_release_queue (void *dummy);
1409
1410 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1411 enum language pretend_language);
1412
1413 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1414 struct dwarf2_per_cu_data *per_cu,
1415 enum language pretend_language);
1416
1417 static void process_queue (void);
1418
1419 static void find_file_and_directory (struct die_info *die,
1420 struct dwarf2_cu *cu,
1421 char **name, char **comp_dir);
1422
1423 static char *file_full_name (int file, struct line_header *lh,
1424 const char *comp_dir);
1425
1426 static gdb_byte *read_and_check_comp_unit_head
1427 (struct comp_unit_head *header,
1428 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1429 int is_debug_types_section);
1430
1431 static void init_cutu_and_read_dies
1432 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
1433 die_reader_func_ftype *die_reader_func, void *data);
1434
1435 static void init_cutu_and_read_dies_simple
1436 (struct dwarf2_per_cu_data *this_cu,
1437 die_reader_func_ftype *die_reader_func, void *data);
1438
1439 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1440
1441 static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
1442
1443 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1444
1445 static struct dwo_unit *lookup_dwo_comp_unit
1446 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1447
1448 static struct dwo_unit *lookup_dwo_type_unit
1449 (struct signatured_type *, char *, const char *);
1450
1451 static void free_dwo_file_cleanup (void *);
1452
1453 static void munmap_section_buffer (struct dwarf2_section_info *);
1454
1455 static void process_cu_includes (void);
1456
1457 #if WORDS_BIGENDIAN
1458
1459 /* Convert VALUE between big- and little-endian. */
1460 static offset_type
1461 byte_swap (offset_type value)
1462 {
1463 offset_type result;
1464
1465 result = (value & 0xff) << 24;
1466 result |= (value & 0xff00) << 8;
1467 result |= (value & 0xff0000) >> 8;
1468 result |= (value & 0xff000000) >> 24;
1469 return result;
1470 }
1471
1472 #define MAYBE_SWAP(V) byte_swap (V)
1473
1474 #else
1475 #define MAYBE_SWAP(V) (V)
1476 #endif /* WORDS_BIGENDIAN */
1477
1478 /* The suffix for an index file. */
1479 #define INDEX_SUFFIX ".gdb-index"
1480
1481 static const char *dwarf2_physname (char *name, struct die_info *die,
1482 struct dwarf2_cu *cu);
1483
1484 /* Try to locate the sections we need for DWARF 2 debugging
1485 information and return true if we have enough to do something.
1486 NAMES points to the dwarf2 section names, or is NULL if the standard
1487 ELF names are used. */
1488
1489 int
1490 dwarf2_has_info (struct objfile *objfile,
1491 const struct dwarf2_debug_sections *names)
1492 {
1493 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1494 if (!dwarf2_per_objfile)
1495 {
1496 /* Initialize per-objfile state. */
1497 struct dwarf2_per_objfile *data
1498 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1499
1500 memset (data, 0, sizeof (*data));
1501 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1502 dwarf2_per_objfile = data;
1503
1504 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1505 (void *) names);
1506 dwarf2_per_objfile->objfile = objfile;
1507 }
1508 return (dwarf2_per_objfile->info.asection != NULL
1509 && dwarf2_per_objfile->abbrev.asection != NULL);
1510 }
1511
1512 /* When loading sections, we look either for uncompressed section or for
1513 compressed section names. */
1514
1515 static int
1516 section_is_p (const char *section_name,
1517 const struct dwarf2_section_names *names)
1518 {
1519 if (names->normal != NULL
1520 && strcmp (section_name, names->normal) == 0)
1521 return 1;
1522 if (names->compressed != NULL
1523 && strcmp (section_name, names->compressed) == 0)
1524 return 1;
1525 return 0;
1526 }
1527
1528 /* This function is mapped across the sections and remembers the
1529 offset and size of each of the debugging sections we are interested
1530 in. */
1531
1532 static void
1533 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1534 {
1535 const struct dwarf2_debug_sections *names;
1536
1537 if (vnames == NULL)
1538 names = &dwarf2_elf_names;
1539 else
1540 names = (const struct dwarf2_debug_sections *) vnames;
1541
1542 if (section_is_p (sectp->name, &names->info))
1543 {
1544 dwarf2_per_objfile->info.asection = sectp;
1545 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1546 }
1547 else if (section_is_p (sectp->name, &names->abbrev))
1548 {
1549 dwarf2_per_objfile->abbrev.asection = sectp;
1550 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1551 }
1552 else if (section_is_p (sectp->name, &names->line))
1553 {
1554 dwarf2_per_objfile->line.asection = sectp;
1555 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1556 }
1557 else if (section_is_p (sectp->name, &names->loc))
1558 {
1559 dwarf2_per_objfile->loc.asection = sectp;
1560 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1561 }
1562 else if (section_is_p (sectp->name, &names->macinfo))
1563 {
1564 dwarf2_per_objfile->macinfo.asection = sectp;
1565 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1566 }
1567 else if (section_is_p (sectp->name, &names->macro))
1568 {
1569 dwarf2_per_objfile->macro.asection = sectp;
1570 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1571 }
1572 else if (section_is_p (sectp->name, &names->str))
1573 {
1574 dwarf2_per_objfile->str.asection = sectp;
1575 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1576 }
1577 else if (section_is_p (sectp->name, &names->addr))
1578 {
1579 dwarf2_per_objfile->addr.asection = sectp;
1580 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1581 }
1582 else if (section_is_p (sectp->name, &names->frame))
1583 {
1584 dwarf2_per_objfile->frame.asection = sectp;
1585 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1586 }
1587 else if (section_is_p (sectp->name, &names->eh_frame))
1588 {
1589 flagword aflag = bfd_get_section_flags (abfd, sectp);
1590
1591 if (aflag & SEC_HAS_CONTENTS)
1592 {
1593 dwarf2_per_objfile->eh_frame.asection = sectp;
1594 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1595 }
1596 }
1597 else if (section_is_p (sectp->name, &names->ranges))
1598 {
1599 dwarf2_per_objfile->ranges.asection = sectp;
1600 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1601 }
1602 else if (section_is_p (sectp->name, &names->types))
1603 {
1604 struct dwarf2_section_info type_section;
1605
1606 memset (&type_section, 0, sizeof (type_section));
1607 type_section.asection = sectp;
1608 type_section.size = bfd_get_section_size (sectp);
1609
1610 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1611 &type_section);
1612 }
1613 else if (section_is_p (sectp->name, &names->gdb_index))
1614 {
1615 dwarf2_per_objfile->gdb_index.asection = sectp;
1616 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1617 }
1618
1619 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1620 && bfd_section_vma (abfd, sectp) == 0)
1621 dwarf2_per_objfile->has_section_at_zero = 1;
1622 }
1623
1624 /* Decompress a section that was compressed using zlib. Store the
1625 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1626
1627 static void
1628 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1629 gdb_byte **outbuf, bfd_size_type *outsize)
1630 {
1631 bfd *abfd = sectp->owner;
1632 #ifndef HAVE_ZLIB_H
1633 error (_("Support for zlib-compressed DWARF data (from '%s') "
1634 "is disabled in this copy of GDB"),
1635 bfd_get_filename (abfd));
1636 #else
1637 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1638 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1639 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1640 bfd_size_type uncompressed_size;
1641 gdb_byte *uncompressed_buffer;
1642 z_stream strm;
1643 int rc;
1644 int header_size = 12;
1645
1646 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1647 || bfd_bread (compressed_buffer,
1648 compressed_size, abfd) != compressed_size)
1649 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1650 bfd_get_filename (abfd));
1651
1652 /* Read the zlib header. In this case, it should be "ZLIB" followed
1653 by the uncompressed section size, 8 bytes in big-endian order. */
1654 if (compressed_size < header_size
1655 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1656 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1657 bfd_get_filename (abfd));
1658 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1659 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1660 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1661 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1662 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1663 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1664 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1665 uncompressed_size += compressed_buffer[11];
1666
1667 /* It is possible the section consists of several compressed
1668 buffers concatenated together, so we uncompress in a loop. */
1669 strm.zalloc = NULL;
1670 strm.zfree = NULL;
1671 strm.opaque = NULL;
1672 strm.avail_in = compressed_size - header_size;
1673 strm.next_in = (Bytef*) compressed_buffer + header_size;
1674 strm.avail_out = uncompressed_size;
1675 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1676 uncompressed_size);
1677 rc = inflateInit (&strm);
1678 while (strm.avail_in > 0)
1679 {
1680 if (rc != Z_OK)
1681 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1682 bfd_get_filename (abfd), rc);
1683 strm.next_out = ((Bytef*) uncompressed_buffer
1684 + (uncompressed_size - strm.avail_out));
1685 rc = inflate (&strm, Z_FINISH);
1686 if (rc != Z_STREAM_END)
1687 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1688 bfd_get_filename (abfd), rc);
1689 rc = inflateReset (&strm);
1690 }
1691 rc = inflateEnd (&strm);
1692 if (rc != Z_OK
1693 || strm.avail_out != 0)
1694 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1695 bfd_get_filename (abfd), rc);
1696
1697 do_cleanups (cleanup);
1698 *outbuf = uncompressed_buffer;
1699 *outsize = uncompressed_size;
1700 #endif
1701 }
1702
1703 /* A helper function that decides whether a section is empty,
1704 or not present. */
1705
1706 static int
1707 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1708 {
1709 return info->asection == NULL || info->size == 0;
1710 }
1711
1712 /* Read the contents of the section INFO.
1713 OBJFILE is the main object file, but not necessarily the file where
1714 the section comes from. E.g., for DWO files INFO->asection->owner
1715 is the bfd of the DWO file.
1716 If the section is compressed, uncompress it before returning. */
1717
1718 static void
1719 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1720 {
1721 asection *sectp = info->asection;
1722 bfd *abfd;
1723 gdb_byte *buf, *retbuf;
1724 unsigned char header[4];
1725
1726 if (info->readin)
1727 return;
1728 info->buffer = NULL;
1729 info->map_addr = NULL;
1730 info->readin = 1;
1731
1732 if (dwarf2_section_empty_p (info))
1733 return;
1734
1735 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1736 abfd = sectp->owner;
1737
1738 /* Check if the file has a 4-byte header indicating compression. */
1739 if (info->size > sizeof (header)
1740 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1741 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1742 {
1743 /* Upon decompression, update the buffer and its size. */
1744 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1745 {
1746 zlib_decompress_section (objfile, sectp, &info->buffer,
1747 &info->size);
1748 return;
1749 }
1750 }
1751
1752 #ifdef HAVE_MMAP
1753 if (pagesize == 0)
1754 pagesize = getpagesize ();
1755
1756 /* Only try to mmap sections which are large enough: we don't want to
1757 waste space due to fragmentation. Also, only try mmap for sections
1758 without relocations. */
1759
1760 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1761 {
1762 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1763 MAP_PRIVATE, sectp->filepos,
1764 &info->map_addr, &info->map_len);
1765
1766 if ((caddr_t)info->buffer != MAP_FAILED)
1767 {
1768 #if HAVE_POSIX_MADVISE
1769 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1770 #endif
1771 return;
1772 }
1773 }
1774 #endif
1775
1776 /* If we get here, we are a normal, not-compressed section. */
1777 info->buffer = buf
1778 = obstack_alloc (&objfile->objfile_obstack, info->size);
1779
1780 /* When debugging .o files, we may need to apply relocations; see
1781 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1782 We never compress sections in .o files, so we only need to
1783 try this when the section is not compressed. */
1784 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1785 if (retbuf != NULL)
1786 {
1787 info->buffer = retbuf;
1788 return;
1789 }
1790
1791 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1792 || bfd_bread (buf, info->size, abfd) != info->size)
1793 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1794 bfd_get_filename (abfd));
1795 }
1796
1797 /* A helper function that returns the size of a section in a safe way.
1798 If you are positive that the section has been read before using the
1799 size, then it is safe to refer to the dwarf2_section_info object's
1800 "size" field directly. In other cases, you must call this
1801 function, because for compressed sections the size field is not set
1802 correctly until the section has been read. */
1803
1804 static bfd_size_type
1805 dwarf2_section_size (struct objfile *objfile,
1806 struct dwarf2_section_info *info)
1807 {
1808 if (!info->readin)
1809 dwarf2_read_section (objfile, info);
1810 return info->size;
1811 }
1812
1813 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1814 SECTION_NAME. */
1815
1816 void
1817 dwarf2_get_section_info (struct objfile *objfile,
1818 enum dwarf2_section_enum sect,
1819 asection **sectp, gdb_byte **bufp,
1820 bfd_size_type *sizep)
1821 {
1822 struct dwarf2_per_objfile *data
1823 = objfile_data (objfile, dwarf2_objfile_data_key);
1824 struct dwarf2_section_info *info;
1825
1826 /* We may see an objfile without any DWARF, in which case we just
1827 return nothing. */
1828 if (data == NULL)
1829 {
1830 *sectp = NULL;
1831 *bufp = NULL;
1832 *sizep = 0;
1833 return;
1834 }
1835 switch (sect)
1836 {
1837 case DWARF2_DEBUG_FRAME:
1838 info = &data->frame;
1839 break;
1840 case DWARF2_EH_FRAME:
1841 info = &data->eh_frame;
1842 break;
1843 default:
1844 gdb_assert_not_reached ("unexpected section");
1845 }
1846
1847 dwarf2_read_section (objfile, info);
1848
1849 *sectp = info->asection;
1850 *bufp = info->buffer;
1851 *sizep = info->size;
1852 }
1853
1854 \f
1855 /* DWARF quick_symbols_functions support. */
1856
1857 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1858 unique line tables, so we maintain a separate table of all .debug_line
1859 derived entries to support the sharing.
1860 All the quick functions need is the list of file names. We discard the
1861 line_header when we're done and don't need to record it here. */
1862 struct quick_file_names
1863 {
1864 /* The offset in .debug_line of the line table. We hash on this. */
1865 unsigned int offset;
1866
1867 /* The number of entries in file_names, real_names. */
1868 unsigned int num_file_names;
1869
1870 /* The file names from the line table, after being run through
1871 file_full_name. */
1872 const char **file_names;
1873
1874 /* The file names from the line table after being run through
1875 gdb_realpath. These are computed lazily. */
1876 const char **real_names;
1877 };
1878
1879 /* When using the index (and thus not using psymtabs), each CU has an
1880 object of this type. This is used to hold information needed by
1881 the various "quick" methods. */
1882 struct dwarf2_per_cu_quick_data
1883 {
1884 /* The file table. This can be NULL if there was no file table
1885 or it's currently not read in.
1886 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1887 struct quick_file_names *file_names;
1888
1889 /* The corresponding symbol table. This is NULL if symbols for this
1890 CU have not yet been read. */
1891 struct symtab *symtab;
1892
1893 /* A temporary mark bit used when iterating over all CUs in
1894 expand_symtabs_matching. */
1895 unsigned int mark : 1;
1896
1897 /* True if we've tried to read the file table and found there isn't one.
1898 There will be no point in trying to read it again next time. */
1899 unsigned int no_file_data : 1;
1900 };
1901
1902 /* Hash function for a quick_file_names. */
1903
1904 static hashval_t
1905 hash_file_name_entry (const void *e)
1906 {
1907 const struct quick_file_names *file_data = e;
1908
1909 return file_data->offset;
1910 }
1911
1912 /* Equality function for a quick_file_names. */
1913
1914 static int
1915 eq_file_name_entry (const void *a, const void *b)
1916 {
1917 const struct quick_file_names *ea = a;
1918 const struct quick_file_names *eb = b;
1919
1920 return ea->offset == eb->offset;
1921 }
1922
1923 /* Delete function for a quick_file_names. */
1924
1925 static void
1926 delete_file_name_entry (void *e)
1927 {
1928 struct quick_file_names *file_data = e;
1929 int i;
1930
1931 for (i = 0; i < file_data->num_file_names; ++i)
1932 {
1933 xfree ((void*) file_data->file_names[i]);
1934 if (file_data->real_names)
1935 xfree ((void*) file_data->real_names[i]);
1936 }
1937
1938 /* The space for the struct itself lives on objfile_obstack,
1939 so we don't free it here. */
1940 }
1941
1942 /* Create a quick_file_names hash table. */
1943
1944 static htab_t
1945 create_quick_file_names_table (unsigned int nr_initial_entries)
1946 {
1947 return htab_create_alloc (nr_initial_entries,
1948 hash_file_name_entry, eq_file_name_entry,
1949 delete_file_name_entry, xcalloc, xfree);
1950 }
1951
1952 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1953 have to be created afterwards. You should call age_cached_comp_units after
1954 processing PER_CU->CU. dw2_setup must have been already called. */
1955
1956 static void
1957 load_cu (struct dwarf2_per_cu_data *per_cu)
1958 {
1959 if (per_cu->is_debug_types)
1960 load_full_type_unit (per_cu);
1961 else
1962 load_full_comp_unit (per_cu, language_minimal);
1963
1964 gdb_assert (per_cu->cu != NULL);
1965
1966 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1967 }
1968
1969 /* Read in the symbols for PER_CU. */
1970
1971 static void
1972 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1973 {
1974 struct cleanup *back_to;
1975
1976 back_to = make_cleanup (dwarf2_release_queue, NULL);
1977
1978 if (dwarf2_per_objfile->using_index
1979 ? per_cu->v.quick->symtab == NULL
1980 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
1981 {
1982 queue_comp_unit (per_cu, language_minimal);
1983 load_cu (per_cu);
1984 }
1985
1986 process_queue ();
1987
1988 /* Age the cache, releasing compilation units that have not
1989 been used recently. */
1990 age_cached_comp_units ();
1991
1992 do_cleanups (back_to);
1993 }
1994
1995 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1996 the objfile from which this CU came. Returns the resulting symbol
1997 table. */
1998
1999 static struct symtab *
2000 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2001 {
2002 gdb_assert (dwarf2_per_objfile->using_index);
2003 if (!per_cu->v.quick->symtab)
2004 {
2005 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2006 increment_reading_symtab ();
2007 dw2_do_instantiate_symtab (per_cu);
2008 process_cu_includes ();
2009 do_cleanups (back_to);
2010 }
2011 return per_cu->v.quick->symtab;
2012 }
2013
2014 /* Return the CU given its index. */
2015
2016 static struct dwarf2_per_cu_data *
2017 dw2_get_cu (int index)
2018 {
2019 if (index >= dwarf2_per_objfile->n_comp_units)
2020 {
2021 index -= dwarf2_per_objfile->n_comp_units;
2022 return dwarf2_per_objfile->all_type_units[index];
2023 }
2024 return dwarf2_per_objfile->all_comp_units[index];
2025 }
2026
2027 /* A helper function that knows how to read a 64-bit value in a way
2028 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2029 otherwise. */
2030
2031 static int
2032 extract_cu_value (const char *bytes, ULONGEST *result)
2033 {
2034 if (sizeof (ULONGEST) < 8)
2035 {
2036 int i;
2037
2038 /* Ignore the upper 4 bytes if they are all zero. */
2039 for (i = 0; i < 4; ++i)
2040 if (bytes[i + 4] != 0)
2041 return 0;
2042
2043 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2044 }
2045 else
2046 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2047 return 1;
2048 }
2049
2050 /* Read the CU list from the mapped index, and use it to create all
2051 the CU objects for this objfile. Return 0 if something went wrong,
2052 1 if everything went ok. */
2053
2054 static int
2055 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2056 offset_type cu_list_elements)
2057 {
2058 offset_type i;
2059
2060 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2061 dwarf2_per_objfile->all_comp_units
2062 = obstack_alloc (&objfile->objfile_obstack,
2063 dwarf2_per_objfile->n_comp_units
2064 * sizeof (struct dwarf2_per_cu_data *));
2065
2066 for (i = 0; i < cu_list_elements; i += 2)
2067 {
2068 struct dwarf2_per_cu_data *the_cu;
2069 ULONGEST offset, length;
2070
2071 if (!extract_cu_value (cu_list, &offset)
2072 || !extract_cu_value (cu_list + 8, &length))
2073 return 0;
2074 cu_list += 2 * 8;
2075
2076 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2077 struct dwarf2_per_cu_data);
2078 the_cu->offset.sect_off = offset;
2079 the_cu->length = length;
2080 the_cu->objfile = objfile;
2081 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2082 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2083 struct dwarf2_per_cu_quick_data);
2084 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2085 }
2086
2087 return 1;
2088 }
2089
2090 /* Create the signatured type hash table from the index. */
2091
2092 static int
2093 create_signatured_type_table_from_index (struct objfile *objfile,
2094 struct dwarf2_section_info *section,
2095 const gdb_byte *bytes,
2096 offset_type elements)
2097 {
2098 offset_type i;
2099 htab_t sig_types_hash;
2100
2101 dwarf2_per_objfile->n_type_units = elements / 3;
2102 dwarf2_per_objfile->all_type_units
2103 = obstack_alloc (&objfile->objfile_obstack,
2104 dwarf2_per_objfile->n_type_units
2105 * sizeof (struct dwarf2_per_cu_data *));
2106
2107 sig_types_hash = allocate_signatured_type_table (objfile);
2108
2109 for (i = 0; i < elements; i += 3)
2110 {
2111 struct signatured_type *sig_type;
2112 ULONGEST offset, type_offset_in_tu, signature;
2113 void **slot;
2114
2115 if (!extract_cu_value (bytes, &offset)
2116 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2117 return 0;
2118 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2119 bytes += 3 * 8;
2120
2121 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2122 struct signatured_type);
2123 sig_type->signature = signature;
2124 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2125 sig_type->per_cu.is_debug_types = 1;
2126 sig_type->per_cu.info_or_types_section = section;
2127 sig_type->per_cu.offset.sect_off = offset;
2128 sig_type->per_cu.objfile = objfile;
2129 sig_type->per_cu.v.quick
2130 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2131 struct dwarf2_per_cu_quick_data);
2132
2133 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2134 *slot = sig_type;
2135
2136 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2137 }
2138
2139 dwarf2_per_objfile->signatured_types = sig_types_hash;
2140
2141 return 1;
2142 }
2143
2144 /* Read the address map data from the mapped index, and use it to
2145 populate the objfile's psymtabs_addrmap. */
2146
2147 static void
2148 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2149 {
2150 const gdb_byte *iter, *end;
2151 struct obstack temp_obstack;
2152 struct addrmap *mutable_map;
2153 struct cleanup *cleanup;
2154 CORE_ADDR baseaddr;
2155
2156 obstack_init (&temp_obstack);
2157 cleanup = make_cleanup_obstack_free (&temp_obstack);
2158 mutable_map = addrmap_create_mutable (&temp_obstack);
2159
2160 iter = index->address_table;
2161 end = iter + index->address_table_size;
2162
2163 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2164
2165 while (iter < end)
2166 {
2167 ULONGEST hi, lo, cu_index;
2168 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2169 iter += 8;
2170 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2171 iter += 8;
2172 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2173 iter += 4;
2174
2175 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2176 dw2_get_cu (cu_index));
2177 }
2178
2179 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2180 &objfile->objfile_obstack);
2181 do_cleanups (cleanup);
2182 }
2183
2184 /* The hash function for strings in the mapped index. This is the same as
2185 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2186 implementation. This is necessary because the hash function is tied to the
2187 format of the mapped index file. The hash values do not have to match with
2188 SYMBOL_HASH_NEXT.
2189
2190 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2191
2192 static hashval_t
2193 mapped_index_string_hash (int index_version, const void *p)
2194 {
2195 const unsigned char *str = (const unsigned char *) p;
2196 hashval_t r = 0;
2197 unsigned char c;
2198
2199 while ((c = *str++) != 0)
2200 {
2201 if (index_version >= 5)
2202 c = tolower (c);
2203 r = r * 67 + c - 113;
2204 }
2205
2206 return r;
2207 }
2208
2209 /* Find a slot in the mapped index INDEX for the object named NAME.
2210 If NAME is found, set *VEC_OUT to point to the CU vector in the
2211 constant pool and return 1. If NAME cannot be found, return 0. */
2212
2213 static int
2214 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2215 offset_type **vec_out)
2216 {
2217 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2218 offset_type hash;
2219 offset_type slot, step;
2220 int (*cmp) (const char *, const char *);
2221
2222 if (current_language->la_language == language_cplus
2223 || current_language->la_language == language_java
2224 || current_language->la_language == language_fortran)
2225 {
2226 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2227 not contain any. */
2228 const char *paren = strchr (name, '(');
2229
2230 if (paren)
2231 {
2232 char *dup;
2233
2234 dup = xmalloc (paren - name + 1);
2235 memcpy (dup, name, paren - name);
2236 dup[paren - name] = 0;
2237
2238 make_cleanup (xfree, dup);
2239 name = dup;
2240 }
2241 }
2242
2243 /* Index version 4 did not support case insensitive searches. But the
2244 indices for case insensitive languages are built in lowercase, therefore
2245 simulate our NAME being searched is also lowercased. */
2246 hash = mapped_index_string_hash ((index->version == 4
2247 && case_sensitivity == case_sensitive_off
2248 ? 5 : index->version),
2249 name);
2250
2251 slot = hash & (index->symbol_table_slots - 1);
2252 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2253 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2254
2255 for (;;)
2256 {
2257 /* Convert a slot number to an offset into the table. */
2258 offset_type i = 2 * slot;
2259 const char *str;
2260 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2261 {
2262 do_cleanups (back_to);
2263 return 0;
2264 }
2265
2266 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2267 if (!cmp (name, str))
2268 {
2269 *vec_out = (offset_type *) (index->constant_pool
2270 + MAYBE_SWAP (index->symbol_table[i + 1]));
2271 do_cleanups (back_to);
2272 return 1;
2273 }
2274
2275 slot = (slot + step) & (index->symbol_table_slots - 1);
2276 }
2277 }
2278
2279 /* Read the index file. If everything went ok, initialize the "quick"
2280 elements of all the CUs and return 1. Otherwise, return 0. */
2281
2282 static int
2283 dwarf2_read_index (struct objfile *objfile)
2284 {
2285 char *addr;
2286 struct mapped_index *map;
2287 offset_type *metadata;
2288 const gdb_byte *cu_list;
2289 const gdb_byte *types_list = NULL;
2290 offset_type version, cu_list_elements;
2291 offset_type types_list_elements = 0;
2292 int i;
2293
2294 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2295 return 0;
2296
2297 /* Older elfutils strip versions could keep the section in the main
2298 executable while splitting it for the separate debug info file. */
2299 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2300 & SEC_HAS_CONTENTS) == 0)
2301 return 0;
2302
2303 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2304
2305 addr = dwarf2_per_objfile->gdb_index.buffer;
2306 /* Version check. */
2307 version = MAYBE_SWAP (*(offset_type *) addr);
2308 /* Versions earlier than 3 emitted every copy of a psymbol. This
2309 causes the index to behave very poorly for certain requests. Version 3
2310 contained incomplete addrmap. So, it seems better to just ignore such
2311 indices. */
2312 if (version < 4)
2313 {
2314 static int warning_printed = 0;
2315 if (!warning_printed)
2316 {
2317 warning (_("Skipping obsolete .gdb_index section in %s."),
2318 objfile->name);
2319 warning_printed = 1;
2320 }
2321 return 0;
2322 }
2323 /* Index version 4 uses a different hash function than index version
2324 5 and later.
2325
2326 Versions earlier than 6 did not emit psymbols for inlined
2327 functions. Using these files will cause GDB not to be able to
2328 set breakpoints on inlined functions by name, so we ignore these
2329 indices unless the --use-deprecated-index-sections command line
2330 option was supplied. */
2331 if (version < 6 && !use_deprecated_index_sections)
2332 {
2333 static int warning_printed = 0;
2334 if (!warning_printed)
2335 {
2336 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2337 "--use-deprecated-index-sections to use them anyway"),
2338 objfile->name);
2339 warning_printed = 1;
2340 }
2341 return 0;
2342 }
2343 /* Indexes with higher version than the one supported by GDB may be no
2344 longer backward compatible. */
2345 if (version > 6)
2346 return 0;
2347
2348 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2349 map->version = version;
2350 map->total_size = dwarf2_per_objfile->gdb_index.size;
2351
2352 metadata = (offset_type *) (addr + sizeof (offset_type));
2353
2354 i = 0;
2355 cu_list = addr + MAYBE_SWAP (metadata[i]);
2356 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2357 / 8);
2358 ++i;
2359
2360 types_list = addr + MAYBE_SWAP (metadata[i]);
2361 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2362 - MAYBE_SWAP (metadata[i]))
2363 / 8);
2364 ++i;
2365
2366 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2367 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2368 - MAYBE_SWAP (metadata[i]));
2369 ++i;
2370
2371 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2372 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2373 - MAYBE_SWAP (metadata[i]))
2374 / (2 * sizeof (offset_type)));
2375 ++i;
2376
2377 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2378
2379 /* Don't use the index if it's empty. */
2380 if (map->symbol_table_slots == 0)
2381 return 0;
2382
2383 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2384 return 0;
2385
2386 if (types_list_elements)
2387 {
2388 struct dwarf2_section_info *section;
2389
2390 /* We can only handle a single .debug_types when we have an
2391 index. */
2392 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2393 return 0;
2394
2395 section = VEC_index (dwarf2_section_info_def,
2396 dwarf2_per_objfile->types, 0);
2397
2398 if (!create_signatured_type_table_from_index (objfile, section,
2399 types_list,
2400 types_list_elements))
2401 return 0;
2402 }
2403
2404 create_addrmap_from_index (objfile, map);
2405
2406 dwarf2_per_objfile->index_table = map;
2407 dwarf2_per_objfile->using_index = 1;
2408 dwarf2_per_objfile->quick_file_names_table =
2409 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2410
2411 return 1;
2412 }
2413
2414 /* A helper for the "quick" functions which sets the global
2415 dwarf2_per_objfile according to OBJFILE. */
2416
2417 static void
2418 dw2_setup (struct objfile *objfile)
2419 {
2420 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2421 gdb_assert (dwarf2_per_objfile);
2422 }
2423
2424 /* die_reader_func for dw2_get_file_names. */
2425
2426 static void
2427 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2428 gdb_byte *info_ptr,
2429 struct die_info *comp_unit_die,
2430 int has_children,
2431 void *data)
2432 {
2433 struct dwarf2_cu *cu = reader->cu;
2434 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2435 struct objfile *objfile = dwarf2_per_objfile->objfile;
2436 struct line_header *lh;
2437 struct attribute *attr;
2438 int i;
2439 char *name, *comp_dir;
2440 void **slot;
2441 struct quick_file_names *qfn;
2442 unsigned int line_offset;
2443
2444 lh = NULL;
2445 slot = NULL;
2446 line_offset = 0;
2447
2448 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2449 if (attr)
2450 {
2451 struct quick_file_names find_entry;
2452
2453 line_offset = DW_UNSND (attr);
2454
2455 /* We may have already read in this line header (TU line header sharing).
2456 If we have we're done. */
2457 find_entry.offset = line_offset;
2458 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2459 &find_entry, INSERT);
2460 if (*slot != NULL)
2461 {
2462 this_cu->v.quick->file_names = *slot;
2463 return;
2464 }
2465
2466 lh = dwarf_decode_line_header (line_offset, cu);
2467 }
2468 if (lh == NULL)
2469 {
2470 this_cu->v.quick->no_file_data = 1;
2471 return;
2472 }
2473
2474 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2475 qfn->offset = line_offset;
2476 gdb_assert (slot != NULL);
2477 *slot = qfn;
2478
2479 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2480
2481 qfn->num_file_names = lh->num_file_names;
2482 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2483 lh->num_file_names * sizeof (char *));
2484 for (i = 0; i < lh->num_file_names; ++i)
2485 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2486 qfn->real_names = NULL;
2487
2488 free_line_header (lh);
2489
2490 this_cu->v.quick->file_names = qfn;
2491 }
2492
2493 /* A helper for the "quick" functions which attempts to read the line
2494 table for THIS_CU. */
2495
2496 static struct quick_file_names *
2497 dw2_get_file_names (struct objfile *objfile,
2498 struct dwarf2_per_cu_data *this_cu)
2499 {
2500 if (this_cu->v.quick->file_names != NULL)
2501 return this_cu->v.quick->file_names;
2502 /* If we know there is no line data, no point in looking again. */
2503 if (this_cu->v.quick->no_file_data)
2504 return NULL;
2505
2506 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2507 in the stub for CUs, there's is no need to lookup the DWO file.
2508 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2509 DWO file. */
2510 if (this_cu->is_debug_types)
2511 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2512 else
2513 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2514
2515 if (this_cu->v.quick->no_file_data)
2516 return NULL;
2517 return this_cu->v.quick->file_names;
2518 }
2519
2520 /* A helper for the "quick" functions which computes and caches the
2521 real path for a given file name from the line table. */
2522
2523 static const char *
2524 dw2_get_real_path (struct objfile *objfile,
2525 struct quick_file_names *qfn, int index)
2526 {
2527 if (qfn->real_names == NULL)
2528 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2529 qfn->num_file_names, sizeof (char *));
2530
2531 if (qfn->real_names[index] == NULL)
2532 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2533
2534 return qfn->real_names[index];
2535 }
2536
2537 static struct symtab *
2538 dw2_find_last_source_symtab (struct objfile *objfile)
2539 {
2540 int index;
2541
2542 dw2_setup (objfile);
2543 index = dwarf2_per_objfile->n_comp_units - 1;
2544 return dw2_instantiate_symtab (dw2_get_cu (index));
2545 }
2546
2547 /* Traversal function for dw2_forget_cached_source_info. */
2548
2549 static int
2550 dw2_free_cached_file_names (void **slot, void *info)
2551 {
2552 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2553
2554 if (file_data->real_names)
2555 {
2556 int i;
2557
2558 for (i = 0; i < file_data->num_file_names; ++i)
2559 {
2560 xfree ((void*) file_data->real_names[i]);
2561 file_data->real_names[i] = NULL;
2562 }
2563 }
2564
2565 return 1;
2566 }
2567
2568 static void
2569 dw2_forget_cached_source_info (struct objfile *objfile)
2570 {
2571 dw2_setup (objfile);
2572
2573 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2574 dw2_free_cached_file_names, NULL);
2575 }
2576
2577 /* Helper function for dw2_map_symtabs_matching_filename that expands
2578 the symtabs and calls the iterator. */
2579
2580 static int
2581 dw2_map_expand_apply (struct objfile *objfile,
2582 struct dwarf2_per_cu_data *per_cu,
2583 const char *name,
2584 const char *full_path, const char *real_path,
2585 int (*callback) (struct symtab *, void *),
2586 void *data)
2587 {
2588 struct symtab *last_made = objfile->symtabs;
2589
2590 /* Don't visit already-expanded CUs. */
2591 if (per_cu->v.quick->symtab)
2592 return 0;
2593
2594 /* This may expand more than one symtab, and we want to iterate over
2595 all of them. */
2596 dw2_instantiate_symtab (per_cu);
2597
2598 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2599 objfile->symtabs, last_made);
2600 }
2601
2602 /* Implementation of the map_symtabs_matching_filename method. */
2603
2604 static int
2605 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2606 const char *full_path, const char *real_path,
2607 int (*callback) (struct symtab *, void *),
2608 void *data)
2609 {
2610 int i;
2611 const char *name_basename = lbasename (name);
2612 int name_len = strlen (name);
2613 int is_abs = IS_ABSOLUTE_PATH (name);
2614
2615 dw2_setup (objfile);
2616
2617 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2618 + dwarf2_per_objfile->n_type_units); ++i)
2619 {
2620 int j;
2621 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2622 struct quick_file_names *file_data;
2623
2624 /* We only need to look at symtabs not already expanded. */
2625 if (per_cu->v.quick->symtab)
2626 continue;
2627
2628 file_data = dw2_get_file_names (objfile, per_cu);
2629 if (file_data == NULL)
2630 continue;
2631
2632 for (j = 0; j < file_data->num_file_names; ++j)
2633 {
2634 const char *this_name = file_data->file_names[j];
2635
2636 if (FILENAME_CMP (name, this_name) == 0
2637 || (!is_abs && compare_filenames_for_search (this_name,
2638 name, name_len)))
2639 {
2640 if (dw2_map_expand_apply (objfile, per_cu,
2641 name, full_path, real_path,
2642 callback, data))
2643 return 1;
2644 }
2645
2646 /* Before we invoke realpath, which can get expensive when many
2647 files are involved, do a quick comparison of the basenames. */
2648 if (! basenames_may_differ
2649 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2650 continue;
2651
2652 if (full_path != NULL)
2653 {
2654 const char *this_real_name = dw2_get_real_path (objfile,
2655 file_data, j);
2656
2657 if (this_real_name != NULL
2658 && (FILENAME_CMP (full_path, this_real_name) == 0
2659 || (!is_abs
2660 && compare_filenames_for_search (this_real_name,
2661 name, name_len))))
2662 {
2663 if (dw2_map_expand_apply (objfile, per_cu,
2664 name, full_path, real_path,
2665 callback, data))
2666 return 1;
2667 }
2668 }
2669
2670 if (real_path != NULL)
2671 {
2672 const char *this_real_name = dw2_get_real_path (objfile,
2673 file_data, j);
2674
2675 if (this_real_name != NULL
2676 && (FILENAME_CMP (real_path, this_real_name) == 0
2677 || (!is_abs
2678 && compare_filenames_for_search (this_real_name,
2679 name, name_len))))
2680 {
2681 if (dw2_map_expand_apply (objfile, per_cu,
2682 name, full_path, real_path,
2683 callback, data))
2684 return 1;
2685 }
2686 }
2687 }
2688 }
2689
2690 return 0;
2691 }
2692
2693 static struct symtab *
2694 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2695 const char *name, domain_enum domain)
2696 {
2697 /* We do all the work in the pre_expand_symtabs_matching hook
2698 instead. */
2699 return NULL;
2700 }
2701
2702 /* A helper function that expands all symtabs that hold an object
2703 named NAME. */
2704
2705 static void
2706 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2707 {
2708 dw2_setup (objfile);
2709
2710 /* index_table is NULL if OBJF_READNOW. */
2711 if (dwarf2_per_objfile->index_table)
2712 {
2713 offset_type *vec;
2714
2715 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2716 name, &vec))
2717 {
2718 offset_type i, len = MAYBE_SWAP (*vec);
2719 for (i = 0; i < len; ++i)
2720 {
2721 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2722 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2723
2724 dw2_instantiate_symtab (per_cu);
2725 }
2726 }
2727 }
2728 }
2729
2730 static void
2731 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2732 enum block_enum block_kind, const char *name,
2733 domain_enum domain)
2734 {
2735 dw2_do_expand_symtabs_matching (objfile, name);
2736 }
2737
2738 static void
2739 dw2_print_stats (struct objfile *objfile)
2740 {
2741 int i, count;
2742
2743 dw2_setup (objfile);
2744 count = 0;
2745 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2746 + dwarf2_per_objfile->n_type_units); ++i)
2747 {
2748 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2749
2750 if (!per_cu->v.quick->symtab)
2751 ++count;
2752 }
2753 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2754 }
2755
2756 static void
2757 dw2_dump (struct objfile *objfile)
2758 {
2759 /* Nothing worth printing. */
2760 }
2761
2762 static void
2763 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2764 struct section_offsets *delta)
2765 {
2766 /* There's nothing to relocate here. */
2767 }
2768
2769 static void
2770 dw2_expand_symtabs_for_function (struct objfile *objfile,
2771 const char *func_name)
2772 {
2773 dw2_do_expand_symtabs_matching (objfile, func_name);
2774 }
2775
2776 static void
2777 dw2_expand_all_symtabs (struct objfile *objfile)
2778 {
2779 int i;
2780
2781 dw2_setup (objfile);
2782
2783 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2784 + dwarf2_per_objfile->n_type_units); ++i)
2785 {
2786 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2787
2788 dw2_instantiate_symtab (per_cu);
2789 }
2790 }
2791
2792 static void
2793 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2794 const char *filename)
2795 {
2796 int i;
2797
2798 dw2_setup (objfile);
2799
2800 /* We don't need to consider type units here.
2801 This is only called for examining code, e.g. expand_line_sal.
2802 There can be an order of magnitude (or more) more type units
2803 than comp units, and we avoid them if we can. */
2804
2805 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2806 {
2807 int j;
2808 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2809 struct quick_file_names *file_data;
2810
2811 /* We only need to look at symtabs not already expanded. */
2812 if (per_cu->v.quick->symtab)
2813 continue;
2814
2815 file_data = dw2_get_file_names (objfile, per_cu);
2816 if (file_data == NULL)
2817 continue;
2818
2819 for (j = 0; j < file_data->num_file_names; ++j)
2820 {
2821 const char *this_name = file_data->file_names[j];
2822 if (FILENAME_CMP (this_name, filename) == 0)
2823 {
2824 dw2_instantiate_symtab (per_cu);
2825 break;
2826 }
2827 }
2828 }
2829 }
2830
2831 static const char *
2832 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2833 {
2834 struct dwarf2_per_cu_data *per_cu;
2835 offset_type *vec;
2836 struct quick_file_names *file_data;
2837
2838 dw2_setup (objfile);
2839
2840 /* index_table is NULL if OBJF_READNOW. */
2841 if (!dwarf2_per_objfile->index_table)
2842 {
2843 struct symtab *s;
2844
2845 ALL_OBJFILE_SYMTABS (objfile, s)
2846 if (s->primary)
2847 {
2848 struct blockvector *bv = BLOCKVECTOR (s);
2849 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2850 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2851
2852 if (sym)
2853 return sym->symtab->filename;
2854 }
2855 return NULL;
2856 }
2857
2858 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2859 name, &vec))
2860 return NULL;
2861
2862 /* Note that this just looks at the very first one named NAME -- but
2863 actually we are looking for a function. find_main_filename
2864 should be rewritten so that it doesn't require a custom hook. It
2865 could just use the ordinary symbol tables. */
2866 /* vec[0] is the length, which must always be >0. */
2867 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2868
2869 file_data = dw2_get_file_names (objfile, per_cu);
2870 if (file_data == NULL
2871 || file_data->num_file_names == 0)
2872 return NULL;
2873
2874 return file_data->file_names[file_data->num_file_names - 1];
2875 }
2876
2877 static void
2878 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2879 struct objfile *objfile, int global,
2880 int (*callback) (struct block *,
2881 struct symbol *, void *),
2882 void *data, symbol_compare_ftype *match,
2883 symbol_compare_ftype *ordered_compare)
2884 {
2885 /* Currently unimplemented; used for Ada. The function can be called if the
2886 current language is Ada for a non-Ada objfile using GNU index. As Ada
2887 does not look for non-Ada symbols this function should just return. */
2888 }
2889
2890 static void
2891 dw2_expand_symtabs_matching
2892 (struct objfile *objfile,
2893 int (*file_matcher) (const char *, void *),
2894 int (*name_matcher) (const char *, void *),
2895 enum search_domain kind,
2896 void *data)
2897 {
2898 int i;
2899 offset_type iter;
2900 struct mapped_index *index;
2901
2902 dw2_setup (objfile);
2903
2904 /* index_table is NULL if OBJF_READNOW. */
2905 if (!dwarf2_per_objfile->index_table)
2906 return;
2907 index = dwarf2_per_objfile->index_table;
2908
2909 if (file_matcher != NULL)
2910 {
2911 struct cleanup *cleanup;
2912 htab_t visited_found, visited_not_found;
2913
2914 visited_found = htab_create_alloc (10,
2915 htab_hash_pointer, htab_eq_pointer,
2916 NULL, xcalloc, xfree);
2917 cleanup = make_cleanup_htab_delete (visited_found);
2918 visited_not_found = htab_create_alloc (10,
2919 htab_hash_pointer, htab_eq_pointer,
2920 NULL, xcalloc, xfree);
2921 make_cleanup_htab_delete (visited_not_found);
2922
2923 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2924 + dwarf2_per_objfile->n_type_units); ++i)
2925 {
2926 int j;
2927 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2928 struct quick_file_names *file_data;
2929 void **slot;
2930
2931 per_cu->v.quick->mark = 0;
2932
2933 /* We only need to look at symtabs not already expanded. */
2934 if (per_cu->v.quick->symtab)
2935 continue;
2936
2937 file_data = dw2_get_file_names (objfile, per_cu);
2938 if (file_data == NULL)
2939 continue;
2940
2941 if (htab_find (visited_not_found, file_data) != NULL)
2942 continue;
2943 else if (htab_find (visited_found, file_data) != NULL)
2944 {
2945 per_cu->v.quick->mark = 1;
2946 continue;
2947 }
2948
2949 for (j = 0; j < file_data->num_file_names; ++j)
2950 {
2951 if (file_matcher (file_data->file_names[j], data))
2952 {
2953 per_cu->v.quick->mark = 1;
2954 break;
2955 }
2956 }
2957
2958 slot = htab_find_slot (per_cu->v.quick->mark
2959 ? visited_found
2960 : visited_not_found,
2961 file_data, INSERT);
2962 *slot = file_data;
2963 }
2964
2965 do_cleanups (cleanup);
2966 }
2967
2968 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2969 {
2970 offset_type idx = 2 * iter;
2971 const char *name;
2972 offset_type *vec, vec_len, vec_idx;
2973
2974 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2975 continue;
2976
2977 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2978
2979 if (! (*name_matcher) (name, data))
2980 continue;
2981
2982 /* The name was matched, now expand corresponding CUs that were
2983 marked. */
2984 vec = (offset_type *) (index->constant_pool
2985 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2986 vec_len = MAYBE_SWAP (vec[0]);
2987 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2988 {
2989 struct dwarf2_per_cu_data *per_cu;
2990
2991 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2992 if (file_matcher == NULL || per_cu->v.quick->mark)
2993 dw2_instantiate_symtab (per_cu);
2994 }
2995 }
2996 }
2997
2998 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
2999 symtab. */
3000
3001 static struct symtab *
3002 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3003 {
3004 int i;
3005
3006 if (BLOCKVECTOR (symtab) != NULL
3007 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3008 return symtab;
3009
3010 if (symtab->includes == NULL)
3011 return NULL;
3012
3013 for (i = 0; symtab->includes[i]; ++i)
3014 {
3015 struct symtab *s = symtab->includes[i];
3016
3017 s = recursively_find_pc_sect_symtab (s, pc);
3018 if (s != NULL)
3019 return s;
3020 }
3021
3022 return NULL;
3023 }
3024
3025 static struct symtab *
3026 dw2_find_pc_sect_symtab (struct objfile *objfile,
3027 struct minimal_symbol *msymbol,
3028 CORE_ADDR pc,
3029 struct obj_section *section,
3030 int warn_if_readin)
3031 {
3032 struct dwarf2_per_cu_data *data;
3033 struct symtab *result;
3034
3035 dw2_setup (objfile);
3036
3037 if (!objfile->psymtabs_addrmap)
3038 return NULL;
3039
3040 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3041 if (!data)
3042 return NULL;
3043
3044 if (warn_if_readin && data->v.quick->symtab)
3045 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3046 paddress (get_objfile_arch (objfile), pc));
3047
3048 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3049 gdb_assert (result != NULL);
3050 return result;
3051 }
3052
3053 static void
3054 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3055 void *data, int need_fullname)
3056 {
3057 int i;
3058 struct cleanup *cleanup;
3059 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3060 NULL, xcalloc, xfree);
3061
3062 cleanup = make_cleanup_htab_delete (visited);
3063 dw2_setup (objfile);
3064
3065 /* We can ignore file names coming from already-expanded CUs. */
3066 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3067 + dwarf2_per_objfile->n_type_units); ++i)
3068 {
3069 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3070
3071 if (per_cu->v.quick->symtab)
3072 {
3073 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3074 INSERT);
3075
3076 *slot = per_cu->v.quick->file_names;
3077 }
3078 }
3079
3080 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3081 + dwarf2_per_objfile->n_type_units); ++i)
3082 {
3083 int j;
3084 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3085 struct quick_file_names *file_data;
3086 void **slot;
3087
3088 /* We only need to look at symtabs not already expanded. */
3089 if (per_cu->v.quick->symtab)
3090 continue;
3091
3092 file_data = dw2_get_file_names (objfile, per_cu);
3093 if (file_data == NULL)
3094 continue;
3095
3096 slot = htab_find_slot (visited, file_data, INSERT);
3097 if (*slot)
3098 {
3099 /* Already visited. */
3100 continue;
3101 }
3102 *slot = file_data;
3103
3104 for (j = 0; j < file_data->num_file_names; ++j)
3105 {
3106 const char *this_real_name;
3107
3108 if (need_fullname)
3109 this_real_name = dw2_get_real_path (objfile, file_data, j);
3110 else
3111 this_real_name = NULL;
3112 (*fun) (file_data->file_names[j], this_real_name, data);
3113 }
3114 }
3115
3116 do_cleanups (cleanup);
3117 }
3118
3119 static int
3120 dw2_has_symbols (struct objfile *objfile)
3121 {
3122 return 1;
3123 }
3124
3125 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3126 {
3127 dw2_has_symbols,
3128 dw2_find_last_source_symtab,
3129 dw2_forget_cached_source_info,
3130 dw2_map_symtabs_matching_filename,
3131 dw2_lookup_symbol,
3132 dw2_pre_expand_symtabs_matching,
3133 dw2_print_stats,
3134 dw2_dump,
3135 dw2_relocate,
3136 dw2_expand_symtabs_for_function,
3137 dw2_expand_all_symtabs,
3138 dw2_expand_symtabs_with_filename,
3139 dw2_find_symbol_file,
3140 dw2_map_matching_symbols,
3141 dw2_expand_symtabs_matching,
3142 dw2_find_pc_sect_symtab,
3143 dw2_map_symbol_filenames
3144 };
3145
3146 /* Initialize for reading DWARF for this objfile. Return 0 if this
3147 file will use psymtabs, or 1 if using the GNU index. */
3148
3149 int
3150 dwarf2_initialize_objfile (struct objfile *objfile)
3151 {
3152 /* If we're about to read full symbols, don't bother with the
3153 indices. In this case we also don't care if some other debug
3154 format is making psymtabs, because they are all about to be
3155 expanded anyway. */
3156 if ((objfile->flags & OBJF_READNOW))
3157 {
3158 int i;
3159
3160 dwarf2_per_objfile->using_index = 1;
3161 create_all_comp_units (objfile);
3162 create_all_type_units (objfile);
3163 dwarf2_per_objfile->quick_file_names_table =
3164 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3165
3166 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3167 + dwarf2_per_objfile->n_type_units); ++i)
3168 {
3169 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3170
3171 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3172 struct dwarf2_per_cu_quick_data);
3173 }
3174
3175 /* Return 1 so that gdb sees the "quick" functions. However,
3176 these functions will be no-ops because we will have expanded
3177 all symtabs. */
3178 return 1;
3179 }
3180
3181 if (dwarf2_read_index (objfile))
3182 return 1;
3183
3184 return 0;
3185 }
3186
3187 \f
3188
3189 /* Build a partial symbol table. */
3190
3191 void
3192 dwarf2_build_psymtabs (struct objfile *objfile)
3193 {
3194 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3195 {
3196 init_psymbol_list (objfile, 1024);
3197 }
3198
3199 dwarf2_build_psymtabs_hard (objfile);
3200 }
3201
3202 /* Return TRUE if OFFSET is within CU_HEADER. */
3203
3204 static inline int
3205 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3206 {
3207 sect_offset bottom = { cu_header->offset.sect_off };
3208 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3209 + cu_header->initial_length_size) };
3210
3211 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3212 }
3213
3214 /* Read in the comp unit header information from the debug_info at info_ptr.
3215 NOTE: This leaves members offset, first_die_offset to be filled in
3216 by the caller. */
3217
3218 static gdb_byte *
3219 read_comp_unit_head (struct comp_unit_head *cu_header,
3220 gdb_byte *info_ptr, bfd *abfd)
3221 {
3222 int signed_addr;
3223 unsigned int bytes_read;
3224
3225 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3226 cu_header->initial_length_size = bytes_read;
3227 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3228 info_ptr += bytes_read;
3229 cu_header->version = read_2_bytes (abfd, info_ptr);
3230 info_ptr += 2;
3231 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3232 &bytes_read);
3233 info_ptr += bytes_read;
3234 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3235 info_ptr += 1;
3236 signed_addr = bfd_get_sign_extend_vma (abfd);
3237 if (signed_addr < 0)
3238 internal_error (__FILE__, __LINE__,
3239 _("read_comp_unit_head: dwarf from non elf file"));
3240 cu_header->signed_addr_p = signed_addr;
3241
3242 return info_ptr;
3243 }
3244
3245 /* Subroutine of read_and_check_comp_unit_head and
3246 read_and_check_type_unit_head to simplify them.
3247 Perform various error checking on the header. */
3248
3249 static void
3250 error_check_comp_unit_head (struct comp_unit_head *header,
3251 struct dwarf2_section_info *section)
3252 {
3253 bfd *abfd = section->asection->owner;
3254 const char *filename = bfd_get_filename (abfd);
3255
3256 if (header->version != 2 && header->version != 3 && header->version != 4)
3257 error (_("Dwarf Error: wrong version in compilation unit header "
3258 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3259 filename);
3260
3261 if (header->abbrev_offset.sect_off
3262 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3263 &dwarf2_per_objfile->abbrev))
3264 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3265 "(offset 0x%lx + 6) [in module %s]"),
3266 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3267 filename);
3268
3269 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3270 avoid potential 32-bit overflow. */
3271 if (((unsigned long) header->offset.sect_off
3272 + header->length + header->initial_length_size)
3273 > section->size)
3274 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3275 "(offset 0x%lx + 0) [in module %s]"),
3276 (long) header->length, (long) header->offset.sect_off,
3277 filename);
3278 }
3279
3280 /* Read in a CU/TU header and perform some basic error checking.
3281 The contents of the header are stored in HEADER.
3282 The result is a pointer to the start of the first DIE. */
3283
3284 static gdb_byte *
3285 read_and_check_comp_unit_head (struct comp_unit_head *header,
3286 struct dwarf2_section_info *section,
3287 gdb_byte *info_ptr,
3288 int is_debug_types_section)
3289 {
3290 gdb_byte *beg_of_comp_unit = info_ptr;
3291 bfd *abfd = section->asection->owner;
3292
3293 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3294
3295 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3296
3297 /* If we're reading a type unit, skip over the signature and
3298 type_offset fields. */
3299 if (is_debug_types_section)
3300 info_ptr += 8 /*signature*/ + header->offset_size;
3301
3302 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3303
3304 error_check_comp_unit_head (header, section);
3305
3306 return info_ptr;
3307 }
3308
3309 /* Read in the types comp unit header information from .debug_types entry at
3310 types_ptr. The result is a pointer to one past the end of the header. */
3311
3312 static gdb_byte *
3313 read_and_check_type_unit_head (struct comp_unit_head *header,
3314 struct dwarf2_section_info *section,
3315 gdb_byte *info_ptr,
3316 ULONGEST *signature,
3317 cu_offset *type_offset_in_tu)
3318 {
3319 gdb_byte *beg_of_comp_unit = info_ptr;
3320 bfd *abfd = section->asection->owner;
3321
3322 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3323
3324 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3325
3326 /* If we're reading a type unit, skip over the signature and
3327 type_offset fields. */
3328 if (signature != NULL)
3329 *signature = read_8_bytes (abfd, info_ptr);
3330 info_ptr += 8;
3331 if (type_offset_in_tu != NULL)
3332 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3333 header->offset_size);
3334 info_ptr += header->offset_size;
3335
3336 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3337
3338 error_check_comp_unit_head (header, section);
3339
3340 return info_ptr;
3341 }
3342
3343 /* Allocate a new partial symtab for file named NAME and mark this new
3344 partial symtab as being an include of PST. */
3345
3346 static void
3347 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3348 struct objfile *objfile)
3349 {
3350 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3351
3352 subpst->section_offsets = pst->section_offsets;
3353 subpst->textlow = 0;
3354 subpst->texthigh = 0;
3355
3356 subpst->dependencies = (struct partial_symtab **)
3357 obstack_alloc (&objfile->objfile_obstack,
3358 sizeof (struct partial_symtab *));
3359 subpst->dependencies[0] = pst;
3360 subpst->number_of_dependencies = 1;
3361
3362 subpst->globals_offset = 0;
3363 subpst->n_global_syms = 0;
3364 subpst->statics_offset = 0;
3365 subpst->n_static_syms = 0;
3366 subpst->symtab = NULL;
3367 subpst->read_symtab = pst->read_symtab;
3368 subpst->readin = 0;
3369
3370 /* No private part is necessary for include psymtabs. This property
3371 can be used to differentiate between such include psymtabs and
3372 the regular ones. */
3373 subpst->read_symtab_private = NULL;
3374 }
3375
3376 /* Read the Line Number Program data and extract the list of files
3377 included by the source file represented by PST. Build an include
3378 partial symtab for each of these included files. */
3379
3380 static void
3381 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3382 struct die_info *die,
3383 struct partial_symtab *pst)
3384 {
3385 struct line_header *lh = NULL;
3386 struct attribute *attr;
3387
3388 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3389 if (attr)
3390 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3391 if (lh == NULL)
3392 return; /* No linetable, so no includes. */
3393
3394 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3395 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3396
3397 free_line_header (lh);
3398 }
3399
3400 static hashval_t
3401 hash_signatured_type (const void *item)
3402 {
3403 const struct signatured_type *sig_type = item;
3404
3405 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3406 return sig_type->signature;
3407 }
3408
3409 static int
3410 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3411 {
3412 const struct signatured_type *lhs = item_lhs;
3413 const struct signatured_type *rhs = item_rhs;
3414
3415 return lhs->signature == rhs->signature;
3416 }
3417
3418 /* Allocate a hash table for signatured types. */
3419
3420 static htab_t
3421 allocate_signatured_type_table (struct objfile *objfile)
3422 {
3423 return htab_create_alloc_ex (41,
3424 hash_signatured_type,
3425 eq_signatured_type,
3426 NULL,
3427 &objfile->objfile_obstack,
3428 hashtab_obstack_allocate,
3429 dummy_obstack_deallocate);
3430 }
3431
3432 /* A helper function to add a signatured type CU to a table. */
3433
3434 static int
3435 add_signatured_type_cu_to_table (void **slot, void *datum)
3436 {
3437 struct signatured_type *sigt = *slot;
3438 struct dwarf2_per_cu_data ***datap = datum;
3439
3440 **datap = &sigt->per_cu;
3441 ++*datap;
3442
3443 return 1;
3444 }
3445
3446 /* Create the hash table of all entries in the .debug_types section.
3447 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3448 The result is a pointer to the hash table or NULL if there are
3449 no types. */
3450
3451 static htab_t
3452 create_debug_types_hash_table (struct dwo_file *dwo_file,
3453 VEC (dwarf2_section_info_def) *types)
3454 {
3455 struct objfile *objfile = dwarf2_per_objfile->objfile;
3456 htab_t types_htab = NULL;
3457 int ix;
3458 struct dwarf2_section_info *section;
3459
3460 if (VEC_empty (dwarf2_section_info_def, types))
3461 return NULL;
3462
3463 for (ix = 0;
3464 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3465 ++ix)
3466 {
3467 bfd *abfd;
3468 gdb_byte *info_ptr, *end_ptr;
3469
3470 dwarf2_read_section (objfile, section);
3471 info_ptr = section->buffer;
3472
3473 if (info_ptr == NULL)
3474 continue;
3475
3476 /* We can't set abfd until now because the section may be empty or
3477 not present, in which case section->asection will be NULL. */
3478 abfd = section->asection->owner;
3479
3480 if (types_htab == NULL)
3481 {
3482 if (dwo_file)
3483 types_htab = allocate_dwo_unit_table (objfile);
3484 else
3485 types_htab = allocate_signatured_type_table (objfile);
3486 }
3487
3488 if (dwarf2_die_debug)
3489 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3490 bfd_get_filename (abfd));
3491
3492 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3493 because we don't need to read any dies: the signature is in the
3494 header. */
3495
3496 end_ptr = info_ptr + section->size;
3497 while (info_ptr < end_ptr)
3498 {
3499 sect_offset offset;
3500 cu_offset type_offset_in_tu;
3501 ULONGEST signature;
3502 struct signatured_type *sig_type;
3503 struct dwo_unit *dwo_tu;
3504 void **slot;
3505 gdb_byte *ptr = info_ptr;
3506 struct comp_unit_head header;
3507 unsigned int length;
3508
3509 offset.sect_off = ptr - section->buffer;
3510
3511 /* We need to read the type's signature in order to build the hash
3512 table, but we don't need anything else just yet. */
3513
3514 ptr = read_and_check_type_unit_head (&header, section, ptr,
3515 &signature, &type_offset_in_tu);
3516
3517 length = header.initial_length_size + header.length;
3518
3519 /* Skip dummy type units. */
3520 if (ptr >= info_ptr + length
3521 || peek_abbrev_code (abfd, ptr) == 0)
3522 {
3523 info_ptr += header.initial_length_size + header.length;
3524 continue;
3525 }
3526
3527 if (dwo_file)
3528 {
3529 sig_type = NULL;
3530 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3531 struct dwo_unit);
3532 dwo_tu->dwo_file = dwo_file;
3533 dwo_tu->signature = signature;
3534 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3535 dwo_tu->info_or_types_section = section;
3536 dwo_tu->offset = offset;
3537 dwo_tu->length = length;
3538 }
3539 else
3540 {
3541 /* N.B.: type_offset is not usable if this type uses a DWO file.
3542 The real type_offset is in the DWO file. */
3543 dwo_tu = NULL;
3544 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3545 struct signatured_type);
3546 sig_type->signature = signature;
3547 sig_type->type_offset_in_tu = type_offset_in_tu;
3548 sig_type->per_cu.objfile = objfile;
3549 sig_type->per_cu.is_debug_types = 1;
3550 sig_type->per_cu.info_or_types_section = section;
3551 sig_type->per_cu.offset = offset;
3552 sig_type->per_cu.length = length;
3553 }
3554
3555 slot = htab_find_slot (types_htab,
3556 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3557 INSERT);
3558 gdb_assert (slot != NULL);
3559 if (*slot != NULL)
3560 {
3561 sect_offset dup_offset;
3562
3563 if (dwo_file)
3564 {
3565 const struct dwo_unit *dup_tu = *slot;
3566
3567 dup_offset = dup_tu->offset;
3568 }
3569 else
3570 {
3571 const struct signatured_type *dup_tu = *slot;
3572
3573 dup_offset = dup_tu->per_cu.offset;
3574 }
3575
3576 complaint (&symfile_complaints,
3577 _("debug type entry at offset 0x%x is duplicate to the "
3578 "entry at offset 0x%x, signature 0x%s"),
3579 offset.sect_off, dup_offset.sect_off,
3580 phex (signature, sizeof (signature)));
3581 }
3582 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3583
3584 if (dwarf2_die_debug)
3585 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3586 offset.sect_off,
3587 phex (signature, sizeof (signature)));
3588
3589 info_ptr += length;
3590 }
3591 }
3592
3593 return types_htab;
3594 }
3595
3596 /* Create the hash table of all entries in the .debug_types section,
3597 and initialize all_type_units.
3598 The result is zero if there is an error (e.g. missing .debug_types section),
3599 otherwise non-zero. */
3600
3601 static int
3602 create_all_type_units (struct objfile *objfile)
3603 {
3604 htab_t types_htab;
3605 struct dwarf2_per_cu_data **iter;
3606
3607 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3608 if (types_htab == NULL)
3609 {
3610 dwarf2_per_objfile->signatured_types = NULL;
3611 return 0;
3612 }
3613
3614 dwarf2_per_objfile->signatured_types = types_htab;
3615
3616 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3617 dwarf2_per_objfile->all_type_units
3618 = obstack_alloc (&objfile->objfile_obstack,
3619 dwarf2_per_objfile->n_type_units
3620 * sizeof (struct dwarf2_per_cu_data *));
3621 iter = &dwarf2_per_objfile->all_type_units[0];
3622 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3623 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3624 == dwarf2_per_objfile->n_type_units);
3625
3626 return 1;
3627 }
3628
3629 /* Lookup a signature based type for DW_FORM_ref_sig8.
3630 Returns NULL if signature SIG is not present in the table. */
3631
3632 static struct signatured_type *
3633 lookup_signatured_type (ULONGEST sig)
3634 {
3635 struct signatured_type find_entry, *entry;
3636
3637 if (dwarf2_per_objfile->signatured_types == NULL)
3638 {
3639 complaint (&symfile_complaints,
3640 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3641 return NULL;
3642 }
3643
3644 find_entry.signature = sig;
3645 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3646 return entry;
3647 }
3648
3649 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3650
3651 static void
3652 init_cu_die_reader (struct die_reader_specs *reader,
3653 struct dwarf2_cu *cu,
3654 struct dwarf2_section_info *section,
3655 struct dwo_file *dwo_file)
3656 {
3657 gdb_assert (section->readin && section->buffer != NULL);
3658 reader->abfd = section->asection->owner;
3659 reader->cu = cu;
3660 reader->dwo_file = dwo_file;
3661 reader->die_section = section;
3662 reader->buffer = section->buffer;
3663 reader->buffer_end = section->buffer + section->size;
3664 }
3665
3666 /* Find the base address of the compilation unit for range lists and
3667 location lists. It will normally be specified by DW_AT_low_pc.
3668 In DWARF-3 draft 4, the base address could be overridden by
3669 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3670 compilation units with discontinuous ranges. */
3671
3672 static void
3673 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3674 {
3675 struct attribute *attr;
3676
3677 cu->base_known = 0;
3678 cu->base_address = 0;
3679
3680 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3681 if (attr)
3682 {
3683 cu->base_address = DW_ADDR (attr);
3684 cu->base_known = 1;
3685 }
3686 else
3687 {
3688 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3689 if (attr)
3690 {
3691 cu->base_address = DW_ADDR (attr);
3692 cu->base_known = 1;
3693 }
3694 }
3695 }
3696
3697 /* Initialize a CU (or TU) and read its DIEs.
3698 If the CU defers to a DWO file, read the DWO file as well.
3699
3700 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3701 Otherwise, a new CU is allocated with xmalloc.
3702
3703 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3704 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3705
3706 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3707 linker) then DIE_READER_FUNC will not get called. */
3708
3709 static void
3710 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3711 int use_existing_cu, int keep,
3712 die_reader_func_ftype *die_reader_func,
3713 void *data)
3714 {
3715 struct objfile *objfile = dwarf2_per_objfile->objfile;
3716 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3717 bfd *abfd = section->asection->owner;
3718 struct dwarf2_cu *cu;
3719 gdb_byte *begin_info_ptr, *info_ptr;
3720 struct die_reader_specs reader;
3721 struct die_info *comp_unit_die;
3722 int has_children;
3723 struct attribute *attr;
3724 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3725 struct signatured_type *sig_type = NULL;
3726
3727 if (use_existing_cu)
3728 gdb_assert (keep);
3729
3730 cleanups = make_cleanup (null_cleanup, NULL);
3731
3732 /* This is cheap if the section is already read in. */
3733 dwarf2_read_section (objfile, section);
3734
3735 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3736
3737 if (use_existing_cu && this_cu->cu != NULL)
3738 {
3739 cu = this_cu->cu;
3740 info_ptr += cu->header.first_die_offset.cu_off;
3741 }
3742 else
3743 {
3744 /* If !use_existing_cu, this_cu->cu must be NULL. */
3745 gdb_assert (this_cu->cu == NULL);
3746
3747 cu = xmalloc (sizeof (*cu));
3748 init_one_comp_unit (cu, this_cu);
3749
3750 /* If an error occurs while loading, release our storage. */
3751 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3752
3753 if (this_cu->is_debug_types)
3754 {
3755 ULONGEST signature;
3756
3757 info_ptr = read_and_check_type_unit_head (&cu->header,
3758 section, info_ptr,
3759 &signature, NULL);
3760
3761 /* There's no way to get from PER_CU to its containing
3762 struct signatured_type.
3763 But we have the signature so we can use that. */
3764 sig_type = lookup_signatured_type (signature);
3765 /* We've already scanned all the signatured types,
3766 this must succeed. */
3767 gdb_assert (sig_type != NULL);
3768 gdb_assert (&sig_type->per_cu == this_cu);
3769 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3770
3771 /* LENGTH has not been set yet for type units. */
3772 this_cu->length = cu->header.length + cu->header.initial_length_size;
3773
3774 /* Establish the type offset that can be used to lookup the type. */
3775 sig_type->type_offset_in_section.sect_off =
3776 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3777 }
3778 else
3779 {
3780 info_ptr = read_and_check_comp_unit_head (&cu->header,
3781 section, info_ptr, 0);
3782
3783 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3784 gdb_assert (this_cu->length
3785 == cu->header.length + cu->header.initial_length_size);
3786 }
3787 }
3788
3789 /* Skip dummy compilation units. */
3790 if (info_ptr >= begin_info_ptr + this_cu->length
3791 || peek_abbrev_code (abfd, info_ptr) == 0)
3792 {
3793 do_cleanups (cleanups);
3794 return;
3795 }
3796
3797 /* Read the abbrevs for this compilation unit into a table. */
3798 if (cu->dwarf2_abbrevs == NULL)
3799 {
3800 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3801 make_cleanup (dwarf2_free_abbrev_table, cu);
3802 }
3803
3804 /* Read the top level CU/TU die. */
3805 init_cu_die_reader (&reader, cu, section, NULL);
3806 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3807
3808 /* If we have a DWO stub, process it and then read in the DWO file.
3809 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3810 a DWO CU, that this test will fail. */
3811 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3812 if (attr)
3813 {
3814 char *dwo_name = DW_STRING (attr);
3815 const char *comp_dir;
3816 struct dwo_unit *dwo_unit;
3817 ULONGEST signature; /* Or dwo_id. */
3818 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3819 int i,num_extra_attrs;
3820
3821 if (has_children)
3822 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3823 " has children (offset 0x%x) [in module %s]"),
3824 this_cu->offset.sect_off, bfd_get_filename (abfd));
3825
3826 /* These attributes aren't processed until later:
3827 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3828 However, the attribute is found in the stub which we won't have later.
3829 In order to not impose this complication on the rest of the code,
3830 we read them here and copy them to the DWO CU/TU die. */
3831 stmt_list = low_pc = high_pc = ranges = NULL;
3832
3833 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3834 DWO file. */
3835 if (! this_cu->is_debug_types)
3836 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3837 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3838 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3839 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3840
3841 /* There should be a DW_AT_addr_base attribute here (if needed).
3842 We need the value before we can process DW_FORM_GNU_addr_index. */
3843 cu->addr_base = 0;
3844 cu->have_addr_base = 0;
3845 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3846 if (attr)
3847 {
3848 cu->addr_base = DW_UNSND (attr);
3849 cu->have_addr_base = 1;
3850 }
3851
3852 if (this_cu->is_debug_types)
3853 {
3854 gdb_assert (sig_type != NULL);
3855 signature = sig_type->signature;
3856 }
3857 else
3858 {
3859 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3860 if (! attr)
3861 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3862 dwo_name);
3863 signature = DW_UNSND (attr);
3864 }
3865
3866 /* We may need the comp_dir in order to find the DWO file. */
3867 comp_dir = NULL;
3868 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3869 if (attr)
3870 comp_dir = DW_STRING (attr);
3871
3872 if (this_cu->is_debug_types)
3873 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3874 else
3875 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3876 signature);
3877
3878 if (dwo_unit == NULL)
3879 {
3880 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3881 " with ID %s [in module %s]"),
3882 this_cu->offset.sect_off,
3883 phex (signature, sizeof (signature)),
3884 objfile->name);
3885 }
3886
3887 /* Set up for reading the DWO CU/TU. */
3888 cu->dwo_unit = dwo_unit;
3889 section = dwo_unit->info_or_types_section;
3890 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3891 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3892
3893 if (this_cu->is_debug_types)
3894 {
3895 ULONGEST signature;
3896
3897 info_ptr = read_and_check_type_unit_head (&cu->header,
3898 section, info_ptr,
3899 &signature, NULL);
3900 gdb_assert (sig_type->signature == signature);
3901 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3902 gdb_assert (dwo_unit->length
3903 == cu->header.length + cu->header.initial_length_size);
3904
3905 /* Establish the type offset that can be used to lookup the type.
3906 For DWO files, we don't know it until now. */
3907 sig_type->type_offset_in_section.sect_off =
3908 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3909 }
3910 else
3911 {
3912 info_ptr = read_and_check_comp_unit_head (&cu->header,
3913 section, info_ptr, 0);
3914 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3915 gdb_assert (dwo_unit->length
3916 == cu->header.length + cu->header.initial_length_size);
3917 }
3918
3919 /* Discard the original CU's abbrev table, and read the DWO's. */
3920 dwarf2_free_abbrev_table (cu);
3921 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3922
3923 /* Read in the die, but leave space to copy over the attributes
3924 from the stub. This has the benefit of simplifying the rest of
3925 the code - all the real work is done here. */
3926 num_extra_attrs = ((stmt_list != NULL)
3927 + (low_pc != NULL)
3928 + (high_pc != NULL)
3929 + (ranges != NULL));
3930 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3931 &has_children, num_extra_attrs);
3932
3933 /* Copy over the attributes from the stub to the DWO die. */
3934 i = comp_unit_die->num_attrs;
3935 if (stmt_list != NULL)
3936 comp_unit_die->attrs[i++] = *stmt_list;
3937 if (low_pc != NULL)
3938 comp_unit_die->attrs[i++] = *low_pc;
3939 if (high_pc != NULL)
3940 comp_unit_die->attrs[i++] = *high_pc;
3941 if (ranges != NULL)
3942 comp_unit_die->attrs[i++] = *ranges;
3943 comp_unit_die->num_attrs += num_extra_attrs;
3944
3945 /* Skip dummy compilation units. */
3946 if (info_ptr >= begin_info_ptr + dwo_unit->length
3947 || peek_abbrev_code (abfd, info_ptr) == 0)
3948 {
3949 do_cleanups (cleanups);
3950 return;
3951 }
3952 }
3953
3954 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3955
3956 if (free_cu_cleanup != NULL)
3957 {
3958 if (keep)
3959 {
3960 /* We've successfully allocated this compilation unit. Let our
3961 caller clean it up when finished with it. */
3962 discard_cleanups (free_cu_cleanup);
3963
3964 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3965 So we have to manually free the abbrev table. */
3966 dwarf2_free_abbrev_table (cu);
3967
3968 /* Link this CU into read_in_chain. */
3969 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3970 dwarf2_per_objfile->read_in_chain = this_cu;
3971 }
3972 else
3973 do_cleanups (free_cu_cleanup);
3974 }
3975
3976 do_cleanups (cleanups);
3977 }
3978
3979 /* Read CU/TU THIS_CU in section SECTION,
3980 but do not follow DW_AT_GNU_dwo_name if present.
3981 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3982 have already done the lookup to find the DWO file).
3983
3984 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3985 THIS_CU->is_debug_types, but nothing else.
3986
3987 We fill in THIS_CU->length.
3988
3989 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3990 linker) then DIE_READER_FUNC will not get called.
3991
3992 THIS_CU->cu is always freed when done.
3993 This is done in order to not leave THIS_CU->cu in a state where we have
3994 to care whether it refers to the "main" CU or the DWO CU. */
3995
3996 static void
3997 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3998 struct dwarf2_section_info *abbrev_section,
3999 struct dwo_file *dwo_file,
4000 die_reader_func_ftype *die_reader_func,
4001 void *data)
4002 {
4003 struct objfile *objfile = dwarf2_per_objfile->objfile;
4004 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4005 bfd *abfd = section->asection->owner;
4006 struct dwarf2_cu cu;
4007 gdb_byte *begin_info_ptr, *info_ptr;
4008 struct die_reader_specs reader;
4009 struct cleanup *cleanups;
4010 struct die_info *comp_unit_die;
4011 int has_children;
4012
4013 gdb_assert (this_cu->cu == NULL);
4014
4015 /* This is cheap if the section is already read in. */
4016 dwarf2_read_section (objfile, section);
4017
4018 init_one_comp_unit (&cu, this_cu);
4019
4020 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4021
4022 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4023 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
4024 this_cu->is_debug_types);
4025
4026 this_cu->length = cu.header.length + cu.header.initial_length_size;
4027
4028 /* Skip dummy compilation units. */
4029 if (info_ptr >= begin_info_ptr + this_cu->length
4030 || peek_abbrev_code (abfd, info_ptr) == 0)
4031 {
4032 do_cleanups (cleanups);
4033 return;
4034 }
4035
4036 dwarf2_read_abbrevs (&cu, abbrev_section);
4037 make_cleanup (dwarf2_free_abbrev_table, &cu);
4038
4039 init_cu_die_reader (&reader, &cu, section, dwo_file);
4040 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4041
4042 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4043
4044 do_cleanups (cleanups);
4045 }
4046
4047 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4048 does not lookup the specified DWO file.
4049 This cannot be used to read DWO files.
4050
4051 THIS_CU->cu is always freed when done.
4052 This is done in order to not leave THIS_CU->cu in a state where we have
4053 to care whether it refers to the "main" CU or the DWO CU.
4054 We can revisit this if the data shows there's a performance issue. */
4055
4056 static void
4057 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4058 die_reader_func_ftype *die_reader_func,
4059 void *data)
4060 {
4061 init_cutu_and_read_dies_no_follow (this_cu,
4062 &dwarf2_per_objfile->abbrev,
4063 NULL,
4064 die_reader_func, data);
4065 }
4066
4067 /* die_reader_func for process_psymtab_comp_unit. */
4068
4069 static void
4070 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4071 gdb_byte *info_ptr,
4072 struct die_info *comp_unit_die,
4073 int has_children,
4074 void *data)
4075 {
4076 struct dwarf2_cu *cu = reader->cu;
4077 struct objfile *objfile = cu->objfile;
4078 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4079 struct attribute *attr;
4080 CORE_ADDR baseaddr;
4081 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4082 struct partial_symtab *pst;
4083 int has_pc_info;
4084 const char *filename;
4085 int *want_partial_unit_ptr = data;
4086
4087 if (comp_unit_die->tag == DW_TAG_partial_unit
4088 && (want_partial_unit_ptr == NULL
4089 || !*want_partial_unit_ptr))
4090 return;
4091
4092 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4093
4094 cu->list_in_scope = &file_symbols;
4095
4096 /* Allocate a new partial symbol table structure. */
4097 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4098 if (attr == NULL || !DW_STRING (attr))
4099 filename = "";
4100 else
4101 filename = DW_STRING (attr);
4102 pst = start_psymtab_common (objfile, objfile->section_offsets,
4103 filename,
4104 /* TEXTLOW and TEXTHIGH are set below. */
4105 0,
4106 objfile->global_psymbols.next,
4107 objfile->static_psymbols.next);
4108 pst->psymtabs_addrmap_supported = 1;
4109
4110 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4111 if (attr != NULL)
4112 pst->dirname = DW_STRING (attr);
4113
4114 pst->read_symtab_private = per_cu;
4115
4116 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4117
4118 /* Store the function that reads in the rest of the symbol table. */
4119 pst->read_symtab = dwarf2_psymtab_to_symtab;
4120
4121 per_cu->v.psymtab = pst;
4122
4123 dwarf2_find_base_address (comp_unit_die, cu);
4124
4125 /* Possibly set the default values of LOWPC and HIGHPC from
4126 `DW_AT_ranges'. */
4127 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4128 &best_highpc, cu, pst);
4129 if (has_pc_info == 1 && best_lowpc < best_highpc)
4130 /* Store the contiguous range if it is not empty; it can be empty for
4131 CUs with no code. */
4132 addrmap_set_empty (objfile->psymtabs_addrmap,
4133 best_lowpc + baseaddr,
4134 best_highpc + baseaddr - 1, pst);
4135
4136 /* Check if comp unit has_children.
4137 If so, read the rest of the partial symbols from this comp unit.
4138 If not, there's no more debug_info for this comp unit. */
4139 if (has_children)
4140 {
4141 struct partial_die_info *first_die;
4142 CORE_ADDR lowpc, highpc;
4143
4144 lowpc = ((CORE_ADDR) -1);
4145 highpc = ((CORE_ADDR) 0);
4146
4147 first_die = load_partial_dies (reader, info_ptr, 1);
4148
4149 scan_partial_symbols (first_die, &lowpc, &highpc,
4150 ! has_pc_info, cu);
4151
4152 /* If we didn't find a lowpc, set it to highpc to avoid
4153 complaints from `maint check'. */
4154 if (lowpc == ((CORE_ADDR) -1))
4155 lowpc = highpc;
4156
4157 /* If the compilation unit didn't have an explicit address range,
4158 then use the information extracted from its child dies. */
4159 if (! has_pc_info)
4160 {
4161 best_lowpc = lowpc;
4162 best_highpc = highpc;
4163 }
4164 }
4165 pst->textlow = best_lowpc + baseaddr;
4166 pst->texthigh = best_highpc + baseaddr;
4167
4168 pst->n_global_syms = objfile->global_psymbols.next -
4169 (objfile->global_psymbols.list + pst->globals_offset);
4170 pst->n_static_syms = objfile->static_psymbols.next -
4171 (objfile->static_psymbols.list + pst->statics_offset);
4172 sort_pst_symbols (pst);
4173
4174 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4175 {
4176 int i;
4177 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4178 struct dwarf2_per_cu_data *iter;
4179
4180 /* Fill in 'dependencies' here; we fill in 'users' in a
4181 post-pass. */
4182 pst->number_of_dependencies = len;
4183 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4184 len * sizeof (struct symtab *));
4185 for (i = 0;
4186 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4187 i, iter);
4188 ++i)
4189 pst->dependencies[i] = iter->v.psymtab;
4190
4191 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4192 }
4193
4194 if (per_cu->is_debug_types)
4195 {
4196 /* It's not clear we want to do anything with stmt lists here.
4197 Waiting to see what gcc ultimately does. */
4198 }
4199 else
4200 {
4201 /* Get the list of files included in the current compilation unit,
4202 and build a psymtab for each of them. */
4203 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4204 }
4205 }
4206
4207 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4208 Process compilation unit THIS_CU for a psymtab. */
4209
4210 static void
4211 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4212 int want_partial_unit)
4213 {
4214 /* If this compilation unit was already read in, free the
4215 cached copy in order to read it in again. This is
4216 necessary because we skipped some symbols when we first
4217 read in the compilation unit (see load_partial_dies).
4218 This problem could be avoided, but the benefit is unclear. */
4219 if (this_cu->cu != NULL)
4220 free_one_cached_comp_unit (this_cu);
4221
4222 gdb_assert (! this_cu->is_debug_types);
4223 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4224 &want_partial_unit);
4225
4226 /* Age out any secondary CUs. */
4227 age_cached_comp_units ();
4228 }
4229
4230 /* Traversal function for htab_traverse_noresize.
4231 Process one .debug_types comp-unit. */
4232
4233 static int
4234 process_psymtab_type_unit (void **slot, void *info)
4235 {
4236 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4237 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4238
4239 gdb_assert (per_cu->is_debug_types);
4240 gdb_assert (info == NULL);
4241
4242 /* If this compilation unit was already read in, free the
4243 cached copy in order to read it in again. This is
4244 necessary because we skipped some symbols when we first
4245 read in the compilation unit (see load_partial_dies).
4246 This problem could be avoided, but the benefit is unclear. */
4247 if (per_cu->cu != NULL)
4248 free_one_cached_comp_unit (per_cu);
4249
4250 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4251 NULL);
4252
4253 /* Age out any secondary CUs. */
4254 age_cached_comp_units ();
4255
4256 return 1;
4257 }
4258
4259 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4260 Build partial symbol tables for the .debug_types comp-units. */
4261
4262 static void
4263 build_type_psymtabs (struct objfile *objfile)
4264 {
4265 if (! create_all_type_units (objfile))
4266 return;
4267
4268 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4269 process_psymtab_type_unit, NULL);
4270 }
4271
4272 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4273
4274 static void
4275 psymtabs_addrmap_cleanup (void *o)
4276 {
4277 struct objfile *objfile = o;
4278
4279 objfile->psymtabs_addrmap = NULL;
4280 }
4281
4282 /* Compute the 'user' field for each psymtab in OBJFILE. */
4283
4284 static void
4285 set_partial_user (struct objfile *objfile)
4286 {
4287 int i;
4288
4289 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4290 {
4291 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4292 struct partial_symtab *pst = per_cu->v.psymtab;
4293 int j;
4294
4295 for (j = 0; j < pst->number_of_dependencies; ++j)
4296 {
4297 /* Set the 'user' field only if it is not already set. */
4298 if (pst->dependencies[j]->user == NULL)
4299 pst->dependencies[j]->user = pst;
4300 }
4301 }
4302 }
4303
4304 /* Build the partial symbol table by doing a quick pass through the
4305 .debug_info and .debug_abbrev sections. */
4306
4307 static void
4308 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4309 {
4310 struct cleanup *back_to, *addrmap_cleanup;
4311 struct obstack temp_obstack;
4312 int i;
4313
4314 dwarf2_per_objfile->reading_partial_symbols = 1;
4315
4316 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4317
4318 /* Any cached compilation units will be linked by the per-objfile
4319 read_in_chain. Make sure to free them when we're done. */
4320 back_to = make_cleanup (free_cached_comp_units, NULL);
4321
4322 build_type_psymtabs (objfile);
4323
4324 create_all_comp_units (objfile);
4325
4326 /* Create a temporary address map on a temporary obstack. We later
4327 copy this to the final obstack. */
4328 obstack_init (&temp_obstack);
4329 make_cleanup_obstack_free (&temp_obstack);
4330 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4331 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4332
4333 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4334 {
4335 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4336
4337 process_psymtab_comp_unit (per_cu, 0);
4338 }
4339
4340 set_partial_user (objfile);
4341
4342 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4343 &objfile->objfile_obstack);
4344 discard_cleanups (addrmap_cleanup);
4345
4346 do_cleanups (back_to);
4347 }
4348
4349 /* die_reader_func for load_partial_comp_unit. */
4350
4351 static void
4352 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4353 gdb_byte *info_ptr,
4354 struct die_info *comp_unit_die,
4355 int has_children,
4356 void *data)
4357 {
4358 struct dwarf2_cu *cu = reader->cu;
4359
4360 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4361
4362 /* Check if comp unit has_children.
4363 If so, read the rest of the partial symbols from this comp unit.
4364 If not, there's no more debug_info for this comp unit. */
4365 if (has_children)
4366 load_partial_dies (reader, info_ptr, 0);
4367 }
4368
4369 /* Load the partial DIEs for a secondary CU into memory.
4370 This is also used when rereading a primary CU with load_all_dies. */
4371
4372 static void
4373 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4374 {
4375 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4376 }
4377
4378 /* Create a list of all compilation units in OBJFILE.
4379 This is only done for -readnow and building partial symtabs. */
4380
4381 static void
4382 create_all_comp_units (struct objfile *objfile)
4383 {
4384 int n_allocated;
4385 int n_comp_units;
4386 struct dwarf2_per_cu_data **all_comp_units;
4387 gdb_byte *info_ptr;
4388
4389 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4390 info_ptr = dwarf2_per_objfile->info.buffer;
4391
4392 n_comp_units = 0;
4393 n_allocated = 10;
4394 all_comp_units = xmalloc (n_allocated
4395 * sizeof (struct dwarf2_per_cu_data *));
4396
4397 while (info_ptr < dwarf2_per_objfile->info.buffer
4398 + dwarf2_per_objfile->info.size)
4399 {
4400 unsigned int length, initial_length_size;
4401 struct dwarf2_per_cu_data *this_cu;
4402 sect_offset offset;
4403
4404 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4405
4406 /* Read just enough information to find out where the next
4407 compilation unit is. */
4408 length = read_initial_length (objfile->obfd, info_ptr,
4409 &initial_length_size);
4410
4411 /* Save the compilation unit for later lookup. */
4412 this_cu = obstack_alloc (&objfile->objfile_obstack,
4413 sizeof (struct dwarf2_per_cu_data));
4414 memset (this_cu, 0, sizeof (*this_cu));
4415 this_cu->offset = offset;
4416 this_cu->length = length + initial_length_size;
4417 this_cu->objfile = objfile;
4418 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4419
4420 if (n_comp_units == n_allocated)
4421 {
4422 n_allocated *= 2;
4423 all_comp_units = xrealloc (all_comp_units,
4424 n_allocated
4425 * sizeof (struct dwarf2_per_cu_data *));
4426 }
4427 all_comp_units[n_comp_units++] = this_cu;
4428
4429 info_ptr = info_ptr + this_cu->length;
4430 }
4431
4432 dwarf2_per_objfile->all_comp_units
4433 = obstack_alloc (&objfile->objfile_obstack,
4434 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4435 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4436 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4437 xfree (all_comp_units);
4438 dwarf2_per_objfile->n_comp_units = n_comp_units;
4439 }
4440
4441 /* Process all loaded DIEs for compilation unit CU, starting at
4442 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4443 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4444 DW_AT_ranges). If NEED_PC is set, then this function will set
4445 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4446 and record the covered ranges in the addrmap. */
4447
4448 static void
4449 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4450 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4451 {
4452 struct partial_die_info *pdi;
4453
4454 /* Now, march along the PDI's, descending into ones which have
4455 interesting children but skipping the children of the other ones,
4456 until we reach the end of the compilation unit. */
4457
4458 pdi = first_die;
4459
4460 while (pdi != NULL)
4461 {
4462 fixup_partial_die (pdi, cu);
4463
4464 /* Anonymous namespaces or modules have no name but have interesting
4465 children, so we need to look at them. Ditto for anonymous
4466 enums. */
4467
4468 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4469 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4470 || pdi->tag == DW_TAG_imported_unit)
4471 {
4472 switch (pdi->tag)
4473 {
4474 case DW_TAG_subprogram:
4475 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4476 break;
4477 case DW_TAG_constant:
4478 case DW_TAG_variable:
4479 case DW_TAG_typedef:
4480 case DW_TAG_union_type:
4481 if (!pdi->is_declaration)
4482 {
4483 add_partial_symbol (pdi, cu);
4484 }
4485 break;
4486 case DW_TAG_class_type:
4487 case DW_TAG_interface_type:
4488 case DW_TAG_structure_type:
4489 if (!pdi->is_declaration)
4490 {
4491 add_partial_symbol (pdi, cu);
4492 }
4493 break;
4494 case DW_TAG_enumeration_type:
4495 if (!pdi->is_declaration)
4496 add_partial_enumeration (pdi, cu);
4497 break;
4498 case DW_TAG_base_type:
4499 case DW_TAG_subrange_type:
4500 /* File scope base type definitions are added to the partial
4501 symbol table. */
4502 add_partial_symbol (pdi, cu);
4503 break;
4504 case DW_TAG_namespace:
4505 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4506 break;
4507 case DW_TAG_module:
4508 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4509 break;
4510 case DW_TAG_imported_unit:
4511 {
4512 struct dwarf2_per_cu_data *per_cu;
4513
4514 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4515 cu->objfile);
4516
4517 /* Go read the partial unit, if needed. */
4518 if (per_cu->v.psymtab == NULL)
4519 process_psymtab_comp_unit (per_cu, 1);
4520
4521 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4522 per_cu);
4523 }
4524 break;
4525 default:
4526 break;
4527 }
4528 }
4529
4530 /* If the die has a sibling, skip to the sibling. */
4531
4532 pdi = pdi->die_sibling;
4533 }
4534 }
4535
4536 /* Functions used to compute the fully scoped name of a partial DIE.
4537
4538 Normally, this is simple. For C++, the parent DIE's fully scoped
4539 name is concatenated with "::" and the partial DIE's name. For
4540 Java, the same thing occurs except that "." is used instead of "::".
4541 Enumerators are an exception; they use the scope of their parent
4542 enumeration type, i.e. the name of the enumeration type is not
4543 prepended to the enumerator.
4544
4545 There are two complexities. One is DW_AT_specification; in this
4546 case "parent" means the parent of the target of the specification,
4547 instead of the direct parent of the DIE. The other is compilers
4548 which do not emit DW_TAG_namespace; in this case we try to guess
4549 the fully qualified name of structure types from their members'
4550 linkage names. This must be done using the DIE's children rather
4551 than the children of any DW_AT_specification target. We only need
4552 to do this for structures at the top level, i.e. if the target of
4553 any DW_AT_specification (if any; otherwise the DIE itself) does not
4554 have a parent. */
4555
4556 /* Compute the scope prefix associated with PDI's parent, in
4557 compilation unit CU. The result will be allocated on CU's
4558 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4559 field. NULL is returned if no prefix is necessary. */
4560 static char *
4561 partial_die_parent_scope (struct partial_die_info *pdi,
4562 struct dwarf2_cu *cu)
4563 {
4564 char *grandparent_scope;
4565 struct partial_die_info *parent, *real_pdi;
4566
4567 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4568 then this means the parent of the specification DIE. */
4569
4570 real_pdi = pdi;
4571 while (real_pdi->has_specification)
4572 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4573
4574 parent = real_pdi->die_parent;
4575 if (parent == NULL)
4576 return NULL;
4577
4578 if (parent->scope_set)
4579 return parent->scope;
4580
4581 fixup_partial_die (parent, cu);
4582
4583 grandparent_scope = partial_die_parent_scope (parent, cu);
4584
4585 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4586 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4587 Work around this problem here. */
4588 if (cu->language == language_cplus
4589 && parent->tag == DW_TAG_namespace
4590 && strcmp (parent->name, "::") == 0
4591 && grandparent_scope == NULL)
4592 {
4593 parent->scope = NULL;
4594 parent->scope_set = 1;
4595 return NULL;
4596 }
4597
4598 if (pdi->tag == DW_TAG_enumerator)
4599 /* Enumerators should not get the name of the enumeration as a prefix. */
4600 parent->scope = grandparent_scope;
4601 else if (parent->tag == DW_TAG_namespace
4602 || parent->tag == DW_TAG_module
4603 || parent->tag == DW_TAG_structure_type
4604 || parent->tag == DW_TAG_class_type
4605 || parent->tag == DW_TAG_interface_type
4606 || parent->tag == DW_TAG_union_type
4607 || parent->tag == DW_TAG_enumeration_type)
4608 {
4609 if (grandparent_scope == NULL)
4610 parent->scope = parent->name;
4611 else
4612 parent->scope = typename_concat (&cu->comp_unit_obstack,
4613 grandparent_scope,
4614 parent->name, 0, cu);
4615 }
4616 else
4617 {
4618 /* FIXME drow/2004-04-01: What should we be doing with
4619 function-local names? For partial symbols, we should probably be
4620 ignoring them. */
4621 complaint (&symfile_complaints,
4622 _("unhandled containing DIE tag %d for DIE at %d"),
4623 parent->tag, pdi->offset.sect_off);
4624 parent->scope = grandparent_scope;
4625 }
4626
4627 parent->scope_set = 1;
4628 return parent->scope;
4629 }
4630
4631 /* Return the fully scoped name associated with PDI, from compilation unit
4632 CU. The result will be allocated with malloc. */
4633
4634 static char *
4635 partial_die_full_name (struct partial_die_info *pdi,
4636 struct dwarf2_cu *cu)
4637 {
4638 char *parent_scope;
4639
4640 /* If this is a template instantiation, we can not work out the
4641 template arguments from partial DIEs. So, unfortunately, we have
4642 to go through the full DIEs. At least any work we do building
4643 types here will be reused if full symbols are loaded later. */
4644 if (pdi->has_template_arguments)
4645 {
4646 fixup_partial_die (pdi, cu);
4647
4648 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4649 {
4650 struct die_info *die;
4651 struct attribute attr;
4652 struct dwarf2_cu *ref_cu = cu;
4653
4654 /* DW_FORM_ref_addr is using section offset. */
4655 attr.name = 0;
4656 attr.form = DW_FORM_ref_addr;
4657 attr.u.unsnd = pdi->offset.sect_off;
4658 die = follow_die_ref (NULL, &attr, &ref_cu);
4659
4660 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4661 }
4662 }
4663
4664 parent_scope = partial_die_parent_scope (pdi, cu);
4665 if (parent_scope == NULL)
4666 return NULL;
4667 else
4668 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4669 }
4670
4671 static void
4672 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4673 {
4674 struct objfile *objfile = cu->objfile;
4675 CORE_ADDR addr = 0;
4676 char *actual_name = NULL;
4677 CORE_ADDR baseaddr;
4678 int built_actual_name = 0;
4679
4680 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4681
4682 actual_name = partial_die_full_name (pdi, cu);
4683 if (actual_name)
4684 built_actual_name = 1;
4685
4686 if (actual_name == NULL)
4687 actual_name = pdi->name;
4688
4689 switch (pdi->tag)
4690 {
4691 case DW_TAG_subprogram:
4692 if (pdi->is_external || cu->language == language_ada)
4693 {
4694 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4695 of the global scope. But in Ada, we want to be able to access
4696 nested procedures globally. So all Ada subprograms are stored
4697 in the global scope. */
4698 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4699 mst_text, objfile); */
4700 add_psymbol_to_list (actual_name, strlen (actual_name),
4701 built_actual_name,
4702 VAR_DOMAIN, LOC_BLOCK,
4703 &objfile->global_psymbols,
4704 0, pdi->lowpc + baseaddr,
4705 cu->language, objfile);
4706 }
4707 else
4708 {
4709 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4710 mst_file_text, objfile); */
4711 add_psymbol_to_list (actual_name, strlen (actual_name),
4712 built_actual_name,
4713 VAR_DOMAIN, LOC_BLOCK,
4714 &objfile->static_psymbols,
4715 0, pdi->lowpc + baseaddr,
4716 cu->language, objfile);
4717 }
4718 break;
4719 case DW_TAG_constant:
4720 {
4721 struct psymbol_allocation_list *list;
4722
4723 if (pdi->is_external)
4724 list = &objfile->global_psymbols;
4725 else
4726 list = &objfile->static_psymbols;
4727 add_psymbol_to_list (actual_name, strlen (actual_name),
4728 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4729 list, 0, 0, cu->language, objfile);
4730 }
4731 break;
4732 case DW_TAG_variable:
4733 if (pdi->d.locdesc)
4734 addr = decode_locdesc (pdi->d.locdesc, cu);
4735
4736 if (pdi->d.locdesc
4737 && addr == 0
4738 && !dwarf2_per_objfile->has_section_at_zero)
4739 {
4740 /* A global or static variable may also have been stripped
4741 out by the linker if unused, in which case its address
4742 will be nullified; do not add such variables into partial
4743 symbol table then. */
4744 }
4745 else if (pdi->is_external)
4746 {
4747 /* Global Variable.
4748 Don't enter into the minimal symbol tables as there is
4749 a minimal symbol table entry from the ELF symbols already.
4750 Enter into partial symbol table if it has a location
4751 descriptor or a type.
4752 If the location descriptor is missing, new_symbol will create
4753 a LOC_UNRESOLVED symbol, the address of the variable will then
4754 be determined from the minimal symbol table whenever the variable
4755 is referenced.
4756 The address for the partial symbol table entry is not
4757 used by GDB, but it comes in handy for debugging partial symbol
4758 table building. */
4759
4760 if (pdi->d.locdesc || pdi->has_type)
4761 add_psymbol_to_list (actual_name, strlen (actual_name),
4762 built_actual_name,
4763 VAR_DOMAIN, LOC_STATIC,
4764 &objfile->global_psymbols,
4765 0, addr + baseaddr,
4766 cu->language, objfile);
4767 }
4768 else
4769 {
4770 /* Static Variable. Skip symbols without location descriptors. */
4771 if (pdi->d.locdesc == NULL)
4772 {
4773 if (built_actual_name)
4774 xfree (actual_name);
4775 return;
4776 }
4777 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4778 mst_file_data, objfile); */
4779 add_psymbol_to_list (actual_name, strlen (actual_name),
4780 built_actual_name,
4781 VAR_DOMAIN, LOC_STATIC,
4782 &objfile->static_psymbols,
4783 0, addr + baseaddr,
4784 cu->language, objfile);
4785 }
4786 break;
4787 case DW_TAG_typedef:
4788 case DW_TAG_base_type:
4789 case DW_TAG_subrange_type:
4790 add_psymbol_to_list (actual_name, strlen (actual_name),
4791 built_actual_name,
4792 VAR_DOMAIN, LOC_TYPEDEF,
4793 &objfile->static_psymbols,
4794 0, (CORE_ADDR) 0, cu->language, objfile);
4795 break;
4796 case DW_TAG_namespace:
4797 add_psymbol_to_list (actual_name, strlen (actual_name),
4798 built_actual_name,
4799 VAR_DOMAIN, LOC_TYPEDEF,
4800 &objfile->global_psymbols,
4801 0, (CORE_ADDR) 0, cu->language, objfile);
4802 break;
4803 case DW_TAG_class_type:
4804 case DW_TAG_interface_type:
4805 case DW_TAG_structure_type:
4806 case DW_TAG_union_type:
4807 case DW_TAG_enumeration_type:
4808 /* Skip external references. The DWARF standard says in the section
4809 about "Structure, Union, and Class Type Entries": "An incomplete
4810 structure, union or class type is represented by a structure,
4811 union or class entry that does not have a byte size attribute
4812 and that has a DW_AT_declaration attribute." */
4813 if (!pdi->has_byte_size && pdi->is_declaration)
4814 {
4815 if (built_actual_name)
4816 xfree (actual_name);
4817 return;
4818 }
4819
4820 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4821 static vs. global. */
4822 add_psymbol_to_list (actual_name, strlen (actual_name),
4823 built_actual_name,
4824 STRUCT_DOMAIN, LOC_TYPEDEF,
4825 (cu->language == language_cplus
4826 || cu->language == language_java)
4827 ? &objfile->global_psymbols
4828 : &objfile->static_psymbols,
4829 0, (CORE_ADDR) 0, cu->language, objfile);
4830
4831 break;
4832 case DW_TAG_enumerator:
4833 add_psymbol_to_list (actual_name, strlen (actual_name),
4834 built_actual_name,
4835 VAR_DOMAIN, LOC_CONST,
4836 (cu->language == language_cplus
4837 || cu->language == language_java)
4838 ? &objfile->global_psymbols
4839 : &objfile->static_psymbols,
4840 0, (CORE_ADDR) 0, cu->language, objfile);
4841 break;
4842 default:
4843 break;
4844 }
4845
4846 if (built_actual_name)
4847 xfree (actual_name);
4848 }
4849
4850 /* Read a partial die corresponding to a namespace; also, add a symbol
4851 corresponding to that namespace to the symbol table. NAMESPACE is
4852 the name of the enclosing namespace. */
4853
4854 static void
4855 add_partial_namespace (struct partial_die_info *pdi,
4856 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4857 int need_pc, struct dwarf2_cu *cu)
4858 {
4859 /* Add a symbol for the namespace. */
4860
4861 add_partial_symbol (pdi, cu);
4862
4863 /* Now scan partial symbols in that namespace. */
4864
4865 if (pdi->has_children)
4866 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4867 }
4868
4869 /* Read a partial die corresponding to a Fortran module. */
4870
4871 static void
4872 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4873 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4874 {
4875 /* Now scan partial symbols in that module. */
4876
4877 if (pdi->has_children)
4878 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4879 }
4880
4881 /* Read a partial die corresponding to a subprogram and create a partial
4882 symbol for that subprogram. When the CU language allows it, this
4883 routine also defines a partial symbol for each nested subprogram
4884 that this subprogram contains.
4885
4886 DIE my also be a lexical block, in which case we simply search
4887 recursively for suprograms defined inside that lexical block.
4888 Again, this is only performed when the CU language allows this
4889 type of definitions. */
4890
4891 static void
4892 add_partial_subprogram (struct partial_die_info *pdi,
4893 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4894 int need_pc, struct dwarf2_cu *cu)
4895 {
4896 if (pdi->tag == DW_TAG_subprogram)
4897 {
4898 if (pdi->has_pc_info)
4899 {
4900 if (pdi->lowpc < *lowpc)
4901 *lowpc = pdi->lowpc;
4902 if (pdi->highpc > *highpc)
4903 *highpc = pdi->highpc;
4904 if (need_pc)
4905 {
4906 CORE_ADDR baseaddr;
4907 struct objfile *objfile = cu->objfile;
4908
4909 baseaddr = ANOFFSET (objfile->section_offsets,
4910 SECT_OFF_TEXT (objfile));
4911 addrmap_set_empty (objfile->psymtabs_addrmap,
4912 pdi->lowpc + baseaddr,
4913 pdi->highpc - 1 + baseaddr,
4914 cu->per_cu->v.psymtab);
4915 }
4916 }
4917
4918 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4919 {
4920 if (!pdi->is_declaration)
4921 /* Ignore subprogram DIEs that do not have a name, they are
4922 illegal. Do not emit a complaint at this point, we will
4923 do so when we convert this psymtab into a symtab. */
4924 if (pdi->name)
4925 add_partial_symbol (pdi, cu);
4926 }
4927 }
4928
4929 if (! pdi->has_children)
4930 return;
4931
4932 if (cu->language == language_ada)
4933 {
4934 pdi = pdi->die_child;
4935 while (pdi != NULL)
4936 {
4937 fixup_partial_die (pdi, cu);
4938 if (pdi->tag == DW_TAG_subprogram
4939 || pdi->tag == DW_TAG_lexical_block)
4940 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4941 pdi = pdi->die_sibling;
4942 }
4943 }
4944 }
4945
4946 /* Read a partial die corresponding to an enumeration type. */
4947
4948 static void
4949 add_partial_enumeration (struct partial_die_info *enum_pdi,
4950 struct dwarf2_cu *cu)
4951 {
4952 struct partial_die_info *pdi;
4953
4954 if (enum_pdi->name != NULL)
4955 add_partial_symbol (enum_pdi, cu);
4956
4957 pdi = enum_pdi->die_child;
4958 while (pdi)
4959 {
4960 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4961 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4962 else
4963 add_partial_symbol (pdi, cu);
4964 pdi = pdi->die_sibling;
4965 }
4966 }
4967
4968 /* Return the initial uleb128 in the die at INFO_PTR. */
4969
4970 static unsigned int
4971 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4972 {
4973 unsigned int bytes_read;
4974
4975 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4976 }
4977
4978 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4979 Return the corresponding abbrev, or NULL if the number is zero (indicating
4980 an empty DIE). In either case *BYTES_READ will be set to the length of
4981 the initial number. */
4982
4983 static struct abbrev_info *
4984 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4985 struct dwarf2_cu *cu)
4986 {
4987 bfd *abfd = cu->objfile->obfd;
4988 unsigned int abbrev_number;
4989 struct abbrev_info *abbrev;
4990
4991 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4992
4993 if (abbrev_number == 0)
4994 return NULL;
4995
4996 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4997 if (!abbrev)
4998 {
4999 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5000 abbrev_number, bfd_get_filename (abfd));
5001 }
5002
5003 return abbrev;
5004 }
5005
5006 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5007 Returns a pointer to the end of a series of DIEs, terminated by an empty
5008 DIE. Any children of the skipped DIEs will also be skipped. */
5009
5010 static gdb_byte *
5011 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
5012 {
5013 struct dwarf2_cu *cu = reader->cu;
5014 struct abbrev_info *abbrev;
5015 unsigned int bytes_read;
5016
5017 while (1)
5018 {
5019 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5020 if (abbrev == NULL)
5021 return info_ptr + bytes_read;
5022 else
5023 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5024 }
5025 }
5026
5027 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5028 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5029 abbrev corresponding to that skipped uleb128 should be passed in
5030 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5031 children. */
5032
5033 static gdb_byte *
5034 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5035 struct abbrev_info *abbrev)
5036 {
5037 unsigned int bytes_read;
5038 struct attribute attr;
5039 bfd *abfd = reader->abfd;
5040 struct dwarf2_cu *cu = reader->cu;
5041 gdb_byte *buffer = reader->buffer;
5042 const gdb_byte *buffer_end = reader->buffer_end;
5043 gdb_byte *start_info_ptr = info_ptr;
5044 unsigned int form, i;
5045
5046 for (i = 0; i < abbrev->num_attrs; i++)
5047 {
5048 /* The only abbrev we care about is DW_AT_sibling. */
5049 if (abbrev->attrs[i].name == DW_AT_sibling)
5050 {
5051 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5052 if (attr.form == DW_FORM_ref_addr)
5053 complaint (&symfile_complaints,
5054 _("ignoring absolute DW_AT_sibling"));
5055 else
5056 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5057 }
5058
5059 /* If it isn't DW_AT_sibling, skip this attribute. */
5060 form = abbrev->attrs[i].form;
5061 skip_attribute:
5062 switch (form)
5063 {
5064 case DW_FORM_ref_addr:
5065 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5066 and later it is offset sized. */
5067 if (cu->header.version == 2)
5068 info_ptr += cu->header.addr_size;
5069 else
5070 info_ptr += cu->header.offset_size;
5071 break;
5072 case DW_FORM_addr:
5073 info_ptr += cu->header.addr_size;
5074 break;
5075 case DW_FORM_data1:
5076 case DW_FORM_ref1:
5077 case DW_FORM_flag:
5078 info_ptr += 1;
5079 break;
5080 case DW_FORM_flag_present:
5081 break;
5082 case DW_FORM_data2:
5083 case DW_FORM_ref2:
5084 info_ptr += 2;
5085 break;
5086 case DW_FORM_data4:
5087 case DW_FORM_ref4:
5088 info_ptr += 4;
5089 break;
5090 case DW_FORM_data8:
5091 case DW_FORM_ref8:
5092 case DW_FORM_ref_sig8:
5093 info_ptr += 8;
5094 break;
5095 case DW_FORM_string:
5096 read_direct_string (abfd, info_ptr, &bytes_read);
5097 info_ptr += bytes_read;
5098 break;
5099 case DW_FORM_sec_offset:
5100 case DW_FORM_strp:
5101 info_ptr += cu->header.offset_size;
5102 break;
5103 case DW_FORM_exprloc:
5104 case DW_FORM_block:
5105 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5106 info_ptr += bytes_read;
5107 break;
5108 case DW_FORM_block1:
5109 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5110 break;
5111 case DW_FORM_block2:
5112 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5113 break;
5114 case DW_FORM_block4:
5115 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5116 break;
5117 case DW_FORM_sdata:
5118 case DW_FORM_udata:
5119 case DW_FORM_ref_udata:
5120 case DW_FORM_GNU_addr_index:
5121 case DW_FORM_GNU_str_index:
5122 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
5123 break;
5124 case DW_FORM_indirect:
5125 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5126 info_ptr += bytes_read;
5127 /* We need to continue parsing from here, so just go back to
5128 the top. */
5129 goto skip_attribute;
5130
5131 default:
5132 error (_("Dwarf Error: Cannot handle %s "
5133 "in DWARF reader [in module %s]"),
5134 dwarf_form_name (form),
5135 bfd_get_filename (abfd));
5136 }
5137 }
5138
5139 if (abbrev->has_children)
5140 return skip_children (reader, info_ptr);
5141 else
5142 return info_ptr;
5143 }
5144
5145 /* Locate ORIG_PDI's sibling.
5146 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5147
5148 static gdb_byte *
5149 locate_pdi_sibling (const struct die_reader_specs *reader,
5150 struct partial_die_info *orig_pdi,
5151 gdb_byte *info_ptr)
5152 {
5153 /* Do we know the sibling already? */
5154
5155 if (orig_pdi->sibling)
5156 return orig_pdi->sibling;
5157
5158 /* Are there any children to deal with? */
5159
5160 if (!orig_pdi->has_children)
5161 return info_ptr;
5162
5163 /* Skip the children the long way. */
5164
5165 return skip_children (reader, info_ptr);
5166 }
5167
5168 /* Expand this partial symbol table into a full symbol table. */
5169
5170 static void
5171 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5172 {
5173 if (pst != NULL)
5174 {
5175 if (pst->readin)
5176 {
5177 warning (_("bug: psymtab for %s is already read in."),
5178 pst->filename);
5179 }
5180 else
5181 {
5182 if (info_verbose)
5183 {
5184 printf_filtered (_("Reading in symbols for %s..."),
5185 pst->filename);
5186 gdb_flush (gdb_stdout);
5187 }
5188
5189 /* Restore our global data. */
5190 dwarf2_per_objfile = objfile_data (pst->objfile,
5191 dwarf2_objfile_data_key);
5192
5193 /* If this psymtab is constructed from a debug-only objfile, the
5194 has_section_at_zero flag will not necessarily be correct. We
5195 can get the correct value for this flag by looking at the data
5196 associated with the (presumably stripped) associated objfile. */
5197 if (pst->objfile->separate_debug_objfile_backlink)
5198 {
5199 struct dwarf2_per_objfile *dpo_backlink
5200 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5201 dwarf2_objfile_data_key);
5202
5203 dwarf2_per_objfile->has_section_at_zero
5204 = dpo_backlink->has_section_at_zero;
5205 }
5206
5207 dwarf2_per_objfile->reading_partial_symbols = 0;
5208
5209 psymtab_to_symtab_1 (pst);
5210
5211 /* Finish up the debug error message. */
5212 if (info_verbose)
5213 printf_filtered (_("done.\n"));
5214 }
5215 }
5216
5217 process_cu_includes ();
5218 }
5219 \f
5220 /* Reading in full CUs. */
5221
5222 /* Add PER_CU to the queue. */
5223
5224 static void
5225 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5226 enum language pretend_language)
5227 {
5228 struct dwarf2_queue_item *item;
5229
5230 per_cu->queued = 1;
5231 item = xmalloc (sizeof (*item));
5232 item->per_cu = per_cu;
5233 item->pretend_language = pretend_language;
5234 item->next = NULL;
5235
5236 if (dwarf2_queue == NULL)
5237 dwarf2_queue = item;
5238 else
5239 dwarf2_queue_tail->next = item;
5240
5241 dwarf2_queue_tail = item;
5242 }
5243
5244 /* Process the queue. */
5245
5246 static void
5247 process_queue (void)
5248 {
5249 struct dwarf2_queue_item *item, *next_item;
5250
5251 /* The queue starts out with one item, but following a DIE reference
5252 may load a new CU, adding it to the end of the queue. */
5253 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5254 {
5255 if (dwarf2_per_objfile->using_index
5256 ? !item->per_cu->v.quick->symtab
5257 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5258 process_full_comp_unit (item->per_cu, item->pretend_language);
5259
5260 item->per_cu->queued = 0;
5261 next_item = item->next;
5262 xfree (item);
5263 }
5264
5265 dwarf2_queue_tail = NULL;
5266 }
5267
5268 /* Free all allocated queue entries. This function only releases anything if
5269 an error was thrown; if the queue was processed then it would have been
5270 freed as we went along. */
5271
5272 static void
5273 dwarf2_release_queue (void *dummy)
5274 {
5275 struct dwarf2_queue_item *item, *last;
5276
5277 item = dwarf2_queue;
5278 while (item)
5279 {
5280 /* Anything still marked queued is likely to be in an
5281 inconsistent state, so discard it. */
5282 if (item->per_cu->queued)
5283 {
5284 if (item->per_cu->cu != NULL)
5285 free_one_cached_comp_unit (item->per_cu);
5286 item->per_cu->queued = 0;
5287 }
5288
5289 last = item;
5290 item = item->next;
5291 xfree (last);
5292 }
5293
5294 dwarf2_queue = dwarf2_queue_tail = NULL;
5295 }
5296
5297 /* Read in full symbols for PST, and anything it depends on. */
5298
5299 static void
5300 psymtab_to_symtab_1 (struct partial_symtab *pst)
5301 {
5302 struct dwarf2_per_cu_data *per_cu;
5303 int i;
5304
5305 if (pst->readin)
5306 return;
5307
5308 for (i = 0; i < pst->number_of_dependencies; i++)
5309 if (!pst->dependencies[i]->readin
5310 && pst->dependencies[i]->user == NULL)
5311 {
5312 /* Inform about additional files that need to be read in. */
5313 if (info_verbose)
5314 {
5315 /* FIXME: i18n: Need to make this a single string. */
5316 fputs_filtered (" ", gdb_stdout);
5317 wrap_here ("");
5318 fputs_filtered ("and ", gdb_stdout);
5319 wrap_here ("");
5320 printf_filtered ("%s...", pst->dependencies[i]->filename);
5321 wrap_here (""); /* Flush output. */
5322 gdb_flush (gdb_stdout);
5323 }
5324 psymtab_to_symtab_1 (pst->dependencies[i]);
5325 }
5326
5327 per_cu = pst->read_symtab_private;
5328
5329 if (per_cu == NULL)
5330 {
5331 /* It's an include file, no symbols to read for it.
5332 Everything is in the parent symtab. */
5333 pst->readin = 1;
5334 return;
5335 }
5336
5337 dw2_do_instantiate_symtab (per_cu);
5338 }
5339
5340 /* Trivial hash function for die_info: the hash value of a DIE
5341 is its offset in .debug_info for this objfile. */
5342
5343 static hashval_t
5344 die_hash (const void *item)
5345 {
5346 const struct die_info *die = item;
5347
5348 return die->offset.sect_off;
5349 }
5350
5351 /* Trivial comparison function for die_info structures: two DIEs
5352 are equal if they have the same offset. */
5353
5354 static int
5355 die_eq (const void *item_lhs, const void *item_rhs)
5356 {
5357 const struct die_info *die_lhs = item_lhs;
5358 const struct die_info *die_rhs = item_rhs;
5359
5360 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5361 }
5362
5363 /* die_reader_func for load_full_comp_unit.
5364 This is identical to read_signatured_type_reader,
5365 but is kept separate for now. */
5366
5367 static void
5368 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5369 gdb_byte *info_ptr,
5370 struct die_info *comp_unit_die,
5371 int has_children,
5372 void *data)
5373 {
5374 struct dwarf2_cu *cu = reader->cu;
5375 enum language *language_ptr = data;
5376
5377 gdb_assert (cu->die_hash == NULL);
5378 cu->die_hash =
5379 htab_create_alloc_ex (cu->header.length / 12,
5380 die_hash,
5381 die_eq,
5382 NULL,
5383 &cu->comp_unit_obstack,
5384 hashtab_obstack_allocate,
5385 dummy_obstack_deallocate);
5386
5387 if (has_children)
5388 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5389 &info_ptr, comp_unit_die);
5390 cu->dies = comp_unit_die;
5391 /* comp_unit_die is not stored in die_hash, no need. */
5392
5393 /* We try not to read any attributes in this function, because not
5394 all CUs needed for references have been loaded yet, and symbol
5395 table processing isn't initialized. But we have to set the CU language,
5396 or we won't be able to build types correctly.
5397 Similarly, if we do not read the producer, we can not apply
5398 producer-specific interpretation. */
5399 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
5400 }
5401
5402 /* Load the DIEs associated with PER_CU into memory. */
5403
5404 static void
5405 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5406 enum language pretend_language)
5407 {
5408 gdb_assert (! this_cu->is_debug_types);
5409
5410 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5411 &pretend_language);
5412 }
5413
5414 /* Add a DIE to the delayed physname list. */
5415
5416 static void
5417 add_to_method_list (struct type *type, int fnfield_index, int index,
5418 const char *name, struct die_info *die,
5419 struct dwarf2_cu *cu)
5420 {
5421 struct delayed_method_info mi;
5422 mi.type = type;
5423 mi.fnfield_index = fnfield_index;
5424 mi.index = index;
5425 mi.name = name;
5426 mi.die = die;
5427 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5428 }
5429
5430 /* A cleanup for freeing the delayed method list. */
5431
5432 static void
5433 free_delayed_list (void *ptr)
5434 {
5435 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5436 if (cu->method_list != NULL)
5437 {
5438 VEC_free (delayed_method_info, cu->method_list);
5439 cu->method_list = NULL;
5440 }
5441 }
5442
5443 /* Compute the physnames of any methods on the CU's method list.
5444
5445 The computation of method physnames is delayed in order to avoid the
5446 (bad) condition that one of the method's formal parameters is of an as yet
5447 incomplete type. */
5448
5449 static void
5450 compute_delayed_physnames (struct dwarf2_cu *cu)
5451 {
5452 int i;
5453 struct delayed_method_info *mi;
5454 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5455 {
5456 const char *physname;
5457 struct fn_fieldlist *fn_flp
5458 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5459 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5460 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5461 }
5462 }
5463
5464 /* Go objects should be embedded in a DW_TAG_module DIE,
5465 and it's not clear if/how imported objects will appear.
5466 To keep Go support simple until that's worked out,
5467 go back through what we've read and create something usable.
5468 We could do this while processing each DIE, and feels kinda cleaner,
5469 but that way is more invasive.
5470 This is to, for example, allow the user to type "p var" or "b main"
5471 without having to specify the package name, and allow lookups
5472 of module.object to work in contexts that use the expression
5473 parser. */
5474
5475 static void
5476 fixup_go_packaging (struct dwarf2_cu *cu)
5477 {
5478 char *package_name = NULL;
5479 struct pending *list;
5480 int i;
5481
5482 for (list = global_symbols; list != NULL; list = list->next)
5483 {
5484 for (i = 0; i < list->nsyms; ++i)
5485 {
5486 struct symbol *sym = list->symbol[i];
5487
5488 if (SYMBOL_LANGUAGE (sym) == language_go
5489 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5490 {
5491 char *this_package_name = go_symbol_package_name (sym);
5492
5493 if (this_package_name == NULL)
5494 continue;
5495 if (package_name == NULL)
5496 package_name = this_package_name;
5497 else
5498 {
5499 if (strcmp (package_name, this_package_name) != 0)
5500 complaint (&symfile_complaints,
5501 _("Symtab %s has objects from two different Go packages: %s and %s"),
5502 (sym->symtab && sym->symtab->filename
5503 ? sym->symtab->filename
5504 : cu->objfile->name),
5505 this_package_name, package_name);
5506 xfree (this_package_name);
5507 }
5508 }
5509 }
5510 }
5511
5512 if (package_name != NULL)
5513 {
5514 struct objfile *objfile = cu->objfile;
5515 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5516 package_name, objfile);
5517 struct symbol *sym;
5518
5519 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5520
5521 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5522 SYMBOL_SET_LANGUAGE (sym, language_go);
5523 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5524 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5525 e.g., "main" finds the "main" module and not C's main(). */
5526 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5527 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5528 SYMBOL_TYPE (sym) = type;
5529
5530 add_symbol_to_list (sym, &global_symbols);
5531
5532 xfree (package_name);
5533 }
5534 }
5535
5536 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5537
5538 /* Return the symtab for PER_CU. This works properly regardless of
5539 whether we're using the index or psymtabs. */
5540
5541 static struct symtab *
5542 get_symtab (struct dwarf2_per_cu_data *per_cu)
5543 {
5544 return (dwarf2_per_objfile->using_index
5545 ? per_cu->v.quick->symtab
5546 : per_cu->v.psymtab->symtab);
5547 }
5548
5549 /* A helper function for computing the list of all symbol tables
5550 included by PER_CU. */
5551
5552 static void
5553 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5554 htab_t all_children,
5555 struct dwarf2_per_cu_data *per_cu)
5556 {
5557 void **slot;
5558 int ix;
5559 struct dwarf2_per_cu_data *iter;
5560
5561 slot = htab_find_slot (all_children, per_cu, INSERT);
5562 if (*slot != NULL)
5563 {
5564 /* This inclusion and its children have been processed. */
5565 return;
5566 }
5567
5568 *slot = per_cu;
5569 /* Only add a CU if it has a symbol table. */
5570 if (get_symtab (per_cu) != NULL)
5571 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5572
5573 for (ix = 0;
5574 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5575 ++ix)
5576 recursively_compute_inclusions (result, all_children, iter);
5577 }
5578
5579 /* Compute the symtab 'includes' fields for the symtab related to
5580 PER_CU. */
5581
5582 static void
5583 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5584 {
5585 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5586 {
5587 int ix, len;
5588 struct dwarf2_per_cu_data *iter;
5589 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5590 htab_t all_children;
5591 struct symtab *symtab = get_symtab (per_cu);
5592
5593 /* If we don't have a symtab, we can just skip this case. */
5594 if (symtab == NULL)
5595 return;
5596
5597 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5598 NULL, xcalloc, xfree);
5599
5600 for (ix = 0;
5601 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5602 ix, iter);
5603 ++ix)
5604 recursively_compute_inclusions (&result_children, all_children, iter);
5605
5606 /* Now we have a transitive closure of all the included CUs, so
5607 we can convert it to a list of symtabs. */
5608 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5609 symtab->includes
5610 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5611 (len + 1) * sizeof (struct symtab *));
5612 for (ix = 0;
5613 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5614 ++ix)
5615 symtab->includes[ix] = get_symtab (iter);
5616 symtab->includes[len] = NULL;
5617
5618 VEC_free (dwarf2_per_cu_ptr, result_children);
5619 htab_delete (all_children);
5620 }
5621 }
5622
5623 /* Compute the 'includes' field for the symtabs of all the CUs we just
5624 read. */
5625
5626 static void
5627 process_cu_includes (void)
5628 {
5629 int ix;
5630 struct dwarf2_per_cu_data *iter;
5631
5632 for (ix = 0;
5633 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5634 ix, iter);
5635 ++ix)
5636 compute_symtab_includes (iter);
5637
5638 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5639 }
5640
5641 /* Generate full symbol information for PER_CU, whose DIEs have
5642 already been loaded into memory. */
5643
5644 static void
5645 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5646 enum language pretend_language)
5647 {
5648 struct dwarf2_cu *cu = per_cu->cu;
5649 struct objfile *objfile = per_cu->objfile;
5650 CORE_ADDR lowpc, highpc;
5651 struct symtab *symtab;
5652 struct cleanup *back_to, *delayed_list_cleanup;
5653 CORE_ADDR baseaddr;
5654
5655 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5656
5657 buildsym_init ();
5658 back_to = make_cleanup (really_free_pendings, NULL);
5659 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5660
5661 cu->list_in_scope = &file_symbols;
5662
5663 cu->language = pretend_language;
5664 cu->language_defn = language_def (cu->language);
5665
5666 /* Do line number decoding in read_file_scope () */
5667 process_die (cu->dies, cu);
5668
5669 /* For now fudge the Go package. */
5670 if (cu->language == language_go)
5671 fixup_go_packaging (cu);
5672
5673 /* Now that we have processed all the DIEs in the CU, all the types
5674 should be complete, and it should now be safe to compute all of the
5675 physnames. */
5676 compute_delayed_physnames (cu);
5677 do_cleanups (delayed_list_cleanup);
5678
5679 /* Some compilers don't define a DW_AT_high_pc attribute for the
5680 compilation unit. If the DW_AT_high_pc is missing, synthesize
5681 it, by scanning the DIE's below the compilation unit. */
5682 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5683
5684 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5685
5686 if (symtab != NULL)
5687 {
5688 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5689
5690 /* Set symtab language to language from DW_AT_language. If the
5691 compilation is from a C file generated by language preprocessors, do
5692 not set the language if it was already deduced by start_subfile. */
5693 if (!(cu->language == language_c && symtab->language != language_c))
5694 symtab->language = cu->language;
5695
5696 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5697 produce DW_AT_location with location lists but it can be possibly
5698 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5699 there were bugs in prologue debug info, fixed later in GCC-4.5
5700 by "unwind info for epilogues" patch (which is not directly related).
5701
5702 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5703 needed, it would be wrong due to missing DW_AT_producer there.
5704
5705 Still one can confuse GDB by using non-standard GCC compilation
5706 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5707 */
5708 if (cu->has_loclist && gcc_4_minor >= 5)
5709 symtab->locations_valid = 1;
5710
5711 if (gcc_4_minor >= 5)
5712 symtab->epilogue_unwind_valid = 1;
5713
5714 symtab->call_site_htab = cu->call_site_htab;
5715 }
5716
5717 if (dwarf2_per_objfile->using_index)
5718 per_cu->v.quick->symtab = symtab;
5719 else
5720 {
5721 struct partial_symtab *pst = per_cu->v.psymtab;
5722 pst->symtab = symtab;
5723 pst->readin = 1;
5724 }
5725
5726 /* Push it for inclusion processing later. */
5727 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5728
5729 do_cleanups (back_to);
5730 }
5731
5732 /* Process an imported unit DIE. */
5733
5734 static void
5735 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5736 {
5737 struct attribute *attr;
5738
5739 attr = dwarf2_attr (die, DW_AT_import, cu);
5740 if (attr != NULL)
5741 {
5742 struct dwarf2_per_cu_data *per_cu;
5743 struct symtab *imported_symtab;
5744 sect_offset offset;
5745
5746 offset = dwarf2_get_ref_die_offset (attr);
5747 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5748
5749 /* Queue the unit, if needed. */
5750 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5751 load_full_comp_unit (per_cu, cu->language);
5752
5753 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5754 per_cu);
5755 }
5756 }
5757
5758 /* Process a die and its children. */
5759
5760 static void
5761 process_die (struct die_info *die, struct dwarf2_cu *cu)
5762 {
5763 switch (die->tag)
5764 {
5765 case DW_TAG_padding:
5766 break;
5767 case DW_TAG_compile_unit:
5768 case DW_TAG_partial_unit:
5769 read_file_scope (die, cu);
5770 break;
5771 case DW_TAG_type_unit:
5772 read_type_unit_scope (die, cu);
5773 break;
5774 case DW_TAG_subprogram:
5775 case DW_TAG_inlined_subroutine:
5776 read_func_scope (die, cu);
5777 break;
5778 case DW_TAG_lexical_block:
5779 case DW_TAG_try_block:
5780 case DW_TAG_catch_block:
5781 read_lexical_block_scope (die, cu);
5782 break;
5783 case DW_TAG_GNU_call_site:
5784 read_call_site_scope (die, cu);
5785 break;
5786 case DW_TAG_class_type:
5787 case DW_TAG_interface_type:
5788 case DW_TAG_structure_type:
5789 case DW_TAG_union_type:
5790 process_structure_scope (die, cu);
5791 break;
5792 case DW_TAG_enumeration_type:
5793 process_enumeration_scope (die, cu);
5794 break;
5795
5796 /* These dies have a type, but processing them does not create
5797 a symbol or recurse to process the children. Therefore we can
5798 read them on-demand through read_type_die. */
5799 case DW_TAG_subroutine_type:
5800 case DW_TAG_set_type:
5801 case DW_TAG_array_type:
5802 case DW_TAG_pointer_type:
5803 case DW_TAG_ptr_to_member_type:
5804 case DW_TAG_reference_type:
5805 case DW_TAG_string_type:
5806 break;
5807
5808 case DW_TAG_base_type:
5809 case DW_TAG_subrange_type:
5810 case DW_TAG_typedef:
5811 /* Add a typedef symbol for the type definition, if it has a
5812 DW_AT_name. */
5813 new_symbol (die, read_type_die (die, cu), cu);
5814 break;
5815 case DW_TAG_common_block:
5816 read_common_block (die, cu);
5817 break;
5818 case DW_TAG_common_inclusion:
5819 break;
5820 case DW_TAG_namespace:
5821 processing_has_namespace_info = 1;
5822 read_namespace (die, cu);
5823 break;
5824 case DW_TAG_module:
5825 processing_has_namespace_info = 1;
5826 read_module (die, cu);
5827 break;
5828 case DW_TAG_imported_declaration:
5829 case DW_TAG_imported_module:
5830 processing_has_namespace_info = 1;
5831 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5832 || cu->language != language_fortran))
5833 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5834 dwarf_tag_name (die->tag));
5835 read_import_statement (die, cu);
5836 break;
5837
5838 case DW_TAG_imported_unit:
5839 process_imported_unit_die (die, cu);
5840 break;
5841
5842 default:
5843 new_symbol (die, NULL, cu);
5844 break;
5845 }
5846 }
5847
5848 /* A helper function for dwarf2_compute_name which determines whether DIE
5849 needs to have the name of the scope prepended to the name listed in the
5850 die. */
5851
5852 static int
5853 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5854 {
5855 struct attribute *attr;
5856
5857 switch (die->tag)
5858 {
5859 case DW_TAG_namespace:
5860 case DW_TAG_typedef:
5861 case DW_TAG_class_type:
5862 case DW_TAG_interface_type:
5863 case DW_TAG_structure_type:
5864 case DW_TAG_union_type:
5865 case DW_TAG_enumeration_type:
5866 case DW_TAG_enumerator:
5867 case DW_TAG_subprogram:
5868 case DW_TAG_member:
5869 return 1;
5870
5871 case DW_TAG_variable:
5872 case DW_TAG_constant:
5873 /* We only need to prefix "globally" visible variables. These include
5874 any variable marked with DW_AT_external or any variable that
5875 lives in a namespace. [Variables in anonymous namespaces
5876 require prefixing, but they are not DW_AT_external.] */
5877
5878 if (dwarf2_attr (die, DW_AT_specification, cu))
5879 {
5880 struct dwarf2_cu *spec_cu = cu;
5881
5882 return die_needs_namespace (die_specification (die, &spec_cu),
5883 spec_cu);
5884 }
5885
5886 attr = dwarf2_attr (die, DW_AT_external, cu);
5887 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5888 && die->parent->tag != DW_TAG_module)
5889 return 0;
5890 /* A variable in a lexical block of some kind does not need a
5891 namespace, even though in C++ such variables may be external
5892 and have a mangled name. */
5893 if (die->parent->tag == DW_TAG_lexical_block
5894 || die->parent->tag == DW_TAG_try_block
5895 || die->parent->tag == DW_TAG_catch_block
5896 || die->parent->tag == DW_TAG_subprogram)
5897 return 0;
5898 return 1;
5899
5900 default:
5901 return 0;
5902 }
5903 }
5904
5905 /* Retrieve the last character from a mem_file. */
5906
5907 static void
5908 do_ui_file_peek_last (void *object, const char *buffer, long length)
5909 {
5910 char *last_char_p = (char *) object;
5911
5912 if (length > 0)
5913 *last_char_p = buffer[length - 1];
5914 }
5915
5916 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5917 compute the physname for the object, which include a method's:
5918 - formal parameters (C++/Java),
5919 - receiver type (Go),
5920 - return type (Java).
5921
5922 The term "physname" is a bit confusing.
5923 For C++, for example, it is the demangled name.
5924 For Go, for example, it's the mangled name.
5925
5926 For Ada, return the DIE's linkage name rather than the fully qualified
5927 name. PHYSNAME is ignored..
5928
5929 The result is allocated on the objfile_obstack and canonicalized. */
5930
5931 static const char *
5932 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5933 int physname)
5934 {
5935 struct objfile *objfile = cu->objfile;
5936
5937 if (name == NULL)
5938 name = dwarf2_name (die, cu);
5939
5940 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5941 compute it by typename_concat inside GDB. */
5942 if (cu->language == language_ada
5943 || (cu->language == language_fortran && physname))
5944 {
5945 /* For Ada unit, we prefer the linkage name over the name, as
5946 the former contains the exported name, which the user expects
5947 to be able to reference. Ideally, we want the user to be able
5948 to reference this entity using either natural or linkage name,
5949 but we haven't started looking at this enhancement yet. */
5950 struct attribute *attr;
5951
5952 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5953 if (attr == NULL)
5954 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5955 if (attr && DW_STRING (attr))
5956 return DW_STRING (attr);
5957 }
5958
5959 /* These are the only languages we know how to qualify names in. */
5960 if (name != NULL
5961 && (cu->language == language_cplus || cu->language == language_java
5962 || cu->language == language_fortran))
5963 {
5964 if (die_needs_namespace (die, cu))
5965 {
5966 long length;
5967 const char *prefix;
5968 struct ui_file *buf;
5969
5970 prefix = determine_prefix (die, cu);
5971 buf = mem_fileopen ();
5972 if (*prefix != '\0')
5973 {
5974 char *prefixed_name = typename_concat (NULL, prefix, name,
5975 physname, cu);
5976
5977 fputs_unfiltered (prefixed_name, buf);
5978 xfree (prefixed_name);
5979 }
5980 else
5981 fputs_unfiltered (name, buf);
5982
5983 /* Template parameters may be specified in the DIE's DW_AT_name, or
5984 as children with DW_TAG_template_type_param or
5985 DW_TAG_value_type_param. If the latter, add them to the name
5986 here. If the name already has template parameters, then
5987 skip this step; some versions of GCC emit both, and
5988 it is more efficient to use the pre-computed name.
5989
5990 Something to keep in mind about this process: it is very
5991 unlikely, or in some cases downright impossible, to produce
5992 something that will match the mangled name of a function.
5993 If the definition of the function has the same debug info,
5994 we should be able to match up with it anyway. But fallbacks
5995 using the minimal symbol, for instance to find a method
5996 implemented in a stripped copy of libstdc++, will not work.
5997 If we do not have debug info for the definition, we will have to
5998 match them up some other way.
5999
6000 When we do name matching there is a related problem with function
6001 templates; two instantiated function templates are allowed to
6002 differ only by their return types, which we do not add here. */
6003
6004 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6005 {
6006 struct attribute *attr;
6007 struct die_info *child;
6008 int first = 1;
6009
6010 die->building_fullname = 1;
6011
6012 for (child = die->child; child != NULL; child = child->sibling)
6013 {
6014 struct type *type;
6015 LONGEST value;
6016 gdb_byte *bytes;
6017 struct dwarf2_locexpr_baton *baton;
6018 struct value *v;
6019
6020 if (child->tag != DW_TAG_template_type_param
6021 && child->tag != DW_TAG_template_value_param)
6022 continue;
6023
6024 if (first)
6025 {
6026 fputs_unfiltered ("<", buf);
6027 first = 0;
6028 }
6029 else
6030 fputs_unfiltered (", ", buf);
6031
6032 attr = dwarf2_attr (child, DW_AT_type, cu);
6033 if (attr == NULL)
6034 {
6035 complaint (&symfile_complaints,
6036 _("template parameter missing DW_AT_type"));
6037 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6038 continue;
6039 }
6040 type = die_type (child, cu);
6041
6042 if (child->tag == DW_TAG_template_type_param)
6043 {
6044 c_print_type (type, "", buf, -1, 0);
6045 continue;
6046 }
6047
6048 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6049 if (attr == NULL)
6050 {
6051 complaint (&symfile_complaints,
6052 _("template parameter missing "
6053 "DW_AT_const_value"));
6054 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6055 continue;
6056 }
6057
6058 dwarf2_const_value_attr (attr, type, name,
6059 &cu->comp_unit_obstack, cu,
6060 &value, &bytes, &baton);
6061
6062 if (TYPE_NOSIGN (type))
6063 /* GDB prints characters as NUMBER 'CHAR'. If that's
6064 changed, this can use value_print instead. */
6065 c_printchar (value, type, buf);
6066 else
6067 {
6068 struct value_print_options opts;
6069
6070 if (baton != NULL)
6071 v = dwarf2_evaluate_loc_desc (type, NULL,
6072 baton->data,
6073 baton->size,
6074 baton->per_cu);
6075 else if (bytes != NULL)
6076 {
6077 v = allocate_value (type);
6078 memcpy (value_contents_writeable (v), bytes,
6079 TYPE_LENGTH (type));
6080 }
6081 else
6082 v = value_from_longest (type, value);
6083
6084 /* Specify decimal so that we do not depend on
6085 the radix. */
6086 get_formatted_print_options (&opts, 'd');
6087 opts.raw = 1;
6088 value_print (v, buf, &opts);
6089 release_value (v);
6090 value_free (v);
6091 }
6092 }
6093
6094 die->building_fullname = 0;
6095
6096 if (!first)
6097 {
6098 /* Close the argument list, with a space if necessary
6099 (nested templates). */
6100 char last_char = '\0';
6101 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6102 if (last_char == '>')
6103 fputs_unfiltered (" >", buf);
6104 else
6105 fputs_unfiltered (">", buf);
6106 }
6107 }
6108
6109 /* For Java and C++ methods, append formal parameter type
6110 information, if PHYSNAME. */
6111
6112 if (physname && die->tag == DW_TAG_subprogram
6113 && (cu->language == language_cplus
6114 || cu->language == language_java))
6115 {
6116 struct type *type = read_type_die (die, cu);
6117
6118 c_type_print_args (type, buf, 1, cu->language);
6119
6120 if (cu->language == language_java)
6121 {
6122 /* For java, we must append the return type to method
6123 names. */
6124 if (die->tag == DW_TAG_subprogram)
6125 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6126 0, 0);
6127 }
6128 else if (cu->language == language_cplus)
6129 {
6130 /* Assume that an artificial first parameter is
6131 "this", but do not crash if it is not. RealView
6132 marks unnamed (and thus unused) parameters as
6133 artificial; there is no way to differentiate
6134 the two cases. */
6135 if (TYPE_NFIELDS (type) > 0
6136 && TYPE_FIELD_ARTIFICIAL (type, 0)
6137 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
6138 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6139 0))))
6140 fputs_unfiltered (" const", buf);
6141 }
6142 }
6143
6144 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
6145 &length);
6146 ui_file_delete (buf);
6147
6148 if (cu->language == language_cplus)
6149 {
6150 char *cname
6151 = dwarf2_canonicalize_name (name, cu,
6152 &objfile->objfile_obstack);
6153
6154 if (cname != NULL)
6155 name = cname;
6156 }
6157 }
6158 }
6159
6160 return name;
6161 }
6162
6163 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6164 If scope qualifiers are appropriate they will be added. The result
6165 will be allocated on the objfile_obstack, or NULL if the DIE does
6166 not have a name. NAME may either be from a previous call to
6167 dwarf2_name or NULL.
6168
6169 The output string will be canonicalized (if C++/Java). */
6170
6171 static const char *
6172 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
6173 {
6174 return dwarf2_compute_name (name, die, cu, 0);
6175 }
6176
6177 /* Construct a physname for the given DIE in CU. NAME may either be
6178 from a previous call to dwarf2_name or NULL. The result will be
6179 allocated on the objfile_objstack or NULL if the DIE does not have a
6180 name.
6181
6182 The output string will be canonicalized (if C++/Java). */
6183
6184 static const char *
6185 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6186 {
6187 struct objfile *objfile = cu->objfile;
6188 struct attribute *attr;
6189 const char *retval, *mangled = NULL, *canon = NULL;
6190 struct cleanup *back_to;
6191 int need_copy = 1;
6192
6193 /* In this case dwarf2_compute_name is just a shortcut not building anything
6194 on its own. */
6195 if (!die_needs_namespace (die, cu))
6196 return dwarf2_compute_name (name, die, cu, 1);
6197
6198 back_to = make_cleanup (null_cleanup, NULL);
6199
6200 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6201 if (!attr)
6202 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6203
6204 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6205 has computed. */
6206 if (attr && DW_STRING (attr))
6207 {
6208 char *demangled;
6209
6210 mangled = DW_STRING (attr);
6211
6212 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6213 type. It is easier for GDB users to search for such functions as
6214 `name(params)' than `long name(params)'. In such case the minimal
6215 symbol names do not match the full symbol names but for template
6216 functions there is never a need to look up their definition from their
6217 declaration so the only disadvantage remains the minimal symbol
6218 variant `long name(params)' does not have the proper inferior type.
6219 */
6220
6221 if (cu->language == language_go)
6222 {
6223 /* This is a lie, but we already lie to the caller new_symbol_full.
6224 new_symbol_full assumes we return the mangled name.
6225 This just undoes that lie until things are cleaned up. */
6226 demangled = NULL;
6227 }
6228 else
6229 {
6230 demangled = cplus_demangle (mangled,
6231 (DMGL_PARAMS | DMGL_ANSI
6232 | (cu->language == language_java
6233 ? DMGL_JAVA | DMGL_RET_POSTFIX
6234 : DMGL_RET_DROP)));
6235 }
6236 if (demangled)
6237 {
6238 make_cleanup (xfree, demangled);
6239 canon = demangled;
6240 }
6241 else
6242 {
6243 canon = mangled;
6244 need_copy = 0;
6245 }
6246 }
6247
6248 if (canon == NULL || check_physname)
6249 {
6250 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6251
6252 if (canon != NULL && strcmp (physname, canon) != 0)
6253 {
6254 /* It may not mean a bug in GDB. The compiler could also
6255 compute DW_AT_linkage_name incorrectly. But in such case
6256 GDB would need to be bug-to-bug compatible. */
6257
6258 complaint (&symfile_complaints,
6259 _("Computed physname <%s> does not match demangled <%s> "
6260 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6261 physname, canon, mangled, die->offset.sect_off, objfile->name);
6262
6263 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6264 is available here - over computed PHYSNAME. It is safer
6265 against both buggy GDB and buggy compilers. */
6266
6267 retval = canon;
6268 }
6269 else
6270 {
6271 retval = physname;
6272 need_copy = 0;
6273 }
6274 }
6275 else
6276 retval = canon;
6277
6278 if (need_copy)
6279 retval = obsavestring (retval, strlen (retval),
6280 &objfile->objfile_obstack);
6281
6282 do_cleanups (back_to);
6283 return retval;
6284 }
6285
6286 /* Read the import statement specified by the given die and record it. */
6287
6288 static void
6289 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6290 {
6291 struct objfile *objfile = cu->objfile;
6292 struct attribute *import_attr;
6293 struct die_info *imported_die, *child_die;
6294 struct dwarf2_cu *imported_cu;
6295 const char *imported_name;
6296 const char *imported_name_prefix;
6297 const char *canonical_name;
6298 const char *import_alias;
6299 const char *imported_declaration = NULL;
6300 const char *import_prefix;
6301 VEC (const_char_ptr) *excludes = NULL;
6302 struct cleanup *cleanups;
6303
6304 char *temp;
6305
6306 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6307 if (import_attr == NULL)
6308 {
6309 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6310 dwarf_tag_name (die->tag));
6311 return;
6312 }
6313
6314 imported_cu = cu;
6315 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6316 imported_name = dwarf2_name (imported_die, imported_cu);
6317 if (imported_name == NULL)
6318 {
6319 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6320
6321 The import in the following code:
6322 namespace A
6323 {
6324 typedef int B;
6325 }
6326
6327 int main ()
6328 {
6329 using A::B;
6330 B b;
6331 return b;
6332 }
6333
6334 ...
6335 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6336 <52> DW_AT_decl_file : 1
6337 <53> DW_AT_decl_line : 6
6338 <54> DW_AT_import : <0x75>
6339 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6340 <59> DW_AT_name : B
6341 <5b> DW_AT_decl_file : 1
6342 <5c> DW_AT_decl_line : 2
6343 <5d> DW_AT_type : <0x6e>
6344 ...
6345 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6346 <76> DW_AT_byte_size : 4
6347 <77> DW_AT_encoding : 5 (signed)
6348
6349 imports the wrong die ( 0x75 instead of 0x58 ).
6350 This case will be ignored until the gcc bug is fixed. */
6351 return;
6352 }
6353
6354 /* Figure out the local name after import. */
6355 import_alias = dwarf2_name (die, cu);
6356
6357 /* Figure out where the statement is being imported to. */
6358 import_prefix = determine_prefix (die, cu);
6359
6360 /* Figure out what the scope of the imported die is and prepend it
6361 to the name of the imported die. */
6362 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6363
6364 if (imported_die->tag != DW_TAG_namespace
6365 && imported_die->tag != DW_TAG_module)
6366 {
6367 imported_declaration = imported_name;
6368 canonical_name = imported_name_prefix;
6369 }
6370 else if (strlen (imported_name_prefix) > 0)
6371 {
6372 temp = alloca (strlen (imported_name_prefix)
6373 + 2 + strlen (imported_name) + 1);
6374 strcpy (temp, imported_name_prefix);
6375 strcat (temp, "::");
6376 strcat (temp, imported_name);
6377 canonical_name = temp;
6378 }
6379 else
6380 canonical_name = imported_name;
6381
6382 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6383
6384 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6385 for (child_die = die->child; child_die && child_die->tag;
6386 child_die = sibling_die (child_die))
6387 {
6388 /* DWARF-4: A Fortran use statement with a “rename list” may be
6389 represented by an imported module entry with an import attribute
6390 referring to the module and owned entries corresponding to those
6391 entities that are renamed as part of being imported. */
6392
6393 if (child_die->tag != DW_TAG_imported_declaration)
6394 {
6395 complaint (&symfile_complaints,
6396 _("child DW_TAG_imported_declaration expected "
6397 "- DIE at 0x%x [in module %s]"),
6398 child_die->offset.sect_off, objfile->name);
6399 continue;
6400 }
6401
6402 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6403 if (import_attr == NULL)
6404 {
6405 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6406 dwarf_tag_name (child_die->tag));
6407 continue;
6408 }
6409
6410 imported_cu = cu;
6411 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6412 &imported_cu);
6413 imported_name = dwarf2_name (imported_die, imported_cu);
6414 if (imported_name == NULL)
6415 {
6416 complaint (&symfile_complaints,
6417 _("child DW_TAG_imported_declaration has unknown "
6418 "imported name - DIE at 0x%x [in module %s]"),
6419 child_die->offset.sect_off, objfile->name);
6420 continue;
6421 }
6422
6423 VEC_safe_push (const_char_ptr, excludes, imported_name);
6424
6425 process_die (child_die, cu);
6426 }
6427
6428 cp_add_using_directive (import_prefix,
6429 canonical_name,
6430 import_alias,
6431 imported_declaration,
6432 excludes,
6433 &objfile->objfile_obstack);
6434
6435 do_cleanups (cleanups);
6436 }
6437
6438 /* Cleanup function for read_file_scope. */
6439
6440 static void
6441 free_cu_line_header (void *arg)
6442 {
6443 struct dwarf2_cu *cu = arg;
6444
6445 free_line_header (cu->line_header);
6446 cu->line_header = NULL;
6447 }
6448
6449 static void
6450 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6451 char **name, char **comp_dir)
6452 {
6453 struct attribute *attr;
6454
6455 *name = NULL;
6456 *comp_dir = NULL;
6457
6458 /* Find the filename. Do not use dwarf2_name here, since the filename
6459 is not a source language identifier. */
6460 attr = dwarf2_attr (die, DW_AT_name, cu);
6461 if (attr)
6462 {
6463 *name = DW_STRING (attr);
6464 }
6465
6466 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6467 if (attr)
6468 *comp_dir = DW_STRING (attr);
6469 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6470 {
6471 *comp_dir = ldirname (*name);
6472 if (*comp_dir != NULL)
6473 make_cleanup (xfree, *comp_dir);
6474 }
6475 if (*comp_dir != NULL)
6476 {
6477 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6478 directory, get rid of it. */
6479 char *cp = strchr (*comp_dir, ':');
6480
6481 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6482 *comp_dir = cp + 1;
6483 }
6484
6485 if (*name == NULL)
6486 *name = "<unknown>";
6487 }
6488
6489 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6490 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6491 COMP_DIR is the compilation directory.
6492 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6493
6494 static void
6495 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6496 const char *comp_dir, int want_line_info)
6497 {
6498 struct attribute *attr;
6499
6500 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6501 if (attr)
6502 {
6503 unsigned int line_offset = DW_UNSND (attr);
6504 struct line_header *line_header
6505 = dwarf_decode_line_header (line_offset, cu);
6506
6507 if (line_header)
6508 {
6509 cu->line_header = line_header;
6510 make_cleanup (free_cu_line_header, cu);
6511 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6512 }
6513 }
6514 }
6515
6516 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6517
6518 static void
6519 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6520 {
6521 struct objfile *objfile = dwarf2_per_objfile->objfile;
6522 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6523 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6524 CORE_ADDR highpc = ((CORE_ADDR) 0);
6525 struct attribute *attr;
6526 char *name = NULL;
6527 char *comp_dir = NULL;
6528 struct die_info *child_die;
6529 bfd *abfd = objfile->obfd;
6530 CORE_ADDR baseaddr;
6531
6532 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6533
6534 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6535
6536 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6537 from finish_block. */
6538 if (lowpc == ((CORE_ADDR) -1))
6539 lowpc = highpc;
6540 lowpc += baseaddr;
6541 highpc += baseaddr;
6542
6543 find_file_and_directory (die, cu, &name, &comp_dir);
6544
6545 prepare_one_comp_unit (cu, die, cu->language);
6546
6547 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6548 standardised yet. As a workaround for the language detection we fall
6549 back to the DW_AT_producer string. */
6550 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6551 cu->language = language_opencl;
6552
6553 /* Similar hack for Go. */
6554 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6555 set_cu_language (DW_LANG_Go, cu);
6556
6557 /* We assume that we're processing GCC output. */
6558 processing_gcc_compilation = 2;
6559
6560 processing_has_namespace_info = 0;
6561
6562 start_symtab (name, comp_dir, lowpc);
6563 record_debugformat ("DWARF 2");
6564 record_producer (cu->producer);
6565
6566 /* Decode line number information if present. We do this before
6567 processing child DIEs, so that the line header table is available
6568 for DW_AT_decl_file. */
6569 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6570
6571 /* Process all dies in compilation unit. */
6572 if (die->child != NULL)
6573 {
6574 child_die = die->child;
6575 while (child_die && child_die->tag)
6576 {
6577 process_die (child_die, cu);
6578 child_die = sibling_die (child_die);
6579 }
6580 }
6581
6582 /* Decode macro information, if present. Dwarf 2 macro information
6583 refers to information in the line number info statement program
6584 header, so we can only read it if we've read the header
6585 successfully. */
6586 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6587 if (attr && cu->line_header)
6588 {
6589 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6590 complaint (&symfile_complaints,
6591 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6592
6593 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6594 comp_dir, abfd, cu,
6595 &dwarf2_per_objfile->macro, 1,
6596 ".debug_macro");
6597 }
6598 else
6599 {
6600 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6601 if (attr && cu->line_header)
6602 {
6603 unsigned int macro_offset = DW_UNSND (attr);
6604
6605 dwarf_decode_macros (cu->line_header, macro_offset,
6606 comp_dir, abfd, cu,
6607 &dwarf2_per_objfile->macinfo, 0,
6608 ".debug_macinfo");
6609 }
6610 }
6611
6612 do_cleanups (back_to);
6613 }
6614
6615 /* Process DW_TAG_type_unit.
6616 For TUs we want to skip the first top level sibling if it's not the
6617 actual type being defined by this TU. In this case the first top
6618 level sibling is there to provide context only. */
6619
6620 static void
6621 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6622 {
6623 struct objfile *objfile = cu->objfile;
6624 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6625 CORE_ADDR lowpc;
6626 struct attribute *attr;
6627 char *name = NULL;
6628 char *comp_dir = NULL;
6629 struct die_info *child_die;
6630 bfd *abfd = objfile->obfd;
6631
6632 /* start_symtab needs a low pc, but we don't really have one.
6633 Do what read_file_scope would do in the absence of such info. */
6634 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6635
6636 /* Find the filename. Do not use dwarf2_name here, since the filename
6637 is not a source language identifier. */
6638 attr = dwarf2_attr (die, DW_AT_name, cu);
6639 if (attr)
6640 name = DW_STRING (attr);
6641
6642 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6643 if (attr)
6644 comp_dir = DW_STRING (attr);
6645 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6646 {
6647 comp_dir = ldirname (name);
6648 if (comp_dir != NULL)
6649 make_cleanup (xfree, comp_dir);
6650 }
6651
6652 if (name == NULL)
6653 name = "<unknown>";
6654
6655 prepare_one_comp_unit (cu, die, language_minimal);
6656
6657 /* We assume that we're processing GCC output. */
6658 processing_gcc_compilation = 2;
6659
6660 processing_has_namespace_info = 0;
6661
6662 start_symtab (name, comp_dir, lowpc);
6663 record_debugformat ("DWARF 2");
6664 record_producer (cu->producer);
6665
6666 /* Decode line number information if present. We do this before
6667 processing child DIEs, so that the line header table is available
6668 for DW_AT_decl_file.
6669 We don't need the pc/line-number mapping for type units. */
6670 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6671
6672 /* Process the dies in the type unit. */
6673 if (die->child == NULL)
6674 {
6675 dump_die_for_error (die);
6676 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6677 bfd_get_filename (abfd));
6678 }
6679
6680 child_die = die->child;
6681
6682 while (child_die && child_die->tag)
6683 {
6684 process_die (child_die, cu);
6685
6686 child_die = sibling_die (child_die);
6687 }
6688
6689 do_cleanups (back_to);
6690 }
6691 \f
6692 /* DWO files. */
6693
6694 static hashval_t
6695 hash_dwo_file (const void *item)
6696 {
6697 const struct dwo_file *dwo_file = item;
6698
6699 return htab_hash_string (dwo_file->dwo_name);
6700 }
6701
6702 static int
6703 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6704 {
6705 const struct dwo_file *lhs = item_lhs;
6706 const struct dwo_file *rhs = item_rhs;
6707
6708 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6709 }
6710
6711 /* Allocate a hash table for DWO files. */
6712
6713 static htab_t
6714 allocate_dwo_file_hash_table (void)
6715 {
6716 struct objfile *objfile = dwarf2_per_objfile->objfile;
6717
6718 return htab_create_alloc_ex (41,
6719 hash_dwo_file,
6720 eq_dwo_file,
6721 NULL,
6722 &objfile->objfile_obstack,
6723 hashtab_obstack_allocate,
6724 dummy_obstack_deallocate);
6725 }
6726
6727 static hashval_t
6728 hash_dwo_unit (const void *item)
6729 {
6730 const struct dwo_unit *dwo_unit = item;
6731
6732 /* This drops the top 32 bits of the id, but is ok for a hash. */
6733 return dwo_unit->signature;
6734 }
6735
6736 static int
6737 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6738 {
6739 const struct dwo_unit *lhs = item_lhs;
6740 const struct dwo_unit *rhs = item_rhs;
6741
6742 /* The signature is assumed to be unique within the DWO file.
6743 So while object file CU dwo_id's always have the value zero,
6744 that's OK, assuming each object file DWO file has only one CU,
6745 and that's the rule for now. */
6746 return lhs->signature == rhs->signature;
6747 }
6748
6749 /* Allocate a hash table for DWO CUs,TUs.
6750 There is one of these tables for each of CUs,TUs for each DWO file. */
6751
6752 static htab_t
6753 allocate_dwo_unit_table (struct objfile *objfile)
6754 {
6755 /* Start out with a pretty small number.
6756 Generally DWO files contain only one CU and maybe some TUs. */
6757 return htab_create_alloc_ex (3,
6758 hash_dwo_unit,
6759 eq_dwo_unit,
6760 NULL,
6761 &objfile->objfile_obstack,
6762 hashtab_obstack_allocate,
6763 dummy_obstack_deallocate);
6764 }
6765
6766 /* This function is mapped across the sections and remembers the offset and
6767 size of each of the DWO debugging sections we are interested in. */
6768
6769 static void
6770 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6771 {
6772 struct dwo_file *dwo_file = dwo_file_ptr;
6773 const struct dwo_section_names *names = &dwo_section_names;
6774
6775 if (section_is_p (sectp->name, &names->abbrev_dwo))
6776 {
6777 dwo_file->sections.abbrev.asection = sectp;
6778 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6779 }
6780 else if (section_is_p (sectp->name, &names->info_dwo))
6781 {
6782 dwo_file->sections.info.asection = sectp;
6783 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6784 }
6785 else if (section_is_p (sectp->name, &names->line_dwo))
6786 {
6787 dwo_file->sections.line.asection = sectp;
6788 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6789 }
6790 else if (section_is_p (sectp->name, &names->loc_dwo))
6791 {
6792 dwo_file->sections.loc.asection = sectp;
6793 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6794 }
6795 else if (section_is_p (sectp->name, &names->str_dwo))
6796 {
6797 dwo_file->sections.str.asection = sectp;
6798 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6799 }
6800 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6801 {
6802 dwo_file->sections.str_offsets.asection = sectp;
6803 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6804 }
6805 else if (section_is_p (sectp->name, &names->types_dwo))
6806 {
6807 struct dwarf2_section_info type_section;
6808
6809 memset (&type_section, 0, sizeof (type_section));
6810 type_section.asection = sectp;
6811 type_section.size = bfd_get_section_size (sectp);
6812 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6813 &type_section);
6814 }
6815 }
6816
6817 /* Structure used to pass data to create_debug_info_hash_table_reader. */
6818
6819 struct create_dwo_info_table_data
6820 {
6821 struct dwo_file *dwo_file;
6822 htab_t cu_htab;
6823 };
6824
6825 /* die_reader_func for create_debug_info_hash_table. */
6826
6827 static void
6828 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6829 gdb_byte *info_ptr,
6830 struct die_info *comp_unit_die,
6831 int has_children,
6832 void *datap)
6833 {
6834 struct dwarf2_cu *cu = reader->cu;
6835 struct objfile *objfile = dwarf2_per_objfile->objfile;
6836 sect_offset offset = cu->per_cu->offset;
6837 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6838 struct create_dwo_info_table_data *data = datap;
6839 struct dwo_file *dwo_file = data->dwo_file;
6840 htab_t cu_htab = data->cu_htab;
6841 void **slot;
6842 struct attribute *attr;
6843 struct dwo_unit *dwo_unit;
6844
6845 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6846 if (attr == NULL)
6847 {
6848 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6849 " its dwo_id [in module %s]"),
6850 offset.sect_off, dwo_file->dwo_name);
6851 return;
6852 }
6853
6854 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6855 dwo_unit->dwo_file = dwo_file;
6856 dwo_unit->signature = DW_UNSND (attr);
6857 dwo_unit->info_or_types_section = section;
6858 dwo_unit->offset = offset;
6859 dwo_unit->length = cu->per_cu->length;
6860
6861 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6862 gdb_assert (slot != NULL);
6863 if (*slot != NULL)
6864 {
6865 const struct dwo_unit *dup_dwo_unit = *slot;
6866
6867 complaint (&symfile_complaints,
6868 _("debug entry at offset 0x%x is duplicate to the entry at"
6869 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6870 offset.sect_off, dup_dwo_unit->offset.sect_off,
6871 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6872 dwo_file->dwo_name);
6873 }
6874 else
6875 *slot = dwo_unit;
6876
6877 if (dwarf2_die_debug)
6878 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6879 offset.sect_off,
6880 phex (dwo_unit->signature,
6881 sizeof (dwo_unit->signature)));
6882 }
6883
6884 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6885
6886 static htab_t
6887 create_debug_info_hash_table (struct dwo_file *dwo_file)
6888 {
6889 struct objfile *objfile = dwarf2_per_objfile->objfile;
6890 struct dwarf2_section_info *section = &dwo_file->sections.info;
6891 bfd *abfd;
6892 htab_t cu_htab;
6893 gdb_byte *info_ptr, *end_ptr;
6894 struct create_dwo_info_table_data create_dwo_info_table_data;
6895
6896 dwarf2_read_section (objfile, section);
6897 info_ptr = section->buffer;
6898
6899 if (info_ptr == NULL)
6900 return NULL;
6901
6902 /* We can't set abfd until now because the section may be empty or
6903 not present, in which case section->asection will be NULL. */
6904 abfd = section->asection->owner;
6905
6906 if (dwarf2_die_debug)
6907 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6908 bfd_get_filename (abfd));
6909
6910 cu_htab = allocate_dwo_unit_table (objfile);
6911
6912 create_dwo_info_table_data.dwo_file = dwo_file;
6913 create_dwo_info_table_data.cu_htab = cu_htab;
6914
6915 end_ptr = info_ptr + section->size;
6916 while (info_ptr < end_ptr)
6917 {
6918 struct dwarf2_per_cu_data per_cu;
6919
6920 memset (&per_cu, 0, sizeof (per_cu));
6921 per_cu.objfile = objfile;
6922 per_cu.is_debug_types = 0;
6923 per_cu.offset.sect_off = info_ptr - section->buffer;
6924 per_cu.info_or_types_section = section;
6925
6926 init_cutu_and_read_dies_no_follow (&per_cu,
6927 &dwo_file->sections.abbrev,
6928 dwo_file,
6929 create_debug_info_hash_table_reader,
6930 &create_dwo_info_table_data);
6931
6932 info_ptr += per_cu.length;
6933 }
6934
6935 return cu_htab;
6936 }
6937
6938 /* Subroutine of open_dwo_file to simplify it.
6939 Open the file specified by FILE_NAME and hand it off to BFD for
6940 preliminary analysis. Return a newly initialized bfd *, which
6941 includes a canonicalized copy of FILE_NAME.
6942 In case of trouble, return NULL.
6943 NOTE: This function is derived from symfile_bfd_open. */
6944
6945 static bfd *
6946 try_open_dwo_file (const char *file_name)
6947 {
6948 bfd *sym_bfd;
6949 int desc;
6950 char *absolute_name;
6951
6952 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6953 O_RDONLY | O_BINARY, &absolute_name);
6954 if (desc < 0)
6955 return NULL;
6956
6957 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6958 if (!sym_bfd)
6959 {
6960 xfree (absolute_name);
6961 return NULL;
6962 }
6963 bfd_set_cacheable (sym_bfd, 1);
6964
6965 if (!bfd_check_format (sym_bfd, bfd_object))
6966 {
6967 bfd_close (sym_bfd); /* This also closes desc. */
6968 xfree (absolute_name);
6969 return NULL;
6970 }
6971
6972 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6973 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6974
6975 return sym_bfd;
6976 }
6977
6978 /* Try to open DWO file DWO_NAME.
6979 COMP_DIR is the DW_AT_comp_dir attribute.
6980 The result is the bfd handle of the file.
6981 If there is a problem finding or opening the file, return NULL.
6982 Upon success, the canonicalized path of the file is stored in the bfd,
6983 same as symfile_bfd_open. */
6984
6985 static bfd *
6986 open_dwo_file (const char *dwo_name, const char *comp_dir)
6987 {
6988 bfd *abfd;
6989
6990 if (IS_ABSOLUTE_PATH (dwo_name))
6991 return try_open_dwo_file (dwo_name);
6992
6993 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6994
6995 if (comp_dir != NULL)
6996 {
6997 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6998
6999 /* NOTE: If comp_dir is a relative path, this will also try the
7000 search path, which seems useful. */
7001 abfd = try_open_dwo_file (path_to_try);
7002 xfree (path_to_try);
7003 if (abfd != NULL)
7004 return abfd;
7005 }
7006
7007 /* That didn't work, try debug-file-directory, which, despite its name,
7008 is a list of paths. */
7009
7010 if (*debug_file_directory == '\0')
7011 return NULL;
7012
7013 return try_open_dwo_file (dwo_name);
7014 }
7015
7016 /* Initialize the use of the DWO file specified by DWO_NAME. */
7017
7018 static struct dwo_file *
7019 init_dwo_file (const char *dwo_name, const char *comp_dir)
7020 {
7021 struct objfile *objfile = dwarf2_per_objfile->objfile;
7022 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7023 struct dwo_file);
7024 bfd *abfd;
7025 struct cleanup *cleanups;
7026
7027 if (dwarf2_die_debug)
7028 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7029
7030 abfd = open_dwo_file (dwo_name, comp_dir);
7031 if (abfd == NULL)
7032 return NULL;
7033 dwo_file->dwo_name = dwo_name;
7034 dwo_file->dwo_bfd = abfd;
7035
7036 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7037
7038 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7039
7040 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7041
7042 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7043 dwo_file->sections.types);
7044
7045 discard_cleanups (cleanups);
7046
7047 return dwo_file;
7048 }
7049
7050 /* Lookup DWO file DWO_NAME. */
7051
7052 static struct dwo_file *
7053 lookup_dwo_file (char *dwo_name, const char *comp_dir)
7054 {
7055 struct dwo_file *dwo_file;
7056 struct dwo_file find_entry;
7057 void **slot;
7058
7059 if (dwarf2_per_objfile->dwo_files == NULL)
7060 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7061
7062 /* Have we already seen this DWO file? */
7063 find_entry.dwo_name = dwo_name;
7064 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7065
7066 /* If not, read it in and build a table of the DWOs it contains. */
7067 if (*slot == NULL)
7068 *slot = init_dwo_file (dwo_name, comp_dir);
7069
7070 /* NOTE: This will be NULL if unable to open the file. */
7071 dwo_file = *slot;
7072
7073 return dwo_file;
7074 }
7075
7076 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7077 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7078 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7079 nomenclature as TUs).
7080 The result is the DWO CU or NULL if we didn't find it
7081 (dwo_id mismatch or couldn't find the DWO file). */
7082
7083 static struct dwo_unit *
7084 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7085 char *dwo_name, const char *comp_dir,
7086 ULONGEST signature)
7087 {
7088 struct objfile *objfile = dwarf2_per_objfile->objfile;
7089 struct dwo_file *dwo_file;
7090
7091 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7092 if (dwo_file == NULL)
7093 return NULL;
7094
7095 /* Look up the DWO using its signature(dwo_id). */
7096
7097 if (dwo_file->cus != NULL)
7098 {
7099 struct dwo_unit find_dwo_cu, *dwo_cu;
7100
7101 find_dwo_cu.signature = signature;
7102 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
7103
7104 if (dwo_cu != NULL)
7105 return dwo_cu;
7106 }
7107
7108 /* We didn't find it. This must mean a dwo_id mismatch. */
7109
7110 complaint (&symfile_complaints,
7111 _("Could not find DWO CU referenced by CU at offset 0x%x"
7112 " [in module %s]"),
7113 this_cu->offset.sect_off, objfile->name);
7114 return NULL;
7115 }
7116
7117 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7118 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7119 The result is the DWO CU or NULL if we didn't find it
7120 (dwo_id mismatch or couldn't find the DWO file). */
7121
7122 static struct dwo_unit *
7123 lookup_dwo_type_unit (struct signatured_type *this_tu,
7124 char *dwo_name, const char *comp_dir)
7125 {
7126 struct objfile *objfile = dwarf2_per_objfile->objfile;
7127 struct dwo_file *dwo_file;
7128
7129 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7130 if (dwo_file == NULL)
7131 return NULL;
7132
7133 /* Look up the DWO using its signature(dwo_id). */
7134
7135 if (dwo_file->tus != NULL)
7136 {
7137 struct dwo_unit find_dwo_tu, *dwo_tu;
7138
7139 find_dwo_tu.signature = this_tu->signature;
7140 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7141
7142 if (dwo_tu != NULL)
7143 return dwo_tu;
7144 }
7145
7146 /* We didn't find it. This must mean a dwo_id mismatch. */
7147
7148 complaint (&symfile_complaints,
7149 _("Could not find DWO TU referenced by TU at offset 0x%x"
7150 " [in module %s]"),
7151 this_tu->per_cu.offset.sect_off, objfile->name);
7152 return NULL;
7153 }
7154
7155 /* Free all resources associated with DWO_FILE.
7156 Close the DWO file and munmap the sections.
7157 All memory should be on the objfile obstack. */
7158
7159 static void
7160 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
7161 {
7162 int ix;
7163 struct dwarf2_section_info *section;
7164
7165 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7166 bfd_close (dwo_file->dwo_bfd);
7167
7168 munmap_section_buffer (&dwo_file->sections.abbrev);
7169 munmap_section_buffer (&dwo_file->sections.info);
7170 munmap_section_buffer (&dwo_file->sections.line);
7171 munmap_section_buffer (&dwo_file->sections.loc);
7172 munmap_section_buffer (&dwo_file->sections.str);
7173 munmap_section_buffer (&dwo_file->sections.str_offsets);
7174
7175 for (ix = 0;
7176 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7177 ix, section);
7178 ++ix)
7179 munmap_section_buffer (section);
7180
7181 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7182 }
7183
7184 /* Wrapper for free_dwo_file for use in cleanups. */
7185
7186 static void
7187 free_dwo_file_cleanup (void *arg)
7188 {
7189 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7190 struct objfile *objfile = dwarf2_per_objfile->objfile;
7191
7192 free_dwo_file (dwo_file, objfile);
7193 }
7194
7195 /* Traversal function for free_dwo_files. */
7196
7197 static int
7198 free_dwo_file_from_slot (void **slot, void *info)
7199 {
7200 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7201 struct objfile *objfile = (struct objfile *) info;
7202
7203 free_dwo_file (dwo_file, objfile);
7204
7205 return 1;
7206 }
7207
7208 /* Free all resources associated with DWO_FILES. */
7209
7210 static void
7211 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7212 {
7213 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
7214 }
7215 \f
7216 /* Read in various DIEs. */
7217
7218 /* qsort helper for inherit_abstract_dies. */
7219
7220 static int
7221 unsigned_int_compar (const void *ap, const void *bp)
7222 {
7223 unsigned int a = *(unsigned int *) ap;
7224 unsigned int b = *(unsigned int *) bp;
7225
7226 return (a > b) - (b > a);
7227 }
7228
7229 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
7230 Inherit only the children of the DW_AT_abstract_origin DIE not being
7231 already referenced by DW_AT_abstract_origin from the children of the
7232 current DIE. */
7233
7234 static void
7235 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7236 {
7237 struct die_info *child_die;
7238 unsigned die_children_count;
7239 /* CU offsets which were referenced by children of the current DIE. */
7240 sect_offset *offsets;
7241 sect_offset *offsets_end, *offsetp;
7242 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7243 struct die_info *origin_die;
7244 /* Iterator of the ORIGIN_DIE children. */
7245 struct die_info *origin_child_die;
7246 struct cleanup *cleanups;
7247 struct attribute *attr;
7248 struct dwarf2_cu *origin_cu;
7249 struct pending **origin_previous_list_in_scope;
7250
7251 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7252 if (!attr)
7253 return;
7254
7255 /* Note that following die references may follow to a die in a
7256 different cu. */
7257
7258 origin_cu = cu;
7259 origin_die = follow_die_ref (die, attr, &origin_cu);
7260
7261 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7262 symbols in. */
7263 origin_previous_list_in_scope = origin_cu->list_in_scope;
7264 origin_cu->list_in_scope = cu->list_in_scope;
7265
7266 if (die->tag != origin_die->tag
7267 && !(die->tag == DW_TAG_inlined_subroutine
7268 && origin_die->tag == DW_TAG_subprogram))
7269 complaint (&symfile_complaints,
7270 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7271 die->offset.sect_off, origin_die->offset.sect_off);
7272
7273 child_die = die->child;
7274 die_children_count = 0;
7275 while (child_die && child_die->tag)
7276 {
7277 child_die = sibling_die (child_die);
7278 die_children_count++;
7279 }
7280 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7281 cleanups = make_cleanup (xfree, offsets);
7282
7283 offsets_end = offsets;
7284 child_die = die->child;
7285 while (child_die && child_die->tag)
7286 {
7287 /* For each CHILD_DIE, find the corresponding child of
7288 ORIGIN_DIE. If there is more than one layer of
7289 DW_AT_abstract_origin, follow them all; there shouldn't be,
7290 but GCC versions at least through 4.4 generate this (GCC PR
7291 40573). */
7292 struct die_info *child_origin_die = child_die;
7293 struct dwarf2_cu *child_origin_cu = cu;
7294
7295 while (1)
7296 {
7297 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7298 child_origin_cu);
7299 if (attr == NULL)
7300 break;
7301 child_origin_die = follow_die_ref (child_origin_die, attr,
7302 &child_origin_cu);
7303 }
7304
7305 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7306 counterpart may exist. */
7307 if (child_origin_die != child_die)
7308 {
7309 if (child_die->tag != child_origin_die->tag
7310 && !(child_die->tag == DW_TAG_inlined_subroutine
7311 && child_origin_die->tag == DW_TAG_subprogram))
7312 complaint (&symfile_complaints,
7313 _("Child DIE 0x%x and its abstract origin 0x%x have "
7314 "different tags"), child_die->offset.sect_off,
7315 child_origin_die->offset.sect_off);
7316 if (child_origin_die->parent != origin_die)
7317 complaint (&symfile_complaints,
7318 _("Child DIE 0x%x and its abstract origin 0x%x have "
7319 "different parents"), child_die->offset.sect_off,
7320 child_origin_die->offset.sect_off);
7321 else
7322 *offsets_end++ = child_origin_die->offset;
7323 }
7324 child_die = sibling_die (child_die);
7325 }
7326 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7327 unsigned_int_compar);
7328 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7329 if (offsetp[-1].sect_off == offsetp->sect_off)
7330 complaint (&symfile_complaints,
7331 _("Multiple children of DIE 0x%x refer "
7332 "to DIE 0x%x as their abstract origin"),
7333 die->offset.sect_off, offsetp->sect_off);
7334
7335 offsetp = offsets;
7336 origin_child_die = origin_die->child;
7337 while (origin_child_die && origin_child_die->tag)
7338 {
7339 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7340 while (offsetp < offsets_end
7341 && offsetp->sect_off < origin_child_die->offset.sect_off)
7342 offsetp++;
7343 if (offsetp >= offsets_end
7344 || offsetp->sect_off > origin_child_die->offset.sect_off)
7345 {
7346 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7347 process_die (origin_child_die, origin_cu);
7348 }
7349 origin_child_die = sibling_die (origin_child_die);
7350 }
7351 origin_cu->list_in_scope = origin_previous_list_in_scope;
7352
7353 do_cleanups (cleanups);
7354 }
7355
7356 static void
7357 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7358 {
7359 struct objfile *objfile = cu->objfile;
7360 struct context_stack *new;
7361 CORE_ADDR lowpc;
7362 CORE_ADDR highpc;
7363 struct die_info *child_die;
7364 struct attribute *attr, *call_line, *call_file;
7365 char *name;
7366 CORE_ADDR baseaddr;
7367 struct block *block;
7368 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7369 VEC (symbolp) *template_args = NULL;
7370 struct template_symbol *templ_func = NULL;
7371
7372 if (inlined_func)
7373 {
7374 /* If we do not have call site information, we can't show the
7375 caller of this inlined function. That's too confusing, so
7376 only use the scope for local variables. */
7377 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7378 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7379 if (call_line == NULL || call_file == NULL)
7380 {
7381 read_lexical_block_scope (die, cu);
7382 return;
7383 }
7384 }
7385
7386 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7387
7388 name = dwarf2_name (die, cu);
7389
7390 /* Ignore functions with missing or empty names. These are actually
7391 illegal according to the DWARF standard. */
7392 if (name == NULL)
7393 {
7394 complaint (&symfile_complaints,
7395 _("missing name for subprogram DIE at %d"),
7396 die->offset.sect_off);
7397 return;
7398 }
7399
7400 /* Ignore functions with missing or invalid low and high pc attributes. */
7401 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7402 {
7403 attr = dwarf2_attr (die, DW_AT_external, cu);
7404 if (!attr || !DW_UNSND (attr))
7405 complaint (&symfile_complaints,
7406 _("cannot get low and high bounds "
7407 "for subprogram DIE at %d"),
7408 die->offset.sect_off);
7409 return;
7410 }
7411
7412 lowpc += baseaddr;
7413 highpc += baseaddr;
7414
7415 /* If we have any template arguments, then we must allocate a
7416 different sort of symbol. */
7417 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7418 {
7419 if (child_die->tag == DW_TAG_template_type_param
7420 || child_die->tag == DW_TAG_template_value_param)
7421 {
7422 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7423 struct template_symbol);
7424 templ_func->base.is_cplus_template_function = 1;
7425 break;
7426 }
7427 }
7428
7429 new = push_context (0, lowpc);
7430 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7431 (struct symbol *) templ_func);
7432
7433 /* If there is a location expression for DW_AT_frame_base, record
7434 it. */
7435 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7436 if (attr)
7437 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7438 expression is being recorded directly in the function's symbol
7439 and not in a separate frame-base object. I guess this hack is
7440 to avoid adding some sort of frame-base adjunct/annex to the
7441 function's symbol :-(. The problem with doing this is that it
7442 results in a function symbol with a location expression that
7443 has nothing to do with the location of the function, ouch! The
7444 relationship should be: a function's symbol has-a frame base; a
7445 frame-base has-a location expression. */
7446 dwarf2_symbol_mark_computed (attr, new->name, cu);
7447
7448 cu->list_in_scope = &local_symbols;
7449
7450 if (die->child != NULL)
7451 {
7452 child_die = die->child;
7453 while (child_die && child_die->tag)
7454 {
7455 if (child_die->tag == DW_TAG_template_type_param
7456 || child_die->tag == DW_TAG_template_value_param)
7457 {
7458 struct symbol *arg = new_symbol (child_die, NULL, cu);
7459
7460 if (arg != NULL)
7461 VEC_safe_push (symbolp, template_args, arg);
7462 }
7463 else
7464 process_die (child_die, cu);
7465 child_die = sibling_die (child_die);
7466 }
7467 }
7468
7469 inherit_abstract_dies (die, cu);
7470
7471 /* If we have a DW_AT_specification, we might need to import using
7472 directives from the context of the specification DIE. See the
7473 comment in determine_prefix. */
7474 if (cu->language == language_cplus
7475 && dwarf2_attr (die, DW_AT_specification, cu))
7476 {
7477 struct dwarf2_cu *spec_cu = cu;
7478 struct die_info *spec_die = die_specification (die, &spec_cu);
7479
7480 while (spec_die)
7481 {
7482 child_die = spec_die->child;
7483 while (child_die && child_die->tag)
7484 {
7485 if (child_die->tag == DW_TAG_imported_module)
7486 process_die (child_die, spec_cu);
7487 child_die = sibling_die (child_die);
7488 }
7489
7490 /* In some cases, GCC generates specification DIEs that
7491 themselves contain DW_AT_specification attributes. */
7492 spec_die = die_specification (spec_die, &spec_cu);
7493 }
7494 }
7495
7496 new = pop_context ();
7497 /* Make a block for the local symbols within. */
7498 block = finish_block (new->name, &local_symbols, new->old_blocks,
7499 lowpc, highpc, objfile);
7500
7501 /* For C++, set the block's scope. */
7502 if (cu->language == language_cplus || cu->language == language_fortran)
7503 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7504 determine_prefix (die, cu),
7505 processing_has_namespace_info);
7506
7507 /* If we have address ranges, record them. */
7508 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7509
7510 /* Attach template arguments to function. */
7511 if (! VEC_empty (symbolp, template_args))
7512 {
7513 gdb_assert (templ_func != NULL);
7514
7515 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7516 templ_func->template_arguments
7517 = obstack_alloc (&objfile->objfile_obstack,
7518 (templ_func->n_template_arguments
7519 * sizeof (struct symbol *)));
7520 memcpy (templ_func->template_arguments,
7521 VEC_address (symbolp, template_args),
7522 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7523 VEC_free (symbolp, template_args);
7524 }
7525
7526 /* In C++, we can have functions nested inside functions (e.g., when
7527 a function declares a class that has methods). This means that
7528 when we finish processing a function scope, we may need to go
7529 back to building a containing block's symbol lists. */
7530 local_symbols = new->locals;
7531 param_symbols = new->params;
7532 using_directives = new->using_directives;
7533
7534 /* If we've finished processing a top-level function, subsequent
7535 symbols go in the file symbol list. */
7536 if (outermost_context_p ())
7537 cu->list_in_scope = &file_symbols;
7538 }
7539
7540 /* Process all the DIES contained within a lexical block scope. Start
7541 a new scope, process the dies, and then close the scope. */
7542
7543 static void
7544 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7545 {
7546 struct objfile *objfile = cu->objfile;
7547 struct context_stack *new;
7548 CORE_ADDR lowpc, highpc;
7549 struct die_info *child_die;
7550 CORE_ADDR baseaddr;
7551
7552 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7553
7554 /* Ignore blocks with missing or invalid low and high pc attributes. */
7555 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7556 as multiple lexical blocks? Handling children in a sane way would
7557 be nasty. Might be easier to properly extend generic blocks to
7558 describe ranges. */
7559 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7560 return;
7561 lowpc += baseaddr;
7562 highpc += baseaddr;
7563
7564 push_context (0, lowpc);
7565 if (die->child != NULL)
7566 {
7567 child_die = die->child;
7568 while (child_die && child_die->tag)
7569 {
7570 process_die (child_die, cu);
7571 child_die = sibling_die (child_die);
7572 }
7573 }
7574 new = pop_context ();
7575
7576 if (local_symbols != NULL || using_directives != NULL)
7577 {
7578 struct block *block
7579 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7580 highpc, objfile);
7581
7582 /* Note that recording ranges after traversing children, as we
7583 do here, means that recording a parent's ranges entails
7584 walking across all its children's ranges as they appear in
7585 the address map, which is quadratic behavior.
7586
7587 It would be nicer to record the parent's ranges before
7588 traversing its children, simply overriding whatever you find
7589 there. But since we don't even decide whether to create a
7590 block until after we've traversed its children, that's hard
7591 to do. */
7592 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7593 }
7594 local_symbols = new->locals;
7595 using_directives = new->using_directives;
7596 }
7597
7598 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7599
7600 static void
7601 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7602 {
7603 struct objfile *objfile = cu->objfile;
7604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7605 CORE_ADDR pc, baseaddr;
7606 struct attribute *attr;
7607 struct call_site *call_site, call_site_local;
7608 void **slot;
7609 int nparams;
7610 struct die_info *child_die;
7611
7612 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7613
7614 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7615 if (!attr)
7616 {
7617 complaint (&symfile_complaints,
7618 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7619 "DIE 0x%x [in module %s]"),
7620 die->offset.sect_off, objfile->name);
7621 return;
7622 }
7623 pc = DW_ADDR (attr) + baseaddr;
7624
7625 if (cu->call_site_htab == NULL)
7626 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7627 NULL, &objfile->objfile_obstack,
7628 hashtab_obstack_allocate, NULL);
7629 call_site_local.pc = pc;
7630 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7631 if (*slot != NULL)
7632 {
7633 complaint (&symfile_complaints,
7634 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7635 "DIE 0x%x [in module %s]"),
7636 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7637 return;
7638 }
7639
7640 /* Count parameters at the caller. */
7641
7642 nparams = 0;
7643 for (child_die = die->child; child_die && child_die->tag;
7644 child_die = sibling_die (child_die))
7645 {
7646 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7647 {
7648 complaint (&symfile_complaints,
7649 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7650 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7651 child_die->tag, child_die->offset.sect_off, objfile->name);
7652 continue;
7653 }
7654
7655 nparams++;
7656 }
7657
7658 call_site = obstack_alloc (&objfile->objfile_obstack,
7659 (sizeof (*call_site)
7660 + (sizeof (*call_site->parameter)
7661 * (nparams - 1))));
7662 *slot = call_site;
7663 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7664 call_site->pc = pc;
7665
7666 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7667 {
7668 struct die_info *func_die;
7669
7670 /* Skip also over DW_TAG_inlined_subroutine. */
7671 for (func_die = die->parent;
7672 func_die && func_die->tag != DW_TAG_subprogram
7673 && func_die->tag != DW_TAG_subroutine_type;
7674 func_die = func_die->parent);
7675
7676 /* DW_AT_GNU_all_call_sites is a superset
7677 of DW_AT_GNU_all_tail_call_sites. */
7678 if (func_die
7679 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7680 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7681 {
7682 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7683 not complete. But keep CALL_SITE for look ups via call_site_htab,
7684 both the initial caller containing the real return address PC and
7685 the final callee containing the current PC of a chain of tail
7686 calls do not need to have the tail call list complete. But any
7687 function candidate for a virtual tail call frame searched via
7688 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7689 determined unambiguously. */
7690 }
7691 else
7692 {
7693 struct type *func_type = NULL;
7694
7695 if (func_die)
7696 func_type = get_die_type (func_die, cu);
7697 if (func_type != NULL)
7698 {
7699 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7700
7701 /* Enlist this call site to the function. */
7702 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7703 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7704 }
7705 else
7706 complaint (&symfile_complaints,
7707 _("Cannot find function owning DW_TAG_GNU_call_site "
7708 "DIE 0x%x [in module %s]"),
7709 die->offset.sect_off, objfile->name);
7710 }
7711 }
7712
7713 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7714 if (attr == NULL)
7715 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7716 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7717 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7718 /* Keep NULL DWARF_BLOCK. */;
7719 else if (attr_form_is_block (attr))
7720 {
7721 struct dwarf2_locexpr_baton *dlbaton;
7722
7723 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7724 dlbaton->data = DW_BLOCK (attr)->data;
7725 dlbaton->size = DW_BLOCK (attr)->size;
7726 dlbaton->per_cu = cu->per_cu;
7727
7728 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7729 }
7730 else if (is_ref_attr (attr))
7731 {
7732 struct dwarf2_cu *target_cu = cu;
7733 struct die_info *target_die;
7734
7735 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7736 gdb_assert (target_cu->objfile == objfile);
7737 if (die_is_declaration (target_die, target_cu))
7738 {
7739 const char *target_physname;
7740
7741 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7742 if (target_physname == NULL)
7743 complaint (&symfile_complaints,
7744 _("DW_AT_GNU_call_site_target target DIE has invalid "
7745 "physname, for referencing DIE 0x%x [in module %s]"),
7746 die->offset.sect_off, objfile->name);
7747 else
7748 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7749 }
7750 else
7751 {
7752 CORE_ADDR lowpc;
7753
7754 /* DW_AT_entry_pc should be preferred. */
7755 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7756 complaint (&symfile_complaints,
7757 _("DW_AT_GNU_call_site_target target DIE has invalid "
7758 "low pc, for referencing DIE 0x%x [in module %s]"),
7759 die->offset.sect_off, objfile->name);
7760 else
7761 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7762 }
7763 }
7764 else
7765 complaint (&symfile_complaints,
7766 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7767 "block nor reference, for DIE 0x%x [in module %s]"),
7768 die->offset.sect_off, objfile->name);
7769
7770 call_site->per_cu = cu->per_cu;
7771
7772 for (child_die = die->child;
7773 child_die && child_die->tag;
7774 child_die = sibling_die (child_die))
7775 {
7776 struct call_site_parameter *parameter;
7777
7778 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7779 {
7780 /* Already printed the complaint above. */
7781 continue;
7782 }
7783
7784 gdb_assert (call_site->parameter_count < nparams);
7785 parameter = &call_site->parameter[call_site->parameter_count];
7786
7787 /* DW_AT_location specifies the register number. Value of the data
7788 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7789
7790 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7791 if (!attr || !attr_form_is_block (attr))
7792 {
7793 complaint (&symfile_complaints,
7794 _("No DW_FORM_block* DW_AT_location for "
7795 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7796 child_die->offset.sect_off, objfile->name);
7797 continue;
7798 }
7799 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7800 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7801 if (parameter->dwarf_reg == -1
7802 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7803 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7804 &parameter->fb_offset))
7805 {
7806 complaint (&symfile_complaints,
7807 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7808 "for DW_FORM_block* DW_AT_location for "
7809 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7810 child_die->offset.sect_off, objfile->name);
7811 continue;
7812 }
7813
7814 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7815 if (!attr_form_is_block (attr))
7816 {
7817 complaint (&symfile_complaints,
7818 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7819 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7820 child_die->offset.sect_off, objfile->name);
7821 continue;
7822 }
7823 parameter->value = DW_BLOCK (attr)->data;
7824 parameter->value_size = DW_BLOCK (attr)->size;
7825
7826 /* Parameters are not pre-cleared by memset above. */
7827 parameter->data_value = NULL;
7828 parameter->data_value_size = 0;
7829 call_site->parameter_count++;
7830
7831 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7832 if (attr)
7833 {
7834 if (!attr_form_is_block (attr))
7835 complaint (&symfile_complaints,
7836 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7837 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7838 child_die->offset.sect_off, objfile->name);
7839 else
7840 {
7841 parameter->data_value = DW_BLOCK (attr)->data;
7842 parameter->data_value_size = DW_BLOCK (attr)->size;
7843 }
7844 }
7845 }
7846 }
7847
7848 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
7849 Return 1 if the attributes are present and valid, otherwise, return 0.
7850 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
7851
7852 static int
7853 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
7854 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7855 struct partial_symtab *ranges_pst)
7856 {
7857 struct objfile *objfile = cu->objfile;
7858 struct comp_unit_head *cu_header = &cu->header;
7859 bfd *obfd = objfile->obfd;
7860 unsigned int addr_size = cu_header->addr_size;
7861 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7862 /* Base address selection entry. */
7863 CORE_ADDR base;
7864 int found_base;
7865 unsigned int dummy;
7866 gdb_byte *buffer;
7867 CORE_ADDR marker;
7868 int low_set;
7869 CORE_ADDR low = 0;
7870 CORE_ADDR high = 0;
7871 CORE_ADDR baseaddr;
7872
7873 found_base = cu->base_known;
7874 base = cu->base_address;
7875
7876 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
7877 if (offset >= dwarf2_per_objfile->ranges.size)
7878 {
7879 complaint (&symfile_complaints,
7880 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7881 offset);
7882 return 0;
7883 }
7884 buffer = dwarf2_per_objfile->ranges.buffer + offset;
7885
7886 /* Read in the largest possible address. */
7887 marker = read_address (obfd, buffer, cu, &dummy);
7888 if ((marker & mask) == mask)
7889 {
7890 /* If we found the largest possible address, then
7891 read the base address. */
7892 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7893 buffer += 2 * addr_size;
7894 offset += 2 * addr_size;
7895 found_base = 1;
7896 }
7897
7898 low_set = 0;
7899
7900 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7901
7902 while (1)
7903 {
7904 CORE_ADDR range_beginning, range_end;
7905
7906 range_beginning = read_address (obfd, buffer, cu, &dummy);
7907 buffer += addr_size;
7908 range_end = read_address (obfd, buffer, cu, &dummy);
7909 buffer += addr_size;
7910 offset += 2 * addr_size;
7911
7912 /* An end of list marker is a pair of zero addresses. */
7913 if (range_beginning == 0 && range_end == 0)
7914 /* Found the end of list entry. */
7915 break;
7916
7917 /* Each base address selection entry is a pair of 2 values.
7918 The first is the largest possible address, the second is
7919 the base address. Check for a base address here. */
7920 if ((range_beginning & mask) == mask)
7921 {
7922 /* If we found the largest possible address, then
7923 read the base address. */
7924 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7925 found_base = 1;
7926 continue;
7927 }
7928
7929 if (!found_base)
7930 {
7931 /* We have no valid base address for the ranges
7932 data. */
7933 complaint (&symfile_complaints,
7934 _("Invalid .debug_ranges data (no base address)"));
7935 return 0;
7936 }
7937
7938 if (range_beginning > range_end)
7939 {
7940 /* Inverted range entries are invalid. */
7941 complaint (&symfile_complaints,
7942 _("Invalid .debug_ranges data (inverted range)"));
7943 return 0;
7944 }
7945
7946 /* Empty range entries have no effect. */
7947 if (range_beginning == range_end)
7948 continue;
7949
7950 range_beginning += base;
7951 range_end += base;
7952
7953 if (ranges_pst != NULL)
7954 addrmap_set_empty (objfile->psymtabs_addrmap,
7955 range_beginning + baseaddr,
7956 range_end - 1 + baseaddr,
7957 ranges_pst);
7958
7959 /* FIXME: This is recording everything as a low-high
7960 segment of consecutive addresses. We should have a
7961 data structure for discontiguous block ranges
7962 instead. */
7963 if (! low_set)
7964 {
7965 low = range_beginning;
7966 high = range_end;
7967 low_set = 1;
7968 }
7969 else
7970 {
7971 if (range_beginning < low)
7972 low = range_beginning;
7973 if (range_end > high)
7974 high = range_end;
7975 }
7976 }
7977
7978 if (! low_set)
7979 /* If the first entry is an end-of-list marker, the range
7980 describes an empty scope, i.e. no instructions. */
7981 return 0;
7982
7983 if (low_return)
7984 *low_return = low;
7985 if (high_return)
7986 *high_return = high;
7987 return 1;
7988 }
7989
7990 /* Get low and high pc attributes from a die. Return 1 if the attributes
7991 are present and valid, otherwise, return 0. Return -1 if the range is
7992 discontinuous, i.e. derived from DW_AT_ranges information. */
7993
7994 static int
7995 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
7996 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7997 struct partial_symtab *pst)
7998 {
7999 struct attribute *attr;
8000 struct attribute *attr_high;
8001 CORE_ADDR low = 0;
8002 CORE_ADDR high = 0;
8003 int ret = 0;
8004
8005 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8006 if (attr_high)
8007 {
8008 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8009 if (attr)
8010 {
8011 low = DW_ADDR (attr);
8012 if (attr_high->form == DW_FORM_addr
8013 || attr_high->form == DW_FORM_GNU_addr_index)
8014 high = DW_ADDR (attr_high);
8015 else
8016 high = low + DW_UNSND (attr_high);
8017 }
8018 else
8019 /* Found high w/o low attribute. */
8020 return 0;
8021
8022 /* Found consecutive range of addresses. */
8023 ret = 1;
8024 }
8025 else
8026 {
8027 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8028 if (attr != NULL)
8029 {
8030 /* Value of the DW_AT_ranges attribute is the offset in the
8031 .debug_ranges section. */
8032 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
8033 return 0;
8034 /* Found discontinuous range of addresses. */
8035 ret = -1;
8036 }
8037 }
8038
8039 /* read_partial_die has also the strict LOW < HIGH requirement. */
8040 if (high <= low)
8041 return 0;
8042
8043 /* When using the GNU linker, .gnu.linkonce. sections are used to
8044 eliminate duplicate copies of functions and vtables and such.
8045 The linker will arbitrarily choose one and discard the others.
8046 The AT_*_pc values for such functions refer to local labels in
8047 these sections. If the section from that file was discarded, the
8048 labels are not in the output, so the relocs get a value of 0.
8049 If this is a discarded function, mark the pc bounds as invalid,
8050 so that GDB will ignore it. */
8051 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
8052 return 0;
8053
8054 *lowpc = low;
8055 if (highpc)
8056 *highpc = high;
8057 return ret;
8058 }
8059
8060 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
8061 its low and high PC addresses. Do nothing if these addresses could not
8062 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8063 and HIGHPC to the high address if greater than HIGHPC. */
8064
8065 static void
8066 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8067 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8068 struct dwarf2_cu *cu)
8069 {
8070 CORE_ADDR low, high;
8071 struct die_info *child = die->child;
8072
8073 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
8074 {
8075 *lowpc = min (*lowpc, low);
8076 *highpc = max (*highpc, high);
8077 }
8078
8079 /* If the language does not allow nested subprograms (either inside
8080 subprograms or lexical blocks), we're done. */
8081 if (cu->language != language_ada)
8082 return;
8083
8084 /* Check all the children of the given DIE. If it contains nested
8085 subprograms, then check their pc bounds. Likewise, we need to
8086 check lexical blocks as well, as they may also contain subprogram
8087 definitions. */
8088 while (child && child->tag)
8089 {
8090 if (child->tag == DW_TAG_subprogram
8091 || child->tag == DW_TAG_lexical_block)
8092 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8093 child = sibling_die (child);
8094 }
8095 }
8096
8097 /* Get the low and high pc's represented by the scope DIE, and store
8098 them in *LOWPC and *HIGHPC. If the correct values can't be
8099 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8100
8101 static void
8102 get_scope_pc_bounds (struct die_info *die,
8103 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8104 struct dwarf2_cu *cu)
8105 {
8106 CORE_ADDR best_low = (CORE_ADDR) -1;
8107 CORE_ADDR best_high = (CORE_ADDR) 0;
8108 CORE_ADDR current_low, current_high;
8109
8110 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
8111 {
8112 best_low = current_low;
8113 best_high = current_high;
8114 }
8115 else
8116 {
8117 struct die_info *child = die->child;
8118
8119 while (child && child->tag)
8120 {
8121 switch (child->tag) {
8122 case DW_TAG_subprogram:
8123 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
8124 break;
8125 case DW_TAG_namespace:
8126 case DW_TAG_module:
8127 /* FIXME: carlton/2004-01-16: Should we do this for
8128 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8129 that current GCC's always emit the DIEs corresponding
8130 to definitions of methods of classes as children of a
8131 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8132 the DIEs giving the declarations, which could be
8133 anywhere). But I don't see any reason why the
8134 standards says that they have to be there. */
8135 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8136
8137 if (current_low != ((CORE_ADDR) -1))
8138 {
8139 best_low = min (best_low, current_low);
8140 best_high = max (best_high, current_high);
8141 }
8142 break;
8143 default:
8144 /* Ignore. */
8145 break;
8146 }
8147
8148 child = sibling_die (child);
8149 }
8150 }
8151
8152 *lowpc = best_low;
8153 *highpc = best_high;
8154 }
8155
8156 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
8157 in DIE. */
8158
8159 static void
8160 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8161 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8162 {
8163 struct objfile *objfile = cu->objfile;
8164 struct attribute *attr;
8165 struct attribute *attr_high;
8166
8167 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8168 if (attr_high)
8169 {
8170 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8171 if (attr)
8172 {
8173 CORE_ADDR low = DW_ADDR (attr);
8174 CORE_ADDR high;
8175 if (attr_high->form == DW_FORM_addr
8176 || attr_high->form == DW_FORM_GNU_addr_index)
8177 high = DW_ADDR (attr_high);
8178 else
8179 high = low + DW_UNSND (attr_high);
8180
8181 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8182 }
8183 }
8184
8185 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8186 if (attr)
8187 {
8188 bfd *obfd = objfile->obfd;
8189
8190 /* The value of the DW_AT_ranges attribute is the offset of the
8191 address range list in the .debug_ranges section. */
8192 unsigned long offset = DW_UNSND (attr);
8193 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
8194
8195 /* For some target architectures, but not others, the
8196 read_address function sign-extends the addresses it returns.
8197 To recognize base address selection entries, we need a
8198 mask. */
8199 unsigned int addr_size = cu->header.addr_size;
8200 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8201
8202 /* The base address, to which the next pair is relative. Note
8203 that this 'base' is a DWARF concept: most entries in a range
8204 list are relative, to reduce the number of relocs against the
8205 debugging information. This is separate from this function's
8206 'baseaddr' argument, which GDB uses to relocate debugging
8207 information from a shared library based on the address at
8208 which the library was loaded. */
8209 CORE_ADDR base = cu->base_address;
8210 int base_known = cu->base_known;
8211
8212 gdb_assert (dwarf2_per_objfile->ranges.readin);
8213 if (offset >= dwarf2_per_objfile->ranges.size)
8214 {
8215 complaint (&symfile_complaints,
8216 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8217 offset);
8218 return;
8219 }
8220
8221 for (;;)
8222 {
8223 unsigned int bytes_read;
8224 CORE_ADDR start, end;
8225
8226 start = read_address (obfd, buffer, cu, &bytes_read);
8227 buffer += bytes_read;
8228 end = read_address (obfd, buffer, cu, &bytes_read);
8229 buffer += bytes_read;
8230
8231 /* Did we find the end of the range list? */
8232 if (start == 0 && end == 0)
8233 break;
8234
8235 /* Did we find a base address selection entry? */
8236 else if ((start & base_select_mask) == base_select_mask)
8237 {
8238 base = end;
8239 base_known = 1;
8240 }
8241
8242 /* We found an ordinary address range. */
8243 else
8244 {
8245 if (!base_known)
8246 {
8247 complaint (&symfile_complaints,
8248 _("Invalid .debug_ranges data "
8249 "(no base address)"));
8250 return;
8251 }
8252
8253 if (start > end)
8254 {
8255 /* Inverted range entries are invalid. */
8256 complaint (&symfile_complaints,
8257 _("Invalid .debug_ranges data "
8258 "(inverted range)"));
8259 return;
8260 }
8261
8262 /* Empty range entries have no effect. */
8263 if (start == end)
8264 continue;
8265
8266 record_block_range (block,
8267 baseaddr + base + start,
8268 baseaddr + base + end - 1);
8269 }
8270 }
8271 }
8272 }
8273
8274 /* Check whether the producer field indicates either of GCC < 4.6, or the
8275 Intel C/C++ compiler, and cache the result in CU. */
8276
8277 static void
8278 check_producer (struct dwarf2_cu *cu)
8279 {
8280 const char *cs;
8281 int major, minor, release;
8282
8283 if (cu->producer == NULL)
8284 {
8285 /* For unknown compilers expect their behavior is DWARF version
8286 compliant.
8287
8288 GCC started to support .debug_types sections by -gdwarf-4 since
8289 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8290 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8291 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8292 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8293 }
8294 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
8295 {
8296 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8297
8298 cs = &cu->producer[strlen ("GNU ")];
8299 while (*cs && !isdigit (*cs))
8300 cs++;
8301 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8302 {
8303 /* Not recognized as GCC. */
8304 }
8305 else
8306 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8307 }
8308 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8309 cu->producer_is_icc = 1;
8310 else
8311 {
8312 /* For other non-GCC compilers, expect their behavior is DWARF version
8313 compliant. */
8314 }
8315
8316 cu->checked_producer = 1;
8317 }
8318
8319 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8320 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8321 during 4.6.0 experimental. */
8322
8323 static int
8324 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8325 {
8326 if (!cu->checked_producer)
8327 check_producer (cu);
8328
8329 return cu->producer_is_gxx_lt_4_6;
8330 }
8331
8332 /* Return the default accessibility type if it is not overriden by
8333 DW_AT_accessibility. */
8334
8335 static enum dwarf_access_attribute
8336 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8337 {
8338 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8339 {
8340 /* The default DWARF 2 accessibility for members is public, the default
8341 accessibility for inheritance is private. */
8342
8343 if (die->tag != DW_TAG_inheritance)
8344 return DW_ACCESS_public;
8345 else
8346 return DW_ACCESS_private;
8347 }
8348 else
8349 {
8350 /* DWARF 3+ defines the default accessibility a different way. The same
8351 rules apply now for DW_TAG_inheritance as for the members and it only
8352 depends on the container kind. */
8353
8354 if (die->parent->tag == DW_TAG_class_type)
8355 return DW_ACCESS_private;
8356 else
8357 return DW_ACCESS_public;
8358 }
8359 }
8360
8361 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8362 offset. If the attribute was not found return 0, otherwise return
8363 1. If it was found but could not properly be handled, set *OFFSET
8364 to 0. */
8365
8366 static int
8367 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8368 LONGEST *offset)
8369 {
8370 struct attribute *attr;
8371
8372 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8373 if (attr != NULL)
8374 {
8375 *offset = 0;
8376
8377 /* Note that we do not check for a section offset first here.
8378 This is because DW_AT_data_member_location is new in DWARF 4,
8379 so if we see it, we can assume that a constant form is really
8380 a constant and not a section offset. */
8381 if (attr_form_is_constant (attr))
8382 *offset = dwarf2_get_attr_constant_value (attr, 0);
8383 else if (attr_form_is_section_offset (attr))
8384 dwarf2_complex_location_expr_complaint ();
8385 else if (attr_form_is_block (attr))
8386 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8387 else
8388 dwarf2_complex_location_expr_complaint ();
8389
8390 return 1;
8391 }
8392
8393 return 0;
8394 }
8395
8396 /* Add an aggregate field to the field list. */
8397
8398 static void
8399 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8400 struct dwarf2_cu *cu)
8401 {
8402 struct objfile *objfile = cu->objfile;
8403 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8404 struct nextfield *new_field;
8405 struct attribute *attr;
8406 struct field *fp;
8407 char *fieldname = "";
8408
8409 /* Allocate a new field list entry and link it in. */
8410 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8411 make_cleanup (xfree, new_field);
8412 memset (new_field, 0, sizeof (struct nextfield));
8413
8414 if (die->tag == DW_TAG_inheritance)
8415 {
8416 new_field->next = fip->baseclasses;
8417 fip->baseclasses = new_field;
8418 }
8419 else
8420 {
8421 new_field->next = fip->fields;
8422 fip->fields = new_field;
8423 }
8424 fip->nfields++;
8425
8426 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8427 if (attr)
8428 new_field->accessibility = DW_UNSND (attr);
8429 else
8430 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8431 if (new_field->accessibility != DW_ACCESS_public)
8432 fip->non_public_fields = 1;
8433
8434 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8435 if (attr)
8436 new_field->virtuality = DW_UNSND (attr);
8437 else
8438 new_field->virtuality = DW_VIRTUALITY_none;
8439
8440 fp = &new_field->field;
8441
8442 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8443 {
8444 LONGEST offset;
8445
8446 /* Data member other than a C++ static data member. */
8447
8448 /* Get type of field. */
8449 fp->type = die_type (die, cu);
8450
8451 SET_FIELD_BITPOS (*fp, 0);
8452
8453 /* Get bit size of field (zero if none). */
8454 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8455 if (attr)
8456 {
8457 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8458 }
8459 else
8460 {
8461 FIELD_BITSIZE (*fp) = 0;
8462 }
8463
8464 /* Get bit offset of field. */
8465 if (handle_data_member_location (die, cu, &offset))
8466 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8467 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8468 if (attr)
8469 {
8470 if (gdbarch_bits_big_endian (gdbarch))
8471 {
8472 /* For big endian bits, the DW_AT_bit_offset gives the
8473 additional bit offset from the MSB of the containing
8474 anonymous object to the MSB of the field. We don't
8475 have to do anything special since we don't need to
8476 know the size of the anonymous object. */
8477 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8478 }
8479 else
8480 {
8481 /* For little endian bits, compute the bit offset to the
8482 MSB of the anonymous object, subtract off the number of
8483 bits from the MSB of the field to the MSB of the
8484 object, and then subtract off the number of bits of
8485 the field itself. The result is the bit offset of
8486 the LSB of the field. */
8487 int anonymous_size;
8488 int bit_offset = DW_UNSND (attr);
8489
8490 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8491 if (attr)
8492 {
8493 /* The size of the anonymous object containing
8494 the bit field is explicit, so use the
8495 indicated size (in bytes). */
8496 anonymous_size = DW_UNSND (attr);
8497 }
8498 else
8499 {
8500 /* The size of the anonymous object containing
8501 the bit field must be inferred from the type
8502 attribute of the data member containing the
8503 bit field. */
8504 anonymous_size = TYPE_LENGTH (fp->type);
8505 }
8506 SET_FIELD_BITPOS (*fp,
8507 (FIELD_BITPOS (*fp)
8508 + anonymous_size * bits_per_byte
8509 - bit_offset - FIELD_BITSIZE (*fp)));
8510 }
8511 }
8512
8513 /* Get name of field. */
8514 fieldname = dwarf2_name (die, cu);
8515 if (fieldname == NULL)
8516 fieldname = "";
8517
8518 /* The name is already allocated along with this objfile, so we don't
8519 need to duplicate it for the type. */
8520 fp->name = fieldname;
8521
8522 /* Change accessibility for artificial fields (e.g. virtual table
8523 pointer or virtual base class pointer) to private. */
8524 if (dwarf2_attr (die, DW_AT_artificial, cu))
8525 {
8526 FIELD_ARTIFICIAL (*fp) = 1;
8527 new_field->accessibility = DW_ACCESS_private;
8528 fip->non_public_fields = 1;
8529 }
8530 }
8531 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8532 {
8533 /* C++ static member. */
8534
8535 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8536 is a declaration, but all versions of G++ as of this writing
8537 (so through at least 3.2.1) incorrectly generate
8538 DW_TAG_variable tags. */
8539
8540 const char *physname;
8541
8542 /* Get name of field. */
8543 fieldname = dwarf2_name (die, cu);
8544 if (fieldname == NULL)
8545 return;
8546
8547 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8548 if (attr
8549 /* Only create a symbol if this is an external value.
8550 new_symbol checks this and puts the value in the global symbol
8551 table, which we want. If it is not external, new_symbol
8552 will try to put the value in cu->list_in_scope which is wrong. */
8553 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8554 {
8555 /* A static const member, not much different than an enum as far as
8556 we're concerned, except that we can support more types. */
8557 new_symbol (die, NULL, cu);
8558 }
8559
8560 /* Get physical name. */
8561 physname = dwarf2_physname (fieldname, die, cu);
8562
8563 /* The name is already allocated along with this objfile, so we don't
8564 need to duplicate it for the type. */
8565 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8566 FIELD_TYPE (*fp) = die_type (die, cu);
8567 FIELD_NAME (*fp) = fieldname;
8568 }
8569 else if (die->tag == DW_TAG_inheritance)
8570 {
8571 LONGEST offset;
8572
8573 /* C++ base class field. */
8574 if (handle_data_member_location (die, cu, &offset))
8575 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8576 FIELD_BITSIZE (*fp) = 0;
8577 FIELD_TYPE (*fp) = die_type (die, cu);
8578 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8579 fip->nbaseclasses++;
8580 }
8581 }
8582
8583 /* Add a typedef defined in the scope of the FIP's class. */
8584
8585 static void
8586 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8587 struct dwarf2_cu *cu)
8588 {
8589 struct objfile *objfile = cu->objfile;
8590 struct typedef_field_list *new_field;
8591 struct attribute *attr;
8592 struct typedef_field *fp;
8593 char *fieldname = "";
8594
8595 /* Allocate a new field list entry and link it in. */
8596 new_field = xzalloc (sizeof (*new_field));
8597 make_cleanup (xfree, new_field);
8598
8599 gdb_assert (die->tag == DW_TAG_typedef);
8600
8601 fp = &new_field->field;
8602
8603 /* Get name of field. */
8604 fp->name = dwarf2_name (die, cu);
8605 if (fp->name == NULL)
8606 return;
8607
8608 fp->type = read_type_die (die, cu);
8609
8610 new_field->next = fip->typedef_field_list;
8611 fip->typedef_field_list = new_field;
8612 fip->typedef_field_list_count++;
8613 }
8614
8615 /* Create the vector of fields, and attach it to the type. */
8616
8617 static void
8618 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8619 struct dwarf2_cu *cu)
8620 {
8621 int nfields = fip->nfields;
8622
8623 /* Record the field count, allocate space for the array of fields,
8624 and create blank accessibility bitfields if necessary. */
8625 TYPE_NFIELDS (type) = nfields;
8626 TYPE_FIELDS (type) = (struct field *)
8627 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8628 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8629
8630 if (fip->non_public_fields && cu->language != language_ada)
8631 {
8632 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8633
8634 TYPE_FIELD_PRIVATE_BITS (type) =
8635 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8636 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8637
8638 TYPE_FIELD_PROTECTED_BITS (type) =
8639 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8640 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8641
8642 TYPE_FIELD_IGNORE_BITS (type) =
8643 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8644 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8645 }
8646
8647 /* If the type has baseclasses, allocate and clear a bit vector for
8648 TYPE_FIELD_VIRTUAL_BITS. */
8649 if (fip->nbaseclasses && cu->language != language_ada)
8650 {
8651 int num_bytes = B_BYTES (fip->nbaseclasses);
8652 unsigned char *pointer;
8653
8654 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8655 pointer = TYPE_ALLOC (type, num_bytes);
8656 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8657 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8658 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8659 }
8660
8661 /* Copy the saved-up fields into the field vector. Start from the head of
8662 the list, adding to the tail of the field array, so that they end up in
8663 the same order in the array in which they were added to the list. */
8664 while (nfields-- > 0)
8665 {
8666 struct nextfield *fieldp;
8667
8668 if (fip->fields)
8669 {
8670 fieldp = fip->fields;
8671 fip->fields = fieldp->next;
8672 }
8673 else
8674 {
8675 fieldp = fip->baseclasses;
8676 fip->baseclasses = fieldp->next;
8677 }
8678
8679 TYPE_FIELD (type, nfields) = fieldp->field;
8680 switch (fieldp->accessibility)
8681 {
8682 case DW_ACCESS_private:
8683 if (cu->language != language_ada)
8684 SET_TYPE_FIELD_PRIVATE (type, nfields);
8685 break;
8686
8687 case DW_ACCESS_protected:
8688 if (cu->language != language_ada)
8689 SET_TYPE_FIELD_PROTECTED (type, nfields);
8690 break;
8691
8692 case DW_ACCESS_public:
8693 break;
8694
8695 default:
8696 /* Unknown accessibility. Complain and treat it as public. */
8697 {
8698 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8699 fieldp->accessibility);
8700 }
8701 break;
8702 }
8703 if (nfields < fip->nbaseclasses)
8704 {
8705 switch (fieldp->virtuality)
8706 {
8707 case DW_VIRTUALITY_virtual:
8708 case DW_VIRTUALITY_pure_virtual:
8709 if (cu->language == language_ada)
8710 error (_("unexpected virtuality in component of Ada type"));
8711 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8712 break;
8713 }
8714 }
8715 }
8716 }
8717
8718 /* Add a member function to the proper fieldlist. */
8719
8720 static void
8721 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8722 struct type *type, struct dwarf2_cu *cu)
8723 {
8724 struct objfile *objfile = cu->objfile;
8725 struct attribute *attr;
8726 struct fnfieldlist *flp;
8727 int i;
8728 struct fn_field *fnp;
8729 char *fieldname;
8730 struct nextfnfield *new_fnfield;
8731 struct type *this_type;
8732 enum dwarf_access_attribute accessibility;
8733
8734 if (cu->language == language_ada)
8735 error (_("unexpected member function in Ada type"));
8736
8737 /* Get name of member function. */
8738 fieldname = dwarf2_name (die, cu);
8739 if (fieldname == NULL)
8740 return;
8741
8742 /* Look up member function name in fieldlist. */
8743 for (i = 0; i < fip->nfnfields; i++)
8744 {
8745 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8746 break;
8747 }
8748
8749 /* Create new list element if necessary. */
8750 if (i < fip->nfnfields)
8751 flp = &fip->fnfieldlists[i];
8752 else
8753 {
8754 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8755 {
8756 fip->fnfieldlists = (struct fnfieldlist *)
8757 xrealloc (fip->fnfieldlists,
8758 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8759 * sizeof (struct fnfieldlist));
8760 if (fip->nfnfields == 0)
8761 make_cleanup (free_current_contents, &fip->fnfieldlists);
8762 }
8763 flp = &fip->fnfieldlists[fip->nfnfields];
8764 flp->name = fieldname;
8765 flp->length = 0;
8766 flp->head = NULL;
8767 i = fip->nfnfields++;
8768 }
8769
8770 /* Create a new member function field and chain it to the field list
8771 entry. */
8772 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8773 make_cleanup (xfree, new_fnfield);
8774 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8775 new_fnfield->next = flp->head;
8776 flp->head = new_fnfield;
8777 flp->length++;
8778
8779 /* Fill in the member function field info. */
8780 fnp = &new_fnfield->fnfield;
8781
8782 /* Delay processing of the physname until later. */
8783 if (cu->language == language_cplus || cu->language == language_java)
8784 {
8785 add_to_method_list (type, i, flp->length - 1, fieldname,
8786 die, cu);
8787 }
8788 else
8789 {
8790 const char *physname = dwarf2_physname (fieldname, die, cu);
8791 fnp->physname = physname ? physname : "";
8792 }
8793
8794 fnp->type = alloc_type (objfile);
8795 this_type = read_type_die (die, cu);
8796 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
8797 {
8798 int nparams = TYPE_NFIELDS (this_type);
8799
8800 /* TYPE is the domain of this method, and THIS_TYPE is the type
8801 of the method itself (TYPE_CODE_METHOD). */
8802 smash_to_method_type (fnp->type, type,
8803 TYPE_TARGET_TYPE (this_type),
8804 TYPE_FIELDS (this_type),
8805 TYPE_NFIELDS (this_type),
8806 TYPE_VARARGS (this_type));
8807
8808 /* Handle static member functions.
8809 Dwarf2 has no clean way to discern C++ static and non-static
8810 member functions. G++ helps GDB by marking the first
8811 parameter for non-static member functions (which is the this
8812 pointer) as artificial. We obtain this information from
8813 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
8814 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
8815 fnp->voffset = VOFFSET_STATIC;
8816 }
8817 else
8818 complaint (&symfile_complaints, _("member function type missing for '%s'"),
8819 dwarf2_full_name (fieldname, die, cu));
8820
8821 /* Get fcontext from DW_AT_containing_type if present. */
8822 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8823 fnp->fcontext = die_containing_type (die, cu);
8824
8825 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8826 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
8827
8828 /* Get accessibility. */
8829 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8830 if (attr)
8831 accessibility = DW_UNSND (attr);
8832 else
8833 accessibility = dwarf2_default_access_attribute (die, cu);
8834 switch (accessibility)
8835 {
8836 case DW_ACCESS_private:
8837 fnp->is_private = 1;
8838 break;
8839 case DW_ACCESS_protected:
8840 fnp->is_protected = 1;
8841 break;
8842 }
8843
8844 /* Check for artificial methods. */
8845 attr = dwarf2_attr (die, DW_AT_artificial, cu);
8846 if (attr && DW_UNSND (attr) != 0)
8847 fnp->is_artificial = 1;
8848
8849 /* Get index in virtual function table if it is a virtual member
8850 function. For older versions of GCC, this is an offset in the
8851 appropriate virtual table, as specified by DW_AT_containing_type.
8852 For everyone else, it is an expression to be evaluated relative
8853 to the object address. */
8854
8855 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
8856 if (attr)
8857 {
8858 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8859 {
8860 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8861 {
8862 /* Old-style GCC. */
8863 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8864 }
8865 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8866 || (DW_BLOCK (attr)->size > 1
8867 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8868 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8869 {
8870 struct dwarf_block blk;
8871 int offset;
8872
8873 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8874 ? 1 : 2);
8875 blk.size = DW_BLOCK (attr)->size - offset;
8876 blk.data = DW_BLOCK (attr)->data + offset;
8877 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8878 if ((fnp->voffset % cu->header.addr_size) != 0)
8879 dwarf2_complex_location_expr_complaint ();
8880 else
8881 fnp->voffset /= cu->header.addr_size;
8882 fnp->voffset += 2;
8883 }
8884 else
8885 dwarf2_complex_location_expr_complaint ();
8886
8887 if (!fnp->fcontext)
8888 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8889 }
8890 else if (attr_form_is_section_offset (attr))
8891 {
8892 dwarf2_complex_location_expr_complaint ();
8893 }
8894 else
8895 {
8896 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8897 fieldname);
8898 }
8899 }
8900 else
8901 {
8902 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8903 if (attr && DW_UNSND (attr))
8904 {
8905 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8906 complaint (&symfile_complaints,
8907 _("Member function \"%s\" (offset %d) is virtual "
8908 "but the vtable offset is not specified"),
8909 fieldname, die->offset.sect_off);
8910 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8911 TYPE_CPLUS_DYNAMIC (type) = 1;
8912 }
8913 }
8914 }
8915
8916 /* Create the vector of member function fields, and attach it to the type. */
8917
8918 static void
8919 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
8920 struct dwarf2_cu *cu)
8921 {
8922 struct fnfieldlist *flp;
8923 int i;
8924
8925 if (cu->language == language_ada)
8926 error (_("unexpected member functions in Ada type"));
8927
8928 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8929 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8930 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8931
8932 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8933 {
8934 struct nextfnfield *nfp = flp->head;
8935 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8936 int k;
8937
8938 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8939 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8940 fn_flp->fn_fields = (struct fn_field *)
8941 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8942 for (k = flp->length; (k--, nfp); nfp = nfp->next)
8943 fn_flp->fn_fields[k] = nfp->fnfield;
8944 }
8945
8946 TYPE_NFN_FIELDS (type) = fip->nfnfields;
8947 }
8948
8949 /* Returns non-zero if NAME is the name of a vtable member in CU's
8950 language, zero otherwise. */
8951 static int
8952 is_vtable_name (const char *name, struct dwarf2_cu *cu)
8953 {
8954 static const char vptr[] = "_vptr";
8955 static const char vtable[] = "vtable";
8956
8957 /* Look for the C++ and Java forms of the vtable. */
8958 if ((cu->language == language_java
8959 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8960 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8961 && is_cplus_marker (name[sizeof (vptr) - 1])))
8962 return 1;
8963
8964 return 0;
8965 }
8966
8967 /* GCC outputs unnamed structures that are really pointers to member
8968 functions, with the ABI-specified layout. If TYPE describes
8969 such a structure, smash it into a member function type.
8970
8971 GCC shouldn't do this; it should just output pointer to member DIEs.
8972 This is GCC PR debug/28767. */
8973
8974 static void
8975 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
8976 {
8977 struct type *pfn_type, *domain_type, *new_type;
8978
8979 /* Check for a structure with no name and two children. */
8980 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8981 return;
8982
8983 /* Check for __pfn and __delta members. */
8984 if (TYPE_FIELD_NAME (type, 0) == NULL
8985 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8986 || TYPE_FIELD_NAME (type, 1) == NULL
8987 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8988 return;
8989
8990 /* Find the type of the method. */
8991 pfn_type = TYPE_FIELD_TYPE (type, 0);
8992 if (pfn_type == NULL
8993 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8994 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
8995 return;
8996
8997 /* Look for the "this" argument. */
8998 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8999 if (TYPE_NFIELDS (pfn_type) == 0
9000 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
9001 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
9002 return;
9003
9004 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
9005 new_type = alloc_type (objfile);
9006 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
9007 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9008 TYPE_VARARGS (pfn_type));
9009 smash_to_methodptr_type (type, new_type);
9010 }
9011
9012 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9013 (icc). */
9014
9015 static int
9016 producer_is_icc (struct dwarf2_cu *cu)
9017 {
9018 if (!cu->checked_producer)
9019 check_producer (cu);
9020
9021 return cu->producer_is_icc;
9022 }
9023
9024 /* Called when we find the DIE that starts a structure or union scope
9025 (definition) to create a type for the structure or union. Fill in
9026 the type's name and general properties; the members will not be
9027 processed until process_structure_type.
9028
9029 NOTE: we need to call these functions regardless of whether or not the
9030 DIE has a DW_AT_name attribute, since it might be an anonymous
9031 structure or union. This gets the type entered into our set of
9032 user defined types.
9033
9034 However, if the structure is incomplete (an opaque struct/union)
9035 then suppress creating a symbol table entry for it since gdb only
9036 wants to find the one with the complete definition. Note that if
9037 it is complete, we just call new_symbol, which does it's own
9038 checking about whether the struct/union is anonymous or not (and
9039 suppresses creating a symbol table entry itself). */
9040
9041 static struct type *
9042 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
9043 {
9044 struct objfile *objfile = cu->objfile;
9045 struct type *type;
9046 struct attribute *attr;
9047 char *name;
9048
9049 /* If the definition of this type lives in .debug_types, read that type.
9050 Don't follow DW_AT_specification though, that will take us back up
9051 the chain and we want to go down. */
9052 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9053 if (attr)
9054 {
9055 struct dwarf2_cu *type_cu = cu;
9056 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9057
9058 /* We could just recurse on read_structure_type, but we need to call
9059 get_die_type to ensure only one type for this DIE is created.
9060 This is important, for example, because for c++ classes we need
9061 TYPE_NAME set which is only done by new_symbol. Blech. */
9062 type = read_type_die (type_die, type_cu);
9063
9064 /* TYPE_CU may not be the same as CU.
9065 Ensure TYPE is recorded in CU's type_hash table. */
9066 return set_die_type (die, type, cu);
9067 }
9068
9069 type = alloc_type (objfile);
9070 INIT_CPLUS_SPECIFIC (type);
9071
9072 name = dwarf2_name (die, cu);
9073 if (name != NULL)
9074 {
9075 if (cu->language == language_cplus
9076 || cu->language == language_java)
9077 {
9078 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9079
9080 /* dwarf2_full_name might have already finished building the DIE's
9081 type. If so, there is no need to continue. */
9082 if (get_die_type (die, cu) != NULL)
9083 return get_die_type (die, cu);
9084
9085 TYPE_TAG_NAME (type) = full_name;
9086 if (die->tag == DW_TAG_structure_type
9087 || die->tag == DW_TAG_class_type)
9088 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9089 }
9090 else
9091 {
9092 /* The name is already allocated along with this objfile, so
9093 we don't need to duplicate it for the type. */
9094 TYPE_TAG_NAME (type) = (char *) name;
9095 if (die->tag == DW_TAG_class_type)
9096 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9097 }
9098 }
9099
9100 if (die->tag == DW_TAG_structure_type)
9101 {
9102 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9103 }
9104 else if (die->tag == DW_TAG_union_type)
9105 {
9106 TYPE_CODE (type) = TYPE_CODE_UNION;
9107 }
9108 else
9109 {
9110 TYPE_CODE (type) = TYPE_CODE_CLASS;
9111 }
9112
9113 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9114 TYPE_DECLARED_CLASS (type) = 1;
9115
9116 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9117 if (attr)
9118 {
9119 TYPE_LENGTH (type) = DW_UNSND (attr);
9120 }
9121 else
9122 {
9123 TYPE_LENGTH (type) = 0;
9124 }
9125
9126 if (producer_is_icc (cu))
9127 {
9128 /* ICC does not output the required DW_AT_declaration
9129 on incomplete types, but gives them a size of zero. */
9130 }
9131 else
9132 TYPE_STUB_SUPPORTED (type) = 1;
9133
9134 if (die_is_declaration (die, cu))
9135 TYPE_STUB (type) = 1;
9136 else if (attr == NULL && die->child == NULL
9137 && producer_is_realview (cu->producer))
9138 /* RealView does not output the required DW_AT_declaration
9139 on incomplete types. */
9140 TYPE_STUB (type) = 1;
9141
9142 /* We need to add the type field to the die immediately so we don't
9143 infinitely recurse when dealing with pointers to the structure
9144 type within the structure itself. */
9145 set_die_type (die, type, cu);
9146
9147 /* set_die_type should be already done. */
9148 set_descriptive_type (type, die, cu);
9149
9150 return type;
9151 }
9152
9153 /* Finish creating a structure or union type, including filling in
9154 its members and creating a symbol for it. */
9155
9156 static void
9157 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9158 {
9159 struct objfile *objfile = cu->objfile;
9160 struct die_info *child_die = die->child;
9161 struct type *type;
9162
9163 type = get_die_type (die, cu);
9164 if (type == NULL)
9165 type = read_structure_type (die, cu);
9166
9167 if (die->child != NULL && ! die_is_declaration (die, cu))
9168 {
9169 struct field_info fi;
9170 struct die_info *child_die;
9171 VEC (symbolp) *template_args = NULL;
9172 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9173
9174 memset (&fi, 0, sizeof (struct field_info));
9175
9176 child_die = die->child;
9177
9178 while (child_die && child_die->tag)
9179 {
9180 if (child_die->tag == DW_TAG_member
9181 || child_die->tag == DW_TAG_variable)
9182 {
9183 /* NOTE: carlton/2002-11-05: A C++ static data member
9184 should be a DW_TAG_member that is a declaration, but
9185 all versions of G++ as of this writing (so through at
9186 least 3.2.1) incorrectly generate DW_TAG_variable
9187 tags for them instead. */
9188 dwarf2_add_field (&fi, child_die, cu);
9189 }
9190 else if (child_die->tag == DW_TAG_subprogram)
9191 {
9192 /* C++ member function. */
9193 dwarf2_add_member_fn (&fi, child_die, type, cu);
9194 }
9195 else if (child_die->tag == DW_TAG_inheritance)
9196 {
9197 /* C++ base class field. */
9198 dwarf2_add_field (&fi, child_die, cu);
9199 }
9200 else if (child_die->tag == DW_TAG_typedef)
9201 dwarf2_add_typedef (&fi, child_die, cu);
9202 else if (child_die->tag == DW_TAG_template_type_param
9203 || child_die->tag == DW_TAG_template_value_param)
9204 {
9205 struct symbol *arg = new_symbol (child_die, NULL, cu);
9206
9207 if (arg != NULL)
9208 VEC_safe_push (symbolp, template_args, arg);
9209 }
9210
9211 child_die = sibling_die (child_die);
9212 }
9213
9214 /* Attach template arguments to type. */
9215 if (! VEC_empty (symbolp, template_args))
9216 {
9217 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9218 TYPE_N_TEMPLATE_ARGUMENTS (type)
9219 = VEC_length (symbolp, template_args);
9220 TYPE_TEMPLATE_ARGUMENTS (type)
9221 = obstack_alloc (&objfile->objfile_obstack,
9222 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9223 * sizeof (struct symbol *)));
9224 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9225 VEC_address (symbolp, template_args),
9226 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9227 * sizeof (struct symbol *)));
9228 VEC_free (symbolp, template_args);
9229 }
9230
9231 /* Attach fields and member functions to the type. */
9232 if (fi.nfields)
9233 dwarf2_attach_fields_to_type (&fi, type, cu);
9234 if (fi.nfnfields)
9235 {
9236 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
9237
9238 /* Get the type which refers to the base class (possibly this
9239 class itself) which contains the vtable pointer for the current
9240 class from the DW_AT_containing_type attribute. This use of
9241 DW_AT_containing_type is a GNU extension. */
9242
9243 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9244 {
9245 struct type *t = die_containing_type (die, cu);
9246
9247 TYPE_VPTR_BASETYPE (type) = t;
9248 if (type == t)
9249 {
9250 int i;
9251
9252 /* Our own class provides vtbl ptr. */
9253 for (i = TYPE_NFIELDS (t) - 1;
9254 i >= TYPE_N_BASECLASSES (t);
9255 --i)
9256 {
9257 const char *fieldname = TYPE_FIELD_NAME (t, i);
9258
9259 if (is_vtable_name (fieldname, cu))
9260 {
9261 TYPE_VPTR_FIELDNO (type) = i;
9262 break;
9263 }
9264 }
9265
9266 /* Complain if virtual function table field not found. */
9267 if (i < TYPE_N_BASECLASSES (t))
9268 complaint (&symfile_complaints,
9269 _("virtual function table pointer "
9270 "not found when defining class '%s'"),
9271 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9272 "");
9273 }
9274 else
9275 {
9276 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9277 }
9278 }
9279 else if (cu->producer
9280 && strncmp (cu->producer,
9281 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9282 {
9283 /* The IBM XLC compiler does not provide direct indication
9284 of the containing type, but the vtable pointer is
9285 always named __vfp. */
9286
9287 int i;
9288
9289 for (i = TYPE_NFIELDS (type) - 1;
9290 i >= TYPE_N_BASECLASSES (type);
9291 --i)
9292 {
9293 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9294 {
9295 TYPE_VPTR_FIELDNO (type) = i;
9296 TYPE_VPTR_BASETYPE (type) = type;
9297 break;
9298 }
9299 }
9300 }
9301 }
9302
9303 /* Copy fi.typedef_field_list linked list elements content into the
9304 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9305 if (fi.typedef_field_list)
9306 {
9307 int i = fi.typedef_field_list_count;
9308
9309 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9310 TYPE_TYPEDEF_FIELD_ARRAY (type)
9311 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9312 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9313
9314 /* Reverse the list order to keep the debug info elements order. */
9315 while (--i >= 0)
9316 {
9317 struct typedef_field *dest, *src;
9318
9319 dest = &TYPE_TYPEDEF_FIELD (type, i);
9320 src = &fi.typedef_field_list->field;
9321 fi.typedef_field_list = fi.typedef_field_list->next;
9322 *dest = *src;
9323 }
9324 }
9325
9326 do_cleanups (back_to);
9327
9328 if (HAVE_CPLUS_STRUCT (type))
9329 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9330 }
9331
9332 quirk_gcc_member_function_pointer (type, objfile);
9333
9334 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9335 snapshots) has been known to create a die giving a declaration
9336 for a class that has, as a child, a die giving a definition for a
9337 nested class. So we have to process our children even if the
9338 current die is a declaration. Normally, of course, a declaration
9339 won't have any children at all. */
9340
9341 while (child_die != NULL && child_die->tag)
9342 {
9343 if (child_die->tag == DW_TAG_member
9344 || child_die->tag == DW_TAG_variable
9345 || child_die->tag == DW_TAG_inheritance
9346 || child_die->tag == DW_TAG_template_value_param
9347 || child_die->tag == DW_TAG_template_type_param)
9348 {
9349 /* Do nothing. */
9350 }
9351 else
9352 process_die (child_die, cu);
9353
9354 child_die = sibling_die (child_die);
9355 }
9356
9357 /* Do not consider external references. According to the DWARF standard,
9358 these DIEs are identified by the fact that they have no byte_size
9359 attribute, and a declaration attribute. */
9360 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9361 || !die_is_declaration (die, cu))
9362 new_symbol (die, type, cu);
9363 }
9364
9365 /* Given a DW_AT_enumeration_type die, set its type. We do not
9366 complete the type's fields yet, or create any symbols. */
9367
9368 static struct type *
9369 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9370 {
9371 struct objfile *objfile = cu->objfile;
9372 struct type *type;
9373 struct attribute *attr;
9374 const char *name;
9375
9376 /* If the definition of this type lives in .debug_types, read that type.
9377 Don't follow DW_AT_specification though, that will take us back up
9378 the chain and we want to go down. */
9379 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9380 if (attr)
9381 {
9382 struct dwarf2_cu *type_cu = cu;
9383 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9384
9385 type = read_type_die (type_die, type_cu);
9386
9387 /* TYPE_CU may not be the same as CU.
9388 Ensure TYPE is recorded in CU's type_hash table. */
9389 return set_die_type (die, type, cu);
9390 }
9391
9392 type = alloc_type (objfile);
9393
9394 TYPE_CODE (type) = TYPE_CODE_ENUM;
9395 name = dwarf2_full_name (NULL, die, cu);
9396 if (name != NULL)
9397 TYPE_TAG_NAME (type) = (char *) name;
9398
9399 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9400 if (attr)
9401 {
9402 TYPE_LENGTH (type) = DW_UNSND (attr);
9403 }
9404 else
9405 {
9406 TYPE_LENGTH (type) = 0;
9407 }
9408
9409 /* The enumeration DIE can be incomplete. In Ada, any type can be
9410 declared as private in the package spec, and then defined only
9411 inside the package body. Such types are known as Taft Amendment
9412 Types. When another package uses such a type, an incomplete DIE
9413 may be generated by the compiler. */
9414 if (die_is_declaration (die, cu))
9415 TYPE_STUB (type) = 1;
9416
9417 return set_die_type (die, type, cu);
9418 }
9419
9420 /* Given a pointer to a die which begins an enumeration, process all
9421 the dies that define the members of the enumeration, and create the
9422 symbol for the enumeration type.
9423
9424 NOTE: We reverse the order of the element list. */
9425
9426 static void
9427 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9428 {
9429 struct type *this_type;
9430
9431 this_type = get_die_type (die, cu);
9432 if (this_type == NULL)
9433 this_type = read_enumeration_type (die, cu);
9434
9435 if (die->child != NULL)
9436 {
9437 struct die_info *child_die;
9438 struct symbol *sym;
9439 struct field *fields = NULL;
9440 int num_fields = 0;
9441 int unsigned_enum = 1;
9442 char *name;
9443 int flag_enum = 1;
9444 ULONGEST mask = 0;
9445
9446 child_die = die->child;
9447 while (child_die && child_die->tag)
9448 {
9449 if (child_die->tag != DW_TAG_enumerator)
9450 {
9451 process_die (child_die, cu);
9452 }
9453 else
9454 {
9455 name = dwarf2_name (child_die, cu);
9456 if (name)
9457 {
9458 sym = new_symbol (child_die, this_type, cu);
9459 if (SYMBOL_VALUE (sym) < 0)
9460 {
9461 unsigned_enum = 0;
9462 flag_enum = 0;
9463 }
9464 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9465 flag_enum = 0;
9466 else
9467 mask |= SYMBOL_VALUE (sym);
9468
9469 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9470 {
9471 fields = (struct field *)
9472 xrealloc (fields,
9473 (num_fields + DW_FIELD_ALLOC_CHUNK)
9474 * sizeof (struct field));
9475 }
9476
9477 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9478 FIELD_TYPE (fields[num_fields]) = NULL;
9479 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9480 FIELD_BITSIZE (fields[num_fields]) = 0;
9481
9482 num_fields++;
9483 }
9484 }
9485
9486 child_die = sibling_die (child_die);
9487 }
9488
9489 if (num_fields)
9490 {
9491 TYPE_NFIELDS (this_type) = num_fields;
9492 TYPE_FIELDS (this_type) = (struct field *)
9493 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9494 memcpy (TYPE_FIELDS (this_type), fields,
9495 sizeof (struct field) * num_fields);
9496 xfree (fields);
9497 }
9498 if (unsigned_enum)
9499 TYPE_UNSIGNED (this_type) = 1;
9500 if (flag_enum)
9501 TYPE_FLAG_ENUM (this_type) = 1;
9502 }
9503
9504 /* If we are reading an enum from a .debug_types unit, and the enum
9505 is a declaration, and the enum is not the signatured type in the
9506 unit, then we do not want to add a symbol for it. Adding a
9507 symbol would in some cases obscure the true definition of the
9508 enum, giving users an incomplete type when the definition is
9509 actually available. Note that we do not want to do this for all
9510 enums which are just declarations, because C++0x allows forward
9511 enum declarations. */
9512 if (cu->per_cu->is_debug_types
9513 && die_is_declaration (die, cu))
9514 {
9515 struct signatured_type *sig_type;
9516
9517 sig_type
9518 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9519 cu->per_cu->info_or_types_section,
9520 cu->per_cu->offset);
9521 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9522 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9523 return;
9524 }
9525
9526 new_symbol (die, this_type, cu);
9527 }
9528
9529 /* Extract all information from a DW_TAG_array_type DIE and put it in
9530 the DIE's type field. For now, this only handles one dimensional
9531 arrays. */
9532
9533 static struct type *
9534 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9535 {
9536 struct objfile *objfile = cu->objfile;
9537 struct die_info *child_die;
9538 struct type *type;
9539 struct type *element_type, *range_type, *index_type;
9540 struct type **range_types = NULL;
9541 struct attribute *attr;
9542 int ndim = 0;
9543 struct cleanup *back_to;
9544 char *name;
9545
9546 element_type = die_type (die, cu);
9547
9548 /* The die_type call above may have already set the type for this DIE. */
9549 type = get_die_type (die, cu);
9550 if (type)
9551 return type;
9552
9553 /* Irix 6.2 native cc creates array types without children for
9554 arrays with unspecified length. */
9555 if (die->child == NULL)
9556 {
9557 index_type = objfile_type (objfile)->builtin_int;
9558 range_type = create_range_type (NULL, index_type, 0, -1);
9559 type = create_array_type (NULL, element_type, range_type);
9560 return set_die_type (die, type, cu);
9561 }
9562
9563 back_to = make_cleanup (null_cleanup, NULL);
9564 child_die = die->child;
9565 while (child_die && child_die->tag)
9566 {
9567 if (child_die->tag == DW_TAG_subrange_type)
9568 {
9569 struct type *child_type = read_type_die (child_die, cu);
9570
9571 if (child_type != NULL)
9572 {
9573 /* The range type was succesfully read. Save it for the
9574 array type creation. */
9575 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9576 {
9577 range_types = (struct type **)
9578 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9579 * sizeof (struct type *));
9580 if (ndim == 0)
9581 make_cleanup (free_current_contents, &range_types);
9582 }
9583 range_types[ndim++] = child_type;
9584 }
9585 }
9586 child_die = sibling_die (child_die);
9587 }
9588
9589 /* Dwarf2 dimensions are output from left to right, create the
9590 necessary array types in backwards order. */
9591
9592 type = element_type;
9593
9594 if (read_array_order (die, cu) == DW_ORD_col_major)
9595 {
9596 int i = 0;
9597
9598 while (i < ndim)
9599 type = create_array_type (NULL, type, range_types[i++]);
9600 }
9601 else
9602 {
9603 while (ndim-- > 0)
9604 type = create_array_type (NULL, type, range_types[ndim]);
9605 }
9606
9607 /* Understand Dwarf2 support for vector types (like they occur on
9608 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9609 array type. This is not part of the Dwarf2/3 standard yet, but a
9610 custom vendor extension. The main difference between a regular
9611 array and the vector variant is that vectors are passed by value
9612 to functions. */
9613 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9614 if (attr)
9615 make_vector_type (type);
9616
9617 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9618 implementation may choose to implement triple vectors using this
9619 attribute. */
9620 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9621 if (attr)
9622 {
9623 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9624 TYPE_LENGTH (type) = DW_UNSND (attr);
9625 else
9626 complaint (&symfile_complaints,
9627 _("DW_AT_byte_size for array type smaller "
9628 "than the total size of elements"));
9629 }
9630
9631 name = dwarf2_name (die, cu);
9632 if (name)
9633 TYPE_NAME (type) = name;
9634
9635 /* Install the type in the die. */
9636 set_die_type (die, type, cu);
9637
9638 /* set_die_type should be already done. */
9639 set_descriptive_type (type, die, cu);
9640
9641 do_cleanups (back_to);
9642
9643 return type;
9644 }
9645
9646 static enum dwarf_array_dim_ordering
9647 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9648 {
9649 struct attribute *attr;
9650
9651 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9652
9653 if (attr) return DW_SND (attr);
9654
9655 /* GNU F77 is a special case, as at 08/2004 array type info is the
9656 opposite order to the dwarf2 specification, but data is still
9657 laid out as per normal fortran.
9658
9659 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9660 version checking. */
9661
9662 if (cu->language == language_fortran
9663 && cu->producer && strstr (cu->producer, "GNU F77"))
9664 {
9665 return DW_ORD_row_major;
9666 }
9667
9668 switch (cu->language_defn->la_array_ordering)
9669 {
9670 case array_column_major:
9671 return DW_ORD_col_major;
9672 case array_row_major:
9673 default:
9674 return DW_ORD_row_major;
9675 };
9676 }
9677
9678 /* Extract all information from a DW_TAG_set_type DIE and put it in
9679 the DIE's type field. */
9680
9681 static struct type *
9682 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9683 {
9684 struct type *domain_type, *set_type;
9685 struct attribute *attr;
9686
9687 domain_type = die_type (die, cu);
9688
9689 /* The die_type call above may have already set the type for this DIE. */
9690 set_type = get_die_type (die, cu);
9691 if (set_type)
9692 return set_type;
9693
9694 set_type = create_set_type (NULL, domain_type);
9695
9696 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9697 if (attr)
9698 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9699
9700 return set_die_type (die, set_type, cu);
9701 }
9702
9703 /* First cut: install each common block member as a global variable. */
9704
9705 static void
9706 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9707 {
9708 struct die_info *child_die;
9709 struct attribute *attr;
9710 struct symbol *sym;
9711 CORE_ADDR base = (CORE_ADDR) 0;
9712
9713 attr = dwarf2_attr (die, DW_AT_location, cu);
9714 if (attr)
9715 {
9716 /* Support the .debug_loc offsets. */
9717 if (attr_form_is_block (attr))
9718 {
9719 base = decode_locdesc (DW_BLOCK (attr), cu);
9720 }
9721 else if (attr_form_is_section_offset (attr))
9722 {
9723 dwarf2_complex_location_expr_complaint ();
9724 }
9725 else
9726 {
9727 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9728 "common block member");
9729 }
9730 }
9731 if (die->child != NULL)
9732 {
9733 child_die = die->child;
9734 while (child_die && child_die->tag)
9735 {
9736 LONGEST offset;
9737
9738 sym = new_symbol (child_die, NULL, cu);
9739 if (sym != NULL
9740 && handle_data_member_location (child_die, cu, &offset))
9741 {
9742 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9743 add_symbol_to_list (sym, &global_symbols);
9744 }
9745 child_die = sibling_die (child_die);
9746 }
9747 }
9748 }
9749
9750 /* Create a type for a C++ namespace. */
9751
9752 static struct type *
9753 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9754 {
9755 struct objfile *objfile = cu->objfile;
9756 const char *previous_prefix, *name;
9757 int is_anonymous;
9758 struct type *type;
9759
9760 /* For extensions, reuse the type of the original namespace. */
9761 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9762 {
9763 struct die_info *ext_die;
9764 struct dwarf2_cu *ext_cu = cu;
9765
9766 ext_die = dwarf2_extension (die, &ext_cu);
9767 type = read_type_die (ext_die, ext_cu);
9768
9769 /* EXT_CU may not be the same as CU.
9770 Ensure TYPE is recorded in CU's type_hash table. */
9771 return set_die_type (die, type, cu);
9772 }
9773
9774 name = namespace_name (die, &is_anonymous, cu);
9775
9776 /* Now build the name of the current namespace. */
9777
9778 previous_prefix = determine_prefix (die, cu);
9779 if (previous_prefix[0] != '\0')
9780 name = typename_concat (&objfile->objfile_obstack,
9781 previous_prefix, name, 0, cu);
9782
9783 /* Create the type. */
9784 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9785 objfile);
9786 TYPE_NAME (type) = (char *) name;
9787 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9788
9789 return set_die_type (die, type, cu);
9790 }
9791
9792 /* Read a C++ namespace. */
9793
9794 static void
9795 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9796 {
9797 struct objfile *objfile = cu->objfile;
9798 int is_anonymous;
9799
9800 /* Add a symbol associated to this if we haven't seen the namespace
9801 before. Also, add a using directive if it's an anonymous
9802 namespace. */
9803
9804 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
9805 {
9806 struct type *type;
9807
9808 type = read_type_die (die, cu);
9809 new_symbol (die, type, cu);
9810
9811 namespace_name (die, &is_anonymous, cu);
9812 if (is_anonymous)
9813 {
9814 const char *previous_prefix = determine_prefix (die, cu);
9815
9816 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
9817 NULL, NULL, &objfile->objfile_obstack);
9818 }
9819 }
9820
9821 if (die->child != NULL)
9822 {
9823 struct die_info *child_die = die->child;
9824
9825 while (child_die && child_die->tag)
9826 {
9827 process_die (child_die, cu);
9828 child_die = sibling_die (child_die);
9829 }
9830 }
9831 }
9832
9833 /* Read a Fortran module as type. This DIE can be only a declaration used for
9834 imported module. Still we need that type as local Fortran "use ... only"
9835 declaration imports depend on the created type in determine_prefix. */
9836
9837 static struct type *
9838 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9839 {
9840 struct objfile *objfile = cu->objfile;
9841 char *module_name;
9842 struct type *type;
9843
9844 module_name = dwarf2_name (die, cu);
9845 if (!module_name)
9846 complaint (&symfile_complaints,
9847 _("DW_TAG_module has no name, offset 0x%x"),
9848 die->offset.sect_off);
9849 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9850
9851 /* determine_prefix uses TYPE_TAG_NAME. */
9852 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9853
9854 return set_die_type (die, type, cu);
9855 }
9856
9857 /* Read a Fortran module. */
9858
9859 static void
9860 read_module (struct die_info *die, struct dwarf2_cu *cu)
9861 {
9862 struct die_info *child_die = die->child;
9863
9864 while (child_die && child_die->tag)
9865 {
9866 process_die (child_die, cu);
9867 child_die = sibling_die (child_die);
9868 }
9869 }
9870
9871 /* Return the name of the namespace represented by DIE. Set
9872 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9873 namespace. */
9874
9875 static const char *
9876 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
9877 {
9878 struct die_info *current_die;
9879 const char *name = NULL;
9880
9881 /* Loop through the extensions until we find a name. */
9882
9883 for (current_die = die;
9884 current_die != NULL;
9885 current_die = dwarf2_extension (die, &cu))
9886 {
9887 name = dwarf2_name (current_die, cu);
9888 if (name != NULL)
9889 break;
9890 }
9891
9892 /* Is it an anonymous namespace? */
9893
9894 *is_anonymous = (name == NULL);
9895 if (*is_anonymous)
9896 name = CP_ANONYMOUS_NAMESPACE_STR;
9897
9898 return name;
9899 }
9900
9901 /* Extract all information from a DW_TAG_pointer_type DIE and add to
9902 the user defined type vector. */
9903
9904 static struct type *
9905 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
9906 {
9907 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9908 struct comp_unit_head *cu_header = &cu->header;
9909 struct type *type;
9910 struct attribute *attr_byte_size;
9911 struct attribute *attr_address_class;
9912 int byte_size, addr_class;
9913 struct type *target_type;
9914
9915 target_type = die_type (die, cu);
9916
9917 /* The die_type call above may have already set the type for this DIE. */
9918 type = get_die_type (die, cu);
9919 if (type)
9920 return type;
9921
9922 type = lookup_pointer_type (target_type);
9923
9924 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
9925 if (attr_byte_size)
9926 byte_size = DW_UNSND (attr_byte_size);
9927 else
9928 byte_size = cu_header->addr_size;
9929
9930 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
9931 if (attr_address_class)
9932 addr_class = DW_UNSND (attr_address_class);
9933 else
9934 addr_class = DW_ADDR_none;
9935
9936 /* If the pointer size or address class is different than the
9937 default, create a type variant marked as such and set the
9938 length accordingly. */
9939 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
9940 {
9941 if (gdbarch_address_class_type_flags_p (gdbarch))
9942 {
9943 int type_flags;
9944
9945 type_flags = gdbarch_address_class_type_flags
9946 (gdbarch, byte_size, addr_class);
9947 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9948 == 0);
9949 type = make_type_with_address_space (type, type_flags);
9950 }
9951 else if (TYPE_LENGTH (type) != byte_size)
9952 {
9953 complaint (&symfile_complaints,
9954 _("invalid pointer size %d"), byte_size);
9955 }
9956 else
9957 {
9958 /* Should we also complain about unhandled address classes? */
9959 }
9960 }
9961
9962 TYPE_LENGTH (type) = byte_size;
9963 return set_die_type (die, type, cu);
9964 }
9965
9966 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9967 the user defined type vector. */
9968
9969 static struct type *
9970 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
9971 {
9972 struct type *type;
9973 struct type *to_type;
9974 struct type *domain;
9975
9976 to_type = die_type (die, cu);
9977 domain = die_containing_type (die, cu);
9978
9979 /* The calls above may have already set the type for this DIE. */
9980 type = get_die_type (die, cu);
9981 if (type)
9982 return type;
9983
9984 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9985 type = lookup_methodptr_type (to_type);
9986 else
9987 type = lookup_memberptr_type (to_type, domain);
9988
9989 return set_die_type (die, type, cu);
9990 }
9991
9992 /* Extract all information from a DW_TAG_reference_type DIE and add to
9993 the user defined type vector. */
9994
9995 static struct type *
9996 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
9997 {
9998 struct comp_unit_head *cu_header = &cu->header;
9999 struct type *type, *target_type;
10000 struct attribute *attr;
10001
10002 target_type = die_type (die, cu);
10003
10004 /* The die_type call above may have already set the type for this DIE. */
10005 type = get_die_type (die, cu);
10006 if (type)
10007 return type;
10008
10009 type = lookup_reference_type (target_type);
10010 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10011 if (attr)
10012 {
10013 TYPE_LENGTH (type) = DW_UNSND (attr);
10014 }
10015 else
10016 {
10017 TYPE_LENGTH (type) = cu_header->addr_size;
10018 }
10019 return set_die_type (die, type, cu);
10020 }
10021
10022 static struct type *
10023 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
10024 {
10025 struct type *base_type, *cv_type;
10026
10027 base_type = die_type (die, cu);
10028
10029 /* The die_type call above may have already set the type for this DIE. */
10030 cv_type = get_die_type (die, cu);
10031 if (cv_type)
10032 return cv_type;
10033
10034 /* In case the const qualifier is applied to an array type, the element type
10035 is so qualified, not the array type (section 6.7.3 of C99). */
10036 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10037 {
10038 struct type *el_type, *inner_array;
10039
10040 base_type = copy_type (base_type);
10041 inner_array = base_type;
10042
10043 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10044 {
10045 TYPE_TARGET_TYPE (inner_array) =
10046 copy_type (TYPE_TARGET_TYPE (inner_array));
10047 inner_array = TYPE_TARGET_TYPE (inner_array);
10048 }
10049
10050 el_type = TYPE_TARGET_TYPE (inner_array);
10051 TYPE_TARGET_TYPE (inner_array) =
10052 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10053
10054 return set_die_type (die, base_type, cu);
10055 }
10056
10057 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10058 return set_die_type (die, cv_type, cu);
10059 }
10060
10061 static struct type *
10062 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
10063 {
10064 struct type *base_type, *cv_type;
10065
10066 base_type = die_type (die, cu);
10067
10068 /* The die_type call above may have already set the type for this DIE. */
10069 cv_type = get_die_type (die, cu);
10070 if (cv_type)
10071 return cv_type;
10072
10073 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10074 return set_die_type (die, cv_type, cu);
10075 }
10076
10077 /* Extract all information from a DW_TAG_string_type DIE and add to
10078 the user defined type vector. It isn't really a user defined type,
10079 but it behaves like one, with other DIE's using an AT_user_def_type
10080 attribute to reference it. */
10081
10082 static struct type *
10083 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
10084 {
10085 struct objfile *objfile = cu->objfile;
10086 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10087 struct type *type, *range_type, *index_type, *char_type;
10088 struct attribute *attr;
10089 unsigned int length;
10090
10091 attr = dwarf2_attr (die, DW_AT_string_length, cu);
10092 if (attr)
10093 {
10094 length = DW_UNSND (attr);
10095 }
10096 else
10097 {
10098 /* Check for the DW_AT_byte_size attribute. */
10099 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10100 if (attr)
10101 {
10102 length = DW_UNSND (attr);
10103 }
10104 else
10105 {
10106 length = 1;
10107 }
10108 }
10109
10110 index_type = objfile_type (objfile)->builtin_int;
10111 range_type = create_range_type (NULL, index_type, 1, length);
10112 char_type = language_string_char_type (cu->language_defn, gdbarch);
10113 type = create_string_type (NULL, char_type, range_type);
10114
10115 return set_die_type (die, type, cu);
10116 }
10117
10118 /* Handle DIES due to C code like:
10119
10120 struct foo
10121 {
10122 int (*funcp)(int a, long l);
10123 int b;
10124 };
10125
10126 ('funcp' generates a DW_TAG_subroutine_type DIE). */
10127
10128 static struct type *
10129 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
10130 {
10131 struct objfile *objfile = cu->objfile;
10132 struct type *type; /* Type that this function returns. */
10133 struct type *ftype; /* Function that returns above type. */
10134 struct attribute *attr;
10135
10136 type = die_type (die, cu);
10137
10138 /* The die_type call above may have already set the type for this DIE. */
10139 ftype = get_die_type (die, cu);
10140 if (ftype)
10141 return ftype;
10142
10143 ftype = lookup_function_type (type);
10144
10145 /* All functions in C++, Pascal and Java have prototypes. */
10146 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
10147 if ((attr && (DW_UNSND (attr) != 0))
10148 || cu->language == language_cplus
10149 || cu->language == language_java
10150 || cu->language == language_pascal)
10151 TYPE_PROTOTYPED (ftype) = 1;
10152 else if (producer_is_realview (cu->producer))
10153 /* RealView does not emit DW_AT_prototyped. We can not
10154 distinguish prototyped and unprototyped functions; default to
10155 prototyped, since that is more common in modern code (and
10156 RealView warns about unprototyped functions). */
10157 TYPE_PROTOTYPED (ftype) = 1;
10158
10159 /* Store the calling convention in the type if it's available in
10160 the subroutine die. Otherwise set the calling convention to
10161 the default value DW_CC_normal. */
10162 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
10163 if (attr)
10164 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10165 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10166 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10167 else
10168 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
10169
10170 /* We need to add the subroutine type to the die immediately so
10171 we don't infinitely recurse when dealing with parameters
10172 declared as the same subroutine type. */
10173 set_die_type (die, ftype, cu);
10174
10175 if (die->child != NULL)
10176 {
10177 struct type *void_type = objfile_type (objfile)->builtin_void;
10178 struct die_info *child_die;
10179 int nparams, iparams;
10180
10181 /* Count the number of parameters.
10182 FIXME: GDB currently ignores vararg functions, but knows about
10183 vararg member functions. */
10184 nparams = 0;
10185 child_die = die->child;
10186 while (child_die && child_die->tag)
10187 {
10188 if (child_die->tag == DW_TAG_formal_parameter)
10189 nparams++;
10190 else if (child_die->tag == DW_TAG_unspecified_parameters)
10191 TYPE_VARARGS (ftype) = 1;
10192 child_die = sibling_die (child_die);
10193 }
10194
10195 /* Allocate storage for parameters and fill them in. */
10196 TYPE_NFIELDS (ftype) = nparams;
10197 TYPE_FIELDS (ftype) = (struct field *)
10198 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
10199
10200 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10201 even if we error out during the parameters reading below. */
10202 for (iparams = 0; iparams < nparams; iparams++)
10203 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10204
10205 iparams = 0;
10206 child_die = die->child;
10207 while (child_die && child_die->tag)
10208 {
10209 if (child_die->tag == DW_TAG_formal_parameter)
10210 {
10211 struct type *arg_type;
10212
10213 /* DWARF version 2 has no clean way to discern C++
10214 static and non-static member functions. G++ helps
10215 GDB by marking the first parameter for non-static
10216 member functions (which is the this pointer) as
10217 artificial. We pass this information to
10218 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10219
10220 DWARF version 3 added DW_AT_object_pointer, which GCC
10221 4.5 does not yet generate. */
10222 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
10223 if (attr)
10224 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10225 else
10226 {
10227 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10228
10229 /* GCC/43521: In java, the formal parameter
10230 "this" is sometimes not marked with DW_AT_artificial. */
10231 if (cu->language == language_java)
10232 {
10233 const char *name = dwarf2_name (child_die, cu);
10234
10235 if (name && !strcmp (name, "this"))
10236 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10237 }
10238 }
10239 arg_type = die_type (child_die, cu);
10240
10241 /* RealView does not mark THIS as const, which the testsuite
10242 expects. GCC marks THIS as const in method definitions,
10243 but not in the class specifications (GCC PR 43053). */
10244 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10245 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10246 {
10247 int is_this = 0;
10248 struct dwarf2_cu *arg_cu = cu;
10249 const char *name = dwarf2_name (child_die, cu);
10250
10251 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10252 if (attr)
10253 {
10254 /* If the compiler emits this, use it. */
10255 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10256 is_this = 1;
10257 }
10258 else if (name && strcmp (name, "this") == 0)
10259 /* Function definitions will have the argument names. */
10260 is_this = 1;
10261 else if (name == NULL && iparams == 0)
10262 /* Declarations may not have the names, so like
10263 elsewhere in GDB, assume an artificial first
10264 argument is "this". */
10265 is_this = 1;
10266
10267 if (is_this)
10268 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10269 arg_type, 0);
10270 }
10271
10272 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10273 iparams++;
10274 }
10275 child_die = sibling_die (child_die);
10276 }
10277 }
10278
10279 return ftype;
10280 }
10281
10282 static struct type *
10283 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10284 {
10285 struct objfile *objfile = cu->objfile;
10286 const char *name = NULL;
10287 struct type *this_type, *target_type;
10288
10289 name = dwarf2_full_name (NULL, die, cu);
10290 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10291 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10292 TYPE_NAME (this_type) = (char *) name;
10293 set_die_type (die, this_type, cu);
10294 target_type = die_type (die, cu);
10295 if (target_type != this_type)
10296 TYPE_TARGET_TYPE (this_type) = target_type;
10297 else
10298 {
10299 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10300 spec and cause infinite loops in GDB. */
10301 complaint (&symfile_complaints,
10302 _("Self-referential DW_TAG_typedef "
10303 "- DIE at 0x%x [in module %s]"),
10304 die->offset.sect_off, objfile->name);
10305 TYPE_TARGET_TYPE (this_type) = NULL;
10306 }
10307 return this_type;
10308 }
10309
10310 /* Find a representation of a given base type and install
10311 it in the TYPE field of the die. */
10312
10313 static struct type *
10314 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10315 {
10316 struct objfile *objfile = cu->objfile;
10317 struct type *type;
10318 struct attribute *attr;
10319 int encoding = 0, size = 0;
10320 char *name;
10321 enum type_code code = TYPE_CODE_INT;
10322 int type_flags = 0;
10323 struct type *target_type = NULL;
10324
10325 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10326 if (attr)
10327 {
10328 encoding = DW_UNSND (attr);
10329 }
10330 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10331 if (attr)
10332 {
10333 size = DW_UNSND (attr);
10334 }
10335 name = dwarf2_name (die, cu);
10336 if (!name)
10337 {
10338 complaint (&symfile_complaints,
10339 _("DW_AT_name missing from DW_TAG_base_type"));
10340 }
10341
10342 switch (encoding)
10343 {
10344 case DW_ATE_address:
10345 /* Turn DW_ATE_address into a void * pointer. */
10346 code = TYPE_CODE_PTR;
10347 type_flags |= TYPE_FLAG_UNSIGNED;
10348 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10349 break;
10350 case DW_ATE_boolean:
10351 code = TYPE_CODE_BOOL;
10352 type_flags |= TYPE_FLAG_UNSIGNED;
10353 break;
10354 case DW_ATE_complex_float:
10355 code = TYPE_CODE_COMPLEX;
10356 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10357 break;
10358 case DW_ATE_decimal_float:
10359 code = TYPE_CODE_DECFLOAT;
10360 break;
10361 case DW_ATE_float:
10362 code = TYPE_CODE_FLT;
10363 break;
10364 case DW_ATE_signed:
10365 break;
10366 case DW_ATE_unsigned:
10367 type_flags |= TYPE_FLAG_UNSIGNED;
10368 if (cu->language == language_fortran
10369 && name
10370 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10371 code = TYPE_CODE_CHAR;
10372 break;
10373 case DW_ATE_signed_char:
10374 if (cu->language == language_ada || cu->language == language_m2
10375 || cu->language == language_pascal
10376 || cu->language == language_fortran)
10377 code = TYPE_CODE_CHAR;
10378 break;
10379 case DW_ATE_unsigned_char:
10380 if (cu->language == language_ada || cu->language == language_m2
10381 || cu->language == language_pascal
10382 || cu->language == language_fortran)
10383 code = TYPE_CODE_CHAR;
10384 type_flags |= TYPE_FLAG_UNSIGNED;
10385 break;
10386 case DW_ATE_UTF:
10387 /* We just treat this as an integer and then recognize the
10388 type by name elsewhere. */
10389 break;
10390
10391 default:
10392 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10393 dwarf_type_encoding_name (encoding));
10394 break;
10395 }
10396
10397 type = init_type (code, size, type_flags, NULL, objfile);
10398 TYPE_NAME (type) = name;
10399 TYPE_TARGET_TYPE (type) = target_type;
10400
10401 if (name && strcmp (name, "char") == 0)
10402 TYPE_NOSIGN (type) = 1;
10403
10404 return set_die_type (die, type, cu);
10405 }
10406
10407 /* Read the given DW_AT_subrange DIE. */
10408
10409 static struct type *
10410 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10411 {
10412 struct type *base_type;
10413 struct type *range_type;
10414 struct attribute *attr;
10415 LONGEST low, high;
10416 int low_default_is_valid;
10417 char *name;
10418 LONGEST negative_mask;
10419
10420 base_type = die_type (die, cu);
10421 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10422 check_typedef (base_type);
10423
10424 /* The die_type call above may have already set the type for this DIE. */
10425 range_type = get_die_type (die, cu);
10426 if (range_type)
10427 return range_type;
10428
10429 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10430 omitting DW_AT_lower_bound. */
10431 switch (cu->language)
10432 {
10433 case language_c:
10434 case language_cplus:
10435 low = 0;
10436 low_default_is_valid = 1;
10437 break;
10438 case language_fortran:
10439 low = 1;
10440 low_default_is_valid = 1;
10441 break;
10442 case language_d:
10443 case language_java:
10444 case language_objc:
10445 low = 0;
10446 low_default_is_valid = (cu->header.version >= 4);
10447 break;
10448 case language_ada:
10449 case language_m2:
10450 case language_pascal:
10451 low = 1;
10452 low_default_is_valid = (cu->header.version >= 4);
10453 break;
10454 default:
10455 low = 0;
10456 low_default_is_valid = 0;
10457 break;
10458 }
10459
10460 /* FIXME: For variable sized arrays either of these could be
10461 a variable rather than a constant value. We'll allow it,
10462 but we don't know how to handle it. */
10463 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10464 if (attr)
10465 low = dwarf2_get_attr_constant_value (attr, low);
10466 else if (!low_default_is_valid)
10467 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10468 "- DIE at 0x%x [in module %s]"),
10469 die->offset.sect_off, cu->objfile->name);
10470
10471 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10472 if (attr)
10473 {
10474 if (attr_form_is_block (attr) || is_ref_attr (attr))
10475 {
10476 /* GCC encodes arrays with unspecified or dynamic length
10477 with a DW_FORM_block1 attribute or a reference attribute.
10478 FIXME: GDB does not yet know how to handle dynamic
10479 arrays properly, treat them as arrays with unspecified
10480 length for now.
10481
10482 FIXME: jimb/2003-09-22: GDB does not really know
10483 how to handle arrays of unspecified length
10484 either; we just represent them as zero-length
10485 arrays. Choose an appropriate upper bound given
10486 the lower bound we've computed above. */
10487 high = low - 1;
10488 }
10489 else
10490 high = dwarf2_get_attr_constant_value (attr, 1);
10491 }
10492 else
10493 {
10494 attr = dwarf2_attr (die, DW_AT_count, cu);
10495 if (attr)
10496 {
10497 int count = dwarf2_get_attr_constant_value (attr, 1);
10498 high = low + count - 1;
10499 }
10500 else
10501 {
10502 /* Unspecified array length. */
10503 high = low - 1;
10504 }
10505 }
10506
10507 /* Dwarf-2 specifications explicitly allows to create subrange types
10508 without specifying a base type.
10509 In that case, the base type must be set to the type of
10510 the lower bound, upper bound or count, in that order, if any of these
10511 three attributes references an object that has a type.
10512 If no base type is found, the Dwarf-2 specifications say that
10513 a signed integer type of size equal to the size of an address should
10514 be used.
10515 For the following C code: `extern char gdb_int [];'
10516 GCC produces an empty range DIE.
10517 FIXME: muller/2010-05-28: Possible references to object for low bound,
10518 high bound or count are not yet handled by this code. */
10519 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10520 {
10521 struct objfile *objfile = cu->objfile;
10522 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10523 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10524 struct type *int_type = objfile_type (objfile)->builtin_int;
10525
10526 /* Test "int", "long int", and "long long int" objfile types,
10527 and select the first one having a size above or equal to the
10528 architecture address size. */
10529 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10530 base_type = int_type;
10531 else
10532 {
10533 int_type = objfile_type (objfile)->builtin_long;
10534 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10535 base_type = int_type;
10536 else
10537 {
10538 int_type = objfile_type (objfile)->builtin_long_long;
10539 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10540 base_type = int_type;
10541 }
10542 }
10543 }
10544
10545 negative_mask =
10546 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10547 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10548 low |= negative_mask;
10549 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10550 high |= negative_mask;
10551
10552 range_type = create_range_type (NULL, base_type, low, high);
10553
10554 /* Mark arrays with dynamic length at least as an array of unspecified
10555 length. GDB could check the boundary but before it gets implemented at
10556 least allow accessing the array elements. */
10557 if (attr && attr_form_is_block (attr))
10558 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10559
10560 /* Ada expects an empty array on no boundary attributes. */
10561 if (attr == NULL && cu->language != language_ada)
10562 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10563
10564 name = dwarf2_name (die, cu);
10565 if (name)
10566 TYPE_NAME (range_type) = name;
10567
10568 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10569 if (attr)
10570 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10571
10572 set_die_type (die, range_type, cu);
10573
10574 /* set_die_type should be already done. */
10575 set_descriptive_type (range_type, die, cu);
10576
10577 return range_type;
10578 }
10579
10580 static struct type *
10581 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10582 {
10583 struct type *type;
10584
10585 /* For now, we only support the C meaning of an unspecified type: void. */
10586
10587 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10588 TYPE_NAME (type) = dwarf2_name (die, cu);
10589
10590 return set_die_type (die, type, cu);
10591 }
10592
10593 /* Read a single die and all its descendents. Set the die's sibling
10594 field to NULL; set other fields in the die correctly, and set all
10595 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10596 location of the info_ptr after reading all of those dies. PARENT
10597 is the parent of the die in question. */
10598
10599 static struct die_info *
10600 read_die_and_children (const struct die_reader_specs *reader,
10601 gdb_byte *info_ptr,
10602 gdb_byte **new_info_ptr,
10603 struct die_info *parent)
10604 {
10605 struct die_info *die;
10606 gdb_byte *cur_ptr;
10607 int has_children;
10608
10609 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10610 if (die == NULL)
10611 {
10612 *new_info_ptr = cur_ptr;
10613 return NULL;
10614 }
10615 store_in_ref_table (die, reader->cu);
10616
10617 if (has_children)
10618 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10619 else
10620 {
10621 die->child = NULL;
10622 *new_info_ptr = cur_ptr;
10623 }
10624
10625 die->sibling = NULL;
10626 die->parent = parent;
10627 return die;
10628 }
10629
10630 /* Read a die, all of its descendents, and all of its siblings; set
10631 all of the fields of all of the dies correctly. Arguments are as
10632 in read_die_and_children. */
10633
10634 static struct die_info *
10635 read_die_and_siblings (const struct die_reader_specs *reader,
10636 gdb_byte *info_ptr,
10637 gdb_byte **new_info_ptr,
10638 struct die_info *parent)
10639 {
10640 struct die_info *first_die, *last_sibling;
10641 gdb_byte *cur_ptr;
10642
10643 cur_ptr = info_ptr;
10644 first_die = last_sibling = NULL;
10645
10646 while (1)
10647 {
10648 struct die_info *die
10649 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10650
10651 if (die == NULL)
10652 {
10653 *new_info_ptr = cur_ptr;
10654 return first_die;
10655 }
10656
10657 if (!first_die)
10658 first_die = die;
10659 else
10660 last_sibling->sibling = die;
10661
10662 last_sibling = die;
10663 }
10664 }
10665
10666 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10667 attributes.
10668 The caller is responsible for filling in the extra attributes
10669 and updating (*DIEP)->num_attrs.
10670 Set DIEP to point to a newly allocated die with its information,
10671 except for its child, sibling, and parent fields.
10672 Set HAS_CHILDREN to tell whether the die has children or not. */
10673
10674 static gdb_byte *
10675 read_full_die_1 (const struct die_reader_specs *reader,
10676 struct die_info **diep, gdb_byte *info_ptr,
10677 int *has_children, int num_extra_attrs)
10678 {
10679 unsigned int abbrev_number, bytes_read, i;
10680 sect_offset offset;
10681 struct abbrev_info *abbrev;
10682 struct die_info *die;
10683 struct dwarf2_cu *cu = reader->cu;
10684 bfd *abfd = reader->abfd;
10685
10686 offset.sect_off = info_ptr - reader->buffer;
10687 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10688 info_ptr += bytes_read;
10689 if (!abbrev_number)
10690 {
10691 *diep = NULL;
10692 *has_children = 0;
10693 return info_ptr;
10694 }
10695
10696 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10697 if (!abbrev)
10698 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10699 abbrev_number,
10700 bfd_get_filename (abfd));
10701
10702 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10703 die->offset = offset;
10704 die->tag = abbrev->tag;
10705 die->abbrev = abbrev_number;
10706
10707 /* Make the result usable.
10708 The caller needs to update num_attrs after adding the extra
10709 attributes. */
10710 die->num_attrs = abbrev->num_attrs;
10711
10712 for (i = 0; i < abbrev->num_attrs; ++i)
10713 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10714 info_ptr);
10715
10716 *diep = die;
10717 *has_children = abbrev->has_children;
10718 return info_ptr;
10719 }
10720
10721 /* Read a die and all its attributes.
10722 Set DIEP to point to a newly allocated die with its information,
10723 except for its child, sibling, and parent fields.
10724 Set HAS_CHILDREN to tell whether the die has children or not. */
10725
10726 static gdb_byte *
10727 read_full_die (const struct die_reader_specs *reader,
10728 struct die_info **diep, gdb_byte *info_ptr,
10729 int *has_children)
10730 {
10731 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10732 }
10733
10734 /* In DWARF version 2, the description of the debugging information is
10735 stored in a separate .debug_abbrev section. Before we read any
10736 dies from a section we read in all abbreviations and install them
10737 in a hash table. This function also sets flags in CU describing
10738 the data found in the abbrev table. */
10739
10740 static void
10741 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10742 struct dwarf2_section_info *abbrev_section)
10743
10744 {
10745 bfd *abfd = abbrev_section->asection->owner;
10746 struct comp_unit_head *cu_header = &cu->header;
10747 gdb_byte *abbrev_ptr;
10748 struct abbrev_info *cur_abbrev;
10749 unsigned int abbrev_number, bytes_read, abbrev_name;
10750 unsigned int abbrev_form, hash_number;
10751 struct attr_abbrev *cur_attrs;
10752 unsigned int allocated_attrs;
10753
10754 /* Initialize dwarf2 abbrevs. */
10755 obstack_init (&cu->abbrev_obstack);
10756 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10757 (ABBREV_HASH_SIZE
10758 * sizeof (struct abbrev_info *)));
10759 memset (cu->dwarf2_abbrevs, 0,
10760 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10761
10762 dwarf2_read_section (cu->objfile, abbrev_section);
10763 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10764 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10765 abbrev_ptr += bytes_read;
10766
10767 allocated_attrs = ATTR_ALLOC_CHUNK;
10768 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10769
10770 /* Loop until we reach an abbrev number of 0. */
10771 while (abbrev_number)
10772 {
10773 cur_abbrev = dwarf_alloc_abbrev (cu);
10774
10775 /* read in abbrev header */
10776 cur_abbrev->number = abbrev_number;
10777 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10778 abbrev_ptr += bytes_read;
10779 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10780 abbrev_ptr += 1;
10781
10782 /* now read in declarations */
10783 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10784 abbrev_ptr += bytes_read;
10785 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10786 abbrev_ptr += bytes_read;
10787 while (abbrev_name)
10788 {
10789 if (cur_abbrev->num_attrs == allocated_attrs)
10790 {
10791 allocated_attrs += ATTR_ALLOC_CHUNK;
10792 cur_attrs
10793 = xrealloc (cur_attrs, (allocated_attrs
10794 * sizeof (struct attr_abbrev)));
10795 }
10796
10797 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10798 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
10799 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10800 abbrev_ptr += bytes_read;
10801 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10802 abbrev_ptr += bytes_read;
10803 }
10804
10805 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10806 (cur_abbrev->num_attrs
10807 * sizeof (struct attr_abbrev)));
10808 memcpy (cur_abbrev->attrs, cur_attrs,
10809 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10810
10811 hash_number = abbrev_number % ABBREV_HASH_SIZE;
10812 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10813 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
10814
10815 /* Get next abbreviation.
10816 Under Irix6 the abbreviations for a compilation unit are not
10817 always properly terminated with an abbrev number of 0.
10818 Exit loop if we encounter an abbreviation which we have
10819 already read (which means we are about to read the abbreviations
10820 for the next compile unit) or if the end of the abbreviation
10821 table is reached. */
10822 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10823 >= abbrev_section->size)
10824 break;
10825 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10826 abbrev_ptr += bytes_read;
10827 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
10828 break;
10829 }
10830
10831 xfree (cur_attrs);
10832 }
10833
10834 /* Release the memory used by the abbrev table for a compilation unit. */
10835
10836 static void
10837 dwarf2_free_abbrev_table (void *ptr_to_cu)
10838 {
10839 struct dwarf2_cu *cu = ptr_to_cu;
10840
10841 obstack_free (&cu->abbrev_obstack, NULL);
10842 cu->dwarf2_abbrevs = NULL;
10843 }
10844
10845 /* Lookup an abbrev_info structure in the abbrev hash table. */
10846
10847 static struct abbrev_info *
10848 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
10849 {
10850 unsigned int hash_number;
10851 struct abbrev_info *abbrev;
10852
10853 hash_number = number % ABBREV_HASH_SIZE;
10854 abbrev = cu->dwarf2_abbrevs[hash_number];
10855
10856 while (abbrev)
10857 {
10858 if (abbrev->number == number)
10859 return abbrev;
10860 else
10861 abbrev = abbrev->next;
10862 }
10863 return NULL;
10864 }
10865
10866 /* Returns nonzero if TAG represents a type that we might generate a partial
10867 symbol for. */
10868
10869 static int
10870 is_type_tag_for_partial (int tag)
10871 {
10872 switch (tag)
10873 {
10874 #if 0
10875 /* Some types that would be reasonable to generate partial symbols for,
10876 that we don't at present. */
10877 case DW_TAG_array_type:
10878 case DW_TAG_file_type:
10879 case DW_TAG_ptr_to_member_type:
10880 case DW_TAG_set_type:
10881 case DW_TAG_string_type:
10882 case DW_TAG_subroutine_type:
10883 #endif
10884 case DW_TAG_base_type:
10885 case DW_TAG_class_type:
10886 case DW_TAG_interface_type:
10887 case DW_TAG_enumeration_type:
10888 case DW_TAG_structure_type:
10889 case DW_TAG_subrange_type:
10890 case DW_TAG_typedef:
10891 case DW_TAG_union_type:
10892 return 1;
10893 default:
10894 return 0;
10895 }
10896 }
10897
10898 /* Load all DIEs that are interesting for partial symbols into memory. */
10899
10900 static struct partial_die_info *
10901 load_partial_dies (const struct die_reader_specs *reader,
10902 gdb_byte *info_ptr, int building_psymtab)
10903 {
10904 struct dwarf2_cu *cu = reader->cu;
10905 struct objfile *objfile = cu->objfile;
10906 struct partial_die_info *part_die;
10907 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10908 struct abbrev_info *abbrev;
10909 unsigned int bytes_read;
10910 unsigned int load_all = 0;
10911 int nesting_level = 1;
10912
10913 parent_die = NULL;
10914 last_die = NULL;
10915
10916 gdb_assert (cu->per_cu != NULL);
10917 if (cu->per_cu->load_all_dies)
10918 load_all = 1;
10919
10920 cu->partial_dies
10921 = htab_create_alloc_ex (cu->header.length / 12,
10922 partial_die_hash,
10923 partial_die_eq,
10924 NULL,
10925 &cu->comp_unit_obstack,
10926 hashtab_obstack_allocate,
10927 dummy_obstack_deallocate);
10928
10929 part_die = obstack_alloc (&cu->comp_unit_obstack,
10930 sizeof (struct partial_die_info));
10931
10932 while (1)
10933 {
10934 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10935
10936 /* A NULL abbrev means the end of a series of children. */
10937 if (abbrev == NULL)
10938 {
10939 if (--nesting_level == 0)
10940 {
10941 /* PART_DIE was probably the last thing allocated on the
10942 comp_unit_obstack, so we could call obstack_free
10943 here. We don't do that because the waste is small,
10944 and will be cleaned up when we're done with this
10945 compilation unit. This way, we're also more robust
10946 against other users of the comp_unit_obstack. */
10947 return first_die;
10948 }
10949 info_ptr += bytes_read;
10950 last_die = parent_die;
10951 parent_die = parent_die->die_parent;
10952 continue;
10953 }
10954
10955 /* Check for template arguments. We never save these; if
10956 they're seen, we just mark the parent, and go on our way. */
10957 if (parent_die != NULL
10958 && cu->language == language_cplus
10959 && (abbrev->tag == DW_TAG_template_type_param
10960 || abbrev->tag == DW_TAG_template_value_param))
10961 {
10962 parent_die->has_template_arguments = 1;
10963
10964 if (!load_all)
10965 {
10966 /* We don't need a partial DIE for the template argument. */
10967 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10968 continue;
10969 }
10970 }
10971
10972 /* We only recurse into c++ subprograms looking for template arguments.
10973 Skip their other children. */
10974 if (!load_all
10975 && cu->language == language_cplus
10976 && parent_die != NULL
10977 && parent_die->tag == DW_TAG_subprogram)
10978 {
10979 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10980 continue;
10981 }
10982
10983 /* Check whether this DIE is interesting enough to save. Normally
10984 we would not be interested in members here, but there may be
10985 later variables referencing them via DW_AT_specification (for
10986 static members). */
10987 if (!load_all
10988 && !is_type_tag_for_partial (abbrev->tag)
10989 && abbrev->tag != DW_TAG_constant
10990 && abbrev->tag != DW_TAG_enumerator
10991 && abbrev->tag != DW_TAG_subprogram
10992 && abbrev->tag != DW_TAG_lexical_block
10993 && abbrev->tag != DW_TAG_variable
10994 && abbrev->tag != DW_TAG_namespace
10995 && abbrev->tag != DW_TAG_module
10996 && abbrev->tag != DW_TAG_member
10997 && abbrev->tag != DW_TAG_imported_unit)
10998 {
10999 /* Otherwise we skip to the next sibling, if any. */
11000 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
11001 continue;
11002 }
11003
11004 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11005 info_ptr);
11006
11007 /* This two-pass algorithm for processing partial symbols has a
11008 high cost in cache pressure. Thus, handle some simple cases
11009 here which cover the majority of C partial symbols. DIEs
11010 which neither have specification tags in them, nor could have
11011 specification tags elsewhere pointing at them, can simply be
11012 processed and discarded.
11013
11014 This segment is also optional; scan_partial_symbols and
11015 add_partial_symbol will handle these DIEs if we chain
11016 them in normally. When compilers which do not emit large
11017 quantities of duplicate debug information are more common,
11018 this code can probably be removed. */
11019
11020 /* Any complete simple types at the top level (pretty much all
11021 of them, for a language without namespaces), can be processed
11022 directly. */
11023 if (parent_die == NULL
11024 && part_die->has_specification == 0
11025 && part_die->is_declaration == 0
11026 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
11027 || part_die->tag == DW_TAG_base_type
11028 || part_die->tag == DW_TAG_subrange_type))
11029 {
11030 if (building_psymtab && part_die->name != NULL)
11031 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11032 VAR_DOMAIN, LOC_TYPEDEF,
11033 &objfile->static_psymbols,
11034 0, (CORE_ADDR) 0, cu->language, objfile);
11035 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11036 continue;
11037 }
11038
11039 /* The exception for DW_TAG_typedef with has_children above is
11040 a workaround of GCC PR debug/47510. In the case of this complaint
11041 type_name_no_tag_or_error will error on such types later.
11042
11043 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11044 it could not find the child DIEs referenced later, this is checked
11045 above. In correct DWARF DW_TAG_typedef should have no children. */
11046
11047 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11048 complaint (&symfile_complaints,
11049 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11050 "- DIE at 0x%x [in module %s]"),
11051 part_die->offset.sect_off, objfile->name);
11052
11053 /* If we're at the second level, and we're an enumerator, and
11054 our parent has no specification (meaning possibly lives in a
11055 namespace elsewhere), then we can add the partial symbol now
11056 instead of queueing it. */
11057 if (part_die->tag == DW_TAG_enumerator
11058 && parent_die != NULL
11059 && parent_die->die_parent == NULL
11060 && parent_die->tag == DW_TAG_enumeration_type
11061 && parent_die->has_specification == 0)
11062 {
11063 if (part_die->name == NULL)
11064 complaint (&symfile_complaints,
11065 _("malformed enumerator DIE ignored"));
11066 else if (building_psymtab)
11067 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11068 VAR_DOMAIN, LOC_CONST,
11069 (cu->language == language_cplus
11070 || cu->language == language_java)
11071 ? &objfile->global_psymbols
11072 : &objfile->static_psymbols,
11073 0, (CORE_ADDR) 0, cu->language, objfile);
11074
11075 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11076 continue;
11077 }
11078
11079 /* We'll save this DIE so link it in. */
11080 part_die->die_parent = parent_die;
11081 part_die->die_sibling = NULL;
11082 part_die->die_child = NULL;
11083
11084 if (last_die && last_die == parent_die)
11085 last_die->die_child = part_die;
11086 else if (last_die)
11087 last_die->die_sibling = part_die;
11088
11089 last_die = part_die;
11090
11091 if (first_die == NULL)
11092 first_die = part_die;
11093
11094 /* Maybe add the DIE to the hash table. Not all DIEs that we
11095 find interesting need to be in the hash table, because we
11096 also have the parent/sibling/child chains; only those that we
11097 might refer to by offset later during partial symbol reading.
11098
11099 For now this means things that might have be the target of a
11100 DW_AT_specification, DW_AT_abstract_origin, or
11101 DW_AT_extension. DW_AT_extension will refer only to
11102 namespaces; DW_AT_abstract_origin refers to functions (and
11103 many things under the function DIE, but we do not recurse
11104 into function DIEs during partial symbol reading) and
11105 possibly variables as well; DW_AT_specification refers to
11106 declarations. Declarations ought to have the DW_AT_declaration
11107 flag. It happens that GCC forgets to put it in sometimes, but
11108 only for functions, not for types.
11109
11110 Adding more things than necessary to the hash table is harmless
11111 except for the performance cost. Adding too few will result in
11112 wasted time in find_partial_die, when we reread the compilation
11113 unit with load_all_dies set. */
11114
11115 if (load_all
11116 || abbrev->tag == DW_TAG_constant
11117 || abbrev->tag == DW_TAG_subprogram
11118 || abbrev->tag == DW_TAG_variable
11119 || abbrev->tag == DW_TAG_namespace
11120 || part_die->is_declaration)
11121 {
11122 void **slot;
11123
11124 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11125 part_die->offset.sect_off, INSERT);
11126 *slot = part_die;
11127 }
11128
11129 part_die = obstack_alloc (&cu->comp_unit_obstack,
11130 sizeof (struct partial_die_info));
11131
11132 /* For some DIEs we want to follow their children (if any). For C
11133 we have no reason to follow the children of structures; for other
11134 languages we have to, so that we can get at method physnames
11135 to infer fully qualified class names, for DW_AT_specification,
11136 and for C++ template arguments. For C++, we also look one level
11137 inside functions to find template arguments (if the name of the
11138 function does not already contain the template arguments).
11139
11140 For Ada, we need to scan the children of subprograms and lexical
11141 blocks as well because Ada allows the definition of nested
11142 entities that could be interesting for the debugger, such as
11143 nested subprograms for instance. */
11144 if (last_die->has_children
11145 && (load_all
11146 || last_die->tag == DW_TAG_namespace
11147 || last_die->tag == DW_TAG_module
11148 || last_die->tag == DW_TAG_enumeration_type
11149 || (cu->language == language_cplus
11150 && last_die->tag == DW_TAG_subprogram
11151 && (last_die->name == NULL
11152 || strchr (last_die->name, '<') == NULL))
11153 || (cu->language != language_c
11154 && (last_die->tag == DW_TAG_class_type
11155 || last_die->tag == DW_TAG_interface_type
11156 || last_die->tag == DW_TAG_structure_type
11157 || last_die->tag == DW_TAG_union_type))
11158 || (cu->language == language_ada
11159 && (last_die->tag == DW_TAG_subprogram
11160 || last_die->tag == DW_TAG_lexical_block))))
11161 {
11162 nesting_level++;
11163 parent_die = last_die;
11164 continue;
11165 }
11166
11167 /* Otherwise we skip to the next sibling, if any. */
11168 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11169
11170 /* Back to the top, do it again. */
11171 }
11172 }
11173
11174 /* Read a minimal amount of information into the minimal die structure. */
11175
11176 static gdb_byte *
11177 read_partial_die (const struct die_reader_specs *reader,
11178 struct partial_die_info *part_die,
11179 struct abbrev_info *abbrev, unsigned int abbrev_len,
11180 gdb_byte *info_ptr)
11181 {
11182 struct dwarf2_cu *cu = reader->cu;
11183 struct objfile *objfile = cu->objfile;
11184 gdb_byte *buffer = reader->buffer;
11185 unsigned int i;
11186 struct attribute attr;
11187 int has_low_pc_attr = 0;
11188 int has_high_pc_attr = 0;
11189 int high_pc_relative = 0;
11190
11191 memset (part_die, 0, sizeof (struct partial_die_info));
11192
11193 part_die->offset.sect_off = info_ptr - buffer;
11194
11195 info_ptr += abbrev_len;
11196
11197 if (abbrev == NULL)
11198 return info_ptr;
11199
11200 part_die->tag = abbrev->tag;
11201 part_die->has_children = abbrev->has_children;
11202
11203 for (i = 0; i < abbrev->num_attrs; ++i)
11204 {
11205 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11206
11207 /* Store the data if it is of an attribute we want to keep in a
11208 partial symbol table. */
11209 switch (attr.name)
11210 {
11211 case DW_AT_name:
11212 switch (part_die->tag)
11213 {
11214 case DW_TAG_compile_unit:
11215 case DW_TAG_partial_unit:
11216 case DW_TAG_type_unit:
11217 /* Compilation units have a DW_AT_name that is a filename, not
11218 a source language identifier. */
11219 case DW_TAG_enumeration_type:
11220 case DW_TAG_enumerator:
11221 /* These tags always have simple identifiers already; no need
11222 to canonicalize them. */
11223 part_die->name = DW_STRING (&attr);
11224 break;
11225 default:
11226 part_die->name
11227 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11228 &objfile->objfile_obstack);
11229 break;
11230 }
11231 break;
11232 case DW_AT_linkage_name:
11233 case DW_AT_MIPS_linkage_name:
11234 /* Note that both forms of linkage name might appear. We
11235 assume they will be the same, and we only store the last
11236 one we see. */
11237 if (cu->language == language_ada)
11238 part_die->name = DW_STRING (&attr);
11239 part_die->linkage_name = DW_STRING (&attr);
11240 break;
11241 case DW_AT_low_pc:
11242 has_low_pc_attr = 1;
11243 part_die->lowpc = DW_ADDR (&attr);
11244 break;
11245 case DW_AT_high_pc:
11246 has_high_pc_attr = 1;
11247 if (attr.form == DW_FORM_addr
11248 || attr.form == DW_FORM_GNU_addr_index)
11249 part_die->highpc = DW_ADDR (&attr);
11250 else
11251 {
11252 high_pc_relative = 1;
11253 part_die->highpc = DW_UNSND (&attr);
11254 }
11255 break;
11256 case DW_AT_location:
11257 /* Support the .debug_loc offsets. */
11258 if (attr_form_is_block (&attr))
11259 {
11260 part_die->d.locdesc = DW_BLOCK (&attr);
11261 }
11262 else if (attr_form_is_section_offset (&attr))
11263 {
11264 dwarf2_complex_location_expr_complaint ();
11265 }
11266 else
11267 {
11268 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11269 "partial symbol information");
11270 }
11271 break;
11272 case DW_AT_external:
11273 part_die->is_external = DW_UNSND (&attr);
11274 break;
11275 case DW_AT_declaration:
11276 part_die->is_declaration = DW_UNSND (&attr);
11277 break;
11278 case DW_AT_type:
11279 part_die->has_type = 1;
11280 break;
11281 case DW_AT_abstract_origin:
11282 case DW_AT_specification:
11283 case DW_AT_extension:
11284 part_die->has_specification = 1;
11285 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11286 break;
11287 case DW_AT_sibling:
11288 /* Ignore absolute siblings, they might point outside of
11289 the current compile unit. */
11290 if (attr.form == DW_FORM_ref_addr)
11291 complaint (&symfile_complaints,
11292 _("ignoring absolute DW_AT_sibling"));
11293 else
11294 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11295 break;
11296 case DW_AT_byte_size:
11297 part_die->has_byte_size = 1;
11298 break;
11299 case DW_AT_calling_convention:
11300 /* DWARF doesn't provide a way to identify a program's source-level
11301 entry point. DW_AT_calling_convention attributes are only meant
11302 to describe functions' calling conventions.
11303
11304 However, because it's a necessary piece of information in
11305 Fortran, and because DW_CC_program is the only piece of debugging
11306 information whose definition refers to a 'main program' at all,
11307 several compilers have begun marking Fortran main programs with
11308 DW_CC_program --- even when those functions use the standard
11309 calling conventions.
11310
11311 So until DWARF specifies a way to provide this information and
11312 compilers pick up the new representation, we'll support this
11313 practice. */
11314 if (DW_UNSND (&attr) == DW_CC_program
11315 && cu->language == language_fortran)
11316 {
11317 set_main_name (part_die->name);
11318
11319 /* As this DIE has a static linkage the name would be difficult
11320 to look up later. */
11321 language_of_main = language_fortran;
11322 }
11323 break;
11324 case DW_AT_inline:
11325 if (DW_UNSND (&attr) == DW_INL_inlined
11326 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11327 part_die->may_be_inlined = 1;
11328 break;
11329
11330 case DW_AT_import:
11331 if (part_die->tag == DW_TAG_imported_unit)
11332 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11333 break;
11334
11335 default:
11336 break;
11337 }
11338 }
11339
11340 if (high_pc_relative)
11341 part_die->highpc += part_die->lowpc;
11342
11343 if (has_low_pc_attr && has_high_pc_attr)
11344 {
11345 /* When using the GNU linker, .gnu.linkonce. sections are used to
11346 eliminate duplicate copies of functions and vtables and such.
11347 The linker will arbitrarily choose one and discard the others.
11348 The AT_*_pc values for such functions refer to local labels in
11349 these sections. If the section from that file was discarded, the
11350 labels are not in the output, so the relocs get a value of 0.
11351 If this is a discarded function, mark the pc bounds as invalid,
11352 so that GDB will ignore it. */
11353 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11354 {
11355 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11356
11357 complaint (&symfile_complaints,
11358 _("DW_AT_low_pc %s is zero "
11359 "for DIE at 0x%x [in module %s]"),
11360 paddress (gdbarch, part_die->lowpc),
11361 part_die->offset.sect_off, objfile->name);
11362 }
11363 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11364 else if (part_die->lowpc >= part_die->highpc)
11365 {
11366 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11367
11368 complaint (&symfile_complaints,
11369 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11370 "for DIE at 0x%x [in module %s]"),
11371 paddress (gdbarch, part_die->lowpc),
11372 paddress (gdbarch, part_die->highpc),
11373 part_die->offset.sect_off, objfile->name);
11374 }
11375 else
11376 part_die->has_pc_info = 1;
11377 }
11378
11379 return info_ptr;
11380 }
11381
11382 /* Find a cached partial DIE at OFFSET in CU. */
11383
11384 static struct partial_die_info *
11385 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11386 {
11387 struct partial_die_info *lookup_die = NULL;
11388 struct partial_die_info part_die;
11389
11390 part_die.offset = offset;
11391 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11392 offset.sect_off);
11393
11394 return lookup_die;
11395 }
11396
11397 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11398 except in the case of .debug_types DIEs which do not reference
11399 outside their CU (they do however referencing other types via
11400 DW_FORM_ref_sig8). */
11401
11402 static struct partial_die_info *
11403 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11404 {
11405 struct objfile *objfile = cu->objfile;
11406 struct dwarf2_per_cu_data *per_cu = NULL;
11407 struct partial_die_info *pd = NULL;
11408
11409 if (offset_in_cu_p (&cu->header, offset))
11410 {
11411 pd = find_partial_die_in_comp_unit (offset, cu);
11412 if (pd != NULL)
11413 return pd;
11414 /* We missed recording what we needed.
11415 Load all dies and try again. */
11416 per_cu = cu->per_cu;
11417 }
11418 else
11419 {
11420 /* TUs don't reference other CUs/TUs (except via type signatures). */
11421 if (cu->per_cu->is_debug_types)
11422 {
11423 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11424 " external reference to offset 0x%lx [in module %s].\n"),
11425 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11426 bfd_get_filename (objfile->obfd));
11427 }
11428 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11429
11430 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11431 load_partial_comp_unit (per_cu);
11432
11433 per_cu->cu->last_used = 0;
11434 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11435 }
11436
11437 /* If we didn't find it, and not all dies have been loaded,
11438 load them all and try again. */
11439
11440 if (pd == NULL && per_cu->load_all_dies == 0)
11441 {
11442 per_cu->load_all_dies = 1;
11443
11444 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11445 THIS_CU->cu may already be in use. So we can't just free it and
11446 replace its DIEs with the ones we read in. Instead, we leave those
11447 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11448 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11449 set. */
11450 load_partial_comp_unit (per_cu);
11451
11452 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11453 }
11454
11455 if (pd == NULL)
11456 internal_error (__FILE__, __LINE__,
11457 _("could not find partial DIE 0x%x "
11458 "in cache [from module %s]\n"),
11459 offset.sect_off, bfd_get_filename (objfile->obfd));
11460 return pd;
11461 }
11462
11463 /* See if we can figure out if the class lives in a namespace. We do
11464 this by looking for a member function; its demangled name will
11465 contain namespace info, if there is any. */
11466
11467 static void
11468 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11469 struct dwarf2_cu *cu)
11470 {
11471 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11472 what template types look like, because the demangler
11473 frequently doesn't give the same name as the debug info. We
11474 could fix this by only using the demangled name to get the
11475 prefix (but see comment in read_structure_type). */
11476
11477 struct partial_die_info *real_pdi;
11478 struct partial_die_info *child_pdi;
11479
11480 /* If this DIE (this DIE's specification, if any) has a parent, then
11481 we should not do this. We'll prepend the parent's fully qualified
11482 name when we create the partial symbol. */
11483
11484 real_pdi = struct_pdi;
11485 while (real_pdi->has_specification)
11486 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11487
11488 if (real_pdi->die_parent != NULL)
11489 return;
11490
11491 for (child_pdi = struct_pdi->die_child;
11492 child_pdi != NULL;
11493 child_pdi = child_pdi->die_sibling)
11494 {
11495 if (child_pdi->tag == DW_TAG_subprogram
11496 && child_pdi->linkage_name != NULL)
11497 {
11498 char *actual_class_name
11499 = language_class_name_from_physname (cu->language_defn,
11500 child_pdi->linkage_name);
11501 if (actual_class_name != NULL)
11502 {
11503 struct_pdi->name
11504 = obsavestring (actual_class_name,
11505 strlen (actual_class_name),
11506 &cu->objfile->objfile_obstack);
11507 xfree (actual_class_name);
11508 }
11509 break;
11510 }
11511 }
11512 }
11513
11514 /* Adjust PART_DIE before generating a symbol for it. This function
11515 may set the is_external flag or change the DIE's name. */
11516
11517 static void
11518 fixup_partial_die (struct partial_die_info *part_die,
11519 struct dwarf2_cu *cu)
11520 {
11521 /* Once we've fixed up a die, there's no point in doing so again.
11522 This also avoids a memory leak if we were to call
11523 guess_partial_die_structure_name multiple times. */
11524 if (part_die->fixup_called)
11525 return;
11526
11527 /* If we found a reference attribute and the DIE has no name, try
11528 to find a name in the referred to DIE. */
11529
11530 if (part_die->name == NULL && part_die->has_specification)
11531 {
11532 struct partial_die_info *spec_die;
11533
11534 spec_die = find_partial_die (part_die->spec_offset, cu);
11535
11536 fixup_partial_die (spec_die, cu);
11537
11538 if (spec_die->name)
11539 {
11540 part_die->name = spec_die->name;
11541
11542 /* Copy DW_AT_external attribute if it is set. */
11543 if (spec_die->is_external)
11544 part_die->is_external = spec_die->is_external;
11545 }
11546 }
11547
11548 /* Set default names for some unnamed DIEs. */
11549
11550 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11551 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11552
11553 /* If there is no parent die to provide a namespace, and there are
11554 children, see if we can determine the namespace from their linkage
11555 name. */
11556 if (cu->language == language_cplus
11557 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11558 && part_die->die_parent == NULL
11559 && part_die->has_children
11560 && (part_die->tag == DW_TAG_class_type
11561 || part_die->tag == DW_TAG_structure_type
11562 || part_die->tag == DW_TAG_union_type))
11563 guess_partial_die_structure_name (part_die, cu);
11564
11565 /* GCC might emit a nameless struct or union that has a linkage
11566 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11567 if (part_die->name == NULL
11568 && (part_die->tag == DW_TAG_class_type
11569 || part_die->tag == DW_TAG_interface_type
11570 || part_die->tag == DW_TAG_structure_type
11571 || part_die->tag == DW_TAG_union_type)
11572 && part_die->linkage_name != NULL)
11573 {
11574 char *demangled;
11575
11576 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11577 if (demangled)
11578 {
11579 const char *base;
11580
11581 /* Strip any leading namespaces/classes, keep only the base name.
11582 DW_AT_name for named DIEs does not contain the prefixes. */
11583 base = strrchr (demangled, ':');
11584 if (base && base > demangled && base[-1] == ':')
11585 base++;
11586 else
11587 base = demangled;
11588
11589 part_die->name = obsavestring (base, strlen (base),
11590 &cu->objfile->objfile_obstack);
11591 xfree (demangled);
11592 }
11593 }
11594
11595 part_die->fixup_called = 1;
11596 }
11597
11598 /* Read an attribute value described by an attribute form. */
11599
11600 static gdb_byte *
11601 read_attribute_value (const struct die_reader_specs *reader,
11602 struct attribute *attr, unsigned form,
11603 gdb_byte *info_ptr)
11604 {
11605 struct dwarf2_cu *cu = reader->cu;
11606 bfd *abfd = reader->abfd;
11607 struct comp_unit_head *cu_header = &cu->header;
11608 unsigned int bytes_read;
11609 struct dwarf_block *blk;
11610
11611 attr->form = form;
11612 switch (form)
11613 {
11614 case DW_FORM_ref_addr:
11615 if (cu->header.version == 2)
11616 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11617 else
11618 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11619 &cu->header, &bytes_read);
11620 info_ptr += bytes_read;
11621 break;
11622 case DW_FORM_addr:
11623 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11624 info_ptr += bytes_read;
11625 break;
11626 case DW_FORM_block2:
11627 blk = dwarf_alloc_block (cu);
11628 blk->size = read_2_bytes (abfd, info_ptr);
11629 info_ptr += 2;
11630 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11631 info_ptr += blk->size;
11632 DW_BLOCK (attr) = blk;
11633 break;
11634 case DW_FORM_block4:
11635 blk = dwarf_alloc_block (cu);
11636 blk->size = read_4_bytes (abfd, info_ptr);
11637 info_ptr += 4;
11638 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11639 info_ptr += blk->size;
11640 DW_BLOCK (attr) = blk;
11641 break;
11642 case DW_FORM_data2:
11643 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11644 info_ptr += 2;
11645 break;
11646 case DW_FORM_data4:
11647 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11648 info_ptr += 4;
11649 break;
11650 case DW_FORM_data8:
11651 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11652 info_ptr += 8;
11653 break;
11654 case DW_FORM_sec_offset:
11655 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11656 info_ptr += bytes_read;
11657 break;
11658 case DW_FORM_string:
11659 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11660 DW_STRING_IS_CANONICAL (attr) = 0;
11661 info_ptr += bytes_read;
11662 break;
11663 case DW_FORM_strp:
11664 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11665 &bytes_read);
11666 DW_STRING_IS_CANONICAL (attr) = 0;
11667 info_ptr += bytes_read;
11668 break;
11669 case DW_FORM_exprloc:
11670 case DW_FORM_block:
11671 blk = dwarf_alloc_block (cu);
11672 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11673 info_ptr += bytes_read;
11674 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11675 info_ptr += blk->size;
11676 DW_BLOCK (attr) = blk;
11677 break;
11678 case DW_FORM_block1:
11679 blk = dwarf_alloc_block (cu);
11680 blk->size = read_1_byte (abfd, info_ptr);
11681 info_ptr += 1;
11682 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11683 info_ptr += blk->size;
11684 DW_BLOCK (attr) = blk;
11685 break;
11686 case DW_FORM_data1:
11687 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11688 info_ptr += 1;
11689 break;
11690 case DW_FORM_flag:
11691 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11692 info_ptr += 1;
11693 break;
11694 case DW_FORM_flag_present:
11695 DW_UNSND (attr) = 1;
11696 break;
11697 case DW_FORM_sdata:
11698 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11699 info_ptr += bytes_read;
11700 break;
11701 case DW_FORM_udata:
11702 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11703 info_ptr += bytes_read;
11704 break;
11705 case DW_FORM_ref1:
11706 DW_UNSND (attr) = (cu->header.offset.sect_off
11707 + read_1_byte (abfd, info_ptr));
11708 info_ptr += 1;
11709 break;
11710 case DW_FORM_ref2:
11711 DW_UNSND (attr) = (cu->header.offset.sect_off
11712 + read_2_bytes (abfd, info_ptr));
11713 info_ptr += 2;
11714 break;
11715 case DW_FORM_ref4:
11716 DW_UNSND (attr) = (cu->header.offset.sect_off
11717 + read_4_bytes (abfd, info_ptr));
11718 info_ptr += 4;
11719 break;
11720 case DW_FORM_ref8:
11721 DW_UNSND (attr) = (cu->header.offset.sect_off
11722 + read_8_bytes (abfd, info_ptr));
11723 info_ptr += 8;
11724 break;
11725 case DW_FORM_ref_sig8:
11726 /* Convert the signature to something we can record in DW_UNSND
11727 for later lookup.
11728 NOTE: This is NULL if the type wasn't found. */
11729 DW_SIGNATURED_TYPE (attr) =
11730 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11731 info_ptr += 8;
11732 break;
11733 case DW_FORM_ref_udata:
11734 DW_UNSND (attr) = (cu->header.offset.sect_off
11735 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11736 info_ptr += bytes_read;
11737 break;
11738 case DW_FORM_indirect:
11739 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11740 info_ptr += bytes_read;
11741 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11742 break;
11743 case DW_FORM_GNU_addr_index:
11744 if (reader->dwo_file == NULL)
11745 {
11746 /* For now flag a hard error.
11747 Later we can turn this into a complaint. */
11748 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11749 dwarf_form_name (form),
11750 bfd_get_filename (abfd));
11751 }
11752 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11753 info_ptr += bytes_read;
11754 break;
11755 case DW_FORM_GNU_str_index:
11756 if (reader->dwo_file == NULL)
11757 {
11758 /* For now flag a hard error.
11759 Later we can turn this into a complaint if warranted. */
11760 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11761 dwarf_form_name (form),
11762 bfd_get_filename (abfd));
11763 }
11764 {
11765 ULONGEST str_index =
11766 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11767
11768 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11769 DW_STRING_IS_CANONICAL (attr) = 0;
11770 info_ptr += bytes_read;
11771 }
11772 break;
11773 default:
11774 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11775 dwarf_form_name (form),
11776 bfd_get_filename (abfd));
11777 }
11778
11779 /* We have seen instances where the compiler tried to emit a byte
11780 size attribute of -1 which ended up being encoded as an unsigned
11781 0xffffffff. Although 0xffffffff is technically a valid size value,
11782 an object of this size seems pretty unlikely so we can relatively
11783 safely treat these cases as if the size attribute was invalid and
11784 treat them as zero by default. */
11785 if (attr->name == DW_AT_byte_size
11786 && form == DW_FORM_data4
11787 && DW_UNSND (attr) >= 0xffffffff)
11788 {
11789 complaint
11790 (&symfile_complaints,
11791 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11792 hex_string (DW_UNSND (attr)));
11793 DW_UNSND (attr) = 0;
11794 }
11795
11796 return info_ptr;
11797 }
11798
11799 /* Read an attribute described by an abbreviated attribute. */
11800
11801 static gdb_byte *
11802 read_attribute (const struct die_reader_specs *reader,
11803 struct attribute *attr, struct attr_abbrev *abbrev,
11804 gdb_byte *info_ptr)
11805 {
11806 attr->name = abbrev->name;
11807 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
11808 }
11809
11810 /* Read dwarf information from a buffer. */
11811
11812 static unsigned int
11813 read_1_byte (bfd *abfd, gdb_byte *buf)
11814 {
11815 return bfd_get_8 (abfd, buf);
11816 }
11817
11818 static int
11819 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
11820 {
11821 return bfd_get_signed_8 (abfd, buf);
11822 }
11823
11824 static unsigned int
11825 read_2_bytes (bfd *abfd, gdb_byte *buf)
11826 {
11827 return bfd_get_16 (abfd, buf);
11828 }
11829
11830 static int
11831 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11832 {
11833 return bfd_get_signed_16 (abfd, buf);
11834 }
11835
11836 static unsigned int
11837 read_4_bytes (bfd *abfd, gdb_byte *buf)
11838 {
11839 return bfd_get_32 (abfd, buf);
11840 }
11841
11842 static int
11843 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11844 {
11845 return bfd_get_signed_32 (abfd, buf);
11846 }
11847
11848 static ULONGEST
11849 read_8_bytes (bfd *abfd, gdb_byte *buf)
11850 {
11851 return bfd_get_64 (abfd, buf);
11852 }
11853
11854 static CORE_ADDR
11855 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
11856 unsigned int *bytes_read)
11857 {
11858 struct comp_unit_head *cu_header = &cu->header;
11859 CORE_ADDR retval = 0;
11860
11861 if (cu_header->signed_addr_p)
11862 {
11863 switch (cu_header->addr_size)
11864 {
11865 case 2:
11866 retval = bfd_get_signed_16 (abfd, buf);
11867 break;
11868 case 4:
11869 retval = bfd_get_signed_32 (abfd, buf);
11870 break;
11871 case 8:
11872 retval = bfd_get_signed_64 (abfd, buf);
11873 break;
11874 default:
11875 internal_error (__FILE__, __LINE__,
11876 _("read_address: bad switch, signed [in module %s]"),
11877 bfd_get_filename (abfd));
11878 }
11879 }
11880 else
11881 {
11882 switch (cu_header->addr_size)
11883 {
11884 case 2:
11885 retval = bfd_get_16 (abfd, buf);
11886 break;
11887 case 4:
11888 retval = bfd_get_32 (abfd, buf);
11889 break;
11890 case 8:
11891 retval = bfd_get_64 (abfd, buf);
11892 break;
11893 default:
11894 internal_error (__FILE__, __LINE__,
11895 _("read_address: bad switch, "
11896 "unsigned [in module %s]"),
11897 bfd_get_filename (abfd));
11898 }
11899 }
11900
11901 *bytes_read = cu_header->addr_size;
11902 return retval;
11903 }
11904
11905 /* Read the initial length from a section. The (draft) DWARF 3
11906 specification allows the initial length to take up either 4 bytes
11907 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11908 bytes describe the length and all offsets will be 8 bytes in length
11909 instead of 4.
11910
11911 An older, non-standard 64-bit format is also handled by this
11912 function. The older format in question stores the initial length
11913 as an 8-byte quantity without an escape value. Lengths greater
11914 than 2^32 aren't very common which means that the initial 4 bytes
11915 is almost always zero. Since a length value of zero doesn't make
11916 sense for the 32-bit format, this initial zero can be considered to
11917 be an escape value which indicates the presence of the older 64-bit
11918 format. As written, the code can't detect (old format) lengths
11919 greater than 4GB. If it becomes necessary to handle lengths
11920 somewhat larger than 4GB, we could allow other small values (such
11921 as the non-sensical values of 1, 2, and 3) to also be used as
11922 escape values indicating the presence of the old format.
11923
11924 The value returned via bytes_read should be used to increment the
11925 relevant pointer after calling read_initial_length().
11926
11927 [ Note: read_initial_length() and read_offset() are based on the
11928 document entitled "DWARF Debugging Information Format", revision
11929 3, draft 8, dated November 19, 2001. This document was obtained
11930 from:
11931
11932 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
11933
11934 This document is only a draft and is subject to change. (So beware.)
11935
11936 Details regarding the older, non-standard 64-bit format were
11937 determined empirically by examining 64-bit ELF files produced by
11938 the SGI toolchain on an IRIX 6.5 machine.
11939
11940 - Kevin, July 16, 2002
11941 ] */
11942
11943 static LONGEST
11944 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
11945 {
11946 LONGEST length = bfd_get_32 (abfd, buf);
11947
11948 if (length == 0xffffffff)
11949 {
11950 length = bfd_get_64 (abfd, buf + 4);
11951 *bytes_read = 12;
11952 }
11953 else if (length == 0)
11954 {
11955 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
11956 length = bfd_get_64 (abfd, buf);
11957 *bytes_read = 8;
11958 }
11959 else
11960 {
11961 *bytes_read = 4;
11962 }
11963
11964 return length;
11965 }
11966
11967 /* Cover function for read_initial_length.
11968 Returns the length of the object at BUF, and stores the size of the
11969 initial length in *BYTES_READ and stores the size that offsets will be in
11970 *OFFSET_SIZE.
11971 If the initial length size is not equivalent to that specified in
11972 CU_HEADER then issue a complaint.
11973 This is useful when reading non-comp-unit headers. */
11974
11975 static LONGEST
11976 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11977 const struct comp_unit_head *cu_header,
11978 unsigned int *bytes_read,
11979 unsigned int *offset_size)
11980 {
11981 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11982
11983 gdb_assert (cu_header->initial_length_size == 4
11984 || cu_header->initial_length_size == 8
11985 || cu_header->initial_length_size == 12);
11986
11987 if (cu_header->initial_length_size != *bytes_read)
11988 complaint (&symfile_complaints,
11989 _("intermixed 32-bit and 64-bit DWARF sections"));
11990
11991 *offset_size = (*bytes_read == 4) ? 4 : 8;
11992 return length;
11993 }
11994
11995 /* Read an offset from the data stream. The size of the offset is
11996 given by cu_header->offset_size. */
11997
11998 static LONGEST
11999 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
12000 unsigned int *bytes_read)
12001 {
12002 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
12003
12004 *bytes_read = cu_header->offset_size;
12005 return offset;
12006 }
12007
12008 /* Read an offset from the data stream. */
12009
12010 static LONGEST
12011 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
12012 {
12013 LONGEST retval = 0;
12014
12015 switch (offset_size)
12016 {
12017 case 4:
12018 retval = bfd_get_32 (abfd, buf);
12019 break;
12020 case 8:
12021 retval = bfd_get_64 (abfd, buf);
12022 break;
12023 default:
12024 internal_error (__FILE__, __LINE__,
12025 _("read_offset_1: bad switch [in module %s]"),
12026 bfd_get_filename (abfd));
12027 }
12028
12029 return retval;
12030 }
12031
12032 static gdb_byte *
12033 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
12034 {
12035 /* If the size of a host char is 8 bits, we can return a pointer
12036 to the buffer, otherwise we have to copy the data to a buffer
12037 allocated on the temporary obstack. */
12038 gdb_assert (HOST_CHAR_BIT == 8);
12039 return buf;
12040 }
12041
12042 static char *
12043 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12044 {
12045 /* If the size of a host char is 8 bits, we can return a pointer
12046 to the string, otherwise we have to copy the string to a buffer
12047 allocated on the temporary obstack. */
12048 gdb_assert (HOST_CHAR_BIT == 8);
12049 if (*buf == '\0')
12050 {
12051 *bytes_read_ptr = 1;
12052 return NULL;
12053 }
12054 *bytes_read_ptr = strlen ((char *) buf) + 1;
12055 return (char *) buf;
12056 }
12057
12058 static char *
12059 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12060 {
12061 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12062 if (dwarf2_per_objfile->str.buffer == NULL)
12063 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12064 bfd_get_filename (abfd));
12065 if (str_offset >= dwarf2_per_objfile->str.size)
12066 error (_("DW_FORM_strp pointing outside of "
12067 ".debug_str section [in module %s]"),
12068 bfd_get_filename (abfd));
12069 gdb_assert (HOST_CHAR_BIT == 8);
12070 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12071 return NULL;
12072 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12073 }
12074
12075 static char *
12076 read_indirect_string (bfd *abfd, gdb_byte *buf,
12077 const struct comp_unit_head *cu_header,
12078 unsigned int *bytes_read_ptr)
12079 {
12080 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12081
12082 return read_indirect_string_at_offset (abfd, str_offset);
12083 }
12084
12085 static ULONGEST
12086 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12087 {
12088 ULONGEST result;
12089 unsigned int num_read;
12090 int i, shift;
12091 unsigned char byte;
12092
12093 result = 0;
12094 shift = 0;
12095 num_read = 0;
12096 i = 0;
12097 while (1)
12098 {
12099 byte = bfd_get_8 (abfd, buf);
12100 buf++;
12101 num_read++;
12102 result |= ((ULONGEST) (byte & 127) << shift);
12103 if ((byte & 128) == 0)
12104 {
12105 break;
12106 }
12107 shift += 7;
12108 }
12109 *bytes_read_ptr = num_read;
12110 return result;
12111 }
12112
12113 static LONGEST
12114 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12115 {
12116 LONGEST result;
12117 int i, shift, num_read;
12118 unsigned char byte;
12119
12120 result = 0;
12121 shift = 0;
12122 num_read = 0;
12123 i = 0;
12124 while (1)
12125 {
12126 byte = bfd_get_8 (abfd, buf);
12127 buf++;
12128 num_read++;
12129 result |= ((LONGEST) (byte & 127) << shift);
12130 shift += 7;
12131 if ((byte & 128) == 0)
12132 {
12133 break;
12134 }
12135 }
12136 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12137 result |= -(((LONGEST) 1) << shift);
12138 *bytes_read_ptr = num_read;
12139 return result;
12140 }
12141
12142 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12143 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12144 ADDR_SIZE is the size of addresses from the CU header. */
12145
12146 static CORE_ADDR
12147 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12148 {
12149 struct objfile *objfile = dwarf2_per_objfile->objfile;
12150 bfd *abfd = objfile->obfd;
12151 const gdb_byte *info_ptr;
12152
12153 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12154 if (dwarf2_per_objfile->addr.buffer == NULL)
12155 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12156 objfile->name);
12157 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12158 error (_("DW_FORM_addr_index pointing outside of "
12159 ".debug_addr section [in module %s]"),
12160 objfile->name);
12161 info_ptr = (dwarf2_per_objfile->addr.buffer
12162 + addr_base + addr_index * addr_size);
12163 if (addr_size == 4)
12164 return bfd_get_32 (abfd, info_ptr);
12165 else
12166 return bfd_get_64 (abfd, info_ptr);
12167 }
12168
12169 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12170
12171 static CORE_ADDR
12172 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12173 {
12174 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12175 }
12176
12177 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12178
12179 static CORE_ADDR
12180 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12181 unsigned int *bytes_read)
12182 {
12183 bfd *abfd = cu->objfile->obfd;
12184 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12185
12186 return read_addr_index (cu, addr_index);
12187 }
12188
12189 /* Data structure to pass results from dwarf2_read_addr_index_reader
12190 back to dwarf2_read_addr_index. */
12191
12192 struct dwarf2_read_addr_index_data
12193 {
12194 ULONGEST addr_base;
12195 int addr_size;
12196 };
12197
12198 /* die_reader_func for dwarf2_read_addr_index. */
12199
12200 static void
12201 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12202 gdb_byte *info_ptr,
12203 struct die_info *comp_unit_die,
12204 int has_children,
12205 void *data)
12206 {
12207 struct dwarf2_cu *cu = reader->cu;
12208 struct dwarf2_read_addr_index_data *aidata =
12209 (struct dwarf2_read_addr_index_data *) data;
12210
12211 aidata->addr_base = cu->addr_base;
12212 aidata->addr_size = cu->header.addr_size;
12213 }
12214
12215 /* Given an index in .debug_addr, fetch the value.
12216 NOTE: This can be called during dwarf expression evaluation,
12217 long after the debug information has been read, and thus per_cu->cu
12218 may no longer exist. */
12219
12220 CORE_ADDR
12221 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12222 unsigned int addr_index)
12223 {
12224 struct objfile *objfile = per_cu->objfile;
12225 struct dwarf2_cu *cu = per_cu->cu;
12226 ULONGEST addr_base;
12227 int addr_size;
12228
12229 /* This is intended to be called from outside this file. */
12230 dw2_setup (objfile);
12231
12232 /* We need addr_base and addr_size.
12233 If we don't have PER_CU->cu, we have to get it.
12234 Nasty, but the alternative is storing the needed info in PER_CU,
12235 which at this point doesn't seem justified: it's not clear how frequently
12236 it would get used and it would increase the size of every PER_CU.
12237 Entry points like dwarf2_per_cu_addr_size do a similar thing
12238 so we're not in uncharted territory here.
12239 Alas we need to be a bit more complicated as addr_base is contained
12240 in the DIE.
12241
12242 We don't need to read the entire CU(/TU).
12243 We just need the header and top level die.
12244 IWBN to use the aging mechanism to let us lazily later discard the CU.
12245 See however init_cutu_and_read_dies_simple. */
12246
12247 if (cu != NULL)
12248 {
12249 addr_base = cu->addr_base;
12250 addr_size = cu->header.addr_size;
12251 }
12252 else
12253 {
12254 struct dwarf2_read_addr_index_data aidata;
12255
12256 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12257 &aidata);
12258 addr_base = aidata.addr_base;
12259 addr_size = aidata.addr_size;
12260 }
12261
12262 return read_addr_index_1 (addr_index, addr_base, addr_size);
12263 }
12264
12265 /* Given a DW_AT_str_index, fetch the string. */
12266
12267 static char *
12268 read_str_index (const struct die_reader_specs *reader,
12269 struct dwarf2_cu *cu, ULONGEST str_index)
12270 {
12271 struct objfile *objfile = dwarf2_per_objfile->objfile;
12272 const char *dwo_name = objfile->name;
12273 bfd *abfd = objfile->obfd;
12274 struct dwo_sections *sections = &reader->dwo_file->sections;
12275 gdb_byte *info_ptr;
12276 ULONGEST str_offset;
12277
12278 dwarf2_read_section (objfile, &sections->str);
12279 dwarf2_read_section (objfile, &sections->str_offsets);
12280 if (sections->str.buffer == NULL)
12281 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12282 " in CU at offset 0x%lx [in module %s]"),
12283 (long) cu->header.offset.sect_off, dwo_name);
12284 if (sections->str_offsets.buffer == NULL)
12285 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12286 " in CU at offset 0x%lx [in module %s]"),
12287 (long) cu->header.offset.sect_off, dwo_name);
12288 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12289 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12290 " section in CU at offset 0x%lx [in module %s]"),
12291 (long) cu->header.offset.sect_off, dwo_name);
12292 info_ptr = (sections->str_offsets.buffer
12293 + str_index * cu->header.offset_size);
12294 if (cu->header.offset_size == 4)
12295 str_offset = bfd_get_32 (abfd, info_ptr);
12296 else
12297 str_offset = bfd_get_64 (abfd, info_ptr);
12298 if (str_offset >= sections->str.size)
12299 error (_("Offset from DW_FORM_str_index pointing outside of"
12300 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12301 (long) cu->header.offset.sect_off, dwo_name);
12302 return (char *) (sections->str.buffer + str_offset);
12303 }
12304
12305 /* Return the length of an LEB128 number in BUF. */
12306
12307 static int
12308 leb128_size (const gdb_byte *buf)
12309 {
12310 const gdb_byte *begin = buf;
12311 gdb_byte byte;
12312
12313 while (1)
12314 {
12315 byte = *buf++;
12316 if ((byte & 128) == 0)
12317 return buf - begin;
12318 }
12319 }
12320
12321 static void
12322 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12323 {
12324 switch (lang)
12325 {
12326 case DW_LANG_C89:
12327 case DW_LANG_C99:
12328 case DW_LANG_C:
12329 cu->language = language_c;
12330 break;
12331 case DW_LANG_C_plus_plus:
12332 cu->language = language_cplus;
12333 break;
12334 case DW_LANG_D:
12335 cu->language = language_d;
12336 break;
12337 case DW_LANG_Fortran77:
12338 case DW_LANG_Fortran90:
12339 case DW_LANG_Fortran95:
12340 cu->language = language_fortran;
12341 break;
12342 case DW_LANG_Go:
12343 cu->language = language_go;
12344 break;
12345 case DW_LANG_Mips_Assembler:
12346 cu->language = language_asm;
12347 break;
12348 case DW_LANG_Java:
12349 cu->language = language_java;
12350 break;
12351 case DW_LANG_Ada83:
12352 case DW_LANG_Ada95:
12353 cu->language = language_ada;
12354 break;
12355 case DW_LANG_Modula2:
12356 cu->language = language_m2;
12357 break;
12358 case DW_LANG_Pascal83:
12359 cu->language = language_pascal;
12360 break;
12361 case DW_LANG_ObjC:
12362 cu->language = language_objc;
12363 break;
12364 case DW_LANG_Cobol74:
12365 case DW_LANG_Cobol85:
12366 default:
12367 cu->language = language_minimal;
12368 break;
12369 }
12370 cu->language_defn = language_def (cu->language);
12371 }
12372
12373 /* Return the named attribute or NULL if not there. */
12374
12375 static struct attribute *
12376 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12377 {
12378 for (;;)
12379 {
12380 unsigned int i;
12381 struct attribute *spec = NULL;
12382
12383 for (i = 0; i < die->num_attrs; ++i)
12384 {
12385 if (die->attrs[i].name == name)
12386 return &die->attrs[i];
12387 if (die->attrs[i].name == DW_AT_specification
12388 || die->attrs[i].name == DW_AT_abstract_origin)
12389 spec = &die->attrs[i];
12390 }
12391
12392 if (!spec)
12393 break;
12394
12395 die = follow_die_ref (die, spec, &cu);
12396 }
12397
12398 return NULL;
12399 }
12400
12401 /* Return the named attribute or NULL if not there,
12402 but do not follow DW_AT_specification, etc.
12403 This is for use in contexts where we're reading .debug_types dies.
12404 Following DW_AT_specification, DW_AT_abstract_origin will take us
12405 back up the chain, and we want to go down. */
12406
12407 static struct attribute *
12408 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12409 struct dwarf2_cu *cu)
12410 {
12411 unsigned int i;
12412
12413 for (i = 0; i < die->num_attrs; ++i)
12414 if (die->attrs[i].name == name)
12415 return &die->attrs[i];
12416
12417 return NULL;
12418 }
12419
12420 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12421 and holds a non-zero value. This function should only be used for
12422 DW_FORM_flag or DW_FORM_flag_present attributes. */
12423
12424 static int
12425 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12426 {
12427 struct attribute *attr = dwarf2_attr (die, name, cu);
12428
12429 return (attr && DW_UNSND (attr));
12430 }
12431
12432 static int
12433 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12434 {
12435 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12436 which value is non-zero. However, we have to be careful with
12437 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12438 (via dwarf2_flag_true_p) follows this attribute. So we may
12439 end up accidently finding a declaration attribute that belongs
12440 to a different DIE referenced by the specification attribute,
12441 even though the given DIE does not have a declaration attribute. */
12442 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12443 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12444 }
12445
12446 /* Return the die giving the specification for DIE, if there is
12447 one. *SPEC_CU is the CU containing DIE on input, and the CU
12448 containing the return value on output. If there is no
12449 specification, but there is an abstract origin, that is
12450 returned. */
12451
12452 static struct die_info *
12453 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12454 {
12455 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12456 *spec_cu);
12457
12458 if (spec_attr == NULL)
12459 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12460
12461 if (spec_attr == NULL)
12462 return NULL;
12463 else
12464 return follow_die_ref (die, spec_attr, spec_cu);
12465 }
12466
12467 /* Free the line_header structure *LH, and any arrays and strings it
12468 refers to.
12469 NOTE: This is also used as a "cleanup" function. */
12470
12471 static void
12472 free_line_header (struct line_header *lh)
12473 {
12474 if (lh->standard_opcode_lengths)
12475 xfree (lh->standard_opcode_lengths);
12476
12477 /* Remember that all the lh->file_names[i].name pointers are
12478 pointers into debug_line_buffer, and don't need to be freed. */
12479 if (lh->file_names)
12480 xfree (lh->file_names);
12481
12482 /* Similarly for the include directory names. */
12483 if (lh->include_dirs)
12484 xfree (lh->include_dirs);
12485
12486 xfree (lh);
12487 }
12488
12489 /* Add an entry to LH's include directory table. */
12490
12491 static void
12492 add_include_dir (struct line_header *lh, char *include_dir)
12493 {
12494 /* Grow the array if necessary. */
12495 if (lh->include_dirs_size == 0)
12496 {
12497 lh->include_dirs_size = 1; /* for testing */
12498 lh->include_dirs = xmalloc (lh->include_dirs_size
12499 * sizeof (*lh->include_dirs));
12500 }
12501 else if (lh->num_include_dirs >= lh->include_dirs_size)
12502 {
12503 lh->include_dirs_size *= 2;
12504 lh->include_dirs = xrealloc (lh->include_dirs,
12505 (lh->include_dirs_size
12506 * sizeof (*lh->include_dirs)));
12507 }
12508
12509 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12510 }
12511
12512 /* Add an entry to LH's file name table. */
12513
12514 static void
12515 add_file_name (struct line_header *lh,
12516 char *name,
12517 unsigned int dir_index,
12518 unsigned int mod_time,
12519 unsigned int length)
12520 {
12521 struct file_entry *fe;
12522
12523 /* Grow the array if necessary. */
12524 if (lh->file_names_size == 0)
12525 {
12526 lh->file_names_size = 1; /* for testing */
12527 lh->file_names = xmalloc (lh->file_names_size
12528 * sizeof (*lh->file_names));
12529 }
12530 else if (lh->num_file_names >= lh->file_names_size)
12531 {
12532 lh->file_names_size *= 2;
12533 lh->file_names = xrealloc (lh->file_names,
12534 (lh->file_names_size
12535 * sizeof (*lh->file_names)));
12536 }
12537
12538 fe = &lh->file_names[lh->num_file_names++];
12539 fe->name = name;
12540 fe->dir_index = dir_index;
12541 fe->mod_time = mod_time;
12542 fe->length = length;
12543 fe->included_p = 0;
12544 fe->symtab = NULL;
12545 }
12546
12547 /* Read the statement program header starting at OFFSET in
12548 .debug_line, or .debug_line.dwo. Return a pointer
12549 to a struct line_header, allocated using xmalloc.
12550
12551 NOTE: the strings in the include directory and file name tables of
12552 the returned object point into the dwarf line section buffer,
12553 and must not be freed. */
12554
12555 static struct line_header *
12556 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12557 {
12558 struct cleanup *back_to;
12559 struct line_header *lh;
12560 gdb_byte *line_ptr;
12561 unsigned int bytes_read, offset_size;
12562 int i;
12563 char *cur_dir, *cur_file;
12564 struct dwarf2_section_info *section;
12565 bfd *abfd;
12566
12567 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12568 DWO file. */
12569 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12570 section = &cu->dwo_unit->dwo_file->sections.line;
12571 else
12572 section = &dwarf2_per_objfile->line;
12573
12574 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12575 if (section->buffer == NULL)
12576 {
12577 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12578 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12579 else
12580 complaint (&symfile_complaints, _("missing .debug_line section"));
12581 return 0;
12582 }
12583
12584 /* We can't do this until we know the section is non-empty.
12585 Only then do we know we have such a section. */
12586 abfd = section->asection->owner;
12587
12588 /* Make sure that at least there's room for the total_length field.
12589 That could be 12 bytes long, but we're just going to fudge that. */
12590 if (offset + 4 >= section->size)
12591 {
12592 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12593 return 0;
12594 }
12595
12596 lh = xmalloc (sizeof (*lh));
12597 memset (lh, 0, sizeof (*lh));
12598 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12599 (void *) lh);
12600
12601 line_ptr = section->buffer + offset;
12602
12603 /* Read in the header. */
12604 lh->total_length =
12605 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12606 &bytes_read, &offset_size);
12607 line_ptr += bytes_read;
12608 if (line_ptr + lh->total_length > (section->buffer + section->size))
12609 {
12610 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12611 return 0;
12612 }
12613 lh->statement_program_end = line_ptr + lh->total_length;
12614 lh->version = read_2_bytes (abfd, line_ptr);
12615 line_ptr += 2;
12616 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12617 line_ptr += offset_size;
12618 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12619 line_ptr += 1;
12620 if (lh->version >= 4)
12621 {
12622 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12623 line_ptr += 1;
12624 }
12625 else
12626 lh->maximum_ops_per_instruction = 1;
12627
12628 if (lh->maximum_ops_per_instruction == 0)
12629 {
12630 lh->maximum_ops_per_instruction = 1;
12631 complaint (&symfile_complaints,
12632 _("invalid maximum_ops_per_instruction "
12633 "in `.debug_line' section"));
12634 }
12635
12636 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12637 line_ptr += 1;
12638 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12639 line_ptr += 1;
12640 lh->line_range = read_1_byte (abfd, line_ptr);
12641 line_ptr += 1;
12642 lh->opcode_base = read_1_byte (abfd, line_ptr);
12643 line_ptr += 1;
12644 lh->standard_opcode_lengths
12645 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12646
12647 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12648 for (i = 1; i < lh->opcode_base; ++i)
12649 {
12650 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12651 line_ptr += 1;
12652 }
12653
12654 /* Read directory table. */
12655 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12656 {
12657 line_ptr += bytes_read;
12658 add_include_dir (lh, cur_dir);
12659 }
12660 line_ptr += bytes_read;
12661
12662 /* Read file name table. */
12663 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12664 {
12665 unsigned int dir_index, mod_time, length;
12666
12667 line_ptr += bytes_read;
12668 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12669 line_ptr += bytes_read;
12670 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12671 line_ptr += bytes_read;
12672 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12673 line_ptr += bytes_read;
12674
12675 add_file_name (lh, cur_file, dir_index, mod_time, length);
12676 }
12677 line_ptr += bytes_read;
12678 lh->statement_program_start = line_ptr;
12679
12680 if (line_ptr > (section->buffer + section->size))
12681 complaint (&symfile_complaints,
12682 _("line number info header doesn't "
12683 "fit in `.debug_line' section"));
12684
12685 discard_cleanups (back_to);
12686 return lh;
12687 }
12688
12689 /* Subroutine of dwarf_decode_lines to simplify it.
12690 Return the file name of the psymtab for included file FILE_INDEX
12691 in line header LH of PST.
12692 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12693 If space for the result is malloc'd, it will be freed by a cleanup.
12694 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12695
12696 static char *
12697 psymtab_include_file_name (const struct line_header *lh, int file_index,
12698 const struct partial_symtab *pst,
12699 const char *comp_dir)
12700 {
12701 const struct file_entry fe = lh->file_names [file_index];
12702 char *include_name = fe.name;
12703 char *include_name_to_compare = include_name;
12704 char *dir_name = NULL;
12705 const char *pst_filename;
12706 char *copied_name = NULL;
12707 int file_is_pst;
12708
12709 if (fe.dir_index)
12710 dir_name = lh->include_dirs[fe.dir_index - 1];
12711
12712 if (!IS_ABSOLUTE_PATH (include_name)
12713 && (dir_name != NULL || comp_dir != NULL))
12714 {
12715 /* Avoid creating a duplicate psymtab for PST.
12716 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12717 Before we do the comparison, however, we need to account
12718 for DIR_NAME and COMP_DIR.
12719 First prepend dir_name (if non-NULL). If we still don't
12720 have an absolute path prepend comp_dir (if non-NULL).
12721 However, the directory we record in the include-file's
12722 psymtab does not contain COMP_DIR (to match the
12723 corresponding symtab(s)).
12724
12725 Example:
12726
12727 bash$ cd /tmp
12728 bash$ gcc -g ./hello.c
12729 include_name = "hello.c"
12730 dir_name = "."
12731 DW_AT_comp_dir = comp_dir = "/tmp"
12732 DW_AT_name = "./hello.c" */
12733
12734 if (dir_name != NULL)
12735 {
12736 include_name = concat (dir_name, SLASH_STRING,
12737 include_name, (char *)NULL);
12738 include_name_to_compare = include_name;
12739 make_cleanup (xfree, include_name);
12740 }
12741 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12742 {
12743 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12744 include_name, (char *)NULL);
12745 }
12746 }
12747
12748 pst_filename = pst->filename;
12749 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12750 {
12751 copied_name = concat (pst->dirname, SLASH_STRING,
12752 pst_filename, (char *)NULL);
12753 pst_filename = copied_name;
12754 }
12755
12756 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12757
12758 if (include_name_to_compare != include_name)
12759 xfree (include_name_to_compare);
12760 if (copied_name != NULL)
12761 xfree (copied_name);
12762
12763 if (file_is_pst)
12764 return NULL;
12765 return include_name;
12766 }
12767
12768 /* Ignore this record_line request. */
12769
12770 static void
12771 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12772 {
12773 return;
12774 }
12775
12776 /* Subroutine of dwarf_decode_lines to simplify it.
12777 Process the line number information in LH. */
12778
12779 static void
12780 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12781 struct dwarf2_cu *cu, struct partial_symtab *pst)
12782 {
12783 gdb_byte *line_ptr, *extended_end;
12784 gdb_byte *line_end;
12785 unsigned int bytes_read, extended_len;
12786 unsigned char op_code, extended_op, adj_opcode;
12787 CORE_ADDR baseaddr;
12788 struct objfile *objfile = cu->objfile;
12789 bfd *abfd = objfile->obfd;
12790 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12791 const int decode_for_pst_p = (pst != NULL);
12792 struct subfile *last_subfile = NULL;
12793 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12794 = record_line;
12795
12796 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12797
12798 line_ptr = lh->statement_program_start;
12799 line_end = lh->statement_program_end;
12800
12801 /* Read the statement sequences until there's nothing left. */
12802 while (line_ptr < line_end)
12803 {
12804 /* state machine registers */
12805 CORE_ADDR address = 0;
12806 unsigned int file = 1;
12807 unsigned int line = 1;
12808 unsigned int column = 0;
12809 int is_stmt = lh->default_is_stmt;
12810 int basic_block = 0;
12811 int end_sequence = 0;
12812 CORE_ADDR addr;
12813 unsigned char op_index = 0;
12814
12815 if (!decode_for_pst_p && lh->num_file_names >= file)
12816 {
12817 /* Start a subfile for the current file of the state machine. */
12818 /* lh->include_dirs and lh->file_names are 0-based, but the
12819 directory and file name numbers in the statement program
12820 are 1-based. */
12821 struct file_entry *fe = &lh->file_names[file - 1];
12822 char *dir = NULL;
12823
12824 if (fe->dir_index)
12825 dir = lh->include_dirs[fe->dir_index - 1];
12826
12827 dwarf2_start_subfile (fe->name, dir, comp_dir);
12828 }
12829
12830 /* Decode the table. */
12831 while (!end_sequence)
12832 {
12833 op_code = read_1_byte (abfd, line_ptr);
12834 line_ptr += 1;
12835 if (line_ptr > line_end)
12836 {
12837 dwarf2_debug_line_missing_end_sequence_complaint ();
12838 break;
12839 }
12840
12841 if (op_code >= lh->opcode_base)
12842 {
12843 /* Special operand. */
12844 adj_opcode = op_code - lh->opcode_base;
12845 address += (((op_index + (adj_opcode / lh->line_range))
12846 / lh->maximum_ops_per_instruction)
12847 * lh->minimum_instruction_length);
12848 op_index = ((op_index + (adj_opcode / lh->line_range))
12849 % lh->maximum_ops_per_instruction);
12850 line += lh->line_base + (adj_opcode % lh->line_range);
12851 if (lh->num_file_names < file || file == 0)
12852 dwarf2_debug_line_missing_file_complaint ();
12853 /* For now we ignore lines not starting on an
12854 instruction boundary. */
12855 else if (op_index == 0)
12856 {
12857 lh->file_names[file - 1].included_p = 1;
12858 if (!decode_for_pst_p && is_stmt)
12859 {
12860 if (last_subfile != current_subfile)
12861 {
12862 addr = gdbarch_addr_bits_remove (gdbarch, address);
12863 if (last_subfile)
12864 (*p_record_line) (last_subfile, 0, addr);
12865 last_subfile = current_subfile;
12866 }
12867 /* Append row to matrix using current values. */
12868 addr = gdbarch_addr_bits_remove (gdbarch, address);
12869 (*p_record_line) (current_subfile, line, addr);
12870 }
12871 }
12872 basic_block = 0;
12873 }
12874 else switch (op_code)
12875 {
12876 case DW_LNS_extended_op:
12877 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12878 &bytes_read);
12879 line_ptr += bytes_read;
12880 extended_end = line_ptr + extended_len;
12881 extended_op = read_1_byte (abfd, line_ptr);
12882 line_ptr += 1;
12883 switch (extended_op)
12884 {
12885 case DW_LNE_end_sequence:
12886 p_record_line = record_line;
12887 end_sequence = 1;
12888 break;
12889 case DW_LNE_set_address:
12890 address = read_address (abfd, line_ptr, cu, &bytes_read);
12891
12892 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12893 {
12894 /* This line table is for a function which has been
12895 GCd by the linker. Ignore it. PR gdb/12528 */
12896
12897 long line_offset
12898 = line_ptr - dwarf2_per_objfile->line.buffer;
12899
12900 complaint (&symfile_complaints,
12901 _(".debug_line address at offset 0x%lx is 0 "
12902 "[in module %s]"),
12903 line_offset, objfile->name);
12904 p_record_line = noop_record_line;
12905 }
12906
12907 op_index = 0;
12908 line_ptr += bytes_read;
12909 address += baseaddr;
12910 break;
12911 case DW_LNE_define_file:
12912 {
12913 char *cur_file;
12914 unsigned int dir_index, mod_time, length;
12915
12916 cur_file = read_direct_string (abfd, line_ptr,
12917 &bytes_read);
12918 line_ptr += bytes_read;
12919 dir_index =
12920 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12921 line_ptr += bytes_read;
12922 mod_time =
12923 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12924 line_ptr += bytes_read;
12925 length =
12926 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12927 line_ptr += bytes_read;
12928 add_file_name (lh, cur_file, dir_index, mod_time, length);
12929 }
12930 break;
12931 case DW_LNE_set_discriminator:
12932 /* The discriminator is not interesting to the debugger;
12933 just ignore it. */
12934 line_ptr = extended_end;
12935 break;
12936 default:
12937 complaint (&symfile_complaints,
12938 _("mangled .debug_line section"));
12939 return;
12940 }
12941 /* Make sure that we parsed the extended op correctly. If e.g.
12942 we expected a different address size than the producer used,
12943 we may have read the wrong number of bytes. */
12944 if (line_ptr != extended_end)
12945 {
12946 complaint (&symfile_complaints,
12947 _("mangled .debug_line section"));
12948 return;
12949 }
12950 break;
12951 case DW_LNS_copy:
12952 if (lh->num_file_names < file || file == 0)
12953 dwarf2_debug_line_missing_file_complaint ();
12954 else
12955 {
12956 lh->file_names[file - 1].included_p = 1;
12957 if (!decode_for_pst_p && is_stmt)
12958 {
12959 if (last_subfile != current_subfile)
12960 {
12961 addr = gdbarch_addr_bits_remove (gdbarch, address);
12962 if (last_subfile)
12963 (*p_record_line) (last_subfile, 0, addr);
12964 last_subfile = current_subfile;
12965 }
12966 addr = gdbarch_addr_bits_remove (gdbarch, address);
12967 (*p_record_line) (current_subfile, line, addr);
12968 }
12969 }
12970 basic_block = 0;
12971 break;
12972 case DW_LNS_advance_pc:
12973 {
12974 CORE_ADDR adjust
12975 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12976
12977 address += (((op_index + adjust)
12978 / lh->maximum_ops_per_instruction)
12979 * lh->minimum_instruction_length);
12980 op_index = ((op_index + adjust)
12981 % lh->maximum_ops_per_instruction);
12982 line_ptr += bytes_read;
12983 }
12984 break;
12985 case DW_LNS_advance_line:
12986 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12987 line_ptr += bytes_read;
12988 break;
12989 case DW_LNS_set_file:
12990 {
12991 /* The arrays lh->include_dirs and lh->file_names are
12992 0-based, but the directory and file name numbers in
12993 the statement program are 1-based. */
12994 struct file_entry *fe;
12995 char *dir = NULL;
12996
12997 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12998 line_ptr += bytes_read;
12999 if (lh->num_file_names < file || file == 0)
13000 dwarf2_debug_line_missing_file_complaint ();
13001 else
13002 {
13003 fe = &lh->file_names[file - 1];
13004 if (fe->dir_index)
13005 dir = lh->include_dirs[fe->dir_index - 1];
13006 if (!decode_for_pst_p)
13007 {
13008 last_subfile = current_subfile;
13009 dwarf2_start_subfile (fe->name, dir, comp_dir);
13010 }
13011 }
13012 }
13013 break;
13014 case DW_LNS_set_column:
13015 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13016 line_ptr += bytes_read;
13017 break;
13018 case DW_LNS_negate_stmt:
13019 is_stmt = (!is_stmt);
13020 break;
13021 case DW_LNS_set_basic_block:
13022 basic_block = 1;
13023 break;
13024 /* Add to the address register of the state machine the
13025 address increment value corresponding to special opcode
13026 255. I.e., this value is scaled by the minimum
13027 instruction length since special opcode 255 would have
13028 scaled the increment. */
13029 case DW_LNS_const_add_pc:
13030 {
13031 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13032
13033 address += (((op_index + adjust)
13034 / lh->maximum_ops_per_instruction)
13035 * lh->minimum_instruction_length);
13036 op_index = ((op_index + adjust)
13037 % lh->maximum_ops_per_instruction);
13038 }
13039 break;
13040 case DW_LNS_fixed_advance_pc:
13041 address += read_2_bytes (abfd, line_ptr);
13042 op_index = 0;
13043 line_ptr += 2;
13044 break;
13045 default:
13046 {
13047 /* Unknown standard opcode, ignore it. */
13048 int i;
13049
13050 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13051 {
13052 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13053 line_ptr += bytes_read;
13054 }
13055 }
13056 }
13057 }
13058 if (lh->num_file_names < file || file == 0)
13059 dwarf2_debug_line_missing_file_complaint ();
13060 else
13061 {
13062 lh->file_names[file - 1].included_p = 1;
13063 if (!decode_for_pst_p)
13064 {
13065 addr = gdbarch_addr_bits_remove (gdbarch, address);
13066 (*p_record_line) (current_subfile, 0, addr);
13067 }
13068 }
13069 }
13070 }
13071
13072 /* Decode the Line Number Program (LNP) for the given line_header
13073 structure and CU. The actual information extracted and the type
13074 of structures created from the LNP depends on the value of PST.
13075
13076 1. If PST is NULL, then this procedure uses the data from the program
13077 to create all necessary symbol tables, and their linetables.
13078
13079 2. If PST is not NULL, this procedure reads the program to determine
13080 the list of files included by the unit represented by PST, and
13081 builds all the associated partial symbol tables.
13082
13083 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13084 It is used for relative paths in the line table.
13085 NOTE: When processing partial symtabs (pst != NULL),
13086 comp_dir == pst->dirname.
13087
13088 NOTE: It is important that psymtabs have the same file name (via strcmp)
13089 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13090 symtab we don't use it in the name of the psymtabs we create.
13091 E.g. expand_line_sal requires this when finding psymtabs to expand.
13092 A good testcase for this is mb-inline.exp. */
13093
13094 static void
13095 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13096 struct dwarf2_cu *cu, struct partial_symtab *pst,
13097 int want_line_info)
13098 {
13099 struct objfile *objfile = cu->objfile;
13100 const int decode_for_pst_p = (pst != NULL);
13101 struct subfile *first_subfile = current_subfile;
13102
13103 if (want_line_info)
13104 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13105
13106 if (decode_for_pst_p)
13107 {
13108 int file_index;
13109
13110 /* Now that we're done scanning the Line Header Program, we can
13111 create the psymtab of each included file. */
13112 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13113 if (lh->file_names[file_index].included_p == 1)
13114 {
13115 char *include_name =
13116 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13117 if (include_name != NULL)
13118 dwarf2_create_include_psymtab (include_name, pst, objfile);
13119 }
13120 }
13121 else
13122 {
13123 /* Make sure a symtab is created for every file, even files
13124 which contain only variables (i.e. no code with associated
13125 line numbers). */
13126 int i;
13127
13128 for (i = 0; i < lh->num_file_names; i++)
13129 {
13130 char *dir = NULL;
13131 struct file_entry *fe;
13132
13133 fe = &lh->file_names[i];
13134 if (fe->dir_index)
13135 dir = lh->include_dirs[fe->dir_index - 1];
13136 dwarf2_start_subfile (fe->name, dir, comp_dir);
13137
13138 /* Skip the main file; we don't need it, and it must be
13139 allocated last, so that it will show up before the
13140 non-primary symtabs in the objfile's symtab list. */
13141 if (current_subfile == first_subfile)
13142 continue;
13143
13144 if (current_subfile->symtab == NULL)
13145 current_subfile->symtab = allocate_symtab (current_subfile->name,
13146 objfile);
13147 fe->symtab = current_subfile->symtab;
13148 }
13149 }
13150 }
13151
13152 /* Start a subfile for DWARF. FILENAME is the name of the file and
13153 DIRNAME the name of the source directory which contains FILENAME
13154 or NULL if not known. COMP_DIR is the compilation directory for the
13155 linetable's compilation unit or NULL if not known.
13156 This routine tries to keep line numbers from identical absolute and
13157 relative file names in a common subfile.
13158
13159 Using the `list' example from the GDB testsuite, which resides in
13160 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13161 of /srcdir/list0.c yields the following debugging information for list0.c:
13162
13163 DW_AT_name: /srcdir/list0.c
13164 DW_AT_comp_dir: /compdir
13165 files.files[0].name: list0.h
13166 files.files[0].dir: /srcdir
13167 files.files[1].name: list0.c
13168 files.files[1].dir: /srcdir
13169
13170 The line number information for list0.c has to end up in a single
13171 subfile, so that `break /srcdir/list0.c:1' works as expected.
13172 start_subfile will ensure that this happens provided that we pass the
13173 concatenation of files.files[1].dir and files.files[1].name as the
13174 subfile's name. */
13175
13176 static void
13177 dwarf2_start_subfile (char *filename, const char *dirname,
13178 const char *comp_dir)
13179 {
13180 char *fullname;
13181
13182 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13183 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13184 second argument to start_subfile. To be consistent, we do the
13185 same here. In order not to lose the line information directory,
13186 we concatenate it to the filename when it makes sense.
13187 Note that the Dwarf3 standard says (speaking of filenames in line
13188 information): ``The directory index is ignored for file names
13189 that represent full path names''. Thus ignoring dirname in the
13190 `else' branch below isn't an issue. */
13191
13192 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13193 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13194 else
13195 fullname = filename;
13196
13197 start_subfile (fullname, comp_dir);
13198
13199 if (fullname != filename)
13200 xfree (fullname);
13201 }
13202
13203 static void
13204 var_decode_location (struct attribute *attr, struct symbol *sym,
13205 struct dwarf2_cu *cu)
13206 {
13207 struct objfile *objfile = cu->objfile;
13208 struct comp_unit_head *cu_header = &cu->header;
13209
13210 /* NOTE drow/2003-01-30: There used to be a comment and some special
13211 code here to turn a symbol with DW_AT_external and a
13212 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13213 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13214 with some versions of binutils) where shared libraries could have
13215 relocations against symbols in their debug information - the
13216 minimal symbol would have the right address, but the debug info
13217 would not. It's no longer necessary, because we will explicitly
13218 apply relocations when we read in the debug information now. */
13219
13220 /* A DW_AT_location attribute with no contents indicates that a
13221 variable has been optimized away. */
13222 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13223 {
13224 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13225 return;
13226 }
13227
13228 /* Handle one degenerate form of location expression specially, to
13229 preserve GDB's previous behavior when section offsets are
13230 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13231 then mark this symbol as LOC_STATIC. */
13232
13233 if (attr_form_is_block (attr)
13234 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13235 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13236 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13237 && (DW_BLOCK (attr)->size
13238 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13239 {
13240 unsigned int dummy;
13241
13242 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13243 SYMBOL_VALUE_ADDRESS (sym) =
13244 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13245 else
13246 SYMBOL_VALUE_ADDRESS (sym) =
13247 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13248 SYMBOL_CLASS (sym) = LOC_STATIC;
13249 fixup_symbol_section (sym, objfile);
13250 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13251 SYMBOL_SECTION (sym));
13252 return;
13253 }
13254
13255 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13256 expression evaluator, and use LOC_COMPUTED only when necessary
13257 (i.e. when the value of a register or memory location is
13258 referenced, or a thread-local block, etc.). Then again, it might
13259 not be worthwhile. I'm assuming that it isn't unless performance
13260 or memory numbers show me otherwise. */
13261
13262 dwarf2_symbol_mark_computed (attr, sym, cu);
13263 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13264
13265 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13266 cu->has_loclist = 1;
13267 }
13268
13269 /* Given a pointer to a DWARF information entry, figure out if we need
13270 to make a symbol table entry for it, and if so, create a new entry
13271 and return a pointer to it.
13272 If TYPE is NULL, determine symbol type from the die, otherwise
13273 used the passed type.
13274 If SPACE is not NULL, use it to hold the new symbol. If it is
13275 NULL, allocate a new symbol on the objfile's obstack. */
13276
13277 static struct symbol *
13278 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13279 struct symbol *space)
13280 {
13281 struct objfile *objfile = cu->objfile;
13282 struct symbol *sym = NULL;
13283 char *name;
13284 struct attribute *attr = NULL;
13285 struct attribute *attr2 = NULL;
13286 CORE_ADDR baseaddr;
13287 struct pending **list_to_add = NULL;
13288
13289 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13290
13291 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13292
13293 name = dwarf2_name (die, cu);
13294 if (name)
13295 {
13296 const char *linkagename;
13297 int suppress_add = 0;
13298
13299 if (space)
13300 sym = space;
13301 else
13302 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13303 OBJSTAT (objfile, n_syms++);
13304
13305 /* Cache this symbol's name and the name's demangled form (if any). */
13306 SYMBOL_SET_LANGUAGE (sym, cu->language);
13307 linkagename = dwarf2_physname (name, die, cu);
13308 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13309
13310 /* Fortran does not have mangling standard and the mangling does differ
13311 between gfortran, iFort etc. */
13312 if (cu->language == language_fortran
13313 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13314 symbol_set_demangled_name (&(sym->ginfo),
13315 (char *) dwarf2_full_name (name, die, cu),
13316 NULL);
13317
13318 /* Default assumptions.
13319 Use the passed type or decode it from the die. */
13320 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13321 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13322 if (type != NULL)
13323 SYMBOL_TYPE (sym) = type;
13324 else
13325 SYMBOL_TYPE (sym) = die_type (die, cu);
13326 attr = dwarf2_attr (die,
13327 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13328 cu);
13329 if (attr)
13330 {
13331 SYMBOL_LINE (sym) = DW_UNSND (attr);
13332 }
13333
13334 attr = dwarf2_attr (die,
13335 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13336 cu);
13337 if (attr)
13338 {
13339 int file_index = DW_UNSND (attr);
13340
13341 if (cu->line_header == NULL
13342 || file_index > cu->line_header->num_file_names)
13343 complaint (&symfile_complaints,
13344 _("file index out of range"));
13345 else if (file_index > 0)
13346 {
13347 struct file_entry *fe;
13348
13349 fe = &cu->line_header->file_names[file_index - 1];
13350 SYMBOL_SYMTAB (sym) = fe->symtab;
13351 }
13352 }
13353
13354 switch (die->tag)
13355 {
13356 case DW_TAG_label:
13357 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13358 if (attr)
13359 {
13360 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13361 }
13362 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13363 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13364 SYMBOL_CLASS (sym) = LOC_LABEL;
13365 add_symbol_to_list (sym, cu->list_in_scope);
13366 break;
13367 case DW_TAG_subprogram:
13368 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13369 finish_block. */
13370 SYMBOL_CLASS (sym) = LOC_BLOCK;
13371 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13372 if ((attr2 && (DW_UNSND (attr2) != 0))
13373 || cu->language == language_ada)
13374 {
13375 /* Subprograms marked external are stored as a global symbol.
13376 Ada subprograms, whether marked external or not, are always
13377 stored as a global symbol, because we want to be able to
13378 access them globally. For instance, we want to be able
13379 to break on a nested subprogram without having to
13380 specify the context. */
13381 list_to_add = &global_symbols;
13382 }
13383 else
13384 {
13385 list_to_add = cu->list_in_scope;
13386 }
13387 break;
13388 case DW_TAG_inlined_subroutine:
13389 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13390 finish_block. */
13391 SYMBOL_CLASS (sym) = LOC_BLOCK;
13392 SYMBOL_INLINED (sym) = 1;
13393 list_to_add = cu->list_in_scope;
13394 break;
13395 case DW_TAG_template_value_param:
13396 suppress_add = 1;
13397 /* Fall through. */
13398 case DW_TAG_constant:
13399 case DW_TAG_variable:
13400 case DW_TAG_member:
13401 /* Compilation with minimal debug info may result in
13402 variables with missing type entries. Change the
13403 misleading `void' type to something sensible. */
13404 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13405 SYMBOL_TYPE (sym)
13406 = objfile_type (objfile)->nodebug_data_symbol;
13407
13408 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13409 /* In the case of DW_TAG_member, we should only be called for
13410 static const members. */
13411 if (die->tag == DW_TAG_member)
13412 {
13413 /* dwarf2_add_field uses die_is_declaration,
13414 so we do the same. */
13415 gdb_assert (die_is_declaration (die, cu));
13416 gdb_assert (attr);
13417 }
13418 if (attr)
13419 {
13420 dwarf2_const_value (attr, sym, cu);
13421 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13422 if (!suppress_add)
13423 {
13424 if (attr2 && (DW_UNSND (attr2) != 0))
13425 list_to_add = &global_symbols;
13426 else
13427 list_to_add = cu->list_in_scope;
13428 }
13429 break;
13430 }
13431 attr = dwarf2_attr (die, DW_AT_location, cu);
13432 if (attr)
13433 {
13434 var_decode_location (attr, sym, cu);
13435 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13436 if (SYMBOL_CLASS (sym) == LOC_STATIC
13437 && SYMBOL_VALUE_ADDRESS (sym) == 0
13438 && !dwarf2_per_objfile->has_section_at_zero)
13439 {
13440 /* When a static variable is eliminated by the linker,
13441 the corresponding debug information is not stripped
13442 out, but the variable address is set to null;
13443 do not add such variables into symbol table. */
13444 }
13445 else if (attr2 && (DW_UNSND (attr2) != 0))
13446 {
13447 /* Workaround gfortran PR debug/40040 - it uses
13448 DW_AT_location for variables in -fPIC libraries which may
13449 get overriden by other libraries/executable and get
13450 a different address. Resolve it by the minimal symbol
13451 which may come from inferior's executable using copy
13452 relocation. Make this workaround only for gfortran as for
13453 other compilers GDB cannot guess the minimal symbol
13454 Fortran mangling kind. */
13455 if (cu->language == language_fortran && die->parent
13456 && die->parent->tag == DW_TAG_module
13457 && cu->producer
13458 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13459 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13460
13461 /* A variable with DW_AT_external is never static,
13462 but it may be block-scoped. */
13463 list_to_add = (cu->list_in_scope == &file_symbols
13464 ? &global_symbols : cu->list_in_scope);
13465 }
13466 else
13467 list_to_add = cu->list_in_scope;
13468 }
13469 else
13470 {
13471 /* We do not know the address of this symbol.
13472 If it is an external symbol and we have type information
13473 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13474 The address of the variable will then be determined from
13475 the minimal symbol table whenever the variable is
13476 referenced. */
13477 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13478 if (attr2 && (DW_UNSND (attr2) != 0)
13479 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13480 {
13481 /* A variable with DW_AT_external is never static, but it
13482 may be block-scoped. */
13483 list_to_add = (cu->list_in_scope == &file_symbols
13484 ? &global_symbols : cu->list_in_scope);
13485
13486 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13487 }
13488 else if (!die_is_declaration (die, cu))
13489 {
13490 /* Use the default LOC_OPTIMIZED_OUT class. */
13491 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13492 if (!suppress_add)
13493 list_to_add = cu->list_in_scope;
13494 }
13495 }
13496 break;
13497 case DW_TAG_formal_parameter:
13498 /* If we are inside a function, mark this as an argument. If
13499 not, we might be looking at an argument to an inlined function
13500 when we do not have enough information to show inlined frames;
13501 pretend it's a local variable in that case so that the user can
13502 still see it. */
13503 if (context_stack_depth > 0
13504 && context_stack[context_stack_depth - 1].name != NULL)
13505 SYMBOL_IS_ARGUMENT (sym) = 1;
13506 attr = dwarf2_attr (die, DW_AT_location, cu);
13507 if (attr)
13508 {
13509 var_decode_location (attr, sym, cu);
13510 }
13511 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13512 if (attr)
13513 {
13514 dwarf2_const_value (attr, sym, cu);
13515 }
13516
13517 list_to_add = cu->list_in_scope;
13518 break;
13519 case DW_TAG_unspecified_parameters:
13520 /* From varargs functions; gdb doesn't seem to have any
13521 interest in this information, so just ignore it for now.
13522 (FIXME?) */
13523 break;
13524 case DW_TAG_template_type_param:
13525 suppress_add = 1;
13526 /* Fall through. */
13527 case DW_TAG_class_type:
13528 case DW_TAG_interface_type:
13529 case DW_TAG_structure_type:
13530 case DW_TAG_union_type:
13531 case DW_TAG_set_type:
13532 case DW_TAG_enumeration_type:
13533 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13534 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13535
13536 {
13537 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13538 really ever be static objects: otherwise, if you try
13539 to, say, break of a class's method and you're in a file
13540 which doesn't mention that class, it won't work unless
13541 the check for all static symbols in lookup_symbol_aux
13542 saves you. See the OtherFileClass tests in
13543 gdb.c++/namespace.exp. */
13544
13545 if (!suppress_add)
13546 {
13547 list_to_add = (cu->list_in_scope == &file_symbols
13548 && (cu->language == language_cplus
13549 || cu->language == language_java)
13550 ? &global_symbols : cu->list_in_scope);
13551
13552 /* The semantics of C++ state that "struct foo {
13553 ... }" also defines a typedef for "foo". A Java
13554 class declaration also defines a typedef for the
13555 class. */
13556 if (cu->language == language_cplus
13557 || cu->language == language_java
13558 || cu->language == language_ada)
13559 {
13560 /* The symbol's name is already allocated along
13561 with this objfile, so we don't need to
13562 duplicate it for the type. */
13563 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13564 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13565 }
13566 }
13567 }
13568 break;
13569 case DW_TAG_typedef:
13570 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13571 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13572 list_to_add = cu->list_in_scope;
13573 break;
13574 case DW_TAG_base_type:
13575 case DW_TAG_subrange_type:
13576 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13577 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13578 list_to_add = cu->list_in_scope;
13579 break;
13580 case DW_TAG_enumerator:
13581 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13582 if (attr)
13583 {
13584 dwarf2_const_value (attr, sym, cu);
13585 }
13586 {
13587 /* NOTE: carlton/2003-11-10: See comment above in the
13588 DW_TAG_class_type, etc. block. */
13589
13590 list_to_add = (cu->list_in_scope == &file_symbols
13591 && (cu->language == language_cplus
13592 || cu->language == language_java)
13593 ? &global_symbols : cu->list_in_scope);
13594 }
13595 break;
13596 case DW_TAG_namespace:
13597 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13598 list_to_add = &global_symbols;
13599 break;
13600 default:
13601 /* Not a tag we recognize. Hopefully we aren't processing
13602 trash data, but since we must specifically ignore things
13603 we don't recognize, there is nothing else we should do at
13604 this point. */
13605 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13606 dwarf_tag_name (die->tag));
13607 break;
13608 }
13609
13610 if (suppress_add)
13611 {
13612 sym->hash_next = objfile->template_symbols;
13613 objfile->template_symbols = sym;
13614 list_to_add = NULL;
13615 }
13616
13617 if (list_to_add != NULL)
13618 add_symbol_to_list (sym, list_to_add);
13619
13620 /* For the benefit of old versions of GCC, check for anonymous
13621 namespaces based on the demangled name. */
13622 if (!processing_has_namespace_info
13623 && cu->language == language_cplus)
13624 cp_scan_for_anonymous_namespaces (sym, objfile);
13625 }
13626 return (sym);
13627 }
13628
13629 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13630
13631 static struct symbol *
13632 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13633 {
13634 return new_symbol_full (die, type, cu, NULL);
13635 }
13636
13637 /* Given an attr with a DW_FORM_dataN value in host byte order,
13638 zero-extend it as appropriate for the symbol's type. The DWARF
13639 standard (v4) is not entirely clear about the meaning of using
13640 DW_FORM_dataN for a constant with a signed type, where the type is
13641 wider than the data. The conclusion of a discussion on the DWARF
13642 list was that this is unspecified. We choose to always zero-extend
13643 because that is the interpretation long in use by GCC. */
13644
13645 static gdb_byte *
13646 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13647 const char *name, struct obstack *obstack,
13648 struct dwarf2_cu *cu, LONGEST *value, int bits)
13649 {
13650 struct objfile *objfile = cu->objfile;
13651 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13652 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13653 LONGEST l = DW_UNSND (attr);
13654
13655 if (bits < sizeof (*value) * 8)
13656 {
13657 l &= ((LONGEST) 1 << bits) - 1;
13658 *value = l;
13659 }
13660 else if (bits == sizeof (*value) * 8)
13661 *value = l;
13662 else
13663 {
13664 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13665 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13666 return bytes;
13667 }
13668
13669 return NULL;
13670 }
13671
13672 /* Read a constant value from an attribute. Either set *VALUE, or if
13673 the value does not fit in *VALUE, set *BYTES - either already
13674 allocated on the objfile obstack, or newly allocated on OBSTACK,
13675 or, set *BATON, if we translated the constant to a location
13676 expression. */
13677
13678 static void
13679 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13680 const char *name, struct obstack *obstack,
13681 struct dwarf2_cu *cu,
13682 LONGEST *value, gdb_byte **bytes,
13683 struct dwarf2_locexpr_baton **baton)
13684 {
13685 struct objfile *objfile = cu->objfile;
13686 struct comp_unit_head *cu_header = &cu->header;
13687 struct dwarf_block *blk;
13688 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13689 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13690
13691 *value = 0;
13692 *bytes = NULL;
13693 *baton = NULL;
13694
13695 switch (attr->form)
13696 {
13697 case DW_FORM_addr:
13698 case DW_FORM_GNU_addr_index:
13699 {
13700 gdb_byte *data;
13701
13702 if (TYPE_LENGTH (type) != cu_header->addr_size)
13703 dwarf2_const_value_length_mismatch_complaint (name,
13704 cu_header->addr_size,
13705 TYPE_LENGTH (type));
13706 /* Symbols of this form are reasonably rare, so we just
13707 piggyback on the existing location code rather than writing
13708 a new implementation of symbol_computed_ops. */
13709 *baton = obstack_alloc (&objfile->objfile_obstack,
13710 sizeof (struct dwarf2_locexpr_baton));
13711 (*baton)->per_cu = cu->per_cu;
13712 gdb_assert ((*baton)->per_cu);
13713
13714 (*baton)->size = 2 + cu_header->addr_size;
13715 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13716 (*baton)->data = data;
13717
13718 data[0] = DW_OP_addr;
13719 store_unsigned_integer (&data[1], cu_header->addr_size,
13720 byte_order, DW_ADDR (attr));
13721 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13722 }
13723 break;
13724 case DW_FORM_string:
13725 case DW_FORM_strp:
13726 case DW_FORM_GNU_str_index:
13727 /* DW_STRING is already allocated on the objfile obstack, point
13728 directly to it. */
13729 *bytes = (gdb_byte *) DW_STRING (attr);
13730 break;
13731 case DW_FORM_block1:
13732 case DW_FORM_block2:
13733 case DW_FORM_block4:
13734 case DW_FORM_block:
13735 case DW_FORM_exprloc:
13736 blk = DW_BLOCK (attr);
13737 if (TYPE_LENGTH (type) != blk->size)
13738 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13739 TYPE_LENGTH (type));
13740 *bytes = blk->data;
13741 break;
13742
13743 /* The DW_AT_const_value attributes are supposed to carry the
13744 symbol's value "represented as it would be on the target
13745 architecture." By the time we get here, it's already been
13746 converted to host endianness, so we just need to sign- or
13747 zero-extend it as appropriate. */
13748 case DW_FORM_data1:
13749 *bytes = dwarf2_const_value_data (attr, type, name,
13750 obstack, cu, value, 8);
13751 break;
13752 case DW_FORM_data2:
13753 *bytes = dwarf2_const_value_data (attr, type, name,
13754 obstack, cu, value, 16);
13755 break;
13756 case DW_FORM_data4:
13757 *bytes = dwarf2_const_value_data (attr, type, name,
13758 obstack, cu, value, 32);
13759 break;
13760 case DW_FORM_data8:
13761 *bytes = dwarf2_const_value_data (attr, type, name,
13762 obstack, cu, value, 64);
13763 break;
13764
13765 case DW_FORM_sdata:
13766 *value = DW_SND (attr);
13767 break;
13768
13769 case DW_FORM_udata:
13770 *value = DW_UNSND (attr);
13771 break;
13772
13773 default:
13774 complaint (&symfile_complaints,
13775 _("unsupported const value attribute form: '%s'"),
13776 dwarf_form_name (attr->form));
13777 *value = 0;
13778 break;
13779 }
13780 }
13781
13782
13783 /* Copy constant value from an attribute to a symbol. */
13784
13785 static void
13786 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13787 struct dwarf2_cu *cu)
13788 {
13789 struct objfile *objfile = cu->objfile;
13790 struct comp_unit_head *cu_header = &cu->header;
13791 LONGEST value;
13792 gdb_byte *bytes;
13793 struct dwarf2_locexpr_baton *baton;
13794
13795 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13796 SYMBOL_PRINT_NAME (sym),
13797 &objfile->objfile_obstack, cu,
13798 &value, &bytes, &baton);
13799
13800 if (baton != NULL)
13801 {
13802 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13803 SYMBOL_LOCATION_BATON (sym) = baton;
13804 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13805 }
13806 else if (bytes != NULL)
13807 {
13808 SYMBOL_VALUE_BYTES (sym) = bytes;
13809 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13810 }
13811 else
13812 {
13813 SYMBOL_VALUE (sym) = value;
13814 SYMBOL_CLASS (sym) = LOC_CONST;
13815 }
13816 }
13817
13818 /* Return the type of the die in question using its DW_AT_type attribute. */
13819
13820 static struct type *
13821 die_type (struct die_info *die, struct dwarf2_cu *cu)
13822 {
13823 struct attribute *type_attr;
13824
13825 type_attr = dwarf2_attr (die, DW_AT_type, cu);
13826 if (!type_attr)
13827 {
13828 /* A missing DW_AT_type represents a void type. */
13829 return objfile_type (cu->objfile)->builtin_void;
13830 }
13831
13832 return lookup_die_type (die, type_attr, cu);
13833 }
13834
13835 /* True iff CU's producer generates GNAT Ada auxiliary information
13836 that allows to find parallel types through that information instead
13837 of having to do expensive parallel lookups by type name. */
13838
13839 static int
13840 need_gnat_info (struct dwarf2_cu *cu)
13841 {
13842 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13843 of GNAT produces this auxiliary information, without any indication
13844 that it is produced. Part of enhancing the FSF version of GNAT
13845 to produce that information will be to put in place an indicator
13846 that we can use in order to determine whether the descriptive type
13847 info is available or not. One suggestion that has been made is
13848 to use a new attribute, attached to the CU die. For now, assume
13849 that the descriptive type info is not available. */
13850 return 0;
13851 }
13852
13853 /* Return the auxiliary type of the die in question using its
13854 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13855 attribute is not present. */
13856
13857 static struct type *
13858 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13859 {
13860 struct attribute *type_attr;
13861
13862 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13863 if (!type_attr)
13864 return NULL;
13865
13866 return lookup_die_type (die, type_attr, cu);
13867 }
13868
13869 /* If DIE has a descriptive_type attribute, then set the TYPE's
13870 descriptive type accordingly. */
13871
13872 static void
13873 set_descriptive_type (struct type *type, struct die_info *die,
13874 struct dwarf2_cu *cu)
13875 {
13876 struct type *descriptive_type = die_descriptive_type (die, cu);
13877
13878 if (descriptive_type)
13879 {
13880 ALLOCATE_GNAT_AUX_TYPE (type);
13881 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13882 }
13883 }
13884
13885 /* Return the containing type of the die in question using its
13886 DW_AT_containing_type attribute. */
13887
13888 static struct type *
13889 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13890 {
13891 struct attribute *type_attr;
13892
13893 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
13894 if (!type_attr)
13895 error (_("Dwarf Error: Problem turning containing type into gdb type "
13896 "[in module %s]"), cu->objfile->name);
13897
13898 return lookup_die_type (die, type_attr, cu);
13899 }
13900
13901 /* Look up the type of DIE in CU using its type attribute ATTR.
13902 If there is no type substitute an error marker. */
13903
13904 static struct type *
13905 lookup_die_type (struct die_info *die, struct attribute *attr,
13906 struct dwarf2_cu *cu)
13907 {
13908 struct objfile *objfile = cu->objfile;
13909 struct type *this_type;
13910
13911 /* First see if we have it cached. */
13912
13913 if (is_ref_attr (attr))
13914 {
13915 sect_offset offset = dwarf2_get_ref_die_offset (attr);
13916
13917 this_type = get_die_type_at_offset (offset, cu->per_cu);
13918 }
13919 else if (attr->form == DW_FORM_ref_sig8)
13920 {
13921 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13922
13923 /* sig_type will be NULL if the signatured type is missing from
13924 the debug info. */
13925 if (sig_type == NULL)
13926 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13927 "at 0x%x [in module %s]"),
13928 die->offset.sect_off, objfile->name);
13929
13930 gdb_assert (sig_type->per_cu.is_debug_types);
13931 /* If we haven't filled in type_offset_in_section yet, then we
13932 haven't read the type in yet. */
13933 this_type = NULL;
13934 if (sig_type->type_offset_in_section.sect_off != 0)
13935 {
13936 this_type =
13937 get_die_type_at_offset (sig_type->type_offset_in_section,
13938 &sig_type->per_cu);
13939 }
13940 }
13941 else
13942 {
13943 dump_die_for_error (die);
13944 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
13945 dwarf_attr_name (attr->name), objfile->name);
13946 }
13947
13948 /* If not cached we need to read it in. */
13949
13950 if (this_type == NULL)
13951 {
13952 struct die_info *type_die;
13953 struct dwarf2_cu *type_cu = cu;
13954
13955 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
13956 /* If we found the type now, it's probably because the type came
13957 from an inter-CU reference and the type's CU got expanded before
13958 ours. */
13959 this_type = get_die_type (type_die, type_cu);
13960 if (this_type == NULL)
13961 this_type = read_type_die_1 (type_die, type_cu);
13962 }
13963
13964 /* If we still don't have a type use an error marker. */
13965
13966 if (this_type == NULL)
13967 {
13968 char *message, *saved;
13969
13970 /* read_type_die already issued a complaint. */
13971 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
13972 objfile->name,
13973 cu->header.offset.sect_off,
13974 die->offset.sect_off);
13975 saved = obstack_copy0 (&objfile->objfile_obstack,
13976 message, strlen (message));
13977 xfree (message);
13978
13979 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
13980 }
13981
13982 return this_type;
13983 }
13984
13985 /* Return the type in DIE, CU.
13986 Returns NULL for invalid types.
13987
13988 This first does a lookup in the appropriate type_hash table,
13989 and only reads the die in if necessary.
13990
13991 NOTE: This can be called when reading in partial or full symbols. */
13992
13993 static struct type *
13994 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
13995 {
13996 struct type *this_type;
13997
13998 this_type = get_die_type (die, cu);
13999 if (this_type)
14000 return this_type;
14001
14002 return read_type_die_1 (die, cu);
14003 }
14004
14005 /* Read the type in DIE, CU.
14006 Returns NULL for invalid types. */
14007
14008 static struct type *
14009 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14010 {
14011 struct type *this_type = NULL;
14012
14013 switch (die->tag)
14014 {
14015 case DW_TAG_class_type:
14016 case DW_TAG_interface_type:
14017 case DW_TAG_structure_type:
14018 case DW_TAG_union_type:
14019 this_type = read_structure_type (die, cu);
14020 break;
14021 case DW_TAG_enumeration_type:
14022 this_type = read_enumeration_type (die, cu);
14023 break;
14024 case DW_TAG_subprogram:
14025 case DW_TAG_subroutine_type:
14026 case DW_TAG_inlined_subroutine:
14027 this_type = read_subroutine_type (die, cu);
14028 break;
14029 case DW_TAG_array_type:
14030 this_type = read_array_type (die, cu);
14031 break;
14032 case DW_TAG_set_type:
14033 this_type = read_set_type (die, cu);
14034 break;
14035 case DW_TAG_pointer_type:
14036 this_type = read_tag_pointer_type (die, cu);
14037 break;
14038 case DW_TAG_ptr_to_member_type:
14039 this_type = read_tag_ptr_to_member_type (die, cu);
14040 break;
14041 case DW_TAG_reference_type:
14042 this_type = read_tag_reference_type (die, cu);
14043 break;
14044 case DW_TAG_const_type:
14045 this_type = read_tag_const_type (die, cu);
14046 break;
14047 case DW_TAG_volatile_type:
14048 this_type = read_tag_volatile_type (die, cu);
14049 break;
14050 case DW_TAG_string_type:
14051 this_type = read_tag_string_type (die, cu);
14052 break;
14053 case DW_TAG_typedef:
14054 this_type = read_typedef (die, cu);
14055 break;
14056 case DW_TAG_subrange_type:
14057 this_type = read_subrange_type (die, cu);
14058 break;
14059 case DW_TAG_base_type:
14060 this_type = read_base_type (die, cu);
14061 break;
14062 case DW_TAG_unspecified_type:
14063 this_type = read_unspecified_type (die, cu);
14064 break;
14065 case DW_TAG_namespace:
14066 this_type = read_namespace_type (die, cu);
14067 break;
14068 case DW_TAG_module:
14069 this_type = read_module_type (die, cu);
14070 break;
14071 default:
14072 complaint (&symfile_complaints,
14073 _("unexpected tag in read_type_die: '%s'"),
14074 dwarf_tag_name (die->tag));
14075 break;
14076 }
14077
14078 return this_type;
14079 }
14080
14081 /* See if we can figure out if the class lives in a namespace. We do
14082 this by looking for a member function; its demangled name will
14083 contain namespace info, if there is any.
14084 Return the computed name or NULL.
14085 Space for the result is allocated on the objfile's obstack.
14086 This is the full-die version of guess_partial_die_structure_name.
14087 In this case we know DIE has no useful parent. */
14088
14089 static char *
14090 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14091 {
14092 struct die_info *spec_die;
14093 struct dwarf2_cu *spec_cu;
14094 struct die_info *child;
14095
14096 spec_cu = cu;
14097 spec_die = die_specification (die, &spec_cu);
14098 if (spec_die != NULL)
14099 {
14100 die = spec_die;
14101 cu = spec_cu;
14102 }
14103
14104 for (child = die->child;
14105 child != NULL;
14106 child = child->sibling)
14107 {
14108 if (child->tag == DW_TAG_subprogram)
14109 {
14110 struct attribute *attr;
14111
14112 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14113 if (attr == NULL)
14114 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14115 if (attr != NULL)
14116 {
14117 char *actual_name
14118 = language_class_name_from_physname (cu->language_defn,
14119 DW_STRING (attr));
14120 char *name = NULL;
14121
14122 if (actual_name != NULL)
14123 {
14124 char *die_name = dwarf2_name (die, cu);
14125
14126 if (die_name != NULL
14127 && strcmp (die_name, actual_name) != 0)
14128 {
14129 /* Strip off the class name from the full name.
14130 We want the prefix. */
14131 int die_name_len = strlen (die_name);
14132 int actual_name_len = strlen (actual_name);
14133
14134 /* Test for '::' as a sanity check. */
14135 if (actual_name_len > die_name_len + 2
14136 && actual_name[actual_name_len
14137 - die_name_len - 1] == ':')
14138 name =
14139 obsavestring (actual_name,
14140 actual_name_len - die_name_len - 2,
14141 &cu->objfile->objfile_obstack);
14142 }
14143 }
14144 xfree (actual_name);
14145 return name;
14146 }
14147 }
14148 }
14149
14150 return NULL;
14151 }
14152
14153 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14154 prefix part in such case. See
14155 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14156
14157 static char *
14158 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14159 {
14160 struct attribute *attr;
14161 char *base;
14162
14163 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14164 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14165 return NULL;
14166
14167 attr = dwarf2_attr (die, DW_AT_name, cu);
14168 if (attr != NULL && DW_STRING (attr) != NULL)
14169 return NULL;
14170
14171 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14172 if (attr == NULL)
14173 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14174 if (attr == NULL || DW_STRING (attr) == NULL)
14175 return NULL;
14176
14177 /* dwarf2_name had to be already called. */
14178 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14179
14180 /* Strip the base name, keep any leading namespaces/classes. */
14181 base = strrchr (DW_STRING (attr), ':');
14182 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14183 return "";
14184
14185 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14186 &cu->objfile->objfile_obstack);
14187 }
14188
14189 /* Return the name of the namespace/class that DIE is defined within,
14190 or "" if we can't tell. The caller should not xfree the result.
14191
14192 For example, if we're within the method foo() in the following
14193 code:
14194
14195 namespace N {
14196 class C {
14197 void foo () {
14198 }
14199 };
14200 }
14201
14202 then determine_prefix on foo's die will return "N::C". */
14203
14204 static const char *
14205 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14206 {
14207 struct die_info *parent, *spec_die;
14208 struct dwarf2_cu *spec_cu;
14209 struct type *parent_type;
14210 char *retval;
14211
14212 if (cu->language != language_cplus && cu->language != language_java
14213 && cu->language != language_fortran)
14214 return "";
14215
14216 retval = anonymous_struct_prefix (die, cu);
14217 if (retval)
14218 return retval;
14219
14220 /* We have to be careful in the presence of DW_AT_specification.
14221 For example, with GCC 3.4, given the code
14222
14223 namespace N {
14224 void foo() {
14225 // Definition of N::foo.
14226 }
14227 }
14228
14229 then we'll have a tree of DIEs like this:
14230
14231 1: DW_TAG_compile_unit
14232 2: DW_TAG_namespace // N
14233 3: DW_TAG_subprogram // declaration of N::foo
14234 4: DW_TAG_subprogram // definition of N::foo
14235 DW_AT_specification // refers to die #3
14236
14237 Thus, when processing die #4, we have to pretend that we're in
14238 the context of its DW_AT_specification, namely the contex of die
14239 #3. */
14240 spec_cu = cu;
14241 spec_die = die_specification (die, &spec_cu);
14242 if (spec_die == NULL)
14243 parent = die->parent;
14244 else
14245 {
14246 parent = spec_die->parent;
14247 cu = spec_cu;
14248 }
14249
14250 if (parent == NULL)
14251 return "";
14252 else if (parent->building_fullname)
14253 {
14254 const char *name;
14255 const char *parent_name;
14256
14257 /* It has been seen on RealView 2.2 built binaries,
14258 DW_TAG_template_type_param types actually _defined_ as
14259 children of the parent class:
14260
14261 enum E {};
14262 template class <class Enum> Class{};
14263 Class<enum E> class_e;
14264
14265 1: DW_TAG_class_type (Class)
14266 2: DW_TAG_enumeration_type (E)
14267 3: DW_TAG_enumerator (enum1:0)
14268 3: DW_TAG_enumerator (enum2:1)
14269 ...
14270 2: DW_TAG_template_type_param
14271 DW_AT_type DW_FORM_ref_udata (E)
14272
14273 Besides being broken debug info, it can put GDB into an
14274 infinite loop. Consider:
14275
14276 When we're building the full name for Class<E>, we'll start
14277 at Class, and go look over its template type parameters,
14278 finding E. We'll then try to build the full name of E, and
14279 reach here. We're now trying to build the full name of E,
14280 and look over the parent DIE for containing scope. In the
14281 broken case, if we followed the parent DIE of E, we'd again
14282 find Class, and once again go look at its template type
14283 arguments, etc., etc. Simply don't consider such parent die
14284 as source-level parent of this die (it can't be, the language
14285 doesn't allow it), and break the loop here. */
14286 name = dwarf2_name (die, cu);
14287 parent_name = dwarf2_name (parent, cu);
14288 complaint (&symfile_complaints,
14289 _("template param type '%s' defined within parent '%s'"),
14290 name ? name : "<unknown>",
14291 parent_name ? parent_name : "<unknown>");
14292 return "";
14293 }
14294 else
14295 switch (parent->tag)
14296 {
14297 case DW_TAG_namespace:
14298 parent_type = read_type_die (parent, cu);
14299 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14300 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14301 Work around this problem here. */
14302 if (cu->language == language_cplus
14303 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14304 return "";
14305 /* We give a name to even anonymous namespaces. */
14306 return TYPE_TAG_NAME (parent_type);
14307 case DW_TAG_class_type:
14308 case DW_TAG_interface_type:
14309 case DW_TAG_structure_type:
14310 case DW_TAG_union_type:
14311 case DW_TAG_module:
14312 parent_type = read_type_die (parent, cu);
14313 if (TYPE_TAG_NAME (parent_type) != NULL)
14314 return TYPE_TAG_NAME (parent_type);
14315 else
14316 /* An anonymous structure is only allowed non-static data
14317 members; no typedefs, no member functions, et cetera.
14318 So it does not need a prefix. */
14319 return "";
14320 case DW_TAG_compile_unit:
14321 case DW_TAG_partial_unit:
14322 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14323 if (cu->language == language_cplus
14324 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14325 && die->child != NULL
14326 && (die->tag == DW_TAG_class_type
14327 || die->tag == DW_TAG_structure_type
14328 || die->tag == DW_TAG_union_type))
14329 {
14330 char *name = guess_full_die_structure_name (die, cu);
14331 if (name != NULL)
14332 return name;
14333 }
14334 return "";
14335 default:
14336 return determine_prefix (parent, cu);
14337 }
14338 }
14339
14340 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14341 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14342 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14343 an obconcat, otherwise allocate storage for the result. The CU argument is
14344 used to determine the language and hence, the appropriate separator. */
14345
14346 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14347
14348 static char *
14349 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14350 int physname, struct dwarf2_cu *cu)
14351 {
14352 const char *lead = "";
14353 const char *sep;
14354
14355 if (suffix == NULL || suffix[0] == '\0'
14356 || prefix == NULL || prefix[0] == '\0')
14357 sep = "";
14358 else if (cu->language == language_java)
14359 sep = ".";
14360 else if (cu->language == language_fortran && physname)
14361 {
14362 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14363 DW_AT_MIPS_linkage_name is preferred and used instead. */
14364
14365 lead = "__";
14366 sep = "_MOD_";
14367 }
14368 else
14369 sep = "::";
14370
14371 if (prefix == NULL)
14372 prefix = "";
14373 if (suffix == NULL)
14374 suffix = "";
14375
14376 if (obs == NULL)
14377 {
14378 char *retval
14379 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14380
14381 strcpy (retval, lead);
14382 strcat (retval, prefix);
14383 strcat (retval, sep);
14384 strcat (retval, suffix);
14385 return retval;
14386 }
14387 else
14388 {
14389 /* We have an obstack. */
14390 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14391 }
14392 }
14393
14394 /* Return sibling of die, NULL if no sibling. */
14395
14396 static struct die_info *
14397 sibling_die (struct die_info *die)
14398 {
14399 return die->sibling;
14400 }
14401
14402 /* Get name of a die, return NULL if not found. */
14403
14404 static char *
14405 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14406 struct obstack *obstack)
14407 {
14408 if (name && cu->language == language_cplus)
14409 {
14410 char *canon_name = cp_canonicalize_string (name);
14411
14412 if (canon_name != NULL)
14413 {
14414 if (strcmp (canon_name, name) != 0)
14415 name = obsavestring (canon_name, strlen (canon_name),
14416 obstack);
14417 xfree (canon_name);
14418 }
14419 }
14420
14421 return name;
14422 }
14423
14424 /* Get name of a die, return NULL if not found. */
14425
14426 static char *
14427 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14428 {
14429 struct attribute *attr;
14430
14431 attr = dwarf2_attr (die, DW_AT_name, cu);
14432 if ((!attr || !DW_STRING (attr))
14433 && die->tag != DW_TAG_class_type
14434 && die->tag != DW_TAG_interface_type
14435 && die->tag != DW_TAG_structure_type
14436 && die->tag != DW_TAG_union_type)
14437 return NULL;
14438
14439 switch (die->tag)
14440 {
14441 case DW_TAG_compile_unit:
14442 case DW_TAG_partial_unit:
14443 /* Compilation units have a DW_AT_name that is a filename, not
14444 a source language identifier. */
14445 case DW_TAG_enumeration_type:
14446 case DW_TAG_enumerator:
14447 /* These tags always have simple identifiers already; no need
14448 to canonicalize them. */
14449 return DW_STRING (attr);
14450
14451 case DW_TAG_subprogram:
14452 /* Java constructors will all be named "<init>", so return
14453 the class name when we see this special case. */
14454 if (cu->language == language_java
14455 && DW_STRING (attr) != NULL
14456 && strcmp (DW_STRING (attr), "<init>") == 0)
14457 {
14458 struct dwarf2_cu *spec_cu = cu;
14459 struct die_info *spec_die;
14460
14461 /* GCJ will output '<init>' for Java constructor names.
14462 For this special case, return the name of the parent class. */
14463
14464 /* GCJ may output suprogram DIEs with AT_specification set.
14465 If so, use the name of the specified DIE. */
14466 spec_die = die_specification (die, &spec_cu);
14467 if (spec_die != NULL)
14468 return dwarf2_name (spec_die, spec_cu);
14469
14470 do
14471 {
14472 die = die->parent;
14473 if (die->tag == DW_TAG_class_type)
14474 return dwarf2_name (die, cu);
14475 }
14476 while (die->tag != DW_TAG_compile_unit
14477 && die->tag != DW_TAG_partial_unit);
14478 }
14479 break;
14480
14481 case DW_TAG_class_type:
14482 case DW_TAG_interface_type:
14483 case DW_TAG_structure_type:
14484 case DW_TAG_union_type:
14485 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14486 structures or unions. These were of the form "._%d" in GCC 4.1,
14487 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14488 and GCC 4.4. We work around this problem by ignoring these. */
14489 if (attr && DW_STRING (attr)
14490 && (strncmp (DW_STRING (attr), "._", 2) == 0
14491 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14492 return NULL;
14493
14494 /* GCC might emit a nameless typedef that has a linkage name. See
14495 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14496 if (!attr || DW_STRING (attr) == NULL)
14497 {
14498 char *demangled = NULL;
14499
14500 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14501 if (attr == NULL)
14502 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14503
14504 if (attr == NULL || DW_STRING (attr) == NULL)
14505 return NULL;
14506
14507 /* Avoid demangling DW_STRING (attr) the second time on a second
14508 call for the same DIE. */
14509 if (!DW_STRING_IS_CANONICAL (attr))
14510 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14511
14512 if (demangled)
14513 {
14514 char *base;
14515
14516 /* FIXME: we already did this for the partial symbol... */
14517 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14518 &cu->objfile->objfile_obstack);
14519 DW_STRING_IS_CANONICAL (attr) = 1;
14520 xfree (demangled);
14521
14522 /* Strip any leading namespaces/classes, keep only the base name.
14523 DW_AT_name for named DIEs does not contain the prefixes. */
14524 base = strrchr (DW_STRING (attr), ':');
14525 if (base && base > DW_STRING (attr) && base[-1] == ':')
14526 return &base[1];
14527 else
14528 return DW_STRING (attr);
14529 }
14530 }
14531 break;
14532
14533 default:
14534 break;
14535 }
14536
14537 if (!DW_STRING_IS_CANONICAL (attr))
14538 {
14539 DW_STRING (attr)
14540 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14541 &cu->objfile->objfile_obstack);
14542 DW_STRING_IS_CANONICAL (attr) = 1;
14543 }
14544 return DW_STRING (attr);
14545 }
14546
14547 /* Return the die that this die in an extension of, or NULL if there
14548 is none. *EXT_CU is the CU containing DIE on input, and the CU
14549 containing the return value on output. */
14550
14551 static struct die_info *
14552 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14553 {
14554 struct attribute *attr;
14555
14556 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14557 if (attr == NULL)
14558 return NULL;
14559
14560 return follow_die_ref (die, attr, ext_cu);
14561 }
14562
14563 /* Convert a DIE tag into its string name. */
14564
14565 static const char *
14566 dwarf_tag_name (unsigned tag)
14567 {
14568 const char *name = get_DW_TAG_name (tag);
14569
14570 if (name == NULL)
14571 return "DW_TAG_<unknown>";
14572
14573 return name;
14574 }
14575
14576 /* Convert a DWARF attribute code into its string name. */
14577
14578 static const char *
14579 dwarf_attr_name (unsigned attr)
14580 {
14581 const char *name;
14582
14583 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14584 if (attr == DW_AT_MIPS_fde)
14585 return "DW_AT_MIPS_fde";
14586 #else
14587 if (attr == DW_AT_HP_block_index)
14588 return "DW_AT_HP_block_index";
14589 #endif
14590
14591 name = get_DW_AT_name (attr);
14592
14593 if (name == NULL)
14594 return "DW_AT_<unknown>";
14595
14596 return name;
14597 }
14598
14599 /* Convert a DWARF value form code into its string name. */
14600
14601 static const char *
14602 dwarf_form_name (unsigned form)
14603 {
14604 const char *name = get_DW_FORM_name (form);
14605
14606 if (name == NULL)
14607 return "DW_FORM_<unknown>";
14608
14609 return name;
14610 }
14611
14612 static char *
14613 dwarf_bool_name (unsigned mybool)
14614 {
14615 if (mybool)
14616 return "TRUE";
14617 else
14618 return "FALSE";
14619 }
14620
14621 /* Convert a DWARF type code into its string name. */
14622
14623 static const char *
14624 dwarf_type_encoding_name (unsigned enc)
14625 {
14626 const char *name = get_DW_ATE_name (enc);
14627
14628 if (name == NULL)
14629 return "DW_ATE_<unknown>";
14630
14631 return name;
14632 }
14633
14634 static void
14635 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14636 {
14637 unsigned int i;
14638
14639 print_spaces (indent, f);
14640 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14641 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14642
14643 if (die->parent != NULL)
14644 {
14645 print_spaces (indent, f);
14646 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14647 die->parent->offset.sect_off);
14648 }
14649
14650 print_spaces (indent, f);
14651 fprintf_unfiltered (f, " has children: %s\n",
14652 dwarf_bool_name (die->child != NULL));
14653
14654 print_spaces (indent, f);
14655 fprintf_unfiltered (f, " attributes:\n");
14656
14657 for (i = 0; i < die->num_attrs; ++i)
14658 {
14659 print_spaces (indent, f);
14660 fprintf_unfiltered (f, " %s (%s) ",
14661 dwarf_attr_name (die->attrs[i].name),
14662 dwarf_form_name (die->attrs[i].form));
14663
14664 switch (die->attrs[i].form)
14665 {
14666 case DW_FORM_addr:
14667 case DW_FORM_GNU_addr_index:
14668 fprintf_unfiltered (f, "address: ");
14669 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14670 break;
14671 case DW_FORM_block2:
14672 case DW_FORM_block4:
14673 case DW_FORM_block:
14674 case DW_FORM_block1:
14675 fprintf_unfiltered (f, "block: size %d",
14676 DW_BLOCK (&die->attrs[i])->size);
14677 break;
14678 case DW_FORM_exprloc:
14679 fprintf_unfiltered (f, "expression: size %u",
14680 DW_BLOCK (&die->attrs[i])->size);
14681 break;
14682 case DW_FORM_ref_addr:
14683 fprintf_unfiltered (f, "ref address: ");
14684 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14685 break;
14686 case DW_FORM_ref1:
14687 case DW_FORM_ref2:
14688 case DW_FORM_ref4:
14689 case DW_FORM_ref8:
14690 case DW_FORM_ref_udata:
14691 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14692 (long) (DW_UNSND (&die->attrs[i])));
14693 break;
14694 case DW_FORM_data1:
14695 case DW_FORM_data2:
14696 case DW_FORM_data4:
14697 case DW_FORM_data8:
14698 case DW_FORM_udata:
14699 case DW_FORM_sdata:
14700 fprintf_unfiltered (f, "constant: %s",
14701 pulongest (DW_UNSND (&die->attrs[i])));
14702 break;
14703 case DW_FORM_sec_offset:
14704 fprintf_unfiltered (f, "section offset: %s",
14705 pulongest (DW_UNSND (&die->attrs[i])));
14706 break;
14707 case DW_FORM_ref_sig8:
14708 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14709 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14710 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14711 else
14712 fprintf_unfiltered (f, "signatured type, offset: unknown");
14713 break;
14714 case DW_FORM_string:
14715 case DW_FORM_strp:
14716 case DW_FORM_GNU_str_index:
14717 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14718 DW_STRING (&die->attrs[i])
14719 ? DW_STRING (&die->attrs[i]) : "",
14720 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14721 break;
14722 case DW_FORM_flag:
14723 if (DW_UNSND (&die->attrs[i]))
14724 fprintf_unfiltered (f, "flag: TRUE");
14725 else
14726 fprintf_unfiltered (f, "flag: FALSE");
14727 break;
14728 case DW_FORM_flag_present:
14729 fprintf_unfiltered (f, "flag: TRUE");
14730 break;
14731 case DW_FORM_indirect:
14732 /* The reader will have reduced the indirect form to
14733 the "base form" so this form should not occur. */
14734 fprintf_unfiltered (f,
14735 "unexpected attribute form: DW_FORM_indirect");
14736 break;
14737 default:
14738 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14739 die->attrs[i].form);
14740 break;
14741 }
14742 fprintf_unfiltered (f, "\n");
14743 }
14744 }
14745
14746 static void
14747 dump_die_for_error (struct die_info *die)
14748 {
14749 dump_die_shallow (gdb_stderr, 0, die);
14750 }
14751
14752 static void
14753 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14754 {
14755 int indent = level * 4;
14756
14757 gdb_assert (die != NULL);
14758
14759 if (level >= max_level)
14760 return;
14761
14762 dump_die_shallow (f, indent, die);
14763
14764 if (die->child != NULL)
14765 {
14766 print_spaces (indent, f);
14767 fprintf_unfiltered (f, " Children:");
14768 if (level + 1 < max_level)
14769 {
14770 fprintf_unfiltered (f, "\n");
14771 dump_die_1 (f, level + 1, max_level, die->child);
14772 }
14773 else
14774 {
14775 fprintf_unfiltered (f,
14776 " [not printed, max nesting level reached]\n");
14777 }
14778 }
14779
14780 if (die->sibling != NULL && level > 0)
14781 {
14782 dump_die_1 (f, level, max_level, die->sibling);
14783 }
14784 }
14785
14786 /* This is called from the pdie macro in gdbinit.in.
14787 It's not static so gcc will keep a copy callable from gdb. */
14788
14789 void
14790 dump_die (struct die_info *die, int max_level)
14791 {
14792 dump_die_1 (gdb_stdlog, 0, max_level, die);
14793 }
14794
14795 static void
14796 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14797 {
14798 void **slot;
14799
14800 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14801 INSERT);
14802
14803 *slot = die;
14804 }
14805
14806 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14807 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14808
14809 static int
14810 is_ref_attr (struct attribute *attr)
14811 {
14812 switch (attr->form)
14813 {
14814 case DW_FORM_ref_addr:
14815 case DW_FORM_ref1:
14816 case DW_FORM_ref2:
14817 case DW_FORM_ref4:
14818 case DW_FORM_ref8:
14819 case DW_FORM_ref_udata:
14820 return 1;
14821 default:
14822 return 0;
14823 }
14824 }
14825
14826 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14827 required kind. */
14828
14829 static sect_offset
14830 dwarf2_get_ref_die_offset (struct attribute *attr)
14831 {
14832 sect_offset retval = { DW_UNSND (attr) };
14833
14834 if (is_ref_attr (attr))
14835 return retval;
14836
14837 retval.sect_off = 0;
14838 complaint (&symfile_complaints,
14839 _("unsupported die ref attribute form: '%s'"),
14840 dwarf_form_name (attr->form));
14841 return retval;
14842 }
14843
14844 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14845 * the value held by the attribute is not constant. */
14846
14847 static LONGEST
14848 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14849 {
14850 if (attr->form == DW_FORM_sdata)
14851 return DW_SND (attr);
14852 else if (attr->form == DW_FORM_udata
14853 || attr->form == DW_FORM_data1
14854 || attr->form == DW_FORM_data2
14855 || attr->form == DW_FORM_data4
14856 || attr->form == DW_FORM_data8)
14857 return DW_UNSND (attr);
14858 else
14859 {
14860 complaint (&symfile_complaints,
14861 _("Attribute value is not a constant (%s)"),
14862 dwarf_form_name (attr->form));
14863 return default_value;
14864 }
14865 }
14866
14867 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14868 unit and add it to our queue.
14869 The result is non-zero if PER_CU was queued, otherwise the result is zero
14870 meaning either PER_CU is already queued or it is already loaded. */
14871
14872 static int
14873 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14874 struct dwarf2_per_cu_data *per_cu,
14875 enum language pretend_language)
14876 {
14877 /* We may arrive here during partial symbol reading, if we need full
14878 DIEs to process an unusual case (e.g. template arguments). Do
14879 not queue PER_CU, just tell our caller to load its DIEs. */
14880 if (dwarf2_per_objfile->reading_partial_symbols)
14881 {
14882 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14883 return 1;
14884 return 0;
14885 }
14886
14887 /* Mark the dependence relation so that we don't flush PER_CU
14888 too early. */
14889 dwarf2_add_dependence (this_cu, per_cu);
14890
14891 /* If it's already on the queue, we have nothing to do. */
14892 if (per_cu->queued)
14893 return 0;
14894
14895 /* If the compilation unit is already loaded, just mark it as
14896 used. */
14897 if (per_cu->cu != NULL)
14898 {
14899 per_cu->cu->last_used = 0;
14900 return 0;
14901 }
14902
14903 /* Add it to the queue. */
14904 queue_comp_unit (per_cu, pretend_language);
14905
14906 return 1;
14907 }
14908
14909 /* Follow reference or signature attribute ATTR of SRC_DIE.
14910 On entry *REF_CU is the CU of SRC_DIE.
14911 On exit *REF_CU is the CU of the result. */
14912
14913 static struct die_info *
14914 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14915 struct dwarf2_cu **ref_cu)
14916 {
14917 struct die_info *die;
14918
14919 if (is_ref_attr (attr))
14920 die = follow_die_ref (src_die, attr, ref_cu);
14921 else if (attr->form == DW_FORM_ref_sig8)
14922 die = follow_die_sig (src_die, attr, ref_cu);
14923 else
14924 {
14925 dump_die_for_error (src_die);
14926 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14927 (*ref_cu)->objfile->name);
14928 }
14929
14930 return die;
14931 }
14932
14933 /* Follow reference OFFSET.
14934 On entry *REF_CU is the CU of the source die referencing OFFSET.
14935 On exit *REF_CU is the CU of the result.
14936 Returns NULL if OFFSET is invalid. */
14937
14938 static struct die_info *
14939 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
14940 {
14941 struct die_info temp_die;
14942 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14943
14944 gdb_assert (cu->per_cu != NULL);
14945
14946 target_cu = cu;
14947
14948 if (cu->per_cu->is_debug_types)
14949 {
14950 /* .debug_types CUs cannot reference anything outside their CU.
14951 If they need to, they have to reference a signatured type via
14952 DW_FORM_ref_sig8. */
14953 if (! offset_in_cu_p (&cu->header, offset))
14954 return NULL;
14955 }
14956 else if (! offset_in_cu_p (&cu->header, offset))
14957 {
14958 struct dwarf2_per_cu_data *per_cu;
14959
14960 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14961
14962 /* If necessary, add it to the queue and load its DIEs. */
14963 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
14964 load_full_comp_unit (per_cu, cu->language);
14965
14966 target_cu = per_cu->cu;
14967 }
14968 else if (cu->dies == NULL)
14969 {
14970 /* We're loading full DIEs during partial symbol reading. */
14971 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14972 load_full_comp_unit (cu->per_cu, language_minimal);
14973 }
14974
14975 *ref_cu = target_cu;
14976 temp_die.offset = offset;
14977 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
14978 }
14979
14980 /* Follow reference attribute ATTR of SRC_DIE.
14981 On entry *REF_CU is the CU of SRC_DIE.
14982 On exit *REF_CU is the CU of the result. */
14983
14984 static struct die_info *
14985 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14986 struct dwarf2_cu **ref_cu)
14987 {
14988 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14989 struct dwarf2_cu *cu = *ref_cu;
14990 struct die_info *die;
14991
14992 die = follow_die_offset (offset, ref_cu);
14993 if (!die)
14994 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14995 "at 0x%x [in module %s]"),
14996 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
14997
14998 return die;
14999 }
15000
15001 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15002 Returned value is intended for DW_OP_call*. Returned
15003 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
15004
15005 struct dwarf2_locexpr_baton
15006 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
15007 struct dwarf2_per_cu_data *per_cu,
15008 CORE_ADDR (*get_frame_pc) (void *baton),
15009 void *baton)
15010 {
15011 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
15012 struct dwarf2_cu *cu;
15013 struct die_info *die;
15014 struct attribute *attr;
15015 struct dwarf2_locexpr_baton retval;
15016
15017 dw2_setup (per_cu->objfile);
15018
15019 if (per_cu->cu == NULL)
15020 load_cu (per_cu);
15021 cu = per_cu->cu;
15022
15023 die = follow_die_offset (offset, &cu);
15024 if (!die)
15025 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
15026 offset.sect_off, per_cu->objfile->name);
15027
15028 attr = dwarf2_attr (die, DW_AT_location, cu);
15029 if (!attr)
15030 {
15031 /* DWARF: "If there is no such attribute, then there is no effect.".
15032 DATA is ignored if SIZE is 0. */
15033
15034 retval.data = NULL;
15035 retval.size = 0;
15036 }
15037 else if (attr_form_is_section_offset (attr))
15038 {
15039 struct dwarf2_loclist_baton loclist_baton;
15040 CORE_ADDR pc = (*get_frame_pc) (baton);
15041 size_t size;
15042
15043 fill_in_loclist_baton (cu, &loclist_baton, attr);
15044
15045 retval.data = dwarf2_find_location_expression (&loclist_baton,
15046 &size, pc);
15047 retval.size = size;
15048 }
15049 else
15050 {
15051 if (!attr_form_is_block (attr))
15052 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15053 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15054 offset.sect_off, per_cu->objfile->name);
15055
15056 retval.data = DW_BLOCK (attr)->data;
15057 retval.size = DW_BLOCK (attr)->size;
15058 }
15059 retval.per_cu = cu->per_cu;
15060
15061 age_cached_comp_units ();
15062
15063 return retval;
15064 }
15065
15066 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15067 PER_CU. */
15068
15069 struct type *
15070 dwarf2_get_die_type (cu_offset die_offset,
15071 struct dwarf2_per_cu_data *per_cu)
15072 {
15073 sect_offset die_offset_sect;
15074
15075 dw2_setup (per_cu->objfile);
15076
15077 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15078 return get_die_type_at_offset (die_offset_sect, per_cu);
15079 }
15080
15081 /* Follow the signature attribute ATTR in SRC_DIE.
15082 On entry *REF_CU is the CU of SRC_DIE.
15083 On exit *REF_CU is the CU of the result. */
15084
15085 static struct die_info *
15086 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15087 struct dwarf2_cu **ref_cu)
15088 {
15089 struct objfile *objfile = (*ref_cu)->objfile;
15090 struct die_info temp_die;
15091 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15092 struct dwarf2_cu *sig_cu;
15093 struct die_info *die;
15094
15095 /* sig_type will be NULL if the signatured type is missing from
15096 the debug info. */
15097 if (sig_type == NULL)
15098 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15099 "at 0x%x [in module %s]"),
15100 src_die->offset.sect_off, objfile->name);
15101
15102 /* If necessary, add it to the queue and load its DIEs. */
15103
15104 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15105 read_signatured_type (sig_type);
15106
15107 gdb_assert (sig_type->per_cu.cu != NULL);
15108
15109 sig_cu = sig_type->per_cu.cu;
15110 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15111 temp_die.offset = sig_type->type_offset_in_section;
15112 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15113 temp_die.offset.sect_off);
15114 if (die)
15115 {
15116 *ref_cu = sig_cu;
15117 return die;
15118 }
15119
15120 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15121 "from DIE at 0x%x [in module %s]"),
15122 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15123 }
15124
15125 /* Given an offset of a signatured type, return its signatured_type. */
15126
15127 static struct signatured_type *
15128 lookup_signatured_type_at_offset (struct objfile *objfile,
15129 struct dwarf2_section_info *section,
15130 sect_offset offset)
15131 {
15132 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15133 unsigned int length, initial_length_size;
15134 unsigned int sig_offset;
15135 struct signatured_type find_entry, *sig_type;
15136
15137 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15138 sig_offset = (initial_length_size
15139 + 2 /*version*/
15140 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15141 + 1 /*address_size*/);
15142 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15143 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15144
15145 /* This is only used to lookup previously recorded types.
15146 If we didn't find it, it's our bug. */
15147 gdb_assert (sig_type != NULL);
15148 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15149
15150 return sig_type;
15151 }
15152
15153 /* Load the DIEs associated with type unit PER_CU into memory. */
15154
15155 static void
15156 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15157 {
15158 struct objfile *objfile = per_cu->objfile;
15159 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15160 sect_offset offset = per_cu->offset;
15161 struct signatured_type *sig_type;
15162
15163 dwarf2_read_section (objfile, sect);
15164
15165 /* We have the section offset, but we need the signature to do the
15166 hash table lookup. */
15167 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15168 the signature to assert we found the right one.
15169 Ok, but it's a lot of work. We should simplify things so any needed
15170 assert doesn't require all this clumsiness. */
15171 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15172
15173 gdb_assert (&sig_type->per_cu == per_cu);
15174 gdb_assert (sig_type->per_cu.cu == NULL);
15175
15176 read_signatured_type (sig_type);
15177
15178 gdb_assert (sig_type->per_cu.cu != NULL);
15179 }
15180
15181 /* die_reader_func for read_signatured_type.
15182 This is identical to load_full_comp_unit_reader,
15183 but is kept separate for now. */
15184
15185 static void
15186 read_signatured_type_reader (const struct die_reader_specs *reader,
15187 gdb_byte *info_ptr,
15188 struct die_info *comp_unit_die,
15189 int has_children,
15190 void *data)
15191 {
15192 struct dwarf2_cu *cu = reader->cu;
15193
15194 gdb_assert (cu->die_hash == NULL);
15195 cu->die_hash =
15196 htab_create_alloc_ex (cu->header.length / 12,
15197 die_hash,
15198 die_eq,
15199 NULL,
15200 &cu->comp_unit_obstack,
15201 hashtab_obstack_allocate,
15202 dummy_obstack_deallocate);
15203
15204 if (has_children)
15205 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15206 &info_ptr, comp_unit_die);
15207 cu->dies = comp_unit_die;
15208 /* comp_unit_die is not stored in die_hash, no need. */
15209
15210 /* We try not to read any attributes in this function, because not
15211 all CUs needed for references have been loaded yet, and symbol
15212 table processing isn't initialized. But we have to set the CU language,
15213 or we won't be able to build types correctly.
15214 Similarly, if we do not read the producer, we can not apply
15215 producer-specific interpretation. */
15216 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15217 }
15218
15219 /* Read in a signatured type and build its CU and DIEs.
15220 If the type is a stub for the real type in a DWO file,
15221 read in the real type from the DWO file as well. */
15222
15223 static void
15224 read_signatured_type (struct signatured_type *sig_type)
15225 {
15226 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15227
15228 gdb_assert (per_cu->is_debug_types);
15229 gdb_assert (per_cu->cu == NULL);
15230
15231 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15232 }
15233
15234 /* Decode simple location descriptions.
15235 Given a pointer to a dwarf block that defines a location, compute
15236 the location and return the value.
15237
15238 NOTE drow/2003-11-18: This function is called in two situations
15239 now: for the address of static or global variables (partial symbols
15240 only) and for offsets into structures which are expected to be
15241 (more or less) constant. The partial symbol case should go away,
15242 and only the constant case should remain. That will let this
15243 function complain more accurately. A few special modes are allowed
15244 without complaint for global variables (for instance, global
15245 register values and thread-local values).
15246
15247 A location description containing no operations indicates that the
15248 object is optimized out. The return value is 0 for that case.
15249 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15250 callers will only want a very basic result and this can become a
15251 complaint.
15252
15253 Note that stack[0] is unused except as a default error return. */
15254
15255 static CORE_ADDR
15256 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15257 {
15258 struct objfile *objfile = cu->objfile;
15259 int i;
15260 int size = blk->size;
15261 gdb_byte *data = blk->data;
15262 CORE_ADDR stack[64];
15263 int stacki;
15264 unsigned int bytes_read, unsnd;
15265 gdb_byte op;
15266
15267 i = 0;
15268 stacki = 0;
15269 stack[stacki] = 0;
15270 stack[++stacki] = 0;
15271
15272 while (i < size)
15273 {
15274 op = data[i++];
15275 switch (op)
15276 {
15277 case DW_OP_lit0:
15278 case DW_OP_lit1:
15279 case DW_OP_lit2:
15280 case DW_OP_lit3:
15281 case DW_OP_lit4:
15282 case DW_OP_lit5:
15283 case DW_OP_lit6:
15284 case DW_OP_lit7:
15285 case DW_OP_lit8:
15286 case DW_OP_lit9:
15287 case DW_OP_lit10:
15288 case DW_OP_lit11:
15289 case DW_OP_lit12:
15290 case DW_OP_lit13:
15291 case DW_OP_lit14:
15292 case DW_OP_lit15:
15293 case DW_OP_lit16:
15294 case DW_OP_lit17:
15295 case DW_OP_lit18:
15296 case DW_OP_lit19:
15297 case DW_OP_lit20:
15298 case DW_OP_lit21:
15299 case DW_OP_lit22:
15300 case DW_OP_lit23:
15301 case DW_OP_lit24:
15302 case DW_OP_lit25:
15303 case DW_OP_lit26:
15304 case DW_OP_lit27:
15305 case DW_OP_lit28:
15306 case DW_OP_lit29:
15307 case DW_OP_lit30:
15308 case DW_OP_lit31:
15309 stack[++stacki] = op - DW_OP_lit0;
15310 break;
15311
15312 case DW_OP_reg0:
15313 case DW_OP_reg1:
15314 case DW_OP_reg2:
15315 case DW_OP_reg3:
15316 case DW_OP_reg4:
15317 case DW_OP_reg5:
15318 case DW_OP_reg6:
15319 case DW_OP_reg7:
15320 case DW_OP_reg8:
15321 case DW_OP_reg9:
15322 case DW_OP_reg10:
15323 case DW_OP_reg11:
15324 case DW_OP_reg12:
15325 case DW_OP_reg13:
15326 case DW_OP_reg14:
15327 case DW_OP_reg15:
15328 case DW_OP_reg16:
15329 case DW_OP_reg17:
15330 case DW_OP_reg18:
15331 case DW_OP_reg19:
15332 case DW_OP_reg20:
15333 case DW_OP_reg21:
15334 case DW_OP_reg22:
15335 case DW_OP_reg23:
15336 case DW_OP_reg24:
15337 case DW_OP_reg25:
15338 case DW_OP_reg26:
15339 case DW_OP_reg27:
15340 case DW_OP_reg28:
15341 case DW_OP_reg29:
15342 case DW_OP_reg30:
15343 case DW_OP_reg31:
15344 stack[++stacki] = op - DW_OP_reg0;
15345 if (i < size)
15346 dwarf2_complex_location_expr_complaint ();
15347 break;
15348
15349 case DW_OP_regx:
15350 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15351 i += bytes_read;
15352 stack[++stacki] = unsnd;
15353 if (i < size)
15354 dwarf2_complex_location_expr_complaint ();
15355 break;
15356
15357 case DW_OP_addr:
15358 stack[++stacki] = read_address (objfile->obfd, &data[i],
15359 cu, &bytes_read);
15360 i += bytes_read;
15361 break;
15362
15363 case DW_OP_const1u:
15364 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15365 i += 1;
15366 break;
15367
15368 case DW_OP_const1s:
15369 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15370 i += 1;
15371 break;
15372
15373 case DW_OP_const2u:
15374 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15375 i += 2;
15376 break;
15377
15378 case DW_OP_const2s:
15379 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15380 i += 2;
15381 break;
15382
15383 case DW_OP_const4u:
15384 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15385 i += 4;
15386 break;
15387
15388 case DW_OP_const4s:
15389 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15390 i += 4;
15391 break;
15392
15393 case DW_OP_const8u:
15394 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15395 i += 8;
15396 break;
15397
15398 case DW_OP_constu:
15399 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15400 &bytes_read);
15401 i += bytes_read;
15402 break;
15403
15404 case DW_OP_consts:
15405 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15406 i += bytes_read;
15407 break;
15408
15409 case DW_OP_dup:
15410 stack[stacki + 1] = stack[stacki];
15411 stacki++;
15412 break;
15413
15414 case DW_OP_plus:
15415 stack[stacki - 1] += stack[stacki];
15416 stacki--;
15417 break;
15418
15419 case DW_OP_plus_uconst:
15420 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15421 &bytes_read);
15422 i += bytes_read;
15423 break;
15424
15425 case DW_OP_minus:
15426 stack[stacki - 1] -= stack[stacki];
15427 stacki--;
15428 break;
15429
15430 case DW_OP_deref:
15431 /* If we're not the last op, then we definitely can't encode
15432 this using GDB's address_class enum. This is valid for partial
15433 global symbols, although the variable's address will be bogus
15434 in the psymtab. */
15435 if (i < size)
15436 dwarf2_complex_location_expr_complaint ();
15437 break;
15438
15439 case DW_OP_GNU_push_tls_address:
15440 /* The top of the stack has the offset from the beginning
15441 of the thread control block at which the variable is located. */
15442 /* Nothing should follow this operator, so the top of stack would
15443 be returned. */
15444 /* This is valid for partial global symbols, but the variable's
15445 address will be bogus in the psymtab. Make it always at least
15446 non-zero to not look as a variable garbage collected by linker
15447 which have DW_OP_addr 0. */
15448 if (i < size)
15449 dwarf2_complex_location_expr_complaint ();
15450 stack[stacki]++;
15451 break;
15452
15453 case DW_OP_GNU_uninit:
15454 break;
15455
15456 case DW_OP_GNU_addr_index:
15457 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15458 &bytes_read);
15459 i += bytes_read;
15460 break;
15461
15462 default:
15463 {
15464 const char *name = get_DW_OP_name (op);
15465
15466 if (name)
15467 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15468 name);
15469 else
15470 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15471 op);
15472 }
15473
15474 return (stack[stacki]);
15475 }
15476
15477 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15478 outside of the allocated space. Also enforce minimum>0. */
15479 if (stacki >= ARRAY_SIZE (stack) - 1)
15480 {
15481 complaint (&symfile_complaints,
15482 _("location description stack overflow"));
15483 return 0;
15484 }
15485
15486 if (stacki <= 0)
15487 {
15488 complaint (&symfile_complaints,
15489 _("location description stack underflow"));
15490 return 0;
15491 }
15492 }
15493 return (stack[stacki]);
15494 }
15495
15496 /* memory allocation interface */
15497
15498 static struct dwarf_block *
15499 dwarf_alloc_block (struct dwarf2_cu *cu)
15500 {
15501 struct dwarf_block *blk;
15502
15503 blk = (struct dwarf_block *)
15504 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15505 return (blk);
15506 }
15507
15508 static struct abbrev_info *
15509 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
15510 {
15511 struct abbrev_info *abbrev;
15512
15513 abbrev = (struct abbrev_info *)
15514 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
15515 memset (abbrev, 0, sizeof (struct abbrev_info));
15516 return (abbrev);
15517 }
15518
15519 static struct die_info *
15520 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15521 {
15522 struct die_info *die;
15523 size_t size = sizeof (struct die_info);
15524
15525 if (num_attrs > 1)
15526 size += (num_attrs - 1) * sizeof (struct attribute);
15527
15528 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15529 memset (die, 0, sizeof (struct die_info));
15530 return (die);
15531 }
15532
15533 \f
15534 /* Macro support. */
15535
15536 /* Return the full name of file number I in *LH's file name table.
15537 Use COMP_DIR as the name of the current directory of the
15538 compilation. The result is allocated using xmalloc; the caller is
15539 responsible for freeing it. */
15540 static char *
15541 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15542 {
15543 /* Is the file number a valid index into the line header's file name
15544 table? Remember that file numbers start with one, not zero. */
15545 if (1 <= file && file <= lh->num_file_names)
15546 {
15547 struct file_entry *fe = &lh->file_names[file - 1];
15548
15549 if (IS_ABSOLUTE_PATH (fe->name))
15550 return xstrdup (fe->name);
15551 else
15552 {
15553 const char *dir;
15554 int dir_len;
15555 char *full_name;
15556
15557 if (fe->dir_index)
15558 dir = lh->include_dirs[fe->dir_index - 1];
15559 else
15560 dir = comp_dir;
15561
15562 if (dir)
15563 {
15564 dir_len = strlen (dir);
15565 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15566 strcpy (full_name, dir);
15567 full_name[dir_len] = '/';
15568 strcpy (full_name + dir_len + 1, fe->name);
15569 return full_name;
15570 }
15571 else
15572 return xstrdup (fe->name);
15573 }
15574 }
15575 else
15576 {
15577 /* The compiler produced a bogus file number. We can at least
15578 record the macro definitions made in the file, even if we
15579 won't be able to find the file by name. */
15580 char fake_name[80];
15581
15582 sprintf (fake_name, "<bad macro file number %d>", file);
15583
15584 complaint (&symfile_complaints,
15585 _("bad file number in macro information (%d)"),
15586 file);
15587
15588 return xstrdup (fake_name);
15589 }
15590 }
15591
15592
15593 static struct macro_source_file *
15594 macro_start_file (int file, int line,
15595 struct macro_source_file *current_file,
15596 const char *comp_dir,
15597 struct line_header *lh, struct objfile *objfile)
15598 {
15599 /* The full name of this source file. */
15600 char *full_name = file_full_name (file, lh, comp_dir);
15601
15602 /* We don't create a macro table for this compilation unit
15603 at all until we actually get a filename. */
15604 if (! pending_macros)
15605 pending_macros = new_macro_table (&objfile->objfile_obstack,
15606 objfile->macro_cache);
15607
15608 if (! current_file)
15609 {
15610 /* If we have no current file, then this must be the start_file
15611 directive for the compilation unit's main source file. */
15612 current_file = macro_set_main (pending_macros, full_name);
15613 macro_define_special (pending_macros);
15614 }
15615 else
15616 current_file = macro_include (current_file, line, full_name);
15617
15618 xfree (full_name);
15619
15620 return current_file;
15621 }
15622
15623
15624 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15625 followed by a null byte. */
15626 static char *
15627 copy_string (const char *buf, int len)
15628 {
15629 char *s = xmalloc (len + 1);
15630
15631 memcpy (s, buf, len);
15632 s[len] = '\0';
15633 return s;
15634 }
15635
15636
15637 static const char *
15638 consume_improper_spaces (const char *p, const char *body)
15639 {
15640 if (*p == ' ')
15641 {
15642 complaint (&symfile_complaints,
15643 _("macro definition contains spaces "
15644 "in formal argument list:\n`%s'"),
15645 body);
15646
15647 while (*p == ' ')
15648 p++;
15649 }
15650
15651 return p;
15652 }
15653
15654
15655 static void
15656 parse_macro_definition (struct macro_source_file *file, int line,
15657 const char *body)
15658 {
15659 const char *p;
15660
15661 /* The body string takes one of two forms. For object-like macro
15662 definitions, it should be:
15663
15664 <macro name> " " <definition>
15665
15666 For function-like macro definitions, it should be:
15667
15668 <macro name> "() " <definition>
15669 or
15670 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15671
15672 Spaces may appear only where explicitly indicated, and in the
15673 <definition>.
15674
15675 The Dwarf 2 spec says that an object-like macro's name is always
15676 followed by a space, but versions of GCC around March 2002 omit
15677 the space when the macro's definition is the empty string.
15678
15679 The Dwarf 2 spec says that there should be no spaces between the
15680 formal arguments in a function-like macro's formal argument list,
15681 but versions of GCC around March 2002 include spaces after the
15682 commas. */
15683
15684
15685 /* Find the extent of the macro name. The macro name is terminated
15686 by either a space or null character (for an object-like macro) or
15687 an opening paren (for a function-like macro). */
15688 for (p = body; *p; p++)
15689 if (*p == ' ' || *p == '(')
15690 break;
15691
15692 if (*p == ' ' || *p == '\0')
15693 {
15694 /* It's an object-like macro. */
15695 int name_len = p - body;
15696 char *name = copy_string (body, name_len);
15697 const char *replacement;
15698
15699 if (*p == ' ')
15700 replacement = body + name_len + 1;
15701 else
15702 {
15703 dwarf2_macro_malformed_definition_complaint (body);
15704 replacement = body + name_len;
15705 }
15706
15707 macro_define_object (file, line, name, replacement);
15708
15709 xfree (name);
15710 }
15711 else if (*p == '(')
15712 {
15713 /* It's a function-like macro. */
15714 char *name = copy_string (body, p - body);
15715 int argc = 0;
15716 int argv_size = 1;
15717 char **argv = xmalloc (argv_size * sizeof (*argv));
15718
15719 p++;
15720
15721 p = consume_improper_spaces (p, body);
15722
15723 /* Parse the formal argument list. */
15724 while (*p && *p != ')')
15725 {
15726 /* Find the extent of the current argument name. */
15727 const char *arg_start = p;
15728
15729 while (*p && *p != ',' && *p != ')' && *p != ' ')
15730 p++;
15731
15732 if (! *p || p == arg_start)
15733 dwarf2_macro_malformed_definition_complaint (body);
15734 else
15735 {
15736 /* Make sure argv has room for the new argument. */
15737 if (argc >= argv_size)
15738 {
15739 argv_size *= 2;
15740 argv = xrealloc (argv, argv_size * sizeof (*argv));
15741 }
15742
15743 argv[argc++] = copy_string (arg_start, p - arg_start);
15744 }
15745
15746 p = consume_improper_spaces (p, body);
15747
15748 /* Consume the comma, if present. */
15749 if (*p == ',')
15750 {
15751 p++;
15752
15753 p = consume_improper_spaces (p, body);
15754 }
15755 }
15756
15757 if (*p == ')')
15758 {
15759 p++;
15760
15761 if (*p == ' ')
15762 /* Perfectly formed definition, no complaints. */
15763 macro_define_function (file, line, name,
15764 argc, (const char **) argv,
15765 p + 1);
15766 else if (*p == '\0')
15767 {
15768 /* Complain, but do define it. */
15769 dwarf2_macro_malformed_definition_complaint (body);
15770 macro_define_function (file, line, name,
15771 argc, (const char **) argv,
15772 p);
15773 }
15774 else
15775 /* Just complain. */
15776 dwarf2_macro_malformed_definition_complaint (body);
15777 }
15778 else
15779 /* Just complain. */
15780 dwarf2_macro_malformed_definition_complaint (body);
15781
15782 xfree (name);
15783 {
15784 int i;
15785
15786 for (i = 0; i < argc; i++)
15787 xfree (argv[i]);
15788 }
15789 xfree (argv);
15790 }
15791 else
15792 dwarf2_macro_malformed_definition_complaint (body);
15793 }
15794
15795 /* Skip some bytes from BYTES according to the form given in FORM.
15796 Returns the new pointer. */
15797
15798 static gdb_byte *
15799 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
15800 enum dwarf_form form,
15801 unsigned int offset_size,
15802 struct dwarf2_section_info *section)
15803 {
15804 unsigned int bytes_read;
15805
15806 switch (form)
15807 {
15808 case DW_FORM_data1:
15809 case DW_FORM_flag:
15810 ++bytes;
15811 break;
15812
15813 case DW_FORM_data2:
15814 bytes += 2;
15815 break;
15816
15817 case DW_FORM_data4:
15818 bytes += 4;
15819 break;
15820
15821 case DW_FORM_data8:
15822 bytes += 8;
15823 break;
15824
15825 case DW_FORM_string:
15826 read_direct_string (abfd, bytes, &bytes_read);
15827 bytes += bytes_read;
15828 break;
15829
15830 case DW_FORM_sec_offset:
15831 case DW_FORM_strp:
15832 bytes += offset_size;
15833 break;
15834
15835 case DW_FORM_block:
15836 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15837 bytes += bytes_read;
15838 break;
15839
15840 case DW_FORM_block1:
15841 bytes += 1 + read_1_byte (abfd, bytes);
15842 break;
15843 case DW_FORM_block2:
15844 bytes += 2 + read_2_bytes (abfd, bytes);
15845 break;
15846 case DW_FORM_block4:
15847 bytes += 4 + read_4_bytes (abfd, bytes);
15848 break;
15849
15850 case DW_FORM_sdata:
15851 case DW_FORM_udata:
15852 case DW_FORM_GNU_addr_index:
15853 case DW_FORM_GNU_str_index:
15854 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
15855 if (bytes == NULL)
15856 {
15857 dwarf2_section_buffer_overflow_complaint (section);
15858 return NULL;
15859 }
15860 break;
15861
15862 default:
15863 {
15864 complain:
15865 complaint (&symfile_complaints,
15866 _("invalid form 0x%x in `%s'"),
15867 form,
15868 section->asection->name);
15869 return NULL;
15870 }
15871 }
15872
15873 return bytes;
15874 }
15875
15876 /* A helper for dwarf_decode_macros that handles skipping an unknown
15877 opcode. Returns an updated pointer to the macro data buffer; or,
15878 on error, issues a complaint and returns NULL. */
15879
15880 static gdb_byte *
15881 skip_unknown_opcode (unsigned int opcode,
15882 gdb_byte **opcode_definitions,
15883 gdb_byte *mac_ptr, gdb_byte *mac_end,
15884 bfd *abfd,
15885 unsigned int offset_size,
15886 struct dwarf2_section_info *section)
15887 {
15888 unsigned int bytes_read, i;
15889 unsigned long arg;
15890 gdb_byte *defn;
15891
15892 if (opcode_definitions[opcode] == NULL)
15893 {
15894 complaint (&symfile_complaints,
15895 _("unrecognized DW_MACFINO opcode 0x%x"),
15896 opcode);
15897 return NULL;
15898 }
15899
15900 defn = opcode_definitions[opcode];
15901 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15902 defn += bytes_read;
15903
15904 for (i = 0; i < arg; ++i)
15905 {
15906 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
15907 section);
15908 if (mac_ptr == NULL)
15909 {
15910 /* skip_form_bytes already issued the complaint. */
15911 return NULL;
15912 }
15913 }
15914
15915 return mac_ptr;
15916 }
15917
15918 /* A helper function which parses the header of a macro section.
15919 If the macro section is the extended (for now called "GNU") type,
15920 then this updates *OFFSET_SIZE. Returns a pointer to just after
15921 the header, or issues a complaint and returns NULL on error. */
15922
15923 static gdb_byte *
15924 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15925 bfd *abfd,
15926 gdb_byte *mac_ptr,
15927 unsigned int *offset_size,
15928 int section_is_gnu)
15929 {
15930 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15931
15932 if (section_is_gnu)
15933 {
15934 unsigned int version, flags;
15935
15936 version = read_2_bytes (abfd, mac_ptr);
15937 if (version != 4)
15938 {
15939 complaint (&symfile_complaints,
15940 _("unrecognized version `%d' in .debug_macro section"),
15941 version);
15942 return NULL;
15943 }
15944 mac_ptr += 2;
15945
15946 flags = read_1_byte (abfd, mac_ptr);
15947 ++mac_ptr;
15948 *offset_size = (flags & 1) ? 8 : 4;
15949
15950 if ((flags & 2) != 0)
15951 /* We don't need the line table offset. */
15952 mac_ptr += *offset_size;
15953
15954 /* Vendor opcode descriptions. */
15955 if ((flags & 4) != 0)
15956 {
15957 unsigned int i, count;
15958
15959 count = read_1_byte (abfd, mac_ptr);
15960 ++mac_ptr;
15961 for (i = 0; i < count; ++i)
15962 {
15963 unsigned int opcode, bytes_read;
15964 unsigned long arg;
15965
15966 opcode = read_1_byte (abfd, mac_ptr);
15967 ++mac_ptr;
15968 opcode_definitions[opcode] = mac_ptr;
15969 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15970 mac_ptr += bytes_read;
15971 mac_ptr += arg;
15972 }
15973 }
15974 }
15975
15976 return mac_ptr;
15977 }
15978
15979 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15980 including DW_MACRO_GNU_transparent_include. */
15981
15982 static void
15983 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15984 struct macro_source_file *current_file,
15985 struct line_header *lh, char *comp_dir,
15986 struct dwarf2_section_info *section,
15987 int section_is_gnu,
15988 unsigned int offset_size,
15989 struct objfile *objfile,
15990 htab_t include_hash)
15991 {
15992 enum dwarf_macro_record_type macinfo_type;
15993 int at_commandline;
15994 gdb_byte *opcode_definitions[256];
15995
15996 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15997 &offset_size, section_is_gnu);
15998 if (mac_ptr == NULL)
15999 {
16000 /* We already issued a complaint. */
16001 return;
16002 }
16003
16004 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16005 GDB is still reading the definitions from command line. First
16006 DW_MACINFO_start_file will need to be ignored as it was already executed
16007 to create CURRENT_FILE for the main source holding also the command line
16008 definitions. On first met DW_MACINFO_start_file this flag is reset to
16009 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16010
16011 at_commandline = 1;
16012
16013 do
16014 {
16015 /* Do we at least have room for a macinfo type byte? */
16016 if (mac_ptr >= mac_end)
16017 {
16018 dwarf2_section_buffer_overflow_complaint (section);
16019 break;
16020 }
16021
16022 macinfo_type = read_1_byte (abfd, mac_ptr);
16023 mac_ptr++;
16024
16025 /* Note that we rely on the fact that the corresponding GNU and
16026 DWARF constants are the same. */
16027 switch (macinfo_type)
16028 {
16029 /* A zero macinfo type indicates the end of the macro
16030 information. */
16031 case 0:
16032 break;
16033
16034 case DW_MACRO_GNU_define:
16035 case DW_MACRO_GNU_undef:
16036 case DW_MACRO_GNU_define_indirect:
16037 case DW_MACRO_GNU_undef_indirect:
16038 {
16039 unsigned int bytes_read;
16040 int line;
16041 char *body;
16042 int is_define;
16043
16044 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16045 mac_ptr += bytes_read;
16046
16047 if (macinfo_type == DW_MACRO_GNU_define
16048 || macinfo_type == DW_MACRO_GNU_undef)
16049 {
16050 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16051 mac_ptr += bytes_read;
16052 }
16053 else
16054 {
16055 LONGEST str_offset;
16056
16057 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16058 mac_ptr += offset_size;
16059
16060 body = read_indirect_string_at_offset (abfd, str_offset);
16061 }
16062
16063 is_define = (macinfo_type == DW_MACRO_GNU_define
16064 || macinfo_type == DW_MACRO_GNU_define_indirect);
16065 if (! current_file)
16066 {
16067 /* DWARF violation as no main source is present. */
16068 complaint (&symfile_complaints,
16069 _("debug info with no main source gives macro %s "
16070 "on line %d: %s"),
16071 is_define ? _("definition") : _("undefinition"),
16072 line, body);
16073 break;
16074 }
16075 if ((line == 0 && !at_commandline)
16076 || (line != 0 && at_commandline))
16077 complaint (&symfile_complaints,
16078 _("debug info gives %s macro %s with %s line %d: %s"),
16079 at_commandline ? _("command-line") : _("in-file"),
16080 is_define ? _("definition") : _("undefinition"),
16081 line == 0 ? _("zero") : _("non-zero"), line, body);
16082
16083 if (is_define)
16084 parse_macro_definition (current_file, line, body);
16085 else
16086 {
16087 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16088 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16089 macro_undef (current_file, line, body);
16090 }
16091 }
16092 break;
16093
16094 case DW_MACRO_GNU_start_file:
16095 {
16096 unsigned int bytes_read;
16097 int line, file;
16098
16099 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16100 mac_ptr += bytes_read;
16101 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16102 mac_ptr += bytes_read;
16103
16104 if ((line == 0 && !at_commandline)
16105 || (line != 0 && at_commandline))
16106 complaint (&symfile_complaints,
16107 _("debug info gives source %d included "
16108 "from %s at %s line %d"),
16109 file, at_commandline ? _("command-line") : _("file"),
16110 line == 0 ? _("zero") : _("non-zero"), line);
16111
16112 if (at_commandline)
16113 {
16114 /* This DW_MACRO_GNU_start_file was executed in the
16115 pass one. */
16116 at_commandline = 0;
16117 }
16118 else
16119 current_file = macro_start_file (file, line,
16120 current_file, comp_dir,
16121 lh, objfile);
16122 }
16123 break;
16124
16125 case DW_MACRO_GNU_end_file:
16126 if (! current_file)
16127 complaint (&symfile_complaints,
16128 _("macro debug info has an unmatched "
16129 "`close_file' directive"));
16130 else
16131 {
16132 current_file = current_file->included_by;
16133 if (! current_file)
16134 {
16135 enum dwarf_macro_record_type next_type;
16136
16137 /* GCC circa March 2002 doesn't produce the zero
16138 type byte marking the end of the compilation
16139 unit. Complain if it's not there, but exit no
16140 matter what. */
16141
16142 /* Do we at least have room for a macinfo type byte? */
16143 if (mac_ptr >= mac_end)
16144 {
16145 dwarf2_section_buffer_overflow_complaint (section);
16146 return;
16147 }
16148
16149 /* We don't increment mac_ptr here, so this is just
16150 a look-ahead. */
16151 next_type = read_1_byte (abfd, mac_ptr);
16152 if (next_type != 0)
16153 complaint (&symfile_complaints,
16154 _("no terminating 0-type entry for "
16155 "macros in `.debug_macinfo' section"));
16156
16157 return;
16158 }
16159 }
16160 break;
16161
16162 case DW_MACRO_GNU_transparent_include:
16163 {
16164 LONGEST offset;
16165 void **slot;
16166
16167 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16168 mac_ptr += offset_size;
16169
16170 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16171 if (*slot != NULL)
16172 {
16173 /* This has actually happened; see
16174 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16175 complaint (&symfile_complaints,
16176 _("recursive DW_MACRO_GNU_transparent_include in "
16177 ".debug_macro section"));
16178 }
16179 else
16180 {
16181 *slot = mac_ptr;
16182
16183 dwarf_decode_macro_bytes (abfd,
16184 section->buffer + offset,
16185 mac_end, current_file,
16186 lh, comp_dir,
16187 section, section_is_gnu,
16188 offset_size, objfile, include_hash);
16189
16190 htab_remove_elt (include_hash, mac_ptr);
16191 }
16192 }
16193 break;
16194
16195 case DW_MACINFO_vendor_ext:
16196 if (!section_is_gnu)
16197 {
16198 unsigned int bytes_read;
16199 int constant;
16200
16201 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16202 mac_ptr += bytes_read;
16203 read_direct_string (abfd, mac_ptr, &bytes_read);
16204 mac_ptr += bytes_read;
16205
16206 /* We don't recognize any vendor extensions. */
16207 break;
16208 }
16209 /* FALLTHROUGH */
16210
16211 default:
16212 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16213 mac_ptr, mac_end, abfd, offset_size,
16214 section);
16215 if (mac_ptr == NULL)
16216 return;
16217 break;
16218 }
16219 } while (macinfo_type != 0);
16220 }
16221
16222 static void
16223 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16224 char *comp_dir, bfd *abfd,
16225 struct dwarf2_cu *cu,
16226 struct dwarf2_section_info *section,
16227 int section_is_gnu, const char *section_name)
16228 {
16229 struct objfile *objfile = dwarf2_per_objfile->objfile;
16230 gdb_byte *mac_ptr, *mac_end;
16231 struct macro_source_file *current_file = 0;
16232 enum dwarf_macro_record_type macinfo_type;
16233 unsigned int offset_size = cu->header.offset_size;
16234 gdb_byte *opcode_definitions[256];
16235 struct cleanup *cleanup;
16236 htab_t include_hash;
16237 void **slot;
16238
16239 dwarf2_read_section (objfile, section);
16240 if (section->buffer == NULL)
16241 {
16242 complaint (&symfile_complaints, _("missing %s section"), section_name);
16243 return;
16244 }
16245
16246 /* First pass: Find the name of the base filename.
16247 This filename is needed in order to process all macros whose definition
16248 (or undefinition) comes from the command line. These macros are defined
16249 before the first DW_MACINFO_start_file entry, and yet still need to be
16250 associated to the base file.
16251
16252 To determine the base file name, we scan the macro definitions until we
16253 reach the first DW_MACINFO_start_file entry. We then initialize
16254 CURRENT_FILE accordingly so that any macro definition found before the
16255 first DW_MACINFO_start_file can still be associated to the base file. */
16256
16257 mac_ptr = section->buffer + offset;
16258 mac_end = section->buffer + section->size;
16259
16260 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16261 &offset_size, section_is_gnu);
16262 if (mac_ptr == NULL)
16263 {
16264 /* We already issued a complaint. */
16265 return;
16266 }
16267
16268 do
16269 {
16270 /* Do we at least have room for a macinfo type byte? */
16271 if (mac_ptr >= mac_end)
16272 {
16273 /* Complaint is printed during the second pass as GDB will probably
16274 stop the first pass earlier upon finding
16275 DW_MACINFO_start_file. */
16276 break;
16277 }
16278
16279 macinfo_type = read_1_byte (abfd, mac_ptr);
16280 mac_ptr++;
16281
16282 /* Note that we rely on the fact that the corresponding GNU and
16283 DWARF constants are the same. */
16284 switch (macinfo_type)
16285 {
16286 /* A zero macinfo type indicates the end of the macro
16287 information. */
16288 case 0:
16289 break;
16290
16291 case DW_MACRO_GNU_define:
16292 case DW_MACRO_GNU_undef:
16293 /* Only skip the data by MAC_PTR. */
16294 {
16295 unsigned int bytes_read;
16296
16297 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16298 mac_ptr += bytes_read;
16299 read_direct_string (abfd, mac_ptr, &bytes_read);
16300 mac_ptr += bytes_read;
16301 }
16302 break;
16303
16304 case DW_MACRO_GNU_start_file:
16305 {
16306 unsigned int bytes_read;
16307 int line, file;
16308
16309 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16310 mac_ptr += bytes_read;
16311 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16312 mac_ptr += bytes_read;
16313
16314 current_file = macro_start_file (file, line, current_file,
16315 comp_dir, lh, objfile);
16316 }
16317 break;
16318
16319 case DW_MACRO_GNU_end_file:
16320 /* No data to skip by MAC_PTR. */
16321 break;
16322
16323 case DW_MACRO_GNU_define_indirect:
16324 case DW_MACRO_GNU_undef_indirect:
16325 {
16326 unsigned int bytes_read;
16327
16328 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16329 mac_ptr += bytes_read;
16330 mac_ptr += offset_size;
16331 }
16332 break;
16333
16334 case DW_MACRO_GNU_transparent_include:
16335 /* Note that, according to the spec, a transparent include
16336 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16337 skip this opcode. */
16338 mac_ptr += offset_size;
16339 break;
16340
16341 case DW_MACINFO_vendor_ext:
16342 /* Only skip the data by MAC_PTR. */
16343 if (!section_is_gnu)
16344 {
16345 unsigned int bytes_read;
16346
16347 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16348 mac_ptr += bytes_read;
16349 read_direct_string (abfd, mac_ptr, &bytes_read);
16350 mac_ptr += bytes_read;
16351 }
16352 /* FALLTHROUGH */
16353
16354 default:
16355 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16356 mac_ptr, mac_end, abfd, offset_size,
16357 section);
16358 if (mac_ptr == NULL)
16359 return;
16360 break;
16361 }
16362 } while (macinfo_type != 0 && current_file == NULL);
16363
16364 /* Second pass: Process all entries.
16365
16366 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16367 command-line macro definitions/undefinitions. This flag is unset when we
16368 reach the first DW_MACINFO_start_file entry. */
16369
16370 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16371 NULL, xcalloc, xfree);
16372 cleanup = make_cleanup_htab_delete (include_hash);
16373 mac_ptr = section->buffer + offset;
16374 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16375 *slot = mac_ptr;
16376 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16377 current_file, lh, comp_dir, section, section_is_gnu,
16378 offset_size, objfile, include_hash);
16379 do_cleanups (cleanup);
16380 }
16381
16382 /* Check if the attribute's form is a DW_FORM_block*
16383 if so return true else false. */
16384
16385 static int
16386 attr_form_is_block (struct attribute *attr)
16387 {
16388 return (attr == NULL ? 0 :
16389 attr->form == DW_FORM_block1
16390 || attr->form == DW_FORM_block2
16391 || attr->form == DW_FORM_block4
16392 || attr->form == DW_FORM_block
16393 || attr->form == DW_FORM_exprloc);
16394 }
16395
16396 /* Return non-zero if ATTR's value is a section offset --- classes
16397 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16398 You may use DW_UNSND (attr) to retrieve such offsets.
16399
16400 Section 7.5.4, "Attribute Encodings", explains that no attribute
16401 may have a value that belongs to more than one of these classes; it
16402 would be ambiguous if we did, because we use the same forms for all
16403 of them. */
16404
16405 static int
16406 attr_form_is_section_offset (struct attribute *attr)
16407 {
16408 return (attr->form == DW_FORM_data4
16409 || attr->form == DW_FORM_data8
16410 || attr->form == DW_FORM_sec_offset);
16411 }
16412
16413 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16414 zero otherwise. When this function returns true, you can apply
16415 dwarf2_get_attr_constant_value to it.
16416
16417 However, note that for some attributes you must check
16418 attr_form_is_section_offset before using this test. DW_FORM_data4
16419 and DW_FORM_data8 are members of both the constant class, and of
16420 the classes that contain offsets into other debug sections
16421 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16422 that, if an attribute's can be either a constant or one of the
16423 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16424 taken as section offsets, not constants. */
16425
16426 static int
16427 attr_form_is_constant (struct attribute *attr)
16428 {
16429 switch (attr->form)
16430 {
16431 case DW_FORM_sdata:
16432 case DW_FORM_udata:
16433 case DW_FORM_data1:
16434 case DW_FORM_data2:
16435 case DW_FORM_data4:
16436 case DW_FORM_data8:
16437 return 1;
16438 default:
16439 return 0;
16440 }
16441 }
16442
16443 /* Return the .debug_loc section to use for CU.
16444 For DWO files use .debug_loc.dwo. */
16445
16446 static struct dwarf2_section_info *
16447 cu_debug_loc_section (struct dwarf2_cu *cu)
16448 {
16449 if (cu->dwo_unit)
16450 return &cu->dwo_unit->dwo_file->sections.loc;
16451 return &dwarf2_per_objfile->loc;
16452 }
16453
16454 /* A helper function that fills in a dwarf2_loclist_baton. */
16455
16456 static void
16457 fill_in_loclist_baton (struct dwarf2_cu *cu,
16458 struct dwarf2_loclist_baton *baton,
16459 struct attribute *attr)
16460 {
16461 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16462
16463 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16464
16465 baton->per_cu = cu->per_cu;
16466 gdb_assert (baton->per_cu);
16467 /* We don't know how long the location list is, but make sure we
16468 don't run off the edge of the section. */
16469 baton->size = section->size - DW_UNSND (attr);
16470 baton->data = section->buffer + DW_UNSND (attr);
16471 baton->base_address = cu->base_address;
16472 baton->from_dwo = cu->dwo_unit != NULL;
16473 }
16474
16475 static void
16476 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16477 struct dwarf2_cu *cu)
16478 {
16479 struct objfile *objfile = dwarf2_per_objfile->objfile;
16480 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16481
16482 if (attr_form_is_section_offset (attr)
16483 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16484 the section. If so, fall through to the complaint in the
16485 other branch. */
16486 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16487 {
16488 struct dwarf2_loclist_baton *baton;
16489
16490 baton = obstack_alloc (&objfile->objfile_obstack,
16491 sizeof (struct dwarf2_loclist_baton));
16492
16493 fill_in_loclist_baton (cu, baton, attr);
16494
16495 if (cu->base_known == 0)
16496 complaint (&symfile_complaints,
16497 _("Location list used without "
16498 "specifying the CU base address."));
16499
16500 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16501 SYMBOL_LOCATION_BATON (sym) = baton;
16502 }
16503 else
16504 {
16505 struct dwarf2_locexpr_baton *baton;
16506
16507 baton = obstack_alloc (&objfile->objfile_obstack,
16508 sizeof (struct dwarf2_locexpr_baton));
16509 baton->per_cu = cu->per_cu;
16510 gdb_assert (baton->per_cu);
16511
16512 if (attr_form_is_block (attr))
16513 {
16514 /* Note that we're just copying the block's data pointer
16515 here, not the actual data. We're still pointing into the
16516 info_buffer for SYM's objfile; right now we never release
16517 that buffer, but when we do clean up properly this may
16518 need to change. */
16519 baton->size = DW_BLOCK (attr)->size;
16520 baton->data = DW_BLOCK (attr)->data;
16521 }
16522 else
16523 {
16524 dwarf2_invalid_attrib_class_complaint ("location description",
16525 SYMBOL_NATURAL_NAME (sym));
16526 baton->size = 0;
16527 }
16528
16529 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16530 SYMBOL_LOCATION_BATON (sym) = baton;
16531 }
16532 }
16533
16534 /* Return the OBJFILE associated with the compilation unit CU. If CU
16535 came from a separate debuginfo file, then the master objfile is
16536 returned. */
16537
16538 struct objfile *
16539 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16540 {
16541 struct objfile *objfile = per_cu->objfile;
16542
16543 /* Return the master objfile, so that we can report and look up the
16544 correct file containing this variable. */
16545 if (objfile->separate_debug_objfile_backlink)
16546 objfile = objfile->separate_debug_objfile_backlink;
16547
16548 return objfile;
16549 }
16550
16551 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16552 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16553 CU_HEADERP first. */
16554
16555 static const struct comp_unit_head *
16556 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16557 struct dwarf2_per_cu_data *per_cu)
16558 {
16559 struct objfile *objfile;
16560 struct dwarf2_per_objfile *per_objfile;
16561 gdb_byte *info_ptr;
16562
16563 if (per_cu->cu)
16564 return &per_cu->cu->header;
16565
16566 objfile = per_cu->objfile;
16567 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16568 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16569
16570 memset (cu_headerp, 0, sizeof (*cu_headerp));
16571 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16572
16573 return cu_headerp;
16574 }
16575
16576 /* Return the address size given in the compilation unit header for CU. */
16577
16578 int
16579 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16580 {
16581 struct comp_unit_head cu_header_local;
16582 const struct comp_unit_head *cu_headerp;
16583
16584 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16585
16586 return cu_headerp->addr_size;
16587 }
16588
16589 /* Return the offset size given in the compilation unit header for CU. */
16590
16591 int
16592 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16593 {
16594 struct comp_unit_head cu_header_local;
16595 const struct comp_unit_head *cu_headerp;
16596
16597 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16598
16599 return cu_headerp->offset_size;
16600 }
16601
16602 /* See its dwarf2loc.h declaration. */
16603
16604 int
16605 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16606 {
16607 struct comp_unit_head cu_header_local;
16608 const struct comp_unit_head *cu_headerp;
16609
16610 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16611
16612 if (cu_headerp->version == 2)
16613 return cu_headerp->addr_size;
16614 else
16615 return cu_headerp->offset_size;
16616 }
16617
16618 /* Return the text offset of the CU. The returned offset comes from
16619 this CU's objfile. If this objfile came from a separate debuginfo
16620 file, then the offset may be different from the corresponding
16621 offset in the parent objfile. */
16622
16623 CORE_ADDR
16624 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16625 {
16626 struct objfile *objfile = per_cu->objfile;
16627
16628 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16629 }
16630
16631 /* Locate the .debug_info compilation unit from CU's objfile which contains
16632 the DIE at OFFSET. Raises an error on failure. */
16633
16634 static struct dwarf2_per_cu_data *
16635 dwarf2_find_containing_comp_unit (sect_offset offset,
16636 struct objfile *objfile)
16637 {
16638 struct dwarf2_per_cu_data *this_cu;
16639 int low, high;
16640
16641 low = 0;
16642 high = dwarf2_per_objfile->n_comp_units - 1;
16643 while (high > low)
16644 {
16645 int mid = low + (high - low) / 2;
16646
16647 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16648 >= offset.sect_off)
16649 high = mid;
16650 else
16651 low = mid + 1;
16652 }
16653 gdb_assert (low == high);
16654 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16655 > offset.sect_off)
16656 {
16657 if (low == 0)
16658 error (_("Dwarf Error: could not find partial DIE containing "
16659 "offset 0x%lx [in module %s]"),
16660 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16661
16662 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16663 <= offset.sect_off);
16664 return dwarf2_per_objfile->all_comp_units[low-1];
16665 }
16666 else
16667 {
16668 this_cu = dwarf2_per_objfile->all_comp_units[low];
16669 if (low == dwarf2_per_objfile->n_comp_units - 1
16670 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16671 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16672 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16673 return this_cu;
16674 }
16675 }
16676
16677 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16678
16679 static void
16680 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16681 {
16682 memset (cu, 0, sizeof (*cu));
16683 per_cu->cu = cu;
16684 cu->per_cu = per_cu;
16685 cu->objfile = per_cu->objfile;
16686 obstack_init (&cu->comp_unit_obstack);
16687 }
16688
16689 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16690
16691 static void
16692 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16693 enum language pretend_language)
16694 {
16695 struct attribute *attr;
16696
16697 /* Set the language we're debugging. */
16698 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16699 if (attr)
16700 set_cu_language (DW_UNSND (attr), cu);
16701 else
16702 {
16703 cu->language = pretend_language;
16704 cu->language_defn = language_def (cu->language);
16705 }
16706
16707 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16708 if (attr)
16709 cu->producer = DW_STRING (attr);
16710 }
16711
16712 /* Release one cached compilation unit, CU. We unlink it from the tree
16713 of compilation units, but we don't remove it from the read_in_chain;
16714 the caller is responsible for that.
16715 NOTE: DATA is a void * because this function is also used as a
16716 cleanup routine. */
16717
16718 static void
16719 free_heap_comp_unit (void *data)
16720 {
16721 struct dwarf2_cu *cu = data;
16722
16723 gdb_assert (cu->per_cu != NULL);
16724 cu->per_cu->cu = NULL;
16725 cu->per_cu = NULL;
16726
16727 obstack_free (&cu->comp_unit_obstack, NULL);
16728
16729 xfree (cu);
16730 }
16731
16732 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16733 when we're finished with it. We can't free the pointer itself, but be
16734 sure to unlink it from the cache. Also release any associated storage. */
16735
16736 static void
16737 free_stack_comp_unit (void *data)
16738 {
16739 struct dwarf2_cu *cu = data;
16740
16741 gdb_assert (cu->per_cu != NULL);
16742 cu->per_cu->cu = NULL;
16743 cu->per_cu = NULL;
16744
16745 obstack_free (&cu->comp_unit_obstack, NULL);
16746 cu->partial_dies = NULL;
16747 }
16748
16749 /* Free all cached compilation units. */
16750
16751 static void
16752 free_cached_comp_units (void *data)
16753 {
16754 struct dwarf2_per_cu_data *per_cu, **last_chain;
16755
16756 per_cu = dwarf2_per_objfile->read_in_chain;
16757 last_chain = &dwarf2_per_objfile->read_in_chain;
16758 while (per_cu != NULL)
16759 {
16760 struct dwarf2_per_cu_data *next_cu;
16761
16762 next_cu = per_cu->cu->read_in_chain;
16763
16764 free_heap_comp_unit (per_cu->cu);
16765 *last_chain = next_cu;
16766
16767 per_cu = next_cu;
16768 }
16769 }
16770
16771 /* Increase the age counter on each cached compilation unit, and free
16772 any that are too old. */
16773
16774 static void
16775 age_cached_comp_units (void)
16776 {
16777 struct dwarf2_per_cu_data *per_cu, **last_chain;
16778
16779 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16780 per_cu = dwarf2_per_objfile->read_in_chain;
16781 while (per_cu != NULL)
16782 {
16783 per_cu->cu->last_used ++;
16784 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16785 dwarf2_mark (per_cu->cu);
16786 per_cu = per_cu->cu->read_in_chain;
16787 }
16788
16789 per_cu = dwarf2_per_objfile->read_in_chain;
16790 last_chain = &dwarf2_per_objfile->read_in_chain;
16791 while (per_cu != NULL)
16792 {
16793 struct dwarf2_per_cu_data *next_cu;
16794
16795 next_cu = per_cu->cu->read_in_chain;
16796
16797 if (!per_cu->cu->mark)
16798 {
16799 free_heap_comp_unit (per_cu->cu);
16800 *last_chain = next_cu;
16801 }
16802 else
16803 last_chain = &per_cu->cu->read_in_chain;
16804
16805 per_cu = next_cu;
16806 }
16807 }
16808
16809 /* Remove a single compilation unit from the cache. */
16810
16811 static void
16812 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
16813 {
16814 struct dwarf2_per_cu_data *per_cu, **last_chain;
16815
16816 per_cu = dwarf2_per_objfile->read_in_chain;
16817 last_chain = &dwarf2_per_objfile->read_in_chain;
16818 while (per_cu != NULL)
16819 {
16820 struct dwarf2_per_cu_data *next_cu;
16821
16822 next_cu = per_cu->cu->read_in_chain;
16823
16824 if (per_cu == target_per_cu)
16825 {
16826 free_heap_comp_unit (per_cu->cu);
16827 per_cu->cu = NULL;
16828 *last_chain = next_cu;
16829 break;
16830 }
16831 else
16832 last_chain = &per_cu->cu->read_in_chain;
16833
16834 per_cu = next_cu;
16835 }
16836 }
16837
16838 /* Release all extra memory associated with OBJFILE. */
16839
16840 void
16841 dwarf2_free_objfile (struct objfile *objfile)
16842 {
16843 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16844
16845 if (dwarf2_per_objfile == NULL)
16846 return;
16847
16848 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16849 free_cached_comp_units (NULL);
16850
16851 if (dwarf2_per_objfile->quick_file_names_table)
16852 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16853
16854 /* Everything else should be on the objfile obstack. */
16855 }
16856
16857 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16858 We store these in a hash table separate from the DIEs, and preserve them
16859 when the DIEs are flushed out of cache.
16860
16861 The CU "per_cu" pointer is needed because offset alone is not enough to
16862 uniquely identify the type. A file may have multiple .debug_types sections,
16863 or the type may come from a DWO file. We have to use something in
16864 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16865 routine, get_die_type_at_offset, from outside this file, and thus won't
16866 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16867 of the objfile. */
16868
16869 struct dwarf2_per_cu_offset_and_type
16870 {
16871 const struct dwarf2_per_cu_data *per_cu;
16872 sect_offset offset;
16873 struct type *type;
16874 };
16875
16876 /* Hash function for a dwarf2_per_cu_offset_and_type. */
16877
16878 static hashval_t
16879 per_cu_offset_and_type_hash (const void *item)
16880 {
16881 const struct dwarf2_per_cu_offset_and_type *ofs = item;
16882
16883 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
16884 }
16885
16886 /* Equality function for a dwarf2_per_cu_offset_and_type. */
16887
16888 static int
16889 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16890 {
16891 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16892 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
16893
16894 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16895 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
16896 }
16897
16898 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16899 table if necessary. For convenience, return TYPE.
16900
16901 The DIEs reading must have careful ordering to:
16902 * Not cause infite loops trying to read in DIEs as a prerequisite for
16903 reading current DIE.
16904 * Not trying to dereference contents of still incompletely read in types
16905 while reading in other DIEs.
16906 * Enable referencing still incompletely read in types just by a pointer to
16907 the type without accessing its fields.
16908
16909 Therefore caller should follow these rules:
16910 * Try to fetch any prerequisite types we may need to build this DIE type
16911 before building the type and calling set_die_type.
16912 * After building type call set_die_type for current DIE as soon as
16913 possible before fetching more types to complete the current type.
16914 * Make the type as complete as possible before fetching more types. */
16915
16916 static struct type *
16917 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16918 {
16919 struct dwarf2_per_cu_offset_and_type **slot, ofs;
16920 struct objfile *objfile = cu->objfile;
16921
16922 /* For Ada types, make sure that the gnat-specific data is always
16923 initialized (if not already set). There are a few types where
16924 we should not be doing so, because the type-specific area is
16925 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16926 where the type-specific area is used to store the floatformat).
16927 But this is not a problem, because the gnat-specific information
16928 is actually not needed for these types. */
16929 if (need_gnat_info (cu)
16930 && TYPE_CODE (type) != TYPE_CODE_FUNC
16931 && TYPE_CODE (type) != TYPE_CODE_FLT
16932 && !HAVE_GNAT_AUX_INFO (type))
16933 INIT_GNAT_SPECIFIC (type);
16934
16935 if (dwarf2_per_objfile->die_type_hash == NULL)
16936 {
16937 dwarf2_per_objfile->die_type_hash =
16938 htab_create_alloc_ex (127,
16939 per_cu_offset_and_type_hash,
16940 per_cu_offset_and_type_eq,
16941 NULL,
16942 &objfile->objfile_obstack,
16943 hashtab_obstack_allocate,
16944 dummy_obstack_deallocate);
16945 }
16946
16947 ofs.per_cu = cu->per_cu;
16948 ofs.offset = die->offset;
16949 ofs.type = type;
16950 slot = (struct dwarf2_per_cu_offset_and_type **)
16951 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
16952 if (*slot)
16953 complaint (&symfile_complaints,
16954 _("A problem internal to GDB: DIE 0x%x has type already set"),
16955 die->offset.sect_off);
16956 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16957 **slot = ofs;
16958 return type;
16959 }
16960
16961 /* Look up the type for the die at OFFSET in the appropriate type_hash
16962 table, or return NULL if the die does not have a saved type. */
16963
16964 static struct type *
16965 get_die_type_at_offset (sect_offset offset,
16966 struct dwarf2_per_cu_data *per_cu)
16967 {
16968 struct dwarf2_per_cu_offset_and_type *slot, ofs;
16969
16970 if (dwarf2_per_objfile->die_type_hash == NULL)
16971 return NULL;
16972
16973 ofs.per_cu = per_cu;
16974 ofs.offset = offset;
16975 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
16976 if (slot)
16977 return slot->type;
16978 else
16979 return NULL;
16980 }
16981
16982 /* Look up the type for DIE in the appropriate type_hash table,
16983 or return NULL if DIE does not have a saved type. */
16984
16985 static struct type *
16986 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16987 {
16988 return get_die_type_at_offset (die->offset, cu->per_cu);
16989 }
16990
16991 /* Add a dependence relationship from CU to REF_PER_CU. */
16992
16993 static void
16994 dwarf2_add_dependence (struct dwarf2_cu *cu,
16995 struct dwarf2_per_cu_data *ref_per_cu)
16996 {
16997 void **slot;
16998
16999 if (cu->dependencies == NULL)
17000 cu->dependencies
17001 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17002 NULL, &cu->comp_unit_obstack,
17003 hashtab_obstack_allocate,
17004 dummy_obstack_deallocate);
17005
17006 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17007 if (*slot == NULL)
17008 *slot = ref_per_cu;
17009 }
17010
17011 /* Subroutine of dwarf2_mark to pass to htab_traverse.
17012 Set the mark field in every compilation unit in the
17013 cache that we must keep because we are keeping CU. */
17014
17015 static int
17016 dwarf2_mark_helper (void **slot, void *data)
17017 {
17018 struct dwarf2_per_cu_data *per_cu;
17019
17020 per_cu = (struct dwarf2_per_cu_data *) *slot;
17021
17022 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17023 reading of the chain. As such dependencies remain valid it is not much
17024 useful to track and undo them during QUIT cleanups. */
17025 if (per_cu->cu == NULL)
17026 return 1;
17027
17028 if (per_cu->cu->mark)
17029 return 1;
17030 per_cu->cu->mark = 1;
17031
17032 if (per_cu->cu->dependencies != NULL)
17033 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17034
17035 return 1;
17036 }
17037
17038 /* Set the mark field in CU and in every other compilation unit in the
17039 cache that we must keep because we are keeping CU. */
17040
17041 static void
17042 dwarf2_mark (struct dwarf2_cu *cu)
17043 {
17044 if (cu->mark)
17045 return;
17046 cu->mark = 1;
17047 if (cu->dependencies != NULL)
17048 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17049 }
17050
17051 static void
17052 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17053 {
17054 while (per_cu)
17055 {
17056 per_cu->cu->mark = 0;
17057 per_cu = per_cu->cu->read_in_chain;
17058 }
17059 }
17060
17061 /* Trivial hash function for partial_die_info: the hash value of a DIE
17062 is its offset in .debug_info for this objfile. */
17063
17064 static hashval_t
17065 partial_die_hash (const void *item)
17066 {
17067 const struct partial_die_info *part_die = item;
17068
17069 return part_die->offset.sect_off;
17070 }
17071
17072 /* Trivial comparison function for partial_die_info structures: two DIEs
17073 are equal if they have the same offset. */
17074
17075 static int
17076 partial_die_eq (const void *item_lhs, const void *item_rhs)
17077 {
17078 const struct partial_die_info *part_die_lhs = item_lhs;
17079 const struct partial_die_info *part_die_rhs = item_rhs;
17080
17081 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17082 }
17083
17084 static struct cmd_list_element *set_dwarf2_cmdlist;
17085 static struct cmd_list_element *show_dwarf2_cmdlist;
17086
17087 static void
17088 set_dwarf2_cmd (char *args, int from_tty)
17089 {
17090 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17091 }
17092
17093 static void
17094 show_dwarf2_cmd (char *args, int from_tty)
17095 {
17096 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17097 }
17098
17099 /* If section described by INFO was mmapped, munmap it now. */
17100
17101 static void
17102 munmap_section_buffer (struct dwarf2_section_info *info)
17103 {
17104 if (info->map_addr != NULL)
17105 {
17106 #ifdef HAVE_MMAP
17107 int res;
17108
17109 res = munmap (info->map_addr, info->map_len);
17110 gdb_assert (res == 0);
17111 #else
17112 /* Without HAVE_MMAP, we should never be here to begin with. */
17113 gdb_assert_not_reached ("no mmap support");
17114 #endif
17115 }
17116 }
17117
17118 /* munmap debug sections for OBJFILE, if necessary. */
17119
17120 static void
17121 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17122 {
17123 struct dwarf2_per_objfile *data = d;
17124 int ix;
17125 struct dwarf2_section_info *section;
17126
17127 /* This is sorted according to the order they're defined in to make it easier
17128 to keep in sync. */
17129 munmap_section_buffer (&data->info);
17130 munmap_section_buffer (&data->abbrev);
17131 munmap_section_buffer (&data->line);
17132 munmap_section_buffer (&data->loc);
17133 munmap_section_buffer (&data->macinfo);
17134 munmap_section_buffer (&data->macro);
17135 munmap_section_buffer (&data->str);
17136 munmap_section_buffer (&data->ranges);
17137 munmap_section_buffer (&data->addr);
17138 munmap_section_buffer (&data->frame);
17139 munmap_section_buffer (&data->eh_frame);
17140 munmap_section_buffer (&data->gdb_index);
17141
17142 for (ix = 0;
17143 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17144 ++ix)
17145 munmap_section_buffer (section);
17146
17147 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17148 VEC_free (dwarf2_per_cu_ptr,
17149 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17150
17151 VEC_free (dwarf2_section_info_def, data->types);
17152
17153 if (data->dwo_files)
17154 free_dwo_files (data->dwo_files, objfile);
17155 }
17156
17157 \f
17158 /* The "save gdb-index" command. */
17159
17160 /* The contents of the hash table we create when building the string
17161 table. */
17162 struct strtab_entry
17163 {
17164 offset_type offset;
17165 const char *str;
17166 };
17167
17168 /* Hash function for a strtab_entry.
17169
17170 Function is used only during write_hash_table so no index format backward
17171 compatibility is needed. */
17172
17173 static hashval_t
17174 hash_strtab_entry (const void *e)
17175 {
17176 const struct strtab_entry *entry = e;
17177 return mapped_index_string_hash (INT_MAX, entry->str);
17178 }
17179
17180 /* Equality function for a strtab_entry. */
17181
17182 static int
17183 eq_strtab_entry (const void *a, const void *b)
17184 {
17185 const struct strtab_entry *ea = a;
17186 const struct strtab_entry *eb = b;
17187 return !strcmp (ea->str, eb->str);
17188 }
17189
17190 /* Create a strtab_entry hash table. */
17191
17192 static htab_t
17193 create_strtab (void)
17194 {
17195 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17196 xfree, xcalloc, xfree);
17197 }
17198
17199 /* Add a string to the constant pool. Return the string's offset in
17200 host order. */
17201
17202 static offset_type
17203 add_string (htab_t table, struct obstack *cpool, const char *str)
17204 {
17205 void **slot;
17206 struct strtab_entry entry;
17207 struct strtab_entry *result;
17208
17209 entry.str = str;
17210 slot = htab_find_slot (table, &entry, INSERT);
17211 if (*slot)
17212 result = *slot;
17213 else
17214 {
17215 result = XNEW (struct strtab_entry);
17216 result->offset = obstack_object_size (cpool);
17217 result->str = str;
17218 obstack_grow_str0 (cpool, str);
17219 *slot = result;
17220 }
17221 return result->offset;
17222 }
17223
17224 /* An entry in the symbol table. */
17225 struct symtab_index_entry
17226 {
17227 /* The name of the symbol. */
17228 const char *name;
17229 /* The offset of the name in the constant pool. */
17230 offset_type index_offset;
17231 /* A sorted vector of the indices of all the CUs that hold an object
17232 of this name. */
17233 VEC (offset_type) *cu_indices;
17234 };
17235
17236 /* The symbol table. This is a power-of-2-sized hash table. */
17237 struct mapped_symtab
17238 {
17239 offset_type n_elements;
17240 offset_type size;
17241 struct symtab_index_entry **data;
17242 };
17243
17244 /* Hash function for a symtab_index_entry. */
17245
17246 static hashval_t
17247 hash_symtab_entry (const void *e)
17248 {
17249 const struct symtab_index_entry *entry = e;
17250 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17251 sizeof (offset_type) * VEC_length (offset_type,
17252 entry->cu_indices),
17253 0);
17254 }
17255
17256 /* Equality function for a symtab_index_entry. */
17257
17258 static int
17259 eq_symtab_entry (const void *a, const void *b)
17260 {
17261 const struct symtab_index_entry *ea = a;
17262 const struct symtab_index_entry *eb = b;
17263 int len = VEC_length (offset_type, ea->cu_indices);
17264 if (len != VEC_length (offset_type, eb->cu_indices))
17265 return 0;
17266 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17267 VEC_address (offset_type, eb->cu_indices),
17268 sizeof (offset_type) * len);
17269 }
17270
17271 /* Destroy a symtab_index_entry. */
17272
17273 static void
17274 delete_symtab_entry (void *p)
17275 {
17276 struct symtab_index_entry *entry = p;
17277 VEC_free (offset_type, entry->cu_indices);
17278 xfree (entry);
17279 }
17280
17281 /* Create a hash table holding symtab_index_entry objects. */
17282
17283 static htab_t
17284 create_symbol_hash_table (void)
17285 {
17286 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17287 delete_symtab_entry, xcalloc, xfree);
17288 }
17289
17290 /* Create a new mapped symtab object. */
17291
17292 static struct mapped_symtab *
17293 create_mapped_symtab (void)
17294 {
17295 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17296 symtab->n_elements = 0;
17297 symtab->size = 1024;
17298 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17299 return symtab;
17300 }
17301
17302 /* Destroy a mapped_symtab. */
17303
17304 static void
17305 cleanup_mapped_symtab (void *p)
17306 {
17307 struct mapped_symtab *symtab = p;
17308 /* The contents of the array are freed when the other hash table is
17309 destroyed. */
17310 xfree (symtab->data);
17311 xfree (symtab);
17312 }
17313
17314 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17315 the slot.
17316
17317 Function is used only during write_hash_table so no index format backward
17318 compatibility is needed. */
17319
17320 static struct symtab_index_entry **
17321 find_slot (struct mapped_symtab *symtab, const char *name)
17322 {
17323 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17324
17325 index = hash & (symtab->size - 1);
17326 step = ((hash * 17) & (symtab->size - 1)) | 1;
17327
17328 for (;;)
17329 {
17330 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17331 return &symtab->data[index];
17332 index = (index + step) & (symtab->size - 1);
17333 }
17334 }
17335
17336 /* Expand SYMTAB's hash table. */
17337
17338 static void
17339 hash_expand (struct mapped_symtab *symtab)
17340 {
17341 offset_type old_size = symtab->size;
17342 offset_type i;
17343 struct symtab_index_entry **old_entries = symtab->data;
17344
17345 symtab->size *= 2;
17346 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17347
17348 for (i = 0; i < old_size; ++i)
17349 {
17350 if (old_entries[i])
17351 {
17352 struct symtab_index_entry **slot = find_slot (symtab,
17353 old_entries[i]->name);
17354 *slot = old_entries[i];
17355 }
17356 }
17357
17358 xfree (old_entries);
17359 }
17360
17361 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17362 is the index of the CU in which the symbol appears. */
17363
17364 static void
17365 add_index_entry (struct mapped_symtab *symtab, const char *name,
17366 offset_type cu_index)
17367 {
17368 struct symtab_index_entry **slot;
17369
17370 ++symtab->n_elements;
17371 if (4 * symtab->n_elements / 3 >= symtab->size)
17372 hash_expand (symtab);
17373
17374 slot = find_slot (symtab, name);
17375 if (!*slot)
17376 {
17377 *slot = XNEW (struct symtab_index_entry);
17378 (*slot)->name = name;
17379 (*slot)->cu_indices = NULL;
17380 }
17381 /* Don't push an index twice. Due to how we add entries we only
17382 have to check the last one. */
17383 if (VEC_empty (offset_type, (*slot)->cu_indices)
17384 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
17385 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17386 }
17387
17388 /* Add a vector of indices to the constant pool. */
17389
17390 static offset_type
17391 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17392 struct symtab_index_entry *entry)
17393 {
17394 void **slot;
17395
17396 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17397 if (!*slot)
17398 {
17399 offset_type len = VEC_length (offset_type, entry->cu_indices);
17400 offset_type val = MAYBE_SWAP (len);
17401 offset_type iter;
17402 int i;
17403
17404 *slot = entry;
17405 entry->index_offset = obstack_object_size (cpool);
17406
17407 obstack_grow (cpool, &val, sizeof (val));
17408 for (i = 0;
17409 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17410 ++i)
17411 {
17412 val = MAYBE_SWAP (iter);
17413 obstack_grow (cpool, &val, sizeof (val));
17414 }
17415 }
17416 else
17417 {
17418 struct symtab_index_entry *old_entry = *slot;
17419 entry->index_offset = old_entry->index_offset;
17420 entry = old_entry;
17421 }
17422 return entry->index_offset;
17423 }
17424
17425 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17426 constant pool entries going into the obstack CPOOL. */
17427
17428 static void
17429 write_hash_table (struct mapped_symtab *symtab,
17430 struct obstack *output, struct obstack *cpool)
17431 {
17432 offset_type i;
17433 htab_t symbol_hash_table;
17434 htab_t str_table;
17435
17436 symbol_hash_table = create_symbol_hash_table ();
17437 str_table = create_strtab ();
17438
17439 /* We add all the index vectors to the constant pool first, to
17440 ensure alignment is ok. */
17441 for (i = 0; i < symtab->size; ++i)
17442 {
17443 if (symtab->data[i])
17444 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17445 }
17446
17447 /* Now write out the hash table. */
17448 for (i = 0; i < symtab->size; ++i)
17449 {
17450 offset_type str_off, vec_off;
17451
17452 if (symtab->data[i])
17453 {
17454 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17455 vec_off = symtab->data[i]->index_offset;
17456 }
17457 else
17458 {
17459 /* While 0 is a valid constant pool index, it is not valid
17460 to have 0 for both offsets. */
17461 str_off = 0;
17462 vec_off = 0;
17463 }
17464
17465 str_off = MAYBE_SWAP (str_off);
17466 vec_off = MAYBE_SWAP (vec_off);
17467
17468 obstack_grow (output, &str_off, sizeof (str_off));
17469 obstack_grow (output, &vec_off, sizeof (vec_off));
17470 }
17471
17472 htab_delete (str_table);
17473 htab_delete (symbol_hash_table);
17474 }
17475
17476 /* Struct to map psymtab to CU index in the index file. */
17477 struct psymtab_cu_index_map
17478 {
17479 struct partial_symtab *psymtab;
17480 unsigned int cu_index;
17481 };
17482
17483 static hashval_t
17484 hash_psymtab_cu_index (const void *item)
17485 {
17486 const struct psymtab_cu_index_map *map = item;
17487
17488 return htab_hash_pointer (map->psymtab);
17489 }
17490
17491 static int
17492 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17493 {
17494 const struct psymtab_cu_index_map *lhs = item_lhs;
17495 const struct psymtab_cu_index_map *rhs = item_rhs;
17496
17497 return lhs->psymtab == rhs->psymtab;
17498 }
17499
17500 /* Helper struct for building the address table. */
17501 struct addrmap_index_data
17502 {
17503 struct objfile *objfile;
17504 struct obstack *addr_obstack;
17505 htab_t cu_index_htab;
17506
17507 /* Non-zero if the previous_* fields are valid.
17508 We can't write an entry until we see the next entry (since it is only then
17509 that we know the end of the entry). */
17510 int previous_valid;
17511 /* Index of the CU in the table of all CUs in the index file. */
17512 unsigned int previous_cu_index;
17513 /* Start address of the CU. */
17514 CORE_ADDR previous_cu_start;
17515 };
17516
17517 /* Write an address entry to OBSTACK. */
17518
17519 static void
17520 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17521 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17522 {
17523 offset_type cu_index_to_write;
17524 char addr[8];
17525 CORE_ADDR baseaddr;
17526
17527 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17528
17529 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17530 obstack_grow (obstack, addr, 8);
17531 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17532 obstack_grow (obstack, addr, 8);
17533 cu_index_to_write = MAYBE_SWAP (cu_index);
17534 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17535 }
17536
17537 /* Worker function for traversing an addrmap to build the address table. */
17538
17539 static int
17540 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17541 {
17542 struct addrmap_index_data *data = datap;
17543 struct partial_symtab *pst = obj;
17544
17545 if (data->previous_valid)
17546 add_address_entry (data->objfile, data->addr_obstack,
17547 data->previous_cu_start, start_addr,
17548 data->previous_cu_index);
17549
17550 data->previous_cu_start = start_addr;
17551 if (pst != NULL)
17552 {
17553 struct psymtab_cu_index_map find_map, *map;
17554 find_map.psymtab = pst;
17555 map = htab_find (data->cu_index_htab, &find_map);
17556 gdb_assert (map != NULL);
17557 data->previous_cu_index = map->cu_index;
17558 data->previous_valid = 1;
17559 }
17560 else
17561 data->previous_valid = 0;
17562
17563 return 0;
17564 }
17565
17566 /* Write OBJFILE's address map to OBSTACK.
17567 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17568 in the index file. */
17569
17570 static void
17571 write_address_map (struct objfile *objfile, struct obstack *obstack,
17572 htab_t cu_index_htab)
17573 {
17574 struct addrmap_index_data addrmap_index_data;
17575
17576 /* When writing the address table, we have to cope with the fact that
17577 the addrmap iterator only provides the start of a region; we have to
17578 wait until the next invocation to get the start of the next region. */
17579
17580 addrmap_index_data.objfile = objfile;
17581 addrmap_index_data.addr_obstack = obstack;
17582 addrmap_index_data.cu_index_htab = cu_index_htab;
17583 addrmap_index_data.previous_valid = 0;
17584
17585 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17586 &addrmap_index_data);
17587
17588 /* It's highly unlikely the last entry (end address = 0xff...ff)
17589 is valid, but we should still handle it.
17590 The end address is recorded as the start of the next region, but that
17591 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17592 anyway. */
17593 if (addrmap_index_data.previous_valid)
17594 add_address_entry (objfile, obstack,
17595 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17596 addrmap_index_data.previous_cu_index);
17597 }
17598
17599 /* Add a list of partial symbols to SYMTAB. */
17600
17601 static void
17602 write_psymbols (struct mapped_symtab *symtab,
17603 htab_t psyms_seen,
17604 struct partial_symbol **psymp,
17605 int count,
17606 offset_type cu_index,
17607 int is_static)
17608 {
17609 for (; count-- > 0; ++psymp)
17610 {
17611 void **slot, *lookup;
17612
17613 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17614 error (_("Ada is not currently supported by the index"));
17615
17616 /* We only want to add a given psymbol once. However, we also
17617 want to account for whether it is global or static. So, we
17618 may add it twice, using slightly different values. */
17619 if (is_static)
17620 {
17621 uintptr_t val = 1 | (uintptr_t) *psymp;
17622
17623 lookup = (void *) val;
17624 }
17625 else
17626 lookup = *psymp;
17627
17628 /* Only add a given psymbol once. */
17629 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17630 if (!*slot)
17631 {
17632 *slot = lookup;
17633 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
17634 }
17635 }
17636 }
17637
17638 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17639 exception if there is an error. */
17640
17641 static void
17642 write_obstack (FILE *file, struct obstack *obstack)
17643 {
17644 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17645 file)
17646 != obstack_object_size (obstack))
17647 error (_("couldn't data write to file"));
17648 }
17649
17650 /* Unlink a file if the argument is not NULL. */
17651
17652 static void
17653 unlink_if_set (void *p)
17654 {
17655 char **filename = p;
17656 if (*filename)
17657 unlink (*filename);
17658 }
17659
17660 /* A helper struct used when iterating over debug_types. */
17661 struct signatured_type_index_data
17662 {
17663 struct objfile *objfile;
17664 struct mapped_symtab *symtab;
17665 struct obstack *types_list;
17666 htab_t psyms_seen;
17667 int cu_index;
17668 };
17669
17670 /* A helper function that writes a single signatured_type to an
17671 obstack. */
17672
17673 static int
17674 write_one_signatured_type (void **slot, void *d)
17675 {
17676 struct signatured_type_index_data *info = d;
17677 struct signatured_type *entry = (struct signatured_type *) *slot;
17678 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17679 struct partial_symtab *psymtab = per_cu->v.psymtab;
17680 gdb_byte val[8];
17681
17682 write_psymbols (info->symtab,
17683 info->psyms_seen,
17684 info->objfile->global_psymbols.list
17685 + psymtab->globals_offset,
17686 psymtab->n_global_syms, info->cu_index,
17687 0);
17688 write_psymbols (info->symtab,
17689 info->psyms_seen,
17690 info->objfile->static_psymbols.list
17691 + psymtab->statics_offset,
17692 psymtab->n_static_syms, info->cu_index,
17693 1);
17694
17695 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17696 entry->per_cu.offset.sect_off);
17697 obstack_grow (info->types_list, val, 8);
17698 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17699 entry->type_offset_in_tu.cu_off);
17700 obstack_grow (info->types_list, val, 8);
17701 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17702 obstack_grow (info->types_list, val, 8);
17703
17704 ++info->cu_index;
17705
17706 return 1;
17707 }
17708
17709 /* Recurse into all "included" dependencies and write their symbols as
17710 if they appeared in this psymtab. */
17711
17712 static void
17713 recursively_write_psymbols (struct objfile *objfile,
17714 struct partial_symtab *psymtab,
17715 struct mapped_symtab *symtab,
17716 htab_t psyms_seen,
17717 offset_type cu_index)
17718 {
17719 int i;
17720
17721 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17722 if (psymtab->dependencies[i]->user != NULL)
17723 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17724 symtab, psyms_seen, cu_index);
17725
17726 write_psymbols (symtab,
17727 psyms_seen,
17728 objfile->global_psymbols.list + psymtab->globals_offset,
17729 psymtab->n_global_syms, cu_index,
17730 0);
17731 write_psymbols (symtab,
17732 psyms_seen,
17733 objfile->static_psymbols.list + psymtab->statics_offset,
17734 psymtab->n_static_syms, cu_index,
17735 1);
17736 }
17737
17738 /* Create an index file for OBJFILE in the directory DIR. */
17739
17740 static void
17741 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17742 {
17743 struct cleanup *cleanup;
17744 char *filename, *cleanup_filename;
17745 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17746 struct obstack cu_list, types_cu_list;
17747 int i;
17748 FILE *out_file;
17749 struct mapped_symtab *symtab;
17750 offset_type val, size_of_contents, total_len;
17751 struct stat st;
17752 htab_t psyms_seen;
17753 htab_t cu_index_htab;
17754 struct psymtab_cu_index_map *psymtab_cu_index_map;
17755
17756 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
17757 return;
17758
17759 if (dwarf2_per_objfile->using_index)
17760 error (_("Cannot use an index to create the index"));
17761
17762 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17763 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17764
17765 if (stat (objfile->name, &st) < 0)
17766 perror_with_name (objfile->name);
17767
17768 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17769 INDEX_SUFFIX, (char *) NULL);
17770 cleanup = make_cleanup (xfree, filename);
17771
17772 out_file = fopen (filename, "wb");
17773 if (!out_file)
17774 error (_("Can't open `%s' for writing"), filename);
17775
17776 cleanup_filename = filename;
17777 make_cleanup (unlink_if_set, &cleanup_filename);
17778
17779 symtab = create_mapped_symtab ();
17780 make_cleanup (cleanup_mapped_symtab, symtab);
17781
17782 obstack_init (&addr_obstack);
17783 make_cleanup_obstack_free (&addr_obstack);
17784
17785 obstack_init (&cu_list);
17786 make_cleanup_obstack_free (&cu_list);
17787
17788 obstack_init (&types_cu_list);
17789 make_cleanup_obstack_free (&types_cu_list);
17790
17791 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17792 NULL, xcalloc, xfree);
17793 make_cleanup_htab_delete (psyms_seen);
17794
17795 /* While we're scanning CU's create a table that maps a psymtab pointer
17796 (which is what addrmap records) to its index (which is what is recorded
17797 in the index file). This will later be needed to write the address
17798 table. */
17799 cu_index_htab = htab_create_alloc (100,
17800 hash_psymtab_cu_index,
17801 eq_psymtab_cu_index,
17802 NULL, xcalloc, xfree);
17803 make_cleanup_htab_delete (cu_index_htab);
17804 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17805 xmalloc (sizeof (struct psymtab_cu_index_map)
17806 * dwarf2_per_objfile->n_comp_units);
17807 make_cleanup (xfree, psymtab_cu_index_map);
17808
17809 /* The CU list is already sorted, so we don't need to do additional
17810 work here. Also, the debug_types entries do not appear in
17811 all_comp_units, but only in their own hash table. */
17812 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17813 {
17814 struct dwarf2_per_cu_data *per_cu
17815 = dwarf2_per_objfile->all_comp_units[i];
17816 struct partial_symtab *psymtab = per_cu->v.psymtab;
17817 gdb_byte val[8];
17818 struct psymtab_cu_index_map *map;
17819 void **slot;
17820
17821 if (psymtab->user == NULL)
17822 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
17823
17824 map = &psymtab_cu_index_map[i];
17825 map->psymtab = psymtab;
17826 map->cu_index = i;
17827 slot = htab_find_slot (cu_index_htab, map, INSERT);
17828 gdb_assert (slot != NULL);
17829 gdb_assert (*slot == NULL);
17830 *slot = map;
17831
17832 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17833 per_cu->offset.sect_off);
17834 obstack_grow (&cu_list, val, 8);
17835 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17836 obstack_grow (&cu_list, val, 8);
17837 }
17838
17839 /* Dump the address map. */
17840 write_address_map (objfile, &addr_obstack, cu_index_htab);
17841
17842 /* Write out the .debug_type entries, if any. */
17843 if (dwarf2_per_objfile->signatured_types)
17844 {
17845 struct signatured_type_index_data sig_data;
17846
17847 sig_data.objfile = objfile;
17848 sig_data.symtab = symtab;
17849 sig_data.types_list = &types_cu_list;
17850 sig_data.psyms_seen = psyms_seen;
17851 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17852 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17853 write_one_signatured_type, &sig_data);
17854 }
17855
17856 obstack_init (&constant_pool);
17857 make_cleanup_obstack_free (&constant_pool);
17858 obstack_init (&symtab_obstack);
17859 make_cleanup_obstack_free (&symtab_obstack);
17860 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17861
17862 obstack_init (&contents);
17863 make_cleanup_obstack_free (&contents);
17864 size_of_contents = 6 * sizeof (offset_type);
17865 total_len = size_of_contents;
17866
17867 /* The version number. */
17868 val = MAYBE_SWAP (6);
17869 obstack_grow (&contents, &val, sizeof (val));
17870
17871 /* The offset of the CU list from the start of the file. */
17872 val = MAYBE_SWAP (total_len);
17873 obstack_grow (&contents, &val, sizeof (val));
17874 total_len += obstack_object_size (&cu_list);
17875
17876 /* The offset of the types CU list from the start of the file. */
17877 val = MAYBE_SWAP (total_len);
17878 obstack_grow (&contents, &val, sizeof (val));
17879 total_len += obstack_object_size (&types_cu_list);
17880
17881 /* The offset of the address table from the start of the file. */
17882 val = MAYBE_SWAP (total_len);
17883 obstack_grow (&contents, &val, sizeof (val));
17884 total_len += obstack_object_size (&addr_obstack);
17885
17886 /* The offset of the symbol table from the start of the file. */
17887 val = MAYBE_SWAP (total_len);
17888 obstack_grow (&contents, &val, sizeof (val));
17889 total_len += obstack_object_size (&symtab_obstack);
17890
17891 /* The offset of the constant pool from the start of the file. */
17892 val = MAYBE_SWAP (total_len);
17893 obstack_grow (&contents, &val, sizeof (val));
17894 total_len += obstack_object_size (&constant_pool);
17895
17896 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17897
17898 write_obstack (out_file, &contents);
17899 write_obstack (out_file, &cu_list);
17900 write_obstack (out_file, &types_cu_list);
17901 write_obstack (out_file, &addr_obstack);
17902 write_obstack (out_file, &symtab_obstack);
17903 write_obstack (out_file, &constant_pool);
17904
17905 fclose (out_file);
17906
17907 /* We want to keep the file, so we set cleanup_filename to NULL
17908 here. See unlink_if_set. */
17909 cleanup_filename = NULL;
17910
17911 do_cleanups (cleanup);
17912 }
17913
17914 /* Implementation of the `save gdb-index' command.
17915
17916 Note that the file format used by this command is documented in the
17917 GDB manual. Any changes here must be documented there. */
17918
17919 static void
17920 save_gdb_index_command (char *arg, int from_tty)
17921 {
17922 struct objfile *objfile;
17923
17924 if (!arg || !*arg)
17925 error (_("usage: save gdb-index DIRECTORY"));
17926
17927 ALL_OBJFILES (objfile)
17928 {
17929 struct stat st;
17930
17931 /* If the objfile does not correspond to an actual file, skip it. */
17932 if (stat (objfile->name, &st) < 0)
17933 continue;
17934
17935 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17936 if (dwarf2_per_objfile)
17937 {
17938 volatile struct gdb_exception except;
17939
17940 TRY_CATCH (except, RETURN_MASK_ERROR)
17941 {
17942 write_psymtabs_to_index (objfile, arg);
17943 }
17944 if (except.reason < 0)
17945 exception_fprintf (gdb_stderr, except,
17946 _("Error while writing index for `%s': "),
17947 objfile->name);
17948 }
17949 }
17950 }
17951
17952 \f
17953
17954 int dwarf2_always_disassemble;
17955
17956 static void
17957 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17958 struct cmd_list_element *c, const char *value)
17959 {
17960 fprintf_filtered (file,
17961 _("Whether to always disassemble "
17962 "DWARF expressions is %s.\n"),
17963 value);
17964 }
17965
17966 static void
17967 show_check_physname (struct ui_file *file, int from_tty,
17968 struct cmd_list_element *c, const char *value)
17969 {
17970 fprintf_filtered (file,
17971 _("Whether to check \"physname\" is %s.\n"),
17972 value);
17973 }
17974
17975 void _initialize_dwarf2_read (void);
17976
17977 void
17978 _initialize_dwarf2_read (void)
17979 {
17980 struct cmd_list_element *c;
17981
17982 dwarf2_objfile_data_key
17983 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17984
17985 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17986 Set DWARF 2 specific variables.\n\
17987 Configure DWARF 2 variables such as the cache size"),
17988 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17989 0/*allow-unknown*/, &maintenance_set_cmdlist);
17990
17991 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17992 Show DWARF 2 specific variables\n\
17993 Show DWARF 2 variables such as the cache size"),
17994 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17995 0/*allow-unknown*/, &maintenance_show_cmdlist);
17996
17997 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17998 &dwarf2_max_cache_age, _("\
17999 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18000 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18001 A higher limit means that cached compilation units will be stored\n\
18002 in memory longer, and more total memory will be used. Zero disables\n\
18003 caching, which can slow down startup."),
18004 NULL,
18005 show_dwarf2_max_cache_age,
18006 &set_dwarf2_cmdlist,
18007 &show_dwarf2_cmdlist);
18008
18009 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18010 &dwarf2_always_disassemble, _("\
18011 Set whether `info address' always disassembles DWARF expressions."), _("\
18012 Show whether `info address' always disassembles DWARF expressions."), _("\
18013 When enabled, DWARF expressions are always printed in an assembly-like\n\
18014 syntax. When disabled, expressions will be printed in a more\n\
18015 conversational style, when possible."),
18016 NULL,
18017 show_dwarf2_always_disassemble,
18018 &set_dwarf2_cmdlist,
18019 &show_dwarf2_cmdlist);
18020
18021 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18022 Set debugging of the dwarf2 DIE reader."), _("\
18023 Show debugging of the dwarf2 DIE reader."), _("\
18024 When enabled (non-zero), DIEs are dumped after they are read in.\n\
18025 The value is the maximum depth to print."),
18026 NULL,
18027 NULL,
18028 &setdebuglist, &showdebuglist);
18029
18030 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18031 Set cross-checking of \"physname\" code against demangler."), _("\
18032 Show cross-checking of \"physname\" code against demangler."), _("\
18033 When enabled, GDB's internal \"physname\" code is checked against\n\
18034 the demangler."),
18035 NULL, show_check_physname,
18036 &setdebuglist, &showdebuglist);
18037
18038 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
18039 _("\
18040 Save a gdb-index file.\n\
18041 Usage: save gdb-index DIRECTORY"),
18042 &save_cmdlist);
18043 set_cmd_completer (c, filename_completer);
18044 }