Remove cleanup from build_type_psymtabs_1
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2018 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 "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "psympriv.h"
57 #include <sys/stat.h>
58 #include "completer.h"
59 #include "vec.h"
60 #include "c-lang.h"
61 #include "go-lang.h"
62 #include "valprint.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
65 #include <ctype.h>
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "filestuff.h"
70 #include "build-id.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76 #include "common/byte-vector.h"
77 #include "common/hash_enum.h"
78 #include "filename-seen-cache.h"
79 #include "producer.h"
80 #include <fcntl.h>
81 #include <sys/types.h>
82 #include <algorithm>
83 #include <unordered_set>
84 #include <unordered_map>
85 #include "selftest.h"
86 #include <cmath>
87 #include <set>
88 #include <forward_list>
89 #include "rust-lang.h"
90 #include "common/pathstuff.h"
91
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
96
97 /* When non-zero, dump DIEs after they are read in. */
98 static unsigned int dwarf_die_debug = 0;
99
100 /* When non-zero, dump line number entries as they are read in. */
101 static unsigned int dwarf_line_debug = 0;
102
103 /* When non-zero, cross-check physname against demangler. */
104 static int check_physname = 0;
105
106 /* When non-zero, do not reject deprecated .gdb_index sections. */
107 static int use_deprecated_index_sections = 0;
108
109 static const struct objfile_data *dwarf2_objfile_data_key;
110
111 /* The "aclass" indices for various kinds of computed DWARF symbols. */
112
113 static int dwarf2_locexpr_index;
114 static int dwarf2_loclist_index;
115 static int dwarf2_locexpr_block_index;
116 static int dwarf2_loclist_block_index;
117
118 /* A descriptor for dwarf sections.
119
120 S.ASECTION, SIZE are typically initialized when the objfile is first
121 scanned. BUFFER, READIN are filled in later when the section is read.
122 If the section contained compressed data then SIZE is updated to record
123 the uncompressed size of the section.
124
125 DWP file format V2 introduces a wrinkle that is easiest to handle by
126 creating the concept of virtual sections contained within a real section.
127 In DWP V2 the sections of the input DWO files are concatenated together
128 into one section, but section offsets are kept relative to the original
129 input section.
130 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
131 the real section this "virtual" section is contained in, and BUFFER,SIZE
132 describe the virtual section. */
133
134 struct dwarf2_section_info
135 {
136 union
137 {
138 /* If this is a real section, the bfd section. */
139 asection *section;
140 /* If this is a virtual section, pointer to the containing ("real")
141 section. */
142 struct dwarf2_section_info *containing_section;
143 } s;
144 /* Pointer to section data, only valid if readin. */
145 const gdb_byte *buffer;
146 /* The size of the section, real or virtual. */
147 bfd_size_type size;
148 /* If this is a virtual section, the offset in the real section.
149 Only valid if is_virtual. */
150 bfd_size_type virtual_offset;
151 /* True if we have tried to read this section. */
152 char readin;
153 /* True if this is a virtual section, False otherwise.
154 This specifies which of s.section and s.containing_section to use. */
155 char is_virtual;
156 };
157
158 typedef struct dwarf2_section_info dwarf2_section_info_def;
159 DEF_VEC_O (dwarf2_section_info_def);
160
161 /* All offsets in the index are of this type. It must be
162 architecture-independent. */
163 typedef uint32_t offset_type;
164
165 DEF_VEC_I (offset_type);
166
167 /* Ensure only legit values are used. */
168 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
169 do { \
170 gdb_assert ((unsigned int) (value) <= 1); \
171 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
172 } while (0)
173
174 /* Ensure only legit values are used. */
175 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
176 do { \
177 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
178 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
179 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
180 } while (0)
181
182 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
183 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
184 do { \
185 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
186 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
187 } while (0)
188
189 #if WORDS_BIGENDIAN
190
191 /* Convert VALUE between big- and little-endian. */
192
193 static offset_type
194 byte_swap (offset_type value)
195 {
196 offset_type result;
197
198 result = (value & 0xff) << 24;
199 result |= (value & 0xff00) << 8;
200 result |= (value & 0xff0000) >> 8;
201 result |= (value & 0xff000000) >> 24;
202 return result;
203 }
204
205 #define MAYBE_SWAP(V) byte_swap (V)
206
207 #else
208 #define MAYBE_SWAP(V) static_cast<offset_type> (V)
209 #endif /* WORDS_BIGENDIAN */
210
211 /* An index into a (C++) symbol name component in a symbol name as
212 recorded in the mapped_index's symbol table. For each C++ symbol
213 in the symbol table, we record one entry for the start of each
214 component in the symbol in a table of name components, and then
215 sort the table, in order to be able to binary search symbol names,
216 ignoring leading namespaces, both completion and regular look up.
217 For example, for symbol "A::B::C", we'll have an entry that points
218 to "A::B::C", another that points to "B::C", and another for "C".
219 Note that function symbols in GDB index have no parameter
220 information, just the function/method names. You can convert a
221 name_component to a "const char *" using the
222 'mapped_index::symbol_name_at(offset_type)' method. */
223
224 struct name_component
225 {
226 /* Offset in the symbol name where the component starts. Stored as
227 a (32-bit) offset instead of a pointer to save memory and improve
228 locality on 64-bit architectures. */
229 offset_type name_offset;
230
231 /* The symbol's index in the symbol and constant pool tables of a
232 mapped_index. */
233 offset_type idx;
234 };
235
236 /* Base class containing bits shared by both .gdb_index and
237 .debug_name indexes. */
238
239 struct mapped_index_base
240 {
241 /* The name_component table (a sorted vector). See name_component's
242 description above. */
243 std::vector<name_component> name_components;
244
245 /* How NAME_COMPONENTS is sorted. */
246 enum case_sensitivity name_components_casing;
247
248 /* Return the number of names in the symbol table. */
249 virtual size_t symbol_name_count () const = 0;
250
251 /* Get the name of the symbol at IDX in the symbol table. */
252 virtual const char *symbol_name_at (offset_type idx) const = 0;
253
254 /* Return whether the name at IDX in the symbol table should be
255 ignored. */
256 virtual bool symbol_name_slot_invalid (offset_type idx) const
257 {
258 return false;
259 }
260
261 /* Build the symbol name component sorted vector, if we haven't
262 yet. */
263 void build_name_components ();
264
265 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
266 possible matches for LN_NO_PARAMS in the name component
267 vector. */
268 std::pair<std::vector<name_component>::const_iterator,
269 std::vector<name_component>::const_iterator>
270 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
271
272 /* Prevent deleting/destroying via a base class pointer. */
273 protected:
274 ~mapped_index_base() = default;
275 };
276
277 /* A description of the mapped index. The file format is described in
278 a comment by the code that writes the index. */
279 struct mapped_index final : public mapped_index_base
280 {
281 /* A slot/bucket in the symbol table hash. */
282 struct symbol_table_slot
283 {
284 const offset_type name;
285 const offset_type vec;
286 };
287
288 /* Index data format version. */
289 int version;
290
291 /* The total length of the buffer. */
292 off_t total_size;
293
294 /* The address table data. */
295 gdb::array_view<const gdb_byte> address_table;
296
297 /* The symbol table, implemented as a hash table. */
298 gdb::array_view<symbol_table_slot> symbol_table;
299
300 /* A pointer to the constant pool. */
301 const char *constant_pool;
302
303 bool symbol_name_slot_invalid (offset_type idx) const override
304 {
305 const auto &bucket = this->symbol_table[idx];
306 return bucket.name == 0 && bucket.vec;
307 }
308
309 /* Convenience method to get at the name of the symbol at IDX in the
310 symbol table. */
311 const char *symbol_name_at (offset_type idx) const override
312 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
313
314 size_t symbol_name_count () const override
315 { return this->symbol_table.size (); }
316 };
317
318 /* A description of the mapped .debug_names.
319 Uninitialized map has CU_COUNT 0. */
320 struct mapped_debug_names final : public mapped_index_base
321 {
322 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
323 : dwarf2_per_objfile (dwarf2_per_objfile_)
324 {}
325
326 struct dwarf2_per_objfile *dwarf2_per_objfile;
327 bfd_endian dwarf5_byte_order;
328 bool dwarf5_is_dwarf64;
329 bool augmentation_is_gdb;
330 uint8_t offset_size;
331 uint32_t cu_count = 0;
332 uint32_t tu_count, bucket_count, name_count;
333 const gdb_byte *cu_table_reordered, *tu_table_reordered;
334 const uint32_t *bucket_table_reordered, *hash_table_reordered;
335 const gdb_byte *name_table_string_offs_reordered;
336 const gdb_byte *name_table_entry_offs_reordered;
337 const gdb_byte *entry_pool;
338
339 struct index_val
340 {
341 ULONGEST dwarf_tag;
342 struct attr
343 {
344 /* Attribute name DW_IDX_*. */
345 ULONGEST dw_idx;
346
347 /* Attribute form DW_FORM_*. */
348 ULONGEST form;
349
350 /* Value if FORM is DW_FORM_implicit_const. */
351 LONGEST implicit_const;
352 };
353 std::vector<attr> attr_vec;
354 };
355
356 std::unordered_map<ULONGEST, index_val> abbrev_map;
357
358 const char *namei_to_name (uint32_t namei) const;
359
360 /* Implementation of the mapped_index_base virtual interface, for
361 the name_components cache. */
362
363 const char *symbol_name_at (offset_type idx) const override
364 { return namei_to_name (idx); }
365
366 size_t symbol_name_count () const override
367 { return this->name_count; }
368 };
369
370 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
371 DEF_VEC_P (dwarf2_per_cu_ptr);
372
373 struct tu_stats
374 {
375 int nr_uniq_abbrev_tables;
376 int nr_symtabs;
377 int nr_symtab_sharers;
378 int nr_stmt_less_type_units;
379 int nr_all_type_units_reallocs;
380 };
381
382 /* Collection of data recorded per objfile.
383 This hangs off of dwarf2_objfile_data_key. */
384
385 struct dwarf2_per_objfile : public allocate_on_obstack
386 {
387 /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the
388 dwarf2 section names, or is NULL if the standard ELF names are
389 used. */
390 dwarf2_per_objfile (struct objfile *objfile,
391 const dwarf2_debug_sections *names);
392
393 ~dwarf2_per_objfile ();
394
395 DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile);
396
397 /* Free all cached compilation units. */
398 void free_cached_comp_units ();
399 private:
400 /* This function is mapped across the sections and remembers the
401 offset and size of each of the debugging sections we are
402 interested in. */
403 void locate_sections (bfd *abfd, asection *sectp,
404 const dwarf2_debug_sections &names);
405
406 public:
407 dwarf2_section_info info {};
408 dwarf2_section_info abbrev {};
409 dwarf2_section_info line {};
410 dwarf2_section_info loc {};
411 dwarf2_section_info loclists {};
412 dwarf2_section_info macinfo {};
413 dwarf2_section_info macro {};
414 dwarf2_section_info str {};
415 dwarf2_section_info line_str {};
416 dwarf2_section_info ranges {};
417 dwarf2_section_info rnglists {};
418 dwarf2_section_info addr {};
419 dwarf2_section_info frame {};
420 dwarf2_section_info eh_frame {};
421 dwarf2_section_info gdb_index {};
422 dwarf2_section_info debug_names {};
423 dwarf2_section_info debug_aranges {};
424
425 VEC (dwarf2_section_info_def) *types = NULL;
426
427 /* Back link. */
428 struct objfile *objfile = NULL;
429
430 /* Table of all the compilation units. This is used to locate
431 the target compilation unit of a particular reference. */
432 struct dwarf2_per_cu_data **all_comp_units = NULL;
433
434 /* The number of compilation units in ALL_COMP_UNITS. */
435 int n_comp_units = 0;
436
437 /* The number of .debug_types-related CUs. */
438 int n_type_units = 0;
439
440 /* The number of elements allocated in all_type_units.
441 If there are skeleton-less TUs, we add them to all_type_units lazily. */
442 int n_allocated_type_units = 0;
443
444 /* The .debug_types-related CUs (TUs).
445 This is stored in malloc space because we may realloc it. */
446 struct signatured_type **all_type_units = NULL;
447
448 /* Table of struct type_unit_group objects.
449 The hash key is the DW_AT_stmt_list value. */
450 htab_t type_unit_groups {};
451
452 /* A table mapping .debug_types signatures to its signatured_type entry.
453 This is NULL if the .debug_types section hasn't been read in yet. */
454 htab_t signatured_types {};
455
456 /* Type unit statistics, to see how well the scaling improvements
457 are doing. */
458 struct tu_stats tu_stats {};
459
460 /* A chain of compilation units that are currently read in, so that
461 they can be freed later. */
462 dwarf2_per_cu_data *read_in_chain = NULL;
463
464 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
465 This is NULL if the table hasn't been allocated yet. */
466 htab_t dwo_files {};
467
468 /* True if we've checked for whether there is a DWP file. */
469 bool dwp_checked = false;
470
471 /* The DWP file if there is one, or NULL. */
472 struct dwp_file *dwp_file = NULL;
473
474 /* The shared '.dwz' file, if one exists. This is used when the
475 original data was compressed using 'dwz -m'. */
476 struct dwz_file *dwz_file = NULL;
477
478 /* A flag indicating whether this objfile has a section loaded at a
479 VMA of 0. */
480 bool has_section_at_zero = false;
481
482 /* True if we are using the mapped index,
483 or we are faking it for OBJF_READNOW's sake. */
484 bool using_index = false;
485
486 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
487 mapped_index *index_table = NULL;
488
489 /* The mapped index, or NULL if .debug_names is missing or not being used. */
490 std::unique_ptr<mapped_debug_names> debug_names_table;
491
492 /* When using index_table, this keeps track of all quick_file_names entries.
493 TUs typically share line table entries with a CU, so we maintain a
494 separate table of all line table entries to support the sharing.
495 Note that while there can be way more TUs than CUs, we've already
496 sorted all the TUs into "type unit groups", grouped by their
497 DW_AT_stmt_list value. Therefore the only sharing done here is with a
498 CU and its associated TU group if there is one. */
499 htab_t quick_file_names_table {};
500
501 /* Set during partial symbol reading, to prevent queueing of full
502 symbols. */
503 bool reading_partial_symbols = false;
504
505 /* Table mapping type DIEs to their struct type *.
506 This is NULL if not allocated yet.
507 The mapping is done via (CU/TU + DIE offset) -> type. */
508 htab_t die_type_hash {};
509
510 /* The CUs we recently read. */
511 VEC (dwarf2_per_cu_ptr) *just_read_cus = NULL;
512
513 /* Table containing line_header indexed by offset and offset_in_dwz. */
514 htab_t line_header_hash {};
515
516 /* Table containing all filenames. This is an optional because the
517 table is lazily constructed on first access. */
518 gdb::optional<filename_seen_cache> filenames_cache;
519 };
520
521 /* Get the dwarf2_per_objfile associated to OBJFILE. */
522
523 struct dwarf2_per_objfile *
524 get_dwarf2_per_objfile (struct objfile *objfile)
525 {
526 return ((struct dwarf2_per_objfile *)
527 objfile_data (objfile, dwarf2_objfile_data_key));
528 }
529
530 /* Set the dwarf2_per_objfile associated to OBJFILE. */
531
532 void
533 set_dwarf2_per_objfile (struct objfile *objfile,
534 struct dwarf2_per_objfile *dwarf2_per_objfile)
535 {
536 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
537 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
538 }
539
540 /* Default names of the debugging sections. */
541
542 /* Note that if the debugging section has been compressed, it might
543 have a name like .zdebug_info. */
544
545 static const struct dwarf2_debug_sections dwarf2_elf_names =
546 {
547 { ".debug_info", ".zdebug_info" },
548 { ".debug_abbrev", ".zdebug_abbrev" },
549 { ".debug_line", ".zdebug_line" },
550 { ".debug_loc", ".zdebug_loc" },
551 { ".debug_loclists", ".zdebug_loclists" },
552 { ".debug_macinfo", ".zdebug_macinfo" },
553 { ".debug_macro", ".zdebug_macro" },
554 { ".debug_str", ".zdebug_str" },
555 { ".debug_line_str", ".zdebug_line_str" },
556 { ".debug_ranges", ".zdebug_ranges" },
557 { ".debug_rnglists", ".zdebug_rnglists" },
558 { ".debug_types", ".zdebug_types" },
559 { ".debug_addr", ".zdebug_addr" },
560 { ".debug_frame", ".zdebug_frame" },
561 { ".eh_frame", NULL },
562 { ".gdb_index", ".zgdb_index" },
563 { ".debug_names", ".zdebug_names" },
564 { ".debug_aranges", ".zdebug_aranges" },
565 23
566 };
567
568 /* List of DWO/DWP sections. */
569
570 static const struct dwop_section_names
571 {
572 struct dwarf2_section_names abbrev_dwo;
573 struct dwarf2_section_names info_dwo;
574 struct dwarf2_section_names line_dwo;
575 struct dwarf2_section_names loc_dwo;
576 struct dwarf2_section_names loclists_dwo;
577 struct dwarf2_section_names macinfo_dwo;
578 struct dwarf2_section_names macro_dwo;
579 struct dwarf2_section_names str_dwo;
580 struct dwarf2_section_names str_offsets_dwo;
581 struct dwarf2_section_names types_dwo;
582 struct dwarf2_section_names cu_index;
583 struct dwarf2_section_names tu_index;
584 }
585 dwop_section_names =
586 {
587 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
588 { ".debug_info.dwo", ".zdebug_info.dwo" },
589 { ".debug_line.dwo", ".zdebug_line.dwo" },
590 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
591 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
592 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
593 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
594 { ".debug_str.dwo", ".zdebug_str.dwo" },
595 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
596 { ".debug_types.dwo", ".zdebug_types.dwo" },
597 { ".debug_cu_index", ".zdebug_cu_index" },
598 { ".debug_tu_index", ".zdebug_tu_index" },
599 };
600
601 /* local data types */
602
603 /* The data in a compilation unit header, after target2host
604 translation, looks like this. */
605 struct comp_unit_head
606 {
607 unsigned int length;
608 short version;
609 unsigned char addr_size;
610 unsigned char signed_addr_p;
611 sect_offset abbrev_sect_off;
612
613 /* Size of file offsets; either 4 or 8. */
614 unsigned int offset_size;
615
616 /* Size of the length field; either 4 or 12. */
617 unsigned int initial_length_size;
618
619 enum dwarf_unit_type unit_type;
620
621 /* Offset to the first byte of this compilation unit header in the
622 .debug_info section, for resolving relative reference dies. */
623 sect_offset sect_off;
624
625 /* Offset to first die in this cu from the start of the cu.
626 This will be the first byte following the compilation unit header. */
627 cu_offset first_die_cu_offset;
628
629 /* 64-bit signature of this type unit - it is valid only for
630 UNIT_TYPE DW_UT_type. */
631 ULONGEST signature;
632
633 /* For types, offset in the type's DIE of the type defined by this TU. */
634 cu_offset type_cu_offset_in_tu;
635 };
636
637 /* Type used for delaying computation of method physnames.
638 See comments for compute_delayed_physnames. */
639 struct delayed_method_info
640 {
641 /* The type to which the method is attached, i.e., its parent class. */
642 struct type *type;
643
644 /* The index of the method in the type's function fieldlists. */
645 int fnfield_index;
646
647 /* The index of the method in the fieldlist. */
648 int index;
649
650 /* The name of the DIE. */
651 const char *name;
652
653 /* The DIE associated with this method. */
654 struct die_info *die;
655 };
656
657 /* Internal state when decoding a particular compilation unit. */
658 struct dwarf2_cu
659 {
660 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
661 ~dwarf2_cu ();
662
663 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
664
665 /* The header of the compilation unit. */
666 struct comp_unit_head header {};
667
668 /* Base address of this compilation unit. */
669 CORE_ADDR base_address = 0;
670
671 /* Non-zero if base_address has been set. */
672 int base_known = 0;
673
674 /* The language we are debugging. */
675 enum language language = language_unknown;
676 const struct language_defn *language_defn = nullptr;
677
678 const char *producer = nullptr;
679
680 /* The generic symbol table building routines have separate lists for
681 file scope symbols and all all other scopes (local scopes). So
682 we need to select the right one to pass to add_symbol_to_list().
683 We do it by keeping a pointer to the correct list in list_in_scope.
684
685 FIXME: The original dwarf code just treated the file scope as the
686 first local scope, and all other local scopes as nested local
687 scopes, and worked fine. Check to see if we really need to
688 distinguish these in buildsym.c. */
689 struct pending **list_in_scope = nullptr;
690
691 /* Hash table holding all the loaded partial DIEs
692 with partial_die->offset.SECT_OFF as hash. */
693 htab_t partial_dies = nullptr;
694
695 /* Storage for things with the same lifetime as this read-in compilation
696 unit, including partial DIEs. */
697 auto_obstack comp_unit_obstack;
698
699 /* When multiple dwarf2_cu structures are living in memory, this field
700 chains them all together, so that they can be released efficiently.
701 We will probably also want a generation counter so that most-recently-used
702 compilation units are cached... */
703 struct dwarf2_per_cu_data *read_in_chain = nullptr;
704
705 /* Backlink to our per_cu entry. */
706 struct dwarf2_per_cu_data *per_cu;
707
708 /* How many compilation units ago was this CU last referenced? */
709 int last_used = 0;
710
711 /* A hash table of DIE cu_offset for following references with
712 die_info->offset.sect_off as hash. */
713 htab_t die_hash = nullptr;
714
715 /* Full DIEs if read in. */
716 struct die_info *dies = nullptr;
717
718 /* A set of pointers to dwarf2_per_cu_data objects for compilation
719 units referenced by this one. Only set during full symbol processing;
720 partial symbol tables do not have dependencies. */
721 htab_t dependencies = nullptr;
722
723 /* Header data from the line table, during full symbol processing. */
724 struct line_header *line_header = nullptr;
725 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
726 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
727 this is the DW_TAG_compile_unit die for this CU. We'll hold on
728 to the line header as long as this DIE is being processed. See
729 process_die_scope. */
730 die_info *line_header_die_owner = nullptr;
731
732 /* A list of methods which need to have physnames computed
733 after all type information has been read. */
734 std::vector<delayed_method_info> method_list;
735
736 /* To be copied to symtab->call_site_htab. */
737 htab_t call_site_htab = nullptr;
738
739 /* Non-NULL if this CU came from a DWO file.
740 There is an invariant here that is important to remember:
741 Except for attributes copied from the top level DIE in the "main"
742 (or "stub") file in preparation for reading the DWO file
743 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
744 Either there isn't a DWO file (in which case this is NULL and the point
745 is moot), or there is and either we're not going to read it (in which
746 case this is NULL) or there is and we are reading it (in which case this
747 is non-NULL). */
748 struct dwo_unit *dwo_unit = nullptr;
749
750 /* The DW_AT_addr_base attribute if present, zero otherwise
751 (zero is a valid value though).
752 Note this value comes from the Fission stub CU/TU's DIE. */
753 ULONGEST addr_base = 0;
754
755 /* The DW_AT_ranges_base attribute if present, zero otherwise
756 (zero is a valid value though).
757 Note this value comes from the Fission stub CU/TU's DIE.
758 Also note that the value is zero in the non-DWO case so this value can
759 be used without needing to know whether DWO files are in use or not.
760 N.B. This does not apply to DW_AT_ranges appearing in
761 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
762 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
763 DW_AT_ranges_base *would* have to be applied, and we'd have to care
764 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
765 ULONGEST ranges_base = 0;
766
767 /* When reading debug info generated by older versions of rustc, we
768 have to rewrite some union types to be struct types with a
769 variant part. This rewriting must be done after the CU is fully
770 read in, because otherwise at the point of rewriting some struct
771 type might not have been fully processed. So, we keep a list of
772 all such types here and process them after expansion. */
773 std::vector<struct type *> rust_unions;
774
775 /* Mark used when releasing cached dies. */
776 unsigned int mark : 1;
777
778 /* This CU references .debug_loc. See the symtab->locations_valid field.
779 This test is imperfect as there may exist optimized debug code not using
780 any location list and still facing inlining issues if handled as
781 unoptimized code. For a future better test see GCC PR other/32998. */
782 unsigned int has_loclist : 1;
783
784 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
785 if all the producer_is_* fields are valid. This information is cached
786 because profiling CU expansion showed excessive time spent in
787 producer_is_gxx_lt_4_6. */
788 unsigned int checked_producer : 1;
789 unsigned int producer_is_gxx_lt_4_6 : 1;
790 unsigned int producer_is_gcc_lt_4_3 : 1;
791 unsigned int producer_is_icc_lt_14 : 1;
792
793 /* When set, the file that we're processing is known to have
794 debugging info for C++ namespaces. GCC 3.3.x did not produce
795 this information, but later versions do. */
796
797 unsigned int processing_has_namespace_info : 1;
798
799 struct partial_die_info *find_partial_die (sect_offset sect_off);
800 };
801
802 /* Persistent data held for a compilation unit, even when not
803 processing it. We put a pointer to this structure in the
804 read_symtab_private field of the psymtab. */
805
806 struct dwarf2_per_cu_data
807 {
808 /* The start offset and length of this compilation unit.
809 NOTE: Unlike comp_unit_head.length, this length includes
810 initial_length_size.
811 If the DIE refers to a DWO file, this is always of the original die,
812 not the DWO file. */
813 sect_offset sect_off;
814 unsigned int length;
815
816 /* DWARF standard version this data has been read from (such as 4 or 5). */
817 short dwarf_version;
818
819 /* Flag indicating this compilation unit will be read in before
820 any of the current compilation units are processed. */
821 unsigned int queued : 1;
822
823 /* This flag will be set when reading partial DIEs if we need to load
824 absolutely all DIEs for this compilation unit, instead of just the ones
825 we think are interesting. It gets set if we look for a DIE in the
826 hash table and don't find it. */
827 unsigned int load_all_dies : 1;
828
829 /* Non-zero if this CU is from .debug_types.
830 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
831 this is non-zero. */
832 unsigned int is_debug_types : 1;
833
834 /* Non-zero if this CU is from the .dwz file. */
835 unsigned int is_dwz : 1;
836
837 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
838 This flag is only valid if is_debug_types is true.
839 We can't read a CU directly from a DWO file: There are required
840 attributes in the stub. */
841 unsigned int reading_dwo_directly : 1;
842
843 /* Non-zero if the TU has been read.
844 This is used to assist the "Stay in DWO Optimization" for Fission:
845 When reading a DWO, it's faster to read TUs from the DWO instead of
846 fetching them from random other DWOs (due to comdat folding).
847 If the TU has already been read, the optimization is unnecessary
848 (and unwise - we don't want to change where gdb thinks the TU lives
849 "midflight").
850 This flag is only valid if is_debug_types is true. */
851 unsigned int tu_read : 1;
852
853 /* The section this CU/TU lives in.
854 If the DIE refers to a DWO file, this is always the original die,
855 not the DWO file. */
856 struct dwarf2_section_info *section;
857
858 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
859 of the CU cache it gets reset to NULL again. This is left as NULL for
860 dummy CUs (a CU header, but nothing else). */
861 struct dwarf2_cu *cu;
862
863 /* The corresponding dwarf2_per_objfile. */
864 struct dwarf2_per_objfile *dwarf2_per_objfile;
865
866 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
867 is active. Otherwise, the 'psymtab' field is active. */
868 union
869 {
870 /* The partial symbol table associated with this compilation unit,
871 or NULL for unread partial units. */
872 struct partial_symtab *psymtab;
873
874 /* Data needed by the "quick" functions. */
875 struct dwarf2_per_cu_quick_data *quick;
876 } v;
877
878 /* The CUs we import using DW_TAG_imported_unit. This is filled in
879 while reading psymtabs, used to compute the psymtab dependencies,
880 and then cleared. Then it is filled in again while reading full
881 symbols, and only deleted when the objfile is destroyed.
882
883 This is also used to work around a difference between the way gold
884 generates .gdb_index version <=7 and the way gdb does. Arguably this
885 is a gold bug. For symbols coming from TUs, gold records in the index
886 the CU that includes the TU instead of the TU itself. This breaks
887 dw2_lookup_symbol: It assumes that if the index says symbol X lives
888 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
889 will find X. Alas TUs live in their own symtab, so after expanding CU Y
890 we need to look in TU Z to find X. Fortunately, this is akin to
891 DW_TAG_imported_unit, so we just use the same mechanism: For
892 .gdb_index version <=7 this also records the TUs that the CU referred
893 to. Concurrently with this change gdb was modified to emit version 8
894 indices so we only pay a price for gold generated indices.
895 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
896 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
897 };
898
899 /* Entry in the signatured_types hash table. */
900
901 struct signatured_type
902 {
903 /* The "per_cu" object of this type.
904 This struct is used iff per_cu.is_debug_types.
905 N.B.: This is the first member so that it's easy to convert pointers
906 between them. */
907 struct dwarf2_per_cu_data per_cu;
908
909 /* The type's signature. */
910 ULONGEST signature;
911
912 /* Offset in the TU of the type's DIE, as read from the TU header.
913 If this TU is a DWO stub and the definition lives in a DWO file
914 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
915 cu_offset type_offset_in_tu;
916
917 /* Offset in the section of the type's DIE.
918 If the definition lives in a DWO file, this is the offset in the
919 .debug_types.dwo section.
920 The value is zero until the actual value is known.
921 Zero is otherwise not a valid section offset. */
922 sect_offset type_offset_in_section;
923
924 /* Type units are grouped by their DW_AT_stmt_list entry so that they
925 can share them. This points to the containing symtab. */
926 struct type_unit_group *type_unit_group;
927
928 /* The type.
929 The first time we encounter this type we fully read it in and install it
930 in the symbol tables. Subsequent times we only need the type. */
931 struct type *type;
932
933 /* Containing DWO unit.
934 This field is valid iff per_cu.reading_dwo_directly. */
935 struct dwo_unit *dwo_unit;
936 };
937
938 typedef struct signatured_type *sig_type_ptr;
939 DEF_VEC_P (sig_type_ptr);
940
941 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
942 This includes type_unit_group and quick_file_names. */
943
944 struct stmt_list_hash
945 {
946 /* The DWO unit this table is from or NULL if there is none. */
947 struct dwo_unit *dwo_unit;
948
949 /* Offset in .debug_line or .debug_line.dwo. */
950 sect_offset line_sect_off;
951 };
952
953 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
954 an object of this type. */
955
956 struct type_unit_group
957 {
958 /* dwarf2read.c's main "handle" on a TU symtab.
959 To simplify things we create an artificial CU that "includes" all the
960 type units using this stmt_list so that the rest of the code still has
961 a "per_cu" handle on the symtab.
962 This PER_CU is recognized by having no section. */
963 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
964 struct dwarf2_per_cu_data per_cu;
965
966 /* The TUs that share this DW_AT_stmt_list entry.
967 This is added to while parsing type units to build partial symtabs,
968 and is deleted afterwards and not used again. */
969 VEC (sig_type_ptr) *tus;
970
971 /* The compunit symtab.
972 Type units in a group needn't all be defined in the same source file,
973 so we create an essentially anonymous symtab as the compunit symtab. */
974 struct compunit_symtab *compunit_symtab;
975
976 /* The data used to construct the hash key. */
977 struct stmt_list_hash hash;
978
979 /* The number of symtabs from the line header.
980 The value here must match line_header.num_file_names. */
981 unsigned int num_symtabs;
982
983 /* The symbol tables for this TU (obtained from the files listed in
984 DW_AT_stmt_list).
985 WARNING: The order of entries here must match the order of entries
986 in the line header. After the first TU using this type_unit_group, the
987 line header for the subsequent TUs is recreated from this. This is done
988 because we need to use the same symtabs for each TU using the same
989 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
990 there's no guarantee the line header doesn't have duplicate entries. */
991 struct symtab **symtabs;
992 };
993
994 /* These sections are what may appear in a (real or virtual) DWO file. */
995
996 struct dwo_sections
997 {
998 struct dwarf2_section_info abbrev;
999 struct dwarf2_section_info line;
1000 struct dwarf2_section_info loc;
1001 struct dwarf2_section_info loclists;
1002 struct dwarf2_section_info macinfo;
1003 struct dwarf2_section_info macro;
1004 struct dwarf2_section_info str;
1005 struct dwarf2_section_info str_offsets;
1006 /* In the case of a virtual DWO file, these two are unused. */
1007 struct dwarf2_section_info info;
1008 VEC (dwarf2_section_info_def) *types;
1009 };
1010
1011 /* CUs/TUs in DWP/DWO files. */
1012
1013 struct dwo_unit
1014 {
1015 /* Backlink to the containing struct dwo_file. */
1016 struct dwo_file *dwo_file;
1017
1018 /* The "id" that distinguishes this CU/TU.
1019 .debug_info calls this "dwo_id", .debug_types calls this "signature".
1020 Since signatures came first, we stick with it for consistency. */
1021 ULONGEST signature;
1022
1023 /* The section this CU/TU lives in, in the DWO file. */
1024 struct dwarf2_section_info *section;
1025
1026 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
1027 sect_offset sect_off;
1028 unsigned int length;
1029
1030 /* For types, offset in the type's DIE of the type defined by this TU. */
1031 cu_offset type_offset_in_tu;
1032 };
1033
1034 /* include/dwarf2.h defines the DWP section codes.
1035 It defines a max value but it doesn't define a min value, which we
1036 use for error checking, so provide one. */
1037
1038 enum dwp_v2_section_ids
1039 {
1040 DW_SECT_MIN = 1
1041 };
1042
1043 /* Data for one DWO file.
1044
1045 This includes virtual DWO files (a virtual DWO file is a DWO file as it
1046 appears in a DWP file). DWP files don't really have DWO files per se -
1047 comdat folding of types "loses" the DWO file they came from, and from
1048 a high level view DWP files appear to contain a mass of random types.
1049 However, to maintain consistency with the non-DWP case we pretend DWP
1050 files contain virtual DWO files, and we assign each TU with one virtual
1051 DWO file (generally based on the line and abbrev section offsets -
1052 a heuristic that seems to work in practice). */
1053
1054 struct dwo_file
1055 {
1056 /* The DW_AT_GNU_dwo_name attribute.
1057 For virtual DWO files the name is constructed from the section offsets
1058 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
1059 from related CU+TUs. */
1060 const char *dwo_name;
1061
1062 /* The DW_AT_comp_dir attribute. */
1063 const char *comp_dir;
1064
1065 /* The bfd, when the file is open. Otherwise this is NULL.
1066 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
1067 bfd *dbfd;
1068
1069 /* The sections that make up this DWO file.
1070 Remember that for virtual DWO files in DWP V2, these are virtual
1071 sections (for lack of a better name). */
1072 struct dwo_sections sections;
1073
1074 /* The CUs in the file.
1075 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
1076 an extension to handle LLVM's Link Time Optimization output (where
1077 multiple source files may be compiled into a single object/dwo pair). */
1078 htab_t cus;
1079
1080 /* Table of TUs in the file.
1081 Each element is a struct dwo_unit. */
1082 htab_t tus;
1083 };
1084
1085 /* These sections are what may appear in a DWP file. */
1086
1087 struct dwp_sections
1088 {
1089 /* These are used by both DWP version 1 and 2. */
1090 struct dwarf2_section_info str;
1091 struct dwarf2_section_info cu_index;
1092 struct dwarf2_section_info tu_index;
1093
1094 /* These are only used by DWP version 2 files.
1095 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
1096 sections are referenced by section number, and are not recorded here.
1097 In DWP version 2 there is at most one copy of all these sections, each
1098 section being (effectively) comprised of the concatenation of all of the
1099 individual sections that exist in the version 1 format.
1100 To keep the code simple we treat each of these concatenated pieces as a
1101 section itself (a virtual section?). */
1102 struct dwarf2_section_info abbrev;
1103 struct dwarf2_section_info info;
1104 struct dwarf2_section_info line;
1105 struct dwarf2_section_info loc;
1106 struct dwarf2_section_info macinfo;
1107 struct dwarf2_section_info macro;
1108 struct dwarf2_section_info str_offsets;
1109 struct dwarf2_section_info types;
1110 };
1111
1112 /* These sections are what may appear in a virtual DWO file in DWP version 1.
1113 A virtual DWO file is a DWO file as it appears in a DWP file. */
1114
1115 struct virtual_v1_dwo_sections
1116 {
1117 struct dwarf2_section_info abbrev;
1118 struct dwarf2_section_info line;
1119 struct dwarf2_section_info loc;
1120 struct dwarf2_section_info macinfo;
1121 struct dwarf2_section_info macro;
1122 struct dwarf2_section_info str_offsets;
1123 /* Each DWP hash table entry records one CU or one TU.
1124 That is recorded here, and copied to dwo_unit.section. */
1125 struct dwarf2_section_info info_or_types;
1126 };
1127
1128 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
1129 In version 2, the sections of the DWO files are concatenated together
1130 and stored in one section of that name. Thus each ELF section contains
1131 several "virtual" sections. */
1132
1133 struct virtual_v2_dwo_sections
1134 {
1135 bfd_size_type abbrev_offset;
1136 bfd_size_type abbrev_size;
1137
1138 bfd_size_type line_offset;
1139 bfd_size_type line_size;
1140
1141 bfd_size_type loc_offset;
1142 bfd_size_type loc_size;
1143
1144 bfd_size_type macinfo_offset;
1145 bfd_size_type macinfo_size;
1146
1147 bfd_size_type macro_offset;
1148 bfd_size_type macro_size;
1149
1150 bfd_size_type str_offsets_offset;
1151 bfd_size_type str_offsets_size;
1152
1153 /* Each DWP hash table entry records one CU or one TU.
1154 That is recorded here, and copied to dwo_unit.section. */
1155 bfd_size_type info_or_types_offset;
1156 bfd_size_type info_or_types_size;
1157 };
1158
1159 /* Contents of DWP hash tables. */
1160
1161 struct dwp_hash_table
1162 {
1163 uint32_t version, nr_columns;
1164 uint32_t nr_units, nr_slots;
1165 const gdb_byte *hash_table, *unit_table;
1166 union
1167 {
1168 struct
1169 {
1170 const gdb_byte *indices;
1171 } v1;
1172 struct
1173 {
1174 /* This is indexed by column number and gives the id of the section
1175 in that column. */
1176 #define MAX_NR_V2_DWO_SECTIONS \
1177 (1 /* .debug_info or .debug_types */ \
1178 + 1 /* .debug_abbrev */ \
1179 + 1 /* .debug_line */ \
1180 + 1 /* .debug_loc */ \
1181 + 1 /* .debug_str_offsets */ \
1182 + 1 /* .debug_macro or .debug_macinfo */)
1183 int section_ids[MAX_NR_V2_DWO_SECTIONS];
1184 const gdb_byte *offsets;
1185 const gdb_byte *sizes;
1186 } v2;
1187 } section_pool;
1188 };
1189
1190 /* Data for one DWP file. */
1191
1192 struct dwp_file
1193 {
1194 /* Name of the file. */
1195 const char *name;
1196
1197 /* File format version. */
1198 int version;
1199
1200 /* The bfd. */
1201 bfd *dbfd;
1202
1203 /* Section info for this file. */
1204 struct dwp_sections sections;
1205
1206 /* Table of CUs in the file. */
1207 const struct dwp_hash_table *cus;
1208
1209 /* Table of TUs in the file. */
1210 const struct dwp_hash_table *tus;
1211
1212 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
1213 htab_t loaded_cus;
1214 htab_t loaded_tus;
1215
1216 /* Table to map ELF section numbers to their sections.
1217 This is only needed for the DWP V1 file format. */
1218 unsigned int num_sections;
1219 asection **elf_sections;
1220 };
1221
1222 /* This represents a '.dwz' file. */
1223
1224 struct dwz_file
1225 {
1226 /* A dwz file can only contain a few sections. */
1227 struct dwarf2_section_info abbrev;
1228 struct dwarf2_section_info info;
1229 struct dwarf2_section_info str;
1230 struct dwarf2_section_info line;
1231 struct dwarf2_section_info macro;
1232 struct dwarf2_section_info gdb_index;
1233 struct dwarf2_section_info debug_names;
1234
1235 /* The dwz's BFD. */
1236 bfd *dwz_bfd;
1237 };
1238
1239 /* Struct used to pass misc. parameters to read_die_and_children, et
1240 al. which are used for both .debug_info and .debug_types dies.
1241 All parameters here are unchanging for the life of the call. This
1242 struct exists to abstract away the constant parameters of die reading. */
1243
1244 struct die_reader_specs
1245 {
1246 /* The bfd of die_section. */
1247 bfd* abfd;
1248
1249 /* The CU of the DIE we are parsing. */
1250 struct dwarf2_cu *cu;
1251
1252 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1253 struct dwo_file *dwo_file;
1254
1255 /* The section the die comes from.
1256 This is either .debug_info or .debug_types, or the .dwo variants. */
1257 struct dwarf2_section_info *die_section;
1258
1259 /* die_section->buffer. */
1260 const gdb_byte *buffer;
1261
1262 /* The end of the buffer. */
1263 const gdb_byte *buffer_end;
1264
1265 /* The value of the DW_AT_comp_dir attribute. */
1266 const char *comp_dir;
1267
1268 /* The abbreviation table to use when reading the DIEs. */
1269 struct abbrev_table *abbrev_table;
1270 };
1271
1272 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1273 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1274 const gdb_byte *info_ptr,
1275 struct die_info *comp_unit_die,
1276 int has_children,
1277 void *data);
1278
1279 /* A 1-based directory index. This is a strong typedef to prevent
1280 accidentally using a directory index as a 0-based index into an
1281 array/vector. */
1282 enum class dir_index : unsigned int {};
1283
1284 /* Likewise, a 1-based file name index. */
1285 enum class file_name_index : unsigned int {};
1286
1287 struct file_entry
1288 {
1289 file_entry () = default;
1290
1291 file_entry (const char *name_, dir_index d_index_,
1292 unsigned int mod_time_, unsigned int length_)
1293 : name (name_),
1294 d_index (d_index_),
1295 mod_time (mod_time_),
1296 length (length_)
1297 {}
1298
1299 /* Return the include directory at D_INDEX stored in LH. Returns
1300 NULL if D_INDEX is out of bounds. */
1301 const char *include_dir (const line_header *lh) const;
1302
1303 /* The file name. Note this is an observing pointer. The memory is
1304 owned by debug_line_buffer. */
1305 const char *name {};
1306
1307 /* The directory index (1-based). */
1308 dir_index d_index {};
1309
1310 unsigned int mod_time {};
1311
1312 unsigned int length {};
1313
1314 /* True if referenced by the Line Number Program. */
1315 bool included_p {};
1316
1317 /* The associated symbol table, if any. */
1318 struct symtab *symtab {};
1319 };
1320
1321 /* The line number information for a compilation unit (found in the
1322 .debug_line section) begins with a "statement program header",
1323 which contains the following information. */
1324 struct line_header
1325 {
1326 line_header ()
1327 : offset_in_dwz {}
1328 {}
1329
1330 /* Add an entry to the include directory table. */
1331 void add_include_dir (const char *include_dir);
1332
1333 /* Add an entry to the file name table. */
1334 void add_file_name (const char *name, dir_index d_index,
1335 unsigned int mod_time, unsigned int length);
1336
1337 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1338 is out of bounds. */
1339 const char *include_dir_at (dir_index index) const
1340 {
1341 /* Convert directory index number (1-based) to vector index
1342 (0-based). */
1343 size_t vec_index = to_underlying (index) - 1;
1344
1345 if (vec_index >= include_dirs.size ())
1346 return NULL;
1347 return include_dirs[vec_index];
1348 }
1349
1350 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1351 is out of bounds. */
1352 file_entry *file_name_at (file_name_index index)
1353 {
1354 /* Convert file name index number (1-based) to vector index
1355 (0-based). */
1356 size_t vec_index = to_underlying (index) - 1;
1357
1358 if (vec_index >= file_names.size ())
1359 return NULL;
1360 return &file_names[vec_index];
1361 }
1362
1363 /* Const version of the above. */
1364 const file_entry *file_name_at (unsigned int index) const
1365 {
1366 if (index >= file_names.size ())
1367 return NULL;
1368 return &file_names[index];
1369 }
1370
1371 /* Offset of line number information in .debug_line section. */
1372 sect_offset sect_off {};
1373
1374 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1375 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1376
1377 unsigned int total_length {};
1378 unsigned short version {};
1379 unsigned int header_length {};
1380 unsigned char minimum_instruction_length {};
1381 unsigned char maximum_ops_per_instruction {};
1382 unsigned char default_is_stmt {};
1383 int line_base {};
1384 unsigned char line_range {};
1385 unsigned char opcode_base {};
1386
1387 /* standard_opcode_lengths[i] is the number of operands for the
1388 standard opcode whose value is i. This means that
1389 standard_opcode_lengths[0] is unused, and the last meaningful
1390 element is standard_opcode_lengths[opcode_base - 1]. */
1391 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1392
1393 /* The include_directories table. Note these are observing
1394 pointers. The memory is owned by debug_line_buffer. */
1395 std::vector<const char *> include_dirs;
1396
1397 /* The file_names table. */
1398 std::vector<file_entry> file_names;
1399
1400 /* The start and end of the statement program following this
1401 header. These point into dwarf2_per_objfile->line_buffer. */
1402 const gdb_byte *statement_program_start {}, *statement_program_end {};
1403 };
1404
1405 typedef std::unique_ptr<line_header> line_header_up;
1406
1407 const char *
1408 file_entry::include_dir (const line_header *lh) const
1409 {
1410 return lh->include_dir_at (d_index);
1411 }
1412
1413 /* When we construct a partial symbol table entry we only
1414 need this much information. */
1415 struct partial_die_info : public allocate_on_obstack
1416 {
1417 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1418
1419 /* Disable assign but still keep copy ctor, which is needed
1420 load_partial_dies. */
1421 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1422
1423 /* Adjust the partial die before generating a symbol for it. This
1424 function may set the is_external flag or change the DIE's
1425 name. */
1426 void fixup (struct dwarf2_cu *cu);
1427
1428 /* Read a minimal amount of information into the minimal die
1429 structure. */
1430 const gdb_byte *read (const struct die_reader_specs *reader,
1431 const struct abbrev_info &abbrev,
1432 const gdb_byte *info_ptr);
1433
1434 /* Offset of this DIE. */
1435 const sect_offset sect_off;
1436
1437 /* DWARF-2 tag for this DIE. */
1438 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1439
1440 /* Assorted flags describing the data found in this DIE. */
1441 const unsigned int has_children : 1;
1442
1443 unsigned int is_external : 1;
1444 unsigned int is_declaration : 1;
1445 unsigned int has_type : 1;
1446 unsigned int has_specification : 1;
1447 unsigned int has_pc_info : 1;
1448 unsigned int may_be_inlined : 1;
1449
1450 /* This DIE has been marked DW_AT_main_subprogram. */
1451 unsigned int main_subprogram : 1;
1452
1453 /* Flag set if the SCOPE field of this structure has been
1454 computed. */
1455 unsigned int scope_set : 1;
1456
1457 /* Flag set if the DIE has a byte_size attribute. */
1458 unsigned int has_byte_size : 1;
1459
1460 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1461 unsigned int has_const_value : 1;
1462
1463 /* Flag set if any of the DIE's children are template arguments. */
1464 unsigned int has_template_arguments : 1;
1465
1466 /* Flag set if fixup has been called on this die. */
1467 unsigned int fixup_called : 1;
1468
1469 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1470 unsigned int is_dwz : 1;
1471
1472 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1473 unsigned int spec_is_dwz : 1;
1474
1475 /* The name of this DIE. Normally the value of DW_AT_name, but
1476 sometimes a default name for unnamed DIEs. */
1477 const char *name = nullptr;
1478
1479 /* The linkage name, if present. */
1480 const char *linkage_name = nullptr;
1481
1482 /* The scope to prepend to our children. This is generally
1483 allocated on the comp_unit_obstack, so will disappear
1484 when this compilation unit leaves the cache. */
1485 const char *scope = nullptr;
1486
1487 /* Some data associated with the partial DIE. The tag determines
1488 which field is live. */
1489 union
1490 {
1491 /* The location description associated with this DIE, if any. */
1492 struct dwarf_block *locdesc;
1493 /* The offset of an import, for DW_TAG_imported_unit. */
1494 sect_offset sect_off;
1495 } d {};
1496
1497 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1498 CORE_ADDR lowpc = 0;
1499 CORE_ADDR highpc = 0;
1500
1501 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1502 DW_AT_sibling, if any. */
1503 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1504 could return DW_AT_sibling values to its caller load_partial_dies. */
1505 const gdb_byte *sibling = nullptr;
1506
1507 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1508 DW_AT_specification (or DW_AT_abstract_origin or
1509 DW_AT_extension). */
1510 sect_offset spec_offset {};
1511
1512 /* Pointers to this DIE's parent, first child, and next sibling,
1513 if any. */
1514 struct partial_die_info *die_parent = nullptr;
1515 struct partial_die_info *die_child = nullptr;
1516 struct partial_die_info *die_sibling = nullptr;
1517
1518 friend struct partial_die_info *
1519 dwarf2_cu::find_partial_die (sect_offset sect_off);
1520
1521 private:
1522 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1523 partial_die_info (sect_offset sect_off)
1524 : partial_die_info (sect_off, DW_TAG_padding, 0)
1525 {
1526 }
1527
1528 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1529 int has_children_)
1530 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1531 {
1532 is_external = 0;
1533 is_declaration = 0;
1534 has_type = 0;
1535 has_specification = 0;
1536 has_pc_info = 0;
1537 may_be_inlined = 0;
1538 main_subprogram = 0;
1539 scope_set = 0;
1540 has_byte_size = 0;
1541 has_const_value = 0;
1542 has_template_arguments = 0;
1543 fixup_called = 0;
1544 is_dwz = 0;
1545 spec_is_dwz = 0;
1546 }
1547 };
1548
1549 /* This data structure holds the information of an abbrev. */
1550 struct abbrev_info
1551 {
1552 unsigned int number; /* number identifying abbrev */
1553 enum dwarf_tag tag; /* dwarf tag */
1554 unsigned short has_children; /* boolean */
1555 unsigned short num_attrs; /* number of attributes */
1556 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1557 struct abbrev_info *next; /* next in chain */
1558 };
1559
1560 struct attr_abbrev
1561 {
1562 ENUM_BITFIELD(dwarf_attribute) name : 16;
1563 ENUM_BITFIELD(dwarf_form) form : 16;
1564
1565 /* It is valid only if FORM is DW_FORM_implicit_const. */
1566 LONGEST implicit_const;
1567 };
1568
1569 /* Size of abbrev_table.abbrev_hash_table. */
1570 #define ABBREV_HASH_SIZE 121
1571
1572 /* Top level data structure to contain an abbreviation table. */
1573
1574 struct abbrev_table
1575 {
1576 explicit abbrev_table (sect_offset off)
1577 : sect_off (off)
1578 {
1579 m_abbrevs =
1580 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1581 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1582 }
1583
1584 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1585
1586 /* Allocate space for a struct abbrev_info object in
1587 ABBREV_TABLE. */
1588 struct abbrev_info *alloc_abbrev ();
1589
1590 /* Add an abbreviation to the table. */
1591 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1592
1593 /* Look up an abbrev in the table.
1594 Returns NULL if the abbrev is not found. */
1595
1596 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1597
1598
1599 /* Where the abbrev table came from.
1600 This is used as a sanity check when the table is used. */
1601 const sect_offset sect_off;
1602
1603 /* Storage for the abbrev table. */
1604 auto_obstack abbrev_obstack;
1605
1606 private:
1607
1608 /* Hash table of abbrevs.
1609 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1610 It could be statically allocated, but the previous code didn't so we
1611 don't either. */
1612 struct abbrev_info **m_abbrevs;
1613 };
1614
1615 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1616
1617 /* Attributes have a name and a value. */
1618 struct attribute
1619 {
1620 ENUM_BITFIELD(dwarf_attribute) name : 16;
1621 ENUM_BITFIELD(dwarf_form) form : 15;
1622
1623 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1624 field should be in u.str (existing only for DW_STRING) but it is kept
1625 here for better struct attribute alignment. */
1626 unsigned int string_is_canonical : 1;
1627
1628 union
1629 {
1630 const char *str;
1631 struct dwarf_block *blk;
1632 ULONGEST unsnd;
1633 LONGEST snd;
1634 CORE_ADDR addr;
1635 ULONGEST signature;
1636 }
1637 u;
1638 };
1639
1640 /* This data structure holds a complete die structure. */
1641 struct die_info
1642 {
1643 /* DWARF-2 tag for this DIE. */
1644 ENUM_BITFIELD(dwarf_tag) tag : 16;
1645
1646 /* Number of attributes */
1647 unsigned char num_attrs;
1648
1649 /* True if we're presently building the full type name for the
1650 type derived from this DIE. */
1651 unsigned char building_fullname : 1;
1652
1653 /* True if this die is in process. PR 16581. */
1654 unsigned char in_process : 1;
1655
1656 /* Abbrev number */
1657 unsigned int abbrev;
1658
1659 /* Offset in .debug_info or .debug_types section. */
1660 sect_offset sect_off;
1661
1662 /* The dies in a compilation unit form an n-ary tree. PARENT
1663 points to this die's parent; CHILD points to the first child of
1664 this node; and all the children of a given node are chained
1665 together via their SIBLING fields. */
1666 struct die_info *child; /* Its first child, if any. */
1667 struct die_info *sibling; /* Its next sibling, if any. */
1668 struct die_info *parent; /* Its parent, if any. */
1669
1670 /* An array of attributes, with NUM_ATTRS elements. There may be
1671 zero, but it's not common and zero-sized arrays are not
1672 sufficiently portable C. */
1673 struct attribute attrs[1];
1674 };
1675
1676 /* Get at parts of an attribute structure. */
1677
1678 #define DW_STRING(attr) ((attr)->u.str)
1679 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1680 #define DW_UNSND(attr) ((attr)->u.unsnd)
1681 #define DW_BLOCK(attr) ((attr)->u.blk)
1682 #define DW_SND(attr) ((attr)->u.snd)
1683 #define DW_ADDR(attr) ((attr)->u.addr)
1684 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1685
1686 /* Blocks are a bunch of untyped bytes. */
1687 struct dwarf_block
1688 {
1689 size_t size;
1690
1691 /* Valid only if SIZE is not zero. */
1692 const gdb_byte *data;
1693 };
1694
1695 #ifndef ATTR_ALLOC_CHUNK
1696 #define ATTR_ALLOC_CHUNK 4
1697 #endif
1698
1699 /* Allocate fields for structs, unions and enums in this size. */
1700 #ifndef DW_FIELD_ALLOC_CHUNK
1701 #define DW_FIELD_ALLOC_CHUNK 4
1702 #endif
1703
1704 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1705 but this would require a corresponding change in unpack_field_as_long
1706 and friends. */
1707 static int bits_per_byte = 8;
1708
1709 /* When reading a variant or variant part, we track a bit more
1710 information about the field, and store it in an object of this
1711 type. */
1712
1713 struct variant_field
1714 {
1715 /* If we see a DW_TAG_variant, then this will be the discriminant
1716 value. */
1717 ULONGEST discriminant_value;
1718 /* If we see a DW_TAG_variant, then this will be set if this is the
1719 default branch. */
1720 bool default_branch;
1721 /* While reading a DW_TAG_variant_part, this will be set if this
1722 field is the discriminant. */
1723 bool is_discriminant;
1724 };
1725
1726 struct nextfield
1727 {
1728 struct nextfield *next;
1729 int accessibility;
1730 int virtuality;
1731 /* Extra information to describe a variant or variant part. */
1732 struct variant_field variant;
1733 struct field field;
1734 };
1735
1736 struct nextfnfield
1737 {
1738 struct nextfnfield *next;
1739 struct fn_field fnfield;
1740 };
1741
1742 struct fnfieldlist
1743 {
1744 const char *name;
1745 int length;
1746 struct nextfnfield *head;
1747 };
1748
1749 struct decl_field_list
1750 {
1751 struct decl_field field;
1752 struct decl_field_list *next;
1753 };
1754
1755 /* The routines that read and process dies for a C struct or C++ class
1756 pass lists of data member fields and lists of member function fields
1757 in an instance of a field_info structure, as defined below. */
1758 struct field_info
1759 {
1760 /* List of data member and baseclasses fields. */
1761 struct nextfield *fields, *baseclasses;
1762
1763 /* Number of fields (including baseclasses). */
1764 int nfields;
1765
1766 /* Number of baseclasses. */
1767 int nbaseclasses;
1768
1769 /* Set if the accesibility of one of the fields is not public. */
1770 int non_public_fields;
1771
1772 /* Member function fieldlist array, contains name of possibly overloaded
1773 member function, number of overloaded member functions and a pointer
1774 to the head of the member function field chain. */
1775 struct fnfieldlist *fnfieldlists;
1776
1777 /* Number of entries in the fnfieldlists array. */
1778 int nfnfields;
1779
1780 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1781 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1782 struct decl_field_list *typedef_field_list;
1783 unsigned typedef_field_list_count;
1784
1785 /* Nested types defined by this class and the number of elements in this
1786 list. */
1787 struct decl_field_list *nested_types_list;
1788 unsigned nested_types_list_count;
1789 };
1790
1791 /* One item on the queue of compilation units to read in full symbols
1792 for. */
1793 struct dwarf2_queue_item
1794 {
1795 struct dwarf2_per_cu_data *per_cu;
1796 enum language pretend_language;
1797 struct dwarf2_queue_item *next;
1798 };
1799
1800 /* The current queue. */
1801 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1802
1803 /* Loaded secondary compilation units are kept in memory until they
1804 have not been referenced for the processing of this many
1805 compilation units. Set this to zero to disable caching. Cache
1806 sizes of up to at least twenty will improve startup time for
1807 typical inter-CU-reference binaries, at an obvious memory cost. */
1808 static int dwarf_max_cache_age = 5;
1809 static void
1810 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1811 struct cmd_list_element *c, const char *value)
1812 {
1813 fprintf_filtered (file, _("The upper bound on the age of cached "
1814 "DWARF compilation units is %s.\n"),
1815 value);
1816 }
1817 \f
1818 /* local function prototypes */
1819
1820 static const char *get_section_name (const struct dwarf2_section_info *);
1821
1822 static const char *get_section_file_name (const struct dwarf2_section_info *);
1823
1824 static void dwarf2_find_base_address (struct die_info *die,
1825 struct dwarf2_cu *cu);
1826
1827 static struct partial_symtab *create_partial_symtab
1828 (struct dwarf2_per_cu_data *per_cu, const char *name);
1829
1830 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1831 const gdb_byte *info_ptr,
1832 struct die_info *type_unit_die,
1833 int has_children, void *data);
1834
1835 static void dwarf2_build_psymtabs_hard
1836 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1837
1838 static void scan_partial_symbols (struct partial_die_info *,
1839 CORE_ADDR *, CORE_ADDR *,
1840 int, struct dwarf2_cu *);
1841
1842 static void add_partial_symbol (struct partial_die_info *,
1843 struct dwarf2_cu *);
1844
1845 static void add_partial_namespace (struct partial_die_info *pdi,
1846 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1847 int set_addrmap, struct dwarf2_cu *cu);
1848
1849 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1850 CORE_ADDR *highpc, int set_addrmap,
1851 struct dwarf2_cu *cu);
1852
1853 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1854 struct dwarf2_cu *cu);
1855
1856 static void add_partial_subprogram (struct partial_die_info *pdi,
1857 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1858 int need_pc, struct dwarf2_cu *cu);
1859
1860 static void dwarf2_read_symtab (struct partial_symtab *,
1861 struct objfile *);
1862
1863 static void psymtab_to_symtab_1 (struct partial_symtab *);
1864
1865 static abbrev_table_up abbrev_table_read_table
1866 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1867 sect_offset);
1868
1869 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1870
1871 static struct partial_die_info *load_partial_dies
1872 (const struct die_reader_specs *, const gdb_byte *, int);
1873
1874 static struct partial_die_info *find_partial_die (sect_offset, int,
1875 struct dwarf2_cu *);
1876
1877 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1878 struct attribute *, struct attr_abbrev *,
1879 const gdb_byte *);
1880
1881 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1882
1883 static int read_1_signed_byte (bfd *, const gdb_byte *);
1884
1885 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1886
1887 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1888
1889 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1890
1891 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1892 unsigned int *);
1893
1894 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1895
1896 static LONGEST read_checked_initial_length_and_offset
1897 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1898 unsigned int *, unsigned int *);
1899
1900 static LONGEST read_offset (bfd *, const gdb_byte *,
1901 const struct comp_unit_head *,
1902 unsigned int *);
1903
1904 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1905
1906 static sect_offset read_abbrev_offset
1907 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1908 struct dwarf2_section_info *, sect_offset);
1909
1910 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1911
1912 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1913
1914 static const char *read_indirect_string
1915 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1916 const struct comp_unit_head *, unsigned int *);
1917
1918 static const char *read_indirect_line_string
1919 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1920 const struct comp_unit_head *, unsigned int *);
1921
1922 static const char *read_indirect_string_at_offset
1923 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1924 LONGEST str_offset);
1925
1926 static const char *read_indirect_string_from_dwz
1927 (struct objfile *objfile, struct dwz_file *, LONGEST);
1928
1929 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1930
1931 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1932 const gdb_byte *,
1933 unsigned int *);
1934
1935 static const char *read_str_index (const struct die_reader_specs *reader,
1936 ULONGEST str_index);
1937
1938 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1939
1940 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1941 struct dwarf2_cu *);
1942
1943 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1944 unsigned int);
1945
1946 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1947 struct dwarf2_cu *cu);
1948
1949 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1950 struct dwarf2_cu *cu);
1951
1952 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1953
1954 static struct die_info *die_specification (struct die_info *die,
1955 struct dwarf2_cu **);
1956
1957 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1958 struct dwarf2_cu *cu);
1959
1960 static void dwarf_decode_lines (struct line_header *, const char *,
1961 struct dwarf2_cu *, struct partial_symtab *,
1962 CORE_ADDR, int decode_mapping);
1963
1964 static void dwarf2_start_subfile (const char *, const char *);
1965
1966 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1967 const char *, const char *,
1968 CORE_ADDR);
1969
1970 static struct symbol *new_symbol (struct die_info *, struct type *,
1971 struct dwarf2_cu *, struct symbol * = NULL);
1972
1973 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1974 struct dwarf2_cu *);
1975
1976 static void dwarf2_const_value_attr (const struct attribute *attr,
1977 struct type *type,
1978 const char *name,
1979 struct obstack *obstack,
1980 struct dwarf2_cu *cu, LONGEST *value,
1981 const gdb_byte **bytes,
1982 struct dwarf2_locexpr_baton **baton);
1983
1984 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1985
1986 static int need_gnat_info (struct dwarf2_cu *);
1987
1988 static struct type *die_descriptive_type (struct die_info *,
1989 struct dwarf2_cu *);
1990
1991 static void set_descriptive_type (struct type *, struct die_info *,
1992 struct dwarf2_cu *);
1993
1994 static struct type *die_containing_type (struct die_info *,
1995 struct dwarf2_cu *);
1996
1997 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1998 struct dwarf2_cu *);
1999
2000 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
2001
2002 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
2003
2004 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
2005
2006 static char *typename_concat (struct obstack *obs, const char *prefix,
2007 const char *suffix, int physname,
2008 struct dwarf2_cu *cu);
2009
2010 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
2011
2012 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
2013
2014 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
2015
2016 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
2017
2018 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
2019
2020 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
2021
2022 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
2023 struct dwarf2_cu *, struct partial_symtab *);
2024
2025 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
2026 values. Keep the items ordered with increasing constraints compliance. */
2027 enum pc_bounds_kind
2028 {
2029 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
2030 PC_BOUNDS_NOT_PRESENT,
2031
2032 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
2033 were present but they do not form a valid range of PC addresses. */
2034 PC_BOUNDS_INVALID,
2035
2036 /* Discontiguous range was found - that is DW_AT_ranges was found. */
2037 PC_BOUNDS_RANGES,
2038
2039 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
2040 PC_BOUNDS_HIGH_LOW,
2041 };
2042
2043 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
2044 CORE_ADDR *, CORE_ADDR *,
2045 struct dwarf2_cu *,
2046 struct partial_symtab *);
2047
2048 static void get_scope_pc_bounds (struct die_info *,
2049 CORE_ADDR *, CORE_ADDR *,
2050 struct dwarf2_cu *);
2051
2052 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
2053 CORE_ADDR, struct dwarf2_cu *);
2054
2055 static void dwarf2_add_field (struct field_info *, struct die_info *,
2056 struct dwarf2_cu *);
2057
2058 static void dwarf2_attach_fields_to_type (struct field_info *,
2059 struct type *, struct dwarf2_cu *);
2060
2061 static void dwarf2_add_member_fn (struct field_info *,
2062 struct die_info *, struct type *,
2063 struct dwarf2_cu *);
2064
2065 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
2066 struct type *,
2067 struct dwarf2_cu *);
2068
2069 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
2070
2071 static void read_common_block (struct die_info *, struct dwarf2_cu *);
2072
2073 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
2074
2075 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
2076
2077 static struct using_direct **using_directives (enum language);
2078
2079 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
2080
2081 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
2082
2083 static struct type *read_module_type (struct die_info *die,
2084 struct dwarf2_cu *cu);
2085
2086 static const char *namespace_name (struct die_info *die,
2087 int *is_anonymous, struct dwarf2_cu *);
2088
2089 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
2090
2091 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
2092
2093 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
2094 struct dwarf2_cu *);
2095
2096 static struct die_info *read_die_and_siblings_1
2097 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
2098 struct die_info *);
2099
2100 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
2101 const gdb_byte *info_ptr,
2102 const gdb_byte **new_info_ptr,
2103 struct die_info *parent);
2104
2105 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
2106 struct die_info **, const gdb_byte *,
2107 int *, int);
2108
2109 static const gdb_byte *read_full_die (const struct die_reader_specs *,
2110 struct die_info **, const gdb_byte *,
2111 int *);
2112
2113 static void process_die (struct die_info *, struct dwarf2_cu *);
2114
2115 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
2116 struct obstack *);
2117
2118 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
2119
2120 static const char *dwarf2_full_name (const char *name,
2121 struct die_info *die,
2122 struct dwarf2_cu *cu);
2123
2124 static const char *dwarf2_physname (const char *name, struct die_info *die,
2125 struct dwarf2_cu *cu);
2126
2127 static struct die_info *dwarf2_extension (struct die_info *die,
2128 struct dwarf2_cu **);
2129
2130 static const char *dwarf_tag_name (unsigned int);
2131
2132 static const char *dwarf_attr_name (unsigned int);
2133
2134 static const char *dwarf_form_name (unsigned int);
2135
2136 static const char *dwarf_bool_name (unsigned int);
2137
2138 static const char *dwarf_type_encoding_name (unsigned int);
2139
2140 static struct die_info *sibling_die (struct die_info *);
2141
2142 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
2143
2144 static void dump_die_for_error (struct die_info *);
2145
2146 static void dump_die_1 (struct ui_file *, int level, int max_level,
2147 struct die_info *);
2148
2149 /*static*/ void dump_die (struct die_info *, int max_level);
2150
2151 static void store_in_ref_table (struct die_info *,
2152 struct dwarf2_cu *);
2153
2154 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
2155
2156 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
2157
2158 static struct die_info *follow_die_ref_or_sig (struct die_info *,
2159 const struct attribute *,
2160 struct dwarf2_cu **);
2161
2162 static struct die_info *follow_die_ref (struct die_info *,
2163 const struct attribute *,
2164 struct dwarf2_cu **);
2165
2166 static struct die_info *follow_die_sig (struct die_info *,
2167 const struct attribute *,
2168 struct dwarf2_cu **);
2169
2170 static struct type *get_signatured_type (struct die_info *, ULONGEST,
2171 struct dwarf2_cu *);
2172
2173 static struct type *get_DW_AT_signature_type (struct die_info *,
2174 const struct attribute *,
2175 struct dwarf2_cu *);
2176
2177 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
2178
2179 static void read_signatured_type (struct signatured_type *);
2180
2181 static int attr_to_dynamic_prop (const struct attribute *attr,
2182 struct die_info *die, struct dwarf2_cu *cu,
2183 struct dynamic_prop *prop);
2184
2185 /* memory allocation interface */
2186
2187 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
2188
2189 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
2190
2191 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2192
2193 static int attr_form_is_block (const struct attribute *);
2194
2195 static int attr_form_is_section_offset (const struct attribute *);
2196
2197 static int attr_form_is_constant (const struct attribute *);
2198
2199 static int attr_form_is_ref (const struct attribute *);
2200
2201 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
2202 struct dwarf2_loclist_baton *baton,
2203 const struct attribute *attr);
2204
2205 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
2206 struct symbol *sym,
2207 struct dwarf2_cu *cu,
2208 int is_block);
2209
2210 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
2211 const gdb_byte *info_ptr,
2212 struct abbrev_info *abbrev);
2213
2214 static hashval_t partial_die_hash (const void *item);
2215
2216 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
2217
2218 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
2219 (sect_offset sect_off, unsigned int offset_in_dwz,
2220 struct dwarf2_per_objfile *dwarf2_per_objfile);
2221
2222 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
2223 struct die_info *comp_unit_die,
2224 enum language pretend_language);
2225
2226 static void free_cached_comp_units (void *);
2227
2228 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2229
2230 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
2231
2232 static struct type *set_die_type (struct die_info *, struct type *,
2233 struct dwarf2_cu *);
2234
2235 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2236
2237 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
2238
2239 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
2240 enum language);
2241
2242 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
2243 enum language);
2244
2245 static void process_full_type_unit (struct dwarf2_per_cu_data *,
2246 enum language);
2247
2248 static void dwarf2_add_dependence (struct dwarf2_cu *,
2249 struct dwarf2_per_cu_data *);
2250
2251 static void dwarf2_mark (struct dwarf2_cu *);
2252
2253 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
2254
2255 static struct type *get_die_type_at_offset (sect_offset,
2256 struct dwarf2_per_cu_data *);
2257
2258 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
2259
2260 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
2261 enum language pretend_language);
2262
2263 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
2264
2265 /* Class, the destructor of which frees all allocated queue entries. This
2266 will only have work to do if an error was thrown while processing the
2267 dwarf. If no error was thrown then the queue entries should have all
2268 been processed, and freed, as we went along. */
2269
2270 class dwarf2_queue_guard
2271 {
2272 public:
2273 dwarf2_queue_guard () = default;
2274
2275 /* Free any entries remaining on the queue. There should only be
2276 entries left if we hit an error while processing the dwarf. */
2277 ~dwarf2_queue_guard ()
2278 {
2279 struct dwarf2_queue_item *item, *last;
2280
2281 item = dwarf2_queue;
2282 while (item)
2283 {
2284 /* Anything still marked queued is likely to be in an
2285 inconsistent state, so discard it. */
2286 if (item->per_cu->queued)
2287 {
2288 if (item->per_cu->cu != NULL)
2289 free_one_cached_comp_unit (item->per_cu);
2290 item->per_cu->queued = 0;
2291 }
2292
2293 last = item;
2294 item = item->next;
2295 xfree (last);
2296 }
2297
2298 dwarf2_queue = dwarf2_queue_tail = NULL;
2299 }
2300 };
2301
2302 /* The return type of find_file_and_directory. Note, the enclosed
2303 string pointers are only valid while this object is valid. */
2304
2305 struct file_and_directory
2306 {
2307 /* The filename. This is never NULL. */
2308 const char *name;
2309
2310 /* The compilation directory. NULL if not known. If we needed to
2311 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2312 points directly to the DW_AT_comp_dir string attribute owned by
2313 the obstack that owns the DIE. */
2314 const char *comp_dir;
2315
2316 /* If we needed to build a new string for comp_dir, this is what
2317 owns the storage. */
2318 std::string comp_dir_storage;
2319 };
2320
2321 static file_and_directory find_file_and_directory (struct die_info *die,
2322 struct dwarf2_cu *cu);
2323
2324 static char *file_full_name (int file, struct line_header *lh,
2325 const char *comp_dir);
2326
2327 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2328 enum class rcuh_kind { COMPILE, TYPE };
2329
2330 static const gdb_byte *read_and_check_comp_unit_head
2331 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2332 struct comp_unit_head *header,
2333 struct dwarf2_section_info *section,
2334 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2335 rcuh_kind section_kind);
2336
2337 static void init_cutu_and_read_dies
2338 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2339 int use_existing_cu, int keep,
2340 die_reader_func_ftype *die_reader_func, void *data);
2341
2342 static void init_cutu_and_read_dies_simple
2343 (struct dwarf2_per_cu_data *this_cu,
2344 die_reader_func_ftype *die_reader_func, void *data);
2345
2346 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2347
2348 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2349
2350 static struct dwo_unit *lookup_dwo_unit_in_dwp
2351 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2352 struct dwp_file *dwp_file, const char *comp_dir,
2353 ULONGEST signature, int is_debug_types);
2354
2355 static struct dwp_file *get_dwp_file
2356 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2357
2358 static struct dwo_unit *lookup_dwo_comp_unit
2359 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2360
2361 static struct dwo_unit *lookup_dwo_type_unit
2362 (struct signatured_type *, const char *, const char *);
2363
2364 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2365
2366 static void free_dwo_file_cleanup (void *);
2367
2368 struct free_dwo_file_cleanup_data
2369 {
2370 struct dwo_file *dwo_file;
2371 struct dwarf2_per_objfile *dwarf2_per_objfile;
2372 };
2373
2374 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2375
2376 static void check_producer (struct dwarf2_cu *cu);
2377
2378 static void free_line_header_voidp (void *arg);
2379 \f
2380 /* Various complaints about symbol reading that don't abort the process. */
2381
2382 static void
2383 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2384 {
2385 complaint (&symfile_complaints,
2386 _("statement list doesn't fit in .debug_line section"));
2387 }
2388
2389 static void
2390 dwarf2_debug_line_missing_file_complaint (void)
2391 {
2392 complaint (&symfile_complaints,
2393 _(".debug_line section has line data without a file"));
2394 }
2395
2396 static void
2397 dwarf2_debug_line_missing_end_sequence_complaint (void)
2398 {
2399 complaint (&symfile_complaints,
2400 _(".debug_line section has line "
2401 "program sequence without an end"));
2402 }
2403
2404 static void
2405 dwarf2_complex_location_expr_complaint (void)
2406 {
2407 complaint (&symfile_complaints, _("location expression too complex"));
2408 }
2409
2410 static void
2411 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2412 int arg3)
2413 {
2414 complaint (&symfile_complaints,
2415 _("const value length mismatch for '%s', got %d, expected %d"),
2416 arg1, arg2, arg3);
2417 }
2418
2419 static void
2420 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2421 {
2422 complaint (&symfile_complaints,
2423 _("debug info runs off end of %s section"
2424 " [in module %s]"),
2425 get_section_name (section),
2426 get_section_file_name (section));
2427 }
2428
2429 static void
2430 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2431 {
2432 complaint (&symfile_complaints,
2433 _("macro debug info contains a "
2434 "malformed macro definition:\n`%s'"),
2435 arg1);
2436 }
2437
2438 static void
2439 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2440 {
2441 complaint (&symfile_complaints,
2442 _("invalid attribute class or form for '%s' in '%s'"),
2443 arg1, arg2);
2444 }
2445
2446 /* Hash function for line_header_hash. */
2447
2448 static hashval_t
2449 line_header_hash (const struct line_header *ofs)
2450 {
2451 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2452 }
2453
2454 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2455
2456 static hashval_t
2457 line_header_hash_voidp (const void *item)
2458 {
2459 const struct line_header *ofs = (const struct line_header *) item;
2460
2461 return line_header_hash (ofs);
2462 }
2463
2464 /* Equality function for line_header_hash. */
2465
2466 static int
2467 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2468 {
2469 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2470 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2471
2472 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2473 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2474 }
2475
2476 \f
2477
2478 /* Read the given attribute value as an address, taking the attribute's
2479 form into account. */
2480
2481 static CORE_ADDR
2482 attr_value_as_address (struct attribute *attr)
2483 {
2484 CORE_ADDR addr;
2485
2486 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2487 {
2488 /* Aside from a few clearly defined exceptions, attributes that
2489 contain an address must always be in DW_FORM_addr form.
2490 Unfortunately, some compilers happen to be violating this
2491 requirement by encoding addresses using other forms, such
2492 as DW_FORM_data4 for example. For those broken compilers,
2493 we try to do our best, without any guarantee of success,
2494 to interpret the address correctly. It would also be nice
2495 to generate a complaint, but that would require us to maintain
2496 a list of legitimate cases where a non-address form is allowed,
2497 as well as update callers to pass in at least the CU's DWARF
2498 version. This is more overhead than what we're willing to
2499 expand for a pretty rare case. */
2500 addr = DW_UNSND (attr);
2501 }
2502 else
2503 addr = DW_ADDR (attr);
2504
2505 return addr;
2506 }
2507
2508 /* The suffix for an index file. */
2509 #define INDEX4_SUFFIX ".gdb-index"
2510 #define INDEX5_SUFFIX ".debug_names"
2511 #define DEBUG_STR_SUFFIX ".debug_str"
2512
2513 /* See declaration. */
2514
2515 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2516 const dwarf2_debug_sections *names)
2517 : objfile (objfile_)
2518 {
2519 if (names == NULL)
2520 names = &dwarf2_elf_names;
2521
2522 bfd *obfd = objfile->obfd;
2523
2524 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2525 locate_sections (obfd, sec, *names);
2526 }
2527
2528 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2529
2530 dwarf2_per_objfile::~dwarf2_per_objfile ()
2531 {
2532 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2533 free_cached_comp_units ();
2534
2535 if (quick_file_names_table)
2536 htab_delete (quick_file_names_table);
2537
2538 if (line_header_hash)
2539 htab_delete (line_header_hash);
2540
2541 for (int ix = 0; ix < n_comp_units; ++ix)
2542 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2543
2544 for (int ix = 0; ix < n_type_units; ++ix)
2545 VEC_free (dwarf2_per_cu_ptr,
2546 all_type_units[ix]->per_cu.imported_symtabs);
2547 xfree (all_type_units);
2548
2549 VEC_free (dwarf2_section_info_def, types);
2550
2551 if (dwo_files != NULL)
2552 free_dwo_files (dwo_files, objfile);
2553 if (dwp_file != NULL)
2554 gdb_bfd_unref (dwp_file->dbfd);
2555
2556 if (dwz_file != NULL && dwz_file->dwz_bfd)
2557 gdb_bfd_unref (dwz_file->dwz_bfd);
2558
2559 if (index_table != NULL)
2560 index_table->~mapped_index ();
2561
2562 /* Everything else should be on the objfile obstack. */
2563 }
2564
2565 /* See declaration. */
2566
2567 void
2568 dwarf2_per_objfile::free_cached_comp_units ()
2569 {
2570 dwarf2_per_cu_data *per_cu = read_in_chain;
2571 dwarf2_per_cu_data **last_chain = &read_in_chain;
2572 while (per_cu != NULL)
2573 {
2574 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2575
2576 delete per_cu->cu;
2577 *last_chain = next_cu;
2578 per_cu = next_cu;
2579 }
2580 }
2581
2582 /* Try to locate the sections we need for DWARF 2 debugging
2583 information and return true if we have enough to do something.
2584 NAMES points to the dwarf2 section names, or is NULL if the standard
2585 ELF names are used. */
2586
2587 int
2588 dwarf2_has_info (struct objfile *objfile,
2589 const struct dwarf2_debug_sections *names)
2590 {
2591 if (objfile->flags & OBJF_READNEVER)
2592 return 0;
2593
2594 struct dwarf2_per_objfile *dwarf2_per_objfile
2595 = get_dwarf2_per_objfile (objfile);
2596
2597 if (dwarf2_per_objfile == NULL)
2598 {
2599 /* Initialize per-objfile state. */
2600 dwarf2_per_objfile
2601 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2602 names);
2603 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2604 }
2605 return (!dwarf2_per_objfile->info.is_virtual
2606 && dwarf2_per_objfile->info.s.section != NULL
2607 && !dwarf2_per_objfile->abbrev.is_virtual
2608 && dwarf2_per_objfile->abbrev.s.section != NULL);
2609 }
2610
2611 /* Return the containing section of virtual section SECTION. */
2612
2613 static struct dwarf2_section_info *
2614 get_containing_section (const struct dwarf2_section_info *section)
2615 {
2616 gdb_assert (section->is_virtual);
2617 return section->s.containing_section;
2618 }
2619
2620 /* Return the bfd owner of SECTION. */
2621
2622 static struct bfd *
2623 get_section_bfd_owner (const struct dwarf2_section_info *section)
2624 {
2625 if (section->is_virtual)
2626 {
2627 section = get_containing_section (section);
2628 gdb_assert (!section->is_virtual);
2629 }
2630 return section->s.section->owner;
2631 }
2632
2633 /* Return the bfd section of SECTION.
2634 Returns NULL if the section is not present. */
2635
2636 static asection *
2637 get_section_bfd_section (const struct dwarf2_section_info *section)
2638 {
2639 if (section->is_virtual)
2640 {
2641 section = get_containing_section (section);
2642 gdb_assert (!section->is_virtual);
2643 }
2644 return section->s.section;
2645 }
2646
2647 /* Return the name of SECTION. */
2648
2649 static const char *
2650 get_section_name (const struct dwarf2_section_info *section)
2651 {
2652 asection *sectp = get_section_bfd_section (section);
2653
2654 gdb_assert (sectp != NULL);
2655 return bfd_section_name (get_section_bfd_owner (section), sectp);
2656 }
2657
2658 /* Return the name of the file SECTION is in. */
2659
2660 static const char *
2661 get_section_file_name (const struct dwarf2_section_info *section)
2662 {
2663 bfd *abfd = get_section_bfd_owner (section);
2664
2665 return bfd_get_filename (abfd);
2666 }
2667
2668 /* Return the id of SECTION.
2669 Returns 0 if SECTION doesn't exist. */
2670
2671 static int
2672 get_section_id (const struct dwarf2_section_info *section)
2673 {
2674 asection *sectp = get_section_bfd_section (section);
2675
2676 if (sectp == NULL)
2677 return 0;
2678 return sectp->id;
2679 }
2680
2681 /* Return the flags of SECTION.
2682 SECTION (or containing section if this is a virtual section) must exist. */
2683
2684 static int
2685 get_section_flags (const struct dwarf2_section_info *section)
2686 {
2687 asection *sectp = get_section_bfd_section (section);
2688
2689 gdb_assert (sectp != NULL);
2690 return bfd_get_section_flags (sectp->owner, sectp);
2691 }
2692
2693 /* When loading sections, we look either for uncompressed section or for
2694 compressed section names. */
2695
2696 static int
2697 section_is_p (const char *section_name,
2698 const struct dwarf2_section_names *names)
2699 {
2700 if (names->normal != NULL
2701 && strcmp (section_name, names->normal) == 0)
2702 return 1;
2703 if (names->compressed != NULL
2704 && strcmp (section_name, names->compressed) == 0)
2705 return 1;
2706 return 0;
2707 }
2708
2709 /* See declaration. */
2710
2711 void
2712 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2713 const dwarf2_debug_sections &names)
2714 {
2715 flagword aflag = bfd_get_section_flags (abfd, sectp);
2716
2717 if ((aflag & SEC_HAS_CONTENTS) == 0)
2718 {
2719 }
2720 else if (section_is_p (sectp->name, &names.info))
2721 {
2722 this->info.s.section = sectp;
2723 this->info.size = bfd_get_section_size (sectp);
2724 }
2725 else if (section_is_p (sectp->name, &names.abbrev))
2726 {
2727 this->abbrev.s.section = sectp;
2728 this->abbrev.size = bfd_get_section_size (sectp);
2729 }
2730 else if (section_is_p (sectp->name, &names.line))
2731 {
2732 this->line.s.section = sectp;
2733 this->line.size = bfd_get_section_size (sectp);
2734 }
2735 else if (section_is_p (sectp->name, &names.loc))
2736 {
2737 this->loc.s.section = sectp;
2738 this->loc.size = bfd_get_section_size (sectp);
2739 }
2740 else if (section_is_p (sectp->name, &names.loclists))
2741 {
2742 this->loclists.s.section = sectp;
2743 this->loclists.size = bfd_get_section_size (sectp);
2744 }
2745 else if (section_is_p (sectp->name, &names.macinfo))
2746 {
2747 this->macinfo.s.section = sectp;
2748 this->macinfo.size = bfd_get_section_size (sectp);
2749 }
2750 else if (section_is_p (sectp->name, &names.macro))
2751 {
2752 this->macro.s.section = sectp;
2753 this->macro.size = bfd_get_section_size (sectp);
2754 }
2755 else if (section_is_p (sectp->name, &names.str))
2756 {
2757 this->str.s.section = sectp;
2758 this->str.size = bfd_get_section_size (sectp);
2759 }
2760 else if (section_is_p (sectp->name, &names.line_str))
2761 {
2762 this->line_str.s.section = sectp;
2763 this->line_str.size = bfd_get_section_size (sectp);
2764 }
2765 else if (section_is_p (sectp->name, &names.addr))
2766 {
2767 this->addr.s.section = sectp;
2768 this->addr.size = bfd_get_section_size (sectp);
2769 }
2770 else if (section_is_p (sectp->name, &names.frame))
2771 {
2772 this->frame.s.section = sectp;
2773 this->frame.size = bfd_get_section_size (sectp);
2774 }
2775 else if (section_is_p (sectp->name, &names.eh_frame))
2776 {
2777 this->eh_frame.s.section = sectp;
2778 this->eh_frame.size = bfd_get_section_size (sectp);
2779 }
2780 else if (section_is_p (sectp->name, &names.ranges))
2781 {
2782 this->ranges.s.section = sectp;
2783 this->ranges.size = bfd_get_section_size (sectp);
2784 }
2785 else if (section_is_p (sectp->name, &names.rnglists))
2786 {
2787 this->rnglists.s.section = sectp;
2788 this->rnglists.size = bfd_get_section_size (sectp);
2789 }
2790 else if (section_is_p (sectp->name, &names.types))
2791 {
2792 struct dwarf2_section_info type_section;
2793
2794 memset (&type_section, 0, sizeof (type_section));
2795 type_section.s.section = sectp;
2796 type_section.size = bfd_get_section_size (sectp);
2797
2798 VEC_safe_push (dwarf2_section_info_def, this->types,
2799 &type_section);
2800 }
2801 else if (section_is_p (sectp->name, &names.gdb_index))
2802 {
2803 this->gdb_index.s.section = sectp;
2804 this->gdb_index.size = bfd_get_section_size (sectp);
2805 }
2806 else if (section_is_p (sectp->name, &names.debug_names))
2807 {
2808 this->debug_names.s.section = sectp;
2809 this->debug_names.size = bfd_get_section_size (sectp);
2810 }
2811 else if (section_is_p (sectp->name, &names.debug_aranges))
2812 {
2813 this->debug_aranges.s.section = sectp;
2814 this->debug_aranges.size = bfd_get_section_size (sectp);
2815 }
2816
2817 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2818 && bfd_section_vma (abfd, sectp) == 0)
2819 this->has_section_at_zero = true;
2820 }
2821
2822 /* A helper function that decides whether a section is empty,
2823 or not present. */
2824
2825 static int
2826 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2827 {
2828 if (section->is_virtual)
2829 return section->size == 0;
2830 return section->s.section == NULL || section->size == 0;
2831 }
2832
2833 /* Read the contents of the section INFO.
2834 OBJFILE is the main object file, but not necessarily the file where
2835 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2836 of the DWO file.
2837 If the section is compressed, uncompress it before returning. */
2838
2839 static void
2840 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2841 {
2842 asection *sectp;
2843 bfd *abfd;
2844 gdb_byte *buf, *retbuf;
2845
2846 if (info->readin)
2847 return;
2848 info->buffer = NULL;
2849 info->readin = 1;
2850
2851 if (dwarf2_section_empty_p (info))
2852 return;
2853
2854 sectp = get_section_bfd_section (info);
2855
2856 /* If this is a virtual section we need to read in the real one first. */
2857 if (info->is_virtual)
2858 {
2859 struct dwarf2_section_info *containing_section =
2860 get_containing_section (info);
2861
2862 gdb_assert (sectp != NULL);
2863 if ((sectp->flags & SEC_RELOC) != 0)
2864 {
2865 error (_("Dwarf Error: DWP format V2 with relocations is not"
2866 " supported in section %s [in module %s]"),
2867 get_section_name (info), get_section_file_name (info));
2868 }
2869 dwarf2_read_section (objfile, containing_section);
2870 /* Other code should have already caught virtual sections that don't
2871 fit. */
2872 gdb_assert (info->virtual_offset + info->size
2873 <= containing_section->size);
2874 /* If the real section is empty or there was a problem reading the
2875 section we shouldn't get here. */
2876 gdb_assert (containing_section->buffer != NULL);
2877 info->buffer = containing_section->buffer + info->virtual_offset;
2878 return;
2879 }
2880
2881 /* If the section has relocations, we must read it ourselves.
2882 Otherwise we attach it to the BFD. */
2883 if ((sectp->flags & SEC_RELOC) == 0)
2884 {
2885 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2886 return;
2887 }
2888
2889 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2890 info->buffer = buf;
2891
2892 /* When debugging .o files, we may need to apply relocations; see
2893 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2894 We never compress sections in .o files, so we only need to
2895 try this when the section is not compressed. */
2896 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2897 if (retbuf != NULL)
2898 {
2899 info->buffer = retbuf;
2900 return;
2901 }
2902
2903 abfd = get_section_bfd_owner (info);
2904 gdb_assert (abfd != NULL);
2905
2906 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2907 || bfd_bread (buf, info->size, abfd) != info->size)
2908 {
2909 error (_("Dwarf Error: Can't read DWARF data"
2910 " in section %s [in module %s]"),
2911 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2912 }
2913 }
2914
2915 /* A helper function that returns the size of a section in a safe way.
2916 If you are positive that the section has been read before using the
2917 size, then it is safe to refer to the dwarf2_section_info object's
2918 "size" field directly. In other cases, you must call this
2919 function, because for compressed sections the size field is not set
2920 correctly until the section has been read. */
2921
2922 static bfd_size_type
2923 dwarf2_section_size (struct objfile *objfile,
2924 struct dwarf2_section_info *info)
2925 {
2926 if (!info->readin)
2927 dwarf2_read_section (objfile, info);
2928 return info->size;
2929 }
2930
2931 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2932 SECTION_NAME. */
2933
2934 void
2935 dwarf2_get_section_info (struct objfile *objfile,
2936 enum dwarf2_section_enum sect,
2937 asection **sectp, const gdb_byte **bufp,
2938 bfd_size_type *sizep)
2939 {
2940 struct dwarf2_per_objfile *data
2941 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2942 dwarf2_objfile_data_key);
2943 struct dwarf2_section_info *info;
2944
2945 /* We may see an objfile without any DWARF, in which case we just
2946 return nothing. */
2947 if (data == NULL)
2948 {
2949 *sectp = NULL;
2950 *bufp = NULL;
2951 *sizep = 0;
2952 return;
2953 }
2954 switch (sect)
2955 {
2956 case DWARF2_DEBUG_FRAME:
2957 info = &data->frame;
2958 break;
2959 case DWARF2_EH_FRAME:
2960 info = &data->eh_frame;
2961 break;
2962 default:
2963 gdb_assert_not_reached ("unexpected section");
2964 }
2965
2966 dwarf2_read_section (objfile, info);
2967
2968 *sectp = get_section_bfd_section (info);
2969 *bufp = info->buffer;
2970 *sizep = info->size;
2971 }
2972
2973 /* A helper function to find the sections for a .dwz file. */
2974
2975 static void
2976 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2977 {
2978 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2979
2980 /* Note that we only support the standard ELF names, because .dwz
2981 is ELF-only (at the time of writing). */
2982 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2983 {
2984 dwz_file->abbrev.s.section = sectp;
2985 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2986 }
2987 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2988 {
2989 dwz_file->info.s.section = sectp;
2990 dwz_file->info.size = bfd_get_section_size (sectp);
2991 }
2992 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2993 {
2994 dwz_file->str.s.section = sectp;
2995 dwz_file->str.size = bfd_get_section_size (sectp);
2996 }
2997 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2998 {
2999 dwz_file->line.s.section = sectp;
3000 dwz_file->line.size = bfd_get_section_size (sectp);
3001 }
3002 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
3003 {
3004 dwz_file->macro.s.section = sectp;
3005 dwz_file->macro.size = bfd_get_section_size (sectp);
3006 }
3007 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
3008 {
3009 dwz_file->gdb_index.s.section = sectp;
3010 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
3011 }
3012 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
3013 {
3014 dwz_file->debug_names.s.section = sectp;
3015 dwz_file->debug_names.size = bfd_get_section_size (sectp);
3016 }
3017 }
3018
3019 /* Open the separate '.dwz' debug file, if needed. Return NULL if
3020 there is no .gnu_debugaltlink section in the file. Error if there
3021 is such a section but the file cannot be found. */
3022
3023 static struct dwz_file *
3024 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
3025 {
3026 const char *filename;
3027 struct dwz_file *result;
3028 bfd_size_type buildid_len_arg;
3029 size_t buildid_len;
3030 bfd_byte *buildid;
3031
3032 if (dwarf2_per_objfile->dwz_file != NULL)
3033 return dwarf2_per_objfile->dwz_file;
3034
3035 bfd_set_error (bfd_error_no_error);
3036 gdb::unique_xmalloc_ptr<char> data
3037 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
3038 &buildid_len_arg, &buildid));
3039 if (data == NULL)
3040 {
3041 if (bfd_get_error () == bfd_error_no_error)
3042 return NULL;
3043 error (_("could not read '.gnu_debugaltlink' section: %s"),
3044 bfd_errmsg (bfd_get_error ()));
3045 }
3046
3047 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
3048
3049 buildid_len = (size_t) buildid_len_arg;
3050
3051 filename = data.get ();
3052
3053 std::string abs_storage;
3054 if (!IS_ABSOLUTE_PATH (filename))
3055 {
3056 gdb::unique_xmalloc_ptr<char> abs
3057 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
3058
3059 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
3060 filename = abs_storage.c_str ();
3061 }
3062
3063 /* First try the file name given in the section. If that doesn't
3064 work, try to use the build-id instead. */
3065 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
3066 if (dwz_bfd != NULL)
3067 {
3068 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
3069 dwz_bfd.release ();
3070 }
3071
3072 if (dwz_bfd == NULL)
3073 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
3074
3075 if (dwz_bfd == NULL)
3076 error (_("could not find '.gnu_debugaltlink' file for %s"),
3077 objfile_name (dwarf2_per_objfile->objfile));
3078
3079 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
3080 struct dwz_file);
3081 result->dwz_bfd = dwz_bfd.release ();
3082
3083 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
3084
3085 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
3086 dwarf2_per_objfile->dwz_file = result;
3087 return result;
3088 }
3089 \f
3090 /* DWARF quick_symbols_functions support. */
3091
3092 /* TUs can share .debug_line entries, and there can be a lot more TUs than
3093 unique line tables, so we maintain a separate table of all .debug_line
3094 derived entries to support the sharing.
3095 All the quick functions need is the list of file names. We discard the
3096 line_header when we're done and don't need to record it here. */
3097 struct quick_file_names
3098 {
3099 /* The data used to construct the hash key. */
3100 struct stmt_list_hash hash;
3101
3102 /* The number of entries in file_names, real_names. */
3103 unsigned int num_file_names;
3104
3105 /* The file names from the line table, after being run through
3106 file_full_name. */
3107 const char **file_names;
3108
3109 /* The file names from the line table after being run through
3110 gdb_realpath. These are computed lazily. */
3111 const char **real_names;
3112 };
3113
3114 /* When using the index (and thus not using psymtabs), each CU has an
3115 object of this type. This is used to hold information needed by
3116 the various "quick" methods. */
3117 struct dwarf2_per_cu_quick_data
3118 {
3119 /* The file table. This can be NULL if there was no file table
3120 or it's currently not read in.
3121 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
3122 struct quick_file_names *file_names;
3123
3124 /* The corresponding symbol table. This is NULL if symbols for this
3125 CU have not yet been read. */
3126 struct compunit_symtab *compunit_symtab;
3127
3128 /* A temporary mark bit used when iterating over all CUs in
3129 expand_symtabs_matching. */
3130 unsigned int mark : 1;
3131
3132 /* True if we've tried to read the file table and found there isn't one.
3133 There will be no point in trying to read it again next time. */
3134 unsigned int no_file_data : 1;
3135 };
3136
3137 /* Utility hash function for a stmt_list_hash. */
3138
3139 static hashval_t
3140 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
3141 {
3142 hashval_t v = 0;
3143
3144 if (stmt_list_hash->dwo_unit != NULL)
3145 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
3146 v += to_underlying (stmt_list_hash->line_sect_off);
3147 return v;
3148 }
3149
3150 /* Utility equality function for a stmt_list_hash. */
3151
3152 static int
3153 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
3154 const struct stmt_list_hash *rhs)
3155 {
3156 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
3157 return 0;
3158 if (lhs->dwo_unit != NULL
3159 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
3160 return 0;
3161
3162 return lhs->line_sect_off == rhs->line_sect_off;
3163 }
3164
3165 /* Hash function for a quick_file_names. */
3166
3167 static hashval_t
3168 hash_file_name_entry (const void *e)
3169 {
3170 const struct quick_file_names *file_data
3171 = (const struct quick_file_names *) e;
3172
3173 return hash_stmt_list_entry (&file_data->hash);
3174 }
3175
3176 /* Equality function for a quick_file_names. */
3177
3178 static int
3179 eq_file_name_entry (const void *a, const void *b)
3180 {
3181 const struct quick_file_names *ea = (const struct quick_file_names *) a;
3182 const struct quick_file_names *eb = (const struct quick_file_names *) b;
3183
3184 return eq_stmt_list_entry (&ea->hash, &eb->hash);
3185 }
3186
3187 /* Delete function for a quick_file_names. */
3188
3189 static void
3190 delete_file_name_entry (void *e)
3191 {
3192 struct quick_file_names *file_data = (struct quick_file_names *) e;
3193 int i;
3194
3195 for (i = 0; i < file_data->num_file_names; ++i)
3196 {
3197 xfree ((void*) file_data->file_names[i]);
3198 if (file_data->real_names)
3199 xfree ((void*) file_data->real_names[i]);
3200 }
3201
3202 /* The space for the struct itself lives on objfile_obstack,
3203 so we don't free it here. */
3204 }
3205
3206 /* Create a quick_file_names hash table. */
3207
3208 static htab_t
3209 create_quick_file_names_table (unsigned int nr_initial_entries)
3210 {
3211 return htab_create_alloc (nr_initial_entries,
3212 hash_file_name_entry, eq_file_name_entry,
3213 delete_file_name_entry, xcalloc, xfree);
3214 }
3215
3216 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
3217 have to be created afterwards. You should call age_cached_comp_units after
3218 processing PER_CU->CU. dw2_setup must have been already called. */
3219
3220 static void
3221 load_cu (struct dwarf2_per_cu_data *per_cu)
3222 {
3223 if (per_cu->is_debug_types)
3224 load_full_type_unit (per_cu);
3225 else
3226 load_full_comp_unit (per_cu, language_minimal);
3227
3228 if (per_cu->cu == NULL)
3229 return; /* Dummy CU. */
3230
3231 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
3232 }
3233
3234 /* Read in the symbols for PER_CU. */
3235
3236 static void
3237 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3238 {
3239 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3240
3241 /* Skip type_unit_groups, reading the type units they contain
3242 is handled elsewhere. */
3243 if (IS_TYPE_UNIT_GROUP (per_cu))
3244 return;
3245
3246 /* The destructor of dwarf2_queue_guard frees any entries left on
3247 the queue. After this point we're guaranteed to leave this function
3248 with the dwarf queue empty. */
3249 dwarf2_queue_guard q_guard;
3250
3251 if (dwarf2_per_objfile->using_index
3252 ? per_cu->v.quick->compunit_symtab == NULL
3253 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
3254 {
3255 queue_comp_unit (per_cu, language_minimal);
3256 load_cu (per_cu);
3257
3258 /* If we just loaded a CU from a DWO, and we're working with an index
3259 that may badly handle TUs, load all the TUs in that DWO as well.
3260 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
3261 if (!per_cu->is_debug_types
3262 && per_cu->cu != NULL
3263 && per_cu->cu->dwo_unit != NULL
3264 && dwarf2_per_objfile->index_table != NULL
3265 && dwarf2_per_objfile->index_table->version <= 7
3266 /* DWP files aren't supported yet. */
3267 && get_dwp_file (dwarf2_per_objfile) == NULL)
3268 queue_and_load_all_dwo_tus (per_cu);
3269 }
3270
3271 process_queue (dwarf2_per_objfile);
3272
3273 /* Age the cache, releasing compilation units that have not
3274 been used recently. */
3275 age_cached_comp_units (dwarf2_per_objfile);
3276 }
3277
3278 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
3279 the objfile from which this CU came. Returns the resulting symbol
3280 table. */
3281
3282 static struct compunit_symtab *
3283 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
3284 {
3285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3286
3287 gdb_assert (dwarf2_per_objfile->using_index);
3288 if (!per_cu->v.quick->compunit_symtab)
3289 {
3290 struct cleanup *back_to = make_cleanup (free_cached_comp_units,
3291 dwarf2_per_objfile);
3292 scoped_restore decrementer = increment_reading_symtab ();
3293 dw2_do_instantiate_symtab (per_cu);
3294 process_cu_includes (dwarf2_per_objfile);
3295 do_cleanups (back_to);
3296 }
3297
3298 return per_cu->v.quick->compunit_symtab;
3299 }
3300
3301 /* Return the CU/TU given its index.
3302
3303 This is intended for loops like:
3304
3305 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3306 + dwarf2_per_objfile->n_type_units); ++i)
3307 {
3308 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3309
3310 ...;
3311 }
3312 */
3313
3314 static struct dwarf2_per_cu_data *
3315 dw2_get_cutu (struct dwarf2_per_objfile *dwarf2_per_objfile,
3316 int index)
3317 {
3318 if (index >= dwarf2_per_objfile->n_comp_units)
3319 {
3320 index -= dwarf2_per_objfile->n_comp_units;
3321 gdb_assert (index < dwarf2_per_objfile->n_type_units);
3322 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
3323 }
3324
3325 return dwarf2_per_objfile->all_comp_units[index];
3326 }
3327
3328 /* Return the CU given its index.
3329 This differs from dw2_get_cutu in that it's for when you know INDEX
3330 refers to a CU. */
3331
3332 static struct dwarf2_per_cu_data *
3333 dw2_get_cu (struct dwarf2_per_objfile *dwarf2_per_objfile, int index)
3334 {
3335 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
3336
3337 return dwarf2_per_objfile->all_comp_units[index];
3338 }
3339
3340 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3341 objfile_obstack, and constructed with the specified field
3342 values. */
3343
3344 static dwarf2_per_cu_data *
3345 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3346 struct dwarf2_section_info *section,
3347 int is_dwz,
3348 sect_offset sect_off, ULONGEST length)
3349 {
3350 struct objfile *objfile = dwarf2_per_objfile->objfile;
3351 dwarf2_per_cu_data *the_cu
3352 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3353 struct dwarf2_per_cu_data);
3354 the_cu->sect_off = sect_off;
3355 the_cu->length = length;
3356 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3357 the_cu->section = section;
3358 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3359 struct dwarf2_per_cu_quick_data);
3360 the_cu->is_dwz = is_dwz;
3361 return the_cu;
3362 }
3363
3364 /* A helper for create_cus_from_index that handles a given list of
3365 CUs. */
3366
3367 static void
3368 create_cus_from_index_list (struct objfile *objfile,
3369 const gdb_byte *cu_list, offset_type n_elements,
3370 struct dwarf2_section_info *section,
3371 int is_dwz,
3372 int base_offset)
3373 {
3374 offset_type i;
3375 struct dwarf2_per_objfile *dwarf2_per_objfile
3376 = get_dwarf2_per_objfile (objfile);
3377
3378 for (i = 0; i < n_elements; i += 2)
3379 {
3380 gdb_static_assert (sizeof (ULONGEST) >= 8);
3381
3382 sect_offset sect_off
3383 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3384 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3385 cu_list += 2 * 8;
3386
3387 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
3388 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3389 sect_off, length);
3390 }
3391 }
3392
3393 /* Read the CU list from the mapped index, and use it to create all
3394 the CU objects for this objfile. */
3395
3396 static void
3397 create_cus_from_index (struct objfile *objfile,
3398 const gdb_byte *cu_list, offset_type cu_list_elements,
3399 const gdb_byte *dwz_list, offset_type dwz_elements)
3400 {
3401 struct dwz_file *dwz;
3402 struct dwarf2_per_objfile *dwarf2_per_objfile
3403 = get_dwarf2_per_objfile (objfile);
3404
3405 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3406 dwarf2_per_objfile->all_comp_units =
3407 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3408 dwarf2_per_objfile->n_comp_units);
3409
3410 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
3411 &dwarf2_per_objfile->info, 0, 0);
3412
3413 if (dwz_elements == 0)
3414 return;
3415
3416 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3417 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
3418 cu_list_elements / 2);
3419 }
3420
3421 /* Create the signatured type hash table from the index. */
3422
3423 static void
3424 create_signatured_type_table_from_index (struct objfile *objfile,
3425 struct dwarf2_section_info *section,
3426 const gdb_byte *bytes,
3427 offset_type elements)
3428 {
3429 offset_type i;
3430 htab_t sig_types_hash;
3431 struct dwarf2_per_objfile *dwarf2_per_objfile
3432 = get_dwarf2_per_objfile (objfile);
3433
3434 dwarf2_per_objfile->n_type_units
3435 = dwarf2_per_objfile->n_allocated_type_units
3436 = elements / 3;
3437 dwarf2_per_objfile->all_type_units =
3438 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3439
3440 sig_types_hash = allocate_signatured_type_table (objfile);
3441
3442 for (i = 0; i < elements; i += 3)
3443 {
3444 struct signatured_type *sig_type;
3445 ULONGEST signature;
3446 void **slot;
3447 cu_offset type_offset_in_tu;
3448
3449 gdb_static_assert (sizeof (ULONGEST) >= 8);
3450 sect_offset sect_off
3451 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3452 type_offset_in_tu
3453 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3454 BFD_ENDIAN_LITTLE);
3455 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3456 bytes += 3 * 8;
3457
3458 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3459 struct signatured_type);
3460 sig_type->signature = signature;
3461 sig_type->type_offset_in_tu = type_offset_in_tu;
3462 sig_type->per_cu.is_debug_types = 1;
3463 sig_type->per_cu.section = section;
3464 sig_type->per_cu.sect_off = sect_off;
3465 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3466 sig_type->per_cu.v.quick
3467 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3468 struct dwarf2_per_cu_quick_data);
3469
3470 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3471 *slot = sig_type;
3472
3473 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3474 }
3475
3476 dwarf2_per_objfile->signatured_types = sig_types_hash;
3477 }
3478
3479 /* Create the signatured type hash table from .debug_names. */
3480
3481 static void
3482 create_signatured_type_table_from_debug_names
3483 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3484 const mapped_debug_names &map,
3485 struct dwarf2_section_info *section,
3486 struct dwarf2_section_info *abbrev_section)
3487 {
3488 struct objfile *objfile = dwarf2_per_objfile->objfile;
3489
3490 dwarf2_read_section (objfile, section);
3491 dwarf2_read_section (objfile, abbrev_section);
3492
3493 dwarf2_per_objfile->n_type_units
3494 = dwarf2_per_objfile->n_allocated_type_units
3495 = map.tu_count;
3496 dwarf2_per_objfile->all_type_units
3497 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3498
3499 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3500
3501 for (uint32_t i = 0; i < map.tu_count; ++i)
3502 {
3503 struct signatured_type *sig_type;
3504 ULONGEST signature;
3505 void **slot;
3506 cu_offset type_offset_in_tu;
3507
3508 sect_offset sect_off
3509 = (sect_offset) (extract_unsigned_integer
3510 (map.tu_table_reordered + i * map.offset_size,
3511 map.offset_size,
3512 map.dwarf5_byte_order));
3513
3514 comp_unit_head cu_header;
3515 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3516 abbrev_section,
3517 section->buffer + to_underlying (sect_off),
3518 rcuh_kind::TYPE);
3519
3520 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3521 struct signatured_type);
3522 sig_type->signature = cu_header.signature;
3523 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3524 sig_type->per_cu.is_debug_types = 1;
3525 sig_type->per_cu.section = section;
3526 sig_type->per_cu.sect_off = sect_off;
3527 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3528 sig_type->per_cu.v.quick
3529 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3530 struct dwarf2_per_cu_quick_data);
3531
3532 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3533 *slot = sig_type;
3534
3535 dwarf2_per_objfile->all_type_units[i] = sig_type;
3536 }
3537
3538 dwarf2_per_objfile->signatured_types = sig_types_hash;
3539 }
3540
3541 /* Read the address map data from the mapped index, and use it to
3542 populate the objfile's psymtabs_addrmap. */
3543
3544 static void
3545 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3546 struct mapped_index *index)
3547 {
3548 struct objfile *objfile = dwarf2_per_objfile->objfile;
3549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3550 const gdb_byte *iter, *end;
3551 struct addrmap *mutable_map;
3552 CORE_ADDR baseaddr;
3553
3554 auto_obstack temp_obstack;
3555
3556 mutable_map = addrmap_create_mutable (&temp_obstack);
3557
3558 iter = index->address_table.data ();
3559 end = iter + index->address_table.size ();
3560
3561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3562
3563 while (iter < end)
3564 {
3565 ULONGEST hi, lo, cu_index;
3566 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3567 iter += 8;
3568 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3569 iter += 8;
3570 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3571 iter += 4;
3572
3573 if (lo > hi)
3574 {
3575 complaint (&symfile_complaints,
3576 _(".gdb_index address table has invalid range (%s - %s)"),
3577 hex_string (lo), hex_string (hi));
3578 continue;
3579 }
3580
3581 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3582 {
3583 complaint (&symfile_complaints,
3584 _(".gdb_index address table has invalid CU number %u"),
3585 (unsigned) cu_index);
3586 continue;
3587 }
3588
3589 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3590 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3591 addrmap_set_empty (mutable_map, lo, hi - 1,
3592 dw2_get_cutu (dwarf2_per_objfile, cu_index));
3593 }
3594
3595 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3596 &objfile->objfile_obstack);
3597 }
3598
3599 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3600 populate the objfile's psymtabs_addrmap. */
3601
3602 static void
3603 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3604 struct dwarf2_section_info *section)
3605 {
3606 struct objfile *objfile = dwarf2_per_objfile->objfile;
3607 bfd *abfd = objfile->obfd;
3608 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3609 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3610 SECT_OFF_TEXT (objfile));
3611
3612 auto_obstack temp_obstack;
3613 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3614
3615 std::unordered_map<sect_offset,
3616 dwarf2_per_cu_data *,
3617 gdb::hash_enum<sect_offset>>
3618 debug_info_offset_to_per_cu;
3619 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3620 {
3621 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, cui);
3622 const auto insertpair
3623 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3624 if (!insertpair.second)
3625 {
3626 warning (_("Section .debug_aranges in %s has duplicate "
3627 "debug_info_offset %s, ignoring .debug_aranges."),
3628 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3629 return;
3630 }
3631 }
3632
3633 dwarf2_read_section (objfile, section);
3634
3635 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3636
3637 const gdb_byte *addr = section->buffer;
3638
3639 while (addr < section->buffer + section->size)
3640 {
3641 const gdb_byte *const entry_addr = addr;
3642 unsigned int bytes_read;
3643
3644 const LONGEST entry_length = read_initial_length (abfd, addr,
3645 &bytes_read);
3646 addr += bytes_read;
3647
3648 const gdb_byte *const entry_end = addr + entry_length;
3649 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3650 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3651 if (addr + entry_length > section->buffer + section->size)
3652 {
3653 warning (_("Section .debug_aranges in %s entry at offset %zu "
3654 "length %s exceeds section length %s, "
3655 "ignoring .debug_aranges."),
3656 objfile_name (objfile), entry_addr - section->buffer,
3657 plongest (bytes_read + entry_length),
3658 pulongest (section->size));
3659 return;
3660 }
3661
3662 /* The version number. */
3663 const uint16_t version = read_2_bytes (abfd, addr);
3664 addr += 2;
3665 if (version != 2)
3666 {
3667 warning (_("Section .debug_aranges in %s entry at offset %zu "
3668 "has unsupported version %d, ignoring .debug_aranges."),
3669 objfile_name (objfile), entry_addr - section->buffer,
3670 version);
3671 return;
3672 }
3673
3674 const uint64_t debug_info_offset
3675 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3676 addr += offset_size;
3677 const auto per_cu_it
3678 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3679 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3680 {
3681 warning (_("Section .debug_aranges in %s entry at offset %zu "
3682 "debug_info_offset %s does not exists, "
3683 "ignoring .debug_aranges."),
3684 objfile_name (objfile), entry_addr - section->buffer,
3685 pulongest (debug_info_offset));
3686 return;
3687 }
3688 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3689
3690 const uint8_t address_size = *addr++;
3691 if (address_size < 1 || address_size > 8)
3692 {
3693 warning (_("Section .debug_aranges in %s entry at offset %zu "
3694 "address_size %u is invalid, ignoring .debug_aranges."),
3695 objfile_name (objfile), entry_addr - section->buffer,
3696 address_size);
3697 return;
3698 }
3699
3700 const uint8_t segment_selector_size = *addr++;
3701 if (segment_selector_size != 0)
3702 {
3703 warning (_("Section .debug_aranges in %s entry at offset %zu "
3704 "segment_selector_size %u is not supported, "
3705 "ignoring .debug_aranges."),
3706 objfile_name (objfile), entry_addr - section->buffer,
3707 segment_selector_size);
3708 return;
3709 }
3710
3711 /* Must pad to an alignment boundary that is twice the address
3712 size. It is undocumented by the DWARF standard but GCC does
3713 use it. */
3714 for (size_t padding = ((-(addr - section->buffer))
3715 & (2 * address_size - 1));
3716 padding > 0; padding--)
3717 if (*addr++ != 0)
3718 {
3719 warning (_("Section .debug_aranges in %s entry at offset %zu "
3720 "padding is not zero, ignoring .debug_aranges."),
3721 objfile_name (objfile), entry_addr - section->buffer);
3722 return;
3723 }
3724
3725 for (;;)
3726 {
3727 if (addr + 2 * address_size > entry_end)
3728 {
3729 warning (_("Section .debug_aranges in %s entry at offset %zu "
3730 "address list is not properly terminated, "
3731 "ignoring .debug_aranges."),
3732 objfile_name (objfile), entry_addr - section->buffer);
3733 return;
3734 }
3735 ULONGEST start = extract_unsigned_integer (addr, address_size,
3736 dwarf5_byte_order);
3737 addr += address_size;
3738 ULONGEST length = extract_unsigned_integer (addr, address_size,
3739 dwarf5_byte_order);
3740 addr += address_size;
3741 if (start == 0 && length == 0)
3742 break;
3743 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3744 {
3745 /* Symbol was eliminated due to a COMDAT group. */
3746 continue;
3747 }
3748 ULONGEST end = start + length;
3749 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3750 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3751 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3752 }
3753 }
3754
3755 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3756 &objfile->objfile_obstack);
3757 }
3758
3759 /* The hash function for strings in the mapped index. This is the same as
3760 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3761 implementation. This is necessary because the hash function is tied to the
3762 format of the mapped index file. The hash values do not have to match with
3763 SYMBOL_HASH_NEXT.
3764
3765 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3766
3767 static hashval_t
3768 mapped_index_string_hash (int index_version, const void *p)
3769 {
3770 const unsigned char *str = (const unsigned char *) p;
3771 hashval_t r = 0;
3772 unsigned char c;
3773
3774 while ((c = *str++) != 0)
3775 {
3776 if (index_version >= 5)
3777 c = tolower (c);
3778 r = r * 67 + c - 113;
3779 }
3780
3781 return r;
3782 }
3783
3784 /* Find a slot in the mapped index INDEX for the object named NAME.
3785 If NAME is found, set *VEC_OUT to point to the CU vector in the
3786 constant pool and return true. If NAME cannot be found, return
3787 false. */
3788
3789 static bool
3790 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3791 offset_type **vec_out)
3792 {
3793 offset_type hash;
3794 offset_type slot, step;
3795 int (*cmp) (const char *, const char *);
3796
3797 gdb::unique_xmalloc_ptr<char> without_params;
3798 if (current_language->la_language == language_cplus
3799 || current_language->la_language == language_fortran
3800 || current_language->la_language == language_d)
3801 {
3802 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3803 not contain any. */
3804
3805 if (strchr (name, '(') != NULL)
3806 {
3807 without_params = cp_remove_params (name);
3808
3809 if (without_params != NULL)
3810 name = without_params.get ();
3811 }
3812 }
3813
3814 /* Index version 4 did not support case insensitive searches. But the
3815 indices for case insensitive languages are built in lowercase, therefore
3816 simulate our NAME being searched is also lowercased. */
3817 hash = mapped_index_string_hash ((index->version == 4
3818 && case_sensitivity == case_sensitive_off
3819 ? 5 : index->version),
3820 name);
3821
3822 slot = hash & (index->symbol_table.size () - 1);
3823 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3824 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3825
3826 for (;;)
3827 {
3828 const char *str;
3829
3830 const auto &bucket = index->symbol_table[slot];
3831 if (bucket.name == 0 && bucket.vec == 0)
3832 return false;
3833
3834 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3835 if (!cmp (name, str))
3836 {
3837 *vec_out = (offset_type *) (index->constant_pool
3838 + MAYBE_SWAP (bucket.vec));
3839 return true;
3840 }
3841
3842 slot = (slot + step) & (index->symbol_table.size () - 1);
3843 }
3844 }
3845
3846 /* A helper function that reads the .gdb_index from SECTION and fills
3847 in MAP. FILENAME is the name of the file containing the section;
3848 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3849 ok to use deprecated sections.
3850
3851 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3852 out parameters that are filled in with information about the CU and
3853 TU lists in the section.
3854
3855 Returns 1 if all went well, 0 otherwise. */
3856
3857 static int
3858 read_index_from_section (struct objfile *objfile,
3859 const char *filename,
3860 int deprecated_ok,
3861 struct dwarf2_section_info *section,
3862 struct mapped_index *map,
3863 const gdb_byte **cu_list,
3864 offset_type *cu_list_elements,
3865 const gdb_byte **types_list,
3866 offset_type *types_list_elements)
3867 {
3868 const gdb_byte *addr;
3869 offset_type version;
3870 offset_type *metadata;
3871 int i;
3872
3873 if (dwarf2_section_empty_p (section))
3874 return 0;
3875
3876 /* Older elfutils strip versions could keep the section in the main
3877 executable while splitting it for the separate debug info file. */
3878 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3879 return 0;
3880
3881 dwarf2_read_section (objfile, section);
3882
3883 addr = section->buffer;
3884 /* Version check. */
3885 version = MAYBE_SWAP (*(offset_type *) addr);
3886 /* Versions earlier than 3 emitted every copy of a psymbol. This
3887 causes the index to behave very poorly for certain requests. Version 3
3888 contained incomplete addrmap. So, it seems better to just ignore such
3889 indices. */
3890 if (version < 4)
3891 {
3892 static int warning_printed = 0;
3893 if (!warning_printed)
3894 {
3895 warning (_("Skipping obsolete .gdb_index section in %s."),
3896 filename);
3897 warning_printed = 1;
3898 }
3899 return 0;
3900 }
3901 /* Index version 4 uses a different hash function than index version
3902 5 and later.
3903
3904 Versions earlier than 6 did not emit psymbols for inlined
3905 functions. Using these files will cause GDB not to be able to
3906 set breakpoints on inlined functions by name, so we ignore these
3907 indices unless the user has done
3908 "set use-deprecated-index-sections on". */
3909 if (version < 6 && !deprecated_ok)
3910 {
3911 static int warning_printed = 0;
3912 if (!warning_printed)
3913 {
3914 warning (_("\
3915 Skipping deprecated .gdb_index section in %s.\n\
3916 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3917 to use the section anyway."),
3918 filename);
3919 warning_printed = 1;
3920 }
3921 return 0;
3922 }
3923 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3924 of the TU (for symbols coming from TUs),
3925 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3926 Plus gold-generated indices can have duplicate entries for global symbols,
3927 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3928 These are just performance bugs, and we can't distinguish gdb-generated
3929 indices from gold-generated ones, so issue no warning here. */
3930
3931 /* Indexes with higher version than the one supported by GDB may be no
3932 longer backward compatible. */
3933 if (version > 8)
3934 return 0;
3935
3936 map->version = version;
3937 map->total_size = section->size;
3938
3939 metadata = (offset_type *) (addr + sizeof (offset_type));
3940
3941 i = 0;
3942 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3943 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3944 / 8);
3945 ++i;
3946
3947 *types_list = addr + MAYBE_SWAP (metadata[i]);
3948 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3949 - MAYBE_SWAP (metadata[i]))
3950 / 8);
3951 ++i;
3952
3953 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3954 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3955 map->address_table
3956 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3957 ++i;
3958
3959 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3960 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3961 map->symbol_table
3962 = gdb::array_view<mapped_index::symbol_table_slot>
3963 ((mapped_index::symbol_table_slot *) symbol_table,
3964 (mapped_index::symbol_table_slot *) symbol_table_end);
3965
3966 ++i;
3967 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3968
3969 return 1;
3970 }
3971
3972 /* Read .gdb_index. If everything went ok, initialize the "quick"
3973 elements of all the CUs and return 1. Otherwise, return 0. */
3974
3975 static int
3976 dwarf2_read_index (struct objfile *objfile)
3977 {
3978 struct mapped_index local_map, *map;
3979 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3980 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3981 struct dwz_file *dwz;
3982 struct dwarf2_per_objfile *dwarf2_per_objfile
3983 = get_dwarf2_per_objfile (objfile);
3984
3985 if (!read_index_from_section (objfile, objfile_name (objfile),
3986 use_deprecated_index_sections,
3987 &dwarf2_per_objfile->gdb_index, &local_map,
3988 &cu_list, &cu_list_elements,
3989 &types_list, &types_list_elements))
3990 return 0;
3991
3992 /* Don't use the index if it's empty. */
3993 if (local_map.symbol_table.empty ())
3994 return 0;
3995
3996 /* If there is a .dwz file, read it so we can get its CU list as
3997 well. */
3998 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3999 if (dwz != NULL)
4000 {
4001 struct mapped_index dwz_map;
4002 const gdb_byte *dwz_types_ignore;
4003 offset_type dwz_types_elements_ignore;
4004
4005 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
4006 1,
4007 &dwz->gdb_index, &dwz_map,
4008 &dwz_list, &dwz_list_elements,
4009 &dwz_types_ignore,
4010 &dwz_types_elements_ignore))
4011 {
4012 warning (_("could not read '.gdb_index' section from %s; skipping"),
4013 bfd_get_filename (dwz->dwz_bfd));
4014 return 0;
4015 }
4016 }
4017
4018 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
4019 dwz_list_elements);
4020
4021 if (types_list_elements)
4022 {
4023 struct dwarf2_section_info *section;
4024
4025 /* We can only handle a single .debug_types when we have an
4026 index. */
4027 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
4028 return 0;
4029
4030 section = VEC_index (dwarf2_section_info_def,
4031 dwarf2_per_objfile->types, 0);
4032
4033 create_signatured_type_table_from_index (objfile, section, types_list,
4034 types_list_elements);
4035 }
4036
4037 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
4038
4039 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
4040 map = new (map) mapped_index ();
4041 *map = local_map;
4042
4043 dwarf2_per_objfile->index_table = map;
4044 dwarf2_per_objfile->using_index = 1;
4045 dwarf2_per_objfile->quick_file_names_table =
4046 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4047
4048 return 1;
4049 }
4050
4051 /* die_reader_func for dw2_get_file_names. */
4052
4053 static void
4054 dw2_get_file_names_reader (const struct die_reader_specs *reader,
4055 const gdb_byte *info_ptr,
4056 struct die_info *comp_unit_die,
4057 int has_children,
4058 void *data)
4059 {
4060 struct dwarf2_cu *cu = reader->cu;
4061 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
4062 struct dwarf2_per_objfile *dwarf2_per_objfile
4063 = cu->per_cu->dwarf2_per_objfile;
4064 struct objfile *objfile = dwarf2_per_objfile->objfile;
4065 struct dwarf2_per_cu_data *lh_cu;
4066 struct attribute *attr;
4067 int i;
4068 void **slot;
4069 struct quick_file_names *qfn;
4070
4071 gdb_assert (! this_cu->is_debug_types);
4072
4073 /* Our callers never want to match partial units -- instead they
4074 will match the enclosing full CU. */
4075 if (comp_unit_die->tag == DW_TAG_partial_unit)
4076 {
4077 this_cu->v.quick->no_file_data = 1;
4078 return;
4079 }
4080
4081 lh_cu = this_cu;
4082 slot = NULL;
4083
4084 line_header_up lh;
4085 sect_offset line_offset {};
4086
4087 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4088 if (attr)
4089 {
4090 struct quick_file_names find_entry;
4091
4092 line_offset = (sect_offset) DW_UNSND (attr);
4093
4094 /* We may have already read in this line header (TU line header sharing).
4095 If we have we're done. */
4096 find_entry.hash.dwo_unit = cu->dwo_unit;
4097 find_entry.hash.line_sect_off = line_offset;
4098 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
4099 &find_entry, INSERT);
4100 if (*slot != NULL)
4101 {
4102 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
4103 return;
4104 }
4105
4106 lh = dwarf_decode_line_header (line_offset, cu);
4107 }
4108 if (lh == NULL)
4109 {
4110 lh_cu->v.quick->no_file_data = 1;
4111 return;
4112 }
4113
4114 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
4115 qfn->hash.dwo_unit = cu->dwo_unit;
4116 qfn->hash.line_sect_off = line_offset;
4117 gdb_assert (slot != NULL);
4118 *slot = qfn;
4119
4120 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
4121
4122 qfn->num_file_names = lh->file_names.size ();
4123 qfn->file_names =
4124 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
4125 for (i = 0; i < lh->file_names.size (); ++i)
4126 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
4127 qfn->real_names = NULL;
4128
4129 lh_cu->v.quick->file_names = qfn;
4130 }
4131
4132 /* A helper for the "quick" functions which attempts to read the line
4133 table for THIS_CU. */
4134
4135 static struct quick_file_names *
4136 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
4137 {
4138 /* This should never be called for TUs. */
4139 gdb_assert (! this_cu->is_debug_types);
4140 /* Nor type unit groups. */
4141 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
4142
4143 if (this_cu->v.quick->file_names != NULL)
4144 return this_cu->v.quick->file_names;
4145 /* If we know there is no line data, no point in looking again. */
4146 if (this_cu->v.quick->no_file_data)
4147 return NULL;
4148
4149 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
4150
4151 if (this_cu->v.quick->no_file_data)
4152 return NULL;
4153 return this_cu->v.quick->file_names;
4154 }
4155
4156 /* A helper for the "quick" functions which computes and caches the
4157 real path for a given file name from the line table. */
4158
4159 static const char *
4160 dw2_get_real_path (struct objfile *objfile,
4161 struct quick_file_names *qfn, int index)
4162 {
4163 if (qfn->real_names == NULL)
4164 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
4165 qfn->num_file_names, const char *);
4166
4167 if (qfn->real_names[index] == NULL)
4168 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
4169
4170 return qfn->real_names[index];
4171 }
4172
4173 static struct symtab *
4174 dw2_find_last_source_symtab (struct objfile *objfile)
4175 {
4176 struct dwarf2_per_objfile *dwarf2_per_objfile
4177 = get_dwarf2_per_objfile (objfile);
4178 int index = dwarf2_per_objfile->n_comp_units - 1;
4179 dwarf2_per_cu_data *dwarf_cu = dw2_get_cutu (dwarf2_per_objfile, index);
4180 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
4181
4182 if (cust == NULL)
4183 return NULL;
4184
4185 return compunit_primary_filetab (cust);
4186 }
4187
4188 /* Traversal function for dw2_forget_cached_source_info. */
4189
4190 static int
4191 dw2_free_cached_file_names (void **slot, void *info)
4192 {
4193 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
4194
4195 if (file_data->real_names)
4196 {
4197 int i;
4198
4199 for (i = 0; i < file_data->num_file_names; ++i)
4200 {
4201 xfree ((void*) file_data->real_names[i]);
4202 file_data->real_names[i] = NULL;
4203 }
4204 }
4205
4206 return 1;
4207 }
4208
4209 static void
4210 dw2_forget_cached_source_info (struct objfile *objfile)
4211 {
4212 struct dwarf2_per_objfile *dwarf2_per_objfile
4213 = get_dwarf2_per_objfile (objfile);
4214
4215 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
4216 dw2_free_cached_file_names, NULL);
4217 }
4218
4219 /* Helper function for dw2_map_symtabs_matching_filename that expands
4220 the symtabs and calls the iterator. */
4221
4222 static int
4223 dw2_map_expand_apply (struct objfile *objfile,
4224 struct dwarf2_per_cu_data *per_cu,
4225 const char *name, const char *real_path,
4226 gdb::function_view<bool (symtab *)> callback)
4227 {
4228 struct compunit_symtab *last_made = objfile->compunit_symtabs;
4229
4230 /* Don't visit already-expanded CUs. */
4231 if (per_cu->v.quick->compunit_symtab)
4232 return 0;
4233
4234 /* This may expand more than one symtab, and we want to iterate over
4235 all of them. */
4236 dw2_instantiate_symtab (per_cu);
4237
4238 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
4239 last_made, callback);
4240 }
4241
4242 /* Implementation of the map_symtabs_matching_filename method. */
4243
4244 static bool
4245 dw2_map_symtabs_matching_filename
4246 (struct objfile *objfile, const char *name, const char *real_path,
4247 gdb::function_view<bool (symtab *)> callback)
4248 {
4249 int i;
4250 const char *name_basename = lbasename (name);
4251 struct dwarf2_per_objfile *dwarf2_per_objfile
4252 = get_dwarf2_per_objfile (objfile);
4253
4254 /* The rule is CUs specify all the files, including those used by
4255 any TU, so there's no need to scan TUs here. */
4256
4257 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4258 {
4259 int j;
4260 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
4261 struct quick_file_names *file_data;
4262
4263 /* We only need to look at symtabs not already expanded. */
4264 if (per_cu->v.quick->compunit_symtab)
4265 continue;
4266
4267 file_data = dw2_get_file_names (per_cu);
4268 if (file_data == NULL)
4269 continue;
4270
4271 for (j = 0; j < file_data->num_file_names; ++j)
4272 {
4273 const char *this_name = file_data->file_names[j];
4274 const char *this_real_name;
4275
4276 if (compare_filenames_for_search (this_name, name))
4277 {
4278 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4279 callback))
4280 return true;
4281 continue;
4282 }
4283
4284 /* Before we invoke realpath, which can get expensive when many
4285 files are involved, do a quick comparison of the basenames. */
4286 if (! basenames_may_differ
4287 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
4288 continue;
4289
4290 this_real_name = dw2_get_real_path (objfile, file_data, j);
4291 if (compare_filenames_for_search (this_real_name, name))
4292 {
4293 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4294 callback))
4295 return true;
4296 continue;
4297 }
4298
4299 if (real_path != NULL)
4300 {
4301 gdb_assert (IS_ABSOLUTE_PATH (real_path));
4302 gdb_assert (IS_ABSOLUTE_PATH (name));
4303 if (this_real_name != NULL
4304 && FILENAME_CMP (real_path, this_real_name) == 0)
4305 {
4306 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
4307 callback))
4308 return true;
4309 continue;
4310 }
4311 }
4312 }
4313 }
4314
4315 return false;
4316 }
4317
4318 /* Struct used to manage iterating over all CUs looking for a symbol. */
4319
4320 struct dw2_symtab_iterator
4321 {
4322 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
4323 struct dwarf2_per_objfile *dwarf2_per_objfile;
4324 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
4325 int want_specific_block;
4326 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
4327 Unused if !WANT_SPECIFIC_BLOCK. */
4328 int block_index;
4329 /* The kind of symbol we're looking for. */
4330 domain_enum domain;
4331 /* The list of CUs from the index entry of the symbol,
4332 or NULL if not found. */
4333 offset_type *vec;
4334 /* The next element in VEC to look at. */
4335 int next;
4336 /* The number of elements in VEC, or zero if there is no match. */
4337 int length;
4338 /* Have we seen a global version of the symbol?
4339 If so we can ignore all further global instances.
4340 This is to work around gold/15646, inefficient gold-generated
4341 indices. */
4342 int global_seen;
4343 };
4344
4345 /* Initialize the index symtab iterator ITER.
4346 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
4347 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
4348
4349 static void
4350 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
4351 struct dwarf2_per_objfile *dwarf2_per_objfile,
4352 int want_specific_block,
4353 int block_index,
4354 domain_enum domain,
4355 const char *name)
4356 {
4357 iter->dwarf2_per_objfile = dwarf2_per_objfile;
4358 iter->want_specific_block = want_specific_block;
4359 iter->block_index = block_index;
4360 iter->domain = domain;
4361 iter->next = 0;
4362 iter->global_seen = 0;
4363
4364 mapped_index *index = dwarf2_per_objfile->index_table;
4365
4366 /* index is NULL if OBJF_READNOW. */
4367 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
4368 iter->length = MAYBE_SWAP (*iter->vec);
4369 else
4370 {
4371 iter->vec = NULL;
4372 iter->length = 0;
4373 }
4374 }
4375
4376 /* Return the next matching CU or NULL if there are no more. */
4377
4378 static struct dwarf2_per_cu_data *
4379 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4380 {
4381 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4382
4383 for ( ; iter->next < iter->length; ++iter->next)
4384 {
4385 offset_type cu_index_and_attrs =
4386 MAYBE_SWAP (iter->vec[iter->next + 1]);
4387 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4388 struct dwarf2_per_cu_data *per_cu;
4389 int want_static = iter->block_index != GLOBAL_BLOCK;
4390 /* This value is only valid for index versions >= 7. */
4391 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4392 gdb_index_symbol_kind symbol_kind =
4393 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4394 /* Only check the symbol attributes if they're present.
4395 Indices prior to version 7 don't record them,
4396 and indices >= 7 may elide them for certain symbols
4397 (gold does this). */
4398 int attrs_valid =
4399 (dwarf2_per_objfile->index_table->version >= 7
4400 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4401
4402 /* Don't crash on bad data. */
4403 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4404 + dwarf2_per_objfile->n_type_units))
4405 {
4406 complaint (&symfile_complaints,
4407 _(".gdb_index entry has bad CU index"
4408 " [in module %s]"),
4409 objfile_name (dwarf2_per_objfile->objfile));
4410 continue;
4411 }
4412
4413 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
4414
4415 /* Skip if already read in. */
4416 if (per_cu->v.quick->compunit_symtab)
4417 continue;
4418
4419 /* Check static vs global. */
4420 if (attrs_valid)
4421 {
4422 if (iter->want_specific_block
4423 && want_static != is_static)
4424 continue;
4425 /* Work around gold/15646. */
4426 if (!is_static && iter->global_seen)
4427 continue;
4428 if (!is_static)
4429 iter->global_seen = 1;
4430 }
4431
4432 /* Only check the symbol's kind if it has one. */
4433 if (attrs_valid)
4434 {
4435 switch (iter->domain)
4436 {
4437 case VAR_DOMAIN:
4438 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4439 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4440 /* Some types are also in VAR_DOMAIN. */
4441 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4442 continue;
4443 break;
4444 case STRUCT_DOMAIN:
4445 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4446 continue;
4447 break;
4448 case LABEL_DOMAIN:
4449 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4450 continue;
4451 break;
4452 default:
4453 break;
4454 }
4455 }
4456
4457 ++iter->next;
4458 return per_cu;
4459 }
4460
4461 return NULL;
4462 }
4463
4464 static struct compunit_symtab *
4465 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4466 const char *name, domain_enum domain)
4467 {
4468 struct compunit_symtab *stab_best = NULL;
4469 struct dwarf2_per_objfile *dwarf2_per_objfile
4470 = get_dwarf2_per_objfile (objfile);
4471
4472 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4473
4474 struct dw2_symtab_iterator iter;
4475 struct dwarf2_per_cu_data *per_cu;
4476
4477 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4478
4479 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4480 {
4481 struct symbol *sym, *with_opaque = NULL;
4482 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4483 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4484 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4485
4486 sym = block_find_symbol (block, name, domain,
4487 block_find_non_opaque_type_preferred,
4488 &with_opaque);
4489
4490 /* Some caution must be observed with overloaded functions
4491 and methods, since the index will not contain any overload
4492 information (but NAME might contain it). */
4493
4494 if (sym != NULL
4495 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4496 return stab;
4497 if (with_opaque != NULL
4498 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4499 stab_best = stab;
4500
4501 /* Keep looking through other CUs. */
4502 }
4503
4504 return stab_best;
4505 }
4506
4507 static void
4508 dw2_print_stats (struct objfile *objfile)
4509 {
4510 struct dwarf2_per_objfile *dwarf2_per_objfile
4511 = get_dwarf2_per_objfile (objfile);
4512 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4513 int count = 0;
4514
4515 for (int i = 0; i < total; ++i)
4516 {
4517 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4518
4519 if (!per_cu->v.quick->compunit_symtab)
4520 ++count;
4521 }
4522 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4523 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4524 }
4525
4526 /* This dumps minimal information about the index.
4527 It is called via "mt print objfiles".
4528 One use is to verify .gdb_index has been loaded by the
4529 gdb.dwarf2/gdb-index.exp testcase. */
4530
4531 static void
4532 dw2_dump (struct objfile *objfile)
4533 {
4534 struct dwarf2_per_objfile *dwarf2_per_objfile
4535 = get_dwarf2_per_objfile (objfile);
4536
4537 gdb_assert (dwarf2_per_objfile->using_index);
4538 printf_filtered (".gdb_index:");
4539 if (dwarf2_per_objfile->index_table != NULL)
4540 {
4541 printf_filtered (" version %d\n",
4542 dwarf2_per_objfile->index_table->version);
4543 }
4544 else
4545 printf_filtered (" faked for \"readnow\"\n");
4546 printf_filtered ("\n");
4547 }
4548
4549 static void
4550 dw2_relocate (struct objfile *objfile,
4551 const struct section_offsets *new_offsets,
4552 const struct section_offsets *delta)
4553 {
4554 /* There's nothing to relocate here. */
4555 }
4556
4557 static void
4558 dw2_expand_symtabs_for_function (struct objfile *objfile,
4559 const char *func_name)
4560 {
4561 struct dwarf2_per_objfile *dwarf2_per_objfile
4562 = get_dwarf2_per_objfile (objfile);
4563
4564 struct dw2_symtab_iterator iter;
4565 struct dwarf2_per_cu_data *per_cu;
4566
4567 /* Note: It doesn't matter what we pass for block_index here. */
4568 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4569 func_name);
4570
4571 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4572 dw2_instantiate_symtab (per_cu);
4573
4574 }
4575
4576 static void
4577 dw2_expand_all_symtabs (struct objfile *objfile)
4578 {
4579 struct dwarf2_per_objfile *dwarf2_per_objfile
4580 = get_dwarf2_per_objfile (objfile);
4581 int total_units = (dwarf2_per_objfile->n_comp_units
4582 + dwarf2_per_objfile->n_type_units);
4583
4584 for (int i = 0; i < total_units; ++i)
4585 {
4586 struct dwarf2_per_cu_data *per_cu
4587 = dw2_get_cutu (dwarf2_per_objfile, i);
4588
4589 dw2_instantiate_symtab (per_cu);
4590 }
4591 }
4592
4593 static void
4594 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4595 const char *fullname)
4596 {
4597 struct dwarf2_per_objfile *dwarf2_per_objfile
4598 = get_dwarf2_per_objfile (objfile);
4599
4600 /* We don't need to consider type units here.
4601 This is only called for examining code, e.g. expand_line_sal.
4602 There can be an order of magnitude (or more) more type units
4603 than comp units, and we avoid them if we can. */
4604
4605 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4606 {
4607 int j;
4608 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
4609 struct quick_file_names *file_data;
4610
4611 /* We only need to look at symtabs not already expanded. */
4612 if (per_cu->v.quick->compunit_symtab)
4613 continue;
4614
4615 file_data = dw2_get_file_names (per_cu);
4616 if (file_data == NULL)
4617 continue;
4618
4619 for (j = 0; j < file_data->num_file_names; ++j)
4620 {
4621 const char *this_fullname = file_data->file_names[j];
4622
4623 if (filename_cmp (this_fullname, fullname) == 0)
4624 {
4625 dw2_instantiate_symtab (per_cu);
4626 break;
4627 }
4628 }
4629 }
4630 }
4631
4632 static void
4633 dw2_map_matching_symbols (struct objfile *objfile,
4634 const char * name, domain_enum domain,
4635 int global,
4636 int (*callback) (struct block *,
4637 struct symbol *, void *),
4638 void *data, symbol_name_match_type match,
4639 symbol_compare_ftype *ordered_compare)
4640 {
4641 /* Currently unimplemented; used for Ada. The function can be called if the
4642 current language is Ada for a non-Ada objfile using GNU index. As Ada
4643 does not look for non-Ada symbols this function should just return. */
4644 }
4645
4646 /* Symbol name matcher for .gdb_index names.
4647
4648 Symbol names in .gdb_index have a few particularities:
4649
4650 - There's no indication of which is the language of each symbol.
4651
4652 Since each language has its own symbol name matching algorithm,
4653 and we don't know which language is the right one, we must match
4654 each symbol against all languages. This would be a potential
4655 performance problem if it were not mitigated by the
4656 mapped_index::name_components lookup table, which significantly
4657 reduces the number of times we need to call into this matcher,
4658 making it a non-issue.
4659
4660 - Symbol names in the index have no overload (parameter)
4661 information. I.e., in C++, "foo(int)" and "foo(long)" both
4662 appear as "foo" in the index, for example.
4663
4664 This means that the lookup names passed to the symbol name
4665 matcher functions must have no parameter information either
4666 because (e.g.) symbol search name "foo" does not match
4667 lookup-name "foo(int)" [while swapping search name for lookup
4668 name would match].
4669 */
4670 class gdb_index_symbol_name_matcher
4671 {
4672 public:
4673 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4674 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4675
4676 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4677 Returns true if any matcher matches. */
4678 bool matches (const char *symbol_name);
4679
4680 private:
4681 /* A reference to the lookup name we're matching against. */
4682 const lookup_name_info &m_lookup_name;
4683
4684 /* A vector holding all the different symbol name matchers, for all
4685 languages. */
4686 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4687 };
4688
4689 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4690 (const lookup_name_info &lookup_name)
4691 : m_lookup_name (lookup_name)
4692 {
4693 /* Prepare the vector of comparison functions upfront, to avoid
4694 doing the same work for each symbol. Care is taken to avoid
4695 matching with the same matcher more than once if/when multiple
4696 languages use the same matcher function. */
4697 auto &matchers = m_symbol_name_matcher_funcs;
4698 matchers.reserve (nr_languages);
4699
4700 matchers.push_back (default_symbol_name_matcher);
4701
4702 for (int i = 0; i < nr_languages; i++)
4703 {
4704 const language_defn *lang = language_def ((enum language) i);
4705 symbol_name_matcher_ftype *name_matcher
4706 = get_symbol_name_matcher (lang, m_lookup_name);
4707
4708 /* Don't insert the same comparison routine more than once.
4709 Note that we do this linear walk instead of a seemingly
4710 cheaper sorted insert, or use a std::set or something like
4711 that, because relative order of function addresses is not
4712 stable. This is not a problem in practice because the number
4713 of supported languages is low, and the cost here is tiny
4714 compared to the number of searches we'll do afterwards using
4715 this object. */
4716 if (name_matcher != default_symbol_name_matcher
4717 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4718 == matchers.end ()))
4719 matchers.push_back (name_matcher);
4720 }
4721 }
4722
4723 bool
4724 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4725 {
4726 for (auto matches_name : m_symbol_name_matcher_funcs)
4727 if (matches_name (symbol_name, m_lookup_name, NULL))
4728 return true;
4729
4730 return false;
4731 }
4732
4733 /* Starting from a search name, return the string that finds the upper
4734 bound of all strings that start with SEARCH_NAME in a sorted name
4735 list. Returns the empty string to indicate that the upper bound is
4736 the end of the list. */
4737
4738 static std::string
4739 make_sort_after_prefix_name (const char *search_name)
4740 {
4741 /* When looking to complete "func", we find the upper bound of all
4742 symbols that start with "func" by looking for where we'd insert
4743 the closest string that would follow "func" in lexicographical
4744 order. Usually, that's "func"-with-last-character-incremented,
4745 i.e. "fund". Mind non-ASCII characters, though. Usually those
4746 will be UTF-8 multi-byte sequences, but we can't be certain.
4747 Especially mind the 0xff character, which is a valid character in
4748 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4749 rule out compilers allowing it in identifiers. Note that
4750 conveniently, strcmp/strcasecmp are specified to compare
4751 characters interpreted as unsigned char. So what we do is treat
4752 the whole string as a base 256 number composed of a sequence of
4753 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4754 to 0, and carries 1 to the following more-significant position.
4755 If the very first character in SEARCH_NAME ends up incremented
4756 and carries/overflows, then the upper bound is the end of the
4757 list. The string after the empty string is also the empty
4758 string.
4759
4760 Some examples of this operation:
4761
4762 SEARCH_NAME => "+1" RESULT
4763
4764 "abc" => "abd"
4765 "ab\xff" => "ac"
4766 "\xff" "a" "\xff" => "\xff" "b"
4767 "\xff" => ""
4768 "\xff\xff" => ""
4769 "" => ""
4770
4771 Then, with these symbols for example:
4772
4773 func
4774 func1
4775 fund
4776
4777 completing "func" looks for symbols between "func" and
4778 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4779 which finds "func" and "func1", but not "fund".
4780
4781 And with:
4782
4783 funcÿ (Latin1 'ÿ' [0xff])
4784 funcÿ1
4785 fund
4786
4787 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4788 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4789
4790 And with:
4791
4792 ÿÿ (Latin1 'ÿ' [0xff])
4793 ÿÿ1
4794
4795 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4796 the end of the list.
4797 */
4798 std::string after = search_name;
4799 while (!after.empty () && (unsigned char) after.back () == 0xff)
4800 after.pop_back ();
4801 if (!after.empty ())
4802 after.back () = (unsigned char) after.back () + 1;
4803 return after;
4804 }
4805
4806 /* See declaration. */
4807
4808 std::pair<std::vector<name_component>::const_iterator,
4809 std::vector<name_component>::const_iterator>
4810 mapped_index_base::find_name_components_bounds
4811 (const lookup_name_info &lookup_name_without_params) const
4812 {
4813 auto *name_cmp
4814 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4815
4816 const char *cplus
4817 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4818
4819 /* Comparison function object for lower_bound that matches against a
4820 given symbol name. */
4821 auto lookup_compare_lower = [&] (const name_component &elem,
4822 const char *name)
4823 {
4824 const char *elem_qualified = this->symbol_name_at (elem.idx);
4825 const char *elem_name = elem_qualified + elem.name_offset;
4826 return name_cmp (elem_name, name) < 0;
4827 };
4828
4829 /* Comparison function object for upper_bound that matches against a
4830 given symbol name. */
4831 auto lookup_compare_upper = [&] (const char *name,
4832 const name_component &elem)
4833 {
4834 const char *elem_qualified = this->symbol_name_at (elem.idx);
4835 const char *elem_name = elem_qualified + elem.name_offset;
4836 return name_cmp (name, elem_name) < 0;
4837 };
4838
4839 auto begin = this->name_components.begin ();
4840 auto end = this->name_components.end ();
4841
4842 /* Find the lower bound. */
4843 auto lower = [&] ()
4844 {
4845 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4846 return begin;
4847 else
4848 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4849 } ();
4850
4851 /* Find the upper bound. */
4852 auto upper = [&] ()
4853 {
4854 if (lookup_name_without_params.completion_mode ())
4855 {
4856 /* In completion mode, we want UPPER to point past all
4857 symbols names that have the same prefix. I.e., with
4858 these symbols, and completing "func":
4859
4860 function << lower bound
4861 function1
4862 other_function << upper bound
4863
4864 We find the upper bound by looking for the insertion
4865 point of "func"-with-last-character-incremented,
4866 i.e. "fund". */
4867 std::string after = make_sort_after_prefix_name (cplus);
4868 if (after.empty ())
4869 return end;
4870 return std::lower_bound (lower, end, after.c_str (),
4871 lookup_compare_lower);
4872 }
4873 else
4874 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4875 } ();
4876
4877 return {lower, upper};
4878 }
4879
4880 /* See declaration. */
4881
4882 void
4883 mapped_index_base::build_name_components ()
4884 {
4885 if (!this->name_components.empty ())
4886 return;
4887
4888 this->name_components_casing = case_sensitivity;
4889 auto *name_cmp
4890 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4891
4892 /* The code below only knows how to break apart components of C++
4893 symbol names (and other languages that use '::' as
4894 namespace/module separator). If we add support for wild matching
4895 to some language that uses some other operator (E.g., Ada, Go and
4896 D use '.'), then we'll need to try splitting the symbol name
4897 according to that language too. Note that Ada does support wild
4898 matching, but doesn't currently support .gdb_index. */
4899 auto count = this->symbol_name_count ();
4900 for (offset_type idx = 0; idx < count; idx++)
4901 {
4902 if (this->symbol_name_slot_invalid (idx))
4903 continue;
4904
4905 const char *name = this->symbol_name_at (idx);
4906
4907 /* Add each name component to the name component table. */
4908 unsigned int previous_len = 0;
4909 for (unsigned int current_len = cp_find_first_component (name);
4910 name[current_len] != '\0';
4911 current_len += cp_find_first_component (name + current_len))
4912 {
4913 gdb_assert (name[current_len] == ':');
4914 this->name_components.push_back ({previous_len, idx});
4915 /* Skip the '::'. */
4916 current_len += 2;
4917 previous_len = current_len;
4918 }
4919 this->name_components.push_back ({previous_len, idx});
4920 }
4921
4922 /* Sort name_components elements by name. */
4923 auto name_comp_compare = [&] (const name_component &left,
4924 const name_component &right)
4925 {
4926 const char *left_qualified = this->symbol_name_at (left.idx);
4927 const char *right_qualified = this->symbol_name_at (right.idx);
4928
4929 const char *left_name = left_qualified + left.name_offset;
4930 const char *right_name = right_qualified + right.name_offset;
4931
4932 return name_cmp (left_name, right_name) < 0;
4933 };
4934
4935 std::sort (this->name_components.begin (),
4936 this->name_components.end (),
4937 name_comp_compare);
4938 }
4939
4940 /* Helper for dw2_expand_symtabs_matching that works with a
4941 mapped_index_base instead of the containing objfile. This is split
4942 to a separate function in order to be able to unit test the
4943 name_components matching using a mock mapped_index_base. For each
4944 symbol name that matches, calls MATCH_CALLBACK, passing it the
4945 symbol's index in the mapped_index_base symbol table. */
4946
4947 static void
4948 dw2_expand_symtabs_matching_symbol
4949 (mapped_index_base &index,
4950 const lookup_name_info &lookup_name_in,
4951 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4952 enum search_domain kind,
4953 gdb::function_view<void (offset_type)> match_callback)
4954 {
4955 lookup_name_info lookup_name_without_params
4956 = lookup_name_in.make_ignore_params ();
4957 gdb_index_symbol_name_matcher lookup_name_matcher
4958 (lookup_name_without_params);
4959
4960 /* Build the symbol name component sorted vector, if we haven't
4961 yet. */
4962 index.build_name_components ();
4963
4964 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4965
4966 /* Now for each symbol name in range, check to see if we have a name
4967 match, and if so, call the MATCH_CALLBACK callback. */
4968
4969 /* The same symbol may appear more than once in the range though.
4970 E.g., if we're looking for symbols that complete "w", and we have
4971 a symbol named "w1::w2", we'll find the two name components for
4972 that same symbol in the range. To be sure we only call the
4973 callback once per symbol, we first collect the symbol name
4974 indexes that matched in a temporary vector and ignore
4975 duplicates. */
4976 std::vector<offset_type> matches;
4977 matches.reserve (std::distance (bounds.first, bounds.second));
4978
4979 for (; bounds.first != bounds.second; ++bounds.first)
4980 {
4981 const char *qualified = index.symbol_name_at (bounds.first->idx);
4982
4983 if (!lookup_name_matcher.matches (qualified)
4984 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4985 continue;
4986
4987 matches.push_back (bounds.first->idx);
4988 }
4989
4990 std::sort (matches.begin (), matches.end ());
4991
4992 /* Finally call the callback, once per match. */
4993 ULONGEST prev = -1;
4994 for (offset_type idx : matches)
4995 {
4996 if (prev != idx)
4997 {
4998 match_callback (idx);
4999 prev = idx;
5000 }
5001 }
5002
5003 /* Above we use a type wider than idx's for 'prev', since 0 and
5004 (offset_type)-1 are both possible values. */
5005 static_assert (sizeof (prev) > sizeof (offset_type), "");
5006 }
5007
5008 #if GDB_SELF_TEST
5009
5010 namespace selftests { namespace dw2_expand_symtabs_matching {
5011
5012 /* A mock .gdb_index/.debug_names-like name index table, enough to
5013 exercise dw2_expand_symtabs_matching_symbol, which works with the
5014 mapped_index_base interface. Builds an index from the symbol list
5015 passed as parameter to the constructor. */
5016 class mock_mapped_index : public mapped_index_base
5017 {
5018 public:
5019 mock_mapped_index (gdb::array_view<const char *> symbols)
5020 : m_symbol_table (symbols)
5021 {}
5022
5023 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
5024
5025 /* Return the number of names in the symbol table. */
5026 virtual size_t symbol_name_count () const
5027 {
5028 return m_symbol_table.size ();
5029 }
5030
5031 /* Get the name of the symbol at IDX in the symbol table. */
5032 virtual const char *symbol_name_at (offset_type idx) const
5033 {
5034 return m_symbol_table[idx];
5035 }
5036
5037 private:
5038 gdb::array_view<const char *> m_symbol_table;
5039 };
5040
5041 /* Convenience function that converts a NULL pointer to a "<null>"
5042 string, to pass to print routines. */
5043
5044 static const char *
5045 string_or_null (const char *str)
5046 {
5047 return str != NULL ? str : "<null>";
5048 }
5049
5050 /* Check if a lookup_name_info built from
5051 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
5052 index. EXPECTED_LIST is the list of expected matches, in expected
5053 matching order. If no match expected, then an empty list is
5054 specified. Returns true on success. On failure prints a warning
5055 indicating the file:line that failed, and returns false. */
5056
5057 static bool
5058 check_match (const char *file, int line,
5059 mock_mapped_index &mock_index,
5060 const char *name, symbol_name_match_type match_type,
5061 bool completion_mode,
5062 std::initializer_list<const char *> expected_list)
5063 {
5064 lookup_name_info lookup_name (name, match_type, completion_mode);
5065
5066 bool matched = true;
5067
5068 auto mismatch = [&] (const char *expected_str,
5069 const char *got)
5070 {
5071 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
5072 "expected=\"%s\", got=\"%s\"\n"),
5073 file, line,
5074 (match_type == symbol_name_match_type::FULL
5075 ? "FULL" : "WILD"),
5076 name, string_or_null (expected_str), string_or_null (got));
5077 matched = false;
5078 };
5079
5080 auto expected_it = expected_list.begin ();
5081 auto expected_end = expected_list.end ();
5082
5083 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
5084 NULL, ALL_DOMAIN,
5085 [&] (offset_type idx)
5086 {
5087 const char *matched_name = mock_index.symbol_name_at (idx);
5088 const char *expected_str
5089 = expected_it == expected_end ? NULL : *expected_it++;
5090
5091 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
5092 mismatch (expected_str, matched_name);
5093 });
5094
5095 const char *expected_str
5096 = expected_it == expected_end ? NULL : *expected_it++;
5097 if (expected_str != NULL)
5098 mismatch (expected_str, NULL);
5099
5100 return matched;
5101 }
5102
5103 /* The symbols added to the mock mapped_index for testing (in
5104 canonical form). */
5105 static const char *test_symbols[] = {
5106 "function",
5107 "std::bar",
5108 "std::zfunction",
5109 "std::zfunction2",
5110 "w1::w2",
5111 "ns::foo<char*>",
5112 "ns::foo<int>",
5113 "ns::foo<long>",
5114 "ns2::tmpl<int>::foo2",
5115 "(anonymous namespace)::A::B::C",
5116
5117 /* These are used to check that the increment-last-char in the
5118 matching algorithm for completion doesn't match "t1_fund" when
5119 completing "t1_func". */
5120 "t1_func",
5121 "t1_func1",
5122 "t1_fund",
5123 "t1_fund1",
5124
5125 /* A UTF-8 name with multi-byte sequences to make sure that
5126 cp-name-parser understands this as a single identifier ("função"
5127 is "function" in PT). */
5128 u8"u8função",
5129
5130 /* \377 (0xff) is Latin1 'ÿ'. */
5131 "yfunc\377",
5132
5133 /* \377 (0xff) is Latin1 'ÿ'. */
5134 "\377",
5135 "\377\377123",
5136
5137 /* A name with all sorts of complications. Starts with "z" to make
5138 it easier for the completion tests below. */
5139 #define Z_SYM_NAME \
5140 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
5141 "::tuple<(anonymous namespace)::ui*, " \
5142 "std::default_delete<(anonymous namespace)::ui>, void>"
5143
5144 Z_SYM_NAME
5145 };
5146
5147 /* Returns true if the mapped_index_base::find_name_component_bounds
5148 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
5149 in completion mode. */
5150
5151 static bool
5152 check_find_bounds_finds (mapped_index_base &index,
5153 const char *search_name,
5154 gdb::array_view<const char *> expected_syms)
5155 {
5156 lookup_name_info lookup_name (search_name,
5157 symbol_name_match_type::FULL, true);
5158
5159 auto bounds = index.find_name_components_bounds (lookup_name);
5160
5161 size_t distance = std::distance (bounds.first, bounds.second);
5162 if (distance != expected_syms.size ())
5163 return false;
5164
5165 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
5166 {
5167 auto nc_elem = bounds.first + exp_elem;
5168 const char *qualified = index.symbol_name_at (nc_elem->idx);
5169 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
5170 return false;
5171 }
5172
5173 return true;
5174 }
5175
5176 /* Test the lower-level mapped_index::find_name_component_bounds
5177 method. */
5178
5179 static void
5180 test_mapped_index_find_name_component_bounds ()
5181 {
5182 mock_mapped_index mock_index (test_symbols);
5183
5184 mock_index.build_name_components ();
5185
5186 /* Test the lower-level mapped_index::find_name_component_bounds
5187 method in completion mode. */
5188 {
5189 static const char *expected_syms[] = {
5190 "t1_func",
5191 "t1_func1",
5192 };
5193
5194 SELF_CHECK (check_find_bounds_finds (mock_index,
5195 "t1_func", expected_syms));
5196 }
5197
5198 /* Check that the increment-last-char in the name matching algorithm
5199 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
5200 {
5201 static const char *expected_syms1[] = {
5202 "\377",
5203 "\377\377123",
5204 };
5205 SELF_CHECK (check_find_bounds_finds (mock_index,
5206 "\377", expected_syms1));
5207
5208 static const char *expected_syms2[] = {
5209 "\377\377123",
5210 };
5211 SELF_CHECK (check_find_bounds_finds (mock_index,
5212 "\377\377", expected_syms2));
5213 }
5214 }
5215
5216 /* Test dw2_expand_symtabs_matching_symbol. */
5217
5218 static void
5219 test_dw2_expand_symtabs_matching_symbol ()
5220 {
5221 mock_mapped_index mock_index (test_symbols);
5222
5223 /* We let all tests run until the end even if some fails, for debug
5224 convenience. */
5225 bool any_mismatch = false;
5226
5227 /* Create the expected symbols list (an initializer_list). Needed
5228 because lists have commas, and we need to pass them to CHECK,
5229 which is a macro. */
5230 #define EXPECT(...) { __VA_ARGS__ }
5231
5232 /* Wrapper for check_match that passes down the current
5233 __FILE__/__LINE__. */
5234 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
5235 any_mismatch |= !check_match (__FILE__, __LINE__, \
5236 mock_index, \
5237 NAME, MATCH_TYPE, COMPLETION_MODE, \
5238 EXPECTED_LIST)
5239
5240 /* Identity checks. */
5241 for (const char *sym : test_symbols)
5242 {
5243 /* Should be able to match all existing symbols. */
5244 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
5245 EXPECT (sym));
5246
5247 /* Should be able to match all existing symbols with
5248 parameters. */
5249 std::string with_params = std::string (sym) + "(int)";
5250 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5251 EXPECT (sym));
5252
5253 /* Should be able to match all existing symbols with
5254 parameters and qualifiers. */
5255 with_params = std::string (sym) + " ( int ) const";
5256 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5257 EXPECT (sym));
5258
5259 /* This should really find sym, but cp-name-parser.y doesn't
5260 know about lvalue/rvalue qualifiers yet. */
5261 with_params = std::string (sym) + " ( int ) &&";
5262 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
5263 {});
5264 }
5265
5266 /* Check that the name matching algorithm for completion doesn't get
5267 confused with Latin1 'ÿ' / 0xff. */
5268 {
5269 static const char str[] = "\377";
5270 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5271 EXPECT ("\377", "\377\377123"));
5272 }
5273
5274 /* Check that the increment-last-char in the matching algorithm for
5275 completion doesn't match "t1_fund" when completing "t1_func". */
5276 {
5277 static const char str[] = "t1_func";
5278 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
5279 EXPECT ("t1_func", "t1_func1"));
5280 }
5281
5282 /* Check that completion mode works at each prefix of the expected
5283 symbol name. */
5284 {
5285 static const char str[] = "function(int)";
5286 size_t len = strlen (str);
5287 std::string lookup;
5288
5289 for (size_t i = 1; i < len; i++)
5290 {
5291 lookup.assign (str, i);
5292 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5293 EXPECT ("function"));
5294 }
5295 }
5296
5297 /* While "w" is a prefix of both components, the match function
5298 should still only be called once. */
5299 {
5300 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
5301 EXPECT ("w1::w2"));
5302 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
5303 EXPECT ("w1::w2"));
5304 }
5305
5306 /* Same, with a "complicated" symbol. */
5307 {
5308 static const char str[] = Z_SYM_NAME;
5309 size_t len = strlen (str);
5310 std::string lookup;
5311
5312 for (size_t i = 1; i < len; i++)
5313 {
5314 lookup.assign (str, i);
5315 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
5316 EXPECT (Z_SYM_NAME));
5317 }
5318 }
5319
5320 /* In FULL mode, an incomplete symbol doesn't match. */
5321 {
5322 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
5323 {});
5324 }
5325
5326 /* A complete symbol with parameters matches any overload, since the
5327 index has no overload info. */
5328 {
5329 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
5330 EXPECT ("std::zfunction", "std::zfunction2"));
5331 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
5332 EXPECT ("std::zfunction", "std::zfunction2"));
5333 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
5334 EXPECT ("std::zfunction", "std::zfunction2"));
5335 }
5336
5337 /* Check that whitespace is ignored appropriately. A symbol with a
5338 template argument list. */
5339 {
5340 static const char expected[] = "ns::foo<int>";
5341 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
5342 EXPECT (expected));
5343 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
5344 EXPECT (expected));
5345 }
5346
5347 /* Check that whitespace is ignored appropriately. A symbol with a
5348 template argument list that includes a pointer. */
5349 {
5350 static const char expected[] = "ns::foo<char*>";
5351 /* Try both completion and non-completion modes. */
5352 static const bool completion_mode[2] = {false, true};
5353 for (size_t i = 0; i < 2; i++)
5354 {
5355 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
5356 completion_mode[i], EXPECT (expected));
5357 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
5358 completion_mode[i], EXPECT (expected));
5359
5360 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
5361 completion_mode[i], EXPECT (expected));
5362 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
5363 completion_mode[i], EXPECT (expected));
5364 }
5365 }
5366
5367 {
5368 /* Check method qualifiers are ignored. */
5369 static const char expected[] = "ns::foo<char*>";
5370 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
5371 symbol_name_match_type::FULL, true, EXPECT (expected));
5372 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
5373 symbol_name_match_type::FULL, true, EXPECT (expected));
5374 CHECK_MATCH ("foo < char * > ( int ) const",
5375 symbol_name_match_type::WILD, true, EXPECT (expected));
5376 CHECK_MATCH ("foo < char * > ( int ) &&",
5377 symbol_name_match_type::WILD, true, EXPECT (expected));
5378 }
5379
5380 /* Test lookup names that don't match anything. */
5381 {
5382 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
5383 {});
5384
5385 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
5386 {});
5387 }
5388
5389 /* Some wild matching tests, exercising "(anonymous namespace)",
5390 which should not be confused with a parameter list. */
5391 {
5392 static const char *syms[] = {
5393 "A::B::C",
5394 "B::C",
5395 "C",
5396 "A :: B :: C ( int )",
5397 "B :: C ( int )",
5398 "C ( int )",
5399 };
5400
5401 for (const char *s : syms)
5402 {
5403 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
5404 EXPECT ("(anonymous namespace)::A::B::C"));
5405 }
5406 }
5407
5408 {
5409 static const char expected[] = "ns2::tmpl<int>::foo2";
5410 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5411 EXPECT (expected));
5412 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5413 EXPECT (expected));
5414 }
5415
5416 SELF_CHECK (!any_mismatch);
5417
5418 #undef EXPECT
5419 #undef CHECK_MATCH
5420 }
5421
5422 static void
5423 run_test ()
5424 {
5425 test_mapped_index_find_name_component_bounds ();
5426 test_dw2_expand_symtabs_matching_symbol ();
5427 }
5428
5429 }} // namespace selftests::dw2_expand_symtabs_matching
5430
5431 #endif /* GDB_SELF_TEST */
5432
5433 /* If FILE_MATCHER is NULL or if PER_CU has
5434 dwarf2_per_cu_quick_data::MARK set (see
5435 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5436 EXPANSION_NOTIFY on it. */
5437
5438 static void
5439 dw2_expand_symtabs_matching_one
5440 (struct dwarf2_per_cu_data *per_cu,
5441 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5442 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5443 {
5444 if (file_matcher == NULL || per_cu->v.quick->mark)
5445 {
5446 bool symtab_was_null
5447 = (per_cu->v.quick->compunit_symtab == NULL);
5448
5449 dw2_instantiate_symtab (per_cu);
5450
5451 if (expansion_notify != NULL
5452 && symtab_was_null
5453 && per_cu->v.quick->compunit_symtab != NULL)
5454 expansion_notify (per_cu->v.quick->compunit_symtab);
5455 }
5456 }
5457
5458 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5459 matched, to expand corresponding CUs that were marked. IDX is the
5460 index of the symbol name that matched. */
5461
5462 static void
5463 dw2_expand_marked_cus
5464 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5465 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5466 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5467 search_domain kind)
5468 {
5469 offset_type *vec, vec_len, vec_idx;
5470 bool global_seen = false;
5471 mapped_index &index = *dwarf2_per_objfile->index_table;
5472
5473 vec = (offset_type *) (index.constant_pool
5474 + MAYBE_SWAP (index.symbol_table[idx].vec));
5475 vec_len = MAYBE_SWAP (vec[0]);
5476 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5477 {
5478 struct dwarf2_per_cu_data *per_cu;
5479 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5480 /* This value is only valid for index versions >= 7. */
5481 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5482 gdb_index_symbol_kind symbol_kind =
5483 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5484 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5485 /* Only check the symbol attributes if they're present.
5486 Indices prior to version 7 don't record them,
5487 and indices >= 7 may elide them for certain symbols
5488 (gold does this). */
5489 int attrs_valid =
5490 (index.version >= 7
5491 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5492
5493 /* Work around gold/15646. */
5494 if (attrs_valid)
5495 {
5496 if (!is_static && global_seen)
5497 continue;
5498 if (!is_static)
5499 global_seen = true;
5500 }
5501
5502 /* Only check the symbol's kind if it has one. */
5503 if (attrs_valid)
5504 {
5505 switch (kind)
5506 {
5507 case VARIABLES_DOMAIN:
5508 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5509 continue;
5510 break;
5511 case FUNCTIONS_DOMAIN:
5512 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5513 continue;
5514 break;
5515 case TYPES_DOMAIN:
5516 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5517 continue;
5518 break;
5519 default:
5520 break;
5521 }
5522 }
5523
5524 /* Don't crash on bad data. */
5525 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5526 + dwarf2_per_objfile->n_type_units))
5527 {
5528 complaint (&symfile_complaints,
5529 _(".gdb_index entry has bad CU index"
5530 " [in module %s]"),
5531 objfile_name (dwarf2_per_objfile->objfile));
5532 continue;
5533 }
5534
5535 per_cu = dw2_get_cutu (dwarf2_per_objfile, cu_index);
5536 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5537 expansion_notify);
5538 }
5539 }
5540
5541 /* If FILE_MATCHER is non-NULL, set all the
5542 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5543 that match FILE_MATCHER. */
5544
5545 static void
5546 dw_expand_symtabs_matching_file_matcher
5547 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5548 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5549 {
5550 if (file_matcher == NULL)
5551 return;
5552
5553 objfile *const objfile = dwarf2_per_objfile->objfile;
5554
5555 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5556 htab_eq_pointer,
5557 NULL, xcalloc, xfree));
5558 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5559 htab_eq_pointer,
5560 NULL, xcalloc, xfree));
5561
5562 /* The rule is CUs specify all the files, including those used by
5563 any TU, so there's no need to scan TUs here. */
5564
5565 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5566 {
5567 int j;
5568 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5569 struct quick_file_names *file_data;
5570 void **slot;
5571
5572 QUIT;
5573
5574 per_cu->v.quick->mark = 0;
5575
5576 /* We only need to look at symtabs not already expanded. */
5577 if (per_cu->v.quick->compunit_symtab)
5578 continue;
5579
5580 file_data = dw2_get_file_names (per_cu);
5581 if (file_data == NULL)
5582 continue;
5583
5584 if (htab_find (visited_not_found.get (), file_data) != NULL)
5585 continue;
5586 else if (htab_find (visited_found.get (), file_data) != NULL)
5587 {
5588 per_cu->v.quick->mark = 1;
5589 continue;
5590 }
5591
5592 for (j = 0; j < file_data->num_file_names; ++j)
5593 {
5594 const char *this_real_name;
5595
5596 if (file_matcher (file_data->file_names[j], false))
5597 {
5598 per_cu->v.quick->mark = 1;
5599 break;
5600 }
5601
5602 /* Before we invoke realpath, which can get expensive when many
5603 files are involved, do a quick comparison of the basenames. */
5604 if (!basenames_may_differ
5605 && !file_matcher (lbasename (file_data->file_names[j]),
5606 true))
5607 continue;
5608
5609 this_real_name = dw2_get_real_path (objfile, file_data, j);
5610 if (file_matcher (this_real_name, false))
5611 {
5612 per_cu->v.quick->mark = 1;
5613 break;
5614 }
5615 }
5616
5617 slot = htab_find_slot (per_cu->v.quick->mark
5618 ? visited_found.get ()
5619 : visited_not_found.get (),
5620 file_data, INSERT);
5621 *slot = file_data;
5622 }
5623 }
5624
5625 static void
5626 dw2_expand_symtabs_matching
5627 (struct objfile *objfile,
5628 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5629 const lookup_name_info &lookup_name,
5630 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5631 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5632 enum search_domain kind)
5633 {
5634 struct dwarf2_per_objfile *dwarf2_per_objfile
5635 = get_dwarf2_per_objfile (objfile);
5636
5637 /* index_table is NULL if OBJF_READNOW. */
5638 if (!dwarf2_per_objfile->index_table)
5639 return;
5640
5641 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5642
5643 mapped_index &index = *dwarf2_per_objfile->index_table;
5644
5645 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5646 symbol_matcher,
5647 kind, [&] (offset_type idx)
5648 {
5649 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5650 expansion_notify, kind);
5651 });
5652 }
5653
5654 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5655 symtab. */
5656
5657 static struct compunit_symtab *
5658 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5659 CORE_ADDR pc)
5660 {
5661 int i;
5662
5663 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5664 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5665 return cust;
5666
5667 if (cust->includes == NULL)
5668 return NULL;
5669
5670 for (i = 0; cust->includes[i]; ++i)
5671 {
5672 struct compunit_symtab *s = cust->includes[i];
5673
5674 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5675 if (s != NULL)
5676 return s;
5677 }
5678
5679 return NULL;
5680 }
5681
5682 static struct compunit_symtab *
5683 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5684 struct bound_minimal_symbol msymbol,
5685 CORE_ADDR pc,
5686 struct obj_section *section,
5687 int warn_if_readin)
5688 {
5689 struct dwarf2_per_cu_data *data;
5690 struct compunit_symtab *result;
5691
5692 if (!objfile->psymtabs_addrmap)
5693 return NULL;
5694
5695 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5696 pc);
5697 if (!data)
5698 return NULL;
5699
5700 if (warn_if_readin && data->v.quick->compunit_symtab)
5701 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5702 paddress (get_objfile_arch (objfile), pc));
5703
5704 result
5705 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5706 pc);
5707 gdb_assert (result != NULL);
5708 return result;
5709 }
5710
5711 static void
5712 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5713 void *data, int need_fullname)
5714 {
5715 struct dwarf2_per_objfile *dwarf2_per_objfile
5716 = get_dwarf2_per_objfile (objfile);
5717
5718 if (!dwarf2_per_objfile->filenames_cache)
5719 {
5720 dwarf2_per_objfile->filenames_cache.emplace ();
5721
5722 htab_up visited (htab_create_alloc (10,
5723 htab_hash_pointer, htab_eq_pointer,
5724 NULL, xcalloc, xfree));
5725
5726 /* The rule is CUs specify all the files, including those used
5727 by any TU, so there's no need to scan TUs here. We can
5728 ignore file names coming from already-expanded CUs. */
5729
5730 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5731 {
5732 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
5733
5734 if (per_cu->v.quick->compunit_symtab)
5735 {
5736 void **slot = htab_find_slot (visited.get (),
5737 per_cu->v.quick->file_names,
5738 INSERT);
5739
5740 *slot = per_cu->v.quick->file_names;
5741 }
5742 }
5743
5744 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5745 {
5746 dwarf2_per_cu_data *per_cu = dw2_get_cu (dwarf2_per_objfile, i);
5747 struct quick_file_names *file_data;
5748 void **slot;
5749
5750 /* We only need to look at symtabs not already expanded. */
5751 if (per_cu->v.quick->compunit_symtab)
5752 continue;
5753
5754 file_data = dw2_get_file_names (per_cu);
5755 if (file_data == NULL)
5756 continue;
5757
5758 slot = htab_find_slot (visited.get (), file_data, INSERT);
5759 if (*slot)
5760 {
5761 /* Already visited. */
5762 continue;
5763 }
5764 *slot = file_data;
5765
5766 for (int j = 0; j < file_data->num_file_names; ++j)
5767 {
5768 const char *filename = file_data->file_names[j];
5769 dwarf2_per_objfile->filenames_cache->seen (filename);
5770 }
5771 }
5772 }
5773
5774 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5775 {
5776 gdb::unique_xmalloc_ptr<char> this_real_name;
5777
5778 if (need_fullname)
5779 this_real_name = gdb_realpath (filename);
5780 (*fun) (filename, this_real_name.get (), data);
5781 });
5782 }
5783
5784 static int
5785 dw2_has_symbols (struct objfile *objfile)
5786 {
5787 return 1;
5788 }
5789
5790 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5791 {
5792 dw2_has_symbols,
5793 dw2_find_last_source_symtab,
5794 dw2_forget_cached_source_info,
5795 dw2_map_symtabs_matching_filename,
5796 dw2_lookup_symbol,
5797 dw2_print_stats,
5798 dw2_dump,
5799 dw2_relocate,
5800 dw2_expand_symtabs_for_function,
5801 dw2_expand_all_symtabs,
5802 dw2_expand_symtabs_with_fullname,
5803 dw2_map_matching_symbols,
5804 dw2_expand_symtabs_matching,
5805 dw2_find_pc_sect_compunit_symtab,
5806 NULL,
5807 dw2_map_symbol_filenames
5808 };
5809
5810 /* DWARF-5 debug_names reader. */
5811
5812 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5813 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5814
5815 /* A helper function that reads the .debug_names section in SECTION
5816 and fills in MAP. FILENAME is the name of the file containing the
5817 section; it is used for error reporting.
5818
5819 Returns true if all went well, false otherwise. */
5820
5821 static bool
5822 read_debug_names_from_section (struct objfile *objfile,
5823 const char *filename,
5824 struct dwarf2_section_info *section,
5825 mapped_debug_names &map)
5826 {
5827 if (dwarf2_section_empty_p (section))
5828 return false;
5829
5830 /* Older elfutils strip versions could keep the section in the main
5831 executable while splitting it for the separate debug info file. */
5832 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5833 return false;
5834
5835 dwarf2_read_section (objfile, section);
5836
5837 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5838
5839 const gdb_byte *addr = section->buffer;
5840
5841 bfd *const abfd = get_section_bfd_owner (section);
5842
5843 unsigned int bytes_read;
5844 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5845 addr += bytes_read;
5846
5847 map.dwarf5_is_dwarf64 = bytes_read != 4;
5848 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5849 if (bytes_read + length != section->size)
5850 {
5851 /* There may be multiple per-CU indices. */
5852 warning (_("Section .debug_names in %s length %s does not match "
5853 "section length %s, ignoring .debug_names."),
5854 filename, plongest (bytes_read + length),
5855 pulongest (section->size));
5856 return false;
5857 }
5858
5859 /* The version number. */
5860 uint16_t version = read_2_bytes (abfd, addr);
5861 addr += 2;
5862 if (version != 5)
5863 {
5864 warning (_("Section .debug_names in %s has unsupported version %d, "
5865 "ignoring .debug_names."),
5866 filename, version);
5867 return false;
5868 }
5869
5870 /* Padding. */
5871 uint16_t padding = read_2_bytes (abfd, addr);
5872 addr += 2;
5873 if (padding != 0)
5874 {
5875 warning (_("Section .debug_names in %s has unsupported padding %d, "
5876 "ignoring .debug_names."),
5877 filename, padding);
5878 return false;
5879 }
5880
5881 /* comp_unit_count - The number of CUs in the CU list. */
5882 map.cu_count = read_4_bytes (abfd, addr);
5883 addr += 4;
5884
5885 /* local_type_unit_count - The number of TUs in the local TU
5886 list. */
5887 map.tu_count = read_4_bytes (abfd, addr);
5888 addr += 4;
5889
5890 /* foreign_type_unit_count - The number of TUs in the foreign TU
5891 list. */
5892 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5893 addr += 4;
5894 if (foreign_tu_count != 0)
5895 {
5896 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5897 "ignoring .debug_names."),
5898 filename, static_cast<unsigned long> (foreign_tu_count));
5899 return false;
5900 }
5901
5902 /* bucket_count - The number of hash buckets in the hash lookup
5903 table. */
5904 map.bucket_count = read_4_bytes (abfd, addr);
5905 addr += 4;
5906
5907 /* name_count - The number of unique names in the index. */
5908 map.name_count = read_4_bytes (abfd, addr);
5909 addr += 4;
5910
5911 /* abbrev_table_size - The size in bytes of the abbreviations
5912 table. */
5913 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5914 addr += 4;
5915
5916 /* augmentation_string_size - The size in bytes of the augmentation
5917 string. This value is rounded up to a multiple of 4. */
5918 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5919 addr += 4;
5920 map.augmentation_is_gdb = ((augmentation_string_size
5921 == sizeof (dwarf5_augmentation))
5922 && memcmp (addr, dwarf5_augmentation,
5923 sizeof (dwarf5_augmentation)) == 0);
5924 augmentation_string_size += (-augmentation_string_size) & 3;
5925 addr += augmentation_string_size;
5926
5927 /* List of CUs */
5928 map.cu_table_reordered = addr;
5929 addr += map.cu_count * map.offset_size;
5930
5931 /* List of Local TUs */
5932 map.tu_table_reordered = addr;
5933 addr += map.tu_count * map.offset_size;
5934
5935 /* Hash Lookup Table */
5936 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5937 addr += map.bucket_count * 4;
5938 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5939 addr += map.name_count * 4;
5940
5941 /* Name Table */
5942 map.name_table_string_offs_reordered = addr;
5943 addr += map.name_count * map.offset_size;
5944 map.name_table_entry_offs_reordered = addr;
5945 addr += map.name_count * map.offset_size;
5946
5947 const gdb_byte *abbrev_table_start = addr;
5948 for (;;)
5949 {
5950 unsigned int bytes_read;
5951 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5952 addr += bytes_read;
5953 if (index_num == 0)
5954 break;
5955
5956 const auto insertpair
5957 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5958 if (!insertpair.second)
5959 {
5960 warning (_("Section .debug_names in %s has duplicate index %s, "
5961 "ignoring .debug_names."),
5962 filename, pulongest (index_num));
5963 return false;
5964 }
5965 mapped_debug_names::index_val &indexval = insertpair.first->second;
5966 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5967 addr += bytes_read;
5968
5969 for (;;)
5970 {
5971 mapped_debug_names::index_val::attr attr;
5972 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5973 addr += bytes_read;
5974 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5975 addr += bytes_read;
5976 if (attr.form == DW_FORM_implicit_const)
5977 {
5978 attr.implicit_const = read_signed_leb128 (abfd, addr,
5979 &bytes_read);
5980 addr += bytes_read;
5981 }
5982 if (attr.dw_idx == 0 && attr.form == 0)
5983 break;
5984 indexval.attr_vec.push_back (std::move (attr));
5985 }
5986 }
5987 if (addr != abbrev_table_start + abbrev_table_size)
5988 {
5989 warning (_("Section .debug_names in %s has abbreviation_table "
5990 "of size %zu vs. written as %u, ignoring .debug_names."),
5991 filename, addr - abbrev_table_start, abbrev_table_size);
5992 return false;
5993 }
5994 map.entry_pool = addr;
5995
5996 return true;
5997 }
5998
5999 /* A helper for create_cus_from_debug_names that handles the MAP's CU
6000 list. */
6001
6002 static void
6003 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
6004 const mapped_debug_names &map,
6005 dwarf2_section_info &section,
6006 bool is_dwz, int base_offset)
6007 {
6008 sect_offset sect_off_prev;
6009 for (uint32_t i = 0; i <= map.cu_count; ++i)
6010 {
6011 sect_offset sect_off_next;
6012 if (i < map.cu_count)
6013 {
6014 sect_off_next
6015 = (sect_offset) (extract_unsigned_integer
6016 (map.cu_table_reordered + i * map.offset_size,
6017 map.offset_size,
6018 map.dwarf5_byte_order));
6019 }
6020 else
6021 sect_off_next = (sect_offset) section.size;
6022 if (i >= 1)
6023 {
6024 const ULONGEST length = sect_off_next - sect_off_prev;
6025 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
6026 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
6027 sect_off_prev, length);
6028 }
6029 sect_off_prev = sect_off_next;
6030 }
6031 }
6032
6033 /* Read the CU list from the mapped index, and use it to create all
6034 the CU objects for this dwarf2_per_objfile. */
6035
6036 static void
6037 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
6038 const mapped_debug_names &map,
6039 const mapped_debug_names &dwz_map)
6040 {
6041 struct objfile *objfile = dwarf2_per_objfile->objfile;
6042
6043 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
6044 dwarf2_per_objfile->all_comp_units
6045 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
6046 dwarf2_per_objfile->n_comp_units);
6047
6048 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
6049 dwarf2_per_objfile->info,
6050 false /* is_dwz */,
6051 0 /* base_offset */);
6052
6053 if (dwz_map.cu_count == 0)
6054 return;
6055
6056 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6057 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
6058 true /* is_dwz */,
6059 map.cu_count /* base_offset */);
6060 }
6061
6062 /* Read .debug_names. If everything went ok, initialize the "quick"
6063 elements of all the CUs and return true. Otherwise, return false. */
6064
6065 static bool
6066 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
6067 {
6068 mapped_debug_names local_map (dwarf2_per_objfile);
6069 mapped_debug_names dwz_map (dwarf2_per_objfile);
6070 struct objfile *objfile = dwarf2_per_objfile->objfile;
6071
6072 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
6073 &dwarf2_per_objfile->debug_names,
6074 local_map))
6075 return false;
6076
6077 /* Don't use the index if it's empty. */
6078 if (local_map.name_count == 0)
6079 return false;
6080
6081 /* If there is a .dwz file, read it so we can get its CU list as
6082 well. */
6083 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
6084 if (dwz != NULL)
6085 {
6086 if (!read_debug_names_from_section (objfile,
6087 bfd_get_filename (dwz->dwz_bfd),
6088 &dwz->debug_names, dwz_map))
6089 {
6090 warning (_("could not read '.debug_names' section from %s; skipping"),
6091 bfd_get_filename (dwz->dwz_bfd));
6092 return false;
6093 }
6094 }
6095
6096 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
6097
6098 if (local_map.tu_count != 0)
6099 {
6100 /* We can only handle a single .debug_types when we have an
6101 index. */
6102 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
6103 return false;
6104
6105 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
6106 dwarf2_per_objfile->types, 0);
6107
6108 create_signatured_type_table_from_debug_names
6109 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
6110 }
6111
6112 create_addrmap_from_aranges (dwarf2_per_objfile,
6113 &dwarf2_per_objfile->debug_aranges);
6114
6115 dwarf2_per_objfile->debug_names_table.reset
6116 (new mapped_debug_names (dwarf2_per_objfile));
6117 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
6118 dwarf2_per_objfile->using_index = 1;
6119 dwarf2_per_objfile->quick_file_names_table =
6120 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6121
6122 return true;
6123 }
6124
6125 /* Symbol name hashing function as specified by DWARF-5. */
6126
6127 static uint32_t
6128 dwarf5_djb_hash (const char *str_)
6129 {
6130 const unsigned char *str = (const unsigned char *) str_;
6131
6132 /* Note: tolower here ignores UTF-8, which isn't fully compliant.
6133 See http://dwarfstd.org/ShowIssue.php?issue=161027.1. */
6134
6135 uint32_t hash = 5381;
6136 while (int c = *str++)
6137 hash = hash * 33 + tolower (c);
6138 return hash;
6139 }
6140
6141 /* Type used to manage iterating over all CUs looking for a symbol for
6142 .debug_names. */
6143
6144 class dw2_debug_names_iterator
6145 {
6146 public:
6147 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
6148 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
6149 dw2_debug_names_iterator (const mapped_debug_names &map,
6150 bool want_specific_block,
6151 block_enum block_index, domain_enum domain,
6152 const char *name)
6153 : m_map (map), m_want_specific_block (want_specific_block),
6154 m_block_index (block_index), m_domain (domain),
6155 m_addr (find_vec_in_debug_names (map, name))
6156 {}
6157
6158 dw2_debug_names_iterator (const mapped_debug_names &map,
6159 search_domain search, uint32_t namei)
6160 : m_map (map),
6161 m_search (search),
6162 m_addr (find_vec_in_debug_names (map, namei))
6163 {}
6164
6165 /* Return the next matching CU or NULL if there are no more. */
6166 dwarf2_per_cu_data *next ();
6167
6168 private:
6169 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6170 const char *name);
6171 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
6172 uint32_t namei);
6173
6174 /* The internalized form of .debug_names. */
6175 const mapped_debug_names &m_map;
6176
6177 /* If true, only look for symbols that match BLOCK_INDEX. */
6178 const bool m_want_specific_block = false;
6179
6180 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
6181 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
6182 value. */
6183 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
6184
6185 /* The kind of symbol we're looking for. */
6186 const domain_enum m_domain = UNDEF_DOMAIN;
6187 const search_domain m_search = ALL_DOMAIN;
6188
6189 /* The list of CUs from the index entry of the symbol, or NULL if
6190 not found. */
6191 const gdb_byte *m_addr;
6192 };
6193
6194 const char *
6195 mapped_debug_names::namei_to_name (uint32_t namei) const
6196 {
6197 const ULONGEST namei_string_offs
6198 = extract_unsigned_integer ((name_table_string_offs_reordered
6199 + namei * offset_size),
6200 offset_size,
6201 dwarf5_byte_order);
6202 return read_indirect_string_at_offset
6203 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
6204 }
6205
6206 /* Find a slot in .debug_names for the object named NAME. If NAME is
6207 found, return pointer to its pool data. If NAME cannot be found,
6208 return NULL. */
6209
6210 const gdb_byte *
6211 dw2_debug_names_iterator::find_vec_in_debug_names
6212 (const mapped_debug_names &map, const char *name)
6213 {
6214 int (*cmp) (const char *, const char *);
6215
6216 if (current_language->la_language == language_cplus
6217 || current_language->la_language == language_fortran
6218 || current_language->la_language == language_d)
6219 {
6220 /* NAME is already canonical. Drop any qualifiers as
6221 .debug_names does not contain any. */
6222
6223 if (strchr (name, '(') != NULL)
6224 {
6225 gdb::unique_xmalloc_ptr<char> without_params
6226 = cp_remove_params (name);
6227
6228 if (without_params != NULL)
6229 {
6230 name = without_params.get();
6231 }
6232 }
6233 }
6234
6235 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
6236
6237 const uint32_t full_hash = dwarf5_djb_hash (name);
6238 uint32_t namei
6239 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6240 (map.bucket_table_reordered
6241 + (full_hash % map.bucket_count)), 4,
6242 map.dwarf5_byte_order);
6243 if (namei == 0)
6244 return NULL;
6245 --namei;
6246 if (namei >= map.name_count)
6247 {
6248 complaint (&symfile_complaints,
6249 _("Wrong .debug_names with name index %u but name_count=%u "
6250 "[in module %s]"),
6251 namei, map.name_count,
6252 objfile_name (map.dwarf2_per_objfile->objfile));
6253 return NULL;
6254 }
6255
6256 for (;;)
6257 {
6258 const uint32_t namei_full_hash
6259 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
6260 (map.hash_table_reordered + namei), 4,
6261 map.dwarf5_byte_order);
6262 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
6263 return NULL;
6264
6265 if (full_hash == namei_full_hash)
6266 {
6267 const char *const namei_string = map.namei_to_name (namei);
6268
6269 #if 0 /* An expensive sanity check. */
6270 if (namei_full_hash != dwarf5_djb_hash (namei_string))
6271 {
6272 complaint (&symfile_complaints,
6273 _("Wrong .debug_names hash for string at index %u "
6274 "[in module %s]"),
6275 namei, objfile_name (dwarf2_per_objfile->objfile));
6276 return NULL;
6277 }
6278 #endif
6279
6280 if (cmp (namei_string, name) == 0)
6281 {
6282 const ULONGEST namei_entry_offs
6283 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6284 + namei * map.offset_size),
6285 map.offset_size, map.dwarf5_byte_order);
6286 return map.entry_pool + namei_entry_offs;
6287 }
6288 }
6289
6290 ++namei;
6291 if (namei >= map.name_count)
6292 return NULL;
6293 }
6294 }
6295
6296 const gdb_byte *
6297 dw2_debug_names_iterator::find_vec_in_debug_names
6298 (const mapped_debug_names &map, uint32_t namei)
6299 {
6300 if (namei >= map.name_count)
6301 {
6302 complaint (&symfile_complaints,
6303 _("Wrong .debug_names with name index %u but name_count=%u "
6304 "[in module %s]"),
6305 namei, map.name_count,
6306 objfile_name (map.dwarf2_per_objfile->objfile));
6307 return NULL;
6308 }
6309
6310 const ULONGEST namei_entry_offs
6311 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
6312 + namei * map.offset_size),
6313 map.offset_size, map.dwarf5_byte_order);
6314 return map.entry_pool + namei_entry_offs;
6315 }
6316
6317 /* See dw2_debug_names_iterator. */
6318
6319 dwarf2_per_cu_data *
6320 dw2_debug_names_iterator::next ()
6321 {
6322 if (m_addr == NULL)
6323 return NULL;
6324
6325 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
6326 struct objfile *objfile = dwarf2_per_objfile->objfile;
6327 bfd *const abfd = objfile->obfd;
6328
6329 again:
6330
6331 unsigned int bytes_read;
6332 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6333 m_addr += bytes_read;
6334 if (abbrev == 0)
6335 return NULL;
6336
6337 const auto indexval_it = m_map.abbrev_map.find (abbrev);
6338 if (indexval_it == m_map.abbrev_map.cend ())
6339 {
6340 complaint (&symfile_complaints,
6341 _("Wrong .debug_names undefined abbrev code %s "
6342 "[in module %s]"),
6343 pulongest (abbrev), objfile_name (objfile));
6344 return NULL;
6345 }
6346 const mapped_debug_names::index_val &indexval = indexval_it->second;
6347 bool have_is_static = false;
6348 bool is_static;
6349 dwarf2_per_cu_data *per_cu = NULL;
6350 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
6351 {
6352 ULONGEST ull;
6353 switch (attr.form)
6354 {
6355 case DW_FORM_implicit_const:
6356 ull = attr.implicit_const;
6357 break;
6358 case DW_FORM_flag_present:
6359 ull = 1;
6360 break;
6361 case DW_FORM_udata:
6362 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
6363 m_addr += bytes_read;
6364 break;
6365 default:
6366 complaint (&symfile_complaints,
6367 _("Unsupported .debug_names form %s [in module %s]"),
6368 dwarf_form_name (attr.form),
6369 objfile_name (objfile));
6370 return NULL;
6371 }
6372 switch (attr.dw_idx)
6373 {
6374 case DW_IDX_compile_unit:
6375 /* Don't crash on bad data. */
6376 if (ull >= dwarf2_per_objfile->n_comp_units)
6377 {
6378 complaint (&symfile_complaints,
6379 _(".debug_names entry has bad CU index %s"
6380 " [in module %s]"),
6381 pulongest (ull),
6382 objfile_name (dwarf2_per_objfile->objfile));
6383 continue;
6384 }
6385 per_cu = dw2_get_cutu (dwarf2_per_objfile, ull);
6386 break;
6387 case DW_IDX_type_unit:
6388 /* Don't crash on bad data. */
6389 if (ull >= dwarf2_per_objfile->n_type_units)
6390 {
6391 complaint (&symfile_complaints,
6392 _(".debug_names entry has bad TU index %s"
6393 " [in module %s]"),
6394 pulongest (ull),
6395 objfile_name (dwarf2_per_objfile->objfile));
6396 continue;
6397 }
6398 per_cu = dw2_get_cutu (dwarf2_per_objfile,
6399 dwarf2_per_objfile->n_comp_units + ull);
6400 break;
6401 case DW_IDX_GNU_internal:
6402 if (!m_map.augmentation_is_gdb)
6403 break;
6404 have_is_static = true;
6405 is_static = true;
6406 break;
6407 case DW_IDX_GNU_external:
6408 if (!m_map.augmentation_is_gdb)
6409 break;
6410 have_is_static = true;
6411 is_static = false;
6412 break;
6413 }
6414 }
6415
6416 /* Skip if already read in. */
6417 if (per_cu->v.quick->compunit_symtab)
6418 goto again;
6419
6420 /* Check static vs global. */
6421 if (have_is_static)
6422 {
6423 const bool want_static = m_block_index != GLOBAL_BLOCK;
6424 if (m_want_specific_block && want_static != is_static)
6425 goto again;
6426 }
6427
6428 /* Match dw2_symtab_iter_next, symbol_kind
6429 and debug_names::psymbol_tag. */
6430 switch (m_domain)
6431 {
6432 case VAR_DOMAIN:
6433 switch (indexval.dwarf_tag)
6434 {
6435 case DW_TAG_variable:
6436 case DW_TAG_subprogram:
6437 /* Some types are also in VAR_DOMAIN. */
6438 case DW_TAG_typedef:
6439 case DW_TAG_structure_type:
6440 break;
6441 default:
6442 goto again;
6443 }
6444 break;
6445 case STRUCT_DOMAIN:
6446 switch (indexval.dwarf_tag)
6447 {
6448 case DW_TAG_typedef:
6449 case DW_TAG_structure_type:
6450 break;
6451 default:
6452 goto again;
6453 }
6454 break;
6455 case LABEL_DOMAIN:
6456 switch (indexval.dwarf_tag)
6457 {
6458 case 0:
6459 case DW_TAG_variable:
6460 break;
6461 default:
6462 goto again;
6463 }
6464 break;
6465 default:
6466 break;
6467 }
6468
6469 /* Match dw2_expand_symtabs_matching, symbol_kind and
6470 debug_names::psymbol_tag. */
6471 switch (m_search)
6472 {
6473 case VARIABLES_DOMAIN:
6474 switch (indexval.dwarf_tag)
6475 {
6476 case DW_TAG_variable:
6477 break;
6478 default:
6479 goto again;
6480 }
6481 break;
6482 case FUNCTIONS_DOMAIN:
6483 switch (indexval.dwarf_tag)
6484 {
6485 case DW_TAG_subprogram:
6486 break;
6487 default:
6488 goto again;
6489 }
6490 break;
6491 case TYPES_DOMAIN:
6492 switch (indexval.dwarf_tag)
6493 {
6494 case DW_TAG_typedef:
6495 case DW_TAG_structure_type:
6496 break;
6497 default:
6498 goto again;
6499 }
6500 break;
6501 default:
6502 break;
6503 }
6504
6505 return per_cu;
6506 }
6507
6508 static struct compunit_symtab *
6509 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6510 const char *name, domain_enum domain)
6511 {
6512 const block_enum block_index = static_cast<block_enum> (block_index_int);
6513 struct dwarf2_per_objfile *dwarf2_per_objfile
6514 = get_dwarf2_per_objfile (objfile);
6515
6516 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6517 if (!mapp)
6518 {
6519 /* index is NULL if OBJF_READNOW. */
6520 return NULL;
6521 }
6522 const auto &map = *mapp;
6523
6524 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6525 block_index, domain, name);
6526
6527 struct compunit_symtab *stab_best = NULL;
6528 struct dwarf2_per_cu_data *per_cu;
6529 while ((per_cu = iter.next ()) != NULL)
6530 {
6531 struct symbol *sym, *with_opaque = NULL;
6532 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6533 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6534 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6535
6536 sym = block_find_symbol (block, name, domain,
6537 block_find_non_opaque_type_preferred,
6538 &with_opaque);
6539
6540 /* Some caution must be observed with overloaded functions and
6541 methods, since the index will not contain any overload
6542 information (but NAME might contain it). */
6543
6544 if (sym != NULL
6545 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6546 return stab;
6547 if (with_opaque != NULL
6548 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6549 stab_best = stab;
6550
6551 /* Keep looking through other CUs. */
6552 }
6553
6554 return stab_best;
6555 }
6556
6557 /* This dumps minimal information about .debug_names. It is called
6558 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6559 uses this to verify that .debug_names has been loaded. */
6560
6561 static void
6562 dw2_debug_names_dump (struct objfile *objfile)
6563 {
6564 struct dwarf2_per_objfile *dwarf2_per_objfile
6565 = get_dwarf2_per_objfile (objfile);
6566
6567 gdb_assert (dwarf2_per_objfile->using_index);
6568 printf_filtered (".debug_names:");
6569 if (dwarf2_per_objfile->debug_names_table)
6570 printf_filtered (" exists\n");
6571 else
6572 printf_filtered (" faked for \"readnow\"\n");
6573 printf_filtered ("\n");
6574 }
6575
6576 static void
6577 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6578 const char *func_name)
6579 {
6580 struct dwarf2_per_objfile *dwarf2_per_objfile
6581 = get_dwarf2_per_objfile (objfile);
6582
6583 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6584 if (dwarf2_per_objfile->debug_names_table)
6585 {
6586 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6587
6588 /* Note: It doesn't matter what we pass for block_index here. */
6589 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6590 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6591
6592 struct dwarf2_per_cu_data *per_cu;
6593 while ((per_cu = iter.next ()) != NULL)
6594 dw2_instantiate_symtab (per_cu);
6595 }
6596 }
6597
6598 static void
6599 dw2_debug_names_expand_symtabs_matching
6600 (struct objfile *objfile,
6601 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6602 const lookup_name_info &lookup_name,
6603 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6604 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6605 enum search_domain kind)
6606 {
6607 struct dwarf2_per_objfile *dwarf2_per_objfile
6608 = get_dwarf2_per_objfile (objfile);
6609
6610 /* debug_names_table is NULL if OBJF_READNOW. */
6611 if (!dwarf2_per_objfile->debug_names_table)
6612 return;
6613
6614 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6615
6616 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6617
6618 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6619 symbol_matcher,
6620 kind, [&] (offset_type namei)
6621 {
6622 /* The name was matched, now expand corresponding CUs that were
6623 marked. */
6624 dw2_debug_names_iterator iter (map, kind, namei);
6625
6626 struct dwarf2_per_cu_data *per_cu;
6627 while ((per_cu = iter.next ()) != NULL)
6628 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6629 expansion_notify);
6630 });
6631 }
6632
6633 const struct quick_symbol_functions dwarf2_debug_names_functions =
6634 {
6635 dw2_has_symbols,
6636 dw2_find_last_source_symtab,
6637 dw2_forget_cached_source_info,
6638 dw2_map_symtabs_matching_filename,
6639 dw2_debug_names_lookup_symbol,
6640 dw2_print_stats,
6641 dw2_debug_names_dump,
6642 dw2_relocate,
6643 dw2_debug_names_expand_symtabs_for_function,
6644 dw2_expand_all_symtabs,
6645 dw2_expand_symtabs_with_fullname,
6646 dw2_map_matching_symbols,
6647 dw2_debug_names_expand_symtabs_matching,
6648 dw2_find_pc_sect_compunit_symtab,
6649 NULL,
6650 dw2_map_symbol_filenames
6651 };
6652
6653 /* See symfile.h. */
6654
6655 bool
6656 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6657 {
6658 struct dwarf2_per_objfile *dwarf2_per_objfile
6659 = get_dwarf2_per_objfile (objfile);
6660
6661 /* If we're about to read full symbols, don't bother with the
6662 indices. In this case we also don't care if some other debug
6663 format is making psymtabs, because they are all about to be
6664 expanded anyway. */
6665 if ((objfile->flags & OBJF_READNOW))
6666 {
6667 int i;
6668
6669 dwarf2_per_objfile->using_index = 1;
6670 create_all_comp_units (dwarf2_per_objfile);
6671 create_all_type_units (dwarf2_per_objfile);
6672 dwarf2_per_objfile->quick_file_names_table =
6673 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6674
6675 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6676 + dwarf2_per_objfile->n_type_units); ++i)
6677 {
6678 dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
6679
6680 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6681 struct dwarf2_per_cu_quick_data);
6682 }
6683
6684 /* Return 1 so that gdb sees the "quick" functions. However,
6685 these functions will be no-ops because we will have expanded
6686 all symtabs. */
6687 *index_kind = dw_index_kind::GDB_INDEX;
6688 return true;
6689 }
6690
6691 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6692 {
6693 *index_kind = dw_index_kind::DEBUG_NAMES;
6694 return true;
6695 }
6696
6697 if (dwarf2_read_index (objfile))
6698 {
6699 *index_kind = dw_index_kind::GDB_INDEX;
6700 return true;
6701 }
6702
6703 return false;
6704 }
6705
6706 \f
6707
6708 /* Build a partial symbol table. */
6709
6710 void
6711 dwarf2_build_psymtabs (struct objfile *objfile)
6712 {
6713 struct dwarf2_per_objfile *dwarf2_per_objfile
6714 = get_dwarf2_per_objfile (objfile);
6715
6716 if (objfile->global_psymbols.capacity () == 0
6717 && objfile->static_psymbols.capacity () == 0)
6718 init_psymbol_list (objfile, 1024);
6719
6720 TRY
6721 {
6722 /* This isn't really ideal: all the data we allocate on the
6723 objfile's obstack is still uselessly kept around. However,
6724 freeing it seems unsafe. */
6725 psymtab_discarder psymtabs (objfile);
6726 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6727 psymtabs.keep ();
6728 }
6729 CATCH (except, RETURN_MASK_ERROR)
6730 {
6731 exception_print (gdb_stderr, except);
6732 }
6733 END_CATCH
6734 }
6735
6736 /* Return the total length of the CU described by HEADER. */
6737
6738 static unsigned int
6739 get_cu_length (const struct comp_unit_head *header)
6740 {
6741 return header->initial_length_size + header->length;
6742 }
6743
6744 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6745
6746 static inline bool
6747 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6748 {
6749 sect_offset bottom = cu_header->sect_off;
6750 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6751
6752 return sect_off >= bottom && sect_off < top;
6753 }
6754
6755 /* Find the base address of the compilation unit for range lists and
6756 location lists. It will normally be specified by DW_AT_low_pc.
6757 In DWARF-3 draft 4, the base address could be overridden by
6758 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6759 compilation units with discontinuous ranges. */
6760
6761 static void
6762 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6763 {
6764 struct attribute *attr;
6765
6766 cu->base_known = 0;
6767 cu->base_address = 0;
6768
6769 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6770 if (attr)
6771 {
6772 cu->base_address = attr_value_as_address (attr);
6773 cu->base_known = 1;
6774 }
6775 else
6776 {
6777 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6778 if (attr)
6779 {
6780 cu->base_address = attr_value_as_address (attr);
6781 cu->base_known = 1;
6782 }
6783 }
6784 }
6785
6786 /* Read in the comp unit header information from the debug_info at info_ptr.
6787 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6788 NOTE: This leaves members offset, first_die_offset to be filled in
6789 by the caller. */
6790
6791 static const gdb_byte *
6792 read_comp_unit_head (struct comp_unit_head *cu_header,
6793 const gdb_byte *info_ptr,
6794 struct dwarf2_section_info *section,
6795 rcuh_kind section_kind)
6796 {
6797 int signed_addr;
6798 unsigned int bytes_read;
6799 const char *filename = get_section_file_name (section);
6800 bfd *abfd = get_section_bfd_owner (section);
6801
6802 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6803 cu_header->initial_length_size = bytes_read;
6804 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6805 info_ptr += bytes_read;
6806 cu_header->version = read_2_bytes (abfd, info_ptr);
6807 info_ptr += 2;
6808 if (cu_header->version < 5)
6809 switch (section_kind)
6810 {
6811 case rcuh_kind::COMPILE:
6812 cu_header->unit_type = DW_UT_compile;
6813 break;
6814 case rcuh_kind::TYPE:
6815 cu_header->unit_type = DW_UT_type;
6816 break;
6817 default:
6818 internal_error (__FILE__, __LINE__,
6819 _("read_comp_unit_head: invalid section_kind"));
6820 }
6821 else
6822 {
6823 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6824 (read_1_byte (abfd, info_ptr));
6825 info_ptr += 1;
6826 switch (cu_header->unit_type)
6827 {
6828 case DW_UT_compile:
6829 if (section_kind != rcuh_kind::COMPILE)
6830 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6831 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6832 filename);
6833 break;
6834 case DW_UT_type:
6835 section_kind = rcuh_kind::TYPE;
6836 break;
6837 default:
6838 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6839 "(is %d, should be %d or %d) [in module %s]"),
6840 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6841 }
6842
6843 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6844 info_ptr += 1;
6845 }
6846 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6847 cu_header,
6848 &bytes_read);
6849 info_ptr += bytes_read;
6850 if (cu_header->version < 5)
6851 {
6852 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6853 info_ptr += 1;
6854 }
6855 signed_addr = bfd_get_sign_extend_vma (abfd);
6856 if (signed_addr < 0)
6857 internal_error (__FILE__, __LINE__,
6858 _("read_comp_unit_head: dwarf from non elf file"));
6859 cu_header->signed_addr_p = signed_addr;
6860
6861 if (section_kind == rcuh_kind::TYPE)
6862 {
6863 LONGEST type_offset;
6864
6865 cu_header->signature = read_8_bytes (abfd, info_ptr);
6866 info_ptr += 8;
6867
6868 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6869 info_ptr += bytes_read;
6870 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6871 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6872 error (_("Dwarf Error: Too big type_offset in compilation unit "
6873 "header (is %s) [in module %s]"), plongest (type_offset),
6874 filename);
6875 }
6876
6877 return info_ptr;
6878 }
6879
6880 /* Helper function that returns the proper abbrev section for
6881 THIS_CU. */
6882
6883 static struct dwarf2_section_info *
6884 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6885 {
6886 struct dwarf2_section_info *abbrev;
6887 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6888
6889 if (this_cu->is_dwz)
6890 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6891 else
6892 abbrev = &dwarf2_per_objfile->abbrev;
6893
6894 return abbrev;
6895 }
6896
6897 /* Subroutine of read_and_check_comp_unit_head and
6898 read_and_check_type_unit_head to simplify them.
6899 Perform various error checking on the header. */
6900
6901 static void
6902 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6903 struct comp_unit_head *header,
6904 struct dwarf2_section_info *section,
6905 struct dwarf2_section_info *abbrev_section)
6906 {
6907 const char *filename = get_section_file_name (section);
6908
6909 if (header->version < 2 || header->version > 5)
6910 error (_("Dwarf Error: wrong version in compilation unit header "
6911 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6912 filename);
6913
6914 if (to_underlying (header->abbrev_sect_off)
6915 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6916 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6917 "(offset %s + 6) [in module %s]"),
6918 sect_offset_str (header->abbrev_sect_off),
6919 sect_offset_str (header->sect_off),
6920 filename);
6921
6922 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6923 avoid potential 32-bit overflow. */
6924 if (((ULONGEST) header->sect_off + get_cu_length (header))
6925 > section->size)
6926 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6927 "(offset %s + 0) [in module %s]"),
6928 header->length, sect_offset_str (header->sect_off),
6929 filename);
6930 }
6931
6932 /* Read in a CU/TU header and perform some basic error checking.
6933 The contents of the header are stored in HEADER.
6934 The result is a pointer to the start of the first DIE. */
6935
6936 static const gdb_byte *
6937 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6938 struct comp_unit_head *header,
6939 struct dwarf2_section_info *section,
6940 struct dwarf2_section_info *abbrev_section,
6941 const gdb_byte *info_ptr,
6942 rcuh_kind section_kind)
6943 {
6944 const gdb_byte *beg_of_comp_unit = info_ptr;
6945
6946 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6947
6948 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6949
6950 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6951
6952 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6953 abbrev_section);
6954
6955 return info_ptr;
6956 }
6957
6958 /* Fetch the abbreviation table offset from a comp or type unit header. */
6959
6960 static sect_offset
6961 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6962 struct dwarf2_section_info *section,
6963 sect_offset sect_off)
6964 {
6965 bfd *abfd = get_section_bfd_owner (section);
6966 const gdb_byte *info_ptr;
6967 unsigned int initial_length_size, offset_size;
6968 uint16_t version;
6969
6970 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6971 info_ptr = section->buffer + to_underlying (sect_off);
6972 read_initial_length (abfd, info_ptr, &initial_length_size);
6973 offset_size = initial_length_size == 4 ? 4 : 8;
6974 info_ptr += initial_length_size;
6975
6976 version = read_2_bytes (abfd, info_ptr);
6977 info_ptr += 2;
6978 if (version >= 5)
6979 {
6980 /* Skip unit type and address size. */
6981 info_ptr += 2;
6982 }
6983
6984 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6985 }
6986
6987 /* Allocate a new partial symtab for file named NAME and mark this new
6988 partial symtab as being an include of PST. */
6989
6990 static void
6991 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6992 struct objfile *objfile)
6993 {
6994 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6995
6996 if (!IS_ABSOLUTE_PATH (subpst->filename))
6997 {
6998 /* It shares objfile->objfile_obstack. */
6999 subpst->dirname = pst->dirname;
7000 }
7001
7002 subpst->textlow = 0;
7003 subpst->texthigh = 0;
7004
7005 subpst->dependencies
7006 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
7007 subpst->dependencies[0] = pst;
7008 subpst->number_of_dependencies = 1;
7009
7010 subpst->globals_offset = 0;
7011 subpst->n_global_syms = 0;
7012 subpst->statics_offset = 0;
7013 subpst->n_static_syms = 0;
7014 subpst->compunit_symtab = NULL;
7015 subpst->read_symtab = pst->read_symtab;
7016 subpst->readin = 0;
7017
7018 /* No private part is necessary for include psymtabs. This property
7019 can be used to differentiate between such include psymtabs and
7020 the regular ones. */
7021 subpst->read_symtab_private = NULL;
7022 }
7023
7024 /* Read the Line Number Program data and extract the list of files
7025 included by the source file represented by PST. Build an include
7026 partial symtab for each of these included files. */
7027
7028 static void
7029 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
7030 struct die_info *die,
7031 struct partial_symtab *pst)
7032 {
7033 line_header_up lh;
7034 struct attribute *attr;
7035
7036 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7037 if (attr)
7038 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
7039 if (lh == NULL)
7040 return; /* No linetable, so no includes. */
7041
7042 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
7043 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
7044 }
7045
7046 static hashval_t
7047 hash_signatured_type (const void *item)
7048 {
7049 const struct signatured_type *sig_type
7050 = (const struct signatured_type *) item;
7051
7052 /* This drops the top 32 bits of the signature, but is ok for a hash. */
7053 return sig_type->signature;
7054 }
7055
7056 static int
7057 eq_signatured_type (const void *item_lhs, const void *item_rhs)
7058 {
7059 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
7060 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
7061
7062 return lhs->signature == rhs->signature;
7063 }
7064
7065 /* Allocate a hash table for signatured types. */
7066
7067 static htab_t
7068 allocate_signatured_type_table (struct objfile *objfile)
7069 {
7070 return htab_create_alloc_ex (41,
7071 hash_signatured_type,
7072 eq_signatured_type,
7073 NULL,
7074 &objfile->objfile_obstack,
7075 hashtab_obstack_allocate,
7076 dummy_obstack_deallocate);
7077 }
7078
7079 /* A helper function to add a signatured type CU to a table. */
7080
7081 static int
7082 add_signatured_type_cu_to_table (void **slot, void *datum)
7083 {
7084 struct signatured_type *sigt = (struct signatured_type *) *slot;
7085 struct signatured_type ***datap = (struct signatured_type ***) datum;
7086
7087 **datap = sigt;
7088 ++*datap;
7089
7090 return 1;
7091 }
7092
7093 /* A helper for create_debug_types_hash_table. Read types from SECTION
7094 and fill them into TYPES_HTAB. It will process only type units,
7095 therefore DW_UT_type. */
7096
7097 static void
7098 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7099 struct dwo_file *dwo_file,
7100 dwarf2_section_info *section, htab_t &types_htab,
7101 rcuh_kind section_kind)
7102 {
7103 struct objfile *objfile = dwarf2_per_objfile->objfile;
7104 struct dwarf2_section_info *abbrev_section;
7105 bfd *abfd;
7106 const gdb_byte *info_ptr, *end_ptr;
7107
7108 abbrev_section = (dwo_file != NULL
7109 ? &dwo_file->sections.abbrev
7110 : &dwarf2_per_objfile->abbrev);
7111
7112 if (dwarf_read_debug)
7113 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
7114 get_section_name (section),
7115 get_section_file_name (abbrev_section));
7116
7117 dwarf2_read_section (objfile, section);
7118 info_ptr = section->buffer;
7119
7120 if (info_ptr == NULL)
7121 return;
7122
7123 /* We can't set abfd until now because the section may be empty or
7124 not present, in which case the bfd is unknown. */
7125 abfd = get_section_bfd_owner (section);
7126
7127 /* We don't use init_cutu_and_read_dies_simple, or some such, here
7128 because we don't need to read any dies: the signature is in the
7129 header. */
7130
7131 end_ptr = info_ptr + section->size;
7132 while (info_ptr < end_ptr)
7133 {
7134 struct signatured_type *sig_type;
7135 struct dwo_unit *dwo_tu;
7136 void **slot;
7137 const gdb_byte *ptr = info_ptr;
7138 struct comp_unit_head header;
7139 unsigned int length;
7140
7141 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
7142
7143 /* Initialize it due to a false compiler warning. */
7144 header.signature = -1;
7145 header.type_cu_offset_in_tu = (cu_offset) -1;
7146
7147 /* We need to read the type's signature in order to build the hash
7148 table, but we don't need anything else just yet. */
7149
7150 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
7151 abbrev_section, ptr, section_kind);
7152
7153 length = get_cu_length (&header);
7154
7155 /* Skip dummy type units. */
7156 if (ptr >= info_ptr + length
7157 || peek_abbrev_code (abfd, ptr) == 0
7158 || header.unit_type != DW_UT_type)
7159 {
7160 info_ptr += length;
7161 continue;
7162 }
7163
7164 if (types_htab == NULL)
7165 {
7166 if (dwo_file)
7167 types_htab = allocate_dwo_unit_table (objfile);
7168 else
7169 types_htab = allocate_signatured_type_table (objfile);
7170 }
7171
7172 if (dwo_file)
7173 {
7174 sig_type = NULL;
7175 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7176 struct dwo_unit);
7177 dwo_tu->dwo_file = dwo_file;
7178 dwo_tu->signature = header.signature;
7179 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
7180 dwo_tu->section = section;
7181 dwo_tu->sect_off = sect_off;
7182 dwo_tu->length = length;
7183 }
7184 else
7185 {
7186 /* N.B.: type_offset is not usable if this type uses a DWO file.
7187 The real type_offset is in the DWO file. */
7188 dwo_tu = NULL;
7189 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7190 struct signatured_type);
7191 sig_type->signature = header.signature;
7192 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
7193 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7194 sig_type->per_cu.is_debug_types = 1;
7195 sig_type->per_cu.section = section;
7196 sig_type->per_cu.sect_off = sect_off;
7197 sig_type->per_cu.length = length;
7198 }
7199
7200 slot = htab_find_slot (types_htab,
7201 dwo_file ? (void*) dwo_tu : (void *) sig_type,
7202 INSERT);
7203 gdb_assert (slot != NULL);
7204 if (*slot != NULL)
7205 {
7206 sect_offset dup_sect_off;
7207
7208 if (dwo_file)
7209 {
7210 const struct dwo_unit *dup_tu
7211 = (const struct dwo_unit *) *slot;
7212
7213 dup_sect_off = dup_tu->sect_off;
7214 }
7215 else
7216 {
7217 const struct signatured_type *dup_tu
7218 = (const struct signatured_type *) *slot;
7219
7220 dup_sect_off = dup_tu->per_cu.sect_off;
7221 }
7222
7223 complaint (&symfile_complaints,
7224 _("debug type entry at offset %s is duplicate to"
7225 " the entry at offset %s, signature %s"),
7226 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7227 hex_string (header.signature));
7228 }
7229 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
7230
7231 if (dwarf_read_debug > 1)
7232 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
7233 sect_offset_str (sect_off),
7234 hex_string (header.signature));
7235
7236 info_ptr += length;
7237 }
7238 }
7239
7240 /* Create the hash table of all entries in the .debug_types
7241 (or .debug_types.dwo) section(s).
7242 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
7243 otherwise it is NULL.
7244
7245 The result is a pointer to the hash table or NULL if there are no types.
7246
7247 Note: This function processes DWO files only, not DWP files. */
7248
7249 static void
7250 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
7251 struct dwo_file *dwo_file,
7252 VEC (dwarf2_section_info_def) *types,
7253 htab_t &types_htab)
7254 {
7255 int ix;
7256 struct dwarf2_section_info *section;
7257
7258 if (VEC_empty (dwarf2_section_info_def, types))
7259 return;
7260
7261 for (ix = 0;
7262 VEC_iterate (dwarf2_section_info_def, types, ix, section);
7263 ++ix)
7264 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
7265 types_htab, rcuh_kind::TYPE);
7266 }
7267
7268 /* Create the hash table of all entries in the .debug_types section,
7269 and initialize all_type_units.
7270 The result is zero if there is an error (e.g. missing .debug_types section),
7271 otherwise non-zero. */
7272
7273 static int
7274 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7275 {
7276 htab_t types_htab = NULL;
7277 struct signatured_type **iter;
7278
7279 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
7280 &dwarf2_per_objfile->info, types_htab,
7281 rcuh_kind::COMPILE);
7282 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
7283 dwarf2_per_objfile->types, types_htab);
7284 if (types_htab == NULL)
7285 {
7286 dwarf2_per_objfile->signatured_types = NULL;
7287 return 0;
7288 }
7289
7290 dwarf2_per_objfile->signatured_types = types_htab;
7291
7292 dwarf2_per_objfile->n_type_units
7293 = dwarf2_per_objfile->n_allocated_type_units
7294 = htab_elements (types_htab);
7295 dwarf2_per_objfile->all_type_units =
7296 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
7297 iter = &dwarf2_per_objfile->all_type_units[0];
7298 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
7299 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
7300 == dwarf2_per_objfile->n_type_units);
7301
7302 return 1;
7303 }
7304
7305 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
7306 If SLOT is non-NULL, it is the entry to use in the hash table.
7307 Otherwise we find one. */
7308
7309 static struct signatured_type *
7310 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
7311 void **slot)
7312 {
7313 struct objfile *objfile = dwarf2_per_objfile->objfile;
7314 int n_type_units = dwarf2_per_objfile->n_type_units;
7315 struct signatured_type *sig_type;
7316
7317 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
7318 ++n_type_units;
7319 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
7320 {
7321 if (dwarf2_per_objfile->n_allocated_type_units == 0)
7322 dwarf2_per_objfile->n_allocated_type_units = 1;
7323 dwarf2_per_objfile->n_allocated_type_units *= 2;
7324 dwarf2_per_objfile->all_type_units
7325 = XRESIZEVEC (struct signatured_type *,
7326 dwarf2_per_objfile->all_type_units,
7327 dwarf2_per_objfile->n_allocated_type_units);
7328 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7329 }
7330 dwarf2_per_objfile->n_type_units = n_type_units;
7331
7332 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7333 struct signatured_type);
7334 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
7335 sig_type->signature = sig;
7336 sig_type->per_cu.is_debug_types = 1;
7337 if (dwarf2_per_objfile->using_index)
7338 {
7339 sig_type->per_cu.v.quick =
7340 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7341 struct dwarf2_per_cu_quick_data);
7342 }
7343
7344 if (slot == NULL)
7345 {
7346 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7347 sig_type, INSERT);
7348 }
7349 gdb_assert (*slot == NULL);
7350 *slot = sig_type;
7351 /* The rest of sig_type must be filled in by the caller. */
7352 return sig_type;
7353 }
7354
7355 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7356 Fill in SIG_ENTRY with DWO_ENTRY. */
7357
7358 static void
7359 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7360 struct signatured_type *sig_entry,
7361 struct dwo_unit *dwo_entry)
7362 {
7363 /* Make sure we're not clobbering something we don't expect to. */
7364 gdb_assert (! sig_entry->per_cu.queued);
7365 gdb_assert (sig_entry->per_cu.cu == NULL);
7366 if (dwarf2_per_objfile->using_index)
7367 {
7368 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7369 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7370 }
7371 else
7372 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7373 gdb_assert (sig_entry->signature == dwo_entry->signature);
7374 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7375 gdb_assert (sig_entry->type_unit_group == NULL);
7376 gdb_assert (sig_entry->dwo_unit == NULL);
7377
7378 sig_entry->per_cu.section = dwo_entry->section;
7379 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7380 sig_entry->per_cu.length = dwo_entry->length;
7381 sig_entry->per_cu.reading_dwo_directly = 1;
7382 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7383 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7384 sig_entry->dwo_unit = dwo_entry;
7385 }
7386
7387 /* Subroutine of lookup_signatured_type.
7388 If we haven't read the TU yet, create the signatured_type data structure
7389 for a TU to be read in directly from a DWO file, bypassing the stub.
7390 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7391 using .gdb_index, then when reading a CU we want to stay in the DWO file
7392 containing that CU. Otherwise we could end up reading several other DWO
7393 files (due to comdat folding) to process the transitive closure of all the
7394 mentioned TUs, and that can be slow. The current DWO file will have every
7395 type signature that it needs.
7396 We only do this for .gdb_index because in the psymtab case we already have
7397 to read all the DWOs to build the type unit groups. */
7398
7399 static struct signatured_type *
7400 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7401 {
7402 struct dwarf2_per_objfile *dwarf2_per_objfile
7403 = cu->per_cu->dwarf2_per_objfile;
7404 struct objfile *objfile = dwarf2_per_objfile->objfile;
7405 struct dwo_file *dwo_file;
7406 struct dwo_unit find_dwo_entry, *dwo_entry;
7407 struct signatured_type find_sig_entry, *sig_entry;
7408 void **slot;
7409
7410 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7411
7412 /* If TU skeletons have been removed then we may not have read in any
7413 TUs yet. */
7414 if (dwarf2_per_objfile->signatured_types == NULL)
7415 {
7416 dwarf2_per_objfile->signatured_types
7417 = allocate_signatured_type_table (objfile);
7418 }
7419
7420 /* We only ever need to read in one copy of a signatured type.
7421 Use the global signatured_types array to do our own comdat-folding
7422 of types. If this is the first time we're reading this TU, and
7423 the TU has an entry in .gdb_index, replace the recorded data from
7424 .gdb_index with this TU. */
7425
7426 find_sig_entry.signature = sig;
7427 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7428 &find_sig_entry, INSERT);
7429 sig_entry = (struct signatured_type *) *slot;
7430
7431 /* We can get here with the TU already read, *or* in the process of being
7432 read. Don't reassign the global entry to point to this DWO if that's
7433 the case. Also note that if the TU is already being read, it may not
7434 have come from a DWO, the program may be a mix of Fission-compiled
7435 code and non-Fission-compiled code. */
7436
7437 /* Have we already tried to read this TU?
7438 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7439 needn't exist in the global table yet). */
7440 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7441 return sig_entry;
7442
7443 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7444 dwo_unit of the TU itself. */
7445 dwo_file = cu->dwo_unit->dwo_file;
7446
7447 /* Ok, this is the first time we're reading this TU. */
7448 if (dwo_file->tus == NULL)
7449 return NULL;
7450 find_dwo_entry.signature = sig;
7451 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7452 if (dwo_entry == NULL)
7453 return NULL;
7454
7455 /* If the global table doesn't have an entry for this TU, add one. */
7456 if (sig_entry == NULL)
7457 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7458
7459 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7460 sig_entry->per_cu.tu_read = 1;
7461 return sig_entry;
7462 }
7463
7464 /* Subroutine of lookup_signatured_type.
7465 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7466 then try the DWP file. If the TU stub (skeleton) has been removed then
7467 it won't be in .gdb_index. */
7468
7469 static struct signatured_type *
7470 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7471 {
7472 struct dwarf2_per_objfile *dwarf2_per_objfile
7473 = cu->per_cu->dwarf2_per_objfile;
7474 struct objfile *objfile = dwarf2_per_objfile->objfile;
7475 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7476 struct dwo_unit *dwo_entry;
7477 struct signatured_type find_sig_entry, *sig_entry;
7478 void **slot;
7479
7480 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7481 gdb_assert (dwp_file != NULL);
7482
7483 /* If TU skeletons have been removed then we may not have read in any
7484 TUs yet. */
7485 if (dwarf2_per_objfile->signatured_types == NULL)
7486 {
7487 dwarf2_per_objfile->signatured_types
7488 = allocate_signatured_type_table (objfile);
7489 }
7490
7491 find_sig_entry.signature = sig;
7492 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7493 &find_sig_entry, INSERT);
7494 sig_entry = (struct signatured_type *) *slot;
7495
7496 /* Have we already tried to read this TU?
7497 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7498 needn't exist in the global table yet). */
7499 if (sig_entry != NULL)
7500 return sig_entry;
7501
7502 if (dwp_file->tus == NULL)
7503 return NULL;
7504 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7505 sig, 1 /* is_debug_types */);
7506 if (dwo_entry == NULL)
7507 return NULL;
7508
7509 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7510 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7511
7512 return sig_entry;
7513 }
7514
7515 /* Lookup a signature based type for DW_FORM_ref_sig8.
7516 Returns NULL if signature SIG is not present in the table.
7517 It is up to the caller to complain about this. */
7518
7519 static struct signatured_type *
7520 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7521 {
7522 struct dwarf2_per_objfile *dwarf2_per_objfile
7523 = cu->per_cu->dwarf2_per_objfile;
7524
7525 if (cu->dwo_unit
7526 && dwarf2_per_objfile->using_index)
7527 {
7528 /* We're in a DWO/DWP file, and we're using .gdb_index.
7529 These cases require special processing. */
7530 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7531 return lookup_dwo_signatured_type (cu, sig);
7532 else
7533 return lookup_dwp_signatured_type (cu, sig);
7534 }
7535 else
7536 {
7537 struct signatured_type find_entry, *entry;
7538
7539 if (dwarf2_per_objfile->signatured_types == NULL)
7540 return NULL;
7541 find_entry.signature = sig;
7542 entry = ((struct signatured_type *)
7543 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7544 return entry;
7545 }
7546 }
7547 \f
7548 /* Low level DIE reading support. */
7549
7550 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7551
7552 static void
7553 init_cu_die_reader (struct die_reader_specs *reader,
7554 struct dwarf2_cu *cu,
7555 struct dwarf2_section_info *section,
7556 struct dwo_file *dwo_file,
7557 struct abbrev_table *abbrev_table)
7558 {
7559 gdb_assert (section->readin && section->buffer != NULL);
7560 reader->abfd = get_section_bfd_owner (section);
7561 reader->cu = cu;
7562 reader->dwo_file = dwo_file;
7563 reader->die_section = section;
7564 reader->buffer = section->buffer;
7565 reader->buffer_end = section->buffer + section->size;
7566 reader->comp_dir = NULL;
7567 reader->abbrev_table = abbrev_table;
7568 }
7569
7570 /* Subroutine of init_cutu_and_read_dies to simplify it.
7571 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7572 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7573 already.
7574
7575 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7576 from it to the DIE in the DWO. If NULL we are skipping the stub.
7577 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7578 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7579 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7580 STUB_COMP_DIR may be non-NULL.
7581 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7582 are filled in with the info of the DIE from the DWO file.
7583 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7584 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7585 kept around for at least as long as *RESULT_READER.
7586
7587 The result is non-zero if a valid (non-dummy) DIE was found. */
7588
7589 static int
7590 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7591 struct dwo_unit *dwo_unit,
7592 struct die_info *stub_comp_unit_die,
7593 const char *stub_comp_dir,
7594 struct die_reader_specs *result_reader,
7595 const gdb_byte **result_info_ptr,
7596 struct die_info **result_comp_unit_die,
7597 int *result_has_children,
7598 abbrev_table_up *result_dwo_abbrev_table)
7599 {
7600 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7601 struct objfile *objfile = dwarf2_per_objfile->objfile;
7602 struct dwarf2_cu *cu = this_cu->cu;
7603 bfd *abfd;
7604 const gdb_byte *begin_info_ptr, *info_ptr;
7605 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7606 int i,num_extra_attrs;
7607 struct dwarf2_section_info *dwo_abbrev_section;
7608 struct attribute *attr;
7609 struct die_info *comp_unit_die;
7610
7611 /* At most one of these may be provided. */
7612 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7613
7614 /* These attributes aren't processed until later:
7615 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7616 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7617 referenced later. However, these attributes are found in the stub
7618 which we won't have later. In order to not impose this complication
7619 on the rest of the code, we read them here and copy them to the
7620 DWO CU/TU die. */
7621
7622 stmt_list = NULL;
7623 low_pc = NULL;
7624 high_pc = NULL;
7625 ranges = NULL;
7626 comp_dir = NULL;
7627
7628 if (stub_comp_unit_die != NULL)
7629 {
7630 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7631 DWO file. */
7632 if (! this_cu->is_debug_types)
7633 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7634 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7635 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7636 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7637 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7638
7639 /* There should be a DW_AT_addr_base attribute here (if needed).
7640 We need the value before we can process DW_FORM_GNU_addr_index. */
7641 cu->addr_base = 0;
7642 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7643 if (attr)
7644 cu->addr_base = DW_UNSND (attr);
7645
7646 /* There should be a DW_AT_ranges_base attribute here (if needed).
7647 We need the value before we can process DW_AT_ranges. */
7648 cu->ranges_base = 0;
7649 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7650 if (attr)
7651 cu->ranges_base = DW_UNSND (attr);
7652 }
7653 else if (stub_comp_dir != NULL)
7654 {
7655 /* Reconstruct the comp_dir attribute to simplify the code below. */
7656 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7657 comp_dir->name = DW_AT_comp_dir;
7658 comp_dir->form = DW_FORM_string;
7659 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7660 DW_STRING (comp_dir) = stub_comp_dir;
7661 }
7662
7663 /* Set up for reading the DWO CU/TU. */
7664 cu->dwo_unit = dwo_unit;
7665 dwarf2_section_info *section = dwo_unit->section;
7666 dwarf2_read_section (objfile, section);
7667 abfd = get_section_bfd_owner (section);
7668 begin_info_ptr = info_ptr = (section->buffer
7669 + to_underlying (dwo_unit->sect_off));
7670 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7671
7672 if (this_cu->is_debug_types)
7673 {
7674 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7675
7676 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7677 &cu->header, section,
7678 dwo_abbrev_section,
7679 info_ptr, rcuh_kind::TYPE);
7680 /* This is not an assert because it can be caused by bad debug info. */
7681 if (sig_type->signature != cu->header.signature)
7682 {
7683 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7684 " TU at offset %s [in module %s]"),
7685 hex_string (sig_type->signature),
7686 hex_string (cu->header.signature),
7687 sect_offset_str (dwo_unit->sect_off),
7688 bfd_get_filename (abfd));
7689 }
7690 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7691 /* For DWOs coming from DWP files, we don't know the CU length
7692 nor the type's offset in the TU until now. */
7693 dwo_unit->length = get_cu_length (&cu->header);
7694 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7695
7696 /* Establish the type offset that can be used to lookup the type.
7697 For DWO files, we don't know it until now. */
7698 sig_type->type_offset_in_section
7699 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7700 }
7701 else
7702 {
7703 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7704 &cu->header, section,
7705 dwo_abbrev_section,
7706 info_ptr, rcuh_kind::COMPILE);
7707 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7708 /* For DWOs coming from DWP files, we don't know the CU length
7709 until now. */
7710 dwo_unit->length = get_cu_length (&cu->header);
7711 }
7712
7713 *result_dwo_abbrev_table
7714 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7715 cu->header.abbrev_sect_off);
7716 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7717 result_dwo_abbrev_table->get ());
7718
7719 /* Read in the die, but leave space to copy over the attributes
7720 from the stub. This has the benefit of simplifying the rest of
7721 the code - all the work to maintain the illusion of a single
7722 DW_TAG_{compile,type}_unit DIE is done here. */
7723 num_extra_attrs = ((stmt_list != NULL)
7724 + (low_pc != NULL)
7725 + (high_pc != NULL)
7726 + (ranges != NULL)
7727 + (comp_dir != NULL));
7728 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7729 result_has_children, num_extra_attrs);
7730
7731 /* Copy over the attributes from the stub to the DIE we just read in. */
7732 comp_unit_die = *result_comp_unit_die;
7733 i = comp_unit_die->num_attrs;
7734 if (stmt_list != NULL)
7735 comp_unit_die->attrs[i++] = *stmt_list;
7736 if (low_pc != NULL)
7737 comp_unit_die->attrs[i++] = *low_pc;
7738 if (high_pc != NULL)
7739 comp_unit_die->attrs[i++] = *high_pc;
7740 if (ranges != NULL)
7741 comp_unit_die->attrs[i++] = *ranges;
7742 if (comp_dir != NULL)
7743 comp_unit_die->attrs[i++] = *comp_dir;
7744 comp_unit_die->num_attrs += num_extra_attrs;
7745
7746 if (dwarf_die_debug)
7747 {
7748 fprintf_unfiltered (gdb_stdlog,
7749 "Read die from %s@0x%x of %s:\n",
7750 get_section_name (section),
7751 (unsigned) (begin_info_ptr - section->buffer),
7752 bfd_get_filename (abfd));
7753 dump_die (comp_unit_die, dwarf_die_debug);
7754 }
7755
7756 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7757 TUs by skipping the stub and going directly to the entry in the DWO file.
7758 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7759 to get it via circuitous means. Blech. */
7760 if (comp_dir != NULL)
7761 result_reader->comp_dir = DW_STRING (comp_dir);
7762
7763 /* Skip dummy compilation units. */
7764 if (info_ptr >= begin_info_ptr + dwo_unit->length
7765 || peek_abbrev_code (abfd, info_ptr) == 0)
7766 return 0;
7767
7768 *result_info_ptr = info_ptr;
7769 return 1;
7770 }
7771
7772 /* Subroutine of init_cutu_and_read_dies to simplify it.
7773 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7774 Returns NULL if the specified DWO unit cannot be found. */
7775
7776 static struct dwo_unit *
7777 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7778 struct die_info *comp_unit_die)
7779 {
7780 struct dwarf2_cu *cu = this_cu->cu;
7781 ULONGEST signature;
7782 struct dwo_unit *dwo_unit;
7783 const char *comp_dir, *dwo_name;
7784
7785 gdb_assert (cu != NULL);
7786
7787 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7788 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7789 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7790
7791 if (this_cu->is_debug_types)
7792 {
7793 struct signatured_type *sig_type;
7794
7795 /* Since this_cu is the first member of struct signatured_type,
7796 we can go from a pointer to one to a pointer to the other. */
7797 sig_type = (struct signatured_type *) this_cu;
7798 signature = sig_type->signature;
7799 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7800 }
7801 else
7802 {
7803 struct attribute *attr;
7804
7805 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7806 if (! attr)
7807 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7808 " [in module %s]"),
7809 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7810 signature = DW_UNSND (attr);
7811 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7812 signature);
7813 }
7814
7815 return dwo_unit;
7816 }
7817
7818 /* Subroutine of init_cutu_and_read_dies to simplify it.
7819 See it for a description of the parameters.
7820 Read a TU directly from a DWO file, bypassing the stub. */
7821
7822 static void
7823 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7824 int use_existing_cu, int keep,
7825 die_reader_func_ftype *die_reader_func,
7826 void *data)
7827 {
7828 std::unique_ptr<dwarf2_cu> new_cu;
7829 struct signatured_type *sig_type;
7830 struct die_reader_specs reader;
7831 const gdb_byte *info_ptr;
7832 struct die_info *comp_unit_die;
7833 int has_children;
7834 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7835
7836 /* Verify we can do the following downcast, and that we have the
7837 data we need. */
7838 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7839 sig_type = (struct signatured_type *) this_cu;
7840 gdb_assert (sig_type->dwo_unit != NULL);
7841
7842 if (use_existing_cu && this_cu->cu != NULL)
7843 {
7844 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7845 /* There's no need to do the rereading_dwo_cu handling that
7846 init_cutu_and_read_dies does since we don't read the stub. */
7847 }
7848 else
7849 {
7850 /* If !use_existing_cu, this_cu->cu must be NULL. */
7851 gdb_assert (this_cu->cu == NULL);
7852 new_cu.reset (new dwarf2_cu (this_cu));
7853 }
7854
7855 /* A future optimization, if needed, would be to use an existing
7856 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7857 could share abbrev tables. */
7858
7859 /* The abbreviation table used by READER, this must live at least as long as
7860 READER. */
7861 abbrev_table_up dwo_abbrev_table;
7862
7863 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7864 NULL /* stub_comp_unit_die */,
7865 sig_type->dwo_unit->dwo_file->comp_dir,
7866 &reader, &info_ptr,
7867 &comp_unit_die, &has_children,
7868 &dwo_abbrev_table) == 0)
7869 {
7870 /* Dummy die. */
7871 return;
7872 }
7873
7874 /* All the "real" work is done here. */
7875 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7876
7877 /* This duplicates the code in init_cutu_and_read_dies,
7878 but the alternative is making the latter more complex.
7879 This function is only for the special case of using DWO files directly:
7880 no point in overly complicating the general case just to handle this. */
7881 if (new_cu != NULL && keep)
7882 {
7883 /* Link this CU into read_in_chain. */
7884 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7885 dwarf2_per_objfile->read_in_chain = this_cu;
7886 /* The chain owns it now. */
7887 new_cu.release ();
7888 }
7889 }
7890
7891 /* Initialize a CU (or TU) and read its DIEs.
7892 If the CU defers to a DWO file, read the DWO file as well.
7893
7894 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7895 Otherwise the table specified in the comp unit header is read in and used.
7896 This is an optimization for when we already have the abbrev table.
7897
7898 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7899 Otherwise, a new CU is allocated with xmalloc.
7900
7901 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7902 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7903
7904 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7905 linker) then DIE_READER_FUNC will not get called. */
7906
7907 static void
7908 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7909 struct abbrev_table *abbrev_table,
7910 int use_existing_cu, int keep,
7911 die_reader_func_ftype *die_reader_func,
7912 void *data)
7913 {
7914 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7915 struct objfile *objfile = dwarf2_per_objfile->objfile;
7916 struct dwarf2_section_info *section = this_cu->section;
7917 bfd *abfd = get_section_bfd_owner (section);
7918 struct dwarf2_cu *cu;
7919 const gdb_byte *begin_info_ptr, *info_ptr;
7920 struct die_reader_specs reader;
7921 struct die_info *comp_unit_die;
7922 int has_children;
7923 struct attribute *attr;
7924 struct signatured_type *sig_type = NULL;
7925 struct dwarf2_section_info *abbrev_section;
7926 /* Non-zero if CU currently points to a DWO file and we need to
7927 reread it. When this happens we need to reread the skeleton die
7928 before we can reread the DWO file (this only applies to CUs, not TUs). */
7929 int rereading_dwo_cu = 0;
7930
7931 if (dwarf_die_debug)
7932 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7933 this_cu->is_debug_types ? "type" : "comp",
7934 sect_offset_str (this_cu->sect_off));
7935
7936 if (use_existing_cu)
7937 gdb_assert (keep);
7938
7939 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7940 file (instead of going through the stub), short-circuit all of this. */
7941 if (this_cu->reading_dwo_directly)
7942 {
7943 /* Narrow down the scope of possibilities to have to understand. */
7944 gdb_assert (this_cu->is_debug_types);
7945 gdb_assert (abbrev_table == NULL);
7946 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7947 die_reader_func, data);
7948 return;
7949 }
7950
7951 /* This is cheap if the section is already read in. */
7952 dwarf2_read_section (objfile, section);
7953
7954 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7955
7956 abbrev_section = get_abbrev_section_for_cu (this_cu);
7957
7958 std::unique_ptr<dwarf2_cu> new_cu;
7959 if (use_existing_cu && this_cu->cu != NULL)
7960 {
7961 cu = this_cu->cu;
7962 /* If this CU is from a DWO file we need to start over, we need to
7963 refetch the attributes from the skeleton CU.
7964 This could be optimized by retrieving those attributes from when we
7965 were here the first time: the previous comp_unit_die was stored in
7966 comp_unit_obstack. But there's no data yet that we need this
7967 optimization. */
7968 if (cu->dwo_unit != NULL)
7969 rereading_dwo_cu = 1;
7970 }
7971 else
7972 {
7973 /* If !use_existing_cu, this_cu->cu must be NULL. */
7974 gdb_assert (this_cu->cu == NULL);
7975 new_cu.reset (new dwarf2_cu (this_cu));
7976 cu = new_cu.get ();
7977 }
7978
7979 /* Get the header. */
7980 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7981 {
7982 /* We already have the header, there's no need to read it in again. */
7983 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7984 }
7985 else
7986 {
7987 if (this_cu->is_debug_types)
7988 {
7989 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7990 &cu->header, section,
7991 abbrev_section, info_ptr,
7992 rcuh_kind::TYPE);
7993
7994 /* Since per_cu is the first member of struct signatured_type,
7995 we can go from a pointer to one to a pointer to the other. */
7996 sig_type = (struct signatured_type *) this_cu;
7997 gdb_assert (sig_type->signature == cu->header.signature);
7998 gdb_assert (sig_type->type_offset_in_tu
7999 == cu->header.type_cu_offset_in_tu);
8000 gdb_assert (this_cu->sect_off == cu->header.sect_off);
8001
8002 /* LENGTH has not been set yet for type units if we're
8003 using .gdb_index. */
8004 this_cu->length = get_cu_length (&cu->header);
8005
8006 /* Establish the type offset that can be used to lookup the type. */
8007 sig_type->type_offset_in_section =
8008 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
8009
8010 this_cu->dwarf_version = cu->header.version;
8011 }
8012 else
8013 {
8014 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8015 &cu->header, section,
8016 abbrev_section,
8017 info_ptr,
8018 rcuh_kind::COMPILE);
8019
8020 gdb_assert (this_cu->sect_off == cu->header.sect_off);
8021 gdb_assert (this_cu->length == get_cu_length (&cu->header));
8022 this_cu->dwarf_version = cu->header.version;
8023 }
8024 }
8025
8026 /* Skip dummy compilation units. */
8027 if (info_ptr >= begin_info_ptr + this_cu->length
8028 || peek_abbrev_code (abfd, info_ptr) == 0)
8029 return;
8030
8031 /* If we don't have them yet, read the abbrevs for this compilation unit.
8032 And if we need to read them now, make sure they're freed when we're
8033 done (own the table through ABBREV_TABLE_HOLDER). */
8034 abbrev_table_up abbrev_table_holder;
8035 if (abbrev_table != NULL)
8036 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
8037 else
8038 {
8039 abbrev_table_holder
8040 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8041 cu->header.abbrev_sect_off);
8042 abbrev_table = abbrev_table_holder.get ();
8043 }
8044
8045 /* Read the top level CU/TU die. */
8046 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
8047 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8048
8049 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
8050 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
8051 table from the DWO file and pass the ownership over to us. It will be
8052 referenced from READER, so we must make sure to free it after we're done
8053 with READER.
8054
8055 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
8056 DWO CU, that this test will fail (the attribute will not be present). */
8057 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
8058 abbrev_table_up dwo_abbrev_table;
8059 if (attr)
8060 {
8061 struct dwo_unit *dwo_unit;
8062 struct die_info *dwo_comp_unit_die;
8063
8064 if (has_children)
8065 {
8066 complaint (&symfile_complaints,
8067 _("compilation unit with DW_AT_GNU_dwo_name"
8068 " has children (offset %s) [in module %s]"),
8069 sect_offset_str (this_cu->sect_off),
8070 bfd_get_filename (abfd));
8071 }
8072 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
8073 if (dwo_unit != NULL)
8074 {
8075 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
8076 comp_unit_die, NULL,
8077 &reader, &info_ptr,
8078 &dwo_comp_unit_die, &has_children,
8079 &dwo_abbrev_table) == 0)
8080 {
8081 /* Dummy die. */
8082 return;
8083 }
8084 comp_unit_die = dwo_comp_unit_die;
8085 }
8086 else
8087 {
8088 /* Yikes, we couldn't find the rest of the DIE, we only have
8089 the stub. A complaint has already been logged. There's
8090 not much more we can do except pass on the stub DIE to
8091 die_reader_func. We don't want to throw an error on bad
8092 debug info. */
8093 }
8094 }
8095
8096 /* All of the above is setup for this call. Yikes. */
8097 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8098
8099 /* Done, clean up. */
8100 if (new_cu != NULL && keep)
8101 {
8102 /* Link this CU into read_in_chain. */
8103 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8104 dwarf2_per_objfile->read_in_chain = this_cu;
8105 /* The chain owns it now. */
8106 new_cu.release ();
8107 }
8108 }
8109
8110 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
8111 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
8112 to have already done the lookup to find the DWO file).
8113
8114 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
8115 THIS_CU->is_debug_types, but nothing else.
8116
8117 We fill in THIS_CU->length.
8118
8119 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
8120 linker) then DIE_READER_FUNC will not get called.
8121
8122 THIS_CU->cu is always freed when done.
8123 This is done in order to not leave THIS_CU->cu in a state where we have
8124 to care whether it refers to the "main" CU or the DWO CU. */
8125
8126 static void
8127 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
8128 struct dwo_file *dwo_file,
8129 die_reader_func_ftype *die_reader_func,
8130 void *data)
8131 {
8132 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
8133 struct objfile *objfile = dwarf2_per_objfile->objfile;
8134 struct dwarf2_section_info *section = this_cu->section;
8135 bfd *abfd = get_section_bfd_owner (section);
8136 struct dwarf2_section_info *abbrev_section;
8137 const gdb_byte *begin_info_ptr, *info_ptr;
8138 struct die_reader_specs reader;
8139 struct die_info *comp_unit_die;
8140 int has_children;
8141
8142 if (dwarf_die_debug)
8143 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
8144 this_cu->is_debug_types ? "type" : "comp",
8145 sect_offset_str (this_cu->sect_off));
8146
8147 gdb_assert (this_cu->cu == NULL);
8148
8149 abbrev_section = (dwo_file != NULL
8150 ? &dwo_file->sections.abbrev
8151 : get_abbrev_section_for_cu (this_cu));
8152
8153 /* This is cheap if the section is already read in. */
8154 dwarf2_read_section (objfile, section);
8155
8156 struct dwarf2_cu cu (this_cu);
8157
8158 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
8159 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
8160 &cu.header, section,
8161 abbrev_section, info_ptr,
8162 (this_cu->is_debug_types
8163 ? rcuh_kind::TYPE
8164 : rcuh_kind::COMPILE));
8165
8166 this_cu->length = get_cu_length (&cu.header);
8167
8168 /* Skip dummy compilation units. */
8169 if (info_ptr >= begin_info_ptr + this_cu->length
8170 || peek_abbrev_code (abfd, info_ptr) == 0)
8171 return;
8172
8173 abbrev_table_up abbrev_table
8174 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
8175 cu.header.abbrev_sect_off);
8176
8177 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
8178 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
8179
8180 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
8181 }
8182
8183 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
8184 does not lookup the specified DWO file.
8185 This cannot be used to read DWO files.
8186
8187 THIS_CU->cu is always freed when done.
8188 This is done in order to not leave THIS_CU->cu in a state where we have
8189 to care whether it refers to the "main" CU or the DWO CU.
8190 We can revisit this if the data shows there's a performance issue. */
8191
8192 static void
8193 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
8194 die_reader_func_ftype *die_reader_func,
8195 void *data)
8196 {
8197 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
8198 }
8199 \f
8200 /* Type Unit Groups.
8201
8202 Type Unit Groups are a way to collapse the set of all TUs (type units) into
8203 a more manageable set. The grouping is done by DW_AT_stmt_list entry
8204 so that all types coming from the same compilation (.o file) are grouped
8205 together. A future step could be to put the types in the same symtab as
8206 the CU the types ultimately came from. */
8207
8208 static hashval_t
8209 hash_type_unit_group (const void *item)
8210 {
8211 const struct type_unit_group *tu_group
8212 = (const struct type_unit_group *) item;
8213
8214 return hash_stmt_list_entry (&tu_group->hash);
8215 }
8216
8217 static int
8218 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
8219 {
8220 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
8221 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
8222
8223 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
8224 }
8225
8226 /* Allocate a hash table for type unit groups. */
8227
8228 static htab_t
8229 allocate_type_unit_groups_table (struct objfile *objfile)
8230 {
8231 return htab_create_alloc_ex (3,
8232 hash_type_unit_group,
8233 eq_type_unit_group,
8234 NULL,
8235 &objfile->objfile_obstack,
8236 hashtab_obstack_allocate,
8237 dummy_obstack_deallocate);
8238 }
8239
8240 /* Type units that don't have DW_AT_stmt_list are grouped into their own
8241 partial symtabs. We combine several TUs per psymtab to not let the size
8242 of any one psymtab grow too big. */
8243 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
8244 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
8245
8246 /* Helper routine for get_type_unit_group.
8247 Create the type_unit_group object used to hold one or more TUs. */
8248
8249 static struct type_unit_group *
8250 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
8251 {
8252 struct dwarf2_per_objfile *dwarf2_per_objfile
8253 = cu->per_cu->dwarf2_per_objfile;
8254 struct objfile *objfile = dwarf2_per_objfile->objfile;
8255 struct dwarf2_per_cu_data *per_cu;
8256 struct type_unit_group *tu_group;
8257
8258 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8259 struct type_unit_group);
8260 per_cu = &tu_group->per_cu;
8261 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8262
8263 if (dwarf2_per_objfile->using_index)
8264 {
8265 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8266 struct dwarf2_per_cu_quick_data);
8267 }
8268 else
8269 {
8270 unsigned int line_offset = to_underlying (line_offset_struct);
8271 struct partial_symtab *pst;
8272 char *name;
8273
8274 /* Give the symtab a useful name for debug purposes. */
8275 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
8276 name = xstrprintf ("<type_units_%d>",
8277 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
8278 else
8279 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
8280
8281 pst = create_partial_symtab (per_cu, name);
8282 pst->anonymous = 1;
8283
8284 xfree (name);
8285 }
8286
8287 tu_group->hash.dwo_unit = cu->dwo_unit;
8288 tu_group->hash.line_sect_off = line_offset_struct;
8289
8290 return tu_group;
8291 }
8292
8293 /* Look up the type_unit_group for type unit CU, and create it if necessary.
8294 STMT_LIST is a DW_AT_stmt_list attribute. */
8295
8296 static struct type_unit_group *
8297 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
8298 {
8299 struct dwarf2_per_objfile *dwarf2_per_objfile
8300 = cu->per_cu->dwarf2_per_objfile;
8301 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8302 struct type_unit_group *tu_group;
8303 void **slot;
8304 unsigned int line_offset;
8305 struct type_unit_group type_unit_group_for_lookup;
8306
8307 if (dwarf2_per_objfile->type_unit_groups == NULL)
8308 {
8309 dwarf2_per_objfile->type_unit_groups =
8310 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
8311 }
8312
8313 /* Do we need to create a new group, or can we use an existing one? */
8314
8315 if (stmt_list)
8316 {
8317 line_offset = DW_UNSND (stmt_list);
8318 ++tu_stats->nr_symtab_sharers;
8319 }
8320 else
8321 {
8322 /* Ugh, no stmt_list. Rare, but we have to handle it.
8323 We can do various things here like create one group per TU or
8324 spread them over multiple groups to split up the expansion work.
8325 To avoid worst case scenarios (too many groups or too large groups)
8326 we, umm, group them in bunches. */
8327 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8328 | (tu_stats->nr_stmt_less_type_units
8329 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8330 ++tu_stats->nr_stmt_less_type_units;
8331 }
8332
8333 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8334 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8335 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8336 &type_unit_group_for_lookup, INSERT);
8337 if (*slot != NULL)
8338 {
8339 tu_group = (struct type_unit_group *) *slot;
8340 gdb_assert (tu_group != NULL);
8341 }
8342 else
8343 {
8344 sect_offset line_offset_struct = (sect_offset) line_offset;
8345 tu_group = create_type_unit_group (cu, line_offset_struct);
8346 *slot = tu_group;
8347 ++tu_stats->nr_symtabs;
8348 }
8349
8350 return tu_group;
8351 }
8352 \f
8353 /* Partial symbol tables. */
8354
8355 /* Create a psymtab named NAME and assign it to PER_CU.
8356
8357 The caller must fill in the following details:
8358 dirname, textlow, texthigh. */
8359
8360 static struct partial_symtab *
8361 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8362 {
8363 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8364 struct partial_symtab *pst;
8365
8366 pst = start_psymtab_common (objfile, name, 0,
8367 objfile->global_psymbols,
8368 objfile->static_psymbols);
8369
8370 pst->psymtabs_addrmap_supported = 1;
8371
8372 /* This is the glue that links PST into GDB's symbol API. */
8373 pst->read_symtab_private = per_cu;
8374 pst->read_symtab = dwarf2_read_symtab;
8375 per_cu->v.psymtab = pst;
8376
8377 return pst;
8378 }
8379
8380 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8381 type. */
8382
8383 struct process_psymtab_comp_unit_data
8384 {
8385 /* True if we are reading a DW_TAG_partial_unit. */
8386
8387 int want_partial_unit;
8388
8389 /* The "pretend" language that is used if the CU doesn't declare a
8390 language. */
8391
8392 enum language pretend_language;
8393 };
8394
8395 /* die_reader_func for process_psymtab_comp_unit. */
8396
8397 static void
8398 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8399 const gdb_byte *info_ptr,
8400 struct die_info *comp_unit_die,
8401 int has_children,
8402 void *data)
8403 {
8404 struct dwarf2_cu *cu = reader->cu;
8405 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8406 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8407 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8408 CORE_ADDR baseaddr;
8409 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8410 struct partial_symtab *pst;
8411 enum pc_bounds_kind cu_bounds_kind;
8412 const char *filename;
8413 struct process_psymtab_comp_unit_data *info
8414 = (struct process_psymtab_comp_unit_data *) data;
8415
8416 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8417 return;
8418
8419 gdb_assert (! per_cu->is_debug_types);
8420
8421 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8422
8423 cu->list_in_scope = &file_symbols;
8424
8425 /* Allocate a new partial symbol table structure. */
8426 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8427 if (filename == NULL)
8428 filename = "";
8429
8430 pst = create_partial_symtab (per_cu, filename);
8431
8432 /* This must be done before calling dwarf2_build_include_psymtabs. */
8433 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8434
8435 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8436
8437 dwarf2_find_base_address (comp_unit_die, cu);
8438
8439 /* Possibly set the default values of LOWPC and HIGHPC from
8440 `DW_AT_ranges'. */
8441 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8442 &best_highpc, cu, pst);
8443 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8444 /* Store the contiguous range if it is not empty; it can be empty for
8445 CUs with no code. */
8446 addrmap_set_empty (objfile->psymtabs_addrmap,
8447 gdbarch_adjust_dwarf2_addr (gdbarch,
8448 best_lowpc + baseaddr),
8449 gdbarch_adjust_dwarf2_addr (gdbarch,
8450 best_highpc + baseaddr) - 1,
8451 pst);
8452
8453 /* Check if comp unit has_children.
8454 If so, read the rest of the partial symbols from this comp unit.
8455 If not, there's no more debug_info for this comp unit. */
8456 if (has_children)
8457 {
8458 struct partial_die_info *first_die;
8459 CORE_ADDR lowpc, highpc;
8460
8461 lowpc = ((CORE_ADDR) -1);
8462 highpc = ((CORE_ADDR) 0);
8463
8464 first_die = load_partial_dies (reader, info_ptr, 1);
8465
8466 scan_partial_symbols (first_die, &lowpc, &highpc,
8467 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8468
8469 /* If we didn't find a lowpc, set it to highpc to avoid
8470 complaints from `maint check'. */
8471 if (lowpc == ((CORE_ADDR) -1))
8472 lowpc = highpc;
8473
8474 /* If the compilation unit didn't have an explicit address range,
8475 then use the information extracted from its child dies. */
8476 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8477 {
8478 best_lowpc = lowpc;
8479 best_highpc = highpc;
8480 }
8481 }
8482 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8483 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8484
8485 end_psymtab_common (objfile, pst);
8486
8487 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8488 {
8489 int i;
8490 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8491 struct dwarf2_per_cu_data *iter;
8492
8493 /* Fill in 'dependencies' here; we fill in 'users' in a
8494 post-pass. */
8495 pst->number_of_dependencies = len;
8496 pst->dependencies =
8497 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8498 for (i = 0;
8499 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8500 i, iter);
8501 ++i)
8502 pst->dependencies[i] = iter->v.psymtab;
8503
8504 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8505 }
8506
8507 /* Get the list of files included in the current compilation unit,
8508 and build a psymtab for each of them. */
8509 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8510
8511 if (dwarf_read_debug)
8512 {
8513 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8514
8515 fprintf_unfiltered (gdb_stdlog,
8516 "Psymtab for %s unit @%s: %s - %s"
8517 ", %d global, %d static syms\n",
8518 per_cu->is_debug_types ? "type" : "comp",
8519 sect_offset_str (per_cu->sect_off),
8520 paddress (gdbarch, pst->textlow),
8521 paddress (gdbarch, pst->texthigh),
8522 pst->n_global_syms, pst->n_static_syms);
8523 }
8524 }
8525
8526 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8527 Process compilation unit THIS_CU for a psymtab. */
8528
8529 static void
8530 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8531 int want_partial_unit,
8532 enum language pretend_language)
8533 {
8534 /* If this compilation unit was already read in, free the
8535 cached copy in order to read it in again. This is
8536 necessary because we skipped some symbols when we first
8537 read in the compilation unit (see load_partial_dies).
8538 This problem could be avoided, but the benefit is unclear. */
8539 if (this_cu->cu != NULL)
8540 free_one_cached_comp_unit (this_cu);
8541
8542 if (this_cu->is_debug_types)
8543 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8544 NULL);
8545 else
8546 {
8547 process_psymtab_comp_unit_data info;
8548 info.want_partial_unit = want_partial_unit;
8549 info.pretend_language = pretend_language;
8550 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8551 process_psymtab_comp_unit_reader, &info);
8552 }
8553
8554 /* Age out any secondary CUs. */
8555 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8556 }
8557
8558 /* Reader function for build_type_psymtabs. */
8559
8560 static void
8561 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8562 const gdb_byte *info_ptr,
8563 struct die_info *type_unit_die,
8564 int has_children,
8565 void *data)
8566 {
8567 struct dwarf2_per_objfile *dwarf2_per_objfile
8568 = reader->cu->per_cu->dwarf2_per_objfile;
8569 struct objfile *objfile = dwarf2_per_objfile->objfile;
8570 struct dwarf2_cu *cu = reader->cu;
8571 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8572 struct signatured_type *sig_type;
8573 struct type_unit_group *tu_group;
8574 struct attribute *attr;
8575 struct partial_die_info *first_die;
8576 CORE_ADDR lowpc, highpc;
8577 struct partial_symtab *pst;
8578
8579 gdb_assert (data == NULL);
8580 gdb_assert (per_cu->is_debug_types);
8581 sig_type = (struct signatured_type *) per_cu;
8582
8583 if (! has_children)
8584 return;
8585
8586 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8587 tu_group = get_type_unit_group (cu, attr);
8588
8589 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8590
8591 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8592 cu->list_in_scope = &file_symbols;
8593 pst = create_partial_symtab (per_cu, "");
8594 pst->anonymous = 1;
8595
8596 first_die = load_partial_dies (reader, info_ptr, 1);
8597
8598 lowpc = (CORE_ADDR) -1;
8599 highpc = (CORE_ADDR) 0;
8600 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8601
8602 end_psymtab_common (objfile, pst);
8603 }
8604
8605 /* Struct used to sort TUs by their abbreviation table offset. */
8606
8607 struct tu_abbrev_offset
8608 {
8609 struct signatured_type *sig_type;
8610 sect_offset abbrev_offset;
8611 };
8612
8613 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8614
8615 static bool
8616 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8617 const struct tu_abbrev_offset &b)
8618 {
8619 return a.abbrev_offset < b.abbrev_offset;
8620 }
8621
8622 /* Efficiently read all the type units.
8623 This does the bulk of the work for build_type_psymtabs.
8624
8625 The efficiency is because we sort TUs by the abbrev table they use and
8626 only read each abbrev table once. In one program there are 200K TUs
8627 sharing 8K abbrev tables.
8628
8629 The main purpose of this function is to support building the
8630 dwarf2_per_objfile->type_unit_groups table.
8631 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8632 can collapse the search space by grouping them by stmt_list.
8633 The savings can be significant, in the same program from above the 200K TUs
8634 share 8K stmt_list tables.
8635
8636 FUNC is expected to call get_type_unit_group, which will create the
8637 struct type_unit_group if necessary and add it to
8638 dwarf2_per_objfile->type_unit_groups. */
8639
8640 static void
8641 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8642 {
8643 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8644 abbrev_table_up abbrev_table;
8645 sect_offset abbrev_offset;
8646 int i;
8647
8648 /* It's up to the caller to not call us multiple times. */
8649 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8650
8651 if (dwarf2_per_objfile->n_type_units == 0)
8652 return;
8653
8654 /* TUs typically share abbrev tables, and there can be way more TUs than
8655 abbrev tables. Sort by abbrev table to reduce the number of times we
8656 read each abbrev table in.
8657 Alternatives are to punt or to maintain a cache of abbrev tables.
8658 This is simpler and efficient enough for now.
8659
8660 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8661 symtab to use). Typically TUs with the same abbrev offset have the same
8662 stmt_list value too so in practice this should work well.
8663
8664 The basic algorithm here is:
8665
8666 sort TUs by abbrev table
8667 for each TU with same abbrev table:
8668 read abbrev table if first user
8669 read TU top level DIE
8670 [IWBN if DWO skeletons had DW_AT_stmt_list]
8671 call FUNC */
8672
8673 if (dwarf_read_debug)
8674 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8675
8676 /* Sort in a separate table to maintain the order of all_type_units
8677 for .gdb_index: TU indices directly index all_type_units. */
8678 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8679 (dwarf2_per_objfile->n_type_units);
8680 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8681 {
8682 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8683
8684 sorted_by_abbrev[i].sig_type = sig_type;
8685 sorted_by_abbrev[i].abbrev_offset =
8686 read_abbrev_offset (dwarf2_per_objfile,
8687 sig_type->per_cu.section,
8688 sig_type->per_cu.sect_off);
8689 }
8690 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8691 sort_tu_by_abbrev_offset);
8692
8693 abbrev_offset = (sect_offset) ~(unsigned) 0;
8694
8695 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8696 {
8697 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8698
8699 /* Switch to the next abbrev table if necessary. */
8700 if (abbrev_table == NULL
8701 || tu->abbrev_offset != abbrev_offset)
8702 {
8703 abbrev_offset = tu->abbrev_offset;
8704 abbrev_table =
8705 abbrev_table_read_table (dwarf2_per_objfile,
8706 &dwarf2_per_objfile->abbrev,
8707 abbrev_offset);
8708 ++tu_stats->nr_uniq_abbrev_tables;
8709 }
8710
8711 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8712 0, 0, build_type_psymtabs_reader, NULL);
8713 }
8714 }
8715
8716 /* Print collected type unit statistics. */
8717
8718 static void
8719 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8720 {
8721 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8722
8723 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8724 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8725 dwarf2_per_objfile->n_type_units);
8726 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8727 tu_stats->nr_uniq_abbrev_tables);
8728 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8729 tu_stats->nr_symtabs);
8730 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8731 tu_stats->nr_symtab_sharers);
8732 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8733 tu_stats->nr_stmt_less_type_units);
8734 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8735 tu_stats->nr_all_type_units_reallocs);
8736 }
8737
8738 /* Traversal function for build_type_psymtabs. */
8739
8740 static int
8741 build_type_psymtab_dependencies (void **slot, void *info)
8742 {
8743 struct dwarf2_per_objfile *dwarf2_per_objfile
8744 = (struct dwarf2_per_objfile *) info;
8745 struct objfile *objfile = dwarf2_per_objfile->objfile;
8746 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8747 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8748 struct partial_symtab *pst = per_cu->v.psymtab;
8749 int len = VEC_length (sig_type_ptr, tu_group->tus);
8750 struct signatured_type *iter;
8751 int i;
8752
8753 gdb_assert (len > 0);
8754 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8755
8756 pst->number_of_dependencies = len;
8757 pst->dependencies =
8758 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8759 for (i = 0;
8760 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8761 ++i)
8762 {
8763 gdb_assert (iter->per_cu.is_debug_types);
8764 pst->dependencies[i] = iter->per_cu.v.psymtab;
8765 iter->type_unit_group = tu_group;
8766 }
8767
8768 VEC_free (sig_type_ptr, tu_group->tus);
8769
8770 return 1;
8771 }
8772
8773 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8774 Build partial symbol tables for the .debug_types comp-units. */
8775
8776 static void
8777 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8778 {
8779 if (! create_all_type_units (dwarf2_per_objfile))
8780 return;
8781
8782 build_type_psymtabs_1 (dwarf2_per_objfile);
8783 }
8784
8785 /* Traversal function for process_skeletonless_type_unit.
8786 Read a TU in a DWO file and build partial symbols for it. */
8787
8788 static int
8789 process_skeletonless_type_unit (void **slot, void *info)
8790 {
8791 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8792 struct dwarf2_per_objfile *dwarf2_per_objfile
8793 = (struct dwarf2_per_objfile *) info;
8794 struct signatured_type find_entry, *entry;
8795
8796 /* If this TU doesn't exist in the global table, add it and read it in. */
8797
8798 if (dwarf2_per_objfile->signatured_types == NULL)
8799 {
8800 dwarf2_per_objfile->signatured_types
8801 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8802 }
8803
8804 find_entry.signature = dwo_unit->signature;
8805 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8806 INSERT);
8807 /* If we've already seen this type there's nothing to do. What's happening
8808 is we're doing our own version of comdat-folding here. */
8809 if (*slot != NULL)
8810 return 1;
8811
8812 /* This does the job that create_all_type_units would have done for
8813 this TU. */
8814 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8815 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8816 *slot = entry;
8817
8818 /* This does the job that build_type_psymtabs_1 would have done. */
8819 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8820 build_type_psymtabs_reader, NULL);
8821
8822 return 1;
8823 }
8824
8825 /* Traversal function for process_skeletonless_type_units. */
8826
8827 static int
8828 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8829 {
8830 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8831
8832 if (dwo_file->tus != NULL)
8833 {
8834 htab_traverse_noresize (dwo_file->tus,
8835 process_skeletonless_type_unit, info);
8836 }
8837
8838 return 1;
8839 }
8840
8841 /* Scan all TUs of DWO files, verifying we've processed them.
8842 This is needed in case a TU was emitted without its skeleton.
8843 Note: This can't be done until we know what all the DWO files are. */
8844
8845 static void
8846 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8847 {
8848 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8849 if (get_dwp_file (dwarf2_per_objfile) == NULL
8850 && dwarf2_per_objfile->dwo_files != NULL)
8851 {
8852 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8853 process_dwo_file_for_skeletonless_type_units,
8854 dwarf2_per_objfile);
8855 }
8856 }
8857
8858 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8859
8860 static void
8861 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8862 {
8863 int i;
8864
8865 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8866 {
8867 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8868 struct partial_symtab *pst = per_cu->v.psymtab;
8869 int j;
8870
8871 if (pst == NULL)
8872 continue;
8873
8874 for (j = 0; j < pst->number_of_dependencies; ++j)
8875 {
8876 /* Set the 'user' field only if it is not already set. */
8877 if (pst->dependencies[j]->user == NULL)
8878 pst->dependencies[j]->user = pst;
8879 }
8880 }
8881 }
8882
8883 /* Build the partial symbol table by doing a quick pass through the
8884 .debug_info and .debug_abbrev sections. */
8885
8886 static void
8887 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8888 {
8889 struct cleanup *back_to;
8890 int i;
8891 struct objfile *objfile = dwarf2_per_objfile->objfile;
8892
8893 if (dwarf_read_debug)
8894 {
8895 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8896 objfile_name (objfile));
8897 }
8898
8899 dwarf2_per_objfile->reading_partial_symbols = 1;
8900
8901 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8902
8903 /* Any cached compilation units will be linked by the per-objfile
8904 read_in_chain. Make sure to free them when we're done. */
8905 back_to = make_cleanup (free_cached_comp_units, dwarf2_per_objfile);
8906
8907 build_type_psymtabs (dwarf2_per_objfile);
8908
8909 create_all_comp_units (dwarf2_per_objfile);
8910
8911 /* Create a temporary address map on a temporary obstack. We later
8912 copy this to the final obstack. */
8913 auto_obstack temp_obstack;
8914
8915 scoped_restore save_psymtabs_addrmap
8916 = make_scoped_restore (&objfile->psymtabs_addrmap,
8917 addrmap_create_mutable (&temp_obstack));
8918
8919 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8920 {
8921 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (dwarf2_per_objfile, i);
8922
8923 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8924 }
8925
8926 /* This has to wait until we read the CUs, we need the list of DWOs. */
8927 process_skeletonless_type_units (dwarf2_per_objfile);
8928
8929 /* Now that all TUs have been processed we can fill in the dependencies. */
8930 if (dwarf2_per_objfile->type_unit_groups != NULL)
8931 {
8932 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8933 build_type_psymtab_dependencies, dwarf2_per_objfile);
8934 }
8935
8936 if (dwarf_read_debug)
8937 print_tu_stats (dwarf2_per_objfile);
8938
8939 set_partial_user (dwarf2_per_objfile);
8940
8941 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8942 &objfile->objfile_obstack);
8943 /* At this point we want to keep the address map. */
8944 save_psymtabs_addrmap.release ();
8945
8946 do_cleanups (back_to);
8947
8948 if (dwarf_read_debug)
8949 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8950 objfile_name (objfile));
8951 }
8952
8953 /* die_reader_func for load_partial_comp_unit. */
8954
8955 static void
8956 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8957 const gdb_byte *info_ptr,
8958 struct die_info *comp_unit_die,
8959 int has_children,
8960 void *data)
8961 {
8962 struct dwarf2_cu *cu = reader->cu;
8963
8964 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8965
8966 /* Check if comp unit has_children.
8967 If so, read the rest of the partial symbols from this comp unit.
8968 If not, there's no more debug_info for this comp unit. */
8969 if (has_children)
8970 load_partial_dies (reader, info_ptr, 0);
8971 }
8972
8973 /* Load the partial DIEs for a secondary CU into memory.
8974 This is also used when rereading a primary CU with load_all_dies. */
8975
8976 static void
8977 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8978 {
8979 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8980 load_partial_comp_unit_reader, NULL);
8981 }
8982
8983 static void
8984 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8985 struct dwarf2_section_info *section,
8986 struct dwarf2_section_info *abbrev_section,
8987 unsigned int is_dwz,
8988 int *n_allocated,
8989 int *n_comp_units,
8990 struct dwarf2_per_cu_data ***all_comp_units)
8991 {
8992 const gdb_byte *info_ptr;
8993 struct objfile *objfile = dwarf2_per_objfile->objfile;
8994
8995 if (dwarf_read_debug)
8996 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8997 get_section_name (section),
8998 get_section_file_name (section));
8999
9000 dwarf2_read_section (objfile, section);
9001
9002 info_ptr = section->buffer;
9003
9004 while (info_ptr < section->buffer + section->size)
9005 {
9006 struct dwarf2_per_cu_data *this_cu;
9007
9008 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
9009
9010 comp_unit_head cu_header;
9011 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
9012 abbrev_section, info_ptr,
9013 rcuh_kind::COMPILE);
9014
9015 /* Save the compilation unit for later lookup. */
9016 if (cu_header.unit_type != DW_UT_type)
9017 {
9018 this_cu = XOBNEW (&objfile->objfile_obstack,
9019 struct dwarf2_per_cu_data);
9020 memset (this_cu, 0, sizeof (*this_cu));
9021 }
9022 else
9023 {
9024 auto sig_type = XOBNEW (&objfile->objfile_obstack,
9025 struct signatured_type);
9026 memset (sig_type, 0, sizeof (*sig_type));
9027 sig_type->signature = cu_header.signature;
9028 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
9029 this_cu = &sig_type->per_cu;
9030 }
9031 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9032 this_cu->sect_off = sect_off;
9033 this_cu->length = cu_header.length + cu_header.initial_length_size;
9034 this_cu->is_dwz = is_dwz;
9035 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
9036 this_cu->section = section;
9037
9038 if (*n_comp_units == *n_allocated)
9039 {
9040 *n_allocated *= 2;
9041 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
9042 *all_comp_units, *n_allocated);
9043 }
9044 (*all_comp_units)[*n_comp_units] = this_cu;
9045 ++*n_comp_units;
9046
9047 info_ptr = info_ptr + this_cu->length;
9048 }
9049 }
9050
9051 /* Create a list of all compilation units in OBJFILE.
9052 This is only done for -readnow and building partial symtabs. */
9053
9054 static void
9055 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
9056 {
9057 int n_allocated;
9058 int n_comp_units;
9059 struct dwarf2_per_cu_data **all_comp_units;
9060 struct dwz_file *dwz;
9061 struct objfile *objfile = dwarf2_per_objfile->objfile;
9062
9063 n_comp_units = 0;
9064 n_allocated = 10;
9065 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
9066
9067 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
9068 &dwarf2_per_objfile->abbrev, 0,
9069 &n_allocated, &n_comp_units, &all_comp_units);
9070
9071 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
9072 if (dwz != NULL)
9073 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
9074 1, &n_allocated, &n_comp_units,
9075 &all_comp_units);
9076
9077 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
9078 struct dwarf2_per_cu_data *,
9079 n_comp_units);
9080 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
9081 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
9082 xfree (all_comp_units);
9083 dwarf2_per_objfile->n_comp_units = n_comp_units;
9084 }
9085
9086 /* Process all loaded DIEs for compilation unit CU, starting at
9087 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
9088 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
9089 DW_AT_ranges). See the comments of add_partial_subprogram on how
9090 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
9091
9092 static void
9093 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
9094 CORE_ADDR *highpc, int set_addrmap,
9095 struct dwarf2_cu *cu)
9096 {
9097 struct partial_die_info *pdi;
9098
9099 /* Now, march along the PDI's, descending into ones which have
9100 interesting children but skipping the children of the other ones,
9101 until we reach the end of the compilation unit. */
9102
9103 pdi = first_die;
9104
9105 while (pdi != NULL)
9106 {
9107 pdi->fixup (cu);
9108
9109 /* Anonymous namespaces or modules have no name but have interesting
9110 children, so we need to look at them. Ditto for anonymous
9111 enums. */
9112
9113 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
9114 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
9115 || pdi->tag == DW_TAG_imported_unit
9116 || pdi->tag == DW_TAG_inlined_subroutine)
9117 {
9118 switch (pdi->tag)
9119 {
9120 case DW_TAG_subprogram:
9121 case DW_TAG_inlined_subroutine:
9122 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9123 break;
9124 case DW_TAG_constant:
9125 case DW_TAG_variable:
9126 case DW_TAG_typedef:
9127 case DW_TAG_union_type:
9128 if (!pdi->is_declaration)
9129 {
9130 add_partial_symbol (pdi, cu);
9131 }
9132 break;
9133 case DW_TAG_class_type:
9134 case DW_TAG_interface_type:
9135 case DW_TAG_structure_type:
9136 if (!pdi->is_declaration)
9137 {
9138 add_partial_symbol (pdi, cu);
9139 }
9140 if (cu->language == language_rust && pdi->has_children)
9141 scan_partial_symbols (pdi->die_child, lowpc, highpc,
9142 set_addrmap, cu);
9143 break;
9144 case DW_TAG_enumeration_type:
9145 if (!pdi->is_declaration)
9146 add_partial_enumeration (pdi, cu);
9147 break;
9148 case DW_TAG_base_type:
9149 case DW_TAG_subrange_type:
9150 /* File scope base type definitions are added to the partial
9151 symbol table. */
9152 add_partial_symbol (pdi, cu);
9153 break;
9154 case DW_TAG_namespace:
9155 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
9156 break;
9157 case DW_TAG_module:
9158 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
9159 break;
9160 case DW_TAG_imported_unit:
9161 {
9162 struct dwarf2_per_cu_data *per_cu;
9163
9164 /* For now we don't handle imported units in type units. */
9165 if (cu->per_cu->is_debug_types)
9166 {
9167 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9168 " supported in type units [in module %s]"),
9169 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9170 }
9171
9172 per_cu = dwarf2_find_containing_comp_unit
9173 (pdi->d.sect_off, pdi->is_dwz,
9174 cu->per_cu->dwarf2_per_objfile);
9175
9176 /* Go read the partial unit, if needed. */
9177 if (per_cu->v.psymtab == NULL)
9178 process_psymtab_comp_unit (per_cu, 1, cu->language);
9179
9180 VEC_safe_push (dwarf2_per_cu_ptr,
9181 cu->per_cu->imported_symtabs, per_cu);
9182 }
9183 break;
9184 case DW_TAG_imported_declaration:
9185 add_partial_symbol (pdi, cu);
9186 break;
9187 default:
9188 break;
9189 }
9190 }
9191
9192 /* If the die has a sibling, skip to the sibling. */
9193
9194 pdi = pdi->die_sibling;
9195 }
9196 }
9197
9198 /* Functions used to compute the fully scoped name of a partial DIE.
9199
9200 Normally, this is simple. For C++, the parent DIE's fully scoped
9201 name is concatenated with "::" and the partial DIE's name.
9202 Enumerators are an exception; they use the scope of their parent
9203 enumeration type, i.e. the name of the enumeration type is not
9204 prepended to the enumerator.
9205
9206 There are two complexities. One is DW_AT_specification; in this
9207 case "parent" means the parent of the target of the specification,
9208 instead of the direct parent of the DIE. The other is compilers
9209 which do not emit DW_TAG_namespace; in this case we try to guess
9210 the fully qualified name of structure types from their members'
9211 linkage names. This must be done using the DIE's children rather
9212 than the children of any DW_AT_specification target. We only need
9213 to do this for structures at the top level, i.e. if the target of
9214 any DW_AT_specification (if any; otherwise the DIE itself) does not
9215 have a parent. */
9216
9217 /* Compute the scope prefix associated with PDI's parent, in
9218 compilation unit CU. The result will be allocated on CU's
9219 comp_unit_obstack, or a copy of the already allocated PDI->NAME
9220 field. NULL is returned if no prefix is necessary. */
9221 static const char *
9222 partial_die_parent_scope (struct partial_die_info *pdi,
9223 struct dwarf2_cu *cu)
9224 {
9225 const char *grandparent_scope;
9226 struct partial_die_info *parent, *real_pdi;
9227
9228 /* We need to look at our parent DIE; if we have a DW_AT_specification,
9229 then this means the parent of the specification DIE. */
9230
9231 real_pdi = pdi;
9232 while (real_pdi->has_specification)
9233 real_pdi = find_partial_die (real_pdi->spec_offset,
9234 real_pdi->spec_is_dwz, cu);
9235
9236 parent = real_pdi->die_parent;
9237 if (parent == NULL)
9238 return NULL;
9239
9240 if (parent->scope_set)
9241 return parent->scope;
9242
9243 parent->fixup (cu);
9244
9245 grandparent_scope = partial_die_parent_scope (parent, cu);
9246
9247 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9248 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9249 Work around this problem here. */
9250 if (cu->language == language_cplus
9251 && parent->tag == DW_TAG_namespace
9252 && strcmp (parent->name, "::") == 0
9253 && grandparent_scope == NULL)
9254 {
9255 parent->scope = NULL;
9256 parent->scope_set = 1;
9257 return NULL;
9258 }
9259
9260 if (pdi->tag == DW_TAG_enumerator)
9261 /* Enumerators should not get the name of the enumeration as a prefix. */
9262 parent->scope = grandparent_scope;
9263 else if (parent->tag == DW_TAG_namespace
9264 || parent->tag == DW_TAG_module
9265 || parent->tag == DW_TAG_structure_type
9266 || parent->tag == DW_TAG_class_type
9267 || parent->tag == DW_TAG_interface_type
9268 || parent->tag == DW_TAG_union_type
9269 || parent->tag == DW_TAG_enumeration_type)
9270 {
9271 if (grandparent_scope == NULL)
9272 parent->scope = parent->name;
9273 else
9274 parent->scope = typename_concat (&cu->comp_unit_obstack,
9275 grandparent_scope,
9276 parent->name, 0, cu);
9277 }
9278 else
9279 {
9280 /* FIXME drow/2004-04-01: What should we be doing with
9281 function-local names? For partial symbols, we should probably be
9282 ignoring them. */
9283 complaint (&symfile_complaints,
9284 _("unhandled containing DIE tag %d for DIE at %s"),
9285 parent->tag, sect_offset_str (pdi->sect_off));
9286 parent->scope = grandparent_scope;
9287 }
9288
9289 parent->scope_set = 1;
9290 return parent->scope;
9291 }
9292
9293 /* Return the fully scoped name associated with PDI, from compilation unit
9294 CU. The result will be allocated with malloc. */
9295
9296 static char *
9297 partial_die_full_name (struct partial_die_info *pdi,
9298 struct dwarf2_cu *cu)
9299 {
9300 const char *parent_scope;
9301
9302 /* If this is a template instantiation, we can not work out the
9303 template arguments from partial DIEs. So, unfortunately, we have
9304 to go through the full DIEs. At least any work we do building
9305 types here will be reused if full symbols are loaded later. */
9306 if (pdi->has_template_arguments)
9307 {
9308 pdi->fixup (cu);
9309
9310 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
9311 {
9312 struct die_info *die;
9313 struct attribute attr;
9314 struct dwarf2_cu *ref_cu = cu;
9315
9316 /* DW_FORM_ref_addr is using section offset. */
9317 attr.name = (enum dwarf_attribute) 0;
9318 attr.form = DW_FORM_ref_addr;
9319 attr.u.unsnd = to_underlying (pdi->sect_off);
9320 die = follow_die_ref (NULL, &attr, &ref_cu);
9321
9322 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
9323 }
9324 }
9325
9326 parent_scope = partial_die_parent_scope (pdi, cu);
9327 if (parent_scope == NULL)
9328 return NULL;
9329 else
9330 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
9331 }
9332
9333 static void
9334 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
9335 {
9336 struct dwarf2_per_objfile *dwarf2_per_objfile
9337 = cu->per_cu->dwarf2_per_objfile;
9338 struct objfile *objfile = dwarf2_per_objfile->objfile;
9339 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9340 CORE_ADDR addr = 0;
9341 const char *actual_name = NULL;
9342 CORE_ADDR baseaddr;
9343 char *built_actual_name;
9344
9345 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9346
9347 built_actual_name = partial_die_full_name (pdi, cu);
9348 if (built_actual_name != NULL)
9349 actual_name = built_actual_name;
9350
9351 if (actual_name == NULL)
9352 actual_name = pdi->name;
9353
9354 switch (pdi->tag)
9355 {
9356 case DW_TAG_inlined_subroutine:
9357 case DW_TAG_subprogram:
9358 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
9359 if (pdi->is_external || cu->language == language_ada)
9360 {
9361 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
9362 of the global scope. But in Ada, we want to be able to access
9363 nested procedures globally. So all Ada subprograms are stored
9364 in the global scope. */
9365 add_psymbol_to_list (actual_name, strlen (actual_name),
9366 built_actual_name != NULL,
9367 VAR_DOMAIN, LOC_BLOCK,
9368 &objfile->global_psymbols,
9369 addr, cu->language, objfile);
9370 }
9371 else
9372 {
9373 add_psymbol_to_list (actual_name, strlen (actual_name),
9374 built_actual_name != NULL,
9375 VAR_DOMAIN, LOC_BLOCK,
9376 &objfile->static_psymbols,
9377 addr, cu->language, objfile);
9378 }
9379
9380 if (pdi->main_subprogram && actual_name != NULL)
9381 set_objfile_main_name (objfile, actual_name, cu->language);
9382 break;
9383 case DW_TAG_constant:
9384 {
9385 std::vector<partial_symbol *> *list;
9386
9387 if (pdi->is_external)
9388 list = &objfile->global_psymbols;
9389 else
9390 list = &objfile->static_psymbols;
9391 add_psymbol_to_list (actual_name, strlen (actual_name),
9392 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9393 list, 0, cu->language, objfile);
9394 }
9395 break;
9396 case DW_TAG_variable:
9397 if (pdi->d.locdesc)
9398 addr = decode_locdesc (pdi->d.locdesc, cu);
9399
9400 if (pdi->d.locdesc
9401 && addr == 0
9402 && !dwarf2_per_objfile->has_section_at_zero)
9403 {
9404 /* A global or static variable may also have been stripped
9405 out by the linker if unused, in which case its address
9406 will be nullified; do not add such variables into partial
9407 symbol table then. */
9408 }
9409 else if (pdi->is_external)
9410 {
9411 /* Global Variable.
9412 Don't enter into the minimal symbol tables as there is
9413 a minimal symbol table entry from the ELF symbols already.
9414 Enter into partial symbol table if it has a location
9415 descriptor or a type.
9416 If the location descriptor is missing, new_symbol will create
9417 a LOC_UNRESOLVED symbol, the address of the variable will then
9418 be determined from the minimal symbol table whenever the variable
9419 is referenced.
9420 The address for the partial symbol table entry is not
9421 used by GDB, but it comes in handy for debugging partial symbol
9422 table building. */
9423
9424 if (pdi->d.locdesc || pdi->has_type)
9425 add_psymbol_to_list (actual_name, strlen (actual_name),
9426 built_actual_name != NULL,
9427 VAR_DOMAIN, LOC_STATIC,
9428 &objfile->global_psymbols,
9429 addr + baseaddr,
9430 cu->language, objfile);
9431 }
9432 else
9433 {
9434 int has_loc = pdi->d.locdesc != NULL;
9435
9436 /* Static Variable. Skip symbols whose value we cannot know (those
9437 without location descriptors or constant values). */
9438 if (!has_loc && !pdi->has_const_value)
9439 {
9440 xfree (built_actual_name);
9441 return;
9442 }
9443
9444 add_psymbol_to_list (actual_name, strlen (actual_name),
9445 built_actual_name != NULL,
9446 VAR_DOMAIN, LOC_STATIC,
9447 &objfile->static_psymbols,
9448 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9449 cu->language, objfile);
9450 }
9451 break;
9452 case DW_TAG_typedef:
9453 case DW_TAG_base_type:
9454 case DW_TAG_subrange_type:
9455 add_psymbol_to_list (actual_name, strlen (actual_name),
9456 built_actual_name != NULL,
9457 VAR_DOMAIN, LOC_TYPEDEF,
9458 &objfile->static_psymbols,
9459 0, cu->language, objfile);
9460 break;
9461 case DW_TAG_imported_declaration:
9462 case DW_TAG_namespace:
9463 add_psymbol_to_list (actual_name, strlen (actual_name),
9464 built_actual_name != NULL,
9465 VAR_DOMAIN, LOC_TYPEDEF,
9466 &objfile->global_psymbols,
9467 0, cu->language, objfile);
9468 break;
9469 case DW_TAG_module:
9470 add_psymbol_to_list (actual_name, strlen (actual_name),
9471 built_actual_name != NULL,
9472 MODULE_DOMAIN, LOC_TYPEDEF,
9473 &objfile->global_psymbols,
9474 0, cu->language, objfile);
9475 break;
9476 case DW_TAG_class_type:
9477 case DW_TAG_interface_type:
9478 case DW_TAG_structure_type:
9479 case DW_TAG_union_type:
9480 case DW_TAG_enumeration_type:
9481 /* Skip external references. The DWARF standard says in the section
9482 about "Structure, Union, and Class Type Entries": "An incomplete
9483 structure, union or class type is represented by a structure,
9484 union or class entry that does not have a byte size attribute
9485 and that has a DW_AT_declaration attribute." */
9486 if (!pdi->has_byte_size && pdi->is_declaration)
9487 {
9488 xfree (built_actual_name);
9489 return;
9490 }
9491
9492 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9493 static vs. global. */
9494 add_psymbol_to_list (actual_name, strlen (actual_name),
9495 built_actual_name != NULL,
9496 STRUCT_DOMAIN, LOC_TYPEDEF,
9497 cu->language == language_cplus
9498 ? &objfile->global_psymbols
9499 : &objfile->static_psymbols,
9500 0, cu->language, objfile);
9501
9502 break;
9503 case DW_TAG_enumerator:
9504 add_psymbol_to_list (actual_name, strlen (actual_name),
9505 built_actual_name != NULL,
9506 VAR_DOMAIN, LOC_CONST,
9507 cu->language == language_cplus
9508 ? &objfile->global_psymbols
9509 : &objfile->static_psymbols,
9510 0, cu->language, objfile);
9511 break;
9512 default:
9513 break;
9514 }
9515
9516 xfree (built_actual_name);
9517 }
9518
9519 /* Read a partial die corresponding to a namespace; also, add a symbol
9520 corresponding to that namespace to the symbol table. NAMESPACE is
9521 the name of the enclosing namespace. */
9522
9523 static void
9524 add_partial_namespace (struct partial_die_info *pdi,
9525 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9526 int set_addrmap, struct dwarf2_cu *cu)
9527 {
9528 /* Add a symbol for the namespace. */
9529
9530 add_partial_symbol (pdi, cu);
9531
9532 /* Now scan partial symbols in that namespace. */
9533
9534 if (pdi->has_children)
9535 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9536 }
9537
9538 /* Read a partial die corresponding to a Fortran module. */
9539
9540 static void
9541 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9542 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9543 {
9544 /* Add a symbol for the namespace. */
9545
9546 add_partial_symbol (pdi, cu);
9547
9548 /* Now scan partial symbols in that module. */
9549
9550 if (pdi->has_children)
9551 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9552 }
9553
9554 /* Read a partial die corresponding to a subprogram or an inlined
9555 subprogram and create a partial symbol for that subprogram.
9556 When the CU language allows it, this routine also defines a partial
9557 symbol for each nested subprogram that this subprogram contains.
9558 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9559 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9560
9561 PDI may also be a lexical block, in which case we simply search
9562 recursively for subprograms defined inside that lexical block.
9563 Again, this is only performed when the CU language allows this
9564 type of definitions. */
9565
9566 static void
9567 add_partial_subprogram (struct partial_die_info *pdi,
9568 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9569 int set_addrmap, struct dwarf2_cu *cu)
9570 {
9571 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9572 {
9573 if (pdi->has_pc_info)
9574 {
9575 if (pdi->lowpc < *lowpc)
9576 *lowpc = pdi->lowpc;
9577 if (pdi->highpc > *highpc)
9578 *highpc = pdi->highpc;
9579 if (set_addrmap)
9580 {
9581 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9582 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9583 CORE_ADDR baseaddr;
9584 CORE_ADDR highpc;
9585 CORE_ADDR lowpc;
9586
9587 baseaddr = ANOFFSET (objfile->section_offsets,
9588 SECT_OFF_TEXT (objfile));
9589 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9590 pdi->lowpc + baseaddr);
9591 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9592 pdi->highpc + baseaddr);
9593 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9594 cu->per_cu->v.psymtab);
9595 }
9596 }
9597
9598 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9599 {
9600 if (!pdi->is_declaration)
9601 /* Ignore subprogram DIEs that do not have a name, they are
9602 illegal. Do not emit a complaint at this point, we will
9603 do so when we convert this psymtab into a symtab. */
9604 if (pdi->name)
9605 add_partial_symbol (pdi, cu);
9606 }
9607 }
9608
9609 if (! pdi->has_children)
9610 return;
9611
9612 if (cu->language == language_ada)
9613 {
9614 pdi = pdi->die_child;
9615 while (pdi != NULL)
9616 {
9617 pdi->fixup (cu);
9618 if (pdi->tag == DW_TAG_subprogram
9619 || pdi->tag == DW_TAG_inlined_subroutine
9620 || pdi->tag == DW_TAG_lexical_block)
9621 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9622 pdi = pdi->die_sibling;
9623 }
9624 }
9625 }
9626
9627 /* Read a partial die corresponding to an enumeration type. */
9628
9629 static void
9630 add_partial_enumeration (struct partial_die_info *enum_pdi,
9631 struct dwarf2_cu *cu)
9632 {
9633 struct partial_die_info *pdi;
9634
9635 if (enum_pdi->name != NULL)
9636 add_partial_symbol (enum_pdi, cu);
9637
9638 pdi = enum_pdi->die_child;
9639 while (pdi)
9640 {
9641 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9642 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9643 else
9644 add_partial_symbol (pdi, cu);
9645 pdi = pdi->die_sibling;
9646 }
9647 }
9648
9649 /* Return the initial uleb128 in the die at INFO_PTR. */
9650
9651 static unsigned int
9652 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9653 {
9654 unsigned int bytes_read;
9655
9656 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9657 }
9658
9659 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9660 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9661
9662 Return the corresponding abbrev, or NULL if the number is zero (indicating
9663 an empty DIE). In either case *BYTES_READ will be set to the length of
9664 the initial number. */
9665
9666 static struct abbrev_info *
9667 peek_die_abbrev (const die_reader_specs &reader,
9668 const gdb_byte *info_ptr, unsigned int *bytes_read)
9669 {
9670 dwarf2_cu *cu = reader.cu;
9671 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9672 unsigned int abbrev_number
9673 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9674
9675 if (abbrev_number == 0)
9676 return NULL;
9677
9678 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9679 if (!abbrev)
9680 {
9681 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9682 " at offset %s [in module %s]"),
9683 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9684 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9685 }
9686
9687 return abbrev;
9688 }
9689
9690 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9691 Returns a pointer to the end of a series of DIEs, terminated by an empty
9692 DIE. Any children of the skipped DIEs will also be skipped. */
9693
9694 static const gdb_byte *
9695 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9696 {
9697 while (1)
9698 {
9699 unsigned int bytes_read;
9700 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9701
9702 if (abbrev == NULL)
9703 return info_ptr + bytes_read;
9704 else
9705 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9706 }
9707 }
9708
9709 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9710 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9711 abbrev corresponding to that skipped uleb128 should be passed in
9712 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9713 children. */
9714
9715 static const gdb_byte *
9716 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9717 struct abbrev_info *abbrev)
9718 {
9719 unsigned int bytes_read;
9720 struct attribute attr;
9721 bfd *abfd = reader->abfd;
9722 struct dwarf2_cu *cu = reader->cu;
9723 const gdb_byte *buffer = reader->buffer;
9724 const gdb_byte *buffer_end = reader->buffer_end;
9725 unsigned int form, i;
9726
9727 for (i = 0; i < abbrev->num_attrs; i++)
9728 {
9729 /* The only abbrev we care about is DW_AT_sibling. */
9730 if (abbrev->attrs[i].name == DW_AT_sibling)
9731 {
9732 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9733 if (attr.form == DW_FORM_ref_addr)
9734 complaint (&symfile_complaints,
9735 _("ignoring absolute DW_AT_sibling"));
9736 else
9737 {
9738 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9739 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9740
9741 if (sibling_ptr < info_ptr)
9742 complaint (&symfile_complaints,
9743 _("DW_AT_sibling points backwards"));
9744 else if (sibling_ptr > reader->buffer_end)
9745 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9746 else
9747 return sibling_ptr;
9748 }
9749 }
9750
9751 /* If it isn't DW_AT_sibling, skip this attribute. */
9752 form = abbrev->attrs[i].form;
9753 skip_attribute:
9754 switch (form)
9755 {
9756 case DW_FORM_ref_addr:
9757 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9758 and later it is offset sized. */
9759 if (cu->header.version == 2)
9760 info_ptr += cu->header.addr_size;
9761 else
9762 info_ptr += cu->header.offset_size;
9763 break;
9764 case DW_FORM_GNU_ref_alt:
9765 info_ptr += cu->header.offset_size;
9766 break;
9767 case DW_FORM_addr:
9768 info_ptr += cu->header.addr_size;
9769 break;
9770 case DW_FORM_data1:
9771 case DW_FORM_ref1:
9772 case DW_FORM_flag:
9773 info_ptr += 1;
9774 break;
9775 case DW_FORM_flag_present:
9776 case DW_FORM_implicit_const:
9777 break;
9778 case DW_FORM_data2:
9779 case DW_FORM_ref2:
9780 info_ptr += 2;
9781 break;
9782 case DW_FORM_data4:
9783 case DW_FORM_ref4:
9784 info_ptr += 4;
9785 break;
9786 case DW_FORM_data8:
9787 case DW_FORM_ref8:
9788 case DW_FORM_ref_sig8:
9789 info_ptr += 8;
9790 break;
9791 case DW_FORM_data16:
9792 info_ptr += 16;
9793 break;
9794 case DW_FORM_string:
9795 read_direct_string (abfd, info_ptr, &bytes_read);
9796 info_ptr += bytes_read;
9797 break;
9798 case DW_FORM_sec_offset:
9799 case DW_FORM_strp:
9800 case DW_FORM_GNU_strp_alt:
9801 info_ptr += cu->header.offset_size;
9802 break;
9803 case DW_FORM_exprloc:
9804 case DW_FORM_block:
9805 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9806 info_ptr += bytes_read;
9807 break;
9808 case DW_FORM_block1:
9809 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9810 break;
9811 case DW_FORM_block2:
9812 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9813 break;
9814 case DW_FORM_block4:
9815 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9816 break;
9817 case DW_FORM_sdata:
9818 case DW_FORM_udata:
9819 case DW_FORM_ref_udata:
9820 case DW_FORM_GNU_addr_index:
9821 case DW_FORM_GNU_str_index:
9822 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9823 break;
9824 case DW_FORM_indirect:
9825 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9826 info_ptr += bytes_read;
9827 /* We need to continue parsing from here, so just go back to
9828 the top. */
9829 goto skip_attribute;
9830
9831 default:
9832 error (_("Dwarf Error: Cannot handle %s "
9833 "in DWARF reader [in module %s]"),
9834 dwarf_form_name (form),
9835 bfd_get_filename (abfd));
9836 }
9837 }
9838
9839 if (abbrev->has_children)
9840 return skip_children (reader, info_ptr);
9841 else
9842 return info_ptr;
9843 }
9844
9845 /* Locate ORIG_PDI's sibling.
9846 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9847
9848 static const gdb_byte *
9849 locate_pdi_sibling (const struct die_reader_specs *reader,
9850 struct partial_die_info *orig_pdi,
9851 const gdb_byte *info_ptr)
9852 {
9853 /* Do we know the sibling already? */
9854
9855 if (orig_pdi->sibling)
9856 return orig_pdi->sibling;
9857
9858 /* Are there any children to deal with? */
9859
9860 if (!orig_pdi->has_children)
9861 return info_ptr;
9862
9863 /* Skip the children the long way. */
9864
9865 return skip_children (reader, info_ptr);
9866 }
9867
9868 /* Expand this partial symbol table into a full symbol table. SELF is
9869 not NULL. */
9870
9871 static void
9872 dwarf2_read_symtab (struct partial_symtab *self,
9873 struct objfile *objfile)
9874 {
9875 struct dwarf2_per_objfile *dwarf2_per_objfile
9876 = get_dwarf2_per_objfile (objfile);
9877
9878 if (self->readin)
9879 {
9880 warning (_("bug: psymtab for %s is already read in."),
9881 self->filename);
9882 }
9883 else
9884 {
9885 if (info_verbose)
9886 {
9887 printf_filtered (_("Reading in symbols for %s..."),
9888 self->filename);
9889 gdb_flush (gdb_stdout);
9890 }
9891
9892 /* If this psymtab is constructed from a debug-only objfile, the
9893 has_section_at_zero flag will not necessarily be correct. We
9894 can get the correct value for this flag by looking at the data
9895 associated with the (presumably stripped) associated objfile. */
9896 if (objfile->separate_debug_objfile_backlink)
9897 {
9898 struct dwarf2_per_objfile *dpo_backlink
9899 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9900
9901 dwarf2_per_objfile->has_section_at_zero
9902 = dpo_backlink->has_section_at_zero;
9903 }
9904
9905 dwarf2_per_objfile->reading_partial_symbols = 0;
9906
9907 psymtab_to_symtab_1 (self);
9908
9909 /* Finish up the debug error message. */
9910 if (info_verbose)
9911 printf_filtered (_("done.\n"));
9912 }
9913
9914 process_cu_includes (dwarf2_per_objfile);
9915 }
9916 \f
9917 /* Reading in full CUs. */
9918
9919 /* Add PER_CU to the queue. */
9920
9921 static void
9922 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9923 enum language pretend_language)
9924 {
9925 struct dwarf2_queue_item *item;
9926
9927 per_cu->queued = 1;
9928 item = XNEW (struct dwarf2_queue_item);
9929 item->per_cu = per_cu;
9930 item->pretend_language = pretend_language;
9931 item->next = NULL;
9932
9933 if (dwarf2_queue == NULL)
9934 dwarf2_queue = item;
9935 else
9936 dwarf2_queue_tail->next = item;
9937
9938 dwarf2_queue_tail = item;
9939 }
9940
9941 /* If PER_CU is not yet queued, add it to the queue.
9942 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9943 dependency.
9944 The result is non-zero if PER_CU was queued, otherwise the result is zero
9945 meaning either PER_CU is already queued or it is already loaded.
9946
9947 N.B. There is an invariant here that if a CU is queued then it is loaded.
9948 The caller is required to load PER_CU if we return non-zero. */
9949
9950 static int
9951 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9952 struct dwarf2_per_cu_data *per_cu,
9953 enum language pretend_language)
9954 {
9955 /* We may arrive here during partial symbol reading, if we need full
9956 DIEs to process an unusual case (e.g. template arguments). Do
9957 not queue PER_CU, just tell our caller to load its DIEs. */
9958 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9959 {
9960 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9961 return 1;
9962 return 0;
9963 }
9964
9965 /* Mark the dependence relation so that we don't flush PER_CU
9966 too early. */
9967 if (dependent_cu != NULL)
9968 dwarf2_add_dependence (dependent_cu, per_cu);
9969
9970 /* If it's already on the queue, we have nothing to do. */
9971 if (per_cu->queued)
9972 return 0;
9973
9974 /* If the compilation unit is already loaded, just mark it as
9975 used. */
9976 if (per_cu->cu != NULL)
9977 {
9978 per_cu->cu->last_used = 0;
9979 return 0;
9980 }
9981
9982 /* Add it to the queue. */
9983 queue_comp_unit (per_cu, pretend_language);
9984
9985 return 1;
9986 }
9987
9988 /* Process the queue. */
9989
9990 static void
9991 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9992 {
9993 struct dwarf2_queue_item *item, *next_item;
9994
9995 if (dwarf_read_debug)
9996 {
9997 fprintf_unfiltered (gdb_stdlog,
9998 "Expanding one or more symtabs of objfile %s ...\n",
9999 objfile_name (dwarf2_per_objfile->objfile));
10000 }
10001
10002 /* The queue starts out with one item, but following a DIE reference
10003 may load a new CU, adding it to the end of the queue. */
10004 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
10005 {
10006 if ((dwarf2_per_objfile->using_index
10007 ? !item->per_cu->v.quick->compunit_symtab
10008 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10009 /* Skip dummy CUs. */
10010 && item->per_cu->cu != NULL)
10011 {
10012 struct dwarf2_per_cu_data *per_cu = item->per_cu;
10013 unsigned int debug_print_threshold;
10014 char buf[100];
10015
10016 if (per_cu->is_debug_types)
10017 {
10018 struct signatured_type *sig_type =
10019 (struct signatured_type *) per_cu;
10020
10021 sprintf (buf, "TU %s at offset %s",
10022 hex_string (sig_type->signature),
10023 sect_offset_str (per_cu->sect_off));
10024 /* There can be 100s of TUs.
10025 Only print them in verbose mode. */
10026 debug_print_threshold = 2;
10027 }
10028 else
10029 {
10030 sprintf (buf, "CU at offset %s",
10031 sect_offset_str (per_cu->sect_off));
10032 debug_print_threshold = 1;
10033 }
10034
10035 if (dwarf_read_debug >= debug_print_threshold)
10036 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
10037
10038 if (per_cu->is_debug_types)
10039 process_full_type_unit (per_cu, item->pretend_language);
10040 else
10041 process_full_comp_unit (per_cu, item->pretend_language);
10042
10043 if (dwarf_read_debug >= debug_print_threshold)
10044 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
10045 }
10046
10047 item->per_cu->queued = 0;
10048 next_item = item->next;
10049 xfree (item);
10050 }
10051
10052 dwarf2_queue_tail = NULL;
10053
10054 if (dwarf_read_debug)
10055 {
10056 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
10057 objfile_name (dwarf2_per_objfile->objfile));
10058 }
10059 }
10060
10061 /* Read in full symbols for PST, and anything it depends on. */
10062
10063 static void
10064 psymtab_to_symtab_1 (struct partial_symtab *pst)
10065 {
10066 struct dwarf2_per_cu_data *per_cu;
10067 int i;
10068
10069 if (pst->readin)
10070 return;
10071
10072 for (i = 0; i < pst->number_of_dependencies; i++)
10073 if (!pst->dependencies[i]->readin
10074 && pst->dependencies[i]->user == NULL)
10075 {
10076 /* Inform about additional files that need to be read in. */
10077 if (info_verbose)
10078 {
10079 /* FIXME: i18n: Need to make this a single string. */
10080 fputs_filtered (" ", gdb_stdout);
10081 wrap_here ("");
10082 fputs_filtered ("and ", gdb_stdout);
10083 wrap_here ("");
10084 printf_filtered ("%s...", pst->dependencies[i]->filename);
10085 wrap_here (""); /* Flush output. */
10086 gdb_flush (gdb_stdout);
10087 }
10088 psymtab_to_symtab_1 (pst->dependencies[i]);
10089 }
10090
10091 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10092
10093 if (per_cu == NULL)
10094 {
10095 /* It's an include file, no symbols to read for it.
10096 Everything is in the parent symtab. */
10097 pst->readin = 1;
10098 return;
10099 }
10100
10101 dw2_do_instantiate_symtab (per_cu);
10102 }
10103
10104 /* Trivial hash function for die_info: the hash value of a DIE
10105 is its offset in .debug_info for this objfile. */
10106
10107 static hashval_t
10108 die_hash (const void *item)
10109 {
10110 const struct die_info *die = (const struct die_info *) item;
10111
10112 return to_underlying (die->sect_off);
10113 }
10114
10115 /* Trivial comparison function for die_info structures: two DIEs
10116 are equal if they have the same offset. */
10117
10118 static int
10119 die_eq (const void *item_lhs, const void *item_rhs)
10120 {
10121 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
10122 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
10123
10124 return die_lhs->sect_off == die_rhs->sect_off;
10125 }
10126
10127 /* die_reader_func for load_full_comp_unit.
10128 This is identical to read_signatured_type_reader,
10129 but is kept separate for now. */
10130
10131 static void
10132 load_full_comp_unit_reader (const struct die_reader_specs *reader,
10133 const gdb_byte *info_ptr,
10134 struct die_info *comp_unit_die,
10135 int has_children,
10136 void *data)
10137 {
10138 struct dwarf2_cu *cu = reader->cu;
10139 enum language *language_ptr = (enum language *) data;
10140
10141 gdb_assert (cu->die_hash == NULL);
10142 cu->die_hash =
10143 htab_create_alloc_ex (cu->header.length / 12,
10144 die_hash,
10145 die_eq,
10146 NULL,
10147 &cu->comp_unit_obstack,
10148 hashtab_obstack_allocate,
10149 dummy_obstack_deallocate);
10150
10151 if (has_children)
10152 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
10153 &info_ptr, comp_unit_die);
10154 cu->dies = comp_unit_die;
10155 /* comp_unit_die is not stored in die_hash, no need. */
10156
10157 /* We try not to read any attributes in this function, because not
10158 all CUs needed for references have been loaded yet, and symbol
10159 table processing isn't initialized. But we have to set the CU language,
10160 or we won't be able to build types correctly.
10161 Similarly, if we do not read the producer, we can not apply
10162 producer-specific interpretation. */
10163 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
10164 }
10165
10166 /* Load the DIEs associated with PER_CU into memory. */
10167
10168 static void
10169 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
10170 enum language pretend_language)
10171 {
10172 gdb_assert (! this_cu->is_debug_types);
10173
10174 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
10175 load_full_comp_unit_reader, &pretend_language);
10176 }
10177
10178 /* Add a DIE to the delayed physname list. */
10179
10180 static void
10181 add_to_method_list (struct type *type, int fnfield_index, int index,
10182 const char *name, struct die_info *die,
10183 struct dwarf2_cu *cu)
10184 {
10185 struct delayed_method_info mi;
10186 mi.type = type;
10187 mi.fnfield_index = fnfield_index;
10188 mi.index = index;
10189 mi.name = name;
10190 mi.die = die;
10191 cu->method_list.push_back (mi);
10192 }
10193
10194 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
10195 "const" / "volatile". If so, decrements LEN by the length of the
10196 modifier and return true. Otherwise return false. */
10197
10198 template<size_t N>
10199 static bool
10200 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
10201 {
10202 size_t mod_len = sizeof (mod) - 1;
10203 if (len > mod_len && startswith (physname + (len - mod_len), mod))
10204 {
10205 len -= mod_len;
10206 return true;
10207 }
10208 return false;
10209 }
10210
10211 /* Compute the physnames of any methods on the CU's method list.
10212
10213 The computation of method physnames is delayed in order to avoid the
10214 (bad) condition that one of the method's formal parameters is of an as yet
10215 incomplete type. */
10216
10217 static void
10218 compute_delayed_physnames (struct dwarf2_cu *cu)
10219 {
10220 /* Only C++ delays computing physnames. */
10221 if (cu->method_list.empty ())
10222 return;
10223 gdb_assert (cu->language == language_cplus);
10224
10225 for (struct delayed_method_info &mi : cu->method_list)
10226 {
10227 const char *physname;
10228 struct fn_fieldlist *fn_flp
10229 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
10230 physname = dwarf2_physname (mi.name, mi.die, cu);
10231 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
10232 = physname ? physname : "";
10233
10234 /* Since there's no tag to indicate whether a method is a
10235 const/volatile overload, extract that information out of the
10236 demangled name. */
10237 if (physname != NULL)
10238 {
10239 size_t len = strlen (physname);
10240
10241 while (1)
10242 {
10243 if (physname[len] == ')') /* shortcut */
10244 break;
10245 else if (check_modifier (physname, len, " const"))
10246 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
10247 else if (check_modifier (physname, len, " volatile"))
10248 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
10249 else
10250 break;
10251 }
10252 }
10253 }
10254
10255 /* The list is no longer needed. */
10256 cu->method_list.clear ();
10257 }
10258
10259 /* Go objects should be embedded in a DW_TAG_module DIE,
10260 and it's not clear if/how imported objects will appear.
10261 To keep Go support simple until that's worked out,
10262 go back through what we've read and create something usable.
10263 We could do this while processing each DIE, and feels kinda cleaner,
10264 but that way is more invasive.
10265 This is to, for example, allow the user to type "p var" or "b main"
10266 without having to specify the package name, and allow lookups
10267 of module.object to work in contexts that use the expression
10268 parser. */
10269
10270 static void
10271 fixup_go_packaging (struct dwarf2_cu *cu)
10272 {
10273 char *package_name = NULL;
10274 struct pending *list;
10275 int i;
10276
10277 for (list = global_symbols; list != NULL; list = list->next)
10278 {
10279 for (i = 0; i < list->nsyms; ++i)
10280 {
10281 struct symbol *sym = list->symbol[i];
10282
10283 if (SYMBOL_LANGUAGE (sym) == language_go
10284 && SYMBOL_CLASS (sym) == LOC_BLOCK)
10285 {
10286 char *this_package_name = go_symbol_package_name (sym);
10287
10288 if (this_package_name == NULL)
10289 continue;
10290 if (package_name == NULL)
10291 package_name = this_package_name;
10292 else
10293 {
10294 struct objfile *objfile
10295 = cu->per_cu->dwarf2_per_objfile->objfile;
10296 if (strcmp (package_name, this_package_name) != 0)
10297 complaint (&symfile_complaints,
10298 _("Symtab %s has objects from two different Go packages: %s and %s"),
10299 (symbol_symtab (sym) != NULL
10300 ? symtab_to_filename_for_display
10301 (symbol_symtab (sym))
10302 : objfile_name (objfile)),
10303 this_package_name, package_name);
10304 xfree (this_package_name);
10305 }
10306 }
10307 }
10308 }
10309
10310 if (package_name != NULL)
10311 {
10312 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10313 const char *saved_package_name
10314 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
10315 package_name,
10316 strlen (package_name));
10317 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
10318 saved_package_name);
10319 struct symbol *sym;
10320
10321 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10322
10323 sym = allocate_symbol (objfile);
10324 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
10325 SYMBOL_SET_NAMES (sym, saved_package_name,
10326 strlen (saved_package_name), 0, objfile);
10327 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
10328 e.g., "main" finds the "main" module and not C's main(). */
10329 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10330 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
10331 SYMBOL_TYPE (sym) = type;
10332
10333 add_symbol_to_list (sym, &global_symbols);
10334
10335 xfree (package_name);
10336 }
10337 }
10338
10339 /* Allocate a fully-qualified name consisting of the two parts on the
10340 obstack. */
10341
10342 static const char *
10343 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
10344 {
10345 return obconcat (obstack, p1, "::", p2, (char *) NULL);
10346 }
10347
10348 /* A helper that allocates a struct discriminant_info to attach to a
10349 union type. */
10350
10351 static struct discriminant_info *
10352 alloc_discriminant_info (struct type *type, int discriminant_index,
10353 int default_index)
10354 {
10355 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10356 gdb_assert (discriminant_index == -1
10357 || (discriminant_index >= 0
10358 && discriminant_index < TYPE_NFIELDS (type)));
10359 gdb_assert (default_index == -1
10360 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
10361
10362 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
10363
10364 struct discriminant_info *disc
10365 = ((struct discriminant_info *)
10366 TYPE_ZALLOC (type,
10367 offsetof (struct discriminant_info, discriminants)
10368 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10369 disc->default_index = default_index;
10370 disc->discriminant_index = discriminant_index;
10371
10372 struct dynamic_prop prop;
10373 prop.kind = PROP_UNDEFINED;
10374 prop.data.baton = disc;
10375
10376 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10377
10378 return disc;
10379 }
10380
10381 /* Some versions of rustc emitted enums in an unusual way.
10382
10383 Ordinary enums were emitted as unions. The first element of each
10384 structure in the union was named "RUST$ENUM$DISR". This element
10385 held the discriminant.
10386
10387 These versions of Rust also implemented the "non-zero"
10388 optimization. When the enum had two values, and one is empty and
10389 the other holds a pointer that cannot be zero, the pointer is used
10390 as the discriminant, with a zero value meaning the empty variant.
10391 Here, the union's first member is of the form
10392 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10393 where the fieldnos are the indices of the fields that should be
10394 traversed in order to find the field (which may be several fields deep)
10395 and the variantname is the name of the variant of the case when the
10396 field is zero.
10397
10398 This function recognizes whether TYPE is of one of these forms,
10399 and, if so, smashes it to be a variant type. */
10400
10401 static void
10402 quirk_rust_enum (struct type *type, struct objfile *objfile)
10403 {
10404 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10405
10406 /* We don't need to deal with empty enums. */
10407 if (TYPE_NFIELDS (type) == 0)
10408 return;
10409
10410 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10411 if (TYPE_NFIELDS (type) == 1
10412 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10413 {
10414 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10415
10416 /* Decode the field name to find the offset of the
10417 discriminant. */
10418 ULONGEST bit_offset = 0;
10419 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10420 while (name[0] >= '0' && name[0] <= '9')
10421 {
10422 char *tail;
10423 unsigned long index = strtoul (name, &tail, 10);
10424 name = tail;
10425 if (*name != '$'
10426 || index >= TYPE_NFIELDS (field_type)
10427 || (TYPE_FIELD_LOC_KIND (field_type, index)
10428 != FIELD_LOC_KIND_BITPOS))
10429 {
10430 complaint (&symfile_complaints,
10431 _("Could not parse Rust enum encoding string \"%s\""
10432 "[in module %s]"),
10433 TYPE_FIELD_NAME (type, 0),
10434 objfile_name (objfile));
10435 return;
10436 }
10437 ++name;
10438
10439 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10440 field_type = TYPE_FIELD_TYPE (field_type, index);
10441 }
10442
10443 /* Make a union to hold the variants. */
10444 struct type *union_type = alloc_type (objfile);
10445 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10446 TYPE_NFIELDS (union_type) = 3;
10447 TYPE_FIELDS (union_type)
10448 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10449 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10450
10451 /* Put the discriminant must at index 0. */
10452 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10453 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10454 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10455 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10456
10457 /* The order of fields doesn't really matter, so put the real
10458 field at index 1 and the data-less field at index 2. */
10459 struct discriminant_info *disc
10460 = alloc_discriminant_info (union_type, 0, 1);
10461 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10462 TYPE_FIELD_NAME (union_type, 1)
10463 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10464 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10465 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10466 TYPE_FIELD_NAME (union_type, 1));
10467
10468 const char *dataless_name
10469 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10470 name);
10471 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10472 dataless_name);
10473 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10474 /* NAME points into the original discriminant name, which
10475 already has the correct lifetime. */
10476 TYPE_FIELD_NAME (union_type, 2) = name;
10477 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10478 disc->discriminants[2] = 0;
10479
10480 /* Smash this type to be a structure type. We have to do this
10481 because the type has already been recorded. */
10482 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10483 TYPE_NFIELDS (type) = 1;
10484 TYPE_FIELDS (type)
10485 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10486
10487 /* Install the variant part. */
10488 TYPE_FIELD_TYPE (type, 0) = union_type;
10489 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10490 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10491 }
10492 else if (TYPE_NFIELDS (type) == 1)
10493 {
10494 /* We assume that a union with a single field is a univariant
10495 enum. */
10496 /* Smash this type to be a structure type. We have to do this
10497 because the type has already been recorded. */
10498 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10499
10500 /* Make a union to hold the variants. */
10501 struct type *union_type = alloc_type (objfile);
10502 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10503 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10504 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10505 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10506
10507 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10508 const char *variant_name
10509 = rust_last_path_segment (TYPE_NAME (field_type));
10510 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10511 TYPE_NAME (field_type)
10512 = rust_fully_qualify (&objfile->objfile_obstack,
10513 TYPE_NAME (type), variant_name);
10514
10515 /* Install the union in the outer struct type. */
10516 TYPE_NFIELDS (type) = 1;
10517 TYPE_FIELDS (type)
10518 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10519 TYPE_FIELD_TYPE (type, 0) = union_type;
10520 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10521 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10522
10523 alloc_discriminant_info (union_type, -1, 0);
10524 }
10525 else
10526 {
10527 struct type *disr_type = nullptr;
10528 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10529 {
10530 disr_type = TYPE_FIELD_TYPE (type, i);
10531
10532 if (TYPE_NFIELDS (disr_type) == 0)
10533 {
10534 /* Could be data-less variant, so keep going. */
10535 }
10536 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10537 "RUST$ENUM$DISR") != 0)
10538 {
10539 /* Not a Rust enum. */
10540 return;
10541 }
10542 else
10543 {
10544 /* Found one. */
10545 break;
10546 }
10547 }
10548
10549 /* If we got here without a discriminant, then it's probably
10550 just a union. */
10551 if (disr_type == nullptr)
10552 return;
10553
10554 /* Smash this type to be a structure type. We have to do this
10555 because the type has already been recorded. */
10556 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10557
10558 /* Make a union to hold the variants. */
10559 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10560 struct type *union_type = alloc_type (objfile);
10561 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10562 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10563 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10564 TYPE_FIELDS (union_type)
10565 = (struct field *) TYPE_ZALLOC (union_type,
10566 (TYPE_NFIELDS (union_type)
10567 * sizeof (struct field)));
10568
10569 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10570 TYPE_NFIELDS (type) * sizeof (struct field));
10571
10572 /* Install the discriminant at index 0 in the union. */
10573 TYPE_FIELD (union_type, 0) = *disr_field;
10574 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10575 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10576
10577 /* Install the union in the outer struct type. */
10578 TYPE_FIELD_TYPE (type, 0) = union_type;
10579 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10580 TYPE_NFIELDS (type) = 1;
10581
10582 /* Set the size and offset of the union type. */
10583 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10584
10585 /* We need a way to find the correct discriminant given a
10586 variant name. For convenience we build a map here. */
10587 struct type *enum_type = FIELD_TYPE (*disr_field);
10588 std::unordered_map<std::string, ULONGEST> discriminant_map;
10589 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10590 {
10591 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10592 {
10593 const char *name
10594 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10595 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10596 }
10597 }
10598
10599 int n_fields = TYPE_NFIELDS (union_type);
10600 struct discriminant_info *disc
10601 = alloc_discriminant_info (union_type, 0, -1);
10602 /* Skip the discriminant here. */
10603 for (int i = 1; i < n_fields; ++i)
10604 {
10605 /* Find the final word in the name of this variant's type.
10606 That name can be used to look up the correct
10607 discriminant. */
10608 const char *variant_name
10609 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10610 i)));
10611
10612 auto iter = discriminant_map.find (variant_name);
10613 if (iter != discriminant_map.end ())
10614 disc->discriminants[i] = iter->second;
10615
10616 /* Remove the discriminant field. */
10617 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10618 --TYPE_NFIELDS (sub_type);
10619 ++TYPE_FIELDS (sub_type);
10620 TYPE_FIELD_NAME (union_type, i) = variant_name;
10621 TYPE_NAME (sub_type)
10622 = rust_fully_qualify (&objfile->objfile_obstack,
10623 TYPE_NAME (type), variant_name);
10624 }
10625 }
10626 }
10627
10628 /* Rewrite some Rust unions to be structures with variants parts. */
10629
10630 static void
10631 rust_union_quirks (struct dwarf2_cu *cu)
10632 {
10633 gdb_assert (cu->language == language_rust);
10634 for (struct type *type : cu->rust_unions)
10635 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10636 }
10637
10638 /* Return the symtab for PER_CU. This works properly regardless of
10639 whether we're using the index or psymtabs. */
10640
10641 static struct compunit_symtab *
10642 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10643 {
10644 return (per_cu->dwarf2_per_objfile->using_index
10645 ? per_cu->v.quick->compunit_symtab
10646 : per_cu->v.psymtab->compunit_symtab);
10647 }
10648
10649 /* A helper function for computing the list of all symbol tables
10650 included by PER_CU. */
10651
10652 static void
10653 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10654 htab_t all_children, htab_t all_type_symtabs,
10655 struct dwarf2_per_cu_data *per_cu,
10656 struct compunit_symtab *immediate_parent)
10657 {
10658 void **slot;
10659 int ix;
10660 struct compunit_symtab *cust;
10661 struct dwarf2_per_cu_data *iter;
10662
10663 slot = htab_find_slot (all_children, per_cu, INSERT);
10664 if (*slot != NULL)
10665 {
10666 /* This inclusion and its children have been processed. */
10667 return;
10668 }
10669
10670 *slot = per_cu;
10671 /* Only add a CU if it has a symbol table. */
10672 cust = get_compunit_symtab (per_cu);
10673 if (cust != NULL)
10674 {
10675 /* If this is a type unit only add its symbol table if we haven't
10676 seen it yet (type unit per_cu's can share symtabs). */
10677 if (per_cu->is_debug_types)
10678 {
10679 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10680 if (*slot == NULL)
10681 {
10682 *slot = cust;
10683 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10684 if (cust->user == NULL)
10685 cust->user = immediate_parent;
10686 }
10687 }
10688 else
10689 {
10690 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10691 if (cust->user == NULL)
10692 cust->user = immediate_parent;
10693 }
10694 }
10695
10696 for (ix = 0;
10697 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10698 ++ix)
10699 {
10700 recursively_compute_inclusions (result, all_children,
10701 all_type_symtabs, iter, cust);
10702 }
10703 }
10704
10705 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10706 PER_CU. */
10707
10708 static void
10709 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10710 {
10711 gdb_assert (! per_cu->is_debug_types);
10712
10713 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10714 {
10715 int ix, len;
10716 struct dwarf2_per_cu_data *per_cu_iter;
10717 struct compunit_symtab *compunit_symtab_iter;
10718 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10719 htab_t all_children, all_type_symtabs;
10720 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10721
10722 /* If we don't have a symtab, we can just skip this case. */
10723 if (cust == NULL)
10724 return;
10725
10726 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10727 NULL, xcalloc, xfree);
10728 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10729 NULL, xcalloc, xfree);
10730
10731 for (ix = 0;
10732 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10733 ix, per_cu_iter);
10734 ++ix)
10735 {
10736 recursively_compute_inclusions (&result_symtabs, all_children,
10737 all_type_symtabs, per_cu_iter,
10738 cust);
10739 }
10740
10741 /* Now we have a transitive closure of all the included symtabs. */
10742 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10743 cust->includes
10744 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10745 struct compunit_symtab *, len + 1);
10746 for (ix = 0;
10747 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10748 compunit_symtab_iter);
10749 ++ix)
10750 cust->includes[ix] = compunit_symtab_iter;
10751 cust->includes[len] = NULL;
10752
10753 VEC_free (compunit_symtab_ptr, result_symtabs);
10754 htab_delete (all_children);
10755 htab_delete (all_type_symtabs);
10756 }
10757 }
10758
10759 /* Compute the 'includes' field for the symtabs of all the CUs we just
10760 read. */
10761
10762 static void
10763 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10764 {
10765 int ix;
10766 struct dwarf2_per_cu_data *iter;
10767
10768 for (ix = 0;
10769 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10770 ix, iter);
10771 ++ix)
10772 {
10773 if (! iter->is_debug_types)
10774 compute_compunit_symtab_includes (iter);
10775 }
10776
10777 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10778 }
10779
10780 /* Generate full symbol information for PER_CU, whose DIEs have
10781 already been loaded into memory. */
10782
10783 static void
10784 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10785 enum language pretend_language)
10786 {
10787 struct dwarf2_cu *cu = per_cu->cu;
10788 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10789 struct objfile *objfile = dwarf2_per_objfile->objfile;
10790 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10791 CORE_ADDR lowpc, highpc;
10792 struct compunit_symtab *cust;
10793 CORE_ADDR baseaddr;
10794 struct block *static_block;
10795 CORE_ADDR addr;
10796
10797 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10798
10799 buildsym_init ();
10800 scoped_free_pendings free_pending;
10801
10802 /* Clear the list here in case something was left over. */
10803 cu->method_list.clear ();
10804
10805 cu->list_in_scope = &file_symbols;
10806
10807 cu->language = pretend_language;
10808 cu->language_defn = language_def (cu->language);
10809
10810 /* Do line number decoding in read_file_scope () */
10811 process_die (cu->dies, cu);
10812
10813 /* For now fudge the Go package. */
10814 if (cu->language == language_go)
10815 fixup_go_packaging (cu);
10816
10817 /* Now that we have processed all the DIEs in the CU, all the types
10818 should be complete, and it should now be safe to compute all of the
10819 physnames. */
10820 compute_delayed_physnames (cu);
10821
10822 if (cu->language == language_rust)
10823 rust_union_quirks (cu);
10824
10825 /* Some compilers don't define a DW_AT_high_pc attribute for the
10826 compilation unit. If the DW_AT_high_pc is missing, synthesize
10827 it, by scanning the DIE's below the compilation unit. */
10828 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10829
10830 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10831 static_block = end_symtab_get_static_block (addr, 0, 1);
10832
10833 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10834 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10835 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10836 addrmap to help ensure it has an accurate map of pc values belonging to
10837 this comp unit. */
10838 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10839
10840 cust = end_symtab_from_static_block (static_block,
10841 SECT_OFF_TEXT (objfile), 0);
10842
10843 if (cust != NULL)
10844 {
10845 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10846
10847 /* Set symtab language to language from DW_AT_language. If the
10848 compilation is from a C file generated by language preprocessors, do
10849 not set the language if it was already deduced by start_subfile. */
10850 if (!(cu->language == language_c
10851 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10852 COMPUNIT_FILETABS (cust)->language = cu->language;
10853
10854 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10855 produce DW_AT_location with location lists but it can be possibly
10856 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10857 there were bugs in prologue debug info, fixed later in GCC-4.5
10858 by "unwind info for epilogues" patch (which is not directly related).
10859
10860 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10861 needed, it would be wrong due to missing DW_AT_producer there.
10862
10863 Still one can confuse GDB by using non-standard GCC compilation
10864 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10865 */
10866 if (cu->has_loclist && gcc_4_minor >= 5)
10867 cust->locations_valid = 1;
10868
10869 if (gcc_4_minor >= 5)
10870 cust->epilogue_unwind_valid = 1;
10871
10872 cust->call_site_htab = cu->call_site_htab;
10873 }
10874
10875 if (dwarf2_per_objfile->using_index)
10876 per_cu->v.quick->compunit_symtab = cust;
10877 else
10878 {
10879 struct partial_symtab *pst = per_cu->v.psymtab;
10880 pst->compunit_symtab = cust;
10881 pst->readin = 1;
10882 }
10883
10884 /* Push it for inclusion processing later. */
10885 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10886 }
10887
10888 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10889 already been loaded into memory. */
10890
10891 static void
10892 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10893 enum language pretend_language)
10894 {
10895 struct dwarf2_cu *cu = per_cu->cu;
10896 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10897 struct objfile *objfile = dwarf2_per_objfile->objfile;
10898 struct compunit_symtab *cust;
10899 struct signatured_type *sig_type;
10900
10901 gdb_assert (per_cu->is_debug_types);
10902 sig_type = (struct signatured_type *) per_cu;
10903
10904 buildsym_init ();
10905 scoped_free_pendings free_pending;
10906
10907 /* Clear the list here in case something was left over. */
10908 cu->method_list.clear ();
10909
10910 cu->list_in_scope = &file_symbols;
10911
10912 cu->language = pretend_language;
10913 cu->language_defn = language_def (cu->language);
10914
10915 /* The symbol tables are set up in read_type_unit_scope. */
10916 process_die (cu->dies, cu);
10917
10918 /* For now fudge the Go package. */
10919 if (cu->language == language_go)
10920 fixup_go_packaging (cu);
10921
10922 /* Now that we have processed all the DIEs in the CU, all the types
10923 should be complete, and it should now be safe to compute all of the
10924 physnames. */
10925 compute_delayed_physnames (cu);
10926
10927 if (cu->language == language_rust)
10928 rust_union_quirks (cu);
10929
10930 /* TUs share symbol tables.
10931 If this is the first TU to use this symtab, complete the construction
10932 of it with end_expandable_symtab. Otherwise, complete the addition of
10933 this TU's symbols to the existing symtab. */
10934 if (sig_type->type_unit_group->compunit_symtab == NULL)
10935 {
10936 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10937 sig_type->type_unit_group->compunit_symtab = cust;
10938
10939 if (cust != NULL)
10940 {
10941 /* Set symtab language to language from DW_AT_language. If the
10942 compilation is from a C file generated by language preprocessors,
10943 do not set the language if it was already deduced by
10944 start_subfile. */
10945 if (!(cu->language == language_c
10946 && COMPUNIT_FILETABS (cust)->language != language_c))
10947 COMPUNIT_FILETABS (cust)->language = cu->language;
10948 }
10949 }
10950 else
10951 {
10952 augment_type_symtab ();
10953 cust = sig_type->type_unit_group->compunit_symtab;
10954 }
10955
10956 if (dwarf2_per_objfile->using_index)
10957 per_cu->v.quick->compunit_symtab = cust;
10958 else
10959 {
10960 struct partial_symtab *pst = per_cu->v.psymtab;
10961 pst->compunit_symtab = cust;
10962 pst->readin = 1;
10963 }
10964 }
10965
10966 /* Process an imported unit DIE. */
10967
10968 static void
10969 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10970 {
10971 struct attribute *attr;
10972
10973 /* For now we don't handle imported units in type units. */
10974 if (cu->per_cu->is_debug_types)
10975 {
10976 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10977 " supported in type units [in module %s]"),
10978 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10979 }
10980
10981 attr = dwarf2_attr (die, DW_AT_import, cu);
10982 if (attr != NULL)
10983 {
10984 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10985 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10986 dwarf2_per_cu_data *per_cu
10987 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10988 cu->per_cu->dwarf2_per_objfile);
10989
10990 /* If necessary, add it to the queue and load its DIEs. */
10991 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10992 load_full_comp_unit (per_cu, cu->language);
10993
10994 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10995 per_cu);
10996 }
10997 }
10998
10999 /* RAII object that represents a process_die scope: i.e.,
11000 starts/finishes processing a DIE. */
11001 class process_die_scope
11002 {
11003 public:
11004 process_die_scope (die_info *die, dwarf2_cu *cu)
11005 : m_die (die), m_cu (cu)
11006 {
11007 /* We should only be processing DIEs not already in process. */
11008 gdb_assert (!m_die->in_process);
11009 m_die->in_process = true;
11010 }
11011
11012 ~process_die_scope ()
11013 {
11014 m_die->in_process = false;
11015
11016 /* If we're done processing the DIE for the CU that owns the line
11017 header, we don't need the line header anymore. */
11018 if (m_cu->line_header_die_owner == m_die)
11019 {
11020 delete m_cu->line_header;
11021 m_cu->line_header = NULL;
11022 m_cu->line_header_die_owner = NULL;
11023 }
11024 }
11025
11026 private:
11027 die_info *m_die;
11028 dwarf2_cu *m_cu;
11029 };
11030
11031 /* Process a die and its children. */
11032
11033 static void
11034 process_die (struct die_info *die, struct dwarf2_cu *cu)
11035 {
11036 process_die_scope scope (die, cu);
11037
11038 switch (die->tag)
11039 {
11040 case DW_TAG_padding:
11041 break;
11042 case DW_TAG_compile_unit:
11043 case DW_TAG_partial_unit:
11044 read_file_scope (die, cu);
11045 break;
11046 case DW_TAG_type_unit:
11047 read_type_unit_scope (die, cu);
11048 break;
11049 case DW_TAG_subprogram:
11050 case DW_TAG_inlined_subroutine:
11051 read_func_scope (die, cu);
11052 break;
11053 case DW_TAG_lexical_block:
11054 case DW_TAG_try_block:
11055 case DW_TAG_catch_block:
11056 read_lexical_block_scope (die, cu);
11057 break;
11058 case DW_TAG_call_site:
11059 case DW_TAG_GNU_call_site:
11060 read_call_site_scope (die, cu);
11061 break;
11062 case DW_TAG_class_type:
11063 case DW_TAG_interface_type:
11064 case DW_TAG_structure_type:
11065 case DW_TAG_union_type:
11066 process_structure_scope (die, cu);
11067 break;
11068 case DW_TAG_enumeration_type:
11069 process_enumeration_scope (die, cu);
11070 break;
11071
11072 /* These dies have a type, but processing them does not create
11073 a symbol or recurse to process the children. Therefore we can
11074 read them on-demand through read_type_die. */
11075 case DW_TAG_subroutine_type:
11076 case DW_TAG_set_type:
11077 case DW_TAG_array_type:
11078 case DW_TAG_pointer_type:
11079 case DW_TAG_ptr_to_member_type:
11080 case DW_TAG_reference_type:
11081 case DW_TAG_rvalue_reference_type:
11082 case DW_TAG_string_type:
11083 break;
11084
11085 case DW_TAG_base_type:
11086 case DW_TAG_subrange_type:
11087 case DW_TAG_typedef:
11088 /* Add a typedef symbol for the type definition, if it has a
11089 DW_AT_name. */
11090 new_symbol (die, read_type_die (die, cu), cu);
11091 break;
11092 case DW_TAG_common_block:
11093 read_common_block (die, cu);
11094 break;
11095 case DW_TAG_common_inclusion:
11096 break;
11097 case DW_TAG_namespace:
11098 cu->processing_has_namespace_info = 1;
11099 read_namespace (die, cu);
11100 break;
11101 case DW_TAG_module:
11102 cu->processing_has_namespace_info = 1;
11103 read_module (die, cu);
11104 break;
11105 case DW_TAG_imported_declaration:
11106 cu->processing_has_namespace_info = 1;
11107 if (read_namespace_alias (die, cu))
11108 break;
11109 /* The declaration is not a global namespace alias: fall through. */
11110 case DW_TAG_imported_module:
11111 cu->processing_has_namespace_info = 1;
11112 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
11113 || cu->language != language_fortran))
11114 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
11115 dwarf_tag_name (die->tag));
11116 read_import_statement (die, cu);
11117 break;
11118
11119 case DW_TAG_imported_unit:
11120 process_imported_unit_die (die, cu);
11121 break;
11122
11123 case DW_TAG_variable:
11124 read_variable (die, cu);
11125 break;
11126
11127 default:
11128 new_symbol (die, NULL, cu);
11129 break;
11130 }
11131 }
11132 \f
11133 /* DWARF name computation. */
11134
11135 /* A helper function for dwarf2_compute_name which determines whether DIE
11136 needs to have the name of the scope prepended to the name listed in the
11137 die. */
11138
11139 static int
11140 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
11141 {
11142 struct attribute *attr;
11143
11144 switch (die->tag)
11145 {
11146 case DW_TAG_namespace:
11147 case DW_TAG_typedef:
11148 case DW_TAG_class_type:
11149 case DW_TAG_interface_type:
11150 case DW_TAG_structure_type:
11151 case DW_TAG_union_type:
11152 case DW_TAG_enumeration_type:
11153 case DW_TAG_enumerator:
11154 case DW_TAG_subprogram:
11155 case DW_TAG_inlined_subroutine:
11156 case DW_TAG_member:
11157 case DW_TAG_imported_declaration:
11158 return 1;
11159
11160 case DW_TAG_variable:
11161 case DW_TAG_constant:
11162 /* We only need to prefix "globally" visible variables. These include
11163 any variable marked with DW_AT_external or any variable that
11164 lives in a namespace. [Variables in anonymous namespaces
11165 require prefixing, but they are not DW_AT_external.] */
11166
11167 if (dwarf2_attr (die, DW_AT_specification, cu))
11168 {
11169 struct dwarf2_cu *spec_cu = cu;
11170
11171 return die_needs_namespace (die_specification (die, &spec_cu),
11172 spec_cu);
11173 }
11174
11175 attr = dwarf2_attr (die, DW_AT_external, cu);
11176 if (attr == NULL && die->parent->tag != DW_TAG_namespace
11177 && die->parent->tag != DW_TAG_module)
11178 return 0;
11179 /* A variable in a lexical block of some kind does not need a
11180 namespace, even though in C++ such variables may be external
11181 and have a mangled name. */
11182 if (die->parent->tag == DW_TAG_lexical_block
11183 || die->parent->tag == DW_TAG_try_block
11184 || die->parent->tag == DW_TAG_catch_block
11185 || die->parent->tag == DW_TAG_subprogram)
11186 return 0;
11187 return 1;
11188
11189 default:
11190 return 0;
11191 }
11192 }
11193
11194 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
11195 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11196 defined for the given DIE. */
11197
11198 static struct attribute *
11199 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
11200 {
11201 struct attribute *attr;
11202
11203 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
11204 if (attr == NULL)
11205 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
11206
11207 return attr;
11208 }
11209
11210 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
11211 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
11212 defined for the given DIE. */
11213
11214 static const char *
11215 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
11216 {
11217 const char *linkage_name;
11218
11219 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
11220 if (linkage_name == NULL)
11221 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
11222
11223 return linkage_name;
11224 }
11225
11226 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
11227 compute the physname for the object, which include a method's:
11228 - formal parameters (C++),
11229 - receiver type (Go),
11230
11231 The term "physname" is a bit confusing.
11232 For C++, for example, it is the demangled name.
11233 For Go, for example, it's the mangled name.
11234
11235 For Ada, return the DIE's linkage name rather than the fully qualified
11236 name. PHYSNAME is ignored..
11237
11238 The result is allocated on the objfile_obstack and canonicalized. */
11239
11240 static const char *
11241 dwarf2_compute_name (const char *name,
11242 struct die_info *die, struct dwarf2_cu *cu,
11243 int physname)
11244 {
11245 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11246
11247 if (name == NULL)
11248 name = dwarf2_name (die, cu);
11249
11250 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
11251 but otherwise compute it by typename_concat inside GDB.
11252 FIXME: Actually this is not really true, or at least not always true.
11253 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
11254 Fortran names because there is no mangling standard. So new_symbol
11255 will set the demangled name to the result of dwarf2_full_name, and it is
11256 the demangled name that GDB uses if it exists. */
11257 if (cu->language == language_ada
11258 || (cu->language == language_fortran && physname))
11259 {
11260 /* For Ada unit, we prefer the linkage name over the name, as
11261 the former contains the exported name, which the user expects
11262 to be able to reference. Ideally, we want the user to be able
11263 to reference this entity using either natural or linkage name,
11264 but we haven't started looking at this enhancement yet. */
11265 const char *linkage_name = dw2_linkage_name (die, cu);
11266
11267 if (linkage_name != NULL)
11268 return linkage_name;
11269 }
11270
11271 /* These are the only languages we know how to qualify names in. */
11272 if (name != NULL
11273 && (cu->language == language_cplus
11274 || cu->language == language_fortran || cu->language == language_d
11275 || cu->language == language_rust))
11276 {
11277 if (die_needs_namespace (die, cu))
11278 {
11279 const char *prefix;
11280 const char *canonical_name = NULL;
11281
11282 string_file buf;
11283
11284 prefix = determine_prefix (die, cu);
11285 if (*prefix != '\0')
11286 {
11287 char *prefixed_name = typename_concat (NULL, prefix, name,
11288 physname, cu);
11289
11290 buf.puts (prefixed_name);
11291 xfree (prefixed_name);
11292 }
11293 else
11294 buf.puts (name);
11295
11296 /* Template parameters may be specified in the DIE's DW_AT_name, or
11297 as children with DW_TAG_template_type_param or
11298 DW_TAG_value_type_param. If the latter, add them to the name
11299 here. If the name already has template parameters, then
11300 skip this step; some versions of GCC emit both, and
11301 it is more efficient to use the pre-computed name.
11302
11303 Something to keep in mind about this process: it is very
11304 unlikely, or in some cases downright impossible, to produce
11305 something that will match the mangled name of a function.
11306 If the definition of the function has the same debug info,
11307 we should be able to match up with it anyway. But fallbacks
11308 using the minimal symbol, for instance to find a method
11309 implemented in a stripped copy of libstdc++, will not work.
11310 If we do not have debug info for the definition, we will have to
11311 match them up some other way.
11312
11313 When we do name matching there is a related problem with function
11314 templates; two instantiated function templates are allowed to
11315 differ only by their return types, which we do not add here. */
11316
11317 if (cu->language == language_cplus && strchr (name, '<') == NULL)
11318 {
11319 struct attribute *attr;
11320 struct die_info *child;
11321 int first = 1;
11322
11323 die->building_fullname = 1;
11324
11325 for (child = die->child; child != NULL; child = child->sibling)
11326 {
11327 struct type *type;
11328 LONGEST value;
11329 const gdb_byte *bytes;
11330 struct dwarf2_locexpr_baton *baton;
11331 struct value *v;
11332
11333 if (child->tag != DW_TAG_template_type_param
11334 && child->tag != DW_TAG_template_value_param)
11335 continue;
11336
11337 if (first)
11338 {
11339 buf.puts ("<");
11340 first = 0;
11341 }
11342 else
11343 buf.puts (", ");
11344
11345 attr = dwarf2_attr (child, DW_AT_type, cu);
11346 if (attr == NULL)
11347 {
11348 complaint (&symfile_complaints,
11349 _("template parameter missing DW_AT_type"));
11350 buf.puts ("UNKNOWN_TYPE");
11351 continue;
11352 }
11353 type = die_type (child, cu);
11354
11355 if (child->tag == DW_TAG_template_type_param)
11356 {
11357 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
11358 continue;
11359 }
11360
11361 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11362 if (attr == NULL)
11363 {
11364 complaint (&symfile_complaints,
11365 _("template parameter missing "
11366 "DW_AT_const_value"));
11367 buf.puts ("UNKNOWN_VALUE");
11368 continue;
11369 }
11370
11371 dwarf2_const_value_attr (attr, type, name,
11372 &cu->comp_unit_obstack, cu,
11373 &value, &bytes, &baton);
11374
11375 if (TYPE_NOSIGN (type))
11376 /* GDB prints characters as NUMBER 'CHAR'. If that's
11377 changed, this can use value_print instead. */
11378 c_printchar (value, type, &buf);
11379 else
11380 {
11381 struct value_print_options opts;
11382
11383 if (baton != NULL)
11384 v = dwarf2_evaluate_loc_desc (type, NULL,
11385 baton->data,
11386 baton->size,
11387 baton->per_cu);
11388 else if (bytes != NULL)
11389 {
11390 v = allocate_value (type);
11391 memcpy (value_contents_writeable (v), bytes,
11392 TYPE_LENGTH (type));
11393 }
11394 else
11395 v = value_from_longest (type, value);
11396
11397 /* Specify decimal so that we do not depend on
11398 the radix. */
11399 get_formatted_print_options (&opts, 'd');
11400 opts.raw = 1;
11401 value_print (v, &buf, &opts);
11402 release_value (v);
11403 value_free (v);
11404 }
11405 }
11406
11407 die->building_fullname = 0;
11408
11409 if (!first)
11410 {
11411 /* Close the argument list, with a space if necessary
11412 (nested templates). */
11413 if (!buf.empty () && buf.string ().back () == '>')
11414 buf.puts (" >");
11415 else
11416 buf.puts (">");
11417 }
11418 }
11419
11420 /* For C++ methods, append formal parameter type
11421 information, if PHYSNAME. */
11422
11423 if (physname && die->tag == DW_TAG_subprogram
11424 && cu->language == language_cplus)
11425 {
11426 struct type *type = read_type_die (die, cu);
11427
11428 c_type_print_args (type, &buf, 1, cu->language,
11429 &type_print_raw_options);
11430
11431 if (cu->language == language_cplus)
11432 {
11433 /* Assume that an artificial first parameter is
11434 "this", but do not crash if it is not. RealView
11435 marks unnamed (and thus unused) parameters as
11436 artificial; there is no way to differentiate
11437 the two cases. */
11438 if (TYPE_NFIELDS (type) > 0
11439 && TYPE_FIELD_ARTIFICIAL (type, 0)
11440 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11441 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11442 0))))
11443 buf.puts (" const");
11444 }
11445 }
11446
11447 const std::string &intermediate_name = buf.string ();
11448
11449 if (cu->language == language_cplus)
11450 canonical_name
11451 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11452 &objfile->per_bfd->storage_obstack);
11453
11454 /* If we only computed INTERMEDIATE_NAME, or if
11455 INTERMEDIATE_NAME is already canonical, then we need to
11456 copy it to the appropriate obstack. */
11457 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11458 name = ((const char *)
11459 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11460 intermediate_name.c_str (),
11461 intermediate_name.length ()));
11462 else
11463 name = canonical_name;
11464 }
11465 }
11466
11467 return name;
11468 }
11469
11470 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11471 If scope qualifiers are appropriate they will be added. The result
11472 will be allocated on the storage_obstack, or NULL if the DIE does
11473 not have a name. NAME may either be from a previous call to
11474 dwarf2_name or NULL.
11475
11476 The output string will be canonicalized (if C++). */
11477
11478 static const char *
11479 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11480 {
11481 return dwarf2_compute_name (name, die, cu, 0);
11482 }
11483
11484 /* Construct a physname for the given DIE in CU. NAME may either be
11485 from a previous call to dwarf2_name or NULL. The result will be
11486 allocated on the objfile_objstack or NULL if the DIE does not have a
11487 name.
11488
11489 The output string will be canonicalized (if C++). */
11490
11491 static const char *
11492 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11493 {
11494 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11495 const char *retval, *mangled = NULL, *canon = NULL;
11496 int need_copy = 1;
11497
11498 /* In this case dwarf2_compute_name is just a shortcut not building anything
11499 on its own. */
11500 if (!die_needs_namespace (die, cu))
11501 return dwarf2_compute_name (name, die, cu, 1);
11502
11503 mangled = dw2_linkage_name (die, cu);
11504
11505 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11506 See https://github.com/rust-lang/rust/issues/32925. */
11507 if (cu->language == language_rust && mangled != NULL
11508 && strchr (mangled, '{') != NULL)
11509 mangled = NULL;
11510
11511 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11512 has computed. */
11513 gdb::unique_xmalloc_ptr<char> demangled;
11514 if (mangled != NULL)
11515 {
11516
11517 if (cu->language == language_go)
11518 {
11519 /* This is a lie, but we already lie to the caller new_symbol.
11520 new_symbol assumes we return the mangled name.
11521 This just undoes that lie until things are cleaned up. */
11522 }
11523 else
11524 {
11525 /* Use DMGL_RET_DROP for C++ template functions to suppress
11526 their return type. It is easier for GDB users to search
11527 for such functions as `name(params)' than `long name(params)'.
11528 In such case the minimal symbol names do not match the full
11529 symbol names but for template functions there is never a need
11530 to look up their definition from their declaration so
11531 the only disadvantage remains the minimal symbol variant
11532 `long name(params)' does not have the proper inferior type. */
11533 demangled.reset (gdb_demangle (mangled,
11534 (DMGL_PARAMS | DMGL_ANSI
11535 | DMGL_RET_DROP)));
11536 }
11537 if (demangled)
11538 canon = demangled.get ();
11539 else
11540 {
11541 canon = mangled;
11542 need_copy = 0;
11543 }
11544 }
11545
11546 if (canon == NULL || check_physname)
11547 {
11548 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11549
11550 if (canon != NULL && strcmp (physname, canon) != 0)
11551 {
11552 /* It may not mean a bug in GDB. The compiler could also
11553 compute DW_AT_linkage_name incorrectly. But in such case
11554 GDB would need to be bug-to-bug compatible. */
11555
11556 complaint (&symfile_complaints,
11557 _("Computed physname <%s> does not match demangled <%s> "
11558 "(from linkage <%s>) - DIE at %s [in module %s]"),
11559 physname, canon, mangled, sect_offset_str (die->sect_off),
11560 objfile_name (objfile));
11561
11562 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11563 is available here - over computed PHYSNAME. It is safer
11564 against both buggy GDB and buggy compilers. */
11565
11566 retval = canon;
11567 }
11568 else
11569 {
11570 retval = physname;
11571 need_copy = 0;
11572 }
11573 }
11574 else
11575 retval = canon;
11576
11577 if (need_copy)
11578 retval = ((const char *)
11579 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11580 retval, strlen (retval)));
11581
11582 return retval;
11583 }
11584
11585 /* Inspect DIE in CU for a namespace alias. If one exists, record
11586 a new symbol for it.
11587
11588 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11589
11590 static int
11591 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11592 {
11593 struct attribute *attr;
11594
11595 /* If the die does not have a name, this is not a namespace
11596 alias. */
11597 attr = dwarf2_attr (die, DW_AT_name, cu);
11598 if (attr != NULL)
11599 {
11600 int num;
11601 struct die_info *d = die;
11602 struct dwarf2_cu *imported_cu = cu;
11603
11604 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11605 keep inspecting DIEs until we hit the underlying import. */
11606 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11607 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11608 {
11609 attr = dwarf2_attr (d, DW_AT_import, cu);
11610 if (attr == NULL)
11611 break;
11612
11613 d = follow_die_ref (d, attr, &imported_cu);
11614 if (d->tag != DW_TAG_imported_declaration)
11615 break;
11616 }
11617
11618 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11619 {
11620 complaint (&symfile_complaints,
11621 _("DIE at %s has too many recursively imported "
11622 "declarations"), sect_offset_str (d->sect_off));
11623 return 0;
11624 }
11625
11626 if (attr != NULL)
11627 {
11628 struct type *type;
11629 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11630
11631 type = get_die_type_at_offset (sect_off, cu->per_cu);
11632 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11633 {
11634 /* This declaration is a global namespace alias. Add
11635 a symbol for it whose type is the aliased namespace. */
11636 new_symbol (die, type, cu);
11637 return 1;
11638 }
11639 }
11640 }
11641
11642 return 0;
11643 }
11644
11645 /* Return the using directives repository (global or local?) to use in the
11646 current context for LANGUAGE.
11647
11648 For Ada, imported declarations can materialize renamings, which *may* be
11649 global. However it is impossible (for now?) in DWARF to distinguish
11650 "external" imported declarations and "static" ones. As all imported
11651 declarations seem to be static in all other languages, make them all CU-wide
11652 global only in Ada. */
11653
11654 static struct using_direct **
11655 using_directives (enum language language)
11656 {
11657 if (language == language_ada && context_stack_depth == 0)
11658 return &global_using_directives;
11659 else
11660 return &local_using_directives;
11661 }
11662
11663 /* Read the import statement specified by the given die and record it. */
11664
11665 static void
11666 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11667 {
11668 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11669 struct attribute *import_attr;
11670 struct die_info *imported_die, *child_die;
11671 struct dwarf2_cu *imported_cu;
11672 const char *imported_name;
11673 const char *imported_name_prefix;
11674 const char *canonical_name;
11675 const char *import_alias;
11676 const char *imported_declaration = NULL;
11677 const char *import_prefix;
11678 std::vector<const char *> excludes;
11679
11680 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11681 if (import_attr == NULL)
11682 {
11683 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11684 dwarf_tag_name (die->tag));
11685 return;
11686 }
11687
11688 imported_cu = cu;
11689 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11690 imported_name = dwarf2_name (imported_die, imported_cu);
11691 if (imported_name == NULL)
11692 {
11693 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11694
11695 The import in the following code:
11696 namespace A
11697 {
11698 typedef int B;
11699 }
11700
11701 int main ()
11702 {
11703 using A::B;
11704 B b;
11705 return b;
11706 }
11707
11708 ...
11709 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11710 <52> DW_AT_decl_file : 1
11711 <53> DW_AT_decl_line : 6
11712 <54> DW_AT_import : <0x75>
11713 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11714 <59> DW_AT_name : B
11715 <5b> DW_AT_decl_file : 1
11716 <5c> DW_AT_decl_line : 2
11717 <5d> DW_AT_type : <0x6e>
11718 ...
11719 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11720 <76> DW_AT_byte_size : 4
11721 <77> DW_AT_encoding : 5 (signed)
11722
11723 imports the wrong die ( 0x75 instead of 0x58 ).
11724 This case will be ignored until the gcc bug is fixed. */
11725 return;
11726 }
11727
11728 /* Figure out the local name after import. */
11729 import_alias = dwarf2_name (die, cu);
11730
11731 /* Figure out where the statement is being imported to. */
11732 import_prefix = determine_prefix (die, cu);
11733
11734 /* Figure out what the scope of the imported die is and prepend it
11735 to the name of the imported die. */
11736 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11737
11738 if (imported_die->tag != DW_TAG_namespace
11739 && imported_die->tag != DW_TAG_module)
11740 {
11741 imported_declaration = imported_name;
11742 canonical_name = imported_name_prefix;
11743 }
11744 else if (strlen (imported_name_prefix) > 0)
11745 canonical_name = obconcat (&objfile->objfile_obstack,
11746 imported_name_prefix,
11747 (cu->language == language_d ? "." : "::"),
11748 imported_name, (char *) NULL);
11749 else
11750 canonical_name = imported_name;
11751
11752 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11753 for (child_die = die->child; child_die && child_die->tag;
11754 child_die = sibling_die (child_die))
11755 {
11756 /* DWARF-4: A Fortran use statement with a “rename list” may be
11757 represented by an imported module entry with an import attribute
11758 referring to the module and owned entries corresponding to those
11759 entities that are renamed as part of being imported. */
11760
11761 if (child_die->tag != DW_TAG_imported_declaration)
11762 {
11763 complaint (&symfile_complaints,
11764 _("child DW_TAG_imported_declaration expected "
11765 "- DIE at %s [in module %s]"),
11766 sect_offset_str (child_die->sect_off),
11767 objfile_name (objfile));
11768 continue;
11769 }
11770
11771 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11772 if (import_attr == NULL)
11773 {
11774 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11775 dwarf_tag_name (child_die->tag));
11776 continue;
11777 }
11778
11779 imported_cu = cu;
11780 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11781 &imported_cu);
11782 imported_name = dwarf2_name (imported_die, imported_cu);
11783 if (imported_name == NULL)
11784 {
11785 complaint (&symfile_complaints,
11786 _("child DW_TAG_imported_declaration has unknown "
11787 "imported name - DIE at %s [in module %s]"),
11788 sect_offset_str (child_die->sect_off),
11789 objfile_name (objfile));
11790 continue;
11791 }
11792
11793 excludes.push_back (imported_name);
11794
11795 process_die (child_die, cu);
11796 }
11797
11798 add_using_directive (using_directives (cu->language),
11799 import_prefix,
11800 canonical_name,
11801 import_alias,
11802 imported_declaration,
11803 excludes,
11804 0,
11805 &objfile->objfile_obstack);
11806 }
11807
11808 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11809 types, but gives them a size of zero. Starting with version 14,
11810 ICC is compatible with GCC. */
11811
11812 static int
11813 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11814 {
11815 if (!cu->checked_producer)
11816 check_producer (cu);
11817
11818 return cu->producer_is_icc_lt_14;
11819 }
11820
11821 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11822 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11823 this, it was first present in GCC release 4.3.0. */
11824
11825 static int
11826 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11827 {
11828 if (!cu->checked_producer)
11829 check_producer (cu);
11830
11831 return cu->producer_is_gcc_lt_4_3;
11832 }
11833
11834 static file_and_directory
11835 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11836 {
11837 file_and_directory res;
11838
11839 /* Find the filename. Do not use dwarf2_name here, since the filename
11840 is not a source language identifier. */
11841 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11842 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11843
11844 if (res.comp_dir == NULL
11845 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11846 && IS_ABSOLUTE_PATH (res.name))
11847 {
11848 res.comp_dir_storage = ldirname (res.name);
11849 if (!res.comp_dir_storage.empty ())
11850 res.comp_dir = res.comp_dir_storage.c_str ();
11851 }
11852 if (res.comp_dir != NULL)
11853 {
11854 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11855 directory, get rid of it. */
11856 const char *cp = strchr (res.comp_dir, ':');
11857
11858 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11859 res.comp_dir = cp + 1;
11860 }
11861
11862 if (res.name == NULL)
11863 res.name = "<unknown>";
11864
11865 return res;
11866 }
11867
11868 /* Handle DW_AT_stmt_list for a compilation unit.
11869 DIE is the DW_TAG_compile_unit die for CU.
11870 COMP_DIR is the compilation directory. LOWPC is passed to
11871 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11872
11873 static void
11874 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11875 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11876 {
11877 struct dwarf2_per_objfile *dwarf2_per_objfile
11878 = cu->per_cu->dwarf2_per_objfile;
11879 struct objfile *objfile = dwarf2_per_objfile->objfile;
11880 struct attribute *attr;
11881 struct line_header line_header_local;
11882 hashval_t line_header_local_hash;
11883 void **slot;
11884 int decode_mapping;
11885
11886 gdb_assert (! cu->per_cu->is_debug_types);
11887
11888 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11889 if (attr == NULL)
11890 return;
11891
11892 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11893
11894 /* The line header hash table is only created if needed (it exists to
11895 prevent redundant reading of the line table for partial_units).
11896 If we're given a partial_unit, we'll need it. If we're given a
11897 compile_unit, then use the line header hash table if it's already
11898 created, but don't create one just yet. */
11899
11900 if (dwarf2_per_objfile->line_header_hash == NULL
11901 && die->tag == DW_TAG_partial_unit)
11902 {
11903 dwarf2_per_objfile->line_header_hash
11904 = htab_create_alloc_ex (127, line_header_hash_voidp,
11905 line_header_eq_voidp,
11906 free_line_header_voidp,
11907 &objfile->objfile_obstack,
11908 hashtab_obstack_allocate,
11909 dummy_obstack_deallocate);
11910 }
11911
11912 line_header_local.sect_off = line_offset;
11913 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11914 line_header_local_hash = line_header_hash (&line_header_local);
11915 if (dwarf2_per_objfile->line_header_hash != NULL)
11916 {
11917 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11918 &line_header_local,
11919 line_header_local_hash, NO_INSERT);
11920
11921 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11922 is not present in *SLOT (since if there is something in *SLOT then
11923 it will be for a partial_unit). */
11924 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11925 {
11926 gdb_assert (*slot != NULL);
11927 cu->line_header = (struct line_header *) *slot;
11928 return;
11929 }
11930 }
11931
11932 /* dwarf_decode_line_header does not yet provide sufficient information.
11933 We always have to call also dwarf_decode_lines for it. */
11934 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11935 if (lh == NULL)
11936 return;
11937
11938 cu->line_header = lh.release ();
11939 cu->line_header_die_owner = die;
11940
11941 if (dwarf2_per_objfile->line_header_hash == NULL)
11942 slot = NULL;
11943 else
11944 {
11945 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11946 &line_header_local,
11947 line_header_local_hash, INSERT);
11948 gdb_assert (slot != NULL);
11949 }
11950 if (slot != NULL && *slot == NULL)
11951 {
11952 /* This newly decoded line number information unit will be owned
11953 by line_header_hash hash table. */
11954 *slot = cu->line_header;
11955 cu->line_header_die_owner = NULL;
11956 }
11957 else
11958 {
11959 /* We cannot free any current entry in (*slot) as that struct line_header
11960 may be already used by multiple CUs. Create only temporary decoded
11961 line_header for this CU - it may happen at most once for each line
11962 number information unit. And if we're not using line_header_hash
11963 then this is what we want as well. */
11964 gdb_assert (die->tag != DW_TAG_partial_unit);
11965 }
11966 decode_mapping = (die->tag != DW_TAG_partial_unit);
11967 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11968 decode_mapping);
11969
11970 }
11971
11972 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11973
11974 static void
11975 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11976 {
11977 struct dwarf2_per_objfile *dwarf2_per_objfile
11978 = cu->per_cu->dwarf2_per_objfile;
11979 struct objfile *objfile = dwarf2_per_objfile->objfile;
11980 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11981 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11982 CORE_ADDR highpc = ((CORE_ADDR) 0);
11983 struct attribute *attr;
11984 struct die_info *child_die;
11985 CORE_ADDR baseaddr;
11986
11987 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11988
11989 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11990
11991 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11992 from finish_block. */
11993 if (lowpc == ((CORE_ADDR) -1))
11994 lowpc = highpc;
11995 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11996
11997 file_and_directory fnd = find_file_and_directory (die, cu);
11998
11999 prepare_one_comp_unit (cu, die, cu->language);
12000
12001 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
12002 standardised yet. As a workaround for the language detection we fall
12003 back to the DW_AT_producer string. */
12004 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
12005 cu->language = language_opencl;
12006
12007 /* Similar hack for Go. */
12008 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
12009 set_cu_language (DW_LANG_Go, cu);
12010
12011 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
12012
12013 /* Decode line number information if present. We do this before
12014 processing child DIEs, so that the line header table is available
12015 for DW_AT_decl_file. */
12016 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
12017
12018 /* Process all dies in compilation unit. */
12019 if (die->child != NULL)
12020 {
12021 child_die = die->child;
12022 while (child_die && child_die->tag)
12023 {
12024 process_die (child_die, cu);
12025 child_die = sibling_die (child_die);
12026 }
12027 }
12028
12029 /* Decode macro information, if present. Dwarf 2 macro information
12030 refers to information in the line number info statement program
12031 header, so we can only read it if we've read the header
12032 successfully. */
12033 attr = dwarf2_attr (die, DW_AT_macros, cu);
12034 if (attr == NULL)
12035 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
12036 if (attr && cu->line_header)
12037 {
12038 if (dwarf2_attr (die, DW_AT_macro_info, cu))
12039 complaint (&symfile_complaints,
12040 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
12041
12042 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
12043 }
12044 else
12045 {
12046 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
12047 if (attr && cu->line_header)
12048 {
12049 unsigned int macro_offset = DW_UNSND (attr);
12050
12051 dwarf_decode_macros (cu, macro_offset, 0);
12052 }
12053 }
12054 }
12055
12056 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
12057 Create the set of symtabs used by this TU, or if this TU is sharing
12058 symtabs with another TU and the symtabs have already been created
12059 then restore those symtabs in the line header.
12060 We don't need the pc/line-number mapping for type units. */
12061
12062 static void
12063 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
12064 {
12065 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
12066 struct type_unit_group *tu_group;
12067 int first_time;
12068 struct attribute *attr;
12069 unsigned int i;
12070 struct signatured_type *sig_type;
12071
12072 gdb_assert (per_cu->is_debug_types);
12073 sig_type = (struct signatured_type *) per_cu;
12074
12075 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
12076
12077 /* If we're using .gdb_index (includes -readnow) then
12078 per_cu->type_unit_group may not have been set up yet. */
12079 if (sig_type->type_unit_group == NULL)
12080 sig_type->type_unit_group = get_type_unit_group (cu, attr);
12081 tu_group = sig_type->type_unit_group;
12082
12083 /* If we've already processed this stmt_list there's no real need to
12084 do it again, we could fake it and just recreate the part we need
12085 (file name,index -> symtab mapping). If data shows this optimization
12086 is useful we can do it then. */
12087 first_time = tu_group->compunit_symtab == NULL;
12088
12089 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
12090 debug info. */
12091 line_header_up lh;
12092 if (attr != NULL)
12093 {
12094 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
12095 lh = dwarf_decode_line_header (line_offset, cu);
12096 }
12097 if (lh == NULL)
12098 {
12099 if (first_time)
12100 dwarf2_start_symtab (cu, "", NULL, 0);
12101 else
12102 {
12103 gdb_assert (tu_group->symtabs == NULL);
12104 restart_symtab (tu_group->compunit_symtab, "", 0);
12105 }
12106 return;
12107 }
12108
12109 cu->line_header = lh.release ();
12110 cu->line_header_die_owner = die;
12111
12112 if (first_time)
12113 {
12114 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
12115
12116 /* Note: We don't assign tu_group->compunit_symtab yet because we're
12117 still initializing it, and our caller (a few levels up)
12118 process_full_type_unit still needs to know if this is the first
12119 time. */
12120
12121 tu_group->num_symtabs = cu->line_header->file_names.size ();
12122 tu_group->symtabs = XNEWVEC (struct symtab *,
12123 cu->line_header->file_names.size ());
12124
12125 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12126 {
12127 file_entry &fe = cu->line_header->file_names[i];
12128
12129 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
12130
12131 if (current_subfile->symtab == NULL)
12132 {
12133 /* NOTE: start_subfile will recognize when it's been
12134 passed a file it has already seen. So we can't
12135 assume there's a simple mapping from
12136 cu->line_header->file_names to subfiles, plus
12137 cu->line_header->file_names may contain dups. */
12138 current_subfile->symtab
12139 = allocate_symtab (cust, current_subfile->name);
12140 }
12141
12142 fe.symtab = current_subfile->symtab;
12143 tu_group->symtabs[i] = fe.symtab;
12144 }
12145 }
12146 else
12147 {
12148 restart_symtab (tu_group->compunit_symtab, "", 0);
12149
12150 for (i = 0; i < cu->line_header->file_names.size (); ++i)
12151 {
12152 file_entry &fe = cu->line_header->file_names[i];
12153
12154 fe.symtab = tu_group->symtabs[i];
12155 }
12156 }
12157
12158 /* The main symtab is allocated last. Type units don't have DW_AT_name
12159 so they don't have a "real" (so to speak) symtab anyway.
12160 There is later code that will assign the main symtab to all symbols
12161 that don't have one. We need to handle the case of a symbol with a
12162 missing symtab (DW_AT_decl_file) anyway. */
12163 }
12164
12165 /* Process DW_TAG_type_unit.
12166 For TUs we want to skip the first top level sibling if it's not the
12167 actual type being defined by this TU. In this case the first top
12168 level sibling is there to provide context only. */
12169
12170 static void
12171 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
12172 {
12173 struct die_info *child_die;
12174
12175 prepare_one_comp_unit (cu, die, language_minimal);
12176
12177 /* Initialize (or reinitialize) the machinery for building symtabs.
12178 We do this before processing child DIEs, so that the line header table
12179 is available for DW_AT_decl_file. */
12180 setup_type_unit_groups (die, cu);
12181
12182 if (die->child != NULL)
12183 {
12184 child_die = die->child;
12185 while (child_die && child_die->tag)
12186 {
12187 process_die (child_die, cu);
12188 child_die = sibling_die (child_die);
12189 }
12190 }
12191 }
12192 \f
12193 /* DWO/DWP files.
12194
12195 http://gcc.gnu.org/wiki/DebugFission
12196 http://gcc.gnu.org/wiki/DebugFissionDWP
12197
12198 To simplify handling of both DWO files ("object" files with the DWARF info)
12199 and DWP files (a file with the DWOs packaged up into one file), we treat
12200 DWP files as having a collection of virtual DWO files. */
12201
12202 static hashval_t
12203 hash_dwo_file (const void *item)
12204 {
12205 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
12206 hashval_t hash;
12207
12208 hash = htab_hash_string (dwo_file->dwo_name);
12209 if (dwo_file->comp_dir != NULL)
12210 hash += htab_hash_string (dwo_file->comp_dir);
12211 return hash;
12212 }
12213
12214 static int
12215 eq_dwo_file (const void *item_lhs, const void *item_rhs)
12216 {
12217 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
12218 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
12219
12220 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
12221 return 0;
12222 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
12223 return lhs->comp_dir == rhs->comp_dir;
12224 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
12225 }
12226
12227 /* Allocate a hash table for DWO files. */
12228
12229 static htab_t
12230 allocate_dwo_file_hash_table (struct objfile *objfile)
12231 {
12232 return htab_create_alloc_ex (41,
12233 hash_dwo_file,
12234 eq_dwo_file,
12235 NULL,
12236 &objfile->objfile_obstack,
12237 hashtab_obstack_allocate,
12238 dummy_obstack_deallocate);
12239 }
12240
12241 /* Lookup DWO file DWO_NAME. */
12242
12243 static void **
12244 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
12245 const char *dwo_name,
12246 const char *comp_dir)
12247 {
12248 struct dwo_file find_entry;
12249 void **slot;
12250
12251 if (dwarf2_per_objfile->dwo_files == NULL)
12252 dwarf2_per_objfile->dwo_files
12253 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
12254
12255 memset (&find_entry, 0, sizeof (find_entry));
12256 find_entry.dwo_name = dwo_name;
12257 find_entry.comp_dir = comp_dir;
12258 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
12259
12260 return slot;
12261 }
12262
12263 static hashval_t
12264 hash_dwo_unit (const void *item)
12265 {
12266 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12267
12268 /* This drops the top 32 bits of the id, but is ok for a hash. */
12269 return dwo_unit->signature;
12270 }
12271
12272 static int
12273 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
12274 {
12275 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
12276 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
12277
12278 /* The signature is assumed to be unique within the DWO file.
12279 So while object file CU dwo_id's always have the value zero,
12280 that's OK, assuming each object file DWO file has only one CU,
12281 and that's the rule for now. */
12282 return lhs->signature == rhs->signature;
12283 }
12284
12285 /* Allocate a hash table for DWO CUs,TUs.
12286 There is one of these tables for each of CUs,TUs for each DWO file. */
12287
12288 static htab_t
12289 allocate_dwo_unit_table (struct objfile *objfile)
12290 {
12291 /* Start out with a pretty small number.
12292 Generally DWO files contain only one CU and maybe some TUs. */
12293 return htab_create_alloc_ex (3,
12294 hash_dwo_unit,
12295 eq_dwo_unit,
12296 NULL,
12297 &objfile->objfile_obstack,
12298 hashtab_obstack_allocate,
12299 dummy_obstack_deallocate);
12300 }
12301
12302 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
12303
12304 struct create_dwo_cu_data
12305 {
12306 struct dwo_file *dwo_file;
12307 struct dwo_unit dwo_unit;
12308 };
12309
12310 /* die_reader_func for create_dwo_cu. */
12311
12312 static void
12313 create_dwo_cu_reader (const struct die_reader_specs *reader,
12314 const gdb_byte *info_ptr,
12315 struct die_info *comp_unit_die,
12316 int has_children,
12317 void *datap)
12318 {
12319 struct dwarf2_cu *cu = reader->cu;
12320 sect_offset sect_off = cu->per_cu->sect_off;
12321 struct dwarf2_section_info *section = cu->per_cu->section;
12322 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
12323 struct dwo_file *dwo_file = data->dwo_file;
12324 struct dwo_unit *dwo_unit = &data->dwo_unit;
12325 struct attribute *attr;
12326
12327 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
12328 if (attr == NULL)
12329 {
12330 complaint (&symfile_complaints,
12331 _("Dwarf Error: debug entry at offset %s is missing"
12332 " its dwo_id [in module %s]"),
12333 sect_offset_str (sect_off), dwo_file->dwo_name);
12334 return;
12335 }
12336
12337 dwo_unit->dwo_file = dwo_file;
12338 dwo_unit->signature = DW_UNSND (attr);
12339 dwo_unit->section = section;
12340 dwo_unit->sect_off = sect_off;
12341 dwo_unit->length = cu->per_cu->length;
12342
12343 if (dwarf_read_debug)
12344 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12345 sect_offset_str (sect_off),
12346 hex_string (dwo_unit->signature));
12347 }
12348
12349 /* Create the dwo_units for the CUs in a DWO_FILE.
12350 Note: This function processes DWO files only, not DWP files. */
12351
12352 static void
12353 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12354 struct dwo_file &dwo_file, dwarf2_section_info &section,
12355 htab_t &cus_htab)
12356 {
12357 struct objfile *objfile = dwarf2_per_objfile->objfile;
12358 const gdb_byte *info_ptr, *end_ptr;
12359
12360 dwarf2_read_section (objfile, &section);
12361 info_ptr = section.buffer;
12362
12363 if (info_ptr == NULL)
12364 return;
12365
12366 if (dwarf_read_debug)
12367 {
12368 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12369 get_section_name (&section),
12370 get_section_file_name (&section));
12371 }
12372
12373 end_ptr = info_ptr + section.size;
12374 while (info_ptr < end_ptr)
12375 {
12376 struct dwarf2_per_cu_data per_cu;
12377 struct create_dwo_cu_data create_dwo_cu_data;
12378 struct dwo_unit *dwo_unit;
12379 void **slot;
12380 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12381
12382 memset (&create_dwo_cu_data.dwo_unit, 0,
12383 sizeof (create_dwo_cu_data.dwo_unit));
12384 memset (&per_cu, 0, sizeof (per_cu));
12385 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12386 per_cu.is_debug_types = 0;
12387 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12388 per_cu.section = &section;
12389 create_dwo_cu_data.dwo_file = &dwo_file;
12390
12391 init_cutu_and_read_dies_no_follow (
12392 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12393 info_ptr += per_cu.length;
12394
12395 // If the unit could not be parsed, skip it.
12396 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12397 continue;
12398
12399 if (cus_htab == NULL)
12400 cus_htab = allocate_dwo_unit_table (objfile);
12401
12402 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12403 *dwo_unit = create_dwo_cu_data.dwo_unit;
12404 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12405 gdb_assert (slot != NULL);
12406 if (*slot != NULL)
12407 {
12408 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12409 sect_offset dup_sect_off = dup_cu->sect_off;
12410
12411 complaint (&symfile_complaints,
12412 _("debug cu entry at offset %s is duplicate to"
12413 " the entry at offset %s, signature %s"),
12414 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12415 hex_string (dwo_unit->signature));
12416 }
12417 *slot = (void *)dwo_unit;
12418 }
12419 }
12420
12421 /* DWP file .debug_{cu,tu}_index section format:
12422 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12423
12424 DWP Version 1:
12425
12426 Both index sections have the same format, and serve to map a 64-bit
12427 signature to a set of section numbers. Each section begins with a header,
12428 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12429 indexes, and a pool of 32-bit section numbers. The index sections will be
12430 aligned at 8-byte boundaries in the file.
12431
12432 The index section header consists of:
12433
12434 V, 32 bit version number
12435 -, 32 bits unused
12436 N, 32 bit number of compilation units or type units in the index
12437 M, 32 bit number of slots in the hash table
12438
12439 Numbers are recorded using the byte order of the application binary.
12440
12441 The hash table begins at offset 16 in the section, and consists of an array
12442 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12443 order of the application binary). Unused slots in the hash table are 0.
12444 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12445
12446 The parallel table begins immediately after the hash table
12447 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12448 array of 32-bit indexes (using the byte order of the application binary),
12449 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12450 table contains a 32-bit index into the pool of section numbers. For unused
12451 hash table slots, the corresponding entry in the parallel table will be 0.
12452
12453 The pool of section numbers begins immediately following the hash table
12454 (at offset 16 + 12 * M from the beginning of the section). The pool of
12455 section numbers consists of an array of 32-bit words (using the byte order
12456 of the application binary). Each item in the array is indexed starting
12457 from 0. The hash table entry provides the index of the first section
12458 number in the set. Additional section numbers in the set follow, and the
12459 set is terminated by a 0 entry (section number 0 is not used in ELF).
12460
12461 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12462 section must be the first entry in the set, and the .debug_abbrev.dwo must
12463 be the second entry. Other members of the set may follow in any order.
12464
12465 ---
12466
12467 DWP Version 2:
12468
12469 DWP Version 2 combines all the .debug_info, etc. sections into one,
12470 and the entries in the index tables are now offsets into these sections.
12471 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12472 section.
12473
12474 Index Section Contents:
12475 Header
12476 Hash Table of Signatures dwp_hash_table.hash_table
12477 Parallel Table of Indices dwp_hash_table.unit_table
12478 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12479 Table of Section Sizes dwp_hash_table.v2.sizes
12480
12481 The index section header consists of:
12482
12483 V, 32 bit version number
12484 L, 32 bit number of columns in the table of section offsets
12485 N, 32 bit number of compilation units or type units in the index
12486 M, 32 bit number of slots in the hash table
12487
12488 Numbers are recorded using the byte order of the application binary.
12489
12490 The hash table has the same format as version 1.
12491 The parallel table of indices has the same format as version 1,
12492 except that the entries are origin-1 indices into the table of sections
12493 offsets and the table of section sizes.
12494
12495 The table of offsets begins immediately following the parallel table
12496 (at offset 16 + 12 * M from the beginning of the section). The table is
12497 a two-dimensional array of 32-bit words (using the byte order of the
12498 application binary), with L columns and N+1 rows, in row-major order.
12499 Each row in the array is indexed starting from 0. The first row provides
12500 a key to the remaining rows: each column in this row provides an identifier
12501 for a debug section, and the offsets in the same column of subsequent rows
12502 refer to that section. The section identifiers are:
12503
12504 DW_SECT_INFO 1 .debug_info.dwo
12505 DW_SECT_TYPES 2 .debug_types.dwo
12506 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12507 DW_SECT_LINE 4 .debug_line.dwo
12508 DW_SECT_LOC 5 .debug_loc.dwo
12509 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12510 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12511 DW_SECT_MACRO 8 .debug_macro.dwo
12512
12513 The offsets provided by the CU and TU index sections are the base offsets
12514 for the contributions made by each CU or TU to the corresponding section
12515 in the package file. Each CU and TU header contains an abbrev_offset
12516 field, used to find the abbreviations table for that CU or TU within the
12517 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12518 be interpreted as relative to the base offset given in the index section.
12519 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12520 should be interpreted as relative to the base offset for .debug_line.dwo,
12521 and offsets into other debug sections obtained from DWARF attributes should
12522 also be interpreted as relative to the corresponding base offset.
12523
12524 The table of sizes begins immediately following the table of offsets.
12525 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12526 with L columns and N rows, in row-major order. Each row in the array is
12527 indexed starting from 1 (row 0 is shared by the two tables).
12528
12529 ---
12530
12531 Hash table lookup is handled the same in version 1 and 2:
12532
12533 We assume that N and M will not exceed 2^32 - 1.
12534 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12535
12536 Given a 64-bit compilation unit signature or a type signature S, an entry
12537 in the hash table is located as follows:
12538
12539 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12540 the low-order k bits all set to 1.
12541
12542 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12543
12544 3) If the hash table entry at index H matches the signature, use that
12545 entry. If the hash table entry at index H is unused (all zeroes),
12546 terminate the search: the signature is not present in the table.
12547
12548 4) Let H = (H + H') modulo M. Repeat at Step 3.
12549
12550 Because M > N and H' and M are relatively prime, the search is guaranteed
12551 to stop at an unused slot or find the match. */
12552
12553 /* Create a hash table to map DWO IDs to their CU/TU entry in
12554 .debug_{info,types}.dwo in DWP_FILE.
12555 Returns NULL if there isn't one.
12556 Note: This function processes DWP files only, not DWO files. */
12557
12558 static struct dwp_hash_table *
12559 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12560 struct dwp_file *dwp_file, int is_debug_types)
12561 {
12562 struct objfile *objfile = dwarf2_per_objfile->objfile;
12563 bfd *dbfd = dwp_file->dbfd;
12564 const gdb_byte *index_ptr, *index_end;
12565 struct dwarf2_section_info *index;
12566 uint32_t version, nr_columns, nr_units, nr_slots;
12567 struct dwp_hash_table *htab;
12568
12569 if (is_debug_types)
12570 index = &dwp_file->sections.tu_index;
12571 else
12572 index = &dwp_file->sections.cu_index;
12573
12574 if (dwarf2_section_empty_p (index))
12575 return NULL;
12576 dwarf2_read_section (objfile, index);
12577
12578 index_ptr = index->buffer;
12579 index_end = index_ptr + index->size;
12580
12581 version = read_4_bytes (dbfd, index_ptr);
12582 index_ptr += 4;
12583 if (version == 2)
12584 nr_columns = read_4_bytes (dbfd, index_ptr);
12585 else
12586 nr_columns = 0;
12587 index_ptr += 4;
12588 nr_units = read_4_bytes (dbfd, index_ptr);
12589 index_ptr += 4;
12590 nr_slots = read_4_bytes (dbfd, index_ptr);
12591 index_ptr += 4;
12592
12593 if (version != 1 && version != 2)
12594 {
12595 error (_("Dwarf Error: unsupported DWP file version (%s)"
12596 " [in module %s]"),
12597 pulongest (version), dwp_file->name);
12598 }
12599 if (nr_slots != (nr_slots & -nr_slots))
12600 {
12601 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12602 " is not power of 2 [in module %s]"),
12603 pulongest (nr_slots), dwp_file->name);
12604 }
12605
12606 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12607 htab->version = version;
12608 htab->nr_columns = nr_columns;
12609 htab->nr_units = nr_units;
12610 htab->nr_slots = nr_slots;
12611 htab->hash_table = index_ptr;
12612 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12613
12614 /* Exit early if the table is empty. */
12615 if (nr_slots == 0 || nr_units == 0
12616 || (version == 2 && nr_columns == 0))
12617 {
12618 /* All must be zero. */
12619 if (nr_slots != 0 || nr_units != 0
12620 || (version == 2 && nr_columns != 0))
12621 {
12622 complaint (&symfile_complaints,
12623 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12624 " all zero [in modules %s]"),
12625 dwp_file->name);
12626 }
12627 return htab;
12628 }
12629
12630 if (version == 1)
12631 {
12632 htab->section_pool.v1.indices =
12633 htab->unit_table + sizeof (uint32_t) * nr_slots;
12634 /* It's harder to decide whether the section is too small in v1.
12635 V1 is deprecated anyway so we punt. */
12636 }
12637 else
12638 {
12639 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12640 int *ids = htab->section_pool.v2.section_ids;
12641 /* Reverse map for error checking. */
12642 int ids_seen[DW_SECT_MAX + 1];
12643 int i;
12644
12645 if (nr_columns < 2)
12646 {
12647 error (_("Dwarf Error: bad DWP hash table, too few columns"
12648 " in section table [in module %s]"),
12649 dwp_file->name);
12650 }
12651 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12652 {
12653 error (_("Dwarf Error: bad DWP hash table, too many columns"
12654 " in section table [in module %s]"),
12655 dwp_file->name);
12656 }
12657 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12658 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12659 for (i = 0; i < nr_columns; ++i)
12660 {
12661 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12662
12663 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12664 {
12665 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12666 " in section table [in module %s]"),
12667 id, dwp_file->name);
12668 }
12669 if (ids_seen[id] != -1)
12670 {
12671 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12672 " id %d in section table [in module %s]"),
12673 id, dwp_file->name);
12674 }
12675 ids_seen[id] = i;
12676 ids[i] = id;
12677 }
12678 /* Must have exactly one info or types section. */
12679 if (((ids_seen[DW_SECT_INFO] != -1)
12680 + (ids_seen[DW_SECT_TYPES] != -1))
12681 != 1)
12682 {
12683 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12684 " DWO info/types section [in module %s]"),
12685 dwp_file->name);
12686 }
12687 /* Must have an abbrev section. */
12688 if (ids_seen[DW_SECT_ABBREV] == -1)
12689 {
12690 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12691 " section [in module %s]"),
12692 dwp_file->name);
12693 }
12694 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12695 htab->section_pool.v2.sizes =
12696 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12697 * nr_units * nr_columns);
12698 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12699 * nr_units * nr_columns))
12700 > index_end)
12701 {
12702 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12703 " [in module %s]"),
12704 dwp_file->name);
12705 }
12706 }
12707
12708 return htab;
12709 }
12710
12711 /* Update SECTIONS with the data from SECTP.
12712
12713 This function is like the other "locate" section routines that are
12714 passed to bfd_map_over_sections, but in this context the sections to
12715 read comes from the DWP V1 hash table, not the full ELF section table.
12716
12717 The result is non-zero for success, or zero if an error was found. */
12718
12719 static int
12720 locate_v1_virtual_dwo_sections (asection *sectp,
12721 struct virtual_v1_dwo_sections *sections)
12722 {
12723 const struct dwop_section_names *names = &dwop_section_names;
12724
12725 if (section_is_p (sectp->name, &names->abbrev_dwo))
12726 {
12727 /* There can be only one. */
12728 if (sections->abbrev.s.section != NULL)
12729 return 0;
12730 sections->abbrev.s.section = sectp;
12731 sections->abbrev.size = bfd_get_section_size (sectp);
12732 }
12733 else if (section_is_p (sectp->name, &names->info_dwo)
12734 || section_is_p (sectp->name, &names->types_dwo))
12735 {
12736 /* There can be only one. */
12737 if (sections->info_or_types.s.section != NULL)
12738 return 0;
12739 sections->info_or_types.s.section = sectp;
12740 sections->info_or_types.size = bfd_get_section_size (sectp);
12741 }
12742 else if (section_is_p (sectp->name, &names->line_dwo))
12743 {
12744 /* There can be only one. */
12745 if (sections->line.s.section != NULL)
12746 return 0;
12747 sections->line.s.section = sectp;
12748 sections->line.size = bfd_get_section_size (sectp);
12749 }
12750 else if (section_is_p (sectp->name, &names->loc_dwo))
12751 {
12752 /* There can be only one. */
12753 if (sections->loc.s.section != NULL)
12754 return 0;
12755 sections->loc.s.section = sectp;
12756 sections->loc.size = bfd_get_section_size (sectp);
12757 }
12758 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12759 {
12760 /* There can be only one. */
12761 if (sections->macinfo.s.section != NULL)
12762 return 0;
12763 sections->macinfo.s.section = sectp;
12764 sections->macinfo.size = bfd_get_section_size (sectp);
12765 }
12766 else if (section_is_p (sectp->name, &names->macro_dwo))
12767 {
12768 /* There can be only one. */
12769 if (sections->macro.s.section != NULL)
12770 return 0;
12771 sections->macro.s.section = sectp;
12772 sections->macro.size = bfd_get_section_size (sectp);
12773 }
12774 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12775 {
12776 /* There can be only one. */
12777 if (sections->str_offsets.s.section != NULL)
12778 return 0;
12779 sections->str_offsets.s.section = sectp;
12780 sections->str_offsets.size = bfd_get_section_size (sectp);
12781 }
12782 else
12783 {
12784 /* No other kind of section is valid. */
12785 return 0;
12786 }
12787
12788 return 1;
12789 }
12790
12791 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12792 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12793 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12794 This is for DWP version 1 files. */
12795
12796 static struct dwo_unit *
12797 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12798 struct dwp_file *dwp_file,
12799 uint32_t unit_index,
12800 const char *comp_dir,
12801 ULONGEST signature, int is_debug_types)
12802 {
12803 struct objfile *objfile = dwarf2_per_objfile->objfile;
12804 const struct dwp_hash_table *dwp_htab =
12805 is_debug_types ? dwp_file->tus : dwp_file->cus;
12806 bfd *dbfd = dwp_file->dbfd;
12807 const char *kind = is_debug_types ? "TU" : "CU";
12808 struct dwo_file *dwo_file;
12809 struct dwo_unit *dwo_unit;
12810 struct virtual_v1_dwo_sections sections;
12811 void **dwo_file_slot;
12812 int i;
12813
12814 gdb_assert (dwp_file->version == 1);
12815
12816 if (dwarf_read_debug)
12817 {
12818 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12819 kind,
12820 pulongest (unit_index), hex_string (signature),
12821 dwp_file->name);
12822 }
12823
12824 /* Fetch the sections of this DWO unit.
12825 Put a limit on the number of sections we look for so that bad data
12826 doesn't cause us to loop forever. */
12827
12828 #define MAX_NR_V1_DWO_SECTIONS \
12829 (1 /* .debug_info or .debug_types */ \
12830 + 1 /* .debug_abbrev */ \
12831 + 1 /* .debug_line */ \
12832 + 1 /* .debug_loc */ \
12833 + 1 /* .debug_str_offsets */ \
12834 + 1 /* .debug_macro or .debug_macinfo */ \
12835 + 1 /* trailing zero */)
12836
12837 memset (&sections, 0, sizeof (sections));
12838
12839 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12840 {
12841 asection *sectp;
12842 uint32_t section_nr =
12843 read_4_bytes (dbfd,
12844 dwp_htab->section_pool.v1.indices
12845 + (unit_index + i) * sizeof (uint32_t));
12846
12847 if (section_nr == 0)
12848 break;
12849 if (section_nr >= dwp_file->num_sections)
12850 {
12851 error (_("Dwarf Error: bad DWP hash table, section number too large"
12852 " [in module %s]"),
12853 dwp_file->name);
12854 }
12855
12856 sectp = dwp_file->elf_sections[section_nr];
12857 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12858 {
12859 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12860 " [in module %s]"),
12861 dwp_file->name);
12862 }
12863 }
12864
12865 if (i < 2
12866 || dwarf2_section_empty_p (&sections.info_or_types)
12867 || dwarf2_section_empty_p (&sections.abbrev))
12868 {
12869 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12870 " [in module %s]"),
12871 dwp_file->name);
12872 }
12873 if (i == MAX_NR_V1_DWO_SECTIONS)
12874 {
12875 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12876 " [in module %s]"),
12877 dwp_file->name);
12878 }
12879
12880 /* It's easier for the rest of the code if we fake a struct dwo_file and
12881 have dwo_unit "live" in that. At least for now.
12882
12883 The DWP file can be made up of a random collection of CUs and TUs.
12884 However, for each CU + set of TUs that came from the same original DWO
12885 file, we can combine them back into a virtual DWO file to save space
12886 (fewer struct dwo_file objects to allocate). Remember that for really
12887 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12888
12889 std::string virtual_dwo_name =
12890 string_printf ("virtual-dwo/%d-%d-%d-%d",
12891 get_section_id (&sections.abbrev),
12892 get_section_id (&sections.line),
12893 get_section_id (&sections.loc),
12894 get_section_id (&sections.str_offsets));
12895 /* Can we use an existing virtual DWO file? */
12896 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12897 virtual_dwo_name.c_str (),
12898 comp_dir);
12899 /* Create one if necessary. */
12900 if (*dwo_file_slot == NULL)
12901 {
12902 if (dwarf_read_debug)
12903 {
12904 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12905 virtual_dwo_name.c_str ());
12906 }
12907 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12908 dwo_file->dwo_name
12909 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12910 virtual_dwo_name.c_str (),
12911 virtual_dwo_name.size ());
12912 dwo_file->comp_dir = comp_dir;
12913 dwo_file->sections.abbrev = sections.abbrev;
12914 dwo_file->sections.line = sections.line;
12915 dwo_file->sections.loc = sections.loc;
12916 dwo_file->sections.macinfo = sections.macinfo;
12917 dwo_file->sections.macro = sections.macro;
12918 dwo_file->sections.str_offsets = sections.str_offsets;
12919 /* The "str" section is global to the entire DWP file. */
12920 dwo_file->sections.str = dwp_file->sections.str;
12921 /* The info or types section is assigned below to dwo_unit,
12922 there's no need to record it in dwo_file.
12923 Also, we can't simply record type sections in dwo_file because
12924 we record a pointer into the vector in dwo_unit. As we collect more
12925 types we'll grow the vector and eventually have to reallocate space
12926 for it, invalidating all copies of pointers into the previous
12927 contents. */
12928 *dwo_file_slot = dwo_file;
12929 }
12930 else
12931 {
12932 if (dwarf_read_debug)
12933 {
12934 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12935 virtual_dwo_name.c_str ());
12936 }
12937 dwo_file = (struct dwo_file *) *dwo_file_slot;
12938 }
12939
12940 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12941 dwo_unit->dwo_file = dwo_file;
12942 dwo_unit->signature = signature;
12943 dwo_unit->section =
12944 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12945 *dwo_unit->section = sections.info_or_types;
12946 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12947
12948 return dwo_unit;
12949 }
12950
12951 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12952 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12953 piece within that section used by a TU/CU, return a virtual section
12954 of just that piece. */
12955
12956 static struct dwarf2_section_info
12957 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12958 struct dwarf2_section_info *section,
12959 bfd_size_type offset, bfd_size_type size)
12960 {
12961 struct dwarf2_section_info result;
12962 asection *sectp;
12963
12964 gdb_assert (section != NULL);
12965 gdb_assert (!section->is_virtual);
12966
12967 memset (&result, 0, sizeof (result));
12968 result.s.containing_section = section;
12969 result.is_virtual = 1;
12970
12971 if (size == 0)
12972 return result;
12973
12974 sectp = get_section_bfd_section (section);
12975
12976 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12977 bounds of the real section. This is a pretty-rare event, so just
12978 flag an error (easier) instead of a warning and trying to cope. */
12979 if (sectp == NULL
12980 || offset + size > bfd_get_section_size (sectp))
12981 {
12982 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12983 " in section %s [in module %s]"),
12984 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12985 objfile_name (dwarf2_per_objfile->objfile));
12986 }
12987
12988 result.virtual_offset = offset;
12989 result.size = size;
12990 return result;
12991 }
12992
12993 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12994 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12995 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12996 This is for DWP version 2 files. */
12997
12998 static struct dwo_unit *
12999 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
13000 struct dwp_file *dwp_file,
13001 uint32_t unit_index,
13002 const char *comp_dir,
13003 ULONGEST signature, int is_debug_types)
13004 {
13005 struct objfile *objfile = dwarf2_per_objfile->objfile;
13006 const struct dwp_hash_table *dwp_htab =
13007 is_debug_types ? dwp_file->tus : dwp_file->cus;
13008 bfd *dbfd = dwp_file->dbfd;
13009 const char *kind = is_debug_types ? "TU" : "CU";
13010 struct dwo_file *dwo_file;
13011 struct dwo_unit *dwo_unit;
13012 struct virtual_v2_dwo_sections sections;
13013 void **dwo_file_slot;
13014 int i;
13015
13016 gdb_assert (dwp_file->version == 2);
13017
13018 if (dwarf_read_debug)
13019 {
13020 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
13021 kind,
13022 pulongest (unit_index), hex_string (signature),
13023 dwp_file->name);
13024 }
13025
13026 /* Fetch the section offsets of this DWO unit. */
13027
13028 memset (&sections, 0, sizeof (sections));
13029
13030 for (i = 0; i < dwp_htab->nr_columns; ++i)
13031 {
13032 uint32_t offset = read_4_bytes (dbfd,
13033 dwp_htab->section_pool.v2.offsets
13034 + (((unit_index - 1) * dwp_htab->nr_columns
13035 + i)
13036 * sizeof (uint32_t)));
13037 uint32_t size = read_4_bytes (dbfd,
13038 dwp_htab->section_pool.v2.sizes
13039 + (((unit_index - 1) * dwp_htab->nr_columns
13040 + i)
13041 * sizeof (uint32_t)));
13042
13043 switch (dwp_htab->section_pool.v2.section_ids[i])
13044 {
13045 case DW_SECT_INFO:
13046 case DW_SECT_TYPES:
13047 sections.info_or_types_offset = offset;
13048 sections.info_or_types_size = size;
13049 break;
13050 case DW_SECT_ABBREV:
13051 sections.abbrev_offset = offset;
13052 sections.abbrev_size = size;
13053 break;
13054 case DW_SECT_LINE:
13055 sections.line_offset = offset;
13056 sections.line_size = size;
13057 break;
13058 case DW_SECT_LOC:
13059 sections.loc_offset = offset;
13060 sections.loc_size = size;
13061 break;
13062 case DW_SECT_STR_OFFSETS:
13063 sections.str_offsets_offset = offset;
13064 sections.str_offsets_size = size;
13065 break;
13066 case DW_SECT_MACINFO:
13067 sections.macinfo_offset = offset;
13068 sections.macinfo_size = size;
13069 break;
13070 case DW_SECT_MACRO:
13071 sections.macro_offset = offset;
13072 sections.macro_size = size;
13073 break;
13074 }
13075 }
13076
13077 /* It's easier for the rest of the code if we fake a struct dwo_file and
13078 have dwo_unit "live" in that. At least for now.
13079
13080 The DWP file can be made up of a random collection of CUs and TUs.
13081 However, for each CU + set of TUs that came from the same original DWO
13082 file, we can combine them back into a virtual DWO file to save space
13083 (fewer struct dwo_file objects to allocate). Remember that for really
13084 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
13085
13086 std::string virtual_dwo_name =
13087 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
13088 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
13089 (long) (sections.line_size ? sections.line_offset : 0),
13090 (long) (sections.loc_size ? sections.loc_offset : 0),
13091 (long) (sections.str_offsets_size
13092 ? sections.str_offsets_offset : 0));
13093 /* Can we use an existing virtual DWO file? */
13094 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13095 virtual_dwo_name.c_str (),
13096 comp_dir);
13097 /* Create one if necessary. */
13098 if (*dwo_file_slot == NULL)
13099 {
13100 if (dwarf_read_debug)
13101 {
13102 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
13103 virtual_dwo_name.c_str ());
13104 }
13105 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13106 dwo_file->dwo_name
13107 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
13108 virtual_dwo_name.c_str (),
13109 virtual_dwo_name.size ());
13110 dwo_file->comp_dir = comp_dir;
13111 dwo_file->sections.abbrev =
13112 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
13113 sections.abbrev_offset, sections.abbrev_size);
13114 dwo_file->sections.line =
13115 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
13116 sections.line_offset, sections.line_size);
13117 dwo_file->sections.loc =
13118 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
13119 sections.loc_offset, sections.loc_size);
13120 dwo_file->sections.macinfo =
13121 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
13122 sections.macinfo_offset, sections.macinfo_size);
13123 dwo_file->sections.macro =
13124 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
13125 sections.macro_offset, sections.macro_size);
13126 dwo_file->sections.str_offsets =
13127 create_dwp_v2_section (dwarf2_per_objfile,
13128 &dwp_file->sections.str_offsets,
13129 sections.str_offsets_offset,
13130 sections.str_offsets_size);
13131 /* The "str" section is global to the entire DWP file. */
13132 dwo_file->sections.str = dwp_file->sections.str;
13133 /* The info or types section is assigned below to dwo_unit,
13134 there's no need to record it in dwo_file.
13135 Also, we can't simply record type sections in dwo_file because
13136 we record a pointer into the vector in dwo_unit. As we collect more
13137 types we'll grow the vector and eventually have to reallocate space
13138 for it, invalidating all copies of pointers into the previous
13139 contents. */
13140 *dwo_file_slot = dwo_file;
13141 }
13142 else
13143 {
13144 if (dwarf_read_debug)
13145 {
13146 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
13147 virtual_dwo_name.c_str ());
13148 }
13149 dwo_file = (struct dwo_file *) *dwo_file_slot;
13150 }
13151
13152 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
13153 dwo_unit->dwo_file = dwo_file;
13154 dwo_unit->signature = signature;
13155 dwo_unit->section =
13156 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
13157 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
13158 is_debug_types
13159 ? &dwp_file->sections.types
13160 : &dwp_file->sections.info,
13161 sections.info_or_types_offset,
13162 sections.info_or_types_size);
13163 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
13164
13165 return dwo_unit;
13166 }
13167
13168 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
13169 Returns NULL if the signature isn't found. */
13170
13171 static struct dwo_unit *
13172 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
13173 struct dwp_file *dwp_file, const char *comp_dir,
13174 ULONGEST signature, int is_debug_types)
13175 {
13176 const struct dwp_hash_table *dwp_htab =
13177 is_debug_types ? dwp_file->tus : dwp_file->cus;
13178 bfd *dbfd = dwp_file->dbfd;
13179 uint32_t mask = dwp_htab->nr_slots - 1;
13180 uint32_t hash = signature & mask;
13181 uint32_t hash2 = ((signature >> 32) & mask) | 1;
13182 unsigned int i;
13183 void **slot;
13184 struct dwo_unit find_dwo_cu;
13185
13186 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
13187 find_dwo_cu.signature = signature;
13188 slot = htab_find_slot (is_debug_types
13189 ? dwp_file->loaded_tus
13190 : dwp_file->loaded_cus,
13191 &find_dwo_cu, INSERT);
13192
13193 if (*slot != NULL)
13194 return (struct dwo_unit *) *slot;
13195
13196 /* Use a for loop so that we don't loop forever on bad debug info. */
13197 for (i = 0; i < dwp_htab->nr_slots; ++i)
13198 {
13199 ULONGEST signature_in_table;
13200
13201 signature_in_table =
13202 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
13203 if (signature_in_table == signature)
13204 {
13205 uint32_t unit_index =
13206 read_4_bytes (dbfd,
13207 dwp_htab->unit_table + hash * sizeof (uint32_t));
13208
13209 if (dwp_file->version == 1)
13210 {
13211 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
13212 dwp_file, unit_index,
13213 comp_dir, signature,
13214 is_debug_types);
13215 }
13216 else
13217 {
13218 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
13219 dwp_file, unit_index,
13220 comp_dir, signature,
13221 is_debug_types);
13222 }
13223 return (struct dwo_unit *) *slot;
13224 }
13225 if (signature_in_table == 0)
13226 return NULL;
13227 hash = (hash + hash2) & mask;
13228 }
13229
13230 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
13231 " [in module %s]"),
13232 dwp_file->name);
13233 }
13234
13235 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
13236 Open the file specified by FILE_NAME and hand it off to BFD for
13237 preliminary analysis. Return a newly initialized bfd *, which
13238 includes a canonicalized copy of FILE_NAME.
13239 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
13240 SEARCH_CWD is true if the current directory is to be searched.
13241 It will be searched before debug-file-directory.
13242 If successful, the file is added to the bfd include table of the
13243 objfile's bfd (see gdb_bfd_record_inclusion).
13244 If unable to find/open the file, return NULL.
13245 NOTE: This function is derived from symfile_bfd_open. */
13246
13247 static gdb_bfd_ref_ptr
13248 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13249 const char *file_name, int is_dwp, int search_cwd)
13250 {
13251 int desc;
13252 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
13253 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
13254 to debug_file_directory. */
13255 const char *search_path;
13256 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
13257
13258 gdb::unique_xmalloc_ptr<char> search_path_holder;
13259 if (search_cwd)
13260 {
13261 if (*debug_file_directory != '\0')
13262 {
13263 search_path_holder.reset (concat (".", dirname_separator_string,
13264 debug_file_directory,
13265 (char *) NULL));
13266 search_path = search_path_holder.get ();
13267 }
13268 else
13269 search_path = ".";
13270 }
13271 else
13272 search_path = debug_file_directory;
13273
13274 openp_flags flags = OPF_RETURN_REALPATH;
13275 if (is_dwp)
13276 flags |= OPF_SEARCH_IN_PATH;
13277
13278 gdb::unique_xmalloc_ptr<char> absolute_name;
13279 desc = openp (search_path, flags, file_name,
13280 O_RDONLY | O_BINARY, &absolute_name);
13281 if (desc < 0)
13282 return NULL;
13283
13284 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
13285 gnutarget, desc));
13286 if (sym_bfd == NULL)
13287 return NULL;
13288 bfd_set_cacheable (sym_bfd.get (), 1);
13289
13290 if (!bfd_check_format (sym_bfd.get (), bfd_object))
13291 return NULL;
13292
13293 /* Success. Record the bfd as having been included by the objfile's bfd.
13294 This is important because things like demangled_names_hash lives in the
13295 objfile's per_bfd space and may have references to things like symbol
13296 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
13297 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13298
13299 return sym_bfd;
13300 }
13301
13302 /* Try to open DWO file FILE_NAME.
13303 COMP_DIR is the DW_AT_comp_dir attribute.
13304 The result is the bfd handle of the file.
13305 If there is a problem finding or opening the file, return NULL.
13306 Upon success, the canonicalized path of the file is stored in the bfd,
13307 same as symfile_bfd_open. */
13308
13309 static gdb_bfd_ref_ptr
13310 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13311 const char *file_name, const char *comp_dir)
13312 {
13313 if (IS_ABSOLUTE_PATH (file_name))
13314 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13315 0 /*is_dwp*/, 0 /*search_cwd*/);
13316
13317 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
13318
13319 if (comp_dir != NULL)
13320 {
13321 char *path_to_try = concat (comp_dir, SLASH_STRING,
13322 file_name, (char *) NULL);
13323
13324 /* NOTE: If comp_dir is a relative path, this will also try the
13325 search path, which seems useful. */
13326 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
13327 path_to_try,
13328 0 /*is_dwp*/,
13329 1 /*search_cwd*/));
13330 xfree (path_to_try);
13331 if (abfd != NULL)
13332 return abfd;
13333 }
13334
13335 /* That didn't work, try debug-file-directory, which, despite its name,
13336 is a list of paths. */
13337
13338 if (*debug_file_directory == '\0')
13339 return NULL;
13340
13341 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13342 0 /*is_dwp*/, 1 /*search_cwd*/);
13343 }
13344
13345 /* This function is mapped across the sections and remembers the offset and
13346 size of each of the DWO debugging sections we are interested in. */
13347
13348 static void
13349 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13350 {
13351 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13352 const struct dwop_section_names *names = &dwop_section_names;
13353
13354 if (section_is_p (sectp->name, &names->abbrev_dwo))
13355 {
13356 dwo_sections->abbrev.s.section = sectp;
13357 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
13358 }
13359 else if (section_is_p (sectp->name, &names->info_dwo))
13360 {
13361 dwo_sections->info.s.section = sectp;
13362 dwo_sections->info.size = bfd_get_section_size (sectp);
13363 }
13364 else if (section_is_p (sectp->name, &names->line_dwo))
13365 {
13366 dwo_sections->line.s.section = sectp;
13367 dwo_sections->line.size = bfd_get_section_size (sectp);
13368 }
13369 else if (section_is_p (sectp->name, &names->loc_dwo))
13370 {
13371 dwo_sections->loc.s.section = sectp;
13372 dwo_sections->loc.size = bfd_get_section_size (sectp);
13373 }
13374 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13375 {
13376 dwo_sections->macinfo.s.section = sectp;
13377 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
13378 }
13379 else if (section_is_p (sectp->name, &names->macro_dwo))
13380 {
13381 dwo_sections->macro.s.section = sectp;
13382 dwo_sections->macro.size = bfd_get_section_size (sectp);
13383 }
13384 else if (section_is_p (sectp->name, &names->str_dwo))
13385 {
13386 dwo_sections->str.s.section = sectp;
13387 dwo_sections->str.size = bfd_get_section_size (sectp);
13388 }
13389 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13390 {
13391 dwo_sections->str_offsets.s.section = sectp;
13392 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
13393 }
13394 else if (section_is_p (sectp->name, &names->types_dwo))
13395 {
13396 struct dwarf2_section_info type_section;
13397
13398 memset (&type_section, 0, sizeof (type_section));
13399 type_section.s.section = sectp;
13400 type_section.size = bfd_get_section_size (sectp);
13401 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
13402 &type_section);
13403 }
13404 }
13405
13406 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13407 by PER_CU. This is for the non-DWP case.
13408 The result is NULL if DWO_NAME can't be found. */
13409
13410 static struct dwo_file *
13411 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13412 const char *dwo_name, const char *comp_dir)
13413 {
13414 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13415 struct objfile *objfile = dwarf2_per_objfile->objfile;
13416 struct dwo_file *dwo_file;
13417 struct cleanup *cleanups;
13418
13419 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
13420 if (dbfd == NULL)
13421 {
13422 if (dwarf_read_debug)
13423 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13424 return NULL;
13425 }
13426 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
13427 dwo_file->dwo_name = dwo_name;
13428 dwo_file->comp_dir = comp_dir;
13429 dwo_file->dbfd = dbfd.release ();
13430
13431 free_dwo_file_cleanup_data *cleanup_data = XNEW (free_dwo_file_cleanup_data);
13432 cleanup_data->dwo_file = dwo_file;
13433 cleanup_data->dwarf2_per_objfile = dwarf2_per_objfile;
13434
13435 cleanups = make_cleanup (free_dwo_file_cleanup, cleanup_data);
13436
13437 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
13438 &dwo_file->sections);
13439
13440 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13441 dwo_file->cus);
13442
13443 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file,
13444 dwo_file->sections.types, dwo_file->tus);
13445
13446 discard_cleanups (cleanups);
13447
13448 if (dwarf_read_debug)
13449 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13450
13451 return dwo_file;
13452 }
13453
13454 /* This function is mapped across the sections and remembers the offset and
13455 size of each of the DWP debugging sections common to version 1 and 2 that
13456 we are interested in. */
13457
13458 static void
13459 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13460 void *dwp_file_ptr)
13461 {
13462 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13463 const struct dwop_section_names *names = &dwop_section_names;
13464 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13465
13466 /* Record the ELF section number for later lookup: this is what the
13467 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13468 gdb_assert (elf_section_nr < dwp_file->num_sections);
13469 dwp_file->elf_sections[elf_section_nr] = sectp;
13470
13471 /* Look for specific sections that we need. */
13472 if (section_is_p (sectp->name, &names->str_dwo))
13473 {
13474 dwp_file->sections.str.s.section = sectp;
13475 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13476 }
13477 else if (section_is_p (sectp->name, &names->cu_index))
13478 {
13479 dwp_file->sections.cu_index.s.section = sectp;
13480 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13481 }
13482 else if (section_is_p (sectp->name, &names->tu_index))
13483 {
13484 dwp_file->sections.tu_index.s.section = sectp;
13485 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13486 }
13487 }
13488
13489 /* This function is mapped across the sections and remembers the offset and
13490 size of each of the DWP version 2 debugging sections that we are interested
13491 in. This is split into a separate function because we don't know if we
13492 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13493
13494 static void
13495 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13496 {
13497 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13498 const struct dwop_section_names *names = &dwop_section_names;
13499 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13500
13501 /* Record the ELF section number for later lookup: this is what the
13502 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13503 gdb_assert (elf_section_nr < dwp_file->num_sections);
13504 dwp_file->elf_sections[elf_section_nr] = sectp;
13505
13506 /* Look for specific sections that we need. */
13507 if (section_is_p (sectp->name, &names->abbrev_dwo))
13508 {
13509 dwp_file->sections.abbrev.s.section = sectp;
13510 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13511 }
13512 else if (section_is_p (sectp->name, &names->info_dwo))
13513 {
13514 dwp_file->sections.info.s.section = sectp;
13515 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13516 }
13517 else if (section_is_p (sectp->name, &names->line_dwo))
13518 {
13519 dwp_file->sections.line.s.section = sectp;
13520 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13521 }
13522 else if (section_is_p (sectp->name, &names->loc_dwo))
13523 {
13524 dwp_file->sections.loc.s.section = sectp;
13525 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13526 }
13527 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13528 {
13529 dwp_file->sections.macinfo.s.section = sectp;
13530 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13531 }
13532 else if (section_is_p (sectp->name, &names->macro_dwo))
13533 {
13534 dwp_file->sections.macro.s.section = sectp;
13535 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13536 }
13537 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13538 {
13539 dwp_file->sections.str_offsets.s.section = sectp;
13540 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13541 }
13542 else if (section_is_p (sectp->name, &names->types_dwo))
13543 {
13544 dwp_file->sections.types.s.section = sectp;
13545 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13546 }
13547 }
13548
13549 /* Hash function for dwp_file loaded CUs/TUs. */
13550
13551 static hashval_t
13552 hash_dwp_loaded_cutus (const void *item)
13553 {
13554 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13555
13556 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13557 return dwo_unit->signature;
13558 }
13559
13560 /* Equality function for dwp_file loaded CUs/TUs. */
13561
13562 static int
13563 eq_dwp_loaded_cutus (const void *a, const void *b)
13564 {
13565 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13566 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13567
13568 return dua->signature == dub->signature;
13569 }
13570
13571 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13572
13573 static htab_t
13574 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13575 {
13576 return htab_create_alloc_ex (3,
13577 hash_dwp_loaded_cutus,
13578 eq_dwp_loaded_cutus,
13579 NULL,
13580 &objfile->objfile_obstack,
13581 hashtab_obstack_allocate,
13582 dummy_obstack_deallocate);
13583 }
13584
13585 /* Try to open DWP file FILE_NAME.
13586 The result is the bfd handle of the file.
13587 If there is a problem finding or opening the file, return NULL.
13588 Upon success, the canonicalized path of the file is stored in the bfd,
13589 same as symfile_bfd_open. */
13590
13591 static gdb_bfd_ref_ptr
13592 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13593 const char *file_name)
13594 {
13595 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13596 1 /*is_dwp*/,
13597 1 /*search_cwd*/));
13598 if (abfd != NULL)
13599 return abfd;
13600
13601 /* Work around upstream bug 15652.
13602 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13603 [Whether that's a "bug" is debatable, but it is getting in our way.]
13604 We have no real idea where the dwp file is, because gdb's realpath-ing
13605 of the executable's path may have discarded the needed info.
13606 [IWBN if the dwp file name was recorded in the executable, akin to
13607 .gnu_debuglink, but that doesn't exist yet.]
13608 Strip the directory from FILE_NAME and search again. */
13609 if (*debug_file_directory != '\0')
13610 {
13611 /* Don't implicitly search the current directory here.
13612 If the user wants to search "." to handle this case,
13613 it must be added to debug-file-directory. */
13614 return try_open_dwop_file (dwarf2_per_objfile,
13615 lbasename (file_name), 1 /*is_dwp*/,
13616 0 /*search_cwd*/);
13617 }
13618
13619 return NULL;
13620 }
13621
13622 /* Initialize the use of the DWP file for the current objfile.
13623 By convention the name of the DWP file is ${objfile}.dwp.
13624 The result is NULL if it can't be found. */
13625
13626 static struct dwp_file *
13627 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13628 {
13629 struct objfile *objfile = dwarf2_per_objfile->objfile;
13630 struct dwp_file *dwp_file;
13631
13632 /* Try to find first .dwp for the binary file before any symbolic links
13633 resolving. */
13634
13635 /* If the objfile is a debug file, find the name of the real binary
13636 file and get the name of dwp file from there. */
13637 std::string dwp_name;
13638 if (objfile->separate_debug_objfile_backlink != NULL)
13639 {
13640 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13641 const char *backlink_basename = lbasename (backlink->original_name);
13642
13643 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13644 }
13645 else
13646 dwp_name = objfile->original_name;
13647
13648 dwp_name += ".dwp";
13649
13650 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13651 if (dbfd == NULL
13652 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13653 {
13654 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13655 dwp_name = objfile_name (objfile);
13656 dwp_name += ".dwp";
13657 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13658 }
13659
13660 if (dbfd == NULL)
13661 {
13662 if (dwarf_read_debug)
13663 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13664 return NULL;
13665 }
13666 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13667 dwp_file->name = bfd_get_filename (dbfd.get ());
13668 dwp_file->dbfd = dbfd.release ();
13669
13670 /* +1: section 0 is unused */
13671 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13672 dwp_file->elf_sections =
13673 OBSTACK_CALLOC (&objfile->objfile_obstack,
13674 dwp_file->num_sections, asection *);
13675
13676 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13677 dwp_file);
13678
13679 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13680
13681 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13682
13683 /* The DWP file version is stored in the hash table. Oh well. */
13684 if (dwp_file->cus && dwp_file->tus
13685 && dwp_file->cus->version != dwp_file->tus->version)
13686 {
13687 /* Technically speaking, we should try to limp along, but this is
13688 pretty bizarre. We use pulongest here because that's the established
13689 portability solution (e.g, we cannot use %u for uint32_t). */
13690 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13691 " TU version %s [in DWP file %s]"),
13692 pulongest (dwp_file->cus->version),
13693 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13694 }
13695
13696 if (dwp_file->cus)
13697 dwp_file->version = dwp_file->cus->version;
13698 else if (dwp_file->tus)
13699 dwp_file->version = dwp_file->tus->version;
13700 else
13701 dwp_file->version = 2;
13702
13703 if (dwp_file->version == 2)
13704 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13705 dwp_file);
13706
13707 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13708 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13709
13710 if (dwarf_read_debug)
13711 {
13712 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13713 fprintf_unfiltered (gdb_stdlog,
13714 " %s CUs, %s TUs\n",
13715 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13716 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13717 }
13718
13719 return dwp_file;
13720 }
13721
13722 /* Wrapper around open_and_init_dwp_file, only open it once. */
13723
13724 static struct dwp_file *
13725 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13726 {
13727 if (! dwarf2_per_objfile->dwp_checked)
13728 {
13729 dwarf2_per_objfile->dwp_file
13730 = open_and_init_dwp_file (dwarf2_per_objfile);
13731 dwarf2_per_objfile->dwp_checked = 1;
13732 }
13733 return dwarf2_per_objfile->dwp_file;
13734 }
13735
13736 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13737 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13738 or in the DWP file for the objfile, referenced by THIS_UNIT.
13739 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13740 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13741
13742 This is called, for example, when wanting to read a variable with a
13743 complex location. Therefore we don't want to do file i/o for every call.
13744 Therefore we don't want to look for a DWO file on every call.
13745 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13746 then we check if we've already seen DWO_NAME, and only THEN do we check
13747 for a DWO file.
13748
13749 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13750 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13751
13752 static struct dwo_unit *
13753 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13754 const char *dwo_name, const char *comp_dir,
13755 ULONGEST signature, int is_debug_types)
13756 {
13757 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13758 struct objfile *objfile = dwarf2_per_objfile->objfile;
13759 const char *kind = is_debug_types ? "TU" : "CU";
13760 void **dwo_file_slot;
13761 struct dwo_file *dwo_file;
13762 struct dwp_file *dwp_file;
13763
13764 /* First see if there's a DWP file.
13765 If we have a DWP file but didn't find the DWO inside it, don't
13766 look for the original DWO file. It makes gdb behave differently
13767 depending on whether one is debugging in the build tree. */
13768
13769 dwp_file = get_dwp_file (dwarf2_per_objfile);
13770 if (dwp_file != NULL)
13771 {
13772 const struct dwp_hash_table *dwp_htab =
13773 is_debug_types ? dwp_file->tus : dwp_file->cus;
13774
13775 if (dwp_htab != NULL)
13776 {
13777 struct dwo_unit *dwo_cutu =
13778 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13779 signature, is_debug_types);
13780
13781 if (dwo_cutu != NULL)
13782 {
13783 if (dwarf_read_debug)
13784 {
13785 fprintf_unfiltered (gdb_stdlog,
13786 "Virtual DWO %s %s found: @%s\n",
13787 kind, hex_string (signature),
13788 host_address_to_string (dwo_cutu));
13789 }
13790 return dwo_cutu;
13791 }
13792 }
13793 }
13794 else
13795 {
13796 /* No DWP file, look for the DWO file. */
13797
13798 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13799 dwo_name, comp_dir);
13800 if (*dwo_file_slot == NULL)
13801 {
13802 /* Read in the file and build a table of the CUs/TUs it contains. */
13803 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13804 }
13805 /* NOTE: This will be NULL if unable to open the file. */
13806 dwo_file = (struct dwo_file *) *dwo_file_slot;
13807
13808 if (dwo_file != NULL)
13809 {
13810 struct dwo_unit *dwo_cutu = NULL;
13811
13812 if (is_debug_types && dwo_file->tus)
13813 {
13814 struct dwo_unit find_dwo_cutu;
13815
13816 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13817 find_dwo_cutu.signature = signature;
13818 dwo_cutu
13819 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13820 }
13821 else if (!is_debug_types && dwo_file->cus)
13822 {
13823 struct dwo_unit find_dwo_cutu;
13824
13825 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13826 find_dwo_cutu.signature = signature;
13827 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13828 &find_dwo_cutu);
13829 }
13830
13831 if (dwo_cutu != NULL)
13832 {
13833 if (dwarf_read_debug)
13834 {
13835 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13836 kind, dwo_name, hex_string (signature),
13837 host_address_to_string (dwo_cutu));
13838 }
13839 return dwo_cutu;
13840 }
13841 }
13842 }
13843
13844 /* We didn't find it. This could mean a dwo_id mismatch, or
13845 someone deleted the DWO/DWP file, or the search path isn't set up
13846 correctly to find the file. */
13847
13848 if (dwarf_read_debug)
13849 {
13850 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13851 kind, dwo_name, hex_string (signature));
13852 }
13853
13854 /* This is a warning and not a complaint because it can be caused by
13855 pilot error (e.g., user accidentally deleting the DWO). */
13856 {
13857 /* Print the name of the DWP file if we looked there, helps the user
13858 better diagnose the problem. */
13859 std::string dwp_text;
13860
13861 if (dwp_file != NULL)
13862 dwp_text = string_printf (" [in DWP file %s]",
13863 lbasename (dwp_file->name));
13864
13865 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13866 " [in module %s]"),
13867 kind, dwo_name, hex_string (signature),
13868 dwp_text.c_str (),
13869 this_unit->is_debug_types ? "TU" : "CU",
13870 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13871 }
13872 return NULL;
13873 }
13874
13875 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13876 See lookup_dwo_cutu_unit for details. */
13877
13878 static struct dwo_unit *
13879 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13880 const char *dwo_name, const char *comp_dir,
13881 ULONGEST signature)
13882 {
13883 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13884 }
13885
13886 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13887 See lookup_dwo_cutu_unit for details. */
13888
13889 static struct dwo_unit *
13890 lookup_dwo_type_unit (struct signatured_type *this_tu,
13891 const char *dwo_name, const char *comp_dir)
13892 {
13893 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13894 }
13895
13896 /* Traversal function for queue_and_load_all_dwo_tus. */
13897
13898 static int
13899 queue_and_load_dwo_tu (void **slot, void *info)
13900 {
13901 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13902 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13903 ULONGEST signature = dwo_unit->signature;
13904 struct signatured_type *sig_type =
13905 lookup_dwo_signatured_type (per_cu->cu, signature);
13906
13907 if (sig_type != NULL)
13908 {
13909 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13910
13911 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13912 a real dependency of PER_CU on SIG_TYPE. That is detected later
13913 while processing PER_CU. */
13914 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13915 load_full_type_unit (sig_cu);
13916 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13917 }
13918
13919 return 1;
13920 }
13921
13922 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13923 The DWO may have the only definition of the type, though it may not be
13924 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13925 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13926
13927 static void
13928 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13929 {
13930 struct dwo_unit *dwo_unit;
13931 struct dwo_file *dwo_file;
13932
13933 gdb_assert (!per_cu->is_debug_types);
13934 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13935 gdb_assert (per_cu->cu != NULL);
13936
13937 dwo_unit = per_cu->cu->dwo_unit;
13938 gdb_assert (dwo_unit != NULL);
13939
13940 dwo_file = dwo_unit->dwo_file;
13941 if (dwo_file->tus != NULL)
13942 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13943 }
13944
13945 /* Free all resources associated with DWO_FILE.
13946 Close the DWO file and munmap the sections.
13947 All memory should be on the objfile obstack. */
13948
13949 static void
13950 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
13951 {
13952
13953 /* Note: dbfd is NULL for virtual DWO files. */
13954 gdb_bfd_unref (dwo_file->dbfd);
13955
13956 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13957 }
13958
13959 /* Wrapper for free_dwo_file for use in cleanups. */
13960
13961 static void
13962 free_dwo_file_cleanup (void *arg)
13963 {
13964 struct free_dwo_file_cleanup_data *data
13965 = (struct free_dwo_file_cleanup_data *) arg;
13966 struct objfile *objfile = data->dwarf2_per_objfile->objfile;
13967
13968 free_dwo_file (data->dwo_file, objfile);
13969
13970 xfree (data);
13971 }
13972
13973 /* Traversal function for free_dwo_files. */
13974
13975 static int
13976 free_dwo_file_from_slot (void **slot, void *info)
13977 {
13978 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13979 struct objfile *objfile = (struct objfile *) info;
13980
13981 free_dwo_file (dwo_file, objfile);
13982
13983 return 1;
13984 }
13985
13986 /* Free all resources associated with DWO_FILES. */
13987
13988 static void
13989 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13990 {
13991 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13992 }
13993 \f
13994 /* Read in various DIEs. */
13995
13996 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13997 Inherit only the children of the DW_AT_abstract_origin DIE not being
13998 already referenced by DW_AT_abstract_origin from the children of the
13999 current DIE. */
14000
14001 static void
14002 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
14003 {
14004 struct die_info *child_die;
14005 sect_offset *offsetp;
14006 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
14007 struct die_info *origin_die;
14008 /* Iterator of the ORIGIN_DIE children. */
14009 struct die_info *origin_child_die;
14010 struct attribute *attr;
14011 struct dwarf2_cu *origin_cu;
14012 struct pending **origin_previous_list_in_scope;
14013
14014 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14015 if (!attr)
14016 return;
14017
14018 /* Note that following die references may follow to a die in a
14019 different cu. */
14020
14021 origin_cu = cu;
14022 origin_die = follow_die_ref (die, attr, &origin_cu);
14023
14024 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
14025 symbols in. */
14026 origin_previous_list_in_scope = origin_cu->list_in_scope;
14027 origin_cu->list_in_scope = cu->list_in_scope;
14028
14029 if (die->tag != origin_die->tag
14030 && !(die->tag == DW_TAG_inlined_subroutine
14031 && origin_die->tag == DW_TAG_subprogram))
14032 complaint (&symfile_complaints,
14033 _("DIE %s and its abstract origin %s have different tags"),
14034 sect_offset_str (die->sect_off),
14035 sect_offset_str (origin_die->sect_off));
14036
14037 std::vector<sect_offset> offsets;
14038
14039 for (child_die = die->child;
14040 child_die && child_die->tag;
14041 child_die = sibling_die (child_die))
14042 {
14043 struct die_info *child_origin_die;
14044 struct dwarf2_cu *child_origin_cu;
14045
14046 /* We are trying to process concrete instance entries:
14047 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
14048 it's not relevant to our analysis here. i.e. detecting DIEs that are
14049 present in the abstract instance but not referenced in the concrete
14050 one. */
14051 if (child_die->tag == DW_TAG_call_site
14052 || child_die->tag == DW_TAG_GNU_call_site)
14053 continue;
14054
14055 /* For each CHILD_DIE, find the corresponding child of
14056 ORIGIN_DIE. If there is more than one layer of
14057 DW_AT_abstract_origin, follow them all; there shouldn't be,
14058 but GCC versions at least through 4.4 generate this (GCC PR
14059 40573). */
14060 child_origin_die = child_die;
14061 child_origin_cu = cu;
14062 while (1)
14063 {
14064 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
14065 child_origin_cu);
14066 if (attr == NULL)
14067 break;
14068 child_origin_die = follow_die_ref (child_origin_die, attr,
14069 &child_origin_cu);
14070 }
14071
14072 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
14073 counterpart may exist. */
14074 if (child_origin_die != child_die)
14075 {
14076 if (child_die->tag != child_origin_die->tag
14077 && !(child_die->tag == DW_TAG_inlined_subroutine
14078 && child_origin_die->tag == DW_TAG_subprogram))
14079 complaint (&symfile_complaints,
14080 _("Child DIE %s and its abstract origin %s have "
14081 "different tags"),
14082 sect_offset_str (child_die->sect_off),
14083 sect_offset_str (child_origin_die->sect_off));
14084 if (child_origin_die->parent != origin_die)
14085 complaint (&symfile_complaints,
14086 _("Child DIE %s and its abstract origin %s have "
14087 "different parents"),
14088 sect_offset_str (child_die->sect_off),
14089 sect_offset_str (child_origin_die->sect_off));
14090 else
14091 offsets.push_back (child_origin_die->sect_off);
14092 }
14093 }
14094 std::sort (offsets.begin (), offsets.end ());
14095 sect_offset *offsets_end = offsets.data () + offsets.size ();
14096 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
14097 if (offsetp[-1] == *offsetp)
14098 complaint (&symfile_complaints,
14099 _("Multiple children of DIE %s refer "
14100 "to DIE %s as their abstract origin"),
14101 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
14102
14103 offsetp = offsets.data ();
14104 origin_child_die = origin_die->child;
14105 while (origin_child_die && origin_child_die->tag)
14106 {
14107 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
14108 while (offsetp < offsets_end
14109 && *offsetp < origin_child_die->sect_off)
14110 offsetp++;
14111 if (offsetp >= offsets_end
14112 || *offsetp > origin_child_die->sect_off)
14113 {
14114 /* Found that ORIGIN_CHILD_DIE is really not referenced.
14115 Check whether we're already processing ORIGIN_CHILD_DIE.
14116 This can happen with mutually referenced abstract_origins.
14117 PR 16581. */
14118 if (!origin_child_die->in_process)
14119 process_die (origin_child_die, origin_cu);
14120 }
14121 origin_child_die = sibling_die (origin_child_die);
14122 }
14123 origin_cu->list_in_scope = origin_previous_list_in_scope;
14124 }
14125
14126 static void
14127 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
14128 {
14129 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14130 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14131 struct context_stack *newobj;
14132 CORE_ADDR lowpc;
14133 CORE_ADDR highpc;
14134 struct die_info *child_die;
14135 struct attribute *attr, *call_line, *call_file;
14136 const char *name;
14137 CORE_ADDR baseaddr;
14138 struct block *block;
14139 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14140 std::vector<struct symbol *> template_args;
14141 struct template_symbol *templ_func = NULL;
14142
14143 if (inlined_func)
14144 {
14145 /* If we do not have call site information, we can't show the
14146 caller of this inlined function. That's too confusing, so
14147 only use the scope for local variables. */
14148 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
14149 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
14150 if (call_line == NULL || call_file == NULL)
14151 {
14152 read_lexical_block_scope (die, cu);
14153 return;
14154 }
14155 }
14156
14157 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14158
14159 name = dwarf2_name (die, cu);
14160
14161 /* Ignore functions with missing or empty names. These are actually
14162 illegal according to the DWARF standard. */
14163 if (name == NULL)
14164 {
14165 complaint (&symfile_complaints,
14166 _("missing name for subprogram DIE at %s"),
14167 sect_offset_str (die->sect_off));
14168 return;
14169 }
14170
14171 /* Ignore functions with missing or invalid low and high pc attributes. */
14172 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
14173 <= PC_BOUNDS_INVALID)
14174 {
14175 attr = dwarf2_attr (die, DW_AT_external, cu);
14176 if (!attr || !DW_UNSND (attr))
14177 complaint (&symfile_complaints,
14178 _("cannot get low and high bounds "
14179 "for subprogram DIE at %s"),
14180 sect_offset_str (die->sect_off));
14181 return;
14182 }
14183
14184 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14185 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14186
14187 /* If we have any template arguments, then we must allocate a
14188 different sort of symbol. */
14189 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
14190 {
14191 if (child_die->tag == DW_TAG_template_type_param
14192 || child_die->tag == DW_TAG_template_value_param)
14193 {
14194 templ_func = allocate_template_symbol (objfile);
14195 templ_func->subclass = SYMBOL_TEMPLATE;
14196 break;
14197 }
14198 }
14199
14200 newobj = push_context (0, lowpc);
14201 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
14202 (struct symbol *) templ_func);
14203
14204 /* If there is a location expression for DW_AT_frame_base, record
14205 it. */
14206 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
14207 if (attr)
14208 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
14209
14210 /* If there is a location for the static link, record it. */
14211 newobj->static_link = NULL;
14212 attr = dwarf2_attr (die, DW_AT_static_link, cu);
14213 if (attr)
14214 {
14215 newobj->static_link
14216 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
14217 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
14218 }
14219
14220 cu->list_in_scope = &local_symbols;
14221
14222 if (die->child != NULL)
14223 {
14224 child_die = die->child;
14225 while (child_die && child_die->tag)
14226 {
14227 if (child_die->tag == DW_TAG_template_type_param
14228 || child_die->tag == DW_TAG_template_value_param)
14229 {
14230 struct symbol *arg = new_symbol (child_die, NULL, cu);
14231
14232 if (arg != NULL)
14233 template_args.push_back (arg);
14234 }
14235 else
14236 process_die (child_die, cu);
14237 child_die = sibling_die (child_die);
14238 }
14239 }
14240
14241 inherit_abstract_dies (die, cu);
14242
14243 /* If we have a DW_AT_specification, we might need to import using
14244 directives from the context of the specification DIE. See the
14245 comment in determine_prefix. */
14246 if (cu->language == language_cplus
14247 && dwarf2_attr (die, DW_AT_specification, cu))
14248 {
14249 struct dwarf2_cu *spec_cu = cu;
14250 struct die_info *spec_die = die_specification (die, &spec_cu);
14251
14252 while (spec_die)
14253 {
14254 child_die = spec_die->child;
14255 while (child_die && child_die->tag)
14256 {
14257 if (child_die->tag == DW_TAG_imported_module)
14258 process_die (child_die, spec_cu);
14259 child_die = sibling_die (child_die);
14260 }
14261
14262 /* In some cases, GCC generates specification DIEs that
14263 themselves contain DW_AT_specification attributes. */
14264 spec_die = die_specification (spec_die, &spec_cu);
14265 }
14266 }
14267
14268 newobj = pop_context ();
14269 /* Make a block for the local symbols within. */
14270 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
14271 newobj->static_link, lowpc, highpc);
14272
14273 /* For C++, set the block's scope. */
14274 if ((cu->language == language_cplus
14275 || cu->language == language_fortran
14276 || cu->language == language_d
14277 || cu->language == language_rust)
14278 && cu->processing_has_namespace_info)
14279 block_set_scope (block, determine_prefix (die, cu),
14280 &objfile->objfile_obstack);
14281
14282 /* If we have address ranges, record them. */
14283 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14284
14285 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
14286
14287 /* Attach template arguments to function. */
14288 if (!template_args.empty ())
14289 {
14290 gdb_assert (templ_func != NULL);
14291
14292 templ_func->n_template_arguments = template_args.size ();
14293 templ_func->template_arguments
14294 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
14295 templ_func->n_template_arguments);
14296 memcpy (templ_func->template_arguments,
14297 template_args.data (),
14298 (templ_func->n_template_arguments * sizeof (struct symbol *)));
14299 }
14300
14301 /* In C++, we can have functions nested inside functions (e.g., when
14302 a function declares a class that has methods). This means that
14303 when we finish processing a function scope, we may need to go
14304 back to building a containing block's symbol lists. */
14305 local_symbols = newobj->locals;
14306 local_using_directives = newobj->local_using_directives;
14307
14308 /* If we've finished processing a top-level function, subsequent
14309 symbols go in the file symbol list. */
14310 if (outermost_context_p ())
14311 cu->list_in_scope = &file_symbols;
14312 }
14313
14314 /* Process all the DIES contained within a lexical block scope. Start
14315 a new scope, process the dies, and then close the scope. */
14316
14317 static void
14318 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
14319 {
14320 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14321 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14322 struct context_stack *newobj;
14323 CORE_ADDR lowpc, highpc;
14324 struct die_info *child_die;
14325 CORE_ADDR baseaddr;
14326
14327 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14328
14329 /* Ignore blocks with missing or invalid low and high pc attributes. */
14330 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
14331 as multiple lexical blocks? Handling children in a sane way would
14332 be nasty. Might be easier to properly extend generic blocks to
14333 describe ranges. */
14334 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
14335 {
14336 case PC_BOUNDS_NOT_PRESENT:
14337 /* DW_TAG_lexical_block has no attributes, process its children as if
14338 there was no wrapping by that DW_TAG_lexical_block.
14339 GCC does no longer produces such DWARF since GCC r224161. */
14340 for (child_die = die->child;
14341 child_die != NULL && child_die->tag;
14342 child_die = sibling_die (child_die))
14343 process_die (child_die, cu);
14344 return;
14345 case PC_BOUNDS_INVALID:
14346 return;
14347 }
14348 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14349 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
14350
14351 push_context (0, lowpc);
14352 if (die->child != NULL)
14353 {
14354 child_die = die->child;
14355 while (child_die && child_die->tag)
14356 {
14357 process_die (child_die, cu);
14358 child_die = sibling_die (child_die);
14359 }
14360 }
14361 inherit_abstract_dies (die, cu);
14362 newobj = pop_context ();
14363
14364 if (local_symbols != NULL || local_using_directives != NULL)
14365 {
14366 struct block *block
14367 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
14368 newobj->start_addr, highpc);
14369
14370 /* Note that recording ranges after traversing children, as we
14371 do here, means that recording a parent's ranges entails
14372 walking across all its children's ranges as they appear in
14373 the address map, which is quadratic behavior.
14374
14375 It would be nicer to record the parent's ranges before
14376 traversing its children, simply overriding whatever you find
14377 there. But since we don't even decide whether to create a
14378 block until after we've traversed its children, that's hard
14379 to do. */
14380 dwarf2_record_block_ranges (die, block, baseaddr, cu);
14381 }
14382 local_symbols = newobj->locals;
14383 local_using_directives = newobj->local_using_directives;
14384 }
14385
14386 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14387
14388 static void
14389 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14390 {
14391 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14392 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14393 CORE_ADDR pc, baseaddr;
14394 struct attribute *attr;
14395 struct call_site *call_site, call_site_local;
14396 void **slot;
14397 int nparams;
14398 struct die_info *child_die;
14399
14400 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14401
14402 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14403 if (attr == NULL)
14404 {
14405 /* This was a pre-DWARF-5 GNU extension alias
14406 for DW_AT_call_return_pc. */
14407 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14408 }
14409 if (!attr)
14410 {
14411 complaint (&symfile_complaints,
14412 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
14413 "DIE %s [in module %s]"),
14414 sect_offset_str (die->sect_off), objfile_name (objfile));
14415 return;
14416 }
14417 pc = attr_value_as_address (attr) + baseaddr;
14418 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14419
14420 if (cu->call_site_htab == NULL)
14421 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14422 NULL, &objfile->objfile_obstack,
14423 hashtab_obstack_allocate, NULL);
14424 call_site_local.pc = pc;
14425 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14426 if (*slot != NULL)
14427 {
14428 complaint (&symfile_complaints,
14429 _("Duplicate PC %s for DW_TAG_call_site "
14430 "DIE %s [in module %s]"),
14431 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14432 objfile_name (objfile));
14433 return;
14434 }
14435
14436 /* Count parameters at the caller. */
14437
14438 nparams = 0;
14439 for (child_die = die->child; child_die && child_die->tag;
14440 child_die = sibling_die (child_die))
14441 {
14442 if (child_die->tag != DW_TAG_call_site_parameter
14443 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14444 {
14445 complaint (&symfile_complaints,
14446 _("Tag %d is not DW_TAG_call_site_parameter in "
14447 "DW_TAG_call_site child DIE %s [in module %s]"),
14448 child_die->tag, sect_offset_str (child_die->sect_off),
14449 objfile_name (objfile));
14450 continue;
14451 }
14452
14453 nparams++;
14454 }
14455
14456 call_site
14457 = ((struct call_site *)
14458 obstack_alloc (&objfile->objfile_obstack,
14459 sizeof (*call_site)
14460 + (sizeof (*call_site->parameter) * (nparams - 1))));
14461 *slot = call_site;
14462 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14463 call_site->pc = pc;
14464
14465 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14466 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14467 {
14468 struct die_info *func_die;
14469
14470 /* Skip also over DW_TAG_inlined_subroutine. */
14471 for (func_die = die->parent;
14472 func_die && func_die->tag != DW_TAG_subprogram
14473 && func_die->tag != DW_TAG_subroutine_type;
14474 func_die = func_die->parent);
14475
14476 /* DW_AT_call_all_calls is a superset
14477 of DW_AT_call_all_tail_calls. */
14478 if (func_die
14479 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14480 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14481 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14482 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14483 {
14484 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14485 not complete. But keep CALL_SITE for look ups via call_site_htab,
14486 both the initial caller containing the real return address PC and
14487 the final callee containing the current PC of a chain of tail
14488 calls do not need to have the tail call list complete. But any
14489 function candidate for a virtual tail call frame searched via
14490 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14491 determined unambiguously. */
14492 }
14493 else
14494 {
14495 struct type *func_type = NULL;
14496
14497 if (func_die)
14498 func_type = get_die_type (func_die, cu);
14499 if (func_type != NULL)
14500 {
14501 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14502
14503 /* Enlist this call site to the function. */
14504 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14505 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14506 }
14507 else
14508 complaint (&symfile_complaints,
14509 _("Cannot find function owning DW_TAG_call_site "
14510 "DIE %s [in module %s]"),
14511 sect_offset_str (die->sect_off), objfile_name (objfile));
14512 }
14513 }
14514
14515 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14516 if (attr == NULL)
14517 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14518 if (attr == NULL)
14519 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14520 if (attr == NULL)
14521 {
14522 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14523 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14524 }
14525 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14526 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14527 /* Keep NULL DWARF_BLOCK. */;
14528 else if (attr_form_is_block (attr))
14529 {
14530 struct dwarf2_locexpr_baton *dlbaton;
14531
14532 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14533 dlbaton->data = DW_BLOCK (attr)->data;
14534 dlbaton->size = DW_BLOCK (attr)->size;
14535 dlbaton->per_cu = cu->per_cu;
14536
14537 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14538 }
14539 else if (attr_form_is_ref (attr))
14540 {
14541 struct dwarf2_cu *target_cu = cu;
14542 struct die_info *target_die;
14543
14544 target_die = follow_die_ref (die, attr, &target_cu);
14545 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14546 if (die_is_declaration (target_die, target_cu))
14547 {
14548 const char *target_physname;
14549
14550 /* Prefer the mangled name; otherwise compute the demangled one. */
14551 target_physname = dw2_linkage_name (target_die, target_cu);
14552 if (target_physname == NULL)
14553 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14554 if (target_physname == NULL)
14555 complaint (&symfile_complaints,
14556 _("DW_AT_call_target target DIE has invalid "
14557 "physname, for referencing DIE %s [in module %s]"),
14558 sect_offset_str (die->sect_off), objfile_name (objfile));
14559 else
14560 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14561 }
14562 else
14563 {
14564 CORE_ADDR lowpc;
14565
14566 /* DW_AT_entry_pc should be preferred. */
14567 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14568 <= PC_BOUNDS_INVALID)
14569 complaint (&symfile_complaints,
14570 _("DW_AT_call_target target DIE has invalid "
14571 "low pc, for referencing DIE %s [in module %s]"),
14572 sect_offset_str (die->sect_off), objfile_name (objfile));
14573 else
14574 {
14575 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14576 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14577 }
14578 }
14579 }
14580 else
14581 complaint (&symfile_complaints,
14582 _("DW_TAG_call_site DW_AT_call_target is neither "
14583 "block nor reference, for DIE %s [in module %s]"),
14584 sect_offset_str (die->sect_off), objfile_name (objfile));
14585
14586 call_site->per_cu = cu->per_cu;
14587
14588 for (child_die = die->child;
14589 child_die && child_die->tag;
14590 child_die = sibling_die (child_die))
14591 {
14592 struct call_site_parameter *parameter;
14593 struct attribute *loc, *origin;
14594
14595 if (child_die->tag != DW_TAG_call_site_parameter
14596 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14597 {
14598 /* Already printed the complaint above. */
14599 continue;
14600 }
14601
14602 gdb_assert (call_site->parameter_count < nparams);
14603 parameter = &call_site->parameter[call_site->parameter_count];
14604
14605 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14606 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14607 register is contained in DW_AT_call_value. */
14608
14609 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14610 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14611 if (origin == NULL)
14612 {
14613 /* This was a pre-DWARF-5 GNU extension alias
14614 for DW_AT_call_parameter. */
14615 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14616 }
14617 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14618 {
14619 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14620
14621 sect_offset sect_off
14622 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14623 if (!offset_in_cu_p (&cu->header, sect_off))
14624 {
14625 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14626 binding can be done only inside one CU. Such referenced DIE
14627 therefore cannot be even moved to DW_TAG_partial_unit. */
14628 complaint (&symfile_complaints,
14629 _("DW_AT_call_parameter offset is not in CU for "
14630 "DW_TAG_call_site child DIE %s [in module %s]"),
14631 sect_offset_str (child_die->sect_off),
14632 objfile_name (objfile));
14633 continue;
14634 }
14635 parameter->u.param_cu_off
14636 = (cu_offset) (sect_off - cu->header.sect_off);
14637 }
14638 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14639 {
14640 complaint (&symfile_complaints,
14641 _("No DW_FORM_block* DW_AT_location for "
14642 "DW_TAG_call_site child DIE %s [in module %s]"),
14643 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14644 continue;
14645 }
14646 else
14647 {
14648 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14649 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14650 if (parameter->u.dwarf_reg != -1)
14651 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14652 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14653 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14654 &parameter->u.fb_offset))
14655 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14656 else
14657 {
14658 complaint (&symfile_complaints,
14659 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14660 "for DW_FORM_block* DW_AT_location is supported for "
14661 "DW_TAG_call_site child DIE %s "
14662 "[in module %s]"),
14663 sect_offset_str (child_die->sect_off),
14664 objfile_name (objfile));
14665 continue;
14666 }
14667 }
14668
14669 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14670 if (attr == NULL)
14671 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14672 if (!attr_form_is_block (attr))
14673 {
14674 complaint (&symfile_complaints,
14675 _("No DW_FORM_block* DW_AT_call_value for "
14676 "DW_TAG_call_site child DIE %s [in module %s]"),
14677 sect_offset_str (child_die->sect_off),
14678 objfile_name (objfile));
14679 continue;
14680 }
14681 parameter->value = DW_BLOCK (attr)->data;
14682 parameter->value_size = DW_BLOCK (attr)->size;
14683
14684 /* Parameters are not pre-cleared by memset above. */
14685 parameter->data_value = NULL;
14686 parameter->data_value_size = 0;
14687 call_site->parameter_count++;
14688
14689 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14690 if (attr == NULL)
14691 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14692 if (attr)
14693 {
14694 if (!attr_form_is_block (attr))
14695 complaint (&symfile_complaints,
14696 _("No DW_FORM_block* DW_AT_call_data_value for "
14697 "DW_TAG_call_site child DIE %s [in module %s]"),
14698 sect_offset_str (child_die->sect_off),
14699 objfile_name (objfile));
14700 else
14701 {
14702 parameter->data_value = DW_BLOCK (attr)->data;
14703 parameter->data_value_size = DW_BLOCK (attr)->size;
14704 }
14705 }
14706 }
14707 }
14708
14709 /* Helper function for read_variable. If DIE represents a virtual
14710 table, then return the type of the concrete object that is
14711 associated with the virtual table. Otherwise, return NULL. */
14712
14713 static struct type *
14714 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14715 {
14716 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14717 if (attr == NULL)
14718 return NULL;
14719
14720 /* Find the type DIE. */
14721 struct die_info *type_die = NULL;
14722 struct dwarf2_cu *type_cu = cu;
14723
14724 if (attr_form_is_ref (attr))
14725 type_die = follow_die_ref (die, attr, &type_cu);
14726 if (type_die == NULL)
14727 return NULL;
14728
14729 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14730 return NULL;
14731 return die_containing_type (type_die, type_cu);
14732 }
14733
14734 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14735
14736 static void
14737 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14738 {
14739 struct rust_vtable_symbol *storage = NULL;
14740
14741 if (cu->language == language_rust)
14742 {
14743 struct type *containing_type = rust_containing_type (die, cu);
14744
14745 if (containing_type != NULL)
14746 {
14747 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14748
14749 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14750 struct rust_vtable_symbol);
14751 initialize_objfile_symbol (storage);
14752 storage->concrete_type = containing_type;
14753 storage->subclass = SYMBOL_RUST_VTABLE;
14754 }
14755 }
14756
14757 new_symbol (die, NULL, cu, storage);
14758 }
14759
14760 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14761 reading .debug_rnglists.
14762 Callback's type should be:
14763 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14764 Return true if the attributes are present and valid, otherwise,
14765 return false. */
14766
14767 template <typename Callback>
14768 static bool
14769 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14770 Callback &&callback)
14771 {
14772 struct dwarf2_per_objfile *dwarf2_per_objfile
14773 = cu->per_cu->dwarf2_per_objfile;
14774 struct objfile *objfile = dwarf2_per_objfile->objfile;
14775 bfd *obfd = objfile->obfd;
14776 /* Base address selection entry. */
14777 CORE_ADDR base;
14778 int found_base;
14779 const gdb_byte *buffer;
14780 CORE_ADDR baseaddr;
14781 bool overflow = false;
14782
14783 found_base = cu->base_known;
14784 base = cu->base_address;
14785
14786 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14787 if (offset >= dwarf2_per_objfile->rnglists.size)
14788 {
14789 complaint (&symfile_complaints,
14790 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14791 offset);
14792 return false;
14793 }
14794 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14795
14796 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14797
14798 while (1)
14799 {
14800 /* Initialize it due to a false compiler warning. */
14801 CORE_ADDR range_beginning = 0, range_end = 0;
14802 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14803 + dwarf2_per_objfile->rnglists.size);
14804 unsigned int bytes_read;
14805
14806 if (buffer == buf_end)
14807 {
14808 overflow = true;
14809 break;
14810 }
14811 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14812 switch (rlet)
14813 {
14814 case DW_RLE_end_of_list:
14815 break;
14816 case DW_RLE_base_address:
14817 if (buffer + cu->header.addr_size > buf_end)
14818 {
14819 overflow = true;
14820 break;
14821 }
14822 base = read_address (obfd, buffer, cu, &bytes_read);
14823 found_base = 1;
14824 buffer += bytes_read;
14825 break;
14826 case DW_RLE_start_length:
14827 if (buffer + cu->header.addr_size > buf_end)
14828 {
14829 overflow = true;
14830 break;
14831 }
14832 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14833 buffer += bytes_read;
14834 range_end = (range_beginning
14835 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14836 buffer += bytes_read;
14837 if (buffer > buf_end)
14838 {
14839 overflow = true;
14840 break;
14841 }
14842 break;
14843 case DW_RLE_offset_pair:
14844 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14845 buffer += bytes_read;
14846 if (buffer > buf_end)
14847 {
14848 overflow = true;
14849 break;
14850 }
14851 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14852 buffer += bytes_read;
14853 if (buffer > buf_end)
14854 {
14855 overflow = true;
14856 break;
14857 }
14858 break;
14859 case DW_RLE_start_end:
14860 if (buffer + 2 * cu->header.addr_size > buf_end)
14861 {
14862 overflow = true;
14863 break;
14864 }
14865 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14866 buffer += bytes_read;
14867 range_end = read_address (obfd, buffer, cu, &bytes_read);
14868 buffer += bytes_read;
14869 break;
14870 default:
14871 complaint (&symfile_complaints,
14872 _("Invalid .debug_rnglists data (no base address)"));
14873 return false;
14874 }
14875 if (rlet == DW_RLE_end_of_list || overflow)
14876 break;
14877 if (rlet == DW_RLE_base_address)
14878 continue;
14879
14880 if (!found_base)
14881 {
14882 /* We have no valid base address for the ranges
14883 data. */
14884 complaint (&symfile_complaints,
14885 _("Invalid .debug_rnglists data (no base address)"));
14886 return false;
14887 }
14888
14889 if (range_beginning > range_end)
14890 {
14891 /* Inverted range entries are invalid. */
14892 complaint (&symfile_complaints,
14893 _("Invalid .debug_rnglists data (inverted range)"));
14894 return false;
14895 }
14896
14897 /* Empty range entries have no effect. */
14898 if (range_beginning == range_end)
14899 continue;
14900
14901 range_beginning += base;
14902 range_end += base;
14903
14904 /* A not-uncommon case of bad debug info.
14905 Don't pollute the addrmap with bad data. */
14906 if (range_beginning + baseaddr == 0
14907 && !dwarf2_per_objfile->has_section_at_zero)
14908 {
14909 complaint (&symfile_complaints,
14910 _(".debug_rnglists entry has start address of zero"
14911 " [in module %s]"), objfile_name (objfile));
14912 continue;
14913 }
14914
14915 callback (range_beginning, range_end);
14916 }
14917
14918 if (overflow)
14919 {
14920 complaint (&symfile_complaints,
14921 _("Offset %d is not terminated "
14922 "for DW_AT_ranges attribute"),
14923 offset);
14924 return false;
14925 }
14926
14927 return true;
14928 }
14929
14930 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14931 Callback's type should be:
14932 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14933 Return 1 if the attributes are present and valid, otherwise, return 0. */
14934
14935 template <typename Callback>
14936 static int
14937 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14938 Callback &&callback)
14939 {
14940 struct dwarf2_per_objfile *dwarf2_per_objfile
14941 = cu->per_cu->dwarf2_per_objfile;
14942 struct objfile *objfile = dwarf2_per_objfile->objfile;
14943 struct comp_unit_head *cu_header = &cu->header;
14944 bfd *obfd = objfile->obfd;
14945 unsigned int addr_size = cu_header->addr_size;
14946 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14947 /* Base address selection entry. */
14948 CORE_ADDR base;
14949 int found_base;
14950 unsigned int dummy;
14951 const gdb_byte *buffer;
14952 CORE_ADDR baseaddr;
14953
14954 if (cu_header->version >= 5)
14955 return dwarf2_rnglists_process (offset, cu, callback);
14956
14957 found_base = cu->base_known;
14958 base = cu->base_address;
14959
14960 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14961 if (offset >= dwarf2_per_objfile->ranges.size)
14962 {
14963 complaint (&symfile_complaints,
14964 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14965 offset);
14966 return 0;
14967 }
14968 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14969
14970 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14971
14972 while (1)
14973 {
14974 CORE_ADDR range_beginning, range_end;
14975
14976 range_beginning = read_address (obfd, buffer, cu, &dummy);
14977 buffer += addr_size;
14978 range_end = read_address (obfd, buffer, cu, &dummy);
14979 buffer += addr_size;
14980 offset += 2 * addr_size;
14981
14982 /* An end of list marker is a pair of zero addresses. */
14983 if (range_beginning == 0 && range_end == 0)
14984 /* Found the end of list entry. */
14985 break;
14986
14987 /* Each base address selection entry is a pair of 2 values.
14988 The first is the largest possible address, the second is
14989 the base address. Check for a base address here. */
14990 if ((range_beginning & mask) == mask)
14991 {
14992 /* If we found the largest possible address, then we already
14993 have the base address in range_end. */
14994 base = range_end;
14995 found_base = 1;
14996 continue;
14997 }
14998
14999 if (!found_base)
15000 {
15001 /* We have no valid base address for the ranges
15002 data. */
15003 complaint (&symfile_complaints,
15004 _("Invalid .debug_ranges data (no base address)"));
15005 return 0;
15006 }
15007
15008 if (range_beginning > range_end)
15009 {
15010 /* Inverted range entries are invalid. */
15011 complaint (&symfile_complaints,
15012 _("Invalid .debug_ranges data (inverted range)"));
15013 return 0;
15014 }
15015
15016 /* Empty range entries have no effect. */
15017 if (range_beginning == range_end)
15018 continue;
15019
15020 range_beginning += base;
15021 range_end += base;
15022
15023 /* A not-uncommon case of bad debug info.
15024 Don't pollute the addrmap with bad data. */
15025 if (range_beginning + baseaddr == 0
15026 && !dwarf2_per_objfile->has_section_at_zero)
15027 {
15028 complaint (&symfile_complaints,
15029 _(".debug_ranges entry has start address of zero"
15030 " [in module %s]"), objfile_name (objfile));
15031 continue;
15032 }
15033
15034 callback (range_beginning, range_end);
15035 }
15036
15037 return 1;
15038 }
15039
15040 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
15041 Return 1 if the attributes are present and valid, otherwise, return 0.
15042 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
15043
15044 static int
15045 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
15046 CORE_ADDR *high_return, struct dwarf2_cu *cu,
15047 struct partial_symtab *ranges_pst)
15048 {
15049 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15050 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15051 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
15052 SECT_OFF_TEXT (objfile));
15053 int low_set = 0;
15054 CORE_ADDR low = 0;
15055 CORE_ADDR high = 0;
15056 int retval;
15057
15058 retval = dwarf2_ranges_process (offset, cu,
15059 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
15060 {
15061 if (ranges_pst != NULL)
15062 {
15063 CORE_ADDR lowpc;
15064 CORE_ADDR highpc;
15065
15066 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15067 range_beginning + baseaddr);
15068 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
15069 range_end + baseaddr);
15070 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
15071 ranges_pst);
15072 }
15073
15074 /* FIXME: This is recording everything as a low-high
15075 segment of consecutive addresses. We should have a
15076 data structure for discontiguous block ranges
15077 instead. */
15078 if (! low_set)
15079 {
15080 low = range_beginning;
15081 high = range_end;
15082 low_set = 1;
15083 }
15084 else
15085 {
15086 if (range_beginning < low)
15087 low = range_beginning;
15088 if (range_end > high)
15089 high = range_end;
15090 }
15091 });
15092 if (!retval)
15093 return 0;
15094
15095 if (! low_set)
15096 /* If the first entry is an end-of-list marker, the range
15097 describes an empty scope, i.e. no instructions. */
15098 return 0;
15099
15100 if (low_return)
15101 *low_return = low;
15102 if (high_return)
15103 *high_return = high;
15104 return 1;
15105 }
15106
15107 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
15108 definition for the return value. *LOWPC and *HIGHPC are set iff
15109 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
15110
15111 static enum pc_bounds_kind
15112 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
15113 CORE_ADDR *highpc, struct dwarf2_cu *cu,
15114 struct partial_symtab *pst)
15115 {
15116 struct dwarf2_per_objfile *dwarf2_per_objfile
15117 = cu->per_cu->dwarf2_per_objfile;
15118 struct attribute *attr;
15119 struct attribute *attr_high;
15120 CORE_ADDR low = 0;
15121 CORE_ADDR high = 0;
15122 enum pc_bounds_kind ret;
15123
15124 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15125 if (attr_high)
15126 {
15127 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15128 if (attr)
15129 {
15130 low = attr_value_as_address (attr);
15131 high = attr_value_as_address (attr_high);
15132 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15133 high += low;
15134 }
15135 else
15136 /* Found high w/o low attribute. */
15137 return PC_BOUNDS_INVALID;
15138
15139 /* Found consecutive range of addresses. */
15140 ret = PC_BOUNDS_HIGH_LOW;
15141 }
15142 else
15143 {
15144 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15145 if (attr != NULL)
15146 {
15147 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15148 We take advantage of the fact that DW_AT_ranges does not appear
15149 in DW_TAG_compile_unit of DWO files. */
15150 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15151 unsigned int ranges_offset = (DW_UNSND (attr)
15152 + (need_ranges_base
15153 ? cu->ranges_base
15154 : 0));
15155
15156 /* Value of the DW_AT_ranges attribute is the offset in the
15157 .debug_ranges section. */
15158 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
15159 return PC_BOUNDS_INVALID;
15160 /* Found discontinuous range of addresses. */
15161 ret = PC_BOUNDS_RANGES;
15162 }
15163 else
15164 return PC_BOUNDS_NOT_PRESENT;
15165 }
15166
15167 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
15168 if (high <= low)
15169 return PC_BOUNDS_INVALID;
15170
15171 /* When using the GNU linker, .gnu.linkonce. sections are used to
15172 eliminate duplicate copies of functions and vtables and such.
15173 The linker will arbitrarily choose one and discard the others.
15174 The AT_*_pc values for such functions refer to local labels in
15175 these sections. If the section from that file was discarded, the
15176 labels are not in the output, so the relocs get a value of 0.
15177 If this is a discarded function, mark the pc bounds as invalid,
15178 so that GDB will ignore it. */
15179 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
15180 return PC_BOUNDS_INVALID;
15181
15182 *lowpc = low;
15183 if (highpc)
15184 *highpc = high;
15185 return ret;
15186 }
15187
15188 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
15189 its low and high PC addresses. Do nothing if these addresses could not
15190 be determined. Otherwise, set LOWPC to the low address if it is smaller,
15191 and HIGHPC to the high address if greater than HIGHPC. */
15192
15193 static void
15194 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
15195 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15196 struct dwarf2_cu *cu)
15197 {
15198 CORE_ADDR low, high;
15199 struct die_info *child = die->child;
15200
15201 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
15202 {
15203 *lowpc = std::min (*lowpc, low);
15204 *highpc = std::max (*highpc, high);
15205 }
15206
15207 /* If the language does not allow nested subprograms (either inside
15208 subprograms or lexical blocks), we're done. */
15209 if (cu->language != language_ada)
15210 return;
15211
15212 /* Check all the children of the given DIE. If it contains nested
15213 subprograms, then check their pc bounds. Likewise, we need to
15214 check lexical blocks as well, as they may also contain subprogram
15215 definitions. */
15216 while (child && child->tag)
15217 {
15218 if (child->tag == DW_TAG_subprogram
15219 || child->tag == DW_TAG_lexical_block)
15220 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
15221 child = sibling_die (child);
15222 }
15223 }
15224
15225 /* Get the low and high pc's represented by the scope DIE, and store
15226 them in *LOWPC and *HIGHPC. If the correct values can't be
15227 determined, set *LOWPC to -1 and *HIGHPC to 0. */
15228
15229 static void
15230 get_scope_pc_bounds (struct die_info *die,
15231 CORE_ADDR *lowpc, CORE_ADDR *highpc,
15232 struct dwarf2_cu *cu)
15233 {
15234 CORE_ADDR best_low = (CORE_ADDR) -1;
15235 CORE_ADDR best_high = (CORE_ADDR) 0;
15236 CORE_ADDR current_low, current_high;
15237
15238 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
15239 >= PC_BOUNDS_RANGES)
15240 {
15241 best_low = current_low;
15242 best_high = current_high;
15243 }
15244 else
15245 {
15246 struct die_info *child = die->child;
15247
15248 while (child && child->tag)
15249 {
15250 switch (child->tag) {
15251 case DW_TAG_subprogram:
15252 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
15253 break;
15254 case DW_TAG_namespace:
15255 case DW_TAG_module:
15256 /* FIXME: carlton/2004-01-16: Should we do this for
15257 DW_TAG_class_type/DW_TAG_structure_type, too? I think
15258 that current GCC's always emit the DIEs corresponding
15259 to definitions of methods of classes as children of a
15260 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
15261 the DIEs giving the declarations, which could be
15262 anywhere). But I don't see any reason why the
15263 standards says that they have to be there. */
15264 get_scope_pc_bounds (child, &current_low, &current_high, cu);
15265
15266 if (current_low != ((CORE_ADDR) -1))
15267 {
15268 best_low = std::min (best_low, current_low);
15269 best_high = std::max (best_high, current_high);
15270 }
15271 break;
15272 default:
15273 /* Ignore. */
15274 break;
15275 }
15276
15277 child = sibling_die (child);
15278 }
15279 }
15280
15281 *lowpc = best_low;
15282 *highpc = best_high;
15283 }
15284
15285 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
15286 in DIE. */
15287
15288 static void
15289 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
15290 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
15291 {
15292 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15293 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15294 struct attribute *attr;
15295 struct attribute *attr_high;
15296
15297 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
15298 if (attr_high)
15299 {
15300 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15301 if (attr)
15302 {
15303 CORE_ADDR low = attr_value_as_address (attr);
15304 CORE_ADDR high = attr_value_as_address (attr_high);
15305
15306 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
15307 high += low;
15308
15309 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
15310 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
15311 record_block_range (block, low, high - 1);
15312 }
15313 }
15314
15315 attr = dwarf2_attr (die, DW_AT_ranges, cu);
15316 if (attr)
15317 {
15318 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
15319 We take advantage of the fact that DW_AT_ranges does not appear
15320 in DW_TAG_compile_unit of DWO files. */
15321 int need_ranges_base = die->tag != DW_TAG_compile_unit;
15322
15323 /* The value of the DW_AT_ranges attribute is the offset of the
15324 address range list in the .debug_ranges section. */
15325 unsigned long offset = (DW_UNSND (attr)
15326 + (need_ranges_base ? cu->ranges_base : 0));
15327 const gdb_byte *buffer;
15328
15329 /* For some target architectures, but not others, the
15330 read_address function sign-extends the addresses it returns.
15331 To recognize base address selection entries, we need a
15332 mask. */
15333 unsigned int addr_size = cu->header.addr_size;
15334 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
15335
15336 /* The base address, to which the next pair is relative. Note
15337 that this 'base' is a DWARF concept: most entries in a range
15338 list are relative, to reduce the number of relocs against the
15339 debugging information. This is separate from this function's
15340 'baseaddr' argument, which GDB uses to relocate debugging
15341 information from a shared library based on the address at
15342 which the library was loaded. */
15343 CORE_ADDR base = cu->base_address;
15344 int base_known = cu->base_known;
15345
15346 dwarf2_ranges_process (offset, cu,
15347 [&] (CORE_ADDR start, CORE_ADDR end)
15348 {
15349 start += baseaddr;
15350 end += baseaddr;
15351 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
15352 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
15353 record_block_range (block, start, end - 1);
15354 });
15355 }
15356 }
15357
15358 /* Check whether the producer field indicates either of GCC < 4.6, or the
15359 Intel C/C++ compiler, and cache the result in CU. */
15360
15361 static void
15362 check_producer (struct dwarf2_cu *cu)
15363 {
15364 int major, minor;
15365
15366 if (cu->producer == NULL)
15367 {
15368 /* For unknown compilers expect their behavior is DWARF version
15369 compliant.
15370
15371 GCC started to support .debug_types sections by -gdwarf-4 since
15372 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
15373 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
15374 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
15375 interpreted incorrectly by GDB now - GCC PR debug/48229. */
15376 }
15377 else if (producer_is_gcc (cu->producer, &major, &minor))
15378 {
15379 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
15380 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
15381 }
15382 else if (producer_is_icc (cu->producer, &major, &minor))
15383 cu->producer_is_icc_lt_14 = major < 14;
15384 else
15385 {
15386 /* For other non-GCC compilers, expect their behavior is DWARF version
15387 compliant. */
15388 }
15389
15390 cu->checked_producer = 1;
15391 }
15392
15393 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
15394 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
15395 during 4.6.0 experimental. */
15396
15397 static int
15398 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15399 {
15400 if (!cu->checked_producer)
15401 check_producer (cu);
15402
15403 return cu->producer_is_gxx_lt_4_6;
15404 }
15405
15406 /* Return the default accessibility type if it is not overriden by
15407 DW_AT_accessibility. */
15408
15409 static enum dwarf_access_attribute
15410 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15411 {
15412 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15413 {
15414 /* The default DWARF 2 accessibility for members is public, the default
15415 accessibility for inheritance is private. */
15416
15417 if (die->tag != DW_TAG_inheritance)
15418 return DW_ACCESS_public;
15419 else
15420 return DW_ACCESS_private;
15421 }
15422 else
15423 {
15424 /* DWARF 3+ defines the default accessibility a different way. The same
15425 rules apply now for DW_TAG_inheritance as for the members and it only
15426 depends on the container kind. */
15427
15428 if (die->parent->tag == DW_TAG_class_type)
15429 return DW_ACCESS_private;
15430 else
15431 return DW_ACCESS_public;
15432 }
15433 }
15434
15435 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15436 offset. If the attribute was not found return 0, otherwise return
15437 1. If it was found but could not properly be handled, set *OFFSET
15438 to 0. */
15439
15440 static int
15441 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15442 LONGEST *offset)
15443 {
15444 struct attribute *attr;
15445
15446 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15447 if (attr != NULL)
15448 {
15449 *offset = 0;
15450
15451 /* Note that we do not check for a section offset first here.
15452 This is because DW_AT_data_member_location is new in DWARF 4,
15453 so if we see it, we can assume that a constant form is really
15454 a constant and not a section offset. */
15455 if (attr_form_is_constant (attr))
15456 *offset = dwarf2_get_attr_constant_value (attr, 0);
15457 else if (attr_form_is_section_offset (attr))
15458 dwarf2_complex_location_expr_complaint ();
15459 else if (attr_form_is_block (attr))
15460 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15461 else
15462 dwarf2_complex_location_expr_complaint ();
15463
15464 return 1;
15465 }
15466
15467 return 0;
15468 }
15469
15470 /* Add an aggregate field to the field list. */
15471
15472 static void
15473 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15474 struct dwarf2_cu *cu)
15475 {
15476 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15477 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15478 struct nextfield *new_field;
15479 struct attribute *attr;
15480 struct field *fp;
15481 const char *fieldname = "";
15482
15483 /* Allocate a new field list entry and link it in. */
15484 new_field = XNEW (struct nextfield);
15485 make_cleanup (xfree, new_field);
15486 memset (new_field, 0, sizeof (struct nextfield));
15487
15488 if (die->tag == DW_TAG_inheritance)
15489 {
15490 new_field->next = fip->baseclasses;
15491 fip->baseclasses = new_field;
15492 }
15493 else
15494 {
15495 new_field->next = fip->fields;
15496 fip->fields = new_field;
15497 }
15498 fip->nfields++;
15499
15500 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15501 if (attr)
15502 new_field->accessibility = DW_UNSND (attr);
15503 else
15504 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15505 if (new_field->accessibility != DW_ACCESS_public)
15506 fip->non_public_fields = 1;
15507
15508 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15509 if (attr)
15510 new_field->virtuality = DW_UNSND (attr);
15511 else
15512 new_field->virtuality = DW_VIRTUALITY_none;
15513
15514 fp = &new_field->field;
15515
15516 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15517 {
15518 LONGEST offset;
15519
15520 /* Data member other than a C++ static data member. */
15521
15522 /* Get type of field. */
15523 fp->type = die_type (die, cu);
15524
15525 SET_FIELD_BITPOS (*fp, 0);
15526
15527 /* Get bit size of field (zero if none). */
15528 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15529 if (attr)
15530 {
15531 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15532 }
15533 else
15534 {
15535 FIELD_BITSIZE (*fp) = 0;
15536 }
15537
15538 /* Get bit offset of field. */
15539 if (handle_data_member_location (die, cu, &offset))
15540 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15541 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15542 if (attr)
15543 {
15544 if (gdbarch_bits_big_endian (gdbarch))
15545 {
15546 /* For big endian bits, the DW_AT_bit_offset gives the
15547 additional bit offset from the MSB of the containing
15548 anonymous object to the MSB of the field. We don't
15549 have to do anything special since we don't need to
15550 know the size of the anonymous object. */
15551 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15552 }
15553 else
15554 {
15555 /* For little endian bits, compute the bit offset to the
15556 MSB of the anonymous object, subtract off the number of
15557 bits from the MSB of the field to the MSB of the
15558 object, and then subtract off the number of bits of
15559 the field itself. The result is the bit offset of
15560 the LSB of the field. */
15561 int anonymous_size;
15562 int bit_offset = DW_UNSND (attr);
15563
15564 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15565 if (attr)
15566 {
15567 /* The size of the anonymous object containing
15568 the bit field is explicit, so use the
15569 indicated size (in bytes). */
15570 anonymous_size = DW_UNSND (attr);
15571 }
15572 else
15573 {
15574 /* The size of the anonymous object containing
15575 the bit field must be inferred from the type
15576 attribute of the data member containing the
15577 bit field. */
15578 anonymous_size = TYPE_LENGTH (fp->type);
15579 }
15580 SET_FIELD_BITPOS (*fp,
15581 (FIELD_BITPOS (*fp)
15582 + anonymous_size * bits_per_byte
15583 - bit_offset - FIELD_BITSIZE (*fp)));
15584 }
15585 }
15586 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15587 if (attr != NULL)
15588 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15589 + dwarf2_get_attr_constant_value (attr, 0)));
15590
15591 /* Get name of field. */
15592 fieldname = dwarf2_name (die, cu);
15593 if (fieldname == NULL)
15594 fieldname = "";
15595
15596 /* The name is already allocated along with this objfile, so we don't
15597 need to duplicate it for the type. */
15598 fp->name = fieldname;
15599
15600 /* Change accessibility for artificial fields (e.g. virtual table
15601 pointer or virtual base class pointer) to private. */
15602 if (dwarf2_attr (die, DW_AT_artificial, cu))
15603 {
15604 FIELD_ARTIFICIAL (*fp) = 1;
15605 new_field->accessibility = DW_ACCESS_private;
15606 fip->non_public_fields = 1;
15607 }
15608 }
15609 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15610 {
15611 /* C++ static member. */
15612
15613 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15614 is a declaration, but all versions of G++ as of this writing
15615 (so through at least 3.2.1) incorrectly generate
15616 DW_TAG_variable tags. */
15617
15618 const char *physname;
15619
15620 /* Get name of field. */
15621 fieldname = dwarf2_name (die, cu);
15622 if (fieldname == NULL)
15623 return;
15624
15625 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15626 if (attr
15627 /* Only create a symbol if this is an external value.
15628 new_symbol checks this and puts the value in the global symbol
15629 table, which we want. If it is not external, new_symbol
15630 will try to put the value in cu->list_in_scope which is wrong. */
15631 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15632 {
15633 /* A static const member, not much different than an enum as far as
15634 we're concerned, except that we can support more types. */
15635 new_symbol (die, NULL, cu);
15636 }
15637
15638 /* Get physical name. */
15639 physname = dwarf2_physname (fieldname, die, cu);
15640
15641 /* The name is already allocated along with this objfile, so we don't
15642 need to duplicate it for the type. */
15643 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15644 FIELD_TYPE (*fp) = die_type (die, cu);
15645 FIELD_NAME (*fp) = fieldname;
15646 }
15647 else if (die->tag == DW_TAG_inheritance)
15648 {
15649 LONGEST offset;
15650
15651 /* C++ base class field. */
15652 if (handle_data_member_location (die, cu, &offset))
15653 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15654 FIELD_BITSIZE (*fp) = 0;
15655 FIELD_TYPE (*fp) = die_type (die, cu);
15656 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15657 fip->nbaseclasses++;
15658 }
15659 else if (die->tag == DW_TAG_variant_part)
15660 {
15661 /* process_structure_scope will treat this DIE as a union. */
15662 process_structure_scope (die, cu);
15663
15664 /* The variant part is relative to the start of the enclosing
15665 structure. */
15666 SET_FIELD_BITPOS (*fp, 0);
15667 fp->type = get_die_type (die, cu);
15668 fp->artificial = 1;
15669 fp->name = "<<variant>>";
15670 }
15671 else
15672 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15673 }
15674
15675 /* Can the type given by DIE define another type? */
15676
15677 static bool
15678 type_can_define_types (const struct die_info *die)
15679 {
15680 switch (die->tag)
15681 {
15682 case DW_TAG_typedef:
15683 case DW_TAG_class_type:
15684 case DW_TAG_structure_type:
15685 case DW_TAG_union_type:
15686 case DW_TAG_enumeration_type:
15687 return true;
15688
15689 default:
15690 return false;
15691 }
15692 }
15693
15694 /* Add a type definition defined in the scope of the FIP's class. */
15695
15696 static void
15697 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15698 struct dwarf2_cu *cu)
15699 {
15700 struct decl_field_list *new_field;
15701 struct decl_field *fp;
15702
15703 /* Allocate a new field list entry and link it in. */
15704 new_field = XCNEW (struct decl_field_list);
15705 make_cleanup (xfree, new_field);
15706
15707 gdb_assert (type_can_define_types (die));
15708
15709 fp = &new_field->field;
15710
15711 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15712 fp->name = dwarf2_name (die, cu);
15713 fp->type = read_type_die (die, cu);
15714
15715 /* Save accessibility. */
15716 enum dwarf_access_attribute accessibility;
15717 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15718 if (attr != NULL)
15719 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15720 else
15721 accessibility = dwarf2_default_access_attribute (die, cu);
15722 switch (accessibility)
15723 {
15724 case DW_ACCESS_public:
15725 /* The assumed value if neither private nor protected. */
15726 break;
15727 case DW_ACCESS_private:
15728 fp->is_private = 1;
15729 break;
15730 case DW_ACCESS_protected:
15731 fp->is_protected = 1;
15732 break;
15733 default:
15734 complaint (&symfile_complaints,
15735 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15736 }
15737
15738 if (die->tag == DW_TAG_typedef)
15739 {
15740 new_field->next = fip->typedef_field_list;
15741 fip->typedef_field_list = new_field;
15742 fip->typedef_field_list_count++;
15743 }
15744 else
15745 {
15746 new_field->next = fip->nested_types_list;
15747 fip->nested_types_list = new_field;
15748 fip->nested_types_list_count++;
15749 }
15750 }
15751
15752 /* Create the vector of fields, and attach it to the type. */
15753
15754 static void
15755 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15756 struct dwarf2_cu *cu)
15757 {
15758 int nfields = fip->nfields;
15759
15760 /* Record the field count, allocate space for the array of fields,
15761 and create blank accessibility bitfields if necessary. */
15762 TYPE_NFIELDS (type) = nfields;
15763 TYPE_FIELDS (type) = (struct field *)
15764 TYPE_ALLOC (type, sizeof (struct field) * nfields);
15765 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
15766
15767 if (fip->non_public_fields && cu->language != language_ada)
15768 {
15769 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15770
15771 TYPE_FIELD_PRIVATE_BITS (type) =
15772 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15773 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15774
15775 TYPE_FIELD_PROTECTED_BITS (type) =
15776 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15777 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15778
15779 TYPE_FIELD_IGNORE_BITS (type) =
15780 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15781 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15782 }
15783
15784 /* If the type has baseclasses, allocate and clear a bit vector for
15785 TYPE_FIELD_VIRTUAL_BITS. */
15786 if (fip->nbaseclasses && cu->language != language_ada)
15787 {
15788 int num_bytes = B_BYTES (fip->nbaseclasses);
15789 unsigned char *pointer;
15790
15791 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15792 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15793 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15794 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
15795 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
15796 }
15797
15798 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15799 {
15800 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15801
15802 int index = nfields - 1;
15803 struct nextfield *field = fip->fields;
15804
15805 while (index >= 0)
15806 {
15807 if (field->variant.is_discriminant)
15808 di->discriminant_index = index;
15809 else if (field->variant.default_branch)
15810 di->default_index = index;
15811 else
15812 di->discriminants[index] = field->variant.discriminant_value;
15813
15814 --index;
15815 field = field->next;
15816 }
15817 }
15818
15819 /* Copy the saved-up fields into the field vector. Start from the head of
15820 the list, adding to the tail of the field array, so that they end up in
15821 the same order in the array in which they were added to the list. */
15822 while (nfields-- > 0)
15823 {
15824 struct nextfield *fieldp;
15825
15826 if (fip->fields)
15827 {
15828 fieldp = fip->fields;
15829 fip->fields = fieldp->next;
15830 }
15831 else
15832 {
15833 fieldp = fip->baseclasses;
15834 fip->baseclasses = fieldp->next;
15835 }
15836
15837 TYPE_FIELD (type, nfields) = fieldp->field;
15838 switch (fieldp->accessibility)
15839 {
15840 case DW_ACCESS_private:
15841 if (cu->language != language_ada)
15842 SET_TYPE_FIELD_PRIVATE (type, nfields);
15843 break;
15844
15845 case DW_ACCESS_protected:
15846 if (cu->language != language_ada)
15847 SET_TYPE_FIELD_PROTECTED (type, nfields);
15848 break;
15849
15850 case DW_ACCESS_public:
15851 break;
15852
15853 default:
15854 /* Unknown accessibility. Complain and treat it as public. */
15855 {
15856 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15857 fieldp->accessibility);
15858 }
15859 break;
15860 }
15861 if (nfields < fip->nbaseclasses)
15862 {
15863 switch (fieldp->virtuality)
15864 {
15865 case DW_VIRTUALITY_virtual:
15866 case DW_VIRTUALITY_pure_virtual:
15867 if (cu->language == language_ada)
15868 error (_("unexpected virtuality in component of Ada type"));
15869 SET_TYPE_FIELD_VIRTUAL (type, nfields);
15870 break;
15871 }
15872 }
15873 }
15874 }
15875
15876 /* Return true if this member function is a constructor, false
15877 otherwise. */
15878
15879 static int
15880 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15881 {
15882 const char *fieldname;
15883 const char *type_name;
15884 int len;
15885
15886 if (die->parent == NULL)
15887 return 0;
15888
15889 if (die->parent->tag != DW_TAG_structure_type
15890 && die->parent->tag != DW_TAG_union_type
15891 && die->parent->tag != DW_TAG_class_type)
15892 return 0;
15893
15894 fieldname = dwarf2_name (die, cu);
15895 type_name = dwarf2_name (die->parent, cu);
15896 if (fieldname == NULL || type_name == NULL)
15897 return 0;
15898
15899 len = strlen (fieldname);
15900 return (strncmp (fieldname, type_name, len) == 0
15901 && (type_name[len] == '\0' || type_name[len] == '<'));
15902 }
15903
15904 /* Add a member function to the proper fieldlist. */
15905
15906 static void
15907 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15908 struct type *type, struct dwarf2_cu *cu)
15909 {
15910 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15911 struct attribute *attr;
15912 struct fnfieldlist *flp;
15913 int i;
15914 struct fn_field *fnp;
15915 const char *fieldname;
15916 struct nextfnfield *new_fnfield;
15917 struct type *this_type;
15918 enum dwarf_access_attribute accessibility;
15919
15920 if (cu->language == language_ada)
15921 error (_("unexpected member function in Ada type"));
15922
15923 /* Get name of member function. */
15924 fieldname = dwarf2_name (die, cu);
15925 if (fieldname == NULL)
15926 return;
15927
15928 /* Look up member function name in fieldlist. */
15929 for (i = 0; i < fip->nfnfields; i++)
15930 {
15931 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15932 break;
15933 }
15934
15935 /* Create new list element if necessary. */
15936 if (i < fip->nfnfields)
15937 flp = &fip->fnfieldlists[i];
15938 else
15939 {
15940 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
15941 {
15942 fip->fnfieldlists = (struct fnfieldlist *)
15943 xrealloc (fip->fnfieldlists,
15944 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
15945 * sizeof (struct fnfieldlist));
15946 if (fip->nfnfields == 0)
15947 make_cleanup (free_current_contents, &fip->fnfieldlists);
15948 }
15949 flp = &fip->fnfieldlists[fip->nfnfields];
15950 flp->name = fieldname;
15951 flp->length = 0;
15952 flp->head = NULL;
15953 i = fip->nfnfields++;
15954 }
15955
15956 /* Create a new member function field and chain it to the field list
15957 entry. */
15958 new_fnfield = XNEW (struct nextfnfield);
15959 make_cleanup (xfree, new_fnfield);
15960 memset (new_fnfield, 0, sizeof (struct nextfnfield));
15961 new_fnfield->next = flp->head;
15962 flp->head = new_fnfield;
15963 flp->length++;
15964
15965 /* Fill in the member function field info. */
15966 fnp = &new_fnfield->fnfield;
15967
15968 /* Delay processing of the physname until later. */
15969 if (cu->language == language_cplus)
15970 {
15971 add_to_method_list (type, i, flp->length - 1, fieldname,
15972 die, cu);
15973 }
15974 else
15975 {
15976 const char *physname = dwarf2_physname (fieldname, die, cu);
15977 fnp->physname = physname ? physname : "";
15978 }
15979
15980 fnp->type = alloc_type (objfile);
15981 this_type = read_type_die (die, cu);
15982 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15983 {
15984 int nparams = TYPE_NFIELDS (this_type);
15985
15986 /* TYPE is the domain of this method, and THIS_TYPE is the type
15987 of the method itself (TYPE_CODE_METHOD). */
15988 smash_to_method_type (fnp->type, type,
15989 TYPE_TARGET_TYPE (this_type),
15990 TYPE_FIELDS (this_type),
15991 TYPE_NFIELDS (this_type),
15992 TYPE_VARARGS (this_type));
15993
15994 /* Handle static member functions.
15995 Dwarf2 has no clean way to discern C++ static and non-static
15996 member functions. G++ helps GDB by marking the first
15997 parameter for non-static member functions (which is the this
15998 pointer) as artificial. We obtain this information from
15999 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
16000 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
16001 fnp->voffset = VOFFSET_STATIC;
16002 }
16003 else
16004 complaint (&symfile_complaints, _("member function type missing for '%s'"),
16005 dwarf2_full_name (fieldname, die, cu));
16006
16007 /* Get fcontext from DW_AT_containing_type if present. */
16008 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16009 fnp->fcontext = die_containing_type (die, cu);
16010
16011 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
16012 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
16013
16014 /* Get accessibility. */
16015 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
16016 if (attr)
16017 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
16018 else
16019 accessibility = dwarf2_default_access_attribute (die, cu);
16020 switch (accessibility)
16021 {
16022 case DW_ACCESS_private:
16023 fnp->is_private = 1;
16024 break;
16025 case DW_ACCESS_protected:
16026 fnp->is_protected = 1;
16027 break;
16028 }
16029
16030 /* Check for artificial methods. */
16031 attr = dwarf2_attr (die, DW_AT_artificial, cu);
16032 if (attr && DW_UNSND (attr) != 0)
16033 fnp->is_artificial = 1;
16034
16035 fnp->is_constructor = dwarf2_is_constructor (die, cu);
16036
16037 /* Get index in virtual function table if it is a virtual member
16038 function. For older versions of GCC, this is an offset in the
16039 appropriate virtual table, as specified by DW_AT_containing_type.
16040 For everyone else, it is an expression to be evaluated relative
16041 to the object address. */
16042
16043 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
16044 if (attr)
16045 {
16046 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
16047 {
16048 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
16049 {
16050 /* Old-style GCC. */
16051 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
16052 }
16053 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
16054 || (DW_BLOCK (attr)->size > 1
16055 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
16056 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
16057 {
16058 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
16059 if ((fnp->voffset % cu->header.addr_size) != 0)
16060 dwarf2_complex_location_expr_complaint ();
16061 else
16062 fnp->voffset /= cu->header.addr_size;
16063 fnp->voffset += 2;
16064 }
16065 else
16066 dwarf2_complex_location_expr_complaint ();
16067
16068 if (!fnp->fcontext)
16069 {
16070 /* If there is no `this' field and no DW_AT_containing_type,
16071 we cannot actually find a base class context for the
16072 vtable! */
16073 if (TYPE_NFIELDS (this_type) == 0
16074 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
16075 {
16076 complaint (&symfile_complaints,
16077 _("cannot determine context for virtual member "
16078 "function \"%s\" (offset %s)"),
16079 fieldname, sect_offset_str (die->sect_off));
16080 }
16081 else
16082 {
16083 fnp->fcontext
16084 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
16085 }
16086 }
16087 }
16088 else if (attr_form_is_section_offset (attr))
16089 {
16090 dwarf2_complex_location_expr_complaint ();
16091 }
16092 else
16093 {
16094 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
16095 fieldname);
16096 }
16097 }
16098 else
16099 {
16100 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
16101 if (attr && DW_UNSND (attr))
16102 {
16103 /* GCC does this, as of 2008-08-25; PR debug/37237. */
16104 complaint (&symfile_complaints,
16105 _("Member function \"%s\" (offset %s) is virtual "
16106 "but the vtable offset is not specified"),
16107 fieldname, sect_offset_str (die->sect_off));
16108 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16109 TYPE_CPLUS_DYNAMIC (type) = 1;
16110 }
16111 }
16112 }
16113
16114 /* Create the vector of member function fields, and attach it to the type. */
16115
16116 static void
16117 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
16118 struct dwarf2_cu *cu)
16119 {
16120 struct fnfieldlist *flp;
16121 int i;
16122
16123 if (cu->language == language_ada)
16124 error (_("unexpected member functions in Ada type"));
16125
16126 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16127 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
16128 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
16129
16130 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
16131 {
16132 struct nextfnfield *nfp = flp->head;
16133 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
16134 int k;
16135
16136 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
16137 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
16138 fn_flp->fn_fields = (struct fn_field *)
16139 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
16140 for (k = flp->length; (k--, nfp); nfp = nfp->next)
16141 fn_flp->fn_fields[k] = nfp->fnfield;
16142 }
16143
16144 TYPE_NFN_FIELDS (type) = fip->nfnfields;
16145 }
16146
16147 /* Returns non-zero if NAME is the name of a vtable member in CU's
16148 language, zero otherwise. */
16149 static int
16150 is_vtable_name (const char *name, struct dwarf2_cu *cu)
16151 {
16152 static const char vptr[] = "_vptr";
16153
16154 /* Look for the C++ form of the vtable. */
16155 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
16156 return 1;
16157
16158 return 0;
16159 }
16160
16161 /* GCC outputs unnamed structures that are really pointers to member
16162 functions, with the ABI-specified layout. If TYPE describes
16163 such a structure, smash it into a member function type.
16164
16165 GCC shouldn't do this; it should just output pointer to member DIEs.
16166 This is GCC PR debug/28767. */
16167
16168 static void
16169 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
16170 {
16171 struct type *pfn_type, *self_type, *new_type;
16172
16173 /* Check for a structure with no name and two children. */
16174 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
16175 return;
16176
16177 /* Check for __pfn and __delta members. */
16178 if (TYPE_FIELD_NAME (type, 0) == NULL
16179 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
16180 || TYPE_FIELD_NAME (type, 1) == NULL
16181 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
16182 return;
16183
16184 /* Find the type of the method. */
16185 pfn_type = TYPE_FIELD_TYPE (type, 0);
16186 if (pfn_type == NULL
16187 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
16188 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
16189 return;
16190
16191 /* Look for the "this" argument. */
16192 pfn_type = TYPE_TARGET_TYPE (pfn_type);
16193 if (TYPE_NFIELDS (pfn_type) == 0
16194 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
16195 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
16196 return;
16197
16198 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
16199 new_type = alloc_type (objfile);
16200 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
16201 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
16202 TYPE_VARARGS (pfn_type));
16203 smash_to_methodptr_type (type, new_type);
16204 }
16205
16206
16207 /* Called when we find the DIE that starts a structure or union scope
16208 (definition) to create a type for the structure or union. Fill in
16209 the type's name and general properties; the members will not be
16210 processed until process_structure_scope. A symbol table entry for
16211 the type will also not be done until process_structure_scope (assuming
16212 the type has a name).
16213
16214 NOTE: we need to call these functions regardless of whether or not the
16215 DIE has a DW_AT_name attribute, since it might be an anonymous
16216 structure or union. This gets the type entered into our set of
16217 user defined types. */
16218
16219 static struct type *
16220 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16221 {
16222 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16223 struct type *type;
16224 struct attribute *attr;
16225 const char *name;
16226
16227 /* If the definition of this type lives in .debug_types, read that type.
16228 Don't follow DW_AT_specification though, that will take us back up
16229 the chain and we want to go down. */
16230 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16231 if (attr)
16232 {
16233 type = get_DW_AT_signature_type (die, attr, cu);
16234
16235 /* The type's CU may not be the same as CU.
16236 Ensure TYPE is recorded with CU in die_type_hash. */
16237 return set_die_type (die, type, cu);
16238 }
16239
16240 type = alloc_type (objfile);
16241 INIT_CPLUS_SPECIFIC (type);
16242
16243 name = dwarf2_name (die, cu);
16244 if (name != NULL)
16245 {
16246 if (cu->language == language_cplus
16247 || cu->language == language_d
16248 || cu->language == language_rust)
16249 {
16250 const char *full_name = dwarf2_full_name (name, die, cu);
16251
16252 /* dwarf2_full_name might have already finished building the DIE's
16253 type. If so, there is no need to continue. */
16254 if (get_die_type (die, cu) != NULL)
16255 return get_die_type (die, cu);
16256
16257 TYPE_TAG_NAME (type) = full_name;
16258 if (die->tag == DW_TAG_structure_type
16259 || die->tag == DW_TAG_class_type)
16260 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16261 }
16262 else
16263 {
16264 /* The name is already allocated along with this objfile, so
16265 we don't need to duplicate it for the type. */
16266 TYPE_TAG_NAME (type) = name;
16267 if (die->tag == DW_TAG_class_type)
16268 TYPE_NAME (type) = TYPE_TAG_NAME (type);
16269 }
16270 }
16271
16272 if (die->tag == DW_TAG_structure_type)
16273 {
16274 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16275 }
16276 else if (die->tag == DW_TAG_union_type)
16277 {
16278 TYPE_CODE (type) = TYPE_CODE_UNION;
16279 }
16280 else if (die->tag == DW_TAG_variant_part)
16281 {
16282 TYPE_CODE (type) = TYPE_CODE_UNION;
16283 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
16284 }
16285 else
16286 {
16287 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16288 }
16289
16290 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16291 TYPE_DECLARED_CLASS (type) = 1;
16292
16293 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16294 if (attr)
16295 {
16296 if (attr_form_is_constant (attr))
16297 TYPE_LENGTH (type) = DW_UNSND (attr);
16298 else
16299 {
16300 /* For the moment, dynamic type sizes are not supported
16301 by GDB's struct type. The actual size is determined
16302 on-demand when resolving the type of a given object,
16303 so set the type's length to zero for now. Otherwise,
16304 we record an expression as the length, and that expression
16305 could lead to a very large value, which could eventually
16306 lead to us trying to allocate that much memory when creating
16307 a value of that type. */
16308 TYPE_LENGTH (type) = 0;
16309 }
16310 }
16311 else
16312 {
16313 TYPE_LENGTH (type) = 0;
16314 }
16315
16316 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16317 {
16318 /* ICC<14 does not output the required DW_AT_declaration on
16319 incomplete types, but gives them a size of zero. */
16320 TYPE_STUB (type) = 1;
16321 }
16322 else
16323 TYPE_STUB_SUPPORTED (type) = 1;
16324
16325 if (die_is_declaration (die, cu))
16326 TYPE_STUB (type) = 1;
16327 else if (attr == NULL && die->child == NULL
16328 && producer_is_realview (cu->producer))
16329 /* RealView does not output the required DW_AT_declaration
16330 on incomplete types. */
16331 TYPE_STUB (type) = 1;
16332
16333 /* We need to add the type field to the die immediately so we don't
16334 infinitely recurse when dealing with pointers to the structure
16335 type within the structure itself. */
16336 set_die_type (die, type, cu);
16337
16338 /* set_die_type should be already done. */
16339 set_descriptive_type (type, die, cu);
16340
16341 return type;
16342 }
16343
16344 /* A helper for process_structure_scope that handles a single member
16345 DIE. */
16346
16347 static void
16348 handle_struct_member_die (struct die_info *child_die, struct type *type,
16349 struct field_info *fi,
16350 std::vector<struct symbol *> *template_args,
16351 struct dwarf2_cu *cu)
16352 {
16353 if (child_die->tag == DW_TAG_member
16354 || child_die->tag == DW_TAG_variable
16355 || child_die->tag == DW_TAG_variant_part)
16356 {
16357 /* NOTE: carlton/2002-11-05: A C++ static data member
16358 should be a DW_TAG_member that is a declaration, but
16359 all versions of G++ as of this writing (so through at
16360 least 3.2.1) incorrectly generate DW_TAG_variable
16361 tags for them instead. */
16362 dwarf2_add_field (fi, child_die, cu);
16363 }
16364 else if (child_die->tag == DW_TAG_subprogram)
16365 {
16366 /* Rust doesn't have member functions in the C++ sense.
16367 However, it does emit ordinary functions as children
16368 of a struct DIE. */
16369 if (cu->language == language_rust)
16370 read_func_scope (child_die, cu);
16371 else
16372 {
16373 /* C++ member function. */
16374 dwarf2_add_member_fn (fi, child_die, type, cu);
16375 }
16376 }
16377 else if (child_die->tag == DW_TAG_inheritance)
16378 {
16379 /* C++ base class field. */
16380 dwarf2_add_field (fi, child_die, cu);
16381 }
16382 else if (type_can_define_types (child_die))
16383 dwarf2_add_type_defn (fi, child_die, cu);
16384 else if (child_die->tag == DW_TAG_template_type_param
16385 || child_die->tag == DW_TAG_template_value_param)
16386 {
16387 struct symbol *arg = new_symbol (child_die, NULL, cu);
16388
16389 if (arg != NULL)
16390 template_args->push_back (arg);
16391 }
16392 else if (child_die->tag == DW_TAG_variant)
16393 {
16394 /* In a variant we want to get the discriminant and also add a
16395 field for our sole member child. */
16396 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16397
16398 for (struct die_info *variant_child = child_die->child;
16399 variant_child != NULL;
16400 variant_child = sibling_die (variant_child))
16401 {
16402 if (variant_child->tag == DW_TAG_member)
16403 {
16404 handle_struct_member_die (variant_child, type, fi,
16405 template_args, cu);
16406 /* Only handle the one. */
16407 break;
16408 }
16409 }
16410
16411 /* We don't handle this but we might as well report it if we see
16412 it. */
16413 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16414 complaint (&symfile_complaints,
16415 _("DW_AT_discr_list is not supported yet"
16416 " - DIE at %s [in module %s]"),
16417 sect_offset_str (child_die->sect_off),
16418 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16419
16420 /* The first field was just added, so we can stash the
16421 discriminant there. */
16422 gdb_assert (fi->fields != NULL);
16423 if (discr == NULL)
16424 fi->fields->variant.default_branch = true;
16425 else
16426 fi->fields->variant.discriminant_value = DW_UNSND (discr);
16427 }
16428 }
16429
16430 /* Finish creating a structure or union type, including filling in
16431 its members and creating a symbol for it. */
16432
16433 static void
16434 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16435 {
16436 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16437 struct die_info *child_die;
16438 struct type *type;
16439
16440 type = get_die_type (die, cu);
16441 if (type == NULL)
16442 type = read_structure_type (die, cu);
16443
16444 /* When reading a DW_TAG_variant_part, we need to notice when we
16445 read the discriminant member, so we can record it later in the
16446 discriminant_info. */
16447 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16448 sect_offset discr_offset;
16449
16450 if (is_variant_part)
16451 {
16452 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16453 if (discr == NULL)
16454 {
16455 /* Maybe it's a univariant form, an extension we support.
16456 In this case arrange not to check the offset. */
16457 is_variant_part = false;
16458 }
16459 else if (attr_form_is_ref (discr))
16460 {
16461 struct dwarf2_cu *target_cu = cu;
16462 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16463
16464 discr_offset = target_die->sect_off;
16465 }
16466 else
16467 {
16468 complaint (&symfile_complaints,
16469 _("DW_AT_discr does not have DIE reference form"
16470 " - DIE at %s [in module %s]"),
16471 sect_offset_str (die->sect_off),
16472 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16473 is_variant_part = false;
16474 }
16475 }
16476
16477 if (die->child != NULL && ! die_is_declaration (die, cu))
16478 {
16479 struct field_info fi;
16480 std::vector<struct symbol *> template_args;
16481 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
16482
16483 memset (&fi, 0, sizeof (struct field_info));
16484
16485 child_die = die->child;
16486
16487 while (child_die && child_die->tag)
16488 {
16489 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16490
16491 if (is_variant_part && discr_offset == child_die->sect_off)
16492 fi.fields->variant.is_discriminant = true;
16493
16494 child_die = sibling_die (child_die);
16495 }
16496
16497 /* Attach template arguments to type. */
16498 if (!template_args.empty ())
16499 {
16500 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16501 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16502 TYPE_TEMPLATE_ARGUMENTS (type)
16503 = XOBNEWVEC (&objfile->objfile_obstack,
16504 struct symbol *,
16505 TYPE_N_TEMPLATE_ARGUMENTS (type));
16506 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16507 template_args.data (),
16508 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16509 * sizeof (struct symbol *)));
16510 }
16511
16512 /* Attach fields and member functions to the type. */
16513 if (fi.nfields)
16514 dwarf2_attach_fields_to_type (&fi, type, cu);
16515 if (fi.nfnfields)
16516 {
16517 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16518
16519 /* Get the type which refers to the base class (possibly this
16520 class itself) which contains the vtable pointer for the current
16521 class from the DW_AT_containing_type attribute. This use of
16522 DW_AT_containing_type is a GNU extension. */
16523
16524 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16525 {
16526 struct type *t = die_containing_type (die, cu);
16527
16528 set_type_vptr_basetype (type, t);
16529 if (type == t)
16530 {
16531 int i;
16532
16533 /* Our own class provides vtbl ptr. */
16534 for (i = TYPE_NFIELDS (t) - 1;
16535 i >= TYPE_N_BASECLASSES (t);
16536 --i)
16537 {
16538 const char *fieldname = TYPE_FIELD_NAME (t, i);
16539
16540 if (is_vtable_name (fieldname, cu))
16541 {
16542 set_type_vptr_fieldno (type, i);
16543 break;
16544 }
16545 }
16546
16547 /* Complain if virtual function table field not found. */
16548 if (i < TYPE_N_BASECLASSES (t))
16549 complaint (&symfile_complaints,
16550 _("virtual function table pointer "
16551 "not found when defining class '%s'"),
16552 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16553 "");
16554 }
16555 else
16556 {
16557 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16558 }
16559 }
16560 else if (cu->producer
16561 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16562 {
16563 /* The IBM XLC compiler does not provide direct indication
16564 of the containing type, but the vtable pointer is
16565 always named __vfp. */
16566
16567 int i;
16568
16569 for (i = TYPE_NFIELDS (type) - 1;
16570 i >= TYPE_N_BASECLASSES (type);
16571 --i)
16572 {
16573 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16574 {
16575 set_type_vptr_fieldno (type, i);
16576 set_type_vptr_basetype (type, type);
16577 break;
16578 }
16579 }
16580 }
16581 }
16582
16583 /* Copy fi.typedef_field_list linked list elements content into the
16584 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16585 if (fi.typedef_field_list)
16586 {
16587 int i = fi.typedef_field_list_count;
16588
16589 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16590 TYPE_TYPEDEF_FIELD_ARRAY (type)
16591 = ((struct decl_field *)
16592 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
16593 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
16594
16595 /* Reverse the list order to keep the debug info elements order. */
16596 while (--i >= 0)
16597 {
16598 struct decl_field *dest, *src;
16599
16600 dest = &TYPE_TYPEDEF_FIELD (type, i);
16601 src = &fi.typedef_field_list->field;
16602 fi.typedef_field_list = fi.typedef_field_list->next;
16603 *dest = *src;
16604 }
16605 }
16606
16607 /* Copy fi.nested_types_list linked list elements content into the
16608 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16609 if (fi.nested_types_list != NULL && cu->language != language_ada)
16610 {
16611 int i = fi.nested_types_list_count;
16612
16613 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16614 TYPE_NESTED_TYPES_ARRAY (type)
16615 = ((struct decl_field *)
16616 TYPE_ALLOC (type, sizeof (struct decl_field) * i));
16617 TYPE_NESTED_TYPES_COUNT (type) = i;
16618
16619 /* Reverse the list order to keep the debug info elements order. */
16620 while (--i >= 0)
16621 {
16622 struct decl_field *dest, *src;
16623
16624 dest = &TYPE_NESTED_TYPES_FIELD (type, i);
16625 src = &fi.nested_types_list->field;
16626 fi.nested_types_list = fi.nested_types_list->next;
16627 *dest = *src;
16628 }
16629 }
16630
16631 do_cleanups (back_to);
16632 }
16633
16634 quirk_gcc_member_function_pointer (type, objfile);
16635 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16636 cu->rust_unions.push_back (type);
16637
16638 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16639 snapshots) has been known to create a die giving a declaration
16640 for a class that has, as a child, a die giving a definition for a
16641 nested class. So we have to process our children even if the
16642 current die is a declaration. Normally, of course, a declaration
16643 won't have any children at all. */
16644
16645 child_die = die->child;
16646
16647 while (child_die != NULL && child_die->tag)
16648 {
16649 if (child_die->tag == DW_TAG_member
16650 || child_die->tag == DW_TAG_variable
16651 || child_die->tag == DW_TAG_inheritance
16652 || child_die->tag == DW_TAG_template_value_param
16653 || child_die->tag == DW_TAG_template_type_param)
16654 {
16655 /* Do nothing. */
16656 }
16657 else
16658 process_die (child_die, cu);
16659
16660 child_die = sibling_die (child_die);
16661 }
16662
16663 /* Do not consider external references. According to the DWARF standard,
16664 these DIEs are identified by the fact that they have no byte_size
16665 attribute, and a declaration attribute. */
16666 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16667 || !die_is_declaration (die, cu))
16668 new_symbol (die, type, cu);
16669 }
16670
16671 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16672 update TYPE using some information only available in DIE's children. */
16673
16674 static void
16675 update_enumeration_type_from_children (struct die_info *die,
16676 struct type *type,
16677 struct dwarf2_cu *cu)
16678 {
16679 struct die_info *child_die;
16680 int unsigned_enum = 1;
16681 int flag_enum = 1;
16682 ULONGEST mask = 0;
16683
16684 auto_obstack obstack;
16685
16686 for (child_die = die->child;
16687 child_die != NULL && child_die->tag;
16688 child_die = sibling_die (child_die))
16689 {
16690 struct attribute *attr;
16691 LONGEST value;
16692 const gdb_byte *bytes;
16693 struct dwarf2_locexpr_baton *baton;
16694 const char *name;
16695
16696 if (child_die->tag != DW_TAG_enumerator)
16697 continue;
16698
16699 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16700 if (attr == NULL)
16701 continue;
16702
16703 name = dwarf2_name (child_die, cu);
16704 if (name == NULL)
16705 name = "<anonymous enumerator>";
16706
16707 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16708 &value, &bytes, &baton);
16709 if (value < 0)
16710 {
16711 unsigned_enum = 0;
16712 flag_enum = 0;
16713 }
16714 else if ((mask & value) != 0)
16715 flag_enum = 0;
16716 else
16717 mask |= value;
16718
16719 /* If we already know that the enum type is neither unsigned, nor
16720 a flag type, no need to look at the rest of the enumerates. */
16721 if (!unsigned_enum && !flag_enum)
16722 break;
16723 }
16724
16725 if (unsigned_enum)
16726 TYPE_UNSIGNED (type) = 1;
16727 if (flag_enum)
16728 TYPE_FLAG_ENUM (type) = 1;
16729 }
16730
16731 /* Given a DW_AT_enumeration_type die, set its type. We do not
16732 complete the type's fields yet, or create any symbols. */
16733
16734 static struct type *
16735 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16736 {
16737 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16738 struct type *type;
16739 struct attribute *attr;
16740 const char *name;
16741
16742 /* If the definition of this type lives in .debug_types, read that type.
16743 Don't follow DW_AT_specification though, that will take us back up
16744 the chain and we want to go down. */
16745 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16746 if (attr)
16747 {
16748 type = get_DW_AT_signature_type (die, attr, cu);
16749
16750 /* The type's CU may not be the same as CU.
16751 Ensure TYPE is recorded with CU in die_type_hash. */
16752 return set_die_type (die, type, cu);
16753 }
16754
16755 type = alloc_type (objfile);
16756
16757 TYPE_CODE (type) = TYPE_CODE_ENUM;
16758 name = dwarf2_full_name (NULL, die, cu);
16759 if (name != NULL)
16760 TYPE_TAG_NAME (type) = name;
16761
16762 attr = dwarf2_attr (die, DW_AT_type, cu);
16763 if (attr != NULL)
16764 {
16765 struct type *underlying_type = die_type (die, cu);
16766
16767 TYPE_TARGET_TYPE (type) = underlying_type;
16768 }
16769
16770 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16771 if (attr)
16772 {
16773 TYPE_LENGTH (type) = DW_UNSND (attr);
16774 }
16775 else
16776 {
16777 TYPE_LENGTH (type) = 0;
16778 }
16779
16780 /* The enumeration DIE can be incomplete. In Ada, any type can be
16781 declared as private in the package spec, and then defined only
16782 inside the package body. Such types are known as Taft Amendment
16783 Types. When another package uses such a type, an incomplete DIE
16784 may be generated by the compiler. */
16785 if (die_is_declaration (die, cu))
16786 TYPE_STUB (type) = 1;
16787
16788 /* Finish the creation of this type by using the enum's children.
16789 We must call this even when the underlying type has been provided
16790 so that we can determine if we're looking at a "flag" enum. */
16791 update_enumeration_type_from_children (die, type, cu);
16792
16793 /* If this type has an underlying type that is not a stub, then we
16794 may use its attributes. We always use the "unsigned" attribute
16795 in this situation, because ordinarily we guess whether the type
16796 is unsigned -- but the guess can be wrong and the underlying type
16797 can tell us the reality. However, we defer to a local size
16798 attribute if one exists, because this lets the compiler override
16799 the underlying type if needed. */
16800 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16801 {
16802 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16803 if (TYPE_LENGTH (type) == 0)
16804 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16805 }
16806
16807 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16808
16809 return set_die_type (die, type, cu);
16810 }
16811
16812 /* Given a pointer to a die which begins an enumeration, process all
16813 the dies that define the members of the enumeration, and create the
16814 symbol for the enumeration type.
16815
16816 NOTE: We reverse the order of the element list. */
16817
16818 static void
16819 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16820 {
16821 struct type *this_type;
16822
16823 this_type = get_die_type (die, cu);
16824 if (this_type == NULL)
16825 this_type = read_enumeration_type (die, cu);
16826
16827 if (die->child != NULL)
16828 {
16829 struct die_info *child_die;
16830 struct symbol *sym;
16831 struct field *fields = NULL;
16832 int num_fields = 0;
16833 const char *name;
16834
16835 child_die = die->child;
16836 while (child_die && child_die->tag)
16837 {
16838 if (child_die->tag != DW_TAG_enumerator)
16839 {
16840 process_die (child_die, cu);
16841 }
16842 else
16843 {
16844 name = dwarf2_name (child_die, cu);
16845 if (name)
16846 {
16847 sym = new_symbol (child_die, this_type, cu);
16848
16849 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16850 {
16851 fields = (struct field *)
16852 xrealloc (fields,
16853 (num_fields + DW_FIELD_ALLOC_CHUNK)
16854 * sizeof (struct field));
16855 }
16856
16857 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16858 FIELD_TYPE (fields[num_fields]) = NULL;
16859 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16860 FIELD_BITSIZE (fields[num_fields]) = 0;
16861
16862 num_fields++;
16863 }
16864 }
16865
16866 child_die = sibling_die (child_die);
16867 }
16868
16869 if (num_fields)
16870 {
16871 TYPE_NFIELDS (this_type) = num_fields;
16872 TYPE_FIELDS (this_type) = (struct field *)
16873 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16874 memcpy (TYPE_FIELDS (this_type), fields,
16875 sizeof (struct field) * num_fields);
16876 xfree (fields);
16877 }
16878 }
16879
16880 /* If we are reading an enum from a .debug_types unit, and the enum
16881 is a declaration, and the enum is not the signatured type in the
16882 unit, then we do not want to add a symbol for it. Adding a
16883 symbol would in some cases obscure the true definition of the
16884 enum, giving users an incomplete type when the definition is
16885 actually available. Note that we do not want to do this for all
16886 enums which are just declarations, because C++0x allows forward
16887 enum declarations. */
16888 if (cu->per_cu->is_debug_types
16889 && die_is_declaration (die, cu))
16890 {
16891 struct signatured_type *sig_type;
16892
16893 sig_type = (struct signatured_type *) cu->per_cu;
16894 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16895 if (sig_type->type_offset_in_section != die->sect_off)
16896 return;
16897 }
16898
16899 new_symbol (die, this_type, cu);
16900 }
16901
16902 /* Extract all information from a DW_TAG_array_type DIE and put it in
16903 the DIE's type field. For now, this only handles one dimensional
16904 arrays. */
16905
16906 static struct type *
16907 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16908 {
16909 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16910 struct die_info *child_die;
16911 struct type *type;
16912 struct type *element_type, *range_type, *index_type;
16913 struct attribute *attr;
16914 const char *name;
16915 struct dynamic_prop *byte_stride_prop = NULL;
16916 unsigned int bit_stride = 0;
16917
16918 element_type = die_type (die, cu);
16919
16920 /* The die_type call above may have already set the type for this DIE. */
16921 type = get_die_type (die, cu);
16922 if (type)
16923 return type;
16924
16925 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16926 if (attr != NULL)
16927 {
16928 int stride_ok;
16929
16930 byte_stride_prop
16931 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16932 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16933 if (!stride_ok)
16934 {
16935 complaint (&symfile_complaints,
16936 _("unable to read array DW_AT_byte_stride "
16937 " - DIE at %s [in module %s]"),
16938 sect_offset_str (die->sect_off),
16939 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16940 /* Ignore this attribute. We will likely not be able to print
16941 arrays of this type correctly, but there is little we can do
16942 to help if we cannot read the attribute's value. */
16943 byte_stride_prop = NULL;
16944 }
16945 }
16946
16947 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16948 if (attr != NULL)
16949 bit_stride = DW_UNSND (attr);
16950
16951 /* Irix 6.2 native cc creates array types without children for
16952 arrays with unspecified length. */
16953 if (die->child == NULL)
16954 {
16955 index_type = objfile_type (objfile)->builtin_int;
16956 range_type = create_static_range_type (NULL, index_type, 0, -1);
16957 type = create_array_type_with_stride (NULL, element_type, range_type,
16958 byte_stride_prop, bit_stride);
16959 return set_die_type (die, type, cu);
16960 }
16961
16962 std::vector<struct type *> range_types;
16963 child_die = die->child;
16964 while (child_die && child_die->tag)
16965 {
16966 if (child_die->tag == DW_TAG_subrange_type)
16967 {
16968 struct type *child_type = read_type_die (child_die, cu);
16969
16970 if (child_type != NULL)
16971 {
16972 /* The range type was succesfully read. Save it for the
16973 array type creation. */
16974 range_types.push_back (child_type);
16975 }
16976 }
16977 child_die = sibling_die (child_die);
16978 }
16979
16980 /* Dwarf2 dimensions are output from left to right, create the
16981 necessary array types in backwards order. */
16982
16983 type = element_type;
16984
16985 if (read_array_order (die, cu) == DW_ORD_col_major)
16986 {
16987 int i = 0;
16988
16989 while (i < range_types.size ())
16990 type = create_array_type_with_stride (NULL, type, range_types[i++],
16991 byte_stride_prop, bit_stride);
16992 }
16993 else
16994 {
16995 size_t ndim = range_types.size ();
16996 while (ndim-- > 0)
16997 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16998 byte_stride_prop, bit_stride);
16999 }
17000
17001 /* Understand Dwarf2 support for vector types (like they occur on
17002 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17003 array type. This is not part of the Dwarf2/3 standard yet, but a
17004 custom vendor extension. The main difference between a regular
17005 array and the vector variant is that vectors are passed by value
17006 to functions. */
17007 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17008 if (attr)
17009 make_vector_type (type);
17010
17011 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17012 implementation may choose to implement triple vectors using this
17013 attribute. */
17014 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17015 if (attr)
17016 {
17017 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
17018 TYPE_LENGTH (type) = DW_UNSND (attr);
17019 else
17020 complaint (&symfile_complaints,
17021 _("DW_AT_byte_size for array type smaller "
17022 "than the total size of elements"));
17023 }
17024
17025 name = dwarf2_name (die, cu);
17026 if (name)
17027 TYPE_NAME (type) = name;
17028
17029 /* Install the type in the die. */
17030 set_die_type (die, type, cu);
17031
17032 /* set_die_type should be already done. */
17033 set_descriptive_type (type, die, cu);
17034
17035 return type;
17036 }
17037
17038 static enum dwarf_array_dim_ordering
17039 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17040 {
17041 struct attribute *attr;
17042
17043 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17044
17045 if (attr)
17046 return (enum dwarf_array_dim_ordering) DW_SND (attr);
17047
17048 /* GNU F77 is a special case, as at 08/2004 array type info is the
17049 opposite order to the dwarf2 specification, but data is still
17050 laid out as per normal fortran.
17051
17052 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17053 version checking. */
17054
17055 if (cu->language == language_fortran
17056 && cu->producer && strstr (cu->producer, "GNU F77"))
17057 {
17058 return DW_ORD_row_major;
17059 }
17060
17061 switch (cu->language_defn->la_array_ordering)
17062 {
17063 case array_column_major:
17064 return DW_ORD_col_major;
17065 case array_row_major:
17066 default:
17067 return DW_ORD_row_major;
17068 };
17069 }
17070
17071 /* Extract all information from a DW_TAG_set_type DIE and put it in
17072 the DIE's type field. */
17073
17074 static struct type *
17075 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17076 {
17077 struct type *domain_type, *set_type;
17078 struct attribute *attr;
17079
17080 domain_type = die_type (die, cu);
17081
17082 /* The die_type call above may have already set the type for this DIE. */
17083 set_type = get_die_type (die, cu);
17084 if (set_type)
17085 return set_type;
17086
17087 set_type = create_set_type (NULL, domain_type);
17088
17089 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17090 if (attr)
17091 TYPE_LENGTH (set_type) = DW_UNSND (attr);
17092
17093 return set_die_type (die, set_type, cu);
17094 }
17095
17096 /* A helper for read_common_block that creates a locexpr baton.
17097 SYM is the symbol which we are marking as computed.
17098 COMMON_DIE is the DIE for the common block.
17099 COMMON_LOC is the location expression attribute for the common
17100 block itself.
17101 MEMBER_LOC is the location expression attribute for the particular
17102 member of the common block that we are processing.
17103 CU is the CU from which the above come. */
17104
17105 static void
17106 mark_common_block_symbol_computed (struct symbol *sym,
17107 struct die_info *common_die,
17108 struct attribute *common_loc,
17109 struct attribute *member_loc,
17110 struct dwarf2_cu *cu)
17111 {
17112 struct dwarf2_per_objfile *dwarf2_per_objfile
17113 = cu->per_cu->dwarf2_per_objfile;
17114 struct objfile *objfile = dwarf2_per_objfile->objfile;
17115 struct dwarf2_locexpr_baton *baton;
17116 gdb_byte *ptr;
17117 unsigned int cu_off;
17118 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
17119 LONGEST offset = 0;
17120
17121 gdb_assert (common_loc && member_loc);
17122 gdb_assert (attr_form_is_block (common_loc));
17123 gdb_assert (attr_form_is_block (member_loc)
17124 || attr_form_is_constant (member_loc));
17125
17126 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17127 baton->per_cu = cu->per_cu;
17128 gdb_assert (baton->per_cu);
17129
17130 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17131
17132 if (attr_form_is_constant (member_loc))
17133 {
17134 offset = dwarf2_get_attr_constant_value (member_loc, 0);
17135 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17136 }
17137 else
17138 baton->size += DW_BLOCK (member_loc)->size;
17139
17140 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17141 baton->data = ptr;
17142
17143 *ptr++ = DW_OP_call4;
17144 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17145 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17146 ptr += 4;
17147
17148 if (attr_form_is_constant (member_loc))
17149 {
17150 *ptr++ = DW_OP_addr;
17151 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17152 ptr += cu->header.addr_size;
17153 }
17154 else
17155 {
17156 /* We have to copy the data here, because DW_OP_call4 will only
17157 use a DW_AT_location attribute. */
17158 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
17159 ptr += DW_BLOCK (member_loc)->size;
17160 }
17161
17162 *ptr++ = DW_OP_plus;
17163 gdb_assert (ptr - baton->data == baton->size);
17164
17165 SYMBOL_LOCATION_BATON (sym) = baton;
17166 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17167 }
17168
17169 /* Create appropriate locally-scoped variables for all the
17170 DW_TAG_common_block entries. Also create a struct common_block
17171 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17172 is used to sepate the common blocks name namespace from regular
17173 variable names. */
17174
17175 static void
17176 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17177 {
17178 struct attribute *attr;
17179
17180 attr = dwarf2_attr (die, DW_AT_location, cu);
17181 if (attr)
17182 {
17183 /* Support the .debug_loc offsets. */
17184 if (attr_form_is_block (attr))
17185 {
17186 /* Ok. */
17187 }
17188 else if (attr_form_is_section_offset (attr))
17189 {
17190 dwarf2_complex_location_expr_complaint ();
17191 attr = NULL;
17192 }
17193 else
17194 {
17195 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17196 "common block member");
17197 attr = NULL;
17198 }
17199 }
17200
17201 if (die->child != NULL)
17202 {
17203 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17204 struct die_info *child_die;
17205 size_t n_entries = 0, size;
17206 struct common_block *common_block;
17207 struct symbol *sym;
17208
17209 for (child_die = die->child;
17210 child_die && child_die->tag;
17211 child_die = sibling_die (child_die))
17212 ++n_entries;
17213
17214 size = (sizeof (struct common_block)
17215 + (n_entries - 1) * sizeof (struct symbol *));
17216 common_block
17217 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17218 size);
17219 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17220 common_block->n_entries = 0;
17221
17222 for (child_die = die->child;
17223 child_die && child_die->tag;
17224 child_die = sibling_die (child_die))
17225 {
17226 /* Create the symbol in the DW_TAG_common_block block in the current
17227 symbol scope. */
17228 sym = new_symbol (child_die, NULL, cu);
17229 if (sym != NULL)
17230 {
17231 struct attribute *member_loc;
17232
17233 common_block->contents[common_block->n_entries++] = sym;
17234
17235 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17236 cu);
17237 if (member_loc)
17238 {
17239 /* GDB has handled this for a long time, but it is
17240 not specified by DWARF. It seems to have been
17241 emitted by gfortran at least as recently as:
17242 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17243 complaint (&symfile_complaints,
17244 _("Variable in common block has "
17245 "DW_AT_data_member_location "
17246 "- DIE at %s [in module %s]"),
17247 sect_offset_str (child_die->sect_off),
17248 objfile_name (objfile));
17249
17250 if (attr_form_is_section_offset (member_loc))
17251 dwarf2_complex_location_expr_complaint ();
17252 else if (attr_form_is_constant (member_loc)
17253 || attr_form_is_block (member_loc))
17254 {
17255 if (attr)
17256 mark_common_block_symbol_computed (sym, die, attr,
17257 member_loc, cu);
17258 }
17259 else
17260 dwarf2_complex_location_expr_complaint ();
17261 }
17262 }
17263 }
17264
17265 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17266 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17267 }
17268 }
17269
17270 /* Create a type for a C++ namespace. */
17271
17272 static struct type *
17273 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17274 {
17275 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17276 const char *previous_prefix, *name;
17277 int is_anonymous;
17278 struct type *type;
17279
17280 /* For extensions, reuse the type of the original namespace. */
17281 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17282 {
17283 struct die_info *ext_die;
17284 struct dwarf2_cu *ext_cu = cu;
17285
17286 ext_die = dwarf2_extension (die, &ext_cu);
17287 type = read_type_die (ext_die, ext_cu);
17288
17289 /* EXT_CU may not be the same as CU.
17290 Ensure TYPE is recorded with CU in die_type_hash. */
17291 return set_die_type (die, type, cu);
17292 }
17293
17294 name = namespace_name (die, &is_anonymous, cu);
17295
17296 /* Now build the name of the current namespace. */
17297
17298 previous_prefix = determine_prefix (die, cu);
17299 if (previous_prefix[0] != '\0')
17300 name = typename_concat (&objfile->objfile_obstack,
17301 previous_prefix, name, 0, cu);
17302
17303 /* Create the type. */
17304 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17305 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17306
17307 return set_die_type (die, type, cu);
17308 }
17309
17310 /* Read a namespace scope. */
17311
17312 static void
17313 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17314 {
17315 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17316 int is_anonymous;
17317
17318 /* Add a symbol associated to this if we haven't seen the namespace
17319 before. Also, add a using directive if it's an anonymous
17320 namespace. */
17321
17322 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17323 {
17324 struct type *type;
17325
17326 type = read_type_die (die, cu);
17327 new_symbol (die, type, cu);
17328
17329 namespace_name (die, &is_anonymous, cu);
17330 if (is_anonymous)
17331 {
17332 const char *previous_prefix = determine_prefix (die, cu);
17333
17334 std::vector<const char *> excludes;
17335 add_using_directive (using_directives (cu->language),
17336 previous_prefix, TYPE_NAME (type), NULL,
17337 NULL, excludes, 0, &objfile->objfile_obstack);
17338 }
17339 }
17340
17341 if (die->child != NULL)
17342 {
17343 struct die_info *child_die = die->child;
17344
17345 while (child_die && child_die->tag)
17346 {
17347 process_die (child_die, cu);
17348 child_die = sibling_die (child_die);
17349 }
17350 }
17351 }
17352
17353 /* Read a Fortran module as type. This DIE can be only a declaration used for
17354 imported module. Still we need that type as local Fortran "use ... only"
17355 declaration imports depend on the created type in determine_prefix. */
17356
17357 static struct type *
17358 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17359 {
17360 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17361 const char *module_name;
17362 struct type *type;
17363
17364 module_name = dwarf2_name (die, cu);
17365 if (!module_name)
17366 complaint (&symfile_complaints,
17367 _("DW_TAG_module has no name, offset %s"),
17368 sect_offset_str (die->sect_off));
17369 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17370
17371 /* determine_prefix uses TYPE_TAG_NAME. */
17372 TYPE_TAG_NAME (type) = TYPE_NAME (type);
17373
17374 return set_die_type (die, type, cu);
17375 }
17376
17377 /* Read a Fortran module. */
17378
17379 static void
17380 read_module (struct die_info *die, struct dwarf2_cu *cu)
17381 {
17382 struct die_info *child_die = die->child;
17383 struct type *type;
17384
17385 type = read_type_die (die, cu);
17386 new_symbol (die, type, cu);
17387
17388 while (child_die && child_die->tag)
17389 {
17390 process_die (child_die, cu);
17391 child_die = sibling_die (child_die);
17392 }
17393 }
17394
17395 /* Return the name of the namespace represented by DIE. Set
17396 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17397 namespace. */
17398
17399 static const char *
17400 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17401 {
17402 struct die_info *current_die;
17403 const char *name = NULL;
17404
17405 /* Loop through the extensions until we find a name. */
17406
17407 for (current_die = die;
17408 current_die != NULL;
17409 current_die = dwarf2_extension (die, &cu))
17410 {
17411 /* We don't use dwarf2_name here so that we can detect the absence
17412 of a name -> anonymous namespace. */
17413 name = dwarf2_string_attr (die, DW_AT_name, cu);
17414
17415 if (name != NULL)
17416 break;
17417 }
17418
17419 /* Is it an anonymous namespace? */
17420
17421 *is_anonymous = (name == NULL);
17422 if (*is_anonymous)
17423 name = CP_ANONYMOUS_NAMESPACE_STR;
17424
17425 return name;
17426 }
17427
17428 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17429 the user defined type vector. */
17430
17431 static struct type *
17432 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17433 {
17434 struct gdbarch *gdbarch
17435 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17436 struct comp_unit_head *cu_header = &cu->header;
17437 struct type *type;
17438 struct attribute *attr_byte_size;
17439 struct attribute *attr_address_class;
17440 int byte_size, addr_class;
17441 struct type *target_type;
17442
17443 target_type = die_type (die, cu);
17444
17445 /* The die_type call above may have already set the type for this DIE. */
17446 type = get_die_type (die, cu);
17447 if (type)
17448 return type;
17449
17450 type = lookup_pointer_type (target_type);
17451
17452 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17453 if (attr_byte_size)
17454 byte_size = DW_UNSND (attr_byte_size);
17455 else
17456 byte_size = cu_header->addr_size;
17457
17458 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17459 if (attr_address_class)
17460 addr_class = DW_UNSND (attr_address_class);
17461 else
17462 addr_class = DW_ADDR_none;
17463
17464 /* If the pointer size or address class is different than the
17465 default, create a type variant marked as such and set the
17466 length accordingly. */
17467 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
17468 {
17469 if (gdbarch_address_class_type_flags_p (gdbarch))
17470 {
17471 int type_flags;
17472
17473 type_flags = gdbarch_address_class_type_flags
17474 (gdbarch, byte_size, addr_class);
17475 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17476 == 0);
17477 type = make_type_with_address_space (type, type_flags);
17478 }
17479 else if (TYPE_LENGTH (type) != byte_size)
17480 {
17481 complaint (&symfile_complaints,
17482 _("invalid pointer size %d"), byte_size);
17483 }
17484 else
17485 {
17486 /* Should we also complain about unhandled address classes? */
17487 }
17488 }
17489
17490 TYPE_LENGTH (type) = byte_size;
17491 return set_die_type (die, type, cu);
17492 }
17493
17494 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17495 the user defined type vector. */
17496
17497 static struct type *
17498 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17499 {
17500 struct type *type;
17501 struct type *to_type;
17502 struct type *domain;
17503
17504 to_type = die_type (die, cu);
17505 domain = die_containing_type (die, cu);
17506
17507 /* The calls above may have already set the type for this DIE. */
17508 type = get_die_type (die, cu);
17509 if (type)
17510 return type;
17511
17512 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17513 type = lookup_methodptr_type (to_type);
17514 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17515 {
17516 struct type *new_type
17517 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17518
17519 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17520 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17521 TYPE_VARARGS (to_type));
17522 type = lookup_methodptr_type (new_type);
17523 }
17524 else
17525 type = lookup_memberptr_type (to_type, domain);
17526
17527 return set_die_type (die, type, cu);
17528 }
17529
17530 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17531 the user defined type vector. */
17532
17533 static struct type *
17534 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17535 enum type_code refcode)
17536 {
17537 struct comp_unit_head *cu_header = &cu->header;
17538 struct type *type, *target_type;
17539 struct attribute *attr;
17540
17541 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17542
17543 target_type = die_type (die, cu);
17544
17545 /* The die_type call above may have already set the type for this DIE. */
17546 type = get_die_type (die, cu);
17547 if (type)
17548 return type;
17549
17550 type = lookup_reference_type (target_type, refcode);
17551 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17552 if (attr)
17553 {
17554 TYPE_LENGTH (type) = DW_UNSND (attr);
17555 }
17556 else
17557 {
17558 TYPE_LENGTH (type) = cu_header->addr_size;
17559 }
17560 return set_die_type (die, type, cu);
17561 }
17562
17563 /* Add the given cv-qualifiers to the element type of the array. GCC
17564 outputs DWARF type qualifiers that apply to an array, not the
17565 element type. But GDB relies on the array element type to carry
17566 the cv-qualifiers. This mimics section 6.7.3 of the C99
17567 specification. */
17568
17569 static struct type *
17570 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17571 struct type *base_type, int cnst, int voltl)
17572 {
17573 struct type *el_type, *inner_array;
17574
17575 base_type = copy_type (base_type);
17576 inner_array = base_type;
17577
17578 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17579 {
17580 TYPE_TARGET_TYPE (inner_array) =
17581 copy_type (TYPE_TARGET_TYPE (inner_array));
17582 inner_array = TYPE_TARGET_TYPE (inner_array);
17583 }
17584
17585 el_type = TYPE_TARGET_TYPE (inner_array);
17586 cnst |= TYPE_CONST (el_type);
17587 voltl |= TYPE_VOLATILE (el_type);
17588 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17589
17590 return set_die_type (die, base_type, cu);
17591 }
17592
17593 static struct type *
17594 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17595 {
17596 struct type *base_type, *cv_type;
17597
17598 base_type = die_type (die, cu);
17599
17600 /* The die_type call above may have already set the type for this DIE. */
17601 cv_type = get_die_type (die, cu);
17602 if (cv_type)
17603 return cv_type;
17604
17605 /* In case the const qualifier is applied to an array type, the element type
17606 is so qualified, not the array type (section 6.7.3 of C99). */
17607 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17608 return add_array_cv_type (die, cu, base_type, 1, 0);
17609
17610 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17611 return set_die_type (die, cv_type, cu);
17612 }
17613
17614 static struct type *
17615 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17616 {
17617 struct type *base_type, *cv_type;
17618
17619 base_type = die_type (die, cu);
17620
17621 /* The die_type call above may have already set the type for this DIE. */
17622 cv_type = get_die_type (die, cu);
17623 if (cv_type)
17624 return cv_type;
17625
17626 /* In case the volatile qualifier is applied to an array type, the
17627 element type is so qualified, not the array type (section 6.7.3
17628 of C99). */
17629 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17630 return add_array_cv_type (die, cu, base_type, 0, 1);
17631
17632 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17633 return set_die_type (die, cv_type, cu);
17634 }
17635
17636 /* Handle DW_TAG_restrict_type. */
17637
17638 static struct type *
17639 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17640 {
17641 struct type *base_type, *cv_type;
17642
17643 base_type = die_type (die, cu);
17644
17645 /* The die_type call above may have already set the type for this DIE. */
17646 cv_type = get_die_type (die, cu);
17647 if (cv_type)
17648 return cv_type;
17649
17650 cv_type = make_restrict_type (base_type);
17651 return set_die_type (die, cv_type, cu);
17652 }
17653
17654 /* Handle DW_TAG_atomic_type. */
17655
17656 static struct type *
17657 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17658 {
17659 struct type *base_type, *cv_type;
17660
17661 base_type = die_type (die, cu);
17662
17663 /* The die_type call above may have already set the type for this DIE. */
17664 cv_type = get_die_type (die, cu);
17665 if (cv_type)
17666 return cv_type;
17667
17668 cv_type = make_atomic_type (base_type);
17669 return set_die_type (die, cv_type, cu);
17670 }
17671
17672 /* Extract all information from a DW_TAG_string_type DIE and add to
17673 the user defined type vector. It isn't really a user defined type,
17674 but it behaves like one, with other DIE's using an AT_user_def_type
17675 attribute to reference it. */
17676
17677 static struct type *
17678 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17679 {
17680 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17681 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17682 struct type *type, *range_type, *index_type, *char_type;
17683 struct attribute *attr;
17684 unsigned int length;
17685
17686 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17687 if (attr)
17688 {
17689 length = DW_UNSND (attr);
17690 }
17691 else
17692 {
17693 /* Check for the DW_AT_byte_size attribute. */
17694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17695 if (attr)
17696 {
17697 length = DW_UNSND (attr);
17698 }
17699 else
17700 {
17701 length = 1;
17702 }
17703 }
17704
17705 index_type = objfile_type (objfile)->builtin_int;
17706 range_type = create_static_range_type (NULL, index_type, 1, length);
17707 char_type = language_string_char_type (cu->language_defn, gdbarch);
17708 type = create_string_type (NULL, char_type, range_type);
17709
17710 return set_die_type (die, type, cu);
17711 }
17712
17713 /* Assuming that DIE corresponds to a function, returns nonzero
17714 if the function is prototyped. */
17715
17716 static int
17717 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17718 {
17719 struct attribute *attr;
17720
17721 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17722 if (attr && (DW_UNSND (attr) != 0))
17723 return 1;
17724
17725 /* The DWARF standard implies that the DW_AT_prototyped attribute
17726 is only meaninful for C, but the concept also extends to other
17727 languages that allow unprototyped functions (Eg: Objective C).
17728 For all other languages, assume that functions are always
17729 prototyped. */
17730 if (cu->language != language_c
17731 && cu->language != language_objc
17732 && cu->language != language_opencl)
17733 return 1;
17734
17735 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17736 prototyped and unprototyped functions; default to prototyped,
17737 since that is more common in modern code (and RealView warns
17738 about unprototyped functions). */
17739 if (producer_is_realview (cu->producer))
17740 return 1;
17741
17742 return 0;
17743 }
17744
17745 /* Handle DIES due to C code like:
17746
17747 struct foo
17748 {
17749 int (*funcp)(int a, long l);
17750 int b;
17751 };
17752
17753 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17754
17755 static struct type *
17756 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17757 {
17758 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17759 struct type *type; /* Type that this function returns. */
17760 struct type *ftype; /* Function that returns above type. */
17761 struct attribute *attr;
17762
17763 type = die_type (die, cu);
17764
17765 /* The die_type call above may have already set the type for this DIE. */
17766 ftype = get_die_type (die, cu);
17767 if (ftype)
17768 return ftype;
17769
17770 ftype = lookup_function_type (type);
17771
17772 if (prototyped_function_p (die, cu))
17773 TYPE_PROTOTYPED (ftype) = 1;
17774
17775 /* Store the calling convention in the type if it's available in
17776 the subroutine die. Otherwise set the calling convention to
17777 the default value DW_CC_normal. */
17778 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17779 if (attr)
17780 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17781 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17782 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17783 else
17784 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17785
17786 /* Record whether the function returns normally to its caller or not
17787 if the DWARF producer set that information. */
17788 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17789 if (attr && (DW_UNSND (attr) != 0))
17790 TYPE_NO_RETURN (ftype) = 1;
17791
17792 /* We need to add the subroutine type to the die immediately so
17793 we don't infinitely recurse when dealing with parameters
17794 declared as the same subroutine type. */
17795 set_die_type (die, ftype, cu);
17796
17797 if (die->child != NULL)
17798 {
17799 struct type *void_type = objfile_type (objfile)->builtin_void;
17800 struct die_info *child_die;
17801 int nparams, iparams;
17802
17803 /* Count the number of parameters.
17804 FIXME: GDB currently ignores vararg functions, but knows about
17805 vararg member functions. */
17806 nparams = 0;
17807 child_die = die->child;
17808 while (child_die && child_die->tag)
17809 {
17810 if (child_die->tag == DW_TAG_formal_parameter)
17811 nparams++;
17812 else if (child_die->tag == DW_TAG_unspecified_parameters)
17813 TYPE_VARARGS (ftype) = 1;
17814 child_die = sibling_die (child_die);
17815 }
17816
17817 /* Allocate storage for parameters and fill them in. */
17818 TYPE_NFIELDS (ftype) = nparams;
17819 TYPE_FIELDS (ftype) = (struct field *)
17820 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17821
17822 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17823 even if we error out during the parameters reading below. */
17824 for (iparams = 0; iparams < nparams; iparams++)
17825 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17826
17827 iparams = 0;
17828 child_die = die->child;
17829 while (child_die && child_die->tag)
17830 {
17831 if (child_die->tag == DW_TAG_formal_parameter)
17832 {
17833 struct type *arg_type;
17834
17835 /* DWARF version 2 has no clean way to discern C++
17836 static and non-static member functions. G++ helps
17837 GDB by marking the first parameter for non-static
17838 member functions (which is the this pointer) as
17839 artificial. We pass this information to
17840 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17841
17842 DWARF version 3 added DW_AT_object_pointer, which GCC
17843 4.5 does not yet generate. */
17844 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17845 if (attr)
17846 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17847 else
17848 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17849 arg_type = die_type (child_die, cu);
17850
17851 /* RealView does not mark THIS as const, which the testsuite
17852 expects. GCC marks THIS as const in method definitions,
17853 but not in the class specifications (GCC PR 43053). */
17854 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17855 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17856 {
17857 int is_this = 0;
17858 struct dwarf2_cu *arg_cu = cu;
17859 const char *name = dwarf2_name (child_die, cu);
17860
17861 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17862 if (attr)
17863 {
17864 /* If the compiler emits this, use it. */
17865 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17866 is_this = 1;
17867 }
17868 else if (name && strcmp (name, "this") == 0)
17869 /* Function definitions will have the argument names. */
17870 is_this = 1;
17871 else if (name == NULL && iparams == 0)
17872 /* Declarations may not have the names, so like
17873 elsewhere in GDB, assume an artificial first
17874 argument is "this". */
17875 is_this = 1;
17876
17877 if (is_this)
17878 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17879 arg_type, 0);
17880 }
17881
17882 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17883 iparams++;
17884 }
17885 child_die = sibling_die (child_die);
17886 }
17887 }
17888
17889 return ftype;
17890 }
17891
17892 static struct type *
17893 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17894 {
17895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17896 const char *name = NULL;
17897 struct type *this_type, *target_type;
17898
17899 name = dwarf2_full_name (NULL, die, cu);
17900 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17901 TYPE_TARGET_STUB (this_type) = 1;
17902 set_die_type (die, this_type, cu);
17903 target_type = die_type (die, cu);
17904 if (target_type != this_type)
17905 TYPE_TARGET_TYPE (this_type) = target_type;
17906 else
17907 {
17908 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17909 spec and cause infinite loops in GDB. */
17910 complaint (&symfile_complaints,
17911 _("Self-referential DW_TAG_typedef "
17912 "- DIE at %s [in module %s]"),
17913 sect_offset_str (die->sect_off), objfile_name (objfile));
17914 TYPE_TARGET_TYPE (this_type) = NULL;
17915 }
17916 return this_type;
17917 }
17918
17919 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17920 (which may be different from NAME) to the architecture back-end to allow
17921 it to guess the correct format if necessary. */
17922
17923 static struct type *
17924 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17925 const char *name_hint)
17926 {
17927 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17928 const struct floatformat **format;
17929 struct type *type;
17930
17931 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17932 if (format)
17933 type = init_float_type (objfile, bits, name, format);
17934 else
17935 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17936
17937 return type;
17938 }
17939
17940 /* Find a representation of a given base type and install
17941 it in the TYPE field of the die. */
17942
17943 static struct type *
17944 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17945 {
17946 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17947 struct type *type;
17948 struct attribute *attr;
17949 int encoding = 0, bits = 0;
17950 const char *name;
17951
17952 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17953 if (attr)
17954 {
17955 encoding = DW_UNSND (attr);
17956 }
17957 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17958 if (attr)
17959 {
17960 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17961 }
17962 name = dwarf2_name (die, cu);
17963 if (!name)
17964 {
17965 complaint (&symfile_complaints,
17966 _("DW_AT_name missing from DW_TAG_base_type"));
17967 }
17968
17969 switch (encoding)
17970 {
17971 case DW_ATE_address:
17972 /* Turn DW_ATE_address into a void * pointer. */
17973 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17974 type = init_pointer_type (objfile, bits, name, type);
17975 break;
17976 case DW_ATE_boolean:
17977 type = init_boolean_type (objfile, bits, 1, name);
17978 break;
17979 case DW_ATE_complex_float:
17980 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17981 type = init_complex_type (objfile, name, type);
17982 break;
17983 case DW_ATE_decimal_float:
17984 type = init_decfloat_type (objfile, bits, name);
17985 break;
17986 case DW_ATE_float:
17987 type = dwarf2_init_float_type (objfile, bits, name, name);
17988 break;
17989 case DW_ATE_signed:
17990 type = init_integer_type (objfile, bits, 0, name);
17991 break;
17992 case DW_ATE_unsigned:
17993 if (cu->language == language_fortran
17994 && name
17995 && startswith (name, "character("))
17996 type = init_character_type (objfile, bits, 1, name);
17997 else
17998 type = init_integer_type (objfile, bits, 1, name);
17999 break;
18000 case DW_ATE_signed_char:
18001 if (cu->language == language_ada || cu->language == language_m2
18002 || cu->language == language_pascal
18003 || cu->language == language_fortran)
18004 type = init_character_type (objfile, bits, 0, name);
18005 else
18006 type = init_integer_type (objfile, bits, 0, name);
18007 break;
18008 case DW_ATE_unsigned_char:
18009 if (cu->language == language_ada || cu->language == language_m2
18010 || cu->language == language_pascal
18011 || cu->language == language_fortran
18012 || cu->language == language_rust)
18013 type = init_character_type (objfile, bits, 1, name);
18014 else
18015 type = init_integer_type (objfile, bits, 1, name);
18016 break;
18017 case DW_ATE_UTF:
18018 {
18019 gdbarch *arch = get_objfile_arch (objfile);
18020
18021 if (bits == 16)
18022 type = builtin_type (arch)->builtin_char16;
18023 else if (bits == 32)
18024 type = builtin_type (arch)->builtin_char32;
18025 else
18026 {
18027 complaint (&symfile_complaints,
18028 _("unsupported DW_ATE_UTF bit size: '%d'"),
18029 bits);
18030 type = init_integer_type (objfile, bits, 1, name);
18031 }
18032 return set_die_type (die, type, cu);
18033 }
18034 break;
18035
18036 default:
18037 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
18038 dwarf_type_encoding_name (encoding));
18039 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18040 break;
18041 }
18042
18043 if (name && strcmp (name, "char") == 0)
18044 TYPE_NOSIGN (type) = 1;
18045
18046 return set_die_type (die, type, cu);
18047 }
18048
18049 /* Parse dwarf attribute if it's a block, reference or constant and put the
18050 resulting value of the attribute into struct bound_prop.
18051 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18052
18053 static int
18054 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18055 struct dwarf2_cu *cu, struct dynamic_prop *prop)
18056 {
18057 struct dwarf2_property_baton *baton;
18058 struct obstack *obstack
18059 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
18060
18061 if (attr == NULL || prop == NULL)
18062 return 0;
18063
18064 if (attr_form_is_block (attr))
18065 {
18066 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18067 baton->referenced_type = NULL;
18068 baton->locexpr.per_cu = cu->per_cu;
18069 baton->locexpr.size = DW_BLOCK (attr)->size;
18070 baton->locexpr.data = DW_BLOCK (attr)->data;
18071 prop->data.baton = baton;
18072 prop->kind = PROP_LOCEXPR;
18073 gdb_assert (prop->data.baton != NULL);
18074 }
18075 else if (attr_form_is_ref (attr))
18076 {
18077 struct dwarf2_cu *target_cu = cu;
18078 struct die_info *target_die;
18079 struct attribute *target_attr;
18080
18081 target_die = follow_die_ref (die, attr, &target_cu);
18082 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18083 if (target_attr == NULL)
18084 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18085 target_cu);
18086 if (target_attr == NULL)
18087 return 0;
18088
18089 switch (target_attr->name)
18090 {
18091 case DW_AT_location:
18092 if (attr_form_is_section_offset (target_attr))
18093 {
18094 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18095 baton->referenced_type = die_type (target_die, target_cu);
18096 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18097 prop->data.baton = baton;
18098 prop->kind = PROP_LOCLIST;
18099 gdb_assert (prop->data.baton != NULL);
18100 }
18101 else if (attr_form_is_block (target_attr))
18102 {
18103 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18104 baton->referenced_type = die_type (target_die, target_cu);
18105 baton->locexpr.per_cu = cu->per_cu;
18106 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18107 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18108 prop->data.baton = baton;
18109 prop->kind = PROP_LOCEXPR;
18110 gdb_assert (prop->data.baton != NULL);
18111 }
18112 else
18113 {
18114 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18115 "dynamic property");
18116 return 0;
18117 }
18118 break;
18119 case DW_AT_data_member_location:
18120 {
18121 LONGEST offset;
18122
18123 if (!handle_data_member_location (target_die, target_cu,
18124 &offset))
18125 return 0;
18126
18127 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18128 baton->referenced_type = read_type_die (target_die->parent,
18129 target_cu);
18130 baton->offset_info.offset = offset;
18131 baton->offset_info.type = die_type (target_die, target_cu);
18132 prop->data.baton = baton;
18133 prop->kind = PROP_ADDR_OFFSET;
18134 break;
18135 }
18136 }
18137 }
18138 else if (attr_form_is_constant (attr))
18139 {
18140 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18141 prop->kind = PROP_CONST;
18142 }
18143 else
18144 {
18145 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18146 dwarf2_name (die, cu));
18147 return 0;
18148 }
18149
18150 return 1;
18151 }
18152
18153 /* Read the given DW_AT_subrange DIE. */
18154
18155 static struct type *
18156 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18157 {
18158 struct type *base_type, *orig_base_type;
18159 struct type *range_type;
18160 struct attribute *attr;
18161 struct dynamic_prop low, high;
18162 int low_default_is_valid;
18163 int high_bound_is_count = 0;
18164 const char *name;
18165 LONGEST negative_mask;
18166
18167 orig_base_type = die_type (die, cu);
18168 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18169 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18170 creating the range type, but we use the result of check_typedef
18171 when examining properties of the type. */
18172 base_type = check_typedef (orig_base_type);
18173
18174 /* The die_type call above may have already set the type for this DIE. */
18175 range_type = get_die_type (die, cu);
18176 if (range_type)
18177 return range_type;
18178
18179 low.kind = PROP_CONST;
18180 high.kind = PROP_CONST;
18181 high.data.const_val = 0;
18182
18183 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18184 omitting DW_AT_lower_bound. */
18185 switch (cu->language)
18186 {
18187 case language_c:
18188 case language_cplus:
18189 low.data.const_val = 0;
18190 low_default_is_valid = 1;
18191 break;
18192 case language_fortran:
18193 low.data.const_val = 1;
18194 low_default_is_valid = 1;
18195 break;
18196 case language_d:
18197 case language_objc:
18198 case language_rust:
18199 low.data.const_val = 0;
18200 low_default_is_valid = (cu->header.version >= 4);
18201 break;
18202 case language_ada:
18203 case language_m2:
18204 case language_pascal:
18205 low.data.const_val = 1;
18206 low_default_is_valid = (cu->header.version >= 4);
18207 break;
18208 default:
18209 low.data.const_val = 0;
18210 low_default_is_valid = 0;
18211 break;
18212 }
18213
18214 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18215 if (attr)
18216 attr_to_dynamic_prop (attr, die, cu, &low);
18217 else if (!low_default_is_valid)
18218 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
18219 "- DIE at %s [in module %s]"),
18220 sect_offset_str (die->sect_off),
18221 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18222
18223 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
18224 if (!attr_to_dynamic_prop (attr, die, cu, &high))
18225 {
18226 attr = dwarf2_attr (die, DW_AT_count, cu);
18227 if (attr_to_dynamic_prop (attr, die, cu, &high))
18228 {
18229 /* If bounds are constant do the final calculation here. */
18230 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18231 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18232 else
18233 high_bound_is_count = 1;
18234 }
18235 }
18236
18237 /* Dwarf-2 specifications explicitly allows to create subrange types
18238 without specifying a base type.
18239 In that case, the base type must be set to the type of
18240 the lower bound, upper bound or count, in that order, if any of these
18241 three attributes references an object that has a type.
18242 If no base type is found, the Dwarf-2 specifications say that
18243 a signed integer type of size equal to the size of an address should
18244 be used.
18245 For the following C code: `extern char gdb_int [];'
18246 GCC produces an empty range DIE.
18247 FIXME: muller/2010-05-28: Possible references to object for low bound,
18248 high bound or count are not yet handled by this code. */
18249 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
18250 {
18251 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18252 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18253 int addr_size = gdbarch_addr_bit (gdbarch) /8;
18254 struct type *int_type = objfile_type (objfile)->builtin_int;
18255
18256 /* Test "int", "long int", and "long long int" objfile types,
18257 and select the first one having a size above or equal to the
18258 architecture address size. */
18259 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18260 base_type = int_type;
18261 else
18262 {
18263 int_type = objfile_type (objfile)->builtin_long;
18264 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18265 base_type = int_type;
18266 else
18267 {
18268 int_type = objfile_type (objfile)->builtin_long_long;
18269 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
18270 base_type = int_type;
18271 }
18272 }
18273 }
18274
18275 /* Normally, the DWARF producers are expected to use a signed
18276 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18277 But this is unfortunately not always the case, as witnessed
18278 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18279 is used instead. To work around that ambiguity, we treat
18280 the bounds as signed, and thus sign-extend their values, when
18281 the base type is signed. */
18282 negative_mask =
18283 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18284 if (low.kind == PROP_CONST
18285 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18286 low.data.const_val |= negative_mask;
18287 if (high.kind == PROP_CONST
18288 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18289 high.data.const_val |= negative_mask;
18290
18291 range_type = create_range_type (NULL, orig_base_type, &low, &high);
18292
18293 if (high_bound_is_count)
18294 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18295
18296 /* Ada expects an empty array on no boundary attributes. */
18297 if (attr == NULL && cu->language != language_ada)
18298 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18299
18300 name = dwarf2_name (die, cu);
18301 if (name)
18302 TYPE_NAME (range_type) = name;
18303
18304 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18305 if (attr)
18306 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18307
18308 set_die_type (die, range_type, cu);
18309
18310 /* set_die_type should be already done. */
18311 set_descriptive_type (range_type, die, cu);
18312
18313 return range_type;
18314 }
18315
18316 static struct type *
18317 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18318 {
18319 struct type *type;
18320
18321 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18322 NULL);
18323 TYPE_NAME (type) = dwarf2_name (die, cu);
18324
18325 /* In Ada, an unspecified type is typically used when the description
18326 of the type is defered to a different unit. When encountering
18327 such a type, we treat it as a stub, and try to resolve it later on,
18328 when needed. */
18329 if (cu->language == language_ada)
18330 TYPE_STUB (type) = 1;
18331
18332 return set_die_type (die, type, cu);
18333 }
18334
18335 /* Read a single die and all its descendents. Set the die's sibling
18336 field to NULL; set other fields in the die correctly, and set all
18337 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18338 location of the info_ptr after reading all of those dies. PARENT
18339 is the parent of the die in question. */
18340
18341 static struct die_info *
18342 read_die_and_children (const struct die_reader_specs *reader,
18343 const gdb_byte *info_ptr,
18344 const gdb_byte **new_info_ptr,
18345 struct die_info *parent)
18346 {
18347 struct die_info *die;
18348 const gdb_byte *cur_ptr;
18349 int has_children;
18350
18351 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18352 if (die == NULL)
18353 {
18354 *new_info_ptr = cur_ptr;
18355 return NULL;
18356 }
18357 store_in_ref_table (die, reader->cu);
18358
18359 if (has_children)
18360 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18361 else
18362 {
18363 die->child = NULL;
18364 *new_info_ptr = cur_ptr;
18365 }
18366
18367 die->sibling = NULL;
18368 die->parent = parent;
18369 return die;
18370 }
18371
18372 /* Read a die, all of its descendents, and all of its siblings; set
18373 all of the fields of all of the dies correctly. Arguments are as
18374 in read_die_and_children. */
18375
18376 static struct die_info *
18377 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18378 const gdb_byte *info_ptr,
18379 const gdb_byte **new_info_ptr,
18380 struct die_info *parent)
18381 {
18382 struct die_info *first_die, *last_sibling;
18383 const gdb_byte *cur_ptr;
18384
18385 cur_ptr = info_ptr;
18386 first_die = last_sibling = NULL;
18387
18388 while (1)
18389 {
18390 struct die_info *die
18391 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18392
18393 if (die == NULL)
18394 {
18395 *new_info_ptr = cur_ptr;
18396 return first_die;
18397 }
18398
18399 if (!first_die)
18400 first_die = die;
18401 else
18402 last_sibling->sibling = die;
18403
18404 last_sibling = die;
18405 }
18406 }
18407
18408 /* Read a die, all of its descendents, and all of its siblings; set
18409 all of the fields of all of the dies correctly. Arguments are as
18410 in read_die_and_children.
18411 This the main entry point for reading a DIE and all its children. */
18412
18413 static struct die_info *
18414 read_die_and_siblings (const struct die_reader_specs *reader,
18415 const gdb_byte *info_ptr,
18416 const gdb_byte **new_info_ptr,
18417 struct die_info *parent)
18418 {
18419 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18420 new_info_ptr, parent);
18421
18422 if (dwarf_die_debug)
18423 {
18424 fprintf_unfiltered (gdb_stdlog,
18425 "Read die from %s@0x%x of %s:\n",
18426 get_section_name (reader->die_section),
18427 (unsigned) (info_ptr - reader->die_section->buffer),
18428 bfd_get_filename (reader->abfd));
18429 dump_die (die, dwarf_die_debug);
18430 }
18431
18432 return die;
18433 }
18434
18435 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18436 attributes.
18437 The caller is responsible for filling in the extra attributes
18438 and updating (*DIEP)->num_attrs.
18439 Set DIEP to point to a newly allocated die with its information,
18440 except for its child, sibling, and parent fields.
18441 Set HAS_CHILDREN to tell whether the die has children or not. */
18442
18443 static const gdb_byte *
18444 read_full_die_1 (const struct die_reader_specs *reader,
18445 struct die_info **diep, const gdb_byte *info_ptr,
18446 int *has_children, int num_extra_attrs)
18447 {
18448 unsigned int abbrev_number, bytes_read, i;
18449 struct abbrev_info *abbrev;
18450 struct die_info *die;
18451 struct dwarf2_cu *cu = reader->cu;
18452 bfd *abfd = reader->abfd;
18453
18454 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18455 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18456 info_ptr += bytes_read;
18457 if (!abbrev_number)
18458 {
18459 *diep = NULL;
18460 *has_children = 0;
18461 return info_ptr;
18462 }
18463
18464 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18465 if (!abbrev)
18466 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18467 abbrev_number,
18468 bfd_get_filename (abfd));
18469
18470 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18471 die->sect_off = sect_off;
18472 die->tag = abbrev->tag;
18473 die->abbrev = abbrev_number;
18474
18475 /* Make the result usable.
18476 The caller needs to update num_attrs after adding the extra
18477 attributes. */
18478 die->num_attrs = abbrev->num_attrs;
18479
18480 for (i = 0; i < abbrev->num_attrs; ++i)
18481 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18482 info_ptr);
18483
18484 *diep = die;
18485 *has_children = abbrev->has_children;
18486 return info_ptr;
18487 }
18488
18489 /* Read a die and all its attributes.
18490 Set DIEP to point to a newly allocated die with its information,
18491 except for its child, sibling, and parent fields.
18492 Set HAS_CHILDREN to tell whether the die has children or not. */
18493
18494 static const gdb_byte *
18495 read_full_die (const struct die_reader_specs *reader,
18496 struct die_info **diep, const gdb_byte *info_ptr,
18497 int *has_children)
18498 {
18499 const gdb_byte *result;
18500
18501 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18502
18503 if (dwarf_die_debug)
18504 {
18505 fprintf_unfiltered (gdb_stdlog,
18506 "Read die from %s@0x%x of %s:\n",
18507 get_section_name (reader->die_section),
18508 (unsigned) (info_ptr - reader->die_section->buffer),
18509 bfd_get_filename (reader->abfd));
18510 dump_die (*diep, dwarf_die_debug);
18511 }
18512
18513 return result;
18514 }
18515 \f
18516 /* Abbreviation tables.
18517
18518 In DWARF version 2, the description of the debugging information is
18519 stored in a separate .debug_abbrev section. Before we read any
18520 dies from a section we read in all abbreviations and install them
18521 in a hash table. */
18522
18523 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18524
18525 struct abbrev_info *
18526 abbrev_table::alloc_abbrev ()
18527 {
18528 struct abbrev_info *abbrev;
18529
18530 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18531 memset (abbrev, 0, sizeof (struct abbrev_info));
18532
18533 return abbrev;
18534 }
18535
18536 /* Add an abbreviation to the table. */
18537
18538 void
18539 abbrev_table::add_abbrev (unsigned int abbrev_number,
18540 struct abbrev_info *abbrev)
18541 {
18542 unsigned int hash_number;
18543
18544 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18545 abbrev->next = m_abbrevs[hash_number];
18546 m_abbrevs[hash_number] = abbrev;
18547 }
18548
18549 /* Look up an abbrev in the table.
18550 Returns NULL if the abbrev is not found. */
18551
18552 struct abbrev_info *
18553 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18554 {
18555 unsigned int hash_number;
18556 struct abbrev_info *abbrev;
18557
18558 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18559 abbrev = m_abbrevs[hash_number];
18560
18561 while (abbrev)
18562 {
18563 if (abbrev->number == abbrev_number)
18564 return abbrev;
18565 abbrev = abbrev->next;
18566 }
18567 return NULL;
18568 }
18569
18570 /* Read in an abbrev table. */
18571
18572 static abbrev_table_up
18573 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18574 struct dwarf2_section_info *section,
18575 sect_offset sect_off)
18576 {
18577 struct objfile *objfile = dwarf2_per_objfile->objfile;
18578 bfd *abfd = get_section_bfd_owner (section);
18579 const gdb_byte *abbrev_ptr;
18580 struct abbrev_info *cur_abbrev;
18581 unsigned int abbrev_number, bytes_read, abbrev_name;
18582 unsigned int abbrev_form;
18583 struct attr_abbrev *cur_attrs;
18584 unsigned int allocated_attrs;
18585
18586 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18587
18588 dwarf2_read_section (objfile, section);
18589 abbrev_ptr = section->buffer + to_underlying (sect_off);
18590 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18591 abbrev_ptr += bytes_read;
18592
18593 allocated_attrs = ATTR_ALLOC_CHUNK;
18594 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18595
18596 /* Loop until we reach an abbrev number of 0. */
18597 while (abbrev_number)
18598 {
18599 cur_abbrev = abbrev_table->alloc_abbrev ();
18600
18601 /* read in abbrev header */
18602 cur_abbrev->number = abbrev_number;
18603 cur_abbrev->tag
18604 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18605 abbrev_ptr += bytes_read;
18606 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18607 abbrev_ptr += 1;
18608
18609 /* now read in declarations */
18610 for (;;)
18611 {
18612 LONGEST implicit_const;
18613
18614 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18615 abbrev_ptr += bytes_read;
18616 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18617 abbrev_ptr += bytes_read;
18618 if (abbrev_form == DW_FORM_implicit_const)
18619 {
18620 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18621 &bytes_read);
18622 abbrev_ptr += bytes_read;
18623 }
18624 else
18625 {
18626 /* Initialize it due to a false compiler warning. */
18627 implicit_const = -1;
18628 }
18629
18630 if (abbrev_name == 0)
18631 break;
18632
18633 if (cur_abbrev->num_attrs == allocated_attrs)
18634 {
18635 allocated_attrs += ATTR_ALLOC_CHUNK;
18636 cur_attrs
18637 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18638 }
18639
18640 cur_attrs[cur_abbrev->num_attrs].name
18641 = (enum dwarf_attribute) abbrev_name;
18642 cur_attrs[cur_abbrev->num_attrs].form
18643 = (enum dwarf_form) abbrev_form;
18644 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18645 ++cur_abbrev->num_attrs;
18646 }
18647
18648 cur_abbrev->attrs =
18649 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18650 cur_abbrev->num_attrs);
18651 memcpy (cur_abbrev->attrs, cur_attrs,
18652 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18653
18654 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18655
18656 /* Get next abbreviation.
18657 Under Irix6 the abbreviations for a compilation unit are not
18658 always properly terminated with an abbrev number of 0.
18659 Exit loop if we encounter an abbreviation which we have
18660 already read (which means we are about to read the abbreviations
18661 for the next compile unit) or if the end of the abbreviation
18662 table is reached. */
18663 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18664 break;
18665 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18666 abbrev_ptr += bytes_read;
18667 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18668 break;
18669 }
18670
18671 xfree (cur_attrs);
18672 return abbrev_table;
18673 }
18674
18675 /* Returns nonzero if TAG represents a type that we might generate a partial
18676 symbol for. */
18677
18678 static int
18679 is_type_tag_for_partial (int tag)
18680 {
18681 switch (tag)
18682 {
18683 #if 0
18684 /* Some types that would be reasonable to generate partial symbols for,
18685 that we don't at present. */
18686 case DW_TAG_array_type:
18687 case DW_TAG_file_type:
18688 case DW_TAG_ptr_to_member_type:
18689 case DW_TAG_set_type:
18690 case DW_TAG_string_type:
18691 case DW_TAG_subroutine_type:
18692 #endif
18693 case DW_TAG_base_type:
18694 case DW_TAG_class_type:
18695 case DW_TAG_interface_type:
18696 case DW_TAG_enumeration_type:
18697 case DW_TAG_structure_type:
18698 case DW_TAG_subrange_type:
18699 case DW_TAG_typedef:
18700 case DW_TAG_union_type:
18701 return 1;
18702 default:
18703 return 0;
18704 }
18705 }
18706
18707 /* Load all DIEs that are interesting for partial symbols into memory. */
18708
18709 static struct partial_die_info *
18710 load_partial_dies (const struct die_reader_specs *reader,
18711 const gdb_byte *info_ptr, int building_psymtab)
18712 {
18713 struct dwarf2_cu *cu = reader->cu;
18714 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18715 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18716 unsigned int bytes_read;
18717 unsigned int load_all = 0;
18718 int nesting_level = 1;
18719
18720 parent_die = NULL;
18721 last_die = NULL;
18722
18723 gdb_assert (cu->per_cu != NULL);
18724 if (cu->per_cu->load_all_dies)
18725 load_all = 1;
18726
18727 cu->partial_dies
18728 = htab_create_alloc_ex (cu->header.length / 12,
18729 partial_die_hash,
18730 partial_die_eq,
18731 NULL,
18732 &cu->comp_unit_obstack,
18733 hashtab_obstack_allocate,
18734 dummy_obstack_deallocate);
18735
18736 while (1)
18737 {
18738 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18739
18740 /* A NULL abbrev means the end of a series of children. */
18741 if (abbrev == NULL)
18742 {
18743 if (--nesting_level == 0)
18744 return first_die;
18745
18746 info_ptr += bytes_read;
18747 last_die = parent_die;
18748 parent_die = parent_die->die_parent;
18749 continue;
18750 }
18751
18752 /* Check for template arguments. We never save these; if
18753 they're seen, we just mark the parent, and go on our way. */
18754 if (parent_die != NULL
18755 && cu->language == language_cplus
18756 && (abbrev->tag == DW_TAG_template_type_param
18757 || abbrev->tag == DW_TAG_template_value_param))
18758 {
18759 parent_die->has_template_arguments = 1;
18760
18761 if (!load_all)
18762 {
18763 /* We don't need a partial DIE for the template argument. */
18764 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18765 continue;
18766 }
18767 }
18768
18769 /* We only recurse into c++ subprograms looking for template arguments.
18770 Skip their other children. */
18771 if (!load_all
18772 && cu->language == language_cplus
18773 && parent_die != NULL
18774 && parent_die->tag == DW_TAG_subprogram)
18775 {
18776 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18777 continue;
18778 }
18779
18780 /* Check whether this DIE is interesting enough to save. Normally
18781 we would not be interested in members here, but there may be
18782 later variables referencing them via DW_AT_specification (for
18783 static members). */
18784 if (!load_all
18785 && !is_type_tag_for_partial (abbrev->tag)
18786 && abbrev->tag != DW_TAG_constant
18787 && abbrev->tag != DW_TAG_enumerator
18788 && abbrev->tag != DW_TAG_subprogram
18789 && abbrev->tag != DW_TAG_inlined_subroutine
18790 && abbrev->tag != DW_TAG_lexical_block
18791 && abbrev->tag != DW_TAG_variable
18792 && abbrev->tag != DW_TAG_namespace
18793 && abbrev->tag != DW_TAG_module
18794 && abbrev->tag != DW_TAG_member
18795 && abbrev->tag != DW_TAG_imported_unit
18796 && abbrev->tag != DW_TAG_imported_declaration)
18797 {
18798 /* Otherwise we skip to the next sibling, if any. */
18799 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18800 continue;
18801 }
18802
18803 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18804 abbrev);
18805
18806 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18807
18808 /* This two-pass algorithm for processing partial symbols has a
18809 high cost in cache pressure. Thus, handle some simple cases
18810 here which cover the majority of C partial symbols. DIEs
18811 which neither have specification tags in them, nor could have
18812 specification tags elsewhere pointing at them, can simply be
18813 processed and discarded.
18814
18815 This segment is also optional; scan_partial_symbols and
18816 add_partial_symbol will handle these DIEs if we chain
18817 them in normally. When compilers which do not emit large
18818 quantities of duplicate debug information are more common,
18819 this code can probably be removed. */
18820
18821 /* Any complete simple types at the top level (pretty much all
18822 of them, for a language without namespaces), can be processed
18823 directly. */
18824 if (parent_die == NULL
18825 && pdi.has_specification == 0
18826 && pdi.is_declaration == 0
18827 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18828 || pdi.tag == DW_TAG_base_type
18829 || pdi.tag == DW_TAG_subrange_type))
18830 {
18831 if (building_psymtab && pdi.name != NULL)
18832 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18833 VAR_DOMAIN, LOC_TYPEDEF,
18834 &objfile->static_psymbols,
18835 0, cu->language, objfile);
18836 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18837 continue;
18838 }
18839
18840 /* The exception for DW_TAG_typedef with has_children above is
18841 a workaround of GCC PR debug/47510. In the case of this complaint
18842 type_name_no_tag_or_error will error on such types later.
18843
18844 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18845 it could not find the child DIEs referenced later, this is checked
18846 above. In correct DWARF DW_TAG_typedef should have no children. */
18847
18848 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18849 complaint (&symfile_complaints,
18850 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18851 "- DIE at %s [in module %s]"),
18852 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18853
18854 /* If we're at the second level, and we're an enumerator, and
18855 our parent has no specification (meaning possibly lives in a
18856 namespace elsewhere), then we can add the partial symbol now
18857 instead of queueing it. */
18858 if (pdi.tag == DW_TAG_enumerator
18859 && parent_die != NULL
18860 && parent_die->die_parent == NULL
18861 && parent_die->tag == DW_TAG_enumeration_type
18862 && parent_die->has_specification == 0)
18863 {
18864 if (pdi.name == NULL)
18865 complaint (&symfile_complaints,
18866 _("malformed enumerator DIE ignored"));
18867 else if (building_psymtab)
18868 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18869 VAR_DOMAIN, LOC_CONST,
18870 cu->language == language_cplus
18871 ? &objfile->global_psymbols
18872 : &objfile->static_psymbols,
18873 0, cu->language, objfile);
18874
18875 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18876 continue;
18877 }
18878
18879 struct partial_die_info *part_die
18880 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18881
18882 /* We'll save this DIE so link it in. */
18883 part_die->die_parent = parent_die;
18884 part_die->die_sibling = NULL;
18885 part_die->die_child = NULL;
18886
18887 if (last_die && last_die == parent_die)
18888 last_die->die_child = part_die;
18889 else if (last_die)
18890 last_die->die_sibling = part_die;
18891
18892 last_die = part_die;
18893
18894 if (first_die == NULL)
18895 first_die = part_die;
18896
18897 /* Maybe add the DIE to the hash table. Not all DIEs that we
18898 find interesting need to be in the hash table, because we
18899 also have the parent/sibling/child chains; only those that we
18900 might refer to by offset later during partial symbol reading.
18901
18902 For now this means things that might have be the target of a
18903 DW_AT_specification, DW_AT_abstract_origin, or
18904 DW_AT_extension. DW_AT_extension will refer only to
18905 namespaces; DW_AT_abstract_origin refers to functions (and
18906 many things under the function DIE, but we do not recurse
18907 into function DIEs during partial symbol reading) and
18908 possibly variables as well; DW_AT_specification refers to
18909 declarations. Declarations ought to have the DW_AT_declaration
18910 flag. It happens that GCC forgets to put it in sometimes, but
18911 only for functions, not for types.
18912
18913 Adding more things than necessary to the hash table is harmless
18914 except for the performance cost. Adding too few will result in
18915 wasted time in find_partial_die, when we reread the compilation
18916 unit with load_all_dies set. */
18917
18918 if (load_all
18919 || abbrev->tag == DW_TAG_constant
18920 || abbrev->tag == DW_TAG_subprogram
18921 || abbrev->tag == DW_TAG_variable
18922 || abbrev->tag == DW_TAG_namespace
18923 || part_die->is_declaration)
18924 {
18925 void **slot;
18926
18927 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18928 to_underlying (part_die->sect_off),
18929 INSERT);
18930 *slot = part_die;
18931 }
18932
18933 /* For some DIEs we want to follow their children (if any). For C
18934 we have no reason to follow the children of structures; for other
18935 languages we have to, so that we can get at method physnames
18936 to infer fully qualified class names, for DW_AT_specification,
18937 and for C++ template arguments. For C++, we also look one level
18938 inside functions to find template arguments (if the name of the
18939 function does not already contain the template arguments).
18940
18941 For Ada, we need to scan the children of subprograms and lexical
18942 blocks as well because Ada allows the definition of nested
18943 entities that could be interesting for the debugger, such as
18944 nested subprograms for instance. */
18945 if (last_die->has_children
18946 && (load_all
18947 || last_die->tag == DW_TAG_namespace
18948 || last_die->tag == DW_TAG_module
18949 || last_die->tag == DW_TAG_enumeration_type
18950 || (cu->language == language_cplus
18951 && last_die->tag == DW_TAG_subprogram
18952 && (last_die->name == NULL
18953 || strchr (last_die->name, '<') == NULL))
18954 || (cu->language != language_c
18955 && (last_die->tag == DW_TAG_class_type
18956 || last_die->tag == DW_TAG_interface_type
18957 || last_die->tag == DW_TAG_structure_type
18958 || last_die->tag == DW_TAG_union_type))
18959 || (cu->language == language_ada
18960 && (last_die->tag == DW_TAG_subprogram
18961 || last_die->tag == DW_TAG_lexical_block))))
18962 {
18963 nesting_level++;
18964 parent_die = last_die;
18965 continue;
18966 }
18967
18968 /* Otherwise we skip to the next sibling, if any. */
18969 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18970
18971 /* Back to the top, do it again. */
18972 }
18973 }
18974
18975 partial_die_info::partial_die_info (sect_offset sect_off_,
18976 struct abbrev_info *abbrev)
18977 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18978 {
18979 }
18980
18981 /* Read a minimal amount of information into the minimal die structure.
18982 INFO_PTR should point just after the initial uleb128 of a DIE. */
18983
18984 const gdb_byte *
18985 partial_die_info::read (const struct die_reader_specs *reader,
18986 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18987 {
18988 struct dwarf2_cu *cu = reader->cu;
18989 struct dwarf2_per_objfile *dwarf2_per_objfile
18990 = cu->per_cu->dwarf2_per_objfile;
18991 unsigned int i;
18992 int has_low_pc_attr = 0;
18993 int has_high_pc_attr = 0;
18994 int high_pc_relative = 0;
18995
18996 for (i = 0; i < abbrev.num_attrs; ++i)
18997 {
18998 struct attribute attr;
18999
19000 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19001
19002 /* Store the data if it is of an attribute we want to keep in a
19003 partial symbol table. */
19004 switch (attr.name)
19005 {
19006 case DW_AT_name:
19007 switch (tag)
19008 {
19009 case DW_TAG_compile_unit:
19010 case DW_TAG_partial_unit:
19011 case DW_TAG_type_unit:
19012 /* Compilation units have a DW_AT_name that is a filename, not
19013 a source language identifier. */
19014 case DW_TAG_enumeration_type:
19015 case DW_TAG_enumerator:
19016 /* These tags always have simple identifiers already; no need
19017 to canonicalize them. */
19018 name = DW_STRING (&attr);
19019 break;
19020 default:
19021 {
19022 struct objfile *objfile = dwarf2_per_objfile->objfile;
19023
19024 name
19025 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
19026 &objfile->per_bfd->storage_obstack);
19027 }
19028 break;
19029 }
19030 break;
19031 case DW_AT_linkage_name:
19032 case DW_AT_MIPS_linkage_name:
19033 /* Note that both forms of linkage name might appear. We
19034 assume they will be the same, and we only store the last
19035 one we see. */
19036 if (cu->language == language_ada)
19037 name = DW_STRING (&attr);
19038 linkage_name = DW_STRING (&attr);
19039 break;
19040 case DW_AT_low_pc:
19041 has_low_pc_attr = 1;
19042 lowpc = attr_value_as_address (&attr);
19043 break;
19044 case DW_AT_high_pc:
19045 has_high_pc_attr = 1;
19046 highpc = attr_value_as_address (&attr);
19047 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19048 high_pc_relative = 1;
19049 break;
19050 case DW_AT_location:
19051 /* Support the .debug_loc offsets. */
19052 if (attr_form_is_block (&attr))
19053 {
19054 d.locdesc = DW_BLOCK (&attr);
19055 }
19056 else if (attr_form_is_section_offset (&attr))
19057 {
19058 dwarf2_complex_location_expr_complaint ();
19059 }
19060 else
19061 {
19062 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19063 "partial symbol information");
19064 }
19065 break;
19066 case DW_AT_external:
19067 is_external = DW_UNSND (&attr);
19068 break;
19069 case DW_AT_declaration:
19070 is_declaration = DW_UNSND (&attr);
19071 break;
19072 case DW_AT_type:
19073 has_type = 1;
19074 break;
19075 case DW_AT_abstract_origin:
19076 case DW_AT_specification:
19077 case DW_AT_extension:
19078 has_specification = 1;
19079 spec_offset = dwarf2_get_ref_die_offset (&attr);
19080 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19081 || cu->per_cu->is_dwz);
19082 break;
19083 case DW_AT_sibling:
19084 /* Ignore absolute siblings, they might point outside of
19085 the current compile unit. */
19086 if (attr.form == DW_FORM_ref_addr)
19087 complaint (&symfile_complaints,
19088 _("ignoring absolute DW_AT_sibling"));
19089 else
19090 {
19091 const gdb_byte *buffer = reader->buffer;
19092 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19093 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19094
19095 if (sibling_ptr < info_ptr)
19096 complaint (&symfile_complaints,
19097 _("DW_AT_sibling points backwards"));
19098 else if (sibling_ptr > reader->buffer_end)
19099 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19100 else
19101 sibling = sibling_ptr;
19102 }
19103 break;
19104 case DW_AT_byte_size:
19105 has_byte_size = 1;
19106 break;
19107 case DW_AT_const_value:
19108 has_const_value = 1;
19109 break;
19110 case DW_AT_calling_convention:
19111 /* DWARF doesn't provide a way to identify a program's source-level
19112 entry point. DW_AT_calling_convention attributes are only meant
19113 to describe functions' calling conventions.
19114
19115 However, because it's a necessary piece of information in
19116 Fortran, and before DWARF 4 DW_CC_program was the only
19117 piece of debugging information whose definition refers to
19118 a 'main program' at all, several compilers marked Fortran
19119 main programs with DW_CC_program --- even when those
19120 functions use the standard calling conventions.
19121
19122 Although DWARF now specifies a way to provide this
19123 information, we support this practice for backward
19124 compatibility. */
19125 if (DW_UNSND (&attr) == DW_CC_program
19126 && cu->language == language_fortran)
19127 main_subprogram = 1;
19128 break;
19129 case DW_AT_inline:
19130 if (DW_UNSND (&attr) == DW_INL_inlined
19131 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19132 may_be_inlined = 1;
19133 break;
19134
19135 case DW_AT_import:
19136 if (tag == DW_TAG_imported_unit)
19137 {
19138 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19139 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19140 || cu->per_cu->is_dwz);
19141 }
19142 break;
19143
19144 case DW_AT_main_subprogram:
19145 main_subprogram = DW_UNSND (&attr);
19146 break;
19147
19148 default:
19149 break;
19150 }
19151 }
19152
19153 if (high_pc_relative)
19154 highpc += lowpc;
19155
19156 if (has_low_pc_attr && has_high_pc_attr)
19157 {
19158 /* When using the GNU linker, .gnu.linkonce. sections are used to
19159 eliminate duplicate copies of functions and vtables and such.
19160 The linker will arbitrarily choose one and discard the others.
19161 The AT_*_pc values for such functions refer to local labels in
19162 these sections. If the section from that file was discarded, the
19163 labels are not in the output, so the relocs get a value of 0.
19164 If this is a discarded function, mark the pc bounds as invalid,
19165 so that GDB will ignore it. */
19166 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19167 {
19168 struct objfile *objfile = dwarf2_per_objfile->objfile;
19169 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19170
19171 complaint (&symfile_complaints,
19172 _("DW_AT_low_pc %s is zero "
19173 "for DIE at %s [in module %s]"),
19174 paddress (gdbarch, lowpc),
19175 sect_offset_str (sect_off),
19176 objfile_name (objfile));
19177 }
19178 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19179 else if (lowpc >= highpc)
19180 {
19181 struct objfile *objfile = dwarf2_per_objfile->objfile;
19182 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19183
19184 complaint (&symfile_complaints,
19185 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19186 "for DIE at %s [in module %s]"),
19187 paddress (gdbarch, lowpc),
19188 paddress (gdbarch, highpc),
19189 sect_offset_str (sect_off),
19190 objfile_name (objfile));
19191 }
19192 else
19193 has_pc_info = 1;
19194 }
19195
19196 return info_ptr;
19197 }
19198
19199 /* Find a cached partial DIE at OFFSET in CU. */
19200
19201 struct partial_die_info *
19202 dwarf2_cu::find_partial_die (sect_offset sect_off)
19203 {
19204 struct partial_die_info *lookup_die = NULL;
19205 struct partial_die_info part_die (sect_off);
19206
19207 lookup_die = ((struct partial_die_info *)
19208 htab_find_with_hash (partial_dies, &part_die,
19209 to_underlying (sect_off)));
19210
19211 return lookup_die;
19212 }
19213
19214 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19215 except in the case of .debug_types DIEs which do not reference
19216 outside their CU (they do however referencing other types via
19217 DW_FORM_ref_sig8). */
19218
19219 static struct partial_die_info *
19220 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19221 {
19222 struct dwarf2_per_objfile *dwarf2_per_objfile
19223 = cu->per_cu->dwarf2_per_objfile;
19224 struct objfile *objfile = dwarf2_per_objfile->objfile;
19225 struct dwarf2_per_cu_data *per_cu = NULL;
19226 struct partial_die_info *pd = NULL;
19227
19228 if (offset_in_dwz == cu->per_cu->is_dwz
19229 && offset_in_cu_p (&cu->header, sect_off))
19230 {
19231 pd = cu->find_partial_die (sect_off);
19232 if (pd != NULL)
19233 return pd;
19234 /* We missed recording what we needed.
19235 Load all dies and try again. */
19236 per_cu = cu->per_cu;
19237 }
19238 else
19239 {
19240 /* TUs don't reference other CUs/TUs (except via type signatures). */
19241 if (cu->per_cu->is_debug_types)
19242 {
19243 error (_("Dwarf Error: Type Unit at offset %s contains"
19244 " external reference to offset %s [in module %s].\n"),
19245 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19246 bfd_get_filename (objfile->obfd));
19247 }
19248 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19249 dwarf2_per_objfile);
19250
19251 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19252 load_partial_comp_unit (per_cu);
19253
19254 per_cu->cu->last_used = 0;
19255 pd = per_cu->cu->find_partial_die (sect_off);
19256 }
19257
19258 /* If we didn't find it, and not all dies have been loaded,
19259 load them all and try again. */
19260
19261 if (pd == NULL && per_cu->load_all_dies == 0)
19262 {
19263 per_cu->load_all_dies = 1;
19264
19265 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19266 THIS_CU->cu may already be in use. So we can't just free it and
19267 replace its DIEs with the ones we read in. Instead, we leave those
19268 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19269 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19270 set. */
19271 load_partial_comp_unit (per_cu);
19272
19273 pd = per_cu->cu->find_partial_die (sect_off);
19274 }
19275
19276 if (pd == NULL)
19277 internal_error (__FILE__, __LINE__,
19278 _("could not find partial DIE %s "
19279 "in cache [from module %s]\n"),
19280 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19281 return pd;
19282 }
19283
19284 /* See if we can figure out if the class lives in a namespace. We do
19285 this by looking for a member function; its demangled name will
19286 contain namespace info, if there is any. */
19287
19288 static void
19289 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19290 struct dwarf2_cu *cu)
19291 {
19292 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19293 what template types look like, because the demangler
19294 frequently doesn't give the same name as the debug info. We
19295 could fix this by only using the demangled name to get the
19296 prefix (but see comment in read_structure_type). */
19297
19298 struct partial_die_info *real_pdi;
19299 struct partial_die_info *child_pdi;
19300
19301 /* If this DIE (this DIE's specification, if any) has a parent, then
19302 we should not do this. We'll prepend the parent's fully qualified
19303 name when we create the partial symbol. */
19304
19305 real_pdi = struct_pdi;
19306 while (real_pdi->has_specification)
19307 real_pdi = find_partial_die (real_pdi->spec_offset,
19308 real_pdi->spec_is_dwz, cu);
19309
19310 if (real_pdi->die_parent != NULL)
19311 return;
19312
19313 for (child_pdi = struct_pdi->die_child;
19314 child_pdi != NULL;
19315 child_pdi = child_pdi->die_sibling)
19316 {
19317 if (child_pdi->tag == DW_TAG_subprogram
19318 && child_pdi->linkage_name != NULL)
19319 {
19320 char *actual_class_name
19321 = language_class_name_from_physname (cu->language_defn,
19322 child_pdi->linkage_name);
19323 if (actual_class_name != NULL)
19324 {
19325 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19326 struct_pdi->name
19327 = ((const char *)
19328 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19329 actual_class_name,
19330 strlen (actual_class_name)));
19331 xfree (actual_class_name);
19332 }
19333 break;
19334 }
19335 }
19336 }
19337
19338 void
19339 partial_die_info::fixup (struct dwarf2_cu *cu)
19340 {
19341 /* Once we've fixed up a die, there's no point in doing so again.
19342 This also avoids a memory leak if we were to call
19343 guess_partial_die_structure_name multiple times. */
19344 if (fixup_called)
19345 return;
19346
19347 /* If we found a reference attribute and the DIE has no name, try
19348 to find a name in the referred to DIE. */
19349
19350 if (name == NULL && has_specification)
19351 {
19352 struct partial_die_info *spec_die;
19353
19354 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
19355
19356 spec_die->fixup (cu);
19357
19358 if (spec_die->name)
19359 {
19360 name = spec_die->name;
19361
19362 /* Copy DW_AT_external attribute if it is set. */
19363 if (spec_die->is_external)
19364 is_external = spec_die->is_external;
19365 }
19366 }
19367
19368 /* Set default names for some unnamed DIEs. */
19369
19370 if (name == NULL && tag == DW_TAG_namespace)
19371 name = CP_ANONYMOUS_NAMESPACE_STR;
19372
19373 /* If there is no parent die to provide a namespace, and there are
19374 children, see if we can determine the namespace from their linkage
19375 name. */
19376 if (cu->language == language_cplus
19377 && !VEC_empty (dwarf2_section_info_def,
19378 cu->per_cu->dwarf2_per_objfile->types)
19379 && die_parent == NULL
19380 && has_children
19381 && (tag == DW_TAG_class_type
19382 || tag == DW_TAG_structure_type
19383 || tag == DW_TAG_union_type))
19384 guess_partial_die_structure_name (this, cu);
19385
19386 /* GCC might emit a nameless struct or union that has a linkage
19387 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19388 if (name == NULL
19389 && (tag == DW_TAG_class_type
19390 || tag == DW_TAG_interface_type
19391 || tag == DW_TAG_structure_type
19392 || tag == DW_TAG_union_type)
19393 && linkage_name != NULL)
19394 {
19395 char *demangled;
19396
19397 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19398 if (demangled)
19399 {
19400 const char *base;
19401
19402 /* Strip any leading namespaces/classes, keep only the base name.
19403 DW_AT_name for named DIEs does not contain the prefixes. */
19404 base = strrchr (demangled, ':');
19405 if (base && base > demangled && base[-1] == ':')
19406 base++;
19407 else
19408 base = demangled;
19409
19410 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19411 name
19412 = ((const char *)
19413 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19414 base, strlen (base)));
19415 xfree (demangled);
19416 }
19417 }
19418
19419 fixup_called = 1;
19420 }
19421
19422 /* Read an attribute value described by an attribute form. */
19423
19424 static const gdb_byte *
19425 read_attribute_value (const struct die_reader_specs *reader,
19426 struct attribute *attr, unsigned form,
19427 LONGEST implicit_const, const gdb_byte *info_ptr)
19428 {
19429 struct dwarf2_cu *cu = reader->cu;
19430 struct dwarf2_per_objfile *dwarf2_per_objfile
19431 = cu->per_cu->dwarf2_per_objfile;
19432 struct objfile *objfile = dwarf2_per_objfile->objfile;
19433 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19434 bfd *abfd = reader->abfd;
19435 struct comp_unit_head *cu_header = &cu->header;
19436 unsigned int bytes_read;
19437 struct dwarf_block *blk;
19438
19439 attr->form = (enum dwarf_form) form;
19440 switch (form)
19441 {
19442 case DW_FORM_ref_addr:
19443 if (cu->header.version == 2)
19444 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19445 else
19446 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19447 &cu->header, &bytes_read);
19448 info_ptr += bytes_read;
19449 break;
19450 case DW_FORM_GNU_ref_alt:
19451 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19452 info_ptr += bytes_read;
19453 break;
19454 case DW_FORM_addr:
19455 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19456 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19457 info_ptr += bytes_read;
19458 break;
19459 case DW_FORM_block2:
19460 blk = dwarf_alloc_block (cu);
19461 blk->size = read_2_bytes (abfd, info_ptr);
19462 info_ptr += 2;
19463 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19464 info_ptr += blk->size;
19465 DW_BLOCK (attr) = blk;
19466 break;
19467 case DW_FORM_block4:
19468 blk = dwarf_alloc_block (cu);
19469 blk->size = read_4_bytes (abfd, info_ptr);
19470 info_ptr += 4;
19471 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19472 info_ptr += blk->size;
19473 DW_BLOCK (attr) = blk;
19474 break;
19475 case DW_FORM_data2:
19476 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19477 info_ptr += 2;
19478 break;
19479 case DW_FORM_data4:
19480 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19481 info_ptr += 4;
19482 break;
19483 case DW_FORM_data8:
19484 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19485 info_ptr += 8;
19486 break;
19487 case DW_FORM_data16:
19488 blk = dwarf_alloc_block (cu);
19489 blk->size = 16;
19490 blk->data = read_n_bytes (abfd, info_ptr, 16);
19491 info_ptr += 16;
19492 DW_BLOCK (attr) = blk;
19493 break;
19494 case DW_FORM_sec_offset:
19495 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19496 info_ptr += bytes_read;
19497 break;
19498 case DW_FORM_string:
19499 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19500 DW_STRING_IS_CANONICAL (attr) = 0;
19501 info_ptr += bytes_read;
19502 break;
19503 case DW_FORM_strp:
19504 if (!cu->per_cu->is_dwz)
19505 {
19506 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19507 abfd, info_ptr, cu_header,
19508 &bytes_read);
19509 DW_STRING_IS_CANONICAL (attr) = 0;
19510 info_ptr += bytes_read;
19511 break;
19512 }
19513 /* FALLTHROUGH */
19514 case DW_FORM_line_strp:
19515 if (!cu->per_cu->is_dwz)
19516 {
19517 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19518 abfd, info_ptr,
19519 cu_header, &bytes_read);
19520 DW_STRING_IS_CANONICAL (attr) = 0;
19521 info_ptr += bytes_read;
19522 break;
19523 }
19524 /* FALLTHROUGH */
19525 case DW_FORM_GNU_strp_alt:
19526 {
19527 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19528 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19529 &bytes_read);
19530
19531 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19532 dwz, str_offset);
19533 DW_STRING_IS_CANONICAL (attr) = 0;
19534 info_ptr += bytes_read;
19535 }
19536 break;
19537 case DW_FORM_exprloc:
19538 case DW_FORM_block:
19539 blk = dwarf_alloc_block (cu);
19540 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19541 info_ptr += bytes_read;
19542 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19543 info_ptr += blk->size;
19544 DW_BLOCK (attr) = blk;
19545 break;
19546 case DW_FORM_block1:
19547 blk = dwarf_alloc_block (cu);
19548 blk->size = read_1_byte (abfd, info_ptr);
19549 info_ptr += 1;
19550 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19551 info_ptr += blk->size;
19552 DW_BLOCK (attr) = blk;
19553 break;
19554 case DW_FORM_data1:
19555 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19556 info_ptr += 1;
19557 break;
19558 case DW_FORM_flag:
19559 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19560 info_ptr += 1;
19561 break;
19562 case DW_FORM_flag_present:
19563 DW_UNSND (attr) = 1;
19564 break;
19565 case DW_FORM_sdata:
19566 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19567 info_ptr += bytes_read;
19568 break;
19569 case DW_FORM_udata:
19570 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19571 info_ptr += bytes_read;
19572 break;
19573 case DW_FORM_ref1:
19574 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19575 + read_1_byte (abfd, info_ptr));
19576 info_ptr += 1;
19577 break;
19578 case DW_FORM_ref2:
19579 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19580 + read_2_bytes (abfd, info_ptr));
19581 info_ptr += 2;
19582 break;
19583 case DW_FORM_ref4:
19584 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19585 + read_4_bytes (abfd, info_ptr));
19586 info_ptr += 4;
19587 break;
19588 case DW_FORM_ref8:
19589 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19590 + read_8_bytes (abfd, info_ptr));
19591 info_ptr += 8;
19592 break;
19593 case DW_FORM_ref_sig8:
19594 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19595 info_ptr += 8;
19596 break;
19597 case DW_FORM_ref_udata:
19598 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19599 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19600 info_ptr += bytes_read;
19601 break;
19602 case DW_FORM_indirect:
19603 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19604 info_ptr += bytes_read;
19605 if (form == DW_FORM_implicit_const)
19606 {
19607 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19608 info_ptr += bytes_read;
19609 }
19610 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19611 info_ptr);
19612 break;
19613 case DW_FORM_implicit_const:
19614 DW_SND (attr) = implicit_const;
19615 break;
19616 case DW_FORM_GNU_addr_index:
19617 if (reader->dwo_file == NULL)
19618 {
19619 /* For now flag a hard error.
19620 Later we can turn this into a complaint. */
19621 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19622 dwarf_form_name (form),
19623 bfd_get_filename (abfd));
19624 }
19625 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19626 info_ptr += bytes_read;
19627 break;
19628 case DW_FORM_GNU_str_index:
19629 if (reader->dwo_file == NULL)
19630 {
19631 /* For now flag a hard error.
19632 Later we can turn this into a complaint if warranted. */
19633 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19634 dwarf_form_name (form),
19635 bfd_get_filename (abfd));
19636 }
19637 {
19638 ULONGEST str_index =
19639 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19640
19641 DW_STRING (attr) = read_str_index (reader, str_index);
19642 DW_STRING_IS_CANONICAL (attr) = 0;
19643 info_ptr += bytes_read;
19644 }
19645 break;
19646 default:
19647 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19648 dwarf_form_name (form),
19649 bfd_get_filename (abfd));
19650 }
19651
19652 /* Super hack. */
19653 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19654 attr->form = DW_FORM_GNU_ref_alt;
19655
19656 /* We have seen instances where the compiler tried to emit a byte
19657 size attribute of -1 which ended up being encoded as an unsigned
19658 0xffffffff. Although 0xffffffff is technically a valid size value,
19659 an object of this size seems pretty unlikely so we can relatively
19660 safely treat these cases as if the size attribute was invalid and
19661 treat them as zero by default. */
19662 if (attr->name == DW_AT_byte_size
19663 && form == DW_FORM_data4
19664 && DW_UNSND (attr) >= 0xffffffff)
19665 {
19666 complaint
19667 (&symfile_complaints,
19668 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19669 hex_string (DW_UNSND (attr)));
19670 DW_UNSND (attr) = 0;
19671 }
19672
19673 return info_ptr;
19674 }
19675
19676 /* Read an attribute described by an abbreviated attribute. */
19677
19678 static const gdb_byte *
19679 read_attribute (const struct die_reader_specs *reader,
19680 struct attribute *attr, struct attr_abbrev *abbrev,
19681 const gdb_byte *info_ptr)
19682 {
19683 attr->name = abbrev->name;
19684 return read_attribute_value (reader, attr, abbrev->form,
19685 abbrev->implicit_const, info_ptr);
19686 }
19687
19688 /* Read dwarf information from a buffer. */
19689
19690 static unsigned int
19691 read_1_byte (bfd *abfd, const gdb_byte *buf)
19692 {
19693 return bfd_get_8 (abfd, buf);
19694 }
19695
19696 static int
19697 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19698 {
19699 return bfd_get_signed_8 (abfd, buf);
19700 }
19701
19702 static unsigned int
19703 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19704 {
19705 return bfd_get_16 (abfd, buf);
19706 }
19707
19708 static int
19709 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19710 {
19711 return bfd_get_signed_16 (abfd, buf);
19712 }
19713
19714 static unsigned int
19715 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19716 {
19717 return bfd_get_32 (abfd, buf);
19718 }
19719
19720 static int
19721 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19722 {
19723 return bfd_get_signed_32 (abfd, buf);
19724 }
19725
19726 static ULONGEST
19727 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19728 {
19729 return bfd_get_64 (abfd, buf);
19730 }
19731
19732 static CORE_ADDR
19733 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19734 unsigned int *bytes_read)
19735 {
19736 struct comp_unit_head *cu_header = &cu->header;
19737 CORE_ADDR retval = 0;
19738
19739 if (cu_header->signed_addr_p)
19740 {
19741 switch (cu_header->addr_size)
19742 {
19743 case 2:
19744 retval = bfd_get_signed_16 (abfd, buf);
19745 break;
19746 case 4:
19747 retval = bfd_get_signed_32 (abfd, buf);
19748 break;
19749 case 8:
19750 retval = bfd_get_signed_64 (abfd, buf);
19751 break;
19752 default:
19753 internal_error (__FILE__, __LINE__,
19754 _("read_address: bad switch, signed [in module %s]"),
19755 bfd_get_filename (abfd));
19756 }
19757 }
19758 else
19759 {
19760 switch (cu_header->addr_size)
19761 {
19762 case 2:
19763 retval = bfd_get_16 (abfd, buf);
19764 break;
19765 case 4:
19766 retval = bfd_get_32 (abfd, buf);
19767 break;
19768 case 8:
19769 retval = bfd_get_64 (abfd, buf);
19770 break;
19771 default:
19772 internal_error (__FILE__, __LINE__,
19773 _("read_address: bad switch, "
19774 "unsigned [in module %s]"),
19775 bfd_get_filename (abfd));
19776 }
19777 }
19778
19779 *bytes_read = cu_header->addr_size;
19780 return retval;
19781 }
19782
19783 /* Read the initial length from a section. The (draft) DWARF 3
19784 specification allows the initial length to take up either 4 bytes
19785 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19786 bytes describe the length and all offsets will be 8 bytes in length
19787 instead of 4.
19788
19789 An older, non-standard 64-bit format is also handled by this
19790 function. The older format in question stores the initial length
19791 as an 8-byte quantity without an escape value. Lengths greater
19792 than 2^32 aren't very common which means that the initial 4 bytes
19793 is almost always zero. Since a length value of zero doesn't make
19794 sense for the 32-bit format, this initial zero can be considered to
19795 be an escape value which indicates the presence of the older 64-bit
19796 format. As written, the code can't detect (old format) lengths
19797 greater than 4GB. If it becomes necessary to handle lengths
19798 somewhat larger than 4GB, we could allow other small values (such
19799 as the non-sensical values of 1, 2, and 3) to also be used as
19800 escape values indicating the presence of the old format.
19801
19802 The value returned via bytes_read should be used to increment the
19803 relevant pointer after calling read_initial_length().
19804
19805 [ Note: read_initial_length() and read_offset() are based on the
19806 document entitled "DWARF Debugging Information Format", revision
19807 3, draft 8, dated November 19, 2001. This document was obtained
19808 from:
19809
19810 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19811
19812 This document is only a draft and is subject to change. (So beware.)
19813
19814 Details regarding the older, non-standard 64-bit format were
19815 determined empirically by examining 64-bit ELF files produced by
19816 the SGI toolchain on an IRIX 6.5 machine.
19817
19818 - Kevin, July 16, 2002
19819 ] */
19820
19821 static LONGEST
19822 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19823 {
19824 LONGEST length = bfd_get_32 (abfd, buf);
19825
19826 if (length == 0xffffffff)
19827 {
19828 length = bfd_get_64 (abfd, buf + 4);
19829 *bytes_read = 12;
19830 }
19831 else if (length == 0)
19832 {
19833 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19834 length = bfd_get_64 (abfd, buf);
19835 *bytes_read = 8;
19836 }
19837 else
19838 {
19839 *bytes_read = 4;
19840 }
19841
19842 return length;
19843 }
19844
19845 /* Cover function for read_initial_length.
19846 Returns the length of the object at BUF, and stores the size of the
19847 initial length in *BYTES_READ and stores the size that offsets will be in
19848 *OFFSET_SIZE.
19849 If the initial length size is not equivalent to that specified in
19850 CU_HEADER then issue a complaint.
19851 This is useful when reading non-comp-unit headers. */
19852
19853 static LONGEST
19854 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19855 const struct comp_unit_head *cu_header,
19856 unsigned int *bytes_read,
19857 unsigned int *offset_size)
19858 {
19859 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19860
19861 gdb_assert (cu_header->initial_length_size == 4
19862 || cu_header->initial_length_size == 8
19863 || cu_header->initial_length_size == 12);
19864
19865 if (cu_header->initial_length_size != *bytes_read)
19866 complaint (&symfile_complaints,
19867 _("intermixed 32-bit and 64-bit DWARF sections"));
19868
19869 *offset_size = (*bytes_read == 4) ? 4 : 8;
19870 return length;
19871 }
19872
19873 /* Read an offset from the data stream. The size of the offset is
19874 given by cu_header->offset_size. */
19875
19876 static LONGEST
19877 read_offset (bfd *abfd, const gdb_byte *buf,
19878 const struct comp_unit_head *cu_header,
19879 unsigned int *bytes_read)
19880 {
19881 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19882
19883 *bytes_read = cu_header->offset_size;
19884 return offset;
19885 }
19886
19887 /* Read an offset from the data stream. */
19888
19889 static LONGEST
19890 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19891 {
19892 LONGEST retval = 0;
19893
19894 switch (offset_size)
19895 {
19896 case 4:
19897 retval = bfd_get_32 (abfd, buf);
19898 break;
19899 case 8:
19900 retval = bfd_get_64 (abfd, buf);
19901 break;
19902 default:
19903 internal_error (__FILE__, __LINE__,
19904 _("read_offset_1: bad switch [in module %s]"),
19905 bfd_get_filename (abfd));
19906 }
19907
19908 return retval;
19909 }
19910
19911 static const gdb_byte *
19912 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19913 {
19914 /* If the size of a host char is 8 bits, we can return a pointer
19915 to the buffer, otherwise we have to copy the data to a buffer
19916 allocated on the temporary obstack. */
19917 gdb_assert (HOST_CHAR_BIT == 8);
19918 return buf;
19919 }
19920
19921 static const char *
19922 read_direct_string (bfd *abfd, const gdb_byte *buf,
19923 unsigned int *bytes_read_ptr)
19924 {
19925 /* If the size of a host char is 8 bits, we can return a pointer
19926 to the string, otherwise we have to copy the string to a buffer
19927 allocated on the temporary obstack. */
19928 gdb_assert (HOST_CHAR_BIT == 8);
19929 if (*buf == '\0')
19930 {
19931 *bytes_read_ptr = 1;
19932 return NULL;
19933 }
19934 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19935 return (const char *) buf;
19936 }
19937
19938 /* Return pointer to string at section SECT offset STR_OFFSET with error
19939 reporting strings FORM_NAME and SECT_NAME. */
19940
19941 static const char *
19942 read_indirect_string_at_offset_from (struct objfile *objfile,
19943 bfd *abfd, LONGEST str_offset,
19944 struct dwarf2_section_info *sect,
19945 const char *form_name,
19946 const char *sect_name)
19947 {
19948 dwarf2_read_section (objfile, sect);
19949 if (sect->buffer == NULL)
19950 error (_("%s used without %s section [in module %s]"),
19951 form_name, sect_name, bfd_get_filename (abfd));
19952 if (str_offset >= sect->size)
19953 error (_("%s pointing outside of %s section [in module %s]"),
19954 form_name, sect_name, bfd_get_filename (abfd));
19955 gdb_assert (HOST_CHAR_BIT == 8);
19956 if (sect->buffer[str_offset] == '\0')
19957 return NULL;
19958 return (const char *) (sect->buffer + str_offset);
19959 }
19960
19961 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19962
19963 static const char *
19964 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19965 bfd *abfd, LONGEST str_offset)
19966 {
19967 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19968 abfd, str_offset,
19969 &dwarf2_per_objfile->str,
19970 "DW_FORM_strp", ".debug_str");
19971 }
19972
19973 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19974
19975 static const char *
19976 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19977 bfd *abfd, LONGEST str_offset)
19978 {
19979 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19980 abfd, str_offset,
19981 &dwarf2_per_objfile->line_str,
19982 "DW_FORM_line_strp",
19983 ".debug_line_str");
19984 }
19985
19986 /* Read a string at offset STR_OFFSET in the .debug_str section from
19987 the .dwz file DWZ. Throw an error if the offset is too large. If
19988 the string consists of a single NUL byte, return NULL; otherwise
19989 return a pointer to the string. */
19990
19991 static const char *
19992 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19993 LONGEST str_offset)
19994 {
19995 dwarf2_read_section (objfile, &dwz->str);
19996
19997 if (dwz->str.buffer == NULL)
19998 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19999 "section [in module %s]"),
20000 bfd_get_filename (dwz->dwz_bfd));
20001 if (str_offset >= dwz->str.size)
20002 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20003 ".debug_str section [in module %s]"),
20004 bfd_get_filename (dwz->dwz_bfd));
20005 gdb_assert (HOST_CHAR_BIT == 8);
20006 if (dwz->str.buffer[str_offset] == '\0')
20007 return NULL;
20008 return (const char *) (dwz->str.buffer + str_offset);
20009 }
20010
20011 /* Return pointer to string at .debug_str offset as read from BUF.
20012 BUF is assumed to be in a compilation unit described by CU_HEADER.
20013 Return *BYTES_READ_PTR count of bytes read from BUF. */
20014
20015 static const char *
20016 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20017 const gdb_byte *buf,
20018 const struct comp_unit_head *cu_header,
20019 unsigned int *bytes_read_ptr)
20020 {
20021 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20022
20023 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20024 }
20025
20026 /* Return pointer to string at .debug_line_str offset as read from BUF.
20027 BUF is assumed to be in a compilation unit described by CU_HEADER.
20028 Return *BYTES_READ_PTR count of bytes read from BUF. */
20029
20030 static const char *
20031 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20032 bfd *abfd, const gdb_byte *buf,
20033 const struct comp_unit_head *cu_header,
20034 unsigned int *bytes_read_ptr)
20035 {
20036 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20037
20038 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20039 str_offset);
20040 }
20041
20042 ULONGEST
20043 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20044 unsigned int *bytes_read_ptr)
20045 {
20046 ULONGEST result;
20047 unsigned int num_read;
20048 int shift;
20049 unsigned char byte;
20050
20051 result = 0;
20052 shift = 0;
20053 num_read = 0;
20054 while (1)
20055 {
20056 byte = bfd_get_8 (abfd, buf);
20057 buf++;
20058 num_read++;
20059 result |= ((ULONGEST) (byte & 127) << shift);
20060 if ((byte & 128) == 0)
20061 {
20062 break;
20063 }
20064 shift += 7;
20065 }
20066 *bytes_read_ptr = num_read;
20067 return result;
20068 }
20069
20070 static LONGEST
20071 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20072 unsigned int *bytes_read_ptr)
20073 {
20074 LONGEST result;
20075 int shift, num_read;
20076 unsigned char byte;
20077
20078 result = 0;
20079 shift = 0;
20080 num_read = 0;
20081 while (1)
20082 {
20083 byte = bfd_get_8 (abfd, buf);
20084 buf++;
20085 num_read++;
20086 result |= ((LONGEST) (byte & 127) << shift);
20087 shift += 7;
20088 if ((byte & 128) == 0)
20089 {
20090 break;
20091 }
20092 }
20093 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20094 result |= -(((LONGEST) 1) << shift);
20095 *bytes_read_ptr = num_read;
20096 return result;
20097 }
20098
20099 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20100 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20101 ADDR_SIZE is the size of addresses from the CU header. */
20102
20103 static CORE_ADDR
20104 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20105 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20106 {
20107 struct objfile *objfile = dwarf2_per_objfile->objfile;
20108 bfd *abfd = objfile->obfd;
20109 const gdb_byte *info_ptr;
20110
20111 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20112 if (dwarf2_per_objfile->addr.buffer == NULL)
20113 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20114 objfile_name (objfile));
20115 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20116 error (_("DW_FORM_addr_index pointing outside of "
20117 ".debug_addr section [in module %s]"),
20118 objfile_name (objfile));
20119 info_ptr = (dwarf2_per_objfile->addr.buffer
20120 + addr_base + addr_index * addr_size);
20121 if (addr_size == 4)
20122 return bfd_get_32 (abfd, info_ptr);
20123 else
20124 return bfd_get_64 (abfd, info_ptr);
20125 }
20126
20127 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20128
20129 static CORE_ADDR
20130 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20131 {
20132 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20133 cu->addr_base, cu->header.addr_size);
20134 }
20135
20136 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20137
20138 static CORE_ADDR
20139 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20140 unsigned int *bytes_read)
20141 {
20142 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20143 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20144
20145 return read_addr_index (cu, addr_index);
20146 }
20147
20148 /* Data structure to pass results from dwarf2_read_addr_index_reader
20149 back to dwarf2_read_addr_index. */
20150
20151 struct dwarf2_read_addr_index_data
20152 {
20153 ULONGEST addr_base;
20154 int addr_size;
20155 };
20156
20157 /* die_reader_func for dwarf2_read_addr_index. */
20158
20159 static void
20160 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20161 const gdb_byte *info_ptr,
20162 struct die_info *comp_unit_die,
20163 int has_children,
20164 void *data)
20165 {
20166 struct dwarf2_cu *cu = reader->cu;
20167 struct dwarf2_read_addr_index_data *aidata =
20168 (struct dwarf2_read_addr_index_data *) data;
20169
20170 aidata->addr_base = cu->addr_base;
20171 aidata->addr_size = cu->header.addr_size;
20172 }
20173
20174 /* Given an index in .debug_addr, fetch the value.
20175 NOTE: This can be called during dwarf expression evaluation,
20176 long after the debug information has been read, and thus per_cu->cu
20177 may no longer exist. */
20178
20179 CORE_ADDR
20180 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20181 unsigned int addr_index)
20182 {
20183 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20184 struct objfile *objfile = dwarf2_per_objfile->objfile;
20185 struct dwarf2_cu *cu = per_cu->cu;
20186 ULONGEST addr_base;
20187 int addr_size;
20188
20189 /* We need addr_base and addr_size.
20190 If we don't have PER_CU->cu, we have to get it.
20191 Nasty, but the alternative is storing the needed info in PER_CU,
20192 which at this point doesn't seem justified: it's not clear how frequently
20193 it would get used and it would increase the size of every PER_CU.
20194 Entry points like dwarf2_per_cu_addr_size do a similar thing
20195 so we're not in uncharted territory here.
20196 Alas we need to be a bit more complicated as addr_base is contained
20197 in the DIE.
20198
20199 We don't need to read the entire CU(/TU).
20200 We just need the header and top level die.
20201
20202 IWBN to use the aging mechanism to let us lazily later discard the CU.
20203 For now we skip this optimization. */
20204
20205 if (cu != NULL)
20206 {
20207 addr_base = cu->addr_base;
20208 addr_size = cu->header.addr_size;
20209 }
20210 else
20211 {
20212 struct dwarf2_read_addr_index_data aidata;
20213
20214 /* Note: We can't use init_cutu_and_read_dies_simple here,
20215 we need addr_base. */
20216 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
20217 dwarf2_read_addr_index_reader, &aidata);
20218 addr_base = aidata.addr_base;
20219 addr_size = aidata.addr_size;
20220 }
20221
20222 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20223 addr_size);
20224 }
20225
20226 /* Given a DW_FORM_GNU_str_index, fetch the string.
20227 This is only used by the Fission support. */
20228
20229 static const char *
20230 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20231 {
20232 struct dwarf2_cu *cu = reader->cu;
20233 struct dwarf2_per_objfile *dwarf2_per_objfile
20234 = cu->per_cu->dwarf2_per_objfile;
20235 struct objfile *objfile = dwarf2_per_objfile->objfile;
20236 const char *objf_name = objfile_name (objfile);
20237 bfd *abfd = objfile->obfd;
20238 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20239 struct dwarf2_section_info *str_offsets_section =
20240 &reader->dwo_file->sections.str_offsets;
20241 const gdb_byte *info_ptr;
20242 ULONGEST str_offset;
20243 static const char form_name[] = "DW_FORM_GNU_str_index";
20244
20245 dwarf2_read_section (objfile, str_section);
20246 dwarf2_read_section (objfile, str_offsets_section);
20247 if (str_section->buffer == NULL)
20248 error (_("%s used without .debug_str.dwo section"
20249 " in CU at offset %s [in module %s]"),
20250 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20251 if (str_offsets_section->buffer == NULL)
20252 error (_("%s used without .debug_str_offsets.dwo section"
20253 " in CU at offset %s [in module %s]"),
20254 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20255 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20256 error (_("%s pointing outside of .debug_str_offsets.dwo"
20257 " section in CU at offset %s [in module %s]"),
20258 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20259 info_ptr = (str_offsets_section->buffer
20260 + str_index * cu->header.offset_size);
20261 if (cu->header.offset_size == 4)
20262 str_offset = bfd_get_32 (abfd, info_ptr);
20263 else
20264 str_offset = bfd_get_64 (abfd, info_ptr);
20265 if (str_offset >= str_section->size)
20266 error (_("Offset from %s pointing outside of"
20267 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20268 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20269 return (const char *) (str_section->buffer + str_offset);
20270 }
20271
20272 /* Return the length of an LEB128 number in BUF. */
20273
20274 static int
20275 leb128_size (const gdb_byte *buf)
20276 {
20277 const gdb_byte *begin = buf;
20278 gdb_byte byte;
20279
20280 while (1)
20281 {
20282 byte = *buf++;
20283 if ((byte & 128) == 0)
20284 return buf - begin;
20285 }
20286 }
20287
20288 static void
20289 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20290 {
20291 switch (lang)
20292 {
20293 case DW_LANG_C89:
20294 case DW_LANG_C99:
20295 case DW_LANG_C11:
20296 case DW_LANG_C:
20297 case DW_LANG_UPC:
20298 cu->language = language_c;
20299 break;
20300 case DW_LANG_Java:
20301 case DW_LANG_C_plus_plus:
20302 case DW_LANG_C_plus_plus_11:
20303 case DW_LANG_C_plus_plus_14:
20304 cu->language = language_cplus;
20305 break;
20306 case DW_LANG_D:
20307 cu->language = language_d;
20308 break;
20309 case DW_LANG_Fortran77:
20310 case DW_LANG_Fortran90:
20311 case DW_LANG_Fortran95:
20312 case DW_LANG_Fortran03:
20313 case DW_LANG_Fortran08:
20314 cu->language = language_fortran;
20315 break;
20316 case DW_LANG_Go:
20317 cu->language = language_go;
20318 break;
20319 case DW_LANG_Mips_Assembler:
20320 cu->language = language_asm;
20321 break;
20322 case DW_LANG_Ada83:
20323 case DW_LANG_Ada95:
20324 cu->language = language_ada;
20325 break;
20326 case DW_LANG_Modula2:
20327 cu->language = language_m2;
20328 break;
20329 case DW_LANG_Pascal83:
20330 cu->language = language_pascal;
20331 break;
20332 case DW_LANG_ObjC:
20333 cu->language = language_objc;
20334 break;
20335 case DW_LANG_Rust:
20336 case DW_LANG_Rust_old:
20337 cu->language = language_rust;
20338 break;
20339 case DW_LANG_Cobol74:
20340 case DW_LANG_Cobol85:
20341 default:
20342 cu->language = language_minimal;
20343 break;
20344 }
20345 cu->language_defn = language_def (cu->language);
20346 }
20347
20348 /* Return the named attribute or NULL if not there. */
20349
20350 static struct attribute *
20351 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20352 {
20353 for (;;)
20354 {
20355 unsigned int i;
20356 struct attribute *spec = NULL;
20357
20358 for (i = 0; i < die->num_attrs; ++i)
20359 {
20360 if (die->attrs[i].name == name)
20361 return &die->attrs[i];
20362 if (die->attrs[i].name == DW_AT_specification
20363 || die->attrs[i].name == DW_AT_abstract_origin)
20364 spec = &die->attrs[i];
20365 }
20366
20367 if (!spec)
20368 break;
20369
20370 die = follow_die_ref (die, spec, &cu);
20371 }
20372
20373 return NULL;
20374 }
20375
20376 /* Return the named attribute or NULL if not there,
20377 but do not follow DW_AT_specification, etc.
20378 This is for use in contexts where we're reading .debug_types dies.
20379 Following DW_AT_specification, DW_AT_abstract_origin will take us
20380 back up the chain, and we want to go down. */
20381
20382 static struct attribute *
20383 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20384 {
20385 unsigned int i;
20386
20387 for (i = 0; i < die->num_attrs; ++i)
20388 if (die->attrs[i].name == name)
20389 return &die->attrs[i];
20390
20391 return NULL;
20392 }
20393
20394 /* Return the string associated with a string-typed attribute, or NULL if it
20395 is either not found or is of an incorrect type. */
20396
20397 static const char *
20398 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20399 {
20400 struct attribute *attr;
20401 const char *str = NULL;
20402
20403 attr = dwarf2_attr (die, name, cu);
20404
20405 if (attr != NULL)
20406 {
20407 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20408 || attr->form == DW_FORM_string
20409 || attr->form == DW_FORM_GNU_str_index
20410 || attr->form == DW_FORM_GNU_strp_alt)
20411 str = DW_STRING (attr);
20412 else
20413 complaint (&symfile_complaints,
20414 _("string type expected for attribute %s for "
20415 "DIE at %s in module %s"),
20416 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20417 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20418 }
20419
20420 return str;
20421 }
20422
20423 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20424 and holds a non-zero value. This function should only be used for
20425 DW_FORM_flag or DW_FORM_flag_present attributes. */
20426
20427 static int
20428 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20429 {
20430 struct attribute *attr = dwarf2_attr (die, name, cu);
20431
20432 return (attr && DW_UNSND (attr));
20433 }
20434
20435 static int
20436 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20437 {
20438 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20439 which value is non-zero. However, we have to be careful with
20440 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20441 (via dwarf2_flag_true_p) follows this attribute. So we may
20442 end up accidently finding a declaration attribute that belongs
20443 to a different DIE referenced by the specification attribute,
20444 even though the given DIE does not have a declaration attribute. */
20445 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20446 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20447 }
20448
20449 /* Return the die giving the specification for DIE, if there is
20450 one. *SPEC_CU is the CU containing DIE on input, and the CU
20451 containing the return value on output. If there is no
20452 specification, but there is an abstract origin, that is
20453 returned. */
20454
20455 static struct die_info *
20456 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20457 {
20458 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20459 *spec_cu);
20460
20461 if (spec_attr == NULL)
20462 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20463
20464 if (spec_attr == NULL)
20465 return NULL;
20466 else
20467 return follow_die_ref (die, spec_attr, spec_cu);
20468 }
20469
20470 /* Stub for free_line_header to match void * callback types. */
20471
20472 static void
20473 free_line_header_voidp (void *arg)
20474 {
20475 struct line_header *lh = (struct line_header *) arg;
20476
20477 delete lh;
20478 }
20479
20480 void
20481 line_header::add_include_dir (const char *include_dir)
20482 {
20483 if (dwarf_line_debug >= 2)
20484 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20485 include_dirs.size () + 1, include_dir);
20486
20487 include_dirs.push_back (include_dir);
20488 }
20489
20490 void
20491 line_header::add_file_name (const char *name,
20492 dir_index d_index,
20493 unsigned int mod_time,
20494 unsigned int length)
20495 {
20496 if (dwarf_line_debug >= 2)
20497 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20498 (unsigned) file_names.size () + 1, name);
20499
20500 file_names.emplace_back (name, d_index, mod_time, length);
20501 }
20502
20503 /* A convenience function to find the proper .debug_line section for a CU. */
20504
20505 static struct dwarf2_section_info *
20506 get_debug_line_section (struct dwarf2_cu *cu)
20507 {
20508 struct dwarf2_section_info *section;
20509 struct dwarf2_per_objfile *dwarf2_per_objfile
20510 = cu->per_cu->dwarf2_per_objfile;
20511
20512 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20513 DWO file. */
20514 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20515 section = &cu->dwo_unit->dwo_file->sections.line;
20516 else if (cu->per_cu->is_dwz)
20517 {
20518 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20519
20520 section = &dwz->line;
20521 }
20522 else
20523 section = &dwarf2_per_objfile->line;
20524
20525 return section;
20526 }
20527
20528 /* Read directory or file name entry format, starting with byte of
20529 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20530 entries count and the entries themselves in the described entry
20531 format. */
20532
20533 static void
20534 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20535 bfd *abfd, const gdb_byte **bufp,
20536 struct line_header *lh,
20537 const struct comp_unit_head *cu_header,
20538 void (*callback) (struct line_header *lh,
20539 const char *name,
20540 dir_index d_index,
20541 unsigned int mod_time,
20542 unsigned int length))
20543 {
20544 gdb_byte format_count, formati;
20545 ULONGEST data_count, datai;
20546 const gdb_byte *buf = *bufp;
20547 const gdb_byte *format_header_data;
20548 unsigned int bytes_read;
20549
20550 format_count = read_1_byte (abfd, buf);
20551 buf += 1;
20552 format_header_data = buf;
20553 for (formati = 0; formati < format_count; formati++)
20554 {
20555 read_unsigned_leb128 (abfd, buf, &bytes_read);
20556 buf += bytes_read;
20557 read_unsigned_leb128 (abfd, buf, &bytes_read);
20558 buf += bytes_read;
20559 }
20560
20561 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20562 buf += bytes_read;
20563 for (datai = 0; datai < data_count; datai++)
20564 {
20565 const gdb_byte *format = format_header_data;
20566 struct file_entry fe;
20567
20568 for (formati = 0; formati < format_count; formati++)
20569 {
20570 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20571 format += bytes_read;
20572
20573 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20574 format += bytes_read;
20575
20576 gdb::optional<const char *> string;
20577 gdb::optional<unsigned int> uint;
20578
20579 switch (form)
20580 {
20581 case DW_FORM_string:
20582 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20583 buf += bytes_read;
20584 break;
20585
20586 case DW_FORM_line_strp:
20587 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20588 abfd, buf,
20589 cu_header,
20590 &bytes_read));
20591 buf += bytes_read;
20592 break;
20593
20594 case DW_FORM_data1:
20595 uint.emplace (read_1_byte (abfd, buf));
20596 buf += 1;
20597 break;
20598
20599 case DW_FORM_data2:
20600 uint.emplace (read_2_bytes (abfd, buf));
20601 buf += 2;
20602 break;
20603
20604 case DW_FORM_data4:
20605 uint.emplace (read_4_bytes (abfd, buf));
20606 buf += 4;
20607 break;
20608
20609 case DW_FORM_data8:
20610 uint.emplace (read_8_bytes (abfd, buf));
20611 buf += 8;
20612 break;
20613
20614 case DW_FORM_udata:
20615 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20616 buf += bytes_read;
20617 break;
20618
20619 case DW_FORM_block:
20620 /* It is valid only for DW_LNCT_timestamp which is ignored by
20621 current GDB. */
20622 break;
20623 }
20624
20625 switch (content_type)
20626 {
20627 case DW_LNCT_path:
20628 if (string.has_value ())
20629 fe.name = *string;
20630 break;
20631 case DW_LNCT_directory_index:
20632 if (uint.has_value ())
20633 fe.d_index = (dir_index) *uint;
20634 break;
20635 case DW_LNCT_timestamp:
20636 if (uint.has_value ())
20637 fe.mod_time = *uint;
20638 break;
20639 case DW_LNCT_size:
20640 if (uint.has_value ())
20641 fe.length = *uint;
20642 break;
20643 case DW_LNCT_MD5:
20644 break;
20645 default:
20646 complaint (&symfile_complaints,
20647 _("Unknown format content type %s"),
20648 pulongest (content_type));
20649 }
20650 }
20651
20652 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20653 }
20654
20655 *bufp = buf;
20656 }
20657
20658 /* Read the statement program header starting at OFFSET in
20659 .debug_line, or .debug_line.dwo. Return a pointer
20660 to a struct line_header, allocated using xmalloc.
20661 Returns NULL if there is a problem reading the header, e.g., if it
20662 has a version we don't understand.
20663
20664 NOTE: the strings in the include directory and file name tables of
20665 the returned object point into the dwarf line section buffer,
20666 and must not be freed. */
20667
20668 static line_header_up
20669 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20670 {
20671 const gdb_byte *line_ptr;
20672 unsigned int bytes_read, offset_size;
20673 int i;
20674 const char *cur_dir, *cur_file;
20675 struct dwarf2_section_info *section;
20676 bfd *abfd;
20677 struct dwarf2_per_objfile *dwarf2_per_objfile
20678 = cu->per_cu->dwarf2_per_objfile;
20679
20680 section = get_debug_line_section (cu);
20681 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20682 if (section->buffer == NULL)
20683 {
20684 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20685 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20686 else
20687 complaint (&symfile_complaints, _("missing .debug_line section"));
20688 return 0;
20689 }
20690
20691 /* We can't do this until we know the section is non-empty.
20692 Only then do we know we have such a section. */
20693 abfd = get_section_bfd_owner (section);
20694
20695 /* Make sure that at least there's room for the total_length field.
20696 That could be 12 bytes long, but we're just going to fudge that. */
20697 if (to_underlying (sect_off) + 4 >= section->size)
20698 {
20699 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20700 return 0;
20701 }
20702
20703 line_header_up lh (new line_header ());
20704
20705 lh->sect_off = sect_off;
20706 lh->offset_in_dwz = cu->per_cu->is_dwz;
20707
20708 line_ptr = section->buffer + to_underlying (sect_off);
20709
20710 /* Read in the header. */
20711 lh->total_length =
20712 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20713 &bytes_read, &offset_size);
20714 line_ptr += bytes_read;
20715 if (line_ptr + lh->total_length > (section->buffer + section->size))
20716 {
20717 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20718 return 0;
20719 }
20720 lh->statement_program_end = line_ptr + lh->total_length;
20721 lh->version = read_2_bytes (abfd, line_ptr);
20722 line_ptr += 2;
20723 if (lh->version > 5)
20724 {
20725 /* This is a version we don't understand. The format could have
20726 changed in ways we don't handle properly so just punt. */
20727 complaint (&symfile_complaints,
20728 _("unsupported version in .debug_line section"));
20729 return NULL;
20730 }
20731 if (lh->version >= 5)
20732 {
20733 gdb_byte segment_selector_size;
20734
20735 /* Skip address size. */
20736 read_1_byte (abfd, line_ptr);
20737 line_ptr += 1;
20738
20739 segment_selector_size = read_1_byte (abfd, line_ptr);
20740 line_ptr += 1;
20741 if (segment_selector_size != 0)
20742 {
20743 complaint (&symfile_complaints,
20744 _("unsupported segment selector size %u "
20745 "in .debug_line section"),
20746 segment_selector_size);
20747 return NULL;
20748 }
20749 }
20750 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20751 line_ptr += offset_size;
20752 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20753 line_ptr += 1;
20754 if (lh->version >= 4)
20755 {
20756 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20757 line_ptr += 1;
20758 }
20759 else
20760 lh->maximum_ops_per_instruction = 1;
20761
20762 if (lh->maximum_ops_per_instruction == 0)
20763 {
20764 lh->maximum_ops_per_instruction = 1;
20765 complaint (&symfile_complaints,
20766 _("invalid maximum_ops_per_instruction "
20767 "in `.debug_line' section"));
20768 }
20769
20770 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20771 line_ptr += 1;
20772 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20773 line_ptr += 1;
20774 lh->line_range = read_1_byte (abfd, line_ptr);
20775 line_ptr += 1;
20776 lh->opcode_base = read_1_byte (abfd, line_ptr);
20777 line_ptr += 1;
20778 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20779
20780 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20781 for (i = 1; i < lh->opcode_base; ++i)
20782 {
20783 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20784 line_ptr += 1;
20785 }
20786
20787 if (lh->version >= 5)
20788 {
20789 /* Read directory table. */
20790 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20791 &cu->header,
20792 [] (struct line_header *lh, const char *name,
20793 dir_index d_index, unsigned int mod_time,
20794 unsigned int length)
20795 {
20796 lh->add_include_dir (name);
20797 });
20798
20799 /* Read file name table. */
20800 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20801 &cu->header,
20802 [] (struct line_header *lh, const char *name,
20803 dir_index d_index, unsigned int mod_time,
20804 unsigned int length)
20805 {
20806 lh->add_file_name (name, d_index, mod_time, length);
20807 });
20808 }
20809 else
20810 {
20811 /* Read directory table. */
20812 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20813 {
20814 line_ptr += bytes_read;
20815 lh->add_include_dir (cur_dir);
20816 }
20817 line_ptr += bytes_read;
20818
20819 /* Read file name table. */
20820 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20821 {
20822 unsigned int mod_time, length;
20823 dir_index d_index;
20824
20825 line_ptr += bytes_read;
20826 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20827 line_ptr += bytes_read;
20828 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20829 line_ptr += bytes_read;
20830 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20831 line_ptr += bytes_read;
20832
20833 lh->add_file_name (cur_file, d_index, mod_time, length);
20834 }
20835 line_ptr += bytes_read;
20836 }
20837 lh->statement_program_start = line_ptr;
20838
20839 if (line_ptr > (section->buffer + section->size))
20840 complaint (&symfile_complaints,
20841 _("line number info header doesn't "
20842 "fit in `.debug_line' section"));
20843
20844 return lh;
20845 }
20846
20847 /* Subroutine of dwarf_decode_lines to simplify it.
20848 Return the file name of the psymtab for included file FILE_INDEX
20849 in line header LH of PST.
20850 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20851 If space for the result is malloc'd, *NAME_HOLDER will be set.
20852 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20853
20854 static const char *
20855 psymtab_include_file_name (const struct line_header *lh, int file_index,
20856 const struct partial_symtab *pst,
20857 const char *comp_dir,
20858 gdb::unique_xmalloc_ptr<char> *name_holder)
20859 {
20860 const file_entry &fe = lh->file_names[file_index];
20861 const char *include_name = fe.name;
20862 const char *include_name_to_compare = include_name;
20863 const char *pst_filename;
20864 int file_is_pst;
20865
20866 const char *dir_name = fe.include_dir (lh);
20867
20868 gdb::unique_xmalloc_ptr<char> hold_compare;
20869 if (!IS_ABSOLUTE_PATH (include_name)
20870 && (dir_name != NULL || comp_dir != NULL))
20871 {
20872 /* Avoid creating a duplicate psymtab for PST.
20873 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20874 Before we do the comparison, however, we need to account
20875 for DIR_NAME and COMP_DIR.
20876 First prepend dir_name (if non-NULL). If we still don't
20877 have an absolute path prepend comp_dir (if non-NULL).
20878 However, the directory we record in the include-file's
20879 psymtab does not contain COMP_DIR (to match the
20880 corresponding symtab(s)).
20881
20882 Example:
20883
20884 bash$ cd /tmp
20885 bash$ gcc -g ./hello.c
20886 include_name = "hello.c"
20887 dir_name = "."
20888 DW_AT_comp_dir = comp_dir = "/tmp"
20889 DW_AT_name = "./hello.c"
20890
20891 */
20892
20893 if (dir_name != NULL)
20894 {
20895 name_holder->reset (concat (dir_name, SLASH_STRING,
20896 include_name, (char *) NULL));
20897 include_name = name_holder->get ();
20898 include_name_to_compare = include_name;
20899 }
20900 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20901 {
20902 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20903 include_name, (char *) NULL));
20904 include_name_to_compare = hold_compare.get ();
20905 }
20906 }
20907
20908 pst_filename = pst->filename;
20909 gdb::unique_xmalloc_ptr<char> copied_name;
20910 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20911 {
20912 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20913 pst_filename, (char *) NULL));
20914 pst_filename = copied_name.get ();
20915 }
20916
20917 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20918
20919 if (file_is_pst)
20920 return NULL;
20921 return include_name;
20922 }
20923
20924 /* State machine to track the state of the line number program. */
20925
20926 class lnp_state_machine
20927 {
20928 public:
20929 /* Initialize a machine state for the start of a line number
20930 program. */
20931 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20932
20933 file_entry *current_file ()
20934 {
20935 /* lh->file_names is 0-based, but the file name numbers in the
20936 statement program are 1-based. */
20937 return m_line_header->file_name_at (m_file);
20938 }
20939
20940 /* Record the line in the state machine. END_SEQUENCE is true if
20941 we're processing the end of a sequence. */
20942 void record_line (bool end_sequence);
20943
20944 /* Check address and if invalid nop-out the rest of the lines in this
20945 sequence. */
20946 void check_line_address (struct dwarf2_cu *cu,
20947 const gdb_byte *line_ptr,
20948 CORE_ADDR lowpc, CORE_ADDR address);
20949
20950 void handle_set_discriminator (unsigned int discriminator)
20951 {
20952 m_discriminator = discriminator;
20953 m_line_has_non_zero_discriminator |= discriminator != 0;
20954 }
20955
20956 /* Handle DW_LNE_set_address. */
20957 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20958 {
20959 m_op_index = 0;
20960 address += baseaddr;
20961 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20962 }
20963
20964 /* Handle DW_LNS_advance_pc. */
20965 void handle_advance_pc (CORE_ADDR adjust);
20966
20967 /* Handle a special opcode. */
20968 void handle_special_opcode (unsigned char op_code);
20969
20970 /* Handle DW_LNS_advance_line. */
20971 void handle_advance_line (int line_delta)
20972 {
20973 advance_line (line_delta);
20974 }
20975
20976 /* Handle DW_LNS_set_file. */
20977 void handle_set_file (file_name_index file);
20978
20979 /* Handle DW_LNS_negate_stmt. */
20980 void handle_negate_stmt ()
20981 {
20982 m_is_stmt = !m_is_stmt;
20983 }
20984
20985 /* Handle DW_LNS_const_add_pc. */
20986 void handle_const_add_pc ();
20987
20988 /* Handle DW_LNS_fixed_advance_pc. */
20989 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20990 {
20991 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20992 m_op_index = 0;
20993 }
20994
20995 /* Handle DW_LNS_copy. */
20996 void handle_copy ()
20997 {
20998 record_line (false);
20999 m_discriminator = 0;
21000 }
21001
21002 /* Handle DW_LNE_end_sequence. */
21003 void handle_end_sequence ()
21004 {
21005 m_record_line_callback = ::record_line;
21006 }
21007
21008 private:
21009 /* Advance the line by LINE_DELTA. */
21010 void advance_line (int line_delta)
21011 {
21012 m_line += line_delta;
21013
21014 if (line_delta != 0)
21015 m_line_has_non_zero_discriminator = m_discriminator != 0;
21016 }
21017
21018 gdbarch *m_gdbarch;
21019
21020 /* True if we're recording lines.
21021 Otherwise we're building partial symtabs and are just interested in
21022 finding include files mentioned by the line number program. */
21023 bool m_record_lines_p;
21024
21025 /* The line number header. */
21026 line_header *m_line_header;
21027
21028 /* These are part of the standard DWARF line number state machine,
21029 and initialized according to the DWARF spec. */
21030
21031 unsigned char m_op_index = 0;
21032 /* The line table index (1-based) of the current file. */
21033 file_name_index m_file = (file_name_index) 1;
21034 unsigned int m_line = 1;
21035
21036 /* These are initialized in the constructor. */
21037
21038 CORE_ADDR m_address;
21039 bool m_is_stmt;
21040 unsigned int m_discriminator;
21041
21042 /* Additional bits of state we need to track. */
21043
21044 /* The last file that we called dwarf2_start_subfile for.
21045 This is only used for TLLs. */
21046 unsigned int m_last_file = 0;
21047 /* The last file a line number was recorded for. */
21048 struct subfile *m_last_subfile = NULL;
21049
21050 /* The function to call to record a line. */
21051 record_line_ftype *m_record_line_callback = NULL;
21052
21053 /* The last line number that was recorded, used to coalesce
21054 consecutive entries for the same line. This can happen, for
21055 example, when discriminators are present. PR 17276. */
21056 unsigned int m_last_line = 0;
21057 bool m_line_has_non_zero_discriminator = false;
21058 };
21059
21060 void
21061 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21062 {
21063 CORE_ADDR addr_adj = (((m_op_index + adjust)
21064 / m_line_header->maximum_ops_per_instruction)
21065 * m_line_header->minimum_instruction_length);
21066 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21067 m_op_index = ((m_op_index + adjust)
21068 % m_line_header->maximum_ops_per_instruction);
21069 }
21070
21071 void
21072 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21073 {
21074 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21075 CORE_ADDR addr_adj = (((m_op_index
21076 + (adj_opcode / m_line_header->line_range))
21077 / m_line_header->maximum_ops_per_instruction)
21078 * m_line_header->minimum_instruction_length);
21079 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21080 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21081 % m_line_header->maximum_ops_per_instruction);
21082
21083 int line_delta = (m_line_header->line_base
21084 + (adj_opcode % m_line_header->line_range));
21085 advance_line (line_delta);
21086 record_line (false);
21087 m_discriminator = 0;
21088 }
21089
21090 void
21091 lnp_state_machine::handle_set_file (file_name_index file)
21092 {
21093 m_file = file;
21094
21095 const file_entry *fe = current_file ();
21096 if (fe == NULL)
21097 dwarf2_debug_line_missing_file_complaint ();
21098 else if (m_record_lines_p)
21099 {
21100 const char *dir = fe->include_dir (m_line_header);
21101
21102 m_last_subfile = current_subfile;
21103 m_line_has_non_zero_discriminator = m_discriminator != 0;
21104 dwarf2_start_subfile (fe->name, dir);
21105 }
21106 }
21107
21108 void
21109 lnp_state_machine::handle_const_add_pc ()
21110 {
21111 CORE_ADDR adjust
21112 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21113
21114 CORE_ADDR addr_adj
21115 = (((m_op_index + adjust)
21116 / m_line_header->maximum_ops_per_instruction)
21117 * m_line_header->minimum_instruction_length);
21118
21119 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21120 m_op_index = ((m_op_index + adjust)
21121 % m_line_header->maximum_ops_per_instruction);
21122 }
21123
21124 /* Ignore this record_line request. */
21125
21126 static void
21127 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
21128 {
21129 return;
21130 }
21131
21132 /* Return non-zero if we should add LINE to the line number table.
21133 LINE is the line to add, LAST_LINE is the last line that was added,
21134 LAST_SUBFILE is the subfile for LAST_LINE.
21135 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21136 had a non-zero discriminator.
21137
21138 We have to be careful in the presence of discriminators.
21139 E.g., for this line:
21140
21141 for (i = 0; i < 100000; i++);
21142
21143 clang can emit four line number entries for that one line,
21144 each with a different discriminator.
21145 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21146
21147 However, we want gdb to coalesce all four entries into one.
21148 Otherwise the user could stepi into the middle of the line and
21149 gdb would get confused about whether the pc really was in the
21150 middle of the line.
21151
21152 Things are further complicated by the fact that two consecutive
21153 line number entries for the same line is a heuristic used by gcc
21154 to denote the end of the prologue. So we can't just discard duplicate
21155 entries, we have to be selective about it. The heuristic we use is
21156 that we only collapse consecutive entries for the same line if at least
21157 one of those entries has a non-zero discriminator. PR 17276.
21158
21159 Note: Addresses in the line number state machine can never go backwards
21160 within one sequence, thus this coalescing is ok. */
21161
21162 static int
21163 dwarf_record_line_p (unsigned int line, unsigned int last_line,
21164 int line_has_non_zero_discriminator,
21165 struct subfile *last_subfile)
21166 {
21167 if (current_subfile != last_subfile)
21168 return 1;
21169 if (line != last_line)
21170 return 1;
21171 /* Same line for the same file that we've seen already.
21172 As a last check, for pr 17276, only record the line if the line
21173 has never had a non-zero discriminator. */
21174 if (!line_has_non_zero_discriminator)
21175 return 1;
21176 return 0;
21177 }
21178
21179 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
21180 in the line table of subfile SUBFILE. */
21181
21182 static void
21183 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21184 unsigned int line, CORE_ADDR address,
21185 record_line_ftype p_record_line)
21186 {
21187 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21188
21189 if (dwarf_line_debug)
21190 {
21191 fprintf_unfiltered (gdb_stdlog,
21192 "Recording line %u, file %s, address %s\n",
21193 line, lbasename (subfile->name),
21194 paddress (gdbarch, address));
21195 }
21196
21197 (*p_record_line) (subfile, line, addr);
21198 }
21199
21200 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21201 Mark the end of a set of line number records.
21202 The arguments are the same as for dwarf_record_line_1.
21203 If SUBFILE is NULL the request is ignored. */
21204
21205 static void
21206 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21207 CORE_ADDR address, record_line_ftype p_record_line)
21208 {
21209 if (subfile == NULL)
21210 return;
21211
21212 if (dwarf_line_debug)
21213 {
21214 fprintf_unfiltered (gdb_stdlog,
21215 "Finishing current line, file %s, address %s\n",
21216 lbasename (subfile->name),
21217 paddress (gdbarch, address));
21218 }
21219
21220 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
21221 }
21222
21223 void
21224 lnp_state_machine::record_line (bool end_sequence)
21225 {
21226 if (dwarf_line_debug)
21227 {
21228 fprintf_unfiltered (gdb_stdlog,
21229 "Processing actual line %u: file %u,"
21230 " address %s, is_stmt %u, discrim %u\n",
21231 m_line, to_underlying (m_file),
21232 paddress (m_gdbarch, m_address),
21233 m_is_stmt, m_discriminator);
21234 }
21235
21236 file_entry *fe = current_file ();
21237
21238 if (fe == NULL)
21239 dwarf2_debug_line_missing_file_complaint ();
21240 /* For now we ignore lines not starting on an instruction boundary.
21241 But not when processing end_sequence for compatibility with the
21242 previous version of the code. */
21243 else if (m_op_index == 0 || end_sequence)
21244 {
21245 fe->included_p = 1;
21246 if (m_record_lines_p && m_is_stmt)
21247 {
21248 if (m_last_subfile != current_subfile || end_sequence)
21249 {
21250 dwarf_finish_line (m_gdbarch, m_last_subfile,
21251 m_address, m_record_line_callback);
21252 }
21253
21254 if (!end_sequence)
21255 {
21256 if (dwarf_record_line_p (m_line, m_last_line,
21257 m_line_has_non_zero_discriminator,
21258 m_last_subfile))
21259 {
21260 dwarf_record_line_1 (m_gdbarch, current_subfile,
21261 m_line, m_address,
21262 m_record_line_callback);
21263 }
21264 m_last_subfile = current_subfile;
21265 m_last_line = m_line;
21266 }
21267 }
21268 }
21269 }
21270
21271 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
21272 bool record_lines_p)
21273 {
21274 m_gdbarch = arch;
21275 m_record_lines_p = record_lines_p;
21276 m_line_header = lh;
21277
21278 m_record_line_callback = ::record_line;
21279
21280 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21281 was a line entry for it so that the backend has a chance to adjust it
21282 and also record it in case it needs it. This is currently used by MIPS
21283 code, cf. `mips_adjust_dwarf2_line'. */
21284 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21285 m_is_stmt = lh->default_is_stmt;
21286 m_discriminator = 0;
21287 }
21288
21289 void
21290 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21291 const gdb_byte *line_ptr,
21292 CORE_ADDR lowpc, CORE_ADDR address)
21293 {
21294 /* If address < lowpc then it's not a usable value, it's outside the
21295 pc range of the CU. However, we restrict the test to only address
21296 values of zero to preserve GDB's previous behaviour which is to
21297 handle the specific case of a function being GC'd by the linker. */
21298
21299 if (address == 0 && address < lowpc)
21300 {
21301 /* This line table is for a function which has been
21302 GCd by the linker. Ignore it. PR gdb/12528 */
21303
21304 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21305 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21306
21307 complaint (&symfile_complaints,
21308 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21309 line_offset, objfile_name (objfile));
21310 m_record_line_callback = noop_record_line;
21311 /* Note: record_line_callback is left as noop_record_line until
21312 we see DW_LNE_end_sequence. */
21313 }
21314 }
21315
21316 /* Subroutine of dwarf_decode_lines to simplify it.
21317 Process the line number information in LH.
21318 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21319 program in order to set included_p for every referenced header. */
21320
21321 static void
21322 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21323 const int decode_for_pst_p, CORE_ADDR lowpc)
21324 {
21325 const gdb_byte *line_ptr, *extended_end;
21326 const gdb_byte *line_end;
21327 unsigned int bytes_read, extended_len;
21328 unsigned char op_code, extended_op;
21329 CORE_ADDR baseaddr;
21330 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21331 bfd *abfd = objfile->obfd;
21332 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21333 /* True if we're recording line info (as opposed to building partial
21334 symtabs and just interested in finding include files mentioned by
21335 the line number program). */
21336 bool record_lines_p = !decode_for_pst_p;
21337
21338 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21339
21340 line_ptr = lh->statement_program_start;
21341 line_end = lh->statement_program_end;
21342
21343 /* Read the statement sequences until there's nothing left. */
21344 while (line_ptr < line_end)
21345 {
21346 /* The DWARF line number program state machine. Reset the state
21347 machine at the start of each sequence. */
21348 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
21349 bool end_sequence = false;
21350
21351 if (record_lines_p)
21352 {
21353 /* Start a subfile for the current file of the state
21354 machine. */
21355 const file_entry *fe = state_machine.current_file ();
21356
21357 if (fe != NULL)
21358 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
21359 }
21360
21361 /* Decode the table. */
21362 while (line_ptr < line_end && !end_sequence)
21363 {
21364 op_code = read_1_byte (abfd, line_ptr);
21365 line_ptr += 1;
21366
21367 if (op_code >= lh->opcode_base)
21368 {
21369 /* Special opcode. */
21370 state_machine.handle_special_opcode (op_code);
21371 }
21372 else switch (op_code)
21373 {
21374 case DW_LNS_extended_op:
21375 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21376 &bytes_read);
21377 line_ptr += bytes_read;
21378 extended_end = line_ptr + extended_len;
21379 extended_op = read_1_byte (abfd, line_ptr);
21380 line_ptr += 1;
21381 switch (extended_op)
21382 {
21383 case DW_LNE_end_sequence:
21384 state_machine.handle_end_sequence ();
21385 end_sequence = true;
21386 break;
21387 case DW_LNE_set_address:
21388 {
21389 CORE_ADDR address
21390 = read_address (abfd, line_ptr, cu, &bytes_read);
21391 line_ptr += bytes_read;
21392
21393 state_machine.check_line_address (cu, line_ptr,
21394 lowpc, address);
21395 state_machine.handle_set_address (baseaddr, address);
21396 }
21397 break;
21398 case DW_LNE_define_file:
21399 {
21400 const char *cur_file;
21401 unsigned int mod_time, length;
21402 dir_index dindex;
21403
21404 cur_file = read_direct_string (abfd, line_ptr,
21405 &bytes_read);
21406 line_ptr += bytes_read;
21407 dindex = (dir_index)
21408 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21409 line_ptr += bytes_read;
21410 mod_time =
21411 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21412 line_ptr += bytes_read;
21413 length =
21414 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21415 line_ptr += bytes_read;
21416 lh->add_file_name (cur_file, dindex, mod_time, length);
21417 }
21418 break;
21419 case DW_LNE_set_discriminator:
21420 {
21421 /* The discriminator is not interesting to the
21422 debugger; just ignore it. We still need to
21423 check its value though:
21424 if there are consecutive entries for the same
21425 (non-prologue) line we want to coalesce them.
21426 PR 17276. */
21427 unsigned int discr
21428 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21429 line_ptr += bytes_read;
21430
21431 state_machine.handle_set_discriminator (discr);
21432 }
21433 break;
21434 default:
21435 complaint (&symfile_complaints,
21436 _("mangled .debug_line section"));
21437 return;
21438 }
21439 /* Make sure that we parsed the extended op correctly. If e.g.
21440 we expected a different address size than the producer used,
21441 we may have read the wrong number of bytes. */
21442 if (line_ptr != extended_end)
21443 {
21444 complaint (&symfile_complaints,
21445 _("mangled .debug_line section"));
21446 return;
21447 }
21448 break;
21449 case DW_LNS_copy:
21450 state_machine.handle_copy ();
21451 break;
21452 case DW_LNS_advance_pc:
21453 {
21454 CORE_ADDR adjust
21455 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21456 line_ptr += bytes_read;
21457
21458 state_machine.handle_advance_pc (adjust);
21459 }
21460 break;
21461 case DW_LNS_advance_line:
21462 {
21463 int line_delta
21464 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21465 line_ptr += bytes_read;
21466
21467 state_machine.handle_advance_line (line_delta);
21468 }
21469 break;
21470 case DW_LNS_set_file:
21471 {
21472 file_name_index file
21473 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21474 &bytes_read);
21475 line_ptr += bytes_read;
21476
21477 state_machine.handle_set_file (file);
21478 }
21479 break;
21480 case DW_LNS_set_column:
21481 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21482 line_ptr += bytes_read;
21483 break;
21484 case DW_LNS_negate_stmt:
21485 state_machine.handle_negate_stmt ();
21486 break;
21487 case DW_LNS_set_basic_block:
21488 break;
21489 /* Add to the address register of the state machine the
21490 address increment value corresponding to special opcode
21491 255. I.e., this value is scaled by the minimum
21492 instruction length since special opcode 255 would have
21493 scaled the increment. */
21494 case DW_LNS_const_add_pc:
21495 state_machine.handle_const_add_pc ();
21496 break;
21497 case DW_LNS_fixed_advance_pc:
21498 {
21499 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21500 line_ptr += 2;
21501
21502 state_machine.handle_fixed_advance_pc (addr_adj);
21503 }
21504 break;
21505 default:
21506 {
21507 /* Unknown standard opcode, ignore it. */
21508 int i;
21509
21510 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21511 {
21512 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21513 line_ptr += bytes_read;
21514 }
21515 }
21516 }
21517 }
21518
21519 if (!end_sequence)
21520 dwarf2_debug_line_missing_end_sequence_complaint ();
21521
21522 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21523 in which case we still finish recording the last line). */
21524 state_machine.record_line (true);
21525 }
21526 }
21527
21528 /* Decode the Line Number Program (LNP) for the given line_header
21529 structure and CU. The actual information extracted and the type
21530 of structures created from the LNP depends on the value of PST.
21531
21532 1. If PST is NULL, then this procedure uses the data from the program
21533 to create all necessary symbol tables, and their linetables.
21534
21535 2. If PST is not NULL, this procedure reads the program to determine
21536 the list of files included by the unit represented by PST, and
21537 builds all the associated partial symbol tables.
21538
21539 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21540 It is used for relative paths in the line table.
21541 NOTE: When processing partial symtabs (pst != NULL),
21542 comp_dir == pst->dirname.
21543
21544 NOTE: It is important that psymtabs have the same file name (via strcmp)
21545 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21546 symtab we don't use it in the name of the psymtabs we create.
21547 E.g. expand_line_sal requires this when finding psymtabs to expand.
21548 A good testcase for this is mb-inline.exp.
21549
21550 LOWPC is the lowest address in CU (or 0 if not known).
21551
21552 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21553 for its PC<->lines mapping information. Otherwise only the filename
21554 table is read in. */
21555
21556 static void
21557 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21558 struct dwarf2_cu *cu, struct partial_symtab *pst,
21559 CORE_ADDR lowpc, int decode_mapping)
21560 {
21561 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21562 const int decode_for_pst_p = (pst != NULL);
21563
21564 if (decode_mapping)
21565 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21566
21567 if (decode_for_pst_p)
21568 {
21569 int file_index;
21570
21571 /* Now that we're done scanning the Line Header Program, we can
21572 create the psymtab of each included file. */
21573 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21574 if (lh->file_names[file_index].included_p == 1)
21575 {
21576 gdb::unique_xmalloc_ptr<char> name_holder;
21577 const char *include_name =
21578 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21579 &name_holder);
21580 if (include_name != NULL)
21581 dwarf2_create_include_psymtab (include_name, pst, objfile);
21582 }
21583 }
21584 else
21585 {
21586 /* Make sure a symtab is created for every file, even files
21587 which contain only variables (i.e. no code with associated
21588 line numbers). */
21589 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21590 int i;
21591
21592 for (i = 0; i < lh->file_names.size (); i++)
21593 {
21594 file_entry &fe = lh->file_names[i];
21595
21596 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21597
21598 if (current_subfile->symtab == NULL)
21599 {
21600 current_subfile->symtab
21601 = allocate_symtab (cust, current_subfile->name);
21602 }
21603 fe.symtab = current_subfile->symtab;
21604 }
21605 }
21606 }
21607
21608 /* Start a subfile for DWARF. FILENAME is the name of the file and
21609 DIRNAME the name of the source directory which contains FILENAME
21610 or NULL if not known.
21611 This routine tries to keep line numbers from identical absolute and
21612 relative file names in a common subfile.
21613
21614 Using the `list' example from the GDB testsuite, which resides in
21615 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21616 of /srcdir/list0.c yields the following debugging information for list0.c:
21617
21618 DW_AT_name: /srcdir/list0.c
21619 DW_AT_comp_dir: /compdir
21620 files.files[0].name: list0.h
21621 files.files[0].dir: /srcdir
21622 files.files[1].name: list0.c
21623 files.files[1].dir: /srcdir
21624
21625 The line number information for list0.c has to end up in a single
21626 subfile, so that `break /srcdir/list0.c:1' works as expected.
21627 start_subfile will ensure that this happens provided that we pass the
21628 concatenation of files.files[1].dir and files.files[1].name as the
21629 subfile's name. */
21630
21631 static void
21632 dwarf2_start_subfile (const char *filename, const char *dirname)
21633 {
21634 char *copy = NULL;
21635
21636 /* In order not to lose the line information directory,
21637 we concatenate it to the filename when it makes sense.
21638 Note that the Dwarf3 standard says (speaking of filenames in line
21639 information): ``The directory index is ignored for file names
21640 that represent full path names''. Thus ignoring dirname in the
21641 `else' branch below isn't an issue. */
21642
21643 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21644 {
21645 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21646 filename = copy;
21647 }
21648
21649 start_subfile (filename);
21650
21651 if (copy != NULL)
21652 xfree (copy);
21653 }
21654
21655 /* Start a symtab for DWARF.
21656 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21657
21658 static struct compunit_symtab *
21659 dwarf2_start_symtab (struct dwarf2_cu *cu,
21660 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21661 {
21662 struct compunit_symtab *cust
21663 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21664 low_pc, cu->language);
21665
21666 record_debugformat ("DWARF 2");
21667 record_producer (cu->producer);
21668
21669 /* We assume that we're processing GCC output. */
21670 processing_gcc_compilation = 2;
21671
21672 cu->processing_has_namespace_info = 0;
21673
21674 return cust;
21675 }
21676
21677 static void
21678 var_decode_location (struct attribute *attr, struct symbol *sym,
21679 struct dwarf2_cu *cu)
21680 {
21681 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21682 struct comp_unit_head *cu_header = &cu->header;
21683
21684 /* NOTE drow/2003-01-30: There used to be a comment and some special
21685 code here to turn a symbol with DW_AT_external and a
21686 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21687 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21688 with some versions of binutils) where shared libraries could have
21689 relocations against symbols in their debug information - the
21690 minimal symbol would have the right address, but the debug info
21691 would not. It's no longer necessary, because we will explicitly
21692 apply relocations when we read in the debug information now. */
21693
21694 /* A DW_AT_location attribute with no contents indicates that a
21695 variable has been optimized away. */
21696 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21697 {
21698 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21699 return;
21700 }
21701
21702 /* Handle one degenerate form of location expression specially, to
21703 preserve GDB's previous behavior when section offsets are
21704 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21705 then mark this symbol as LOC_STATIC. */
21706
21707 if (attr_form_is_block (attr)
21708 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21709 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21710 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21711 && (DW_BLOCK (attr)->size
21712 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21713 {
21714 unsigned int dummy;
21715
21716 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21717 SYMBOL_VALUE_ADDRESS (sym) =
21718 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21719 else
21720 SYMBOL_VALUE_ADDRESS (sym) =
21721 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21722 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21723 fixup_symbol_section (sym, objfile);
21724 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21725 SYMBOL_SECTION (sym));
21726 return;
21727 }
21728
21729 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21730 expression evaluator, and use LOC_COMPUTED only when necessary
21731 (i.e. when the value of a register or memory location is
21732 referenced, or a thread-local block, etc.). Then again, it might
21733 not be worthwhile. I'm assuming that it isn't unless performance
21734 or memory numbers show me otherwise. */
21735
21736 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21737
21738 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21739 cu->has_loclist = 1;
21740 }
21741
21742 /* Given a pointer to a DWARF information entry, figure out if we need
21743 to make a symbol table entry for it, and if so, create a new entry
21744 and return a pointer to it.
21745 If TYPE is NULL, determine symbol type from the die, otherwise
21746 used the passed type.
21747 If SPACE is not NULL, use it to hold the new symbol. If it is
21748 NULL, allocate a new symbol on the objfile's obstack. */
21749
21750 static struct symbol *
21751 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21752 struct symbol *space)
21753 {
21754 struct dwarf2_per_objfile *dwarf2_per_objfile
21755 = cu->per_cu->dwarf2_per_objfile;
21756 struct objfile *objfile = dwarf2_per_objfile->objfile;
21757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21758 struct symbol *sym = NULL;
21759 const char *name;
21760 struct attribute *attr = NULL;
21761 struct attribute *attr2 = NULL;
21762 CORE_ADDR baseaddr;
21763 struct pending **list_to_add = NULL;
21764
21765 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21766
21767 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21768
21769 name = dwarf2_name (die, cu);
21770 if (name)
21771 {
21772 const char *linkagename;
21773 int suppress_add = 0;
21774
21775 if (space)
21776 sym = space;
21777 else
21778 sym = allocate_symbol (objfile);
21779 OBJSTAT (objfile, n_syms++);
21780
21781 /* Cache this symbol's name and the name's demangled form (if any). */
21782 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21783 linkagename = dwarf2_physname (name, die, cu);
21784 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21785
21786 /* Fortran does not have mangling standard and the mangling does differ
21787 between gfortran, iFort etc. */
21788 if (cu->language == language_fortran
21789 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21790 symbol_set_demangled_name (&(sym->ginfo),
21791 dwarf2_full_name (name, die, cu),
21792 NULL);
21793
21794 /* Default assumptions.
21795 Use the passed type or decode it from the die. */
21796 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21797 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21798 if (type != NULL)
21799 SYMBOL_TYPE (sym) = type;
21800 else
21801 SYMBOL_TYPE (sym) = die_type (die, cu);
21802 attr = dwarf2_attr (die,
21803 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21804 cu);
21805 if (attr)
21806 {
21807 SYMBOL_LINE (sym) = DW_UNSND (attr);
21808 }
21809
21810 attr = dwarf2_attr (die,
21811 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21812 cu);
21813 if (attr)
21814 {
21815 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21816 struct file_entry *fe;
21817
21818 if (cu->line_header != NULL)
21819 fe = cu->line_header->file_name_at (file_index);
21820 else
21821 fe = NULL;
21822
21823 if (fe == NULL)
21824 complaint (&symfile_complaints,
21825 _("file index out of range"));
21826 else
21827 symbol_set_symtab (sym, fe->symtab);
21828 }
21829
21830 switch (die->tag)
21831 {
21832 case DW_TAG_label:
21833 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21834 if (attr)
21835 {
21836 CORE_ADDR addr;
21837
21838 addr = attr_value_as_address (attr);
21839 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21840 SYMBOL_VALUE_ADDRESS (sym) = addr;
21841 }
21842 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21843 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21844 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21845 add_symbol_to_list (sym, cu->list_in_scope);
21846 break;
21847 case DW_TAG_subprogram:
21848 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21849 finish_block. */
21850 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21851 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21852 if ((attr2 && (DW_UNSND (attr2) != 0))
21853 || cu->language == language_ada)
21854 {
21855 /* Subprograms marked external are stored as a global symbol.
21856 Ada subprograms, whether marked external or not, are always
21857 stored as a global symbol, because we want to be able to
21858 access them globally. For instance, we want to be able
21859 to break on a nested subprogram without having to
21860 specify the context. */
21861 list_to_add = &global_symbols;
21862 }
21863 else
21864 {
21865 list_to_add = cu->list_in_scope;
21866 }
21867 break;
21868 case DW_TAG_inlined_subroutine:
21869 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21870 finish_block. */
21871 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21872 SYMBOL_INLINED (sym) = 1;
21873 list_to_add = cu->list_in_scope;
21874 break;
21875 case DW_TAG_template_value_param:
21876 suppress_add = 1;
21877 /* Fall through. */
21878 case DW_TAG_constant:
21879 case DW_TAG_variable:
21880 case DW_TAG_member:
21881 /* Compilation with minimal debug info may result in
21882 variables with missing type entries. Change the
21883 misleading `void' type to something sensible. */
21884 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21885 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21886
21887 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21888 /* In the case of DW_TAG_member, we should only be called for
21889 static const members. */
21890 if (die->tag == DW_TAG_member)
21891 {
21892 /* dwarf2_add_field uses die_is_declaration,
21893 so we do the same. */
21894 gdb_assert (die_is_declaration (die, cu));
21895 gdb_assert (attr);
21896 }
21897 if (attr)
21898 {
21899 dwarf2_const_value (attr, sym, cu);
21900 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21901 if (!suppress_add)
21902 {
21903 if (attr2 && (DW_UNSND (attr2) != 0))
21904 list_to_add = &global_symbols;
21905 else
21906 list_to_add = cu->list_in_scope;
21907 }
21908 break;
21909 }
21910 attr = dwarf2_attr (die, DW_AT_location, cu);
21911 if (attr)
21912 {
21913 var_decode_location (attr, sym, cu);
21914 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21915
21916 /* Fortran explicitly imports any global symbols to the local
21917 scope by DW_TAG_common_block. */
21918 if (cu->language == language_fortran && die->parent
21919 && die->parent->tag == DW_TAG_common_block)
21920 attr2 = NULL;
21921
21922 if (SYMBOL_CLASS (sym) == LOC_STATIC
21923 && SYMBOL_VALUE_ADDRESS (sym) == 0
21924 && !dwarf2_per_objfile->has_section_at_zero)
21925 {
21926 /* When a static variable is eliminated by the linker,
21927 the corresponding debug information is not stripped
21928 out, but the variable address is set to null;
21929 do not add such variables into symbol table. */
21930 }
21931 else if (attr2 && (DW_UNSND (attr2) != 0))
21932 {
21933 /* Workaround gfortran PR debug/40040 - it uses
21934 DW_AT_location for variables in -fPIC libraries which may
21935 get overriden by other libraries/executable and get
21936 a different address. Resolve it by the minimal symbol
21937 which may come from inferior's executable using copy
21938 relocation. Make this workaround only for gfortran as for
21939 other compilers GDB cannot guess the minimal symbol
21940 Fortran mangling kind. */
21941 if (cu->language == language_fortran && die->parent
21942 && die->parent->tag == DW_TAG_module
21943 && cu->producer
21944 && startswith (cu->producer, "GNU Fortran"))
21945 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21946
21947 /* A variable with DW_AT_external is never static,
21948 but it may be block-scoped. */
21949 list_to_add = (cu->list_in_scope == &file_symbols
21950 ? &global_symbols : cu->list_in_scope);
21951 }
21952 else
21953 list_to_add = cu->list_in_scope;
21954 }
21955 else
21956 {
21957 /* We do not know the address of this symbol.
21958 If it is an external symbol and we have type information
21959 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21960 The address of the variable will then be determined from
21961 the minimal symbol table whenever the variable is
21962 referenced. */
21963 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21964
21965 /* Fortran explicitly imports any global symbols to the local
21966 scope by DW_TAG_common_block. */
21967 if (cu->language == language_fortran && die->parent
21968 && die->parent->tag == DW_TAG_common_block)
21969 {
21970 /* SYMBOL_CLASS doesn't matter here because
21971 read_common_block is going to reset it. */
21972 if (!suppress_add)
21973 list_to_add = cu->list_in_scope;
21974 }
21975 else if (attr2 && (DW_UNSND (attr2) != 0)
21976 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21977 {
21978 /* A variable with DW_AT_external is never static, but it
21979 may be block-scoped. */
21980 list_to_add = (cu->list_in_scope == &file_symbols
21981 ? &global_symbols : cu->list_in_scope);
21982
21983 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21984 }
21985 else if (!die_is_declaration (die, cu))
21986 {
21987 /* Use the default LOC_OPTIMIZED_OUT class. */
21988 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21989 if (!suppress_add)
21990 list_to_add = cu->list_in_scope;
21991 }
21992 }
21993 break;
21994 case DW_TAG_formal_parameter:
21995 /* If we are inside a function, mark this as an argument. If
21996 not, we might be looking at an argument to an inlined function
21997 when we do not have enough information to show inlined frames;
21998 pretend it's a local variable in that case so that the user can
21999 still see it. */
22000 if (context_stack_depth > 0
22001 && context_stack[context_stack_depth - 1].name != NULL)
22002 SYMBOL_IS_ARGUMENT (sym) = 1;
22003 attr = dwarf2_attr (die, DW_AT_location, cu);
22004 if (attr)
22005 {
22006 var_decode_location (attr, sym, cu);
22007 }
22008 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22009 if (attr)
22010 {
22011 dwarf2_const_value (attr, sym, cu);
22012 }
22013
22014 list_to_add = cu->list_in_scope;
22015 break;
22016 case DW_TAG_unspecified_parameters:
22017 /* From varargs functions; gdb doesn't seem to have any
22018 interest in this information, so just ignore it for now.
22019 (FIXME?) */
22020 break;
22021 case DW_TAG_template_type_param:
22022 suppress_add = 1;
22023 /* Fall through. */
22024 case DW_TAG_class_type:
22025 case DW_TAG_interface_type:
22026 case DW_TAG_structure_type:
22027 case DW_TAG_union_type:
22028 case DW_TAG_set_type:
22029 case DW_TAG_enumeration_type:
22030 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22031 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22032
22033 {
22034 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22035 really ever be static objects: otherwise, if you try
22036 to, say, break of a class's method and you're in a file
22037 which doesn't mention that class, it won't work unless
22038 the check for all static symbols in lookup_symbol_aux
22039 saves you. See the OtherFileClass tests in
22040 gdb.c++/namespace.exp. */
22041
22042 if (!suppress_add)
22043 {
22044 list_to_add = (cu->list_in_scope == &file_symbols
22045 && cu->language == language_cplus
22046 ? &global_symbols : cu->list_in_scope);
22047
22048 /* The semantics of C++ state that "struct foo {
22049 ... }" also defines a typedef for "foo". */
22050 if (cu->language == language_cplus
22051 || cu->language == language_ada
22052 || cu->language == language_d
22053 || cu->language == language_rust)
22054 {
22055 /* The symbol's name is already allocated along
22056 with this objfile, so we don't need to
22057 duplicate it for the type. */
22058 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22059 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
22060 }
22061 }
22062 }
22063 break;
22064 case DW_TAG_typedef:
22065 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22066 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22067 list_to_add = cu->list_in_scope;
22068 break;
22069 case DW_TAG_base_type:
22070 case DW_TAG_subrange_type:
22071 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22072 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22073 list_to_add = cu->list_in_scope;
22074 break;
22075 case DW_TAG_enumerator:
22076 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22077 if (attr)
22078 {
22079 dwarf2_const_value (attr, sym, cu);
22080 }
22081 {
22082 /* NOTE: carlton/2003-11-10: See comment above in the
22083 DW_TAG_class_type, etc. block. */
22084
22085 list_to_add = (cu->list_in_scope == &file_symbols
22086 && cu->language == language_cplus
22087 ? &global_symbols : cu->list_in_scope);
22088 }
22089 break;
22090 case DW_TAG_imported_declaration:
22091 case DW_TAG_namespace:
22092 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22093 list_to_add = &global_symbols;
22094 break;
22095 case DW_TAG_module:
22096 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22097 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22098 list_to_add = &global_symbols;
22099 break;
22100 case DW_TAG_common_block:
22101 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22102 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22103 add_symbol_to_list (sym, cu->list_in_scope);
22104 break;
22105 default:
22106 /* Not a tag we recognize. Hopefully we aren't processing
22107 trash data, but since we must specifically ignore things
22108 we don't recognize, there is nothing else we should do at
22109 this point. */
22110 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
22111 dwarf_tag_name (die->tag));
22112 break;
22113 }
22114
22115 if (suppress_add)
22116 {
22117 sym->hash_next = objfile->template_symbols;
22118 objfile->template_symbols = sym;
22119 list_to_add = NULL;
22120 }
22121
22122 if (list_to_add != NULL)
22123 add_symbol_to_list (sym, list_to_add);
22124
22125 /* For the benefit of old versions of GCC, check for anonymous
22126 namespaces based on the demangled name. */
22127 if (!cu->processing_has_namespace_info
22128 && cu->language == language_cplus)
22129 cp_scan_for_anonymous_namespaces (sym, objfile);
22130 }
22131 return (sym);
22132 }
22133
22134 /* Given an attr with a DW_FORM_dataN value in host byte order,
22135 zero-extend it as appropriate for the symbol's type. The DWARF
22136 standard (v4) is not entirely clear about the meaning of using
22137 DW_FORM_dataN for a constant with a signed type, where the type is
22138 wider than the data. The conclusion of a discussion on the DWARF
22139 list was that this is unspecified. We choose to always zero-extend
22140 because that is the interpretation long in use by GCC. */
22141
22142 static gdb_byte *
22143 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22144 struct dwarf2_cu *cu, LONGEST *value, int bits)
22145 {
22146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22147 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22148 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22149 LONGEST l = DW_UNSND (attr);
22150
22151 if (bits < sizeof (*value) * 8)
22152 {
22153 l &= ((LONGEST) 1 << bits) - 1;
22154 *value = l;
22155 }
22156 else if (bits == sizeof (*value) * 8)
22157 *value = l;
22158 else
22159 {
22160 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22161 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22162 return bytes;
22163 }
22164
22165 return NULL;
22166 }
22167
22168 /* Read a constant value from an attribute. Either set *VALUE, or if
22169 the value does not fit in *VALUE, set *BYTES - either already
22170 allocated on the objfile obstack, or newly allocated on OBSTACK,
22171 or, set *BATON, if we translated the constant to a location
22172 expression. */
22173
22174 static void
22175 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22176 const char *name, struct obstack *obstack,
22177 struct dwarf2_cu *cu,
22178 LONGEST *value, const gdb_byte **bytes,
22179 struct dwarf2_locexpr_baton **baton)
22180 {
22181 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22182 struct comp_unit_head *cu_header = &cu->header;
22183 struct dwarf_block *blk;
22184 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22185 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22186
22187 *value = 0;
22188 *bytes = NULL;
22189 *baton = NULL;
22190
22191 switch (attr->form)
22192 {
22193 case DW_FORM_addr:
22194 case DW_FORM_GNU_addr_index:
22195 {
22196 gdb_byte *data;
22197
22198 if (TYPE_LENGTH (type) != cu_header->addr_size)
22199 dwarf2_const_value_length_mismatch_complaint (name,
22200 cu_header->addr_size,
22201 TYPE_LENGTH (type));
22202 /* Symbols of this form are reasonably rare, so we just
22203 piggyback on the existing location code rather than writing
22204 a new implementation of symbol_computed_ops. */
22205 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22206 (*baton)->per_cu = cu->per_cu;
22207 gdb_assert ((*baton)->per_cu);
22208
22209 (*baton)->size = 2 + cu_header->addr_size;
22210 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22211 (*baton)->data = data;
22212
22213 data[0] = DW_OP_addr;
22214 store_unsigned_integer (&data[1], cu_header->addr_size,
22215 byte_order, DW_ADDR (attr));
22216 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22217 }
22218 break;
22219 case DW_FORM_string:
22220 case DW_FORM_strp:
22221 case DW_FORM_GNU_str_index:
22222 case DW_FORM_GNU_strp_alt:
22223 /* DW_STRING is already allocated on the objfile obstack, point
22224 directly to it. */
22225 *bytes = (const gdb_byte *) DW_STRING (attr);
22226 break;
22227 case DW_FORM_block1:
22228 case DW_FORM_block2:
22229 case DW_FORM_block4:
22230 case DW_FORM_block:
22231 case DW_FORM_exprloc:
22232 case DW_FORM_data16:
22233 blk = DW_BLOCK (attr);
22234 if (TYPE_LENGTH (type) != blk->size)
22235 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22236 TYPE_LENGTH (type));
22237 *bytes = blk->data;
22238 break;
22239
22240 /* The DW_AT_const_value attributes are supposed to carry the
22241 symbol's value "represented as it would be on the target
22242 architecture." By the time we get here, it's already been
22243 converted to host endianness, so we just need to sign- or
22244 zero-extend it as appropriate. */
22245 case DW_FORM_data1:
22246 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22247 break;
22248 case DW_FORM_data2:
22249 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22250 break;
22251 case DW_FORM_data4:
22252 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22253 break;
22254 case DW_FORM_data8:
22255 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22256 break;
22257
22258 case DW_FORM_sdata:
22259 case DW_FORM_implicit_const:
22260 *value = DW_SND (attr);
22261 break;
22262
22263 case DW_FORM_udata:
22264 *value = DW_UNSND (attr);
22265 break;
22266
22267 default:
22268 complaint (&symfile_complaints,
22269 _("unsupported const value attribute form: '%s'"),
22270 dwarf_form_name (attr->form));
22271 *value = 0;
22272 break;
22273 }
22274 }
22275
22276
22277 /* Copy constant value from an attribute to a symbol. */
22278
22279 static void
22280 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22281 struct dwarf2_cu *cu)
22282 {
22283 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22284 LONGEST value;
22285 const gdb_byte *bytes;
22286 struct dwarf2_locexpr_baton *baton;
22287
22288 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22289 SYMBOL_PRINT_NAME (sym),
22290 &objfile->objfile_obstack, cu,
22291 &value, &bytes, &baton);
22292
22293 if (baton != NULL)
22294 {
22295 SYMBOL_LOCATION_BATON (sym) = baton;
22296 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22297 }
22298 else if (bytes != NULL)
22299 {
22300 SYMBOL_VALUE_BYTES (sym) = bytes;
22301 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22302 }
22303 else
22304 {
22305 SYMBOL_VALUE (sym) = value;
22306 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22307 }
22308 }
22309
22310 /* Return the type of the die in question using its DW_AT_type attribute. */
22311
22312 static struct type *
22313 die_type (struct die_info *die, struct dwarf2_cu *cu)
22314 {
22315 struct attribute *type_attr;
22316
22317 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22318 if (!type_attr)
22319 {
22320 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22321 /* A missing DW_AT_type represents a void type. */
22322 return objfile_type (objfile)->builtin_void;
22323 }
22324
22325 return lookup_die_type (die, type_attr, cu);
22326 }
22327
22328 /* True iff CU's producer generates GNAT Ada auxiliary information
22329 that allows to find parallel types through that information instead
22330 of having to do expensive parallel lookups by type name. */
22331
22332 static int
22333 need_gnat_info (struct dwarf2_cu *cu)
22334 {
22335 /* Assume that the Ada compiler was GNAT, which always produces
22336 the auxiliary information. */
22337 return (cu->language == language_ada);
22338 }
22339
22340 /* Return the auxiliary type of the die in question using its
22341 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22342 attribute is not present. */
22343
22344 static struct type *
22345 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22346 {
22347 struct attribute *type_attr;
22348
22349 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22350 if (!type_attr)
22351 return NULL;
22352
22353 return lookup_die_type (die, type_attr, cu);
22354 }
22355
22356 /* If DIE has a descriptive_type attribute, then set the TYPE's
22357 descriptive type accordingly. */
22358
22359 static void
22360 set_descriptive_type (struct type *type, struct die_info *die,
22361 struct dwarf2_cu *cu)
22362 {
22363 struct type *descriptive_type = die_descriptive_type (die, cu);
22364
22365 if (descriptive_type)
22366 {
22367 ALLOCATE_GNAT_AUX_TYPE (type);
22368 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22369 }
22370 }
22371
22372 /* Return the containing type of the die in question using its
22373 DW_AT_containing_type attribute. */
22374
22375 static struct type *
22376 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22377 {
22378 struct attribute *type_attr;
22379 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22380
22381 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22382 if (!type_attr)
22383 error (_("Dwarf Error: Problem turning containing type into gdb type "
22384 "[in module %s]"), objfile_name (objfile));
22385
22386 return lookup_die_type (die, type_attr, cu);
22387 }
22388
22389 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22390
22391 static struct type *
22392 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22393 {
22394 struct dwarf2_per_objfile *dwarf2_per_objfile
22395 = cu->per_cu->dwarf2_per_objfile;
22396 struct objfile *objfile = dwarf2_per_objfile->objfile;
22397 char *message, *saved;
22398
22399 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
22400 objfile_name (objfile),
22401 sect_offset_str (cu->header.sect_off),
22402 sect_offset_str (die->sect_off));
22403 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22404 message, strlen (message));
22405 xfree (message);
22406
22407 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22408 }
22409
22410 /* Look up the type of DIE in CU using its type attribute ATTR.
22411 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22412 DW_AT_containing_type.
22413 If there is no type substitute an error marker. */
22414
22415 static struct type *
22416 lookup_die_type (struct die_info *die, const struct attribute *attr,
22417 struct dwarf2_cu *cu)
22418 {
22419 struct dwarf2_per_objfile *dwarf2_per_objfile
22420 = cu->per_cu->dwarf2_per_objfile;
22421 struct objfile *objfile = dwarf2_per_objfile->objfile;
22422 struct type *this_type;
22423
22424 gdb_assert (attr->name == DW_AT_type
22425 || attr->name == DW_AT_GNAT_descriptive_type
22426 || attr->name == DW_AT_containing_type);
22427
22428 /* First see if we have it cached. */
22429
22430 if (attr->form == DW_FORM_GNU_ref_alt)
22431 {
22432 struct dwarf2_per_cu_data *per_cu;
22433 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22434
22435 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22436 dwarf2_per_objfile);
22437 this_type = get_die_type_at_offset (sect_off, per_cu);
22438 }
22439 else if (attr_form_is_ref (attr))
22440 {
22441 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22442
22443 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22444 }
22445 else if (attr->form == DW_FORM_ref_sig8)
22446 {
22447 ULONGEST signature = DW_SIGNATURE (attr);
22448
22449 return get_signatured_type (die, signature, cu);
22450 }
22451 else
22452 {
22453 complaint (&symfile_complaints,
22454 _("Dwarf Error: Bad type attribute %s in DIE"
22455 " at %s [in module %s]"),
22456 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22457 objfile_name (objfile));
22458 return build_error_marker_type (cu, die);
22459 }
22460
22461 /* If not cached we need to read it in. */
22462
22463 if (this_type == NULL)
22464 {
22465 struct die_info *type_die = NULL;
22466 struct dwarf2_cu *type_cu = cu;
22467
22468 if (attr_form_is_ref (attr))
22469 type_die = follow_die_ref (die, attr, &type_cu);
22470 if (type_die == NULL)
22471 return build_error_marker_type (cu, die);
22472 /* If we find the type now, it's probably because the type came
22473 from an inter-CU reference and the type's CU got expanded before
22474 ours. */
22475 this_type = read_type_die (type_die, type_cu);
22476 }
22477
22478 /* If we still don't have a type use an error marker. */
22479
22480 if (this_type == NULL)
22481 return build_error_marker_type (cu, die);
22482
22483 return this_type;
22484 }
22485
22486 /* Return the type in DIE, CU.
22487 Returns NULL for invalid types.
22488
22489 This first does a lookup in die_type_hash,
22490 and only reads the die in if necessary.
22491
22492 NOTE: This can be called when reading in partial or full symbols. */
22493
22494 static struct type *
22495 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22496 {
22497 struct type *this_type;
22498
22499 this_type = get_die_type (die, cu);
22500 if (this_type)
22501 return this_type;
22502
22503 return read_type_die_1 (die, cu);
22504 }
22505
22506 /* Read the type in DIE, CU.
22507 Returns NULL for invalid types. */
22508
22509 static struct type *
22510 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22511 {
22512 struct type *this_type = NULL;
22513
22514 switch (die->tag)
22515 {
22516 case DW_TAG_class_type:
22517 case DW_TAG_interface_type:
22518 case DW_TAG_structure_type:
22519 case DW_TAG_union_type:
22520 this_type = read_structure_type (die, cu);
22521 break;
22522 case DW_TAG_enumeration_type:
22523 this_type = read_enumeration_type (die, cu);
22524 break;
22525 case DW_TAG_subprogram:
22526 case DW_TAG_subroutine_type:
22527 case DW_TAG_inlined_subroutine:
22528 this_type = read_subroutine_type (die, cu);
22529 break;
22530 case DW_TAG_array_type:
22531 this_type = read_array_type (die, cu);
22532 break;
22533 case DW_TAG_set_type:
22534 this_type = read_set_type (die, cu);
22535 break;
22536 case DW_TAG_pointer_type:
22537 this_type = read_tag_pointer_type (die, cu);
22538 break;
22539 case DW_TAG_ptr_to_member_type:
22540 this_type = read_tag_ptr_to_member_type (die, cu);
22541 break;
22542 case DW_TAG_reference_type:
22543 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22544 break;
22545 case DW_TAG_rvalue_reference_type:
22546 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22547 break;
22548 case DW_TAG_const_type:
22549 this_type = read_tag_const_type (die, cu);
22550 break;
22551 case DW_TAG_volatile_type:
22552 this_type = read_tag_volatile_type (die, cu);
22553 break;
22554 case DW_TAG_restrict_type:
22555 this_type = read_tag_restrict_type (die, cu);
22556 break;
22557 case DW_TAG_string_type:
22558 this_type = read_tag_string_type (die, cu);
22559 break;
22560 case DW_TAG_typedef:
22561 this_type = read_typedef (die, cu);
22562 break;
22563 case DW_TAG_subrange_type:
22564 this_type = read_subrange_type (die, cu);
22565 break;
22566 case DW_TAG_base_type:
22567 this_type = read_base_type (die, cu);
22568 break;
22569 case DW_TAG_unspecified_type:
22570 this_type = read_unspecified_type (die, cu);
22571 break;
22572 case DW_TAG_namespace:
22573 this_type = read_namespace_type (die, cu);
22574 break;
22575 case DW_TAG_module:
22576 this_type = read_module_type (die, cu);
22577 break;
22578 case DW_TAG_atomic_type:
22579 this_type = read_tag_atomic_type (die, cu);
22580 break;
22581 default:
22582 complaint (&symfile_complaints,
22583 _("unexpected tag in read_type_die: '%s'"),
22584 dwarf_tag_name (die->tag));
22585 break;
22586 }
22587
22588 return this_type;
22589 }
22590
22591 /* See if we can figure out if the class lives in a namespace. We do
22592 this by looking for a member function; its demangled name will
22593 contain namespace info, if there is any.
22594 Return the computed name or NULL.
22595 Space for the result is allocated on the objfile's obstack.
22596 This is the full-die version of guess_partial_die_structure_name.
22597 In this case we know DIE has no useful parent. */
22598
22599 static char *
22600 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22601 {
22602 struct die_info *spec_die;
22603 struct dwarf2_cu *spec_cu;
22604 struct die_info *child;
22605 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22606
22607 spec_cu = cu;
22608 spec_die = die_specification (die, &spec_cu);
22609 if (spec_die != NULL)
22610 {
22611 die = spec_die;
22612 cu = spec_cu;
22613 }
22614
22615 for (child = die->child;
22616 child != NULL;
22617 child = child->sibling)
22618 {
22619 if (child->tag == DW_TAG_subprogram)
22620 {
22621 const char *linkage_name = dw2_linkage_name (child, cu);
22622
22623 if (linkage_name != NULL)
22624 {
22625 char *actual_name
22626 = language_class_name_from_physname (cu->language_defn,
22627 linkage_name);
22628 char *name = NULL;
22629
22630 if (actual_name != NULL)
22631 {
22632 const char *die_name = dwarf2_name (die, cu);
22633
22634 if (die_name != NULL
22635 && strcmp (die_name, actual_name) != 0)
22636 {
22637 /* Strip off the class name from the full name.
22638 We want the prefix. */
22639 int die_name_len = strlen (die_name);
22640 int actual_name_len = strlen (actual_name);
22641
22642 /* Test for '::' as a sanity check. */
22643 if (actual_name_len > die_name_len + 2
22644 && actual_name[actual_name_len
22645 - die_name_len - 1] == ':')
22646 name = (char *) obstack_copy0 (
22647 &objfile->per_bfd->storage_obstack,
22648 actual_name, actual_name_len - die_name_len - 2);
22649 }
22650 }
22651 xfree (actual_name);
22652 return name;
22653 }
22654 }
22655 }
22656
22657 return NULL;
22658 }
22659
22660 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22661 prefix part in such case. See
22662 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22663
22664 static const char *
22665 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22666 {
22667 struct attribute *attr;
22668 const char *base;
22669
22670 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22671 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22672 return NULL;
22673
22674 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22675 return NULL;
22676
22677 attr = dw2_linkage_name_attr (die, cu);
22678 if (attr == NULL || DW_STRING (attr) == NULL)
22679 return NULL;
22680
22681 /* dwarf2_name had to be already called. */
22682 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22683
22684 /* Strip the base name, keep any leading namespaces/classes. */
22685 base = strrchr (DW_STRING (attr), ':');
22686 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22687 return "";
22688
22689 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22690 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22691 DW_STRING (attr),
22692 &base[-1] - DW_STRING (attr));
22693 }
22694
22695 /* Return the name of the namespace/class that DIE is defined within,
22696 or "" if we can't tell. The caller should not xfree the result.
22697
22698 For example, if we're within the method foo() in the following
22699 code:
22700
22701 namespace N {
22702 class C {
22703 void foo () {
22704 }
22705 };
22706 }
22707
22708 then determine_prefix on foo's die will return "N::C". */
22709
22710 static const char *
22711 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22712 {
22713 struct dwarf2_per_objfile *dwarf2_per_objfile
22714 = cu->per_cu->dwarf2_per_objfile;
22715 struct die_info *parent, *spec_die;
22716 struct dwarf2_cu *spec_cu;
22717 struct type *parent_type;
22718 const char *retval;
22719
22720 if (cu->language != language_cplus
22721 && cu->language != language_fortran && cu->language != language_d
22722 && cu->language != language_rust)
22723 return "";
22724
22725 retval = anonymous_struct_prefix (die, cu);
22726 if (retval)
22727 return retval;
22728
22729 /* We have to be careful in the presence of DW_AT_specification.
22730 For example, with GCC 3.4, given the code
22731
22732 namespace N {
22733 void foo() {
22734 // Definition of N::foo.
22735 }
22736 }
22737
22738 then we'll have a tree of DIEs like this:
22739
22740 1: DW_TAG_compile_unit
22741 2: DW_TAG_namespace // N
22742 3: DW_TAG_subprogram // declaration of N::foo
22743 4: DW_TAG_subprogram // definition of N::foo
22744 DW_AT_specification // refers to die #3
22745
22746 Thus, when processing die #4, we have to pretend that we're in
22747 the context of its DW_AT_specification, namely the contex of die
22748 #3. */
22749 spec_cu = cu;
22750 spec_die = die_specification (die, &spec_cu);
22751 if (spec_die == NULL)
22752 parent = die->parent;
22753 else
22754 {
22755 parent = spec_die->parent;
22756 cu = spec_cu;
22757 }
22758
22759 if (parent == NULL)
22760 return "";
22761 else if (parent->building_fullname)
22762 {
22763 const char *name;
22764 const char *parent_name;
22765
22766 /* It has been seen on RealView 2.2 built binaries,
22767 DW_TAG_template_type_param types actually _defined_ as
22768 children of the parent class:
22769
22770 enum E {};
22771 template class <class Enum> Class{};
22772 Class<enum E> class_e;
22773
22774 1: DW_TAG_class_type (Class)
22775 2: DW_TAG_enumeration_type (E)
22776 3: DW_TAG_enumerator (enum1:0)
22777 3: DW_TAG_enumerator (enum2:1)
22778 ...
22779 2: DW_TAG_template_type_param
22780 DW_AT_type DW_FORM_ref_udata (E)
22781
22782 Besides being broken debug info, it can put GDB into an
22783 infinite loop. Consider:
22784
22785 When we're building the full name for Class<E>, we'll start
22786 at Class, and go look over its template type parameters,
22787 finding E. We'll then try to build the full name of E, and
22788 reach here. We're now trying to build the full name of E,
22789 and look over the parent DIE for containing scope. In the
22790 broken case, if we followed the parent DIE of E, we'd again
22791 find Class, and once again go look at its template type
22792 arguments, etc., etc. Simply don't consider such parent die
22793 as source-level parent of this die (it can't be, the language
22794 doesn't allow it), and break the loop here. */
22795 name = dwarf2_name (die, cu);
22796 parent_name = dwarf2_name (parent, cu);
22797 complaint (&symfile_complaints,
22798 _("template param type '%s' defined within parent '%s'"),
22799 name ? name : "<unknown>",
22800 parent_name ? parent_name : "<unknown>");
22801 return "";
22802 }
22803 else
22804 switch (parent->tag)
22805 {
22806 case DW_TAG_namespace:
22807 parent_type = read_type_die (parent, cu);
22808 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22809 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22810 Work around this problem here. */
22811 if (cu->language == language_cplus
22812 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22813 return "";
22814 /* We give a name to even anonymous namespaces. */
22815 return TYPE_TAG_NAME (parent_type);
22816 case DW_TAG_class_type:
22817 case DW_TAG_interface_type:
22818 case DW_TAG_structure_type:
22819 case DW_TAG_union_type:
22820 case DW_TAG_module:
22821 parent_type = read_type_die (parent, cu);
22822 if (TYPE_TAG_NAME (parent_type) != NULL)
22823 return TYPE_TAG_NAME (parent_type);
22824 else
22825 /* An anonymous structure is only allowed non-static data
22826 members; no typedefs, no member functions, et cetera.
22827 So it does not need a prefix. */
22828 return "";
22829 case DW_TAG_compile_unit:
22830 case DW_TAG_partial_unit:
22831 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22832 if (cu->language == language_cplus
22833 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22834 && die->child != NULL
22835 && (die->tag == DW_TAG_class_type
22836 || die->tag == DW_TAG_structure_type
22837 || die->tag == DW_TAG_union_type))
22838 {
22839 char *name = guess_full_die_structure_name (die, cu);
22840 if (name != NULL)
22841 return name;
22842 }
22843 return "";
22844 case DW_TAG_enumeration_type:
22845 parent_type = read_type_die (parent, cu);
22846 if (TYPE_DECLARED_CLASS (parent_type))
22847 {
22848 if (TYPE_TAG_NAME (parent_type) != NULL)
22849 return TYPE_TAG_NAME (parent_type);
22850 return "";
22851 }
22852 /* Fall through. */
22853 default:
22854 return determine_prefix (parent, cu);
22855 }
22856 }
22857
22858 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22859 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22860 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22861 an obconcat, otherwise allocate storage for the result. The CU argument is
22862 used to determine the language and hence, the appropriate separator. */
22863
22864 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22865
22866 static char *
22867 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22868 int physname, struct dwarf2_cu *cu)
22869 {
22870 const char *lead = "";
22871 const char *sep;
22872
22873 if (suffix == NULL || suffix[0] == '\0'
22874 || prefix == NULL || prefix[0] == '\0')
22875 sep = "";
22876 else if (cu->language == language_d)
22877 {
22878 /* For D, the 'main' function could be defined in any module, but it
22879 should never be prefixed. */
22880 if (strcmp (suffix, "D main") == 0)
22881 {
22882 prefix = "";
22883 sep = "";
22884 }
22885 else
22886 sep = ".";
22887 }
22888 else if (cu->language == language_fortran && physname)
22889 {
22890 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22891 DW_AT_MIPS_linkage_name is preferred and used instead. */
22892
22893 lead = "__";
22894 sep = "_MOD_";
22895 }
22896 else
22897 sep = "::";
22898
22899 if (prefix == NULL)
22900 prefix = "";
22901 if (suffix == NULL)
22902 suffix = "";
22903
22904 if (obs == NULL)
22905 {
22906 char *retval
22907 = ((char *)
22908 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22909
22910 strcpy (retval, lead);
22911 strcat (retval, prefix);
22912 strcat (retval, sep);
22913 strcat (retval, suffix);
22914 return retval;
22915 }
22916 else
22917 {
22918 /* We have an obstack. */
22919 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22920 }
22921 }
22922
22923 /* Return sibling of die, NULL if no sibling. */
22924
22925 static struct die_info *
22926 sibling_die (struct die_info *die)
22927 {
22928 return die->sibling;
22929 }
22930
22931 /* Get name of a die, return NULL if not found. */
22932
22933 static const char *
22934 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22935 struct obstack *obstack)
22936 {
22937 if (name && cu->language == language_cplus)
22938 {
22939 std::string canon_name = cp_canonicalize_string (name);
22940
22941 if (!canon_name.empty ())
22942 {
22943 if (canon_name != name)
22944 name = (const char *) obstack_copy0 (obstack,
22945 canon_name.c_str (),
22946 canon_name.length ());
22947 }
22948 }
22949
22950 return name;
22951 }
22952
22953 /* Get name of a die, return NULL if not found.
22954 Anonymous namespaces are converted to their magic string. */
22955
22956 static const char *
22957 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22958 {
22959 struct attribute *attr;
22960 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22961
22962 attr = dwarf2_attr (die, DW_AT_name, cu);
22963 if ((!attr || !DW_STRING (attr))
22964 && die->tag != DW_TAG_namespace
22965 && die->tag != DW_TAG_class_type
22966 && die->tag != DW_TAG_interface_type
22967 && die->tag != DW_TAG_structure_type
22968 && die->tag != DW_TAG_union_type)
22969 return NULL;
22970
22971 switch (die->tag)
22972 {
22973 case DW_TAG_compile_unit:
22974 case DW_TAG_partial_unit:
22975 /* Compilation units have a DW_AT_name that is a filename, not
22976 a source language identifier. */
22977 case DW_TAG_enumeration_type:
22978 case DW_TAG_enumerator:
22979 /* These tags always have simple identifiers already; no need
22980 to canonicalize them. */
22981 return DW_STRING (attr);
22982
22983 case DW_TAG_namespace:
22984 if (attr != NULL && DW_STRING (attr) != NULL)
22985 return DW_STRING (attr);
22986 return CP_ANONYMOUS_NAMESPACE_STR;
22987
22988 case DW_TAG_class_type:
22989 case DW_TAG_interface_type:
22990 case DW_TAG_structure_type:
22991 case DW_TAG_union_type:
22992 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22993 structures or unions. These were of the form "._%d" in GCC 4.1,
22994 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22995 and GCC 4.4. We work around this problem by ignoring these. */
22996 if (attr && DW_STRING (attr)
22997 && (startswith (DW_STRING (attr), "._")
22998 || startswith (DW_STRING (attr), "<anonymous")))
22999 return NULL;
23000
23001 /* GCC might emit a nameless typedef that has a linkage name. See
23002 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23003 if (!attr || DW_STRING (attr) == NULL)
23004 {
23005 char *demangled = NULL;
23006
23007 attr = dw2_linkage_name_attr (die, cu);
23008 if (attr == NULL || DW_STRING (attr) == NULL)
23009 return NULL;
23010
23011 /* Avoid demangling DW_STRING (attr) the second time on a second
23012 call for the same DIE. */
23013 if (!DW_STRING_IS_CANONICAL (attr))
23014 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
23015
23016 if (demangled)
23017 {
23018 const char *base;
23019
23020 /* FIXME: we already did this for the partial symbol... */
23021 DW_STRING (attr)
23022 = ((const char *)
23023 obstack_copy0 (&objfile->per_bfd->storage_obstack,
23024 demangled, strlen (demangled)));
23025 DW_STRING_IS_CANONICAL (attr) = 1;
23026 xfree (demangled);
23027
23028 /* Strip any leading namespaces/classes, keep only the base name.
23029 DW_AT_name for named DIEs does not contain the prefixes. */
23030 base = strrchr (DW_STRING (attr), ':');
23031 if (base && base > DW_STRING (attr) && base[-1] == ':')
23032 return &base[1];
23033 else
23034 return DW_STRING (attr);
23035 }
23036 }
23037 break;
23038
23039 default:
23040 break;
23041 }
23042
23043 if (!DW_STRING_IS_CANONICAL (attr))
23044 {
23045 DW_STRING (attr)
23046 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23047 &objfile->per_bfd->storage_obstack);
23048 DW_STRING_IS_CANONICAL (attr) = 1;
23049 }
23050 return DW_STRING (attr);
23051 }
23052
23053 /* Return the die that this die in an extension of, or NULL if there
23054 is none. *EXT_CU is the CU containing DIE on input, and the CU
23055 containing the return value on output. */
23056
23057 static struct die_info *
23058 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23059 {
23060 struct attribute *attr;
23061
23062 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23063 if (attr == NULL)
23064 return NULL;
23065
23066 return follow_die_ref (die, attr, ext_cu);
23067 }
23068
23069 /* Convert a DIE tag into its string name. */
23070
23071 static const char *
23072 dwarf_tag_name (unsigned tag)
23073 {
23074 const char *name = get_DW_TAG_name (tag);
23075
23076 if (name == NULL)
23077 return "DW_TAG_<unknown>";
23078
23079 return name;
23080 }
23081
23082 /* Convert a DWARF attribute code into its string name. */
23083
23084 static const char *
23085 dwarf_attr_name (unsigned attr)
23086 {
23087 const char *name;
23088
23089 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23090 if (attr == DW_AT_MIPS_fde)
23091 return "DW_AT_MIPS_fde";
23092 #else
23093 if (attr == DW_AT_HP_block_index)
23094 return "DW_AT_HP_block_index";
23095 #endif
23096
23097 name = get_DW_AT_name (attr);
23098
23099 if (name == NULL)
23100 return "DW_AT_<unknown>";
23101
23102 return name;
23103 }
23104
23105 /* Convert a DWARF value form code into its string name. */
23106
23107 static const char *
23108 dwarf_form_name (unsigned form)
23109 {
23110 const char *name = get_DW_FORM_name (form);
23111
23112 if (name == NULL)
23113 return "DW_FORM_<unknown>";
23114
23115 return name;
23116 }
23117
23118 static const char *
23119 dwarf_bool_name (unsigned mybool)
23120 {
23121 if (mybool)
23122 return "TRUE";
23123 else
23124 return "FALSE";
23125 }
23126
23127 /* Convert a DWARF type code into its string name. */
23128
23129 static const char *
23130 dwarf_type_encoding_name (unsigned enc)
23131 {
23132 const char *name = get_DW_ATE_name (enc);
23133
23134 if (name == NULL)
23135 return "DW_ATE_<unknown>";
23136
23137 return name;
23138 }
23139
23140 static void
23141 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23142 {
23143 unsigned int i;
23144
23145 print_spaces (indent, f);
23146 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23147 dwarf_tag_name (die->tag), die->abbrev,
23148 sect_offset_str (die->sect_off));
23149
23150 if (die->parent != NULL)
23151 {
23152 print_spaces (indent, f);
23153 fprintf_unfiltered (f, " parent at offset: %s\n",
23154 sect_offset_str (die->parent->sect_off));
23155 }
23156
23157 print_spaces (indent, f);
23158 fprintf_unfiltered (f, " has children: %s\n",
23159 dwarf_bool_name (die->child != NULL));
23160
23161 print_spaces (indent, f);
23162 fprintf_unfiltered (f, " attributes:\n");
23163
23164 for (i = 0; i < die->num_attrs; ++i)
23165 {
23166 print_spaces (indent, f);
23167 fprintf_unfiltered (f, " %s (%s) ",
23168 dwarf_attr_name (die->attrs[i].name),
23169 dwarf_form_name (die->attrs[i].form));
23170
23171 switch (die->attrs[i].form)
23172 {
23173 case DW_FORM_addr:
23174 case DW_FORM_GNU_addr_index:
23175 fprintf_unfiltered (f, "address: ");
23176 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23177 break;
23178 case DW_FORM_block2:
23179 case DW_FORM_block4:
23180 case DW_FORM_block:
23181 case DW_FORM_block1:
23182 fprintf_unfiltered (f, "block: size %s",
23183 pulongest (DW_BLOCK (&die->attrs[i])->size));
23184 break;
23185 case DW_FORM_exprloc:
23186 fprintf_unfiltered (f, "expression: size %s",
23187 pulongest (DW_BLOCK (&die->attrs[i])->size));
23188 break;
23189 case DW_FORM_data16:
23190 fprintf_unfiltered (f, "constant of 16 bytes");
23191 break;
23192 case DW_FORM_ref_addr:
23193 fprintf_unfiltered (f, "ref address: ");
23194 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23195 break;
23196 case DW_FORM_GNU_ref_alt:
23197 fprintf_unfiltered (f, "alt ref address: ");
23198 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23199 break;
23200 case DW_FORM_ref1:
23201 case DW_FORM_ref2:
23202 case DW_FORM_ref4:
23203 case DW_FORM_ref8:
23204 case DW_FORM_ref_udata:
23205 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23206 (long) (DW_UNSND (&die->attrs[i])));
23207 break;
23208 case DW_FORM_data1:
23209 case DW_FORM_data2:
23210 case DW_FORM_data4:
23211 case DW_FORM_data8:
23212 case DW_FORM_udata:
23213 case DW_FORM_sdata:
23214 fprintf_unfiltered (f, "constant: %s",
23215 pulongest (DW_UNSND (&die->attrs[i])));
23216 break;
23217 case DW_FORM_sec_offset:
23218 fprintf_unfiltered (f, "section offset: %s",
23219 pulongest (DW_UNSND (&die->attrs[i])));
23220 break;
23221 case DW_FORM_ref_sig8:
23222 fprintf_unfiltered (f, "signature: %s",
23223 hex_string (DW_SIGNATURE (&die->attrs[i])));
23224 break;
23225 case DW_FORM_string:
23226 case DW_FORM_strp:
23227 case DW_FORM_line_strp:
23228 case DW_FORM_GNU_str_index:
23229 case DW_FORM_GNU_strp_alt:
23230 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23231 DW_STRING (&die->attrs[i])
23232 ? DW_STRING (&die->attrs[i]) : "",
23233 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23234 break;
23235 case DW_FORM_flag:
23236 if (DW_UNSND (&die->attrs[i]))
23237 fprintf_unfiltered (f, "flag: TRUE");
23238 else
23239 fprintf_unfiltered (f, "flag: FALSE");
23240 break;
23241 case DW_FORM_flag_present:
23242 fprintf_unfiltered (f, "flag: TRUE");
23243 break;
23244 case DW_FORM_indirect:
23245 /* The reader will have reduced the indirect form to
23246 the "base form" so this form should not occur. */
23247 fprintf_unfiltered (f,
23248 "unexpected attribute form: DW_FORM_indirect");
23249 break;
23250 case DW_FORM_implicit_const:
23251 fprintf_unfiltered (f, "constant: %s",
23252 plongest (DW_SND (&die->attrs[i])));
23253 break;
23254 default:
23255 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23256 die->attrs[i].form);
23257 break;
23258 }
23259 fprintf_unfiltered (f, "\n");
23260 }
23261 }
23262
23263 static void
23264 dump_die_for_error (struct die_info *die)
23265 {
23266 dump_die_shallow (gdb_stderr, 0, die);
23267 }
23268
23269 static void
23270 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23271 {
23272 int indent = level * 4;
23273
23274 gdb_assert (die != NULL);
23275
23276 if (level >= max_level)
23277 return;
23278
23279 dump_die_shallow (f, indent, die);
23280
23281 if (die->child != NULL)
23282 {
23283 print_spaces (indent, f);
23284 fprintf_unfiltered (f, " Children:");
23285 if (level + 1 < max_level)
23286 {
23287 fprintf_unfiltered (f, "\n");
23288 dump_die_1 (f, level + 1, max_level, die->child);
23289 }
23290 else
23291 {
23292 fprintf_unfiltered (f,
23293 " [not printed, max nesting level reached]\n");
23294 }
23295 }
23296
23297 if (die->sibling != NULL && level > 0)
23298 {
23299 dump_die_1 (f, level, max_level, die->sibling);
23300 }
23301 }
23302
23303 /* This is called from the pdie macro in gdbinit.in.
23304 It's not static so gcc will keep a copy callable from gdb. */
23305
23306 void
23307 dump_die (struct die_info *die, int max_level)
23308 {
23309 dump_die_1 (gdb_stdlog, 0, max_level, die);
23310 }
23311
23312 static void
23313 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23314 {
23315 void **slot;
23316
23317 slot = htab_find_slot_with_hash (cu->die_hash, die,
23318 to_underlying (die->sect_off),
23319 INSERT);
23320
23321 *slot = die;
23322 }
23323
23324 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23325 required kind. */
23326
23327 static sect_offset
23328 dwarf2_get_ref_die_offset (const struct attribute *attr)
23329 {
23330 if (attr_form_is_ref (attr))
23331 return (sect_offset) DW_UNSND (attr);
23332
23333 complaint (&symfile_complaints,
23334 _("unsupported die ref attribute form: '%s'"),
23335 dwarf_form_name (attr->form));
23336 return {};
23337 }
23338
23339 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23340 * the value held by the attribute is not constant. */
23341
23342 static LONGEST
23343 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23344 {
23345 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23346 return DW_SND (attr);
23347 else if (attr->form == DW_FORM_udata
23348 || attr->form == DW_FORM_data1
23349 || attr->form == DW_FORM_data2
23350 || attr->form == DW_FORM_data4
23351 || attr->form == DW_FORM_data8)
23352 return DW_UNSND (attr);
23353 else
23354 {
23355 /* For DW_FORM_data16 see attr_form_is_constant. */
23356 complaint (&symfile_complaints,
23357 _("Attribute value is not a constant (%s)"),
23358 dwarf_form_name (attr->form));
23359 return default_value;
23360 }
23361 }
23362
23363 /* Follow reference or signature attribute ATTR of SRC_DIE.
23364 On entry *REF_CU is the CU of SRC_DIE.
23365 On exit *REF_CU is the CU of the result. */
23366
23367 static struct die_info *
23368 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23369 struct dwarf2_cu **ref_cu)
23370 {
23371 struct die_info *die;
23372
23373 if (attr_form_is_ref (attr))
23374 die = follow_die_ref (src_die, attr, ref_cu);
23375 else if (attr->form == DW_FORM_ref_sig8)
23376 die = follow_die_sig (src_die, attr, ref_cu);
23377 else
23378 {
23379 dump_die_for_error (src_die);
23380 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23381 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23382 }
23383
23384 return die;
23385 }
23386
23387 /* Follow reference OFFSET.
23388 On entry *REF_CU is the CU of the source die referencing OFFSET.
23389 On exit *REF_CU is the CU of the result.
23390 Returns NULL if OFFSET is invalid. */
23391
23392 static struct die_info *
23393 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23394 struct dwarf2_cu **ref_cu)
23395 {
23396 struct die_info temp_die;
23397 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23398 struct dwarf2_per_objfile *dwarf2_per_objfile
23399 = cu->per_cu->dwarf2_per_objfile;
23400 struct objfile *objfile = dwarf2_per_objfile->objfile;
23401
23402 gdb_assert (cu->per_cu != NULL);
23403
23404 target_cu = cu;
23405
23406 if (cu->per_cu->is_debug_types)
23407 {
23408 /* .debug_types CUs cannot reference anything outside their CU.
23409 If they need to, they have to reference a signatured type via
23410 DW_FORM_ref_sig8. */
23411 if (!offset_in_cu_p (&cu->header, sect_off))
23412 return NULL;
23413 }
23414 else if (offset_in_dwz != cu->per_cu->is_dwz
23415 || !offset_in_cu_p (&cu->header, sect_off))
23416 {
23417 struct dwarf2_per_cu_data *per_cu;
23418
23419 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23420 dwarf2_per_objfile);
23421
23422 /* If necessary, add it to the queue and load its DIEs. */
23423 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23424 load_full_comp_unit (per_cu, cu->language);
23425
23426 target_cu = per_cu->cu;
23427 }
23428 else if (cu->dies == NULL)
23429 {
23430 /* We're loading full DIEs during partial symbol reading. */
23431 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23432 load_full_comp_unit (cu->per_cu, language_minimal);
23433 }
23434
23435 *ref_cu = target_cu;
23436 temp_die.sect_off = sect_off;
23437 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23438 &temp_die,
23439 to_underlying (sect_off));
23440 }
23441
23442 /* Follow reference attribute ATTR of SRC_DIE.
23443 On entry *REF_CU is the CU of SRC_DIE.
23444 On exit *REF_CU is the CU of the result. */
23445
23446 static struct die_info *
23447 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23448 struct dwarf2_cu **ref_cu)
23449 {
23450 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23451 struct dwarf2_cu *cu = *ref_cu;
23452 struct die_info *die;
23453
23454 die = follow_die_offset (sect_off,
23455 (attr->form == DW_FORM_GNU_ref_alt
23456 || cu->per_cu->is_dwz),
23457 ref_cu);
23458 if (!die)
23459 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23460 "at %s [in module %s]"),
23461 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23462 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23463
23464 return die;
23465 }
23466
23467 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23468 Returned value is intended for DW_OP_call*. Returned
23469 dwarf2_locexpr_baton->data has lifetime of
23470 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23471
23472 struct dwarf2_locexpr_baton
23473 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23474 struct dwarf2_per_cu_data *per_cu,
23475 CORE_ADDR (*get_frame_pc) (void *baton),
23476 void *baton)
23477 {
23478 struct dwarf2_cu *cu;
23479 struct die_info *die;
23480 struct attribute *attr;
23481 struct dwarf2_locexpr_baton retval;
23482 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23483 struct dwarf2_per_objfile *dwarf2_per_objfile
23484 = get_dwarf2_per_objfile (objfile);
23485
23486 if (per_cu->cu == NULL)
23487 load_cu (per_cu);
23488 cu = per_cu->cu;
23489 if (cu == NULL)
23490 {
23491 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23492 Instead just throw an error, not much else we can do. */
23493 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23494 sect_offset_str (sect_off), objfile_name (objfile));
23495 }
23496
23497 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23498 if (!die)
23499 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23500 sect_offset_str (sect_off), objfile_name (objfile));
23501
23502 attr = dwarf2_attr (die, DW_AT_location, cu);
23503 if (!attr)
23504 {
23505 /* DWARF: "If there is no such attribute, then there is no effect.".
23506 DATA is ignored if SIZE is 0. */
23507
23508 retval.data = NULL;
23509 retval.size = 0;
23510 }
23511 else if (attr_form_is_section_offset (attr))
23512 {
23513 struct dwarf2_loclist_baton loclist_baton;
23514 CORE_ADDR pc = (*get_frame_pc) (baton);
23515 size_t size;
23516
23517 fill_in_loclist_baton (cu, &loclist_baton, attr);
23518
23519 retval.data = dwarf2_find_location_expression (&loclist_baton,
23520 &size, pc);
23521 retval.size = size;
23522 }
23523 else
23524 {
23525 if (!attr_form_is_block (attr))
23526 error (_("Dwarf Error: DIE at %s referenced in module %s "
23527 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23528 sect_offset_str (sect_off), objfile_name (objfile));
23529
23530 retval.data = DW_BLOCK (attr)->data;
23531 retval.size = DW_BLOCK (attr)->size;
23532 }
23533 retval.per_cu = cu->per_cu;
23534
23535 age_cached_comp_units (dwarf2_per_objfile);
23536
23537 return retval;
23538 }
23539
23540 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23541 offset. */
23542
23543 struct dwarf2_locexpr_baton
23544 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23545 struct dwarf2_per_cu_data *per_cu,
23546 CORE_ADDR (*get_frame_pc) (void *baton),
23547 void *baton)
23548 {
23549 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23550
23551 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23552 }
23553
23554 /* Write a constant of a given type as target-ordered bytes into
23555 OBSTACK. */
23556
23557 static const gdb_byte *
23558 write_constant_as_bytes (struct obstack *obstack,
23559 enum bfd_endian byte_order,
23560 struct type *type,
23561 ULONGEST value,
23562 LONGEST *len)
23563 {
23564 gdb_byte *result;
23565
23566 *len = TYPE_LENGTH (type);
23567 result = (gdb_byte *) obstack_alloc (obstack, *len);
23568 store_unsigned_integer (result, *len, byte_order, value);
23569
23570 return result;
23571 }
23572
23573 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23574 pointer to the constant bytes and set LEN to the length of the
23575 data. If memory is needed, allocate it on OBSTACK. If the DIE
23576 does not have a DW_AT_const_value, return NULL. */
23577
23578 const gdb_byte *
23579 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23580 struct dwarf2_per_cu_data *per_cu,
23581 struct obstack *obstack,
23582 LONGEST *len)
23583 {
23584 struct dwarf2_cu *cu;
23585 struct die_info *die;
23586 struct attribute *attr;
23587 const gdb_byte *result = NULL;
23588 struct type *type;
23589 LONGEST value;
23590 enum bfd_endian byte_order;
23591 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23592
23593 if (per_cu->cu == NULL)
23594 load_cu (per_cu);
23595 cu = per_cu->cu;
23596 if (cu == NULL)
23597 {
23598 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23599 Instead just throw an error, not much else we can do. */
23600 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23601 sect_offset_str (sect_off), objfile_name (objfile));
23602 }
23603
23604 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23605 if (!die)
23606 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23607 sect_offset_str (sect_off), objfile_name (objfile));
23608
23609 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23610 if (attr == NULL)
23611 return NULL;
23612
23613 byte_order = (bfd_big_endian (objfile->obfd)
23614 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23615
23616 switch (attr->form)
23617 {
23618 case DW_FORM_addr:
23619 case DW_FORM_GNU_addr_index:
23620 {
23621 gdb_byte *tem;
23622
23623 *len = cu->header.addr_size;
23624 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23625 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23626 result = tem;
23627 }
23628 break;
23629 case DW_FORM_string:
23630 case DW_FORM_strp:
23631 case DW_FORM_GNU_str_index:
23632 case DW_FORM_GNU_strp_alt:
23633 /* DW_STRING is already allocated on the objfile obstack, point
23634 directly to it. */
23635 result = (const gdb_byte *) DW_STRING (attr);
23636 *len = strlen (DW_STRING (attr));
23637 break;
23638 case DW_FORM_block1:
23639 case DW_FORM_block2:
23640 case DW_FORM_block4:
23641 case DW_FORM_block:
23642 case DW_FORM_exprloc:
23643 case DW_FORM_data16:
23644 result = DW_BLOCK (attr)->data;
23645 *len = DW_BLOCK (attr)->size;
23646 break;
23647
23648 /* The DW_AT_const_value attributes are supposed to carry the
23649 symbol's value "represented as it would be on the target
23650 architecture." By the time we get here, it's already been
23651 converted to host endianness, so we just need to sign- or
23652 zero-extend it as appropriate. */
23653 case DW_FORM_data1:
23654 type = die_type (die, cu);
23655 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23656 if (result == NULL)
23657 result = write_constant_as_bytes (obstack, byte_order,
23658 type, value, len);
23659 break;
23660 case DW_FORM_data2:
23661 type = die_type (die, cu);
23662 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23663 if (result == NULL)
23664 result = write_constant_as_bytes (obstack, byte_order,
23665 type, value, len);
23666 break;
23667 case DW_FORM_data4:
23668 type = die_type (die, cu);
23669 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23670 if (result == NULL)
23671 result = write_constant_as_bytes (obstack, byte_order,
23672 type, value, len);
23673 break;
23674 case DW_FORM_data8:
23675 type = die_type (die, cu);
23676 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23677 if (result == NULL)
23678 result = write_constant_as_bytes (obstack, byte_order,
23679 type, value, len);
23680 break;
23681
23682 case DW_FORM_sdata:
23683 case DW_FORM_implicit_const:
23684 type = die_type (die, cu);
23685 result = write_constant_as_bytes (obstack, byte_order,
23686 type, DW_SND (attr), len);
23687 break;
23688
23689 case DW_FORM_udata:
23690 type = die_type (die, cu);
23691 result = write_constant_as_bytes (obstack, byte_order,
23692 type, DW_UNSND (attr), len);
23693 break;
23694
23695 default:
23696 complaint (&symfile_complaints,
23697 _("unsupported const value attribute form: '%s'"),
23698 dwarf_form_name (attr->form));
23699 break;
23700 }
23701
23702 return result;
23703 }
23704
23705 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23706 valid type for this die is found. */
23707
23708 struct type *
23709 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23710 struct dwarf2_per_cu_data *per_cu)
23711 {
23712 struct dwarf2_cu *cu;
23713 struct die_info *die;
23714
23715 if (per_cu->cu == NULL)
23716 load_cu (per_cu);
23717 cu = per_cu->cu;
23718 if (!cu)
23719 return NULL;
23720
23721 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23722 if (!die)
23723 return NULL;
23724
23725 return die_type (die, cu);
23726 }
23727
23728 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23729 PER_CU. */
23730
23731 struct type *
23732 dwarf2_get_die_type (cu_offset die_offset,
23733 struct dwarf2_per_cu_data *per_cu)
23734 {
23735 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23736 return get_die_type_at_offset (die_offset_sect, per_cu);
23737 }
23738
23739 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23740 On entry *REF_CU is the CU of SRC_DIE.
23741 On exit *REF_CU is the CU of the result.
23742 Returns NULL if the referenced DIE isn't found. */
23743
23744 static struct die_info *
23745 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23746 struct dwarf2_cu **ref_cu)
23747 {
23748 struct die_info temp_die;
23749 struct dwarf2_cu *sig_cu;
23750 struct die_info *die;
23751
23752 /* While it might be nice to assert sig_type->type == NULL here,
23753 we can get here for DW_AT_imported_declaration where we need
23754 the DIE not the type. */
23755
23756 /* If necessary, add it to the queue and load its DIEs. */
23757
23758 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23759 read_signatured_type (sig_type);
23760
23761 sig_cu = sig_type->per_cu.cu;
23762 gdb_assert (sig_cu != NULL);
23763 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23764 temp_die.sect_off = sig_type->type_offset_in_section;
23765 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23766 to_underlying (temp_die.sect_off));
23767 if (die)
23768 {
23769 struct dwarf2_per_objfile *dwarf2_per_objfile
23770 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23771
23772 /* For .gdb_index version 7 keep track of included TUs.
23773 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23774 if (dwarf2_per_objfile->index_table != NULL
23775 && dwarf2_per_objfile->index_table->version <= 7)
23776 {
23777 VEC_safe_push (dwarf2_per_cu_ptr,
23778 (*ref_cu)->per_cu->imported_symtabs,
23779 sig_cu->per_cu);
23780 }
23781
23782 *ref_cu = sig_cu;
23783 return die;
23784 }
23785
23786 return NULL;
23787 }
23788
23789 /* Follow signatured type referenced by ATTR in SRC_DIE.
23790 On entry *REF_CU is the CU of SRC_DIE.
23791 On exit *REF_CU is the CU of the result.
23792 The result is the DIE of the type.
23793 If the referenced type cannot be found an error is thrown. */
23794
23795 static struct die_info *
23796 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23797 struct dwarf2_cu **ref_cu)
23798 {
23799 ULONGEST signature = DW_SIGNATURE (attr);
23800 struct signatured_type *sig_type;
23801 struct die_info *die;
23802
23803 gdb_assert (attr->form == DW_FORM_ref_sig8);
23804
23805 sig_type = lookup_signatured_type (*ref_cu, signature);
23806 /* sig_type will be NULL if the signatured type is missing from
23807 the debug info. */
23808 if (sig_type == NULL)
23809 {
23810 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23811 " from DIE at %s [in module %s]"),
23812 hex_string (signature), sect_offset_str (src_die->sect_off),
23813 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23814 }
23815
23816 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23817 if (die == NULL)
23818 {
23819 dump_die_for_error (src_die);
23820 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23821 " from DIE at %s [in module %s]"),
23822 hex_string (signature), sect_offset_str (src_die->sect_off),
23823 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23824 }
23825
23826 return die;
23827 }
23828
23829 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23830 reading in and processing the type unit if necessary. */
23831
23832 static struct type *
23833 get_signatured_type (struct die_info *die, ULONGEST signature,
23834 struct dwarf2_cu *cu)
23835 {
23836 struct dwarf2_per_objfile *dwarf2_per_objfile
23837 = cu->per_cu->dwarf2_per_objfile;
23838 struct signatured_type *sig_type;
23839 struct dwarf2_cu *type_cu;
23840 struct die_info *type_die;
23841 struct type *type;
23842
23843 sig_type = lookup_signatured_type (cu, signature);
23844 /* sig_type will be NULL if the signatured type is missing from
23845 the debug info. */
23846 if (sig_type == NULL)
23847 {
23848 complaint (&symfile_complaints,
23849 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23850 " from DIE at %s [in module %s]"),
23851 hex_string (signature), sect_offset_str (die->sect_off),
23852 objfile_name (dwarf2_per_objfile->objfile));
23853 return build_error_marker_type (cu, die);
23854 }
23855
23856 /* If we already know the type we're done. */
23857 if (sig_type->type != NULL)
23858 return sig_type->type;
23859
23860 type_cu = cu;
23861 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23862 if (type_die != NULL)
23863 {
23864 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23865 is created. This is important, for example, because for c++ classes
23866 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23867 type = read_type_die (type_die, type_cu);
23868 if (type == NULL)
23869 {
23870 complaint (&symfile_complaints,
23871 _("Dwarf Error: Cannot build signatured type %s"
23872 " referenced from DIE at %s [in module %s]"),
23873 hex_string (signature), sect_offset_str (die->sect_off),
23874 objfile_name (dwarf2_per_objfile->objfile));
23875 type = build_error_marker_type (cu, die);
23876 }
23877 }
23878 else
23879 {
23880 complaint (&symfile_complaints,
23881 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23882 " from DIE at %s [in module %s]"),
23883 hex_string (signature), sect_offset_str (die->sect_off),
23884 objfile_name (dwarf2_per_objfile->objfile));
23885 type = build_error_marker_type (cu, die);
23886 }
23887 sig_type->type = type;
23888
23889 return type;
23890 }
23891
23892 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23893 reading in and processing the type unit if necessary. */
23894
23895 static struct type *
23896 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23897 struct dwarf2_cu *cu) /* ARI: editCase function */
23898 {
23899 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23900 if (attr_form_is_ref (attr))
23901 {
23902 struct dwarf2_cu *type_cu = cu;
23903 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23904
23905 return read_type_die (type_die, type_cu);
23906 }
23907 else if (attr->form == DW_FORM_ref_sig8)
23908 {
23909 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23910 }
23911 else
23912 {
23913 struct dwarf2_per_objfile *dwarf2_per_objfile
23914 = cu->per_cu->dwarf2_per_objfile;
23915
23916 complaint (&symfile_complaints,
23917 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23918 " at %s [in module %s]"),
23919 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23920 objfile_name (dwarf2_per_objfile->objfile));
23921 return build_error_marker_type (cu, die);
23922 }
23923 }
23924
23925 /* Load the DIEs associated with type unit PER_CU into memory. */
23926
23927 static void
23928 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23929 {
23930 struct signatured_type *sig_type;
23931
23932 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23933 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23934
23935 /* We have the per_cu, but we need the signatured_type.
23936 Fortunately this is an easy translation. */
23937 gdb_assert (per_cu->is_debug_types);
23938 sig_type = (struct signatured_type *) per_cu;
23939
23940 gdb_assert (per_cu->cu == NULL);
23941
23942 read_signatured_type (sig_type);
23943
23944 gdb_assert (per_cu->cu != NULL);
23945 }
23946
23947 /* die_reader_func for read_signatured_type.
23948 This is identical to load_full_comp_unit_reader,
23949 but is kept separate for now. */
23950
23951 static void
23952 read_signatured_type_reader (const struct die_reader_specs *reader,
23953 const gdb_byte *info_ptr,
23954 struct die_info *comp_unit_die,
23955 int has_children,
23956 void *data)
23957 {
23958 struct dwarf2_cu *cu = reader->cu;
23959
23960 gdb_assert (cu->die_hash == NULL);
23961 cu->die_hash =
23962 htab_create_alloc_ex (cu->header.length / 12,
23963 die_hash,
23964 die_eq,
23965 NULL,
23966 &cu->comp_unit_obstack,
23967 hashtab_obstack_allocate,
23968 dummy_obstack_deallocate);
23969
23970 if (has_children)
23971 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23972 &info_ptr, comp_unit_die);
23973 cu->dies = comp_unit_die;
23974 /* comp_unit_die is not stored in die_hash, no need. */
23975
23976 /* We try not to read any attributes in this function, because not
23977 all CUs needed for references have been loaded yet, and symbol
23978 table processing isn't initialized. But we have to set the CU language,
23979 or we won't be able to build types correctly.
23980 Similarly, if we do not read the producer, we can not apply
23981 producer-specific interpretation. */
23982 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23983 }
23984
23985 /* Read in a signatured type and build its CU and DIEs.
23986 If the type is a stub for the real type in a DWO file,
23987 read in the real type from the DWO file as well. */
23988
23989 static void
23990 read_signatured_type (struct signatured_type *sig_type)
23991 {
23992 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23993
23994 gdb_assert (per_cu->is_debug_types);
23995 gdb_assert (per_cu->cu == NULL);
23996
23997 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23998 read_signatured_type_reader, NULL);
23999 sig_type->per_cu.tu_read = 1;
24000 }
24001
24002 /* Decode simple location descriptions.
24003 Given a pointer to a dwarf block that defines a location, compute
24004 the location and return the value.
24005
24006 NOTE drow/2003-11-18: This function is called in two situations
24007 now: for the address of static or global variables (partial symbols
24008 only) and for offsets into structures which are expected to be
24009 (more or less) constant. The partial symbol case should go away,
24010 and only the constant case should remain. That will let this
24011 function complain more accurately. A few special modes are allowed
24012 without complaint for global variables (for instance, global
24013 register values and thread-local values).
24014
24015 A location description containing no operations indicates that the
24016 object is optimized out. The return value is 0 for that case.
24017 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24018 callers will only want a very basic result and this can become a
24019 complaint.
24020
24021 Note that stack[0] is unused except as a default error return. */
24022
24023 static CORE_ADDR
24024 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24025 {
24026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24027 size_t i;
24028 size_t size = blk->size;
24029 const gdb_byte *data = blk->data;
24030 CORE_ADDR stack[64];
24031 int stacki;
24032 unsigned int bytes_read, unsnd;
24033 gdb_byte op;
24034
24035 i = 0;
24036 stacki = 0;
24037 stack[stacki] = 0;
24038 stack[++stacki] = 0;
24039
24040 while (i < size)
24041 {
24042 op = data[i++];
24043 switch (op)
24044 {
24045 case DW_OP_lit0:
24046 case DW_OP_lit1:
24047 case DW_OP_lit2:
24048 case DW_OP_lit3:
24049 case DW_OP_lit4:
24050 case DW_OP_lit5:
24051 case DW_OP_lit6:
24052 case DW_OP_lit7:
24053 case DW_OP_lit8:
24054 case DW_OP_lit9:
24055 case DW_OP_lit10:
24056 case DW_OP_lit11:
24057 case DW_OP_lit12:
24058 case DW_OP_lit13:
24059 case DW_OP_lit14:
24060 case DW_OP_lit15:
24061 case DW_OP_lit16:
24062 case DW_OP_lit17:
24063 case DW_OP_lit18:
24064 case DW_OP_lit19:
24065 case DW_OP_lit20:
24066 case DW_OP_lit21:
24067 case DW_OP_lit22:
24068 case DW_OP_lit23:
24069 case DW_OP_lit24:
24070 case DW_OP_lit25:
24071 case DW_OP_lit26:
24072 case DW_OP_lit27:
24073 case DW_OP_lit28:
24074 case DW_OP_lit29:
24075 case DW_OP_lit30:
24076 case DW_OP_lit31:
24077 stack[++stacki] = op - DW_OP_lit0;
24078 break;
24079
24080 case DW_OP_reg0:
24081 case DW_OP_reg1:
24082 case DW_OP_reg2:
24083 case DW_OP_reg3:
24084 case DW_OP_reg4:
24085 case DW_OP_reg5:
24086 case DW_OP_reg6:
24087 case DW_OP_reg7:
24088 case DW_OP_reg8:
24089 case DW_OP_reg9:
24090 case DW_OP_reg10:
24091 case DW_OP_reg11:
24092 case DW_OP_reg12:
24093 case DW_OP_reg13:
24094 case DW_OP_reg14:
24095 case DW_OP_reg15:
24096 case DW_OP_reg16:
24097 case DW_OP_reg17:
24098 case DW_OP_reg18:
24099 case DW_OP_reg19:
24100 case DW_OP_reg20:
24101 case DW_OP_reg21:
24102 case DW_OP_reg22:
24103 case DW_OP_reg23:
24104 case DW_OP_reg24:
24105 case DW_OP_reg25:
24106 case DW_OP_reg26:
24107 case DW_OP_reg27:
24108 case DW_OP_reg28:
24109 case DW_OP_reg29:
24110 case DW_OP_reg30:
24111 case DW_OP_reg31:
24112 stack[++stacki] = op - DW_OP_reg0;
24113 if (i < size)
24114 dwarf2_complex_location_expr_complaint ();
24115 break;
24116
24117 case DW_OP_regx:
24118 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24119 i += bytes_read;
24120 stack[++stacki] = unsnd;
24121 if (i < size)
24122 dwarf2_complex_location_expr_complaint ();
24123 break;
24124
24125 case DW_OP_addr:
24126 stack[++stacki] = read_address (objfile->obfd, &data[i],
24127 cu, &bytes_read);
24128 i += bytes_read;
24129 break;
24130
24131 case DW_OP_const1u:
24132 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24133 i += 1;
24134 break;
24135
24136 case DW_OP_const1s:
24137 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24138 i += 1;
24139 break;
24140
24141 case DW_OP_const2u:
24142 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24143 i += 2;
24144 break;
24145
24146 case DW_OP_const2s:
24147 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24148 i += 2;
24149 break;
24150
24151 case DW_OP_const4u:
24152 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24153 i += 4;
24154 break;
24155
24156 case DW_OP_const4s:
24157 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24158 i += 4;
24159 break;
24160
24161 case DW_OP_const8u:
24162 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24163 i += 8;
24164 break;
24165
24166 case DW_OP_constu:
24167 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24168 &bytes_read);
24169 i += bytes_read;
24170 break;
24171
24172 case DW_OP_consts:
24173 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24174 i += bytes_read;
24175 break;
24176
24177 case DW_OP_dup:
24178 stack[stacki + 1] = stack[stacki];
24179 stacki++;
24180 break;
24181
24182 case DW_OP_plus:
24183 stack[stacki - 1] += stack[stacki];
24184 stacki--;
24185 break;
24186
24187 case DW_OP_plus_uconst:
24188 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24189 &bytes_read);
24190 i += bytes_read;
24191 break;
24192
24193 case DW_OP_minus:
24194 stack[stacki - 1] -= stack[stacki];
24195 stacki--;
24196 break;
24197
24198 case DW_OP_deref:
24199 /* If we're not the last op, then we definitely can't encode
24200 this using GDB's address_class enum. This is valid for partial
24201 global symbols, although the variable's address will be bogus
24202 in the psymtab. */
24203 if (i < size)
24204 dwarf2_complex_location_expr_complaint ();
24205 break;
24206
24207 case DW_OP_GNU_push_tls_address:
24208 case DW_OP_form_tls_address:
24209 /* The top of the stack has the offset from the beginning
24210 of the thread control block at which the variable is located. */
24211 /* Nothing should follow this operator, so the top of stack would
24212 be returned. */
24213 /* This is valid for partial global symbols, but the variable's
24214 address will be bogus in the psymtab. Make it always at least
24215 non-zero to not look as a variable garbage collected by linker
24216 which have DW_OP_addr 0. */
24217 if (i < size)
24218 dwarf2_complex_location_expr_complaint ();
24219 stack[stacki]++;
24220 break;
24221
24222 case DW_OP_GNU_uninit:
24223 break;
24224
24225 case DW_OP_GNU_addr_index:
24226 case DW_OP_GNU_const_index:
24227 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24228 &bytes_read);
24229 i += bytes_read;
24230 break;
24231
24232 default:
24233 {
24234 const char *name = get_DW_OP_name (op);
24235
24236 if (name)
24237 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
24238 name);
24239 else
24240 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
24241 op);
24242 }
24243
24244 return (stack[stacki]);
24245 }
24246
24247 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24248 outside of the allocated space. Also enforce minimum>0. */
24249 if (stacki >= ARRAY_SIZE (stack) - 1)
24250 {
24251 complaint (&symfile_complaints,
24252 _("location description stack overflow"));
24253 return 0;
24254 }
24255
24256 if (stacki <= 0)
24257 {
24258 complaint (&symfile_complaints,
24259 _("location description stack underflow"));
24260 return 0;
24261 }
24262 }
24263 return (stack[stacki]);
24264 }
24265
24266 /* memory allocation interface */
24267
24268 static struct dwarf_block *
24269 dwarf_alloc_block (struct dwarf2_cu *cu)
24270 {
24271 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24272 }
24273
24274 static struct die_info *
24275 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24276 {
24277 struct die_info *die;
24278 size_t size = sizeof (struct die_info);
24279
24280 if (num_attrs > 1)
24281 size += (num_attrs - 1) * sizeof (struct attribute);
24282
24283 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24284 memset (die, 0, sizeof (struct die_info));
24285 return (die);
24286 }
24287
24288 \f
24289 /* Macro support. */
24290
24291 /* Return file name relative to the compilation directory of file number I in
24292 *LH's file name table. The result is allocated using xmalloc; the caller is
24293 responsible for freeing it. */
24294
24295 static char *
24296 file_file_name (int file, struct line_header *lh)
24297 {
24298 /* Is the file number a valid index into the line header's file name
24299 table? Remember that file numbers start with one, not zero. */
24300 if (1 <= file && file <= lh->file_names.size ())
24301 {
24302 const file_entry &fe = lh->file_names[file - 1];
24303
24304 if (!IS_ABSOLUTE_PATH (fe.name))
24305 {
24306 const char *dir = fe.include_dir (lh);
24307 if (dir != NULL)
24308 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24309 }
24310 return xstrdup (fe.name);
24311 }
24312 else
24313 {
24314 /* The compiler produced a bogus file number. We can at least
24315 record the macro definitions made in the file, even if we
24316 won't be able to find the file by name. */
24317 char fake_name[80];
24318
24319 xsnprintf (fake_name, sizeof (fake_name),
24320 "<bad macro file number %d>", file);
24321
24322 complaint (&symfile_complaints,
24323 _("bad file number in macro information (%d)"),
24324 file);
24325
24326 return xstrdup (fake_name);
24327 }
24328 }
24329
24330 /* Return the full name of file number I in *LH's file name table.
24331 Use COMP_DIR as the name of the current directory of the
24332 compilation. The result is allocated using xmalloc; the caller is
24333 responsible for freeing it. */
24334 static char *
24335 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24336 {
24337 /* Is the file number a valid index into the line header's file name
24338 table? Remember that file numbers start with one, not zero. */
24339 if (1 <= file && file <= lh->file_names.size ())
24340 {
24341 char *relative = file_file_name (file, lh);
24342
24343 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24344 return relative;
24345 return reconcat (relative, comp_dir, SLASH_STRING,
24346 relative, (char *) NULL);
24347 }
24348 else
24349 return file_file_name (file, lh);
24350 }
24351
24352
24353 static struct macro_source_file *
24354 macro_start_file (int file, int line,
24355 struct macro_source_file *current_file,
24356 struct line_header *lh)
24357 {
24358 /* File name relative to the compilation directory of this source file. */
24359 char *file_name = file_file_name (file, lh);
24360
24361 if (! current_file)
24362 {
24363 /* Note: We don't create a macro table for this compilation unit
24364 at all until we actually get a filename. */
24365 struct macro_table *macro_table = get_macro_table ();
24366
24367 /* If we have no current file, then this must be the start_file
24368 directive for the compilation unit's main source file. */
24369 current_file = macro_set_main (macro_table, file_name);
24370 macro_define_special (macro_table);
24371 }
24372 else
24373 current_file = macro_include (current_file, line, file_name);
24374
24375 xfree (file_name);
24376
24377 return current_file;
24378 }
24379
24380 static const char *
24381 consume_improper_spaces (const char *p, const char *body)
24382 {
24383 if (*p == ' ')
24384 {
24385 complaint (&symfile_complaints,
24386 _("macro definition contains spaces "
24387 "in formal argument list:\n`%s'"),
24388 body);
24389
24390 while (*p == ' ')
24391 p++;
24392 }
24393
24394 return p;
24395 }
24396
24397
24398 static void
24399 parse_macro_definition (struct macro_source_file *file, int line,
24400 const char *body)
24401 {
24402 const char *p;
24403
24404 /* The body string takes one of two forms. For object-like macro
24405 definitions, it should be:
24406
24407 <macro name> " " <definition>
24408
24409 For function-like macro definitions, it should be:
24410
24411 <macro name> "() " <definition>
24412 or
24413 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24414
24415 Spaces may appear only where explicitly indicated, and in the
24416 <definition>.
24417
24418 The Dwarf 2 spec says that an object-like macro's name is always
24419 followed by a space, but versions of GCC around March 2002 omit
24420 the space when the macro's definition is the empty string.
24421
24422 The Dwarf 2 spec says that there should be no spaces between the
24423 formal arguments in a function-like macro's formal argument list,
24424 but versions of GCC around March 2002 include spaces after the
24425 commas. */
24426
24427
24428 /* Find the extent of the macro name. The macro name is terminated
24429 by either a space or null character (for an object-like macro) or
24430 an opening paren (for a function-like macro). */
24431 for (p = body; *p; p++)
24432 if (*p == ' ' || *p == '(')
24433 break;
24434
24435 if (*p == ' ' || *p == '\0')
24436 {
24437 /* It's an object-like macro. */
24438 int name_len = p - body;
24439 char *name = savestring (body, name_len);
24440 const char *replacement;
24441
24442 if (*p == ' ')
24443 replacement = body + name_len + 1;
24444 else
24445 {
24446 dwarf2_macro_malformed_definition_complaint (body);
24447 replacement = body + name_len;
24448 }
24449
24450 macro_define_object (file, line, name, replacement);
24451
24452 xfree (name);
24453 }
24454 else if (*p == '(')
24455 {
24456 /* It's a function-like macro. */
24457 char *name = savestring (body, p - body);
24458 int argc = 0;
24459 int argv_size = 1;
24460 char **argv = XNEWVEC (char *, argv_size);
24461
24462 p++;
24463
24464 p = consume_improper_spaces (p, body);
24465
24466 /* Parse the formal argument list. */
24467 while (*p && *p != ')')
24468 {
24469 /* Find the extent of the current argument name. */
24470 const char *arg_start = p;
24471
24472 while (*p && *p != ',' && *p != ')' && *p != ' ')
24473 p++;
24474
24475 if (! *p || p == arg_start)
24476 dwarf2_macro_malformed_definition_complaint (body);
24477 else
24478 {
24479 /* Make sure argv has room for the new argument. */
24480 if (argc >= argv_size)
24481 {
24482 argv_size *= 2;
24483 argv = XRESIZEVEC (char *, argv, argv_size);
24484 }
24485
24486 argv[argc++] = savestring (arg_start, p - arg_start);
24487 }
24488
24489 p = consume_improper_spaces (p, body);
24490
24491 /* Consume the comma, if present. */
24492 if (*p == ',')
24493 {
24494 p++;
24495
24496 p = consume_improper_spaces (p, body);
24497 }
24498 }
24499
24500 if (*p == ')')
24501 {
24502 p++;
24503
24504 if (*p == ' ')
24505 /* Perfectly formed definition, no complaints. */
24506 macro_define_function (file, line, name,
24507 argc, (const char **) argv,
24508 p + 1);
24509 else if (*p == '\0')
24510 {
24511 /* Complain, but do define it. */
24512 dwarf2_macro_malformed_definition_complaint (body);
24513 macro_define_function (file, line, name,
24514 argc, (const char **) argv,
24515 p);
24516 }
24517 else
24518 /* Just complain. */
24519 dwarf2_macro_malformed_definition_complaint (body);
24520 }
24521 else
24522 /* Just complain. */
24523 dwarf2_macro_malformed_definition_complaint (body);
24524
24525 xfree (name);
24526 {
24527 int i;
24528
24529 for (i = 0; i < argc; i++)
24530 xfree (argv[i]);
24531 }
24532 xfree (argv);
24533 }
24534 else
24535 dwarf2_macro_malformed_definition_complaint (body);
24536 }
24537
24538 /* Skip some bytes from BYTES according to the form given in FORM.
24539 Returns the new pointer. */
24540
24541 static const gdb_byte *
24542 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24543 enum dwarf_form form,
24544 unsigned int offset_size,
24545 struct dwarf2_section_info *section)
24546 {
24547 unsigned int bytes_read;
24548
24549 switch (form)
24550 {
24551 case DW_FORM_data1:
24552 case DW_FORM_flag:
24553 ++bytes;
24554 break;
24555
24556 case DW_FORM_data2:
24557 bytes += 2;
24558 break;
24559
24560 case DW_FORM_data4:
24561 bytes += 4;
24562 break;
24563
24564 case DW_FORM_data8:
24565 bytes += 8;
24566 break;
24567
24568 case DW_FORM_data16:
24569 bytes += 16;
24570 break;
24571
24572 case DW_FORM_string:
24573 read_direct_string (abfd, bytes, &bytes_read);
24574 bytes += bytes_read;
24575 break;
24576
24577 case DW_FORM_sec_offset:
24578 case DW_FORM_strp:
24579 case DW_FORM_GNU_strp_alt:
24580 bytes += offset_size;
24581 break;
24582
24583 case DW_FORM_block:
24584 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24585 bytes += bytes_read;
24586 break;
24587
24588 case DW_FORM_block1:
24589 bytes += 1 + read_1_byte (abfd, bytes);
24590 break;
24591 case DW_FORM_block2:
24592 bytes += 2 + read_2_bytes (abfd, bytes);
24593 break;
24594 case DW_FORM_block4:
24595 bytes += 4 + read_4_bytes (abfd, bytes);
24596 break;
24597
24598 case DW_FORM_sdata:
24599 case DW_FORM_udata:
24600 case DW_FORM_GNU_addr_index:
24601 case DW_FORM_GNU_str_index:
24602 bytes = gdb_skip_leb128 (bytes, buffer_end);
24603 if (bytes == NULL)
24604 {
24605 dwarf2_section_buffer_overflow_complaint (section);
24606 return NULL;
24607 }
24608 break;
24609
24610 case DW_FORM_implicit_const:
24611 break;
24612
24613 default:
24614 {
24615 complaint (&symfile_complaints,
24616 _("invalid form 0x%x in `%s'"),
24617 form, get_section_name (section));
24618 return NULL;
24619 }
24620 }
24621
24622 return bytes;
24623 }
24624
24625 /* A helper for dwarf_decode_macros that handles skipping an unknown
24626 opcode. Returns an updated pointer to the macro data buffer; or,
24627 on error, issues a complaint and returns NULL. */
24628
24629 static const gdb_byte *
24630 skip_unknown_opcode (unsigned int opcode,
24631 const gdb_byte **opcode_definitions,
24632 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24633 bfd *abfd,
24634 unsigned int offset_size,
24635 struct dwarf2_section_info *section)
24636 {
24637 unsigned int bytes_read, i;
24638 unsigned long arg;
24639 const gdb_byte *defn;
24640
24641 if (opcode_definitions[opcode] == NULL)
24642 {
24643 complaint (&symfile_complaints,
24644 _("unrecognized DW_MACFINO opcode 0x%x"),
24645 opcode);
24646 return NULL;
24647 }
24648
24649 defn = opcode_definitions[opcode];
24650 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24651 defn += bytes_read;
24652
24653 for (i = 0; i < arg; ++i)
24654 {
24655 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24656 (enum dwarf_form) defn[i], offset_size,
24657 section);
24658 if (mac_ptr == NULL)
24659 {
24660 /* skip_form_bytes already issued the complaint. */
24661 return NULL;
24662 }
24663 }
24664
24665 return mac_ptr;
24666 }
24667
24668 /* A helper function which parses the header of a macro section.
24669 If the macro section is the extended (for now called "GNU") type,
24670 then this updates *OFFSET_SIZE. Returns a pointer to just after
24671 the header, or issues a complaint and returns NULL on error. */
24672
24673 static const gdb_byte *
24674 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24675 bfd *abfd,
24676 const gdb_byte *mac_ptr,
24677 unsigned int *offset_size,
24678 int section_is_gnu)
24679 {
24680 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24681
24682 if (section_is_gnu)
24683 {
24684 unsigned int version, flags;
24685
24686 version = read_2_bytes (abfd, mac_ptr);
24687 if (version != 4 && version != 5)
24688 {
24689 complaint (&symfile_complaints,
24690 _("unrecognized version `%d' in .debug_macro section"),
24691 version);
24692 return NULL;
24693 }
24694 mac_ptr += 2;
24695
24696 flags = read_1_byte (abfd, mac_ptr);
24697 ++mac_ptr;
24698 *offset_size = (flags & 1) ? 8 : 4;
24699
24700 if ((flags & 2) != 0)
24701 /* We don't need the line table offset. */
24702 mac_ptr += *offset_size;
24703
24704 /* Vendor opcode descriptions. */
24705 if ((flags & 4) != 0)
24706 {
24707 unsigned int i, count;
24708
24709 count = read_1_byte (abfd, mac_ptr);
24710 ++mac_ptr;
24711 for (i = 0; i < count; ++i)
24712 {
24713 unsigned int opcode, bytes_read;
24714 unsigned long arg;
24715
24716 opcode = read_1_byte (abfd, mac_ptr);
24717 ++mac_ptr;
24718 opcode_definitions[opcode] = mac_ptr;
24719 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24720 mac_ptr += bytes_read;
24721 mac_ptr += arg;
24722 }
24723 }
24724 }
24725
24726 return mac_ptr;
24727 }
24728
24729 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24730 including DW_MACRO_import. */
24731
24732 static void
24733 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24734 bfd *abfd,
24735 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24736 struct macro_source_file *current_file,
24737 struct line_header *lh,
24738 struct dwarf2_section_info *section,
24739 int section_is_gnu, int section_is_dwz,
24740 unsigned int offset_size,
24741 htab_t include_hash)
24742 {
24743 struct objfile *objfile = dwarf2_per_objfile->objfile;
24744 enum dwarf_macro_record_type macinfo_type;
24745 int at_commandline;
24746 const gdb_byte *opcode_definitions[256];
24747
24748 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24749 &offset_size, section_is_gnu);
24750 if (mac_ptr == NULL)
24751 {
24752 /* We already issued a complaint. */
24753 return;
24754 }
24755
24756 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24757 GDB is still reading the definitions from command line. First
24758 DW_MACINFO_start_file will need to be ignored as it was already executed
24759 to create CURRENT_FILE for the main source holding also the command line
24760 definitions. On first met DW_MACINFO_start_file this flag is reset to
24761 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24762
24763 at_commandline = 1;
24764
24765 do
24766 {
24767 /* Do we at least have room for a macinfo type byte? */
24768 if (mac_ptr >= mac_end)
24769 {
24770 dwarf2_section_buffer_overflow_complaint (section);
24771 break;
24772 }
24773
24774 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24775 mac_ptr++;
24776
24777 /* Note that we rely on the fact that the corresponding GNU and
24778 DWARF constants are the same. */
24779 DIAGNOSTIC_PUSH
24780 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24781 switch (macinfo_type)
24782 {
24783 /* A zero macinfo type indicates the end of the macro
24784 information. */
24785 case 0:
24786 break;
24787
24788 case DW_MACRO_define:
24789 case DW_MACRO_undef:
24790 case DW_MACRO_define_strp:
24791 case DW_MACRO_undef_strp:
24792 case DW_MACRO_define_sup:
24793 case DW_MACRO_undef_sup:
24794 {
24795 unsigned int bytes_read;
24796 int line;
24797 const char *body;
24798 int is_define;
24799
24800 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24801 mac_ptr += bytes_read;
24802
24803 if (macinfo_type == DW_MACRO_define
24804 || macinfo_type == DW_MACRO_undef)
24805 {
24806 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24807 mac_ptr += bytes_read;
24808 }
24809 else
24810 {
24811 LONGEST str_offset;
24812
24813 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24814 mac_ptr += offset_size;
24815
24816 if (macinfo_type == DW_MACRO_define_sup
24817 || macinfo_type == DW_MACRO_undef_sup
24818 || section_is_dwz)
24819 {
24820 struct dwz_file *dwz
24821 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24822
24823 body = read_indirect_string_from_dwz (objfile,
24824 dwz, str_offset);
24825 }
24826 else
24827 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24828 abfd, str_offset);
24829 }
24830
24831 is_define = (macinfo_type == DW_MACRO_define
24832 || macinfo_type == DW_MACRO_define_strp
24833 || macinfo_type == DW_MACRO_define_sup);
24834 if (! current_file)
24835 {
24836 /* DWARF violation as no main source is present. */
24837 complaint (&symfile_complaints,
24838 _("debug info with no main source gives macro %s "
24839 "on line %d: %s"),
24840 is_define ? _("definition") : _("undefinition"),
24841 line, body);
24842 break;
24843 }
24844 if ((line == 0 && !at_commandline)
24845 || (line != 0 && at_commandline))
24846 complaint (&symfile_complaints,
24847 _("debug info gives %s macro %s with %s line %d: %s"),
24848 at_commandline ? _("command-line") : _("in-file"),
24849 is_define ? _("definition") : _("undefinition"),
24850 line == 0 ? _("zero") : _("non-zero"), line, body);
24851
24852 if (is_define)
24853 parse_macro_definition (current_file, line, body);
24854 else
24855 {
24856 gdb_assert (macinfo_type == DW_MACRO_undef
24857 || macinfo_type == DW_MACRO_undef_strp
24858 || macinfo_type == DW_MACRO_undef_sup);
24859 macro_undef (current_file, line, body);
24860 }
24861 }
24862 break;
24863
24864 case DW_MACRO_start_file:
24865 {
24866 unsigned int bytes_read;
24867 int line, file;
24868
24869 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24870 mac_ptr += bytes_read;
24871 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24872 mac_ptr += bytes_read;
24873
24874 if ((line == 0 && !at_commandline)
24875 || (line != 0 && at_commandline))
24876 complaint (&symfile_complaints,
24877 _("debug info gives source %d included "
24878 "from %s at %s line %d"),
24879 file, at_commandline ? _("command-line") : _("file"),
24880 line == 0 ? _("zero") : _("non-zero"), line);
24881
24882 if (at_commandline)
24883 {
24884 /* This DW_MACRO_start_file was executed in the
24885 pass one. */
24886 at_commandline = 0;
24887 }
24888 else
24889 current_file = macro_start_file (file, line, current_file, lh);
24890 }
24891 break;
24892
24893 case DW_MACRO_end_file:
24894 if (! current_file)
24895 complaint (&symfile_complaints,
24896 _("macro debug info has an unmatched "
24897 "`close_file' directive"));
24898 else
24899 {
24900 current_file = current_file->included_by;
24901 if (! current_file)
24902 {
24903 enum dwarf_macro_record_type next_type;
24904
24905 /* GCC circa March 2002 doesn't produce the zero
24906 type byte marking the end of the compilation
24907 unit. Complain if it's not there, but exit no
24908 matter what. */
24909
24910 /* Do we at least have room for a macinfo type byte? */
24911 if (mac_ptr >= mac_end)
24912 {
24913 dwarf2_section_buffer_overflow_complaint (section);
24914 return;
24915 }
24916
24917 /* We don't increment mac_ptr here, so this is just
24918 a look-ahead. */
24919 next_type
24920 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24921 mac_ptr);
24922 if (next_type != 0)
24923 complaint (&symfile_complaints,
24924 _("no terminating 0-type entry for "
24925 "macros in `.debug_macinfo' section"));
24926
24927 return;
24928 }
24929 }
24930 break;
24931
24932 case DW_MACRO_import:
24933 case DW_MACRO_import_sup:
24934 {
24935 LONGEST offset;
24936 void **slot;
24937 bfd *include_bfd = abfd;
24938 struct dwarf2_section_info *include_section = section;
24939 const gdb_byte *include_mac_end = mac_end;
24940 int is_dwz = section_is_dwz;
24941 const gdb_byte *new_mac_ptr;
24942
24943 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24944 mac_ptr += offset_size;
24945
24946 if (macinfo_type == DW_MACRO_import_sup)
24947 {
24948 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24949
24950 dwarf2_read_section (objfile, &dwz->macro);
24951
24952 include_section = &dwz->macro;
24953 include_bfd = get_section_bfd_owner (include_section);
24954 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24955 is_dwz = 1;
24956 }
24957
24958 new_mac_ptr = include_section->buffer + offset;
24959 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24960
24961 if (*slot != NULL)
24962 {
24963 /* This has actually happened; see
24964 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24965 complaint (&symfile_complaints,
24966 _("recursive DW_MACRO_import in "
24967 ".debug_macro section"));
24968 }
24969 else
24970 {
24971 *slot = (void *) new_mac_ptr;
24972
24973 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24974 include_bfd, new_mac_ptr,
24975 include_mac_end, current_file, lh,
24976 section, section_is_gnu, is_dwz,
24977 offset_size, include_hash);
24978
24979 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24980 }
24981 }
24982 break;
24983
24984 case DW_MACINFO_vendor_ext:
24985 if (!section_is_gnu)
24986 {
24987 unsigned int bytes_read;
24988
24989 /* This reads the constant, but since we don't recognize
24990 any vendor extensions, we ignore it. */
24991 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24992 mac_ptr += bytes_read;
24993 read_direct_string (abfd, mac_ptr, &bytes_read);
24994 mac_ptr += bytes_read;
24995
24996 /* We don't recognize any vendor extensions. */
24997 break;
24998 }
24999 /* FALLTHROUGH */
25000
25001 default:
25002 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25003 mac_ptr, mac_end, abfd, offset_size,
25004 section);
25005 if (mac_ptr == NULL)
25006 return;
25007 break;
25008 }
25009 DIAGNOSTIC_POP
25010 } while (macinfo_type != 0);
25011 }
25012
25013 static void
25014 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25015 int section_is_gnu)
25016 {
25017 struct dwarf2_per_objfile *dwarf2_per_objfile
25018 = cu->per_cu->dwarf2_per_objfile;
25019 struct objfile *objfile = dwarf2_per_objfile->objfile;
25020 struct line_header *lh = cu->line_header;
25021 bfd *abfd;
25022 const gdb_byte *mac_ptr, *mac_end;
25023 struct macro_source_file *current_file = 0;
25024 enum dwarf_macro_record_type macinfo_type;
25025 unsigned int offset_size = cu->header.offset_size;
25026 const gdb_byte *opcode_definitions[256];
25027 void **slot;
25028 struct dwarf2_section_info *section;
25029 const char *section_name;
25030
25031 if (cu->dwo_unit != NULL)
25032 {
25033 if (section_is_gnu)
25034 {
25035 section = &cu->dwo_unit->dwo_file->sections.macro;
25036 section_name = ".debug_macro.dwo";
25037 }
25038 else
25039 {
25040 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25041 section_name = ".debug_macinfo.dwo";
25042 }
25043 }
25044 else
25045 {
25046 if (section_is_gnu)
25047 {
25048 section = &dwarf2_per_objfile->macro;
25049 section_name = ".debug_macro";
25050 }
25051 else
25052 {
25053 section = &dwarf2_per_objfile->macinfo;
25054 section_name = ".debug_macinfo";
25055 }
25056 }
25057
25058 dwarf2_read_section (objfile, section);
25059 if (section->buffer == NULL)
25060 {
25061 complaint (&symfile_complaints, _("missing %s section"), section_name);
25062 return;
25063 }
25064 abfd = get_section_bfd_owner (section);
25065
25066 /* First pass: Find the name of the base filename.
25067 This filename is needed in order to process all macros whose definition
25068 (or undefinition) comes from the command line. These macros are defined
25069 before the first DW_MACINFO_start_file entry, and yet still need to be
25070 associated to the base file.
25071
25072 To determine the base file name, we scan the macro definitions until we
25073 reach the first DW_MACINFO_start_file entry. We then initialize
25074 CURRENT_FILE accordingly so that any macro definition found before the
25075 first DW_MACINFO_start_file can still be associated to the base file. */
25076
25077 mac_ptr = section->buffer + offset;
25078 mac_end = section->buffer + section->size;
25079
25080 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25081 &offset_size, section_is_gnu);
25082 if (mac_ptr == NULL)
25083 {
25084 /* We already issued a complaint. */
25085 return;
25086 }
25087
25088 do
25089 {
25090 /* Do we at least have room for a macinfo type byte? */
25091 if (mac_ptr >= mac_end)
25092 {
25093 /* Complaint is printed during the second pass as GDB will probably
25094 stop the first pass earlier upon finding
25095 DW_MACINFO_start_file. */
25096 break;
25097 }
25098
25099 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25100 mac_ptr++;
25101
25102 /* Note that we rely on the fact that the corresponding GNU and
25103 DWARF constants are the same. */
25104 DIAGNOSTIC_PUSH
25105 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25106 switch (macinfo_type)
25107 {
25108 /* A zero macinfo type indicates the end of the macro
25109 information. */
25110 case 0:
25111 break;
25112
25113 case DW_MACRO_define:
25114 case DW_MACRO_undef:
25115 /* Only skip the data by MAC_PTR. */
25116 {
25117 unsigned int bytes_read;
25118
25119 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25120 mac_ptr += bytes_read;
25121 read_direct_string (abfd, mac_ptr, &bytes_read);
25122 mac_ptr += bytes_read;
25123 }
25124 break;
25125
25126 case DW_MACRO_start_file:
25127 {
25128 unsigned int bytes_read;
25129 int line, file;
25130
25131 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25132 mac_ptr += bytes_read;
25133 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25134 mac_ptr += bytes_read;
25135
25136 current_file = macro_start_file (file, line, current_file, lh);
25137 }
25138 break;
25139
25140 case DW_MACRO_end_file:
25141 /* No data to skip by MAC_PTR. */
25142 break;
25143
25144 case DW_MACRO_define_strp:
25145 case DW_MACRO_undef_strp:
25146 case DW_MACRO_define_sup:
25147 case DW_MACRO_undef_sup:
25148 {
25149 unsigned int bytes_read;
25150
25151 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25152 mac_ptr += bytes_read;
25153 mac_ptr += offset_size;
25154 }
25155 break;
25156
25157 case DW_MACRO_import:
25158 case DW_MACRO_import_sup:
25159 /* Note that, according to the spec, a transparent include
25160 chain cannot call DW_MACRO_start_file. So, we can just
25161 skip this opcode. */
25162 mac_ptr += offset_size;
25163 break;
25164
25165 case DW_MACINFO_vendor_ext:
25166 /* Only skip the data by MAC_PTR. */
25167 if (!section_is_gnu)
25168 {
25169 unsigned int bytes_read;
25170
25171 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25172 mac_ptr += bytes_read;
25173 read_direct_string (abfd, mac_ptr, &bytes_read);
25174 mac_ptr += bytes_read;
25175 }
25176 /* FALLTHROUGH */
25177
25178 default:
25179 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25180 mac_ptr, mac_end, abfd, offset_size,
25181 section);
25182 if (mac_ptr == NULL)
25183 return;
25184 break;
25185 }
25186 DIAGNOSTIC_POP
25187 } while (macinfo_type != 0 && current_file == NULL);
25188
25189 /* Second pass: Process all entries.
25190
25191 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25192 command-line macro definitions/undefinitions. This flag is unset when we
25193 reach the first DW_MACINFO_start_file entry. */
25194
25195 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25196 htab_eq_pointer,
25197 NULL, xcalloc, xfree));
25198 mac_ptr = section->buffer + offset;
25199 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25200 *slot = (void *) mac_ptr;
25201 dwarf_decode_macro_bytes (dwarf2_per_objfile,
25202 abfd, mac_ptr, mac_end,
25203 current_file, lh, section,
25204 section_is_gnu, 0, offset_size,
25205 include_hash.get ());
25206 }
25207
25208 /* Check if the attribute's form is a DW_FORM_block*
25209 if so return true else false. */
25210
25211 static int
25212 attr_form_is_block (const struct attribute *attr)
25213 {
25214 return (attr == NULL ? 0 :
25215 attr->form == DW_FORM_block1
25216 || attr->form == DW_FORM_block2
25217 || attr->form == DW_FORM_block4
25218 || attr->form == DW_FORM_block
25219 || attr->form == DW_FORM_exprloc);
25220 }
25221
25222 /* Return non-zero if ATTR's value is a section offset --- classes
25223 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25224 You may use DW_UNSND (attr) to retrieve such offsets.
25225
25226 Section 7.5.4, "Attribute Encodings", explains that no attribute
25227 may have a value that belongs to more than one of these classes; it
25228 would be ambiguous if we did, because we use the same forms for all
25229 of them. */
25230
25231 static int
25232 attr_form_is_section_offset (const struct attribute *attr)
25233 {
25234 return (attr->form == DW_FORM_data4
25235 || attr->form == DW_FORM_data8
25236 || attr->form == DW_FORM_sec_offset);
25237 }
25238
25239 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25240 zero otherwise. When this function returns true, you can apply
25241 dwarf2_get_attr_constant_value to it.
25242
25243 However, note that for some attributes you must check
25244 attr_form_is_section_offset before using this test. DW_FORM_data4
25245 and DW_FORM_data8 are members of both the constant class, and of
25246 the classes that contain offsets into other debug sections
25247 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25248 that, if an attribute's can be either a constant or one of the
25249 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25250 taken as section offsets, not constants.
25251
25252 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25253 cannot handle that. */
25254
25255 static int
25256 attr_form_is_constant (const struct attribute *attr)
25257 {
25258 switch (attr->form)
25259 {
25260 case DW_FORM_sdata:
25261 case DW_FORM_udata:
25262 case DW_FORM_data1:
25263 case DW_FORM_data2:
25264 case DW_FORM_data4:
25265 case DW_FORM_data8:
25266 case DW_FORM_implicit_const:
25267 return 1;
25268 default:
25269 return 0;
25270 }
25271 }
25272
25273
25274 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25275 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25276
25277 static int
25278 attr_form_is_ref (const struct attribute *attr)
25279 {
25280 switch (attr->form)
25281 {
25282 case DW_FORM_ref_addr:
25283 case DW_FORM_ref1:
25284 case DW_FORM_ref2:
25285 case DW_FORM_ref4:
25286 case DW_FORM_ref8:
25287 case DW_FORM_ref_udata:
25288 case DW_FORM_GNU_ref_alt:
25289 return 1;
25290 default:
25291 return 0;
25292 }
25293 }
25294
25295 /* Return the .debug_loc section to use for CU.
25296 For DWO files use .debug_loc.dwo. */
25297
25298 static struct dwarf2_section_info *
25299 cu_debug_loc_section (struct dwarf2_cu *cu)
25300 {
25301 struct dwarf2_per_objfile *dwarf2_per_objfile
25302 = cu->per_cu->dwarf2_per_objfile;
25303
25304 if (cu->dwo_unit)
25305 {
25306 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25307
25308 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25309 }
25310 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25311 : &dwarf2_per_objfile->loc);
25312 }
25313
25314 /* A helper function that fills in a dwarf2_loclist_baton. */
25315
25316 static void
25317 fill_in_loclist_baton (struct dwarf2_cu *cu,
25318 struct dwarf2_loclist_baton *baton,
25319 const struct attribute *attr)
25320 {
25321 struct dwarf2_per_objfile *dwarf2_per_objfile
25322 = cu->per_cu->dwarf2_per_objfile;
25323 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25324
25325 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25326
25327 baton->per_cu = cu->per_cu;
25328 gdb_assert (baton->per_cu);
25329 /* We don't know how long the location list is, but make sure we
25330 don't run off the edge of the section. */
25331 baton->size = section->size - DW_UNSND (attr);
25332 baton->data = section->buffer + DW_UNSND (attr);
25333 baton->base_address = cu->base_address;
25334 baton->from_dwo = cu->dwo_unit != NULL;
25335 }
25336
25337 static void
25338 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25339 struct dwarf2_cu *cu, int is_block)
25340 {
25341 struct dwarf2_per_objfile *dwarf2_per_objfile
25342 = cu->per_cu->dwarf2_per_objfile;
25343 struct objfile *objfile = dwarf2_per_objfile->objfile;
25344 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25345
25346 if (attr_form_is_section_offset (attr)
25347 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25348 the section. If so, fall through to the complaint in the
25349 other branch. */
25350 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25351 {
25352 struct dwarf2_loclist_baton *baton;
25353
25354 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25355
25356 fill_in_loclist_baton (cu, baton, attr);
25357
25358 if (cu->base_known == 0)
25359 complaint (&symfile_complaints,
25360 _("Location list used without "
25361 "specifying the CU base address."));
25362
25363 SYMBOL_ACLASS_INDEX (sym) = (is_block
25364 ? dwarf2_loclist_block_index
25365 : dwarf2_loclist_index);
25366 SYMBOL_LOCATION_BATON (sym) = baton;
25367 }
25368 else
25369 {
25370 struct dwarf2_locexpr_baton *baton;
25371
25372 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25373 baton->per_cu = cu->per_cu;
25374 gdb_assert (baton->per_cu);
25375
25376 if (attr_form_is_block (attr))
25377 {
25378 /* Note that we're just copying the block's data pointer
25379 here, not the actual data. We're still pointing into the
25380 info_buffer for SYM's objfile; right now we never release
25381 that buffer, but when we do clean up properly this may
25382 need to change. */
25383 baton->size = DW_BLOCK (attr)->size;
25384 baton->data = DW_BLOCK (attr)->data;
25385 }
25386 else
25387 {
25388 dwarf2_invalid_attrib_class_complaint ("location description",
25389 SYMBOL_NATURAL_NAME (sym));
25390 baton->size = 0;
25391 }
25392
25393 SYMBOL_ACLASS_INDEX (sym) = (is_block
25394 ? dwarf2_locexpr_block_index
25395 : dwarf2_locexpr_index);
25396 SYMBOL_LOCATION_BATON (sym) = baton;
25397 }
25398 }
25399
25400 /* Return the OBJFILE associated with the compilation unit CU. If CU
25401 came from a separate debuginfo file, then the master objfile is
25402 returned. */
25403
25404 struct objfile *
25405 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25406 {
25407 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25408
25409 /* Return the master objfile, so that we can report and look up the
25410 correct file containing this variable. */
25411 if (objfile->separate_debug_objfile_backlink)
25412 objfile = objfile->separate_debug_objfile_backlink;
25413
25414 return objfile;
25415 }
25416
25417 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25418 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25419 CU_HEADERP first. */
25420
25421 static const struct comp_unit_head *
25422 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25423 struct dwarf2_per_cu_data *per_cu)
25424 {
25425 const gdb_byte *info_ptr;
25426
25427 if (per_cu->cu)
25428 return &per_cu->cu->header;
25429
25430 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25431
25432 memset (cu_headerp, 0, sizeof (*cu_headerp));
25433 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25434 rcuh_kind::COMPILE);
25435
25436 return cu_headerp;
25437 }
25438
25439 /* Return the address size given in the compilation unit header for CU. */
25440
25441 int
25442 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25443 {
25444 struct comp_unit_head cu_header_local;
25445 const struct comp_unit_head *cu_headerp;
25446
25447 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25448
25449 return cu_headerp->addr_size;
25450 }
25451
25452 /* Return the offset size given in the compilation unit header for CU. */
25453
25454 int
25455 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25456 {
25457 struct comp_unit_head cu_header_local;
25458 const struct comp_unit_head *cu_headerp;
25459
25460 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25461
25462 return cu_headerp->offset_size;
25463 }
25464
25465 /* See its dwarf2loc.h declaration. */
25466
25467 int
25468 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25469 {
25470 struct comp_unit_head cu_header_local;
25471 const struct comp_unit_head *cu_headerp;
25472
25473 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25474
25475 if (cu_headerp->version == 2)
25476 return cu_headerp->addr_size;
25477 else
25478 return cu_headerp->offset_size;
25479 }
25480
25481 /* Return the text offset of the CU. The returned offset comes from
25482 this CU's objfile. If this objfile came from a separate debuginfo
25483 file, then the offset may be different from the corresponding
25484 offset in the parent objfile. */
25485
25486 CORE_ADDR
25487 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25488 {
25489 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25490
25491 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25492 }
25493
25494 /* Return DWARF version number of PER_CU. */
25495
25496 short
25497 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25498 {
25499 return per_cu->dwarf_version;
25500 }
25501
25502 /* Locate the .debug_info compilation unit from CU's objfile which contains
25503 the DIE at OFFSET. Raises an error on failure. */
25504
25505 static struct dwarf2_per_cu_data *
25506 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25507 unsigned int offset_in_dwz,
25508 struct dwarf2_per_objfile *dwarf2_per_objfile)
25509 {
25510 struct dwarf2_per_cu_data *this_cu;
25511 int low, high;
25512 const sect_offset *cu_off;
25513
25514 low = 0;
25515 high = dwarf2_per_objfile->n_comp_units - 1;
25516 while (high > low)
25517 {
25518 struct dwarf2_per_cu_data *mid_cu;
25519 int mid = low + (high - low) / 2;
25520
25521 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25522 cu_off = &mid_cu->sect_off;
25523 if (mid_cu->is_dwz > offset_in_dwz
25524 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
25525 high = mid;
25526 else
25527 low = mid + 1;
25528 }
25529 gdb_assert (low == high);
25530 this_cu = dwarf2_per_objfile->all_comp_units[low];
25531 cu_off = &this_cu->sect_off;
25532 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
25533 {
25534 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25535 error (_("Dwarf Error: could not find partial DIE containing "
25536 "offset %s [in module %s]"),
25537 sect_offset_str (sect_off),
25538 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25539
25540 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25541 <= sect_off);
25542 return dwarf2_per_objfile->all_comp_units[low-1];
25543 }
25544 else
25545 {
25546 this_cu = dwarf2_per_objfile->all_comp_units[low];
25547 if (low == dwarf2_per_objfile->n_comp_units - 1
25548 && sect_off >= this_cu->sect_off + this_cu->length)
25549 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25550 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25551 return this_cu;
25552 }
25553 }
25554
25555 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25556
25557 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25558 : per_cu (per_cu_),
25559 mark (0),
25560 has_loclist (0),
25561 checked_producer (0),
25562 producer_is_gxx_lt_4_6 (0),
25563 producer_is_gcc_lt_4_3 (0),
25564 producer_is_icc_lt_14 (0),
25565 processing_has_namespace_info (0)
25566 {
25567 per_cu->cu = this;
25568 }
25569
25570 /* Destroy a dwarf2_cu. */
25571
25572 dwarf2_cu::~dwarf2_cu ()
25573 {
25574 per_cu->cu = NULL;
25575 }
25576
25577 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25578
25579 static void
25580 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25581 enum language pretend_language)
25582 {
25583 struct attribute *attr;
25584
25585 /* Set the language we're debugging. */
25586 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25587 if (attr)
25588 set_cu_language (DW_UNSND (attr), cu);
25589 else
25590 {
25591 cu->language = pretend_language;
25592 cu->language_defn = language_def (cu->language);
25593 }
25594
25595 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25596 }
25597
25598 /* Free all cached compilation units. */
25599
25600 static void
25601 free_cached_comp_units (void *data)
25602 {
25603 struct dwarf2_per_objfile *dwarf2_per_objfile
25604 = (struct dwarf2_per_objfile *) data;
25605
25606 dwarf2_per_objfile->free_cached_comp_units ();
25607 }
25608
25609 /* Increase the age counter on each cached compilation unit, and free
25610 any that are too old. */
25611
25612 static void
25613 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25614 {
25615 struct dwarf2_per_cu_data *per_cu, **last_chain;
25616
25617 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25618 per_cu = dwarf2_per_objfile->read_in_chain;
25619 while (per_cu != NULL)
25620 {
25621 per_cu->cu->last_used ++;
25622 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25623 dwarf2_mark (per_cu->cu);
25624 per_cu = per_cu->cu->read_in_chain;
25625 }
25626
25627 per_cu = dwarf2_per_objfile->read_in_chain;
25628 last_chain = &dwarf2_per_objfile->read_in_chain;
25629 while (per_cu != NULL)
25630 {
25631 struct dwarf2_per_cu_data *next_cu;
25632
25633 next_cu = per_cu->cu->read_in_chain;
25634
25635 if (!per_cu->cu->mark)
25636 {
25637 delete per_cu->cu;
25638 *last_chain = next_cu;
25639 }
25640 else
25641 last_chain = &per_cu->cu->read_in_chain;
25642
25643 per_cu = next_cu;
25644 }
25645 }
25646
25647 /* Remove a single compilation unit from the cache. */
25648
25649 static void
25650 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25651 {
25652 struct dwarf2_per_cu_data *per_cu, **last_chain;
25653 struct dwarf2_per_objfile *dwarf2_per_objfile
25654 = target_per_cu->dwarf2_per_objfile;
25655
25656 per_cu = dwarf2_per_objfile->read_in_chain;
25657 last_chain = &dwarf2_per_objfile->read_in_chain;
25658 while (per_cu != NULL)
25659 {
25660 struct dwarf2_per_cu_data *next_cu;
25661
25662 next_cu = per_cu->cu->read_in_chain;
25663
25664 if (per_cu == target_per_cu)
25665 {
25666 delete per_cu->cu;
25667 per_cu->cu = NULL;
25668 *last_chain = next_cu;
25669 break;
25670 }
25671 else
25672 last_chain = &per_cu->cu->read_in_chain;
25673
25674 per_cu = next_cu;
25675 }
25676 }
25677
25678 /* Release all extra memory associated with OBJFILE. */
25679
25680 void
25681 dwarf2_free_objfile (struct objfile *objfile)
25682 {
25683 struct dwarf2_per_objfile *dwarf2_per_objfile
25684 = get_dwarf2_per_objfile (objfile);
25685
25686 delete dwarf2_per_objfile;
25687 }
25688
25689 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25690 We store these in a hash table separate from the DIEs, and preserve them
25691 when the DIEs are flushed out of cache.
25692
25693 The CU "per_cu" pointer is needed because offset alone is not enough to
25694 uniquely identify the type. A file may have multiple .debug_types sections,
25695 or the type may come from a DWO file. Furthermore, while it's more logical
25696 to use per_cu->section+offset, with Fission the section with the data is in
25697 the DWO file but we don't know that section at the point we need it.
25698 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25699 because we can enter the lookup routine, get_die_type_at_offset, from
25700 outside this file, and thus won't necessarily have PER_CU->cu.
25701 Fortunately, PER_CU is stable for the life of the objfile. */
25702
25703 struct dwarf2_per_cu_offset_and_type
25704 {
25705 const struct dwarf2_per_cu_data *per_cu;
25706 sect_offset sect_off;
25707 struct type *type;
25708 };
25709
25710 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25711
25712 static hashval_t
25713 per_cu_offset_and_type_hash (const void *item)
25714 {
25715 const struct dwarf2_per_cu_offset_and_type *ofs
25716 = (const struct dwarf2_per_cu_offset_and_type *) item;
25717
25718 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25719 }
25720
25721 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25722
25723 static int
25724 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25725 {
25726 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25727 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25728 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25729 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25730
25731 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25732 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25733 }
25734
25735 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25736 table if necessary. For convenience, return TYPE.
25737
25738 The DIEs reading must have careful ordering to:
25739 * Not cause infite loops trying to read in DIEs as a prerequisite for
25740 reading current DIE.
25741 * Not trying to dereference contents of still incompletely read in types
25742 while reading in other DIEs.
25743 * Enable referencing still incompletely read in types just by a pointer to
25744 the type without accessing its fields.
25745
25746 Therefore caller should follow these rules:
25747 * Try to fetch any prerequisite types we may need to build this DIE type
25748 before building the type and calling set_die_type.
25749 * After building type call set_die_type for current DIE as soon as
25750 possible before fetching more types to complete the current type.
25751 * Make the type as complete as possible before fetching more types. */
25752
25753 static struct type *
25754 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25755 {
25756 struct dwarf2_per_objfile *dwarf2_per_objfile
25757 = cu->per_cu->dwarf2_per_objfile;
25758 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25759 struct objfile *objfile = dwarf2_per_objfile->objfile;
25760 struct attribute *attr;
25761 struct dynamic_prop prop;
25762
25763 /* For Ada types, make sure that the gnat-specific data is always
25764 initialized (if not already set). There are a few types where
25765 we should not be doing so, because the type-specific area is
25766 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25767 where the type-specific area is used to store the floatformat).
25768 But this is not a problem, because the gnat-specific information
25769 is actually not needed for these types. */
25770 if (need_gnat_info (cu)
25771 && TYPE_CODE (type) != TYPE_CODE_FUNC
25772 && TYPE_CODE (type) != TYPE_CODE_FLT
25773 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25774 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25775 && TYPE_CODE (type) != TYPE_CODE_METHOD
25776 && !HAVE_GNAT_AUX_INFO (type))
25777 INIT_GNAT_SPECIFIC (type);
25778
25779 /* Read DW_AT_allocated and set in type. */
25780 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25781 if (attr_form_is_block (attr))
25782 {
25783 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25784 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25785 }
25786 else if (attr != NULL)
25787 {
25788 complaint (&symfile_complaints,
25789 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25790 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25791 sect_offset_str (die->sect_off));
25792 }
25793
25794 /* Read DW_AT_associated and set in type. */
25795 attr = dwarf2_attr (die, DW_AT_associated, cu);
25796 if (attr_form_is_block (attr))
25797 {
25798 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25799 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25800 }
25801 else if (attr != NULL)
25802 {
25803 complaint (&symfile_complaints,
25804 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25805 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25806 sect_offset_str (die->sect_off));
25807 }
25808
25809 /* Read DW_AT_data_location and set in type. */
25810 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25811 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25812 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25813
25814 if (dwarf2_per_objfile->die_type_hash == NULL)
25815 {
25816 dwarf2_per_objfile->die_type_hash =
25817 htab_create_alloc_ex (127,
25818 per_cu_offset_and_type_hash,
25819 per_cu_offset_and_type_eq,
25820 NULL,
25821 &objfile->objfile_obstack,
25822 hashtab_obstack_allocate,
25823 dummy_obstack_deallocate);
25824 }
25825
25826 ofs.per_cu = cu->per_cu;
25827 ofs.sect_off = die->sect_off;
25828 ofs.type = type;
25829 slot = (struct dwarf2_per_cu_offset_and_type **)
25830 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25831 if (*slot)
25832 complaint (&symfile_complaints,
25833 _("A problem internal to GDB: DIE %s has type already set"),
25834 sect_offset_str (die->sect_off));
25835 *slot = XOBNEW (&objfile->objfile_obstack,
25836 struct dwarf2_per_cu_offset_and_type);
25837 **slot = ofs;
25838 return type;
25839 }
25840
25841 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25842 or return NULL if the die does not have a saved type. */
25843
25844 static struct type *
25845 get_die_type_at_offset (sect_offset sect_off,
25846 struct dwarf2_per_cu_data *per_cu)
25847 {
25848 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25849 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25850
25851 if (dwarf2_per_objfile->die_type_hash == NULL)
25852 return NULL;
25853
25854 ofs.per_cu = per_cu;
25855 ofs.sect_off = sect_off;
25856 slot = ((struct dwarf2_per_cu_offset_and_type *)
25857 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25858 if (slot)
25859 return slot->type;
25860 else
25861 return NULL;
25862 }
25863
25864 /* Look up the type for DIE in CU in die_type_hash,
25865 or return NULL if DIE does not have a saved type. */
25866
25867 static struct type *
25868 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25869 {
25870 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25871 }
25872
25873 /* Add a dependence relationship from CU to REF_PER_CU. */
25874
25875 static void
25876 dwarf2_add_dependence (struct dwarf2_cu *cu,
25877 struct dwarf2_per_cu_data *ref_per_cu)
25878 {
25879 void **slot;
25880
25881 if (cu->dependencies == NULL)
25882 cu->dependencies
25883 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25884 NULL, &cu->comp_unit_obstack,
25885 hashtab_obstack_allocate,
25886 dummy_obstack_deallocate);
25887
25888 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25889 if (*slot == NULL)
25890 *slot = ref_per_cu;
25891 }
25892
25893 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25894 Set the mark field in every compilation unit in the
25895 cache that we must keep because we are keeping CU. */
25896
25897 static int
25898 dwarf2_mark_helper (void **slot, void *data)
25899 {
25900 struct dwarf2_per_cu_data *per_cu;
25901
25902 per_cu = (struct dwarf2_per_cu_data *) *slot;
25903
25904 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25905 reading of the chain. As such dependencies remain valid it is not much
25906 useful to track and undo them during QUIT cleanups. */
25907 if (per_cu->cu == NULL)
25908 return 1;
25909
25910 if (per_cu->cu->mark)
25911 return 1;
25912 per_cu->cu->mark = 1;
25913
25914 if (per_cu->cu->dependencies != NULL)
25915 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25916
25917 return 1;
25918 }
25919
25920 /* Set the mark field in CU and in every other compilation unit in the
25921 cache that we must keep because we are keeping CU. */
25922
25923 static void
25924 dwarf2_mark (struct dwarf2_cu *cu)
25925 {
25926 if (cu->mark)
25927 return;
25928 cu->mark = 1;
25929 if (cu->dependencies != NULL)
25930 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25931 }
25932
25933 static void
25934 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25935 {
25936 while (per_cu)
25937 {
25938 per_cu->cu->mark = 0;
25939 per_cu = per_cu->cu->read_in_chain;
25940 }
25941 }
25942
25943 /* Trivial hash function for partial_die_info: the hash value of a DIE
25944 is its offset in .debug_info for this objfile. */
25945
25946 static hashval_t
25947 partial_die_hash (const void *item)
25948 {
25949 const struct partial_die_info *part_die
25950 = (const struct partial_die_info *) item;
25951
25952 return to_underlying (part_die->sect_off);
25953 }
25954
25955 /* Trivial comparison function for partial_die_info structures: two DIEs
25956 are equal if they have the same offset. */
25957
25958 static int
25959 partial_die_eq (const void *item_lhs, const void *item_rhs)
25960 {
25961 const struct partial_die_info *part_die_lhs
25962 = (const struct partial_die_info *) item_lhs;
25963 const struct partial_die_info *part_die_rhs
25964 = (const struct partial_die_info *) item_rhs;
25965
25966 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25967 }
25968
25969 static struct cmd_list_element *set_dwarf_cmdlist;
25970 static struct cmd_list_element *show_dwarf_cmdlist;
25971
25972 static void
25973 set_dwarf_cmd (const char *args, int from_tty)
25974 {
25975 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25976 gdb_stdout);
25977 }
25978
25979 static void
25980 show_dwarf_cmd (const char *args, int from_tty)
25981 {
25982 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25983 }
25984
25985 /* The "save gdb-index" command. */
25986
25987 /* Write SIZE bytes from the buffer pointed to by DATA to FILE, with
25988 error checking. */
25989
25990 static void
25991 file_write (FILE *file, const void *data, size_t size)
25992 {
25993 if (fwrite (data, 1, size, file) != size)
25994 error (_("couldn't data write to file"));
25995 }
25996
25997 /* Write the contents of VEC to FILE, with error checking. */
25998
25999 template<typename Elem, typename Alloc>
26000 static void
26001 file_write (FILE *file, const std::vector<Elem, Alloc> &vec)
26002 {
26003 file_write (file, vec.data (), vec.size () * sizeof (vec[0]));
26004 }
26005
26006 /* In-memory buffer to prepare data to be written later to a file. */
26007 class data_buf
26008 {
26009 public:
26010 /* Copy DATA to the end of the buffer. */
26011 template<typename T>
26012 void append_data (const T &data)
26013 {
26014 std::copy (reinterpret_cast<const gdb_byte *> (&data),
26015 reinterpret_cast<const gdb_byte *> (&data + 1),
26016 grow (sizeof (data)));
26017 }
26018
26019 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
26020 terminating zero is appended too. */
26021 void append_cstr0 (const char *cstr)
26022 {
26023 const size_t size = strlen (cstr) + 1;
26024 std::copy (cstr, cstr + size, grow (size));
26025 }
26026
26027 /* Store INPUT as ULEB128 to the end of buffer. */
26028 void append_unsigned_leb128 (ULONGEST input)
26029 {
26030 for (;;)
26031 {
26032 gdb_byte output = input & 0x7f;
26033 input >>= 7;
26034 if (input)
26035 output |= 0x80;
26036 append_data (output);
26037 if (input == 0)
26038 break;
26039 }
26040 }
26041
26042 /* Accept a host-format integer in VAL and append it to the buffer
26043 as a target-format integer which is LEN bytes long. */
26044 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
26045 {
26046 ::store_unsigned_integer (grow (len), len, byte_order, val);
26047 }
26048
26049 /* Return the size of the buffer. */
26050 size_t size () const
26051 {
26052 return m_vec.size ();
26053 }
26054
26055 /* Return true iff the buffer is empty. */
26056 bool empty () const
26057 {
26058 return m_vec.empty ();
26059 }
26060
26061 /* Write the buffer to FILE. */
26062 void file_write (FILE *file) const
26063 {
26064 ::file_write (file, m_vec);
26065 }
26066
26067 private:
26068 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
26069 the start of the new block. */
26070 gdb_byte *grow (size_t size)
26071 {
26072 m_vec.resize (m_vec.size () + size);
26073 return &*m_vec.end () - size;
26074 }
26075
26076 gdb::byte_vector m_vec;
26077 };
26078
26079 /* An entry in the symbol table. */
26080 struct symtab_index_entry
26081 {
26082 /* The name of the symbol. */
26083 const char *name;
26084 /* The offset of the name in the constant pool. */
26085 offset_type index_offset;
26086 /* A sorted vector of the indices of all the CUs that hold an object
26087 of this name. */
26088 std::vector<offset_type> cu_indices;
26089 };
26090
26091 /* The symbol table. This is a power-of-2-sized hash table. */
26092 struct mapped_symtab
26093 {
26094 mapped_symtab ()
26095 {
26096 data.resize (1024);
26097 }
26098
26099 offset_type n_elements = 0;
26100 std::vector<symtab_index_entry> data;
26101 };
26102
26103 /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
26104 the slot.
26105
26106 Function is used only during write_hash_table so no index format backward
26107 compatibility is needed. */
26108
26109 static symtab_index_entry &
26110 find_slot (struct mapped_symtab *symtab, const char *name)
26111 {
26112 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
26113
26114 index = hash & (symtab->data.size () - 1);
26115 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
26116
26117 for (;;)
26118 {
26119 if (symtab->data[index].name == NULL
26120 || strcmp (name, symtab->data[index].name) == 0)
26121 return symtab->data[index];
26122 index = (index + step) & (symtab->data.size () - 1);
26123 }
26124 }
26125
26126 /* Expand SYMTAB's hash table. */
26127
26128 static void
26129 hash_expand (struct mapped_symtab *symtab)
26130 {
26131 auto old_entries = std::move (symtab->data);
26132
26133 symtab->data.clear ();
26134 symtab->data.resize (old_entries.size () * 2);
26135
26136 for (auto &it : old_entries)
26137 if (it.name != NULL)
26138 {
26139 auto &ref = find_slot (symtab, it.name);
26140 ref = std::move (it);
26141 }
26142 }
26143
26144 /* Add an entry to SYMTAB. NAME is the name of the symbol.
26145 CU_INDEX is the index of the CU in which the symbol appears.
26146 IS_STATIC is one if the symbol is static, otherwise zero (global). */
26147
26148 static void
26149 add_index_entry (struct mapped_symtab *symtab, const char *name,
26150 int is_static, gdb_index_symbol_kind kind,
26151 offset_type cu_index)
26152 {
26153 offset_type cu_index_and_attrs;
26154
26155 ++symtab->n_elements;
26156 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
26157 hash_expand (symtab);
26158
26159 symtab_index_entry &slot = find_slot (symtab, name);
26160 if (slot.name == NULL)
26161 {
26162 slot.name = name;
26163 /* index_offset is set later. */
26164 }
26165
26166 cu_index_and_attrs = 0;
26167 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
26168 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
26169 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
26170
26171 /* We don't want to record an index value twice as we want to avoid the
26172 duplication.
26173 We process all global symbols and then all static symbols
26174 (which would allow us to avoid the duplication by only having to check
26175 the last entry pushed), but a symbol could have multiple kinds in one CU.
26176 To keep things simple we don't worry about the duplication here and
26177 sort and uniqufy the list after we've processed all symbols. */
26178 slot.cu_indices.push_back (cu_index_and_attrs);
26179 }
26180
26181 /* Sort and remove duplicates of all symbols' cu_indices lists. */
26182
26183 static void
26184 uniquify_cu_indices (struct mapped_symtab *symtab)
26185 {
26186 for (auto &entry : symtab->data)
26187 {
26188 if (entry.name != NULL && !entry.cu_indices.empty ())
26189 {
26190 auto &cu_indices = entry.cu_indices;
26191 std::sort (cu_indices.begin (), cu_indices.end ());
26192 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
26193 cu_indices.erase (from, cu_indices.end ());
26194 }
26195 }
26196 }
26197
26198 /* A form of 'const char *' suitable for container keys. Only the
26199 pointer is stored. The strings themselves are compared, not the
26200 pointers. */
26201 class c_str_view
26202 {
26203 public:
26204 c_str_view (const char *cstr)
26205 : m_cstr (cstr)
26206 {}
26207
26208 bool operator== (const c_str_view &other) const
26209 {
26210 return strcmp (m_cstr, other.m_cstr) == 0;
26211 }
26212
26213 /* Return the underlying C string. Note, the returned string is
26214 only a reference with lifetime of this object. */
26215 const char *c_str () const
26216 {
26217 return m_cstr;
26218 }
26219
26220 private:
26221 friend class c_str_view_hasher;
26222 const char *const m_cstr;
26223 };
26224
26225 /* A std::unordered_map::hasher for c_str_view that uses the right
26226 hash function for strings in a mapped index. */
26227 class c_str_view_hasher
26228 {
26229 public:
26230 size_t operator () (const c_str_view &x) const
26231 {
26232 return mapped_index_string_hash (INT_MAX, x.m_cstr);
26233 }
26234 };
26235
26236 /* A std::unordered_map::hasher for std::vector<>. */
26237 template<typename T>
26238 class vector_hasher
26239 {
26240 public:
26241 size_t operator () (const std::vector<T> &key) const
26242 {
26243 return iterative_hash (key.data (),
26244 sizeof (key.front ()) * key.size (), 0);
26245 }
26246 };
26247
26248 /* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
26249 constant pool entries going into the data buffer CPOOL. */
26250
26251 static void
26252 write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
26253 {
26254 {
26255 /* Elements are sorted vectors of the indices of all the CUs that
26256 hold an object of this name. */
26257 std::unordered_map<std::vector<offset_type>, offset_type,
26258 vector_hasher<offset_type>>
26259 symbol_hash_table;
26260
26261 /* We add all the index vectors to the constant pool first, to
26262 ensure alignment is ok. */
26263 for (symtab_index_entry &entry : symtab->data)
26264 {
26265 if (entry.name == NULL)
26266 continue;
26267 gdb_assert (entry.index_offset == 0);
26268
26269 /* Finding before inserting is faster than always trying to
26270 insert, because inserting always allocates a node, does the
26271 lookup, and then destroys the new node if another node
26272 already had the same key. C++17 try_emplace will avoid
26273 this. */
26274 const auto found
26275 = symbol_hash_table.find (entry.cu_indices);
26276 if (found != symbol_hash_table.end ())
26277 {
26278 entry.index_offset = found->second;
26279 continue;
26280 }
26281
26282 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
26283 entry.index_offset = cpool.size ();
26284 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
26285 for (const auto index : entry.cu_indices)
26286 cpool.append_data (MAYBE_SWAP (index));
26287 }
26288 }
26289
26290 /* Now write out the hash table. */
26291 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
26292 for (const auto &entry : symtab->data)
26293 {
26294 offset_type str_off, vec_off;
26295
26296 if (entry.name != NULL)
26297 {
26298 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
26299 if (insertpair.second)
26300 cpool.append_cstr0 (entry.name);
26301 str_off = insertpair.first->second;
26302 vec_off = entry.index_offset;
26303 }
26304 else
26305 {
26306 /* While 0 is a valid constant pool index, it is not valid
26307 to have 0 for both offsets. */
26308 str_off = 0;
26309 vec_off = 0;
26310 }
26311
26312 output.append_data (MAYBE_SWAP (str_off));
26313 output.append_data (MAYBE_SWAP (vec_off));
26314 }
26315 }
26316
26317 typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
26318
26319 /* Helper struct for building the address table. */
26320 struct addrmap_index_data
26321 {
26322 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
26323 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
26324 {}
26325
26326 struct objfile *objfile;
26327 data_buf &addr_vec;
26328 psym_index_map &cu_index_htab;
26329
26330 /* Non-zero if the previous_* fields are valid.
26331 We can't write an entry until we see the next entry (since it is only then
26332 that we know the end of the entry). */
26333 int previous_valid;
26334 /* Index of the CU in the table of all CUs in the index file. */
26335 unsigned int previous_cu_index;
26336 /* Start address of the CU. */
26337 CORE_ADDR previous_cu_start;
26338 };
26339
26340 /* Write an address entry to ADDR_VEC. */
26341
26342 static void
26343 add_address_entry (struct objfile *objfile, data_buf &addr_vec,
26344 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
26345 {
26346 CORE_ADDR baseaddr;
26347
26348 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
26349
26350 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
26351 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
26352 addr_vec.append_data (MAYBE_SWAP (cu_index));
26353 }
26354
26355 /* Worker function for traversing an addrmap to build the address table. */
26356
26357 static int
26358 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
26359 {
26360 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
26361 struct partial_symtab *pst = (struct partial_symtab *) obj;
26362
26363 if (data->previous_valid)
26364 add_address_entry (data->objfile, data->addr_vec,
26365 data->previous_cu_start, start_addr,
26366 data->previous_cu_index);
26367
26368 data->previous_cu_start = start_addr;
26369 if (pst != NULL)
26370 {
26371 const auto it = data->cu_index_htab.find (pst);
26372 gdb_assert (it != data->cu_index_htab.cend ());
26373 data->previous_cu_index = it->second;
26374 data->previous_valid = 1;
26375 }
26376 else
26377 data->previous_valid = 0;
26378
26379 return 0;
26380 }
26381
26382 /* Write OBJFILE's address map to ADDR_VEC.
26383 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
26384 in the index file. */
26385
26386 static void
26387 write_address_map (struct objfile *objfile, data_buf &addr_vec,
26388 psym_index_map &cu_index_htab)
26389 {
26390 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
26391
26392 /* When writing the address table, we have to cope with the fact that
26393 the addrmap iterator only provides the start of a region; we have to
26394 wait until the next invocation to get the start of the next region. */
26395
26396 addrmap_index_data.objfile = objfile;
26397 addrmap_index_data.previous_valid = 0;
26398
26399 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
26400 &addrmap_index_data);
26401
26402 /* It's highly unlikely the last entry (end address = 0xff...ff)
26403 is valid, but we should still handle it.
26404 The end address is recorded as the start of the next region, but that
26405 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
26406 anyway. */
26407 if (addrmap_index_data.previous_valid)
26408 add_address_entry (objfile, addr_vec,
26409 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
26410 addrmap_index_data.previous_cu_index);
26411 }
26412
26413 /* Return the symbol kind of PSYM. */
26414
26415 static gdb_index_symbol_kind
26416 symbol_kind (struct partial_symbol *psym)
26417 {
26418 domain_enum domain = PSYMBOL_DOMAIN (psym);
26419 enum address_class aclass = PSYMBOL_CLASS (psym);
26420
26421 switch (domain)
26422 {
26423 case VAR_DOMAIN:
26424 switch (aclass)
26425 {
26426 case LOC_BLOCK:
26427 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
26428 case LOC_TYPEDEF:
26429 return GDB_INDEX_SYMBOL_KIND_TYPE;
26430 case LOC_COMPUTED:
26431 case LOC_CONST_BYTES:
26432 case LOC_OPTIMIZED_OUT:
26433 case LOC_STATIC:
26434 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26435 case LOC_CONST:
26436 /* Note: It's currently impossible to recognize psyms as enum values
26437 short of reading the type info. For now punt. */
26438 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
26439 default:
26440 /* There are other LOC_FOO values that one might want to classify
26441 as variables, but dwarf2read.c doesn't currently use them. */
26442 return GDB_INDEX_SYMBOL_KIND_OTHER;
26443 }
26444 case STRUCT_DOMAIN:
26445 return GDB_INDEX_SYMBOL_KIND_TYPE;
26446 default:
26447 return GDB_INDEX_SYMBOL_KIND_OTHER;
26448 }
26449 }
26450
26451 /* Add a list of partial symbols to SYMTAB. */
26452
26453 static void
26454 write_psymbols (struct mapped_symtab *symtab,
26455 std::unordered_set<partial_symbol *> &psyms_seen,
26456 struct partial_symbol **psymp,
26457 int count,
26458 offset_type cu_index,
26459 int is_static)
26460 {
26461 for (; count-- > 0; ++psymp)
26462 {
26463 struct partial_symbol *psym = *psymp;
26464
26465 if (SYMBOL_LANGUAGE (psym) == language_ada)
26466 error (_("Ada is not currently supported by the index"));
26467
26468 /* Only add a given psymbol once. */
26469 if (psyms_seen.insert (psym).second)
26470 {
26471 gdb_index_symbol_kind kind = symbol_kind (psym);
26472
26473 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
26474 is_static, kind, cu_index);
26475 }
26476 }
26477 }
26478
26479 /* A helper struct used when iterating over debug_types. */
26480 struct signatured_type_index_data
26481 {
26482 signatured_type_index_data (data_buf &types_list_,
26483 std::unordered_set<partial_symbol *> &psyms_seen_)
26484 : types_list (types_list_), psyms_seen (psyms_seen_)
26485 {}
26486
26487 struct objfile *objfile;
26488 struct mapped_symtab *symtab;
26489 data_buf &types_list;
26490 std::unordered_set<partial_symbol *> &psyms_seen;
26491 int cu_index;
26492 };
26493
26494 /* A helper function that writes a single signatured_type to an
26495 obstack. */
26496
26497 static int
26498 write_one_signatured_type (void **slot, void *d)
26499 {
26500 struct signatured_type_index_data *info
26501 = (struct signatured_type_index_data *) d;
26502 struct signatured_type *entry = (struct signatured_type *) *slot;
26503 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
26504
26505 write_psymbols (info->symtab,
26506 info->psyms_seen,
26507 &info->objfile->global_psymbols[psymtab->globals_offset],
26508 psymtab->n_global_syms, info->cu_index,
26509 0);
26510 write_psymbols (info->symtab,
26511 info->psyms_seen,
26512 &info->objfile->static_psymbols[psymtab->statics_offset],
26513 psymtab->n_static_syms, info->cu_index,
26514 1);
26515
26516 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26517 to_underlying (entry->per_cu.sect_off));
26518 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
26519 to_underlying (entry->type_offset_in_tu));
26520 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
26521
26522 ++info->cu_index;
26523
26524 return 1;
26525 }
26526
26527 /* Recurse into all "included" dependencies and count their symbols as
26528 if they appeared in this psymtab. */
26529
26530 static void
26531 recursively_count_psymbols (struct partial_symtab *psymtab,
26532 size_t &psyms_seen)
26533 {
26534 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26535 if (psymtab->dependencies[i]->user != NULL)
26536 recursively_count_psymbols (psymtab->dependencies[i],
26537 psyms_seen);
26538
26539 psyms_seen += psymtab->n_global_syms;
26540 psyms_seen += psymtab->n_static_syms;
26541 }
26542
26543 /* Recurse into all "included" dependencies and write their symbols as
26544 if they appeared in this psymtab. */
26545
26546 static void
26547 recursively_write_psymbols (struct objfile *objfile,
26548 struct partial_symtab *psymtab,
26549 struct mapped_symtab *symtab,
26550 std::unordered_set<partial_symbol *> &psyms_seen,
26551 offset_type cu_index)
26552 {
26553 int i;
26554
26555 for (i = 0; i < psymtab->number_of_dependencies; ++i)
26556 if (psymtab->dependencies[i]->user != NULL)
26557 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26558 symtab, psyms_seen, cu_index);
26559
26560 write_psymbols (symtab,
26561 psyms_seen,
26562 &objfile->global_psymbols[psymtab->globals_offset],
26563 psymtab->n_global_syms, cu_index,
26564 0);
26565 write_psymbols (symtab,
26566 psyms_seen,
26567 &objfile->static_psymbols[psymtab->statics_offset],
26568 psymtab->n_static_syms, cu_index,
26569 1);
26570 }
26571
26572 /* DWARF-5 .debug_names builder. */
26573 class debug_names
26574 {
26575 public:
26576 debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile, bool is_dwarf64,
26577 bfd_endian dwarf5_byte_order)
26578 : m_dwarf5_byte_order (dwarf5_byte_order),
26579 m_dwarf32 (dwarf5_byte_order),
26580 m_dwarf64 (dwarf5_byte_order),
26581 m_dwarf (is_dwarf64
26582 ? static_cast<dwarf &> (m_dwarf64)
26583 : static_cast<dwarf &> (m_dwarf32)),
26584 m_name_table_string_offs (m_dwarf.name_table_string_offs),
26585 m_name_table_entry_offs (m_dwarf.name_table_entry_offs),
26586 m_debugstrlookup (dwarf2_per_objfile)
26587 {}
26588
26589 int dwarf5_offset_size () const
26590 {
26591 const bool dwarf5_is_dwarf64 = &m_dwarf == &m_dwarf64;
26592 return dwarf5_is_dwarf64 ? 8 : 4;
26593 }
26594
26595 /* Is this symbol from DW_TAG_compile_unit or DW_TAG_type_unit? */
26596 enum class unit_kind { cu, tu };
26597
26598 /* Insert one symbol. */
26599 void insert (const partial_symbol *psym, int cu_index, bool is_static,
26600 unit_kind kind)
26601 {
26602 const int dwarf_tag = psymbol_tag (psym);
26603 if (dwarf_tag == 0)
26604 return;
26605 const char *const name = SYMBOL_SEARCH_NAME (psym);
26606 const auto insertpair
26607 = m_name_to_value_set.emplace (c_str_view (name),
26608 std::set<symbol_value> ());
26609 std::set<symbol_value> &value_set = insertpair.first->second;
26610 value_set.emplace (symbol_value (dwarf_tag, cu_index, is_static, kind));
26611 }
26612
26613 /* Build all the tables. All symbols must be already inserted.
26614 This function does not call file_write, caller has to do it
26615 afterwards. */
26616 void build ()
26617 {
26618 /* Verify the build method has not be called twice. */
26619 gdb_assert (m_abbrev_table.empty ());
26620 const size_t name_count = m_name_to_value_set.size ();
26621 m_bucket_table.resize
26622 (std::pow (2, std::ceil (std::log2 (name_count * 4 / 3))));
26623 m_hash_table.reserve (name_count);
26624 m_name_table_string_offs.reserve (name_count);
26625 m_name_table_entry_offs.reserve (name_count);
26626
26627 /* Map each hash of symbol to its name and value. */
26628 struct hash_it_pair
26629 {
26630 uint32_t hash;
26631 decltype (m_name_to_value_set)::const_iterator it;
26632 };
26633 std::vector<std::forward_list<hash_it_pair>> bucket_hash;
26634 bucket_hash.resize (m_bucket_table.size ());
26635 for (decltype (m_name_to_value_set)::const_iterator it
26636 = m_name_to_value_set.cbegin ();
26637 it != m_name_to_value_set.cend ();
26638 ++it)
26639 {
26640 const char *const name = it->first.c_str ();
26641 const uint32_t hash = dwarf5_djb_hash (name);
26642 hash_it_pair hashitpair;
26643 hashitpair.hash = hash;
26644 hashitpair.it = it;
26645 auto &slot = bucket_hash[hash % bucket_hash.size()];
26646 slot.push_front (std::move (hashitpair));
26647 }
26648 for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix)
26649 {
26650 const std::forward_list<hash_it_pair> &hashitlist
26651 = bucket_hash[bucket_ix];
26652 if (hashitlist.empty ())
26653 continue;
26654 uint32_t &bucket_slot = m_bucket_table[bucket_ix];
26655 /* The hashes array is indexed starting at 1. */
26656 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&bucket_slot),
26657 sizeof (bucket_slot), m_dwarf5_byte_order,
26658 m_hash_table.size () + 1);
26659 for (const hash_it_pair &hashitpair : hashitlist)
26660 {
26661 m_hash_table.push_back (0);
26662 store_unsigned_integer (reinterpret_cast<gdb_byte *>
26663 (&m_hash_table.back ()),
26664 sizeof (m_hash_table.back ()),
26665 m_dwarf5_byte_order, hashitpair.hash);
26666 const c_str_view &name = hashitpair.it->first;
26667 const std::set<symbol_value> &value_set = hashitpair.it->second;
26668 m_name_table_string_offs.push_back_reorder
26669 (m_debugstrlookup.lookup (name.c_str ()));
26670 m_name_table_entry_offs.push_back_reorder (m_entry_pool.size ());
26671 gdb_assert (!value_set.empty ());
26672 for (const symbol_value &value : value_set)
26673 {
26674 int &idx = m_indexkey_to_idx[index_key (value.dwarf_tag,
26675 value.is_static,
26676 value.kind)];
26677 if (idx == 0)
26678 {
26679 idx = m_idx_next++;
26680 m_abbrev_table.append_unsigned_leb128 (idx);
26681 m_abbrev_table.append_unsigned_leb128 (value.dwarf_tag);
26682 m_abbrev_table.append_unsigned_leb128
26683 (value.kind == unit_kind::cu ? DW_IDX_compile_unit
26684 : DW_IDX_type_unit);
26685 m_abbrev_table.append_unsigned_leb128 (DW_FORM_udata);
26686 m_abbrev_table.append_unsigned_leb128 (value.is_static
26687 ? DW_IDX_GNU_internal
26688 : DW_IDX_GNU_external);
26689 m_abbrev_table.append_unsigned_leb128 (DW_FORM_flag_present);
26690
26691 /* Terminate attributes list. */
26692 m_abbrev_table.append_unsigned_leb128 (0);
26693 m_abbrev_table.append_unsigned_leb128 (0);
26694 }
26695
26696 m_entry_pool.append_unsigned_leb128 (idx);
26697 m_entry_pool.append_unsigned_leb128 (value.cu_index);
26698 }
26699
26700 /* Terminate the list of CUs. */
26701 m_entry_pool.append_unsigned_leb128 (0);
26702 }
26703 }
26704 gdb_assert (m_hash_table.size () == name_count);
26705
26706 /* Terminate tags list. */
26707 m_abbrev_table.append_unsigned_leb128 (0);
26708 }
26709
26710 /* Return .debug_names bucket count. This must be called only after
26711 calling the build method. */
26712 uint32_t bucket_count () const
26713 {
26714 /* Verify the build method has been already called. */
26715 gdb_assert (!m_abbrev_table.empty ());
26716 const uint32_t retval = m_bucket_table.size ();
26717
26718 /* Check for overflow. */
26719 gdb_assert (retval == m_bucket_table.size ());
26720 return retval;
26721 }
26722
26723 /* Return .debug_names names count. This must be called only after
26724 calling the build method. */
26725 uint32_t name_count () const
26726 {
26727 /* Verify the build method has been already called. */
26728 gdb_assert (!m_abbrev_table.empty ());
26729 const uint32_t retval = m_hash_table.size ();
26730
26731 /* Check for overflow. */
26732 gdb_assert (retval == m_hash_table.size ());
26733 return retval;
26734 }
26735
26736 /* Return number of bytes of .debug_names abbreviation table. This
26737 must be called only after calling the build method. */
26738 uint32_t abbrev_table_bytes () const
26739 {
26740 gdb_assert (!m_abbrev_table.empty ());
26741 return m_abbrev_table.size ();
26742 }
26743
26744 /* Recurse into all "included" dependencies and store their symbols
26745 as if they appeared in this psymtab. */
26746 void recursively_write_psymbols
26747 (struct objfile *objfile,
26748 struct partial_symtab *psymtab,
26749 std::unordered_set<partial_symbol *> &psyms_seen,
26750 int cu_index)
26751 {
26752 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
26753 if (psymtab->dependencies[i]->user != NULL)
26754 recursively_write_psymbols (objfile, psymtab->dependencies[i],
26755 psyms_seen, cu_index);
26756
26757 write_psymbols (psyms_seen,
26758 &objfile->global_psymbols[psymtab->globals_offset],
26759 psymtab->n_global_syms, cu_index, false, unit_kind::cu);
26760 write_psymbols (psyms_seen,
26761 &objfile->static_psymbols[psymtab->statics_offset],
26762 psymtab->n_static_syms, cu_index, true, unit_kind::cu);
26763 }
26764
26765 /* Return number of bytes the .debug_names section will have. This
26766 must be called only after calling the build method. */
26767 size_t bytes () const
26768 {
26769 /* Verify the build method has been already called. */
26770 gdb_assert (!m_abbrev_table.empty ());
26771 size_t expected_bytes = 0;
26772 expected_bytes += m_bucket_table.size () * sizeof (m_bucket_table[0]);
26773 expected_bytes += m_hash_table.size () * sizeof (m_hash_table[0]);
26774 expected_bytes += m_name_table_string_offs.bytes ();
26775 expected_bytes += m_name_table_entry_offs.bytes ();
26776 expected_bytes += m_abbrev_table.size ();
26777 expected_bytes += m_entry_pool.size ();
26778 return expected_bytes;
26779 }
26780
26781 /* Write .debug_names to FILE_NAMES and .debug_str addition to
26782 FILE_STR. This must be called only after calling the build
26783 method. */
26784 void file_write (FILE *file_names, FILE *file_str) const
26785 {
26786 /* Verify the build method has been already called. */
26787 gdb_assert (!m_abbrev_table.empty ());
26788 ::file_write (file_names, m_bucket_table);
26789 ::file_write (file_names, m_hash_table);
26790 m_name_table_string_offs.file_write (file_names);
26791 m_name_table_entry_offs.file_write (file_names);
26792 m_abbrev_table.file_write (file_names);
26793 m_entry_pool.file_write (file_names);
26794 m_debugstrlookup.file_write (file_str);
26795 }
26796
26797 /* A helper user data for write_one_signatured_type. */
26798 class write_one_signatured_type_data
26799 {
26800 public:
26801 write_one_signatured_type_data (debug_names &nametable_,
26802 signatured_type_index_data &&info_)
26803 : nametable (nametable_), info (std::move (info_))
26804 {}
26805 debug_names &nametable;
26806 struct signatured_type_index_data info;
26807 };
26808
26809 /* A helper function to pass write_one_signatured_type to
26810 htab_traverse_noresize. */
26811 static int
26812 write_one_signatured_type (void **slot, void *d)
26813 {
26814 write_one_signatured_type_data *data = (write_one_signatured_type_data *) d;
26815 struct signatured_type_index_data *info = &data->info;
26816 struct signatured_type *entry = (struct signatured_type *) *slot;
26817
26818 data->nametable.write_one_signatured_type (entry, info);
26819
26820 return 1;
26821 }
26822
26823 private:
26824
26825 /* Storage for symbol names mapping them to their .debug_str section
26826 offsets. */
26827 class debug_str_lookup
26828 {
26829 public:
26830
26831 /* Object costructor to be called for current DWARF2_PER_OBJFILE.
26832 All .debug_str section strings are automatically stored. */
26833 debug_str_lookup (struct dwarf2_per_objfile *dwarf2_per_objfile)
26834 : m_abfd (dwarf2_per_objfile->objfile->obfd),
26835 m_dwarf2_per_objfile (dwarf2_per_objfile)
26836 {
26837 dwarf2_read_section (dwarf2_per_objfile->objfile,
26838 &dwarf2_per_objfile->str);
26839 if (dwarf2_per_objfile->str.buffer == NULL)
26840 return;
26841 for (const gdb_byte *data = dwarf2_per_objfile->str.buffer;
26842 data < (dwarf2_per_objfile->str.buffer
26843 + dwarf2_per_objfile->str.size);)
26844 {
26845 const char *const s = reinterpret_cast<const char *> (data);
26846 const auto insertpair
26847 = m_str_table.emplace (c_str_view (s),
26848 data - dwarf2_per_objfile->str.buffer);
26849 if (!insertpair.second)
26850 complaint (&symfile_complaints,
26851 _("Duplicate string \"%s\" in "
26852 ".debug_str section [in module %s]"),
26853 s, bfd_get_filename (m_abfd));
26854 data += strlen (s) + 1;
26855 }
26856 }
26857
26858 /* Return offset of symbol name S in the .debug_str section. Add
26859 such symbol to the section's end if it does not exist there
26860 yet. */
26861 size_t lookup (const char *s)
26862 {
26863 const auto it = m_str_table.find (c_str_view (s));
26864 if (it != m_str_table.end ())
26865 return it->second;
26866 const size_t offset = (m_dwarf2_per_objfile->str.size
26867 + m_str_add_buf.size ());
26868 m_str_table.emplace (c_str_view (s), offset);
26869 m_str_add_buf.append_cstr0 (s);
26870 return offset;
26871 }
26872
26873 /* Append the end of the .debug_str section to FILE. */
26874 void file_write (FILE *file) const
26875 {
26876 m_str_add_buf.file_write (file);
26877 }
26878
26879 private:
26880 std::unordered_map<c_str_view, size_t, c_str_view_hasher> m_str_table;
26881 bfd *const m_abfd;
26882 struct dwarf2_per_objfile *m_dwarf2_per_objfile;
26883
26884 /* Data to add at the end of .debug_str for new needed symbol names. */
26885 data_buf m_str_add_buf;
26886 };
26887
26888 /* Container to map used DWARF tags to their .debug_names abbreviation
26889 tags. */
26890 class index_key
26891 {
26892 public:
26893 index_key (int dwarf_tag_, bool is_static_, unit_kind kind_)
26894 : dwarf_tag (dwarf_tag_), is_static (is_static_), kind (kind_)
26895 {
26896 }
26897
26898 bool
26899 operator== (const index_key &other) const
26900 {
26901 return (dwarf_tag == other.dwarf_tag && is_static == other.is_static
26902 && kind == other.kind);
26903 }
26904
26905 const int dwarf_tag;
26906 const bool is_static;
26907 const unit_kind kind;
26908 };
26909
26910 /* Provide std::unordered_map::hasher for index_key. */
26911 class index_key_hasher
26912 {
26913 public:
26914 size_t
26915 operator () (const index_key &key) const
26916 {
26917 return (std::hash<int>() (key.dwarf_tag) << 1) | key.is_static;
26918 }
26919 };
26920
26921 /* Parameters of one symbol entry. */
26922 class symbol_value
26923 {
26924 public:
26925 const int dwarf_tag, cu_index;
26926 const bool is_static;
26927 const unit_kind kind;
26928
26929 symbol_value (int dwarf_tag_, int cu_index_, bool is_static_,
26930 unit_kind kind_)
26931 : dwarf_tag (dwarf_tag_), cu_index (cu_index_), is_static (is_static_),
26932 kind (kind_)
26933 {}
26934
26935 bool
26936 operator< (const symbol_value &other) const
26937 {
26938 #define X(n) \
26939 do \
26940 { \
26941 if (n < other.n) \
26942 return true; \
26943 if (n > other.n) \
26944 return false; \
26945 } \
26946 while (0)
26947 X (dwarf_tag);
26948 X (is_static);
26949 X (kind);
26950 X (cu_index);
26951 #undef X
26952 return false;
26953 }
26954 };
26955
26956 /* Abstract base class to unify DWARF-32 and DWARF-64 name table
26957 output. */
26958 class offset_vec
26959 {
26960 protected:
26961 const bfd_endian dwarf5_byte_order;
26962 public:
26963 explicit offset_vec (bfd_endian dwarf5_byte_order_)
26964 : dwarf5_byte_order (dwarf5_byte_order_)
26965 {}
26966
26967 /* Call std::vector::reserve for NELEM elements. */
26968 virtual void reserve (size_t nelem) = 0;
26969
26970 /* Call std::vector::push_back with store_unsigned_integer byte
26971 reordering for ELEM. */
26972 virtual void push_back_reorder (size_t elem) = 0;
26973
26974 /* Return expected output size in bytes. */
26975 virtual size_t bytes () const = 0;
26976
26977 /* Write name table to FILE. */
26978 virtual void file_write (FILE *file) const = 0;
26979 };
26980
26981 /* Template to unify DWARF-32 and DWARF-64 output. */
26982 template<typename OffsetSize>
26983 class offset_vec_tmpl : public offset_vec
26984 {
26985 public:
26986 explicit offset_vec_tmpl (bfd_endian dwarf5_byte_order_)
26987 : offset_vec (dwarf5_byte_order_)
26988 {}
26989
26990 /* Implement offset_vec::reserve. */
26991 void reserve (size_t nelem) override
26992 {
26993 m_vec.reserve (nelem);
26994 }
26995
26996 /* Implement offset_vec::push_back_reorder. */
26997 void push_back_reorder (size_t elem) override
26998 {
26999 m_vec.push_back (elem);
27000 /* Check for overflow. */
27001 gdb_assert (m_vec.back () == elem);
27002 store_unsigned_integer (reinterpret_cast<gdb_byte *> (&m_vec.back ()),
27003 sizeof (m_vec.back ()), dwarf5_byte_order, elem);
27004 }
27005
27006 /* Implement offset_vec::bytes. */
27007 size_t bytes () const override
27008 {
27009 return m_vec.size () * sizeof (m_vec[0]);
27010 }
27011
27012 /* Implement offset_vec::file_write. */
27013 void file_write (FILE *file) const override
27014 {
27015 ::file_write (file, m_vec);
27016 }
27017
27018 private:
27019 std::vector<OffsetSize> m_vec;
27020 };
27021
27022 /* Base class to unify DWARF-32 and DWARF-64 .debug_names output
27023 respecting name table width. */
27024 class dwarf
27025 {
27026 public:
27027 offset_vec &name_table_string_offs, &name_table_entry_offs;
27028
27029 dwarf (offset_vec &name_table_string_offs_,
27030 offset_vec &name_table_entry_offs_)
27031 : name_table_string_offs (name_table_string_offs_),
27032 name_table_entry_offs (name_table_entry_offs_)
27033 {
27034 }
27035 };
27036
27037 /* Template to unify DWARF-32 and DWARF-64 .debug_names output
27038 respecting name table width. */
27039 template<typename OffsetSize>
27040 class dwarf_tmpl : public dwarf
27041 {
27042 public:
27043 explicit dwarf_tmpl (bfd_endian dwarf5_byte_order_)
27044 : dwarf (m_name_table_string_offs, m_name_table_entry_offs),
27045 m_name_table_string_offs (dwarf5_byte_order_),
27046 m_name_table_entry_offs (dwarf5_byte_order_)
27047 {}
27048
27049 private:
27050 offset_vec_tmpl<OffsetSize> m_name_table_string_offs;
27051 offset_vec_tmpl<OffsetSize> m_name_table_entry_offs;
27052 };
27053
27054 /* Try to reconstruct original DWARF tag for given partial_symbol.
27055 This function is not DWARF-5 compliant but it is sufficient for
27056 GDB as a DWARF-5 index consumer. */
27057 static int psymbol_tag (const struct partial_symbol *psym)
27058 {
27059 domain_enum domain = PSYMBOL_DOMAIN (psym);
27060 enum address_class aclass = PSYMBOL_CLASS (psym);
27061
27062 switch (domain)
27063 {
27064 case VAR_DOMAIN:
27065 switch (aclass)
27066 {
27067 case LOC_BLOCK:
27068 return DW_TAG_subprogram;
27069 case LOC_TYPEDEF:
27070 return DW_TAG_typedef;
27071 case LOC_COMPUTED:
27072 case LOC_CONST_BYTES:
27073 case LOC_OPTIMIZED_OUT:
27074 case LOC_STATIC:
27075 return DW_TAG_variable;
27076 case LOC_CONST:
27077 /* Note: It's currently impossible to recognize psyms as enum values
27078 short of reading the type info. For now punt. */
27079 return DW_TAG_variable;
27080 default:
27081 /* There are other LOC_FOO values that one might want to classify
27082 as variables, but dwarf2read.c doesn't currently use them. */
27083 return DW_TAG_variable;
27084 }
27085 case STRUCT_DOMAIN:
27086 return DW_TAG_structure_type;
27087 default:
27088 return 0;
27089 }
27090 }
27091
27092 /* Call insert for all partial symbols and mark them in PSYMS_SEEN. */
27093 void write_psymbols (std::unordered_set<partial_symbol *> &psyms_seen,
27094 struct partial_symbol **psymp, int count, int cu_index,
27095 bool is_static, unit_kind kind)
27096 {
27097 for (; count-- > 0; ++psymp)
27098 {
27099 struct partial_symbol *psym = *psymp;
27100
27101 if (SYMBOL_LANGUAGE (psym) == language_ada)
27102 error (_("Ada is not currently supported by the index"));
27103
27104 /* Only add a given psymbol once. */
27105 if (psyms_seen.insert (psym).second)
27106 insert (psym, cu_index, is_static, kind);
27107 }
27108 }
27109
27110 /* A helper function that writes a single signatured_type
27111 to a debug_names. */
27112 void
27113 write_one_signatured_type (struct signatured_type *entry,
27114 struct signatured_type_index_data *info)
27115 {
27116 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
27117
27118 write_psymbols (info->psyms_seen,
27119 &info->objfile->global_psymbols[psymtab->globals_offset],
27120 psymtab->n_global_syms, info->cu_index, false,
27121 unit_kind::tu);
27122 write_psymbols (info->psyms_seen,
27123 &info->objfile->static_psymbols[psymtab->statics_offset],
27124 psymtab->n_static_syms, info->cu_index, true,
27125 unit_kind::tu);
27126
27127 info->types_list.append_uint (dwarf5_offset_size (), m_dwarf5_byte_order,
27128 to_underlying (entry->per_cu.sect_off));
27129
27130 ++info->cu_index;
27131 }
27132
27133 /* Store value of each symbol. */
27134 std::unordered_map<c_str_view, std::set<symbol_value>, c_str_view_hasher>
27135 m_name_to_value_set;
27136
27137 /* Tables of DWARF-5 .debug_names. They are in object file byte
27138 order. */
27139 std::vector<uint32_t> m_bucket_table;
27140 std::vector<uint32_t> m_hash_table;
27141
27142 const bfd_endian m_dwarf5_byte_order;
27143 dwarf_tmpl<uint32_t> m_dwarf32;
27144 dwarf_tmpl<uint64_t> m_dwarf64;
27145 dwarf &m_dwarf;
27146 offset_vec &m_name_table_string_offs, &m_name_table_entry_offs;
27147 debug_str_lookup m_debugstrlookup;
27148
27149 /* Map each used .debug_names abbreviation tag parameter to its
27150 index value. */
27151 std::unordered_map<index_key, int, index_key_hasher> m_indexkey_to_idx;
27152
27153 /* Next unused .debug_names abbreviation tag for
27154 m_indexkey_to_idx. */
27155 int m_idx_next = 1;
27156
27157 /* .debug_names abbreviation table. */
27158 data_buf m_abbrev_table;
27159
27160 /* .debug_names entry pool. */
27161 data_buf m_entry_pool;
27162 };
27163
27164 /* Return iff any of the needed offsets does not fit into 32-bit
27165 .debug_names section. */
27166
27167 static bool
27168 check_dwarf64_offsets (struct dwarf2_per_objfile *dwarf2_per_objfile)
27169 {
27170 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27171 {
27172 const dwarf2_per_cu_data &per_cu = *dwarf2_per_objfile->all_comp_units[i];
27173
27174 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27175 return true;
27176 }
27177 for (int i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
27178 {
27179 const signatured_type &sigtype = *dwarf2_per_objfile->all_type_units[i];
27180 const dwarf2_per_cu_data &per_cu = sigtype.per_cu;
27181
27182 if (to_underlying (per_cu.sect_off) >= (static_cast<uint64_t> (1) << 32))
27183 return true;
27184 }
27185 return false;
27186 }
27187
27188 /* The psyms_seen set is potentially going to be largish (~40k
27189 elements when indexing a -g3 build of GDB itself). Estimate the
27190 number of elements in order to avoid too many rehashes, which
27191 require rebuilding buckets and thus many trips to
27192 malloc/free. */
27193
27194 static size_t
27195 psyms_seen_size (struct dwarf2_per_objfile *dwarf2_per_objfile)
27196 {
27197 size_t psyms_count = 0;
27198 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27199 {
27200 struct dwarf2_per_cu_data *per_cu
27201 = dwarf2_per_objfile->all_comp_units[i];
27202 struct partial_symtab *psymtab = per_cu->v.psymtab;
27203
27204 if (psymtab != NULL && psymtab->user == NULL)
27205 recursively_count_psymbols (psymtab, psyms_count);
27206 }
27207 /* Generating an index for gdb itself shows a ratio of
27208 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
27209 return psyms_count / 4;
27210 }
27211
27212 /* Write new .gdb_index section for OBJFILE into OUT_FILE.
27213 Return how many bytes were expected to be written into OUT_FILE. */
27214
27215 static size_t
27216 write_gdbindex (struct dwarf2_per_objfile *dwarf2_per_objfile, FILE *out_file)
27217 {
27218 struct objfile *objfile = dwarf2_per_objfile->objfile;
27219 mapped_symtab symtab;
27220 data_buf cu_list;
27221
27222 /* While we're scanning CU's create a table that maps a psymtab pointer
27223 (which is what addrmap records) to its index (which is what is recorded
27224 in the index file). This will later be needed to write the address
27225 table. */
27226 psym_index_map cu_index_htab;
27227 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
27228
27229 /* The CU list is already sorted, so we don't need to do additional
27230 work here. Also, the debug_types entries do not appear in
27231 all_comp_units, but only in their own hash table. */
27232
27233 std::unordered_set<partial_symbol *> psyms_seen
27234 (psyms_seen_size (dwarf2_per_objfile));
27235 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27236 {
27237 struct dwarf2_per_cu_data *per_cu
27238 = dwarf2_per_objfile->all_comp_units[i];
27239 struct partial_symtab *psymtab = per_cu->v.psymtab;
27240
27241 /* CU of a shared file from 'dwz -m' may be unused by this main file.
27242 It may be referenced from a local scope but in such case it does not
27243 need to be present in .gdb_index. */
27244 if (psymtab == NULL)
27245 continue;
27246
27247 if (psymtab->user == NULL)
27248 recursively_write_psymbols (objfile, psymtab, &symtab,
27249 psyms_seen, i);
27250
27251 const auto insertpair = cu_index_htab.emplace (psymtab, i);
27252 gdb_assert (insertpair.second);
27253
27254 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
27255 to_underlying (per_cu->sect_off));
27256 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
27257 }
27258
27259 /* Dump the address map. */
27260 data_buf addr_vec;
27261 write_address_map (objfile, addr_vec, cu_index_htab);
27262
27263 /* Write out the .debug_type entries, if any. */
27264 data_buf types_cu_list;
27265 if (dwarf2_per_objfile->signatured_types)
27266 {
27267 signatured_type_index_data sig_data (types_cu_list,
27268 psyms_seen);
27269
27270 sig_data.objfile = objfile;
27271 sig_data.symtab = &symtab;
27272 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
27273 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27274 write_one_signatured_type, &sig_data);
27275 }
27276
27277 /* Now that we've processed all symbols we can shrink their cu_indices
27278 lists. */
27279 uniquify_cu_indices (&symtab);
27280
27281 data_buf symtab_vec, constant_pool;
27282 write_hash_table (&symtab, symtab_vec, constant_pool);
27283
27284 data_buf contents;
27285 const offset_type size_of_contents = 6 * sizeof (offset_type);
27286 offset_type total_len = size_of_contents;
27287
27288 /* The version number. */
27289 contents.append_data (MAYBE_SWAP (8));
27290
27291 /* The offset of the CU list from the start of the file. */
27292 contents.append_data (MAYBE_SWAP (total_len));
27293 total_len += cu_list.size ();
27294
27295 /* The offset of the types CU list from the start of the file. */
27296 contents.append_data (MAYBE_SWAP (total_len));
27297 total_len += types_cu_list.size ();
27298
27299 /* The offset of the address table from the start of the file. */
27300 contents.append_data (MAYBE_SWAP (total_len));
27301 total_len += addr_vec.size ();
27302
27303 /* The offset of the symbol table from the start of the file. */
27304 contents.append_data (MAYBE_SWAP (total_len));
27305 total_len += symtab_vec.size ();
27306
27307 /* The offset of the constant pool from the start of the file. */
27308 contents.append_data (MAYBE_SWAP (total_len));
27309 total_len += constant_pool.size ();
27310
27311 gdb_assert (contents.size () == size_of_contents);
27312
27313 contents.file_write (out_file);
27314 cu_list.file_write (out_file);
27315 types_cu_list.file_write (out_file);
27316 addr_vec.file_write (out_file);
27317 symtab_vec.file_write (out_file);
27318 constant_pool.file_write (out_file);
27319
27320 return total_len;
27321 }
27322
27323 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
27324 static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
27325
27326 /* Write a new .debug_names section for OBJFILE into OUT_FILE, write
27327 needed addition to .debug_str section to OUT_FILE_STR. Return how
27328 many bytes were expected to be written into OUT_FILE. */
27329
27330 static size_t
27331 write_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
27332 FILE *out_file, FILE *out_file_str)
27333 {
27334 const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (dwarf2_per_objfile);
27335 struct objfile *objfile = dwarf2_per_objfile->objfile;
27336 const enum bfd_endian dwarf5_byte_order
27337 = gdbarch_byte_order (get_objfile_arch (objfile));
27338
27339 /* The CU list is already sorted, so we don't need to do additional
27340 work here. Also, the debug_types entries do not appear in
27341 all_comp_units, but only in their own hash table. */
27342 data_buf cu_list;
27343 debug_names nametable (dwarf2_per_objfile, dwarf5_is_dwarf64,
27344 dwarf5_byte_order);
27345 std::unordered_set<partial_symbol *>
27346 psyms_seen (psyms_seen_size (dwarf2_per_objfile));
27347 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
27348 {
27349 const dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
27350 partial_symtab *psymtab = per_cu->v.psymtab;
27351
27352 /* CU of a shared file from 'dwz -m' may be unused by this main
27353 file. It may be referenced from a local scope but in such
27354 case it does not need to be present in .debug_names. */
27355 if (psymtab == NULL)
27356 continue;
27357
27358 if (psymtab->user == NULL)
27359 nametable.recursively_write_psymbols (objfile, psymtab, psyms_seen, i);
27360
27361 cu_list.append_uint (nametable.dwarf5_offset_size (), dwarf5_byte_order,
27362 to_underlying (per_cu->sect_off));
27363 }
27364
27365 /* Write out the .debug_type entries, if any. */
27366 data_buf types_cu_list;
27367 if (dwarf2_per_objfile->signatured_types)
27368 {
27369 debug_names::write_one_signatured_type_data sig_data (nametable,
27370 signatured_type_index_data (types_cu_list, psyms_seen));
27371
27372 sig_data.info.objfile = objfile;
27373 /* It is used only for gdb_index. */
27374 sig_data.info.symtab = nullptr;
27375 sig_data.info.cu_index = 0;
27376 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
27377 debug_names::write_one_signatured_type,
27378 &sig_data);
27379 }
27380
27381 nametable.build ();
27382
27383 /* No addr_vec - DWARF-5 uses .debug_aranges generated by GCC. */
27384
27385 const offset_type bytes_of_header
27386 = ((dwarf5_is_dwarf64 ? 12 : 4)
27387 + 2 + 2 + 7 * 4
27388 + sizeof (dwarf5_gdb_augmentation));
27389 size_t expected_bytes = 0;
27390 expected_bytes += bytes_of_header;
27391 expected_bytes += cu_list.size ();
27392 expected_bytes += types_cu_list.size ();
27393 expected_bytes += nametable.bytes ();
27394 data_buf header;
27395
27396 if (!dwarf5_is_dwarf64)
27397 {
27398 const uint64_t size64 = expected_bytes - 4;
27399 gdb_assert (size64 < 0xfffffff0);
27400 header.append_uint (4, dwarf5_byte_order, size64);
27401 }
27402 else
27403 {
27404 header.append_uint (4, dwarf5_byte_order, 0xffffffff);
27405 header.append_uint (8, dwarf5_byte_order, expected_bytes - 12);
27406 }
27407
27408 /* The version number. */
27409 header.append_uint (2, dwarf5_byte_order, 5);
27410
27411 /* Padding. */
27412 header.append_uint (2, dwarf5_byte_order, 0);
27413
27414 /* comp_unit_count - The number of CUs in the CU list. */
27415 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_comp_units);
27416
27417 /* local_type_unit_count - The number of TUs in the local TU
27418 list. */
27419 header.append_uint (4, dwarf5_byte_order, dwarf2_per_objfile->n_type_units);
27420
27421 /* foreign_type_unit_count - The number of TUs in the foreign TU
27422 list. */
27423 header.append_uint (4, dwarf5_byte_order, 0);
27424
27425 /* bucket_count - The number of hash buckets in the hash lookup
27426 table. */
27427 header.append_uint (4, dwarf5_byte_order, nametable.bucket_count ());
27428
27429 /* name_count - The number of unique names in the index. */
27430 header.append_uint (4, dwarf5_byte_order, nametable.name_count ());
27431
27432 /* abbrev_table_size - The size in bytes of the abbreviations
27433 table. */
27434 header.append_uint (4, dwarf5_byte_order, nametable.abbrev_table_bytes ());
27435
27436 /* augmentation_string_size - The size in bytes of the augmentation
27437 string. This value is rounded up to a multiple of 4. */
27438 static_assert (sizeof (dwarf5_gdb_augmentation) % 4 == 0, "");
27439 header.append_uint (4, dwarf5_byte_order, sizeof (dwarf5_gdb_augmentation));
27440 header.append_data (dwarf5_gdb_augmentation);
27441
27442 gdb_assert (header.size () == bytes_of_header);
27443
27444 header.file_write (out_file);
27445 cu_list.file_write (out_file);
27446 types_cu_list.file_write (out_file);
27447 nametable.file_write (out_file, out_file_str);
27448
27449 return expected_bytes;
27450 }
27451
27452 /* Assert that FILE's size is EXPECTED_SIZE. Assumes file's seek
27453 position is at the end of the file. */
27454
27455 static void
27456 assert_file_size (FILE *file, const char *filename, size_t expected_size)
27457 {
27458 const auto file_size = ftell (file);
27459 if (file_size == -1)
27460 error (_("Can't get `%s' size"), filename);
27461 gdb_assert (file_size == expected_size);
27462 }
27463
27464 /* Create an index file for OBJFILE in the directory DIR. */
27465
27466 static void
27467 write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
27468 const char *dir,
27469 dw_index_kind index_kind)
27470 {
27471 struct objfile *objfile = dwarf2_per_objfile->objfile;
27472
27473 if (dwarf2_per_objfile->using_index)
27474 error (_("Cannot use an index to create the index"));
27475
27476 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
27477 error (_("Cannot make an index when the file has multiple .debug_types sections"));
27478
27479 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
27480 return;
27481
27482 struct stat st;
27483 if (stat (objfile_name (objfile), &st) < 0)
27484 perror_with_name (objfile_name (objfile));
27485
27486 std::string filename (std::string (dir) + SLASH_STRING
27487 + lbasename (objfile_name (objfile))
27488 + (index_kind == dw_index_kind::DEBUG_NAMES
27489 ? INDEX5_SUFFIX : INDEX4_SUFFIX));
27490
27491 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb").release ();
27492 if (!out_file)
27493 error (_("Can't open `%s' for writing"), filename.c_str ());
27494
27495 /* Order matters here; we want FILE to be closed before FILENAME is
27496 unlinked, because on MS-Windows one cannot delete a file that is
27497 still open. (Don't call anything here that might throw until
27498 file_closer is created.) */
27499 gdb::unlinker unlink_file (filename.c_str ());
27500 gdb_file_up close_out_file (out_file);
27501
27502 if (index_kind == dw_index_kind::DEBUG_NAMES)
27503 {
27504 std::string filename_str (std::string (dir) + SLASH_STRING
27505 + lbasename (objfile_name (objfile))
27506 + DEBUG_STR_SUFFIX);
27507 FILE *out_file_str
27508 = gdb_fopen_cloexec (filename_str.c_str (), "wb").release ();
27509 if (!out_file_str)
27510 error (_("Can't open `%s' for writing"), filename_str.c_str ());
27511 gdb::unlinker unlink_file_str (filename_str.c_str ());
27512 gdb_file_up close_out_file_str (out_file_str);
27513
27514 const size_t total_len
27515 = write_debug_names (dwarf2_per_objfile, out_file, out_file_str);
27516 assert_file_size (out_file, filename.c_str (), total_len);
27517
27518 /* We want to keep the file .debug_str file too. */
27519 unlink_file_str.keep ();
27520 }
27521 else
27522 {
27523 const size_t total_len
27524 = write_gdbindex (dwarf2_per_objfile, out_file);
27525 assert_file_size (out_file, filename.c_str (), total_len);
27526 }
27527
27528 /* We want to keep the file. */
27529 unlink_file.keep ();
27530 }
27531
27532 /* Implementation of the `save gdb-index' command.
27533
27534 Note that the .gdb_index file format used by this command is
27535 documented in the GDB manual. Any changes here must be documented
27536 there. */
27537
27538 static void
27539 save_gdb_index_command (const char *arg, int from_tty)
27540 {
27541 struct objfile *objfile;
27542 const char dwarf5space[] = "-dwarf-5 ";
27543 dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
27544
27545 if (!arg)
27546 arg = "";
27547
27548 arg = skip_spaces (arg);
27549 if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0)
27550 {
27551 index_kind = dw_index_kind::DEBUG_NAMES;
27552 arg += strlen (dwarf5space);
27553 arg = skip_spaces (arg);
27554 }
27555
27556 if (!*arg)
27557 error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
27558
27559 ALL_OBJFILES (objfile)
27560 {
27561 struct stat st;
27562
27563 /* If the objfile does not correspond to an actual file, skip it. */
27564 if (stat (objfile_name (objfile), &st) < 0)
27565 continue;
27566
27567 struct dwarf2_per_objfile *dwarf2_per_objfile
27568 = get_dwarf2_per_objfile (objfile);
27569
27570 if (dwarf2_per_objfile != NULL)
27571 {
27572 TRY
27573 {
27574 write_psymtabs_to_index (dwarf2_per_objfile, arg, index_kind);
27575 }
27576 CATCH (except, RETURN_MASK_ERROR)
27577 {
27578 exception_fprintf (gdb_stderr, except,
27579 _("Error while writing index for `%s': "),
27580 objfile_name (objfile));
27581 }
27582 END_CATCH
27583 }
27584
27585 }
27586 }
27587
27588 \f
27589
27590 int dwarf_always_disassemble;
27591
27592 static void
27593 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
27594 struct cmd_list_element *c, const char *value)
27595 {
27596 fprintf_filtered (file,
27597 _("Whether to always disassemble "
27598 "DWARF expressions is %s.\n"),
27599 value);
27600 }
27601
27602 static void
27603 show_check_physname (struct ui_file *file, int from_tty,
27604 struct cmd_list_element *c, const char *value)
27605 {
27606 fprintf_filtered (file,
27607 _("Whether to check \"physname\" is %s.\n"),
27608 value);
27609 }
27610
27611 void
27612 _initialize_dwarf2_read (void)
27613 {
27614 struct cmd_list_element *c;
27615
27616 dwarf2_objfile_data_key = register_objfile_data ();
27617
27618 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
27619 Set DWARF specific variables.\n\
27620 Configure DWARF variables such as the cache size"),
27621 &set_dwarf_cmdlist, "maintenance set dwarf ",
27622 0/*allow-unknown*/, &maintenance_set_cmdlist);
27623
27624 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
27625 Show DWARF specific variables\n\
27626 Show DWARF variables such as the cache size"),
27627 &show_dwarf_cmdlist, "maintenance show dwarf ",
27628 0/*allow-unknown*/, &maintenance_show_cmdlist);
27629
27630 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
27631 &dwarf_max_cache_age, _("\
27632 Set the upper bound on the age of cached DWARF compilation units."), _("\
27633 Show the upper bound on the age of cached DWARF compilation units."), _("\
27634 A higher limit means that cached compilation units will be stored\n\
27635 in memory longer, and more total memory will be used. Zero disables\n\
27636 caching, which can slow down startup."),
27637 NULL,
27638 show_dwarf_max_cache_age,
27639 &set_dwarf_cmdlist,
27640 &show_dwarf_cmdlist);
27641
27642 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
27643 &dwarf_always_disassemble, _("\
27644 Set whether `info address' always disassembles DWARF expressions."), _("\
27645 Show whether `info address' always disassembles DWARF expressions."), _("\
27646 When enabled, DWARF expressions are always printed in an assembly-like\n\
27647 syntax. When disabled, expressions will be printed in a more\n\
27648 conversational style, when possible."),
27649 NULL,
27650 show_dwarf_always_disassemble,
27651 &set_dwarf_cmdlist,
27652 &show_dwarf_cmdlist);
27653
27654 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
27655 Set debugging of the DWARF reader."), _("\
27656 Show debugging of the DWARF reader."), _("\
27657 When enabled (non-zero), debugging messages are printed during DWARF\n\
27658 reading and symtab expansion. A value of 1 (one) provides basic\n\
27659 information. A value greater than 1 provides more verbose information."),
27660 NULL,
27661 NULL,
27662 &setdebuglist, &showdebuglist);
27663
27664 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
27665 Set debugging of the DWARF DIE reader."), _("\
27666 Show debugging of the DWARF DIE reader."), _("\
27667 When enabled (non-zero), DIEs are dumped after they are read in.\n\
27668 The value is the maximum depth to print."),
27669 NULL,
27670 NULL,
27671 &setdebuglist, &showdebuglist);
27672
27673 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
27674 Set debugging of the dwarf line reader."), _("\
27675 Show debugging of the dwarf line reader."), _("\
27676 When enabled (non-zero), line number entries are dumped as they are read in.\n\
27677 A value of 1 (one) provides basic information.\n\
27678 A value greater than 1 provides more verbose information."),
27679 NULL,
27680 NULL,
27681 &setdebuglist, &showdebuglist);
27682
27683 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
27684 Set cross-checking of \"physname\" code against demangler."), _("\
27685 Show cross-checking of \"physname\" code against demangler."), _("\
27686 When enabled, GDB's internal \"physname\" code is checked against\n\
27687 the demangler."),
27688 NULL, show_check_physname,
27689 &setdebuglist, &showdebuglist);
27690
27691 add_setshow_boolean_cmd ("use-deprecated-index-sections",
27692 no_class, &use_deprecated_index_sections, _("\
27693 Set whether to use deprecated gdb_index sections."), _("\
27694 Show whether to use deprecated gdb_index sections."), _("\
27695 When enabled, deprecated .gdb_index sections are used anyway.\n\
27696 Normally they are ignored either because of a missing feature or\n\
27697 performance issue.\n\
27698 Warning: This option must be enabled before gdb reads the file."),
27699 NULL,
27700 NULL,
27701 &setlist, &showlist);
27702
27703 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
27704 _("\
27705 Save a gdb-index file.\n\
27706 Usage: save gdb-index [-dwarf-5] DIRECTORY\n\
27707 \n\
27708 No options create one file with .gdb-index extension for pre-DWARF-5\n\
27709 compatible .gdb_index section. With -dwarf-5 creates two files with\n\
27710 extension .debug_names and .debug_str for DWARF-5 .debug_names section."),
27711 &save_cmdlist);
27712 set_cmd_completer (c, filename_completer);
27713
27714 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
27715 &dwarf2_locexpr_funcs);
27716 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
27717 &dwarf2_loclist_funcs);
27718
27719 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
27720 &dwarf2_block_frame_base_locexpr_funcs);
27721 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
27722 &dwarf2_block_frame_base_loclist_funcs);
27723
27724 #if GDB_SELF_TEST
27725 selftests::register_test ("dw2_expand_symtabs_matching",
27726 selftests::dw2_expand_symtabs_matching::run_test);
27727 #endif
27728 }
This page took 0.903383 seconds and 5 git commands to generate.